# Combining fitted parameters to make a variable and telling me the error in it

I've fit my set of data to a function of the form: $$a*\cos(2\pi t)+b*\sin(2\pi t)+c*\cos(4\pi t)+d*\sin(4\pi t)+e$$ with a nonlinear model fit. The parameters $$a,b,c,d,e$$ have errors in it that Mathematica would give me, from the covariance matrix. I want to calculate the phase which is given by $$\arctan(-b/a)$$, and I want Mathematica to tell me the error in the phase. Is this possible?

• You can extract several properties from a model fit object. If you save the object to fit, for example, try calling fit["Properties"]. You may also be interested to know that your model is actually a linear model, since all of the fit parameters are simple multiplications of functions of t. This might be useful, as LinearModelFit has a wider selection of properties which can be examined. – eyorble Aug 15 '19 at 15:21

Assuming you are using mathematica 12 (or higher), this is pretty straightforward with using Around, VectorAround, AroundReplace, etc..

Mathematica, at those versions, has build-in error propagation: So you extract the covariance matrix and use AroundReplace (it even uses the correlated error propagation)

So lets start: assuming your fit is in the variable fit:

covMat = fit["CovarianceMatrix"];
bestParams = fit["BestFitParameters"];
vecErr = bestParams[[All, 1]] ->
VectorAround[bestParams[[All, 2]], 0.5*(covMat + Transpose[covMat])]
AroundReplace[ArcTan[-b/a], vecErr]


The 0.5*(covMat + Transpose[covMat]) is used, since the covariance matrix is calculated by a matrix inversion which is sometimes not stable enough to produce a absolute symmetric matrix which VectorAround will complain about. So we forcefully symmetrize it by hand.

It will even tell you, what the formula for your error is if you plug in symbols:

FullSimplify[
AroundReplace[
ArcTan[-b/a], {{a, b} ->
VectorAround[{A, B}, {{C[A], C[A, B]}, {C[A, B], C[B]}}]}],
A \[Element] Reals && B \[Element] Reals] • thank you sir. So there is no built in function to do this right? It's knowing the math and directly implementing it? – Houndbobsaw Aug 15 '19 at 15:35
• Well, I'd consider it built-in. You dont need to know the math. Just plug you covariance Matrix and estimates into AroundReplace bound to VectorAround. The Rest is figured out by Mathematica without your knowledge of error propagation. – Julien Kluge Aug 15 '19 at 18:15
• I think these are great functions (AroundReplace and VectorAround) but I would never have guessed what these functions do from the names. The documentation could certainly be expanded to also include the term "Delta Method" which is more standard for statisticians. "Propagation of error" is more familiar to engineers and physicists. Also, there are some caveats in using this method. See en.wikipedia.org/wiki/Propagation_of_uncertainty and en.wikipedia.org/wiki/Delta_method. – JimB Aug 16 '19 at 3:21