Main Content

Bayesian vector autoregression (VAR) model with normal conjugate prior and fixed covariance for data likelihood

The Bayesian VAR model object `normalbvarm`

specifies the prior distribution of the array of model coefficients Λ in an *m*-D VAR(*p*) model, where the innovations covariance matrix Σ is known and fixed. The prior distribution of Λ is the normal conjugate prior model.

In general, when you create a Bayesian VAR model object, it specifies the joint prior distribution and characteristics of the VARX model only. That is, the model object is a template intended for further use. Specifically, to incorporate data into the model for posterior distribution analysis, pass the model object and data to the appropriate object function.

To create a `normalbvarm`

object, use either the `normalbvarm`

function (described here) or the `bayesvarm`

function. The syntaxes for each function are similar, but the options differ. `bayesvarm`

enables you to set prior hyperparameter values for Minnesota prior[1] regularization easily, whereas `normalbvarm`

requires the entire specification of prior distribution hyperparameters.

creates a `PriorMdl`

= normalbvarm(`numseries`

,`numlags`

)`numseries`

-D Bayesian VAR(`numlags`

) model object `PriorMdl`

, which specifies dimensionalities and prior assumptions for all model coefficients $$\lambda =\text{vec}\left(\Lambda \right)=\text{vec}\left({\left[\begin{array}{ccccccc}{\Phi}_{1}& {\Phi}_{2}& \cdots & {\Phi}_{p}& c& \delta & {\rm B}\end{array}\right]}^{\prime}\right)$$, where:

`numseries`

=*m*, the number of response time series variables.`numlags`

=*p*, the AR polynomial order.The prior distribution of

*λ*is the normal conjugate prior model.The fixed innovations covariance Σ is the

*m*-by-*m*identity matrix.

sets writable properties (except `PriorMdl`

= normalbvarm(`numseries`

,`numlags`

,`Name,Value`

)`NumSeries`

and `P`

) using name-value pair arguments. Enclose each property name in quotes. For example, `normalbvarm(3,2,'Sigma',4*eye(3),'SeriesNames',["UnemploymentRate" "CPI" "FEDFUNDS"])`

specifies the names of the three response variables in the Bayesian VAR(2) model, and fixes the innovations covariance matrix at `4*eye(3)`

.

`numseries`

— Number of time series `1`

(default) | positive integerNumber of time series *m*, specified as a positive integer. `numseries`

specifies the dimensionality of the multivariate response variable *y _{t}* and innovation

`numseries`

sets the `NumSeries`

property.

**Data Types: **`double`

`numlags`

— Number of lagged responsesnonnegative integer

Number of lagged responses in each equation of *y*_{t}, specified as a nonnegative integer. The resulting model is a VAR(`numlags`

) model; each lag has a `numseries`

-by-`numseries`

coefficient matrix.

`numlags`

sets the `P`

property.

**Data Types: **`double`

You can set writable property values when you create the model object by using name-value pair argument syntax, or after you create the model object by using dot notation. For example, to create a 3-D Bayesian VAR(1) model and label the first through third response variables, and then include a linear time trend term, enter:

PriorMdl = normalbvarm(3,1,'SeriesNames',["UnemploymentRate" "CPI" "FEDFUNDS"]); PriorMdl.IncludeTrend = true;

`Description`

— Model descriptionstring scalar | character vector

Model description, specified as a string scalar or character vector. The default value describes the model dimensionality, for example `'2-Dimensional VAR(3) Model'`

.

**Example: **`"Model 1"`

**Data Types: **`string`

| `char`

`NumSeries`

— Number of time series positive integer

This property is read-only.

Number of time series *m*, specified as a positive integer. `NumSeries`

specifies the dimensionality of the multivariate response variable *y _{t}* and innovation

**Data Types: **`double`

`P`

— Multivariate autoregressive polynomial ordernonnegative integer

This property is read-only.

Multivariate autoregressive polynomial order, specified as a nonnegative integer. `P`

is the maximum lag that has a nonzero coefficient matrix.

`P`

specifies the number of presample observations required to initialize the model.

**Data Types: **`double`

`SeriesNames`

— Response series namesstring vector | cell array of character vectors

Response series names, specified as a `NumSeries`

length string vector. The default is `['Y1' 'Y2' ... 'Y`

. * NumSeries*']

`normalbvarm`

stores `SeriesNames`

as a string vector.**Example: **`["UnemploymentRate" "CPI" "FEDFUNDS"]`

**Data Types: **`string`

