% Copyright (C) 2018 Daisuke Ikeda, Takushi Kurozumi
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or (at
% your option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see http://www.gnu.org/licenses/.

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

% DO NOT CHANGE THIS PART.
% Here we load the values of the deep parameters in a loop.
%
% 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
% initialize indicator
check = 0;
%
% END OF THE FIRST MODEL INDEPENDENT BLOCK.

% THIS BLOCK IS MODEL SPECIFIC.
% Here the user has to define the steady state.
%--------------------------------------------------------------------------
thetaY_p = etaY_p/(etaY_p-1);
thetaN_p = etaN_p/(etaN_p-1);
thetaA_p = etaA_p/(etaA_p-1);
thetaX_p = etaX_p/(etaX_p-1);
gammaAst_p = 1 + gammaAstAR_p/400;
pi_p = 1 + piAR_p/400;
r_p = 1 + rAR_p/400;
beta_p = gammaAst_p*pi_p/r_p;

nU = 1;
uU = 1;
gamAstU = gammaAst_p;
lamU = lambda_p;
dphiU = 1;
gamU = gamAstU^((1-alpha_p)/(thetaA_p-1));
gamTFPU = gamAstU^(1-alpha_p);
deltaKU = deltaK_p;
piU = pi_p;
qU = 1;
mU = beta_p/gamAstU;
rU = gamAstU*piU/beta_p;

rTauU = 1 + (1-tau_p)*(rU - 1);
aU = (1 + 1/lambda_p*(gamU/(1-deltaA_p) - 1))^(-1);
sU = 1/(thetaX_p*thetaY_p);
pinv = 1;
zetaPU = 1;
muU = 1/xi_p*(rU/rTauU - 1);
kU = gamAstU*(1/(alpha_p*sU)*(gamAstU*(1 + muU*(1-xi_p))/beta_p - (1-deltaK_p)*(1 + muU)))^(-1/(1-alpha_p));
delta1_p = sU*alpha_p/(1 + muU)*(1/(kU/gamAstU))^(1-alpha_p);
ddeltaKU = delta1_p;
wU = sU*(1-alpha_p)/(1 + muU)*(kU/gamAstU)^alpha_p;
vU = beta_p*sU*(thetaX_p-1)/(gamU*(1 + muU*(1-xi_p)) - beta_p*(1-deltaA_p)*(1 + muU))*(kU/gamAstU)^alpha_p;
yU = (kU/gamAstU)^alpha_p;
iU = (1 - (1-deltaK_p)/gamAstU)*kU;
lU = wU + iU + vU*(gamU - (1-deltaA_p));
bU = rU*(kU + vU*gamU - lU/xi_p);
dU = thetaX_p*sU*(kU/gamAstU)^alpha_p + (1/rTauU - 1/(gamAstU*piU))*bU - wU - iU - vU*(gamU - (1-deltaA_p));
d_p = dU;
phiU = dU;
iaU = omega_p*lambda_p*vU*(1-deltaA_p)*(gamU - beta_p*(1-deltaA_p))/(gamU - beta_p*(1-deltaA_p)*(1-lambda_p*(1-omega_p)));
jU = gamU*lambda_p*vU*(1-deltaA_p)*(1-omega_p)/(gamU - beta_p*(1-deltaA_p)*(1-lambda_p*(1-omega_p)));
lambda0_p = lambda_p/iaU^omega_p;
idU = beta_p*(1-deltaA_p)*(gamU - (1-deltaA_p))/(gamU*aU)*jU;
chiZ_p = (gamU - (1-deltaA_p))/idU^rho_p;
gdpU = yU - iaU*(1/aU - 1);
cU = (1-ggdp_p)*gdpU - iU - idU;
upP1U = thetaX_p*sU/(1-beta_p*xiP_p)*yU/cU;
upP2U = 1/(1-beta_p*xiP_p)*yU/cU;
upW1U = 1/(1-beta_p*xiW_p);
upW2U = 1/(1-beta_p*xiW_p)*wU/cU;
chiN_p = wU/(thetaN_p*cU);
zetaWU = 1;
drgdprU = bU/rU*(1/(piU*gamAstU)-1)/gdpU;
drgdpr_p = drgdprU; 
xiU = xi_p;
eAU = 1;
eGU = ggdp_p;
eRU = 0;
epr = 1;

sschiZ_p = chiZ_p;
sslambda0_p = lambda0_p;

obs_dlogGDP100dm = 0;
obs_dlogCON100dm = 0; 
obs_dr400dm = 0;
obs_piU400dm = 0;


idyearU = 0;
obs_rd = 0;
obs_adp = 0;

AUX_ENDO_LAG_35_1 = gdpU;
AUX_ENDO_LAG_35_2 = gdpU;
AUX_ENDO_LAG_35_3 = gdpU;
AUX_ENDO_LAG_1_1 = cU;
AUX_ENDO_LAG_1_2 = cU;
AUX_ENDO_LAG_1_3 = cU;


AUX_ENDO_LAG_8_1 =0;
AUX_ENDO_LAG_15_1=0;
AUX_ENDO_LAG_8_2 =0;
AUX_ENDO_LAG_15_2=0;
AUX_ENDO_LAG_8_3 =0;
AUX_ENDO_LAG_15_3=0;
AUX_ENDO_LAG_8_4 =0;
AUX_ENDO_LAG_15_4=0;
AUX_ENDO_LAG_8_5 =0;
AUX_ENDO_LAG_15_5=0;
AUX_ENDO_LAG_8_6 =0;
AUX_ENDO_LAG_15_6=0;

AUX_ENDO_LAG_7_1 = iaU;
AUX_ENDO_LAG_7_2 = iaU;
AUX_ENDO_LAG_7_3 = iaU;
AUX_ENDO_LAG_7_4 = iaU;
AUX_ENDO_LAG_7_5 = iaU;
AUX_ENDO_LAG_7_6 = iaU;
AUX_ENDO_LAG_17_1=piU;
AUX_ENDO_LAG_17_2=piU;


idyearU = 0;
iayearU = 0;

%--------------------------------------------------------------------------
% END OF THE MODEL SPECIFIC BLOCK.


% DO NOT CHANGE THIS PART.
% Here we define the steady state values of the endogenous variables of the model.
%
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
