The Poisson distribution
Count events over time or space. Learn why the variance equals the mean, what exposure does, and what overdispersion means for your tests.
What runs in CodeSplash
- STAT074One sample exact Poisson test Runs in CodeSplash
- Poisson rates (one rate or two rates compared) Verified in Python and R
You will be able to
- Recognize a count that may follow a Poisson distribution
- Compute Poisson probabilities and compare a rate with a reference
- Diagnose overdispersion and say why it matters
Read first
Counts with no upper limit
ReadThe Poisson distributionPoisson distributionThe count of independent events at a steady rate, with a variance equal to its mean. counts events that occur independently at a steady rate : calls per hour, defects per meter, infections per thousand patient-days.
One number sets both the mean and the variance. That's a strong claim, and you can check it.
Is 12 surprising?
PredictPredict first
The usual rate is 8 events per 100 units of exposure. Over 100 units you see 12. How surprised should you be?
Reveal
Mildly unusual, at most. The standard deviation of a Poisson count with mean 8 is , so 12 is about one and a half standard deviations up. The exact test gives p = .154, with a 95% interval for the rate from 6.2 to 21.0.
See the shape
ExploreChange with Parameter 1. When is small the bars pile up near zero. When it's large they start to look like a bell.
AExplorable · The distribution zoo
What do the common distributions look like, and when is a normal approximation good enough?
Which is larger for this distribution: the mean or the median?
Family
μ, df, shape, α, meanlog, min, n or λ depending on the family.
σ, rate, scale, β, sdlog or width; for the binomial p = b/(b + 1).
Solid: the distribution. Dashed: a normal with the same mean and SD.
Commit your prediction above to see the result
Make your prediction to see the result.
Why exposure matters
ReadTwelve events over 100 patient-days and over 1,000 tell different stories. So the rate is events divided by exposure, and the expected count is rate times exposure.
Exposure is why the rates methods in Analyze ask for two columns. Comparing raw counts across units of different size can make a larger unit look worse.
Real counts vary more
ExploreReal counts usually vary more than the mean: patients and wards differ, and events cluster. That's overdispersionOverdispersionCounts that vary more than a Poisson model allows; standard errors come out too small.. Guess how often the plain Poisson comparison rejects when nothing is going on, then lower Negative binomial size.
DExplorable · Overdispersed counts
What happens to a Poisson test when counts vary more than the Poisson allows?
With no real difference, how often does the plain Poisson comparison reject?
Commit your prediction above to see the result
Make your prediction to see the result.
Check your reading
CheckpointCheck yourself
With a rate of , what is the probability of zero events? Give three decimals.
Show the answer
Answer: 0.135
.
Check yourself
12 events occurred over 100 units of exposure. What is the rate per 100 units?
Show the answer
Answer: 12
per 100 units.
Check yourself
Weekly incident counts have a mean of 4 and a variance of 15. What should you do?
Show the answer
Answer: b
A variance far above the mean means Poisson standard errors are too small, and its p-values too small with them. Removing data to fit a model isn't a remedy.
Test a rate in the app
In the appSet Events and Exposure, and add a Group to compare two rates. Use Rates per … units of exposure so the numbers read naturally.
rate = 12.00 per 100.00, 95% CI [6.20, 20.96], p = .154
- p (exact Poisson test)
- .154
Effect size
- Rate per 100.00 12 95% CI [6.201, 20.962]
Plots
Exact (Garwood) intervals.
View data
| Rate per 100.00 | high | group | low |
|---|---|---|---|
| 12 | 20.962 | all rows | 6.201 |
Assumptions
- Events occur independently at a constant rate — Exact Poisson tests assume no clustering; overdispersed counts make them anti-conservative (consider count regression).
- Valid counts and exposure — Events are whole numbers ≥ 0 and exposure is > 0 on every row.
Rates per 100.00 units of exposure
| Group | Events | Exposure | Rate | Lower (exact) | Upper (exact) |
|---|---|---|---|---|---|
| all rows | 12 | 100 | 12 | 6.201 | 20.962 |
The exact R script · R R version 4.5.1 (2025-06-13)
# Poisson rates (one rate or two rates compared)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method rates.poisson 1.2.1 · spec 94146b735b2851b7
# Dataset: e_guide_rate (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_cnt_etest (port of scipy.stats.poisson_means_test), cs_cnt_garwood, cs_lens
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/e_guide_rate.parquet", col_select = c("events", "exposure")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("events", "exposure")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("events", "exposure")) # infinite values stop the run (registry/errors.yaml)
# %% Data checks
ev <- "events"
ex <- "exposure"
events <- as.numeric(df[[ev]])
exposure <- as.numeric(df[[ex]])
bad_counts <- sum(events < 0 | events != round(events))
if (bad_counts > 0) cs_error("CS_E_INVALID_VALUES", column = ev, requirement = "whole numbers ≥ 0 (event counts)", count = bad_counts)
if (any(exposure <= 0)) cs_error("CS_E_INVALID_VALUES", column = ex, requirement = "above 0 on every row (time at risk or size)", count = sum(exposure <= 0))
n <- nrow(df)
per <- 100 # display scale: rates per this many units of exposure
alternative <- "two-sided"
conf <- 0.95
z <- stats::qnorm(1 - (1 - conf) / 2)
X <- sum(events)
T <- sum(exposure)
values <- list(n = n, events_total = X, exposure_total = T, per = per)
notes <- list()
# %% One rate: exact Poisson test against the reference rate
rate0 <- 8 # on the display scale
pt <- stats::poisson.test(X, T, r = rate0 / per, alternative = sub("-", ".", alternative, fixed = TRUE), conf.level = conf)
p <- pt$p.value
est <- unname(pt$estimate) * per
lo <- pt$conf.int[1] * per
hi <- pt$conf.int[2] * per
rows <- list(list(group = "all rows", events = X, exposure = T, rate = est, low = lo, high = hi))
values <- c(values, list(mode = "one-sample", p = p, estimate = est, ci_low = lo, ci_high = hi))
pct <- sprintf("%.0f%%", conf * 100)
summary <- sprintf("%d events over %s units of exposure: %s per %s, %s CI [%s, %s]; exact Poisson test against %s, %s.",
as.integer(X), cs_fmt_num(T), cs_fmt_num(est), cs_fmt_num(per), pct, cs_fmt_num(lo), cs_fmt_num(hi), cs_fmt_num(rate0), cs_fmt_p(p))
apa <- sprintf("rate = %s per %s, %s CI [%s, %s], %s", cs_fmt_num(est), cs_fmt_num(per), pct, cs_fmt_num(lo), cs_fmt_num(hi), cs_fmt_p(p))
statistics <- list(list(key = "p", label = "p (exact Poisson test)", value = p, format = "p"))
if ("none" != "none") warnings <- c(warnings, "The equivalence / non-inferiority lens compares two rates; add a group column to use it.")
effects <- list(list(key = "estimate", label = sprintf("Rate per %s", cs_fmt_num(per)), value = est, ciLow = lo, ciHigh = hi, ciLevel = conf, method = "exact (Garwood)"))
# %% Assumptions
assumptions <- list(
list(key = "poisson", label = "Events occur independently at a constant rate", status = "info",
detail = "Exact Poisson tests assume no clustering; overdispersed counts make them anti-conservative (consider count regression)."),
list(key = "counts", label = "Valid counts and exposure", status = "pass", detail = "Events are whole numbers ≥ 0 and exposure is > 0 on every row.")
)
# %% Result
cs_publish(list(
method = "rates.poisson",
methodVersion = "1.2.1",
title = "Poisson rates",
engine = cs_engine_info("stats", "nanoparquet"),
n = n,
summary = summary,
apa = apa,
statistics = statistics,
tables = list(list(
key = "rates", title = sprintf("Rates per %s units of exposure", cs_fmt_num(per)),
columns = list(list(key = "group", label = "Group", type = "string"), list(key = "events", label = "Events", type = "integer"),
list(key = "exposure", label = "Exposure", type = "number"), list(key = "rate", label = "Rate", type = "number"),
list(key = "low", label = "Lower (exact)", type = "number"), list(key = "high", label = "Upper (exact)", type = "number")),
rows = rows
)),
effectSizes = effects,
assumptions = assumptions,
warnings = warnings,
notes = notes,
charts = list(cs_chart(
"rates", "forest", sprintf("Rate per %s", cs_fmt_num(per)),
list(label = cs_col("group", vapply(rows, function(r) r$group, "")), estimate = cs_col(sprintf("Rate per %s", cs_fmt_num(per)), vapply(rows, function(r) r$rate, 0)),
low = cs_col("low", vapply(rows, function(r) r$low, 0)), high = cs_col("high", vapply(rows, function(r) r$high, 0))),
note = "Exact (Garwood) intervals."
)),
values = values
))
rate = 12.00 per 100.00, 95% CI [6.20, 20.96], p = .154
- p (exact Poisson test)
- .154
Effect size
- Rate per 100.00 12 95% CI [6.201, 20.962]
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Events occur independently at a constant rate — Exact Poisson tests assume no clustering; overdispersed counts make them anti-conservative (consider count regression).
- Valid counts and exposure — Events are whole numbers ≥ 0 and exposure is > 0 on every row.
Rates per 100.00 units of exposure
| Group | Events | Exposure | Rate | Lower (exact) | Upper (exact) |
|---|---|---|---|---|---|
| all rows | 12 | 100 | 12 | 6.201 | 20.962 |
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Poisson rates (one rate or two rates compared)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method rates.poisson 1.2.1 · spec 94146b735b2851b7
# Dataset: e_guide_rate (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
import numpy as np
import pandas as pd
from scipy import stats
import codesplash as cs
from codesplash.stats import counts as csc # exact Poisson / conditional tests as R's poisson.test (twins of codesplashr)
from codesplash.stats import means as csm # equivalence / non-inferiority lens (twin of cs_lens)
# %% Data
df = pd.read_parquet("tests/parity/fixtures/e_guide_rate.parquet", columns=["events", "exposure"])
n_input = len(df)
df = df.dropna(subset=["events", "exposure"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["events", "exposure"]) # infinite values stop the run (registry/errors.yaml)
# %% Data checks
ev, ex = "events", "exposure"
events = df[ev].to_numpy(dtype=float)
exposure = df[ex].to_numpy(dtype=float)
bad_counts = int(np.sum((events < 0) | (events != np.round(events))))
if bad_counts:
cs.cs_error("CS_E_INVALID_VALUES", column=ev, requirement="whole numbers ≥ 0 (event counts)", count=bad_counts)
if np.any(exposure <= 0):
cs.cs_error("CS_E_INVALID_VALUES", column=ex, requirement="above 0 on every row (time at risk or size)", count=int(np.sum(exposure <= 0)))
n = len(df)
per = 100 # display scale: rates per this many units of exposure
alternative = "two-sided"
conf = 0.95
z = stats.norm.ppf(1 - (1 - conf) / 2)
X, T = int(events.sum()), float(exposure.sum())
values = {"n": n, "events_total": X, "exposure_total": T, "per": per}
notes = []
# %% One rate: exact Poisson test against the reference rate (R's poisson.test(x, T, r))
rate0 = 8 # on the display scale
pt = csc.poisson_test(X, T, rate0 / per, alternative, conf)
p = pt["p"]
est, lo, hi = pt["estimate"] * per, pt["ci_low"] * per, pt["ci_high"] * per
rows = [{"group": "all rows", "events": X, "exposure": T, "rate": est, "low": lo, "high": hi}]
values.update({"mode": "one-sample", "p": p, "estimate": est, "ci_low": lo, "ci_high": hi})
summary = f"{X} events over {cs.fmt_num(T)} units of exposure: {cs.fmt_num(est)} per {cs.fmt_num(per)}, {conf:.0%} CI [{cs.fmt_num(lo)}, {cs.fmt_num(hi)}]; exact Poisson test against {cs.fmt_num(rate0)}, {cs.fmt_p(p)}."
apa = f"rate = {cs.fmt_num(est)} per {cs.fmt_num(per)}, {conf:.0%} CI [{cs.fmt_num(lo)}, {cs.fmt_num(hi)}], {cs.fmt_p(p)}"
statistics = [{"key": "p", "label": "p (exact Poisson test)", "value": p, "format": "p"}]
if "none" != "none":
warnings.append("The equivalence / non-inferiority lens compares two rates; add a group column to use it.")
effects = [{"key": "estimate", "label": f"Rate per {cs.fmt_num(per)}", "value": est, "ciLow": lo, "ciHigh": hi, "ciLevel": conf, "method": "exact (Garwood)"}]
# %% Assumptions
assumptions = [
{"key": "poisson", "label": "Events occur independently at a constant rate", "status": "info",
"detail": "Exact Poisson tests assume no clustering; overdispersed counts make them anti-conservative (consider count regression)."},
{"key": "counts", "label": "Valid counts and exposure", "status": "pass", "detail": "Events are whole numbers ≥ 0 and exposure is > 0 on every row."},
]
# %% Result
cs.publish({
"method": "rates.poisson",
"methodVersion": "1.2.1",
"title": "Poisson rates",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n,
"summary": summary,
"apa": apa,
"statistics": statistics,
"tables": [{
"key": "rates", "title": f"Rates per {cs.fmt_num(per)} units of exposure",
"columns": [{"key": "group", "label": "Group", "type": "string"}, {"key": "events", "label": "Events", "type": "integer"},
{"key": "exposure", "label": "Exposure", "type": "number"}, {"key": "rate", "label": "Rate", "type": "number"},
{"key": "low", "label": "Lower (exact)", "type": "number"}, {"key": "high", "label": "Upper (exact)", "type": "number"}],
"rows": rows,
}],
"effectSizes": effects,
"assumptions": assumptions,
"warnings": warnings,
"notes": notes,
"charts": [cs.chart(
"rates", "forest", f"Rate per {cs.fmt_num(per)}",
{"label": cs.col("group", [r["group"] for r in rows]), "estimate": cs.col(f"Rate per {cs.fmt_num(per)}", [r["rate"] for r in rows]),
"low": cs.col("low", [r["low"] for r in rows]), "high": cs.col("high", [r["high"] for r in rows])},
note="Exact (Garwood) intervals.",
)],
"values": values,
})
from scipy import stats
print(stats.poisson.pmf(0, mu=2))
print(stats.poisson.sf(11, mu=8)) # P(12 or more events)dpois(0, lambda = 2)
poisson.test(12, T = 100, r = 8 / 100)Recap
Recap- A Poisson count has one parameter: its mean and variance are both .
- Compare rates, counts per unit of exposure, rather than raw counts.
- Compare the variance with the mean. If it's much larger, plain Poisson tests reject too often.
Concepts in this lesson