econ-ark / DARKolo

fusion projects combining of Dolo/DolARK and Econ-ARK
4 stars 8 forks source link

BufferStock dolo: "too many indices for array" #2

Closed sbenthall closed 4 years ago

sbenthall commented 4 years ago

Error in BufferStock python code.

I'm getting this error both when running:

$ ipython BufferStock.py

and when runnign $ jupytext --to notebook BufferStock.py and then opening the notebook with Jupyter.

In cell 5, # Set a maximum value of the market resources ratio m for use in both models

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-e81ae8575e02> in <module>
      5 
      6 # Obtain the decision rule by time iteration
----> 7 dr = time_iteration(model_dolo,tol=1e-08,verbose=True)

~/projects/econ-ark/dolo/dolo/algos/time_iteration.py in time_iteration(model, dr0, dprocess, with_complementarities, verbose, grid, maxit, inner_maxit, tol, hook, details)
     84     n_s = len(model.symbols['states'])
     85 
---> 86     endo_grid = model.get_grid(**grid)
     87 
     88     exo_grid = dprocess.grid

~/projects/econ-ark/dolo/dolo/compiler/model.py in get_grid(self)
    190         # determine bounds:
    191         domain = self.get_domain()
--> 192         min = domain.min
    193         max = domain.max
    194 

~/projects/econ-ark/dolo/dolo/compiler/objects.py in min(self)
     27     @property
     28     def min(self):
---> 29         return np.array([self[e][0] for e in self.states])
     30 
     31     @property

~/projects/econ-ark/dolo/dolo/compiler/objects.py in <listcomp>(.0)
     27     @property
     28     def min(self):
---> 29         return np.array([self[e][0] for e in self.states])
     30 
     31     @property

IndexError: too many indices for array
sbenthall commented 4 years ago

This is with dolo 4.9.9/master.

sbenthall commented 4 years ago

Inquiring upstream at to dolo project: https://github.com/EconForge/dolo/issues/170

albop commented 4 years ago

This one is a consequence of trying to hack the internals... (I remember I suggested it to Chris so I can't blame anybody). Lines 181 and 182 are modifying the primary data model is imported from. By default field model.data is a ruamel.yaml structure which is directly imported from the yaml file. When you do model.domain you are actually reading this structure using dolang's method eval_data. And this one doesn't know how to deal correctly with list structures, which is what causes the error. If you do model_dolo.data['domain']['m'].__class__, the answer is CommentedSed and you are changing this type when you do model_dolo.data['domain']['m'] = [0,'max_m']. A quick fix is to replace the latter with model_dolo.data['domain']['m'][:] = [0,'max_m'] which modifies inplace the elements of the list. A better fix is to not rely on list manipulation. Instead, define a parameter max_m in the calibration section. And define the domain to be [0,max_m]. do all of that in the yaml file, and if you want to change the calibration use model_dolo.set_calibration(max_m=500). that way you are not relying on undocumented internals. Still, an issue should be created against dolang so that eval_data either knows how to deal with vanilla lists/dicts or raises an error that it doesn't know how to handle the type.