| 1 |
function yhat = fn_forecastsim(Bh,A0h,phi,nn,nexo,Xfexo) |
| 2 |
% yhat = fn_forecastsim(Bh,A0h,phi,nn,nexo,Xfexo) |
| 3 |
% Unconditional forecasts with simulated shocks, not depending on reduced-form or structural shocks. |
| 4 |
% y_hat(t+h) = c + x_hat(t+h-1)*Bh + u, X: 1*k; Bh: k*nvar; y_hat: 1*nvar; u: simulated shocks. |
| 5 |
% |
| 6 |
% Bh: k-by-nvar, the (posterior) estimate of B; |
| 7 |
% A0h: nvar-by-nvar, columns correponding to equations |
| 8 |
% phi: the 1-by-(nvar*lags+1) data matrix where k=nvar*lags+1 |
| 9 |
% (last period plus lags before the beginning of forecast). |
| 10 |
% nn: [nvar,lags,nfqm], nfqm: forecast periods (months or quarters). |
| 11 |
% nexo: number of exogenous variables. The constant term is the default setting. |
| 12 |
% Besides this term, we have nexo-1 exogenous variables. |
| 13 |
% Xfexo: nfqm-by-nexo-1 vector of exoenous variables in the forecast horizon where |
| 14 |
% nfqm: number of forecast periods. |
| 15 |
%----------------- |
| 16 |
% yhat: nfqm*nvar matrix of forecasts. |
| 17 |
% |
| 18 |
% See fn_forecast.m without shocks and forecasterr.m when A0h_in (instead of A0h) is used; |
| 19 |
% fn_forecaststre.m. |
| 20 |
% |
| 21 |
% Copyright (C) 1997-2012 Tao Zha |
| 22 |
% |
| 23 |
% This free software: you can redistribute it and/or modify |
| 24 |
% it under the terms of the GNU General Public License as published by |
| 25 |
% the Free Software Foundation, either version 3 of the License, or |
| 26 |
% (at your option) any later version. |
| 27 |
% |
| 28 |
% It is distributed in the hope that it will be useful, |
| 29 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 |
% GNU General Public License for more details. |
| 32 |
% |
| 33 |
% If you did not received a copy of the GNU General Public License |
| 34 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 35 |
% |
| 36 |
|
| 37 |
|
| 38 |
if nargin == 4 |
| 39 |
nexo=1; % default for constant term |
| 40 |
elseif nexo<1 |
| 41 |
error('We need at least one exogenous term so nexo must >= 1') |
| 42 |
end |
| 43 |
|
| 44 |
% ** setup |
| 45 |
nvar = nn(1); |
| 46 |
lags = nn(2); |
| 47 |
nfqm = nn(3); |
| 48 |
tcwx = nvar*lags; % total coefficeint without exogenous variables |
| 49 |
|
| 50 |
if nexo>1 |
| 51 |
if (nfqm > size(Xfexo,1)) |
| 52 |
disp(' ') |
| 53 |
warning('Make sure the forecast horizon in the exogenous variable matrix Xfexo > forecast periods') |
| 54 |
disp('Press ctrl-c to abort') |
| 55 |
pause |
| 56 |
elseif ((nexo-1) ~= size(Xfexo,2)) |
| 57 |
disp(' ') |
| 58 |
warning('Make sure that nexo matchs the exogenous variable matrix Xfexo') |
| 59 |
disp('Press ctrl-c to abort') |
| 60 |
pause |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
% ** reconstruct x(t) for y(t+h) = x(t+h-1)*B |
| 65 |
% ** where phi = x(t+h-1) with last column being constant |
| 66 |
Ures = randn(nfqm,nvar)/A0h; % Unconditional forecast |
| 67 |
% Now, nfqm-by-nvar -- ready for forecasts |
| 68 |
% Ures: reduced-form residuals. Row--steps; Column--n shocks |
| 69 |
yhat = zeros(nfqm,nvar); |
| 70 |
for k=1:nfqm |
| 71 |
yhat(k,:) = phi*Bh + Ures(k,:);; |
| 72 |
%*** lagged endogenous variables |
| 73 |
phi(1,nvar+1:tcwx) = phi(1,1:tcwx-nvar); |
| 74 |
phi(1,1:nvar) = yhat(k,:); |
| 75 |
%*** exogenous variables excluding constant terms |
| 76 |
if (nexo>1) |
| 77 |
phi(1,tcwx+1:tcwx+nexo-1) = Xfexo(k,:); |
| 78 |
end |
| 79 |
end |