Fitting a Gaussian to a histogram when the bin size is significant I'd like to fit a Gaussian to some experimental data that is binned (the binning is a result of the physical limits of the device). Importantly, the bin size is significant enough that the gaussian cannot be considered flat in the bin window (see pic below). The data is actually 3D but let's just consider the 1D example to start . How does one write a likelihood function for the goodness-of-fit? 
My intuition is to simply consider each bin independent and compare the density verses the integrated Gaussian density in the bin window:
$$
\begin{align}
p(D|\Theta) &= \prod_i^N p(d_i|\Theta)
\\
&= \prod_i^N f\left(d_i - \int_{x_i}^{x_{i+1}}\phi(x|\mu,\sigma)dx\right)
\end{align}
$$
Where N is the number of bins, $d_i$ is the bin height for bin $i$, $\phi(x|\mu,\sigma)$ is the Gaussian PDF, and the integral is over the bin width. My question is: what should I use for $f$? In other words, how is the agreement between $d_i$ and $\phi$ distributed?
Key additional questions:


*

*How does this likelihood function change for higher dimensions?

*The integration of a Gaussian over a finite bin size is pretty expensive to compute. Since again my problem is 3D, I'm going to have to do numerical integration MANY times for millions of bins. Is there a faster way to do it?



 A: If you know that $y_i \in [x_j, x_{j+1})$, where $x_j$'s are cut points from a bin, then you can treat this as interval censored data. In other words, for your case, you can define your likelihood function as 
$\displaystyle \prod_{i = 1}^n (\Phi(r_i|\mu, \sigma) - \Phi(l_i|\mu, \sigma) )$
Where $l_i$ and $r_i$ are the upper and lower limits of the bin which the exact value lines in. 
A note is that the log likelihood is not strictly concave for many of the models for interval censored data, but in practice this is not of much consequence. 
A: You should treat each bin as if it were generating random points uniformly within its bounds. Therefore calculate a weighted average for each bin $(x_l, x_h]$ of $E(x) = \frac{x_h + x_l}{2}$ and $E(x^2) = \frac{x_h^2 + x_lx_h + x_l^2}{3}$.  This weighted average determines a Gaussian.
You can incorporate a prior by treating this Gaussian as a likelihood.
A: Given whuber's comment on my last answer, I suggest you use that answer to find a mean and variance $\mu, \sigma^2$ as a starting point.  Then, calculate the log-likelihood of having observed the bin counts you got $\ell$.  Finally, optimize the mean and variance by gradient descent.  It should be easy to calculate the gradients of the log-likelihood with respect the parameters.  This log-likelihood seems to me to be convex.
