在Scipy的最佳曲线拟合中拟合线性分段函数的问题

问题描述

我想尝试与以下形式的对数日志空间中的分段线性函数拟合的几个数据点-

#Function to fit to. theta is the independent variable. 
def linear_function(theta,A_1,A_2,alpha_1,alpha_2,beta) :
    
    function = np.piecewise(theta,[np.log(theta)<beta],[lambda theta :  A_1 + alpha_1 * np.log(theta),lambda theta : A_2 + (alpha_1-alpha_2)*beta + alpha_2*np.log(theta)])
    return function

我想适合上面函数显示的五个参数。 theta的离散值是-

theta = array([ 0.19809681,0.58675015,1.73791655,5.14759812,15.24685771,45.16022124])

适合的点是-

#Y-values to fit to
y = array([12.10071801,6.00811736,2.39437963,1.25700828,0.72058292,0.35733525])

具有各自的不确定性

yerr = array([4.46519092,1.14850816,0.2958096,0.10562036,0.04955116,0.03622695])

当我将scipy curve_fit设置为-

popt,pcov = curve_fit(linear_function,x,np.log(y),sigma=np.log(yerr))

当我这样做时,我实际上得到了合理的配合(可能是偶然的)。但是,从协方差矩阵获得的不确定性为-

perr = np.sqrt(np.diag(pcov))

某些参数的值很高-

perr = array([5.49812884e-02,1.69241601e+07,9.97003676e-02,2.07066779e-01,8.69758288e+07)

但是,拟合至少对于直线的斜率是合理的。我在下图中显示了拟合度和数据(忽略标签/图例,与天文学有关)

Fit with data plot shown here

为什么拟合的误差如此之高?拟合是否合理只是巧合?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)