%% Draft7d DSGE Model

%% Declaring Endogenous Variables
var

% Patient households
uc_p            % marginal ut. consumption
c_p             % consumption
h_p             % housing
d_p             % deposits
eh              % housing demand preferences (exogenous)

% DC - impatient households
uc_idc          % marginal ut. consumpti   on
c_idc           % consumption
h_idc           % housing
b_idc           % idc hh mortgage debt
m_idc           % idc ltv (exogenous)
n_idc           % idc labor supply

% FC - impatient households
uc_ifc          % marginal ut. consumption
c_ifc           % consumption
h_ifc           % housing
% b_ifc           % ifc hh mortgage debt
l_ifc           % ifc hh mortgage debt - expressed in domestic currency
m_ifc           % ifc ltv (exogenous)
n_ifc           % ifc labor supply

% Entrepreneurs
uc_e            % marginal ut. consumption
c_e             % consumption
k               % capital
b_e             % entrepreneurs debt
m_e             % e ltv (exogenous)
yw              % intermediate production
pw              % intermediate goods price
a               % tfp (exogenous)
u               % capital utiliyation
n               % employment

% Labor supply
w               % wages
uc              % average marginal utility

% Capital producing sector
q_k              % real capital price
i_k              % capital investment

% Housing producing sector
q_h              % real housing price
i_h              % housing investment
h               % aggregate stock of capital
pi_hh           % housing inflation

% Final good producers
y               % final good production
pi              % inflation rate

% Domestic retailers
y_h              % domestic production
p_h             % domestic production price
pi_h            % domestic production inflation

% Importing retailers
y_f             % imported production
p_f             % imported production price
pi_f            % imported production inflation

% Exporting retailers
y_h_star        % exported production
p_h_star        % exported production prices
pi_h_star       % exported production inflation

% Banking sector
l_star          % real external funding
k_b             % bank capital
j_b             % bank profits
R_bidc          % idc mortgage interest rate
R_bifc          % ifc mortgage interest rate
R_be            % entrepeneurs credit interest rate
R_d             % deposit interest rate
R_bstar          % international funding interest rate
v_idc           % capital-to-idc assets ratio (exogenous)
v_ifc           % capital-to-ifc assets ratio (exogenous)
v_e             % capital-to-e assets ratio (exogenous)

% Foreign sector
y_star          % external production (exogenous)
pi_star         % external inflation (exogenous)
Rf_star         % international risk free interest rate (exogenous)
varrho          % risk premium additional term (exogenous)

%Government
g               % government expenditures (exogenous)
T               % government transfers

%Central bank
R               % monetary policy rate

%Aggregate variables
q               % real exchange rate
y_tilde         % gdp
c;              % aggregate consumption

% 68 endogenous variables

%% Exogenous Variables (shocks)
varexo

e_h             % housing demand shock
e_midc          % idc ltv shock
e_mifc          % ifc ltv shock
e_me            % e ltv shock
e_a             % tfp shock
e_vidc          % capital-to-idc assets shock
e_vifc          % capital-to-ifc assets shock
e_ve            % capital-to-e assets shock
e_varrho         % additional rate premium term shock

% Pure shocks (they do not have persistence)
e_y_star        % shock to foreign IS curve
e_pi_star       % shock to foreign Phillips curve
e_rf_star       % shock to foreign Taylor rule
e_g             % fiscal shock
e_r;            % monetary policy shock

% 14 shocks (which allows to include 14 observed variables for estimation)

%% Setting parameters
parameters

% Strictly calibrated parameters

% Population sizes
gamma_p         % size of patient households
gamma_idc       % size of idc households
gamma_ifc       % size of ifc households
gamma_e         % size of entrepreneurs

% Preferences parameters
beta_p          % discount factor of patient households
beta_idc        % discount factor of idc households
beta_ifc        % discount factor of ifc households
beta_e          % discount factor of entrepreneurs
sigma_c         % marginal utility of consumption parameter
sigma_h         % marginal utility of housing parameter
sigma_n         % Inverse of Frish Elasticity
zeta_c          % internal habit formaton parameter

% Labour sector
theta_w         % Calvo probability
zeta_w          % wage indexation parameter
mu_w            % wage elasticity parameter

