Bring Sims' function to estimate linear expectations models to Python

419 views
Skip to first unread message

cbrunos

unread,
Mar 12, 2012, 12:12:47 PM3/12/12
to pystatsmodels
Hi everybody!

My name is Bruno Rodrigues and at the moment I'm writing my Masters
Thesis at Strasbourg University which deals with estimating DSGE
models (I'm discussing the Great Moderation). For now, I'm using my
thesis' advisor matlab code, which consists of Sims' functions to
estimate linear expectations models (gensys, qzswitch, qzdiv, etc) and
some custom written code to do Bayesian inference and compute irfs.

I wish to translate all this to Python. I came here to discuss this,
following Skipper Seabold advice (we already had a discussion on Scipy-
dev mailing list: http://mail.scipy.org/pipermail/scipy-dev/2012-March/017222.html)

For now, I only have qzswitch and qzdiv, which I didn't even
translate, I found them on some Dynare-Python archives. These
functions are useless by themselves but are needed in gensys, which is
the function that actually solves the linear expectations models. I am
translating this right now, and think I might finish soon. The thing
is, I am a beginner with Python and am simply translating Matlab code
to Python code, and I am sure the resulting code is neither efficient,
nor Pythonic.

It would be awesome if interested people could help, with the
objective of having these functions then ported to Cython for maximum
speed.

What do you think?

Skipper Seabold

unread,
Mar 12, 2012, 2:34:27 PM3/12/12
to pystat...@googlegroups.com
On Mon, Mar 12, 2012 at 12:12 PM, cbrunos <r.c.bru...@gmail.com> wrote:
> Hi everybody!
>
> My name is Bruno Rodrigues and at the moment I'm writing my Masters
> Thesis at Strasbourg University which deals with estimating DSGE
> models (I'm discussing the Great Moderation). For now, I'm using my
> thesis' advisor matlab code, which consists of Sims' functions to
> estimate linear expectations models (gensys, qzswitch, qzdiv, etc) and
> some custom written code to do Bayesian inference and compute irfs.
>

Are you doing Bayesian inference on the full DSGE model using the kalman filter?

> I wish to translate all this to Python. I came here to discuss this,
> following Skipper Seabold advice (we already had a discussion on Scipy-
> dev mailing list: http://mail.scipy.org/pipermail/scipy-dev/2012-March/017222.html)
>
> For now, I only have qzswitch and qzdiv, which I didn't even
> translate, I found them on some Dynare-Python archives. These
> functions are useless by themselves but are needed in gensys, which is
> the function that actually solves the linear expectations models. I am
> translating this right now, and think I might finish soon. The thing
> is, I am a beginner with Python and am simply translating Matlab code
> to Python code, and I am sure the resulting code is neither efficient,
> nor Pythonic.
>
> It would be awesome if interested people could help, with the
> objective of having these functions then ported to Cython for maximum
> speed.
>
> What do you think?

I think *some* of this could definitely find a home in statsmodels.
Things that are too DSGE specific might best be left to pymaclab
(parsing of model files, etc.) but things that are pure estimation -
ie., Bayesian kalman filtering, structural VAR, impulse response
functions, maybe even a full gensys solver implementation could find a
home here given that we already have some code of interest. Then
statsmodels might just be a dependency for your code and/or pymaclab.

One thing to be careful about is the license for Chris Sims' code. I
don't recall ever seeing anything explicit in his work. We are only
able to accept open source code that is BSD compatible. Loosely, this
is code not based on GPL code. You might ask Chris if he has released
his code under any particular license.

In any event, you'll find some willing eyes to look over your python
code here (given that people have the time to do so) and give you tips
for making it Pythonic and speeding it up, etc.

You might also be interested in perusing our GSoC ideas page to see if
there's anything there that overlaps with your university work that
you might like to participate on for the google summer of code. Google
pays students a stipend to contribute to open source software projects
over the summer.

https://github.com/statsmodels/statsmodels/wiki/GSoC-Ideas
http://code.google.com/soc/

Skipper

cbrunos

unread,
Mar 12, 2012, 5:33:14 PM3/12/12
to pystat...@googlegroups.com


Are you doing Bayesian inference on the full DSGE model using the kalman filter?


Yes, the DSGE model's likelihood is evaluated using the kalman filter. My thesis advisor's
assistant translated Frank Scohrfheide's GAUSS code to matlab to do that. Then, the model
is estimated using a random-walk metropolis hastings algorithm, and again, this is code
translated for Frank Schorfheide's GAUSS code. For now, I am still only translating Sims'
code to solve the linear expectations model.
 

I think *some* of this could definitely find a home in statsmodels.
Things that are too DSGE specific might best be left to pymaclab
(parsing of model files, etc.) but things that are pure estimation -
ie., Bayesian kalman filtering, structural VAR, impulse response
functions, maybe even a full gensys solver implementation could find a
home here given that we already have some code of interest. Then
statsmodels might just be a dependency for your code and/or pymaclab.

Indeed, this could be great.
 

One thing to be careful about is the license for Chris Sims' code. I
don't recall ever seeing anything explicit in his work. We are only
able to accept open source code that is BSD compatible. Loosely, this
is code not based on GPL code. You might ask Chris if he has released
his code under any particular license.

His original code doesn't have, as far as I know, any particular license. Could
my code by LGPL and accepted by statsmodels? This way, the code can be
added to proprietary software, but modifications would have to be released to
the public. As for Frank Schorfheide's code, AFAIK, it doesn't have any license
neither.

The thing is, in my gensys function I use qzdiv and qzswitch from dynare-python
and these functions are GPL-licensed. This could be problematic as gensys
should then be GPL too.
 

In any event, you'll find some willing eyes to look over your python
code here (given that people have the time to do so) and give you tips
for making it Pythonic and speeding it up, etc.

This is nice, I'll start posting code as soon as I finish testing it.
 

You might also be interested in perusing our GSoC ideas page to see if
there's anything there that overlaps with your university work that
you might like to participate on for the google summer of code. Google
pays students a stipend to contribute to open source software projects
over the summer.

I'll take a look, thanks.
 

 

cbrunos

unread,
Mar 14, 2012, 5:01:26 AM3/14/12
to pystat...@googlegroups.com
Hi,


here is a first draft of gensys' port to python. The file contains:

- gensys.m: Sims' original code
- qzmanip.py: Contains qzswitch and qzdiv, needed for gensys, originally from python-dynare
- qz.py: Provides Generalized Schur decomposition that works just like in matlab by Sven Schreiber (http://econ.schreiberlin.de/schreibersoftware.html)
- gensys.py: Tentative translation of Sims' gensys.m to Python
- example.py: This file contains the necessary arrays to test gensys.py. The matrices in this file is what you get if you solve the model presented in An & Schorfheide - Bayesian analysis of DSGE models (2006)

The code, however, still doesn't work. I have a bug in the svd decomposition, line 65 in gensys.py: ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

It seems like the matrix I try to feed to svd is of dimension 0 and I don't know why. I will try to look for the bug, but thought that people might be interested in taking a look even if it's still not completed and maybe help
me find the bug (if they have time of course).

Cheers!
code_1.tar.gz

Skipper Seabold

unread,
Mar 14, 2012, 9:46:21 AM3/14/12
to pystat...@googlegroups.com

My inclination is not to accept LGPL code. Mainly because I am not a
licensing expert, and it sounds only like it will increase the burden
on us, the package distributors, in the future. Would you be willing
to relicense your code?

> The thing is, in my gensys function I use qzdiv and qzswitch from
> dynare-python
> and these functions are GPL-licensed. This could be problematic as gensys
> should then be GPL too.
>

This could be problematic. They didn't look too hard to code from
scratch given that you know what they're supposed to do.

Skipper Seabold

unread,
Mar 14, 2012, 9:51:07 AM3/14/12
to pystat...@googlegroups.com

Ah, cool. I will try to have a look soon-ish. In the meantime, you
might consider getting familiar with git and posting your code to
github. So that it's version-controlled and easy for others to grab
and browse to stay updated.

https://github.com/

I'm a bit concerned about the translation aspect, if the intention is
to include this in statsmodels, since the license of Sims' code is not
clear. If not, probably no big deal.

Skipper

Message has been deleted

cbrunos

unread,
Mar 14, 2012, 10:31:07 AM3/14/12
to pystat...@googlegroups.com

My inclination is not to accept LGPL code. Mainly because I am not a
licensing expert, and it sounds only like it will increase the burden
on us, the package distributors, in the future. Would you be willing
to relicense your code?

Ok, no problem, the code will be made available with the same license as statsmodels.
I believe firmly in Free Software as defined by the FSF, but believe more in availability of
code.

 

> The thing is, in my gensys function I use qzdiv and qzswitch from
> dynare-python
> and these functions are GPL-licensed. This could be problematic as gensys
> should then be GPL too.
>

This could be problematic. They didn't look too hard to code from
scratch given that you know what they're supposed to do.

Should I ask the Dynare team if they're willing to accept that we
use their implementation of qzdiv and qzswitch, and license with
a BSD-type license?
 

Ah, cool. I will try to have a look soon-ish. In the meantime, you
might consider getting familiar with git and posting your code to
github. So that it's version-controlled and easy for others to grab
and browse to stay updated.


Yep, I'm already somewhat familiar with github, I'll try to read the
guidelines to use it efficiently.

I'm a bit concerned about the translation aspect, if the intention is
to include this in statsmodels, since the license of Sims' code is not
clear. If not, probably no big deal.

Sims is probably very busy, but I could try to ask him if he is ok with this.

cbrunos

unread,
Mar 14, 2012, 10:39:21 AM3/14/12
to pystat...@googlegroups.com
I just checked qzdiv and qzswitch again, and it seems I'm mistaken, no license is specified.

I'm going to ask them what the license is, and if we can use it.

Skipper Seabold

unread,
Mar 14, 2012, 10:40:18 AM3/14/12
to pystat...@googlegroups.com
On Wed, Mar 14, 2012 at 10:29 AM, cbrunos <r.c.bru...@gmail.com> wrote:
>> My inclination is not to accept LGPL code. Mainly because I am not a
>> licensing expert, and it sounds only like it will increase the burden
>> on us, the package distributors, in the future. Would you be willing
>> to relicense your code?
>
> Ok, no problem, the code will be made available with the same license as
> statsmodels.

Awesome, thanks.

> I believe firmly in Free Software as defined by the FSF, but believe more in
> availability of
> code.
>
>
>>

>> > The thing is, in my gensys function I use qzdiv and qzswitch from
>> > dynare-python
>> > and these functions are GPL-licensed. This could be problematic as
>> > gensys
>> > should then be GPL too.
>> >
>>
>> This could be problematic. They didn't look too hard to code from
>> scratch given that you know what they're supposed to do.
>

> Should I ask the Dynare team if they're willing to accept that we
> use their implementation of qzdiv and qzswitch, and license with
> a BSD-type license?
>

It might be worth a shot. Though the original function was written by
Chris Sims again as far as I can tell. I wouldn't be surprised if all
his code were in the public domain. Nobel laureates can be generous.

Though honestly, it looks like these are just reordering functions,
which I *think* can be achieved by using the SELECT argument to dgges,
or using my ordqz wrapper. I'm pretty sure qzdiv and qzswitch were
written before MATLAB had an ordqz function.

Pablo W.

unread,
Mar 14, 2012, 11:48:16 AM3/14/12
to pystat...@googlegroups.com
Hi,

I'm glad to see other people doing DSGE models in python. I had some experience with the questions you raise:

- The lack of the qz routine in scipy is pretty unfortunate for us: it is really the core of the algorithm. Currently, the best solution I'm aware of consists in using system's lapack, which on windows requires to install another software containing lapack (for instance Scilab). I don't really know how hard it would be to append it to the list of included routines (just add a fortran wrapper ?) but I would really be extremely happy to see it happen.
 
- Concerning qz.py and qzdiv.py routines, the python version was written by Sven Schreiber. It consists in two things: a ctypes wrapper of the lapack function and a port of Sims original qzdiv file. While there is no problem with Sven's code (same license as Python) I think that Mr Sims never clearly stated if it was published under some particular license. The dynare project has some derivative work licensed under the gpl and I would bet they have checked with him.

- Like Skipper said below qzdiv is not necessary at all if you use the lapack routine that can reorder the solution.

- Have you tried to use dolo (in github:dynare-python)  ? It is BSD licensed so you can get reuse any portion of code. There is also solution code for the state-free (sims,dynare) and state-full (fackler) perturbation solution so you could use it to check your solution. You could also use it to compile at runtime the functions evaluating the derivatives of the model for each vector of parameter, provided that you code you model in a modfille or a yaml file).

Best regards and good luck

Pablo

Pablo W.

unread,
Mar 14, 2012, 12:29:36 PM3/14/12
to pystat...@googlegroups.com
One more precision:  the Dynare guys got the explicit permission to redistribute Sims' code under the GPL. One would have to ask him directly if it's possible to derive BSD work on it but I don't think it would be too much of a problem.

cbrunos

unread,
Mar 14, 2012, 1:04:30 PM3/14/12
to pystat...@googlegroups.com

In the meantime, youmight consider getting familiar with git and posting your code to


github. So that it's version-controlled and easy for others to grab
and browse to stay updated.

Ok, so I hope I didn't do anything stupid; I cloned the branch to my computer, forked statsmodels,
created a branched called DSGE, added a folder named DSGE with the functions and then did a

git add .
git commit -am 'Added DSGE branch'
git push origin dsge

Now the code appears on my fork. Is that how it works?




cbrunos

unread,
Mar 14, 2012, 1:09:52 PM3/14/12
to pystat...@googlegroups.com
Hello,

thank you for your reply. I will take a look at dolo, I already did this past days, but never really took the time
to analyze the code in detail and see how it works.

Is Sven Schreiber active in developing dynare-python?

What is the status of Bayesian estimation of DSGE models with Python? My thesis advisor's assistant translated
a bunch of code originally written by Frank Schorfheide for this (from GAUSS to Matlab) and I plan on bringing all
this to Python/Cython to speed it up (sampling is really slooow for now).

Cheers!

Pablo W.

unread,
Mar 14, 2012, 1:29:23 PM3/14/12
to pystat...@googlegroups.com


Le mercredi 14 mars 2012 18:09:52 UTC+1, cbrunos a écrit :
Hello,

thank you for your reply. I will take a look at dolo, I already did this past days, but never really took the time
to analyze the code in detail and see how it works.

I'll be glad to help. I have been wanting to introduce it on this mailing list for a while but I never really found the time to make a proper announcement.

Is Sven Schreiber active in developing dynare-python?

No. I asked him for his code a while ago. If I understand well he is now quite active in the gretl community (he did the python bindings).
 

What is the status of Bayesian estimation of DSGE models with Python? My thesis advisor's assistant translated
a bunch of code originally written by Frank Schorfheide for this (from GAUSS to Matlab) and I plan on bringing all
this to Python/Cython to speed it up (sampling is really slooow for now).

As far as I know, there is only the pymaclab package for that.
Due to my research interests, I have mainly focused on solution methods. However, I don't think it would be too hard to implement your own provided you have a solution routine mapping a vector of parameters to an AR(1) process. I you choose to go that way, I would certainly interested in follow the developments.

cbrunos

unread,
Mar 28, 2012, 5:02:15 PM3/28/12
to pystat...@googlegroups.com
[...]
- gensys.py: Tentative translation of Sims' gensys.m to Python
The code, however, still doesn't work. I have a bug in the svd decomposition, line 65 in gensys.py: ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

It seems like the matrix I try to feed to svd is of dimension 0 and I don't know why.[...]

Ok, so I had some spare time this evening and looked further into the issue. Turns out the matrix I'm giving to svd must be an empty array of shape (0,9) but the svd routine in matlab knows how to handle such a matrix: by returning an empty matrix of size 0 by 1 which scipy.linalg.svd does not (an error is returned). Any ideas on how to solve this?

Cheers!

Skipper Seabold

unread,
Apr 4, 2012, 11:01:02 AM4/4/12
to pystat...@googlegroups.com

Can you post a self-contained example of what you mean?

Skipper

Message has been deleted

cbrunos

unread,
Apr 4, 2012, 3:04:03 PM4/4/12
to pystat...@googlegroups.com

Yes for example:

>>> a = numpy.zeros((0,5))

and then

>>> scipy.linalg.svd(a)

returns an error: ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

while Matlab returns an empty array of size 0x1. I've been busy with my masters thesis and haven't had time to look further into the issue.

Skipper Seabold

unread,
Apr 4, 2012, 3:43:23 PM4/4/12
to pystat...@googlegroups.com
On Wed, Apr 4, 2012 at 3:02 PM, cbrunos <r.c.bru...@gmail.com> wrote:
Yes, for example:

a=numpy.zeros((0,5))


and then

scipy.linalg.svd(a)

returns an error: ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

while Matlab returns an empty array of size 0x1. I'm busy at the moment with my masters thesis, and haven't had time to further look into the issue.


Something like this would mimic the matlab behavior though I suspect there's something else going on if you're passing around empty arrays.

def svd_matlab(a):
    a = np.asarray(a)
    if not a.size:
        return np.zeros((0,1))
    else:
        return linalg.svd(a)

Skipper
 

On Wednesday, April 4, 2012 5:01:02 PM UTC+2, jseabold wrote:

cbrunos

unread,
Apr 4, 2012, 4:29:17 PM4/4/12
to pystat...@googlegroups.com

Something like this would mimic the matlab behavior though I suspect there's something else going on if you're passing around empty arrays.

def svd_matlab(a):
    a = np.asarray(a)
    if not a.size:
        return np.zeros((0,1))
    else:
        return linalg.svd(a)

Skipper

I did that (had to change it a bit), and it seems to work; at least, I don't get that error message anymore. Now there are other bugs, line 84. I'm going to bed for tonight, I'll check it out soon. Thanks for your help!

Cheers!
Reply all
Reply to author
Forward
0 new messages