Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/fn_forecast.m

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