%% Script to compute steady state of the model:

function [ys,check] = oilpol_steadystate(ys,exo)
% function [ys,check] = NK_baseline_steadystate(ys,exo)
% computes the steady state for the NK_baseline.mod and uses a numerical
% solver to do so
% Inputs: 
%   - ys        [vector] vector of initial values for the steady state of
%                   the endogenous variables
%   - exo       [vector] vector of values for the exogenous variables
%
% Output: 
%   - ys        [vector] vector of steady state values fpr the the endogenous variables
%   - check     [scalar] set to 0 if steady state computation worked and to
%                    1 of not (allows to impos restriction on parameters)

global M_ 

% read out parameters to access them with their name
NumberOfParameters = M_.param_nbr;
for ii = 1:NumberOfParameters
  paramname = deblank(M_.param_names(ii,:));
  eval([ paramname ' = M_.params(' int2str(ii) ');']);
end
% initialize indicator
check = 0;


%% Enter model equations here

%tic

% CALIBRATION:

    Numsec = 2;                                 % # of industries
    
    betta = 0.99;                            % Time discount factor
    sigma = 1;                             % Inverse of IES
    varphi = 2;                              % Inverse of Frisch
	phi = [0.60,0.40];                     % CD-production materials Home
	phiF = [0.60,0.40];                    % CD-production materials Foreign
    psi = 1 - phi;                              % CD-production labor Home
    psiF = 1 - phiF;                            % CD-production labor Foreign
    xi = [0.35,0.65];                      % SS Consumption share (of total consumption) Home
    xiF = [0.35,0.65];                     % SS Consumption share (of total consumption) Foreign
    zeta =  [0.70, 0.30;                  % SS Material share (of total material) Home (Note: each column sums to one)
             0.30, 0.70];
    zetaF = [0.70, 0.30;                  % SS Material share (of total material) Foreign (Note: each column sums to one)
             0.30, 0.70];
    gammaex = [0.60, 0.15];                 % SS export-GDP ratio
    gammaim = [0.60, 0.15];                 % SS import-GDP ratio
    L = 1/3;                                    % Normalization of hours Home
    eps_p = 0.2;                          % Markup goods markets
    eps_w = 0.2;                          % Markup labor markets
    theta_pj = [0.25, 0.75];
    
    % Oil sector:
    gammao = 0.2;    % Oil share of total GDP
    alphao = 0.2;      % Non-land share of oil output
    phio = 0.7;%.16;  % Use of mainland intermediates
    psio = 1 - phio;    % Use of mainland labor
    Oilshare_ss = gammao/(1 - gammao);  % Oil share of mainland GDP.
    zetao = [0.4, 0.6]; % Oil input shares from goods and services.
    
    if length(zeta) ~= Numsec
        error('Error: Input-output matrix must be square and of size equal to # sectors!');
    end
    
    SS_INF = 1.000;

%% Recursive steady state solution:

% REST OF THE WORLD:
RF = SS_INF/betta;
BarPrjF = ((1 - theta_pj)./(1 - theta_pj*SS_INF^(1/eps_p))).^eps_p;
VjF = (1 - theta_pj).*BarPrjF.^(-(1 + eps_p)/eps_p)./(1 - theta_pj*SS_INF^((1 + eps_p)/eps_p));
RMCjF = (BarPrjF/(1 + eps_p)) .* (1 - betta*theta_pj*SS_INF^((1 + eps_p)/eps_p)) ./ (1 - betta*theta_pj*SS_INF^(1/eps_p));
% Normalization
CF = 1;
% C_j = xi_j*C
CjF = xiF*CF;
% Y_j = C_j + M_{j1} + ... + M_{jJ}
iomatrixF = [zetaF(1,1)*phiF(1), zetaF(1,2)*phiF(2);
             zetaF(2,1)*phiF(1), zetaF(2,2)*phiF(2)];