`IncludeConstant`

— Flag for including model constant `true`

(default) | `false`

Flag for including a model constant *c*, specified as a value in this table.

Value | Description |
---|---|

`false` | Response equations do not include a model constant. |

`true` | All response equations contain a model constant. |

**Data Types: **`logical`

`IncludeTrend`

— Flag for including linear time trend term `false`

(default) | `true`

Flag for including a linear time trend term *δt*, specified as a value in this table.

Value | Description |
---|---|

`false` | Response equations do not include a linear time trend term. |

`true` | All response equations contain a linear time trend term. |

**Data Types: **`logical`

`NumPredictors`

— Number of exogenous predictor variables in model regression component`0`

(default) | nonnegative integerNumber of exogenous predictor variables in the model regression component, specified as a nonnegative integer. `normalbvarm`

includes all predictor variables symmetrically in each response equation.

`Mu`

— Mean of multivariate normal prior on `zeros(NumSeries*(NumSeries*P + IncludeIntercept + IncludeTrend + NumPredictors),1)`

(default) | numeric vectorMean of the multivariate normal prior on *λ*, specified as a `NumSeries*`

-by-1 numeric vector, where `k`

(the number of coefficients in a response equation).* k* = NumSeries*P + IncludeIntercept + IncludeTrend + NumPredictors

`Mu(1:`

corresponds to all coefficients in the equation of response variable * k*)

`SeriesNames(1)`

, `Mu((``k`

+ 1):(2*`k`

))

corresponds to all coefficients in the equation of response variable `SeriesNames(2)`

, and so on. For a set of indices corresponding to an equation:Elements

`1`

through`NumSeries`

correspond to the lag 1 AR coefficients of the response variables ordered by`SeriesNames`

.Elements

`NumSeries + 1`

through`2*NumSeries`

correspond to the lag 2 AR coefficients of the response variables ordered by`SeriesNames`

.In general, elements

`(`

through– 1)*NumSeries + 1`q`

corresponds to the lag*NumSeries`q`

AR coefficients of the response variables ordered by`q`

`SeriesNames`

.If

`IncludeConstant`

is`true`

, element`NumSeries*P + 1`

is the model constant.If

`IncludeTrend`

is`true`

, element`NumSeries*P + 2`

is the linear time trend coefficient.If

`NumPredictors`

> 0, elements`NumSeries*P + 3`

through

constitute the vector of regression coefficients of the exogenous variables.`k`

This figure shows the structure of the transpose of `Mu`

for a 2-D VAR(3) model that contains a constant vector and four exogenous predictors:

$$[\stackrel{{y}_{1,t}}{\overbrace{\begin{array}{ccccccccccc}{\varphi}_{1,11}& {\varphi}_{1,12}& {\varphi}_{2,11}& {\varphi}_{2,12}& {\varphi}_{3,11}& {\varphi}_{3,12}& {c}_{1}& {\beta}_{11}& {\beta}_{12}& {\beta}_{13}& {\beta}_{14}\end{array}}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\stackrel{{y}_{2,t}}{\overbrace{\begin{array}{ccccccccccc}{\varphi}_{1,21}& {\varphi}_{1,22}& {\varphi}_{2,21}& {\varphi}_{2,22}& {\varphi}_{3,21}& {\varphi}_{3,22}& {c}_{2}& {\beta}_{21}& {\beta}_{22}& {\beta}_{23}& {\beta}_{24}\end{array}}}],$$

where

*ϕ*_{q,jk}is element (*j*,*k*) of the lag*q*AR coefficient matrix.*c*_{j}is the model constant in the equation of response variable*j*.*B*_{ju}is the regression coefficient of the exogenous variable*u*in the equation of response variable*j*.

**Tip**

`bayesvarm`

enables you to specify `Mu`

easily by using the Minnesota regularization method. To specify `Mu`

directly:

Set separate variables for the prior mean of each coefficient matrix and vector.

Horizontally concatenate all coefficient means in this order:

$$Coeff=\left[\begin{array}{ccccccc}{\Phi}_{1}& {\Phi}_{2}& \cdots & {\Phi}_{p}& c& \delta & {\rm B}\end{array}\right].$$

Vectorize the transpose of the coefficient mean matrix.

Mu = Coeff.'; Mu = Mu(:);

**Data Types: **`double`

`V`

— Conditional covariance matrix of multivariate normal prior on `eye(NumSeries*(NumSeries*P + IncludeIntercept + IncludeTrend + NumPredictors))`

(default) | symmetric, positive definite numeric matrixConditional covariance matrix of multivariate normal prior on *λ*, specified as a `NumSeries*`

-by-`k`

`NumSeries*`

symmetric, positive definite matrix, where `k`

(the number of coefficients in a response equation).* k* = NumSeries*P + IncludeIntercept + IncludeTrend + NumPredictors

Row and column indices correspond to the model coefficients in the same way as Mu. For example, consider a 3-D VAR(2) model containing a constant and four exogenous variables.

`V(1,1)`

is Var(*ϕ*_{1,11}).`V(5,6)`

is Cov(*ϕ*_{2,12},*ϕ*_{2,13}).`V(8,9)`

is Cov(*β*_{11},*β*_{12}).

**Tip**

`bayesvarm`

enables you to create any Bayesian VAR prior model and specify `V`

easily by using the Minnesota regularization method.

**Data Types: **`double`

`Sigma`

— Fixed innovations covariance matrix Σ`eye(NumSeries)`

(default) | positive definite numeric matrixFixed innovations covariance matrix Σ of the `NumSeries`

innovations at each time *t* = 1,...,*T*, specified as a `NumSeries`

-by-`NumSeries`

positive definite numeric matrix. Rows and columns correspond to innovations in the equations of the response variables ordered by `SeriesNames`

.

`Sigma`

sets the `Covariance`

property.

**Data Types: **`double`

`AR`

— Distribution mean of autoregressive coefficient matrices Φcell vector of numeric matrices

This property is read-only.

Distribution mean of the autoregressive coefficient matrices Φ_{1},…,Φ_{p} associated with the lagged responses, specified as a `P`

-D cell vector of `NumSeries`

-by-`NumSeries`

numeric matrices.

`AR{`

is Φ* j*}

