KAHIBARO
Discord Login Register

11.1. Introduction to Fitting

Why fit experimental data?

In experimental science you rarely know the true underlying law that generated your data. Instead, you measure a set of points with uncertainties, often affected by detector effects, background processes, and limited statistics. Fitting provides a systematic way to compare a theoretical or empirical model to your measured data and to extract quantitative information from that comparison.

In ROOT, fitting connects your data objects, such as histograms and graphs, with mathematical functions represented by TF1 or related classes. When you perform a fit, ROOT adjusts the function parameters so that the function describes the data as closely as possible according to a chosen criterion. For basic fits this criterion is usually the chi square or an unbinned likelihood.

You often fit in order to extract physically meaningful quantities. A Gaussian fit to a peak in an energy spectrum can give you the mean peak position and its width. Fitting an exponential decay can give you a lifetime or attenuation length. A linear fit to a calibration curve converts measured counts into physical units. In each case, the raw points or bins alone are not yet a concise description of your measurement. The fit summarizes them in a compact set of numbers and associated uncertainties.

Fitting is also closely tied to hypothesis testing and model validation. Once you have a model curve that is supposed to describe your data, the quality of the fit tells you whether that model is compatible with the measurement, given the statistical fluctuations that you expect. This is quantified with measures like chi square and p values, which are covered later. For now, the key idea is that fitting is not only about drawing a smooth curve through noisy points, but also about making statistically meaningful statements about the parameters of that curve and about how well it represents the data.

In ROOT, you will usually fit either a histogram, which represents a binned estimate of a probability distribution, or a graph, which represents individual measurements at known coordinates. In both cases the procedure is similar. You specify a function, a range over which the fit should be performed, and optional initial guesses for the parameters. ROOT then calls a minimization algorithm that adjusts the parameters to optimize the agreement between data and model.

Fitting is meaningful only if you choose a model that has a clear interpretation and is appropriate for your data, and if you take into account the correct statistical uncertainties on your measurements.

Model parameters

A fit model in ROOT is represented by a function with parameters. Mathematically, you can think of a model as a function
$$
f(x;\,\vec{p}) = f(x; p_0, p_1, \ldots, p_{n-1}),
$$
where $x$ is the independent variable and $\vec{p}$ is the vector of parameters. During a fit, $x$ corresponds to your measurement axis, for example energy, time, position, or some kinematic variable, and the parameters $p_i$ are quantities that ROOT will adjust to best match the data.

Common examples of parameters include the mean and sigma of a Gaussian peak, the slope and intercept of a straight line, or the normalization of a background component. Parameters are what you want to learn from the data. After the fit, their best fit values and uncertainties are what you typically quote in a scientific result.

When you work with TF1 in ROOT, parameters are referred to by index, starting from zero. For example, for a Gaussian you might have
$$
f(x; p_0, p_1, p_2) = p_0 \exp\!\left( -\frac{(x - p_1)^2}{2 p_2^2} \right),
$$
where $p_0$ is the amplitude, $p_1$ is the mean, and $p_2$ is the sigma. In ROOT, these would be parameter indices 0, 1, and 2 respectively. ROOT automatically stores and updates these parameters during the fit.

During the fitting process, ROOT uses an optimization algorithm to adjust the parameters to minimize a cost function. For chi square fits to histograms this cost function has the form
$$
\chi^2(\vec{p}) = \sum_{i} \frac{\big(y_i - f(x_i;\,\vec{p})\big)^2}{\sigma_i^2},
$$
where $y_i$ are the measured values in bin or data point $i$, $\sigma_i$ are their uncertainties, and $f(x_i;\,\vec{p})$ is the model prediction at the same position. The set of parameter values $\hat{\vec{p}}$ that minimizes $\chi^2$ is taken as the best estimate for the true parameters. In other fitting modes ROOT minimizes a negative log likelihood instead, but the conceptual role of the parameters is the same.

Besides their central values, parameters also have uncertainties and correlations. The uncertainty of a parameter expresses how much it could vary due to statistical fluctuations while still being compatible with the data. The correlation between two parameters encodes how changes in one parameter can be partially compensated by changes in another while preserving a good fit. ROOT computes these uncertainties and correlations from the curvature of the cost function at the minimum. In practice this information is stored in the fit result and can be printed, inspected, or used for further analysis.

It is important to understand that parameters can be handled in different ways. Some parameters are left free, which means the minimizer can change them. Others can be fixed to a constant value, for example when you know a quantity from an external measurement and want to keep it constant in the fit. Parameters can also be constrained or limited to a certain range, for instance to avoid unphysical values like negative widths or negative event counts.

Model parameters are meaningful only in the context of the chosen function. Always link each fitted parameter to a clear physical or practical interpretation, and ensure that any fixed or constrained parameters are justified by external information or physical reasoning.

Residuals

Even for a very good fit, the model curve does not pass exactly through every measured point. The difference between data and model at each point is called a residual. Residuals provide a powerful diagnostic for understanding how well a model describes the data and where it might fail.

For a point or bin at position $x_i$ with measured value $y_i$ and model prediction $f(x_i;\,\hat{\vec{p}})$, the residual is defined as
$$
r_i = y_i - f(x_i;\,\hat{\vec{p}}).
$$
Sometimes residuals are normalized by the uncertainty $\sigma_i$, which gives the so called pull
$$
\text{pull}_i = \frac{y_i - f(x_i;\,\hat{\vec{p}})}{\sigma_i}.
$$
Pulls are particularly useful because, if the model is correct and the uncertainties are estimated correctly, they should be distributed approximately like a standard normal distribution with mean 0 and standard deviation 1.

Residual plots are an essential complement to simply looking at a fitted curve overlaid on the data. By plotting $r_i$ or $\text{pull}_i$ as a function of $x_i$, you can identify systematic patterns that are not easy to see by eye on the main plot. Random fluctuations around zero are expected, but trends such as long sequences of positive residuals followed by long sequences of negative residuals indicate that the model is missing structure in the data. Periodic patterns in the residuals hint at effects like detector nonlinearities or missing oscillatory components in the model.

Residuals also help distinguish local problems from global ones. A localized cluster of large residuals may indicate an outlier region, a bad bin, or a region where the model is not applicable. A slowly varying trend across the full range might signal an incorrect functional form, for instance using a straight line where a gentle curve is needed.

In ROOT based analyses, you often visualize residuals by creating a separate histogram or graph that stores $r_i$ or $\text{pull}_i$ for each bin or point, then drawing this object in a pad below the main plot. This kind of two panel display gives an immediate visual impression of both the overall agreement and the detailed deviations.

From a statistical point of view, residuals are closely related to the goodness of fit. The chi square introduced above can be written as a sum of squared normalized residuals,
$$
\chi^2 = \sum_i \left( \frac{r_i}{\sigma_i} \right)^2.
$$
Large residuals, especially if they appear systematically in certain regions, contribute strongly to $\chi^2$ and therefore reduce the goodness of fit. Inspecting residuals helps you understand why a particular fit has a large or small chi square and guides you in improving the model or the treatment of uncertainties.

Residuals should look statistically random around zero. Visible structure or systematic patterns in residual plots are strong indicators that your model or your uncertainty estimates are incomplete or incorrect.

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!