-
-
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
sample_posterior_predictive generates incorrect-shape samples for DensityDist #3553
Comments
Oh, this is a hard one. I think that we can only aim to wrap the random method to check that the distribution's shape matches the shapes of the drawn samples and then raise an exception otherwise. We should document it and state that the users must take great care in cases where their DensityDists are related to shared tensor's |
With this check in place, I am now seeing problems with For some reason, adding a shape check is causing this test to fail. @lucianopaz -- would you please look at this? I believe my shape error checking is colliding with the structure of Also, @lucianopaz, did I misinterpret your suggestion? I was checking all of random calls in |
@rpgoldman I hadn't seen your last comment before writing #3554, which only addresses the DensityDist stuff. |
@rpgoldman, I just looked at the exception you're seeing and I can confirm it is not a bug. It is related to the fixed issue #3147. Basically, a distribution has its shape, the distribution's parameters have their shape, and the samples returned by the distribution's random broadcast both the parameters' shape and the distribution's own shape. It is part of our very own shell (shape hell). I have to write some proper documentation about this kind of stuff eventually. |
This is fixed by #3554 |
If you have questions about a specific use case, or you are not sure whether this is a bug or not, please post it to our discourse channel: https://discourse.pymc.io
Description of your problem
When generating posterior predictive samples, we try to generate samples for
ObservedRV
s that match the shape of the observations. This fails forDensityDist
.Example
In #3552 I revised
tests/test_distributions_random.py::test_density_dist_with_random_sampleable
to check the shape of the values for the observed random variable, instead of just the length of the trace.So if you check out the change in that PR and run
... you will see the error.
Additional Information
You can see the issue in this test:
We let the user provide an arbitrary
random
function forDensityDist
:pm.DensityDist('density_dist', normal_dist.logp, observed=np.random.randn(observations), random=normal_dist.random)
Contrast this with the random function for the
Normal
distribution:The difference is that the
DensityDist
has no built in mechanism to force the suppliedrandom
function to honor theshape
attribute of the distribution. So, as this test shows, the shape is ignored, which is not the same thing as happens with otherObservedRV
s.As the PR shows, this test should never have been considered as passing for the current definition of
sample_posterior_predictive()
.Suggested solutions
Require the supplied
random
function to take ashape
keyword argument, passshape=self.shape
to it fromDensityDist.random()
, and trust users to Do The Right Thing. Expand the docstring.Disable posterior predictive sampling for
DensityDist
. This seems undesirable.Versions and main components
master
as of todayThe text was updated successfully, but these errors were encountered: