EconForge / dolo.py

Economic modelling in python
BSD 2-Clause "Simplified" License
98 stars 72 forks source link

yaml_import() not working #13

Closed sglyon closed 11 years ago

sglyon commented 12 years ago

I tried to import a .yaml file containing a model that I wrote and it didn't work.

I used the following commands:

from dolo import *

my_model = yaml_import('hw8_1.yaml')

The interpreter picked up an exception that looks like this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-802cb1d1a18d> in <module>()
----> 1 mymodel = yaml_import('hw8_1.yaml')

/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/dolo-0.4_dev_1-py2.7.egg/dolo/misc/yamlfile.pyc in yaml_import(filename, verbose)
    150     f = file(filename)
    151     txt = f.read()
--> 152     model = parse_yaml_text(txt,verbose=verbose)
    153     model['name'] = fname
    154     return model

/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/dolo-0.4_dev_1-py2.7.egg/dolo/misc/yamlfile.pyc in parse_yaml_text(txt, verbose)
     32     else:
     33         vnames = declarations['variables']
---> 34         variables_ordering = [Variable(vn,0) for vn in vnames]
     35         variables_groups = None
     36 

TypeError: __new__() takes exactly 2 arguments (3 given)

I thought it might be an issue with my .yaml file, but it isn't.

This is what happens when I try to import the file example1.yaml

example = yaml_import('/Users/spencerlyon2/Documents/Python/Open_Source/dolo/examples/yaml_files/example1.yaml')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-856615810e54> in <module>()
----> 1 example = yaml_import('/Users/spencerlyon2/Documents/Python/Open_Source/dolo/examples/yaml_files/example1.yaml')

/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/dolo-0.4_dev_1-py2.7.egg/dolo/misc/yamlfile.pyc in yaml_import(filename, verbose)
    150     f = file(filename)
    151     txt = f.read()
--> 152     model = parse_yaml_text(txt,verbose=verbose)
    153     model['name'] = fname
    154     return model

/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/dolo-0.4_dev_1-py2.7.egg/dolo/misc/yamlfile.pyc in parse_yaml_text(txt, verbose)
     32     else:
     33         vnames = declarations['variables']
---> 34         variables_ordering = [Variable(vn,0) for vn in vnames]
     35         variables_groups = None
     36 

TypeError: __new__() takes exactly 2 arguments (3 given)
albop commented 11 years ago

The bug is related with the ways dated-symbols are initialized. The way symbols are implemented actually depends on the version of sympy you have (>< 0.7.2).

It should be possible to type Variable('v') or Variable('v',0), both of them representing $v_t$.

Which version os sympy do you have ? Do you have latest dolo ?

sglyon commented 11 years ago

I am running sympy version '0.7.2-git'.

I do have the latest dolo ('0.4.1')

albop commented 11 years ago

