Skip to content

Bring back distribution moments #5078

Closed
@ricardoV94

Description

@ricardoV94

With #4983 and #5087 finished, it's a good time to bring back moments for our distributions for more stable starting points.

With this we can then do the switch in #5009

We should also update the distribution developer guide to mention the implementation of moments: https://github.com/pymc-devs/pymc/blob/main/docs/source/developer_guide_implementing_distribution.md


How to help?

  1. This PR should give a template on how to implement and test new moments for distributions: https://github.com/pymc-devs/pymc/pull/5087/files

  2. In most cases we should be able to copy the moments we were using in the V3 branch. For example here is what we were doing for the Beta

    self.mean = self.alpha / (self.alpha + self.beta)

    2.1 We used to have multiple moments for some distributions such as mean, median, mode. We only support one moment now, and probably the "higher-order" one is the most useful (that is mean > median > mode)... You might need to truncate the moment if you are dealing with a discrete distribution.
    2.2 We left some of these moments commented out inside the distribution dist classmethod. Make sure they are removed when you implement them!
    # mean = median = mode = mu = at.as_tensor_variable(floatX(mu))
    # variance = 1.0 / self.tau

  3. We have to be careful with size != None and broadcasting properly when when some parameters that are not used in the moment may nevertheless inform about the shape of the distribution. E.g. pm.Normal.dist(mu=0, sigma=np.arange(1, 6)) returns a moment of [mu, mu, mu, mu, mu]. Again Add tests for distributions moments #5087 should give some template to think about this

    def get_moment(rv, size, mu, sigma):
    mu, _ = at.broadcast_arrays(mu, sigma)
    if not rv_size_is_none(size):
    mu = at.full(size, mu)
    return mu

    3.1 In the case where you have to manually broadcast the parameters with each other it's important to add test conditions that would fail if you were not to do that. A straightforward way to do this is to make the used parameter a scalar, the unused one(s) a vector (one at a time) and size None

  4. Just to keep things uniformish, please add the get_moment immediately below the dist classmethod and before logp or logcdf methods.

  5. New tests have to be added in test_distributions_moments.py. Make sure to test different combinations of size and broadcasting to cover the cases mentioned in point 3.

  6. Don't hesitate to ask any questions. You can grab as many distributions to implement moments as you want. Just make sure to write in this issue so that we can keep track of it.

  7. Profit with your new open source KARMA!

The following distributions don't have a moment method implemented:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions