Skip to content

Commit 7c5aa45

Browse files
committed
Weights should be reset to 1/N after resampling.
1 parent ba61f3a commit 7c5aa45

File tree

7 files changed

+238
-250
lines changed

7 files changed

+238
-250
lines changed

12-Particle-Filters.ipynb

Lines changed: 63 additions & 101 deletions
Large diffs are not rendered by default.

animations/particle_animate.ipynb

Lines changed: 74 additions & 47 deletions
Large diffs are not rendered by default.
-109 KB
Loading

kf_book/gif_animate.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ def init_func():
5757

5858
anim = animation.FuncAnimation(fig, func, init_func=init_func,
5959
frames=frames, interval=interval)
60-
anim.save(filename, writer='imagemagick')
60+
61+
import os
62+
basename = os.path.splitext(filename)[0]
63+
anim.save(basename + '.mp4', writer='ffmpeg')
64+
65+
os.system("ffmpeg -y -i {}.mp4 {}.gif".format(basename, basename))
66+
os.remove(basename + '.mp4')

kf_book/pf_internal.py

Lines changed: 92 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
unicode_literals)
1818

1919

20-
from kf_book.book_plots import figsize, end_interactive
2120
from filterpy.monte_carlo import stratified_resample, residual_resample
2221
import matplotlib as mpl
2322
import matplotlib.pyplot as plt
@@ -88,7 +87,7 @@ def resample(self):
8887
w[i] = self.weights[index]
8988

9089
self.particles = p
91-
self.weights = w / np.sum(w)
90+
self.weights.fill(1.0 / self.N)
9291

9392

9493
def estimate(self):
@@ -311,29 +310,27 @@ def test_pf2():
311310

312311
def plot_cumsum(a):
313312

314-
with figsize(y=2):
315-
fig = plt.figure()
316-
N = len(a)
317-
318-
cmap = mpl.colors.ListedColormap([[0., .4, 1.],
319-
[0., .8, 1.],
320-
[1., .8, 0.],
321-
[1., .4, 0.]]*(int(N/4) + 1))
322-
cumsum = np.cumsum(np.asarray(a) / np.sum(a))
323-
cumsum = np.insert(cumsum, 0, 0)
324-
325-
#fig = plt.figure(figsize=(6,3))
326-
fig=plt.gcf()
327-
ax = fig.add_axes([0.05, 0.475, 0.9, 0.15])
328-
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
329-
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
330-
norm=norm,
331-
drawedges=False,
332-
spacing='proportional',
333-
orientation='horizontal')
334-
if N > 10:
335-
bar.set_ticks([])
336-
end_interactive(fig)
313+
fig = plt.figure()
314+
N = len(a)
315+
316+
cmap = mpl.colors.ListedColormap([[0., .4, 1.],
317+
[0., .8, 1.],
318+
[1., .8, 0.],
319+
[1., .4, 0.]]*(int(N/4) + 1))
320+
cumsum = np.cumsum(np.asarray(a) / np.sum(a))
321+
cumsum = np.insert(cumsum, 0, 0)
322+
323+
#fig = plt.figure(figsize=(6,3))
324+
fig=plt.gcf()
325+
ax = fig.add_axes([0.05, 0.475, 0.9, 0.15])
326+
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
327+
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
328+
norm=norm,
329+
drawedges=False,
330+
spacing='proportional',
331+
orientation='horizontal')
332+
if N > 10:
333+
bar.set_ticks([])
337334

338335

339336
def plot_stratified_resample(a):
@@ -346,24 +343,22 @@ def plot_stratified_resample(a):
346343
cumsum = np.cumsum(np.asarray(a) / np.sum(a))
347344
cumsum = np.insert(cumsum, 0, 0)
348345

349-
with figsize(y=2):
350-
fig = plt.figure()
351-
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
352-
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
353-
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
354-
norm=norm,
355-
drawedges=False,
356-
spacing='proportional',
357-
orientation='horizontal')
358-
xs = np.linspace(0., 1.-1./N, N)
359-
ax.vlines(xs, 0, 1, lw=2)
360-
361-
# make N subdivisions, and chose a random position within each one
362-
b = (random(N) + range(N)) / N
363-
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
364-
bar.set_ticks([])
365-
plt.title('stratified resampling')
366-
end_interactive(fig)
346+
fig = plt.figure()
347+
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
348+
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
349+
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
350+
norm=norm,
351+
drawedges=False,
352+
spacing='proportional',
353+
orientation='horizontal')
354+
xs = np.linspace(0., 1.-1./N, N)
355+
ax.vlines(xs, 0, 1, lw=2)
356+
357+
# make N subdivisions, and chose a random position within each one
358+
b = (random(N) + range(N)) / N
359+
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
360+
bar.set_ticks([])
361+
plt.title('stratified resampling')
367362

368363

369364
def plot_systematic_resample(a):
@@ -376,24 +371,22 @@ def plot_systematic_resample(a):
376371
cumsum = np.cumsum(np.asarray(a) / np.sum(a))
377372
cumsum = np.insert(cumsum, 0, 0)
378373

