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

Array-valued discrete distribution #1146

Merged
merged 20 commits into from
Jun 1, 2022
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 @@ -20,6 +20,7 @@ Release Date: TBD
* `FrameModel` and `FrameSet` classes introduced for more modular construction of framed models. `FrameAgentType` dedicated to simulation. [#1117](https://github.com/econ-ark/HARK/pull/1117)
* General control transitions based on decision rules in `FrameAgentType`. [#1117](https://github.com/econ-ark/HARK/pull/1117)
* Adds `distr_of_function` tool to calculate the distribution of a function of a discrete random variable. [#1144](https://github.com/econ-ark/HARK/pull/1144)
* Changes the `DiscreteDistribution` class to allow for arbitrary array-valued random variables. [#1146](https://github.com/econ-ark/HARK/pull/1146)


### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,7 +2800,7 @@ def __init__(self, sigma, n_approx, neutral_measure=False, seed=0):
)
# Change the pmf if necessary
if neutral_measure:
logn_approx.pmf = logn_approx.X * logn_approx.pmf
logn_approx.pmf = (logn_approx.X * logn_approx.pmf).flatten()

super().__init__(pmf=logn_approx.pmf, X=logn_approx.X, seed=seed)

Expand Down
6 changes: 3 additions & 3 deletions HARK/ConsumptionSaving/ConsLaborModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def solve_ConsLaborIntMarg(
# Unpack next period's solution and the productivity shock distribution, and define the inverse (marginal) utilty function
vPfunc_next = solution_next.vPfunc
TranShkPrbs = TranShkDstn.pmf
TranShkVals = TranShkDstn.X
TranShkVals = TranShkDstn.X.flatten()
PermShkPrbs = PermShkDstn.pmf
PermShkVals = PermShkDstn.X
PermShkVals = PermShkDstn.X.flatten()
TranShkCount = TranShkPrbs.size
PermShkCount = PermShkPrbs.size
uPinv = lambda X: CRRAutilityP_inv(X, gam=CRRA)
Expand Down Expand Up @@ -557,7 +557,7 @@ def update_TranShkGrid(self):
TranShkGrid = [] # Create an empty list for TranShkGrid that will be updated
for t in range(self.T_cycle):
TranShkGrid.append(
self.TranShkDstn[t].X
self.TranShkDstn[t].X.flatten()
) # Update/ Extend the list of TranShkGrid with the TranShkVals for each TranShkPrbs
self.TranShkGrid = TranShkGrid # Save that list in self (time-varying)
self.add_to_time_vary(
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/ConsMedModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def update_solution_terminal(self):
"""
# Take last period data, whichever way time is flowing
MedPrice = self.MedPrice[-1]
MedShkVals = self.MedShkDstn[-1].X
MedShkVals = self.MedShkDstn[-1].X.flatten()
MedShkPrbs = self.MedShkDstn[-1].pmf

# Initialize grids of medical need shocks, market resources, and optimal consumption
Expand Down Expand Up @@ -993,7 +993,7 @@ def set_and_update_values(self, solution_next, IncShkDstn, LivPrb, DiscFac):

# Also unpack the medical shock distribution
self.MedShkPrbs = self.MedShkDstn.pmf
self.MedShkVals = self.MedShkDstn.X
self.MedShkVals = self.MedShkDstn.X.flatten()

def def_utility_funcs(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/ConsPrefShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def __init__(
CubicBool,
)
self.PrefShkPrbs = PrefShkDstn.pmf
self.PrefShkVals = PrefShkDstn.X
self.PrefShkVals = PrefShkDstn.X.flatten()

def get_points_for_interpolation(self, EndOfPrdvP, aNrmNow):
"""
Expand Down Expand Up @@ -685,4 +685,4 @@ def __init__(
CubicBool,
)
self.PrefShkPrbs = PrefShkDstn.pmf
self.PrefShkVals = PrefShkDstn.X
self.PrefShkVals = PrefShkDstn.X.flatten()
2 changes: 1 addition & 1 deletion HARK/ConsumptionSaving/ConsRiskyContribModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ def post_return_derivs(inc_shocks, b_aux, g_aux, s):

# Define grids
b_aux_grid = np.concatenate([np.array([0.0]), Rfree * aXtraGrid])
g_aux_grid = np.concatenate([np.array([0.0]), max(RiskyDstn.X) * nNrmGrid])
g_aux_grid = np.concatenate([np.array([0.0]), max(RiskyDstn.X.flatten()) * nNrmGrid])
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like a lot of extra code is now needed to flip vertical vectors/arrays into horizontal ones.
Would it be possible to have the arrays oriented horizontal in the DiscreteDistribution object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possible yes, but I still thought this would be the path of least resistance.

I think other parts of HARK that deal with multivariate distributions use things like dstn.X[n] to find all the possible draws of the nth dimension. This seems like an intuitive and compact code pattern that I would like to keep. It was easier for me to conceptually think that nature was always the last dimension.

Copy link
Contributor

Choose a reason for hiding this comment

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

"X" is truly a terrible name for an object property that is doing so much work. See #1051

I'm not suggesting you replace "X" with something else in this PR, but I wonder what mathematical object X is now.

It's possible that we could have multiple ways of accessing this data within a distribution, so it requires less manipulation before entering and after pulling it from the object.


# Create tiled arrays with conforming dimensions.
b_aux_tiled, g_aux_tiled, Share_tiled = np.meshgrid(
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/tests/test_ConsMarkovModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def setUp(self):

# Replace the default (lognormal) income distribution with a custom one
employed_income_dist = DiscreteDistribution(
np.ones(1), [np.ones(1), np.ones(1)]
np.ones(1), np.array([[1.0],[1.0]])
) # Definitely get income
unemployed_income_dist = DiscreteDistribution(
np.ones(1), [np.ones(1), np.zeros(1)]
np.ones(1), np.array([[1.0],[0.0]])
) # Definitely don't
self.model.IncShkDstn = [
[
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_solution(self):
self.assertEqual(self.agent.solution[0].mNrmMin, 0)
m = np.linspace(self.agent.solution[0].mNrmMin, 5, 200)

self.assertAlmostEqual(self.agent.PrefShkDstn[0].X[5], 0.69046812)
self.assertAlmostEqual(self.agent.PrefShkDstn[0].X[0,5], 0.69046812)

self.assertAlmostEqual(
self.agent.solution[0].cFunc(m, np.ones_like(m))[35], 0.8123891603954809
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_solution(self):

m = np.linspace(self.agent.solution[0].mNrmMin, 5, 200)

self.assertAlmostEqual(self.agent.PrefShkDstn[0].X[5], 0.6904681186891202)
self.assertAlmostEqual(self.agent.PrefShkDstn[0].X[0,5], 0.6904681186891202)

c = self.agent.solution[0].cFunc(m, np.ones_like(m))
self.assertAlmostEqual(c[5], 0.13237946)
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/tests/test_modelcomparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def setUp(self):
MarkovType = MarkovConsumerType(**Markov_primitives)
MarkovType.cycles = 0
employed_income_dist = DiscreteDistribution(
np.ones(1), [np.ones(1), np.ones(1)]
np.ones(1), np.array([[1.0],[1.0]])
)
unemployed_income_dist = DiscreteDistribution(
np.ones(1), [np.ones(1), np.zeros(1)]
np.ones(1), np.array([[1.0],[0.0]])
)
MarkovType.IncShkDstn = [[employed_income_dist, unemployed_income_dist]]

Expand Down
4 changes: 2 additions & 2 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ def distribute_params(agent, param_name, param_count, distribution):
param_count : int
Number of different values the parameter will take on.
distribution : Distribution
A distribution.
A 1-D distribution.

Returns
-------
Expand All @@ -1567,7 +1567,7 @@ def distribute_params(agent, param_name, param_count, distribution):
agent_set[j].assign_parameters(**{'AgentCount': int(agent.AgentCount * param_dist.pmf[j])})
# agent_set[j].__dict__[param_name] = param_dist.X[j]

agent_set[j].assign_parameters(**{param_name: param_dist.X[j]})
agent_set[j].assign_parameters(**{param_name: param_dist.X[0,j]})



Expand Down
Loading