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

Inconsistencies in HARK.distribution: DiscreteDistributionLabeled, combine_indep_dstns, expected #1483

Closed
sbenthall opened this issue Aug 15, 2024 · 1 comment
Assignees
Milestone

Comments

@sbenthall
Copy link
Contributor

sbenthall commented Aug 15, 2024

There's something funky about how a few of our distribution operations work together.

The following code calls on several features in HARK.distribution:

  • DiscreteDistributionLabeled
  • combine_indep_dstns
  • expected

and breaks unexpectedly.

from HARK.distribution import expected, Normal
from HARK.model import discretized_shock_dstn
import numpy as np

shocks = {
    "gamma": Normal(mu= 0, sigma = 1)
}

approx = {
    "gamma" : {"N" : 7}
}

ds = discretized_shock_dstn(shocks, approx)

discretized_shock_dstn creates a DiscreteDistributionLabeled class for each shock passed in (as a dictionary), and then combines them. See here: https://github.com/econ-ark/HARK/blob/master/HARK/model.py#L44

Then the following code breaks:

def sample_f(shocks):    
    return shocks["gamma"] * np.array([2,4])

expected(func = sample_f, dist = ds)

This produces an error:

Cell In[18], line 3, in sample_f(shocks)
      1 def sample_f(shocks):
----> 3     return shocks["gamma"] * np.array([2,4])

ValueError: operands could not be broadcast together with shapes (7,) (2,) 

The issue being that the full grid of the discretized distribution is being passed (in a dictionary, 'labeled' form) to the function sample_f, instead of one value being passed in at a time. This doesn't typically raise an error, but it does when the function includes an array operation.

I believe these different functions in HARK.distribution were written by different people, so many it's a matter of harmonizing them.

@sbenthall sbenthall added this to the 1.0.0 milestone Aug 15, 2024
@sbenthall
Copy link
Contributor Author

Oh, this looks like it's because of the difference between expected and calc_expectations.
expected wants the function passed to it to take... the whole vector of probability atoms?

It says "This function should take the full array of distribution values
and return either arrays of arbitrary shape or scalars."

https://github.com/econ-ark/HARK/blob/master/HARK/distribution.py#L2430

That doesn't make sense to me... the values of either the probabilities or the realized values isn't enough to compute the expected value, and there isn't an argument for both.

I see this is widely used in the HARK models but the docs don't make sense to me. Can somebody explain?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Triage
Development

No branches or pull requests

2 participants