clear;
close all;
clc;

% Directory path in my office computer

path ='F:\Backup17Mei2018_from_w2k\PhD_Fed_Lift_Off\Codes\MyCodes\17Aug18\welfare\';

% Exchange rate
peg_grid = [0 1]; % this is the 'switch' for flexible or peg exchange rate regime, flexible==0, peg==1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% mat files names

mat_files = {'benchmark.mat','mp_lev.mat','mp_ltgdp.mat','mp_cg.mat','mp_ve.mat';
  'nomp_fixER.mat','mp_lev_fixER.mat','mp_ltgdp_fixER.mat','mp_cg_fixER.mat','mp_ve_fixER.mat'};

for jj = 1:length(peg_grid)
    for zz = 1:5
        
peg = peg_grid(jj);

% Macroprudential policy parameters (obtained from policy optimization)
rho_taue = 0.9;        % Shock persistence for time varying tax/subsidy
le_val = 0.01;         % Parameter for leverage instrument in macropru [==(1-rho_taue)*alpha_le]
qky_val = 0.05;        % Parameter for loan-to-GDP instrument in macropru [==(1-rho_taue)*alpha_qky]
cg_val = 0.05;         % Parameter for foreign borrowing instrument in macropru [==(1-rho_taue)*alpha_ve]
ve_val = 0.05;         % Parameter for credit growth instrument in macropru [==(1-rho_taue)*alpha_cg]

le_grid = [0 le_val 0 0 0];
qky_grid = [0 0 qky_val 0 0];
cg_grid = [0 0 0 cg_val 0];
ve_grid = [0 0 0 0 ve_val];

xi_le = le_grid(zz);    % Parameter for leverage instrument in macropru [==(1-rho_taue)*alpha_le]
xi_qky = qky_grid(zz);   % Parameter for loan-to-GDP instrument in macropru [==(1-rho_taue)*alpha_qky]
xi_cg = cg_grid(zz);    % Parameter for foreign borrowing instrument in macropru [==(1-rho_taue)*alpha_ve]
xi_ve = ve_grid(zz); 

f = 0; % assume financial integration is high, do not consider low FI
     
% Parameters

beta = 0.99;    % Time preference factor
sigma = 1;      % Household risk aversion
psi = 0.276;    % Elasticity of labour supply
zeta = 2;       % Adjustment cost of investment
theta = 0.94;   % Exit rate of bankers (benchmark: 0.9 or 0.975 or 0.94)
deltat = 0.01;  % Transfer rate to new bankers (or 0.01) (benchmark: 0.004)
nue = 0.80;     % Home bias EME (or 0.83)
nuc = 0.97;     % Home bias AE (or 0.97)
alpha = 0.3;    % Capital share
delta = 0.025;  % Capital depreciation
eta = 1.5;      % Trade elasticity
n = 0.15;       % Population's size of EME
kappae = 0.3;   % Incentive compatibility constraint parameters - EME (benchmark: 0.3)
kappac = 0.3;   % Incentive compatibility constraint parameters - AE (benchmark: 0.3)
rho = 0.85;     % Shock persistence
rho_polsh_c = 0;
phi = 0.8;      % Calvo parameter
sigmp = 6;      % Micro elasticity of substitution
pi_obj = 1;     % Inflation objective
lambda_r = 0.85;% Taylor rule autocorrelation parameter 
lambda_p = 1.2; % Taylor rule inflation parameter
lambda_y = 0.2; % Taylor rule growth parameter
std_polsh_c=0.01;
lambdae_ss = beta;
lambdac_ss = beta;
rstar_ss = 1/lambdac_ss;
pie_ss = 1;
r_e_ss = pie_ss/lambdae_ss;
mce_ss = (sigmp-1)/sigmp;
mcc_ss = (sigmp-1)/sigmp;
ke_ss = 7.25247; 
kc_ss = 9.214339819153714;
he_ss = 0.738337259929943;
hc_ss = 0.740321055817323;
ye_ss = (ke_ss^alpha)*(he_ss^(1-alpha));
yc_ss = (kc_ss^alpha)*(hc_ss^(1-alpha));
ie_ss = delta*ke_ss;
ic_ss = delta*kc_ss;
ce_ss = ((1-alpha)*mce_ss*(ke_ss^alpha)*(he_ss^(-psi-alpha)))^(1/sigma);
cc_ss = ((1-alpha)*mcc_ss*(kc_ss^alpha)*(hc_ss^(-psi-alpha)))^(1/sigma);
rke_ss = (alpha*mce_ss*ye_ss/ke_ss)+(1-delta);
rkc_ss = (alpha*mcc_ss*yc_ss/kc_ss)+(1-delta);
psid = 2;
low_fin = 0.005;

save param_17aug beta sigma psi zeta theta deltat nue nuc alpha delta eta n kappae kappac rho phi sigmp pi_obj lambda_r lambda_p lambda_y... 
std_polsh_c rho_polsh_c f peg lambdae_ss rstar_ss lambdac_ss pie_ss r_e_ss mce_ss mcc_ss ke_ss kc_ss he_ss hc_ss ye_ss yc_ss ie_ss ic_ss...
ce_ss cc_ss rke_ss rkc_ss psid low_fin rho_taue xi_le xi_qky xi_ve xi_cg 

dynare Fed_paper_17Aug_wel %noclearall nolog 

save(char(mat_files(jj,zz)),'oo_');
     end
end

Model_names=char( 'Benchmark','MP_Leverage','MP_LTGDP','MP_CreditGr','MP_Foreign','No_MP_FixER','MP_Leverage_FixER','MP_LTGDP_FixER','MP_CreditGr_FixER','MP_Foreign_FixER' ); 

%Resize mat_files into only one row 
reshape_mat_files = reshape(mat_files',1,numel(mat_files));

Num_models=length(reshape_mat_files);
omega_e_compare=zeros(1,Num_models);

for mm=1:Num_models
%load Model 
eval(['load ' reshape_mat_files{mm} ';']);

omega_e_compare(mm)=oo_.mean(strmatch('omega_e',M_.endo_names,'exact'));

end

% Consumption equivalent
pi = zeros(1,Num_models);

for ii = 1:5
    pi(ii)=(1/exp((omega_e_compare(ii)-omega_e_compare(1))*(1-beta)))-1;
end

for jj = 6:10
    pi(jj)=(1/exp((omega_e_compare(jj)-omega_e_compare(6))*(1-beta)))-1;
end

% for kk = 12:15
%     pi(kk)=(1/exp((om(kk)-om(11))*(1-beta)))-1;
% end

table_omega_e=array2table([omega_e_compare;pi],'VariableNames',cellstr(Model_names));
writetable(table_omega_e,'welfare_17Aug.xls','Sheet','17AugExchRate','WriteRowNames',true);