% LTV ratios
m_idc_ss        % SS LTV of idc loans
m_ifc_ss        % SS LTV of ifc loans
m_e_ss          % SS LTV of e loans

% Intermediate production sector
alpha           % capital share in intermediate production function
Psi             % standardised utilization adjustmen cost parameter
Psi_1           % utilization adjustmen cost parameter of SS

% Capital goods production sector
delta_k         % depreciation rate of capital
kappa_k         % capital adjustment cost parameter

% Housing production sector
delta_h         % depreciation rate of housing
kappa_h         % depreciation rate of housing 

% Final good production sector
eta              % home bias parameter
mu              % substitutability parameter

% Retailers
p_h_ss          % domestic retailer ss price index
theta_h         % domestic retailer Calvo parameter
zeta_h          % domestic retailer price indexation parameter
p_f_ss          % importing retailer ss price index
theta_f         % importing retailer Calvo parameter
zeta_f          % importing retailer price indexation parameter
theta_h_star    % exporting retailer Calvo parameter
zeta_h_star     % exporting retailer price indexation parameter    
mu_h_star       % exporting retailer goods substitutability parameter    

% Interest rates
R_bidc_ss       % ss of idc credit rate
R_bifc_ss       % ss of ifc credit rate
R_be_ss         % ss of e credit rate
R_bstar_ss      % ss of external funding rate
R_d_ss          % ss of deposit rate
R_ss            % ss of monetary policy rate
Rf_star_ss      % ss of international risk free rate

% Rest of banking sector
kappa_idc       % adjutment cost parameter of capital-to-idc assets ratio
kappa_ifc       % adjutment cost parameter of capital-to-ifc assets ratio
kappa_e         % adjutment cost parameter of capital-to-e assets ratio    
v_idc_ss        % ss of capital-to-idc assets ratio
v_ifc_ss        % ss of capital-to-ifc assets ratio
v_e_ss          % ss of capital-to-e assets ratio
delta_b         % managing bank capital cost parameter
omega_b         % bank dividend policy

% Aggregate variables
pi_ss           % ss of inflation

%Foreign sector
std_y_star      % size of shock to foreign IS curve
std_pi_star     % size of shock to foreign Phillips curve
std_rf_star     % size of shock to foreign Taylor rule
rho_y_star      % autoregressive term for foreign output
rho_pi_star     % autoregressive term for foreign inflation
rho_rf_star     % autoregressive term for foreign interest rate
Psi_b           % interest rate premium
varrho_ss       % interest rate premium additional term
pi_star_ss      % ss foreign inflation 

% Central Bank
phi_r           % persistance parameter 
phi_pi          % inflation stabilization parameter
phi_y           % output stabilization parameter

% Compound parameters
beta_hat        % average beta
gamma_hat       % aggregate size of households

% Ratios -----

c_ytilde        % aggregate consumption / gdp
cp_ytilde       % patient household consumption / gdp
cidc_ytilde     % idc household consumption / gdp
cifc_ytilde     % ifc household consumption / gdp
ce_ytilde       % entrepreneur consumption / gdp
cp_c            % patient household consumption / aggregate consumption
cidc_c          % idc household consumption / aggregate consumption
cifc_c          % ifc household consumption / aggregate consumption
ce_c            % entrepreneur consumption / aggregate consumption

hp_h            % patient household housing / aggregate housing
hidc_h          % idc household housing / aggregate housing
hifc_h          % ifc household housing / aggregate housing
h_ytilde        % aggregate housing / gdp
ih_ytilde       % housing investment / gdp
hidc_ytilde     % idc housing / gdp
hifc_ytilde     % ifc housing / gdp

b_kb            % total credit / bank capital
ytilde_kb       % gdp / bank capital
b_ytilde        % total credit /gdp
D_ytilde        % deposits / gdp
bidc_b          % idc credit / gdp
sbifc_b         % ifc credit /gdp
be_b            % e credit /gdp
bidc_ytilde     % idc mortgage credits / gdp
sbifc_ytilde    % ifc mortgage credits / gdp
be_ytilde       % e credits / gdp
D_kb            % deposits / bank capital
lstar_kb        % foreing bank debt / bank capital

