Thursday, February 12, 2015

py17. Central Limit Theorem and Mean of Beta Distribution in Python

For any distribution (such as Beta, but not limited to Beta), the mean is a normal random variable. It does require that we use sample alpha and beta parameters, for the Beta, for example.


For the Beta distribution, we use numpy.random.beta. For the confidence level calculations, we use the scipy.stats.norm, for the cumulative density function.


The percentage of mean inside margin of error is calculated, for the 5000 Beta distributions.

# ex17.py
from __future__ import print_function, division
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import beta
from scipy.stats import norm
N = 5000
n = 1000
a = 2
b = 5
mn = np.zeros(N)
for i in range(N):
    x = beta(a, b, n)
    mn[i] = x.mean()

mn_samps = mn.mean()
conf_range = range(5,100,5)
zin = np.zeros(len(conf_range))
std_samps = mn.std()
for i,p in enumerate(conf_range):
    z_crit = norm.ppf((100+p)/200)
    moe = z_crit*std_samps
    ins = (mn_samps-moe < mn) & (mn < mn_samps+moe)
    zin[i] = sum(ins)/N

plt.plot(conf_range, zin*100, 'b')
plt.xlabel('Confidence Level')
plt.ylabel('Percentage mean inside moe')
plt.title('Mean of Beta Samples')
plt.show()

Output:

No comments:

Post a Comment