Compute P(X=k) for a Poisson(λ) distribution: the probability of exactly k events in a fixed interval given mean rate λ. Formula: (λ^k × e^−λ) / k!. Three named pieces: lam_k = λ^k, e_neg_lam = math.exp(−λ), k_fact = math.factorial(k). Assemble as numerator / k_fact. Library: scipy.stats.poisson.pmf(k, lam).

By hand

With the library

scipy.stats.poisson.pmf(k, mu) takes k first, then the rate parameter (named mu in scipy). Applies the same formula with full floating-point precision.

naive.py
import math
lam = 3
k = 2
lam_k = lam ** k
e_neg_lam = math.exp(-lam)
k_fact = math.factorial(k)
numerator = lam_k * e_neg_lam
prob = numerator / k_fact
print('RESULT:', round(prob, 6))
library.py
from scipy.stats import poisson
from dalib.display import set_display
set_display()

lam = 3
k = 2
prob = poisson.pmf(k, lam)
print('lambda:', lam, ' k:', k)
print('RESULT:', round(prob, 6))
lambda: 3  k: 2
RESULT: 0.224042

Implementation notes

  • The Poisson distribution models counts of rare, independent events in a fixed interval (e.g. 2 phone calls in a minute given mean rate 3/min). λ is both the mean and the variance of the distribution.
  • Poisson as the limit of Binomial: Binomial(n, λ/n) → Poisson(λ) as n→∞. For large n and small p with fixed λ=np, the Poisson approximation is computationally cheaper.
  • math.exp(-lam) and math.factorial(k) are exact-enough for small k and λ. For very large k, use scipy.stats.poisson.pmf to avoid overflow.
  • Cross-reference: binomial-probability (this chapter) for the discrete distribution this approximates in the large-n limit.