-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Make draw_values draw from the joint distribution #3214
Closed
Closed
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6aedaa9
Merge pull request #1 from pymc-devs/master
lucianopaz 32ce3c2
Merge branch 'master' of https://github.com/pymc-devs/pymc3
lucianopaz bd25baa
Resolved merge conflicts with upstream master, which I had not fetche…
lucianopaz f6ecb23
Fixed most of the bugs encountered due to the incorrect upstream fetc…
lucianopaz df5e3ae
Fixed collections import error
lucianopaz d43d149
Fixed list copy and defaults of DependenceDAG.__init__
lucianopaz 339828d
Implemented `get_first_level_conditionals` to try to get rid of the a…
lucianopaz 890ae74
Cleaned up model.py, made it comply with pep8, and fixed lint error o…
lucianopaz 237f8ba
Fix get_first_level_conditionals and also made DependenceDAG a subcla…
lucianopaz 4ef4ea3
Completely removed DependenceDAG class. The variable dependence graph…
lucianopaz 659647e
Reverted unnecessary format changes.
lucianopaz 4371af2
Added tests for WrapAsHashable. Made get_first_layer_conditionals mor…
lucianopaz 6298d71
Added __ne__ for WrapAsHashable, which delegates to __eq__. This must…
lucianopaz 31d36a2
Resolve comments from PR.
lucianopaz 672907c
Fixed typo. Changed dag edge attributes to only deterministic 0 or 1.…
lucianopaz ba8305f
Finished adaptation of ModelGraph to use networkx for plate detection…
lucianopaz fbbf4c3
Merge branch 'master' into master
lucianopaz 08eccbf
Fixed lint errors and test_step error due to upstream merge conflict.
lucianopaz 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import pymc3 as pm | ||
import numpy as np | ||
import numpy.random as nr | ||
import numpy.testing as npt | ||
import pytest | ||
import theano.tensor as tt | ||
import theano | ||
|
||
from .helpers import SeededTest | ||
from pymc3.distributions.distribution import _draw_value, draw_values | ||
|
||
|
||
|
@@ -60,7 +61,8 @@ def test_vals(self): | |
|
||
def test_simple_model(self): | ||
with pm.Model(): | ||
mu = 2 * tt.constant(np.array([5., 6.])) + theano.shared(np.array(5)) | ||
mu = (2 * tt.constant(np.array([5., 6.])) + | ||
theano.shared(np.array(5))) | ||
a = pm.Normal('a', mu=mu, sd=5, shape=2) | ||
|
||
val1 = draw_values([a]) | ||
|
@@ -72,7 +74,8 @@ def test_simple_model(self): | |
|
||
def test_dep_vars(self): | ||
with pm.Model(): | ||
mu = 2 * tt.constant(np.array([5., 6.])) + theano.shared(np.array(5)) | ||
mu = (2 * tt.constant(np.array([5., 6.])) + | ||
theano.shared(np.array(5))) | ||
sd = pm.HalfNormal('sd', shape=2) | ||
tau = 1 / sd ** 2 | ||
a = pm.Normal('a', mu=mu, tau=tau, shape=2) | ||
|
@@ -84,7 +87,63 @@ def test_dep_vars(self): | |
val2 = draw_values([a], point={'sd': np.array([2., 3.])})[0] | ||
val3 = draw_values([a], point={'sd_log__': np.array([2., 3.])})[0] | ||
val4 = draw_values([a], point={'sd_log__': np.array([2., 3.])})[0] | ||
|
||
assert all([np.all(val1 != val2), np.all(val1 != val3), | ||
np.all(val1 != val4), np.all(val2 != val3), | ||
np.all(val2 != val4), np.all(val3 != val4)]) | ||
|
||
|
||
class TestJointDistributionDrawValues(SeededTest): | ||
def test_joint_distribution(self): | ||
with pm.Model() as model: | ||
a = pm.Normal('a', mu=0, sd=100) | ||
b = pm.Normal('b', mu=a, sd=1e-8) | ||
c = pm.Normal('c', mu=a, sd=1e-8) | ||
d = pm.Deterministic('d', b + c) | ||
|
||
# Expected RVs | ||
nr.seed(self.random_seed) | ||
N = 1000 | ||
norm = np.random.randn(N, 3) | ||
eA = norm[:, 0] * 100 | ||
eB = eA + norm[:, 1] * 1e-8 | ||
eC = eA + norm[:, 2] * 1e-8 | ||
eD = eB + eC | ||
|
||
# Drawn RVs | ||
nr.seed(self.random_seed) | ||
A, B, C, D = list(zip(*[draw_values([a, b, c, d]) for i in range(N)])) | ||
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. I think that here and below you should use |
||
A = np.array(A).flatten() | ||
B = np.array(B).flatten() | ||
C = np.array(C).flatten() | ||
D = np.array(D).flatten() | ||
|
||
# Assert that the drawn samples match the expected values | ||
assert np.allclose(eA, A) | ||
assert np.allclose(eB, B) | ||
assert np.allclose(eC, C) | ||
assert np.allclose(eD, D) | ||
|
||
# Assert that A, B and C have the expected difference | ||
assert np.all(np.abs(A - B) < 1e-6) | ||
assert np.all(np.abs(A - C) < 1e-6) | ||
assert np.all(np.abs(B - C) < 1e-6) | ||
|
||
# Marginal draws | ||
mA = np.array([draw_values([a]) for i in range(N)]).flatten() | ||
mB = np.array([draw_values([b]) for i in range(N)]).flatten() | ||
mC = np.array([draw_values([c]) for i in range(N)]).flatten() | ||
# Also test the with model context of draw_values | ||
with model: | ||
mD = np.array([draw_values([d]) for i in range(N)]).flatten() | ||
|
||
# Assert that the marginal distributions have different sample values | ||
assert not np.all(np.abs(B - mB) < 1e-2) | ||
assert not np.all(np.abs(C - mC) < 1e-2) | ||
assert not np.all(np.abs(D - mD) < 1e-2) | ||
|
||
# Assert that the marginal distributions do not have high cross | ||
# correlation | ||
assert np.abs(np.corrcoef(mA, mB)[0, 1]) < 0.1 | ||
assert np.abs(np.corrcoef(mA, mC)[0, 1]) < 0.1 | ||
assert np.abs(np.corrcoef(mB, mC)[0, 1]) < 0.1 |
This file contains 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
Oops, something went wrong.
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.
SeededTest
will set a random seed before the test runs, so no need to set it in here.