clear all
 ...
global scalv_ ex_ ex_det_ recur_ recurs_ ys_ y_ exe_ exe_det_ lgy_ lgx_ lgr_ dsmpl_ endval_
 ...
global endo_nbr exo_nbr exo_det_nbr iy_  ykmin_  ykmax_  xkmin_  xkmax_ zkmin_ zkmax_ iter_
 ...
global dynatol_ slowc_ maxit_ valf_ ys0_ recurs0_ ex0_ timing_ ct_ gstep_ Sigma_e_ fname_ lgx_orig_ord_ iter_ options_ dr_ oo_ trend_coeff_ eigenvalues_
global M_
M_.ex_det_length = 0;
M_.dname = 'usmodel_ces';
dsmpl_=0;
dynatol_=0.00001;
maxit_=10;
slowc_=1;
timing_=0;
ct_=0;
gstep_=1e-2;
endval_=0;rplottype_=0;
valf_=0;
y_=[];
ex_=[];
ex_det_=[];
fname_ = 'usmodel_ces';
logname_ = 'usmodel_ces.log';
diary off;
warning off;
delete usmodel_ces.log;
warning on;
warning backtrace;
diary usmodel_ces.log;
options_ = [];







global curvw cgy curvp constelab constepinf constebeta cmaw cmap calfa  ...
czcap 
global cbeta csadjcost ctou csigma chabb ccs cinvs cfc cindw cprobw 
global cindp cprobp csigl clandaw crdpi crpi crdy cry crr crhoa 
global crhoas crhob crhog crhols crhoqs crhoms crhopinf crhow ctrend  ...
conster 
global cg cgamma clandap cbetabar cr cpie crk cw cikbar cik 
global clk cky ciy ccy crkky cwhlc cwly 



ctou=.025;
clandaw=1.5;
cg=0.18;
curvp=10;
curvw=10;


calfa=.24;
cgamma=1.004;
cbeta=.9995;
csigma=1.5;
cpie=1.005;
cfc=1.5;
cgy=0.51;

csadjcost= 6.0144;
chabb=    0.6361;
cprobw=   0.8087;
csigl=    1.9423;
cprobp=   0.6;
cindw=    0.3243;
cindp=    0.47;
czcap=    0.2696;
crpi=     1.488;
crr=      0.8762;
cry=      0.0593;
crdy=     0.2347;

crhoa=    0.9977;
crhob=    0.5799;
crhog=    0.9957;
crhols=   0.9928;
crhoqs=   0.7165;
crhoas=1;
crhoms=0;
crhopinf=0;
crhow=0;
cmap = 0;
cmaw  = 0;


clandap=cfc;
cbetabar=cbeta*cgamma^(-csigma);
cr=cpie/(cbeta*cgamma^(-csigma));
crk=(cbeta^(-1))*(cgamma^csigma) - (1-ctou);
cw = (calfa^calfa*(1-calfa)^(1-calfa)/(clandap*crk^calfa))^(1/(1-calfa));
cikbar=(1-(1-ctou)/cgamma);
cik=(1-(1-ctou)/cgamma)*cgamma;
clk=((1-calfa)/calfa)*(crk/cw);
cky=cfc*(clk)^(calfa-1);
ciy=cik*cky;
ccy=1-cg-cik*cky;
crkky=crk*cky;
cwhlc=(1/clandaw)*(1-calfa)/calfa*crk*cky/ccy;
cwly=1-crk*cky;


ctrend=(cgamma-1)*100;
conster=(cr-1)*100;
constepinf=(cpie-1)*100;
constelab=0;

