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

Uniform endpoints #1180

Merged
merged 7 commits into from
Nov 10, 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 @@ -47,6 +47,7 @@ Release Date: TBD
* Renames `DiscreteDistributionXRA` to `DiscreteDistributionLabeled` and updates methods [#1170](https://github.com/econ-ark/HARK/pull/1170)
* Renames `HARK.numba` to `HARK.numba_tools` [#1183](https://github.com/econ-ark/HARK/pull/1183)
* Adds the RNG seed as a property of `DiscreteDistributionLabeled` [#1184](https://github.com/econ-ark/HARK/pull/1184)
* Updates the `approx` method of `HARK.distributions.Uniform` to include the endpoints of the distribution with infinitesimally small (zero) probability mass. [#1180](https://github.com/econ-ark/HARK/pull/1180)

### 0.12.0

Expand Down
12 changes: 10 additions & 2 deletions HARK/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,14 +742,16 @@ def draw(self, N):
)
return draws[0] if len(draws) == 1 else draws

def approx(self, N):
def approx(self, N, endpoint=False):
"""
Makes a discrete approximation to this uniform distribution.

Parameters
----------
N : int
The number of points in the discrete approximation
The number of points in the discrete approximation.
endpoint : bool
Whether to include the endpoints in the approximation.

Returns
-------
Expand All @@ -758,11 +760,17 @@ def approx(self, N):
points for discrete probability mass function.
"""
pmv = np.ones(N) / float(N)

center = (self.top + self.bot) / 2.0
width = (self.top - self.bot) / 2.0
atoms = center + width * np.linspace(-(N - 1.0) / 2.0, (N - 1.0) / 2.0, N) / (
N / 2.0
)

if endpoint: # insert endpoints with infinitesimally small mass
atoms = np.concatenate(([self.bot], atoms, [self.top]))
pmv = np.concatenate(([0.0], pmv, [0.0]))

return DiscreteDistribution(
pmv, atoms, seed=self.RNG.randint(0, 2**31 - 1, dtype="int32")
)
Expand Down
14 changes: 10 additions & 4 deletions HARK/tests/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,16 @@ def test_MVNormal(self):
## Are these tests generator/backend specific?
dist = MVNormal()

#self.assertTrue(
# self.assertTrue(
# np.allclose(dist.draw(1)[0], np.array([2.76405235, 1.40015721]))
#)
# )

dist.draw(100)
dist.reset()

#self.assertTrue(
# self.assertTrue(
# np.allclose(dist.draw(1)[0], np.array([2.76405235, 1.40015721]))
#)
# )

def test_Weibull(self):
Weibull().draw(1)[0]
Expand All @@ -333,6 +333,12 @@ def test_Uniform(self):

self.assertEqual(calc_expectation(uni.approx(10)), 0.5)

uni_discrete = uni.approx(10, endpoint=True)

self.assertEqual(uni_discrete.atoms[0][0], 0.0)
self.assertEqual(uni_discrete.atoms[0][-1], 1.0)
self.assertEqual(calc_expectation(uni.approx(10)), 0.5)

def test_Bernoulli(self):
Bernoulli().draw(1)[0]

Expand Down