Skip to content

Commit

Permalink
passing tests for parsing block configuration and building working DB…
Browse files Browse the repository at this point in the history
…lock, normalized consumption
  • Loading branch information
sbenthall committed Jul 3, 2024
1 parent 2beb4c6 commit 2db6527
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
4 changes: 4 additions & 0 deletions HARK/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ def __post_init__(self):
if isinstance(self.dynamics[v], str):
self.dynamics[v] = math_text_to_lambda(self.dynamics[v])

for r in self.reward:
if isinstance(self.reward[r], str):
self.reward[r] = math_text_to_lambda(self.reward[r])

def get_shocks(self):
return self.shocks

Expand Down
39 changes: 8 additions & 31 deletions HARK/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest


import HARK.model as model
import HARK.parser as parser
import yaml

Expand All @@ -17,35 +17,12 @@ def test_parse(self):
self.consumer_yaml_file

config = yaml.load(self.consumer_yaml_file, Loader=parser.harklang_loader())
pass

"""
try:
config = yaml.load(open('perfect_foresight_full_experimental.yaml', 'r'), Loader= get_loader())
# data is copied
assert config['model']['blocks']['consumption'] == config['model']['strategies'][1]['block']
# data is maintained once in memory and referenced in both places
assert config['model']['blocks']['consumption'] is config['model']['strategies'][1]['block']
# object created by parser
c1 = config['model']['blocks']['consumption']['dynamics']['c']
c2 = config['model']['strategies'][1]['block']['dynamics']['c']
# objects are equal
assert c1 == c2
# objects are identical in memory; the reference is shared.
assert c1 is c2
a_str = config['model']['blocks']['consumption']['dynamics']['a']
a_expr = parse_expr(a_str)
a_func = lambdify(list(a_expr.free_symbols), a_expr, "numpy")
m = np.random.random(100)
c = np.random.random(100)

#import pdb; pdb.set_trace()
self.assertEqual(config['calibration']['DiscFac'], 0.96)
self.assertEqual(config['blocks'][0]['name'], 'consumption normalized')

except yaml.YAMLError as exc:
print("Error in configuration file:", exc)
"""
## construct and test the block
cons_norm_block = model.DBlock(**config['blocks'][0])
cons_norm_block.construct_shocks(config['calibration'])
cons_norm_block.discretize({"theta": {"N": 5}})
self.assertEqual(cons_norm_block.calc_reward({"c": 1, "CRRA": 2})["u"], -1.0)

0 comments on commit 2db6527

Please sign in to comment.