%% Defining Global Parameters
global alpha mu alpha_u mu_u alpha_f mu_f alpha_uf mu_uf varpi varpi_i varpi_u vartheta varpi_f varpi_if varpi_uf vartheta_f varrho_k varrho_u varrho_kf varrho_uf sigma_c sigma_n sigma_cf sigma_nf nu %Elasticities and input shares 
global C_ss GDP_ss GDP_ssf Yt_ss Yt_ssf Yd_ss O_uss C_ssf  Yd_ssf Y_Hss Y_Hssf Y_Fss Y_Fssf O_ussf C_css C_cssf  C_Hss C_Hssf C_Fss C_Fssf I_Hss I_Hssf I_Fssf I_Fss I_uHss I_uHssf I_uFss I_uFssf I_ss I_ssf I_uss I_ussf  Phi_ss Phi_ssf Omega_ss Omega_uss Omega_ssf Omega_ussf R_kss R_kssf R_uss R_ussf N_ss N_uss N_ssf N_ussf K_ss Psi_uss Psi_usss Psi_usssf K_ssf Psi_ussf O_Huss O_Hussf O_Fussf O_cuss O_cussf %Endogenous variables
global beta beta_f h h_f  %Exogenous calibrated variables - even in estimation, we would have these as calibrated
global Oa_ss Oa_ssf Delta_wss Delta_wuss Delta_Hpss Delta_Hfpss  Delta_wssf Delta_wussf  Delta_Fpssf  Delta_Fpss Pi_Hss Pi_Hssf Pi_ss Pi_Fss Pi_Fssf Pi_ssf Pi_wss Pi_wssf Pi_css Pi_cssf Pi_oss %Prices, inflation, wage inflation, optimal reset prices and price dispersion
global psi_w psi_uw psi_p psi_wf psi_pf iota_w iota_p iota_wf iota_pf psi_uwf %Calvo parameters and indexation rules 
global Ta_ss Ta_ssf  Ea_ss Ea_ssf Sa_ss Sa_ssf TB_ss TB_ssf %Nominal and real exchnage rate, and the terms of trade
global Gt_ss Gt_ssf Go_ss Go_ssf %Transformed price indexes
global delta_u delta delta_f delta_uf %Capital depreciation across sectors
global O_uwss O_dwss Po_ss %Oil market clearing
global theta_w theta_p theta_wf theta_pf %Markup elasticities  (Market concet=ntration of firms)
global rho_r rho_g rho_gy rho_gpi rho_gp rho_y rho_e rho_rf  rho_gf rho_gyf rho_gpif rho_yf rho_ef  R_ss F_ss F_ssf R_ssf rho_pi rho_pif tau tau_f  %Monetary and fiscal policy persitence variables 
global aH aC aO aF aH_f aF_f aO_f aC_f xi xi_u xi_f xi_uf %degree of home or foriegn bias in consumption and investment baskets 
global L_Hss L_Hssf G_ss G_uss M_ss Lam_ss Q_ss Q_uss U_ss L_Fss L_Fssf G_ssf G_ussf M_ssf Lam_ssf Q_ssf Q_ussf U_ssf z1 z1_f %directly observable based on steady state assumptions 
global  Zeta_ss Zeta_uss Upsilon_ss Upsilon_uss Xi_Hss Xi_Hssf Sigma_Hss Sigma_Hssf Zeta_ssf Zeta_ussf Upsilon_ssf Upsilon_ussf Xi_Fss Sigma_Fss Xi_Fssf Sigma_Fssf %auxiliary variables in dynamic wage and price setting  
global eps_ss rho_eps eps_uss rho_u Ex_ss eta %productivity shock

