%% generate volatilites and welfare results under optimal policy rules following a positive productivity shock

clear all

%% set the type of shocks

sig_ap=0;   % =1 or -1 implies that the economy faces a 1% positive or negative specific productivity shocks of POEs, =0 implies that the economy doesn't experience such a shock
sig_a=1;    % =1 or -1 implies that the economy faces a 1% positive or negative productivity shock, =0 implies that the economy doesn't experience this shock
sig_m=0;    % =1 or -1 implies that the economy faces a 1% positive or negative traditional monetary policy shock, =0 implies that the economy doesn't experience this shock
sig_xi=0;   % =1 or -1 implies that the economy faces a 1% positive or negative shadow bank financing efficiency shock, =0 implies that the economy doesn't experience this shock
sig_sigma=0;   % =1 or -1 implies that the economy faces a 1% positive or negative uncertainty shock, =0 implies that the economy doesn't experience such a shock
save shock_types sig_ap sig_a sig_m sig_xi sig_sigma

% benchmark regime 
data1=0;
data2=0;
save info data1 data2
dynare benchmark.mod           
oo_bench=oo_;          
save COMBINE1 oo_bench

% optimal structural monetary policy regime
load OPT_SM
data1=opt_sm;
data2=0;
save info data1 data2
dynare benchmark.mod
oo_opt=oo_;  
save COMBINE2 oo_opt

% optimal capital regulatory policy regime
load OPT_CAR
data1=0;
data2=opt_car;
save info data1 data2
dynare benchmark.mod
oo_cc=oo_;  
save COMBINE3 oo_cc

% optimal capital regulatory policy and optimal structural monetary policy jointly
load OPT_JOINT
data1=opt_sm1;
data2=opt_car1;
save info data1 data2
dynare benchmark.mod
oo_joint=oo_;  
save COMBINE4 oo_joint

%% compute volatilites of total output, inflation, default rate, total credit size and credit structure
vol_y=[sqrt(oo_bench.var(26,26))/oo_bench.steady_state(26) sqrt(oo_opt.var(26,26))/oo_opt.steady_state(26)  sqrt(oo_cc.var(26,26))/oo_cc.steady_state(26) sqrt(oo_joint.var(26,26))/oo_joint.steady_state(26)].*100;
vol_pi=[sqrt(oo_bench.var(8,8))/oo_bench.steady_state(8) sqrt(oo_opt.var(8,8))/oo_opt.steady_state(8) sqrt(oo_cc.var(8,8))/oo_cc.steady_state(8) sqrt(oo_joint.var(8,8))/oo_joint.steady_state(8)].*100;
vol_pr=[sqrt(oo_bench.var(38,38))/oo_bench.steady_state(38) sqrt(oo_opt.var(38,38))/oo_opt.steady_state(38) sqrt(oo_cc.var(38,38))/oo_cc.steady_state(38) sqrt(oo_joint.var(38,38))/oo_joint.steady_state(38)].*100;
vol_l=[sqrt(oo_bench.var(57,57))/oo_bench.steady_state(57) sqrt(oo_opt.var(57,57))/oo_opt.steady_state(57) sqrt(oo_cc.var(57,57))/oo_cc.steady_state(57) sqrt(oo_joint.var(57,57))/oo_joint.steady_state(57)].*100;
vol_lls=[sqrt(oo_bench.var(44,44))/oo_bench.steady_state(44) sqrt(oo_opt.var(44,44))/oo_opt.steady_state(44) sqrt(oo_cc.var(44,44))/oo_cc.steady_state(44) sqrt(oo_joint.var(44,44))/oo_joint.steady_state(44)].*100;

% compute welfare gains

