【问题标题】:Statsmodels VARMAX: confidence / predication intervals with more than one endogenous variableStatsmodels VARMAX:具有多个内生变量的置信/预测区间
【发布时间】: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


    【解决方案1】:

    有两种方法可以获得所有变量的置信区间。

    首先,如果您使用summary_frame 方法,则可以使用endog 参数(不幸的是,该参数似乎不在文档字符串中)传递要检索其间隔的变量的整数索引。

    summary_dln_inv = out_sample_forecast.summary_frame(endog=0, alpha=0.05) 
    summary_dln_inc = out_sample_forecast.summary_frame(endog=1, alpha=0.05) 
    

    其次,您可以使用conf_int 方法一次检索所有变量的反函数:

    all_CI = out_sample_forecast.conf_int(alpha=0.05)
    

    产生以下 DataFrame 输出:

                lower dln_inv  lower dln_inc  upper dln_inv  upper dln_inc
    1979-01-01      -0.067805       0.011456       0.101923       0.050345
    1979-04-01      -0.081301      -0.007333       0.095298       0.034796
    1979-07-01      -0.080236      -0.006666       0.096362       0.035463
    1979-10-01      -0.087785      -0.011397       0.088813       0.030732
    1980-01-01      -0.085402      -0.009903       0.091197       0.032226
    

    【讨论】:

      猜你喜欢
      • 2013-07-07
      • 2018-12-21
      • 2015-11-18
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2017-11-02
      • 2023-03-14
      • 2021-01-22
      相关资源
      最近更新 更多