Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96160
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
from thinkbayes import Suite
9
10
11
class M_and_M(Suite):
12
"""Map from hypothesis (A or B) to probability."""
13
14
mix94 = dict(brown=30,
15
yellow=20,
16
red=20,
17
green=10,
18
orange=10,
19
tan=10,
20
blue=0)
21
22
mix96 = dict(blue=24,
23
green=20,
24
orange=16,
25
yellow=14,
26
red=13,
27
brown=13,
28
tan=0)
29
30
hypoA = dict(bag1=mix94, bag2=mix96)
31
hypoB = dict(bag1=mix96, bag2=mix94)
32
33
hypotheses = dict(A=hypoA, B=hypoB)
34
35
def Likelihood(self, data, hypo):
36
"""Computes the likelihood of the data under the hypothesis.
37
38
hypo: string hypothesis (A or B)
39
data: tuple of string bag, string color
40
"""
41
bag, color = data
42
mix = self.hypotheses[hypo][bag]
43
like = mix[color]
44
return like
45
46
47
def main():
48
suite = M_and_M('AB')
49
50
suite.Update(('bag1', 'yellow'))
51
suite.Update(('bag2', 'green'))
52
53
suite.Print()
54
55
56
if __name__ == '__main__':
57
main()
58
59