<<

ECE5550: Applied Kalman Filtering 7–1

PARTICLE FILTERS1

7.1: Numeric integration to solve Bayesian recursion

■ Recall from Chap. 4 that the optimal Bayesian recursion is to:

¯ Compute the pdf for predicting xk given all past observations

j Z D j j Z

f (xk k 1) f (xk xk 1) f (xk 1 k 1) dxk 1. Rx Z k 1

¯ Update the pdf for estimating xk given all observations via

j j Z

f (zk xk) f (xk k 1) Z D

f (xk j k) .

j Z

f (zk k 1) ■ If we knew how to compute these pdfs, then we could find any desired ; e.g., , , , whatever.

■ So far, we have assumed that computing these pdfs is intractable, so have made approximations to arrive at the KF, EKF, and SPKF.

■ We now revisit this assumption.

Numeric integration

■ The prediction step requires evaluating an integral.

■ Z

Normalization of f (xk j k) via its denominator requires an integral

j Z D j j Z f (zk k 1) f (zk xk) f (xk k 1) dxk. R Z xk 1 The contents of this chapter of notes, including the MATLAB examples, are very closely based on the excellent course materials and lecture videos prepared by Prof. James McNames at Portland State University for “ECE 510 State Space Tracking,” and are used with his permission and my gratitude.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–2

■ If state dimension n is large (e.g., n  3), evaluation of these multi- dimensional integrals may be impractical, even with modern CPUs.

■ Computation needed for a given precision scales exponentially with n.

¯ The particle filter will explore some clever ways to avoid this issue.

¯ But, first, we look at numeric integration for low-order problems.

Rectangular integration ■ Suppose that we wish to evaluate 5 the definite integral 4 3 xmax )

x 2 D integral g(x) dx. ( x g Z min 1 ■ The rectangular rule approximates 0

−1 g(x) as piecewise constant. 0 1 2 3 4 5 6 7 8 9 10 x

■ Let Nr be the number of constant regions.

Then, the width of each region is w D (xmax xmin)/Nr , and the integral can be approximated as

Nr 1

1

¡ C C integral  w g(xmin (i /2)w).

i 0 X ■ The trapezoidal rule approximates Trapezoidal integration the function as piecewise linear. 5 4 ■ The integral approximation is 3 )

