如何使用误差棒进行Scipy曲线拟合并获得拟合参数的标准误差?

时间:2016-04-05 05:03:34

标签: python numpy scipy curve-fitting errorbar

我正在努力调整我的数据点。看起来没有错误的拟合不那么乐观,因此现在我试图在每个点处拟合实现错误的数据。我的拟合函数如下:

(?i)                 # Match the remainder of the regex with the options: case insensitive (i)
(                    # Match the regular expression below and capture its match into backreference number 1
   project-             # Match the characters “project-” literally
   (?!                  # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
                           # Match either the regular expression below (attempting the next alternative only if this one fails)
         old                  # Match the characters “old” literally
      |                    # Or match regular expression number 2 below (the entire group fails if this one fails to match)
         rejected             # Match the characters “rejected” literally
   )
   \\b                   # Assert position at a word boundary
   [-0-9a-z/ ]          # Match a single character present in the list below
                           # The character “-”
                           # A character in the range between “0” and “9”
                           # A character in the range between “a” and “z”
                           # One of the characters “/ ”
      +                    # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)

然后我的数据点如下:

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

我的拟合如下:

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]

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

的拟合误差

1 个答案:

答案 0 :(得分:1)

您可以将private static void findSubsets(int array[]) { int numOfSubsets = 1 << array.length; for(int i = 0; i < numOfSubsets; i++) { int pos = array.length - 1; int bitmask = i; System.out.print("{"); while(bitmask > 0) { if((bitmask & 1) == 1) System.out.print(array[pos]+","); bitmask >>= 1; pos--; } System.out.print("}"); } } 与自定义权重结合使用:

scipy.optimize.leastsq

它可以工作,但对初始值非常敏感,因为有一个日志可以进入错误的域(负数),然后失败。在这里,我发现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) 非常接近数据。