forked from itdaniher/probstat-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cdf.py
213 lines (168 loc) · 4.97 KB
/
Cdf.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright 2010 Allen B. Downey
#
# License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""Functions for building CDFs (cumulative distribution functions)."""
import bisect
import math
class Cdf(object):
"""Represents a cumulative distribution function.
Attributes:
xs: sequence of values
ps: sequence of probabilities
name: string used as a graph label.
"""
def __init__(self, xs=None, ps=None, name=''):
self.xs = xs
self.ps = ps
self.name = name
def Prob(self, x):
"""Returns the probability that corresponds to value x.
Computes CDF(x)
Args:
x: number
Returns:
float probability
"""
if x < self.xs[0]: return 0.0
index = bisect.bisect(self.xs, x)
p = self.ps[index-1]
return p
def Value(self, p):
"""Returns the value that corresponds to probability p.
Computes InverseCDF(p)
Args:
p: number in the range [0, 1]
Returns:
number value
"""
if p < 0 or p > 1:
raise ValueError('Probability p must be in range [0, 1]')
if p == 0: return self.xs[0]
if p == 1: return self.xs[-1]
index = bisect.bisect(self.ps, p)
if p == self.ps[index-1]:
return self.xs[index-1]
else:
return self.xs[index]
def Percentile(self, p):
"""Returns the value that corresponds to percentile p.
Args:
p: number in the range [0, 100]
Returns:
number value
"""
return self.Value(p / 100.0)
def Mean(self):
"""Computes the mean of a CDF.
Returns:
float mean
"""
old_p = 0
total = 0.0
for x, new_p in zip(self.xs, self.ps):
p = new_p - old_p
total += p * x
old_p = new_p
return total
def Round(self, multiplier=1000.0):
"""
An entry is added to the cdf only if the percentile differs
from the previous value in a significant digit, where the number
of significant digits is determined by multiplier. The
default is 1000, which keeps log10(1000) = 3 significant digits.
"""
# TODO(write this method)
pass
def Render(self):
"""Generates a sequence of points suitable for plotting.
An empirical CDF is a step function; linear interpolation
can be misleading.
Returns:
tuple of (xs, ps)
"""
xs = [self.xs[0]]
ps = [0.0]
for i, p in enumerate(self.ps):
xs.append(self.xs[i])
ps.append(p)
try:
xs.append(self.xs[i+1])
ps.append(p)
except IndexError:
pass
return xs, ps
def MakeCdf(items, name=''):
"""Makes a cdf from an unsorted histogram.
Args:
items: unsorted sequence of (value, frequency) pairs
name: string name for this CDF
Returns:
cdf: list of (value, fraction) pairs
"""
runsum = 0
xs = []
cs = []
for value, count in sorted(items):
runsum += count
xs.append(value)
cs.append(runsum)
total = float(runsum)
ps = [c/total for c in cs]
cdf = Cdf(xs, ps, name)
return cdf
def MakeCdfFromDict(d, name=''):
"""Makes a CDF from a dictionary that maps values to frequencies.
Args:
d: dictionary that maps values to frequencies.
name: string name for the data.
Returns:
Cdf object
"""
return MakeCdf(d.iteritems(), name)
def MakeCdfFromList(seq, name=''):
"""Creates a CDF from an unsorted sequence.
Args:
seq: unsorted sequence of sortable values
name: string name for the cdf
Returns:
Cdf object
"""
hist = {}
for x in seq:
hist[x] = hist.get(x, 0) + 1
return MakeCdfFromDict(hist, name)
def ProbLess(cdf1, cdf2):
"""Probability that a value from cdf1 is less than one from cdf2.
For continuous distributions F and G, the chance that a sample
from F is less than a sample from G is
Integral(x): pdf_F(x) * (1 - cdf_G(x))
This function computes an approximation of this Integral using
discrete CDFs.
Args:
cdf1: CDF object
cdf2: CDF object
Returns:
float probability
"""
total = 0.0
i = 0
j = 0
x = float('-Inf')
while True:
# sweep through cdf1 and compute p, the marginal prob of v2
unused_x1, p1 = cdf1.data[i]
x2, p2 = cdf1.data[i+1]
p = p2 - p1
# incr through cdf2 to find Prob{x < x2}
while x <= x2:
x, y = cdf2.data[j]
if j == len(cdf2.data)-1:
break
else:
j += 1
# add up the integral
total += p * (1 - y)
i += 1
if i == len(cdf1.data)-1:
break
return total