yh_share        % yh / (yh + yhstar)
yhstar_share    % yhstar / (yh + yhstar)
t_ytilde        % transfer / gdp
pyw_ytilde       % intermediate production / gdp
yh_ytilde       % domestic production / gdp
yf_ytilde       % imports / gdp
qyhstar_ytilde  % exportes / gdp
ik_ytilde       % investment / gdp
wage_ytilde     % labour income / gdp
g_ytilde        % government expenditures / gdp
lstar_ytilde    % foreign debt / gdp
y_ytilde        % absortion / gdp

% Estimated parameters for exogenous process
rho_h           % autochorrelation of preferences shock 
std_h           % size of shock to housing preferences

rho_midc        % autochorrelation of idc ltv
std_midc        % size of shock to idc ltv

rho_mifc        % autochorrelation of ifc ltv
std_mifc        % size of shock to ifc ltv   

rho_me          % autochorrelation of e ltv
std_me          % size of shock to e ltv 

rho_a           % autochorrelation of tfp
std_a           % size of shock to tfp

rho_vidc        % autochorrelation of capital-to-idc assests ratio
std_vidc        % size of shock to capital-to-idc assests ratio

rho_vifc        % autochorrelation of capital-to-ifc assests ratio
std_vifc        % size of shock to capital-to-idc assests ratio

rho_ve          % autochorrelation of capital-to-e assests ratio
std_ve          % size of shock to capital-to-e assests ratio

rho_varrho      % autochorrelation of rate premium parameter
std_varrho      % size of shock to rate premium parameter

rho_g           % autochorrelation of government expenditures
std_g           % size of shock to government expenditures

std_r;          % size of monetary policy shock 


%% Calibration

%-------------------------------
%Calibrated parameter
%-------------------------------

% Preferences parameters
gamma_p=0.45;
gamma_idc=0.20;
gamma_ifc=0.15;
gamma_e=0.20;

% Preferences parameters
beta_idc=0.975;
beta_ifc=0.975;
beta_e=0.98;
sigma_c=2;
sigma_h=3.5;
sigma_n=3.5;
zeta_c=0.75;

% Labour sector
theta_w=0.75;
zeta_w=0.40;
mu_w=0.1;

% LTV ratios
m_idc_ss=0.7;
m_ifc_ss=0.7;
m_e_ss=0.6;

% Intermediate production
alpha=0.3;
Psi=0.2;            %Brzoza

% Capital goods
delta_k=0.025;
kappa_k=0.2;

% Housing production
delta_h=0.0125;
kappa_h=0.02;

% Final good production
eta=0.77;
mu=1;

% Retailers
p_h_ss=1;               %Brzoza
theta_h=0.75;
zeta_h=0.85;
p_f_ss=1;               %Brzoza
theta_f=0.95;
zeta_f=0.75;
theta_h_star=0.10;
zeta_h_star=0.50;
mu_h_star=1;

%Foreign sector
std_y_star=0.01;
std_pi_star=0.01;
std_rf_star=0.01;
rho_y_star=0.8;     
rho_pi_star=0.8;    
rho_rf_star=0.8;    
Psi_b=0.001;

% Central Bank
phi_r=0.70;
phi_pi=1.50;
phi_y=0.50;

% Aggregate variables
pi_ss=1.0275^(1/4);

% Rest of banking
kappa_idc=1.0;        %To be estimated?
kappa_ifc=1.0;        %To be estimated?
kappa_e=1.0;          %To be estimated?
v_idc_ss=1.61;        
v_ifc_ss=1.49;        
v_e_ss=0.16;          
delta_b=0.025;
omega_b=1;            %To be estimated?

%Interest Rates
R_ss=1.0376^(1/4);
Rf_star_ss=1.0144^(1/4);
varrho_ss=0; 
pi_star_ss=Rf_star_ss * pi_ss / R_ss;

% National Accounting Ratios
y_ytilde=0.98;                  
c_ytilde=0.64;
g_ytilde=0.11;
ik_ytilde=0.22;
qyhstar_ytilde=0.25;
yf_ytilde=0.23;
lstar_ytilde=0.35;

% Banking system ratios
b_kb=1/0.134;
b_ytilde=0.25;
bidc_b=0.08;
sbifc_b=0.09;
be_b=0.83;
D_ytilde=0.04;


