Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/euro3.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 2012 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
"""This file contains a partial solution to a problem from
11
MacKay, "Information Theory, Inference, and Learning Algorithms."
12
13
Exercise 3.15 (page 50): A statistical statement appeared in
14
"The Guardian" on Friday January 4, 2002:
15
16
When spun on edge 250 times, a Belgian one-euro coin came
17
up heads 140 times and tails 110. 'It looks very suspicious
18
to me,' said Barry Blight, a statistics lecturer at the London
19
School of Economics. 'If the coin were unbiased, the chance of
20
getting a result as extreme as that would be less than 7%.'
21
22
MacKay asks, "But do these data give evidence that the coin is biased
23
rather than fair?"
24
25
"""
26
27
import thinkbayes2
28
29
30
class Euro(thinkbayes2.Suite):
31
"""Represents hypotheses about the probability of heads."""
32
33
def Likelihood(self, data, hypo):
34
"""Computes the likelihood of the data under the hypothesis.
35
36
hypo: integer value of x, the probability of heads (0-100)
37
data: tuple of (number of heads, number of tails)
38
"""
39
x = hypo / 100.0
40
heads, tails = data
41
like = x**heads * (1-x)**tails
42
return like
43
44
45
def TrianglePrior():
46
"""Makes a Suite with a triangular prior."""
47
suite = Euro()
48
for x in range(0, 51):
49
suite.Set(x, x)
50
for x in range(51, 101):
51
suite.Set(x, 100-x)
52
suite.Normalize()
53
return suite
54
55
56
def SuiteLikelihood(suite, data):
57
"""Computes the weighted average of likelihoods for sub-hypotheses.
58
59
suite: Suite that maps sub-hypotheses to probability
60
data: some representation of the data
61
62
returns: float likelihood
63
"""
64
total = 0
65
for hypo, prob in suite.Items():
66
like = suite.Likelihood(data, hypo)
67
total += prob * like
68
return total
69
70
71
def Main():
72
data = 140, 110
73
data = 8, 12
74
75
suite = Euro()
76
like_f = suite.Likelihood(data, 50)
77
print('p(D|F)', like_f)
78
79
actual_percent = 100.0 * 140 / 250
80
likelihood = suite.Likelihood(data, actual_percent)
81
print('p(D|B_cheat)', likelihood)
82
print('p(D|B_cheat) / p(D|F)', likelihood / like_f)
83
84
like40 = suite.Likelihood(data, 40)
85
like60 = suite.Likelihood(data, 60)
86
likelihood = 0.5 * like40 + 0.5 * like60
87
print('p(D|B_two)', likelihood)
88
print('p(D|B_two) / p(D|F)', likelihood / like_f)
89
90
b_uniform = Euro(range(0, 101))
91
b_uniform.Remove(50)
92
b_uniform.Normalize()
93
likelihood = SuiteLikelihood(b_uniform, data)
94
print('p(D|B_uniform)', likelihood)
95
print('p(D|B_uniform) / p(D|F)', likelihood / like_f)
96
97
b_tri = TrianglePrior()
98
b_tri.Remove(50)
99
b_tri.Normalize()
100
likelihood = b_tri.Update(data)
101
print('p(D|B_tri)', likelihood)
102
print('p(D|B_tri) / p(D|F)', likelihood / like_f)
103
104
105
if __name__ == '__main__':
106
Main()
107
108