LMFIT 无法正确拟合 scipy 具有相同起始参数值的情况

发布于 2025-01-13 14:49:18 字数 1741 浏览 0 评论 0原文

我有一个复杂的曲线拟合函数:

def corr_function(tau: np.ndarray, BG: float, avg_C: float, vz: float):
    wxc = 8.3
    wy = 2.5
    wz = 3.35
    D = 4.4e1 
    return 1/((math.pi)**(3/2)*wxc*wy*wz*avg_C)*(1 + 4*D*tau/(wxc**2))**(-1/2)*(1 + 4*D*tau/(wy**2))**(-1/2)*(1 + 4*D*tau/(wz**2))**(-1/2)*np.exp(-((vz*tau)**2/(wz**2 + 4*D*tau))) + BG

来拟合它

popt, pcov = curve_fit(corr_function, tau, corr, [0, 1e-12, 2e5])

我尝试用 scipy:和 lmfit

model = Model(corr_function, independent_vars=['tau'])
result = model.fit(
   corr,
   tau=tau,
   BG=Parameter('BG', value=0, min=0),
   avg_C=Parameter('avg_C', value=1e-12, min=0),
   vz=Parameter('vz', value=2e5, min=0),
)

,虽然 scipy 收敛到正确的答案(蓝色),但 lmfit 没有(橙色),其中 lmfit 参数根本没有改变在拟合过程中,

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 61
    # data points      = 400
    # variables        = 3
    chi-square         = 1.5370e+12
    reduced chi-square = 3.8714e+09
    Akaike info crit   = 8833.74620
    Bayesian info crit = 8845.72059
##  Warning: uncertainties could not be estimated:
    BG_guess:     at boundary
    avg_C_guess:  at initial value
    avg_C_guess:  at boundary
[[Variables]]
    BG:     0.00000000 (init = 0)
    avg_C:  3.9999e-12 (init = 4e-12)
    vz:     8831416.63 (init = 200000)

我认为我需要 lmfit 来采样更大的参数空间(或更多迭代),有人知道如何做到这一点吗?

图表比较

另外,请注意,我需要输入参数是静态的(不能使它们更接近正确的拟合),因为我需要自动拟合大型参数空间

I have a complicated curve fitting function:

def corr_function(tau: np.ndarray, BG: float, avg_C: float, vz: float):
    wxc = 8.3
    wy = 2.5
    wz = 3.35
    D = 4.4e1 
    return 1/((math.pi)**(3/2)*wxc*wy*wz*avg_C)*(1 + 4*D*tau/(wxc**2))**(-1/2)*(1 + 4*D*tau/(wy**2))**(-1/2)*(1 + 4*D*tau/(wz**2))**(-1/2)*np.exp(-((vz*tau)**2/(wz**2 + 4*D*tau))) + BG

I tried to fit this with scipy:

popt, pcov = curve_fit(corr_function, tau, corr, [0, 1e-12, 2e5])

and lmfit

model = Model(corr_function, independent_vars=['tau'])
result = model.fit(
   corr,
   tau=tau,
   BG=Parameter('BG', value=0, min=0),
   avg_C=Parameter('avg_C', value=1e-12, min=0),
   vz=Parameter('vz', value=2e5, min=0),
)

And while the scipy converges to a proper answer (blue), the lmfit doesn't (orange), where lmfit parameters don't change really at all during fitting

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 61
    # data points      = 400
    # variables        = 3
    chi-square         = 1.5370e+12
    reduced chi-square = 3.8714e+09
    Akaike info crit   = 8833.74620
    Bayesian info crit = 8845.72059
##  Warning: uncertainties could not be estimated:
    BG_guess:     at boundary
    avg_C_guess:  at initial value
    avg_C_guess:  at boundary
[[Variables]]
    BG:     0.00000000 (init = 0)
    avg_C:  3.9999e-12 (init = 4e-12)
    vz:     8831416.63 (init = 200000)

I think I need lmfit to sample a larger parameter space (or more iterations), anyone know how to do this?

graph comparison

Also, note, I need to the input parameters to be static (can't bring them closer to proper fit), as I'll need to automate fitting for large parameter spaces

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文