# When fitting a Gaussian-like function, how does the amount of baseline datapoints affect the fit?

I am fitting a curve to some instrument data. The data is a pulse with a particular functional form, which starts from and returns to a constant (with noise) baseline level before and after the pulse.

I find that if I fit the whole dataset, I get one set of fitted parameters for the pulse, but if I crop out the part of the data containing the pulse and fit only that, I get a different set of fitted parameters. In both cases the fit looks good and the parameters are reasonable, but not equivalent at all.

Does this represent a problem in my fit function or methodology in particular? Or is this something which is generally possible and expected?

If the problem isn't with my fit function in particular, then how might I improve my fitting method to make the results correct, and invariant w.r.t. the amount of baseline samples present in the data set?

I am using lsqcurvefit() in Matlab, with an error function that provides an analytical Jacobian, so the optimizer being used under the hood is a Trust-Region method. I'm not completely familiar with this algorithm, but as far as I know it is in the same category as BFGS or other quasi-Newton methods, with some additional magic to improve robustness and efficiency. I get similar results with other solvers (eg. Conjugate Gradient).

If very different parameter sets give quite similar curves, you are most likely fitting too many parameters, which means that some of the fitted parameters will be sensitive to changes in the data.

Look at the inverse Hessian of the objective function of the two fits to see how well the estimated parameters are determined by the data. Large diagonal entires imply poor conditioning, and hence unreliability of the parameters (even though the fit is good).

This is harmless if you only want to use the smoothed curve to replace your data set. But it is disastrous if you want to treat the parameters as representing substantial information. In this case you need to add a regularization term reflecting qualitative knowledge about your parameters to improve the quaity of your parameter estimates.

See my paper ''Solving ill-conditioned and singular linear systems: A tutorial on regularization'' http://www.mat.univie.ac.at/~neum/papers.html#regtutorial for regularization in fitting by linear least squares, but qualitatively everything said there also applies to nonlinear objectives.

If you fit only data from a window around the peak, then what you're implicitly saying is that data points outside the window don't matter. It would thus not be a surprise if the curve doesn't describe points outside the window well.

On the other hand, if you don't use a window, you say that every point matters equally much, and so the curve has to fit all points. In particular, this means that if you have a lot of points, far more than are in your window, then you might end up with a curve that fits the vast majority of points on the fringes, but provides little fit for the points you really care about, i.e., the ones around the peak.

Fundamentally, what you want to do is more of a philosophical question than a mathematical one: which data points do you want the curve to describe.

That said, from what you describe the curve you get actually does fit all data points, in either case. That means at the least that the model you try to fit to the data points is an accurate representation of the true process that produced these data points. The question then becomes: if you get two different curves from your two approaches, which one is better? That, again, is a question of interpretation: what is it that you care about? Obviously, for the points inside the window, the curve created with the window is the best fit. But, how good is the fit of the curve generated from the window with regards to the points outside the window? And how good is the globally fitted curve for the points inside the window? The standard tool to consider for these questions is to look at the standard deviation; i.e., if the data points are $(t_i,x_i)$ and you fit a curve $f(t)$ to them, the standard deviation would be $\sqrt{\frac 1N \sum_i (x_i-f(t_i))^2}$. It may be that with this measure, both curves are approximately the same, in which case they'd be equally good approximations. Or they're not, on a given subset of points (e.g. all, or only the ones inside the window), in which case you'd know which curve is "better" for your purpose.

You'll find a lot of methods in this regard in books on parameter estimation and statistical fitting. Among the more important ones are those that fit using only a subset of points and then compare by computing the residual for the remainder of the points. If you repeat this procedure with several, different subsets, you'll get a feeling for how sensitive the fitted curve is to the choice of measurement points.