It looks like this bug has already been fixed in master ( https://github.com/albop/dolo/blob/master/dolo/misc/yamlfile.py#L36 ) . How did you install dolo ? From pypi or from source ? Currently, it looks like the best way to install from source is entering the dolo directory and typing pip install . . I found it better than python setup.py when using EPD (less package version conflicts). Can you try again that way ?

albop commented 11 years ago

BTW, contrary to what waas indicated just above, the correct forms are Variable('v') or Variable('v',t=0) the last one being more explicit. (don't look at the close/reopen succession, we'll close the thread when the model can be solved just right)

sglyon commented 11 years ago

I installed by pulling the master branch from albop/dolo. I then used setup.py.

Should I do a sudo pip install dolo instead?

After calling setup.py I can import the model. But it still doesn't give a solution.

sglyon commented 11 years ago

Also one more general comment. Is the steady_state: section where I give actual steady state values, or just initial guesses for dolo to use when solving for the steady state?

see this ipython notebook for more info

albop commented 11 years ago

I recently found out that there can be many problems when mixing easy_install with epd . EPD has its own package management system which is not fully compatible. Using pip insteady seems a good idea ( see https://mail.enthought.com/pipermail/epd-users/2012-January/000597.html ) So instead of python setup.py install you do : pip install . or pip install -e . if you want to make symlinks (if you are tracking the development version for instance). I should update the installation instruction to reflect this.

albop commented 11 years ago

As for your notebooks (nbviewer is great ! does it run code or do you upload precomputed notebookds ?), a few remarks :

Also, there is apparently a small regression: doing display(model) in the notebook should print the equations (latexified) with the residuals, but it doesn't seem to work right now.

albop commented 11 years ago

I fixed a couple of small bugs in master (included the printing of models). Here are a couple of instructions you may want to try:

display(model) dr = solve_decision_rule(model, solve_ss=True)

from dolo.numeric.decision_rules import stoch_simul, impulse_response_function stoch_simul(dr, ['c','y']) impulse_response_function(dr, 'eps', ['c','y'])

sglyon commented 11 years ago

nbviewer must just show computed notebooks because I didn't attach the yaml file in any way.

Thanks for the hints on solving my model. I haven't heard the term 'state-free' used to describe the model. In fact we would label eps as an exogenous state variable, with k as an endogenous state variable. What did you mean by state free (or if it is easier for you), where can I look for more info about that classification?

I am now able to display() the model, but when using the command dr = solve_decision_rule(model, solve_ss=True) I get an error. See the ipynb from the previous comment.

albop commented 11 years ago

Here is the definition of the various kinds of models : http://albop.github.com/dolo/conventions.html .

Statefree models are preferably used in big dsge models, with a large number of equations. They impose basically no restriction on the model (not even to come from an optimization problem).

Models with explicit states are the way to go if you want to try global solutions.

Thr perturbation approach can be used for both but when you have explicit transitions, you don't need to approximate them. Hence the name explicit-controls. By contrast statefree models approximate everything.

Dolo has both. They belong so to say to separate trees as they are not compatible with each other. (for instance, simulations, plots, are implemented separately) Le 2 nov. 2012 00:34, "Spencer Lyon" notifications@github.com a écrit :

nbviewer must just show computed notebooks because I didn't attach the yaml file in any way.

Thanks for the hints on solving my model. I haven't heard the term 'state-free' used to describe the model. In fact we would label eps as an exogenous state variable, with k as an endogenous state variable. What did you mean by state free (or if it is easier for you), where can I look for more info about that classification?

I will try to display() my model and upload it again on nbviewer.

— Reply to this email directly or view it on GitHubhttps://github.com/albop/dolo/issues/13#issuecomment-10000094.

sglyon commented 11 years ago

Ok great thanks that link for the model specifications was exactly what I was looking for.

Were you able to use solve_decision_rule() to get a solution to this model? I am still getting an error as noted in the .ipynb file.

albop commented 11 years ago

Yes, I solved it without problem. Actually, I just replaced the eps name by epsilon because eps reminds me of machine epsilon, but I don't think it is needed. Have you changed the yaml file ? I can't see it anymore. The error says, the covariance matrix cannot be converted to a float, meaning it still has unresolved symbolic objects. Is it the case ? Does it depend on an uninitialized parameter ?

sglyon commented 11 years ago

I have made a change to the yaml file. I changed the .ipynb again and what the link has is the most recent version.

albop commented 11 years ago

Welcome to the "modelling" part. You should add the steady state section. By default variables are initialized to 0 which is inappropriate here since some equations can't evaluated there (as you can see by looking at the infinite residuals). As a result the nonlinear optimizer is probably doing crazy things. If you have the choice it is always better to give good (exact most of the time) initial values.

sglyon commented 11 years ago

That makes a lot of sense. I could and have used a routine like scipy.fsolve or something to solve for the steady state.

What I did just now was add one optional parameter to solve_decision rule. I made the following changes:

def solve_decision_rule(model,order=2,method='default',mlab=None,
                    steady_state = None, solve_ss = False, ss_guess=None):

Then I changed the content of the if statement starting on line 25 to the following:

if solve_ss == True:
    if ss_guess != None:
        y0 = ss_guess
    y = model.solve_for_steady_state(y0)

Doing that and passing a ss_guess argument to solve_decision_rule allowed dolo to solve for the steady state and work properly.

If you think that might be helpful I could submit the updated code in a pull request. Let me know what you think. It may also be a possibility to add a section to the yaml parser that looks for steady state initial guesses in the calibration section. That seems like more work for something that not many people are likely to care about though.

The current state of solving my model is reflected in the same ipython notebook as before. (I just copied the link and pasted it here for ease of use)

sglyon commented 11 years ago

So I just realized I duplicated some of your code. If I am understanding what your code does the steady_state parameter does just what I described for the ss_guess parameter I added. Is that correct?

albop commented 11 years ago

Exactly : if steady_state option is not supplied, then the initial guess is taken from the yaml file (in the steady_state section, which corresponds to initval in dynare). And if solve_ss=True, then an attempt is made to numerically find the steady-state, startging from the initial_guess. If solve_ss=False this initial guess is supposed to represent the steady-state.

I made this last one a separate option because:

I realize there was a bit of confusion in my explanations. Maybe it would be good if the program was just a bit more verbose (provided a verbose option was given).

albop commented 11 years ago

BTW, it looks like your model is now working. Two remarks:

Currently, there is no code in dolo to make anything with the solution beyond computing theoretical moments (somewehere in decision_rules.py), so that any addition would be very beneficial.

From a user perspective, there are many small enhancements that would be great to have :

Actually many of these points involve a way to print tables or numpy arrays. Until now, I was using some custom functions (in misc.printing) to print tables or numpy arrays, and register these custom methods in IPython (this is why numpy arrrays get printed. There are places were it makes a lot of sense to simply display pandas tables as you do in the notebook to display the steady-state (it's exactly the kind of output that should be made by dolo). Maybe, we could consider returning pandas arrays too (in the simulations for instance) but I am not completely sure about it yet.

sglyon commented 11 years ago

Model is working.