clear all;
%addpath G:\dynare\4.4.1\matlab
beta_h = 0.9925;
etta = 2;
h = 0.7;
sigma_m = 3;  %the elasticity of substitution between SOE output and POE output
phi_f = 0.45;%vary the expenditure share of SOE goods φ to capture structural changes in China.
alpha_se = 0.55;
alpha_pe = 0.35;
tau_k = 2; % capital adjustment cost parameter
gamma_s = 0.15;
gamma_p = 0.15;

delta = 0.025; 
epst = 10;%implying an average gross output produced by SOE firms markup of 5%
phip = 0.75;
gamma_pi = 0.13;
gamma_y = 0.13;
gamma_r = 0.89;
rhosigma_se = 0.8566;
rhosigma_pe = 0.8566;
rhoa_pe = 0.8566;
rhoa_se = 0.8566;
rhou = 0.8566;
rho_sigmai = 0.8;
rho_a_pr = 0.5;
rho_a_sr = 0.5;
rho_a_pl = 0.5;
rho_a_sl = 0.5;

phi_p = 0.023;
phi_s = 0.012;

%gamma_pe = 0.97; %the SOE manager’s survival rate
%gamma_se = 0.69; %the POE manager’s survival rate



a_sl_s = 1;
a_pl_s = 1;
a_sr_s = 1;
a_pr_s = 1;
sigmai_s = 1;
a_se_s = 1;
u_s = 1;
pi_s = 1; %zeros inflation steady state
R_h_s = 1/beta_h - 1; %nominal interest rate
q_s = 1;
p_wt_s = (epst-1)/epst;
p_se_s = phi_f*p_wt_s;
p_pe_s = (1-phi_f)*p_wt_s;
yy_sepe_s = 1;

R_se_s = R_h_s/(1-gamma_s);
R_pe_s = R_h_s/(1-gamma_p);

leverage_pe_s = 1.82; %bank zero profit condition
leverage_se_s = 1.67; %bank zero profit condition
sp_pe_s = leverage_pe_s^phi_p;%risk spread
sp_se_s = leverage_se_s^phi_s;%risk spread
Rk_se_s = sp_se_s*(R_se_s+1) - 1; %王立勇(2018)经济研究
Rk_pe_s = sp_pe_s*(R_pe_s+1) - 1; %王立勇(2018)经济研究

r_pe_s = 1 + Rk_pe_s - (1-delta)*q_s;
r_se_s = 1 + Rk_se_s - (1-delta)*q_s;
a_pe_s = ((p_se_s^(1/(1-alpha_se))*r_pe_s^(alpha_pe/(1-alpha_pe))*(1-alpha_se)*alpha_se^(alpha_se/(1-alpha_se)))/(p_pe_s^(1/(1-alpha_pe))*r_se_s^(alpha_se/(1-alpha_se))*(1-alpha_pe)*alpha_pe^(alpha_pe/(1-alpha_pe))))^(1-alpha_pe);
w_s = (1-alpha_pe)*(a_pe_s*p_pe_s*alpha_pe^alpha_pe/r_pe_s^alpha_pe)^(1/(1-alpha_pe));
kk_sepe_s = yy_sepe_s*alpha_se*p_se_s*r_pe_s/(alpha_pe*p_pe_s*r_se_s); 
nn_sepe_s = yy_sepe_s*(1-alpha_se)*p_se_s/((1-alpha_pe)*p_pe_s);
n_s = 1/3;
n_pe_s = n_s/(1+nn_sepe_s);
n_se_s = n_s - n_pe_s;
y_se_s = n_se_s*w_s/((1-alpha_se)*p_se_s);
y_pe_s = n_pe_s*w_s/((1-alpha_pe)*p_pe_s);
k_pe_s = y_pe_s*p_pe_s*alpha_pe/r_pe_s;
k_se_s = y_se_s*p_se_s*alpha_se/r_se_s;
k_s = k_se_s + k_pe_s;
invt_s = delta*k_s;
y_s = (phi_f*y_se_s^((sigma_m-1)/sigma_m) + (1-phi_f)*y_pe_s^((sigma_m-1)/sigma_m))^(sigma_m/(sigma_m-1));

nw_pe_s = k_pe_s/leverage_pe_s;
nw_se_s = k_se_s/leverage_se_s;
b_pe_s = k_pe_s - nw_pe_s;
b_se_s = k_se_s - nw_se_s;
gamma_pe = 1/(Rk_pe_s+1);
gamma_se = 1/(Rk_se_s+1);
c_s = y_s - invt_s ; 
%lam_s = 1/c_s;
lam_s = 1/(c_s-h*c_s) - h/(c_s-h*c_s)*beta_h;
tau_n = w_s*lam_s/n_s^etta;
macro_leverage_s = (b_pe_s+b_se_s)/y_s;

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