如何使用误差线对Scipy曲线进行拟合并获得拟合参数的标准误差? [英] How to do Scipy curve fitting with error bars and obtain standard errors on fitting parameters?

查看:123
本文介绍了如何使用误差线对Scipy曲线进行拟合并获得拟合参数的标准误差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拟合我的数据点.看起来没有错误的拟合并不那么乐观,因此现在我正尝试拟合在每个点实现错误的数据.我的健身功能如下:

I am trying to fit my data points. It looks like the fitting without errors are not that optimistic, therefore now I am trying to fit the data implementing the errors at each point. My fit function is below:

def fit_func(x,a,b,c):
    return np.log10(a*x**b + c)

然后我的数据点如下:

r = [ 0.00528039,0.00721161,0.00873037,0.01108928,0.01413011,0.01790143,0.02263833, 0.02886089,0.03663713,0.04659512,0.05921978,0.07540126,0.09593949, 0.12190075,0.15501736,0.19713563,0.25041524,0.3185025,0.40514023,0.51507869, 0.65489938,0.83278859,1.05865016,1.34624082]   
logf = [-1.1020581079659384, -1.3966927245616112, -1.4571368537041418, -1.5032694247562564, -1.8534775558300272, -2.2715812166948304, -2.2627690390113862, -2.5275290780299331, -3.3798813619309365, -6.0, -2.6270989211307034, -2.6549656159564918, -2.9366845162570079, -3.0955026428779604, -3.2649261507250289, -3.2837123017838366, -3.0493752067042856, -3.3133647996463229, -3.0865051494299243, -3.1347499415910169, -3.1433062918466632, -3.1747394718538979, -3.1797597345585245, -3.1913094832146616]

因为我的数据是对数刻度,logf,所以每个数据点的误差线不是对称的.上方的误差线和下方的误差线如下:

Because my data is in log scale, logf, then the error bar for each data point is not symmetric. The upper error bar and lower error bar are below:

upper = [0.070648916083227764, 0.44346256268274886, 0.11928131794776076, 0.094260899008089094, 0.14357124858039971, 0.27236750587684311, 0.18877122991380402, 0.28707938182603066, 0.72011863806906318, 0, 0.16813325716948757, 0.13624929595316049, 0.21847915642008875, 0.25456116079315372, 0.31078368240910148, 0.23178227464741452, 0.09158189214515966, 0.14020538489677881, 0.059482730164901909, 0.051786777740678414, 0.041126467609954531, 0.034394612910981337, 0.027206248503368613, 0.021847333685597548]
lower = [0.06074797748043137, 0.21479225959441428, 0.093479845697059583, 0.077406149968278104, 0.1077175009766278, 0.16610073183912188, 0.13114254113054535, 0.17133966123838595, 0.57498950902908286, 2.9786837094190934, 0.12090437578535695, 0.10355760401838676, 0.14467588244034646, 0.15942693835964539, 0.17929440903034921, 0.15031667827534712, 0.075592499975030591, 0.10581886912443572, 0.05230849287772843, 0.04626422871423852, 0.03756658820680725, 0.03186944137872727, 0.025601929615431285, 0.02080073540367966]

我的拟合度为:

popt, pcov = optimize.curve_fit(fit_func, r, logf,sigma=[lower,upper])
logf_fit = fit_func(r,*popt)

但这是错误的,我如何实现从scipy进行的曲线拟合以包括上下误差?如何获得拟合参数a,b,c的拟合误差?

But this is wrong, how can I implement the curve fitting from scipy to include the upper and lower errors? How could I get the fitting errors of the fitting parameters a, b, c?

推荐答案

您可以将scipy.optimize.leastsq与自定义权重结合使用:

You can use scipy.optimize.leastsq with custom weights:

import scipy.optimize as optimize
import numpy as np
# redefine lists as array
x=np.array(r)
y=np.array(logf)
errup=np.array(upper)
errlow=np.array(lower)
# error function
def fit_func(x,a,b,c):
    return np.log10(a*x**b + c)
def my_error(V):
    a,b,c=V
    yfit=fit_func(x,a,b,c)
    weight=np.ones_like(yfit)
    weight[yfit>y]=errup[yfit>y] # if the fit point is above the measure, use upper weight
    weight[yfit<=y]=errlow[yfit<=y] # else use lower weight
    return (yfit-y)**2/weight**2
answer=optimize.leastsq(my_error,x0=[0.0001,-1,0.0006])
a,b,c=answer[0]
print(a,b,c)

它可以工作,但是对初始值非常敏感,因为存在一个日志,该日志可能进入错误的域(负数),然后失败.在这里,我发现a=9.14464745425e-06 b=-1.75179880756 c=0.00066720486385与数据非常接近.

It works, but is very sensitive to initial values, since there is a log which can go in wrong domain (negative numbers) and then it fails. Here I find a=9.14464745425e-06 b=-1.75179880756 c=0.00066720486385which is pretty close to data.

这篇关于如何使用误差线对Scipy曲线进行拟合并获得拟合参数的标准误差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