Recursive Forecast with VAR Model
8 views (last 30 days)
Show older comments
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 ?
Thank you in Advance
0 Comments
Answers (1)
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
2 Comments
Jon
on 16 Sep 2020
Edited: 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
See Also
Categories
Find more on Autocorrelated and Heteroscedastic Disturbances in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!