lgy_ = 'a';
lgy_ = str2mat(lgy_,'b');
lgy_ = str2mat(lgy_,'c');
lgy_ = str2mat(lgy_,'cf');
lgy_ = str2mat(lgy_,'dc');
lgy_ = str2mat(lgy_,'dinve');
lgy_ = str2mat(lgy_,'dw');
lgy_ = str2mat(lgy_,'dy');
lgy_ = str2mat(lgy_,'epinfma');
lgy_ = str2mat(lgy_,'ewma');
lgy_ = str2mat(lgy_,'g');
lgy_ = str2mat(lgy_,'inve');
lgy_ = str2mat(lgy_,'invef');
lgy_ = str2mat(lgy_,'k');
lgy_ = str2mat(lgy_,'kf');
lgy_ = str2mat(lgy_,'kp');
lgy_ = str2mat(lgy_,'kpf');
lgy_ = str2mat(lgy_,'lab');
lgy_ = str2mat(lgy_,'labf');
lgy_ = str2mat(lgy_,'labobs');
lgy_ = str2mat(lgy_,'mc');
lgy_ = str2mat(lgy_,'ms');
lgy_ = str2mat(lgy_,'pinf');
lgy_ = str2mat(lgy_,'pinfobs');
lgy_ = str2mat(lgy_,'pk');
lgy_ = str2mat(lgy_,'pkf');
lgy_ = str2mat(lgy_,'qs');
lgy_ = str2mat(lgy_,'r');
lgy_ = str2mat(lgy_,'rk');
lgy_ = str2mat(lgy_,'rkf');
lgy_ = str2mat(lgy_,'robs');
lgy_ = str2mat(lgy_,'rrf');
lgy_ = str2mat(lgy_,'spinf');
lgy_ = str2mat(lgy_,'sw');
lgy_ = str2mat(lgy_,'w');
lgy_ = str2mat(lgy_,'wf');
lgy_ = str2mat(lgy_,'y');
lgy_ = str2mat(lgy_,'yf');
lgy_ = str2mat(lgy_,'zcap');
lgy_ = str2mat(lgy_,'zcapf');
lgx_ = 'ea';
lgx_ = str2mat(lgx_,'eb');
lgx_ = str2mat(lgx_,'eg');
lgx_ = str2mat(lgx_,'em');
lgx_ = str2mat(lgx_,'epinf');
lgx_ = str2mat(lgx_,'eqs');
lgx_ = str2mat(lgx_,'ew');
lgx_orig_ord_ = [1 2 3 5 6 4 7];
endo_nbr = 40;
exo_nbr = 7;
exo_det_nbr = 0;
recur_nbr = 0;
options_.linear=1;
iy_ = [ 1 2 3 4 0 0 0 0 5 6 7 8 9 0 0 10 11 0 0 0 0 12 13 0 0 0 14 15 0 0 0 ...
 0 16 17 18 0 19 20 0 0];
temp = [ 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  ...
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60];
iy_ = [ iy_ ; temp ];
temp = [ 0 0 61 62 0 0 0 0 0 0 0 63 64 0 0 0 0 65 66 0 0 0 67 0 68 69 0 0  ...
70 71 0 0 0 0 72 0 0 0 0 0];
iy_ = [ iy_ ; temp ];
ykmin_ = 1;
ykmax_ = 1;
xkmin_ = 0;
xkmax_ = 0;
zkmin_ = 0;
zkmax_ = 0;
ys_ = zeros(40,1);
exe_ = zeros(7,1);


% (M)SHOCKS 
make_ex_;
Sigma_e_ = zeros(7,7);
Sigma_e_(1,1) = (0.4618)^2;
Sigma_e_(2,2) = (1.8513)^2;
Sigma_e_(3,3) = (0.6090)^2;
Sigma_e_(6,6) = (0.6017)^2;
Sigma_e_(4,4) = (0.2397)^2;
Sigma_e_(5,5) = (0.1455)^2;
Sigma_e_(7,7) = (0.2089)^2;
M_.ex_det_length = 0;




