Skip to content

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

Merged
merged 1 commit into from
May 18, 2018
Merged

Bug Fix for 2909 #2979

merged 1 commit into from
May 18, 2018

Conversation

springcoil
Copy link
Contributor

@springcoil springcoil commented May 17, 2018

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

@springcoil springcoil requested a review from ColCarroll May 17, 2018 19:15
@springcoil springcoil changed the title BUG: Attempt to fix 2909 WIP: Attempt to fix 2909 May 17, 2018
@springcoil springcoil modified the milestones: 3.5, 3.4 May 17, 2018
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,
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

@springcoil
Copy link
Contributor Author

springcoil commented May 17, 2018 via email

@springcoil
Copy link
Contributor Author

When I run it locally.

I get

Out[10]:
array([0.3491, 0.3488, 0.3407, 0.3508, 0.3531, 0.3508, 0.3471, 0.3474,
       0.3461, 0.3523])

In [11]: b.random(size=10000).mean(axis=0)
Out[11]:
array([0.7232, 0.7302, 0.7265, 0.7278, 0.729 , 0.7369, 0.7249, 0.7228,
       0.728 , 0.7335])

In [12]: b.random(size=10000).mean(axis=0)
Out[12]:
array([0.4958, 0.4996, 0.4979, 0.4898, 0.4962, 0.4933, 0.4972, 0.4999,
       0.4938, 0.5012])

In [13]: b.random(size=100000).mean(axis=0)
Out[13]:
array([0.59457, 0.59537, 0.59586, 0.59437, 0.59508, 0.59411, 0.59349,
       0.59711, 0.59586, 0.59473])

So my test implementation seems to be incorrect. However, does the output look correct? Or still some subtle bug?

@springcoil
Copy link
Contributor Author

Originally @ColCarroll said


with pm.Model() as model:
    a = pm.Uniform('a', lower=0, upper=1, shape=10)
    b = pm.Binomial('b', n=1, p=a, shape=10)
    
b.random(size=10000).mean(axis=0)

# array([0.7022, 0.0073, 0.9857, 0.5378, 0.9821, 0.7176, 0.0905, 0.2513, 0.5835, 0.0521])
I would have expected this mean to be close to 0.5 for each element.

``` - is this expected behaviour incorrect now? 

@@ -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:
Copy link
Member

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)

Copy link
Contributor Author

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.

@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

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 :)

@springcoil springcoil force-pushed the 2909-attempt-at-fix branch from 1f8e094 to ca8a997 Compare May 17, 2018 20:14
@springcoil
Copy link
Contributor Author

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.

@ColCarroll
Copy link
Member

Sounds good. I suspect you will also need to update every single distribution that calls draw_values. Maybe try uniform and binomial to see if it makes the test case run sensibly? The uniform call is here.

@springcoil springcoil force-pushed the 2909-attempt-at-fix branch from ca8a997 to 017f36b Compare May 17, 2018 20:46
@springcoil
Copy link
Contributor Author

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
@springcoil springcoil force-pushed the 2909-attempt-at-fix branch from 3cdf561 to b53d413 Compare May 17, 2018 21:49
@springcoil
Copy link
Contributor Author

Ok it seems that this works to some extent. Do we need other tests? @ColCarroll

@springcoil springcoil changed the title WIP: Attempt to fix 2909 Bug Fix for 2909 May 18, 2018
@springcoil springcoil removed the WIP label May 18, 2018
@twiecki twiecki mentioned this pull request May 18, 2018
@ColCarroll
Copy link
Member

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?

@ColCarroll ColCarroll merged commit ac8fe5a into master May 18, 2018
@springcoil
Copy link
Contributor Author

springcoil commented May 18, 2018 via email

@junpenglao junpenglao deleted the 2909-attempt-at-fix branch May 18, 2018 12:51
@springcoil
Copy link
Contributor Author

#2982 -- release notes done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected behavior in random
2 participants