forked from AllenDowney/ThinkBayes2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps_soln.py
64 lines (47 loc) · 1.68 KB
/
gps_soln.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""This file contains code for use with "Think Bayes",
by Allen B. Downey, available from greenteapress.com
Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from __future__ import print_function, division
import numpy
import thinkbayes2
import thinkplot
from itertools import product
class Gps(thinkbayes2.Suite, thinkbayes2.Joint):
"""Represents hypotheses about your location in the field."""
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo:
data:
"""
std = 30
meanx, meany = hypo
x, y = data
like = thinkbayes2.EvalNormalPdf(x, meanx, std)
like *= thinkbayes2.EvalNormalPdf(y, meany, std)
return like
def main():
coords = numpy.linspace(-100, 100, 101)
joint = Gps(product(coords, coords))
joint.Update((51, -15))
joint.Update((48, 90))
pairs = [(11.903060613102866, 19.79168669735705),
(77.10743601503178, 39.87062906535289),
(80.16596823095534, -12.797927542984425),
(67.38157493119053, 83.52841028148538),
(89.43965206875271, 20.52141889230797),
(58.794021026248245, 30.23054016065644),
(2.5844401241265302, 51.012041625783766),
(45.58108994142448, 3.5718287379754585)]
joint.UpdateSet(pairs)
thinkplot.PrePlot(2)
pdfx = joint.Marginal(0)
pdfy = joint.Marginal(1)
thinkplot.Pdf(pdfx, label='posterior x')
thinkplot.Pdf(pdfy, label='posterior y')
thinkplot.Show()
print(pdfx.Mean(), pdfx.Std())
print(pdfy.Mean(), pdfy.Std())
if __name__ == '__main__':
main()