global estim_params_
estim_params_.var_exo = [];
estim_params_.var_endo = [];
estim_params_.corrx = [];
estim_params_.corrn = [];
estim_params_.param_names = [];
estim_params_.param_vals = [];
estim_params_.var_exo = [estim_params_.var_exo;  1, 0.4618, 0.01, 3, 4, 0.1 ...
, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  2, 0.1818513, 0.025, 5, 4 ...
, 0.1, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  3, 0.6090, 0.01, 3, 4, 0.1 ...
, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  6, 0.46017, 0.01, 3, 4 ...
, 0.1, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  4, 0.2397, 0.01, 3, 4, 0.1 ...
, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  5, 0.1455, 0.01, 3, 4, 0.1 ...
, 2, NaN, NaN, NaN];
estim_params_.var_exo = [estim_params_.var_exo;  7, 0.2089, 0.01, 3, 4, 0.1 ...
, 2, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhoa');
estim_params_.param_vals = [estim_params_.param_vals; .9676, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhob');
estim_params_.param_vals = [estim_params_.param_vals; .2703, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhog');
estim_params_.param_vals = [estim_params_.param_vals; .9930, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhoqs');
estim_params_.param_vals = [estim_params_.param_vals; .5724, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhoms');
estim_params_.param_vals = [estim_params_.param_vals; .3, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhopinf');
estim_params_.param_vals = [estim_params_.param_vals; .8692, .01, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crhow');
estim_params_.param_vals = [estim_params_.param_vals; .9546, .001, .9999, 1 ...
, 0.5, 0.20, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cmap');
estim_params_.param_vals = [estim_params_.param_vals; .7652, 0.01, .9999, 1 ...
, 0.5, 0.2, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cmaw');
estim_params_.param_vals = [estim_params_.param_vals; .8936, 0.01, .9999, 1 ...
, 0.5, 0.2, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'csadjcost');
estim_params_.param_vals = [estim_params_.param_vals; 6.3325, 2, 15, 3, 4 ...
, 1.5, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'csigma');
estim_params_.param_vals = [estim_params_.param_vals; 1.2312, 0.25, 3, 3 ...
, 1.50, 0.375, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'chabb');
estim_params_.param_vals = [estim_params_.param_vals; 0.7205, 0.001, 0.99 ...
, 1, 0.7, 0.1, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cprobw');
estim_params_.param_vals = [estim_params_.param_vals; 0.7937, 0.3, 0.95, 1 ...
, 0.5, 0.1, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'csigl');
estim_params_.param_vals = [estim_params_.param_vals; 2.8401, 0.25, 10, 3 ...
, 2, 0.75, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cprobp');
estim_params_.param_vals = [estim_params_.param_vals; 0.7813, 0.5, 0.95, 1 ...
, 0.5, 0.10, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cindw');
estim_params_.param_vals = [estim_params_.param_vals; 0.4425, 0.01, 0.99, 1 ...
, 0.5, 0.15, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cindp');
estim_params_.param_vals = [estim_params_.param_vals; 0.3291, 0.01, 0.99, 1 ...
, 0.5, 0.15, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'czcap');
estim_params_.param_vals = [estim_params_.param_vals; 0.2648, 0.01, 1, 1 ...
, 0.5, 0.15, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cfc');
estim_params_.param_vals = [estim_params_.param_vals; 1.4672, 1.0, 3, 3 ...
, 1.25, 0.125, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crpi');
estim_params_.param_vals = [estim_params_.param_vals; 1.7985, 1.0, 3, 3 ...
, 1.5, 0.25, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crr');
estim_params_.param_vals = [estim_params_.param_vals; 0.8258, 0.5, 0.975, 1 ...
, 0.75, 0.10, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cry');
estim_params_.param_vals = [estim_params_.param_vals; 0.0893, 0.001, 0.5, 3 ...
, 0.125, 0.05, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'crdy');
estim_params_.param_vals = [estim_params_.param_vals; 0.2239, 0.001, 0.5, 3 ...
, 0.125, 0.05, NaN, NaN, NaN];
 ...
estim_params_.param_names = strvcat(estim_params_.param_names,'constepinf');
estim_params_.param_vals = [estim_params_.param_vals; 0.7, 0.1, 2.0, 2 ...
, 0.625, 0.1, NaN, NaN, NaN];
 ...
estim_params_.param_names = strvcat(estim_params_.param_names,'constebeta');
estim_params_.param_vals = [estim_params_.param_vals; 0.7420, 0.01, 2.0, 2 ...
, 0.25, 0.1, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'constelab');
estim_params_.param_vals = [estim_params_.param_vals; 1.2918, -10.0, 10.0 ...
, 3, 0.0, 2.0, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'ctrend');
estim_params_.param_vals = [estim_params_.param_vals; 0.3982, 0.1, 0.8, 3 ...
, 0.4, 0.10, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'cgy');
estim_params_.param_vals = [estim_params_.param_vals; 0.05, 0.01, 2.0, 3 ...
, 0.5, 0.25, NaN, NaN, NaN];
estim_params_.param_names = strvcat(estim_params_.param_names,'calfa');
estim_params_.param_vals = [estim_params_.param_vals; 0.24, 0.01, 1.0, 3 ...
, 0.3, 0.05, NaN, NaN, NaN];


options_.varobs = [];
options_.varobs = strvcat(options_.varobs,'dy');
options_.varobs = strvcat(options_.varobs,'dc');
options_.varobs = strvcat(options_.varobs,'dinve');
options_.varobs = strvcat(options_.varobs,'labobs');
options_.varobs = strvcat(options_.varobs,'pinfobs');
options_.varobs = strvcat(options_.varobs,'dw');
options_.varobs = strvcat(options_.varobs,'robs');


options_.optim_opt = '''MaxIter'',200';
options_.datafile='usmodel_data';
options_.first_obs=71;
options_.presample=4;
options_.lik_init=2;
options_.prefilter=0;
options_.mh_replic=0;
options_.mh_nblck=2;
options_.mh_jscale=0.20;
options_.mh_drop=0.2;
var_list_ = 'dy';
var_list_ = str2mat(var_list_,'dc');
var_list_ = str2mat(var_list_,'dinve');
var_list_ = str2mat(var_list_,'labobs');
var_list_ = str2mat(var_list_,'pinfobs');
var_list_ = str2mat(var_list_,'dw');
var_list_ = str2mat(var_list_,'robs');
dynare_estimation(var_list_);





options_.irf=20;
var_list_ = 'dy';
var_list_ = str2mat(var_list_,'pinfobs');
var_list_ = str2mat(var_list_,'robs');
info=stoch_simul(var_list_);
































save('usmodel_ces_results','oo_','dr_');
diary off
