0001 function [G1pi,C,impact,nmat,TT1,TT2,gev,eu, DD, E2, E5, GAMMA, FL_RANK ]=PI_gensys(a0,a1,a2,a3,c,PSI,NX,NETA,FL_RANK,M_,options_)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 lastwarn('','');
0038 global lq_instruments;
0039 eu=[0;0];C=c;
0040 realsmall=1e-6;
0041 fixdiv=(nargin==6);
0042 n=size(a0,1);
0043 DD=[];E2=[]; E5=0; GAMMA=[];
0044
0045
0046
0047 [U0,S0,V0] = svd(a0);
0048
0049 FL_RANK=rank(S0);
0050 U01=U0(1:n,1:FL_RANK);
0051 U02=U0(1:n,FL_RANK+1:n);
0052 V01=V0(1:n,1:FL_RANK);
0053 V02=V0(1:n,FL_RANK+1:n);
0054 S01=S0(1:FL_RANK,1:FL_RANK);
0055
0056 C1=U02'*a1*V01;
0057 C2=U02'*a1*V02;
0058 C3=U02'*a2*V01;
0059 C4=U02'*a2*V02;
0060 C5=U02'*PSI;
0061 Sinv=eye(FL_RANK);
0062 for i=1:FL_RANK
0063 Sinv(i,i)=1/S01(i,i);
0064 end
0065 F1=Sinv*U01'*a1*V01;
0066 F2=Sinv*U01'*a1*V02;
0067 F3=Sinv*U01'*a2*V01;
0068 F4=Sinv*U01'*a2*V02;
0069 F5=Sinv*U01'*PSI;
0070 singular=0;
0071 warning('', '');
0072 try
0073 if rcond(C2) < 1e-8
0074 singular=1;
0075 else
0076 warning('off','MATLAB:nearlySingularMatrix');
0077 warning('off','MATLAB:singularMatrix');
0078 UAVinv=inv(C2);
0079 [LastWarningTxt LastWarningID]=lastwarn;
0080 if any(any(isinf(UAVinv)))==1
0081 singular=1;
0082 end
0083 end
0084 if singular == 1 || strcmp('MATLAB:nearlySingularMatrix',LastWarningID) == 1 || ...
0085 strcmp('MATLAB:illConditionedMatrix',LastWarningID)==1 || ...
0086 strcmp('MATLAB:singularMatrix',LastWarningID)==1
0087 [C1,C2,C3,C4, C5, F1, F2, F3, F4, F5, M1, M2, UAVinv, FL_RANK, V01, V02] = PI_gensys_singularC(C1,C2,C3,C4, C5, F1, F2, F3, F4, F5, V01, V02, 0);
0088 end
0089 warning('on','MATLAB:singularMatrix');
0090 warning('on','MATLAB:nearlySingularMatrix');
0091 if (any(any(isinf(UAVinv))) || any(any(isnan(UAVinv))))
0092 if(options_.ACES_solver==1)
0093 disp('ERROR! saving PI_gensys_data_dump');
0094 save PI_gensys_data_dump
0095 error('PI_gensys: Inversion of poss. zero matrix UAVinv=inv(U02''*a1*V02)!');
0096 else
0097 warning('PI_gensys: Evading inversion of zero matrix UAVinv=inv(U02''*a1*V02)!');
0098 eu=[0,0];
0099 return;
0100 end
0101 end
0102 catch
0103 errmsg=lasterror;
0104 warning(['error callig PI_gensys_singularC: ' errmsg.message ],'errmsg.identifier');
0105
0106 end
0107
0108
0109
0110 TT1=V02;
0111 TT2=V01;
0112
0113
0114
0115
0116
0117
0118 FF=F2;
0119 G11=UAVinv*C4;
0120 G12=UAVinv*C3;
0121 G13=-UAVinv*C1;
0122 G31=-FF*G11+F4;
0123 G32=-FF*G12+F3;
0124 G33=-FF*G13-F1;
0125 P1=UAVinv*C5;
0126 P3=-FF*P1+F5;
0127 G21=zeros(FL_RANK,(n-FL_RANK));
0128 G22=zeros(FL_RANK,FL_RANK);
0129 G23=eye(FL_RANK);
0130
0131 num_inst=0;
0132
0133
0134 Ze11=zeros(NX,NX);
0135 Ze12=zeros(NX,(n-FL_RANK));
0136 Ze134=zeros(NX,FL_RANK);
0137 Ze31=zeros(FL_RANK,NX);
0138
0139
0140
0141
0142
0143
0144
0145 G1pi=[Ze11 Ze12 Ze134 Ze134; P1 G11 G12 G13; Ze31 G21 G22 G23; P3 G31 G32 G33];
0146
0147 impact=[eye(NX,NX); zeros(n+FL_RANK,NX)];
0148
0149 if(options_.ACES_solver==1)
0150 if isfield(lq_instruments,'names')
0151 num_inst=size(lq_instruments.names,1);
0152 if num_inst>0
0153 i_var=lq_instruments.inst_var_indices;
0154 N1=UAVinv*U02'*lq_instruments.B1;
0155 N3=-FF*N1+Sinv*U01'*lq_instruments.B1;
0156 else
0157 error('WARNING: There are no instrumnets for ACES!');
0158 end
0159 lq_instruments.N1=N1;
0160 lq_instruments.N3=N3;
0161 else
0162 error('WARNING: There are no instrumnets for ACES!');
0163 end
0164 E3=V02*[P1 G11 G12 G13];
0165 E3= E3+ [zeros(size(V01,1),size(E3,2)-size(V01,2)) V01];
0166 E2=-E3;
0167 E5=-V02*N1;
0168 DD=[zeros(NX,size(N1,2));N1; zeros(FL_RANK,size(N1,2));N3];
0169 II=eye(num_inst);
0170 GAMMA=[ E3 -E5
0171 zeros(num_inst,size(E3,2)), II;
0172 ];
0173 eu =[1; 1], nmat=[], gev=[];
0174 return;
0175 end
0176
0177 G0pi=eye(n+FL_RANK+NX);
0178 try
0179
0180
0181
0182
0183 if exist('OCTAVE_VERSION')
0184 [a b q z]=qzcomplex(G0pi,G1pi);
0185 q=q';
0186 else
0187 [a b q z]=qz(G0pi,G1pi);
0188 end
0189 catch
0190 try
0191 lerror=lasterror;
0192 disp(['PI_Gensys: ' lerror.message]);
0193 if 0==strcmp('MATLAB:qz:matrixWithNaNInf',lerror.identifier)
0194 disp '** Unexpected Error PI_Gensys:qz: ** :';
0195 button=questdlg('Continue Y/N?','Unexpected Error in qz','No','Yes','Yes');
0196 switch button
0197 case 'No'
0198 error ('Terminated')
0199
0200
0201 end
0202 end
0203 G1pi=[];impact=[];nmat=[]; gev=[];
0204 eu=[-2;-2];
0205 return
0206 catch
0207 disp '** Unexpected Error in qz ** :';
0208 disp lerror.message;
0209 button=questdlg('Continue Y/N?','Unexpected Error in qz','No','Yes','Yes');
0210 switch button
0211 case 'No'
0212 error ('Terminated')
0213 case 'Yes'
0214 G1pi=[];impact=[];nmat=[]; gev=[];
0215 eu=[-2;-2];
0216 return
0217 end
0218 end
0219 end
0220
0221 if ~fixdiv, div=1.01; end
0222 nunstab=0;
0223 zxz=0;
0224 nn=size(a,1);
0225 for i=1:nn
0226
0227 if ~fixdiv
0228 if abs(a(i,i)) > 0
0229 divhat=abs(b(i,i))/abs(a(i,i));
0230
0231
0232
0233 if 1+realsmall<divhat && divhat<=div
0234 div=.5*(1+divhat);
0235 end
0236 end
0237 end
0238
0239 nunstab=nunstab+(abs(b(i,i))>div*abs(a(i,i)));
0240 if abs(a(i,i))<realsmall && abs(b(i,i))<realsmall
0241 zxz=1;
0242 end
0243 end
0244 div ;
0245 if ~zxz
0246 [a b q z]=qzdiv(div,a,b,q,z);
0247 end
0248
0249 gev=[diag(a) diag(b)];
0250 if zxz
0251 disp('Coincident zeros. Indeterminacy and/or nonexistence.')
0252 eu=[-2;-2];
0253
0254
0255 nmat=[];
0256 return
0257 end
0258 if (FL_RANK ~= nunstab && options_.ACES_solver~=1)
0259 disp(['Number of unstable variables ' num2str(nunstab)]);
0260 disp( ['does not match number of expectational equations ' num2str(FL_RANK)]);
0261 nmat=[];
0262 eu=[-2;-2];
0263 return
0264 end
0265
0266
0267 z1=z(:,1:n+NX)';
0268 z2=z(:,n+NX+1:n+NX+FL_RANK)';
0269
0270
0271 z12=z2(:,1:n+NX);
0272 z22=z2(:,n+NX+1:n+NX+FL_RANK);
0273
0274
0275
0276 nmat=real(inv(z22)*z12);
0277 eu=[1;1];