从scipy.optimize.curve_fit获取与参数估计值相关的标准误差 [英] Getting standard error associated with parameter estimates from scipy.optimize.curve_fit
问题描述
我正在使用 scipy.optimize.curve_fit
将曲线拟合到我拥有的某些数据.在大多数情况下,这些曲线看起来非常吻合.出于某些原因,当我打印出来时,pcov = inf.
我真正需要的是计算与我适合的参数相关的误差,并且即使它确实给了我协方差矩阵,也不确定如何精确地做到这一点.
适合的模型是:
def强度(x,R_out,R_in,K_in,K_out,a,b,c):K_in,K_out = abs(0.0),abs(K_out)如果x< = R_in:返回2 * R_out *(K_out * np.sqrt(1-x ** 2/R_out ** 2)-(K_out-0.0)* np.sqrt(R_in ** 2/R_out ** 2-x ** 2/R_out ** 2))+ celif x> = R_in和x< = R_out:返回K_out * 2 * R_out * np.sqrt(1-x ** 2/R_out ** 2)+ celif x> R_out:返回cstrength_vec = np.vectorize(强度)def strength_vec_self(x,R_out,R_in,K_in,K_out,a,b,c):y = np.zeros(x.shape)对于范围内的我(len(y)):y [i] = intensity_vec(x [i],R_out,R_in,K_in,K_out,a,b,c)返回y
有400个数据点,如果您认为有帮助的话,可以放在这里.
总而言之,我无法获得 curve_fit
来打印我的 pcov
,并且需要帮助才能弄清楚为什么以及是否可以这样做./p>
此外,如果这是一个简短的说明,我想知道如何使用 pcov
数组来获得与我的健康相关的错误.
谢谢
参数的方差是方差-co方差矩阵的对角元素,标准误差是其平方根. np.sqrt(np.diag(pcov))
关于获取 inf
,请查看并比较以下两个示例:
在[129]中:将numpy导入为npdef func(x,a,b,c,d):返回一个* np.exp(-b * x)+ cxdata = np.linspace(0,4,50)y = func(xdata,2.5,1.3,0.5,1)ydata = y + 0.2 * np.random.normal(size = len(xdata))popt,pcov = so.curve_fit(func,xdata,ydata)打印np.sqrt(np.diag(pcov))[inf inf inf inf]
并且:
在[130]中:def func(x,a,b,c):返回一个* np.exp(-b * x)+ cxdata = np.linspace(0,4,50)y = func(xdata,2.5,1.3,0.5)ydata = y + 0.2 * np.random.normal(size = len(xdata))popt,pcov = so.curve_fit(func,xdata,ydata)打印np.sqrt(np.diag(pcov))[0.11097646 0.11849107 0.05230711]
在此极端示例中, d
对函数 func
不起作用,因此它将与 + inf
的方差相关联,或者换句话说,它几乎可以是任何值.从 func
中删除 d
将会很有意义.
实际上,如果参数的比例非常不同,请说:
def func(x,a,b,c,d):#返回a * np.exp(-b * x)+ c返回a * np.exp(-b * x)+ c + d * 1e-10
由于浮点上溢/下溢,您还将获得 inf
.
就您而言,我认为您从未使用过 a
和 b
.因此,就像这里的第一个示例一样.
I am using scipy.optimize.curve_fit
to fit a curve to some data i have. The curves, for the most part, seem to fit very well. For some reason, pcov = inf when i print it off.
What i really need is to calculate the error associated with the parameters i'm fitting, and am not sure how exactly to do this even if it does give me the covariance matrix.
The model being fit to is:
def intensity(x,R_out,R_in,K_in,K_out,a,b,c):
K_in,K_out = abs(0.0),abs(K_out)
if x<=R_in:
return 2*R_out*(K_out*np.sqrt(1-x**2/R_out**2)-
(K_out-0.0)*np.sqrt(R_in**2/R_out**2-x**2/R_out**2)) + c
elif x>=R_in and x<=R_out:
return K_out*2*R_out*np.sqrt(1-x**2/R_out**2) + c
elif x>R_out:
return c
intensity_vec = np.vectorize(intensity)
def intensity_vec_self(x,R_out,R_in,K_in,K_out,a,b,c):
y = np.zeros(x.shape)
for i in range(len(y)):
y[i]=intensity_vec(x[i],R_out,R_in,K_in,K_out,a,b,c)
return y
and there are 400 data points, i can put that on here if you think it will help.
To summarize, i can't get curve_fit
to print off my pcov
and need help as to figure out why and if i can get it to do so.
Also, if it is a quick explanation i would like to know how to use the pcov
array to attain the errors associated with my fit.
Thanks
The variance of parameters are the diagonal elements of the variance-co variance matrix, and the standard error is the square root of it. np.sqrt(np.diag(pcov))
Regarding getting inf
, see and compare these two examples:
In [129]:
import numpy as np
def func(x, a, b, c, d):
return a * np.exp(-b * x) + c
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5, 1)
ydata = y + 0.2 * np.random.normal(size=len(xdata))
popt, pcov = so.curve_fit(func, xdata, ydata)
print np.sqrt(np.diag(pcov))
[ inf inf inf inf]
And:
In [130]:
def func(x, a, b, c):
return a * np.exp(-b * x) + c
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
ydata = y + 0.2 * np.random.normal(size=len(xdata))
popt, pcov = so.curve_fit(func, xdata, ydata)
print np.sqrt(np.diag(pcov))
[ 0.11097646 0.11849107 0.05230711]
In this extreme example, d
has no effect on the function func
, hence it will be associated with variance of +inf
, or in another word, it can be just about any value. Removing d
from func
will get what will make sense.
In reality, if parameters are of very different scale, say:
def func(x, a, b, c, d):
#return a * np.exp(-b * x) + c
return a * np.exp(-b * x) + c + d*1e-10
You will also get inf
due to float point overflow/underflow.
In your case, I think you never used a
and b
. So it is just like the first example here.
这篇关于从scipy.optimize.curve_fit获取与参数估计值相关的标准误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!