Debian Codegraph @

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

raw
1 function [G1,C,impact,fmat,fwt,ywt,gev,eu]=gensys(g0,g1,c,psi,pi,div)
2 %function [G1,C,impact,fmat,fwt,ywt,gev,eu]=gensys(g0,g1,c,psi,pi,div)
3 %System given as
4 % g0*y(t)=g1*y(t-1)+c+psi*z(t)+pi*eta(t),
5 %with z an exogenous variable process and eta being endogenously determined
6 %one-step-ahead expectational errors. Returned system is
7 % y(t)=G1*y(t-1)+C+impact*z(t)+ywt*inv(I-fmat*inv(L))*fwt*z(t+1) .
8 % If z(t) is i.i.d., the last term drops out.
9 % If div is omitted from argument list, a div>1 is calculated.
10 % eu(1)=1 for existence, eu(2)=1 for uniqueness. eu(1)=-1 for
11 % existence only with not-s.c. z; eu=[-2,-2] for coincident zeros.
12 % By Christopher A. Sims
13 % Corrected 10/28/96 by CAS
14 %
15 % Copyright (C) 1997-2012 Christopher A. Sims
16 %
17 % This free software: you can redistribute it and/or modify
18 % it under the terms of the GNU General Public License as published by
19 % the Free Software Foundation, either version 3 of the License, or
20 % (at your option) any later version.
21 %
22 % It is distributed in the hope that it will be useful,
23 % but WITHOUT ANY WARRANTY; without even the implied warranty of
24 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 % GNU General Public License for more details.
26 %
27 % If you did not received a copy of the GNU General Public License
28 % with this software, see <http://www.gnu.org/licenses/>.
29 %
30
31 eu=[0;0];
32 realsmall=1e-6;
33 fixdiv=(nargin==6);
34 n=size(g0,1);
35 [a b q z]=qz(g0,g1);
36 if ~fixdiv, div=1.01; end
37 nunstab=0;
38 zxz=0;
39 for i=1:n
40 %------------------div calc------------
41 if ~fixdiv
42 if abs(a(i,i)) > 0
43 divhat=abs(b(i,i))/abs(a(i,i));
44 if 1+realsmall<divhat & divhat<div
45 div=.5*(1+divhat);
46 end
47 end
48 end
49 %----------------------------------------
50 nunstab=nunstab+(abs(b(i,i))>div*abs(a(i,i)));
51 if abs(a(i,i))<realsmall & abs(b(i,i))<realsmall
52 zxz=1;
53 end
54 end
55 if ~zxz
56 [a b q z]=qzdiv(div,a,b,q,z);
57 end
58 save d:\mex\gensysmkl\abqz.mat a b q z div
59 gev=[diag(a) diag(b)];
60 if zxz
61 %disp('Coincident zeros. Indeterminacy and/or nonexistence.')
62 eu=[-2;-2];
63 return
64 end
65 q1=q(1:n-nunstab,:);
66 q2=q(n-nunstab+1:n,:);
67 a2=a(n-nunstab+1:n,n-nunstab+1:n);
68 b2=b(n-nunstab+1:n,n-nunstab+1:n);
69 etawt=q2*pi;
70 zwt=q2*psi;
71 [ueta,deta,veta]=svd(etawt);
72 md=min(size(deta));
73 bigev=find(diag(deta(1:md,1:md))>realsmall);
74 ueta=ueta(:,bigev);
75 veta=veta(:,bigev);
76 deta=deta(bigev,bigev);
77 [uz,dz,vz]=svd(zwt);
78 md=min(size(dz));
79 bigev=find(diag(dz(1:md,1:md))>realsmall);
80 uz=uz(:,bigev);
81 vz=vz(:,bigev);
82 dz=dz(bigev,bigev);
83 if isempty(bigev)
84 exist=1;
85 existx=1;
86 else
87 exist=norm(uz-ueta*ueta'*uz) < realsmall*n;
88 zwtx0=b2\zwt;
89 zwtx=zwtx0;
90 M=b2\a2;
91 for i=2:nunstab
92 zwtx=[M*zwtx zwtx0];
93 end
94 zwtx=b2*zwtx;
95 [ux,dx,vx]=svd(zwtx);
96 md=min(size(dx));
97 bigev=find(diag(dx(1:md,1:md))>realsmall);
98 ux=ux(:,bigev);
99 vx=vx(:,bigev);
100 dx=dx(bigev,bigev);
101 existx=norm(ux-ueta*ueta'*ux) < realsmall*n;
102 end
103 %----------------------------------------------------
104 % Note that existence and uniqueness are not just matters of comparing
105 % numbers of roots and numbers of endogenous errors. These counts are
106 % reported below because usually they point to the source of the problem.
107 %------------------------------------------------------
108 [ueta1,deta1,veta1]=svd(q1*pi);
109 md=min(size(deta1));
110 bigev=find(diag(deta1(1:md,1:md))>realsmall);
111 ueta1=ueta1(:,bigev);
112 veta1=veta1(:,bigev);
113 deta1=deta1(bigev,bigev);
114 if existx | nunstab==0
115 %disp('solution exists');
116 eu(1)=1;
117 else
118 if exist
119 %disp('solution exists for unforecastable z only');
120 eu(1)=-1;
121 %else
122 %fprintf(1,'No solution. %d unstable roots. %d endog errors.\n',nunstab,size(ueta1,2));
123 end
124 %disp('Generalized eigenvalues')
125 %disp(gev);
126 %md=abs(diag(a))>realsmall;
127 %ev=diag(md.*diag(a)+(1-md).*diag(b))\ev;
128 %disp(ev)
129 % return;
130 end
131 if isempty(veta1)
132 unique=1;
133 else
134 unique=norm(veta1-veta*veta'*veta1)<realsmall*n;
135 end
136 if unique
137 %disp('solution unique');
138 eu(2)=1;
139 else
140 fprintf(1,'Indeterminacy. %d loose endog errors.\n',size(veta1,2)-size(veta,2));
141 %disp('Generalized eigenvalues')
142 %disp(gev);
143 %md=abs(diag(a))>realsmall;
144 %ev=diag(md.*diag(a)+(1-md).*diag(b))\ev;
145 %disp(ev)
146 % return;
147 end
148 tmat = [eye(n-nunstab) -(ueta*(deta\veta')*veta1*deta1*ueta1')'];
149 G0= [tmat*a; zeros(nunstab,n-nunstab) eye(nunstab)];
150 G1= [tmat*b; zeros(nunstab,n)];
151 %----------------------
152 % G0 is always non-singular because by construction there are no zeros on
153 % the diagonal of a(1:n-nunstab,1:n-nunstab), which forms G0's ul corner.
154 %-----------------------
155 G0I=inv(G0);
156 G1=G0I*G1;
157 usix=n-nunstab+1:n;
158 C=G0I*[tmat*q*c;(a(usix,usix)-b(usix,usix))\q2*c];
159 impact=G0I*[tmat*q*psi;zeros(nunstab,size(psi,2))];
160 fmat=b(usix,usix)\a(usix,usix);
161 fwt=-b(usix,usix)\q2*psi;
162 ywt=G0I(:,usix);
163 %-------------------- above are output for system in terms of z'y -------
164 G1=real(z*G1*z');
165 C=real(z*C);
166 impact=real(z*impact);
167 % Correction 10/28/96: formerly line below had real(z*ywt) on rhs, an error.
168 ywt=z*ywt;