`j`

`SeriesNames`

determines the order of response variables and equations. Coefficient signs are those of the VAR model expressed in difference-equation notation.If `P`

= 0, `AR`

is an empty cell. Otherwise, `AR`

is the collection of AR coefficient means extracted from `Mu`

.

**Data Types: **`cell`

`Constant`

— Distribution mean of model constant numeric vector

This property is read-only.

Distribution mean of the model constant *c* (or intercept), specified as a `NumSeries`

-by-1 numeric vector. `Constant(`

is the constant in equation * j*)

`j`

`SeriesNames`

determines the order of equations.If `IncludeConstant`

= `false`

, `Constant`

is an empty array. Otherwise, `Constant`

is the model constant vector mean extracted from `Mu`

.

**Data Types: **`double`

`Trend`

— Distribution mean of linear time trend numeric vector

This property is read-only.

Distribution mean of the linear time trend *δ*, specified as a `NumSeries`

-by-1 numeric vector. `Trend(`

is the linear time trend in equation * j*)

`j`

`SeriesNames`

determines the order of equations.If `IncludeTrend`

= `false`

(the default), `Trend`

is an empty array. Otherwise, `Trend`

is the linear time trend coefficient mean extracted from `Mu`

.

**Data Types: **`double`

`Beta`

— Distribution mean of regression coefficient matrix Βnumeric matrix

This property is read-only.

Distribution mean of the regression coefficient matrix B associated with the exogenous predictor variables, specified as a `NumSeries`

-by-`NumPredictors`

numeric matrix.

`Beta(`

contains the regression coefficients of each predictor in the equation of response variable * j*,:)

`Beta(:,``k`

)

contains the regression coefficient in each equation of predictor `Mu`

and a small variance in `V`

.When you create a model, the predictor variables are hypothetical. You specify predictor data when you operate on the model (for example, when you estimate the posterior by using `estimate`

). Columns of the predictor data determine the order of the columns of `Beta`

.

**Data Types: **`double`

`Covariance`

— Fixed innovations covariance matrix Σpositive definite numeric matrix

This property is read-only.

Fixed innovations covariance matrix Σ of the `NumSeries`

innovations at each time *t* = 1,...,*T*, specified as a `NumSeries`

-by-`NumSeries`

symmetric, positive definite numeric matrix. Rows and columns correspond to innovations in the equations of the response variables ordered by `SeriesNames`

.

The `Sigma`

property sets `Covariance`

.

**Data Types: **`double`

`estimate` | Estimate posterior distribution of Bayesian vector autoregression (VAR) model parameters |

`forecast` | Forecast responses from Bayesian vector autoregression (VAR) model |

