Description
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 for DensityDist
.
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
pytest pymc3/tests/test_distributions_random.py::test_density_dist_with_random_sampleable
... 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:mu, tau, _ = draw_values([self.mu, self.tau, self.sigma], point=point, size=size) return generate_samples(stats.norm.rvs, loc=mu, scale=tau**-0.5, dist_shape=self.shape, size=size)
The difference is that the DensityDist
has no built in mechanism to force the supplied random
function to honor the shape
attribute of the distribution. So, as this test shows, the shape is ignored, which is not the same thing as happens with other ObservedRV
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
- PyMC3 Version:
master
as of today - Other components: irrelevant