Skip to content

Conversation

@maresb
Copy link
Collaborator

@maresb maresb commented Feb 4, 2026

Note

AI Assistance Disclosure: This PR was developed with significant assistance from an AI coding assistant (Claude) via Cursor. The code has not yet been fully reviewed by the author.

Summary

This PR adds two new probability distributions to PyMC-Extras:

  1. Generalized Pareto Distribution (GPD) - A fundamental distribution in extreme value theory for modeling exceedances over a threshold
  2. Extended Generalized Pareto Distribution (ExtGPD) - A transformation of GPD that provides smoother behavior in the lower tail, useful for modeling entire distributions without explicit threshold selection

Features

  • Full implementation with logp, logcdf, icdf, and support_point methods
  • Random sampling via scipy's genpareto
  • Comprehensive test coverage using PyMC's testing utilities
  • The icdf method enables efficient inverse CDF sampling when used with pm.Truncated, avoiding rejection sampling failures

Mathematical Background

The ExtGPD CDF is defined as:
$$G(x \mid \mu, \sigma, \xi, \kappa) = \left[H\left(\frac{x - \mu}{\sigma}\right)\right]^\kappa$$

where $H$ is the standard GPD CDF. The parameter $\kappa > 0$ controls lower tail behavior, and when $\kappa = 1$, ExtGPD reduces to standard GPD.

Test plan

  • pytest tests/distributions/test_continuous.py::TestGenParetoClass - GPD tests
  • pytest tests/distributions/test_continuous.py::TestExtGenParetoClass - ExtGPD tests
  • pytest tests/distributions/test_continuous.py::TestGenPareto - GPD random sampling
  • pytest tests/distributions/test_continuous.py::TestExtGenPareto - ExtGPD random sampling

- Add GenPareto distribution for peaks-over-threshold extreme value modeling
- Add ExtGenPareto distribution following Naveau et al. (2016) for modeling
  entire distributions without threshold selection
- ExtGPD uses CDF G(x) = H(x)^kappa where H is the GPD CDF and kappa > 0
  controls lower tail behavior (reduces to GPD when kappa = 1)
- Include comprehensive tests for logp, logcdf, support_point, and random sampling
This enables PyMC's Truncated distribution to use inverse CDF sampling
instead of rejection sampling, which is critical when the truncation
probability is high (e.g., when kappa is small and most mass is below
the truncation threshold).

The inverse CDF for ExtGPD is: G^{-1}(p) = H^{-1}(p^{1/kappa})
where H^{-1} is the GPD quantile function.
from scipy import stats


class GenParetoRV(RandomVariable):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How tricky is the rvs method to implement symbolically? If scipy is just doing inverse cdf sampling we could also do it.

In that case you would implement the RV as a SymbolicRV. The advantage of that is that the random methods work out of the box on the different backends.

Same for the other distribution

maresb and others added 2 commits February 5, 2026 14:07
…iable

Switch from scipy-based RandomVariable to symbolic inverse CDF sampling.
This enables backend-agnostic random sampling (JAX, NumPy, etc.) by
implementing the inverse CDF transformation directly in PyTensor.

The inverse CDF for GPD is:
- mu + sigma * [(1-u)^(-xi) - 1] / xi  for xi != 0
- mu - sigma * log(1-u)                 for xi = 0

For ExtGPD, we use the transformation u^{1/kappa} before applying the GPD
inverse CDF.

Co-authored-by: Claude AI
Co-authored-by: Cursor <cursoragent@cursor.com>
- Add _exprel helper for (exp(t)-1)/t with correct gradient at t=0
- Use survival probability directly to avoid 1-u cancellation in upper tail
- For ExtGPD, compute GPD survival probability via -expm1(log(u)/kappa)

Co-authored-by: Cursor <cursoragent@cursor.com>
@maresb
Copy link
Collaborator Author

maresb commented Feb 5, 2026

Thanks for the tip @ricardoV94!!!

@aloctavodia
Copy link
Member

Should we try to implement these distributions here https://github.com/pymc-devs/distributions?

@maresb
Copy link
Collaborator Author

maresb commented Feb 5, 2026

Oh cool, thanks @aloctavodia! I wasn't aware of that.

I'd be happy to move it over, but it may be a while before I can get to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants