/*
 * This file solves a model with a weather (flood) shock that interrupts production and damages the capital stock.
 * The production function uses k instead of k(-1) because the effective capital stock used at time t is not known till time t due to the shock.
 * The law of motion for capital becomes
 *
 * k = g(u)*(invest(-1)+(1-delta)*k(-1));
 * where u is the exogenous flood shock and g the damage function.
 *
 * the production function becomes y = exp(a)*f(u)*A*k(-1)^alpha*l^(1-alpha)
 * where exp(a) is the typical cyclical shock and f(u) the loss function
 *
 * The model uses the variable redefinition x=log(X) <=> exp(x)=X to use Dynare's
 * linearization capabilities to perform a log-linearization. Thus, all impulse 
 * response functions are in percentage deviations from the deterministic steady state.
 */

%----------------------------------------------------------------
% define countries 
%----------------------------------------------------------------
@#define countries = ["R","N"]

%----------------------------------------------------------------
% define common variables 
%----------------------------------------------------------------
@#for co in countries
    var y_@{co}, c_@{co}, k_@{co}, a_@{co}, l_@{co};
    varexo e_@{co};

    %----------------------------------------------------------------
    % define common parameters
    %----------------------------------------------------------------
    parameters A_@{co}, beta_@{co}, gama_@{co}, phi_@{co}, alpha_@{co}, delta_@{co}, psi_@{co}, eta_@{co};
    parameters B_@{co},y_ss_@{co},k_ss_@{co},c_ss_@{co},l_ss_@{co};

    A_@{co}=1;
    beta_@{co} = 0.966183575;
    gama_@{co}=2.5;
    phi_@{co} = 0.95;
    alpha_@{co} = 0.33;
    delta_@{co} = 0.025;
    psi_@{co} = 11;
    eta_@{co} = 0.7;

    B_@{co}=alpha_@{co}/(1/(beta_@{co})-(1-delta_@{co}));
    y_ss_@{co}=0.5;
    k_ss_@{co}=B_@{co}*y_ss_@{co};
    c_ss_@{co}=0.6*y_ss_@{co};
    l_ss_@{co}=0.3;
@#endfor

%----------------------------------------------------------------
% define variables for country at risk
%----------------------------------------------------------------
var d, f, g, h, R;
varexo u;

%----------------------------------------------------------------
% define parameters for country at risk
%----------------------------------------------------------------
parameters a1, b, c1, Q, B1, L;
parameters g_ss,f_ss,R_ss,d_ss;

a1=93.53;
Q=0.326;
B1=1.983;
b=0.01306;
c1=0.01375;
L=0.9;

g_ss=0.99;
f_ss=0.99;
R_ss=L*(1-1/(1+Q));
d_ss=0.02;

model;
%----------------------------------------------------------------
% model for country at risk
%----------------------------------------------------------------
c_R^gama_R*psi_R*l_R^(1+eta_R)=(1-alpha_R)*y_R;
k_R = (alpha_R*y_R(+1))/(((c_R(+1)/c_R)^(gama_R))/(beta_R*g)-(1-delta_R));
y_R = exp(a_R)*f*A_R*(k_R(-1)^alpha_R)*(l_R^(1-alpha_R));
k_R+d = g*(y_R-c_R+(1-delta_R)*(k_R(-1)+d(-1)));
d =(((1/(beta_R*g(-1)))*((c_R/c_R(-1))^gama_R)-(1-delta_R))*(((R+h)*(c1+a1*d)^2)/(a1*(c1-b)*h))-y_R)*g-k_R;
a_R = phi_R*a_R(-1)+e_R;
h = 1/(1+Q*exp(-B1*u));
R=L*(1-1/(1+Q*exp(-B1*u)));
f = R+h*(a1*d(-1)+b)/(a1*d(-1)+c1);
g = R+h*(a1*d(-1)+b)/(a1*d(-1)+c1);

%----------------------------------------------------------------
% model for country at no risk
%----------------------------------------------------------------
c_N^gama_N*psi_N*l_N^(1+eta_N)=(1-alpha_N)*y_N;
k_N = (alpha_N*y_N(+1))/(((c_N(+1)/c_N)^(gama_N))/(beta_N*g)-(1-delta_N));
y_N = exp(a_N)*f*A_N*(k_N(-1)^alpha_N)*(l_N^(1-alpha_N));
k_N = y_N-c_N+(1-delta_N)*k_N(-1);
a_N = phi_N*a_N(-1)+e_N;

end;

%----------------------------------------------------------------
%  set common initial and steady state values
%---------------------------------------------------------------
initval;
@#for co in countries
    y_@{co} = y_ss_@{co};
    c_@{co} = c_ss_@{co};
    l_@{co} = l_ss_@{co};
    k_@{co} = k_ss_@{co};
    a_@{co} = 0;
    e_@{co} = 0;
@#endfor

%----------------------------------------------------------------
%  set initial and steady state values for the country at risk
%---------------------------------------------------------------
d=d_ss;
f=f_ss;
g=g_ss;
h=0.95;
R=0.4;
u = 0;
end;
steady;

%----------------------------------------------------------------
%  set shock variances
%---------------------------------------------------------------
shocks;
@#for co in countries
    var e_@{co}; stderr 0.01;
@#endfor

var u; stderr 1;
var e_R, u = 0;
end;

%----------------------------------------------------------------
% generate IRFs
%----------------------------------------------------------------
stoch_simul(periods=100000,nomoments);