📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This file contains code used in "Think Stats",1by Allen B. Downey, available from greenteapress.com23Copyright 2010 Allen B. Downey4License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html5"""67import csv8import datetime9import sys1011import Cdf12import myplot1314def ReadBirthdays(filename='birthdays.csv'):15"""Reads a CSV file of birthdays and returns a list of date objects.1617The first column of the file must contain dates in MM-DD format.1819Args:20filename: string filename2122Returns:23list of datetime.date objects"""24fp = open(filename)25reader = csv.reader(fp)26bdays = []2728for t in reader:29bday = t[0]30month, day = [int(x) for x in bday.split('-')]31date = datetime.date(2010, month, day)32bdays.append(date)3334return bdays3536def Diff(t):37"""Computes the differences between the adjacent elements of a sequence.3839Args:40t: sequence of anything subtractable4142Returns:43list of whatever is in t44"""45diffs = []46for i in range(len(t)-1):47diff = t[i+1] - t[i]48diffs.append(diff)49return diffs505152def Main(script):5354# read 'em and sort 'em55birthdays = ReadBirthdays()56birthdays.sort()5758# compute the intervals in days59deltas = Diff(birthdays)60days = [inter.days for inter in deltas]6162# make and plot the CCDF on a log scale.63cdf = Cdf.MakeCdfFromList(days, name='intervals')64scale = myplot.Cdf(cdf, transform='exponential')65myplot.Save(root='intervals',66xlabel='days',67ylabel='ccdf',68**scale)6970if __name__ == '__main__':71Main(*sys.argv)727374