econometron.Models.VectorAutoReg.VAR
classin theeconometron.Models.VectorAutoRegmodule
Overview
A Vector Autoregression (VAR) model captures the dynamic interdependencies among multiple time series variables, modeling each variable as a linear function of its own lags and the lags of other variables. The VAR class in econometron.Models.VectorAutoReg.VAR allows specification, estimation, and analysis of VAR models with flexible workflows.
Model Specification
Given endogenous variables and lags, the VAR(p) system is:
where are coefficient matrices and is a vector of shocks.
Example For two variables with one lag:
- Units: original data scale
- Use case: multivariate time series analysis, forecasting, policy evaluation
Modes of Operation
The VAR class supports two workflows:
EL_RAPIDO– fully automated: fits the model, selects lags, runs diagnostics, forecasts, and performs structural analysis in a single step.SbS(Step-by-Step) – interactive: guides the user through each stage, allowing custom decisions on specification, estimation, and post-estimation tasks.
In Practice
- Use EL_RAPIDO for rapid end-to-end analysis.
- Use SbS for detailed, user-guided modeling and diagnostics.
class VAR capabilities
- Data validation and stationarity testing
- Lag order selection
- Model fitting and coefficient estimation
- Forecasting and scenario analysis
- Impulse Response Functions (IRFs) and Forecast Error Variance Decomposition (FEVD)
- Flexible automated or interactive workflows
Class Definition
To create an instance of the VAR class, you initialize it with your time series data and various configuration options
from econometron.Models.VectorAutoReg.VAR import VAR
model = VAR(data=[pd.DataFrame], max_p:int = 2, columns:list=None, criterion:str ='AIC', forecast_horizon:int=10, plot:bool=True, bootstrap_n:int=1000, ci_alpha:float=0.05, orth:int=False, check_stationarity:int=True, Key:str=None, Threshold:float=0.8)Parameters
| Attribute | Type | Description |
|---|---|---|
data | pd.DataFrame or pd.Series | The input time series data. Each column represents a different variable. |
max_p | int | The maximum number of lags to consider when selecting the optimal lag order. Default is 2. |
columns | list, optional | A list of column names to use from the data. If None, all numeric columns are used. |
criterion | str | The information criterion to use for selecting the best lag order ('AIC', 'BIC', 'HQIC'). Default is 'AIC'. |
forecast_horizon | int | The number of steps ahead to forecast. Default is 10. |
plot | bool | If True, generates plots for diagnostics, forecasts, IRFs, etc. Default is True. |
bootstrap_n | int | The number of bootstrap repetitions for calculating confidence intervals for IRFs. Default is 1000. |
ci_alpha | float | The significance level for confidence intervals (e.g., 0.05 for 95% CI). Default is 0.05. |
orth | bool | If True, computes Orthogonalized Impulse Responses (uses Cholesky decomposition). Default is False. |
check_stationarity | bool | If True, performs ADF and KPSS tests to check for stationarity on initialization. Default is True. |
Key | str | The operational mode. 'EL_RAPIDO' for automatic full analysis, 'SbS' for interactive step-by-step mode. |
Threshold | float | A value between 0 and 1 representing the minimum final diagnostic score to consider the model "well-fitted". Default is 0.8. |
Class Attributes
These attributes are populated after the model has been initialized and fitted.
| Attribute | Type | Description |
|---|---|---|
stationarity_results | dict | Stores the results of the stationarity tests for each variable. |
coeff_table | pd.DataFrame | A formatted table containing coefficients, standard errors, z-values, and p-values for the best-fitted model. |
fitted | bool | A boolean flag that is True if the model passes the diagnostic checks based on the Threshold. |
best_model | dict | A dictionary containing all results for the VAR model with the optimal lag order (p). |
best_p | int | The optimal number of lags selected based on the specified criterion. |
best_criterion_value | float | The value of the information criterion for the best model. |
all_results | list | A list of dictionaries, where each dictionary contains the estimation results for a specific lag order p. |
roots | list | The roots of the companion matrix. Used to check for model stability. |
Methods
fit(columns=None, p=None, output=True, get_order=False)
Purpose
- Fits the VAR model. This method iterates from lag 1 to
max_p, calculates the information criterion for each, selects the optimal lag orderp, and estimates the final model.
- Parameters:
p(int, optional): If specified, fits a VAR model with this exact lag order instead of selecting one.output(bool): IfTrue, prints the coefficient table and full diagnostic summary.get_order(bool): IfTrue, returns a table of information criteria for all tested lags without fitting the final model.
- Returns: The
best_modeldictionary containing all estimation results.
- Fits the VAR model. This method iterates from lag 1 to
run_full_diagnosis(num_lags=8, plot=False, threshold=0.8)
Purpose Performs a comprehensive set of diagnostic tests on the model's residuals to check for model adequacy.
- Tests performed:
- Stability: Checks if all roots of the companion matrix lie inside the unit circle.
- Serial Correlation: Ljung-Box and Durbin-Watson tests.
- Heteroscedasticity: ARCH-LM test.
- Normality: Jarque-Bera and Shapiro-Wilk tests.
- Structural Breaks: CUSUM test.
- Output: Prints a detailed summary table with scores and verdicts for each test, culminating in a final score and a "Passed" or "Failed" verdict for the overall model.
predict(h, plot=True)
Purpose Generates forecasts for a specified number of periods ahead.
- Parameters:
h(int): The forecast horizon.plot(bool) :Trueby Default
- Returns: A
pd.DataFramecontaining the point forecasts. - Output: If
plot=True, it displays a plot of the historical data along with the forecasts.
impulse_res(h, orth=False, bootstrap=True, n_boot=1000, plot=True, tol=1e-6)
Purpose Computes and plots the Impulse Response Functions (IRFs). IRFs show the effect of a one-time shock to one variable on itself and on other variables in the system over time.
- Parameters:
h(int): The number of periods to trace the impulse response.orth(bool): IfTrue, computes orthogonalized IRFs.
- Returns: A dictionary containing the IRF matrices for each impulse-response pair.
- Output: If
plot=True, it displays a grid of plots for each IRF.
FEVD(h, plot=True)
Puprose Computes the Forecast Error Variance Decomposition (FEVD). FEVD shows the proportion of the forecast error variance for each variable that is attributable to shocks from itself and other variables.
- Parameters:
h(int): The forecast horizon over which to compute the decomposition.
- Returns: A dictionary containing the FEVD results.
- Output: If
plot=True, it displays stacked bar charts showing the decomposition for each variable.
simulate(n_periods, plot=True, tol=1e-6)
Purpose Simulates future paths of the variables based on the estimated model parameters and residual covariance.
- Parameters:
n_periods(int): The number of periods to simulate.
- Returns: A
pd.DataFramecontaining the simulated data. - Output: If
plot=True, it displays a plot of the simulated paths.
Example Usage
In this example we'll work with a dataset containing the following 3 Timeseries:
GDP: Gross Domestic ProductDPCCRV1Q225SBEA: Real Personal Consumption ExpendituresDFF: Federal Funds Rate
The dataset data has a DatetimeIndex with 140 entries, ranging from 1990-04-01 to 2025-01-01,The data is already stationnary (via TransformTS class) using diffrencing.
Example : Normal Usage
1. Initialization
First, let's import The class VAR
from econometron.Models.VectorAutoReg.VAR import VARvm=VAR( data, max_p=2, columns=None, criterion='AIC', forecast_horizon=10, plot=True, bootstrap_n=1000, ci_alpha=0.05, orth=False, check_stationarity=True, Key=None, Threshold=0.8)1. Fitting The Model :
Manual order selection
vm.order_select()
p aic bic hqic
0 4 9.367855 10.187312 9.700858
1 5 9.409163 10.417726 9.819013
2 6 9.476147 10.673816 9.962844
3 7 9.506115 10.892889 10.069658
4 1 9.514875 9.767016 9.617337
5 2 9.555721 9.996968 9.735031
6 3 9.573787 10.204139 9.829944
7 8 9.606017 11.181897 10.246407
8 9 9.687082 11.452067 10.404319
9 10 9.701056 11.655147 10.495140Fitting the Model with The order 4 indexed at 0
res=var.fit(p=4)Or simply You can apply:
Fitting with Automatic order selection
model_1=vm.fit()2. Automated Analysis (EL_RAPIDO mode)
This mode runs the entire analysis pipeline automatically. It's great for a quick, comprehensive overview.
# Initialize in EL_RAPIDO mode
# This will automatically:
# 1. Check for stationarity.
# 2. Select the best lag order up to max_p=4 using AIC.
# 3. Fit the best VAR model.
# 4. Print coefficient tables and a full diagnostic report.
# 5. Generate and plot forecasts, impulse responses, and FEVD.
# 6. Run a simulation.
print("\n--- Running in EL_RAPIDO Mode ---")
var_auto = VAR(
df,
max_p=4,
criterion='AIC',
Key='EL_RAPIDO',
forecast_horizon=12,
bootstrap_n=500 # Using fewer for speed in example
)This will produce a series of tables and plots directly in your console/notebook, providing a complete analysis without any further user input. The output will include the lag selection table, coefficient estimates, a detailed diagnostic summary, and plots for forecasts, IRFs, FEVD, and simulations.
3. Interactive Analysis (SbS mode)
This mode is ideal for users who want to control each step of the process. The class will prompt you for decisions along the way.
# --- Running in Step-by-Step (SbS) Mode ---
print("\n--- Running in SbS Mode ---")
# The class will now wait for your input at each step.
var_sbs = VAR(df, max_p=4, Key='SbS')
# ---- MOCK INTERACTIVE SESSION ----
# After initialization and stationarity checks, it will ask:
# Do you want to refit the model? (y/n): y
# Do you want to change the criterion? (y/n): y
# Enter your criterion (AIC, BIC, HQIC): BIC
# (It prints the lag order table sorted by BIC)
#
# Select VAR order (p): 1
# (Model is refitted with p=1, and diagnostics are printed)
#
# Do you want to compute Impulse Responses? (y/n): y
# (Computes and plots IRFs)
#
# Do you want to compute FEVD? (y/n): n
#
# Do you want to generate Forecasts? (y/n): y
# (Generates and plots forecasts)
#
# Do you want to run a Simulation? (y/n): nThis interactive workflow allows for greater control, letting you inspect results at each stage before proceeding to the next.
