Manipulating the decision rules


I’m trying to use the decision rules to calculate some asset prices. That is, I want to integrate over the shocks conditional on today’s values of the state variables (I don’t want to approximate the asset prices around the nonstochastic steady state as that doesn’t seem to work very well).

Is there a function somewhere inside dynare that will help me do this automatically? I’m a bit worried about my ability to figure out how to unfold the big matrices for the third-order decision rules without getting something wrong.


You can either look at the reference manual in the section about “stoch_simul”: the matrices for decision rules at 1st, 2nd and 3rd order are explained there.

You can also look at the code in “simult_.m” which uses the decision rules to simulate the model; note that for 3rd order, the actual code is contained in a DLL (called dynare_simul_.m) so you have to look at the C++ source code which is not trivial to understand.


Hi everyone,

I’m also trying to unfold the G2 and G3 matrices, but I couldn’t find the sources of the C code of “Dynare_simul” to check what is going on. However, following the instructions in Dynare’s manual, I wrote a small piece of Matlab code to rewrite the G2 matrix generated in Dynare in the “unfolded” way. I’m attaching the file with the matlab function, where the only two arguments necessary to execute it are the G2 matrix and the number of states+shocks of the model.

Is it possible for anyone to check if the output matrix is written in the right way? Or, at least, show the path where we can find the source of the C code that runs the simulations for order=3?

Thanks in advance!

unfoldg2.m (1.03 KB)

I’ve attached some code that I wrote to unfold the decision rules. It takes as input the current values of the state variables and shocks, and outputs the endogenous variables. Sorry it’s not terribly extensively commented.

Thanks, by the way, for the help before.
DecR.m (1.92 KB)

Great piece of code, Ian!!!

With the structure you wrote in the code, it should be easy to generalize to approximations of order higher than 3, right? Of course, as long as the n-th order approximation of the model is written as:

Yt = G1 + G2zt + G2(zt k zt) + G3*(zt k zt k zt) + G4*(zt k zt k zt k zt) + … + Gn*(zt k zt k zt k zt k… zt)



Dear all,

I’m having the same problem… did you ever manage to unfold the G3 matrix?
If you did I would much appreciate if you could share it with me.
Thanks in advance,


Hi Chico,

Following is my version of the code to unfold G3 matrix. You need also the file I posted before with the code to unfold G2 matrix in order to make it run properly.

Comments, suggestions, checks are always welcome.
unfoldg3.m (1.77 KB)


I’m also using Dynare to calculate a third-order approximation that I then use outside Dynare, which means that I have to unfold the oo_.dr.g_2 and oo_.dr.g_3 matrices.

The codes posted here were not working for me: I got an “index out of bounds” error when the G3 function calls the G2 function. I tried to fix them but it wasn’t easy, and finally I ended up writing my own thing. It’s A LOT simpler than what was posted before, so there’s much less room for bugs or typos. Still, don’t underestimate my ability to screw things up: check before use. It’s really simple, it will take you just two minutes.

unfoldg2.m (1015 Bytes)

Can’t upload two files to a single post, so here’s the unfolder for G3
unfoldg3.m (1.04 KB)

I am not sure what you are doing, but you should take a look at the anonymous functions at as well as the sorting in