简体   繁体   English

Statsmodels VARMAX:具有多个内生变量的置信/预测区间

[英]Statsmodels VARMAX: confidence / predication intervals with more than one endogenous variable

I am trying to recover confidence/prediction intervals in Python Statsmodels (Version 0.12.1) with two or more endogenous (y) variables, as is common in VARMAX.我正在尝试使用两个或多个内生 (y) 变量恢复 Python Statsmodels(版本 0.12.1)中的置信/预测区间,这在 VARMAX 中很常见。 The following example correctly predicts the in-sample and out-sample means for two endogenous varialbes.以下示例正确预测了两个内生变量的样本内和样本外均值。 But the in-sample and out-sample confidence intervals are returned only for the first endogenous variable, dln_inv.但是仅返回第一个内生变量 dln_inv 的样本内和样本外置信区间。 I would like to know how to recover the confidence interval for the second variable, dln_inc, as well.我也想知道如何恢复第二个变量 dln_inc 的置信区间。 I would appreciate any help.我将不胜感激任何帮助。

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) 

There are two ways to get confidence intervals for all variables.有两种方法可以获得所有变量的置信区间。

First, if you are using the summary_frame method, you can pass the integer index of variable you want to retrieve intervals for, using the endog argument (which seems not to be in the docstring, unfortunately).首先,如果您使用的是summary_frame方法,您可以使用endog参数(不幸的是,它似乎不在文档字符串中)传递要检索间隔的变量的 integer 索引。

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) 

Second, you can retrieve invervals for all variables at once, using the conf_int method:其次,您可以使用conf_int方法一次检索所有变量的反函数:

all_CI = out_sample_forecast.conf_int(alpha=0.05)

Which yields the following DataFrame output:产生以下 DataFrame output:

            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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 StatsModels 的置信区间和预测区间 - confidence and prediction intervals with StatsModels 统计模型根据异方差一致的标准误差绘制平均置信区间 - Statsmodels Plotting mean confidence intervals based on heteroscedastic consistent standard errors 返回StatsModel中样本外预测的标准和置信区间 - Return std and confidence intervals for out-of-sample prediction in StatsModels 使用 statsmodels SARIMAX 手动创建平均响应的置信区间 - Manually creating confidence intervals for the mean response using statsmodels SARIMAX statsmodels:将多个回归模型的汇总打印在一起 - statsmodels: printing summary of more than one regression models together 从 VARMAX statsmodels 中提取矩阵形式的系数 - Extracting coefficients in matrix form from VARMAX statsmodels 将置信区间对齐一个低于另一个 - Align confidence intervals one below the other Python Statsmodels:将SARIMAX与外生回归变量一起使用以获取预测的均值和置信区间 - Python Statsmodels: Using SARIMAX with exogenous regressors to get predicted mean and confidence intervals 使用statsmodels预测置信区间 - Predicting confidence interval with statsmodels Statsmodels:从VARMAX.fit()获取错误相关矩阵 - Statsmodels: Get the error correlation matrix from VARMAX.fit()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM