
clear all;
close all;

% Set the option for plots
set(0, 'DefaultAxesFontSize',      5);
set(0, 'DefaultTextFontSize',      5);
set(0, 'DefaultUicontrolFontSize', 5);
set(0, 'DefaultAxesLinewidth',   1.5);
set(0, 'DefaultLineLinewidth',   1.5);
set(0, 'DefaultPatchLinewidth',  1.5);


% Declare the calibrated value of each parameter
BETA  = 0.99;                 % subjective discount rate
SIGMA = 2.00;                 % inverse of elasticity of intertemporal substitution
PHI   = 2.00;                 % inverse of Frisch elasticity of substitution %1.00
%CHI   = 1.00;                 % weight of disutility of labor

GAMMA      = 0.40;            % degree of openness for consumption goods   
%GAMMA_STAR = 0.25;            % degree of openness for consumption goods of the Foreign

THETA      = 3.00;            % elasticity of substitution between home and foreign goods % 2.00;
EPSILON    = 10.00;           % elasticity of subsitution between domestic goods %2.00
ALPHA      = 0.33;            % capital share in production function %0.60
DELTA      = 0.02;            % depreciation rate of capital

XI_I       = 2.48;            % charge of investment installation
XI_P       = 50.00;            % degree of price sluggishness %0.01
XI_B       = 0.01;            % magnitude of portfolio adjustment cost

BF_GDP     = 0.49;            % external-bond-to-GDP ratio at SS % 0.25


%% Steady States
A          = 1;
pi         = 1;
pH         = 1;
piH        = 1;
Q          = 1;
dE         = 1;
Rstar      = 1/BETA;
R          = 1/BETA;
r          = pi/BETA;
q          = 1; 
rk         = 1/BETA-(1-DELTA);
mc         = 1;
Ystar      = 1;
YH         = 1;                          % normalization
GDP        = pH*YH;
bF         = BF_GDP*GDP;
K_YH       = ALPHA*(mc/rk);
K          = K_YH*YH;
I          = DELTA*K;
N          = K_YH^(-ALPHA/(1-ALPHA))*YH;
N_YH       = N/YH;
w          = (1-ALPHA)*N_YH^(-1)*mc;
C          = YH-I-(1-1/BETA)*bF;        % Country budget constraint
lambda     = C^(-SIGMA);
CHI        = lambda*w/N^PHI;
GAMMA_STAR = 1-(1-GAMMA)*(C+I);
S          = 1;

%N=1/3;                                 % predetermined

save params ...
BETA SIGMA PHI CHI GAMMA GAMMA_STAR THETA EPSILON ALPHA DELTA ...
XI_I XI_P XI_B ...
BF_GDP...%;
A pi pH piH Q dE Rstar R r q rk ... 
mc Ystar YH GDP bF K_YH K I N N_YH w C lambda...
S;
%RHO_A RHO_R RHO_RF RHO_YF RHO_P RHO_Y RHO_E...


%% Run Dynare using the parameters
%% The option noclearall avoid loosing all data in memory after the code
dynare CESOE.mod noclearall 

Welf_pos     = find(strcmp('Welf', M_.endo_names));
% Unconditional welfare
WelfunCESOE  = oo_.mean(Welf_pos);
save WelfunCESOE
% Conditional welfare
WelfconCESOE = oo_.dr.ys(Welf_pos) ...
               + 0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(Welf_pos));
save WelfconCESOE;

outputsCESOE = oo_;
save outputsCESOE;

volCESOE  = [outputsCESOE.var(2,2), outputsCESOE.var(3,3), ...
             outputsCESOE.var(4,4), outputsCESOE.var(5,5), ...
             outputsCESOE.var(6,6), outputsCESOE.var(7,7), ...
             outputsCESOE.var(8,8), outputsCESOE.var(9,9)];  
% variances of RealGDP Inflation Consumption  Investment 
% ForeignBonds TradeBalance TermsOfTrade  InterestRate 
save  volCESOE;

