Debian Codegraph @

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

raw
1 function [yforelml,yforemgml,yforeqgml,yforeCalygml] = fore_cal(yfore,xdata,nvar,...
2 nSample,nSampleCal,forep,forepq,forepy,q_m,qmEnd,vlist,vlistlog,...
3 vlistper,lmqyIndx)
4 % Converting oringinal forecast "yfore" to series by calendar years and series
5 % with growth rates (annualized for monthly and quarterly series).
6 %
7 %function [yforelml,yforemgml,yforeqgml,yforeCalygml] = fore_cal(yfore,xdata,nvar,...
8 % nSample,nSampleCal,forep,forepq,forepy,q_m,qmEnd,vlist,vlistlog,...
9 % vlistper,mqyIndx)
10 %
11 % yfore: oringal forecast series, all logged except R, U, etc.
12 % xdata: oringal data set beyond the sample into forecast horizon
13 % until yrFin:qmFin, all logged except R, U, etc.
14 % nvar: number of variables
15 % nSample: sample size (including lags or initial periods)
16 % nSampleCal: sample size in terms of calendar years
17 % forep: forecast periods (monthly)
18 % forepq: forecast periods (quarterly)
19 % forepy: forecast periods (yearly)
20 % q_m: quarterly or monthly for the underlying model
21 % qmEnd: last q_m before out-of-sample forecasting
22 % vlist: a list of variables
23 % vlistlog: sub list of variables that are in log
24 % vlistper: sub list of variables that are in percent
25 % lmyqIndx: 4-by-1 1 or 0's. Index for level, mg, qg, and yg; 1: yes; 0: no
26 %----------------
27 % yforelml: monthly level forecast (in percent for R, U, etc.) with the same size as "yfore" in log
28 % yforemgml: monthly growth (at annual rates), in percent
29 % yforeqgml: ML forecast: quarterly growth (at annual rates), in percent
30 % yforeCalygml: ML forecast: annual growth (by calendar years), in percent
31 % forepy-by-nvar
32 %
33 % Written by Tao Zha March 1998
34 % Revision, October 1998. Added lmyqIndx so previous programs may not be compatible.
35 %
36 % Copyright (C) 1997-2012 Tao Zha
37 %
38 % This free software: you can redistribute it and/or modify
39 % it under the terms of the GNU General Public License as published by
40 % the Free Software Foundation, either version 3 of the License, or
41 % (at your option) any later version.
42 %
43 % It is distributed in the hope that it will be useful,
44 % but WITHOUT ANY WARRANTY; without even the implied warranty of
45 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 % GNU General Public License for more details.
47 %
48 % If you did not received a copy of the GNU General Public License
49 % with this software, see <http://www.gnu.org/licenses/>.
50 %
51
52
53 %=================================================
54 % Making everything presentable at FOMC
55 %=================================================
56 %
57 %%%%
58 %$$$ Monthly, prior quarter, and year-over-year change
59 %$$$ Out-of-sample forecasts
60 %%%%
61
62
63 if length(lmqyIndx)~=4
64 warning('lmqyIndx must be a 4-by-1 vector containing 1 or 0')
65 return
66 end
67
68
69
70 %---------------------
71 % Actual data
72 %---------------------
73 yact = xdata(nSample-q_m+1:nSample,:); % the latest (not calender) year
74 yactCal = xdata(nSampleCal-q_m+1:nSampleCal,:); % the lastest calendar year
75
76
77
78 %-----------------------------------------
79 % Converted to monthly level
80 %-----------------------------------------
81 if lmqyIndx(1)
82 yforelml=yfore;
83 yforelml(:,vlistlog)=exp(yfore(:,vlistlog)); % mode, all levels
84 yforelml(:,vlistper)=100*yfore(:,vlistper); % mode, all levels
85 else
86 yforelml = NaN;
87 end
88
89
90
91 %-----------------------------------------
92 % Converted to monthly growth
93 %-----------------------------------------
94 if lmqyIndx(2)
95 yactm = zeros(1,length(vlist)); % the latest month prior to forecasting
96 yforem = zeros(1+forep,length(vlist)); % including the 1 month prior to forecasting
97 %
98 yactm = yact(length(yact(:,1)),:); % last month prior to forecasting
99 yforem(1,:) = yactm; % last month prior to forecasting
100 yforem(2:forep+1,:) = yfore(1:forep,:); % monthly forecasts, all logged.
101 %
102 %@@ monthly growth rate (annualized)
103 yforemg = yforem(2:forep+1,:);
104 yforemg(:,vlistlog) = ...
105 ( yforem(2:forep+1,vlistlog) - yforem(1:forep,vlistlog) ) .* q_m;
106 % monthly change (annualized), 12*log(1+growth rate)
107 yforemgml=yforemg;
108 yforemgml(:,vlistlog) = 100*(exp(yforemg(:,vlistlog))-1);
109 % monthly growth rate (annualized)
110 yforemgml(:,vlistper) = 100*yforemg(:,vlistper); % monthly growth rate (annualized)
111 else
112 yforemgml=NaN;
113 end
114
115
116
117 %-----------------------------------------
118 % Converted to quarterly
119 %-----------------------------------------
120 if lmqyIndx(3)
121 yactQ = xdata(nSample-mod(qmEnd,3)-q_m+1:nSample-mod(qmEnd,3),:);
122 % the latest actual quarter
123 yactq = zeros(4,length(vlist)); % the latest 4 quarters prior to forecasting
124 yforeq = zeros(4+forepq,length(vlist));
125 % including the 4 quarters prior to forecasting
126 %
127 qT1 = length(yactQ(:,1))/3;
128 if qT1 ~= 4
129 error('Must hae 4 actual quarters to compute quarterly change for forecasting!')
130 end
131 for i = 1:qT1
132 i1 = 1+3*(i-1);
133 i2 = 3*i;
134 yactq(i,:) = sum(yact(i1:i2,:)) ./ 3;
135 end
136 yforeq(1:4,:) = yactq;
137 %
138 yforeq_1 = sum(xdata( nSample-mod(qmEnd,3)+1:nSample,:),1);
139 yforeq(5,:) = ( yforeq_1 + sum(yfore(1:3-mod(qmEnd,3),:),1) ) ./ 3;
140 % 1st quarterly forecast which may contain actual data within quarter
141 % note, dimension "1" in sum(ytem,1) is necessary because when
142 % qmEnd=1, sum(ytem,1) will give us a nomber, not a vector. But this
143 % is not what we want.
144 for i = 2:forepq
145 i1 = 3-mod(qmEnd,3) + 1+3*(i-2);
146 i2 = 3-mod(qmEnd,3) + 3*(i-1);
147 yforeq(4+i,:) = sum(yfore(i1:i2,:)) ./ 3;
148 end
149 %
150 %@@ prior quarter growth rate (annualized)
151 yforeqg = yforeq(5:forepq+4,:);
152 %yforeqg(:,vlistlog) = (100*(yforeq(5:qT2+4,vlistlog)-yforeq(1:qT2,vlistlog))) ...
153 % ./ yforeq(1:qT2,vlistlog); % year-over-year
154 %yforeqg(:,vlistlog) = 100*((yforeq(5:qT2+4,vlistlog)./yforeq(4:qT2+3,vlistlog)).^4 - 1 );
155 % prior quarter
156 yforeqg(:,vlistlog) = ...
157 ( yforeq(5:forepq+4,vlistlog) - yforeq(4:forepq+3,vlistlog) ) .* 4;
158 % prior quarter, 4*log(1+growth rate)
159 yforeqgml=yforeqg;
160 yforeqgml(:,vlistlog) = 100*(exp(yforeqg(:,vlistlog))-1);
161 % quarterly growth rate (annualized)
162 yforeqgml(:,vlistper) = 100*yforeqg(:,vlistper);
163 % quarterly growth rate (annualized)
164 else
165 yforeqgml = NaN;
166 end
167
168
169
170 %-----------------------------------------
171 % Converted to calendar years
172 %-----------------------------------------
173 if lmqyIndx(4)
174 yactCaly = zeros(1,length(vlist)); % the latest calendar year
175 yforeCaly = zeros(1+forepy,length(vlist));
176 % including the calendar year prior to forecasting
177 %
178 yT1 = length(yactCal(:,1))/q_m;
179 if yT1 ~= 1
180 error('yT1 Beginings or ends of monthly and calendar series are not the same!')
181 end
182 for i = 1:yT1
183 i1 = 1+q_m*(i-1);
184 i2 = q_m*i;
185 yactCaly(i,:) = sum(yactCal(i1:i2,:)) ./ q_m;
186 end
187 yforeCaly(1,:) = yactCaly;
188 %
189 %@@ initial monthly actual data for calendar years
190 if qmEnd == q_m
191 yforeCaly_1 = 0;
192 else
193 ytem = xdata(nSampleCal+1:nSampleCal+qmEnd,:);
194 %ytem(:,vlistlog) = exp(ytem(:,vlistlog));
195 yforeCaly_1 = sum(ytem,1);
196 % note, dimension "1" in sum(ytem,1) is necessary because when
197 % qmEnd=1, sum(ytem,1) will give us a nomber, not a vector. But this
198 % is not what we want.
199 end
200 %
201 if qmEnd == q_m
202 for i = 1:forepy
203 i1 = 1+q_m*(i-1);
204 i2 = q_m*i;
205 yforeCaly(1+i,:) = sum(yfore(i1:i2,:)) ./ q_m;
206 end
207 else
208 yforeCaly(2,:) = (yforeCaly_1+sum(yfore(1:q_m-qmEnd,:),1)) ./ q_m;
209 % note, dimension "1" in sum(yfore,1) is necessary because when
210 % q_m-qmEnd=1, sum(yfore) will give us a number, not a vector. But this
211 % is not what we want.
212 for i = 2:forepy
213 i1 = q_m-qmEnd+1+q_m*(i-2);
214 i2 = q_m-qmEnd+q_m*(i-1);
215 yforeCaly(1+i,:) = sum(yfore(i1:i2,:)) ./ q_m;
216 end
217 end
218 %
219 %@@ year-over-year growth rate
220 yforeCalyg = yforeCaly(2:forepy+1,:);
221 yforeCalyg(:,vlistlog) = ...
222 yforeCaly(2:forepy+1,vlistlog) - yforeCaly(1:forepy,vlistlog);
223 % year-over-year, log(1+growth rate)
224 yforeCalygml=yforeCalyg;
225 yforeCalygml(:,vlistlog) = 100*(exp(yforeCalyg(:,vlistlog))-1);
226 % annaul growth rate
227 yforeCalygml(:,vlistper) = 100*yforeCalyg(:,vlistper); % annaul growth rate
228 else
229 yforeCalygml = NaN;
230 end
231
232
233
234 %ndraws
235 %
236 % posterior mean of out-of-sample forecasts
237 %yfore1 = (1.0/ndraws)*yfore1; % mean
238 %yforeqg1 = (1.0/ndraws)*yforeqg1; % mean
239 %yforeCalyg1 = (1.0/ndraws)*yforeCalyg1; % mean
240 %
241 %@@@ correlation of inflation and U
242 %IUcov = zeros(forepq,1); % forepq quarters
243 %IUbeta = zeros(forepq,2); % forepq quarters
244 %for i = 1:forepq
245 % indI = 4*forepq+i;
246 % indU = 5*forepq+i;
247 % %junk = corrcoef(yforeqgw(:,indI),yforeqgw(:,indU));
248 % %IUcov(i,1) = junk(1,2);
249 % xjnk = [100*yforeqgw(:,indU) ones(ndraws,1)];
250 % yjnk = yforeqgw(:,indI);
251 % junk = (xjnk'*xjnk)\(xjnk'*yjnk);
252 % IUbeta(i,:) = junk';
253 %end
254
255
256 %%
257 % *** .68 and .90 probability bands of out-of-sample forecasts
258 %yforel = zeros(forep*nvar,1); % preallocating
259 %yforeh = zeros(forep*nvar,1); % preallocating
260 %yforeqgl = zeros(forepq*nvar,1); % preallocating
261 %yforeqgh = zeros(forepq*nvar,1); % preallocating
262
263 %clear yfores yforepgs yforeCalygs
264 %*** write out final results
265 %yforeml = reshape(yforeml,forep,nvar);
266 %yforeqgml = reshape(yforeqgml,forepq,nvar);
267 %yforeCalygml = reshape(yforeCalygml,forepy,nvar);