Prior changed: press Reset to apply.
(Disclaimer: no rocket science here.)
Some time ago I had a bit too much time on my hands and was doodling in a notebook: how do Bayesian beliefs about a coin change as it is flipped? In particular for a coin that looks two-headed but isn't: a run of heads, followed by ordinary random flips. Ten heads in a row happens in roughly 1 in 1,000 attempts; twenty in a row will be seen by about one in a million people who try. Rare, but there will always be lottery winners.
An unknown coin, so why not the Jeffreys prior, Beta(0.5, 0.5)? After the run of heads the posterior is a broad distribution skewed towards heads, and it stays oddly biased through the random flips that follow. Nobody actually believes that about a coin they found on the road.
Surely the coin is fair unless the evidence is overwhelming, so try Beta(10⁶, 10⁶): a million pseudo-counts each way, to say a biased coin is less likely than one in a million. Still odd:
Shocking at first, until one remembers that we mostly choose them for convenience, not because they describe what we believe.
That prior is simply a mixture: conjugate Beta priors averaged with delta priors, where a delta prior is just the limit of a Beta prior with infinitely many pseudo-counts. Each sub-prior updates as usual and the mixture weights follow from Bayes' rule; the maths falls out in a few lines (see the Theory tab). Why does nobody tell one this? Perhaps because Bayesian coins are “too simple and obvious”, so the topic is always skipped, and then forgotten.
A nice bonus: priors that look absurd on their own, such as a fixed belief in p = 1, work perfectly inside a mixture. If I see tails, that prior cannot update, but its weight drops to zero and rules it out.
One should never think something is too simple to deserve a second look. It doesn't always have to be rocket science.
The default settings are this prior: fair coin 10⁶, always heads and always tails 1 each, unknown bias 0.001. Run 20 steps at true probability 1 (all heads; 2²⁰ ≈ 10⁶, the same order as the fair weight) and watch doubt set in. Then run 100 steps at 0.5: the first tail rules out the two-headed coin and “fair” recovers.
The framework implements a composable prior hierarchy where any prior can contain sub-priors, which can themselves be mixtures. This recursive structure is not limited to the binomial/coin-flip case—it generalises to any likelihood function with compatible priors.
CompoundPrior (mixture) ├── DeltaPrior (p=0) # Always tails ├── DeltaPrior (p=1) # Always heads ├── DeltaPrior (p=0.5) # Fair coin ├── BetaPrior (α, β) # Unknown bias └── CompoundPrior (nested) # Sub-mixtures allowed!
Impossible priors are acceptable: when a hypothesis becomes impossible (e.g. p=0 after seeing heads), the mixture update simply sets its weight to zero, excluding it from further inference. A sub-prior given zero prior weight is excluded from the start in the same way.
Conjugate update (after observing k heads, m tails):
α′ = α₀ + k, β′ = β₀ + m
Mean: E[p] = α / (α + β)
Variance: Var(p) = αβ / [(α+β)²(α+β+1)]
Log-normalisation: ln P(D|Beta) = ln B(α′, β′) − ln B(α₀, β₀)
| Prior name | Parameters | Interpretation |
|---|---|---|
| Jeffreys | α=0.5, β=0.5 | Non-informative (default) |
| Uniform | α=1, β=1 | All biases equally likely |
| Haldane | α→0, β→0 | Improper, data-dominated |
Models hypotheses in which the coin’s bias is deemed to be exactly p₀.
Key insight: a Delta prior is a fixed belief: p₀ is held to be exactly the coin's probability, whatever the evidence. Unlike a Beta prior, its parameters never update. On its own that looks absurd for a Bayesian—a belief that no evidence can move. For 0 < p₀ < 1 every sequence of flips is compatible with it, so a lone Delta prior simply never changes. (Only p₀ = 0 or 1 can be refuted outright, by a single contrary flip.)
Inside a mixture it makes perfect sense. The evidence moves the Delta prior's weight instead, through its likelihood (lognorm): up when the data fit p₀ better than the alternatives, down when they fit worse, and to exactly zero when p₀ = 0 or 1 is contradicted. This holds wherever the alternatives sit in the hierarchy. So fixed-belief hypotheses such as “this coin is fair” or “this coin is two-headed” are legitimate priors after all, and not to be dismissed as absurd: they just have to compete with alternatives.
Log-likelihood: ln P(D|p₀) = k·ln(p₀) + m·ln(1−p₀)
| Hypothesis | p₀ | Becomes impossible when… |
|---|---|---|
| Always tails | 0 | Any heads observed |
| Always heads | 1 | Any tails observed |
| Any other fixed bias, including fair (0.5) | 0 < p₀ < 1 | Never |
Prior: P(p) = Σᵢ wᵢ Pᵢ(p)
Weight update (Bayes' rule):
wᵢ′ = P(D|Mᵢ) · wᵢ⁽⁰⁾ / Σⱼ P(D|Mⱼ) · wⱼ⁽⁰⁾
In log-space: ln wᵢ′ = ln P(D|Mᵢ) + ln wᵢ⁽⁰⁾ − logsumexp(…)
How different prior types update:
| Prior type | On evidence |
|---|---|
| BetaPrior | Parameters update (α += heads, β += tails). Distribution sharpens. |
| DeltaPrior | Parameters fixed. Only lognorm changes → affects mixture weight. |
| CompoundPrior | Recursively updates sub-priors, recomputes weights. |
The essence of Bayesian model comparison: the Delta prior hypothesis doesn't “learn”—it either gets supported or ruled out by evidence. Beta priors adapt within a model; Delta priors compete between fixed models.
Mixture mean: E[p] = Σᵢ wᵢ μᵢ
Mixture variance (law of total variance):
Var(p) = Σᵢ wᵢ σᵢ² + [Σᵢ wᵢ μᵢ² − (Σᵢ wᵢ μᵢ)²]
Sub-priors can themselves be CompoundPriors:
Coin (CompoundPrior) ├── One-sided (CompoundPrior) │ ├── Always tails (DeltaPrior p=0) │ └── Always heads (DeltaPrior p=1) ├── Fair (DeltaPrior p=0.5) └── Unknown (BetaPrior α=0.5, β=0.5)
Generality: replace BetaPrior with any conjugate prior (Normal-Normal, Gamma-Poisson, etc.) and the CompoundPrior mechanics remain identical.
From Beta: p ~ Beta(α, β)
From mixture: