0001 function [dr,info,M_,options_,oo_] = dr1_PI(dr,task,M_,options_,oo_)
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
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 global lq_instruments;
0061 info = 0;
0062
0063 options_ = set_default_option(options_,'qz_criterium',1.000001);
0064
0065 xlen = M_.maximum_endo_lead + M_.maximum_endo_lag + 1;
0066
0067 if (options_.aim_solver == 1)
0068 options_.aim_solver == 0;
0069 warning('You can not use AIM with Part Info solver. AIM ignored');
0070 end
0071 if (options_.order > 1)
0072 warning('You can not use order higher than 1 with Part Info solver. Order 1 assumed');
0073 options_.order =1;
0074 end
0075
0076
0077 if options_.ramsey_policy && options_.ACES_solver == 0
0078 if isfield(M_,'orig_model')
0079 orig_model = M_.orig_model;
0080 M_.endo_nbr = orig_model.endo_nbr;
0081 M_.endo_names = orig_model.endo_names;
0082 M_.lead_lag_incidence = orig_model.lead_lag_incidence;
0083 M_.maximum_lead = orig_model.maximum_lead;
0084 M_.maximum_endo_lead = orig_model.maximum_endo_lead;
0085 M_.maximum_lag = orig_model.maximum_lag;
0086 M_.maximum_endo_lag = orig_model.maximum_endo_lag;
0087 end
0088 old_solve_algo = options_.solve_algo;
0089
0090 oo_.steady_state = dynare_solve('ramsey_static',oo_.steady_state,0,M_,options_,oo_,it_);
0091 options_.solve_algo = old_solve_algo;
0092 [junk,junk,multbar] = ramsey_static(oo_.steady_state,M_,options_,oo_,it_);
0093 [jacobia_,M_] = ramsey_dynamic(oo_.steady_state,multbar,M_,options_,oo_,it_);
0094 klen = M_.maximum_lag + M_.maximum_lead + 1;
0095 dr.ys = [oo_.steady_state;zeros(M_.exo_nbr,1);multbar];
0096 else
0097 klen = M_.maximum_lag + M_.maximum_lead + 1;
0098 iyv = M_.lead_lag_incidence';
0099 iyv = iyv(:);
0100 iyr0 = find(iyv) ;
0101 it_ = M_.maximum_lag + 1 ;
0102
0103 if M_.exo_nbr == 0
0104 oo_.exo_steady_state = [] ;
0105 end
0106
0107
0108 if options_.ACES_solver == 1
0109 sim_ruleids=[];
0110 tct_ruleids=[];
0111 if size(M_.equations_tags,1)>0
0112 for teq=1:size(M_.equations_tags,1)
0113 if strcmp(M_.equations_tags(teq,3),'aceslq_sim_rule')
0114 sim_ruleids=[sim_ruleids cell2mat(M_.equations_tags(teq,1))]
0115 end
0116 if strcmp(M_.equations_tags(teq,3),'aceslq_tct_rule')
0117 tct_ruleids=[tct_ruleids cell2mat(M_.equations_tags(teq,1))]
0118 end
0119 end
0120 end
0121 lq_instruments.sim_ruleids=sim_ruleids;
0122 lq_instruments.tct_ruleids=tct_ruleids;
0123
0124 [junk, lq_instruments.xsopt_SS,lq_instruments.lmopt_SS,s2,check] = opt_steady_get;
0125 [qc, DYN_Q] = QPsolve(lq_instruments, s2, check);
0126 z = repmat(lq_instruments.xsopt_SS,1,klen);
0127 else
0128 z = repmat(dr.ys,1,klen);
0129 end
0130 z = z(iyr0) ;
0131 [junk,jacobia_] = feval([M_.fname '_dynamic'],z,[oo_.exo_simul ...
0132 oo_.exo_det_simul], M_.params, dr.ys, it_);
0133
0134 if options_.ACES_solver==1 && (length(sim_ruleids)>0 || length(tct_ruleids)>0 )
0135 if length(sim_ruleids)>0
0136 sim_rule=jacobia_(sim_ruleids,:);
0137
0138 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_sim_rule.txt'], 'sim_rule', '-ascii', '-double', '-tabs');
0139 end
0140 if length(tct_ruleids)>0
0141 tct_rule=jacobia_(tct_ruleids,:);
0142
0143 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_tct_rule.txt'], 'tct_rule', '-ascii', '-double', '-tabs');
0144 end
0145 aces_ruleids=union(tct_ruleids,sim_ruleids);
0146 j_size=size(jacobia_,1);
0147 j_rows=1:j_size;
0148 j_rows = setxor(j_rows,aces_ruleids);
0149 jacobia_=jacobia_(j_rows ,:);
0150 end
0151
0152 end
0153
0154 if options_.debug
0155 save([M_.fname '_debug.mat'],'jacobia_')
0156 end
0157
0158 dr=set_state_space(dr,M_);
0159 kstate = dr.kstate;
0160 kad = dr.kad;
0161 kae = dr.kae;
0162 nstatic = dr.nstatic;
0163 nfwrd = dr.nfwrd;
0164 npred = dr.npred;
0165 nboth = dr.nboth;
0166 order_var = dr.order_var;
0167 nd = size(kstate,1);
0168 nz = nnz(M_.lead_lag_incidence);
0169
0170 sdyn = M_.endo_nbr - nstatic;
0171
0172 k0 = M_.lead_lag_incidence(M_.maximum_endo_lag+1,order_var);
0173 k1 = M_.lead_lag_incidence(find([1:klen] ~= M_.maximum_endo_lag+1),:);
0174
0175 if (options_.aim_solver == 1)
0176 error('Anderson and Moore AIM solver is not compatible with Partial Information models');
0177 end
0178
0179
0180
0181
0182 nendo=M_.endo_nbr;
0183
0184
0185 if(options_.ACES_solver==1)
0186
0187 if isfield(options_,'instruments')
0188 lq_instruments.names=options_.instruments;
0189 end
0190
0191 if isfield(lq_instruments,'names')
0192 num_inst=size(lq_instruments.names,1);
0193 if ~isfield(lq_instruments,'inst_var_indices') && num_inst>0
0194 for i=1:num_inst
0195 i_tmp = strmatch(deblank(lq_instruments.names(i,:)),M_.endo_names,'exact');
0196 if isempty(i_tmp)
0197 error (['One of the specified instrument variables does not exist']) ;
0198 else
0199 i_var(i) = i_tmp;
0200 end
0201 end
0202 lq_instruments.inst_var_indices=i_var;
0203 elseif size(lq_instruments.inst_var_indices)>0
0204 i_var=lq_instruments.inst_var_indices;
0205 if ~num_inst
0206 num_inst=size(lq_instruments.inst_var_indices);
0207 end
0208 else
0209 i_var=[];
0210 num_inst=0;
0211 end
0212 if size(i_var,2)>0 && size(i_var,2)==num_inst
0213 m_var=zeros(nendo,1);
0214 for i=1:nendo
0215 if isempty(find(i_var==i))
0216 m_var(i)=i;
0217 end
0218 end
0219 m_var=nonzeros(m_var);
0220 lq_instruments.m_var=m_var;
0221 else
0222 error('WARNING: There are no instrumnets for ACES!');
0223 end
0224 else
0225 error('WARNING: There are no instrumnets for ACES!');
0226 end
0227 end
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 jlen=dr.nspred+dr.nsfwrd+M_.endo_nbr+M_.exo_nbr;
0246 PSI=-jacobia_(:, jlen-M_.exo_nbr+1:end);
0247
0248 lead_lag=M_.lead_lag_incidence';
0249 max_lead_lag=zeros(nendo,2);
0250 if ( M_.maximum_lag <= 1) && (M_.maximum_lead <= 1)
0251 xlen=size(jacobia_,1);
0252 AA0=zeros(xlen,xlen);
0253 AA2=AA0;
0254 AA3=AA0;
0255 if xlen==nendo
0256 AA1=jacobia_(:,npred+1:npred+nendo);
0257 if M_.maximum_lead ==1
0258 fnd = find(lead_lag(:,M_.maximum_lag+2));
0259 AA0(:, fnd)= jacobia_(:,nonzeros(lead_lag(:,M_.maximum_lag+2)));
0260 end
0261 if npred>0 && M_.maximum_lag ==1
0262 fnd = find(lead_lag(:,1));
0263 AA2(:, fnd)= jacobia_(:,nonzeros(lead_lag(:,1)));
0264 end
0265 elseif options_.ACES_solver==1
0266 if nendo-xlen==num_inst
0267 PSI=[PSI;zeros(num_inst, M_.exo_nbr)];
0268
0269 AA_all=jacobia_(:,npred+1:npred+nendo);
0270 AA1=AA_all(:,lq_instruments.m_var);
0271 lq_instruments.ij1=AA_all(:,lq_instruments.inst_var_indices);
0272 lq_instruments.B1=-[lq_instruments.ij1; eye(num_inst)];
0273 AA1=[AA1, zeros(xlen,num_inst); zeros(num_inst,xlen), eye(num_inst)];
0274
0275 if M_.maximum_lead ==1
0276 AA_all(:,:)=0.0;
0277 fnd = find(lead_lag(:,M_.maximum_lag+2));
0278 AA_all(:, fnd)= jacobia_(:,nonzeros(lead_lag(:,M_.maximum_lag+2)));
0279 AA0=AA_all(:,lq_instruments.m_var);
0280 lq_instruments.ij0=AA_all(:,lq_instruments.inst_var_indices);
0281 lq_instruments.B0=[lq_instruments.ij0; eye(num_inst)];
0282 AA0=[AA0, zeros(xlen,num_inst); zeros(num_inst,xlen+num_inst)];
0283 end
0284 if npred>0 && M_.maximum_lag ==1
0285 AA_all(:,:)=0.0;
0286 fnd = find(lead_lag(:,1));
0287 AA_all(:, fnd)= jacobia_(:,nonzeros(lead_lag(:,1)));
0288 AA2=AA_all(:,lq_instruments.m_var);
0289 lq_instruments.ij2=AA_all(:,lq_instruments.inst_var_indices);
0290 lq_instruments.B2=[lq_instruments.ij2; eye(num_inst)];
0291 AA2=[AA2, lq_instruments.ij2 ; zeros(num_inst,xlen+num_inst)];
0292 end
0293 else
0294 error('ACES number of instruments does match');
0295 end
0296 else
0297 error('More than one lead or lag in the jabian');
0298 end
0299 if M_.orig_endo_nbr<nendo
0300
0301 exp_0= strmatch('AUX_EXPECT_LEAD_0_', M_.endo_names);
0302 num_exp_0=size(exp_0,1);
0303 if num_exp_0>0
0304 AA3(:,exp_0)=AA1(:,exp_0);
0305 XX0=zeros(nendo,num_exp_0);
0306 AA1(:,exp_0)=XX0(:,[1:num_exp_0])
0307 end
0308 end
0309 end
0310 PSI=-[[zeros(xlen-nendo,M_.exo_nbr)];[jacobia_(:, jlen-M_.exo_nbr+1:end)]];
0311 cc=0;
0312 NX=M_.exo_nbr;
0313 NETA=nfwrd+nboth;
0314 FL_RANK=rank(AA0);
0315
0316 try
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 if(options_.ACES_solver==1)
0328 if isfield(lq_instruments,'xsopt_SS')
0329 SSbar= diag([lq_instruments.xsopt_SS(m_var)]);
0330 insSSbar=repmat(lq_instruments.xsopt_SS(lq_instruments.inst_var_indices)',nendo-num_inst,1);
0331 else
0332 SSbar= diag([dr.ys(m_var)]);
0333 insSSbar=repmat(dr.ys(lq_instruments.inst_var_indices)',nendo-num_inst,1);
0334 end
0335 SSbar=diag([diag(SSbar);diag(eye(num_inst))]);
0336 insSSbar=[insSSbar;diag(eye(num_inst))];
0337
0338 AA0=AA0*SSbar;
0339 AA1=AA1*SSbar;
0340 AA2=AA2*SSbar;
0341 lq_instruments.B1=(lq_instruments.B1).*insSSbar;
0342 end
0343
0344 if any(AA3)
0345 AA3=AA3*SSbar;
0346 [G1pi,CC,impact,nmat,TT1,TT2,gev,eu, DD, E2,E5, GAMMA, FL_RANK]=PI_gensysEXP(AA0,AA1,-AA2,AA3,cc,PSI,NX,NETA,FL_RANK, M_, options_);
0347 else
0348 [G1pi,CC,impact,nmat,TT1,TT2,gev,eu, DD, E2,E5, GAMMA, FL_RANK]=PI_gensys(AA0,AA1,-AA2,AA3,cc,PSI,NX,NETA,FL_RANK, M_, options_);
0349 end
0350
0351
0352 if (eu(1) ~= 1 || eu(2) ~= 1) && options_.ACES_solver==0
0353 info(1) = abs(eu(1)+eu(2));
0354 info(2) = 1.0e+8;
0355
0356 end
0357
0358 dr.PI_ghx=G1pi;
0359 dr.PI_ghu=impact;
0360 dr.PI_TT1=TT1;
0361 dr.PI_TT2=TT2;
0362 dr.PI_nmat=nmat;
0363 dr.PI_CC=CC;
0364 dr.PI_gev=gev;
0365 dr.PI_eu=eu;
0366 dr.PI_FL_RANK=FL_RANK;
0367
0368 dr.ghx=G1pi;
0369 dr.ghu=impact;
0370 dr.eigval = eig(G1pi);
0371 dr.rank=FL_RANK;
0372
0373 if options_.ACES_solver==1
0374 betap=options_.planner_discount;
0375 sigma_cov=M_.Sigma_e;
0376
0377 W=(1-betap)*GAMMA'*DYN_Q*GAMMA;
0378
0379 ACES.A=G1pi;
0380 ACES.C=impact;
0381 ACES.D=DD;
0382 ACES.E2=E2;
0383 ACES.E5=E5;
0384 ACES.GAMMA=GAMMA;
0385 ACES_M=size(G1pi,2)-FL_RANK;
0386 ACES_NM=FL_RANK;
0387 ACES.M=ACES_M;
0388 ACES.NM=FL_RANK;
0389
0390 ACES.Q=DYN_Q;
0391 ACES.W=W;
0392 NY=nendo-num_inst;
0393
0394
0395 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_Matrices'], 'ACES');
0396 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_GAMMA'], 'GAMMA');
0397 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_A.txt'], 'G1pi', '-ascii', '-double', '-tabs');
0398 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_C.txt'], 'impact','-ascii', '-double', '-tabs');
0399 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_D.txt'], 'DD', '-ascii', '-double', '-tabs');
0400 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_E2.txt'], 'E2', '-ascii', '-double', '-tabs');
0401 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_E5.txt'], 'E5', '-ascii', '-double', '-tabs');
0402 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_GAMMA.txt'], 'GAMMA', '-ascii', '-double', '-tabs');
0403
0404
0405
0406
0407
0408
0409 ACES_VARS=M_.endo_names;
0410 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_VARS.txt'], 'ACES_VARS', '-ascii', '-tabs');
0411
0412
0413 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_VARnames.txt'),'wt');
0414 ACES_VARS =[ACES_VARS repmat(sprintf('\n'),size(ACES_VARS,1),1)];
0415 fwrite(fid,ACES_VARS.');
0416 fclose(fid);
0417
0418 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_M.txt'),'wt');
0419 fprintf(fid,'%d\n',ACES_M);
0420 fclose(fid);
0421 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_NM.txt'),'wt');
0422 fprintf(fid,'%d\n',ACES_NM);
0423 fclose(fid);
0424 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_betap.txt'),'wt');
0425 fprintf(fid,'%d\n',betap);
0426 fclose(fid);
0427 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_NI.txt'),'wt');
0428 fprintf(fid,'%d\n',num_inst);
0429 fclose(fid);
0430 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_ND.txt'),'wt');
0431 fprintf(fid,'%d\n',NX);
0432 fclose(fid);
0433 fid = fopen(strcat(ACES_DirectoryName,'/',M_.fname,'_ACESLQ_NY.txt'),'wt');
0434 fprintf(fid,'%d\n',NY);
0435 fclose(fid);
0436 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_Q.txt'], 'DYN_Q', '-ascii', '-double', '-tabs');
0437 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_W.txt'], 'W', '-ascii', '-double', '-tabs');
0438 save ([ACES_DirectoryName,'/',M_.fname '_ACESLQ_SIGMAE.txt'], 'sigma_cov', '-ascii', '-double', '-tabs');
0439 end
0440
0441 catch
0442 lerror=lasterror;
0443 if options_.ACES_solver==1
0444 disp('Problem with using Part Info ACES solver:');
0445 error(lerror.message);
0446 else
0447 disp('Problem with using Part Info solver');
0448 error(lerror.message);
0449 end
0450 end
0451
0452
0453
0454
0455
0456 return;