我想用一个简单的估计一个简单的本地级模型 解释变量及其对应的时变系数 以下状态空间形式:
y(t)= mu(t)+ beta(t)* x(t)+ u(t) mu(t + 1)= mu(t)+ v(t + 1) beta(t + 1)= beta(t)+ w(t + 1)
我打算建立一个自定义状态空间模型,但我不确定我是怎么做的 应该分配x(t),解释变量时间序列(pp2中的 例如,nobs x 1)到设计矩阵的一个元素。我把它放在一起 代码为Commandeur和Koopman(2007)第5.1节来说明我的 问题:
import numpy as np
import pandas as pd
from scipy.stats import norm
import statsmodels.api as sm
import matplotlib.pyplot as plt
xls = pd.ExcelFile('/Users/me/SSData/UK KSI.xlsx')
df1 = pd.read_excel(xls, sheet_name='Sheet4')
df2 = pd.read_excel(xls, sheet_name='Sheet3')
lKSI_all = df1['ltotal']
pprice = df2['pprice']
pp2 = np.array(pprice)
class LocalExoDet(sm.tsa.statespace.MLEModel):
def __init__(self, endog, exog):
super(LocalExoDet, self).__init__(endog, k_states=2, exog=exog)
self['design',0,0] = 1.0
self['design',0,1] = self.exog[:]
self['transition'] = np.array([[1,0],[0,1]])
self.initialize_approximate_diffuse()
self.loglikelihood_burn = 1
def param_names(self):
return ['error_var.obs']
def start_params(self):
return [np.std(self.endog)]
def transform_params(self, unconstrained):
return unconstrained**2
def untransform_params(self, constrained):
return constrained**0.5
def update(self, params, *args, **kwargs):
params = super(LocalExoDet, self).update(params, *args, **kwargs)
self['obs_cov', 0, 0] = params[0]