Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/soccer2.py
1901 views
1
"""This file contains code for use with "Think Bayes",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 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 thinkbayes2
12
import thinkplot
13
14
15
class Soccer(thinkbayes2.Suite):
16
"""Represents hypotheses about."""
17
18
def Likelihood(self, data, hypo):
19
"""Computes the likelihood of the data under the hypothesis.
20
21
hypo: goal rate in goals per game
22
data: number of goals scored in a game
23
"""
24
# TODO: fill this in
25
like = 1
26
return like
27
28
def PredictiveDist(self, label='pred'):
29
"""Computes the distribution of goals scored in a game.
30
31
returns: new Pmf (mixture of Poissons)
32
"""
33
# TODO: fill this in
34
lam = 1
35
pred = thinkbayes2.MakePoissonPmf(lam, 15)
36
return pred
37
38
39
def main():
40
hypos = numpy.linspace(0, 12, 201)
41
42
# start with a prior based on a pseudo observation
43
# chosen to yield the right prior mean
44
suite1 = Soccer(hypos, label='Germany')
45
suite1.Update(0.34)
46
suite2 = suite1.Copy(label='Argentina')
47
48
# update with the results of World Cup 2014 final
49
suite1.Update(1)
50
suite2.Update(0)
51
52
print('posterior mean Germany', suite1.Mean())
53
print('posterior mean Argentina', suite2.Mean())
54
55
# plot the posteriors
56
thinkplot.PrePlot(2)
57
thinkplot.Pdfs([suite1, suite2])
58
thinkplot.Show()
59
60
# TODO: compute posterior prob Germany is better than Argentina
61
62
# TODO: compute the Bayes factor of the evidence
63
64
# compute predictive distributions for goals scored in a rematch
65
pred1 = suite1.PredictiveDist(label='Germany')
66
pred2 = suite2.PredictiveDist(label='Argentina')
67
68
# plot the predictive distributions
69
thinkplot.PrePlot(2)
70
thinkplot.Pdfs([pred1, pred2])
71
thinkplot.Show()
72
73
# TODO: compute predictive probability of winning rematch
74
75
76
if __name__ == '__main__':
77
main()
78
79