Getting standard errors on fitted parameters using the optimize.leastsq method in python

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I have a set of data (displacement vs time) which I have fitted to a couple of equations using the optimize.leastsq method. I am now looking to get error values on the fitted parameters. Looking through the documentation the matrix outputted is the jacobian matrix, and I must multiply this by the residual matrix to get my values. Unfortunately I am not a statistician so I am drowning somewhat in the terminology.

From what I understand all I need is the covariance matrix that goes with my fitted parameters, so I can square root the diagonal elements to get my standard error on the fitted parameters. I have a vague memory of reading that the covariance matrix is what is output from the optimize.leastsq method anyway. Is this correct? If not how would you go about getting the residual matrix to multiply the outputted Jacobian by to get my covariance matrix?

Any help would be greatly appreciated. I am very new to python and therefore apologise if the question turns out to be a basic one.

the fitting code is as follows:

fitfunc = lambda p, t: p[0]+p[1]*np.log(t-p[2])+ p[3]*t # Target function'  errfunc = lambda p, t, y: (fitfunc(p, t) - y)# Distance to the target function  p0 = [ 1,1,1,1] # Initial guess for the parameters     out = optimize.leastsq(errfunc, p0[:], args=(t, disp,), full_output=1) 

The args t and disp is and array of time and displcement values (basically just 2 columns of data). I have imported everything needed at the tope of the code. The fitted values and the matrix provided by the output is as follows:

[  7.53847074e-07   1.84931494e-08   3.25102795e+01  -3.28882437e-11]  [[  3.29326356e-01  -7.43957919e-02   8.02246944e+07   2.64522183e-04]  [ -7.43957919e-02   1.70872763e-02  -1.76477289e+07  -6.35825520e-05]  [  8.02246944e+07  -1.76477289e+07   2.51023348e+16   5.87705672e+04]  [  2.64522183e-04  -6.35825520e-05   5.87705672e+04   2.70249488e-07]] 

I suspect the fit is a little suspect anyway at the moment. This will be confirmed when I can get the errors out.

回答1:

Updated on 4/6/2016

Getting the correct errors in the fit parameters can be subtle in most cases.

Let's think about fitting a function y=f(x) for which you have a set of data points (x_i, y_i, yerr_i), where i is an index that runs over each of your data points.

In most physical measurements, the error yerr_i is a systematic uncertainty of the measuring device or procedure, and so it can be thought of as a constant that does not depend on i.

Which fitting function to use, and how to obtain the parameter errors?

The optimize.leastsq method will return the fractional covariance matrix. Multiplying all elements of this matrix by the residual variance (i.e. the reduced chi squared) and taking the square root of the diagonal elements will give you an estimate of the standard deviation of the fit parameters. I have included the code to do that in one of the functions below.

On the other hand, if you use optimize.curvefit, the first part of the above procedure (multiplying by the reduced chi squared) is done for you behind the scenes. You then need to take the square root of the diagonal elements of the covariance matrix to get an estimate of the standard deviation of the fit parameters.

Furthermore, optimize.curvefit provides optional parameters to deal with more general cases, where the yerr_i value is different for each data point. From the documentation:

sigma : None or M-length sequence, optional     If not None, the uncertainties in the ydata array. These are used as     weights in the least-squares problem     i.e. minimising ``np.sum( ((f(xdata, *popt) - ydata) / sigma)**2 )``     If None, the uncertainties are assumed to be 1.  absolute_sigma : bool, optional     If False, `sigma` denotes relative weights of the data points.     The returned covariance matrix `pcov` is based on *estimated*     errors in the data, and is not affected by the overall     magnitude of the values in `sigma`. Only the relative     magnitudes of the `sigma` values matter. 

How c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!