thorek1 / MacroModelling.jl

Macros and functions to work with DSGE models.
https://thorek1.github.io/MacroModelling.jl/stable
MIT License
98 stars 15 forks source link

Simulating an AR1 process #28

Closed eloualiche closed 1 year ago

eloualiche commented 1 year ago

I am trying to understand how to simulate an AR1 process. I was having some issues with negative values even after taking exponentials.

I will try to reproduce this in a simple example below.

@model AR1 begin
    exp_z[0] = exp(z[0])
    z[0] = rho * z[-1] + std_z * eps_z[x]
end;

@parameters AR1 begin
    std_z = 0.01
    rho = 0.2
end;

simulation = simulate(AR1, periods=1000, algorithm=:third_order);
df_res = unstack(simulation |> DataFrame, :Periods, :Variables, :value)
@combine(df_res, :exp_z=mean(:exp_z), :z=mean(:z))

Both of the variables are set at zero (when we would expect exp_z to be at 1).

The counterpart in dynare would look like

var 
z exp_z junk;

varexo 
eps_z ;

parameters 
rho std_z ;
std_z   =   0.01;
rho =   0.2;

model;
    z(0) = rho * z(-1) + std_z * eps_z;
    exp_z(0) = exp(z(0));
    junk=0.9*junk(+1);
end;

shocks;
var eps_z   =   1;
end;

initval;
   z    =   0.0;
    exp_z = 1.0;
    junk = 0.0;
end;

stoch_simul(
    irf_shocks = (
        eps_z
    ),
    order = 3,
    irf = 0,
    noprint,
    periods = 1000,
    replic  = 1
)
  z exp_z;

and in matlab, looking at the simulation results gives:

mean(oo_.endo_simul,2)

ans =

    0.0001
    1.0001
         0

I am wondering if I am missing something about the log-linearization of the variables here?

thorek1 commented 1 year ago

the standard behaviour of simulate is to return absolute deviations from steady state. if you want levels just do: simulate(AR1, levels = true, periods=1000, algorithm=:third_order). you can also plot the simulations: plot_simulations(AR1, algorithm = :third_order, periods=1000) in which case he automatically shows the level and percent deviations for strictly positive variables.

I guess you expected levels as output from simulate. I forgot why I made absolute deviations the default output of simulate. let me think about it again

thorek1 commented 1 year ago

going through the code it seems there is no good reason to return absolute deviations by default. In the next release simulate will return levels by default

eloualiche commented 1 year ago

Thank you!