Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Repository for a workshop on Bayesian statistics

1430 views
1
"""This file contains code used in "Think Stats",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2015 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function, division
9
10
import numpy
11
import thinkbayes
12
import thinkplot
13
14
15
"""
16
This problem presents a solution to the "Bayesian Billiards Problem",
17
presented in this video:
18
19
https://www.youtube.com/watch?v=KhAUfqhLakw
20
21
Based on the formulation in this paper:
22
23
http://www.nature.com/nbt/journal/v22/n9/full/nbt0904-1177.html
24
25
Of a problem originally posed by Bayes himself.
26
"""
27
28
class Billiards(thinkbayes.Suite):
29
30
def Likelihood(self, data, hypo):
31
"""Computes the likelihood of the data under the hypothesis.
32
33
data: tuple (#wins, #losses)
34
hypo: float probability of win
35
"""
36
p = hypo
37
win, lose = data
38
like = p**win * (1-p)**lose
39
return like
40
41
42
def ProbWinMatch(pmf):
43
total = 0
44
for p, prob in pmf.Items():
45
total += prob * (1-p)**3
46
return total
47
48
49
def main():
50
ps = numpy.linspace(0, 1, 101)
51
bill = Billiards(ps)
52
bill.Update((5, 3))
53
thinkplot.Pdf(bill)
54
thinkplot.Save(root='billiards1',
55
xlabel='probability of win',
56
ylabel='PDF',
57
formats=['png'])
58
59
bayes_result = ProbWinMatch(bill)
60
print(thinkbayes.Odds(1-bayes_result))
61
62
mle = 5 / 8
63
freq_result = (1-mle)**3
64
print(thinkbayes.Odds(1-freq_result))
65
66
67
if __name__ == '__main__':
68
main()
69
70