forked from AllenDowney/ProbablyOverthinkingIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurvival.py
More file actions
341 lines (247 loc) · 8.71 KB
/
survival.py
File metadata and controls
341 lines (247 loc) · 8.71 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""This file contains code for use with "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from __future__ import print_function, division
import numpy as np
import pandas as pd
import thinkbayes2
import thinkplot
from collections import Counter
FORMATS = ['pdf', 'eps', 'png']
class SurvivalFunction(object):
"""Represents a survival function."""
def __init__(self, ts, ss, label=''):
self.ts = ts
self.ss = ss
self.label = label
def __len__(self):
return len(self.ts)
def __getitem__(self, t):
return self.Prob(t)
def Prob(self, t):
"""Returns S(t), the probability that corresponds to value t.
t: time
returns: float probability
"""
return np.interp(t, self.ts, self.ss, left=1.0)
def Probs(self, ts):
"""Gets probabilities for a sequence of values."""
return np.interp(ts, self.ts, self.ss, left=1.0)
def Items(self):
"""Sorted list of (t, s) pairs."""
return zip(self.ts, self.ss)
def Render(self):
"""Generates a sequence of points suitable for plotting.
returns: tuple of (sorted times, survival function)
"""
return self.ts, self.ss
def MakeHazardFunction(self, label=''):
"""Computes the hazard function.
This simple version does not take into account the
spacing between the ts. If the ts are not equally
spaced, it is not valid to compare the magnitude of
the hazard function across different time steps.
label: string
returns: HazardFunction object
"""
lams = pd.Series(index=self.ts)
prev = 1.0
for t, s in zip(self.ts, self.ss):
lams[t] = (prev - s) / prev
prev = s
return HazardFunction(lams, label=label)
def MakePmf(self, filler=None):
"""Makes a PMF of lifetimes.
filler: value to replace missing values
returns: Pmf
"""
cdf = thinkbayes2.Cdf(self.ts, 1-self.ss)
pmf = thinkbayes2.Pmf()
for val, prob in cdf.Items():
pmf.Set(val, prob)
cutoff = cdf.ps[-1]
if filler is not None:
pmf[filler] = 1-cutoff
return pmf
def RemainingLifetime(self, filler=None, func=thinkbayes2.Pmf.Mean):
"""Computes remaining lifetime as a function of age.
func: function from conditional Pmf to expected liftime
returns: Series that maps from age to remaining lifetime
"""
pmf = self.MakePmf(filler=filler)
d = {}
for t in sorted(pmf.Values())[:-1]:
pmf[t] = 0
if pmf.Total():
pmf.Normalize()
d[t] = func(pmf) - t
return pd.Series(d)
def MakeSurvivalFromSeq(values, label=''):
"""Makes a survival function based on a complete dataset.
values: sequence of observed lifespans
returns: SurvivalFunction
"""
counter = Counter(values)
ts, freqs = zip(*sorted(counter.items()))
ts = np.asarray(ts)
ps = np.cumsum(freqs, dtype=np.float)
ps /= ps[-1]
ss = 1 - ps
return SurvivalFunction(ts, ss, label)
def MakeSurvivalFromCdf(cdf, label=''):
"""Makes a survival function based on a CDF.
cdf: Cdf
returns: SurvivalFunction
"""
ts = cdf.xs
ss = 1 - cdf.ps
return SurvivalFunction(ts, ss, label)
class HazardFunction(object):
"""Represents a hazard function."""
def __init__(self, d, label=''):
"""Initialize the hazard function.
d: dictionary (or anything that can initialize a series)
label: string
"""
self.series = pd.Series(d)
self.label = label
def __len__(self):
return len(self.series)
def __getitem__(self, t):
return self.series[t]
def Get(self, t, default=np.nan):
return self.series.get(t, default)
def Render(self):
"""Generates a sequence of points suitable for plotting.
returns: tuple of (sorted times, hazard function)
"""
return self.series.index, self.series.values
def MakeSurvival(self, label=''):
"""Makes the survival function.
returns: SurvivalFunction
"""
ts = self.series.index
ss = (1 - self.series).cumprod()
sf = SurvivalFunction(ts, ss, label=label)
return sf
def Extend(self, other):
"""Extends this hazard function by copying the tail from another.
other: HazardFunction
"""
last_index = self.series.index[-1] if len(self) else 0
more = other.series[other.series.index > last_index]
self.series = pd.concat([self.series, more])
def Truncate(self, t):
"""Truncates this hazard function at the given value of t.
t: number
"""
self.series = self.series[self.series.index < t]
def ConditionalSurvival(pmf, t0):
"""Computes conditional survival function.
Probability that duration exceeds t0+t, given that
duration >= t0.
pmf: Pmf of durations
t0: minimum time
returns: tuple of (ts, conditional survivals)
"""
cond = thinkbayes2.Pmf()
for t, p in pmf.Items():
if t >= t0:
cond.Set(t-t0, p)
cond.Normalize()
return MakeSurvivalFromCdf(cond.MakeCdf())
def PlotConditionalSurvival(durations):
"""Plots conditional survival curves for a range of t0.
durations: list of durations
"""
pmf = thinkbayes2.Pmf(durations)
times = [8, 16, 24, 32]
thinkplot.PrePlot(len(times))
for t0 in times:
sf = ConditionalSurvival(pmf, t0)
label = 't0=%d' % t0
thinkplot.Plot(sf, label=label)
thinkplot.Show()
def PlotSurvival(complete):
"""Plots survival and hazard curves.
complete: list of complete lifetimes
"""
thinkplot.PrePlot(3, rows=2)
cdf = thinkbayes2.Cdf(complete, label='cdf')
sf = MakeSurvivalFromCdf(cdf, label='survival')
print(cdf[13])
print(sf[13])
thinkplot.Plot(sf)
thinkplot.Cdf(cdf, alpha=0.2)
thinkplot.Config()
thinkplot.SubPlot(2)
hf = sf.MakeHazardFunction(label='hazard')
print(hf[39])
thinkplot.Plot(hf)
thinkplot.Config(ylim=[0, 0.75])
def PlotHazard(complete, ongoing):
"""Plots the hazard function and survival function.
complete: list of complete lifetimes
ongoing: list of ongoing lifetimes
"""
# plot S(t) based on only complete pregnancies
sf = MakeSurvivalFromSeq(complete)
thinkplot.Plot(sf, label='old S(t)', alpha=0.1)
thinkplot.PrePlot(2)
# plot the hazard function
hf = EstimateHazardFunction(complete, ongoing)
thinkplot.Plot(hf, label='lams(t)', alpha=0.5)
# plot the survival function
sf = hf.MakeSurvival()
thinkplot.Plot(sf, label='S(t)')
thinkplot.Show(xlabel='t (weeks)')
def EstimateHazardFunction(complete, ongoing, label='', verbose=False):
"""Estimates the hazard function by Kaplan-Meier.
http://en.wikipedia.org/wiki/Kaplan%E2%80%93Meier_estimator
complete: list of complete lifetimes
ongoing: list of ongoing lifetimes
label: string
verbose: whether to display intermediate results
"""
if np.sum(np.isnan(complete)):
raise ValueError("complete contains NaNs")
if np.sum(np.isnan(ongoing)):
raise ValueError("ongoing contains NaNs")
hist_complete = Counter(complete)
hist_ongoing = Counter(ongoing)
ts = list(hist_complete | hist_ongoing)
ts.sort()
at_risk = len(complete) + len(ongoing)
lams = pd.Series(index=ts)
for t in ts:
ended = hist_complete[t]
censored = hist_ongoing[t]
lams[t] = ended / at_risk
if verbose:
print('%0.3g\t%d\t%d\t%d\t%0.2g' %
(t, at_risk, ended, censored, lams[t]))
at_risk -= ended + censored
return HazardFunction(lams, label=label)
def EstimateHazardNumpy(complete, ongoing, label=''):
"""Estimates the hazard function by Kaplan-Meier.
Just for fun, this is a version that uses NumPy to
eliminate loops.
complete: list of complete lifetimes
ongoing: list of ongoing lifetimes
label: string
"""
hist_complete = Counter(complete)
hist_ongoing = Counter(ongoing)
ts = set(hist_complete) | set(hist_ongoing)
at_risk = len(complete) + len(ongoing)
ended = [hist_complete[t] for t in ts]
ended_c = np.cumsum(ended)
censored_c = np.cumsum([hist_ongoing[t] for t in ts])
not_at_risk = np.roll(ended_c, 1) + np.roll(censored_c, 1)
not_at_risk[0] = 0
at_risk_array = at_risk - not_at_risk
hs = ended / at_risk_array
lams = dict(zip(ts, hs))
return HazardFunction(lams, label=label)