Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

15. Fitting counts: maximum likelihood

School of Engineering, Nazarbayev University

Track: advanced  ·  Prerequisites: Chapters 6, 7 and 14

Reading: Bevington & Robinson, ch. 10 · Cowan, Statistical Data Analysis, ch. 6–7 · James, Statistical Methods in Experimental Physics

Least squares is maximum likelihood for data with normal errors of known standard deviation (Section 7.3). Histograms and spectra break both conditions: the counts are Poisson-distributed, and the standard deviation of a count is not known in advance but depends on the very quantity being fitted. The usual workarounds, taking σi2=ni\sigma_i^2 = n_i from the data or σi2=μi\sigma_i^2 = \mu_i from the model, bias the result when the counts are small. This chapter quantifies the bias, shows how the Poisson likelihood removes it, and treats two related questions: how to test goodness of fit with few counts, and how to fit individual events without binning them at all.

15.1 Three ways to fit a histogram

Let nin_i be the count in bin ii and μi(p)\mu_i(\mathbf p) its expected value under the model. Three objective functions are in common use.

  • Neyman’s χ2\chi^2, ∑i(ni−μi)2/ni\sum_i (n_i - \mu_i)^2/n_i: the weights come from the observed counts. Empty bins must be given some arbitrary weight (here σi2=1\sigma_i^2 = 1).

  • Pearson’s χ2\chi^2, ∑i(ni−μi)2/μi\sum_i (n_i - \mu_i)^2/\mu_i: the weights come from the model and change during the fit.

  • The Poisson likelihood, L=∏iμinie−μi/ni!L = \prod_i \mu_i^{n_i} e^{-\mu_i}/n_i!, best used in the form of the likelihood-ratio statistic of Baker and Cousins (1984),

χλ2=2∑i[μi−ni+niln⁡niμi],\chi^2_\lambda = 2\sum_i \left[ \mu_i - n_i + n_i \ln\frac{n_i}{\mu_i} \right] ,

which differs from −2ln⁡L-2\ln L only by a constant, so minimising it gives the maximum-likelihood estimate. (A term with ni=0n_i = 0 contributes 2μi2\mu_i.)

The simplest case shows the problem: fitting a constant level μ\mu to kk bins. Setting the derivatives to zero gives closed forms: Neyman’s χ2\chi^2 gives the harmonic mean of the counts, Pearson’s the root-mean-square, and the likelihood the ordinary mean. Since the harmonic mean is never larger than the arithmetic mean and the root-mean-square never smaller, the first is biased low and the second high.

true level   bias of the fitted level (counts per bin)
             Neyman   Pearson   likelihood
      1       +0.18     +0.39     +0.000
      2       -0.40     +0.43     +0.003
      3       -0.78     +0.44     +0.004
      5       -1.10     +0.45     +0.003
     10       -1.07     +0.47     +0.003
     20       -1.00     +0.48     +0.006
     50       -0.96     +0.49     +0.014
    100       -0.94     +0.50     +0.028

For moderate and large counts, Neyman’s fit is too low by about one count per bin and Pearson’s too high by about half a count per bin, however large the counts are. In relative terms the bias fades as 1/μ1/\mu, but it never fades relative to the statistical uncertainty of a large histogram: with 20 bins of 100 counts, the uncertainty of the level is 100/20=2.2\sqrt{100/20} = 2.2 counts, and Neyman’s bias is almost half of that. At one count per bin Neyman’s fit turns positive, an artefact of the arbitrary weight given to empty bins. The likelihood fit is unbiased throughout.

A peak on a background. The same happens in a realistic fit. The next cell generates 2000 histograms of 40 bins, each containing a Gaussian peak of 120 events on a flat background of 3 counts per bin, and fits each with all three methods. A least-squares routine can minimise χλ2\chi^2_\lambda if each term is written as the square of a signed deviance residual, di=sign(ni−μi)2[μi−ni+niln⁡(ni/μi)]d_i = \mathrm{sign}(n_i - \mu_i)\sqrt{2[\mu_i - n_i + n_i\ln(n_i/\mu_i)]}. The uncertainties of the likelihood fit come from the inverse of the Fisher information, ∑i(∂μi/∂pk)(∂μi/∂pl)/μi\sum_i (\partial\mu_i/\partial p_k)(\partial\mu_i/\partial p_l)/\mu_i.

