-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add random kwarg to DensityDist #2106 #2805
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -688,3 +688,47 @@ def ref_rand(size, w, mu, sd): | |
| 'sd': Domain([[1.5, 2., 3.]], edges=(None, None))}, | ||
| size=1000, | ||
| ref_rand=ref_rand) | ||
|
|
||
| def test_density_dist(self): | ||
| def ref_rand(size, mu, sd): | ||
| return st.norm.rvs(size=size, loc=mu, scale=sd) | ||
|
|
||
| class TestDensityDist(pm.DensityDist): | ||
|
|
||
| def __init__(self, **kwargs): | ||
| norm_dist = pm.Normal.dist() | ||
| super(TestDensityDist, self).__init__(logp=norm_dist.logp, random=norm_dist.random) | ||
|
|
||
| pymc3_random(TestDensityDist, {},ref_rand=ref_rand) | ||
|
|
||
| def check_model_samplability(self): | ||
| model = pm.Model() | ||
| with model: | ||
| normal_dist = pm.Normal.dist() | ||
| density_dist = pm.DensityDist('density_dist', normal_dist.logp, random=normal_dist.random) | ||
|
||
| step = pm.Metropolis() | ||
| trace = pm.sample(100, step, tuning=0) | ||
|
|
||
| try: | ||
| ppc = pm.sample_ppc(trace, samples=500, model=model, size=100) | ||
| if len(ppc) == 0: | ||
| npt.assert_true(len(ppc) == 0, 'length of ppc sample is zero') | ||
| except: | ||
| assert False | ||
|
|
||
| def check_scipy_distributions(self): | ||
| model = pm.Model() | ||
| with model: | ||
| norm_dist_logp = st.norm.logpdf | ||
| norm_dist_random = np.random.normal | ||
| density_dist = pm.DensityDist('density_dist', normal_dist_logp, random=normal_dist_random) | ||
|
Member
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 just realized, I think we need to turn this into an observed to actually get sampling from this. |
||
| step = pm.Metropolis() | ||
| trace = pm.sample(100, step, tuning=0) | ||
|
|
||
| try: | ||
| ppc = pm.sample_ppc(trace, samples=500, model=model, size=100) | ||
| if len(ppc) == 0: | ||
|
Member
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. can remove this line. |
||
| npt.assert_true(len(ppc) == 0, 'length of ppc sample is zero') | ||
| except: | ||
| assert False | ||
|
|
||
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.
Should add this to the doc-string.