-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2.py
More file actions
60 lines (54 loc) · 1.57 KB
/
Copy pathp2.py
File metadata and controls
60 lines (54 loc) · 1.57 KB
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
import matplotlib.pyplot as plt
import numpy as np
# q=[0,0,0,0,0]
def sense(p,world,pHit,pMiss):
q=np.zeros(len(p))
size=int(len(p))
obs = np.zeros(size)
for i in range(size):
if "red"==world[i]:
hit=1
else:
hit=0
obs[i]=hit*pHit+(1-hit)*pMiss
q[i]=p[i]*(hit*pHit+(1-hit)*pMiss)
# q.index(q[i])
S=sum(q)
for i in range(size):
q[i]=q[i]/S
print(q)
plt.figure('The Prior distribution')
plt.bar(x=(0,1,2,3,4),height=p,color='g')
plt.ylabel("Probability of being at the positon")
plt.xlabel("Position")
plt.ylim(0,0.5)
plt.figure('Observation model')
plt.bar(x=(0, 1, 2, 3, 4), height=obs, color='b')
plt.ylabel("Probability of being at the positon")
plt.xlabel("Position")
plt.ylim(0, 0.9)
plt.figure('The Posterior distribution')
plt.bar(x=(0, 1, 2, 3, 4), height=q, color='red')
plt.ylabel("Probability of being at the positon")
plt.xlabel("Position")
plt.ylim(0, 0.5)
plt.figure('Plot all')
plt.subplot(311)
plt.bar(x=(0,1,2,3,4),height=p,color='red')
plt.ylabel('Probability')
plt.ylim(0,0.5)
plt.subplot(312)
plt.bar(x=(0,1,2,3,4),height=obs)
plt.ylabel('Likelihood')
plt.ylim(0, 0.9)
plt.subplot(313)
plt.bar(x=(0,1,2,3,4),height=q)
plt.xlabel('Position')
plt.ylabel('Probability')
plt.ylim(0,0.5)
plt.show()
world=["green","red","red","green","green"]
p=[0.2,0.2,0.2,0.2,0.2]
pHit=0.6
pMiss=0.2
sense(p,world,pHit,pMiss)