gpt4 book ai didi

python - leastsq 的置信区间适合 scipy python

转载 作者:太空狗 更新时间:2023-10-30 01:47:12 27 4
gpt4 key购买 nike

如何在 python 中计算最小二乘拟合 (scipy.optimize.leastsq) 的置信区间?

最佳答案

我会使用自举方法。
看这里: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

关于python - leastsq 的置信区间适合 scipy python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5811043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com