-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add tests for distributions moments #5087
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pymc import Bernoulli, Flat, HalfFlat, Normal, TruncatedNormal, Uniform | ||
from pymc.distributions import HalfNormal | ||
from pymc.distributions.shape_utils import rv_size_is_none | ||
from pymc.initial_point import make_initial_point_fn | ||
from pymc.model import Model | ||
|
||
|
||
def test_rv_size_is_none(): | ||
rv = Normal.dist(0, 1, size=None) | ||
assert rv_size_is_none(rv.owner.inputs[1]) | ||
|
||
rv = Normal.dist(0, 1, size=1) | ||
assert not rv_size_is_none(rv.owner.inputs[1]) | ||
|
||
size = Bernoulli.dist(0.5) | ||
rv = Normal.dist(0, 1, size=size) | ||
assert not rv_size_is_none(rv.owner.inputs[1]) | ||
|
||
size = Normal.dist(0, 1).size | ||
rv = Normal.dist(0, 1, size=size) | ||
assert not rv_size_is_none(rv.owner.inputs[1]) | ||
|
||
|
||
def assert_moment_is_expected(model, expected): | ||
fn = make_initial_point_fn( | ||
model=model, | ||
return_transformed=False, | ||
default_strategy="moment", | ||
) | ||
result = fn(0)["x"] | ||
expected = np.asarray(expected) | ||
try: | ||
random_draw = model["x"].eval() | ||
except NotImplementedError: | ||
random_draw = result | ||
assert result.shape == expected.shape == random_draw.shape | ||
assert np.allclose(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"size, expected", | ||
[ | ||
(None, 0), | ||
(5, np.zeros(5)), | ||
((2, 5), np.zeros((2, 5))), | ||
], | ||
) | ||
def test_flat_moment(size, expected): | ||
with Model() as model: | ||
Flat("x", size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"size, expected", | ||
[ | ||
(None, 1), | ||
(5, np.ones(5)), | ||
((2, 5), np.ones((2, 5))), | ||
], | ||
) | ||
def test_halfflat_moment(size, expected): | ||
with Model() as model: | ||
HalfFlat("x", size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"lower, upper, size, expected", | ||
[ | ||
(-1, 1, None, 0), | ||
(-1, 1, 5, np.zeros(5)), | ||
(0, np.arange(1, 6), None, np.arange(1, 6) / 2), | ||
(0, np.arange(1, 6), (2, 5), np.full((2, 5), np.arange(1, 6) / 2)), | ||
], | ||
) | ||
def test_uniform_moment(lower, upper, size, expected): | ||
with Model() as model: | ||
Uniform("x", lower=lower, upper=upper, size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"mu, sigma, size, expected", | ||
[ | ||
(0, 1, None, 0), | ||
(0, np.ones(5), None, np.zeros(5)), | ||
(np.arange(5), 1, None, np.arange(5)), | ||
(np.arange(5), np.arange(1, 6), (2, 5), np.full((2, 5), np.arange(5))), | ||
], | ||
) | ||
def test_normal_moment(mu, sigma, size, expected): | ||
with Model() as model: | ||
Normal("x", mu=mu, sigma=sigma, size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"sigma, size, expected", | ||
[ | ||
(1, None, 1), | ||
(1, 5, np.ones(5)), | ||
(np.arange(5), None, np.arange(5)), | ||
(np.arange(5), (2, 5), np.full((2, 5), np.arange(5))), | ||
], | ||
) | ||
def test_halfnormal_moment(sigma, size, expected): | ||
with Model() as model: | ||
HalfNormal("x", sigma=sigma, size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.skip(reason="aeppl interval transform fails when both edges are None") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@pytest.mark.parametrize( | ||
"mu, sigma, lower, upper, size, expected", | ||
[ | ||
(0.9, 1, -1, 1, None, 0), | ||
(0.9, 1, -np.inf, np.inf, 5, np.full(5, 0.9)), | ||
(np.arange(5), 1, None, 10, (2, 5), np.full((2, 5), 9)), | ||
(1, np.ones(5), -10, np.inf, None, np.full((2, 5), -9)), | ||
], | ||
) | ||
def test_truncatednormal_moment(mu, sigma, lower, upper, size, expected): | ||
with Model() as model: | ||
TruncatedNormal("x", mu=mu, sigma=sigma, lower=lower, upper=upper, size=size) | ||
assert_moment_is_expected(model, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"p, size, expected", | ||
[ | ||
(0.3, None, 0), | ||
(0.9, 5, np.ones(5)), | ||
(np.linspace(0, 1, 4), None, [0, 0, 1, 1]), | ||
(np.linspace(0, 1, 4), (2, 4), np.full((2, 4), [0, 0, 1, 1])), | ||
], | ||
) | ||
def test_bernoulli_moment(p, size, expected): | ||
with Model() as model: | ||
Bernoulli("x", p=p, size=size) | ||
assert_moment_is_expected(model, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a doubt here.
Current implementation: loc + sigma
Wikipedia version: loc + \sqrt{\frac{2}{\pi}}sigma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are right, we should probably be using that. Do you want to open a PR to fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, happily :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit more involved, we should probably also use the truncated normal mean as the moment. We might even be able to simplify the switch statement, if the normal logcdf behaves well with +- infinity values: https://en.wikipedia.org/wiki/Truncated_normal_distribution