%-------------------------------
% Compound parameters
%-------------------------------

% Interest rates
R_bidc_ss=R_ss;
R_bifc_ss=R_ss;
R_be_ss=R_ss;
R_d_ss=R_ss;
R_bstar_ss=Rf_star_ss + varrho_ss;

% Aggregate variables
beta_p=pi_ss/R_ss;
gamma_hat=gamma_p+gamma_idc+gamma_ifc;
beta_hat=(gamma_p/gamma_hat)*beta_p + (gamma_idc/gamma_hat)*beta_idc + (gamma_ifc/gamma_hat)*beta_ifc;
Psi_1=beta_e^(-1)-(1-delta_k)-((beta_e*R_be_ss)^(-1)-pi_ss^(-1))*m_e_ss*(1-delta_k)*pi_ss; %Brzoza

% Banking system ratios
ytilde_kb=(1/b_ytilde)*b_kb;
bidc_ytilde=b_ytilde*bidc_b;
sbifc_ytilde=b_ytilde*sbifc_b;
be_ytilde=b_ytilde*be_b;
D_kb=D_ytilde*ytilde_kb;
lstar_kb=b_kb - D_kb - 1;

% Housing ratios
ih_ytilde=y_ytilde-c_ytilde-ik_ytilde-g_ytilde;
h_ytilde=(1/delta_h)*ih_ytilde;
hidc_ytilde=(1/gamma_idc)*((m_idc_ss*(1-delta_h)*pi_ss)^(-1))*R_bidc_ss*bidc_ytilde;
hifc_ytilde=(1/gamma_ifc)*((m_ifc_ss*(1-delta_h)*pi_ss)^(-1))*R_bifc_ss*sbifc_ytilde;
hidc_h=hidc_ytilde/h_ytilde;
hifc_h=hifc_ytilde/h_ytilde;
hp_h=(1/gamma_p)*(1-gamma_idc*hidc_h-gamma_ifc*hifc_h);

% National Accounting ratios
yh_ytilde=((y_ytilde^(1/(1+mu)) - ((1-eta)^(mu/1+mu))*((yf_ytilde)^(1/(1+mu))))*(1/eta)^(mu/(1+mu)))^(1+mu);
yh_share=yh_ytilde/(yh_ytilde+qyhstar_ytilde);        
yhstar_share=qyhstar_ytilde/(yh_ytilde+qyhstar_ytilde);    
t_ytilde=g_ytilde/gamma_hat;
pyw_ytilde=(alpha*delta_k)^(-1)*Psi_1*ik_ytilde;
wage_ytilde=1-alpha;

% Consumption ratios
cidc_c=(gamma_idc*c_ytilde)^(-1)*(gamma_idc*wage_ytilde-gamma_idc*hidc_h*ih_ytilde-(R_bidc_ss/pi_ss - 1)*bidc_ytilde - gamma_idc*t_ytilde);
cifc_c=(gamma_ifc*c_ytilde)^(-1)*(gamma_ifc*wage_ytilde-gamma_ifc*hifc_h*ih_ytilde-(R_bifc_ss/pi_ss - 1)*sbifc_ytilde - gamma_ifc*t_ytilde);
ce_c=(gamma_e*c_ytilde)^(-1)*(pyw_ytilde-ik_ytilde-wage_ytilde+(1 - R_be_ss/pi_ss)*be_ytilde);
cp_c=(1-gamma_idc*cidc_c-gamma_ifc*cifc_c-gamma_e*ce_c)/gamma_p;
cp_ytilde=cp_c*c_ytilde;
cidc_ytilde=cidc_c*c_ytilde;
cifc_ytilde=cifc_c*c_ytilde;
ce_ytilde=ce_c*c_ytilde;

% Estimated parameters
rho_h=0.6;
std_h=0.01;

rho_midc=0.6;
std_midc=0.01;

rho_mifc=0.6;
std_mifc=0.01;

rho_me=0.6;
std_me=0.01;

rho_a=0.6;
std_a=0.01;

rho_vidc=0.6;
std_vidc=0.01;

rho_vifc=0.6;
std_vifc=0.01;

rho_ve=0.6;
std_ve=0.01;

rho_varrho=0.6;
std_varrho=0.01;

rho_g=0.6;
std_g=0.01;