parameter=calibration;
BETA=parameter(1);
W_gain=[(exp((oo_bench.mean(41)-oo_bench.steady_state(41))*(1-BETA))-1) (exp((oo_opt.mean(41)-oo_opt.steady_state(41))*(1-BETA))-1) (exp((oo_cc.mean(41)-oo_cc.steady_state(41))*(1-BETA))-1) (exp((oo_joint.mean(41)-oo_joint.steady_state(41))*(1-BETA))-1)].*10000 ;

%% generate the first half of table 1 
TABLE_1=[W_gain;
         vol_y;
         vol_pi;
         vol_pr;
         vol_l;
         vol_lls]
save table2 TABLE_1
%% generate figure II1 in appendix
load COMBINE1
load COMBINE2
load COMBINE3
load COMBINE4
figure;

subplot(3,3,1)
plot(100*oo_bench.irfs.tau_p_exp_a(1:10)./oo_bench.steady_state(46),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.tau_p_exp_a(1:10)./oo_opt.steady_state(46),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.tau_p_exp_a(1:10)./oo_cc.steady_state(46),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.tau_p_exp_a(1:10)./oo_joint.steady_state(46),'r:','LineWidth',1.5);
title('结构性货币政策','FontSize',14,'interpreter','latex')
box off


subplot(3,3,2)
plot(100*oo_bench.irfs.GAMMA_exp_a(1:10)./oo_bench.steady_state(53),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.GAMMA_exp_a(1:10)./oo_opt.steady_state(48),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.GAMMA_exp_a(1:10)./oo_cc.steady_state(48),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.GAMMA_exp_a(1:10)./oo_joint.steady_state(48),'r:','LineWidth',1.5);
hold all
title('资本充足率','FontSize',14,'interpreter','latex')
box off
subplot(3,3,3)
plot(100*oo_bench.irfs.gdp_exp_a(1:10)./oo_bench.steady_state(48),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.gdp_exp_a(1:10)./oo_opt.steady_state(48),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.gdp_exp_a(1:10)./oo_cc.steady_state(48),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.gdp_exp_a(1:10)./oo_joint.steady_state(48),'r:','LineWidth',1.5);
title('GDP','FontSize',14,'interpreter','latex')
box off

subplot(3,3,4)
plot(100*oo_bench.irfs.pi_exp_a(1:10)./oo_bench.steady_state(8),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.pi_exp_a(1:10)./oo_opt.steady_state(8),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.pi_exp_a(1:10)./oo_cc.steady_state(8),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.pi_exp_a(1:10)./oo_joint.steady_state(8),'r:','LineWidth',1.5);
title('通胀','FontSize',14,'interpreter','latex')
box off

subplot(3,3,5)
plot(100*oo_bench.irfs.yys_exp_a(1:10)./oo_bench.steady_state(45),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.yys_exp_a(1:10)./oo_opt.steady_state(45),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.yys_exp_a(1:10)./oo_cc.steady_state(45),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.yys_exp_a(1:10)./oo_joint.steady_state(45),'r:','LineWidth',1.5);
title('产业结构','FontSize',14,'interpreter','latex')
box off

subplot(3,3,6)
plot(100*oo_bench.irfs.lls_exp_a(1:10)./oo_bench.steady_state(44),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.lls_exp_a(1:10)./oo_opt.steady_state(44),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.lls_exp_a(1:10)./oo_cc.steady_state(44),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.lls_exp_a(1:10)./oo_joint.steady_state(44),'r:','LineWidth',1.5);
title('商业银行信贷结构','FontSize',14,'interpreter','latex')
box off

