Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

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