Chains and beliefs

Bayesian updating

Bayes' rule turns a prior belief and data into a posterior belief. For the conjugate pairs the posterior stays in the prior's family and its parameters are sums of the data, so updating is exact arithmetic. Means, variances and predictive probabilities are fractions; credible intervals are certified quantiles.

A probability: beta and binomial

import { betaPrior } from "@ratmath/stats";

const prior = betaPrior(1, 1);                       // uniform on [0, 1]
const post = prior.update({ successes: 7, trials: 10 });
String(post);           // "Beta(8, 4): mean 0.666667"
post.mean;              // 2/3
post.mode;              // 7/10, the maximum-likelihood estimate
post.interval("0.95");  // certified enclosures of the 2.5% and 97.5% quantiles
post.predictive(3).table;   // [5/91, 20/91, 36/91, 30/91]: successes in the next 3 trials

With prior density proportional to pa−1(1−p)b−1p^{a-1}(1-p)^{b-1} and kk successes in nn trials, the posterior is proportional to

pa−1(1−p)b−1⋅pk(1−p)n−k=pa+k−1(1−p)b+n−k−1, p^{a-1}(1-p)^{b-1}\cdot p^{k}(1-p)^{n-k} = p^{a+k-1}(1-p)^{b+n-k-1},

which is Beta(a+k, b+n−k)\mathrm{Beta}(a + k,\, b + n - k). Updating twice is updating once with the pooled data, and the order does not matter: the test suite checks both. The predictive law of the next mm trials is beta-binomial, P(K=j)=(mj)(a)j(b)m−j(a+b)mP(K = j) = \binom mj \frac{(a)_j (b)_{m-j}}{(a+b)_m}, with rising factorials, exact.

A rate: gamma with Poisson or exponential data

import { gammaPrior } from "@ratmath/stats";
const calls = gammaPrior(2, 1).update([3, 4, 2, 5]);   // calls per hour in four hours
String(calls);                  // "Gamma(shape 16, rate 5): mean 3.200000"
calls.predictive().pmf(3);      // the chance of exactly 3 calls next hour, exact
const life = gammaPrior(1, 1, { likelihood: "exponential" }).update([0.5, 1.5]);
life.params;                    // { shape: 3, rate: 3 }

For Poisson counts x1,…,xnx_1, \dots, x_n the posterior of a Gamma(α,β)\mathrm{Gamma}(\alpha, \beta) prior is Gamma(α+∑xi, β+n)\mathrm{Gamma}(\alpha + \sum x_i,\, \beta + n); for exponential waiting times it is Gamma(α+n, β+∑xi)\mathrm{Gamma}(\alpha + n,\, \beta + \sum x_i). The predictive law of the next Poisson count is negative binomial.

A mean: normal with known variance

import { normalPrior } from "@ratmath/stats";
const m = normalPrior(0, { variance: 4, dataVariance: 1 }).update([1.2, 0.8, 1.0, 1.4]);
m.variance;   // 4/17: precisions add, 1/4 + 4/1 = 17/4
m.mean;       // 88/85: the precision-weighted average of 0 and the sample mean 11/10
1τ12=1τ02+nσ2,μ1=τ12(μ0τ02+∑xiσ2). \frac{1}{\tau_1^2} = \frac{1}{\tau_0^2} + \frac{n}{\sigma^2}, \qquad \mu_1 = \tau_1^2\Bigl(\frac{\mu_0}{\tau_0^2} + \frac{\sum x_i}{\sigma^2}\Bigr).

The more data, the more the posterior mean follows the sample and the less the prior.

Several categories: Dirichlet and multinomial

import { dirichletPrior } from "@ratmath/stats";
const d = dirichletPrior({ red: 1, green: 1, blue: 1 }).update({ red: 5, green: 2 });
d.mean;               // { red: 3/5, green: 3/10, blue: 1/10 }
d.marginal("red");    // Beta(6, 4): the belief about red alone

Each category's weight grows by its count; the marginal belief about one category's probability is a beta distribution, so its credible interval comes from marginal(category).interval().

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs