# Calculating the parameters of a Beta distribution using the mean and variance

How can I calculate the $\alpha$ and $\beta$ parameters for a Beta distribution if I know the mean and variance that I want the distribution to have? Examples of an R command to do this would be most helpful.

• Note that the betareg package in R uses an alternative parameterization (with the mean, $\mu=\alpha/(\alpha+\beta)$, & the precision, $\phi=\alpha+\beta$--and hence the variance is $\mu(1-\mu)/(1+\phi)$) which obviates the need for these calculations. – gung - Reinstate Monica Jul 25 '12 at 15:48

I set$$\mu=\frac{\alpha}{\alpha+\beta}$$and$$\sigma^2=\frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}$$and solved for $\alpha$ and $\beta$. My results show that$$\alpha=\left(\frac{1-\mu}{\sigma^2}-\frac{1}{\mu}\right)\mu^2$$and$$\beta=\alpha\left(\frac{1}{\mu}-1\right)$$

I've written up some R code to estimate the parameters of the Beta distribution from a given mean, mu, and variance, var:

estBetaParams <- function(mu, var) {
alpha <- ((1 - mu) / var - 1 / mu) * mu ^ 2
beta <- alpha * (1 / mu - 1)
return(params = list(alpha = alpha, beta = beta))
}


There's been some confusion around the bounds of $\mu$ and $\sigma^2$ for any given Beta distribution, so let's make that clear here.

1. $\mu=\frac{\alpha}{\alpha+\beta}\in\left(0, 1\right)$
2. $\sigma^2=\frac{\alpha\beta}{\left(\alpha+\beta\right)^2\left(\alpha+\beta+1\right)}=\frac{\mu\left(1-\mu\right)}{\alpha+\beta+1}<\frac{\mu\left(1-\mu\right)}{1}=\mu\left(1-\mu\right)\in\left(0,0.5^2\right)$
• @stan This will give you the Beta distribution which has the same mean and variance as your data. It will not tell you how well the distribution fits the data. Try the Kolmogorov-Smirnov Test. – assumednormal Aug 19 '12 at 20:19
• When I call this function with estBetaParams(0.06657, 0.1) I get alpha=-0.025, beta=-0.35. How is this possible? – Amelio Vazquez-Reina May 7 '14 at 23:46
• These calculations will only work if the variance is less than the mean*(1-mean). – danno Oct 29 '15 at 18:20
• @danno - It's always the case that $\sigma^2\leq\mu\left(1-\mu\right)$. To see this, rewrite the variance as $\sigma^2=\frac{\mu\left(1-\mu\right)}{\alpha+\beta+1}$. Since $\alpha+\beta+1\geq1$, $\sigma^2\leq\mu\left(1-\mu\right)$. – assumednormal Nov 4 '15 at 3:06
• @AmelioVazquez-Reina If you give your original data I expect it will quickly be obvious why a beta distribution isn't suitable. – Glen_b Nov 4 '15 at 3:49

Here's a generic way to solve these types of problems, using Maple instead of R. This works for other distributions as well:

with(Statistics):
eq1 := mu = Mean(BetaDistribution(alpha, beta)):
eq2 := sigma^2 = Variance(BetaDistribution(alpha, beta)):
solve([eq1, eq2], [alpha, beta]);


\begin{align*} \alpha &= - \frac{\mu (\sigma^2 + \mu^2 - \mu)}{\sigma^2} \\ \beta &= \frac{(\sigma^2 + \mu^2 - \mu) (\mu - 1)}{\sigma^2}. \end{align*}

This is equivalent to Max's solution.

In R, the beta distribution with parameters $\textbf{shape1} = a$ and $\textbf{shape2} = b$ has density

$f(x) = \frac{\Gamma(a+b)}{\Gamma(a) \Gamma(b)} x^{a-1}(1-x)^{b-1}$,

for $a > 0$, $b >0$, and $0 < x < 1$.

In R, you can compute it by

dbeta(x, shape1=a, shape2=b)

In that parametrisation, the mean is $E(X) = \frac{a}{a+b}$ and the variance is $V(X) = \frac{ab}{(a + b)^2 (a + b + 1)}$. So, you can now follow Nick Sabbe's answer.

Good work!

Edit

I find:

$a = \left( \frac{1 - \mu}{V} - \frac{1}{\mu} \right) \mu^2$,

and

$b = \left( \frac{1 - \mu}{V} - \frac{1}{\mu} \right) \mu (1 - \mu)$,

where $\mu=E(X)$ and $V=V(X)$.

• I realise my answer is very similar to the others. Nonetheless, I believe it is always a good point to first check what parametrisation R uses.... – ocram Jun 22 '11 at 18:25

On Wikipedia for example, you can find the following formulas for mean and variance of a beta distribution given alpha and beta: $$\mu=\frac{\alpha}{\alpha+\beta}$$ and $$\sigma^2=\frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}$$ Inverting these ( fill out $\beta=\alpha(\frac{1}{\mu}-1)$ in the bottom equation) should give you the result you want (though it may take some work).

• Wikipedia has a section on parameter estimation that lets you avoid too much work :) – rm999 Jun 22 '11 at 18:10

For a generalized Beta distribution defined on the interval $[a,b]$, you have the relations:

$$\mu=\frac{a\beta+b\alpha}{\alpha+\beta},\quad\sigma^{2}=\frac{\alpha\beta\left(b-a\right)^{2}}{\left(\alpha+\beta\right)^{2}\left(1+\alpha+\beta\right)}$$

which can be inverted to give:

$$\alpha=\lambda\frac{\mu-a}{b-a},\quad\beta=\lambda\frac{b-\mu}{b-a}$$

where

$$\lambda=\frac{\left(\mu-a\right)\left(b-\mu\right)}{\sigma^{2}}-1$$

• A user has attempted to leave the following comment: "there's an error somewhere here. Current formulation does not return the correct variance." – Silverfish Apr 12 '15 at 15:14

Solve the $$\mu$$ equation for either $$\alpha$$ or $$\beta$$, solving for $$\beta$$, you get $$\beta=\frac{\alpha(1-\mu)}{\mu}$$ Then plug this into the second equation, and solve for $$\alpha$$. So you get $$\sigma^2=\frac{\frac{\alpha^2(1-\mu)}{\mu}}{(\alpha+\frac{\alpha(1-\mu)}{\mu})^2(\alpha+\frac{\alpha(1-\mu)}{\mu}+1)}$$ Which simplifies to $$\sigma^2=\frac{\frac{\alpha^2(1-\mu)}{\mu}}{(\frac{\alpha}{\mu})^2\frac{\alpha+\mu}{\mu}}$$ $$\sigma^2=\frac{(1-\mu)\mu^2}{\alpha+\mu}$$ Then finish solving for $$\alpha$$.

I was looking for python, but stumbled upon this. So this would be useful for others like me.

Here is a python code to estimate beta parameters (according to the equations given above):

# estimate parameters of beta dist.
def getAlphaBeta(mu, sigma):
alpha = mu**2 * ((1 - mu) / sigma**2 - 1 / mu)

beta = alpha * (1 / mu - 1)

return {"alpha": 0.5, "beta": 0.1}

print(getAlphaBeta(0.5, 0.1)  # {alpha: 12, beta: 12}


You can verify the parameters $$\alpha$$ and $$\beta$$ by importing scipy.stats.beta package.

• Thanks! I guess you should return {"alpha": alpha, "beta": beta} instead of {"alpha": 0.5, "beta": 0.1} – Yahia Jan 29 '20 at 18:15