mean relative bias over 2000 histograms:
                    background B   peak events S       centre x0         width w
   Neyman                -30.8%           +2.3%           +0.0%           -0.0%
   Pearson               +14.7%           -0.3%           +0.0%           -1.0%
   likelihood             -0.2%           +0.6%           +0.0%           -0.5%
   (scatter of the likelihood estimates: 11%, 11%, 1%, 11% of the true values)
likelihood fit, fraction within ±1 sigma: 0.69, 0.68, 0.65, 0.67

Both χ2\chi^2 fits get the peak right but the background wrong: Neyman’s by about −30 %-30\,\% and Pearson’s by about +15 %+15\,\%, that is by -0.9 and +0.4 counts per bin. The scatter of the background estimate from one histogram is only about 0.3 counts per bin, so these are biases of three and one and a half standard uncertainties, yet nothing in a single fit reveals them: the fitted curves look equally good. The peak survives because it sits in bins with many counts; the background, spread over many bins of a few counts, takes the whole bias. Any quantity that depends on the background level, such as a peak-to-background ratio or a detection limit, inherits it. The likelihood fit is unbiased, and its Fisher-information uncertainties cover close to 68 %.

15.2 Goodness of fit with few counts

After a likelihood fit, χλ2\chi^2_\lambda at the minimum is the natural goodness-of-fit statistic: as a likelihood ratio, it follows the χ2\chi^2 distribution with k−mk - m degrees of freedom asymptotically, for many counts per bin. How quickly is that reached? For a single bin with known μ\mu, the expected contribution to Pearson’s statistic is exactly 1, but its variance is 2+1/μ2 + 1/\mu instead of 2; the contribution to χλ2\chi^2_\lambda has a mean that differs from 1. The cell computes both exactly and then checks the two statistics at the minimum of the 2000 fits above, with 36 degrees of freedom.

mu =  0.29:  chi2_lambda term mean 0.824, var 0.66;   Pearson term mean 1.000, var 5.49
mu =  1.04:  chi2_lambda term mean 1.150, var 1.42;   Pearson term mean 1.000, var 2.96
mu =  2.98:  chi2_lambda term mean 1.095, var 2.41;   Pearson term mean 1.000, var 2.34
mu =  9.62:  chi2_lambda term mean 1.020, var 2.09;   Pearson term mean 1.000, var 2.10
Pearson at ML  at the minimum: mean 35.9 (dof 36), rejected at the nominal 5 % level in 4.7%
chi2_lambda    at the minimum: mean 39.0 (dof 36), rejected at the nominal 5 % level in 11.1%

Around one to ten counts per bin, each bin adds on average up to about 15 % more than 1 to χλ2\chi^2_\lambda, so the statistic is shifted upwards and a test at the nominal 5 % level rejects correct models about twice as often as it should. Pearson’s statistic, evaluated at the likelihood estimates, has the right mean and is much closer to its nominal rate here; its extra variance only matters for bins with well below one count. Two practical conclusions follow. Fit by maximum likelihood, but for the goodness of fit of a sparse histogram, either use Pearson’s statistic at the likelihood estimates or, better, calibrate the statistic by simulation: generate many histograms from the fitted model, fit each, and read the p-value from the simulated distribution (a parametric bootstrap; compare the ordinary bootstrap of Section 9.3, which resamples the data instead). Merging bins until each holds at least five counts is the traditional alternative; it works but throws away shape information.

15.3 Unbinned maximum likelihood

When the individual events are recorded, there is no need to bin them. The likelihood is the product of the probability densities of the events, and it handles conditions that are awkward in a histogram fit, such as a finite observation window. Consider decay times of an unstable state with lifetime τ\tau, observed only between tmin=0.5t_\text{min} = 0.5 (shorter times are lost in the dead time of the electronics) and tmax=4t_\text{max} = 4 (the end of the recording). The density of an observed time is the exponential renormalised to the window,