%% Setting Calibrated Parameter - Would be estimated later on
% Home Country
beta=0.91; %Literature for low income countries 
sigma_c=2; %nverse of intertemporal elasticity of substitution for consumption Berg et al (2013)
sigma_n=10; %Inverse Frisch Berg et al (2013)
alpha=0.30; %literature for lower income countries 
mu=0.05; %labour share in output is 0.65
alpha_u=0.55;
mu_u=0.3;
psi_w=0.9; %wages adjusting wages every 7 quarters 
psi_uw=0.8;
psi_p=0.8; %firms changes prices every 5 quarters 
iota_p=1; %Full indexation of prices
iota_w=1; %full indexation of wages
rho_r=0.7; %monetartry policy - interest rate smoothing -slightly below developed world
rho_y=0.6; %monetary response to output
rho_e=0.25; %monetary response to exchange rate
rho_pi=2; % monetary response to Inflation
rho_g=0.7; %fiscal smoothing parameter
rho_gy= 0.5;
rho_gpi= 0.1;
rho_gp=0.3;
vartheta=9;
varpi=1.15; 
varpi_i=1.5; %Ravenna and Natalucci (2008) monetary policy in emerging econoies
varpi_u=1.5;
varrho_k=25; %Investment adjustment cost - non-oil
varrho_u=20; %Investment adjustment cost -oil Berg et al (2013)
theta_p=4; %around 10% markup
theta_w=4; %around 10% markup
delta=0.025; %literature standard depreciation rate 
delta_u=0.020;
aH=0.72;
aF=0.28; %Degree of openess Cook (2004) - emerging economies 
aC=0.9460;
aO=0.054; % oil intensity in consumption 
xi=0.72;
xi_u=0.72;
h=0.7;
eta=0.5; %not used

%Foreign Country - calibrated to US economy
beta_f=0.99; %literature for high income countries 
sigma_cf=1; %Log consumption preferences - also coincides with estimate from smets and wouters.
sigma_nf=1; 
alpha_f=0.35; %literature for high income countries 
mu_f=0.15; %Literature labour shares in output is 0.5-0.65
alpha_uf=0.59; %Bergholt and Larsen (2016)
mu_uf=0.22;
nu=0.6;
psi_wf=0.75; %average duration of wage spells of four quarters
psi_uwf=0.75;
psi_pf=0.75; %Gali (2015)
iota_pf=1; %Full indexation of wages
iota_wf=1; %Full indexation of prices %Challe and Giantsaorou (2015)
rho_rf=0.8; %monetary policy interest rate smoothign parameter 
rho_yf=0.6;%Interest rate response to output Challe and Giantsaorou (2014) - US economy
rho_ef=0.2; %%Not used no need to be calibrated
rho_pif=1.5; %Interest rate response to inflation Challe and Giantsaorou (2014)

rho_gf=0.9; %Fiscal Smoothing parameter - Bergholt and Larsen (2016)
rho_gyf=0.5; %fiscal response to GDP - Bergholt and Larsen (2015)
rho_gpif=0.1; % fiscal response to inflation 
vartheta_f=12;
varpi_f=0.425; %elasticity of substitution oil and consumption Killian (2012)
varpi_if=1.75; %Elasticity of substiution trade Killian (2012)
varpi_uf=1.75;
varrho_kf=5.74; %Smets and Wouters (2007) Estimate
varrho_uf=7.6;
theta_pf=7.66; %15% markup
theta_wf=6; %20%markup
delta_f=0.025; %literature standard depreciation rate for physical capital
delta_uf=0.015; %No empirical estimate 
aH_f=0.068; %share of home goods in foriegn basket - coincides with xi and xi_u
aF_f=0.9320;
aC_f=0.9880; %share of consumption good in aggregated backet Nakov and pescataori (2010a,b)
aO_f=0.012; %Oil intensity in consumption - Bergholt Larsen and Sennecca (2017), Nakov and Pescataori (2010a,b)
xi_f=0.068;
xi_uf=0.068;
h_f=0.8; %US empirical estimate

%% Steady State values directly observed or normalised
% Home Country
Pi_oss=1; %gross oil price inflation
Pi_ss=1; %Gross inflation rate - CPI
Pi_Hss=1; %Gross inflation rate of home prices
Pi_Hssf=1;
Pi_css=1;%Gross inflation rate - Consumption based CPI
Pi_wss=1; %Gross wage inflation 
Delta_wss=1; %Wage dispersion
Delta_wuss=1;
Delta_Hpss=1; %Price dispersion
Delta_Hfpss=1;
G_ss=1; %Relative optimal reset wage
G_uss=1;
L_Hss=1;
L_Hssf=1;
M_ss =beta;
Q_ss=1; %Tobins Q
Q_uss=1;
R_kss=(1/beta) +delta -1; %Rental rate of Capital
R_uss=(1/beta) +delta_u -1;
z1=R_uss;
Phi_ss=(theta_p-1)/theta_p; %Real marinal cost equals markup in s.s
U_ss = 1; %Normalisation of utilisation of rigs
tau = 0.095; %Royalty paid to government 

