我想估计对数似然函数的Hessian。我估计了有马模型和garch模型的参数。然后我使用粗麻布功能找到粗麻布。但是现在我得到了错误:ValueError:用序列设置数组元素。 eps出错了。
def main():
R_bel = stationary(data_bel)
vP0 = (0.01,0.6,0.02,0.01,0.01, 0.06, 0.90)
b = minimize(garch_loglike, vP0, R_bel, bounds = ((None,None),(None,None),(None,None),(None,None),(0.0001, None), (0.0001, None), (0.0001, None)), options={'disp':True})
print(b.x)
value = garch_loglike(b.x, R_bel)
print("Log likelihood value:", -value)
hessian_ = hessian(garch_loglike)
print(hessian_(b.x, R_bel))
def garch_filter(omega, alpha, beta, eps, R):
iT = len(R)-8
sigma_2 = np.zeros(iT)
for i in range(iT):
if i==0:
sigma_2[i] = omega + alpha*eps[i-1]**2 + beta*np.var(eps)
else:
sigma_2[i] = omega + alpha*eps[i-1]**2 + beta*sigma_2[i-1]
return sigma_2
def eps1(a, b, c, d, R):
eps = np.zeros(len(R)-8)
for t in range(len(R)-8):
if t ==0:
eps[t] = R[1]-R[0]
else:
eps[t] = R[t+8]-a - b*R[t+2] - c*R[t] -d*eps[t-1]
return eps
def garch_loglike(vP, R):
iT = len(R)
a = vP[0]
b=vP[1]
c=vP[2]
d=vP[3]
omega = vP[4]
alpha = vP[5]
beta = vP[6]
eps = eps1(a,b,c,d,R)
sigma_2 = garch_filter(omega, alpha, beta, eps, R)
logL = -(-len(eps)/2 * np.log(2*math.pi) -0.5 *np.sum(np.log(sigma_2) + eps**2/sigma_2))
return logL
def stationary(data):
S=data
R = np.zeros(len(S))
for i in range(len(S)-1):
R[i+1] = np.log(S[i+1] / S[i]) * 100
R = np.delete(R, [0])
return R
如何解决此错误? 谢谢!