`simsmooth` | Simulation smoother of Bayesian vector autoregression (VAR) model |

`simulate` | Simulate coefficients and innovations covariance matrix of Bayesian vector autoregression (VAR) model |

`summarize` | Distribution summary statistics of Bayesian vector autoregression (VAR) model |

Consider the 3-D VAR(4) model for the US inflation (`INFL`

), unemployment (`UNRATE`

), and federal funds (`FEDFUNDS`

) rates.

$$\left[\begin{array}{l}{\text{INFL}}_{t}\\ {\text{UNRATE}}_{t}\\ {\text{FEDFUNDS}}_{t}\end{array}\right]=c+\sum _{j=1}^{4}{\Phi}_{j}\left[\begin{array}{l}{\text{INFL}}_{t-j}\\ {\text{UNRATE}}_{t-j}\\ {\text{FEDFUNDS}}_{t-j}\end{array}\right]+\left[\begin{array}{c}{\epsilon}_{1,t}\\ {\epsilon}_{2,t}\\ {\epsilon}_{3,t}\end{array}\right].$$

For all $$t$$, $${\epsilon}_{t}$$ is a series of independent 3-D normal innovations with a mean of 0 and fixed covariance $\Sigma $ = $\mathit{I}$, the 3-D identity matrix. Assume that the prior distribution $\mathrm{vec}\left({\left[{\Phi}_{1},...,{\Phi}_{4},\mathit{c}\right]}^{\prime}\right)\sim {{\rm N}}_{39}\left(\mu ,\mathit{V}\right)$, where $\mu $ is a 39-by-1 vector of means and $\mathit{V}$ is the 39-by-39 covariance matrix.

Create a normal conjugate prior model for the 3-D VAR(4) model parameters.

numseries = 3; numlags = 4; PriorMdl = normalbvarm(numseries,numlags)

PriorMdl = normalbvarm with properties: Description: "3-Dimensional VAR(4) Model" NumSeries: 3 P: 4 SeriesNames: ["Y1" "Y2" "Y3"] IncludeConstant: 1 IncludeTrend: 0 NumPredictors: 0 Mu: [39x1 double] V: [39x39 double] Sigma: [3x3 double] AR: {[3x3 double] [3x3 double] [3x3 double] [3x3 double]} Constant: [3x1 double] Trend: [3x0 double] Beta: [3x0 double] Covariance: [3x3 double]

`PriorMdl`

is a `normalbvarm`

Bayesian VAR model object representing the prior distribution of the coefficients of the 3-D VAR(4) model. The command line display shows properties of the model. You can display properties by using dot notation.

Display the prior mean matrices of the four AR coefficients by setting each matrix in the cell to a variable.

AR1 = PriorMdl.AR{1}

`AR1 = `*3×3*
0 0 0
0 0 0
0 0 0

AR2 = PriorMdl.AR{2}

`AR2 = `*3×3*
0 0 0
0 0 0
0 0 0

AR3 = PriorMdl.AR{3}

`AR3 = `*3×3*
0 0 0
0 0 0
0 0 0

AR4 = PriorMdl.AR{4}

`AR4 = `*3×3*
0 0 0
0 0 0
0 0 0

`normalbvarm`

centers all AR coefficients at `0`

by default. The `AR`

property is read only, but it is derived from the writeable property `Mu`

.

Display the fixed innovations covariance $\Sigma $.

PriorMdl.Covariance

`ans = `*3×3*
1 0 0
0 1 0
0 0 1

`Covariance`

is a read-only property. To set the value $\Sigma $ , use the `'Sigma'`

name-value pair argument or specify the `Sigma`

property by using dot notation. For example:

PriorMdl.Sigma = 4*eye(PriorMdl.NumSeries);

Consider the 3-D VAR(4) model of Create Normal Conjugate Prior Model.

Suppose econometric theory dictates that

$$\Sigma =\left[\begin{array}{ccc}{10}^{-5}& 0& {10}^{-4}\\ 0& 0.1& -0.2\\ {10}^{-4}& -0.2& 1.6\end{array}\right].$$

Create a normal conjugate prior model for the VAR model coefficients. Specify the value of $$\Sigma $$.

```
numseries = 3;
numlags = 4;
Sigma = [10e-5 0 10e-4; 0 0.1 -0.2; 10e-4 -0.2 1.6];
PriorMdl = normalbvarm(numseries,numlags,'Sigma',Sigma)
```