x 2 ( g(xmin) C g(xmax) g

integral  w 2 1  0

Nr 1

−1 C C g(x iw) . 0 1 2 3 4 5 6 7 8 9 10 min x

i 1 X 

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–3 Application to

■ Can write prediction step as

xmax

j Z D j j Z

f (xk k 1) f (xk xk 1) f (xk 1 k 1) dxk 1 Zxmin

xmax

D Á Z

g(x xk, k 1) dx

Zxmin

 Á Z C Á Z w g(xmin xk, k 1) g(xmax xk, k 1) /2

 Nr 1 

C C Á Z

w g(xmin iw xk, k 1).

i X1 ■ If we can compute this integral for every value of xk, can directly Z estimate posterior distribution f (xk j k).

■ Limiting factor is precision vs. speed: Each integral requires O(Nr ) O 2 calculations at each of Nr evaluation points, or (Nr ) operations.

TRACKINGEXAMPLE: We wish to track time-varying xk for model

D C

xk 1 αxk wk

3 C zk D 0.1xk vk

2 2   N N where wk (0,σw) and vk (0,σv ) are mutually uncorrelated and IID.

■ For the prediction step, we numerically integrate to approximate

j Z D j j Z

f (xk k 1) f (xk xk 1) f (xk 1 k 1) dxk 1. Rx Z k 1

■ For the time-update step, we directly compute

j j Z

f (zk xk) f (xk k 1) Z D

f (xk j k) .

j Z

f (zk k 1)

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–4 ■ Note that the process model gives us

2 

j N f (xk xk 1) (αxk 1,σw) and the measurement model gives us

3 2  j N f (zk xk) (0.1xk ,σv ).

■ 2

D D  D N For this example, let σw 0.2, σv 0.1, α 0.99, and x0 (0,σw).

■ Z

Note that f (x0 j 0) needs to be specified, where

j j Z

f (z0 x0) f (x0 1) Z D

f (x0 j 0) .

j Z

f (z0 1)

■ 2

Z D 

Z j N Since 1 is unknown, we assume f (x0 1) f (x0) (0,σw).

■ Simulation run for 200 samples, of integration from 3 to 3 with

Nr D 500 regions.

■ Z If we are able to find/approximate f (xk j k), we can compute mean, median, mode, whatever.

¯ Can plot these point estimates as functions of time.

¯ Alternately, can plot the sequence of distributions as a pseudo- color image (black is low probability; white is high probability). Z Signals for tracking example f (xk k) 1 0.5 −3

0 −2 0 −0.5 −1 True Mode k k k

−1 −1 z x 0 Mean x Median −1.5 1 95% conf. −2 True state xk −2 2 Measured zk

−3 −2.5 3 0 50 100 150 200 0 50 100 150 200 Iteration k Iteration k

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–5 % Code originally based on AngleTrackingExample.m by James McNames % of Portland State University

clear; close all;

% Define User-Specified Parameters nSamples = 201; xMin = -3; xMax = 3;

measurementNoiseSigma = 0.1; processNoiseSigma = 0.2; nRegions = 500; alpha = 0.99;

% Create Sequence of Observations from Model x = zeros(nSamples+1,1); % time [0..nSamples] z = zeros(nSamples ,1); % time [0..nSamples-1]

x(1) = randn(1)*processNoiseSigma; % initialize x{0} ~ N(0,sigmaw^2) for k=1:nSamples x(k+1) = alpha*x(k) + randn(1)*processNoiseSigma; z(k ) = 0.1*x(k)^3 + randn(1)*measurementNoiseSigma; end x = x(1:nSamples); % constrain to [0..nSamples-1]

% Recursively Estimate the Marginal Posterior xIntegration = linspace(xMin,xMax,nRegions).'; width = mean(diff(xIntegration));

posteriorFiltered = zeros(nRegions,nSamples); posteriorPredicted = zeros(nRegions,1); xHatMedian = zeros(nSamples,1); xLower = zeros(nSamples,1); xUpper = zeros(nSamples,1);

% initialize pdf for f(x{0}) prior = normpdf(xIntegration,0,processNoiseSigma); % compute f(z{0}|x{0})*f(x{0}|Z{-1}) = f(z{0}|x{0})*f(x{0}) posteriorFiltered(:,1) = normpdf(z(1),0.1*xIntegration.^3,... measurementNoiseSigma).*prior; % compute f(x{0}|Z{0})=f(z{0}|x{0})*f(x{0}|Z{-1})/[normalizing cst]

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–6 posteriorFiltered(:,1) = posteriorFiltered(:,1)/trapz(xIntegration,... posteriorFiltered(:,1)); for k=2:nSamples for cRegion=1:nRegions % find f(x{k}=x{i}|x{k-1}) for each x{i} % compute f(x{k}|x{k-1}) prior = normpdf(xIntegration(cRegion),alpha*xIntegration,... processNoiseSigma); % compute f(x{k}|Z{k-1})=integral[ f(x{k}|x{k-1})*f(x{k-1}|Z{k-1}) ] posteriorPredicted(cRegion) = trapz(xIntegration,... prior.*posteriorFiltered(:,k-1)); end % compute f(z{k}|x{k}) likelihood = normpdf(z(k),0.1*xIntegration.^3,measurementNoiseSigma); % compute f(x{k}|Z{k-1})*f(z{k}|x{k}) posteriorFiltered(:,k) = likelihood.*posteriorPredicted; % normalize to get f(x{k}|Z{k}) posteriorFiltered(:,k) = posteriorFiltered(:,k)/trapz(xIntegration,... posteriorFiltered(:,k));

% Find median, 95% = cumtrapz(xIntegration,posteriorFiltered(:,k)); iMedian = find(percentiles <= 0.5,1,'last'); iLower = find(percentiles <= 0.025,1,'last'); if isempty(iLower), iLower = 1; end; iUpper = find(percentiles >= 0.975,1,'first'); if isempty(iUpper), iUpper = length(xIntegration); end xHatMedian(k) = xIntegration(iMedian); xLower(k) = xIntegration(iLower); xUpper(k) = xIntegration(iUpper); end

[~,iMax] = max(posteriorFiltered); xHatMode = xIntegration(iMax); xHatMean = xIntegration'*posteriorFiltered.*width;

% Plot true state and observed sequence figure(10); clf; ax = plotyy(0:nSamples-1,x,0:nSamples-1,z); legend('True state x_k','Measured z_k'); ylabel(ax(1),'x_k'); ylabel(ax(2),'z_k'); title('Signals for tracking example'); xlabel('Iteration k');

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–7

% Plot The Filtered Posterior figure(3); clf; colormap('bone') imagesc(0:nSamples-1,xIntegration,posteriorFiltered); hold on caxis([0,prctile(posteriorFiltered(:),95)]); % more white on plot hp = plot(0:nSamples-1,x,'.',0:nSamples-1,xHatMode,'.',... 0:nSamples-1,xHatMean,'.',0:nSamples-1,xHatMedian,'.',... [0:nSamples-1,0:nSamples-1],[xUpper; xLower],'.'); set(hp,'markersize',12)

xlabel('Iteration k'); ylabel('x_k'); title('Posterior probability of x'); xlim([0 nSamples-1]); ylim([xMin xMax]); legend(hp,{'True','Mode','Mean','Median','95% conf.'},... 'location','eastoutside');

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–8 7.2: Monte-Carlo integration and the importance density

■ Finding the optimal solution via numeric integration is usually not practical since it requires operation over a high-dimensional space.

■ We need to find a way to beat the “curse of dimensionality” and get good approximate results using less computation.

THEPROBLEM: Consider the problem of numerically integrating g(x), where we can evaluate g(x) at any point x that we like:

µ D g(x) dx. Z A POSSIBLE SOLUTION: Monte-Carlo methods “factor” g(x) and write g(x)

µ D f (x) dx, f (x) Z   where f (x) is interpreted as a pdf.

■ E Then, the integral can be interpreted as µ D [g(x)/f (x)].

■ Why does this help? The key idea is to write the integral as an expectation, then approximate the expectation with a sample mean based on from the known pdf f (x).

■ That is, suppose we draw N  1 random samples from pdf f (x). Then,

g(x) g(x) 1 N g(x (i))

D E  µ D f (x) dx , f (x) f (x) N f (x (i))

i 1 Z     X where x (i) is the ith sample drawn from f (x), not x to the power i.

■ If the samples are independent, then the estimate is unbiased and ½ will “almost surely” converge to µ as N ! .

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–9 ■ Can choose f (x) however we like, as long as its support is a superset of the support of g(x).

¯ Can choose f (x) uniform, Gaussian, whatever;

¯ Doesn’t influence convergence; does affect rate of convergence.

EXAMPLE: Suppose we are given a random process yk D cos(vk) where 2

 N vk (0,σv ) and we wish to estimate

2

1 v E [y] D cos(v) exp dv. 2 2

2πσ 2σ Z v  v  ■ We draw Gaussian RVs with zero mean and σv p Monte•Carlo integration and compute 1 N 0.8 1 i ( ) 0.6 cos(v ). ] y

