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.
 A: 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)$.
A: 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).
A: 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.
A: 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)$

A: 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: 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$.
A: 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.
