%steady.M (Using Rotemberg adjustment cost in introducing sticky prices)
%GIVEn pD

%function [errcheck] = steady(piD); 
%This program produces the deep structural parameters  of the model described in Philippine SOE model 

%global am_SS an_SS ax_SS bstar_SS cD_SS cF_SS epimstar_SS epsilon_SS ... 
%       epx_SS er_SS gdp_SS hD_SS hF_SS hn_SS hx_SS lam_SS ... 
%       m_SS mc_SS mr_SS pD_SS pF_SS piD_SS pim_SS PIMSTAR pin_SS ... 
%       pn_SS PX qf_SS qn_SS qx_SS r_SS remit_SS rstar_SS stderr_SS ... 
%       stderr_SS stderr_SS STD_EPIMSTAR_SS STD_EPX_SS STD_ER_SS tb_SS ... 
%       wD_SS wF_SS zet_SS; 
  
%global lmult1_SS lmult2_SS lmult3_SS lmult4_SS lmult5_SS lmult6_SS ... 
%       lmult7_SS lmult8_SS lmult9_SS lmult10_SS lmult11_SS lmult12_SS ... 
%       lmult13_SS lmult14_SS lmult15_SS lmult16_SS lmult17_SS lmult18_SS ... 
%       lmult19_SS lmult20_SS lmult21_SS lmult22_SS lmult23_SS lmult24_SS ... 
%       lmult25_SS lmult26_SS lmult27_SS lmult28_SS lmult29_SS lmult30_SS ... 
%       lmult31_SS; 

