The mean and standard deviation of a binomial random variable with parameters $n$ and $p$ are
$$ \mu = np $$and $$ \sigma = np(1 - p)\text{,}$$ respectively.
Let us recall the example, from the previous section. The probability to pass the final statistic exam is 0.3. We consider a class of 25 students. The random variable $X$, which corresponds to success in the exam is a binomial random variable, and follows a binomial distribution with parameters $n=25$ and $p=0.3$. Thus the mean, $\mu$, and the standard deviation, $\sigma$, can be computed as follows:
$$\mu = np = 25 \times 0.3 = 7.5$$and
$$ \sigma = np(1 - p) = 25 \times 0.3 \times (1-0.3) = 5.25 $$The following plot visualizes the binomial distribution with parameters $n=25$ and $p=0.3$, and its mean $\mu$ and standard deviation $\sigma$.
size = 100000 # number of random samples
random.seed(3) # set seed for reproducibility
random_binom_numbers = stats.binom.rvs(n0, p0, size=size)
plt.figure(figsize=(10, 5))
plt.hist(
random_binom_numbers,
density=True,
bins=len(np.unique(random_binom_numbers)),
color="lightgrey",
edgecolor="black",
) # density=False would make counts
plt.xlabel("Students passing the final exam")
plt.ylabel("Probability")
plt.ylim([0, 0.25])
plt.title("size = 25, p = 0.3")
plt.xticks(
np.arange(min(random_binom_numbers), max(random_binom_numbers) + 1, 2.0)
) # define x-axis ticks
mu = n0 * p0
sigma = n0 * p0 * (1 - p0)
plt.axvline(x=mu, color="red", linestyle="dashed", linewidth=3)
plt.axvline(x=mu - sigma, color="black", linestyle="dashed", linewidth=3)
plt.axvline(x=mu + sigma, color="black", linestyle="dashed", linewidth=3)
plt.text(mu + 0.2, 0.21, "\u03BC", fontsize=14, color="red")
plt.text(mu - sigma + 0.2, 0.21, "-\u03C3", fontsize=14, color="black")
plt.text(mu + sigma + 0.2, 0.21, "+\u03C3", fontsize=14, color="black")
plt.show()
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.