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

Labeled dist-of-fun #1306

Merged
merged 3 commits into from
Jul 26, 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
2 changes: 1 addition & 1 deletion HARK/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ def func_wrapper(x: np.ndarray, *args: Any) -> np.ndarray:

if len(kwargs):
f_query = func(self.dataset, **kwargs)
ldd = DiscreteDistributionLabeled.from_dataset(f_query, self.pmv)
ldd = DiscreteDistributionLabeled.from_dataset(f_query, self.probability)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably standardize this with DiscreteDistribution ... that's on me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was the only kind of "bug" this addresses; it makes some cases work that didn't before.

The other stuff is a design question. Happy to merge this in without resolving that, but also happy to have a conversation, agree, and implement it.


return ldd

Expand Down
39 changes: 39 additions & 0 deletions HARK/tests/test_distribution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

import numpy as np
import xarray as xr

from HARK.distribution import (
Bernoulli,
Expand Down Expand Up @@ -603,3 +604,41 @@ def test_combine_labeled_dist(self):
np.concatenate([de.expected(), abc.expected()]),
)
)


class labeled_transition_tests(unittest.TestCase):
def setUp(self) -> None:
return super().setUp()

def test_expectation_transformation(self):
# Create a basic labeled distribution
base_dist = DiscreteDistributionLabeled(
pmv=np.array([0.5, 0.5]),
atoms=np.array([[1.0, 2.0], [3.0, 4.0]]),
var_names=["a", "b"],
)

# Define a transition function
def transition(shocks, state):
state_new = {}
state_new["m"] = state["m"] * shocks["a"]
state_new["n"] = state["n"] * shocks["b"]
return state_new

m = xr.DataArray(np.linspace(0, 10, 11), name="m", dims=("grid",))
n = xr.DataArray(np.linspace(0, -10, 11), name="n", dims=("grid",))
state_grid = xr.Dataset({"m": m, "n": n})

# Evaluate labeled transformation

# Direct expectation
exp1 = base_dist.expected(transition, state=state_grid)
# Expectation after transformation
new_state_dstn = base_dist.dist_of_func(transition, state=state_grid)
# TODO: needs a cluncky identity function with an extra argument because
# DDL.expected() behavior is very different with and without kwargs.
# Fix!
exp2 = new_state_dstn.expected(lambda x, unused: x, unused=0)

assert np.all(exp1["m"] == exp2["m"]).item()
assert np.all(exp1["n"] == exp2["n"]).item()