Here we will visualize, in Python, a few important distributions that commonly appear in science and statistics. A more exhaustive list of statistical distributions can be found in the stats module of SciPy.

At the end of this IPython Notebook we will demonstrate what is arguably the most famous and profound theorem in statistics: the Central Limit Theorem.

Take the mean of $n$ random samples from ANY arbitrary distribution with a well defined standard deviation $\sigma$ and mean $\mu$. As $n$ gets bigger the distribution of the sample mean will always converge to a Gaussian (normal) distribution with mean $\mu$ and standard deviation $\sigma/\sqrt{n}$.

Colloquially speaking, the theorem states that the average (or sum) of a set of random measurements will tend to a bell-shaped curve no matter the shape of the original meaurement distribution. This explains the ubiquity of the Gaussian distribution in science and statistics. We can demonstrate the Central Limit Thereom in Python by sampling from three different distributions: flat, exponential, and beta.

In [7]:

fromfunctoolsimportpartial# provides capability to define function with partial argumentsN=1000000# number of times n samples are taken. Try varying this number.nobb=101# number of bin boundaries on plotsn=np.array([1,2,3,5,10,100])# number of samples to average overexp_mean=3# mean of exponential distributiona,b=0.7,0.5# parameters of beta distributiondist=[partial(np.random.random),partial(np.random.exponential,exp_mean),partial(np.random.beta,a,b)]title_names=["Flat","Exponential (mean=%.1f)"%exp_mean,"Beta (a=%.1f, b=%.1f)"%(a,b)]drange=np.array([[0,1],[0,10],[0,1]])# ranges of distributionsmeans=np.array([0.5,exp_mean,a/(a+b)])# means of distributionsvar=np.array([1/12,exp_mean**2,a*b/((a+b+1)*(a+b)**2)])# variances of distributionsbinrange=np.array([np.linspace(p,q,nobb)forp,qindrange])ln,ld=len(n),len(dist)plt.figure(figsize=((ld*4)+1,(ln*2)+1))foriinxrange(ln):# loop over number of n samples to average overforjinxrange(ld):# loop over the different distributionsplt.subplot(ln,ld,i*ld+1+j)plt.hist(np.mean(dist[j]((N,n[i])),1),binrange[j],normed=True)plt.xlim(drange[j])ifj==0:plt.ylabel('n=%i'%n[i],fontsize=15)ifi==0:plt.title(title_names[j],fontsize=15)else:clt=(1/(np.sqrt(2*np.pi*var[j]/n[i])))*exp(-(((binrange[j]-means[j])**2)*n[i]/(2*var[j])))plt.plot(binrange[j],clt,'r',linewidth=2)plt.show()

In the graphs above the red curve is the predicted Gaussian distribution from the Central Limit Thereom. Notice that the rate of convergence of the sample mean to the Gaussian depends on the original parent distribution. Also,

the mean of the Gaussian distribution is the same as the original parent distribution,