Skip to content

Instantly share code, notes, and snippets.

@magusafr
Created September 3, 2018 07:50
Show Gist options
  • Save magusafr/2c64284ea4d50cd69b8048df57a82660 to your computer and use it in GitHub Desktop.
Save magusafr/2c64284ea4d50cd69b8048df57a82660 to your computer and use it in GitHub Desktop.
#Write a function sample that simulates N sets of coin flips and
#returns a list of the proportion of heads in each set of N flips
#It may help to use the flip and mean functions that you wrote before
import random
from math import sqrt
from plotting import *
def mean(data):
return float(sum(data))/len(data)
def variance(data):
mu=mean(data)
return sum([(x-mu)**2 for x in data])/len(data)
def stddev(data):
return sqrt(variance(data))
def flip(N):
return [random.random()>0.5 for x in range(N)]
def sample(N):
#Insert your code here
return [mean(flip(N)) for i in range(N)]
N=1000
outcomes=sample(N)
histplot(outcomes,nbins=30)
print mean(outcomes)
print stddev(outcomes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment