clc;
clearvars;
 
% Definition of all variables
% --> real interest rate?
% l labour services
% k 
% h real money balances???
% --> c consumption -endo
% v taste shock?
% --> m real money balances -endo?
% --> y output?/final consumption good
% --> pi inflation -endo
% w real wage rate
% z technology  --> transaction cost technology with mean 1
% a productivity shock
% mu 
% g government expenditures - exo
% --> R gross nominal interest rate on government bonds
% sigma_c
% n_cc
% phi(func) captures real resource costs of transaction
% phi_cc real marginal transaction costs? non-decreasing
% phi_c real marginal transaction costs (in consumption) increasing
% u_cc
% u_c (u_c=u_cv)
% n_c
% phi_h transaction costs - real money balances decrease strictly
% v_ll
% v_l
% n_h
% sigma_h
% phi_hh transaction costs - real money balacnes strictly decreasing
% n_R
% phi_ch transaction costs - non increasing
% phi_hc
% beta discount factor
% omega 
% sigma_tilda coefficient of relative risk aversion
% rho_pi interest rate policy?
% n_ch transaction frictions
% sigma_h
% n_hc The prior distribution for n_hc is chosen to generate a unit output elasticity of money demand at the mean
% alpha
 
%search for variance and cov for shocks
 
 
% Endogenous variables:
var   r_m c_hat v_hat m_hat y_hat pi_hat w_hat z_hat a_hat mu_hat g_hat R_hat;
% Exogenous variables:
varexo epsilon e_a e_g e_mu e_v e_z e_rm ;
 
% Parameters:
parameters   phi_cc  phi_c phi_h eta_cc eta_R c h R_ss eta_h eta_c s_c kappa phi sigma_r_m sigma_m sigma_a sigma_g sigma_mu sigma_v sigma_z rho_a rho_g rho_mu rho_v rho_z beta omega sigma_tilda rho_pi eta_ch sigma_c sigma_h eta_hc alpha;
 
beta = 0.99; 
s_c=0.8; %for ss
alpha = 0.5;
sigma_tilda = 1;
omega = 3.5;
sigma_c = 0.8;
R_ss = 1.0146; %could be R_bar
rho_pi = 0.9692; %mean of 0.85 in R3
eta_ch = 0.3; % mean of 0.3 in R3
sigma_h = 0.29; %at the mean //frisch elasticity of labour ss is also 0.29 
eta_hc = 0.232; %0.8 of sigma_h
rho_a = 0.9221;
rho_g = 0.8535;
rho_mu = 0.8501;
rho_v = 0.6337;

eta_h = 0.0284;
eta_c = 0.2649;

rho_z = 0.8943;
sigma_a = 1;
sigma_g = 1; 
sigma_mu = 1; 
sigma_v = 1; 
sigma_z = 1;
sigma_m = 1; 
sigma_r_m = 1;

phi_h = 1;
phi_cc = 1;
phi_c = 1;
phi = 1;
kappa=((1-alpha)*(1-alpha*beta))/alpha;

c = (eta_c*phi)/phi_c; %eta_c = (phi_c*c)/phi;
h = -(eta_h*phi)/phi_h; %eta_h = -(phi_h*h)/phi;
eta_R=R_ss/(sigma_h*(R_ss-1));
eta_cc=(phi_cc*c)/(1+phi_c);



 
% The Model:
model(linear);

    a_hat = rho_a*a_hat(-1) + e_a;
    g_hat = rho_g*g_hat(-1) + e_g;
    mu_hat = rho_mu*mu_hat(-1) + e_mu;
    v_hat = rho_v*v_hat(-1) + e_v;
    z_hat = rho_z*z_hat(-1) + e_z;
    log(r_m) = e_rm;
    
    
     % sigma_c=-(u_cc*c)/u_c;
     % omega=(v_ll*l)/v_l;
  
     % eta_ch=-(phi_ch*h)/(1+phi_c);
     % eta_hc=(phi_hc*c)/phi_h;
    
     % sigma_h=-(phi_hh*h)/phi_h;
     % sigma_tilda=sigma_c+eta_cc;
 
    
   % rho_pi=(f_pi(pi,1)*pi)/R;
    
    
    c_hat(+1) = (v_hat(+1)+(eta_ch*(m_hat-pi_hat(+1)+z_hat(+1)))+sigma_tilda*c_hat-v_hat-eta_ch*(m_hat(-1)-pi_hat+z_hat)+R_hat-pi_hat(+1))*sigma_tilda;
    
    w_hat = omega*(y_hat-a_hat)-v_hat+sigma_tilda*c_hat-eta_ch*(m_hat(-1)-pi_hat+z_hat)+mu_hat;
    
    pi_hat = beta*pi_hat(+1)+kappa*(w_hat-a_hat);
    
    y_hat = s_c*c_hat + g_hat;     % y_hat=(1-(g/y)-(phi/y)+eta_c)*c_hat+g_hat-eta_h*(m_hat(-1)-pi_hat+z_hat);
    
    m_hat = -eta_R*R_hat+(eta_hc/sigma_h)*c_hat(+1)+pi_hat(+1)+((1-sigma_h)/sigma_h)*z_hat(+1);
    
    R_hat = rho_pi*pi_hat+epsilon; %(^m)
    
end;
 
shocks;
var e_a = sigma_a^2;
var e_g = sigma_g^2;
var e_mu = sigma_mu^2;
var e_v = sigma_v^2;
var e_z = sigma_z^2;
var e_rm = sigma_r_m^2;
var epsilon = sigma_m^2;
end;

initval;
a_hat = 0;
g_hat = 0;
mu_hat = 0;
v_hat = 0;
z_hat = 0;
end;

stoch_simul c_hat y_hat pi_hat m_hat R_hat;
check;
