将其他参数传递给numdifftools Hessian

问题描述 投票:0回答:1

我想获得以下功能的黑森州:

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

我正在尝试使用numdifftools软件包的Hessian函数来完成此操作。在文档中,我找到了以下信息。例如,如果您想要定义为Rosen的rosenbrock函数的粗麻布,则粗麻布的计算方法如下:

> H = nd.Hessian(rosen)([1, 1])

[在点[1,1]处计算黑森州的位置

在文档之后,应该可以为Hessian函数提供参数:

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)

我以以下方式尝试过:

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

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

python kalman-filter hessian log-likelihood
1个回答
0
投票

没有完整的函数和参数很难调试您的呼叫,这就是为什么我只是使用rosen函数制作了一个简单的示例。

© www.soinside.com 2019 - 2024. All rights reserved.