%Foreign Country 
Pi_ssf=1;
Pi_Fss=1;
Pi_Fssf=1;
Pi_cssf=1;
Pi_wssf=1;
Delta_Fpssf=1;
Delta_Fpss=1;
Delta_wssf=1;
Delta_wussf=1;
G_ssf=1; 
G_ussf=1;
L_Fss=1;
L_Fssf=1;
Q_ssf=1;
Q_ussf=1;
M_ssf =beta_f;
R_kssf=(1/beta_f) +delta_f -1;
R_ussf=(1/beta_f) +delta_uf -1;
z1_f=R_ussf;
Phi_ssf=(theta_pf-1)/theta_pf;
U_ssf=1;
tau_f =0.8; %Royalty paid to government - tax rate on oil from Bergholt and larsen (2016)

%% Shocks 
eps_ss=1; %productivity shock home country
rho_eps=0.9; %shock persistence 

eps_uss=1; %oil productivity shock
rho_u=0.8; %shock persistence 
%% Monetary policy from Bond euler equation
%R_ss = beta^(-1); %rate of interest %ramsey - instrument R decided by
%initval condition and equals 1
R_ssf = beta_f^(-1);



%% The transformed price indexes and Exchnage rates.
%Home country
Oa_ss=1; %ratio of oil price to consumption good price
Ta_ss=1; %Terms of trade at home
Ea_ss=1; % Nominal exchange rate
Gt_ss = (aH + aF*Ta_ss^(1-varpi))^(1/(1-varpi));
Go_ss= (aC + aO*Oa_ss^(1-vartheta))^(1/(1-vartheta));

%Foreign Country
Oa_ssf=1;
Ta_ssf=(1/Ta_ss);
Ea_ssf=1;
Sa_ssf=1;
Gt_ssf = (aF_f + aH_f*Ta_ssf^(1-varpi_f))^(1/(1-varpi_f));
Go_ssf = (aO_f*((Oa_ss*Gt_ss)/(Gt_ssf*Ta_ss))^(1-vartheta_f) + aC_f)^(1/(1-vartheta_f));

Sa_ss=((Go_ssf*Gt_ssf*Ta_ss)/(Go_ss*Gt_ss));

%% Call the function and guess the initial values to enable faster convergence
x0=ones(22,1); %initial guess
fun = @Ramsey_ss;
options=optimoptions(@fsolve,'MaxFunEvals',1000000,'MaxIter',1000000);
[x,fval]=fsolve(fun,x0,options)

%Home
C_ss=x(1);
Yd_ss=x(2);
O_uss=x(3);
O_Huss =x(4);
O_cuss=x(5);
N_ss=x(6);
N_uss=x(7);

%Foreign
C_ssf=x(8);
Yd_ssf=x(9);
O_ussf=x(10);
N_ssf=x(11);
N_ussf=x(12);
O_Fussf=x(13);
O_Hussf=x(14);
O_cussf=x(15);
Po_ss=x(16);
O_uwss=x(17);
O_dwss=x(18);
I_ss=x(19);
I_ssf=x(20);
I_uss=x(21);
I_ussf=x(22);

%% Deriving the other steady state values based on the Recursive FSOLVE values obtained
%Home country
Ex_ss = O_uss - O_cuss - O_Huss;
Lam_ss=C_ss^(-sigma_c); %Ramsey so no habit formation

%Consumption and Investment
C_css=aC*Go_ss^(vartheta)*C_ss;
C_Hss = aH*Gt_ss^(varpi)*C_css;
I_Hss=xi*Gt_ss^(varpi_i)*I_ss;
I_uHss=xi*Gt_ss^(varpi_u)*I_uss;
C_Fss=aF*(Gt_ss/Ta_ss)^(varpi)*C_css;
I_Fss=(1-xi)*(Gt_ss/Ta_ss)^(varpi_i)*I_ss;
I_uFss=(1-xi_u)*(Gt_ss/Ta_ss)^(varpi_u)*I_uss;
%capital
K_ss=I_ss/delta;
Psi_uss=I_uss/delta_u;
Psi_usss=U_ss*Psi_uss;

%Dynamic Wage setting 
Zeta_ss = ((Omega_ss*N_ss)/(1-psi_w*beta));
Zeta_uss = ((Omega_uss*N_uss)/(1-psi_uw*beta));
Upsilon_ss=Zeta_ss;
Upsilon_uss=Zeta_uss;

