# Sec. 9.5 Sample Excersize
import numpy as np
def sampling(num, dist):
total=sum(dist)
if (total > 1.0):
dist= [x/total for x in dist]
return np.random.multinomial(num, dist)
def resampling(dist, weight):
# dist = [2,1,0,1,5,1], weight=[0.17, 0.09, 0.0, 0.09, 0.57, 0.8]
total = sum(weight)
new_weight = [x/total for x in weight]
samples = np.random.multinomial(sum(dist), new_weight)
return samples
def stay(dist):
return dist
def moveRight(dist):
new_dist = []
prev = 0.0
for current in dist:
new_dist.append(prev * 0.8 + current * 0.2)
prev = current
new_dist[-1] += current*0.8
return sampling(sum(dist),new_dist)
def observationWeightP(dist): # passage
nOfRooms = len(dist)
return [0.02]+[0.70]*(nOfRooms - 2) + [0.02]
def observationWeightC(dist): # rightCorner
nOfRooms = len(dist)
return [0.02]*(nOfRooms - 1) + [0.70]
def calcWeight(dist, weight):
return list(map(lambda x,y: x*y, dist, weight))
def test():
# test run
num_particles = 10
num_rooms = 5
initial = [int(num_particles/num_rooms)]*num_rooms
print("initial=",initial)
dist1 = moveRight(initial)
print ("after moving right =", dist1)
#
weight1 = calcWeight(dist1, observationWeightP(dist1))
print ("observing Passage (weight)=",[float(format(x,'.2f')) for x in weight1])
dist2 = resampling(dist1, weight1)
print ("Resampled =",dist2)
#
dist3 = moveRight(dist2)
print("after moving right again =",dist3)
weight2 = calcWeight(dist3, observationWeightC(dist3))
print("observing Right Corner (weight) =", [float(format(x,'.2f')) for x in weight2])
dist4 = resampling(dist3, weight2)
print ("Resampled = ",dist4)
#################################################
# Exercise 9-2 - 9-4
def observationWeightR(dist): # passage
nOfRooms = len(dist)
return [0.02]*6+[0.70]+ [0.02]*3
def observationWeightX(dist): # none
return [0.02]*len(dist)
def moveRightR(dist):
new_dist = [0.0]*10
for current in range(10):
if current in [0,4,7]:
new_dist[current]=dist[current] * 0.2
else:
prev = dist[current-1]
if current in [3,6,9]:
new_dist[current]=prev * 0.8 + dist[current]
else:
new_dist[current]=prev * 0.8 + dist[current] * 0.2
#
return sampling(sum(dist),new_dist)
def moveLeftR(dist):
new_dist = [0.0]*10
for current in range(10):
if current in [3,6,9]:
new_dist[current]=dist[current] * 0.2
else:
prev = dist[current+1]
if current in [0,4,7]:
new_dist[current]=prev * 0.8 + dist[current]
else:
new_dist[current]=prev * 0.8 + dist[current] * 0.2
#
return sampling(sum(dist),new_dist)
def oneLine(lst,left,right):
print("\t"+left+"\t",end="")
for x in lst:
print(format(x,'.2f'),end="\t")
print(right)
def roomShowing(dist):
oneLine(dist[0:4],"[","")
oneLine(dist[4:7],"","")
oneLine(dist[7:],"","]")
def ex9():
num_particles = 20
num_rooms = 10
initial = [int(num_particles/num_rooms)]*num_rooms
print("initial:")
roomShowing(initial)
# dist1 = moveRightR(initial)
dist1 = stay(initial)
print ("After staying:")
roomShowing(dist1)
#
weight1 = calcWeight(dist1, observationWeightR(dist1))
print("Observing right wall (weight):")
roomShowing(weight1)
dist2 = resampling(dist1, weight1)
print ("Resampled = ")
roomShowing(dist2)
#
dist3 = moveLeftR(dist2)
print ("After moving to left:")
roomShowing([round(x,3) for x in dist3])
#
weight3 = calcWeight(dist3, observationWeightX(dist3))
print("Observing closed room (weight):")
roomShowing(weight3)
dist4 = resampling(dist3, weight3)
print ("Resampled")
roomShowing(dist4)
if (__name__=='__main__'):
test()
print("\n----------------\n")
ex9()