% Model equations to be entered here

global p_theta p_nu p_u p_beta p_delta p_mu p_alpha xi yi p_epsilon p_phi p_sigma p_omega p_phim
 
p_theta = 0.99; % savers' discount rate

p_beta = 0.985; % borrowers' discount rate

p_delta = 0.01; %quarterly house depreciation rate

p_mu = 0.75; %share of durables which is used as collateral

p_alpha = 0.84; % share of consumables in consumption basket

xi = 0.7; %share of profits paid to savers

yi = 0.1; %share of profits paid to Hand to Mouth

p_epsilon =11; % %elast of substitution 6 - 11

p_sigma =  2; %intertemp elast 1-4

p_phi =  1/3; %elast of labor 1/3 - 3

p_omega = 0.75; % calvo parameter

p_nu = 0.3; %scaling of poor workers labor in production 

p_u  = 0.4; %scaling of Borrowers labor in production

p_phim = 1;

 
x0 = [1, 1, 1, 1, 1, 1, 1, 0.5, 1, 1];
[x,fval] = fsolve(@BTL_I_NSS,x0); % Solving the nonlinear system

C_b0 = x(1);
H_b0 = x(2);
w_b0 = x(3);
C_s0 = x(4);
H_s0 = x(5);
w_s0 = x(6);
C_p0 = x(7);
H_r0 = x(8);
w_p0 = x(9);
q_r0 = x(10);

eta0 = ((p_epsilon-1)/p_epsilon);

Rate0 = 1/p_theta;

Y_c0 = C_b0 + C_s0 + C_p0;

Y_h0 = p_delta*(H_b0+H_s0);

real_lagrange1_b0 = p_alpha*C_b0^(p_alpha-1)*(H_b0-H_r0)^(1-p_alpha)*(C_b0^p_alpha*(H_b0-H_r0)^(1-p_alpha))^(-p_sigma);

real_lagrange2_b0 = real_lagrange1_b0*(1-p_beta*Rate0)/Rate0;

d_o0 = (p_mu*(H_b0-H_r0))/Rate0;

real_lagrange_s0 = p_alpha*C_s0^(p_alpha-1)*H_s0^(1-p_alpha)*(C_s0^p_alpha*H_s0^(1-p_alpha))^(-p_sigma);

N_p_c0 = (eta0*p_nu*Y_c0)/w_p0;

N_b_c0 = (eta0*p_u*Y_c0)/w_b0;

N_s_c0 = (eta0*(1-p_u-p_nu)*Y_c0)/w_s0;

N_p_h0 = (eta0*p_nu*Y_h0)/w_p0;

N_b_h0 = (eta0*p_u*Y_h0)/w_b0;

N_s_h0 = (eta0*(1-p_u-p_nu)*Y_h0)/w_s0;

N_p0 = N_p_c0 + N_p_h0;

N_b0 = N_b_c0 + N_b_h0;

N_s0 = N_s_c0 + N_s_h0;

t_b0 = (1-xi-yi)*(1-eta0)*(Y_c0+Y_h0);

t_p0 = yi*(1-eta0)*(Y_c0+Y_h0);

t_s0 = xi*(Y_c0-w_s0*N_s_c0-w_b0*N_b_c0-w_p0*N_p_c0+Y_h0-w_s0*N_s_h0-w_b0*N_b_h0-w_p0*N_p_h0);

q_h0 = 1;

pie0 = 0;

Z_c0 = 1;

Z_h0 = 1;

B_s0 = d_o0; 

R_s0 = Rate0;

real_lagrange1_b = 1;
w_b = 1;
w_p = 1;
N_p = 1;
q_r = 1;
C_p = 1;
H_r = 1;
t_p = 1;
N_b = 1;
C_b = 1;
H_b = 1;
q_h = 1;
real_lagrange2_b = 1;
Rate_o = 1;
d_o = 1;
t_b = 1;
N_s = 1;
C_s = 1;
H_s = 1;
w_s = 1;
real_lagrange_s = 1;
B_s = 1;
eta = 1;
Y_c = 1;
Z_c = 1;
N_b_c = 1;
N_s_c = 1;
N_p_c = 1;
N_p_h = 1;
N_b_h = 1;
N_s_h = 1;
Z_h = 1;
Y_h = 1;
t_s = 1;
R_s = 1;
pie = 0;


% //Hand to Mouth Workers
  E1  = p_alpha*C_p^(p_alpha-1)*C_p0^(p_alpha-1)*H_r^(1-p_alpha)*H_r0^(1-p_alpha)*(C_p^p_alpha*H_r^(1-p_alpha)*C_p0^p_alpha*H_r0^(1-p_alpha))^(-p_sigma)*w_p*w_p0-N_p^p_phi*N_p0^p_phi;
  E2  = ((C_p^p_alpha*C_p0^p_alpha*H_r^(1-p_alpha)*H_r0^(1-p_alpha))^(-p_sigma))*(-p_alpha*C_p^(p_alpha-1)*C_p0^(p_alpha-1)*H_r^(1-p_alpha)*H_r0^(1-p_alpha)*q_r*q_r0...
       +(1-p_alpha)*C_p^p_alpha*C_p0^p_alpha*H_r^(-p_alpha)*H_r0^(-p_alpha));
  E3  = N_p*N_p0*w_p*w_p0+t_p*t_p0-C_p*C_p0-H_r*H_r0*q_r*q_r0;

