| 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) 1996-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 v]=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 |
% bug detected by Vasco Curdia and Daria Finocchiaro, 2/25/2004 A root of |
| 45 |
% exactly 1.01 and no root between 1 and 1.02, led to div being stuck at 1.01 |
| 46 |
% and the 1.01 root being misclassified as stable. Changing < to <= below fixes this. |
| 47 |
if 1+realsmall<divhat & divhat<=div |
| 48 |
div=.5*(1+divhat); |
| 49 |
end |
| 50 |
end |
| 51 |
end |
| 52 |
% ---------------------------------------- |
| 53 |
nunstab=nunstab+(abs(b(i,i))>div*abs(a(i,i))); |
| 54 |
if abs(a(i,i))<realsmall & abs(b(i,i))<realsmall |
| 55 |
zxz=1; |
| 56 |
end |
| 57 |
end |
| 58 |
div ; |
| 59 |
nunstab; |
| 60 |
if ~zxz |
| 61 |
[a b q z]=qzdiv(div,a,b,q,z); |
| 62 |
end |
| 63 |
gev=[diag(a) diag(b)]; |
| 64 |
if zxz |
| 65 |
disp('Coincident zeros. Indeterminacy and/or nonexistence.') |
| 66 |
eu=[-2;-2]; |
| 67 |
% correction added 7/29/2003. Otherwise the failure to set output |
| 68 |
% arguments leads to an error message and no output (including eu). |
| 69 |
G1=[];C=[];impact=[];fmat=[];fwt=[];ywt=[];gev=[]; |
| 70 |
return |
| 71 |
end |
| 72 |
q1=q(1:n-nunstab,:); |
| 73 |
q2=q(n-nunstab+1:n,:); |
| 74 |
z1=z(:,1:n-nunstab)'; |
| 75 |
z2=z(:,n-nunstab+1:n)'; |
| 76 |
a2=a(n-nunstab+1:n,n-nunstab+1:n); |
| 77 |
b2=b(n-nunstab+1:n,n-nunstab+1:n); |
| 78 |
etawt=q2*pi; |
| 79 |
% zwt=q2*psi; |
| 80 |
[ueta,deta,veta]=svd(etawt); |
| 81 |
md=min(size(deta)); |
| 82 |
bigev=find(diag(deta(1:md,1:md))>realsmall); |
| 83 |
ueta=ueta(:,bigev); |
| 84 |
veta=veta(:,bigev); |
| 85 |
deta=deta(bigev,bigev); |
| 86 |
% ------ corrected code, 3/10/04 |
| 87 |
eu(1) = length(bigev)>=nunstab; |
| 88 |
% ------ Code below allowed "existence" in cases where the initial lagged state was free to take on values |
| 89 |
% ------ inconsistent with existence, so long as the state could w.p.1 remain consistent with a stable solution |
| 90 |
% ------ if its initial lagged value was consistent with a stable solution. This is a mistake, though perhaps there |
| 91 |
% ------ are situations where we would like to know that this "existence for restricted initial state" situation holds. |
| 92 |
%% [uz,dz,vz]=svd(zwt); |
| 93 |
%% md=min(size(dz)); |
| 94 |
%% bigev=find(diag(dz(1:md,1:md))>realsmall); |
| 95 |
%% uz=uz(:,bigev); |
| 96 |
%% vz=vz(:,bigev); |
| 97 |
%% dz=dz(bigev,bigev); |
| 98 |
%% if isempty(bigev) |
| 99 |
%% exist=1; |
| 100 |
%% else |
| 101 |
%% exist=norm(uz-ueta*ueta'*uz) < realsmall*n; |
| 102 |
%% end |
| 103 |
%% if ~isempty(bigev) |
| 104 |
%% zwtx0=b2\zwt; |
| 105 |
%% zwtx=zwtx0; |
| 106 |
%% M=b2\a2; |
| 107 |
%% for i=2:nunstab |
| 108 |
%% zwtx=[M*zwtx zwtx0]; |
| 109 |
%% end |
| 110 |
%% zwtx=b2*zwtx; |
| 111 |
%% [ux,dx,vx]=svd(zwtx); |
| 112 |
%% md=min(size(dx)); |
| 113 |
%% bigev=find(diag(dx(1:md,1:md))>realsmall); |
| 114 |
%% ux=ux(:,bigev); |
| 115 |
%% vx=vx(:,bigev); |
| 116 |
%% dx=dx(bigev,bigev); |
| 117 |
%% existx=norm(ux-ueta*ueta'*ux) < realsmall*n; |
| 118 |
%% else |
| 119 |
%% existx=1; |
| 120 |
%% end |
| 121 |
% ---------------------------------------------------- |
| 122 |
% Note that existence and uniqueness are not just matters of comparing |
| 123 |
% numbers of roots and numbers of endogenous errors. These counts are |
| 124 |
% reported below because usually they point to the source of the problem. |
| 125 |
% ------------------------------------------------------ |
| 126 |
[ueta1,deta1,veta1]=svd(q1*pi); |
| 127 |
md=min(size(deta1)); |
| 128 |
bigev=find(diag(deta1(1:md,1:md))>realsmall); |
| 129 |
ueta1=ueta1(:,bigev); |
| 130 |
veta1=veta1(:,bigev); |
| 131 |
deta1=deta1(bigev,bigev); |
| 132 |
%% if existx | nunstab==0 |
| 133 |
%% %disp('solution exists'); |
| 134 |
%% eu(1)=1; |
| 135 |
%% else |
| 136 |
%% if exist |
| 137 |
%% %disp('solution exists for unforecastable z only'); |
| 138 |
%% eu(1)=-1; |
| 139 |
%% %else |
| 140 |
%% %fprintf(1,'No solution. %d unstable roots. %d endog errors.\n',nunstab,size(ueta1,2)); |
| 141 |
%% end |
| 142 |
%% %disp('Generalized eigenvalues') |
| 143 |
%% %disp(gev); |
| 144 |
%% %md=abs(diag(a))>realsmall; |
| 145 |
%% %ev=diag(md.*diag(a)+(1-md).*diag(b))\ev; |
| 146 |
%% %disp(ev) |
| 147 |
%% % return; |
| 148 |
%% end |
| 149 |
if isempty(veta1) |
| 150 |
unique=1; |
| 151 |
else |
| 152 |
unique=norm(veta1-veta*veta'*veta1)<realsmall*n; |
| 153 |
end |
| 154 |
if unique |
| 155 |
%disp('solution unique'); |
| 156 |
eu(2)=1; |
| 157 |
else |
| 158 |
fprintf(1,'Indeterminacy. %d loose endog errors.\n',size(veta1,2)-size(veta,2)); |
| 159 |
%disp('Generalized eigenvalues') |
| 160 |
%disp(gev); |
| 161 |
%md=abs(diag(a))>realsmall; |
| 162 |
%ev=diag(md.*diag(a)+(1-md).*diag(b))\ev; |
| 163 |
%disp(ev) |
| 164 |
% return; |
| 165 |
end |
| 166 |
tmat = [eye(n-nunstab) -(ueta*(deta\veta')*veta1*deta1*ueta1')']; |
| 167 |
G0= [tmat*a; zeros(nunstab,n-nunstab) eye(nunstab)]; |
| 168 |
G1= [tmat*b; zeros(nunstab,n)]; |
| 169 |
% ---------------------- |
| 170 |
% G0 is always non-singular because by construction there are no zeros on |
| 171 |
% the diagonal of a(1:n-nunstab,1:n-nunstab), which forms G0's ul corner. |
| 172 |
% ----------------------- |
| 173 |
G0I=inv(G0); |
| 174 |
G1=G0I*G1; |
| 175 |
usix=n-nunstab+1:n; |
| 176 |
C=G0I*[tmat*q*c;(a(usix,usix)-b(usix,usix))\q2*c]; |
| 177 |
impact=G0I*[tmat*q*psi;zeros(nunstab,size(psi,2))]; |
| 178 |
fmat=b(usix,usix)\a(usix,usix); |
| 179 |
fwt=-b(usix,usix)\q2*psi; |
| 180 |
ywt=G0I(:,usix); |
| 181 |
% -------------------- above are output for system in terms of z'y ------- |
| 182 |
G1=real(z*G1*z'); |
| 183 |
C=real(z*C); |
| 184 |
impact=real(z*impact); |
| 185 |
% Correction 10/28/96: formerly line below had real(z*ywt) on rhs, an error. |
| 186 |
ywt=z*ywt; |