diff --git a/Documentation/CHANGELOG.md b/Documentation/CHANGELOG.md index 38cc9eab2..8d09cc628 100644 --- a/Documentation/CHANGELOG.md +++ b/Documentation/CHANGELOG.md @@ -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 diff --git a/HARK/metric.py b/HARK/metric.py index d1151058e..3c434d782 100644 --- a/HARK/metric.py +++ b/HARK/metric.py @@ -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( diff --git a/HARK/tests/test_core.py b/HARK/tests/test_core.py index 76fed5d12..6b61a80f8 100644 --- a/HARK/tests/test_core.py +++ b/HARK/tests/test_core.py @@ -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 @@ -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"]