Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/hyrax_soln.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 numpy
11
import thinkbayes2
12
import thinkplot
13
14
15
class Hyrax(thinkbayes2.Suite):
16
"""Represents hypotheses about how many hyraxes there are."""
17
18
def Likelihood(self, data, hypo):
19
"""Computes the likelihood of the data under the hypothesis.
20
21
hypo: total population
22
data: # tagged, # caught (n), # of caught who were tagged (k)
23
"""
24
tagged, n, k = data
25
if hypo < tagged + n - k:
26
return 0
27
28
p = tagged / hypo
29
like = thinkbayes2.EvalBinomialPmf(k, n, p)
30
return like
31
32
33
class Hyrax2(thinkbayes2.Suite):
34
"""Represents hypotheses about how many hyraxes there are."""
35
36
def Likelihood(self, data, hypo):
37
"""Computes the likelihood of the data under the hypothesis.
38
39
hypo: total population (N)
40
data: # tagged (K), # caught (n), # of caught who were tagged (k)
41
"""
42
N = hypo
43
K, n, k = data
44
45
if hypo < K + (n - k):
46
return 0
47
48
like = thinkbayes2.EvalHypergeomPmf(k, N, K, n)
49
return like
50
51
52
def main():
53
hypos = range(1, 1000)
54
suite = Hyrax(hypos)
55
suite2 = Hyrax2(hypos)
56
57
data = 10, 10, 2
58
suite.Update(data)
59
suite2.Update(data)
60
61
thinkplot.Pdf(suite, label='binomial')
62
thinkplot.Pdf(suite, label='hypergeom')
63
thinkplot.Show()
64
65
66
if __name__ == '__main__':
67
main()
68
69