std_r=0.01;

%% The DSGE Model (linear version)
model(linear);

% =========== Patient households ===========

% 1 Marginal utility
uc_p= -(sigma_c/(1-zeta_c))*(c_p - zeta_c*c_p(-1));
 
% 2 Euler equation
uc_p= uc_p(+1) + R_d - pi(+1);
 
% 3 Housing demand
sigma_h*h_p = -uc_p - q_h + ((beta_p*(1-delta_h))/(1-beta_p*(1-delta_h)))*(pi_hh(+1)-R_d)+ eh;

% 4 Housing price inflation
pi_hh = q_h- q_h(-1) + pi;


% =========== IDC households ===========

% 5 Marginal utility
uc_idc= -(sigma_c/(1-zeta_c))*(c_idc - zeta_c*c_idc(-1));

% 6 Housing demand
(1-beta_idc*(1-delta_h)+(1-delta_h)*(beta_idc-pi_ss/R_bidc_ss)*m_idc_ss)*(eh - sigma_h*h_idc) = uc_idc + q_h + (1-delta_h)*m_idc_ss*beta_idc*uc_idc(+1)
+(1-delta_h)*((beta_idc-(pi_ss/R_bidc_ss))*m_idc_ss)*(q_h(+1) + m_idc) - (1-delta_h)*m_idc_ss*(pi_ss/R_bidc_ss)*(pi(+1) + uc_idc - R_bidc)-(1-delta_h)*beta_idc*(uc_idc(+1)+q_h(+1));

% 7 Labour supply
w = uc_idc - sigma_n*n_idc;

% 8 IDC borrowing constraint
R_bidc + b_idc = m_idc + q_h(+1) + pi(+1) + h_idc;   
 
% 9 IDC budget constraint
gamma_idc*(cidc_ytilde)*c_idc + gamma_idc*(hidc_h)*(ih_ytilde)*(q_h + (1/delta_h)*h_idc - ((1-delta_h)/delta_h)*h_idc(-1))+(R_bidc_ss/pi_ss)*(bidc_ytilde)*(R_bidc(-1)+b_idc(-1)-pi)=gamma_idc*wage_ytilde*(w+n_idc)+(bidc_ytilde)*b_idc-gamma_idc*(t_ytilde)*T;


% =========== IFC households ===========

% 10 Marginal utility
uc_ifc= -(sigma_c/(1-zeta_c))*(c_ifc - zeta_c*c_ifc(-1));

% 11 Housing demand
(1-beta_ifc*(1-delta_h)+(1-delta_h)*m_ifc_ss*pi_ss*((beta_ifc/pi_star_ss)-(1/R_bifc_ss)))*(eh - sigma_h*h_ifc) = uc_ifc + q_h + (1-delta_h)*m_ifc_ss*beta_ifc*(pi_ss/pi_star_ss)*(uc_ifc(+1)+q(+1)-q-pi_star(+1)+pi(+1))
+(1-delta_h)*m_ifc_ss*pi_ss*((beta_ifc/pi_star_ss)-(1/R_bifc_ss))*(q_h(+1)+m_ifc)-(1-delta_h)*m_ifc_ss*(pi_ss/R_bifc_ss)*(pi(+1) + uc_ifc - R_bifc)-(1-delta_h)*beta_ifc*(uc_ifc(+1)+q_h(+1));

% 12 Labour supply
w = uc_ifc - sigma_n*n_ifc;

% 13 IFC borrowing constraint
R_bifc + l_ifc = m_ifc + q_h(+1) + pi(+1) + h_ifc;   
 
% 14 IFC budget constraint
gamma_ifc*(cifc_ytilde)*c_ifc + gamma_ifc*(hifc_h)*(ih_ytilde)*(q_h + (1/delta_h)*h_ifc - ((1-delta_h)/delta_h)*h_ifc(-1))+(R_bifc_ss/pi_star_ss)*(sbifc_ytilde)*(R_bifc(-1)+l_ifc(-1)+q-q(-1)-pi_star)=gamma_ifc*wage_ytilde*(w+n_ifc)+(sbifc_ytilde)*l_ifc-gamma_ifc*(t_ytilde)*T;


% =========== Entrepreneurs ===========