PriorMdl = normalbvarm with properties: Description: "3-Dimensional VAR(4) Model" NumSeries: 3 P: 4 SeriesNames: ["Y1" "Y2" "Y3"] IncludeConstant: 1 IncludeTrend: 0 NumPredictors: 0 Mu: [39x1 double] V: [39x39 double] Sigma: [3x3 double] AR: {[3x3 double] [3x3 double] [3x3 double] [3x3 double]} Constant: [3x1 double] Trend: [3x0 double] Beta: [3x0 double] Covariance: [3x3 double]

Because $$\Sigma $$ is fixed for `normalbvarm`

prior models, `PriorMdl.Sigma`

and `PriorMdl.Covariance`

are equal.

PriorMdl.Sigma

`ans = `*3×3*
0.0001 0 0.0010
0 0.1000 -0.2000
0.0010 -0.2000 1.6000

PriorMdl.Covariance

`ans = `*3×3*
0.0001 0 0.0010
0 0.1000 -0.2000
0.0010 -0.2000 1.6000

Consider a 1-D Bayesian AR(2) model for the daily NASDAQ returns from January 2, 1990 through December 31, 2001.

$${y}_{t}=c+{\varphi}_{1}{y}_{t-1}+{\varphi}_{2}{y}_{t-1}+{\epsilon}_{t}.$$

The coefficient prior distribution is $$[\begin{array}{ccc}{\varphi}_{1}& {\varphi}_{2}& c\end{array}{]}^{\prime}|{\sigma}^{2}\sim {N}_{3}\left(\mu ,V\right)$$, where $\mu $ is a 3-by-1 vector of coefficient means and $\mathit{V}$ is a 3-by-3 covariance matrix. Assume Var(${\epsilon}_{\mathit{t}}$) is 2.

Create a normal conjugate prior model for the AR(2) model parameters.

```
numseries = 1;
numlags = 2;
PriorMdl = normalbvarm(numseries,numlags,'Sigma',2)
```

PriorMdl = normalbvarm with properties: Description: "1-Dimensional VAR(2) Model" NumSeries: 1 P: 2 SeriesNames: "Y1" IncludeConstant: 1 IncludeTrend: 0 NumPredictors: 0 Mu: [3x1 double] V: [3x3 double] Sigma: 2 AR: {[0] [0]} Constant: 0 Trend: [1x0 double] Beta: [1x0 double] Covariance: 2

In the 3-D VAR(4) model of Create Normal Conjugate Prior Model, consider excluding lags 2 and 3 from the model.

You cannot exclude coefficient matrices from models, but you can specify high prior tightness on zero for coefficients that you want to exclude.

Create a normal conjugate prior model for the 3-D VAR(4) model parameters. Specify response variable names.

By default, AR coefficient prior means are zero. Specify high tightness values for lags 2 and 3 by setting their prior variances to `1e-6`

. Leave all other coefficient tightness values at their defaults:

`1`

for AR coefficient variances`1e3`

for constant vector variances`0`

for all coefficient covariances

numseries = 3; numlags = 4; seriesnames = ["INFL"; "UNRATE"; "FEDFUNDS"]; vPhi1 = ones(numseries,numseries); vPhi2 = 1e-6*ones(numseries,numseries); vPhi3 = 1e-6*ones(numseries,numseries); vPhi4 = ones(numseries,numseries); vc = 1e3*ones(3,1); Vmat = [vPhi1 vPhi2 vPhi3 vPhi4 vc]'; V = diag(Vmat(:)); PriorMdl = normalbvarm(numseries,numlags,'SeriesNames',seriesnames,... 'V',V)

PriorMdl = normalbvarm with properties: Description: "3-Dimensional VAR(4) Model" NumSeries: 3 P: 4 SeriesNames: ["INFL" "UNRATE" "FEDFUNDS"] IncludeConstant: 1 IncludeTrend: 0 NumPredictors: 0 Mu: [39x1 double] V: [39x39 double] Sigma: [3x3 double] AR: {[3x3 double] [3x3 double] [3x3 double] [3x3 double]} Constant: [3x1 double] Trend: [3x0 double] Beta: [3x0 double] Covariance: [3x3 double]

`normalbvarm`

options enable you to specify coefficient prior hyperparameter values directly, but `bayesvarm`

options are well suited for tuning hyperparameters following the Minnesota regularization method.

Consider the 3-D VAR(4) model of Create Normal Conjugate Prior Model. The model contains 39 coefficients. For coefficient sparsity, create a normal conjugate Bayesian VAR model by using `bayesvarm`

