# -*- coding: utf-8 -*- """ Created on Sat Sep 17 13:52:01 2016 @author: piyushmaheshwari """ import comp_prob_inference import matplotlib.pyplot as plt n = 100000 heads_so_far = 0 fraction_of_heads = [] for i in range (n): if comp_prob_inference.flip_fair_coin() == 'heads': heads_so_far += 1 fraction_of_heads.append (heads_so_far / (i + 1)) plt.figure(figsize=(8,4)) plt.plot (range(1, n+1), fraction_of_heads) plt.xlabel('Number of flips') plt.ylabel('Fraction of heads')
import comp_prob_inference import matplotlib.pyplot as plt #flips = comp_prob_inference.flip_fair_coins(100) #comp_prob_inference.plot_discrete_histogram(flips, frequency=True) n = 100000 heads_so_far = 0 fraction_of_heads = [] for i in range(n): if comp_prob_inference.flip_fair_coin() == 'heads': heads_so_far += 1 fraction_of_heads.append(heads_so_far / float(i + 1)) plt.figure(figsize=(8, 4)) plt.plot(range(1, n + 1), fraction_of_heads) plt.xlabel('Number of flips') plt.ylabel('Fraction of heads') plt.show()
#!/usr/bin/env python # -*- coding: utf-8 -*- import comp_prob_inference as cpi from matplotlib.backends.backend_pdf import PdfPages import matplotlib.pyplot as plt # The PDF document pdf_pages = PdfPages("flips.pdf") print(cpi.flip_fair_coin()) flips = cpi.flip_fair_coins(100) cpi.plot_discrete_histogram(flips) pdf_pages.savefig() cpi.plot_discrete_histogram(flips, frequency=True) pdf_pages.savefig() n = 100000 heads_so_far = 0 fraction_of_heads = [] for i in range(n): if cpi.flip_fair_coin() == 'heads': heads_so_far += 1 fraction_of_heads.append(heads_so_far / (i + 1)) plt.figure(figsize=(8, 4)) plt.plot(range(1, n + 1), fraction_of_heads) plt.xlabel('Number of flips') plt.ylabel('Fraction of heads')