Debian Codegraph @

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

raw
1 function [G1,C,impact,q,a,b,z,eu]=gensysct(g0,g1,c,psi,pi,div)
2 %function [G1,C,impact,q,a,b,z,eu]=gensysct(g0,g1,c,psi,pi,div)
3 %System given as
4 % g0*Dy(t)=g1*y(t)+c+psi*epsilon(t)+pi*eta(t),
5 %with epsilon an exogenous variable process and eta being endogenously determined
6 %white noise expectational errors. Returned system is
7 % Dy(t)=G1*y(t)+C+impact*epsilon(t).
8 % epsilon(t) is assumed to be white noise.
9 % If div is omitted from argument list, a div>0 is calculated.
10 % Also returned is the qz decomposition, q'az'=g0, q'bz'=g1, with a and b
11 % upper triangular and the system ordered so that all zeros on the diagonal of b are in
12 % the lower right corner, all cases where the real part of bii/aii is greater than or
13 % equal to div appear in the next block above the zeros, and the remaining bii/aii's
14 % all have bii/aii<div . These elements can be used to construct the full backward and
15 % forward solution. See the paper "Solving Linear Rational Expectations Models",
16 % http://eco-072399b.princeton.edu/yftp/gensys . Note that if one simply wants the backward
17 % and forward projection of y on epsilon, ignoring existence and uniqueness questions, the
18 % projection can be computed by Fourier methods.
19 % eu(1)=(existence); eu(2)=(uniqueness). Else eu=[-2,2] => indeterminacy via singularity
20 % in the equation system. Else eu(1)=-1 => existence only for white noise epsilon.
21 % realsmall=sqrt(eps)*10;
22 %
23 % Copyright (C) 1997-2012 Christopher A. Sims
24 %
25 % This free software: you can redistribute it and/or modify
26 % it under the terms of the GNU General Public License as published by
27 % the Free Software Foundation, either version 3 of the License, or
28 % (at your option) any later version.
29 %
30 % It is distributed in the hope that it will be useful,
31 % but WITHOUT ANY WARRANTY; without even the implied warranty of
32 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 % GNU General Public License for more details.
34 %
35 % If you did not received a copy of the GNU General Public License
36 % with this software, see <http://www.gnu.org/licenses/>.
37 %
38
39 realsmall=sqrt(eps)*10;
40 %realsmall=1e-3;
41 eu=[0;0];
42 fixdiv=(nargin==6)
43 n=size(g0,1);
44 [a b q z v]=qz(g0,g1);
45 if ~fixdiv, div=.001; end
46 nunstab=0;
47 nzero=0;
48 zxz=0;
49 for i=1:n
50 %------------------div calc------------
51 if ~fixdiv
52 if abs(a(i,i)) > realsmall
53 divhat=real(b(i,i)/a(i,i));
54 if realsmall<divhat & divhat<div
55 div=.5*divhat;
56 end
57 end
58 end
59 %----------------------------------------
60 if abs(a(i,i))<realsmall
61 nzero=nzero+1;
62 nunstab=nunstab+1;
63 if abs(b(i,i))<realsmall
64 zxz=1;
65 end
66 else
67 nunstab=nunstab+ (real(b(i,i)/a(i,i))>div);
68 end
69 end
70 div
71 nunstab
72 nzero
73 % Note that qzdivct first puts all singularities in a in lower right, then puts unstable
74 % roots on top of those.
75 [a b q z]=qzdivct(div,a,b,q,z);
76 gev=[diag(a) diag(b)];
77 if zxz
78 %disp('Coincident zeros. Indeterminacy and/or nonexistence.')
79 eu=[-2;-2];
80 return
81 end
82 q1=q(1:n-nunstab,:);
83 q2=q(n-nunstab+1:n,:);
84 z1=z(:,1:n-nunstab)';
85 z2=z(:,n-nunstab+1:n)';
86 a2=a(n-nunstab+1:n,n-nunstab+1:n);
87 b2=b(n-nunstab+1:n,n-nunstab+1:n);
88 etawt=q2*pi;
89 zwt=q2*psi;
90 [ueta,deta,veta]=svd(etawt);
91 md=min(size(deta));
92 bigev=find(diag(deta(1:md,1:md))>realsmall);
93 ueta=ueta(:,bigev);
94 veta=veta(:,bigev);
95 deta=deta(bigev,bigev);
96 [uz,dz,vz]=svd(zwt);
97 md=min(size(dz));
98 bigev=find(diag(dz(1:md,1:md))>realsmall);
99 uz=uz(:,bigev);
100 vz=vz(:,bigev);
101 dz=dz(bigev,bigev);
102 if isempty(bigev)
103 exist=1;
104 else
105 exist=norm(uz-ueta*ueta'*uz,'fro') < realsmall*n;
106 end
107 if ~isempty(bigev)
108 zwtx0=b2\zwt;
109 zwtx=zwtx0;
110 M=b2\a2;
111 M=M/norm(M);
112 for i=2:nunstab
113 zwtx=[M*zwtx zwtx0];
114 end
115 zwtx=b2*zwtx;
116 [ux,dx,vx]=svd(zwtx);
117 md=min(size(dx));
118 bigev=find(diag(dx(1:md,1:md))>realsmall);
119 ux=ux(:,bigev);
120 vx=vx(:,bigev);
121 dx=dx(bigev,bigev);
122 existx=norm(ux-ueta*ueta'*ux,'fro') < realsmall*n;
123 else
124 existx=1;
125 end
126 %----------------------------------------------------
127 % Note that existence and uniqueness are not just matters of comparing
128 % numbers of roots and numbers of endogenous errors. These counts are
129 % reported below because usually they point to the source of the problem.
130 %------------------------------------------------------
131 [ueta1,deta1,veta1]=svd(q1*pi);
132 md=min(size(deta1));
133 bigev=find(diag(deta1(1:md,1:md))>realsmall);
134 ueta1=ueta1(:,bigev);
135 veta1=veta1(:,bigev);
136 deta1=deta1(bigev,bigev);
137 if existx | nunstab==0
138 %disp('solution exists');
139 eu(1)=1;
140 else
141 if exist
142 %disp('solution exists for unforecastable z only');
143 eu(1)=-1;
144 %else
145 %fprintf(1,'No solution. %d unstable roots. %d endog errors.\n',nunstab,size(ueta1,2));
146 end
147 %disp('Generalized eigenvalues')
148 %disp(gev);
149 %md=abs(diag(a))>realsmall;
150 %ev=diag(md.*diag(a)+(1-md).*diag(b))\ev;
151 %disp(ev)
152 % return;
153 end
154 %disp('Generalized eigenvalues')
155 %disp(gev);
156 %md=abs(diag(a))>realsmall;
157 %ev=diag(md.*diag(a)+(1-md).*diag(b))\ev;
158 %disp(ev)
159 % return;
160 if isempty(veta1)
161 unique=1;
162 else
163 unique=norm(veta1-veta*veta'*veta1,'fro')<realsmall*n;
164 end
165 if unique
166 %disp('solution unique');
167 eu(2)=1;
168 %else
169 %fprintf(1,'Indeterminacy. %d loose endog errors.\n',size(veta1,2)-size(veta,2));
170 %disp('Generalized eigenvalues')
171 %disp(gev);
172 %disp(ev)
173 % return;
174 end
175 tmat = [eye(n-nunstab) -ueta1*deta1*veta1'*veta*(deta\ueta')];
176 G0= [tmat*a; zeros(nunstab,n-nunstab) eye(nunstab)];
177 G1= [tmat*b; zeros(nunstab,n)];
178 %----------------------
179 % G0 is always non-singular because by construction there are no zeros on
180 % the diagonal of a(1:n-nunstab,1:n-nunstab), which forms G0's ul corner.
181 %-----------------------
182 G0I=inv(G0);
183 G1=G0I*G1;
184 usix=n-nunstab+1:n;
185 C=G0I*[tmat*q*c;(a(usix,usix)-b(usix,usix))\q2*c];
186 impact=G0I*[tmat*q*psi;zeros(nunstab,size(psi,2))];
187 %-------------------- above are output for system in terms of z'y -------
188 G1=z*G1*z';
189 if max(max(abs(imag(G1))))>100*realsmall
190 disp('Inaccuracy in G1:')
191 s1=svd(G1);
192 s2=svd(real(G1));
193 disp(max((s1-s2)./(1/12+s1))) % this is reasonable scaling for monthly time unit
194 end
195 G1=real(G1);
196 C=real(z*C);
197 impact=real(z*impact);