-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Bug Fix for 2909 #2979
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
Bug Fix for 2909 #2979
Conversation
a = pm.Uniform('a', lower=0, upper=1, shape=10) | ||
b = pm.Binomial('b', n=1, p=a, shape=10) | ||
array_of_randoms = b.random(size=10000).mean(axis=0) | ||
npt.assert_allclose(array_of_randoms, [0.5338, 0.5443, 0.5313, 0.5392, 0.5372, 0.5435, 0.5287, 0.5432, |
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.
heh, now this is surprising to me that these values are all above 0.5. Any guess why?
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 it varies a bit by the run. That's just one run of it. Is this wrong?
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.
It is random so anything can happen! I am expecting this test case to be equivalent to
samples = []
for _ in range(10000):
a = np.random.uniform(0, 1, 10)
samples.append(np.random.binomial(n=1, p=a))
np.array(samples).mean(axis=0)
and it is very rare to see any values in the resulting array be outside of (0.48, 0.52), much less all 10 values (and less again all 10 being greater than 0.5).
I would guess there is still a subtle bug, or else I am thinking of this wrong.
Ahh interesting.
Yeah I get what you mean finally!!!
So still some subtle bug is here. Is it related to the pm.Uniform being
passed to pm.Binomial - or some bug in my implementation?
…On Thu, May 17, 2018 at 8:39 PM, Colin ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In pymc3/tests/test_distributions_random.py
<#2979 (comment)>:
> @@ -97,6 +97,15 @@ def test_random_sample_returns_nd_array(self):
assert isinstance(mu, np.ndarray)
assert isinstance(tau, np.ndarray)
+ def test_random_sample_returns_correctly(self):
+ # Based on what we discovered in #GH2909
+ with pm.Model():
+ a = pm.Uniform('a', lower=0, upper=1, shape=10)
+ b = pm.Binomial('b', n=1, p=a, shape=10)
+ array_of_randoms = b.random(size=10000).mean(axis=0)
+ npt.assert_allclose(array_of_randoms, [0.5338, 0.5443, 0.5313, 0.5392, 0.5372, 0.5435, 0.5287, 0.5432,
It is random so anything can happen! I am expecting this test case to be
equivalent to
samples = []
for _ in range(10000):
a = np.random.uniform(0, 1, 10)
samples.append(np.random.binomial(n=1, p=a))
np.array(samples).mean(axis=0)
and it is very rare to see any values in the resulting array be outside of
(0.48, 0.52), much less all 10 values (and less again all 10 being greater
than 0.5).
I would guess there is still a subtle bug, or else I am thinking of this
wrong.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2979 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA8DiKntjbeEbOKvoDPbjCu9reLqOO3uks5tzdHbgaJpZM4UDoBJ>
.
--
Peadar Coyle
Skype: springcoilarch
www.twitter.com/springcoil
peadarcoyle.wordpress.com
|
When I run it locally. I get
So my test implementation seems to be incorrect. However, does the output look correct? Or still some subtle bug? |
Originally @ColCarroll said
|
pymc3/distributions/distribution.py
Outdated
@@ -355,7 +357,9 @@ def _draw_value(param, point=None, givens=None): | |||
if point and hasattr(param, 'model') and param.name in point: | |||
return point[param.name] | |||
elif hasattr(param, 'random') and param.random is not None: | |||
return param.random(point=point, size=None) | |||
return param.random(point=point, size=None).mean(axis=0) | |||
elif hasattr(param, 'random') and param.random is not None and size is not None: |
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.
this branch will never get hit (since the above condition will always hit)
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.
Ok I'll remove that branch.
pymc3/distributions/distribution.py
Outdated
@@ -355,7 +357,9 @@ def _draw_value(param, point=None, givens=None): | |||
if point and hasattr(param, 'model') and param.name in point: | |||
return point[param.name] | |||
elif hasattr(param, 'random') and param.random is not None: | |||
return param.random(point=point, size=None) | |||
return param.random(point=point, size=None).mean(axis=0) |
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.
this mean
here is causing the strangeness. In your test, it is just drawing 10 values for a
, taking the mean (which is likely reasonably close to 0.5), and then using that value for all 10k draws of b
.
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.
Let me try removing the mean :)
1f8e094
to
ca8a997
Compare
Ok I've made a few of those changes. I'll wait to see what happens when it runs on travis and then seed the results with those numbers. |
Sounds good. I suspect you will also need to update every single distribution that calls |
ca8a997
to
017f36b
Compare
Seems to make the test case run sensibly locally. I'll let this run and then hardcode in the results. |
Fixing up the test and implementation Adding other draw_values Small test fix
3cdf561
to
b53d413
Compare
Ok it seems that this works to some extent. Do we need other tests? @ColCarroll |
Wow. This looks great! Going to merge since tests pass and it fixes my counterexample, but I will kick the tires on it today. We should also keep an eye on http://pandas.pydata.org/speed/pymc3/ , but I think this is perfect. Thanks for tackling a tough issue, @springcoil ! Can you also add this to the release notes? |
Ok I'll add this to the release notes later
…On Fri, 18 May 2018, 12:40 pm Colin, ***@***.***> wrote:
Merged #2979 <#2979>.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2979 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA8DiApWj8HzSBLHPuZpm1gHVHbG21SKks5tzrMvgaJpZM4UDoBJ>
.
|
#2982 -- release notes done |
I tried to have a go at extending #2946 and adding in a test.
I think there's a chance these tests will be flaky, and I discovered some flakiness locally.
This is an attempt to fix #2909