% 15 Marginal utility
uc_e = -(sigma_c/(1-zeta_c))*(c_e - zeta_c*c_e(-1));
 
% 16 Labour demand
w = pw + a + alpha*u + alpha*(k(-1)-n);
 
% 17 Capital utilization
u = Psi*(pw + a + (1-alpha)*(n-u-k(-1)));  
 
% 18 Euler equation
q_k = (1-delta_k)*beta_e*(q_k(+1) + (uc_e(+1) - uc_e)) + beta_e*Psi_1*(uc_e(+1) - uc_e + (1/Psi)*u(+1)) + 
m_e_ss*(1-delta_k)*pi_ss*(((1/R_be_ss)-(beta_e/pi_ss))*(m_e + q_k(+1)) -(1/R_be_ss)*(R_be - pi(+1)) -(beta_e/pi_ss)*(uc_e(+1) - uc_e));
 
% 19 Borrowing constraint
R_be + b_e = m_e + q_k(+1) + pi(+1) + k;
 
% 20 Production function
yw = a + alpha*(u + k(-1))+(1-alpha)*n;
 
% 21 Budget constraint
gamma_e*(ce_ytilde)*c_e = pyw_ytilde*(pw + yw) + ((1-delta_k)/delta_k)*(ik_ytilde)*(q_k + k(-1)) + (be_ytilde)*b_e - (wage_ytilde)*(w + n)-
(1/delta_k)*(ik_ytilde)*(q_k + k)-(Psi_1/delta_k)*(ik_ytilde)*u - (R_be_ss/pi_ss)*(be_ytilde)*(R_be(-1)+b_e(-1)-pi);


% =========== Labor packer ===========

% 22 Average marginal utility
uc = ((gamma_idc*((cidc_c)^(-sigma_c)))/((gamma_idc*(cidc_c)^(-sigma_c))+(gamma_ifc*(cifc_c)^(-sigma_c))+ (gamma_p*(cp_c)^(-sigma_c))))*uc_idc +
((gamma_ifc*((cifc_c)^(-sigma_c)))/((gamma_idc*(cidc_c)^(-sigma_c))+(gamma_ifc*(cifc_c)^(-sigma_c))+ (gamma_p*(cp_c)^(-sigma_c))))*uc_ifc +
((gamma_p*((cp_c)^(-sigma_c)))/((gamma_idc*(cidc_c)^(-sigma_c))+(gamma_ifc*(cifc_c)^(-sigma_c))+ (gamma_p*(cp_c)^(-sigma_c))))*uc_p;

% 23 Wages         
(theta_w/(1-theta_w))*(w-w(-1)+pi-zeta_w*pi(-1)) = ((1-beta_hat*theta_w)/(1+sigma_n*((1+mu_w)/mu_w)))*(sigma_n*n - uc - w) + ((beta_hat*theta_w)/(1-theta_w))*(w(+1)-w+pi(+1)-zeta_w*pi);


% =========== Capital goods producers ===========

% 24 Capital price
i_k = (kappa_k/(1+beta_p))*q_k + (beta_p/(1+beta_p))*i_k(+1) + (1/(1+beta_p))*(i_k(-1));
  
% 25 Capital accumulation
k = (1-delta_k)*k(-1) + delta_k*i_k;


% =========== Housing producers ===========

% 26 Housing price
i_h = (kappa_h/(1+beta_p))*q_h + (beta_p/(1+beta_p))*i_h(+1) + (1/(1+beta_p))*(i_h(-1));
 
% 27 Housing accumulation
h = (1-delta_h)*h(-1) + delta_h*i_h;


% =========== Final goods producers ===========

% 28 Homogenous final good (this equation is not considered by Brzoza)
% y = (eta^(mu/(mu+1)))*((yh_y)^(1/(mu+1)))*y_h + ((1-eta)^(mu/(mu+1)))*((yf_y)^(1/(mu+1)))*y_f;

% 29 Demand for domestic intermediate goods
y_h = -((1+mu)/mu)*p_h + y;
 
% 30 Demand for imported intermediate goods
y_f = -((1+mu)/mu)*p_f + y;

% 31 Inflation
pi = (1-eta)*((p_f_ss)^(-1/mu))*(pi_f+p_f(-1)) + (eta)*((p_h_ss)^(-1/mu))*(pi_h+p_h(-1));