subplot(3,3,7)
plot(100*(oo_bench.irfs.ls_exp_a(1:10)+oo_bench.irfs.lp_exp_a(1:10))./(oo_bench.steady_state(10)+oo_bench.steady_state(21)),'k-','LineWidth',1.5);
hold all
plot(100*(oo_opt.irfs.ls_exp_a(1:10)+oo_opt.irfs.lp_exp_a(1:10))./(oo_opt.steady_state(10)+oo_opt.steady_state(21)),'b--','LineWidth',1.5);
hold all
plot(100*(oo_cc.irfs.ls_exp_a(1:10)+oo_cc.irfs.lp_exp_a(1:10))./(oo_cc.steady_state(10)+oo_cc.steady_state(21)),'c:','LineWidth',1.5);
hold all
plot(100*(oo_joint.irfs.ls_exp_a(1:10)+oo_cc.irfs.lp_exp_a(1:10))./(oo_joint.steady_state(10)+oo_cc.steady_state(21)),'r:','LineWidth',1.5);
title('商业银行贷款规模','FontSize',14,'interpreter','latex')
box off


subplot(3,3,8)
plot(100*oo_bench.irfs.lp_bar_exp_a(1:10)./oo_bench.steady_state(20),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.lp_bar_exp_a(1:10)./oo_opt.steady_state(20),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.lp_bar_exp_a(1:10)./oo_cc.steady_state(20),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.lp_bar_exp_a(1:10)./oo_joint.steady_state(20),'r:','LineWidth',1.5);
title('影子银行贷款规模','FontSize',14,'interpreter','latex')
box off

subplot(3,3,9)
plot(100*oo_bench.irfs.pr_exp_a(1:10)./oo_bench.steady_state(38),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.pr_exp_a(1:10)./oo_opt.steady_state(38),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.pr_exp_a(1:10)./oo_cc.steady_state(38),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.pr_exp_a(1:10)./oo_joint.steady_state(38),'r:','LineWidth',1.5);
title('影子银行违约率','FontSize',14,'interpreter','latex')
box off
legend('基准情况','最优结构性货币政策规则','最优资本监管规则','联合最优规则','FontSize',12,'interpreter','latex')
legend('boxoff')
%% generate volatilites and welfare results under optimal policy rules following a positive shadow bank financing efficiency shock
clear all

%% set the type of shocks

sig_ap=0;   % =1 or -1 implies that the economy faces a 1% positive or negative specific productivity shocks of POEs, =0 implies that the economy doesn't experience such a shock
sig_a=0;    % =1 or -1 implies that the economy faces a 1% positive or negative productivity shock, =0 implies that the economy doesn't experience this shock
sig_m=0;    % =1 or -1 implies that the economy faces a 1% positive or negative traditional monetary policy shock, =0 implies that the economy doesn't experience this shock
sig_xi=1;   % =1 or -1 implies that the economy faces a 1% positive or negative shadow bank financing efficiency shock, =0 implies that the economy doesn't experience this shock
sig_sigma=0;   % =1 or -1 implies that the economy faces a 1% positive or negative uncertainty shock, =0 implies that the economy doesn't experience such a shock
save shock_types sig_ap sig_a sig_m sig_xi sig_sigma

% benchmark regime 
data1=0;
data2=0;
save info data1 data2
dynare benchmark.mod           
oo_bench=oo_;          
save COMBINE1_sb oo_bench

% optimal structural monetary policy regime
load OPT_SM_sb
data1=opt_sm;
data2=0;
save info data1 data2
dynare benchmark.mod
oo_opt=oo_;  
save COMBINE2_sb oo_opt

% optimal capital regulatory policy regime
load OPT_CAR_sb
data1=0;
data2=opt_car;
save info data1 data2
dynare benchmark.mod
oo_cc=oo_;  
save COMBINE3_sb oo_cc

% optimal capital regulatory policy and optimal structural monetary policy jointly
load OPT_JOINT_sb
data1=opt_sm1;
data2=opt_car1;
save info data1 data2
dynare benchmark.mod
oo_joint=oo_;  
save COMBINE4_sb oo_joint

