Created
September 3, 2018 07:50
-
-
Save magusafr/2c64284ea4d50cd69b8048df57a82660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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