def BornAtButNotBefore(week): table, firsts, others = first.MakeTables(".") first.ProcessTables(firsts, others) DropValues(firsts, week) DropValues(others, week) firstBabies = Pmf.MakePmfFromList(firsts.lengths, name="First babies") otherBabies = Pmf.MakePmfFromList(others.lengths, name="Other babies") return (firstBabies, otherBabies)
return ProbInRange(pmf, lambda week: 38 <= week <= 40) def ProbLate(pmf): return ProbInRange(pmf, lambda week: week >= 41) def FindAndPrintProbs(pmfName, probEarly, probOnTime, probLate): print '{0} pregnancy length probabilities:\nEarly {1:.2f}%\nOn Time {2:.2f}%\nLate {3:.2f}%\n'.format( pmfName, probEarly * 100, probOnTime * 100, probLate * 100) import first all, firsts, others = first.MakeTables() first.ProcessTables(all, firsts, others) import Pmf pmfFirst = Pmf.MakePmfFromList(firsts.lengths, "First Babies") firstProbEarly = ProbEarly(pmfFirst) firstProbOnTime = ProbOnTime(pmfFirst) firstProbLate = ProbLate(pmfFirst) FindAndPrintProbs(pmfFirst.name, firstProbEarly, firstProbOnTime, firstProbLate) pmfOthers = Pmf.MakePmfFromList(others.lengths, "Other Babies") otherProbEarly = ProbEarly(pmfOthers) otherProbOnTime = ProbOnTime(pmfOthers) otherProbLate = ProbLate(pmfOthers) FindAndPrintProbs(pmfOthers.name, otherProbEarly, otherProbOnTime,
Pumpkin(591) ] def Pumpkin(): weights = [p.weight for p in pumpkins] mu, var = thinkstats.MeanVar(weights) s = math.sqrt(var) return (mu, var, s) print 'mean, variance, standard deviation', Pumpkin() # ex 2.2 all, firsts, others = first.MakeTables() first.ProcessTables(firsts, others) firsts.s = math.sqrt(thinkstats.Var(firsts.lengths, firsts.mu)) others.s = math.sqrt(thinkstats.Var(others.lengths, others.mu)) print 'first babies mean:', firsts.mu, 'standard deviation:', firsts.s print 'others mean:', others.mu, 'standard deviation:', others.s print 'mean difference (hours)', (firsts.mu - others.mu) * 7 * 24 print 'spread difference (hours)', (firsts.s - others.s) * 7 * 24 # Distributions firstsPrgLengthFreqDict = {} for length in firsts.lengths: firstsPrgLengthFreqDict[length] = firstsPrgLengthFreqDict.get(length, 0) + 1
def firstBabiesPFM(): all, firsts, others = first.MakeTables() first.ProcessTables(all, firsts, others) return Pmf.MakePmfFromList(firsts.lengths, "First Babies")
def otherBabiesPMF(): all, firsts, others = first.MakeTables() first.ProcessTables(all, firsts, others) return Pmf.MakePmfFromList(others.lengths, "Other Babies")