% %  the code of my_paper

% define 34 endogenous variables
var y ys yn c cg cc m ls h r lamda zs zn ws wn pi q xi fn fs ns nn k ks kn x kg i ig g a t ui uc; 
% y:gross output;  ys:stated-owned entreprenures;  yn:nonstated-owned entrepreneurs;   
% c:gross consume; cc:private consume;  cg:government consume
% m:money demand; ls:labor input of stated-owned entrepreneurs; h:labor input of nonstated-owned entrepreneurs 

% define 4 exogenous variables(from 4 shocks)
varexo e_a e_x e_gi e_gc;

% define parameters
parameters beta sigma_c sigma_l sigma_m alpha_s alpha_n alpha_g delta_i delta_gi phi psi_n
psi_s psi_gi psi_gc sgi sgc alpha_gc vs vn kns knn chi rho_r rho_y rho_pi rho_a rho_x rho_gc rho_gi
ss sn theta lsss lnss se_a se_x se_gc se_gi omega 
% 稳态解参数
yss ysss ynss css cgss ccss mss rss lamdass zsss znss wsss wnss piss qss xiss fnss fsss 
nsss nnss kss ksss knss xss kgss iss igss gss ass tss uiss ucss;

% parameter calibration
beta=0.9928;          % discount factor
sigma_c=0.87;         % consumers' risk aversion coefficient
sigma_l=1;            % 1/sigma_l: the labour inter-temporal elasticity of substitution
sigma_m=1.5;          % 1/sigma_m: the interest rate elasticity coefficient of monetary demand
alpha_s=0.55;         % the capital share of state-owned entreprenure
alpha_n=0.35;         % the capital share of nonstate-owned entreprenure
alpha_g=0.1;          % the output elasticity of state-owned investment spending
delta_i=0.1;          % the depreciation rate of private capital 
delta_gi=0.1;         % the depreciation rate of government capital
phi=0.7418;           % the probability that prices remain unchanged for the next period
psi_n=0.05;           % the elasticity of the external finance premium to nonstate-owned entreprenure
psi_s=0.0137;         % the elasticity of the external finance premium to state-owned entreprenure
psi_gi=0.3999;        % the response to output of Government investment spending
psi_gc=0.6005;        % the response to output of government consume spending
sgi=0.023283471;      % the ratio of government investment spending to output 
sgc=0.119071112;      % the ratio of government consume spending to output
alpha_gc=-0.4193;     % the coefficient of complementary between private consumption and government consumption
vs=0.948;             % survival rate of stated-owned entrepreneurs
vn=0.9376;            % survival rate of nonstated-owned entrepreneurs
kns=1.3;              % steady state ratio of capital to net worth of stated-owned entrepreneurs
knn=1.17;             % steady state ratio of capital to net worth of nonstated-owned entrepreneurs
chi=0.5882;           % capital adjustment cost
rho_r=0.98;           % the lag coefficient of interest rates
rho_y=0.78;           % the coefficient of output gap
rho_pi=1.31;          % the coeffient of inflation rate 
rho_a=0.7625;         % the first-order lag coefficient of technology shock
rho_x=0.6562;         % the first-order lag coefficient of investment effectiveness shock
rho_gc=0.9282;        % the first-order lag coefficient of government consume spending shock
rho_gi=0.9493;        % the first-order lag coefficient of government investment spending shock
ss=1.0036;            % gross steady-state risk premium of stated-owned entrepreneurs
sn=1.0079;            % gross steady-state risk premium of nonstated-owned entrepreneurs
theta=6;              % intermediate-goods elasticity of substitution
lsss=1;               % steady labor input of stated owned entrepreneurs
lnss=4.50;            % steady labor input of nonstated owned entrepreneurs  
se_a=0.0096;          % the standard deviation of technology shock
se_x=0.0331;          % the standard deviation of investment effectiveness shock
se_gc=0.0919;         % the standard deviation of government consume spending shock
se_gi=0.1299;         % the standard deviation of government investment spending shock
omega=0.27;           % the ratio of state-owned entrepreneurs output to gross output

% the steady-state equilibrium using for the log-linearizzed equations
qss=1;                % _ss :means the steady state
ass=1;
uiss=1;
ucss=1;
xss=1;
xiss=(theta-1)/theta;
piss=1.0079;
rss=piss/beta;
fsss=ss*rss/piss;
fnss=sn*rss/piss;
zsss=ss*rss/piss-1+delta_i;
znss=sn*rss/piss-1+delta_i;
ysss=((sgi/(delta_i*omega))^(alpha_g/(1-alpha_s-alpha_g)))*((alpha_s*xiss/zsss)^(alpha_s/(1-alpha_s-alpha_g)))*(lsss^((1-alpha_s)/(1-alpha_s-alpha_g)));
ynss=((sgi/delta_i*(1-omega))^(alpha_g/(1-alpha_n-alpha_g)))*((alpha_n*xiss/znss)^(alpha_n/(1-alpha_n-alpha_g)))*(lnss^((1-alpha_n)/(1-alpha_n-alpha_g)));
yss=ysss+ynss;
igss=sgi*yss;
cgss=sgc*yss;
ksss=ysss*alpha_s*xiss/zsss;
knss=ynss*alpha_n*xiss/znss;
kss=ksss+knss;
iss=delta_i*kss;
ccss=yss-igss-cgss-iss;
css=ccss+alpha_gc*cgss;
mss=(1/((1-beta)*(css^(-sigma_c))))^(-1/sigma_m);
wsss=(1-alpha_s)*xiss*ysss/lsss;
wnss=(1-alpha_n)*xiss*ynss/lnss;
lamdass=((lsss+lnss)^sigma_c)/wsss;
nsss=ksss/kns;
nnss=knss/knn;
gss=igss+cgss;
tss=gss;
kgss=igss/delta_gi;

