clear all;
clc;
close all;
%addpath G:\dynare\4.4.1\matlab
%household
beta_h = 0.9925;
beta_se = 0.92;
beta_pe = 0.925;
beta_b = 0.945;
alpha_se = 0.6;
alpha_pe = 0.45;
phi_m = 0.5;

sigma_m = 3;
m_se = 0.62;
m_pe = 0.57;
tau_pe = 0.19;
tau_se = 0.23;

rho = 0.03;
delta = 0.025;
epst = 10;
phi_p = 0.75;
gamma_pi = 2.5;
gamma_y = 0.17;
gamma_r = 0.89;
ppsi = 2;
rho_lg = 0 ;
rho_y = -2;
delta_g = 0.092;
alpha_g = 0.11;

gamma_g = 0.99;
gamma_s_s = 0.93;
gamma_p = 0.725;


%shock
rho_a_e = 0.8;
rho_a_pe = 0.8;
rho_a_se = 0.8;
rho_sigmai = 0.8;
sigmais = 1;
rho_gamma_s = 0.8;


a_e_s = 1;
a_se_s = 1;
q_s = 1;
pi_s = 1;
mc_s = (epst-1)/epst;
p_w_s = mc_s;
p_se_s = p_w_s; 
p_pe_s = p_w_s;
R_h_s = 1/beta_h - 1;
lamba_b_s = (1-beta_b*(1+R_h_s))/(1-beta_b*rho) ;
R_pe_s = (beta_b*rho - gamma_p*beta_b*(1-rho))*lamba_b_s/beta_b + 1/beta_b - 1;
R_se_s = (beta_b*rho - gamma_s_s*beta_b*(1-rho))*lamba_b_s/beta_b + 1/beta_b - 1;
R_g_s = (beta_b*rho - gamma_g*beta_b*(1-rho))*lamba_b_s/beta_b + 1/beta_b - 1;
lamba_se_s = 1-beta_se*(1 + R_se_s);
lamba_pe_s = 1-beta_pe*(1 + R_pe_s);
R_sk_s = q_s - beta_se*q_s*(1-delta) - lamba_se_s*m_se*q_s/(1+R_se_s);
R_pk_s = q_s - beta_pe*q_s*(1-delta) - lamba_pe_s*m_pe*q_s/(1+R_pe_s);

gov_s = 0.21;

y_sepe_s = phi_m/(1-phi_m) + gov_s/((1-phi_m)*(1-gov_s));

n_s = 1/3;
nn_sepe_s = (1-alpha_se)/(1-alpha_pe)*y_sepe_s;
n_pe_s = n_s/(nn_sepe_s+1);
n_se_s = n_s-n_pe_s; 


w_s = ((1-alpha_se)*(p_se_s*(gov_s*n_se_s/(delta_g*p_se_s*(1-alpha_se)*(phi_m*(1-gov_s) + gov_s)))^alpha_g)^(1/(1-alpha_se))*(alpha_se/R_sk_s)^(alpha_se/(1-alpha_se)))^(1/(1-alpha_g/(1-alpha_se)));


y_pe_s = w_s*n_pe_s/(p_pe_s*(1-alpha_pe));
y_se_s = w_s*n_se_s/(p_se_s*(1-alpha_se));
k_se_s = p_se_s*alpha_se*y_se_s/R_sk_s;
k_pe_s = p_pe_s*alpha_pe*y_pe_s/R_pk_s;
l_se_s = m_se*q_s*k_se_s/(1+R_se_s);
l_pe_s = m_pe*q_s*k_pe_s/(1+R_pe_s);
c_se_s = (1-tau_se)*p_se_s*y_se_s - q_s*delta*k_se_s - R_se_s*l_se_s - w_s*n_se_s;
c_pe_s = (1-tau_pe)*p_pe_s*y_pe_s - q_s*delta*k_pe_s - R_pe_s*l_pe_s - w_s*n_pe_s;
y_s = y_pe_s/(1-phi_m);

g_s = gov_s/(1-gov_s)*y_s;
y_ss = (y_se_s-g_s*(1-phi_m))/phi_m;


k_g_s = g_s/delta_g;

l_g_s = (tau_pe*p_pe_s*y_pe_s + tau_se*p_se_s*y_se_s - p_se_s*g_s)/R_g_s;
D_h_s = gamma_p*l_pe_s + gamma_s_s*l_se_s + gamma_g*l_g_s - (l_pe_s+l_se_s+l_g_s)*rho/(1-rho) ;
c_b_s = R_se_s*l_se_s + R_pe_s*l_pe_s + R_g_s*l_g_s - R_h_s*D_h_s;
k_s = k_pe_s + k_se_s;
invt_s = delta*k_s;
c_h_s = y_s - c_b_s - c_se_s - c_pe_s - invt_s ;
tau = w_s*(1-n_s)/c_h_s;
c_s = c_b_s + c_se_s + c_pe_s + c_h_s;
y_total_s = y_s+g_s;
leverage_se_s = l_se_s/(q_s*k_se_s);
leverage_pe_s = l_pe_s/(q_s*k_pe_s);
leverage_g_s = l_g_s/y_total_s;


a_pe_s = (((k_g_s^alpha_g*p_se_s)^(1/(1-alpha_se))*R_pk_s^(alpha_pe/(1-alpha_pe))*(1-alpha_se)*alpha_se^(alpha_se/(1-alpha_se)))/((k_g_s^alpha_g*p_pe_s)^(1/(1-alpha_pe))*R_sk_s^(alpha_se/(1-alpha_se))*(1-alpha_pe)*alpha_pe^(alpha_pe/(1-alpha_pe))))^(1-alpha_pe);


k_sepe_s = k_se_s/k_pe_s;


%save all the variables into mat file and then load in mod file
save BSR;
dynare BSR;%











