Debian Codegraph @

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

raw
1 function [HDratio,HDvalue,yhatn,yforen] = history2(HDindx,A0,Bhml,phil,nn,...
2 actup,Estrexa,xdata,nvar,nSample,nSampleCal,forep,forepq,forepy,q_m,...
3 qmEnd,vlist,vlistlog,vlistper,lmqyIndx)
4 % [HDratio,HDvalue,yhatn,yforen] = history2(HDindx,A0,Bhml,phil,nn,actup,...
5 % xdata,nvar,nSample,nSampleCal,forep,forepq,forepy,q_m,...
6 % qmEnd,vlist,vlistlog,vlistper,lmqyIndx)
7 %
8 % (Out-of-sample) historical decompostions: alternative approach to history.m in 3 aspects
9 % (1) compute the percentage (not the value itself)
10 % (2) NOT cumulative HD (THIS is NOT correct, TAZ, 03/17/99)
11 % (3) conditional on time t (not time 0).
12 % (4) condtional and uncondtional forecasts
13 %
14 % HDindx: k-by-1 cell where k groups of shocks; index number for particular shocks
15 % A0h: nvar-by-nvar for y(t)A0 = X*A+ + E, where column means equation
16 % Bh: the (posterior) estimate of B for y(t) = X*Bh + E*inv(A0)
17 % phil: the 1-by-(nvar*lags+1) data matrix where k=nvar*lags+1
18 % (last period plus lags before the beginning of forecast)
19 % nn: [nvar,lags,forep], forep: forecast periods (monthly)
20 % actup: actual data periods (monthly) before the beginning of forecasts
21 % xdata: oringal data set, up to the period before (peudo-) out-of-sample forecasting,
22 % all logged except R, U, etc.
23 % Estrexa: forep-by-nvar -- backed out structural shocks for out-of-sample forecast
24 % nvar: number of variables
25 % nSample: sample size (including lags or initial periods)
26 % nSampleCal: sample size in terms of calendar years
27 % forep: forecast periods (monthly)
28 % forepq: forecast periods (quarterly)
29 % forepy: forecast periods (yearly)
30 % q_m: quarterly or monthly for the underlying model
31 % qmEnd: last q_m before out-of-sample forecasting
32 % vlist: a list of variables
33 % vlistlog: sub list of variables that are in log
34 % vlistper: sub list of variables that are in percent
35 % lmyqIndx: 4-by-1 1 or 0's. Index for m log, m level, qg, and yg; 1: yes; 0: no;
36 % if lmyqIndx(1)==1, both monthly log and monthly level are returned
37 %--------------
38 % HDratio: 5-by-k cells, where k groups (see HDindx) of shocks associated with decomposition;
39 % 5: monthly log, monthly level, mg, qg, and calendar yg in this order;
40 % each cell is forep(q)(y)-by-nvar
41 % HDvalue: same dimension as HDratio but with the values (differences between
42 % conditional and unconditional forecasts
43 % yhatn: same dimension as HDratio but with conditional forecasts
44 % yforen: 5-by-1 cells and each cell is forep(q)(y)-by-nvar unconditional forecasts
45 %
46 % October 1998 by Tao Zha.
47 % Last change 3/19/99 on the dimension of "Estrexa" so that previous programs may not be
48 % compatible.
49 %
50 % Copyright (C) 1997-2012 Tao Zha
51 %
52 % This free software: you can redistribute it and/or modify
53 % it under the terms of the GNU General Public License as published by
54 % the Free Software Foundation, either version 3 of the License, or
55 % (at your option) any later version.
56 %
57 % It is distributed in the hope that it will be useful,
58 % but WITHOUT ANY WARRANTY; without even the implied warranty of
59 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 % GNU General Public License for more details.
61 %
62 % If you did not received a copy of the GNU General Public License
63 % with this software, see <http://www.gnu.org/licenses/>.
64 %
65
66 nyout = 1+nargout('fore_cal');
67 % +1 because we want the original logged variables as well.
68 [nHD,jnk] = size(HDindx);
69 yhat=cell(nHD,1);
70 yhatn = cell(nyout,nHD);
71 % row: output such as l, mg, qg, or yg; column: how many decomps
72
73
74
75 %--------------------------------------------------------------------------
76 % Unconditional point forecasts for log, monthly levle, mg, qg, and yg.
77 %--------------------------------------------------------------------------
78 yforeml = forecast(Bhml,phil,nn);
79 yforen = cell(nyout,1);
80 yforen{1} = yforeml;
81 oputstr = '['; % output string
82 for n=2:nyout
83 if n<nyout
84 oputstr = [oputstr 'yforen{' num2str(n) '},'];
85 else
86 oputstr = [oputstr 'yforen{' num2str(n) '}'];
87 end
88 end
89 oputstr =[oputstr ']'];
90 %
91 eval([oputstr ' = fore_cal(yforeml,xdata,nvar,nSample,nSampleCal,forep,'...
92 'forepq,forepy,q_m,qmEnd,vlist,vlistlog,vlistper,lmqyIndx);']);
93
94
95
96 %--------------------------------------------------------------------------
97 % Conditional forecasts on specified paths of shocks
98 % for log, monthly levle, mg, qg, and yg.
99 %--------------------------------------------------------------------------
100 for k=1:nHD
101 Estr = zeros(forep,nvar); % out of sample
102 Estr(:,HDindx{k}) = Estrexa(:,HDindx{k}); % out of sample, MS
103 yhat{k} = forefixe(A0,Bhml,phil,nn,Estr);
104 yhatn{1,k} = yhat{k}; % original logged variables
105 %
106 oputstr = '['; % output string
107 for n=2:nyout
108 if n<nyout
109 oputstr = [oputstr 'yhatn{' num2str(n) ',k},'];
110 else
111 oputstr = [oputstr 'yhatn{' num2str(n) ',k}'];
112 end
113 end
114 oputstr =[oputstr ']'];
115 %
116 eval([oputstr ' = fore_cal(yhat{k},xdata,nvar,nSample,nSampleCal,forep,'...
117 'forepq,forepy,q_m,qmEnd,vlist,vlistlog,vlistper,lmqyIndx);']);
118 end
119
120
121
122 %--------------------------------------------------------------------------
123 % Historical decompositions: both values and raitos (%)
124 %--------------------------------------------------------------------------
125 HDratio=cell(nyout,nHD);
126 HDvalue=HDratio;
127 for n=2:nyout
128 if lmqyIndx(n-1)
129 if (n-1==1)
130 yhatotal = zeros(size(yhatn{1,k}));
131 for k=1:nHD
132 HDvalue{1,k} = yhatn{1,k}-yforen{1};
133 yhatotal = yhatotal + abs(HDvalue{1,k});
134 % a sum of absolute values. One can also use square
135 end
136 %
137 for k=1:nHD
138 HDratio{1,k} = abs(HDvalue{1,k})*100 ./ yhatotal;
139 end
140 end
141 %
142 yhatotal = zeros(size(yhatn{n,k}));
143 for k=1:nHD
144 HDvalue{n,k} = yhatn{n,k}-yforen{n};
145 yhatotal = yhatotal + abs(HDvalue{n,k});
146 % a sum of absolute values. One can also use square
147 end
148 %
149 for k=1:nHD
150 HDratio{n,k} = abs(HDvalue{n,k})*100 ./ yhatotal;
151 end
152 end
153 end
154
155
156 %** check. This first may not be zeros, but the second (monthly log) must be zeros.
157 %HDvalue{5,1}+HDvalue{5,2}-...
158 % (yactCalyg(size(yactCalyg,1)-3:size(yactCalyg,1),:)-yforeCalygml)
159 %HDvalue{1,1}+HDvalue{1,2}-...
160 % (yact(size(yact,1)-forep+1:size(yact,1),:)-yforen{1,1})