1
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – eyorble Aug 15 at 15:21
3
$\begingroup$

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]

Output of AroundReplace symbolically

$\endgroup$
  • $\begingroup$ thank you sir. So there is no built in function to do this right? It's knowing the math and directly implementing it? $\endgroup$ – Houndbobsaw Aug 15 at 15:35
  • $\begingroup$ 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. $\endgroup$ – Julien Kluge Aug 15 at 18:15
  • $\begingroup$ 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. $\endgroup$ – JimB Aug 16 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.