// This is a simple RBC model that incorporates a technology process with
// a small predictable component that serves as a source of long run risk.
// addpath /Applications/Dynare/4.4.3/matlab
// variables are logarithms, normalized by the level of technology

var k y c i r v ev da x;
var sdf_household cf_consumption cf_investment;
varexo eps_a eps_x;

parameters GAMMA RHO BETA ALPHA DELTA MU SIGMA_A PHI SIGMA_X;

// quarterly frequency
GAMMA     = 10;         // risk aversion
RHO       = 1.5;        // intertemporal elasticity of substitution
BETA      = 0.99;       // time preference
ALPHA     = 0.36;       // capital share
DELTA     = 0.025;      // depreciation rate
MU        = 0.005;      // mean growth rate
SIGMA_A   = 0.005;      // volatility of the short-run shock
PHI       = 0.81;       // persistence of the long-run shock
SIGMA_X   = 0.0003;     // volatility od the long-run shock


model;
    // continuation value recursion
    exp((1-GAMMA)*ev) = exp((1-GAMMA)*(v(+1)+da(+1)));
    exp(v) = ( (1-BETA)*exp((1-RHO)*c) + BETA*exp((1-RHO)*ev) )^(1/(1-RHO));

    sdf_household = log(BETA) - RHO*(c-c(-1)+da) + (RHO-GAMMA)*(v+da - ev(-1));

    exp(y) = exp(c) + exp(i);
    exp(i) = exp(k) - (1-DELTA)*exp(k(-1)-da);
    y = ALPHA*(k(-1)-da);
    exp(r) = ALPHA*exp((ALPHA-1)*(k(-1)-da)) + 1 - DELTA;
    1 = exp(sdf_household(+1) + r(+1));

    cf_consumption = c - c(-1) + da;
    cf_investment = i - i(-1) + da;

    da = MU + x(-1) + SIGMA_A*eps_a;
    x = PHI*x(-1) + SIGMA_X*eps_x;

end;

initval;
    da = MU;
    x = 0;

    sdf_household = log(BETA) - RHO*da;
    r = -sdf_household;

    k = log(((exp(r) - (1-DELTA))/ALPHA)^(1/(ALPHA-1))) + da;
    y = ALPHA*(k-da);
    i = k + log(1 - (1-DELTA)/exp(da));
    c = log(exp(y) - exp(i));

    v = 1/(1-RHO)*log( ((1-BETA)*(exp((1-RHO)*c)))/(1-BETA*exp((1-RHO)*da)) );
    ev = v + da;

    cf_consumption = da;
    cf_investment = da;
end;

shocks;
    var eps_a;
    stderr 1;
    var eps_x;
    stderr 1;
end;

steady;

stoch_simul(order=2);