%Dynmaic Price setting
Y_Hss= aC*aH*(Gt_ss^(varpi))*(Go_ss^(vartheta))*C_ss + xi*(Gt_ss^(varpi_i))*I_ss + xi_u*(Gt_ss^(varpi_u))*I_uss;
Y_Hssf=aC_f*aH_f*((Gt_ssf/Ta_ssf)^(varpi_f))*(Go_ssf^(vartheta_f))*C_ssf + xi_f*((Gt_ssf/Ta_ssf)^(varpi_if))*I_ssf + xi_uf*((Gt_ssf/Ta_ssf)^(varpi_uf))*I_ussf;
Xi_Hss = (Y_Hss/(1-psi_p*beta));
Xi_Hssf = (Y_Hssf/(1-psi_p*beta));
Sigma_Hss=Xi_Hss;
Sigma_Hssf=Xi_Hssf;


%% Foreign Country
Lam_ssf=(1-h_f)^(-sigma_cf)*C_ssf^(-sigma_cf);

%Consumption and investment
C_cssf=aC_f*Go_ssf^(vartheta_f)*C_ssf;
C_Hssf=aH_f*(Gt_ssf/Ta_ssf)^(varpi_f)*C_cssf;
C_Fssf=aF_f*(Gt_ssf^(varpi_f))*C_cssf;
I_Hssf=xi_f*(Gt_ssf/Ta_ssf)^(varpi_if)*I_ssf;
I_uHssf=xi_uf*(Gt_ssf/Ta_ssf)^(varpi_uf)*I_ussf;
I_Fssf =(1-xi_f)*(Gt_ssf^(varpi_if))*I_ssf;
I_uFssf=(1-xi_uf)*(Gt_ssf^(varpi_uf))*I_ussf;
%Capital
K_ssf=I_ssf/delta_f;
Psi_ussf=I_ussf/delta_uf;
Psi_usssf=U_ssf*Psi_ussf;

%Dynamic Wage setting 
Zeta_ssf = ((Omega_ssf*N_ssf)/(1-psi_wf*beta_f));
Zeta_ussf = ((Omega_ussf*N_ussf)/(1-psi_uwf*beta_f));
Upsilon_ssf=Zeta_ssf;
Upsilon_ussf=Zeta_ussf;

%Dynmaic Price setting
Y_Fssf= aF_f*aC_f*(Gt_ssf^(varpi_f))*(Go_ssf^(vartheta_f))*C_ssf + (1-xi_f)*(Gt_ssf^(varpi_if))*I_ssf + (1-xi_uf)*(Gt_ssf^(varpi_uf))*I_ussf;
Y_Fss=aF*aC*(Gt_ss/Ta_ss)^(varpi)*(Go_ss^(vartheta))*C_ss + (1-xi)*(Gt_ss/Ta_ss)^(varpi_i)*I_ss + (1-xi_u)*(Gt_ss/Ta_ss)^(varpi_u)*I_uss;
Xi_Fss = (Y_Fss/(1-psi_pf*beta_f));
Xi_Fssf = (Y_Fssf/(1-psi_pf*beta_f));
Sigma_Fss=Xi_Fss;
Sigma_Fssf=Xi_Fssf;

%Trade Balance
TB_ssf = (1/(Gt_ssf*Go_ssf))*Y_Fss - (Ta_ss/Gt_ssf*Go_ssf)*Y_Hssf - Po_ss*Ex_ss;
TB_ss = Po_ss*Ex_ss + (1/(Go_ss*Gt_ss))*Y_Hssf - (Ta_ss/Gt_ss*Go_ss)*Y_Fss;

%GDP
GDP_ssf = C_ssf + I_ssf + I_ussf + TB_ssf;
GDP_ss = C_ss + I_ss + I_uss + TB_ss;

%% Aggregated output 
%Home
Yt_ss = (aH^(1/varpi)*Y_Hss^((varpi-1)/varpi) + aF^(1/varpi)*Y_Fss^((varpi-1)/varpi))^(varpi/(varpi-1));

%Foreign
Yt_ssf=(aH_f^(1/varpi_f)*Y_Hssf^((varpi_f-1)/varpi_f) + aF_f^(1/varpi_f)*Y_Fssf^((varpi_f-1)/varpi_f))^(varpi_f/(varpi_f-1));