本文介绍了scipy python中具有leastsq拟合的置信区间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算python中最小二乘拟合(scipy.optimize.leastsq)的置信区间?
How to calculate confidence interval for the least square fit (scipy.optimize.leastsq) in python?
推荐答案
我会使用引导方法.
请参阅此处:http://phe.rockefeller.edu/LogletLab/whitepaper/node17.html
I would use bootstrapping method.
See here: http://phe.rockefeller.edu/LogletLab/whitepaper/node17.html
噪声高斯的简单示例:
x = arange(-10, 10, 0.01)
# model function
def f(p):
mu, s = p
return exp(-(x-mu)**2/(2*s**2))
# create error function for dataset
def fff(d):
def ff(p):
return d-f(p)
return ff
# create noisy dataset from model
def noisy_data(p):
return f(p)+normal(0,0.1,len(x))
# fit dataset to model with least squares
def fit(d):
ff = fff(d)
p = leastsq(ff,[0,1])[0]
return p
# bootstrap estimation
def bootstrap(d):
p0 = fit(d)
residuals = f(p0)-d
s_residuals = std(residuals)
ps = []
for i in range(1000):
new_d = d+normal(0,s_residuals,len(d))
ps.append(fit(new_d))
ps = array(ps)
mean_params = mean(ps,0)
std_params = std(ps,0)
return mean_params, std_params
data = noisy_data([0.5, 2.1])
mean_params, std_params = bootstrap(data)
print "95% confidence interval:"
print "mu: ", mean_params[0], " +/- ", std_params[0]*1.95996
print "sigma: ", mean_params[1], " +/- ", std_params[1]*1.95996
这篇关于scipy python中具有leastsq拟合的置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!