0001 function ms_sbvar_setup(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 if ~isfield(options_.ms,'initial_year')
0032 error('Must set initial_year option');
0033 end
0034
0035 if ~isfield(options_.ms,'final_year')
0036 error('Must set final_year option');
0037 end
0038
0039 if ~isfield(options_,'datafile')
0040 error('Must set datafile option');
0041 end
0042
0043 options_.data = read_variables(options_.datafile, ...
0044 options_.varobs, [], options_.xls_sheet, options_.xls_range);
0045
0046 if options_.ms.upper_cholesky
0047 if options_.ms.lower_cholesky
0048 error(['Upper Cholesky and lower Cholesky decomposition can''t be ' ...
0049 'requested at the same time!'])
0050 else
0051 options_.ms.restriction_fname = 'upper_cholesky';
0052 end
0053 elseif options_.ms.lower_cholesky
0054 options_.ms.restriction_fname = 'lower_cholesky';
0055 elseif ~isempty(options_.ms.Qi) && ~isempty(options_.ms.Ri)
0056 options_.ms.restriction_fname = 'exclusions';
0057 else
0058 options_.ms.restriction_fname = 0;
0059 end
0060
0061
0062
0063
0064 markov_file = [options_.ms.output_file_tag '_markov_file.dat'];
0065
0066
0067
0068
0069
0070
0071
0072 if length(options_.ms.coefficients_prior_hyperparameters) ~= 6
0073 error('When specifying the coefficients_prior_hyperparameters, you must pass a vector of 6 numbers')
0074 end
0075 mu = options_.ms.coefficients_prior_hyperparameters;
0076 mu = reshape(mu,1,6);
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 galp = options_.ms.alpha;
0089
0090
0091 gbeta = options_.ms.beta;
0092
0093
0094
0095
0096
0097 gsig2_lmdm = options_.ms.gsig2_lmdm;
0098
0099
0100
0101
0102
0103
0104
0105 xdd=options_.data;
0106
0107
0108
0109 q_m = options_.ms.freq;
0110
0111 yrBin=options_.ms.initial_year;
0112
0113
0114 qmBin=options_.ms.initial_subperiod;
0115
0116 yrFin=options_.ms.final_year;
0117
0118 qmFin=options_.ms.final_subperiod;
0119
0120 yrStart=options_.ms.initial_year;
0121
0122 qmStart=options_.ms.initial_subperiod;
0123
0124 yrEnd=options_.ms.final_year;
0125
0126 qmEnd=options_.ms.final_subperiod;
0127
0128 logindx = [];
0129
0130
0131 pctindx = [];
0132
0133
0134
0135
0136 options_.ms.vlist = 1:size(options_.varobs,1);
0137 vlist1=options_.ms.vlist;
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 indxC0Pres =options_.ms.cross_restrictions;
0148
0149
0150 Rform =options_.ms.contemp_reduced_form;
0151
0152
0153 indxPrior =options_.ms.bayesian_prior;
0154
0155 indxDummy = options_.ms.bayesian_prior;
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 hpmsmd = [0.0; 0.0];
0166 indxmsmdeqn = [0; 0; 0; 0];
0167
0168 nStates = -1;
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 xdd(:,logindx) = log(xdd(:,logindx));
0179
0180
0181 xdd(:,pctindx)=.01*xdd(:,pctindx);
0182
0183 if (q_m ~= 12) && (q_m ~= 4)
0184 disp('Warning: data must be monthly or quarterly!')
0185 return
0186 end
0187
0188
0189 nData=(yrFin-yrBin)*q_m + (qmFin-qmBin+1);
0190
0191 nSample=(yrEnd-yrStart)*q_m + (qmEnd-qmEnd+1);
0192
0193 nStart=(yrStart-yrBin)*q_m + (qmStart-qmBin);
0194
0195 nEnd=(yrEnd-yrFin)*q_m + (qmEnd-qmFin);
0196
0197 if (nEnd > 0) || (nStart < 0)
0198 disp('Warning: desired estimation period not in data set!')
0199 return
0200 end
0201 if (nSample <= 0)
0202 disp('Warning: no data points in estimation period!')
0203 return
0204 end
0205
0206
0207 xdgel=xdd(nStart+1:nData+nEnd,vlist1);
0208
0209
0210 baddata = find(isnan(xdgel));
0211 if ~isempty(baddata)
0212 disp('Warning: some data for estimation period are unavailable.')
0213 return
0214 end
0215
0216
0217 nvar=size(xdgel,2);
0218 nexo=1;
0219
0220
0221
0222
0223 [xtx,xty,yty,fss,phi,y,ncoef,xr,Bh] = fn_dataxy(nvar,options_.ms.nlags,xdgel,mu,0,nexo);
0224
0225
0226
0227
0228
0229 if Rform
0230 Ui=cell(nvar,1); Vi=cell(ncoef,1);
0231 for kj=1:nvar
0232 Ui{kj} = eye(nvar); Vi{kj} = eye(ncoef);
0233 end
0234 else
0235 [Ui,Vi,n0,np,ixmC0Pres] = feval(options_.ms.restriction_fname,nvar,nexo,options_.ms);
0236 if min(n0)==0
0237 disp('A0: restrictions give no free parameters in one of equations')
0238 return
0239 elseif min(np)==0
0240 disp('Aplus: Restrictions in give no free parameters in one of equations')
0241 return
0242 end
0243 end
0244
0245
0246
0247
0248
0249 if indxPrior
0250
0251
0252 if 1
0253 [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] = fn_rnrprior_covres_dobs(nvar,q_m,options_.ms.nlags,xdgel,mu,indxDummy,hpmsmd,indxmsmdeqn);
0254 else
0255 [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] = fn_rnrprior(nvar,q_m,options_.ms.nlags,xdgel,mu);
0256 end
0257
0258
0259 [Ptld,H0invtld,Hpinvtld] = fn_rlrprior(Ui,Vi,Pi,H0multi,Hpmulti,nvar);
0260
0261
0262 [Pmat,H0inv,Hpinv] = fn_rlrpostr(xtx,xty,yty,Ptld,H0invtld,Hpinvtld,Ui,Vi);
0263 else
0264
0265 [Pmat,H0inv,Hpinv] = fn_dlrpostr(xtx,xty,yty,Ui,Vi);
0266 end
0267
0268 if Rform
0269
0270 A0hatinv = chol(H0inv{1}/fss);
0271 A0hat=inv(A0hatinv);
0272
0273 Aphat = Pmat{1}*A0hat;
0274 else
0275
0276
0277 x = 10*rand(sum(n0),1);
0278 H0 = eye(sum(n0));
0279 crit = 1.0e-9;
0280 nit = 10000;
0281
0282 [fhat,xhat,grad,Hhat,itct,fcount,retcodehat] = csminwel('fn_a0freefun',x,H0,'fn_a0freegrad',crit,nit,Ui,nvar,n0,fss,H0inv);
0283
0284 A0hat = fn_tran_b2a(xhat,Ui,nvar,n0);
0285
0286 xhat = fn_tran_a2b(A0hat,Ui,nvar,n0);
0287 [Aphat,ghat] = fn_gfmean(xhat,Pmat,Vi,nvar,ncoef,n0,np);
0288 if indxC0Pres
0289 Fhatur0P = Fhat;
0290 for ki = 1:size(ixmC0Pres,1)
0291
0292 ixeq = ixmC0Pres{ki}(1,1);
0293 Lit = Vi{ixeq}(ixmC0Pres{ki}(:,2),:);
0294
0295 ci = ixmC0Pres{ki}(:,4) .* A0hat(ixmC0Pres{ki}(:,3),ixeq);
0296
0297 LtH = Lit/Hpinv{ixeq};
0298 HLV = LtH'/(LtH*Lit');
0299 gihat = Vi{ixeq}'*Fhatur0P(:,ixeq);
0300 Aphat(:,ixeq) = Vi{ixeq}*(gihat + HLV*(ci-Lit*gihat));
0301 end
0302 end
0303 end
0304
0305
0306
0307
0308
0309 matlab_filename = ['matlab_',options_.ms.output_file_tag,'.prn'];
0310 fidForC = fopen(matlab_filename,'w');
0311
0312 fprintf(fidForC,'\n%s\n','//== gxia: alpha parameter for gamma prior of xi ==//');
0313 fprintf(fidForC,' %20.15f ', galp);
0314 fprintf(fidForC, '\n\n');
0315
0316 fprintf(fidForC,'\n%s\n','//== gxib: beta parameter for gamma prior of xi ==//');
0317 fprintf(fidForC,' %20.15f ', gbeta);
0318 fprintf(fidForC, '\n\n');
0319
0320 fprintf(fidForC,'\n%s\n','//== glamdasig: sigma parameter for normal prior of lamda ==//');
0321 fprintf(fidForC,' %20.15f ', sqrt(gsig2_lmdm));
0322 fprintf(fidForC, '\n\n');
0323
0324
0325 fprintf(fidForC,'\n%s\n','//== lags, nvar, nStates, T ==//');
0326 fprintf(fidForC,' %d %d %d %d\n\n\n',options_.ms.nlags, nvar, nStates, fss);
0327
0328
0329 fprintf(fidForC,'\n%s\n','//== A0hat: nvar-by-nvar ==//');
0330 indxFloat = 1;
0331 xM = A0hat;
0332 nrows = nvar;
0333 ncols = nvar;
0334 fn_fprintmatrix(fidForC, xM, nrows, ncols, indxFloat)
0335
0336
0337
0338 fprintf(fidForC,'\n%s\n','//== Aphat: ncoef(lags*nvar+1)-by-nvar ==//');
0339 indxFloat = 1;
0340 xM = Aphat;
0341 nrows = ncoef;
0342 ncols = nvar;
0343 fn_fprintmatrix(fidForC, xM, nrows, ncols, indxFloat)
0344
0345
0346 fprintf(fidForC,'\n%s\n','//== n0const: nvar-by-1 ==//');
0347 indxFloat = 0;
0348 xM = n0;
0349 nrows = 1;
0350 ncols = nvar;
0351 fn_fprintmatrix(fidForC, xM', nrows, ncols, indxFloat)
0352
0353
0354 fprintf(fidForC,'\n%s\n','//== npconst: nvar-by-1 ==//');
0355 indxFloat = 0;
0356 xM = np;
0357 nrows = 1;
0358 ncols = nvar;
0359 fn_fprintmatrix(fidForC, xM', nrows, ncols, indxFloat)
0360
0361
0362 fprintf(fidForC,'\n%s','//== Specification (0=default 1=Sims-Zha 2=Random Walk) ==//');
0363 fprintf(fidForC,'\n%d\n\n',options_.ms.specification);
0364
0365
0366
0367
0368
0369
0370
0371 fprintf(fidForC,'\n%s\n','//== Uiconst: cell(nvar,1) and nvar-by-n0const(i) for the ith cell (equation) ==//');
0372 for i_=1:nvar
0373 fn_fprintmatrix(fidForC, Ui{i_}, nvar, n0(i_), 1);
0374 end
0375
0376
0377
0378
0379
0380
0381
0382 fprintf(fidForC,'\n%s\n','//== Viconst: cell(nvar,1) and ncoef-by-n0const(i) for the ith cell (equation) ==//');
0383 for i_=1:nvar
0384 fn_fprintmatrix(fidForC, Vi{i_}, ncoef, np(i_), 1);
0385 end
0386
0387
0388
0389 fprintf(fidForC,'\n%s\n','//== H0barconstcell: cell(nvar,1) and n-by-n for the ith cell (equation) ==//');
0390 for i_=1:nvar
0391 fn_fprintmatrix(fidForC, H0multi(:,:,i_), nvar, nvar, 1);
0392 end
0393
0394
0395
0396 fprintf(fidForC,'\n%s\n','//== Hpbarconstcell: cell(nvar,1) and ncoef-by-ncoef for the ith cell (equation) ==//');
0397 for i_=1:nvar
0398 fn_fprintmatrix(fidForC, Hpmulti(:,:,i_), ncoef, ncoef, 1);
0399 end
0400
0401
0402 fprintf(fidForC,'\n%s\n','//== Xright -- X: T-by-ncoef ==//');
0403 xM = phi;
0404 nrows = fss;
0405 ncols = ncoef;
0406 for ki=1:nrows
0407 for kj=1:ncols
0408 fprintf(fidForC,' %20.15f ',xM((kj-1)*nrows+ki));
0409 if (kj==ncols)
0410 fprintf(fidForC,'\n');
0411 end
0412 end
0413 if (ki==nrows)
0414 fprintf(fidForC,'\n\n');
0415 end
0416 end
0417
0418
0419 fprintf(fidForC,'\n%s\n','//== Yleft -- Y: T-by-nvar ==//');
0420 xM = y;
0421 nrows = fss;
0422 ncols = nvar;
0423 for ki=1:nrows
0424 for kj=1:ncols
0425 fprintf(fidForC,' %20.15f ',xM((kj-1)*nrows+ki));
0426 if (kj==ncols)
0427 fprintf(fidForC,'\n');
0428 end
0429 end
0430 if (ki==nrows)
0431 fprintf(fidForC,'\n\n');
0432 end
0433 end
0434
0435 fclose(fidForC);
0436
0437
0438
0439
0440 ms_write_markov_file(markov_file,options_)
0441 create_init_file = [matlab_filename,' ',markov_file,' ',options_.ms.file_tag];
0442 [err] = ms_sbvar_create_init_file(create_init_file);
0443 mexErrCheck('ms_sbvar_create_init_file',err);
0444 end