When samples are drawn with non-uniform importance weights, the
effective sample size (ESS) summarizes how many equally-weighted draws
they are statistically worth. ESS bottoms out at 1 when all the mass
sits on a single sample and tops out at N when the weights are
uniform.
For weights w_1, …, w_N::
ESS = (Σ w_i)² / Σ w_i²
The implementation works in log space so it stays stable for weights that span many orders of magnitude.
| Symbol | Signature | Notes |
|---|---|---|
effective_sample_size |
(log_weights) -> float |
Input is 1-D log-weights; output is a scalar in [1, N]. |
logsumexp |
(a, axis=None) -> array |
Numerical primitive used internally; safely handles all-(-inf) slices. |
Both live in active_inference.core.diagnostics.
tests/core/test_diagnostics.py::TestESS covers:
ESS = N.ESS = 1.ESS = 2.ValueError.tests/core/test_diagnostics.py::TestLogSumExp separately covers
overflow safety, axis handling, and the -∞ boundary case.
import numpy as np
from active_inference import effective_sample_size, logsumexp
uniform = effective_sample_size(np.zeros(100)) # → 100.0
peaked = effective_sample_size(np.array([0.0] + [-1e6] * 99)) # → 1.0
# logsumexp avoids the obvious overflow trap.
print(logsumexp(np.array([1000.0, 1000.0, 1000.0]))) # → 1000 + log 3
ESS is computed from log-weights, not raw weights. Take the
log first, or pass np.log(...) if you have weights already.logsumexp is also exported at the top-level package because it
shows up everywhere; prefer it over np.log(np.sum(np.exp(...))).../topics/bayesian_inference.md
— log-space normalization of grid posteriors uses the same trick.../reference/core.md — full API.