Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Dictionary metric #1260

Merged
merged 3 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Release Date: TBD
### Minor Changes

- Adds option `sim_common_Rrisky` to control whether risky-asset models draw common or idiosyncratic returns in simulation. [#1250](https://github.com/econ-ark/HARK/pull/1250),[#1253](https://github.com/econ-ark/HARK/pull/1253)
- Fixes bug in the metric that compares dictionaries with the same keys. [#1260](https://github.com/econ-ark/HARK/pull/1260)

### 0.13.0

Expand Down
2 changes: 1 addition & 1 deletion HARK/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def distance_dicts(dict_a, dict_b):
len_b = len(dict_b)

if len_a == len_b:
if set(dict_a.keys()) == set(dict_b.keys()):
if set(dict_a.keys()) != set(dict_b.keys()):
warn("Dictionaries with keys that do not match are being compared.")
return 1000.0
return np.max(
Expand Down
9 changes: 9 additions & 0 deletions HARK/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def setUp(self):
self.obj_a = MetricObject()
self.obj_b = MetricObject()
self.obj_c = MetricObject()
self.dict_a = {"a": 1, "b": 2}
self.dict_b = {"a": 3, "b": 4}
self.dict_c = {"a": 5, "f": 6}

def test_list(self):
# same length
Expand All @@ -46,6 +49,12 @@ def test_array(self):
distance_metric(np.array(self.list_b), np.array(self.list_b)), 0.0
)

def test_dict(self):
# Same keys (max of diffs across keys)
self.assertEqual(distance_metric(self.dict_a, self.dict_b), 2.0)
# Different keys
self.assertEqual(distance_metric(self.dict_a, self.dict_c), 1000.0)

def test_hark_object_distance(self):
self.obj_a.distance_criteria = ["var_1", "var_2", "var_3"]
self.obj_b.distance_criteria = ["var_1", "var_2", "var_3"]
Expand Down