function [ys,params,check] = MaPCoord_Lv_steadystate(ys,exo,M_,options_)

% function [ys,params,check] = MODEL_FILENAME_steadystate(ys,exo,M_,options_)
% computes the steady state for the MODEL_FILENAME.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
%   - M_        [structure] Dynare model structure
%   - options   [structure] Dynare options structure
%
% Output: 
%   - ys        [vector] vector of steady state values for the the endogenous variables
%   - params    [vector] vector of parameter values
%   - check     [scalar] set to 0 if steady state computation worked and to
%                    1 of not (allows to impose restrictions on parameters)

%Code chunk for dynare 4.5.7 and older.
%function [ys,check]        = CGMPCoord_Lv_steadystate(ys,exo) %first line for older version of dynare (4.5.7 or older)
  %global M_

%Code chunk for dynare 4.5.7 and older.
  %NumberOfParameters = M_.param_nbr;
  %for ii = 1:NumberOfParameters
    %paramname = deblank(M_.param_names(ii,:));
    %eval([ paramname ' = M_.params(' int2str(ii) ');']);
  %end

% read out parameters to access them with their name
NumberOfParameters = M_.param_nbr;
for ii = 1:NumberOfParameters
  paramname = M_.param_names{ii};
  eval([ paramname ' = M_.params(' int2str(ii) ');']);
end

% This bit is suggested by jpfeiffer. It brings values that I declare in main .mod file and recognizes if
% dynare changes the value when looking for a SS that is conditional on the value of the instruments

NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
  varname = M_.endo_names{ii};
  eval([varname '=ys(' int2str(ii) ');']);
end



% initialize indicator
check = 0;


%% Enter model equations here

options=optimset('display','off'); % set options for numerical solver


Bc = 0;
Ba = 0;
Bb = 0;
Ac = 0;
Aa = 0;
Ab = 0;
xic = 0;
xia = 0;
xib = 0;
Lambdac  = betta;
Lambdaa  = betta;
Lambdab  = betta;

%given psiD = 1
%Da = 0;
%Db = 0;

%From eqs 16-18: Capital prices - inv. EEq
Qc = 1;
Qa = 1;
Qb = 1;

%From eqs 1-3: EulerEqs (Global int rate of bonds)
R = 1/betta;
%Ra = 1/betta;
%Rb = 1/betta;

%%
%%%CALL THE NUMERICAL SOLUTION HERE SO THAT WE GET K
%%

% Can comment taus here. These are now declared in tha main mod.
% tauc = -0.863858183772768*1;      
% taua = -0.696564676442737*1;
% taub = -0.696564676442737*1;


% Define the instruments as Dynare calls it
tools = [tauc taua taub];
%tools = [-0.863858183772768  -0.696564676442737   -0.696564676442737]; % Cooperative SS values (under commitment) using Christiano et al 2007

x = find_ss(tools); %calls external file function that solves for the rest of the SS numerically

Cc  = x(1);  %(long_name='Consumption center')
Ca  = x(2);  %(long_name='Consumption emergent country 1(incumbent)')
Cb  = x(3);  %(long_name='Consumption emergent country 2(entrant)')

Hc  = x(4);  %(long_name='(Labor) Hours worked in center')
Ha  = x(5);  %(long_name='(Labor) Hours worked in emergent country 1')
Hb  = x(6);  %(long_name='(Labor) Hours worked in emergent country 2')

% Firm related variables
Yc  = x(7);  %(long_name='Output-Center')
Ya  = x(8);  %(long_name='Output-Emergent country 1')
Yb  = x(9);  %(long_name='Output-Emergent country 2')

Kc  = x(10);  %(long_name='Capital-Center')
Ka  = x(11);  %(long_name='Capital-Emergent country 1')
Kb  = x(12);  %(long_name='Capital-Emergent country 2')

% factor remunerations
%rc  = x(13);  %(long_name='rate of return of capital-Center')
%ra  = x(14);  %(long_name='rate of return of capital-Emergent country 1')
%rb  = x(15);  %(long_name='rate of return of capital-Emergent country 2')