%% compute volatilites of total output, inflation, default rate, total credit size and credit structure
vol_y=[sqrt(oo_bench.var(26,26))/oo_bench.steady_state(26) sqrt(oo_opt.var(26,26))/oo_opt.steady_state(26)  sqrt(oo_cc.var(26,26))/oo_cc.steady_state(26) sqrt(oo_joint.var(26,26))/oo_joint.steady_state(26)].*10000;
vol_pi=[sqrt(oo_bench.var(8,8))/oo_bench.steady_state(8) sqrt(oo_opt.var(8,8))/oo_opt.steady_state(8) sqrt(oo_cc.var(8,8))/oo_cc.steady_state(8) sqrt(oo_joint.var(8,8))/oo_joint.steady_state(8)].*10000;
vol_pr=[sqrt(oo_bench.var(38,38))/oo_bench.steady_state(38) sqrt(oo_opt.var(38,38))/oo_opt.steady_state(38) sqrt(oo_cc.var(38,38))/oo_cc.steady_state(38) sqrt(oo_joint.var(38,38))/oo_joint.steady_state(38)].*100;
vol_l=[sqrt(oo_bench.var(57,57))/oo_bench.steady_state(57) sqrt(oo_opt.var(57,57))/oo_opt.steady_state(57) sqrt(oo_cc.var(57,57))/oo_cc.steady_state(57) sqrt(oo_joint.var(57,57))/oo_joint.steady_state(57)].*100;
vol_lls=[sqrt(oo_bench.var(44,44))/oo_bench.steady_state(44) sqrt(oo_opt.var(44,44))/oo_opt.steady_state(44) sqrt(oo_cc.var(44,44))/oo_cc.steady_state(44) sqrt(oo_joint.var(44,44))/oo_joint.steady_state(44)].*100;

% compute welfare gains

parameter=calibration;
BETA=parameter(1);
W_gain=[(exp((oo_bench.mean(41)-oo_bench.steady_state(41))*(1-BETA))-1) (exp((oo_opt.mean(41)-oo_opt.steady_state(41))*(1-BETA))-1) (exp((oo_cc.mean(41)-oo_cc.steady_state(41))*(1-BETA))-1) (exp((oo_joint.mean(41)-oo_joint.steady_state(41))*(1-BETA))-1)].*10000 ;
%% generate the second half of table 1
TABLE_11=[W_gain;
         vol_y;
         vol_pi;
         vol_pr;
         vol_l;
         vol_lls]

load table2 
TABLE1=[TABLE_1;
    TABLE_11]
save table1 TABLE1

%% generate figure II2 in appendix
load COMBINE1_sb
load COMBINE2_sb
load COMBINE3_sb
load COMBINE4_sb
figure;

subplot(3,3,1)
plot(100*oo_bench.irfs.tau_p_exp_xi(1:10)./oo_bench.steady_state(46),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.tau_p_exp_xi(1:10)./oo_opt.steady_state(46),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.tau_p_exp_xi(1:10)./oo_cc.steady_state(46),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.tau_p_exp_xi(1:10)./oo_joint.steady_state(46),'r:','LineWidth',1.5);
title('结构性货币政策','FontSize',14,'interpreter','latex')
box off


subplot(3,3,2)
plot(100*oo_bench.irfs.GAMMA_exp_xi(1:10)./oo_bench.steady_state(53),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.GAMMA_exp_xi(1:10)./oo_opt.steady_state(48),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.GAMMA_exp_xi(1:10)./oo_cc.steady_state(48),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.GAMMA_exp_xi(1:10)./oo_joint.steady_state(48),'r:','LineWidth',1.5);
hold all
title('资本充足率','FontSize',14,'interpreter','latex')
box off
subplot(3,3,3)
plot(100*oo_bench.irfs.gdp_exp_xi(1:10)./oo_bench.steady_state(48),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.gdp_exp_xi(1:10)./oo_opt.steady_state(48),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.gdp_exp_xi(1:10)./oo_cc.steady_state(48),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.gdp_exp_xi(1:10)./oo_joint.steady_state(48),'r:','LineWidth',1.5);
title('GDP','FontSize',14,'interpreter','latex')
box off

