# Recursive Forecast with VAR Model

10 views (last 30 days)
Claas-Christoph Seidel on 15 Sep 2020
Edited: Jon on 16 Sep 2020
Hey, I am pretty new to MatLab and have to reproduce results from a Paper.
Therefore i have to do one-step-ahead recursive forecasts 1000 times for each quarter of a Vector Autoregression Model. Number of forecast period is 20. For each Forecast, i have to add random error terms multiplied by the Choleski Decomposition to the forecasts.
Coeffs=Coefficients of the VAR Model , Cons=constants of VAR, lastobservation= last historical data of the VAR Variables
The step-by-step quarter forecasts would be then:
forecastq1=(Cons+Coeffs*lastobersavation+Choleski*randn(4,1000));
forecastq2=(cons+Coeffs*forecastq1+Choleski*randn(4,1000));
forecastq3=(cons+Coeffs*forecastq2+Choleski*randn(4,1000));
and this for 20 forecasts.
I am pretty sure, there is a more elegant way to code this, but since this is my first Matlab experience, I am not sure what it is. Do you have any ideas for a loop or function ?

Jon on 15 Sep 2020
Edited: Jon on 15 Sep 2020
You could do it in a loop something like this (I'm assuming the ith forecast value is a scalar, otherwise you may have to adjust your dimensions somewhat:
numForecasts = 20;
% preallocate array to hold results
forecastq = zeros(numForecasts,1);
% initialize first forecast
forecastq(1) = Cons + Coeffs*lastobersavation + Choleski*randn(4,1000);
% loop through quarters making forecasts
for k = 2:numForecasts
forecastq(k)= cons + Coeffs*forecastq(k-1) + Choleski*randn(4,1000);
end
If the Choelski term is not constant then maybe you need some additional code in the loop to update that term with each iteration. Also, of course you will need to assign your cons, and Coeffs before executing the code fragment that I have provided

Claas-Christoph Seidel on 16 Sep 2020
Thanks a lot! But yes, struggeling with the dimensions. Forecasted values are 20 4x1000 Matrices basically, so a 4x1000 Matrix for each forecast. In total I would need a 80x1000 Matrix for all 4 Varibles and 20 Quarters. Already tried a lot to fit dimensions, but still get "Unable to perform assignment because the left and right sides have a different number of elements" reply when trying to perform ´forecastq(1)=....´. Do you have any idea ?
This would help so much !
Thank you.
Jon on 16 Sep 2020
One way to do it would be to save your forecasted values in a 3 dimensional array 4 by 1000 by 20
To get the forecast for the kth quarter you would access is as forecastq(:,:,k) all rows, all columns, of kth quarter
Here's some example code for doing it this way (note I corrected your typo in the variable name lastobservation, so you will have to do that too to be consistent)
% script to try forecast loop
numForecasts = 20;
% initialize with random values
% just to get dimensions for this example
% you would need to do correctly this with actual values
Cons = randn(4,1000);
Coeffs = randn(4,4);
lastobservation = randn(4,1000);
Choleski = randn(4,4);
% preallocate array to hold results
forecastq = zeros(4,1000,numForecasts);
% initialize first forecast
forecastq(:,:,1) = Cons + Coeffs*lastobservation + Choleski*randn(4,1000);
% loop through quarters making forecasts
for k = 2:numForecasts
forecastq(:,:,k)= Cons + Coeffs*forecastq(:,:,k-1) + Choleski*randn(4,1000);
end