Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/gps_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
from itertools import product
15
16
17
class Gps(thinkbayes2.Suite, thinkbayes2.Joint):
18
"""Represents hypotheses about your location in the field."""
19
20
def Likelihood(self, data, hypo):
21
"""Computes the likelihood of the data under the hypothesis.
22
23
hypo:
24
data:
25
"""
26
std = 30
27
meanx, meany = hypo
28
x, y = data
29
like = thinkbayes2.EvalNormalPdf(x, meanx, std)
30
like *= thinkbayes2.EvalNormalPdf(y, meany, std)
31
return like
32
33
34
def main():
35
coords = numpy.linspace(-100, 100, 101)
36
joint = Gps(product(coords, coords))
37
38
joint.Update((51, -15))
39
joint.Update((48, 90))
40
41
pairs = [(11.903060613102866, 19.79168669735705),
42
(77.10743601503178, 39.87062906535289),
43
(80.16596823095534, -12.797927542984425),
44
(67.38157493119053, 83.52841028148538),
45
(89.43965206875271, 20.52141889230797),
46
(58.794021026248245, 30.23054016065644),
47
(2.5844401241265302, 51.012041625783766),
48
(45.58108994142448, 3.5718287379754585)]
49
50
joint.UpdateSet(pairs)
51
52
thinkplot.PrePlot(2)
53
pdfx = joint.Marginal(0)
54
pdfy = joint.Marginal(1)
55
thinkplot.Pdf(pdfx, label='posterior x')
56
thinkplot.Pdf(pdfy, label='posterior y')
57
thinkplot.Show()
58
59
print(pdfx.Mean(), pdfx.Std())
60
print(pdfy.Mean(), pdfy.Std())
61
62
63
if __name__ == '__main__':
64
main()
65
66