Coin Toss

Run the code by clicking on the cell below and then the run button above, below left. Adjust the slider for number of samples to a high value, say 2000. So we can observe a large part of the distribution. Now with that constant, adjust the value of tosses per sample. First keep it at a low number say 5, observe how it isn't quite a bell curve yet. As you move the slider to larger values of number of tosses, the distribution will approach the normal distribution. Or all this is baloney and we should just drink Guinness.

In [4]:
%matplotlib inline 


from ipywidgets import interactive
from scipy.stats import bernoulli 
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


def f(tosses_per_sample, number_of_samples):
    if(tosses_per_sample ==0) :
        print("come on toss it at least once!")
    else:
        meanheads = np.zeros(number_of_samples)
        for i in range(number_of_samples):
          meanheads[i] = np.sum(bernoulli.rvs(0.5, loc=0, size= tosses_per_sample , random_state=None))
        sns.distplot(meanheads, axlabel = "number of heads" )
        plt.show()

interactive_plot = interactive(f, tosses_per_sample=(0,500), number_of_samples=(0,2000))
output = interactive_plot.children[2]
output.layout.height = '350px'
interactive_plot

Author : Gowri Thampi