%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Collateral Constraints in a SOE: Imported Imputs %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%
%%%%%%%%%%%%%%%%%%%%%%%Endogenous Variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var
C      % Consumption
I      % Investment 
B      % External debt
K      % Capital 
Y      % Output 
H      % Hours worked 
lambda % Marginal utility 
phi    % Collateral constraint multiplier  
Rz     % Foreign interest rate
%Now there are two additional variables
M      % Imported imput
S      % Real exchange rate: number of domestic goods for one unit of foreign good. If it goes up, real depreciation for the SOE
;


%%
%%%%%%%%%%%%%%%%%%%%%%%Exogenous Variables%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
varexo 
vmz;   % Foreign interest rate innovation
     
%%
%%%%%%%%%%%%%%%%%%%%%%%Parameters%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameters 
beta theta eta a alpha delta gamma Z rhoz Rzss mu epsilon
;
beta=0.9755;   % Discount factor
theta=1;       % Inverse of Frisch elasticity
eta=1;         % Inverse of EIS
a=4.95;        % Labor supply shifter 
alpha=0.30;    % Capital share
delta=0.025;   % Depreciation rate
gamma=0.2238;  % LTV
Z=1.0891;      % TFP
rhoz=0.95;     % AR(1) coefficient of the exogenous process
mu=0.15;       % Imports share
Rzss=1.02;     % Steady state foreign interest rate
epsilon=1;     % Exports elasticity

%%
%%%%%%%%%%%%%%%%%%%%%%%Non-Linear Model%%%%%%%%%%%%%%%%%%%%%%%%%%F%%%%%%%%%%%%%%%
model;

% These equations are new
mu*Y/M=S;                                                                  % Imports demand
Y=C+I+S^(epsilon);                                                         % Market clearing of domestic good. S^(epsilon) is export demand   

% These equations do not change compared to FCSOE
lambda=(C-a/(1+theta)*H^(1+theta))^(-eta);                                 % Marginal utility of consumption
I=K-(1-delta)*K(-1);                                                       % Capital law of motion
beta*lambda(1)/lambda*(alpha*Y(1)/K+(1-delta)*(1+gamma(1)*phi(1)))=1;      % Capital Euler equation
Rz=(1-rhoz)*Rzss(-1)+rhoz*Rz(-1)+vmz;                                      % Exogenous process

% These equations are slightly different compared to FCSOE to take into account imported inputs
Y=Z*K(-1)^(alpha)*H^(1-alpha-mu)*M^(mu);                                    % Production function
Y=C+I-S*B+S*Rz*B(-1)+S*M;                                                   % Resource constraint (now output is used also to buy imported imputs)
a*H^(theta)=(1-alpha-mu)*Y/H;                                               % Labor market equilibrium 
S*B=gamma*(K(-1)*(1-delta));                                               % Collateral constraint (now B is multiplied by S)
beta*lambda(1)/lambda*Rz(1)*S(1)/S=1-phi;                                  % Debt Euler equation (the Exchange rate plays a role)

%If you want a collateral constraints expressed in terms of future capital
%use these equations instead of Capital Euluer, Collateral constraint and
%Debt Euler
% S(1)*B*Rz(1)=gamma*(K*(1-delta));                
% beta*lambda(1)/lambda*Rz(1)*S(1)/S=1-phi*Rz(1)*S(1)/S;  
% beta*lambda(1)/lambda*(alpha*Y(1)/K+(1-delta))+phi*gamma*(1-delta)=1;  

end;


%% Steady State
initval;
B=1.4688;
C=0.8023;
H=1/3;
K=6.7311;
lambda=2.1397;
phi=0.0050;
Rz=1.0200;
Y=1;
S=0.1767;
M=0.8489;
end;
steady;
check;

%% Shocks
shocks;
var vmz; stderr 0.01/4;
end;

%% Simulation
stoch_simul(irf=100,order=1) 
Y C K B
;