% the log-linearized equilibrium system
model(linear);
% the household sector
sigma_l*(lsss/(lsss+lnss))*ls+sigma_l*(lnss/(lsss+lnss))*h=-sigma_c*c+ws;%(1)
sigma_l*(lsss/(lsss+lnss))*ls+sigma_l*(lnss/(lsss+lnss))*h=-sigma_c*c+wn;%(2)
-sigma_c*c(-1)=-sigma_c*c-pi+r(-1);%-sigma_c*c=-sigma_c*c(+1)-pi(+1)+r;%(3)
sigma_m*m(-1)=(1/(1-beta))*sigma_c*c(-1)-beta*(1/(1-beta))*(sigma_c*c+pi)/piss;%sigma_m*m=(1/(1-beta))*sigma_c*c-beta*(1/(1-beta))*(sigma_c*c(+1)+pi(+1))/piss;%-sigma_m*m=(1/(1-beta))*(-sigma_c*c)-(beta/((1-beta)*pi))*(-sigma_c*c(+1)-pi(+1));%(4)
lamda=lamda(+1)+r;%(5)
c=ccss*cc/css+alpha_gc*cgss*cg/css;%(6)

% the production sector_from the state-owned sector
zs=xi+ys-ks;%(7)
ws=xi+ys-ls;%(8)
ys=alpha_g*kg+alpha_s*ks+(1-alpha_s)*a+(1-alpha_s)*ls;%(9)

% the production sector_from the non-state-owned sector
zn=xi+yn-kn;%(10)
wn=xi+yn-h;%(11)
yn=alpha_g*kg+alpha_n*kn+(1-alpha_n)*a+(1-alpha_n)*h;%(12)
y=ysss*ys/yss+ynss*yn/yss;%y=omega*ys+(1-omega)*yn;%y=ysss*ys/yss+ynss*yn/yss;%(13)

% the price adjustment equation
pi=beta*pi(+1)+(1-beta*phi)*(1-phi)*xi/phi;%(14)

% the bank sector_to the state-owned sector
fn=psi_n*(q(-1)+kn-nn)+r(-1)-pi;%(15)   % fn means the external financing costs to the non-state-owned sector
fn=znss*zn/fnss+(1-delta_i)*q/fnss-q(-1);%(16)
nn(+1)/(vn*fnss)=knn*fn-(knn-1)*(r(-1)-pi)-psi_n*(knn-1)*(kn+q(-1))+(psi_n*(knn-1)+1)*nn;%(17)

% the bank sector _to the non-state-owned sector
fs=psi_s*(q(-1)+ks-ns)+r(-1)-pi;%(18)  fs means the external financing costs to the state-owned sector
fs=zsss*zs/fsss+(1-delta_i)*q/fsss-q(-1);%(19)
ns(+1)/(vs*fsss)=kns*fs-(kns-1)*(r(-1)-pi)-psi_s*(kns-1)*(ks+q(-1))+(psi_s*(kns-1)+1)*ns;%(20)

% Capital goods manufacturers
k=ksss*ks/kss+knss*kn/kss;%k=ksss*ks/kss+knss*kn/kss;%(21)   % total capital=capital from the two sectors
k=delta_i*(x(-1)+i(-1))+(1-delta_i)*k(-1);%k=delta_i*(x(-1)+i(-1))+(1-delta_i)*k(-1);%(22)   Capital evolution equation  
q=chi*delta_i*(i-k)-x;%q=chi*(i-k)-x;%q=chi*delta_i*(i-k)-x;%(23)

% government sector
kg=delta_gi*ig(-1)+(1-delta_gi)*kg(-1);%(24)  % the government capital evolution equation
t=g;%(25)                                     % the total tax=total government expenditure
g=cgss*cg/gss+igss*ig/gss;%(26)               % total goverment expenditure= government consume expenditure+government investment expenditure

% monetary rules and fiscal rules
r=rho_r*r(-1)+rho_y*y+rho_pi*pi;%(27)        % monetary rules
ig=-psi_gi*y+ui;%(28)                        % fiscal rules
cg=-psi_gc*y+uc;%(29)                        % fiscal rules

% shock process
a=rho_a*a(-1)+e_a;%(30)
x=rho_x*x(-1)+e_x;%(31)
ui=rho_gi*ui(-1)+e_gi;%(32)
uc=rho_gc*uc(-1)+e_gc;%(33)

% market clearing
yss*y=ccss*cc+cgss*cg+iss*i+igss*ig;%(34)

end;

//initval;
//end;

resid;
steady;
check;

shocks;
var e_a;
stderr se_a;
var e_x;
stderr se_x;
var e_gi;
stderr se_gi;
var e_gc;
stderr se_gc;
end;

stoch_simul(linear,irf=10) y ys yn c cc k ks kn ls h i r;