379-
with figsize(y=2):
380-
fig = plt.figure()
381-
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
382-
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
383-
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
384-
norm=norm,
385-
drawedges=False,
386-
spacing='proportional',
387-
orientation='horizontal')
388-
xs = np.linspace(0., 1.-1./N, N)
389-
ax.vlines(xs, 0, 1, lw=2)
390-
391-
# make N subdivisions, and chose a random position within each one
392-
b = (random() + np.array(range(N))) / N
393-
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
394-
bar.set_ticks([])
395-
plt.title('systematic resampling')
396-
end_interactive(fig)
374+
fig = plt.figure()
375+
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
376+
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
377+
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
378+
norm=norm,
379+
drawedges=False,
380+
spacing='proportional',
381+
orientation='horizontal')
382+
xs = np.linspace(0., 1.-1./N, N)
383+
ax.vlines(xs, 0, 1, lw=2)
384+
385+
# make N subdivisions, and chose a random position within each one
386+
b = (random() + np.array(range(N))) / N
387+
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
388+
bar.set_ticks([])
389+
plt.title('systematic resampling')
397390

398391

399392
def plot_multinomial_resample(a):
@@ -406,22 +399,20 @@ def plot_multinomial_resample(a):
406399
cumsum = np.cumsum(np.asarray(a) / np.sum(a))
407400
cumsum = np.insert(cumsum, 0, 0)
408401

409-
with figsize(y=2):
410-
fig = plt.figure()
411-
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
412-
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
413-
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
414-
norm=norm,
415-
drawedges=False,
416-
spacing='proportional',
417-
orientation='horizontal')
418-
419-
# make N subdivisions, and chose a random position within each one
420-
b = random(N)
421-
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
422-
bar.set_ticks([])
423-
plt.title('multinomial resampling')
424-
end_interactive(fig)
402+
fig = plt.figure()
403+
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
404+
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
405+
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
406+
norm=norm,
407+
drawedges=False,
408+
spacing='proportional',
409+
orientation='horizontal')
410+
411+
# make N subdivisions, and chose a random position within each one
412+
b = random(N)
413+
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
414+
bar.set_ticks([])
415+
plt.title('multinomial resampling')
425416

426417

427418
def plot_residual_resample(a):
@@ -436,34 +427,36 @@ def plot_residual_resample(a):
436427
[1., .8, 0.],
437428
[1., .4, 0.]]*(int(N/4) + 1))
438429

439-
with figsize(y=2):
440-
fig = plt.figure()
441-
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
442-
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
443-
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
444-
norm=norm,
445-
drawedges=False,
446-
spacing='proportional',
447-
orientation='horizontal')
448-
449-
indexes = residual_resample(a_norm)
450-
bins = np.bincount(indexes)
451-
for i in range(1, N):
452-
n = bins[i-1] # number particles in this sample
453-
if n > 0:
454-
b = np.linspace(cumsum[i-1], cumsum[i], n+2)[1:-1]
455-
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
456-
bar.set_ticks([])
457-
plt.title('residual resampling')
458-
end_interactive(fig)
430+
fig = plt.figure()
431+
ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15])
432+
norm = mpl.colors.BoundaryNorm(cumsum, cmap.N)
433+
bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
434+
norm=norm,
435+
drawedges=False,
436+
spacing='proportional',
437+
orientation='horizontal')
438+
439+
indexes = residual_resample(a_norm)
440+
bins = np.bincount(indexes)
441+
for i in range(1, N):
442+
n = bins[i-1] # number particles in this sample
443+
if n > 0:
444+
b = np.linspace(cumsum[i-1], cumsum[i], n+2)[1:-1]
445+
plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k')
446+
bar.set_ticks([])
447+
plt.title('residual resampling')
448+
459449

460450

461451
if __name__ == '__main__':
462-
plot_residual_resample([.1, .2, .3, .4, .2, .3, .1])
452+
453+
show_two_pf_plots()
454+
455+
#plot_residual_resample([.1, .2, .3, .4, .2, .3, .1])
463456

464457
#example()
465458
#show_two_pf_plots()
466459

467-
a = [.1, .2, .1, .6]
460+
#a = [.1, .2, .1, .6]
468461
#plot_cumsum(a)
469462
#test_pf()

pdf/merge_book.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def merge_notebooks(outfile, filenames):
2424
for fname in filenames:
2525
with io.open(fname, 'r', encoding='utf-8') as f:
2626
nb = nbformat.read(f, nbformat.NO_CONVERT)
27-
remove_formatting(nb)
27+
#remove_formatting(nb)
2828
if not added_appendix and fname[0:8] == 'Appendix':
2929
remove_links_add_appendix(nb)
3030
added_appendix = True

pdf/run_notebooks.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mkdir tmp
22
copy ..\*.ipynb .\tmp
33
copy ..\*.py .\tmp
4-
cp -r ..\code\ .\tmp\code\
4+
cp -r ..\kf_book\ .\tmp\kf_book\
55

66
cd tmp
77

0 commit comments

Comments
 (0)