% 0. HOUSEKEEPING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;
clear all;
close all;
tic;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. MODEL PARAMETERIZATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global alpha beta sigma h Fri delta b zetta gamma Lstar epsilon lambda  Z zphi g_y ;

  alpha  = 0.36;      %capital share in production function
  beta   = 0.9942;    %quarterly discount factor 
  sigma  = 1;         %risk aversion parameter
  h      = 0.815;     %habit formation parameter
  Fri    = 0.276;
  delta  = 0.0212;    %quarterly depreciation rate of capital
  b      = 0.03078;   %capacity utilization parameter
  zetta  = 7.2;       %elasticity of marginal depreciation w.r.t utilization rate
  gamma  = 0.9685;    %survival probability of bankers (0.955)
  Lstar  = 0.3333;    %labor hours
  lambda = 0.1548;    %fraction of diverted funds by bankers (0.1047)
  epsilon = 0.001;  %amount of startup funds as a percentage of net worth
  Z      = 0.027;
  zphi   = 5.25;
  g_y    = 0.20;
  
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2. DETERMINISTIC STEADY-STATE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

global x0;
x0 = [1.4 2.4027 11 0.26 3.10 0.0198 0.0101 10 4 1.2 0.105 1.76 13 0.025 0.03 0.02 0.015 0.01 0.00 3.75 4.1 0.001 0.001];

options = optimset('Display', 'iter', 'MaxIter', 1e8, 'MaxFunEvals', 1e8, 'FunValCheck', 'on', 'TolFun', 1e-15, 'Algorithm', 'levenberg-marquardt' );

[x,fval,exitflag] = fsolve(@baseline1_ss,x0,options);  % Call optimizer

c_ss = x(1);
nu = x(2);
k_ss = x(3);
i_ss = x(4);
w_ss = x(5);
rk_ss = x(6);
rd_ss = x(7);
s_ss = x(8);
n_ss = x(9);
y_ss = x(10);
nu_ss = x(11);
eta_ss = x(12);
d_ss = s_ss - n_ss;
lev_ss = d_ss/n_ss;
car_ss = n_ss/s_ss;
u_ss = x(13);
delta_ss = x(14);
RK_ss = x(15);
RD_ss = x(16);
RB_ss = x(17);
rb_ss = x(18);
PI_ss = x(19);
g_ss = x(20);
m_ss = x(21);
B_ss = x(22);
ms_ss= x(23);





SS_RBC_realrigidities = [c_ss Lstar k_ss i_ss w_ss rk_ss rd_ss s_ss n_ss y_ss nu_ss eta_ss d_ss lev_ss car_ss u_ss delta_ss RK_ss RD_ss RB_ss rb_ss PI_ss  g_ss m_ss B_ss ms_ss ]';
     
save('SS_values','SS_RBC_realrigidities');