subplot(3,3,4)
plot(100*oo_bench.irfs.pi_exp_xi(1:10)./oo_bench.steady_state(8),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.pi_exp_xi(1:10)./oo_opt.steady_state(8),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.pi_exp_xi(1:10)./oo_cc.steady_state(8),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.pi_exp_xi(1:10)./oo_joint.steady_state(8),'r:','LineWidth',1.5);
title('通胀','FontSize',14,'interpreter','latex')
box off

subplot(3,3,5)
plot(100*oo_bench.irfs.yys_exp_xi(1:10)./oo_bench.steady_state(45),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.yys_exp_xi(1:10)./oo_opt.steady_state(45),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.yys_exp_xi(1:10)./oo_cc.steady_state(45),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.yys_exp_xi(1:10)./oo_joint.steady_state(45),'r:','LineWidth',1.5);
title('产业结构','FontSize',14,'interpreter','latex')
box off

subplot(3,3,6)
plot(100*oo_bench.irfs.lls_exp_xi(1:10)./oo_bench.steady_state(44),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.lls_exp_xi(1:10)./oo_opt.steady_state(44),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.lls_exp_xi(1:10)./oo_cc.steady_state(44),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.lls_exp_xi(1:10)./oo_joint.steady_state(44),'r:','LineWidth',1.5);
title('商业银行信贷结构','FontSize',14,'interpreter','latex')
box off

subplot(3,3,7)
plot(100*(oo_bench.irfs.ls_exp_xi(1:10)+oo_bench.irfs.lp_exp_xi(1:10))./(oo_bench.steady_state(10)+oo_bench.steady_state(21)),'k-','LineWidth',1.5);
hold all
plot(100*(oo_opt.irfs.ls_exp_xi(1:10)+oo_opt.irfs.lp_exp_xi(1:10))./(oo_opt.steady_state(10)+oo_opt.steady_state(21)),'b--','LineWidth',1.5);
hold all
plot(100*(oo_cc.irfs.ls_exp_xi(1:10)+oo_cc.irfs.lp_exp_xi(1:10))./(oo_cc.steady_state(10)+oo_cc.steady_state(21)),'c:','LineWidth',1.5);
hold all
plot(100*(oo_joint.irfs.ls_exp_xi(1:10)+oo_cc.irfs.lp_exp_xi(1:10))./(oo_joint.steady_state(10)+oo_cc.steady_state(21)),'r:','LineWidth',1.5);
title('商业银行贷款规模','FontSize',14,'interpreter','latex')
box off


subplot(3,3,8)
plot(100*oo_bench.irfs.lp_bar_exp_xi(1:10)./oo_bench.steady_state(20),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.lp_bar_exp_xi(1:10)./oo_opt.steady_state(20),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.lp_bar_exp_xi(1:10)./oo_cc.steady_state(20),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.lp_bar_exp_xi(1:10)./oo_joint.steady_state(20),'r:','LineWidth',1.5);
title('影子银行贷款规模','FontSize',14,'interpreter','latex')
box off

subplot(3,3,9)
plot(100*oo_bench.irfs.pr_exp_xi(1:10)./oo_bench.steady_state(38),'k-','LineWidth',1.5);
hold all
plot(100*oo_opt.irfs.pr_exp_xi(1:10)./oo_opt.steady_state(38),'b--','LineWidth',1.5);
hold all
plot(100*oo_cc.irfs.pr_exp_xi(1:10)./oo_cc.steady_state(38),'c:','LineWidth',1.5);
hold all
plot(100*oo_joint.irfs.pr_exp_xi(1:10)./oo_joint.steady_state(38),'r:','LineWidth',1.5);
title('影子银行违约率','FontSize',14,'interpreter','latex')
box off
legend('基准情况','最优结构性货币政策规则','最优资本监管规则','联合最优规则','FontSize',12,'interpreter','latex')
legend('boxoff')