Deviation from the Mean · Central Limit Theorem (Optional)
Lesson 5
The code below plots \(\Pr[\alpha=k]\) for a binomially distributed random variable \(\alpha\) with \(n=10000\) and \(k \in [5000-100, 5000+100]\). These probabilities are shown in blue. The red line represents the function \[\frac{1}{\sqrt{2\pi np(1-p)}}\exp\left(-\frac{(k-np)^2}{2np(1-p)}\right).\]
import matplotlib.pyplot as plt
from math import comb, pi, sqrt, exp
def normal(k, n):
return exp(-(k - n / 2) ** 2 / (n / 2)) / sqrt(2 * pi * n / 4)
n = 10000
delta = int(2 * sqrt(n))
neighborhood = range(n // 2 - delta, n // 2 + delta)
plt.bar(neighborhood, [comb(n, k) / 2 ** n for k in neighborhood])
plt.plot(neighborhood, [normal(k, n) for k in neighborhood], color='red')
plt.savefig('de_moivre_laplace.png') =4/image0.png)