. Specify the following, *a priori*:

Each response is an AR(1) model, on average, with lag 1 coefficient 0.75.

Prior self-lag coefficients have variance 100. This large variance setting allows the data to influence the posterior more than the prior.

Prior cross-lag coefficients have variance 1. This small variance setting tightens the cross-lag coefficients to zero during estimation.

Prior coefficient covariances decay with increasing lag at a rate of 2 (that is, lower lags are more important than higher lags).

The innovations covariance $\Sigma $ = $\mathit{I}$.

numseries = 3; numlags = 4; seriesnames = ["INFL"; "UNRATE"; "FEDFUNDS"]; Sigma = eye(numseries); PriorMdl = bayesvarm(numseries,numlags,'ModelType','normal','Sigma',Sigma,... 'Center',0.75,'SelfLag',100,'CrossLag',1,'Decay',2,'SeriesNames',seriesnames)

PriorMdl = normalbvarm with properties: Description: "3-Dimensional VAR(4) Model" NumSeries: 3 P: 4 SeriesNames: ["INFL" "UNRATE" "FEDFUNDS"] IncludeConstant: 1 IncludeTrend: 0 NumPredictors: 0 Mu: [39x1 double] V: [39x39 double] Sigma: [3x3 double] AR: {[3x3 double] [3x3 double] [3x3 double] [3x3 double]} Constant: [3x1 double] Trend: [3x0 double] Beta: [3x0 double] Covariance: [3x3 double]

Display all prior coefficient means.

Phi1 = PriorMdl.AR{1}

`Phi1 = `*3×3*
0.7500 0 0
0 0.7500 0
0 0 0.7500

Phi2 = PriorMdl.AR{2}

`Phi2 = `*3×3*
0 0 0
0 0 0
0 0 0

Phi3 = PriorMdl.AR{3}

`Phi3 = `*3×3*
0 0 0
0 0 0
0 0 0

Phi4 = PriorMdl.AR{4}

`Phi4 = `*3×3*
0 0 0
0 0 0
0 0 0

Display a heatmap of the prior coefficient covariances for each response equation.

numexocoeffseqn = PriorMdl.IncludeConstant + ... PriorMdl.IncludeTrend + PriorMdl.NumPredictors; % Number of exogenous coefficients per equation numcoeffseqn = PriorMdl.NumSeries*PriorMdl.P + numexocoeffseqn; % Total number of coefficients per equation arcoeffnames = strings(numseries,numlags,numseries); for j = 1:numseries % Equations for r = 1:numlags for k = 1:numseries % Response Variables arcoeffnames(k,r,j) = "\phi_{"+r+","+j+k+"}"; end end arcoeffseqn = arcoeffnames(:,:,j); idx = ((j-1)*numcoeffseqn + 1):(numcoeffseqn*j) - numexocoeffseqn; Veqn = PriorMdl.V(idx,idx); figure heatmap(arcoeffseqn(:),arcoeffseqn(:),Veqn); title(sprintf('Equation of %s',seriesnames(j))) end

Consider the 3-D VAR(4) model of Create Normal Conjugate Prior Model. Estimate the posterior distribution, and generate forecasts from the corresponding posterior predictive distribution.

**Load and Preprocess Data**

Load the US macroeconomic data set. Compute the inflation rate. Plot all response series.

load Data_USEconModel seriesnames = ["INFL" "UNRATE" "FEDFUNDS"]; DataTable.INFL = 100*[NaN; price2ret(DataTable.CPIAUCSL)]; figure plot(DataTable.Time,DataTable{:,seriesnames}) legend(seriesnames)

Stabilize the unemployment and federal funds rates by applying the first difference to each series.

```
DataTable.DUNRATE = [NaN; diff(DataTable.UNRATE)];
DataTable.DFEDFUNDS = [NaN; diff(DataTable.FEDFUNDS)];
seriesnames(2:3) = "D" + seriesnames(2:3);
```

Remove all missing values from the data.

rmDataTable = rmmissing(DataTable);

**Create Prior Model**

Create a normal conjugate Bayesian VAR(4) prior model for the three response series. Specify the response variable names. Assume that the innovations covariance is the identity matrix.

```
numseries = numel(seriesnames);
numlags = 4;
PriorMdl = normalbvarm(numseries,numlags,'SeriesNames',seriesnames);
```

**Estimate Posterior Distribution**

Estimate the posterior distribution by passing the prior model and entire data series to `estimate`

.

