Skip to content

sample_posterior_predictive generates incorrect-shape samples for DensityDist #3553

Closed
@rpgoldman

Description

@rpgoldman

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 ObservedRVs 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 for DensityDist:

    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 ObservedRVs.

As the PR shows, this test should never have been considered as passing for the current definition of sample_posterior_predictive().

Suggested solutions

  1. Require the supplied random function to take a shape keyword argument, pass shape=self.shape to it from DensityDist.random(), and trust users to Do The Right Thing. Expand the docstring.

  2. Disable posterior predictive sampling for DensityDist. This seems undesirable.

Versions and main components

  • PyMC3 Version: master as of today
  • Other components: irrelevant

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions