forked from AllenDowney/ThinkStats2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthdays.py
72 lines (54 loc) · 1.62 KB
/
birthdays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import csv
import datetime
import sys
import Cdf
import myplot
def ReadBirthdays(filename='birthdays.csv'):
"""Reads a CSV file of birthdays and returns a list of date objects.
The first column of the file must contain dates in MM-DD format.
Args:
filename: string filename
Returns:
list of datetime.date objects"""
fp = open(filename)
reader = csv.reader(fp)
bdays = []
for t in reader:
bday = t[0]
month, day = [int(x) for x in bday.split('-')]
date = datetime.date(2010, month, day)
bdays.append(date)
return bdays
def Diff(t):
"""Computes the differences between the adjacent elements of a sequence.
Args:
t: sequence of anything subtractable
Returns:
list of whatever is in t
"""
diffs = []
for i in range(len(t)-1):
diff = t[i+1] - t[i]
diffs.append(diff)
return diffs
def Main(script):
# read 'em and sort 'em
birthdays = ReadBirthdays()
birthdays.sort()
# compute the intervals in days
deltas = Diff(birthdays)
days = [inter.days for inter in deltas]
# make and plot the CCDF on a log scale.
cdf = Cdf.MakeCdfFromList(days, name='intervals')
scale = myplot.Cdf(cdf, transform='exponential')
myplot.Save(root='intervals',
xlabel='days',
ylabel='ccdf',
**scale)
if __name__ == '__main__':
Main(*sys.argv)