// Endogenous variables
var R H PI C rr Util Z; //Welf 

// Innovations
varexo tech;

// PARAMETERS
parameters beta theta phi rho alpha nbeta psi tau pitarget;

beta = 0.99;
theta = 5;
phi = 100;
rho = 0.9;
alpha = 1.5;
nbeta = 0.99;
tau=1/(theta-1);
tau=0;
psi=1;
pitarget=1.0;

[ZU,HU,RU,PIU,CU] = steadys(pitarget,beta,phi,tau,theta,psi);

model;

// Utility
Util = log(C)-psi/2*H^2;

// Welfare
//Welf = Util + nbeta*Welf(+1);

//Euler
1/(1+R) = beta*C/(C(+1)*PI(+1));

//new Keynesian Phillips Curve
(1+tau)*(1-theta)+theta*psi*H*C*exp(-Z)-phi*(PI-1)*PI+beta*phi*(PI(+1)-1)*PI(+1);

// resource constraint
C*(1+(phi/2)*(PI-1)^2)-exp(Z)*H;

//Monetary Policy Rule
R = (pitarget/beta)-1+alpha*(PI(+1)-pitarget);

//Technology Shocks

Z = rho*Z(-1)+tech;

// real interest rate

rr-(1+R)/PI(+1);


end;

initval;
R=RU; 
H=HU; 
PI=PIU; 
C=CU;
Z=ZU; 
Util= log(C) - psi/2*H^2;
//Welf=1;
rr=(1+RU)/PIU;
end;

shocks;
var tech;
stderr 1;
end;

//steady;

//stoch_simul;

alpha_s= linspace(1.05,2,100);

nres = length(alpha_s);
results = zeros(nres,2);

i_YU = strmatch('C',M_.endo_names,'exact');
i_piU = strmatch('PI',M_.endo_names,'exact');

ires = 1;
for i1=1:length(alpha_s);
  alpha = alpha_s(i1);

      stoch_simul(irf=0,noprint);

      results(ires,1) = [alpha];
      vv = oo_.var;
      results(ires,2) = vv(i_YU,i_YU) + vv(i_piU,i_piU); // this is the "loss function"
      ires = ires + 1;
end;

Best_Policy=flipud(sortrows(results,2));
Best_Policy=Best_Policy(nres,:)


