17
17
unicode_literals )
18
18
19
19
20
- from kf_book .book_plots import figsize , end_interactive
21
20
from filterpy .monte_carlo import stratified_resample , residual_resample
22
21
import matplotlib as mpl
23
22
import matplotlib .pyplot as plt
@@ -88,7 +87,7 @@ def resample(self):
88
87
w [i ] = self .weights [index ]
89
88
90
89
self .particles = p
91
- self .weights = w / np . sum ( w )
90
+ self .weights . fill ( 1.0 / self . N )
92
91
93
92
94
93
def estimate (self ):
@@ -311,29 +310,27 @@ def test_pf2():
311
310
312
311
def plot_cumsum (a ):
313
312
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 ([])
337
334
338
335
339
336
def plot_stratified_resample (a ):
@@ -346,24 +343,22 @@ def plot_stratified_resample(a):
346
343
cumsum = np .cumsum (np .asarray (a ) / np .sum (a ))
347
344
cumsum = np .insert (cumsum , 0 , 0 )
348
345
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' )
367
362
368
363
369
364
def plot_systematic_resample (a ):
@@ -376,24 +371,22 @@ def plot_systematic_resample(a):
376
371
cumsum = np .cumsum (np .asarray (a ) / np .sum (a ))
377
372
cumsum = np .insert (cumsum , 0 , 0 )
378
373
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' )
397
390
398
391
399
392
def plot_multinomial_resample (a ):
@@ -406,22 +399,20 @@ def plot_multinomial_resample(a):
406
399
cumsum = np .cumsum (np .asarray (a ) / np .sum (a ))
407
400
cumsum = np .insert (cumsum , 0 , 0 )
408
401
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' )
425
416
426
417
427
418
def plot_residual_resample (a ):
@@ -436,34 +427,36 @@ def plot_residual_resample(a):
436
427
[1. , .8 , 0. ],
437
428
[1. , .4 , 0. ]]* (int (N / 4 ) + 1 ))
438
429
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
+
459
449
460
450
461
451
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])
463
456
464
457
#example()
465
458
#show_two_pf_plots()
466
459
467
- a = [.1 , .2 , .1 , .6 ]
460
+ # a = [.1, .2, .1, .6]
468
461
#plot_cumsum(a)
469
462
#test_pf()
0 commit comments