Debian Codegraph @

tests/ms-sbvar/archive-files/ftd_2s_caseall_upperchol4v.m

raw
1 function [Ui,Vi,n0,np] = ftd_2s_caseall_upperchol4v(lags,nvar,nStates,indxEqnTv_m,nexo)
2 % Case 2&3: Policy a0j and a+j have only time-varying structural variances -- Case 2.
3 % All policy and nonpolicy a0j's and the corresponding constant terms are completely time-varying and only the scale
4 % of each variable in d+j,1** (excluding the constant term) is time-varying -- Case 3.
5 %
6 % Variables: Pcom, M2, FFR, y, P, U. Equations: information, policy, money demand, y, P, U.
7 % Exporting orthonormal matrices for the deterministic linear restrictions
8 % (equation by equation) with time-varying A0 and D+** equations.
9 % See Model II.3 on pp.71k-71r in TVBVAR NOTES (and Waggoner and Zha's Gibbs sampling paper and TVBVAR NOTES p.58).
10 %
11 % lags: Maximum length of lag.
12 % nvar: Number of endogeous variables.
13 % nStates: Number of states.
14 % indxEqnTv_m: nvar-by-2. Stores equation characteristics.
15 % 1st column: labels of equations [1:nvar]'.
16 % 2nd column: labels of time-varying features with
17 % 1: indxConst -- all coefficients are constant,
18 % 2: indxStv -- only shocks are time-varying,
19 % 3: indxTva0pv -- a0 are freely time-varying and each variable i for d+ is time-varying only by the scale lambda_i(s_t).
20 % 4: indxTva0ps -- a0 are freely time-varying and only the scale for the whole of d+ is time-varying.
21 % 5: indxTv -- time-varying for all coeffficients (a0 and a+) where the lag length for a+ may be shorter.
22 % nexo: number of exogenous variables. If nexo is not supplied, nexo=1 as default for a constant.
23 % So far this function is written to handle one exogenous variable, which is a constant term.
24 %-----------------
25 % Ui: nvar-by-1 cell. In each cell, nvar*nStates-by-qi*si orthonormal basis for the null of the ith
26 % equation contemporaneous restriction matrix where qi is the number of free parameters
27 % within the state and si is the number of free states.
28 % With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
29 % of total original parameters and bi is a vector of free parameters. When no
30 % restrictions are imposed, we have Ui = I. There must be at least one free
31 % parameter left for the ith equation in the order of [a_i for 1st state, ..., a_i for last state].
32 % Vi: nvar-by-1 cell. In each cell, k*nStates-by-ri*si orthonormal basis for the null of the ith
33 % equation lagged restriction matrix where k is a total of exogenous variables and
34 % ri is the number of free parameters within the state and si is the number of free states.
35 % With this transformation, we have fi = Vi*gi or Vi'*fi = gi where fi is a vector of total original
36 % parameters and gi is a vector of free parameters. The ith equation is in the order of [nvar variables
37 % for 1st lag and 1st state, ..., nvar variables for last lag and 1st state, const for 1st state, nvar
38 % variables for 1st lag and 2nd state, nvar variables for last lag and 2nd state, const for 2nd state, and so on].
39 % n0: nvar-by-1, whose ith element represents the number of free A0 parameters in ith equation in *all states*.
40 % np: nvar-by-1, whose ith element represents the number of free D+ parameters in ith equation in *all states*.
41 %
42 % Tao Zha, February 2003
43
44
45
46 Ui = cell(nvar,1); % initializing for contemporaneous endogenous variables
47 Vi = cell(nvar,1); % initializing for lagged and exogenous variables
48 n0 = zeros(nvar,1); % ith element represents the number of free A0 parameters in ith equation in all states.
49 np = zeros(nvar,1); % ith element represents the number of free D+ parameters in ith equation in all states.
50
51 if (nargin==3)
52 nexo = 1; % 1: constant as default where nexo must be a nonnegative integer
53 end
54
55
56 n = nvar*nStates;
57 kvar=lags*nvar+nexo; % Maximum number of lagged and exogenous variables in each equation under each state.
58 k = kvar*nStates; % Maximum number of lagged and exogenous variables in each equation in all states.
59
60 Qi = zeros(n,n,nvar); % 3rd dim: nvar contemporaneous equations.
61 Ri = zeros(k,k,nvar); % 1st and 2nd dims: lagged and exogenous equations.
62 % Row corresponds to equation with nvar variables for state 1, ..., nvar variables for state nState.
63 % 0 means no restriction.
64 % 1 and -1 or any other number means the linear combination of the corresponding parameters is restricted to 0.
65 % 1 (only 1) means that the corresponding parameter is restricted to 0.
66
67 %nfvar = 6; % number of foreign (Granger causing) variables
68 %nhvar = nvar-nfvar; % number of home (affected) variables.
69
70
71 %-------------------------------------------------------------
72 % Beginning the manual input of the restrictions one quation at a time for A0_s.
73 %-------------------------------------------------------------
74 %
75
76 %======== The first equation ===========
77 eqninx = 1;
78 nreseqn = 3; % Number of linear restrictions for A0(:,eqninx) for each state.
79 if (indxEqnTv_m(eqninx, 2)<=2)
80 %**** For constant A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
81 Qi(1:(nStates-1)*nvar+nreseqn,:,eqninx) = [
82 1 0 0 0 -1 0 0 0
83 0 1 0 0 0 -1 0 0
84 0 0 1 0 0 0 -1 0
85 0 0 0 1 0 0 0 -1
86
87 0 0 0 0 0 1 0 0
88 0 0 0 0 0 0 1 0
89 0 0 0 0 0 0 0 1
90 ];
91 %**** For constant D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
92 for si=1:nStates-1
93 for ki=1:kvar
94 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
95 end
96 end
97 else % Time-varying equations at least for A0_s. For D+_s, constant-parameter equations in general.
98 %**** For time-varying A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
99 Qi(1:nreseqn*nStates,:,eqninx) = [
100 0 1 0 0 0 0 0 0
101 0 0 1 0 0 0 0 0
102 0 0 0 1 0 0 0 0
103
104 0 0 0 0 0 1 0 0
105 0 0 0 0 0 0 1 0
106 0 0 0 0 0 0 0 1
107 ];
108
109 %**** For D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
110 if (indxEqnTv_m(eqninx, 2)==3) % For constant D+** except the constant term. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
111 for si=1:nStates-1
112 for ki=1:kvar-1 % -1: no restrictions on the constant term, which is freely time-varying.
113 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
114 end
115 end
116 elseif (indxEqnTv_m(eqninx, 2)==4) % For constant D+**. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
117 for si=1:nStates-1
118 for ki=1:kvar
119 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
120 end
121 end
122 else
123 error('.../ftd_2s_caseall_simszha5v.m: Have not got time to deal with the simple case indxEqnTv_m(eqninx, 2)=5.')
124 end
125 end
126
127
128 %======== The second equation ===========
129 eqninx = 2;
130 nreseqn = 2; % Number of linear restrictions for A0(:,eqninx) for each state.
131 if (indxEqnTv_m(eqninx, 2)<=2)
132 %**** For constant A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
133 Qi(1:(nStates-1)*nvar+nreseqn,:,eqninx) = [
134 1 0 0 0 -1 0 0 0
135 0 1 0 0 0 -1 0 0
136 0 0 1 0 0 0 -1 0
137 0 0 0 1 0 0 0 -1
138
139 0 0 0 0 0 0 1 0
140 0 0 0 0 0 0 0 1
141 ];
142 %**** For constant D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
143 for si=1:nStates-1
144 for ki=1:kvar
145 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
146 end
147 end
148 else % Time-varying equations at least for A0_s. For D+_s, constant-parameter equations in general.
149 %**** For time-varying A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
150 Qi(1:nreseqn*nStates,:,eqninx) = [
151 0 0 1 0 0 0 0 0
152 0 0 0 1 0 0 0 0
153
154 0 0 0 0 0 0 1 0
155 0 0 0 0 0 0 0 1
156 ];
157
158 %**** For D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
159 if (indxEqnTv_m(eqninx, 2)==3) % For constant D+** except the constant term. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
160 for si=1:nStates-1
161 for ki=1:kvar-1 % -1: no restrictions on the constant term, which is freely time-varying.
162 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
163 end
164 end
165 elseif (indxEqnTv_m(eqninx, 2)==4) % For constant D+**. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
166 for si=1:nStates-1
167 for ki=1:kvar
168 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
169 end
170 end
171 else
172 error('.../ftd_3s_case3a.m: Have not got time to deal with the simple case indxEqnTv_m(eqninx, 2)=5.')
173 end
174
175 %==== For freely time-varying A+ for only the first 6 lags.
176 %==== Lagged restrictions: zeros on all lags except the first 6 lags in the MS equation.
177 % nlagsno0 = 6; % Number of lags to be nonzero.
178 % for si=1:nStates
179 % for ki = 1:lags-nlagsno0
180 % for kj=1:nvar
181 % Ri(kvar*(si-1)+nlagsno0*nvar+nvar*(ki-1)+kj,kvar*(si-1)+nlagsno0*nvar+nvar*(ki-1)+kj,2) = 1;
182 % end
183 % end
184 % end
185 %**** For constant D+_s except the first two lags and the constant term. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
186 % for si=1:nStates-1
187 % for ki=[2*nvar+1:kvar-1]
188 % Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
189 % end
190 % end
191 end
192
193
194 %======== The third equation ===========
195 eqninx = 3;
196 nreseqn = 1; % Number of linear restrictions for the equation for each state.
197 if (indxEqnTv_m(eqninx, 2)<=2)
198 %**** For constant A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
199 Qi(1:(nStates-1)*nvar+nreseqn,:,eqninx) = [
200 1 0 0 0 -1 0 0 0
201 0 1 0 0 0 -1 0 0
202 0 0 1 0 0 0 -1 0
203 0 0 0 1 0 0 0 -1
204
205 0 0 0 0 0 0 0 1
206 ];
207 %**** For constant D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
208 for si=1:nStates-1
209 for ki=1:kvar
210 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
211 end
212 end
213 else % Time-varying equations at least for A0_s. For D+_s, constant-parameter equations in general.
214 %**** For time-varying A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
215 Qi(1:nreseqn*nStates,:,eqninx) = [
216 0 0 0 1 0 0 0 0
217
218 0 0 0 0 0 0 0 1
219 ];
220 %**** For D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
221 if (indxEqnTv_m(eqninx, 2)==3) % For constant D+** except the constant term. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
222 for si=1:nStates-1
223 for ki=1:kvar-1 % -1: no restrictions on the constant term, which is freely time-varying.
224 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
225 end
226 end
227 elseif (indxEqnTv_m(eqninx, 2)==4) % For constant D+**. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
228 for si=1:nStates-1
229 for ki=1:kvar
230 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
231 end
232 end
233 else
234 error('.../ftd_2s_caseall_simszha5v.m: Have not got time to deal with the simple case indxEqnTv_m(eqninx, 2)=5.')
235 end
236 end
237
238
239 %======== The fourth equation ===========
240 eqninx = 4;
241 nreseqn = 0; % Number of linear restrictions for the equation for each state.
242 if (indxEqnTv_m(eqninx, 2)<=2)
243 %**** For constant A0_s. In the order of [a0j(1),...,a0j(nStates)] for the 2nd dim of Qi.
244 Qi(1:(nStates-1)*nvar+nreseqn,:,eqninx) = [
245 1 0 0 0 -1 0 0 0
246 0 1 0 0 0 -1 0 0
247 0 0 1 0 0 0 -1 0
248 0 0 0 1 0 0 0 -1
249 ];
250 %**** For constant D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
251 for si=1:nStates-1
252 for ki=1:kvar
253 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
254 end
255 end
256 else % Time-varying equations at least for A0_s. For D+_s, constant-parameter equations in general.
257 %**** For D+_s. In the order of [aj+(1),...,aj+(nStates)] for the 2nd dim of Ri.
258 if (indxEqnTv_m(eqninx, 2)==3) % For constant D+** except the constant term. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
259 for si=1:nStates-1
260 for ki=1:kvar-1 % -1: no restrictions on the constant term, which is freely time-varying.
261 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
262 end
263 end
264 elseif (indxEqnTv_m(eqninx, 2)==4) % For constant D+**. In the order of [dj**(1),...,dj**(nStates)] for the 2nd dim of Ri.
265 for si=1:nStates-1
266 for ki=1:kvar
267 Ri(kvar*(si-1)+ki,[kvar*(si-1)+ki si*kvar+ki],eqninx) = [1 -1];
268 end
269 end
270 else
271 error('.../ftd_2s_caseall_simszha5v.m: Have not got time to deal with the simple case indxEqnTv_m(eqninx, 2)=5.')
272 end
273 end
274
275
276
277
278
279
280 %===== Lagged restrictions in foreign (Granger causing) block
281 %nfbres = lags*(nvar-nfvar); % number of block restrictions in each foreign equation
282 %bfor = zeros(nfbres,k); % each foreign equation
283 %cnt=0;
284 %for ki = 1:lags
285 % for kj=1:nvar-nfvar
286 % cnt=cnt+1;
287 % bfor(cnt,nvar*(ki-1)+nfvar+kj) = 1;
288 % end
289 %end
290 %%
291 %if cnt~=nfbres
292 % error('Check lagged restrictions in foreign equations!')
293 %end
294 %%
295 %for kj=1:nfvar
296 % Ri(1:nfbres,:,kj) = bfor;
297 %end
298
299
300 %===== Lagged restrictions in home (affected) block
301 %
302 %~~~~~ selected domestic equations
303 %dlrindx = nfvar+1; %[nfvar+1 nfvar+2]; % index for relevant home equations
304 %rfvindx = []; %[6]; %[1 2 3 5]; % index for restricted foreign variables (e.g., Poil, M2, FFR, P).
305 %%nf2hvar = nfvar-length(rfvindx); % number of free parameters -- foreign variables entering the home sector
306 %nhbres = lags*length(rfvindx); % number of block restrictions in each home equation
307 %bhom = zeros(nhbres,k); % each home equation
308 %cnt=0;
309 %for ki = 1:lags
310 % for kj=1:length(rfvindx)
311 % cnt=cnt+1;
312 % bhom(cnt,nvar*(ki-1)+rfvindx(kj)) = 1;
313 % end
314 %end
315 %%
316 %if cnt~=nhbres
317 % error('Check lagged restrictions in domestic equations!')
318 %end
319 %%
320 %for kj=dlrindx
321 % Ri(1:nhbres,:,kj) = bhom;
322 %end
323
324
325
326 for ki=1:nvar % initializing loop for each equation
327 Ui{ki} = null(Qi(:,:,ki));
328 Vi{ki} = null(Ri(:,:,ki));
329 n0(ki) = size(Ui{ki},2);
330 np(ki) = size(Vi{ki},2);
331 end