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