1function [Ui,Vi,n0,np,ixmC0Pres] = ftd_cholesky(lags,nvar,nexo,indxC0Pres)
2%vlist = [1:4];    % regarding "xdd", % 1: p; 2: id; 3: ik; 4: y.
3%For restricted VARs in the form: y_t'*A0 = x_t'*Ap + e_t', where y_t is a vector of endogenous variables
4%      and x_t is a vector of lagged endogenous variables and the constant term (last term).
5%      Note that the columns of A0 and Ap correspnd to equations.
6%
7%    Exporting orthonormal matrices for the deterministic linear restrictions (equation by equation)
8%    See Waggoner and Zha's Gibbs sampling paper.
9%
10% q_m:  quarter or month
11% lags: the maximum length of lag
12% nvar:  number of endogeous variables
13% nexo:  number of exogenous variables.  If nexo is not supplied, nexo=1 as default for a constant
14% indxC0Pres: index for cross-A0-A+ restrictions.  if 1: cross-A0-and-A+ restrictions; 0: idfile is all we have
15%                Example for indxOres==1: restrictions of the form P(t) = P(t-1).
16%                These restrictions have to be manually and carefully keyed in.
17%-----------------
18% Ui: nvar-by-1 cell.  In each cell, nvar-by-qi orthonormal basis for the null of the ith
19%           equation contemporaneous restriction matrix where qi is the number of free parameters.
20%           With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
21%           of total original parameters and bi is a vector of free parameters. When no
22%           restrictions are imposed, we have Ui = I.  There must be at least one free
23%           parameter left for the ith equation.
24% Vi: nvar-by-1 cell.  In each cell, k-by-ri orthonormal basis for the null of the ith
25%           equation lagged restriction matrix where k is a total of exogenous variables and
26%           ri is the number of free parameters. With this transformation, we have fi = Vi*gi
27%           or Vi'*fi = gi where fi is a vector of total original parameters and gi is a
28%           vector of free parameters. There must be at least one free parameter left for
29%           the ith equation.
30% n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation
31% np: nvar-by-1, ith element represents the number of free A+ parameters in ith equation
32% ixmC0Pres:  neq_cres-by-1 cell.  Effective only if indxC0Pres=1, otherwise equals NaN.
33%             neq_cres is the number of equations in which cross-A0-A+ restrictions occur.
34%             In the jth cell representing equation, we have 4 columns:
35%               1st: the jth column (equation) of A+ or A0: f_j or a_j
36%               2nd: the ith element f_j(i) -- the ith element in the jth column of A+
37%               3rd: the hth element a_j(h) -- the hth element in the jth column of A0
38%               4th: the number s such that f_j(i) = s * a_j(h) holds.
39%
40% Tao Zha, May 2000
41
42
43
44Ui = cell(nvar,1);  % initializing for contemporaneous endogenous variables
45Vi = cell(nvar,1);  % initializing for lagged and exogenous variables
46n0 = zeros(nvar,1); % ith element represents the number of free A0 parameters in ith equation
47np = zeros(nvar,1); % ith element represents the number of free A+ parameters in ith equation
48
49if (nargin==2)
50   nexo = 1;  % 1: constant as default where nexo must be a nonnegative integer
51elseif (nargin==3)
52   indxC0Pres = 0;  % default is no cross-A0-and-A+ restrictions.
53end
54
55k = lags*nvar+nexo;  % maximum number of lagged and exogenous variables in each equation
56
57Qi = zeros(nvar,nvar,nvar);   % for nvar contemporaneous equations
58Ri = zeros(k,k,nvar);    % for nvar lagged and exogenous equations
59  % Row corresponds to equation. 0 means no restriction.
60  %                              1 means exclusion restriction such that the corresponding parameter is restricted to 0.
61
62%nfvar = 6;   % number of foreign (Granger causing) variables
63%nhvar = nvar-nfvar;  % number of home (affected) variables.
64
65
66%-------------------------------------------------------------
67%  Beginning the manual input of the restrictions one quation at a time
68%-------------------------------------------------------------
69%The restrictions considered here are in the following form where X means unrestricted:
70%  A0 = [
71%      X  0  X  X
72%		 0  X  X  X
73%		 0  0  X  X
74%		 0  0  0  X
75%			];
76%	Ap = [
77%      X  0  X  X
78%		 0  X  X  X
79%      0  0  X  X
80%      0  0  X  X  (1st lag)
81%      X  0  X  X
82%		 0  X  X  X
83%      0  0  X  X
84%      0  0  X  X  (2nd lag)
85%      X  0  X  X
86%		 0  X  X  X
87%      0  0  X  X
88%      0  0  X  X  (3rd lag)
89%      X  0  X  X
90%		 0  X  X  X
91%      0  0  X  X
92%      0  0  X  X  (4th lag)
93%      0  X  0  0  (constant terms)
94%			];
95
96if (0)
97	%------------------------ Lower triangular A0 ------------------------------
98	%======== The first equation ===========
99
100
101	%======== The second equation ===========
102	Qi(1:1,:,2) = [
103	   1 0 0 0
104	        ];
105
106	%======== The third equation ===========
107	Qi(1:2,:,3) = [
108	   1 0 0 0
109	   0 1 0 0
110	        ];
111
112	%======== The fourth equation ===========
113	Qi(1:3,:,4) = [
114	   1 0 0 0
115	   0 1 0 0
116	   0 0 1 0
117	        ];
118else
119	%------------------------ Upper triangular A0 ------------------------------
120	%======== The first equation ===========
121	Qi(2:4,:,1) = [
122	   0 1 0 0
123	   0 0 1 0
124	   0 0 0 1
125	        ];
126
127	%======== The second equation ===========
128   Qi([1 3:4],:,2) = [
129      1 0 0 0
130      0 0 1 0
131	   0 0 0 1
132	        ];
133
134	%======== The third equation ===========
135	Qi(4:4,:,3) = [
136	   0 0 0 1
137	        ];
138
139	%======== The fourth equation ===========
140end
141
142
143%-------------------------- Lag restrictions. ------------------------------------------
144if (1)
145	%--- Lag restrictions.
146	indxeqn = 1;   %Which equation.
147	nrestrs = (nvar-1)*lags+1;  %Number of restrictions.
148	vars_restr = [2:nvar];  %Variables that are restricted:  id, ik, and y.
149   blags = zeros(nrestrs,k);  %k=nvar*lags+1
150	cnt = 0;
151	for ki = 1:lags
152	   for kj=vars_restr
153	      cnt = cnt+1;
154	      blags(cnt,nvar*(ki-1)+kj) = 1;
155	   end
156	end
157	%--- Keep constant zero.
158	cnt = cnt+1;
159	blags(cnt,end) = 1;  %Constant = 0.
160	if cnt~=nrestrs
161	   error('Check lagged restrictions in 1st equation!')
162	end
163	Ri(1:nrestrs,:,indxeqn) = blags;
164
165	%--- Lag restrictions.
166	indxeqn = 2;   %Which equation.
167   nrestrs = (nvar-1)*lags;  %Number of restrictions.
168   vars_restr = [1 3:nvar];  %Variables that are restricted:  id, ik, and y.
169   blags = zeros(nrestrs,k);  %k=nvar*lags+1
170   cnt = 0;
171   for ki = 1:lags
172      for kj=vars_restr
173         cnt = cnt+1;
174         blags(cnt,nvar*(ki-1)+kj) = 1;
175      end
176   end
177	Ri(1:nrestrs,:,indxeqn) = blags;
178
179	%--- Lag restrictions.
180	indxeqn = 3;   %Which equation.
181	nrestrs = 1;  %Number of restrictions.
182	blags = zeros(nrestrs,k);
183	cnt = 0;
184	%--- Keep constant zero.
185	cnt = cnt+1;
186	blags(cnt,end) = 1;  %Constant = 0.
187	if cnt~=nrestrs
188	   error('Check lagged restrictions in 1st equation!')
189	end
190	Ri(1:nrestrs,:,indxeqn) = blags;
191
192	%--- Lag restrictions.
193	indxeqn = 4;   %Which equation.
194	nrestrs = 1;  %Number of restrictions.
195	blags = zeros(nrestrs,k);
196	cnt = 0;
197	%--- Keep constant zero.
198	cnt = cnt+1;
199	blags(cnt,end) = 1;  %Constant = 0.
200	if cnt~=nrestrs
201	   error('Check lagged restrictions in 1st equation!')
202	end
203	Ri(1:nrestrs,:,indxeqn) = blags;
204end
205
206
207for n=1:nvar   %  initializing loop for each equation
208   Ui{n} = null(Qi(:,:,n));
209   Vi{n} = null(Ri(:,:,n));
210   n0(n) = size(Ui{n},2);
211   np(n) = size(Vi{n},2);
212end
213
214
215
216%(2)-------------------------------------------------------------
217%  Cross-A0-and-A+ rerestrictions one quation at a time
218%    i.e., the first, second, ..., kjth, ..., equation
219%  This type of restriction is used for the New-Keysian model studied by Leeper and Zha
220%    "Assessing Simple Policy Rules: A View from a Complete Macroeconomic Model" published
221%    by St. Louis Fed Review.
222%(2)-------------------------------------------------------------
223%
224if indxC0Pres
225   neq_cres = 3;   % the number of equations in which cross-A0-A+ restrictions occur.
226   ixmC0Pres = cell(neq_cres,1);  % in each cell representing equation, we have 4 columns:
227           % 1st: the jth column (equation) of A+ or A0: f_j or a_j
228           % 2nd: the ith element f_j(i) -- the ith element in the jth column of A+
229           % 3rd: the hth element a_j(h) -- the hth element in the jth column of A0
230           % 4th: the number s such that f_j(i) = s * a_j(h) holds.
231   %** 1st equation
232   ixmC0Pres{1} = [1 2 2 1
233                   1 7 1 1];
234   %** 2nd equation
235   ixmC0Pres{2} = [2 2 2 2];
236   %** 3rd equation
237   ixmC0Pres{3} = [3 7 1 1
238                   3 2 2 1];
239
240
241%         % 4 columns.
242%   ncres = 5;  % manually key in the number of cross-A0-A+ restrictions
243
244%           % 1st: the jth column (equation) of A+ or A0: f_j or a_j
245%           % 2nd: the ith element f_j(i) -- the ith element in the jth column of A+
246%           % 3rd: the hth element a_j(h) -- the hth element in the jth column of A0
247%           % 4th: the number s such that f_j(i) = s * a_j(h) holds.
248else
249   ixmC0Pres = NaN;
250end
251
252