PosteriorMdl = estimate(PriorMdl,rmDataTable{:,seriesnames},'Display','equation');

Bayesian VAR under normal priors and fixed Sigma Effective Sample Size: 197 Number of equations: 3 Number of estimated Parameters: 39 VAR Equations | INFL(-1) DUNRATE(-1) DFEDFUNDS(-1) INFL(-2) DUNRATE(-2) DFEDFUNDS(-2) INFL(-3) DUNRATE(-3) DFEDFUNDS(-3) INFL(-4) DUNRATE(-4) DFEDFUNDS(-4) Constant ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ INFL | 0.1260 -0.4400 0.1049 0.3176 -0.0545 0.0440 0.4173 0.2421 0.0515 0.0247 -0.1639 0.0080 0.1064 | (0.1367) (0.2673) (0.0700) (0.1551) (0.2854) (0.0739) (0.1536) (0.2811) (0.0766) (0.1605) (0.2652) (0.0708) (0.1483) DUNRATE | -0.0236 0.4440 0.0350 0.0900 0.2295 0.0520 -0.0330 0.0567 0.0010 0.0298 -0.1665 0.0104 -0.0536 | (0.1367) (0.2673) (0.0700) (0.1551) (0.2854) (0.0739) (0.1536) (0.2811) (0.0766) (0.1605) (0.2652) (0.0708) (0.1483) DFEDFUNDS | -0.1514 -1.3408 -0.2762 0.3275 -0.2971 -0.3041 0.2609 -0.6971 0.0130 -0.0692 0.1392 -0.1341 -0.3902 | (0.1367) (0.2673) (0.0700) (0.1551) (0.2854) (0.0739) (0.1536) (0.2811) (0.0766) (0.1605) (0.2652) (0.0708) (0.1483) Innovations Covariance Matrix | INFL DUNRATE DFEDFUNDS -------------------------------------- INFL | 1 0 0 | (0) (0) (0) DUNRATE | 0 1 0 | (0) (0) (0) DFEDFUNDS | 0 0 1 | (0) (0) (0)

Because the prior is conjugate for the data likelihood, the posterior is analytically tractable. By default, `estimate`

uses the first four observations as a presample to initialize the model.

**Generate Forecasts from Posterior Predictive Distribution**

From the posterior predictive distribution, generate forecasts over a two-year horizon. Because sampling from the posterior predictive distribution requires the entire data set, specify the prior model in `forecast`

instead of the posterior.

```
fh = 8;
rng(1); % For reproducibility
FY = forecast(PriorMdl,fh,rmDataTable{:,seriesnames});
```

`FY`

is an 8-by-3 matrix of forecasts.

Plot the end of the data set and the forecasts.