N [ E i 1 X 0.4 0.2 N = 10000; sv = 0.75; True mean v = sv randn(1,N); y = cos(v); Estimate * 0 Ey = cumsum(y)./(1:N); plot(1:N,Ey); 0 2000 4000 6000 8000 10000 Sample number

■ 2 The “true mean” can be found as exp( σv /2), or numerically. ■ We see quite fast convergence to the neighborhood of the solution.

Reconsidering our problem

■ In the prediction step, the integral we need to evaluate is already in

the right form. That is,

j Z D j j Z

f (xk k 1) f (xk xk 1) f (xk 1 k 1) dxk 1 Rx Z k 1

is in the form

D

something fn(xk 1) f (xk 1) dxk 1. Z Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–10

■ To use a Monte-Carlo method to compute the integral, we need to

j Z draw random numbers from the posterior distribution f (xk 1 k 1).

¯ Problem: Unless this pdf is uniform or Gaussian or some other well-known distribution, we don’t know how to do this.

¯ That is, we may be able to evaluate the pdf at specific points (probably not, but we’ll deal with that issue later) but we cannot draw IID random samples from the distribution.

¯ However, there are other random number generators that we can draw samples from—how to use this?

■ We define an importance density q(x), which is a known pdf from which we are able to draw random samples.

■ Then, if we wish to evaluate

µ D g(x) f (x) dx, Z but cannot, since we cannot draw random values from f (x), we could instead consider

g(x) f (x) ¡ µ D q(x) dx, q(x) Z where we can draw samples from q(x).

■ Then N (i) (i) N

1 g(x ) f (x ) 1 (i) (i)

D D µÇ g(x )w(x ),

N N q(x (i)) N i 1 i 1 (i) X(i) (i) X

where w(x ) D f (x )/q(x ).

■ This requires that q(x) have support (i.e., q(x)> 0) everywhere f (x) has support.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–11

EXAMPLE: Same as before, want to find E[y], where y D cos(v) and 2

 N v (0,σv ). But, we now assume that we have only a uniform random number generator. ■ Range of uniform RV was chosen Monte•Carlo integration 1

to be ¦4σv. 0.8 N = 10000; sv = 0.75; 0.6 q = 8 sv (rand([1 N])-0.5); ]

