Scipy Optimize曲线拟合有效,但Scipy Optimize Minimize无效

我的回归代码遇到一种奇怪的情况。首先,让我展示一下我的模型(指数和)

$ f(t,\vec{p}) = \sum_n A_n\exp{-B_n t} $
    import functools as ft

    import numpy as np
    import scipy.linalg as scilin
    import scipy.optimize as opt

    def my_func(t,*p):
        val = 0
        for j in range(0,len(p),2):
            val += p[j]*np.exp(-p[j+1]*t)
        return val

    def my_jac(t,*p):
    jac = np.zeros(shape=(len(p),len(t)))
        for j in range(0,2):
            jac[j] = np.exp(-p[j+1]*t)
            jac[j+1] = -p[j]*t*np.exp(-p[j+1]*t)
        return jac.T

    def my_hess(t,*p):
        hess = np.zeros((len(p),2):
            hess[j+1,j] = -t*np.exp(-p[j+1]*t)
            hess[j,j+1] = -t*np.exp(-p[j+1]*t)
            hess[j+1,j+1] = p[j]*(t**2)*np.exp(-p[j+1]*t)
        return hess

以下是我要最小化的以下“成本”功能

    def chisquare(xo,xdata,ydata,covar,func):
        # covar is covariance matrix
        # residual is func(t,*xo) - ydata
        res = func(xdata,*xo) - ydata
        L = scilin.cholesky(covar,lower=True)
        xres = scilin.solve_triangular(L,res,lower=True)
        chisq = xres.dot(xres)
        return chisq

    def chi_jac(jac,xo,func):
        # I introduce a wrapper that takes in the jacobian of func,jac
        # as a parameter. The wrapped function has the same arguments as chisquare
        res = func(xdata,lower=True)
        # (J * Linv) * (Linv.H * res)
        #   lhs      *     rhs
        lhs = scilin.solve_triangular(L,jac(xdata,*xo),lower=True)
        rhs = scilin_solve_triangular(L,res)
        chiJ = lhs.T.dot(rhs)
        return 2*chiJ

    def chi_hess(jac,hess,func):
        # I introduce a wrapper that takes in the jacobian and hessian of func,jac and hess,# as a parameter. The wrapped function has the same arguments as chisquare
        res = func(xdata,lower=True)
        # (a) rhs: Solve L*H = H_l; L*res = xres
        xres = scilin.solve_triangular(L,lower=True)
        Hl = scilin.solve_triangular(L,hess(xdata,*xo).T,lower=True)
        rhs = 2*Hl.T.dot(xres)
        # (b) lhs: solve L*J = J_l
        Jl = scilin.solve_triangular(L,lower=True)
        lhs = 2*Jl.T.dot(Jl)
        return lhs + rhs

下面,我有两种曲线拟合的实现。一个使用scipy.optimize的curve_fit模块,另一个使用scipy.optimize的最小化模块。

    def minsolve(xdata,func,jac,hess):
        my_args = (xdata,func)
        wrap_jac = ft.partial(chi_jac,jac)
        wrap_hess = ft.partial(chi_hess,hess)
        res = opt.minimize(chisquare,args=my_args,jac=wrap_jac,hess=wrap_hess,method='trust-exact')
        if not res.success:
             res_message = f"Status: {res.status};\nmessage: {res.message}"
             raise Exception(res_message)
        return res.x

    # for curve_fit I just use the module directly)
    popt,pcov = opt.curve_fit(func,p0=xo,sigma=covar,method='trf',tr_solver='exact') 

使用curve_fit,我的算法终止,同时得到popt和pcov。但是,当我跑步时 我的optimize.minimize的minsolve实现,成功失败。具体来说,我收到以下消息:

警告:近似值错误导致无法预测改进。

这很奇怪,因为我对同一数据集上的两个模块使用相同的Trust-Region算法。

我要使用最小化模块的原因是因为我想修改$ \ chi ^ 2 $函数,以便进行贝叶斯先验分析。就目前而言,我一直坚持这个问题。

我的有效成本函数(雅可比人,粗麻布人)的实现中是否存在错误?

你们对我应该做什么使工作正常有什么建议?

顺便说一句,有没有办法让Latex在这里渲染?我想以LaTeX形式展示我正在使用的功能,以使事情更加清晰。

编辑:更多信息

scipy.optimize.minimize错误消息的来源来自 精确信任区域中的行搜索算法。

另一方面,scipy.optimize.curve_fit满足收敛的'gtol'标准

abcdefgxxoo 回答:Scipy Optimize曲线拟合有效,但Scipy Optimize Minimize无效

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2587949.html

大家都在问