Skip to content

Commit

Permalink
Merge pull request #884 from sbenthall/calcExpectation
Browse files Browse the repository at this point in the history
Calc expectation
  • Loading branch information
sbenthall authored Dec 15, 2020
2 parents 78ea75d + df82892 commit ccb45f3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ For more information on HARK, see [our Github organization](https://github.com/e

## Changes

### 0.10.9

Release Data: TBD

#### Major Changes

* calcExpectations method for taking the expectation of a distribution over a function (#884)[https://github.com/econ-ark/HARK/pull/884/]

### 0.10.8

Release Data: Nov. 05 2020
Expand Down
11 changes: 10 additions & 1 deletion HARK/ConsumptionSaving/ConsAggShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
CRRAutility_inv,
makeGridExpMult,
)
from HARK.distribution import Uniform, calcExpectation
from HARK.distribution import (
MeanOneLogNormal,
Uniform,
combineIndepDstns,
calcExpectation
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
ConsumerSolution,
IndShockConsumerType,
Expand Down Expand Up @@ -1109,6 +1114,8 @@ def solveConsAggShock(
The net interest factor on assets as a function of capital ratio k.
wFunc : function
The wage rate for labor as a function of capital-to-labor ratio k.
DeprFac : float
Capital Depreciation Rate
Returns
-------
Expand Down Expand Up @@ -1449,6 +1456,8 @@ def solveConsAggMarkov(
The net interest factor on assets as a function of capital ratio k.
wFunc : function
The wage rate for labor as a function of capital-to-labor ratio k.
DeprFac : float
Capital Depreciation Rate
Returns
-------
Expand Down
8 changes: 5 additions & 3 deletions HARK/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def draw(self, N):
----------
N : int
Number of draws in each row.
seed : int
Seed for random number generator.
Returns:
------------
Expand Down Expand Up @@ -217,7 +219,6 @@ def __init__(self, sigma=1.0, seed=0):
mu = -0.5 * sigma ** 2
super().__init__(mu=mu, sigma=sigma, seed=seed)


class Normal(Distribution):
"""
A Normal distribution.
Expand Down Expand Up @@ -584,6 +585,7 @@ def drawDiscrete(self, N, X=None, exact_match=False):
int
) # cutoff points between discrete outcomes
top = 0

# Make a list of event indices that closely matches the discrete distribution
event_list = []
for j in range(events.size):
Expand Down Expand Up @@ -964,8 +966,8 @@ def combineIndepDstns(*distributions, seed=0):
# probability array. So get the probability array, P_out, here.
P_out = np.prod(np.array(P_temp), axis=0)

assert np.isclose(np.sum(P_out),1),'Probabilities do not sum to 1!'
return DiscreteDistribution(P_out, X_out)
assert np.isclose(np.sum(P_out), 1), "Probabilities do not sum to 1!"
return DiscreteDistribution(P_out, X_out, seed=seed)


def calcExpectation(dstn,func=None,values=None):
Expand Down
7 changes: 7 additions & 0 deletions HARK/tests/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def test_Weibull(self):
self.assertEqual(distribution.Weibull().draw(1)[0], 0.79587450816311)

def test_Uniform(self):
uni = distribution.Uniform()

self.assertEqual(distribution.Uniform().draw(1)[0], 0.5488135039273248)

self.assertEqual(
distribution.calcExpectation(uni.approx(10))[0],
0.5
)

def test_Bernoulli(self):
self.assertEqual(distribution.Bernoulli().draw(1)[0], False)

0 comments on commit ccb45f3

Please sign in to comment.