* * y [

y = cos(q).*exp(-q.^2/(2*sv^2)) ... E 0.4 /sqrt(2*pi*sv^2)/(1/(8*sv)); Ey = cumsum(y)./(1:N); 0.2 True mean Gaussian estimate Uniform estimate 0 0 2000 4000 6000 8000 10000 ■ Convergence is slower than before. Sample number ■ Nonetheless, we get the same value in the end.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–12 7.3: Weight normalization and impulse functions

Weight normalization

■ We are getting closer to being able to evaluate the desired equations

j Z D j j Z

f (xk k 1) f (xk xk 1) f (xk 1 k 1) dxk 1. Rx

Z k 1

j j Z

f (zk xk) f (xk k 1) Z D

f (xk j k) .

j Z

f (zk k 1)

j Z

Typically, we do not calculate f (zk k 1) because

¯ It involves evaluation of an integral

¯ It does not depend on xk

j Z D ¯ It serves only to normalize the posterior so f (xk k) dxk 1. Z ■ Thus, we will choose to evaluate something that is only proportional to the posterior—not the posterior itself.

■ How does this affect Monte-Carlo integration?

■ Assume that we know a function that is proportional to f (x). Call it É

f (x) D cf (x), where c is an unknown constant.

■ This will bias our estimate N (i) N (i) (i)

1 (i) fÉ(x ) 1 g(x ) f (x )

É

D  D D Ç µÇ g(x) f (x) dx g(x ) c cµ.

b N q(x (i)) N q(x (i)) i i Z X1 X1 ■ We can compute the bias if we (conceptually) replace the arbitrary E function g(x) by 1. That is, we seek to find [1] D 1 to expose the bias. N (i) N (i)

1 fÉ(x ) 1 f (x )

É

D ¡  ¡ D D µÇ 1 f (x) dx 1 c c.

1 N q(x (i)) N q(x (i)) i i Z X1 X1 1 Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018,| J. McNames{z and G.L.} Plett ECE5550, PARTICLE FILTERS 7–13

N (i) N 1 fÉ(x ) 1

■ (i) D So, c D w(x ).

N q(x (i)) N i i X1 X1 ■ If we divide our biased estimatee by this constant, we remove the bias. ■ So, in general, for any g(x), N (i) N

1 (i) f (x ) 1 (i) (i)

D D µÇ g(x ) g(x )w(x )

N N q(x (i)) N i 1 i 1 X X N (i) 1 (i) w(x )

D g(x )

N 1 N w(x (i)) i 1 N i 1 ! X e N P

(i) (i)

D g(x )w (x ), e

i X1 w(x (i)) (i)

where w (x ) D . N w(x (i)) i 1 ■ This last step is callede weight normalization. P e The impulse function

■ We are making excellent progress in the development of background to the particle filter.

■ One remaining concept relates to how we will represent the probability density functions that are recursively updated.

■ It turns out that we will use the multidimensional Dirac delta (impulse)

function to do so.

D D 8 6 D ¯ δ(x) dx 1 and δ(x a) 0 x a. Z

From these properties, can show f (a) D f (x)δ(x a) dx. Z Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–14

■ A multivariable impulse is defined as

D ¡ ¡ ¡ δ(x) D δ(xi) δ(x0)δ(x1) δ(xn), i Y where xi is the ith element of vector x.

■ Particle filters represent the joint posterior as a sum of impulses.

■ To investigate this, consider a simple example with a Gaussian pdf. N 1 (i) (i)

■ Ç

If we model the pdf as f (x) D δ(x x ), where x are IID N

i X1 samples drawn from f (x), then substitution of fÇ(x) into our integral to find a mean is

µ D g(x) f (x) dx Z N

1 (i)

Ç

D D µÇ g(x) f (x) dx g(x) δ(x x ) dx N

i 1 Z Z X N N

1 (i) 1 (i)

D D g(x)δ(x x ) dx g(x ).

N N i i X1 Z X1

■ So, using our approximation fÇ(x), we arrived at the same estimator as derived earlier based on Monte-Carlo integration.

Gaussian samples

■ Ç But, is it fair to say f (x)  f (x)? 0.4

■ The figure draws N D 100 impulses 0.3 )

sampled from f (x) without arrows x ( 0.2 (for clarity), superimposed on f (x). f 0.1 ■ Hard to argue any equivalence 0 −5 −4 −3 −2 −1 0 1 2 3 4 5 except under an integral. x

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–15 ■ This is a kind of , but if you were given only the impulses it would have been tricky to guess the true shape of the distribution.

■ In some sense, f (x) and fÇ(x) are nothing alike. They are alike in that Ç

g(x) f (x) dx  g(x) f (x) dx and

Z Z x x

Ç 

F(x) D f (α) dα f (α) dα. Z Z

■ So, we can calculate percentiles (including median) from fÇ(x).

2 ■ But, there is no obvious way to calculate mode of f (x) from fÇ(x).

■ Also, in our application, we won’t be able to draw samples from f (x), but will instead draw samples from an importance density q(x).

■ The equivalent effect on the estimated pdf is to weight it

(i) (i) (i) (i) (i)

Ç

D f (x) D w δ(x x ), w f (x )/q(x ). i X Weighted student•t samples ■ This converges in the same 0.4 Gaussian Student t

manner as the unweighted 0.3

approximation of the pdf. ) x

