Moment matching for a beta prior (source code)

= Moment matching for a beta prior
{title2=$\alpha=m\kappa,\quad\beta=(1-m)\kappa$}

<Moment matching for a beta prior> turns a desired <mean> and <variance> for an unknown <probability> into the parameters of a <beta distribution> <prior>. Choose $0<m<1$ and
$$
0<v<m(1-m).
$$
Solving the <beta distribution> <mean> and <variance> identities gives
$$
\kappa=\frac{m(1-m)}v-1,\qquad\alpha=m\kappa,\qquad\beta=(1-m)\kappa.
$$
The strict upper bound on the <variance> makes $\kappa>0$, so both parameters are positive. Substitution gives <mean> $\mathbb E[P]=m$ and <variance> $\operatorname{Var}(P)=v$ exactly. A smaller specified <variance> at fixed <mean> produces a more concentrated <prior distribution>.

For example, a desired <prior> <mean> $m=0.3$ and <variance> $v=0.01$ give $\kappa=0.21/0.01-1=20$, hence the <beta distribution> <prior> $\operatorname{Beta}(6,14)$. With the same <mean> and <variance> $v=0.035$, the calculation instead gives $\kappa=5$ and the broader <beta distribution> <prior> $\operatorname{Beta}(1.5,3.5)$. Both appear in the concentration diagram above.

The excluded boundary cases have clear meanings. Zero <variance> would require $P=m$ with certainty, not a <beta distribution> with finite positive parameters. At the other boundary, any <random variable> $P\in[0,1]$ satisfies $P^2\leq P$, hence <variance> at most $m(1-m)$; equality requires $P$ to take only the endpoint values zero and one. This is a <Bernoulli distribution>, not a <beta distribution>. Thus $v\geq m(1-m)$ cannot be matched by positive finite parameters, and $m=0$ or $m=1$ is also excluded.

After observing $n=h+t>0$ <Bernoulli trials>, the <Beta-binomial conjugacy> makes the <posterior> <mean> a weighted average of the <prior> <mean> $m$ and observed <binomial proportion> $h/n$:
$$
\mathbb E[P\mid h,t]=\frac{\alpha+h}{\kappa+n}=\frac{\kappa}{\kappa+n}m+\frac{n}{\kappa+n}\frac hn.
$$
This formula gives $\kappa$ a precise interpretation as the <prior>'s weight relative to the <sample size> in this <posterior> <mean>. It does not require fictitious observed counts: the <beta distribution> parameters can be any positive real numbers. For the eight-head, two-tail example above, $\kappa=4$ and $m=1/2$, so the <posterior> <mean> is $(4\cdot1/2+10\cdot0.8)/14=5/7$.

// Target: statistical-inference.bigb