Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/cookie3.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 thinkbayes2
11
12
13
class Cookie(thinkbayes2.Suite):
14
"""Suite to represent bowls of cookies."""
15
16
def Likelihood(self, data, hypo):
17
"""The likelihood of the data under the hypothesis.
18
19
data: string cookie type
20
hypo: Hist of cookies
21
"""
22
# compute the likelihood with the current bowls
23
like = hypo[data] / hypo.Total()
24
25
# update the bowl
26
if like:
27
hypo[data] -= 1
28
29
return like
30
31
32
def main():
33
# use Hists to represent the contents of the bowls
34
bowl1 = thinkbayes2.Hist(dict(vanilla=30, chocolate=10))
35
bowl2 = thinkbayes2.Hist(dict(vanilla=20, chocolate=20))
36
37
# instantiate the suite
38
suite = Cookie([bowl1, bowl2])
39
40
print('After 1 vanilla')
41
suite.Update('vanilla')
42
for hypo, prob in suite.Items():
43
print(hypo, prob)
44
45
print('\nAfter 1 vanilla, 1 chocolate')
46
suite.Update('chocolate')
47
for hypo, prob in suite.Items():
48
print(hypo, prob)
49
50
print('\nAfter 1 vanilla, 1 chocolate, 1 vanilla')
51
suite.Update('vanilla')
52
for hypo, prob in suite.Items():
53
print(hypo, prob)
54
55
56
if __name__ == '__main__':
57
main()
58
59