p(t;τ)=e−t/τ/τe−tmin/τ−e−tmax/τ,tmin<t<tmax.p(t;\tau) = \frac{e^{-t/\tau}/\tau}{e^{-t_\text{min}/\tau} - e^{-t_\text{max}/\tau}} , \qquad t_\text{min} < t < t_\text{max} .

The cell simulates 1000 experiments of 200 recorded decays with τ=2\tau = 2, and compares three estimates: the mean recorded time, the mean minus tmint_\text{min} (which would be exact without the upper limit, since the exponential has no memory), and the unbinned maximum-likelihood estimate, with its Δln⁡L=12\Delta\ln L = \tfrac12 interval (Section 7.4).

mean recorded time  : average 1.767 (true 2.0), scatter 0.066
mean minus t_min    : average 1.267 (true 2.0), scatter 0.066
unbinned ML         : average 2.053 (true 2.0), scatter 0.320
first experiment: tau = 2.20 -0.31 +0.43
Delta lnL = 1/2 interval covers 0.678;  true value below 0.160, above 0.162

Both simple estimates are badly biased, because the window removes the long decay times that carry most of the information about a lifetime comparable to the window. The likelihood estimate is biased by less than a fifth of its scatter, and its interval covers at close to the nominal rate. The price is visible in the scatter: the window really does destroy information, and the honest estimate is about five times less precise than the biased ones appear to be. The asymmetric interval of the example reflects the fact that a long lifetime is hard to exclude when the window is short.

Binning and fitting the histogram by the Poisson likelihood gives almost the same answer when the bins are narrow compared with the scale over which the density changes; the unbinned fit is the limit of infinitely narrow bins. With many thousands of events, binning saves computing time; with a few hundred, there is no reason to bin.

<Figure size 1012x814 with 4 Axes>

(a) Bias of a constant level fitted to 20 bins, against the true level. (b) One of the simulated histograms with the three fits; the χ2\chi^2 fits differ from the likelihood fit mainly in the background. (c) Mean and variance (halved) of one bin’s contribution to the two goodness-of-fit statistics; for a χ2\chi^2 variable both would equal 1 (dotted). (d) Lifetime estimates from 1000 experiments with a finite observation window; dotted: the true lifetime.

Summary

  • For histograms, Neyman’s χ2\chi^2 (weights from the data) biases a fitted level by about -1 count per bin, Pearson’s (weights from the model) by about +12+\tfrac12. The bias does not shrink relative to the statistical uncertainty as the counts grow.

  • Fit counts by the Poisson likelihood, conveniently as χλ2\chi^2_\lambda with deviance residuals; uncertainties from the Fisher information or from Δχλ2=1\Delta\chi^2_\lambda = 1.

  • With a few counts per bin, χλ2\chi^2_\lambda at the minimum overstates the misfit; use Pearson’s statistic at the likelihood estimates, or calibrate by simulation.

  • With individual events, use the unbinned likelihood. It handles windows, efficiencies and resolution naturally, where simple averages are biased.

References

  • S. Baker and R. D. Cousins, “Clarification of the use of chi-square and likelihood functions in fits to histograms”, Nucl. Instrum. Methods 221, 437 (1984).

  • W. Cash, “Parameter estimation in astronomy through application of the likelihood ratio”, Astrophys. J. 228, 939 (1979).

  • G. Cowan, Statistical Data Analysis (Oxford University Press, 1998).

  • F. James, Statistical Methods in Experimental Physics, 2nd ed. (World Scientific, 2006).

Exercises

  1. Show that minimising Neyman’s χ2\chi^2 for a constant level gives the harmonic mean of the counts, and Pearson’s the root-mean-square. Expand both for large μ\mu to obtain the biases of -1 and +12+\tfrac12.

  2. Show that χλ2\chi^2_\lambda and −2ln⁡L-2\ln L differ by a term that does not depend on the parameters.

  3. Repeat the peak fit with a background of 30 counts per bin. How do the relative biases of the background change?

  4. Implement the parametric bootstrap for the goodness of fit of Section 15.2 and compare its p-value with the nominal one for a single histogram.

  5. In the lifetime example, bin the decay times into 10, 30 and 100 bins and fit by the Poisson likelihood. How do estimate and uncertainty compare with the unbinned fit?