fp = rmDataTable.Time(end) + calquarters(1:fh); figure plotdata = [rmDataTable{end - 10:end,seriesnames}; FY]; plot([rmDataTable.Time(end - 10:end); fp'],plotdata) hold on plot([fp(1) fp(1)],ylim,'k-.') legend(seriesnames) title('Data and Forecasts') hold off

**Compute Impulse Responses**

Plot impulse response functions by passing posterior estimations to `armairf`

.

`armairf(PosteriorMdl.AR,[],'InnovCov',PosteriorMdl.Covariance)`

A *Bayesian VAR model* treats all coefficients and the innovations covariance matrix as random variables in the *m*-dimensional, stationary VARX(*p*) model. The model has one of the three forms described in this table.

Model | Equation |
---|---|

Reduced-form VAR(p) in difference-equation notation |
$${y}_{t}={\Phi}_{1}{y}_{t-1}+\mathrm{...}+{\Phi}_{p}{y}_{t-p}+c+\delta t+{\rm B}{x}_{t}+{\epsilon}_{t}.$$ |

Multivariate regression |
$${y}_{t}={Z}_{t}\lambda +{\epsilon}_{t}.$$ |

Matrix regression |
$${y}_{t}={\Lambda}^{\prime}{z}_{t}^{\prime}+{\epsilon}_{t}.$$ |

For each time *t* = 1,...,*T*:

*y*is the_{t}*m*-dimensional observed response vector, where*m*=`numseries`

.Φ

_{1},…,Φ_{p}are the*m*-by-*m*AR coefficient matrices of lags 1 through*p*, where*p*=`numlags`

.*c*is the*m*-by-1 vector of model constants if`IncludeConstant`

is`true`

.*δ*is the*m*-by-1 vector of linear time trend coefficients if`IncludeTrend`

is`true`

.Β is the

*m*-by-*r*matrix of regression coefficients of the*r*-by-1 vector of observed exogenous predictors*x*_{t}, where*r*=`NumPredictors`

. All predictor variables appear in each equation.$${z}_{t}=\left[\begin{array}{ccccccc}{y}_{t-1}^{\prime}& {y}_{t-2}^{\prime}& \cdots & {y}_{t-p}^{\prime}& 1& t& {x}_{t}^{\prime}\end{array}\right],$$ which is a 1-by-(

*mp*+*r*+ 2) vector, and*Z*_{t}is the*m*-by-*m*(*mp*+*r*+ 2) block diagonal matrix$$\left[\begin{array}{cccc}{z}_{t}& {0}_{z}& \cdots & {0}_{z}\\ {0}_{z}& {z}_{t}& \cdots & {0}_{z}\\ \vdots & \vdots & \ddots & \vdots \\ {0}_{z}& {0}_{z}& {0}_{z}& {z}_{t}\end{array}\right],$$

where 0

_{z}is a 1-by-(*mp*+*r*+ 2) vector of zeros.$$\Lambda ={\left[\begin{array}{ccccccc}{\Phi}_{1}& {\Phi}_{2}& \cdots & {\Phi}_{p}& c& \delta & {\rm B}\end{array}\right]}^{\prime}$$, which is an (

*mp*+*r*+ 2)-by-*m*random matrix of the coefficients, and the*m*(*mp*+*r*+ 2)-by-1 vector*λ*= vec(Λ).*ε*is an_{t}*m*-by-1 vector of random, serially uncorrelated, multivariate normal innovations with the zero vector for the mean and the*m*-by-*m*matrix Σ for the covariance. This assumption implies that the data likelihood is$$\ell \left(\Lambda ,\Sigma |y,x\right)={\displaystyle \prod _{t=1}^{T}f\left({y}_{t};\Lambda ,\Sigma ,{z}_{t}\right)},$$

where

*f*is the*m*-dimensional multivariate normal density with mean*z*_{t}Λ and covariance Σ, evaluated at*y*_{t}.

Before considering the data, you impose a *joint prior distribution* assumption on (Λ,Σ), which is governed by the distribution *π*(Λ,Σ). In a Bayesian analysis, the distribution of the parameters is updated with information about the parameters obtained from the data likelihood. The result is the *joint posterior distribution*
*π*(Λ,Σ|*Y*,*X*,*Y*_{0}), where:

*Y*is a*T*-by-*m*matrix containing the entire response series {*y*_{t}},*t*= 1,…,*T*.*X*is a*T*-by-*m*matrix containing the entire exogenous series {*x*_{t}},*t*= 1,…,*T*.*Y*_{0}is a*p*-by-*m*matrix of presample data used to initialize the VAR model for estimation.

The *normal conjugate prior model*, outlined in [1], is an *m*-D Bayesian VAR model in which the innovations covariance matrix Σ is known and fixed, while the coefficient vector *λ* = vec(Λ) has the prior distribution

$$\lambda ~{N}_{m(mp+r+{1}_{c}+{1}_{\delta})}\left(\mu ,V\right),$$

where:

*r*=`NumPredictors`

.1

_{c}is 1 if`IncludeConstant`

is true, and 0 otherwise.1

_{δ}is 1 if`IncludeTrend`

is true, and 0 otherwise.

The posterior distribution is proper and analytically tractable.

$$\lambda |{y}_{t},{x}_{t}~{N}_{m(mp)+r+{1}_{c}+{1}_{\delta}}\left(\overline{\mu},\overline{V}\right),$$

where:

$$\overline{\mu}={\left({V}^{-1}+{\displaystyle \sum _{t=1}^{T}{Z}_{t}^{\prime}{\Sigma}^{-1}{Z}_{t}}\right)}^{-1}\left({V}^{-1}\mu +{\displaystyle \sum _{t=1}^{T}{Z}_{t}^{\prime}{\Sigma}^{-1}{y}_{t}}\right).$$

$$\overline{V}={\left({V}^{-1}+{\displaystyle \sum _{t=1}^{T}{Z}_{t}^{\prime}{\Sigma}^{-1}{Z}_{t}}\right)}^{-1}.$$

[1] Litterman, Robert B. "Forecasting with Bayesian Vector Autoregressions: Five Years of Experience." *Journal of Business and Economic Statistics* 4, no. 1 (January 1986): 25–38. https://doi.org/10.2307/1391384.

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)