Skip to content

econometron.Models.VectorAutoReg.VAR

  • class in the econometron.Models.VectorAutoReg module

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

python
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

AttributeTypeDescription
datapd.DataFrame or pd.SeriesThe input time series data. Each column represents a different variable.
max_pintThe maximum number of lags to consider when selecting the optimal lag order. Default is 2.
columnslist, optionalA list of column names to use from the data. If None, all numeric columns are used.
criterionstrThe information criterion to use for selecting the best lag order ('AIC', 'BIC', 'HQIC'). Default is 'AIC'.
forecast_horizonintThe number of steps ahead to forecast. Default is 10.
plotboolIf True, generates plots for diagnostics, forecasts, IRFs, etc. Default is True.
bootstrap_nintThe number of bootstrap repetitions for calculating confidence intervals for IRFs. Default is 1000.
ci_alphafloatThe significance level for confidence intervals (e.g., 0.05 for 95% CI). Default is 0.05.
orthboolIf True, computes Orthogonalized Impulse Responses (uses Cholesky decomposition). Default is False.
check_stationarityboolIf True, performs ADF and KPSS tests to check for stationarity on initialization. Default is True.
KeystrThe operational mode. 'EL_RAPIDO' for automatic full analysis, 'SbS' for interactive step-by-step mode.
ThresholdfloatA 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.

AttributeTypeDescription
stationarity_resultsdictStores the results of the stationarity tests for each variable.
coeff_tablepd.DataFrameA formatted table containing coefficients, standard errors, z-values, and p-values for the best-fitted model.
fittedboolA boolean flag that is True if the model passes the diagnostic checks based on the Threshold.
best_modeldictA dictionary containing all results for the VAR model with the optimal lag order (p).
best_pintThe optimal number of lags selected based on the specified criterion.
best_criterion_valuefloatThe value of the information criterion for the best model.
all_resultslistA list of dictionaries, where each dictionary contains the estimation results for a specific lag order p.
rootslistThe 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 order p, 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): If True, prints the coefficient table and full diagnostic summary.
      • get_order (bool): If True, returns a table of information criteria for all tested lags without fitting the final model.
    • Returns: The best_model dictionary containing all estimation results.

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:
    1. Stability: Checks if all roots of the companion matrix lie inside the unit circle.
    2. Serial Correlation: Ljung-Box and Durbin-Watson tests.
    3. Heteroscedasticity: ARCH-LM test.
    4. Normality: Jarque-Bera and Shapiro-Wilk tests.
    5. 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) : True by Default
  • Returns: A pd.DataFrame containing 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): If True, 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.DataFrame containing 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 Product
  • DPCCRV1Q225SBEA: Real Personal Consumption Expenditures
  • DFF: 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

python
from econometron.Models.VectorAutoReg.VAR import VAR
python
vm=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

python
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.495140

Fitting the Model with The order 4 indexed at 0

python
res=var.fit(p=4)

Or simply You can apply:

Fitting with Automatic order selection

python
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.

python
# 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.

python
# --- 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): n

This interactive workflow allows for greater control, letting you inspect results at each stage before proceeding to the next.