distrax
distrax copied to clipboard
feat(distributions): add Poisson distribution
What does this PR do?
Adds distrax.Poisson(rate) — the Poisson distribution with rate parameter λ > 0, implementing the interface requested in #164.
The Poisson distribution models the number of discrete events occurring at a constant rate. Its PMF is:
P(X = k) = λ^k * exp(-λ) / k!, k = 0, 1, 2, …
Implemented methods
| Method | Notes |
|---|---|
log_prob(k) |
k·log(λ) − λ − log Γ(k+1) |
mean() |
λ |
variance() |
λ |
mode() |
floor(λ) |
entropy() |
Numerical sum (no closed form) |
kl_divergence(other) |
Analytic when other is also Poisson: λ₁ log(λ₁/λ₂) + λ₂ − λ₁ |
sample() |
Delegates to jax.random.poisson |
Supports integer and floating-point dtype for samples, batched rate, and JIT compilation.
Tests
31 tests in poisson_test.py covering:
log_probaccuracy vsscipy.stats.poisson.logpmffor k = 0, 1, 3, 10- Mean, variance, mode correctness
- Entropy vs scipy
- KL divergence (analytic formula and self-divergence = 0)
- Sample shape, non-negativity, moments (large-sample)
- Integer and float dtype samples
- JIT compatibility
Notes
entropy()is computed via a numerical sum (TFP's JAX substrate does not implementPoisson.entropy()). The window is chosen asmin(λ + 10√λ + 30, 2000), which captures >99.999 % of probability mass for rates up to ~1000.equiv_tfp_cls = tfd.Poissonis set for TFP interoperability.
Fixes #164
Friendly bump on this new Poisson distribution PR, open about a month with no review yet. Happy to adjust anything needed. Thanks!