What method is required to implement custom distribution in pymc?

To implement custom distribution in pymc, pm.CustomDist can be used by only supplying logp, but doing so won’t allow for sample_posterior_predictive. So it might be easier to implement the distribution properly

  • So far, every distribution come with RandomVariable sub-class. I get that this define random sampling, which can be mapped to scipy.stats implementation of rvs.
  • This random variable is then used in the main class (sub-class of Continuous, PositiveContinuous, …) via static variable named rv_op. I guess this variable will do the magic behind the scene

There are 4 recurring methods I saw in the code

  • dist classmethod is also simple to implement, but I don’t know the importance of this function
  • moment method - I guess this is where the sampler is going to start sampling? Most distribution implement this using distribution mean, some mode if mean doesn’t exists. And HalfStudentT just use sigma out right. But I am not sure if this method is required
  • logp - Define logp, This is required in CustomDist, so I also think it is required here
  • logcdf - Most distribution have this method implement, but distribution like TruncatedNormal didn’t have logcdf. So this is not a requirement?

So I want to make sure what method is required, and what method is used to speed-up (and speed up what exactly). Thanks

CustomDist allows you to specify a python random function to be used in prior and posterior predictive. There are some examples in the documentation.

Alternatively, if your distribution is simple enough and can be built from other PyMC distributions and PyTensor operations you can define a dist function instead. If you need Normal draws you would use pm.Normal.dist(...) inside that function. PyMC will then use the output of this function to take random draws in prior and posterior predictive.

As an extra, sometimes PyMC will even be able to guess the right logp for a given dist function, and you won’t have to specify it separately.


If you still want to implement a “conventional” distribution, there’s a developer guide here: Implementing a RandomVariable Distribution — PyMC 5.3.1 documentation

This is usually not needed.