% =========== Domestic retailers ===========

% 32 Domestic goods inflation
pi_h = pi + p_h - p_h(-1);
 
% 33 Domestic goods prices
(theta_h/(1+theta_h))*(p_h + pi - p_h(-1) - zeta_h*pi(-1))=(1-beta_p*theta_h)*(pw-p_h) + (beta_p*theta_h/(1-theta_h))*(p_h(+1) - p_h + pi(+1) - zeta_h*pi);
 

% =========== Importing retailers ===========

% 34 Imported goods inflation 
pi_f = pi + p_f - p_f(-1);
 
% 35 Imported goods prices
(theta_f/(1+theta_f))*(p_f + pi - p_f(-1) - zeta_f*pi(-1))=(1-beta_p*theta_f)*(q-p_f) + (beta_p*theta_f/(1-theta_f))*(p_f(+1) - p_f + pi(+1) - zeta_f*pi);

% =========== Exporting retailers ===========

% 36 Demand for exported intermediate goods 
y_h_star = - ((1+mu_h_star)/mu_h_star)*p_h_star + y_star;

% 37 Exported goods inflation
pi_h_star = pi_star + p_h_star - p_h_star(-1);
 
% 38 Exported goods prices
(theta_h_star/(1+theta_h_star))*(p_h_star + pi_star - p_h_star(-1) - zeta_h_star*pi_star(-1))=(1-beta_p*theta_h_star)*(pw-q-p_h_star)+(beta_p*theta_h_star/(1-theta_h_star))*(p_h_star(+1)-p_h_star+pi_star(+1)- zeta_h_star*pi_star);


% =========== Banking sector ===========

% 39 First order conditions
R_bidc = R - (kappa_idc / R_ss) * ((v_idc_ss)^3)* (k_b - b_idc) + (kappa_idc / R_ss) * ((v_idc_ss)^3)* v_idc;

// 40
R_bifc = R - (kappa_ifc / R_ss) * ((v_ifc_ss)^3)* (k_b - l_ifc) + (kappa_ifc / R_ss) * ((v_ifc_ss)^3)* v_ifc;

// 41
R_be = R - (kappa_e / R_ss) * ((v_e_ss)^3)* (k_b - b_e) + (kappa_e / R_ss) * ((v_e_ss)^3)* v_idc;

// 42
R_d =  R;

% 43 Bank balance sheet condition
k_b = (1/v_idc_ss)*b_idc + (1/v_ifc_ss)*l_ifc + (1/v_e_ss)*b_e - D_kb*d_p - lstar_kb*l_star;

% 44 Bank capital accumulation
pi_ss*(k_b + pi) = (1 - delta_b)*k_b(-1) + (pi_ss - (1-delta_b))*j_b(-1);

% 45 Bank profits
((pi_ss-(1-delta_b))/omega_b) * j_b = R_ss*((1/v_idc_ss)*R_bidc + (1/v_ifc_ss)*R_bifc + (1/v_e_ss)*R_be - D_kb*R - (lstar_kb*(pi_star_ss/pi_ss))*R_bstar) + (R_ss-1)*((1/v_idc_ss)*b_idc + (1/v_ifc_ss)*(l_ifc) + (1/v_e_ss)*b_e - D_kb*d_p) - ((R_ss*pi_star_ss/pi_ss)-1)*lstar_kb*l_star;

% 46 UIP condition
R - R_bstar = q(+1) - q + pi(+1) - pi_star(+1);


% =========== Foreign sector ===========

% 47 
y_star = rho_y_star * y_star(-1) + e_y_star;

% 48 
pi_star = rho_pi_star * pi_star(-1) + e_pi_star;

% 49 
Rf_star = rho_rf_star * Rf_star(-1) + e_rf_star;

% 50 Interest rate premium
R_bstar = (Rf_star_ss / R_bstar_ss) * Rf_star + (Psi_b/R_bstar_ss)*(l_star - y_tilde) + (1 - (Rf_star_ss / R_bstar_ss))*varrho;


% =========== Government ===========

% 51 Government expenditures
g = rho_g*g(-1) + e_g;
 
% 52 Government budget constraint
g=T;