( 0.2 f ■ In example, q(x) is the student-t 0.1 distribution with two degrees of

0 freedom. −4 −3 −2 −1 0 1 2 3 4 x

■ (i) (i) (i) 6 D In general, f (x ) 6 D q(x ), so w 1.

2 Can use Viterbi particle filters — see Prof. McNames’ course notes and videos for details.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–16 7.4: Sequential

■ At last, we get to our first particle-filter algorithm!

■ Our goal is to estimate either the joint or marginal pdf

X Z j Z f ( k j k) or f (xk k).

■ Note that the first is the pdf of the entire trajectory up to time k,

f g Xk D x0, x1,..., xk . Very high (and growing) dimension.

KEY IDEA: We assume that the joint pdf can be expressed as

N p N p

(i) (i) (i)

Ç

Z D X D X j X

f ( k k) wk δ( k k ), wk 1. i 1 i 1 X X ■ X Z This is a discrete weighted approximation to true posterior f ( k j k),

(i) (i) N p X g parameterized and completely defined by f , w . k k i 1 ■ To find these parameters efficiently, we desire a recursive algorithm.

■ First, we will need to select an importance density for the prediction step because we cannot sample from the pdfs.

■ Any pdf (including an important density) must factor

Z D j X j Z X j Z X q( k k) q(xk k 1, k)q( k 1 k).

■ We choose an importance density that factors

X Z X j Z D j X j Z

q( k k) q(xk k 1, k)q( k 1 k 1).

■ Not quite recursive, so again, we narrow down the kind of importance density that we will use by requiring that it satisfy

X Z D j j Z j X

q( k k) q(xk xk 1, zk)q( k 1 k 1).

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–17 (i) ■ X This factoring permits samples of the trajectory k to be created (i) sequentially after each observation zk is made by appending xk to

X(i) , where x (i) is made from z and x (i) only. k 1 k k k 1 (i) X(i) ¯ Note: xk is kth member of particle k , where particle comprises (i) entire trajectory and xk is only the most recent point in trajectory; (i) (i) (i)

¯ Similarly, x is penultimate point X , or final point in X . k 1 k k 1

■ We will also need a weight-update equation

(i)

X j Z (i) f ( k k)

wk » (i) .

X j Z q( k k) ■ The samples are chosen by importance sampling, where the samples X(i) are taken at the particle locations k , and we get to choose the X Z importance density q( k j k).

■ We’ll say more on choosing q(¡) later, but for now we can assume we’ve chosen one (e.g., Gaussian).

■ To compute the weights, we’ll also need to be able to recursively X(i) Z calculate f ( k j k).

¯ Note that we can write:

(i) (i)

Z D X j Z j Z X j

f ( k , zk k 1) f ( k zk, k 1) f (zk k 1)

(i)

D X j Z j Z f ( k k) f (zk k 1).

¯ We can also write:

X(i) X(i) X(i) j Z D j Z j Z

f ( k , zk k 1) f (zk k 1, k ) f ( k k 1)

(i) X(i) D j j Z f (zk xk ) f ( k k 1)

(i) (i) X(i) D j j Z

f (z x ) f (x , ) k k k k 1 k 1

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–18

(i) (i) X(i) X(i) D j j Z j Z

f (z x ) f (x , ) f ( ) k k k k 1 k 1 k 1 k 1

(i) (i) (i) (i)

j j j Z D X

f (z x ) f (x x ) f ( ). k k k k 1 k 1 k 1

■ Equating these two forms gives

(i) (i) (i) j f (z j x ) f (x x )

X(i) k k k k 1 X(i) j Z D j Z

f ( ) f ( )

k k k 1 k 1

j Z

f (zk k 1)

(i) (i) (i) X(i) » j j j Z

f (z x ) f (x x ) f ( ) k k k k 1 k 1 k 1

X(i) (i) (i) (i) Z D j j Z j X

q( ) q(x x , z )q( ) k k k k 1 k k 1 k 1

(i) (i) (i) (i)

j j Z j X

f (z x ) f (x x ) f ( ) (i) k k k k 1 k 1 k 1

wk » (i) (i) (i)

j Z j X

q(x x , z )q( ) k k 1 k k 1 k 1

(i) (i) (i) j f (z j x ) f (x x ) k k k k 1 (i)

D w . (i) (i) k 1

q(x j x , z ) k k 1 k ■ So, we end up with a beautiful recursion for the weights.

(i) (i) j ¯ We get to choose q(x x , z ). k k 1 k

(i)

j D ¯ f (zk xk ) is obtained from measurement model zk hk(xk, uk,vk).

(i) (i)

¯ j D

f (x x ) is from process model x f (x , u , w ). k k 1 k k 1 k 1 k 1 k 1 (i)

¯ w is known from the previous time step for every particle i. k 1

■ So, we now have a clever way to estimate the joint posterior

N p

(i) (i) (i)

Ç

Z D X X j X f ( k k) wk δ( k k ).

i 1 X (i) ■ Z But, what we often care about is the marginal posterior f (xk j k), which we can get by integrating the joint.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–19 ■ Not as scary as it might seem.

N p

(i) (i) Ç

Ç X X j Z D X j Z X D X f (xk k) f ( k k) d k 1 wk δ( k k ) d k 1,

i 1 Z Z X where n

X D ¡ ¡ ¡ δ( k) D δ(xi ) δ(x0)δ(x1) δ(xn).

i 0 Y ■ So, N p (i) (i) (i) (i)

Ç X j Z D ¡ ¡ ¡ f (x ) w δ(x x ) δ(x x )δ(x x ) d k k k 0 0 k 1 k 1 k k k 1

i 1 X Z N p

(i) (i)

D wk δ(xk xk ).

i 1 X ■ The impulse notation makes the algorithm completely recursive, if only the marginal is needed.

Sequential importance sampling (SIS) algorithm

■ So, the most basic general particle-filter algorithm, known as sequential importance sampling, can be written as:

(i) (i) N p (i) (i) N p

g D f g

f x , w SIS x , w , z

k k i 1 k 1 k 1 i 1 k D ¯ For i 1: N p,  

(i) (i) j ◆ Draw RV x  q(x x , z ) — randomly! k k k 1 k ◆ Calculate unnormalized weights:

(i) (i) (i) j f (z j x ) f (x x ) (i) k k k k 1 (i)

w D w . k (i) (i) k 1

q(x j x , z ) k k 1 k (i) (i) w ◆ e k Normalize the weights: wk D . N p w(i) i 1 k e P Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014,e 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–20 7.5 Example ■ Suppose we have a car driving around a circular track with radius 200m.

■ Æ y Velocity is nearly constant at 2 per sample, 0 r but may be perturbed by additive Gaussian φk yc

noise modeling bumps, maneuvers, etc. Ô ■ We have a sensor located 500 2m from the

center of the track that measures angle to the θk Æ

car, θ with noise ¦5 typical. k xc x0 ■ We wish to estimate φk (i.e., xk D φk).

C D C Note that xc,k D r cos(φk) x0 and yc,k r sin(φk) y0.

■ Æ 2 D Model sensor noise vk  N (0,(5 ) ) so (where zk θk) y

1 c,k C θ D tan v . k x k  c,k 

■ For the process model, we write

D C C

φk 1 φk ω wk,

2 D  N where wk (0,σw) and ω 2 degrees/sample.

D D Let y0 D x0 500m, r 200m, 10π 2

w  N 0, k 180   ! 5π 2

v  N 0, k 180   ! 50π 10π 2

φ  N , . 0 180 180     !

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–21

■ We have several design choices before we can employ the filter.

¯ j

Importance density q(φk φk 1, θk):

◆ Easiest choice is

2

D j  C

j N

q(φk φk 1, θk) f (φk φk 1) (φk 1 2,σw).

¯ Number of particles:

◆ Performance versus computation tradeoff. ◆ Useful approach—as many as you have patience for.

◆ Here, we use N p D 500.

¯ Estimator of state:

◆ We’ll use the mean, as it is easiest.

r sin(φ ) C y

k 0

■ 1 2  j N Also, the likelihood f (θk φk) tan ,σv .

r cos(φ ) C x   k 0   % Define constants describing problem % ------initialStateSigma = 10*pi/180; measurementNoiseSigma = 5.0*pi/180; processNoiseSigma = 10*pi/180; stateInitial = 50*pi/180; xo = 500; yo = 500; r = 200; omega = 2*pi/180; nSamples = 100;

% Create sequence of observations from model % ------x = zeros(nSamples+1,1); z = zeros(nSamples,1); processNoise = randn(nSamples,1)*processNoiseSigma; measurementNoise = randn(nSamples,1)*measurementNoiseSigma;

x(1) = stateInitial + randn(1)*initialStateSigma; for k=1:nSamples x(k+1) = x(k) + omega + processNoise(k); z(k) = atan((r*sin(x(k))+yo)./(r*cos(x(k))+xo)) + ... measurementNoise(k); end x = x(1:nSamples);

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–22

% Create the Particles % ------nParticles = 500; xp = zeros(nParticles,nSamples); wp = zeros(nParticles,nSamples); xPHatMean = zeros(nSamples,1); % mean-particle estimate of state

for k=1:nSamples processNoise = randn(nParticles,1)*processNoiseSigma; for cp=1:nParticles if k==1 xp(cp,k) = stateInitial + randn(1)*initialStateSigma; wp(cp,k) =1/nParticles; else xp(cp,k) = xp(cp,k-1) + omega + processNoise(cp); zp = atan((r*sin(xp(cp,k))+yo)./(r*cos(xp(cp,k))+xo)); likelihood = normpdf(z(k),zp,measurementNoiseSigma); wp(cp,k) = wp(cp,k-1)*likelihood; end end wp(:,k) = wp(:,k)/sum(wp(:,k)); xPHatMean(k) = sum(wp(:,k).*xp(:,k)); end

■ In the , the true state and measured output are

True state Measured signal 500 90

75 400

(degrees) 60

300 k θ

(degrees) 45 k

φ 200 30

100 State 15

0 Measurement 0 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–23

Comparing particle trajectories with truth 500 Particles Truth ■ The particles, with true state 400

trajectory overlaid, are shown. 300 (degrees) ) i ( k 200 ■ Particles represent many possible φ 100

trajectories to choose from. State

0 0 20 40 60 80 100 Iteration k Filtered posterior PDF ■ Particle-filter estimate is 500 pretty poor, compared to 400

300 True Bayes optimal. Bayes mean (degrees) Part. mean k 200 ■ Need to dig deeper to φ 95% Conf. 100

figure out what’s going State

0 wrong with our estimator. 0 20 40 60 80 100 Iteration k

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–24 7.6: How to display a marginal posterior

■ Weighted student•t samples Would like to visualize particle 0.4 Gaussian estimate of marginal posterior Student t 0.3

Ç Z f (xk j k). ) x

