我正在尝试最小化Fsc,Qsc和Rsc的对数似然性:
def llik_scalars(Fsc,Qsc,Rsc,pred_state,pred_P,y):
T = len(pred_P)
#pred_state = np.array([pred_state[t].item() for t in range(len(pred_state))])
#pred_P = np.array([pred_P[t].item() for t in range(len(pred_P))])
Sigmat = np.array(pred_P) + Rsc
Mut = pred_state
for t in range(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
起初,我尝试将pred_state和pred_P作为矩阵列表传递。这些矩阵的大小为1x1,因此使用注释掉的代码检索了矩阵中数字的列表。 但是,由于我不确定参数是否可以这种形式传递,但我读到可以传递数组,因此在我将pred_state和pred_P作为参数传递之前,已执行注释掉的代码。因此,我将它们作为numpy数组传递。 我尝试使用scipy minimzer进行此操作
x0 = [0.5,np.var(y)/3,np.var(y) *2/3]
minimize(llik_scalars,x0,method = 'nelder-mead',args=(pred_state,y))
我收到此错误:
llik_scalars() missing 2 required positional arguments: 'pred_P' and 'y'
在另一个关于stackoverflow的主题之后,我将代码修改为以下内容,希望能够解决我的问题:
def llik_scalars(Fsc,*args):
pred_state = args[0]
pred_P = args[1]
y = args[2]
T = len(pred_P)
#pred_state = np.array([pred_state[t].item() for t in range(len(pred_state))])
#pred_P = np.array([pred_P[t].item() for t in range(len(pred_P))])
Sigmat = np.array(pred_P) + Rsc
Mut = pred_state
for t in range(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
但是,这将导致以下错误:
pred_P = args[1]
IndexError: tuple index out of range
我看不出这是怎么回事。请帮帮我:)
-编辑:- pred_state以及pred_p和y的前几个条目,我如何将它们传递给llik_scalars。注意状态的初始猜测为0,我通过将方差(pred_P)设置为一百万来使用某种先验扩散。我使用卡尔曼过滤器检索了我的pred_state和pred_P,并对我的F,Q和R进行了初步猜测:
pred_state[:5]
Out[121]: array([ 0.,0.6097107,0.29789331,0.30998801,-0.33307371])
pred_P[:5]
Out[122]:
array([1.00000000e+06,1.24999975e+00,1.13888888e+00,1.13311688e+00,1.13280061e+00])
y[:5]
Out[123]: array([ 1.21942262,0.58464737,0.90278035,-1.52760793,-0.80572172])