% =========== Central bank ===========
// 53
R = phi_r*R(-1) + (1-phi_r)*(phi_pi*pi + phi_y*y_tilde) + e_r;


% =========== Market clearing ===========

% 54 Final goods market clearing condition
(y_ytilde)*y = (c_ytilde)*c + (ik_ytilde)*i_k + (ih_ytilde)*i_h + (g_ytilde)*g + (Psi_1/delta_k)*(ik_ytilde)*u;
 
% 55 Consumption aggregation
c = gamma_p*(cp_c)*c_p + gamma_idc*(cidc_c)*c_idc + gamma_ifc*(cifc_c)*c_ifc + gamma_e*(ce_c)*c_e;
 
% 56 Intermediate homegenous goods market clearing
yw = (yh_share)*y_h + (yhstar_share)*y_h_star;
 
% 57 Housing market clearing
h(-1) = gamma_p*(hp_h)*h_p + gamma_idc*(hidc_h)*h_idc + gamma_ifc*(hifc_h)*h_ifc;
 
% 58 Balance of payments
(p_f_ss*yf_ytilde)*(p_f + y_f) + (lstar_ytilde)*(R_ss/pi_ss)*(q - q(-1) - pi_star + l_star(-1) + R_bstar(-1)) = (qyhstar_ytilde)*(q+ p_h_star + y_h_star) + (lstar_ytilde)*l_star;
 
% 59 GDP definition
y_tilde = (y_ytilde)*y + (qyhstar_ytilde)*(p_h_star + y_h_star + q) - (p_f_ss*yf_ytilde)*(p_f + y_f);

% =========== Exogenous processes ===========

// 60
eh = rho_h*eh(-1) + eh;

// 61
m_idc = rho_midc*m_idc(-1) + e_midc;

// 62
m_ifc = rho_mifc*m_ifc(-1) + e_mifc;

// 63
m_e = rho_me*m_e(-1) + e_me;

// 64
a = rho_a*a(-1) + e_a;

// 65
v_idc = rho_vidc*v_idc(-1) + e_vidc;

// 66
v_ifc = rho_vifc*v_ifc(-1) + e_vifc;

// 67
v_e = rho_ve*v_e(-1) + e_ve;

// 68
varrho = rho_varrho*varrho(-1) + e_varrho;

end;

%% Initial Values (check ss values of somes variables)
initval;
uc_p=0;
c_p=0;
h_p=0;
d_p=0;
eh=0;
uc_idc=0;
c_idc=0;
h_idc=0;
b_idc=0;
m_idc=0;
n_idc=0;
uc_ifc=0;
c_ifc=0;
h_ifc=0;
l_ifc=0;
m_ifc=0;
n_ifc=0;
uc_e=0;
c_e=0;
k=0;
b_e=0;
m_e=0;
yw=0;
pw=0;
a=0;
u=0;
n=0;
w=0;
uc=0;
q_k=0;
i_k=0;
q_h=0;
i_h=0;
h=0;
pi_hh=0;
y=0;
pi=0;
y_h=0;
p_h=0;
pi_h=0;
y_f=0;
p_f=0;
pi_f=0;
y_h_star=0;
p_h_star=0;
pi_h_star=0;
l_star=0;
k_b=0;
j_b=0;
R_bidc=0;
R_bifc=0;
R_be=0;
R_d=0;
R_bstar=0;
v_idc=0;
v_ifc=0;
v_e=0;
y_star=0;
pi_star=0;
Rf_star=0;
varrho=0;
g=0;
T=0;
R=0;
q=0;
y_tilde=0;
c=0;
end;

%% Declaring shocks parametrisation
shocks;
var e_h; stderr std_h;
var e_midc; stderr std_midc;
var e_mifc; stderr std_mifc;
var e_me; stderr std_me;
var e_a; stderr std_a;
var e_vidc; stderr std_vidc;
var e_vifc; stderr std_vifc;
var e_ve; stderr std_ve;
var e_varrho; stderr std_varrho;
var e_y_star; stderr std_y_star;
var e_pi_star; stderr std_pi_star;
var e_rf_star; stderr std_rf_star;
var e_g; stderr std_g;
var e_r; stderr std_r;
end;

%% Final Commands
resid(1);
steady;
check;
model_diagnostics;
stoch_simul(irf=10);