# Propagate error through FindFit

I have several sampled points $(x_i, y_i)$ with mutually independent error on the y coordinate $\sigma_{y_i}$. For simplicity, I'm assuming no error in the x coordinates ($\sigma_{x_i} \ll \sigma_{y_i}$)

I'm attempting to find a model this set of points, and propagate the error (along with any appropriate error from the fit, if applicable) to the model. Is there a simple way to do this? I'm completely unaware about how to normally propagate errors in Mathematica, so I've been doing it by hand using the standard formula for independent errors (where $f(x_1, x_2,... x_i)$):

$$\sigma_f = \sqrt{\sum_{i}{\left(\frac{\partial f}{\partial x_i} \sigma_{x_i}\right)}^2}$$

And while I could re-implement FindFit using the mathematical definition of a least squares fit, (ie, take the gradient of the residuals and solve the system,) then figure out how to throw that into the above formula, I think surely there must be a better way.

If it helps, specifically, the model I'm attempting to fit is $A \cos{\pi x / a}$ (with a constraint on $a$ to avoid a Nyquist issue, of course.) I'd like to obtain $a$, $A$, $\sigma_a$, and $\sigma_A$.

• Using NonlinearModelFit will give you lots more information than FindFit and might provide a more appropriate standard error for whatever function of the estimated parameters that is of interest. Your formula above ignores any covariance among the $x$ values (which I assume are the coefficient estimators). You might want to give more details as to what you need. – JimB Feb 10 '18 at 22:48
• I'll edit to give a bit more information and for clarity. – OmnipotentEntity Feb 10 '18 at 22:49
• @JimB, I've updated the question, please let me know if this question is still unclear. – OmnipotentEntity Feb 10 '18 at 22:54
• You say you want to "propagate the error". I understand that this is a common physics or engineering term but it doesn't tell me what it is you want. Do you want to estimate a standard error for a prediction for a particular $x$? Or find a confidence interval for a prediction? If so, then follow @eyorble 's advice. – JimB Feb 10 '18 at 23:19

Using NonlinearModelFit results in a model object from which some relevant data can be extracted. See:

fit = NonlinearModelFit[data, {A Cos[\[Pi] x/a], a > 0}, {A, a}, x, Method -> NMinimize];
fit["Properties"]
fit["MeanPredictionBands"]


For a list of properties and an example of one of them. I'd suggest investigating the properties to figure out which one best suits your needs.

To assume that the input errors are known entirely, use the guidelines from VarianceEstimatorFunction's documentation and set VarianceEstimatorFunction -> (1&), Weights -> 1/σy^2 in NonlinearModelFit, where 1/σy^2 is the inverse square of the expected error in y for each data point.

• Thanks for this answer, I appreciate your attempt to help. However, I fear I may not have been clear. I've updated the question. Please take a look. – OmnipotentEntity Feb 10 '18 at 22:54
• @OmnipotentEntity If you're looking for the potential error in the parameter estimation, you may want to look at fit["ParameterErrors"]. – eyorble Feb 10 '18 at 22:57
• Yes, but I also need to have input errors as well. The points are not $(x, y)$ but $(x, y \pm \sigma_y)$. – OmnipotentEntity Feb 10 '18 at 22:58
• @OmnipotentEntity NonlinearModelFit automatically estimates input error, but if you happen to know it it can be specified as well. I have updated my answer to include that, and it seems to propagate into the parameter errors as expected. – eyorble Feb 10 '18 at 23:18
• Oh nice! I have a feeling we're really close. Unfortunately, the variance is not uniform between the points, and doesn't have a single estimation function. Can the variance of each point be specified separately? – OmnipotentEntity Feb 10 '18 at 23:24