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.
-
11$\begingroup$ 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. $\endgroup$– gung - Reinstate MonicaCommented Jul 25, 2012 at 15:48
-
2$\begingroup$ This post could be subtitled "help! I don't want to do algebra!" (that's my reason for being here at least) $\endgroup$– Matthew TurnerCommented Oct 6, 2021 at 20:41
7 Answers
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.
- $\mu=\frac{\alpha}{\alpha+\beta}\in\left(0, 1\right)$
- $\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)$
-
2$\begingroup$ @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. $\endgroup$ Commented Aug 19, 2012 at 20:19
-
4$\begingroup$ When I call this function with
estBetaParams(0.06657, 0.1)
I getalpha=-0.025
,beta=-0.35
. How is this possible? $\endgroup$ Commented May 7, 2014 at 23:46 -
5$\begingroup$ These calculations will only work if the variance is less than the mean*(1-mean). $\endgroup$– dannoCommented Oct 29, 2015 at 18:20
-
3$\begingroup$ @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)$. $\endgroup$ Commented Nov 4, 2015 at 3:06
-
5$\begingroup$ Given arbitrary $\mu\in(0,1)$ and $\sigma^2\in(0,0.5^2)$, there exists a beta distribution with mean $\mu$ and variance $\sigma^2$ if and only if $\sigma^2\leq\mu(1-\mu)$. @assumednormal showed the "only if" part of this claim, and danno hints at the "if" part. The takeaway is that not all values of $(\mu,\sigma^2)\in(0,1)\times(0,0.5^2)$ lead to valid beta distributions. Hopefully this clears up a little confusion about negative $\alpha$ and $\beta$! $\endgroup$– David M.Commented Dec 15, 2020 at 19:30
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]);
which leads to the solution
$$ \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)$.
-
$\begingroup$ 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.... $\endgroup$– ocramCommented Jun 22, 2011 at 18:25
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": alpha, "beta": beta}
print(getAlphaBeta(0.5, 0.1)) # {alpha: 12, beta: 12}
You can verify the parameters $\alpha$ and $\beta$ by importing scipy.stats.beta
package.
-
7$\begingroup$ Thanks! I guess you should return {"alpha": alpha, "beta": beta} instead of {"alpha": 0.5, "beta": 0.1} $\endgroup$– YahiaCommented Jan 29, 2020 at 18:15
-
1$\begingroup$ Thanks. Not been active for a while. But I got a notification for this post now and I just updated this. $\endgroup$ Commented Jul 25, 2023 at 19:55
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).
-
1$\begingroup$ Wikipedia has a section on parameter estimation that lets you avoid too much work :) $\endgroup$– rm999Commented Jun 22, 2011 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$$
-
$\begingroup$ A user has attempted to leave the following comment: "there's an error somewhere here. Current formulation does not return the correct variance." $\endgroup$ Commented Apr 12, 2015 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$.