| Download
Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.
This is the accompanying code for this book.
Project: Support and Testing
Views: 7088License: GPL3
"""This file contains code for use with "Think Stats",1by Allen B. Downey, available from greenteapress.com23Copyright 2014 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67from __future__ import print_function89import sys10from operator import itemgetter1112import first13import thinkstats2141516def Mode(hist):17"""Returns the value with the highest frequency.1819hist: Hist object2021returns: value from Hist22"""23p, x = max([(p, x) for x, p in hist.Items()])24return x252627def AllModes(hist):28"""Returns value-freq pairs in decreasing order of frequency.2930hist: Hist object3132returns: iterator of value-freq pairs33"""34return sorted(hist.Items(), key=itemgetter(1), reverse=True)353637def WeightDifference(live, firsts, others):38"""Explore the difference in weight between first babies and others.3940live: DataFrame of all live births41firsts: DataFrame of first babies42others: DataFrame of others43"""44mean0 = live.totalwgt_lb.mean()45mean1 = firsts.totalwgt_lb.mean()46mean2 = others.totalwgt_lb.mean()4748var1 = firsts.totalwgt_lb.var()49var2 = others.totalwgt_lb.var()5051print('Mean')52print('First babies', mean1)53print('Others', mean2)5455print('Variance')56print('First babies', var1)57print('Others', var2)5859print('Difference in lbs', mean1 - mean2)60print('Difference in oz', (mean1 - mean2) * 16)6162print('Difference relative to mean (%age points)',63(mean1 - mean2) / mean0 * 100)6465d = thinkstats2.CohenEffectSize(firsts.totalwgt_lb, others.totalwgt_lb)66print('Cohen d', d)676869def main(script):70"""Tests the functions in this module.7172script: string script name73"""74live, firsts, others = first.MakeFrames()75hist = thinkstats2.Hist(live.prglngth)7677# explore the weight difference between first babies and others78WeightDifference(live, firsts, others)7980# test Mode81mode = Mode(hist)82print('Mode of preg length', mode)83assert(mode == 39)8485# test AllModes86modes = AllModes(hist)87assert(modes[0][1] == 4693)8889for value, freq in modes[:5]:90print(value, freq)9192print('%s: All tests passed.' % script)939495if __name__ == '__main__':96main(*sys.argv)979899