在 statsmodels MLEModel class 中对已知的外生输入使用状态截距
Using the state intercept in statsmodels MLEModel class for known exogenous inputs
statsmodels statespace formulation 中的状态拦截 c_t 似乎是一种将外生控制变量插入状态空间框架的方法。但是,我无法让它工作。我能想到的最简单的示例是一个均值模型,该模型具有已知的基线偏移量,并结合了随机噪声。在 statsmodels 状态空间表示法中,它将是:
a_t = 0 * a_{t - 1} + sin(pi * t / 24) + 0 * eta_t,
y_t = 30 + 1 * a_t + e_t,
其中 t = 0, ..., 999 和 e_t ~ N(0, 4)。你可以在下面看到我是如何尝试实现这个的:
# Python 3.6.3, Statsmodels 0.9.0
import numpy as np
from statsmodels.tsa.statespace.mlemodel import MLEModel
N = 1000
t = np.arange(N)
alpha = 2 * np.sin(np.pi * t / 24)
y = 30 + alpha + 2 * np.random.randn(N)
class Simple(MLEModel):
start_params = [28, 2.2]
param_names = ['int', 'sigma2_e']
def __init__(self, endog, state_int):
super().__init__(endog, k_states = 1)
self.initialize_stationary()
self.loglikelihood_burn = 100
self['transition', 0, 0] = 0.0
self['selection', 0, 0] = 0.0
self['design', 0, 0] = 1.0
self.state_intercept = np.reshape(state_int, (1, N))
def update(self, params, **kwargs):
params = super().update(params, **kwargs)
self['obs_intercept', 0, 0] = params[0]
self['obs_cov', 0, 0] = params[1]
my_Simple = Simple(y, alpha)
mle_results = my_Simple.fit(method = 'nm', maxiter = 1000)
mle_results.summary()
如果估计考虑了偏移量,那么我希望得到大约 4 的方差估计值。但是,如果它忽略它们,则对正弦曲线的变化会更高。当你 运行 它时你会看到,它确实更高。
有什么想法吗?
您不能将属性设置符号用于状态 space 系统矩阵与 MLEModel
class,因此您的 self.state_intercept
调用失败。例如如果你这样做:
print(mle_results.filter_results.state_intercept)
然后你得到:
[[0.]]
相反,您应该这样做:
self['state_intercept'] = np.reshape(state_int, (1, N))
如您所料,这给出了大约 4 的方差估计。
statsmodels statespace formulation 中的状态拦截 c_t 似乎是一种将外生控制变量插入状态空间框架的方法。但是,我无法让它工作。我能想到的最简单的示例是一个均值模型,该模型具有已知的基线偏移量,并结合了随机噪声。在 statsmodels 状态空间表示法中,它将是:
a_t = 0 * a_{t - 1} + sin(pi * t / 24) + 0 * eta_t,
y_t = 30 + 1 * a_t + e_t,
其中 t = 0, ..., 999 和 e_t ~ N(0, 4)。你可以在下面看到我是如何尝试实现这个的:
# Python 3.6.3, Statsmodels 0.9.0
import numpy as np
from statsmodels.tsa.statespace.mlemodel import MLEModel
N = 1000
t = np.arange(N)
alpha = 2 * np.sin(np.pi * t / 24)
y = 30 + alpha + 2 * np.random.randn(N)
class Simple(MLEModel):
start_params = [28, 2.2]
param_names = ['int', 'sigma2_e']
def __init__(self, endog, state_int):
super().__init__(endog, k_states = 1)
self.initialize_stationary()
self.loglikelihood_burn = 100
self['transition', 0, 0] = 0.0
self['selection', 0, 0] = 0.0
self['design', 0, 0] = 1.0
self.state_intercept = np.reshape(state_int, (1, N))
def update(self, params, **kwargs):
params = super().update(params, **kwargs)
self['obs_intercept', 0, 0] = params[0]
self['obs_cov', 0, 0] = params[1]
my_Simple = Simple(y, alpha)
mle_results = my_Simple.fit(method = 'nm', maxiter = 1000)
mle_results.summary()
如果估计考虑了偏移量,那么我希望得到大约 4 的方差估计值。但是,如果它忽略它们,则对正弦曲线的变化会更高。当你 运行 它时你会看到,它确实更高。
有什么想法吗?
您不能将属性设置符号用于状态 space 系统矩阵与 MLEModel
class,因此您的 self.state_intercept
调用失败。例如如果你这样做:
print(mle_results.filter_results.state_intercept)
然后你得到:
[[0.]]
相反,您应该这样做:
self['state_intercept'] = np.reshape(state_int, (1, N))
如您所料,这给出了大约 4 的方差估计。