import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline pd.set_option('display.mpl_style', 'default') # A function to flip a coin `num_times` times. def flip_coin(num_times): coin_flips = np.random.randint(0, 2, num_times) counts = pd.Series(coin_flips).value_counts().reindex([0,1]).fillna(0) counts.index = ['heads', 'tails'] return counts flip_coin(10) flip_coin(20) flip_coin(10000) def mustard_likers_null_hypothesis(sample_size): coin_flips = flip_coin(sample_size) return coin_flips.ix['heads'] counts = pd.Series([mustard_likers_null_hypothesis(10) for i in range(10000)]).value_counts().sort_index() counts counts.plot(kind='bar') mustard_likers = pd.Series([mustard_likers_null_hypothesis(10000) for i in range(10000)]).value_counts().sort_index() plt.xlabel("Number of people who say they like mustard") plt.ylabel("Frequency") mustard_likers.reindex(np.arange(0, 10001)).fillna(0).plot() plt.xlabel("Number of people who say they like mustard") plt.ylabel("Frequency") mustard_likers.reindex(np.arange(4700, 5301)).plot()