%%Soroush Ghazi; Dec 19; This file gets the parameters, initial values, and
%%the (perceived) final value of the shock and creates the Dynare mod file.
function write_rbc_dtr_MIT_modFile(params,initvals,af,numPeriods)
%params = [alpha;beta;delta;psi;theta];
alpha = params(1);
beta  = params(2);
delta = params(3);
psi   = params(4);
theta = params(5);
%initvals = [a0;y0;c0;h0;k0];
a0 = initvals(1);
y0 = initvals(2);
c0 = initvals(3);
h0 = initvals(4);
k0 = initvals(5);
%endvals
y2k = (1/beta-(1-delta))/alpha;
c2k = y2k - delta;
h2k = (y2k/exp(af))^(1/(1-alpha));
kf  = ( ((1-alpha)*exp(af))/(theta*c2k*h2k^(psi+alpha)) )^(1/(1+psi));
hf  = h2k*kf;
yf  = y2k*kf;
cf  = c2k*kf;

%%write mod file
fid = fopen('rbc_dtr_MIT.mod', 'w+');

fprintf(fid,'var y, c, h, k;\n');
fprintf(fid,'varexo a;\n');
fprintf(fid,'\n');

fprintf(fid,'parameters beta alpha delta theta psi;\n');
fprintf(fid,'\n');
fprintf(fid,'alpha = %f;\n', alpha);
fprintf(fid,'beta  = %f;\n', beta);
fprintf(fid,'delta = %f;\n', delta);
fprintf(fid,'psi   = %f;\n', psi);
fprintf(fid,'theta = %f;\n', theta);
fprintf(fid,'\n');

fprintf(fid,'model;\n');
fprintf(fid,'c*theta*h^(1+psi) = (1-alpha)*y;\n');
fprintf(fid,'1 = beta*(c/c(+1))*(alpha*y(+1)/k+(1-delta));\n');
fprintf(fid,'y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));\n');
fprintf(fid,'k = (y-c)+(1-delta)*k(-1);\n');
fprintf(fid,'end;\n');
fprintf(fid,'\n');

fprintf(fid,'steady_state_model;\n');
fprintf(fid,'y2k = (1/beta-(1-delta))/alpha;\n');
fprintf(fid,'c2k = y2k - delta;\n');
fprintf(fid,'h2k = (y2k/exp(a))^(1/(1-alpha));\n');
fprintf(fid,'k = ( ((1-alpha)*exp(a))/(theta*c2k*h2k^(psi+alpha)) )^(1/(1+psi));\n');
fprintf(fid,'h = h2k*k;\n');
fprintf(fid,'y = y2k*k;\n');
fprintf(fid,'c = c2k*k;\n');
fprintf(fid,'end;\n');
fprintf(fid,'steady;\n');
fprintf(fid,'\n');

fprintf(fid,'initval;\n');
fprintf(fid,'a = %f;\n', a0);
fprintf(fid,'y = %f;\n', y0);
fprintf(fid,'c = %f;\n', c0);
fprintf(fid,'h = %f;\n', h0);
fprintf(fid,'k = %f;\n', k0);
fprintf(fid,'end;\n');
fprintf(fid,'\n');

fprintf(fid,'endval;\n');
fprintf(fid,'a = %f;\n', af);
fprintf(fid,'y = %f;\n', yf);
fprintf(fid,'c = %f;\n', cf);
fprintf(fid,'h = %f;\n', hf);
fprintf(fid,'k = %f;\n', kf);
fprintf(fid,'end;\n');
fprintf(fid,'\n');

fprintf(fid,'shocks;\n');
fprintf(fid,'var a; periods 1:500; values (%f);\n', af);
fprintf(fid,'end;\n');
fprintf(fid,'\n');

fprintf(fid,'simul(periods=%d);\n',numPeriods);
fclose(fid);

end