Python 将附加参数传递给numdifftools Hessian

Python 将附加参数传递给numdifftools Hessian,python,kalman-filter,hessian,log-likelihood,Python,Kalman Filter,Hessian,Log Likelihood,我想获得以下函数的Hessian值: def llik_scalars(param_vector, *args): Fsc = param_vector[0] Qsc = param_vector[1] Rsc = param_vector[2] y = args[0] burnin = args[1] F = np.matrix(Fsc) Q = np.matrix(Qsc) R = np.matrix(Rsc) p

我想获得以下函数的Hessian值:

def llik_scalars(param_vector, *args):
    Fsc = param_vector[0]
    Qsc = param_vector[1]
    Rsc = param_vector[2]

    y = args[0]
    burnin = args[1]

    F = np.matrix(Fsc)
    Q = np.matrix(Qsc)
    R = np.matrix(Rsc)

    predstate, predp, _, _ = kalmanfilter(F=F, Q=Q, R=R, y=y, plot = False)
    T = len(predp)
    predstate = np.array([predstate[t].item() for t in range(len(predstate))])
    predp = np.array([predp[t].item() for t in range(len(predp))])

    Sigmat = predp + Rsc
    Mut = predstate

    LL = 0
    for t in range(burnin, T):
        exponent = -0.5 * (y[t]-Mut[t])**2 / Sigmat[t]
        cc = 1 / math.sqrt(2*math.pi*Sigmat[t])
        LL -= math.log(cc*math.exp(exponent))
    return LL
class Hessian(f, step=None, method=’central’, order=2, full_output=False, **step_options)
Parameters
fun [function] function of one array fun(x, *args, **kwds)
我正试图用numdifftools包的Hessian函数来实现这一点。 在文档中,我发现了以下信息。例如,如果需要rosenbrock函数的hessian(定义为Rosen),则hessian的计算方法如下:

> H = nd.Hessian(rosen)([1, 1])
hess = nd.Hessian(kf.llik_scalars(themin.x, (y,burnin)))(themin.x)
其中Hessian在点[1,1]中计算

根据文档,应该可以为Hessian函数提供参数:

def llik_scalars(param_vector, *args):
    Fsc = param_vector[0]
    Qsc = param_vector[1]
    Rsc = param_vector[2]

    y = args[0]
    burnin = args[1]

    F = np.matrix(Fsc)
    Q = np.matrix(Qsc)
    R = np.matrix(Rsc)

    predstate, predp, _, _ = kalmanfilter(F=F, Q=Q, R=R, y=y, plot = False)
    T = len(predp)
    predstate = np.array([predstate[t].item() for t in range(len(predstate))])
    predp = np.array([predp[t].item() for t in range(len(predp))])

    Sigmat = predp + Rsc
    Mut = predstate

    LL = 0
    for t in range(burnin, T):
        exponent = -0.5 * (y[t]-Mut[t])**2 / Sigmat[t]
        cc = 1 / math.sqrt(2*math.pi*Sigmat[t])
        LL -= math.log(cc*math.exp(exponent))
    return LL
class Hessian(f, step=None, method=’central’, order=2, full_output=False, **step_options)
Parameters
fun [function] function of one array fun(x, *args, **kwds)
我通过以下方式尝试了这一点:

> H = nd.Hessian(rosen)([1, 1])
hess = nd.Hessian(kf.llik_scalars(themin.x, (y,burnin)))(themin.x)
themin.x是我想要计算Hessian函数的点

themin.x
Out[49]: array([0.67605231, 0.7457089 , 0.72205726])
运行上述代码时出现的错误:

burnin = args[1]

IndexError: tuple index out of range

我不明白元组是如何超出范围的

在没有完整的函数和参数的情况下调试调用有点困难,这就是为什么我刚才用rosen函数做了一个简单的示例

我添加了两个伪造的附加参数,并将新函数命名为
rosen\u ext

将numdifftools作为nd导入 将numpy作为np导入

def rosen(x):
    return (1-x[0])**2 + 105.*(x[1]-x[0]**2)**2

def rosen_ext(x, *args):
    y = args[0]
    burnin = args[1]

    print(y)
    print(burnin)

    return (1-x[0])**2 + 105.*(x[1]-x[0]**2)**2
我可以以您的方式为
rosen_ext
调用
Hessian
时重现您的错误

x=[2,2]中的
rosen
Hessian
应该是

[[4202. -840.]
 [-840.  210.]]
要获得rosen_ext的此结果,您需要这样调用它:

# call of the Hessian for the modified rosen in some point X = [2, 2]
H = nd.Hessian(rosen_ext)([2, 2], 2, 3)
print(H)
hess = nd.Hessian(kf.llik_scalars)(themin.x, y, burnin)
它返回一组2和3(有助于查看附加参数的放置方式是否正确)和正确答案

2
3
...
2
3
2
3
2
3
[[4202. -840.]
 [-840.  210.]]
使用这种方法,我假设您的呼叫应该是这样的:

# call of the Hessian for the modified rosen in some point X = [2, 2]
H = nd.Hessian(rosen_ext)([2, 2], 2, 3)
print(H)
hess = nd.Hessian(kf.llik_scalars)(themin.x, y, burnin)
我不能调试它,所以你能检查一下它是否工作吗