( 0.2 ■ But, consists of weighted impulses, f as figure from prior example shows. 0.1

0 ■ −4 −3 −2 −1 0 1 2 3 4 Difficult to grasp intuitively. x ■ A popular method of estimating density is to add a series of “bumps” together, where each bump is scaled by the impulse weight and centered at the impulse location.

¯ This is merely popular; it is not necessarily “best.”

■ The bumps are called kernels and should have the following

properties

D b(u)  0, b(u) du 1. Z ■ The width of the kernel is specified by σ . Typically 1 u

b (u) D b , σ σ σ

  D where it is easy to show that bσ (u) du D b(u) du 1. Z Z ■ Then, a kernel density estimator is simply expressed as

N p

(i) (i)

Ç

f (xk) D wk bσ (xk xk ).

i 1 X ■ Kernels usually have even symmetry; Gaussian-shapes are popular.

■ For the prior example, we can estimate the posterior as

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–25 100 weighted student-t samples 0.5 Posterior Importance 0.4 Est. Posterior

0.3 ) x ( f 0.2

0.1

0 -4 -3 -2 -1 0 1 2 3 4 x

■ Popular kernels include

2

Epanechnikov: b(u) D c(1 u )5(u/2)

2 2

Bi-weight: b(u) D c(1 u ) 5(u/2)

2 3

Tri-weight: b(u) D c(1 u ) 5(u/2)

j j Triangular: b(u) D c(1 u )5(u/2) 2

u

Gaussian: b(u) D ce where c is a constant chosen to make the kernel integrate to 1, and 5(u) is the unit-pulse function.

■ The bump’s width is the critical parameter. Much more important than the choice of kernel.

¯ σ large: Smoother pdf estimate, more biased, less variable;

¯ σ small: Coarser pdf estimate, less biased, more variable.

■ Comparing estimated marginal posterior pdf using kernel functions to the true posterior, we get a hint of the problem.

■ The posterior starts out okay, but quickly collapses to a single likely trajectory.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–26

Estimated marginal posterior PDF Filtered posterior PDF 500 500

400 400

True 300 True 300 Bayes mean (degrees) Particle (degrees) Part. mean k 95% Conf. k 200 200 95% Conf. φ φ

100 100 State State

0 0 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k ■ This leads us to look at particle weights (on natural and log scales):

Particle weights 0 Logarithm of particle weights 1 10

0.8 ) ) i i ( ( k k w w 0.6 −50 10 0.4 Weight Weight 0.2

−100 0 10 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k

■ The weights start at the same value, 1/N p, but very quickly one particle “takes over.” The weights of the other particles decay to around zero, and they have no influence on the results.

■ A lot of computation for no good reason.

■ Standard deviation of weights We might also look at the 0.05 standard deviation of the 0.04 weights. 0.03 w

■ σ The increasing value shows that 0.02 weights are becoming very 0.01 unequal, and some particles are 0 0 20 40 60 80 100 being ignored. Iteration k

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–27 7.7: Reducing particle degeneracy

■ In the example, we found that

¯ The particle filter starts off tracking well,

¯ Performance rapidly deteriorates,

¯ Most of the weights become really small,

¯ Increasing the number of particles helps only a little.

■ If only a few of the weights are large, only a few trajectories contribute to the state estimate N p

(i) (i) D xÇ k wk xk .

i 1 X ■ So, most of the particles are wasted clock cycles. What happened? X(i) ¯ Keep in mind that the particles are entire state trajectories k .

¯ Drawing samples from a high-dimensional space.

¯ Difficult to draw probable state trajectories.

¯ Dimension grows linearly with time.

¯ Becomes exponentially difficult to sample efficiently.

¯ Known as the degeneracy problem.

■ (i) Is manifest as wk ! 0 as k increases. ■ To help visualize, we introduce the effective sample size, defined as 1

N pe,k D . N p (w(i))2 i 1 k ■ This is not a derivation, but a usefulP definition. Note that it makes sense for the boundary cases:

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–28

(i) D ¯ If all weights are equal, wk 1/N p and

1 1

D D N D N . pe,k N 2 p p (1/N )2 N p(1/N p) i 1 p

¯ If all weights but oneP are zero, we have

1 D N D 1. pe,k 1 ■ Thus, N is bounded and Effective sample size

pe,k 500  1  N pe,k N p. 400

■ For our prior example, we see 300 k , e that N quickly decreases. p p ,k N e 200

■ We are essentially calculating 100 the state from a single trajectory 0 0 20 40 60 80 100 after 20 or so samples. Iteration k

■ Idea is to eliminate samples with low importance weights and replicate samples with high importance weights.

■ (i) (i) g We resample from our set f xk , wk , with replacement, whenever

N pe,k drops below some threshold (perhaps some percentage of N p).

■ Can resample as many times as we like, but typically we draw as many samples as we started with.

■ The new particles are drawn with a probability given by the

importance weights, so we can assign uniform weights 1/N p to the resulting IID samples.

■ A systematic resampling method is as follows:

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–29

¯ Let c be the cumulative summation of weights i (i) ( j) ck D wk .

j 1 X

1

 D ¯ U

Make a single draw u1 (0, N p ) and set i 1. D ¯ For each j 1: N p,

◆ C u j D u1 ( j 1)/N p. (i)

◆ C While u j > ck , i D i 1.

( j) (i) ( j)

D ◆ D Assign new xk old xk , wk 1/N p.

■ In MATLAB code, this looks like:

nEffective = 1/sum(wp(:,k).^2); if nEffectivec(iPick) && iPick

■ Repeating the previous example Comparing particle trajectories with truth 500 Particles (exactly same system data), with Truth 400 resampling threshold constraint 300

(degrees) 

N pe,k 100, we get the following ) i ( k 200 results. φ 100

■ State The particle trajectories have 0 0 20 40 60 80 100 improved immensely. Iteration k

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–30 ■ The estimated marginal posterior is now a much closer match to the true posterior. Estimated marginal posterior PDF Filtered posterior PDF 500 500

400 400

True 300 True 300 Bayes mean (degrees) Particle (degrees) Part. mean k 95% Conf. k 200 200 95% Conf. φ φ

100 100 State State

0 0 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k ■ The particle weights themselves have improved in diversity.

Particle weights 0 Logarithm of particle weights 0.05 10

0.04 ) ) i i ( ( k k w w 0.03 −50 10 0.02 Weight Weight 0.01

−100 0 10 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k ■ The weight standard deviation has improved, and the effective number of particles has also improved. ×10-3 Standard deviation of weights Effective sample size 4 500

400 3

300 k , e w

2 p σ N 200

1 100

0 0 0 20 40 60 80 100 0 20 40 60 80 100 Iteration k Iteration k ■ So, with resampling, particle filters become a much more effective methodology.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett ECE5550, PARTICLE FILTERS 7–31 Where from here

■ This section on particle filters is only an introduction:

¯ There is a lot of literature on choosing importance densities, on finding MAP , etc.

¯ Prof. McNames’ course covers some of these more advanced topics, if you are interested.

¯ The material we have covered here should prepare you for his lectures and help you read and interpret the literature.

■ We now take a different direction and look at multi-target tracking applications of Kalman filters.

Lecture notes prepared by J. McNames and G.L. Plett. Copyright © 2012, 2014, 2018, J. McNames and G.L. Plett