%Wc  = x(16);  %(long_name='Wage-Center')
%Wa  = x(17);  %(long_name='Wage-Emergent country 1')
%Wb  = x(18);  %(long_name='Wage-Emergent country 2')

Rkc  = x(13);  %(long_name='Gross rate of return of capital-Center')
Rka  = x(14);  %(long_name='Gross rate of return of capital-Emergent country 1')
Rkb  = x(15);  %(long_name='Gross rate of return of capital-Emergent country 2')

%Balance Sheet variables
Nc  = x(16);  %(long_name='Net worth of rep. bank-Center')
Na  = x(17);  %(long_name='Net worth of rep. bank-Emergent country 1')
Nb  = x(18);  %(long_name='Net worth of rep. bank-Emergent country 2')

Fa  = x(19);  %(long_name='Foreign Lending-Emergent country 1')
Fb  = x(20);  %(long_name='Foreign Lending-Emergent country 2')

% Deposits // (EME Deposits will be set as D=V*(psi-1)
Dc  = x(21);  %(long_name='Deposits-Center')
%Da  = x(28);  %(long_name='Deposits-EME1')
%Db  = x(29);  %(long_name='Deposits-EME2')

Rdc = x(22);  %(long_name='Deposits Rate-Center')

Rba  = x(23);  %(long_name='Foreign Loans rate-Emergent country 1')
Rbb  = x(24);  %(long_name='Foreign Loans rate-Emergent country 2')

jc  = x(25);  %(long_name='Derivative of value function of banks J-Center')
ja  = x(26);  %(long_name='Derivative of value function of banks J-Emergent country 1')
jb  = x(27);  %(long_name='Derivative of value function of banks J-Emergent country 2')

muc  = x(28);  %(long_name='Lagrange multiplier of ICC-Center')
mua  = x(29);  %(long_name='Lagrange multiplier of ICC-Emergent country 1')
mub  = x(30);  %(long_name='Lagrange multiplier of ICC-Emergent country 2')

%Tc  = x(41);  %(long_name='Lump sum tax-Center')
%Ta  = x(42);  %(long_name='Lump sum tax-Emergent country 1')
%Tb  = x(43);  %(long_name='Lump sum tax-Emergent country 2')

%Bc  = x0(41);  %(long_name='Bonds-Center')
%Ba  = x0(42);  %(long_name='Bonds-Emergent country 1')
%Bb  = x0(43);  %(long_name='Bonds-Emergent country 2')
%%

%From eqs 19-21: Capital stock dynamics
Ic = delta*Kc;
Ia = delta*Ka;
Ib = delta*Kb;

%% Additional variables for analysis (welfare, spread, etc)

%Welfare
Uc = Cc^(1-sigmma)/(1-sigmma) - Hc^(1+pssi)/(1+pssi);
Ua = Ca^(1-sigmma)/(1-sigmma) - Ha^(1+pssi)/(1+pssi);
Ub = Cb^(1-sigmma)/(1-sigmma) - Hb^(1+pssi)/(1+pssi);

Wc = Uc/(1-betta);
Wa = Ua/(1-betta);
Wb = Ub/(1-betta);

% Spread
Spra = Rka - Rba;
Sprb = Rkb - Rbb;
Sprc = Rkc - Rdc;

% Leverage
Leva = Qa*Ka/(Na);
Levb = Qb*Kb/(Nb);
Levc = Qc*Kc/(Nc); 

% % % Intermediation

% Inta = Qa*Ka;
% Intb = Qb*Kb;
% Intc = Qc*Kc + Fa + Fb;

%%

%% end own model equations

params=NaN(NumberOfParameters,1);
for iter = 1:length(M_.params) %update parameters set in the file
  eval([ 'params(' num2str(iter) ') = ' M_.param_names{iter} ';' ])
end

NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
  varname = M_.endo_names{ii};
  eval(['ys(' int2str(ii) ') = ' varname ';']);
end

%Code chunk for dynare 4.5.7 and older.

  % 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
