Search code examples
pythonscipydata-analysis

Error estimation of Fitting to 2D function


I am having a problem, where I have an numerical solver for a differential equation, and have measured many parameters of that equation, but I need to fit 3. How do I include the error of the measured arguments in the estimation of the error in the fitted paramers?

I am using scipy.optimize.leastsq to estimate the error of the fit. My code is basically this:

import scipy.optimize as opt
def solve(x,y,parameters,arguments):
    #Not important, but I do not know the functional relationship between F(x,y) and the parameters and the arguments
# data is just my data
def difference(params,arguments)
    solutions = solve(data,params,arguments)
    return solutions-data
# p0 is just an array of initial conditions
fit,cov,info,mesg,ier = opt.leastsq(difference,p0,full_output=True,args=measured_values)

This however only gives me errors in the fit parameters, but I know that I have uncertainty in my measured values as well. Is there a way to estimate the error in the fit parameters caused by those arguments?


Solution

  • Since then I realised I can estimate the error using the formula for the partial derivatives and calculating the partial derivatives of the fit myself, numerically. The formula in question is of course:

    # partial(F,x) is the partial derivative of F with respect to x
    err_F = sqrt((partial(F,a)*err_a)**2+(partial(F,b)*err_b)**2+(partial(F,c)*err_c)**2)
    

    As I am not looking for the error in my function I have to define three functions (one for each of my fitted parameters) and calculate their error from this. Hope this helps someone who might have a similar problem.