XjF = ((eye(Numsec) - iomatrixF)\CjF')';
YjF = XjF.*VjF;
% 1 + eps_p = 1 + Phi_j/Y_j
PhijF = (1./RMCjF - VjF).*XjF;%eps_p*YjF;
% M_j/Y_j = phi_j
MjF = phiF.*XjF;%phiF.*YjF;
MjjF = bsxfun(@times, zetaF, MjF);
% GDP = Omega*L
OmegaF = CF/L;
% Omega = (1 + eps_w)*MRS
chi_NF = OmegaF/((L^varphi)*(CF^sigma)*(1 + eps_w));
% RMC_j = Z_j^(-1) * (1/phi_j)^(phi_j) * (Omega/psi_j)^(psi_j)
ZjF = (1./RMCjF) .* (1./phiF).^(phiF) .* (OmegaF./psiF).^(psiF);
% Omega*N_j/Y_j = psi_j
NjF = psiF.*XjF/OmegaF;%psiF.*YjF/OmegaF;
% L = N_j/mu_j
mujF = NjF/L;
% Value added
GDPjF = (1 - phiF).*XjF;%YjF - MjF;
GDPF = sum(GDPjF);
LambdaF = CF^(-sigma);
G1F = LambdaF*XjF.*RMCjF./(1 - betta*theta_pj*SS_INF^((1 + eps_p)/eps_p));
G2F = LambdaF*XjF.*BarPrjF./(1 - betta*theta_pj*SS_INF^(1/eps_p));
TildeG1F = SS_INF^((1 + eps_p)/eps_p)*G1F;
TildeG2F = SS_INF^(1/eps_p)*G2F;

gdpF = log(GDPF);
vaF = log(CF);
cF = log(CF);
lambdaF = log(LambdaF);
lambdaauxF = log(LambdaF/betta);
picF = log(SS_INF);
rrF = log(1/betta);
rF = log(RF);
nF = log(L);
rwF = log(OmegaF);
Consumption = CF;
Hours = L;
UtilityF = log(CF) - chi_NF*(L)^(1 + varphi)/(1 + varphi);
WelfareF = UtilityF/(1 - betta);
for i=1:Numsec
  % Variables
  evalc(sprintf('c%dF = log(CjF(i))', i));
  evalc(sprintf('gdp%dF = log(GDPjF(i))', i));
  evalc(sprintf('y%dF = log(YjF(i))', i));
  evalc(sprintf('x%dF = log(XjF(i))', i));
  evalc(sprintf('m1%dF = log(MjjF(1,i))', i));
  evalc(sprintf('m2%dF = log(MjjF(2,i))', i));
  evalc(sprintf('m%dF = log(MjF(i))', i));
  evalc(sprintf('n%dF = log(mujF(i)*L)', i));
  evalc(sprintf('l%dF = log(L)', i));
  evalc(sprintf('mrs%dF = log(OmegaF/(1 + eps_w))', i));
  evalc(sprintf('piw%dF = log(SS_INF)', i));
  evalc(sprintf('rw%dF = log(OmegaF)', i));
  evalc(sprintf('pip%dF = log(SS_INF)', i));
  evalc(sprintf('pr%dF = log(1)', i));
  evalc(sprintf('barpr%dF = log(BarPrjF(i))', i));
  evalc(sprintf('g1%dF = log(G1F(i))', i));
  evalc(sprintf('tildeg1%dF = log(TildeG1F(i))', i));
  evalc(sprintf('g2%dF = log(G2F(i))', i));
  evalc(sprintf('tildeg2%dF = log(TildeG2F(i))', i));
  evalc(sprintf('v%dF = log(VjF(i))', i));
  evalc(sprintf('prm%dF = log(1)', i));
  evalc(sprintf('rmc%dF = log(RMCjF(i))', i));
  evalc(sprintf('z%dF = log(ZjF(i))', i));
  % Parameters
  evalc(sprintf('theta_p%d = theta_pj(i)', i));
  evalc(sprintf('Phi%dF = PhijF(i)', i));
  evalc(sprintf('phi%dF = phiF(i)', i));
  evalc(sprintf('psi%dF = 1 - phiF(i)', i));
  evalc(sprintf('xi%dF = xiF(i)', i));
  evalc(sprintf('mu%dF = mujF(i)', i));
  evalc(sprintf('zeta1%dF = zetaF(1,i)', i));
  evalc(sprintf('zeta2%dF = zetaF(2,i)', i));
end



% CHECK SOLUTION:

if abs(sum(mujF) - 1) >= 1e-10
    warning('Warning: Employment shares in labor markets do not add up to total labor force');
end
if any(sum(zetaF,1)) ~= 1
    warning('Warning: Material shares do not sum to 1');
end

%% end own model equations

for iter = 1:length(M_.params) %update parameters set in the file
  eval([ 'M_.params(' num2str(iter) ') = ' M_.param_names(iter,:) ';' ])
end

NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
  varname = deblank(M_.endo_names(ii,:));
  eval(['ys(' int2str(ii) ') = ' varname ';']);
end