piD =1.0099;
%Calibration restrictions
a = 1; %set to 1 initially
BETA = 0.9844; %0.9844; try 0.99 also
TAU = 2.15; %adjustment cost parameter
XI = 17.5; %0.65 index of price stickiness (McNelis' value)
PSSI = 0.001; %risk-premium parameter
%CHI = 1; % price indexation parameter (tenative value inferred from McNelis)
%ETA = 6; % 6 1/ETA is the elasticity of substitution for the diffentiated nontradable goods (McNelis 2008)
THETA = 0.261; %0.261Mexican data to correspond to SHX; %0.6 labor share in exportable sector (following McNelis)
ALPHAN = 0.56479466; % share of nontradable goods in domestic consumption ; MEXICAN DATA: 0.579
ALPHAX = 0.091487248; % share of exportables in domestic consumption; MEXICAN DATA: 0.210
ALPHAM = 0.343718092; % share of imported goods in domestic consumption; MEXICAN DATA: 0.211
nbeta = BETA;
NU = 0.1772; %0.3496 SGU value
NUW = 0.1776; % wage
NUP = 1.075248355; %price US CPI/Phil CPI
PX = 1.0000; %1.131493 (U.S. dollar base); 1.08019213 Php base
PIMSTAR = 1.000027593;
SX = 0.413155293; %share of traded value in GDP
SREM = 0.037784499; %0.009671494 - 1981-1999 average
SHN = 0.359;  % MEXICAN DATA - labor share in nontraded sector
H = 0.20;
MU = 0.1; %try 0.1 to 0.2
PID = 1.02276395; %GROSS DOMESTIC INFLATION 1981Q1-2009Q4 using GDP deflation 1985=100
STD_EPX = 0.053940355;
STD_EPIMSTAR = 0.041643769;
STD_ER = 0.246213507;
COV_PXPIM = 0.00200146692811011;
COV_PXR = 0.00754103122551312;
COV_PIMR = 0.00498139462322658;
RHOPX1 = 0.6958;
RHOPX2 = -0.5166;
RHOPIM1 = -1.1784;
RHOPIM2 = -0.1630;
RHOER1 = 1.2500;
RHOER2 = -0.2684;

%Implied Parameters and Steady-State Values of Endogenous Variables
ETA = 1 + 1/MU; % if MU = 0.2 then ETA = 6
OMEGA = SHN *(1+MU); %McNelis - 0.8 labor coefficient in home goods sector
WD_PGDP = (THETA*SX + ((ETA-1)/ETA)*OMEGA*(1-SX))/H; %from calibration; this is wD/pDgdp
r0 = PID/BETA;
zet = 1-1/r0;
SC = (1-SX)/ALPHAN; %calibrated consumption share
CHI = a*WD_PGDP*(1-H)/(SC*(1+NU*zet));
fe = 1 - (CHI/a)*SC*(1+NU*zet)/((1/NUW)*WD_PGDP - (SC*(1+NU*zet)) - SREM); %cost of labor migration

%%steady states
pim = piD;
pin = pim;
r = pim/BETA;
zeta = 1 - 1/r;
rstar = BETA/PIMSTAR; %problematic <1
epsilon = pim/PIMSTAR;
SCD = (1-SX)/ALPHAN;
WDPGDP = (THETA*SX + ((ETA-1)/ETA)*OMEGA*(1-SX)) + (CHI/a)*SCD*(1+NU*zeta);
hD = (THETA*SX + ((ETA-1)/ETA)*OMEGA*(1-SX))*(1/WDPGDP);
hF = 1 - NUW*(1-hD)/(1-fe);
hx = THETA*SX*(1/WDPGDP);
hn = hD - hx;
qn = hn^OMEGA;
qx = hx^THETA;
wD = THETA*PX*qx/hx;
wF = wD/NUW;
pgdp = wD/WDPGDP;
tb = (SX - ALPHAX*(1-SX)/ALPHAN - ALPHAM*(1-SX)/ALPHAN)*pgdp;
pc = (1-SX)*pgdp/ALPHAN;
m = NU*pc;
LAMBDA = a/(pc*(1+NU*zeta));
mc = wD*hn/(OMEGA*qn);
pn = (ETA/(ETA-1))*mc;
pD = (pn/ALPHAN)^ALPHAN * (PX/ALPHAX)^ALPHAX * (1/ALPHAM)^ALPHAM;
pF = NUP*pD;
gdp = pgdp/pD;
cD = pc/pD;
cF = cD*(1+NU*zeta)/NUP;
remit = wF*hF - pF*cF;
bstar = (remit + tb)/(1-rstar/PIMSTAR);
am = ALPHAM*pc;
ax = ALPHAX*pc/PX;
mr = mc;
qf = cD;
an = qn;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%error check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
err(1) = a/cD - LAMBDA*pD*(1+NU*zeta);
err(2) = a/cF - LAMBDA*NUP*pD;
err(3) = CHI/(1-hD) - LAMBDA*wD;
err(4) = CHI/(1-hF) - LAMBDA*wD*(1-fe)/NUW;
err(5) = BETA/PIMSTAR - rstar;
err(6) = 1/r - BETA/pim;
err(7) = 1-zeta - BETA/pim;
err(8) = mc - wD/(OMEGA*hn^(OMEGA-1));
err(9) = qn - hn^OMEGA;
err(10) = mr - mc;
err(11) = mr - ((ETA-1)/ETA)*pn;
err(12) = m - NU*pD*cD;
err(13) = remit - wF*hF + pF*cF;
err(14) = pin - pim;
err(15) = ALPHAN*pD*(qn^ALPHAN)*(ax^ALPHAX)*(am^ALPHAM) - pn*qn;
err(16) = ALPHAX*pD*(qn^ALPHAN)*(ax^ALPHAX)*(am^ALPHAM) - PX*ax;
err(17) = ALPHAM*pD*(qn^ALPHAN)*(ax^ALPHAX)*(am^ALPHAM) - am;
err(18) = qf - (qn^ALPHAN)*(ax^ALPHAX)*(am^ALPHAM);
err(19) = qx - hx^THETA;
err(20) = THETA*PX*hx^THETA - wD*hx;
err(21) = hD - hn - hx;
err(22) = qf - cD;
err(23) = bstar*(1-rstar/PIMSTAR) - remit - tb;
err(24) = tb - PX*(qx-ax) + am;
err(25) = pim - epsilon*PIMSTAR;
err(26) = piD - pim;
err(27) = gdp - (pn*qn + PX*qx)/pD;
err(28) = wD - NUW*wF;
err(29) = pF - NUP*pD;
err(30) = an - qn;

if max(abs(err)) > .1e-8
    error('fatal failed to compute the steady state')
    disp(max(abs(err)));
end

consD_SS = cD; 
pD_SS = pD;	
zet_SS = zeta;
cF_SS = cF;
pF_SS = pF;
hD_SS = hD;
wD_SS = wD;
hF_SS = hF;
wF_SS = wF;
rstar_SS = rstar;
r_SS = r;
lam_SS = LAMBDA;
mc_SS = mc;
hn_SS = hn;
mr_SS = mr;
pn_SS = pn;
pin_SS = pin;
m_SS = m;
remit_SS = remit;
an_SS = an;
ax_SS = ax;
am_SS = am;
qx_SS = qx;
hx_SS = hx;
qf_SS = qf;
qn_SS = qn;
bstar_SS = bstar;
tb_SS = tb;
epsilon_SS = epsilon;
pim_SS = pim;
piD_SS = piD;
gdp_SS = gdp;

save steady.mat a BETA nbeta fe XI PSSI ETA THETA OMEGA ALPHAN ALPHAX ALPHAM NU NUW NUP CHI PX PIMSTAR STD_EPX STD_EPIMSTAR STD_ER COV_PXPIM COV_PXR COV_PIMR RHOPX1 RHOPX2 RHOPIM1 RHOPIM2 RHOER1 RHOER2 consD_SS pD_SS zet_SS cF_SS pF_SS hD_SS wD_SS hF_SS wF_SS rstar_SS r_SS lam_SS mc_SS hn_SS mr_SS pn_SS pin_SS m_SS remit_SS an_SS ax_SS am_SS qx_SS hx_SS qf_SS qn_SS bstar_SS tb_SS epsilon_SS pim_SS piD_SS gdp_SS
%ramseyprob_out_lmss;

%save steady.mat a BETA nbeta fe XI PSSI ETA THETA OMEGA ALPHAN ALPHAX ALPHAM NU NUW NUP CHI PX PIMSTAR STD_EPX STD_EPIMSTAR STD_ER COV_PXPIM COV_PXR COV_PIMR C_SS pD_SS zet_SS cF_SS pF_SS hD_SS wD_SS hF_SS wF_SS rstar_SS r_SS lam_SS mc_SS hn_SS mr_SS pn_SS pin_SS m_SS remit_SS an_SS ax_SS am_SS qx_SS hx_SS qf_SS qn_SS bstar_SS tb_SS epsilon_SS pim_SS piD_SS gdp_SS
