Article 5G99S Beta distribution with given mean and variance

Beta distribution with given mean and variance

by
John
from John D. Cook on (#5G99S)

It occurred to me recently that a problem I solved numerically years ago could be solved analytically, the problem of determining beta distribution parameters so that the distribution has a specified mean and variance. The calculation turns out to be fairly simple. Maybe someone has done it before.

Problem statement

The beta distribution has two positive parameters, a and b, and has probability density proportional to [1]

betamv1.svg

for x between 0 and 1.

The mean of a beta(a, b) distribution is

betamv2.svg

and the variance is

betamv3.svg

Given and ^2 we want to solve for a and b. In order for the problem to be meaningful must be between 0 and 1, and ^2 must be less than (1-). [2]

As we will see shortly, these two necessary conditions for a solution are also sufficient.

Visualization

Graphically, we want to find the intersection of a line of constant mean

beta_mean_contour.png

with a line of constant variance.

beta_var_contour.png

Note that the scales in the two plots differ.

Solution

If we set

betamv4.svg

then b = ka and so we can eliminate b from the equation for variance to get

betamv5.svg

Now since a > 0, we can divide by a^2

betamv6.svg

and from there solve for a:

betamv7.svg

and b = ka.

We require ^2 to be less than (1-), or equivalently we require the ratio of (1-) to ^2 to be greater than 1. It works out that the solution a is the product of the mean and the amount by which the ratio of (1-) to ^2 exceeds 1.

Verification

Here is a little code to check for errors in the derivation above. It generates and ^2 values at random, solves for a and b, then checks that the beta(a, b) distribution has the specified mean and variance.

 from scipy.stats import uniform, beta for _ in range(100): mu = uniform.rvs() sigma2 = uniform.rvs(0, mu*(1-mu)) a = mu*(mu*(1-mu)/sigma2 - 1) b = a*(1-mu)/mu x = beta(a, b) assert( abs(x.mean() - mu) < 1e-10) assert( abs(x.var() - sigma2) < 1e-10) print("Done")
Related posts

[1] It's often easiest to think of probability densities ignoring proportionality constants. Densities integrate to 1, so the proportionality constants are determined by the rest of the expression for the density. In the case of the beta distribution, the proportionality constant works out to (a + b) / (a) (b).

[2] The variance of a beta distribution factors into (1-)/(a + b + 1), so it is less than (1-).

The post Beta distribution with given mean and variance first appeared on John D. Cook.jQW6KuSU5BY
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments