-
Notifications
You must be signed in to change notification settings - Fork 209
/
empyrical_dist.py
836 lines (606 loc) · 19.5 KB
/
empyrical_dist.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
"""
Pmf: Represents a Probability Mass Function (PMF).
Cdf: Represents a Cumulative Distribution Function (CDF).
Copyright 2019 Allen B. Downey
MIT License: https://opensource.org/licenses/MIT
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import interp1d
def underride(d, **options):
"""Add key-value pairs to d only if key is not in d.
d: dictionary
options: keyword args to add to d
:return: modified d
"""
for key, val in options.items():
d.setdefault(key, val)
return d
class Pmf(pd.Series):
"""Represents a probability Mass Function (PMF)."""
def __init__(self, *args, **kwargs):
"""Initialize a Pmf.
Note: this cleans up a weird Series behavior, which is
that Series() and Series([]) yield different results.
See: https://github.com/pandas-dev/pandas/issues/16737
"""
if args:
super().__init__(*args, **kwargs)
else:
underride(kwargs, dtype=np.float64)
super().__init__([], **kwargs)
def copy(self, deep=True):
"""Make a copy.
:return: new Pmf
"""
return Pmf(self, copy=deep)
def __getitem__(self, qs):
"""Look up qs and return ps."""
try:
return super().__getitem__(qs)
except (KeyError, ValueError, IndexError):
return 0
@property
def qs(self):
"""Get the quantities.
:return: NumPy array
"""
return self.index.values
@property
def ps(self):
"""Get the probabilities.
:return: NumPy array
"""
return self.values
def _repr_html_(self):
"""Returns an HTML representation of the series.
Mostly used for Jupyter notebooks.
"""
df = pd.DataFrame(dict(probs=self))
return df._repr_html_()
def normalize(self):
"""Make the probabilities add up to 1 (modifies self).
:return: normalizing constant
"""
total = self.sum()
self /= total
return total
def mean(self):
"""Computes expected value.
:return: float
"""
#TODO: error if not normalized
#TODO: error if the quantities are not numeric
return np.sum(self.ps * self.qs)
def median(self):
"""Median (50th percentile).
:return: float
"""
return self.quantile(0.5)
def quantile(self, ps, **kwargs):
"""Quantiles.
Computes the inverse CDF of ps, that is,
the values that correspond to the given probabilities.
:return: float
"""
return self.make_cdf().quantile(ps, **kwargs)
def var(self):
"""Variance of a PMF.
:return: float
"""
m = self.mean()
d = self.qs - m
return np.sum(d**2 * self.ps)
def std(self):
"""Standard deviation of a PMF.
:return: float
"""
return np.sqrt(self.var())
def choice(self, *args, **kwargs):
"""Makes a random sample.
Uses the probabilities as weights unless `p` is provided.
args: same as np.random.choice
kwargs: same as np.random.choice
:return: NumPy array
"""
underride(kwargs, p=self.ps)
return np.random.choice(self.qs, *args, **kwargs)
def sample(self, *args, **kwargs):
"""Makes a random sample.
Uses the probabilities as weights unless `weights` is provided.
This function returns an array containing a sample of the quantities in this Pmf,
which is different from Series.sample, which returns a Series with a sample of
the rows in the original Series.
args: same as Series.sample
options: same as Series.sample
:return: NumPy array
"""
series = pd.Series(self.qs)
underride(kwargs, weights=self.ps)
sample = series.sample(*args, **kwargs)
return sample.values
def plot(self, **options):
"""Plot the Pmf as a line.
:param options: passed to plt.plot
:return:
"""
underride(options, label=self.name)
plt.plot(self.qs, self.ps, **options)
def bar(self, **options):
"""Makes a bar plot.
options: passed to plt.bar
"""
underride(options, label=self.name)
plt.bar(self.qs, self.ps, **options)
def add(self, x):
"""Computes the Pmf of the sum of values drawn from self and x.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(self, x, np.add.outer)
else:
return Pmf(self.ps, index=self.qs + x)
__add__ = add
__radd__ = add
def sub(self, x):
"""Computes the Pmf of the diff of values drawn from self and other.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(self, x, np.subtract.outer)
else:
return Pmf(self.ps, index=self.qs - x)
subtract = sub
__sub__ = sub
def rsub(self, x):
"""Computes the Pmf of the diff of values drawn from self and other.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(x, self, np.subtract.outer)
else:
return Pmf(self.ps, index=x - self.qs)
__rsub__ = rsub
def mul(self, x):
"""Computes the Pmf of the product of values drawn from self and x.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(self, x, np.multiply.outer)
else:
return Pmf(self.ps, index=self.qs * x)
multiply = mul
__mul__ = mul
__rmul__ = mul
def div(self, x):
"""Computes the Pmf of the ratio of values drawn from self and x.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(self, x, np.divide.outer)
else:
return Pmf(self.ps, index=self.qs / x)
divide = div
__div = div
__truediv__ = div
def rdiv(self, x):
"""Computes the Pmf of the ratio of values drawn from self and x.
x: another Pmf or a scalar or a sequence
:return: new Pmf
"""
if isinstance(x, Pmf):
return pmf_conv(x, self, np.divide.outer)
else:
return Pmf(self.ps, index=x / self.qs)
__rdiv__ = rdiv
__rtruediv__ = rdiv
def make_joint(self, other, **options):
"""Make joint distribution (assuming independence).
:param self:
:param other:
:param options: passed to Pmf constructor
:return: new Pmf
"""
qs = pd.MultiIndex.from_product([self.qs, other.qs])
ps = np.multiply.outer(self.ps, other.ps).flatten()
return Pmf(ps, index=qs, **options)
def marginal(self, i, name=None):
"""Gets the marginal distribution of the indicated variable.
i: index of the variable we want
name: string
:return: Pmf
"""
# TODO: rewrite this using MultiIndex operations
pmf = Pmf(name=name)
for vs, p in self.items():
pmf[vs[i]] += p
return pmf
def conditional(self, i, j, val, name=None):
"""Gets the conditional distribution of the indicated variable.
Distribution of vs[i], conditioned on vs[j] = val.
i: index of the variable we want
j: which variable is conditioned on
val: the value the jth variable has to have
name: string
:return: Pmf
"""
# TODO: rewrite this using MultiIndex operations
pmf = Pmf(name=name)
for vs, p in self.items():
if vs[j] == val:
pmf[vs[i]] += p
pmf.normalize()
return pmf
def update(self, likelihood, data):
"""Bayesian update.
likelihood: function that takes (data, hypo) and returns
likelihood of data under hypo
data: whatever format like_func understands
:return: normalizing constant
"""
for hypo in self.qs:
self[hypo] *= likelihood(data, hypo)
return self.normalize()
def max_prob(self):
"""Value with the highest probability.
:return: the value with the highest probability
"""
return self.idxmax()
def make_cdf(self, normalize=True):
"""Make a Cdf from the Pmf.
It can be good to normalize the cdf even if the Pmf was normalized,
to guarantee that the last element of `ps` is 1.
:return: Cdf
"""
cdf = Cdf(self.cumsum())
if normalize:
cdf.normalize()
return cdf
def quantile(self, ps):
"""Quantities corresponding to given probabilities.
ps: sequence of probabilities
return: sequence of quantities
"""
cdf = self.make_cdf()
return cdf.quantile(ps)
def credible_interval(self, p):
"""Credible interval containing the given probability.
p: float 0-1
:return: array of two quantities
"""
tail = (1-p) / 2
ps = [tail, 1-tail]
return self.quantile(ps)
@staticmethod
def from_seq(seq, normalize=True, sort=True, **options):
"""Make a PMF from a sequence of values.
seq: any kind of sequence
normalize: whether to normalize the Pmf, default True
sort: whether to sort the Pmf by values, default True
options: passed to the pd.Series constructor
:return: Pmf object
"""
series = pd.Series(seq).value_counts(sort=False)
options['copy'] = False
pmf = Pmf(series, **options)
if sort:
pmf.sort_index(inplace=True)
if normalize:
pmf.normalize()
return pmf
# Comparison operators
def gt(self, x):
"""Probability that a sample from this Pmf > x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_gt(self, x)
else:
return self[self.qs > x].sum()
__gt__ = gt
def lt(self, x):
"""Probability that a sample from this Pmf < x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_lt(self, x)
else:
return self[self.qs < x].sum()
__lt__ = lt
def ge(self, x):
"""Probability that a sample from this Pmf >= x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_ge(self, x)
else:
return self[self.qs >= x].sum()
__ge__ = ge
def le(self, x):
"""Probability that a sample from this Pmf <= x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_le(self, x)
else:
return self[self.qs <= x].sum()
__le__ = le
def eq(self, x):
"""Probability that a sample from this Pmf == x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_eq(self, x)
else:
return self[self.qs == x].sum()
__eq__ = eq
def ne(self, x):
"""Probability that a sample from this Pmf != x.
x: number
:return: float probability
"""
if isinstance(x, Pmf):
return pmf_ne(self, x)
else:
return self[self.qs != x].sum()
__ne__ = ne
def pmf_conv(pmf1, pmf2, ufunc):
"""Convolve two PMFs.
pmf1:
pmf2:
ufunc: elementwise function for arrays
:return: new Pmf
"""
qs = ufunc(pmf1.qs, pmf2.qs).flatten()
ps = np.multiply.outer(pmf1.ps, pmf2.ps).flatten()
series = pd.Series(ps).groupby(qs).sum()
return Pmf(series)
def pmf_add(pmf1, pmf2):
"""Distribution of the sum.
pmf1:
pmf2:
:return: new Pmf
"""
return pmf_conv(pmf1, pmf2, np.add.outer)
def pmf_sub(pmf1, pmf2):
"""Distribution of the difference.
pmf1:
pmf2:
:return: new Pmf
"""
return pmf_conv(pmf1, pmf2, np.subtract.outer)
def pmf_mul(pmf1, pmf2):
"""Distribution of the product.
pmf1:
pmf2:
:return: new Pmf
"""
return pmf_conv(pmf1, pmf2, np.multiply.outer)
def pmf_div(pmf1, pmf2):
"""Distribution of the ratio.
pmf1:
pmf2:
:return: new Pmf
"""
return pmf_conv(pmf1, pmf2, np.divide.outer)
def pmf_outer(pmf1, pmf2, ufunc):
"""Computes the outer product of two PMFs.
pmf1:
pmf2:
ufunc: function to apply to the qs
:return: NumPy array
"""
qs = ufunc.outer(pmf1.qs, pmf2.qs)
ps = np.multiply.outer(pmf1.ps, pmf2.ps)
return qs * ps
def pmf_gt(pmf1, pmf2):
"""Probability that a value from pmf1 is greater than a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.greater)
return outer.sum()
def pmf_lt(pmf1, pmf2):
"""Probability that a value from pmf1 is less than a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.less)
return outer.sum()
def pmf_ge(pmf1, pmf2):
"""Probability that a value from pmf1 is >= than a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.greater_equal)
return outer.sum()
def pmf_le(pmf1, pmf2):
"""Probability that a value from pmf1 is <= than a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.less_equal)
return outer.sum()
def pmf_eq(pmf1, pmf2):
"""Probability that a value from pmf1 equals a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.equal)
return outer.sum()
def pmf_ne(pmf1, pmf2):
"""Probability that a value from pmf1 is <= than a value from pmf2.
pmf1: Pmf object
pmf2: Pmf object
:return: float probability
"""
outer = pmf_outer(pmf1, pmf2, np.not_equal)
return outer.sum()
class Cdf(pd.Series):
"""Represents a Cumulative Distribution Function (CDF)."""
def __init__(self, *args, **kwargs):
"""Initialize a Cdf.
Note: this cleans up a weird Series behavior, which is
that Series() and Series([]) yield different results.
See: https://github.com/pandas-dev/pandas/issues/16737
"""
if args:
super().__init__(*args, **kwargs)
else:
underride(kwargs, dtype=np.float64)
super().__init__([], **kwargs)
def copy(self, deep=True):
"""Make a copy.
:return: new Pmf
"""
return Cdf(self, copy=deep)
@staticmethod
def from_seq(seq, normalize=True, sort=True, **options):
"""Make a CDF from a sequence of values.
seq: any kind of sequence
normalize: whether to normalize the Cdf, default True
sort: whether to sort the Cdf by values, default True
options: passed to the pd.Series constructor
:return: CDF object
"""
pmf = Pmf.from_seq(seq, normalize=False, sort=sort, **options)
return pmf.make_cdf(normalize=normalize)
@property
def qs(self):
"""Get the quantities.
:return: NumPy array
"""
return self.index.values
@property
def ps(self):
"""Get the probabilities.
:return: NumPy array
"""
return self.values
def _repr_html_(self):
"""Returns an HTML representation of the series.
Mostly used for Jupyter notebooks.
"""
df = pd.DataFrame(dict(probs=self))
return df._repr_html_()
def plot(self, **options):
"""Plot the Cdf as a line.
:param options: passed to plt.plot
:return:
"""
underride(options, label=self.name)
plt.plot(self.qs, self.ps, **options)
def step(self, **options):
"""Plot the Cdf as a step function.
:param options: passed to plt.step
:return:
"""
underride(options, label=self.name, where='post')
plt.step(self.qs, self.ps, **options)
def normalize(self):
"""Make the probabilities add up to 1 (modifies self).
:return: normalizing constant
"""
total = self.ps[-1]
self /= total
return total
@property
def forward(self, **kwargs):
"""Compute the forward Cdf
:param kwargs: keyword arguments passed to interp1d
:return array of probabilities
"""
underride(kwargs, kind='previous',
copy=False,
assume_sorted=True,
bounds_error=False,
fill_value=(0, 1))
interp = interp1d(self.qs, self.ps, **kwargs)
return interp
@property
def inverse(self, **kwargs):
"""Compute the inverse Cdf
:param kwargs: keyword arguments passed to interp1d
:return array of quantities
"""
underride(kwargs, kind='next',
copy=False,
assume_sorted=True,
bounds_error=False,
fill_value=(self.qs[0], np.nan))
interp = interp1d(self.ps, self.qs, **kwargs)
return interp
# calling a Cdf like a function does forward lookup
__call__ = forward
# quantile is the same as an inverse lookup
quantile = inverse
def make_pmf(self, normalize=False):
"""Make a Pmf from the Cdf.
:return: Cdf
"""
ps = self.ps
diff = np.ediff1d(ps, to_begin=ps[0])
pmf = Pmf(pd.Series(diff, index=self.index.copy()))
if normalize:
pmf.normalize()
return pmf
def choice(self, *args, **kwargs):
"""Makes a random sample.
Uses the probabilities as weights unless `p` is provided.
args: same as np.random.choice
options: same as np.random.choice
:return: NumPy array
"""
# TODO: Make this more efficient by implementing the inverse CDF method.
pmf = self.make_pmf()
return pmf.choice(*args, **kwargs)
def sample(self, *args, **kwargs):
"""Makes a random sample.
Uses the probabilities as weights unless `weights` is provided.
This function returns an array containing a sample of the quantities in this Pmf,
which is different from Series.sample, which returns a Series with a sample of
the rows in the original Series.
args: same as Series.sample
options: same as Series.sample
:return: NumPy array
"""
# TODO: Make this more efficient by implementing the inverse CDF method.
pmf = self.make_pmf()
return pmf.sample(*args, **kwargs)
def mean(self):
"""Expected value.
:return: float
"""
return self.make_pmf().mean()
def var(self):
"""Variance.
:return: float
"""
return self.make_pmf().var()
def std(self):
"""Standard deviation.
:return: float
"""
return self.make_pmf().std()
def median(self):
"""Median (50th percentile).
:return: float
"""
return self.quantile(0.5)