%//Borrowers
  E4  =  real_lagrange1_b*w_b*real_lagrange1_b0*w_b0-N_b^p_phi*N_b0^p_phi;
  E5  =  (1-p_alpha)*(H_b*H_b0-H_r*H_r0)^(-p_alpha)*C_b^p_alpha*C_b0^p_alpha*(C_b^p_alpha*C_b0^p_alpha*(H_b*H_b0-H_r*H_r0)^(1-p_alpha))^-(p_sigma)...
           -real_lagrange1_b*real_lagrange1_b0*q_h*q_h0+real_lagrange2_b*real_lagrange2_b0*p_mu*q_h*q_h0...
           +p_beta*real_lagrange1_b*real_lagrange1_b0*(1-p_delta)*q_h*q_h0;  
  E6  =  (1-p_alpha)*(H_b*H_b0-H_r*H_r0)^(-p_alpha)*C_b^p_alpha*C_b0^p_alpha*(C_b^p_alpha*C_b0^p_alpha*(H_b*H_b0-H_r*H_r0)^(1-p_alpha))^-(p_sigma)...
           -real_lagrange1_b*real_lagrange1_b0*q_r*q_r0+real_lagrange2_b*real_lagrange2_b0*p_mu*q_h*q_h0;
  E7  =  p_alpha*C_b^(p_alpha-1)*C_b0^(p_alpha-1)*(H_b*H_b0-H_r*H_r0)^(1-p_alpha)*(C_b^p_alpha*(H_b*H_b0-H_r*H_r0)^(1-p_alpha)*C_b0^p_alpha)^(-p_sigma)*w_b*w_b0-N_b^p_phi*N_b0^p_phi;
  E8  =  real_lagrange1_b*real_lagrange1_b0-real_lagrange2_b*real_lagrange2_b0*Rate_o*Rate0-(p_beta*Rate_o*Rate0*real_lagrange1_b*real_lagrange1_b0)/(1+pie);
  E9  =  H_r*H_r0*q_r*q_r0+N_b*N_b0*w_b*w_b0+d_o*d_o0+t_b*t_b0-C_b*C_b0-q_h*q_h0*(H_b*H_b0-(1-p_delta)*H_b*H_b0)-(Rate_o*Rate0*d_o*d_o0)/(1+pie);
  E10  =  p_mu*q_h*q_h0*(H_b*H_b0-H_r*H_r0)-Rate_o*Rate0*d_o*d_o0;
 
 % //Savers
  E11  =  real_lagrange_s*real_lagrange_s0*w_s*w_s0-N_s^p_phi*N_s0^p_phi;
  E12  =  (1-p_alpha)*H_s^(-p_alpha)*H_s0^(-p_alpha)*C_s^p_alpha*C_s0^p_alpha*(C_s^p_alpha*C_s0^p_alpha*H_s^(1-p_alpha)*H_s0^(1-p_alpha))^(-p_sigma)-real_lagrange_s*real_lagrange_s0*q_h*q_h0...
           +p_theta*real_lagrange_s*real_lagrange_s0*(1-p_delta)*q_h*q_h0;
  E13  =  (C_s^p_alpha*C_s0^p_alpha*H_s^(1-p_alpha)*H_s0^(1-p_alpha))^(-p_sigma)*p_alpha*C_s^(p_alpha-1)*C_s0^(p_alpha-1)*H_s^(1-p_alpha)*H_s0^(1-p_alpha)*w_s*w_s0-N_s^p_phi*N_s0^p_phi;
  E14 = -real_lagrange_s*real_lagrange_s0+(p_theta*real_lagrange_s*real_lagrange_s0*R_s*Rate0)/(1+pie);
  E15 = C_b*C_b0+C_s*C_s0+C_p*C_p0+(p_omega/2)*Y_c*Y_c0*pie^2-Y_c*Y_c0;
 
  %//Consumption good Firms
  E16 = pie*(1+pie)-(1/2)*pie^2*p_epsilon...
          -(1-p_epsilon)/p_omega-(p_epsilon/p_omega)*eta*eta0...
          -(p_theta*real_lagrange_s*real_lagrange_s0)/(real_lagrange_s*real_lagrange_s0)*pie*(Y_c/Y_c)*(1+pie);
  E17 = ((Y_c*Y_c0)/(Z_c*Z_c0))*(w_p^(p_nu)*w_p0^(p_nu)/p_nu^(p_nu))*(w_b^(p_u-1)*w_b0^(p_u-1)/p_u^(p_u-1))*(w_s^(1-p_nu-p_u)*w_s0^(1-p_nu-p_u)/(1-p_nu-p_u)^(1-p_nu-p_u))-N_b_c*N_b_c0;
  E18 = ((Y_c*Y_c0)/(Z_c*Z_c0))*(w_p^(p_nu-1)*w_p0^(p_nu-1)/p_nu^(p_nu-1))*(w_b^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