【发布时间】:2021-04-05 09:22:30
【问题描述】:
我正在尝试使用两个或多个内生 (y) 变量恢复 Python Statsmodels(版本 0.12.1)中的置信/预测区间,这在 VARMAX 中很常见。以下示例正确预测了两个内生变量的样本内和样本外均值。但是仅返回第一个内生变量 dln_inv 的样本内和样本外置信区间。我也想知道如何恢复第二个变量 dln_inc 的置信区间。我将不胜感激。
import numpy as np
import statsmodels.api as sm
from statsmodels.tsa.api import VARMAX
import warnings
warnings.filterwarnings("ignore")
dta = sm.datasets.webuse('lutkepohl2', 'https://www.stata-press.com/data/r12/')
dta.index = dta.qtr
dta.index.freq = dta.index.inferred_freq
subset = dta.loc['1960-04-01':'1978-10-01', ['dln_inv', 'dln_inc', 'dln_consump']]
endog = subset[['dln_inv', 'dln_inc']] # notice two endogenous variables
exog = subset['dln_consump']
p = int(0)
q = int(1)
model = VARMAX(endog, exog=exog, order=(int(p),int(q))).fit(maxiter=100,disp=False)
in_sample_predicted = model.get_prediction()
in_sample_predicted_means = in_sample_predicted.predicted_mean
# the following command seems to produce the confidence interval for the first endogenous variable, dln_inv
in_sample_CI = in_sample_predicted.summary_frame(alpha=0.05)
n_periods = 5
exog_preforecast = exog + exog * np.random.normal(0,0.5,exog.shape)
out_sample_forecast = model.get_forecast(steps=n_periods,exog=exog_preforecast[-n_periods:])
out_sample_forecast_means = out_sample_forecast.predicted_mean
# the following command seems to produce the confidence interval for the first endogenous variable, dln_inv
out_sample_CI = out_sample_forecast.summary_frame(alpha=0.05)
【问题讨论】:
标签: python statsmodels confidence-interval varmax