G13EJF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G13EJF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G13EJF applies the Unscented Kalman Filter to a nonlinear state space model, with additive noise.
G13EJF uses reverse communication for evaluating the nonlinear functionals of the state space model.

2  Specification

SUBROUTINE G13EJF ( IREVCM, MX, MY, Y, LX, LDLX, LY, LDLY, X, ST, LDST, N, XT, LDXT, FXT, LDFXT, ROPT, LROPT, ICOMM, LICOMM, RCOMM, LRCOMM, IFAIL)
INTEGER  IREVCM, MX, MY, LDLX, LDLY, LDST, N, LDXT, LDFXT, LROPT, ICOMM(LICOMM), LICOMM, LRCOMM, IFAIL
REAL (KIND=nag_wp)  Y(MY), LX(LDLX,*), LY(LDLY,*), X(MX), ST(LDST,*), XT(LDXT,*), FXT(LDFXT,*), ROPT(LROPT), RCOMM(LRCOMM)

3  Description

G13EJF applies the Unscented Kalman Filter (UKF), as described in Julier and Uhlmann (1997b) to a nonlinear state space model, with additive noise, which, at time t, can be described by:
xt+1 =Fxt+vt yt =Hxt+ut  
where xt represents the unobserved state vector of length mx and yt the observed measurement vector of length my. The process noise is denoted vt, which is assumed to have mean zero and covariance structure Σx, and the measurement noise by ut, which is assumed to have mean zero and covariance structure Σy.

3.1  Unscented Kalman Filter Algorithm

Given x^0, an initial estimate of the state and P0 and initial estimate of the state covariance matrix, the UKF can be described as follows:
(a) Generate a set of sigma points (see section Section 3.2):
Xt= x ^ t-1     x ^ t-1 + γ Pt-1     x ^ t-1 - γ Pt-1 (1)
(b) Evaluate the known model function F:
Ft=FXt(2)
The function F is assumed to accept the mx×n matrix, Xt and return an mx×n matrix, Ft. The columns of both Xt and Ft correspond to different possible states. The notation Ft,i is used to denote the ith column of Ft, hence the result of applying F to the ith possible state.
(c) Time Update:
x^t_ = i=1 n Wim Ft,i (3)
Pt_ = i=1 n Wic Ft,i - x ^ t _ Ft,i - x ^ t _ T + Σx (4)
(d) Redraw another set of sigma points (see section Section 3.2):
Yt = x ^ t _     x ^ t _ + γ Pt_     x ^ t _ - γ Pt_ (5)
(e) Evaluate the known model function H:
Ht=HYt(6)
The function H is assumed to accept the mx×n matrix, Yt and return an my×n matrix, Ht. The columns of both Yt and Ht correspond to different possible states. As above Ht,i is used to denote the ith column of Ht.
(f) Measurement Update:
y ^ t = i=1 n Wim Ht,i (7)
Pyyt = i=1 n Wic Ht,i - y ^ t Ht,i - y ^ t T + Σy (8)
P xyt = i=1 n Wic Ft,i - x ^ t _ Ht,i - y ^ t T (9)
Kt = P xyt Pyyt-1 (10)
x^t = x ^ t _ + Kt yt - y ^ t (11)
Pt = Pt_ - Kt Pyyt KtT (12)
Here Kt is the Kalman gain matrix, x^t is the estimated state vector at time t and Pt the corresponding covariance matrix. Rather than implementing the standard UKF as stated above G13EJF uses the square-root form described in the Haykin (2001).

3.2  Sigma Points

A nonlinear state space model involves propagating a vector of random variables through a nonlinear system and we are interested in what happens to the mean and covariance matrix of those variables. Rather than trying to directly propagate the mean and covariance matrix, the UKF uses a set of carefully chosen sample points, referred to as sigma points, and propagates these through the system of interest. An estimate of the propagated mean and covariance matrix is then obtained via the weighted sample mean and covariance matrix.
For a vector of m random variables, x, with mean μ and covariance matrix Σ, the sigma points are usually constructed as:
Xt = μ     μ + γ Σ     μ - γ Σ  
When calculating the weighted sample mean and covariance matrix two sets of weights are required, one used when calculating the weighted sample mean, denoted Wm and one used when calculated the weighted sample covariance matrix, denoted Wc. The weights and multiplier, γ, are constructed as follows:
λ =α2L+κ-L γ =L+λ Wim = λL+λ i=1 12L+λ i=2,3,,2L+1 Wic = λL+λ +1-α2+β i=1 12L+λ i=2,3,,2L+1  
where, usually L=m and α,β and κ are constants. The total number of sigma points, n, is given by 2L+1. The constant α is usually set to somewhere in the range 10-4α1 and for a Gaussian distribution, the optimal values of κ and β are 3-L and 2 respectively.
Rather than redrawing another set of sigma points in (d) of the UKF an alternative method can be used where the sigma points used in (a) are augmented to take into account the process noise. This involves replacing equation (5) with:
Yt = Xt     Xt,1 + γ Σx     Xt,1 - γ Σx (13)
Augmenting the sigma points in this manner requires setting L to 2L (and hence n to 2n-1) and recalculating the weights. These new values are then used for the rest of the algorithm. The advantage of augmenting the sigma points is that it keeps any odd-moments information captured by the original propagated sigma points, at the cost of using a larger number of points.

4  References

Haykin S (2001) Kalman Filtering and Neural Networks John Wiley and Sons
Julier S J (2002) The scaled unscented transformation Proceedings of the 2002 American Control Conference (Volume 6) 4555–4559
Julier S J and Uhlmann J K (1997a) A consistent, debiased method for converting between polar and Cartesian coordinate systems Proceedings of AeroSense97, International Society for Optics and Phonotonics 110–121
Julier S J and Uhlmann J K (1997b) A new extension of the Kalman Filter to nonlinear systems International Symposium for Aerospace/Defense, Sensing, Simulation and Controls (Volume 3) 26

5  Parameters

Note: this routine uses reverse communication. Its use involves an initial entry, intermediate exits and re-entries, and a final exit, as indicated by the parameter IREVCM. Between intermediate exits and re-entries, all parameters other than FXT must remain unchanged.
1:     IREVCM – INTEGERInput/Output
On initial entry: must be set to 0 or 3.
If IREVCM=0, it is assumed that t=0, otherwise it is assumed that t0 and that G13EJF has been called at least once before at an earlier time step.
On intermediate exit: IREVCM=1 or 2. The value of IREVCM specifies what intermediate values are returned by this routine and what values the calling program must assign to parameters of G13EJF before re-entering the routine. Details of the output and required input are given in the individual parameter descriptions.
On intermediate re-entry: IREVCM must remain unchanged.
On final exit: IREVCM=3
Constraint: IREVCM=0, 1, 2 or 3.
2:     MX – INTEGERInput
On entry: mx, the number of state variables.
Constraint: MX1.
3:     MY – INTEGERInput
On entry: my, the number of observed variables.
Constraint: MY1.
4:     YMY – REAL (KIND=nag_wp) arrayInput
On entry: yt, the observed data at the current time point.
5:     LXLDLX* – REAL (KIND=nag_wp) arrayInput
Note: the second dimension of the array LX must be at least MX.
On entry: Lx, such that LxLxT=Σx, i.e., the lower triangular part of a Cholesky decomposition of the process noise covariance structure. Only the lower triangular part of LX is referenced.
If LDLX=0, there is no process noise (vt=0 for all t) and LX is not referenced.
If Σx is time dependent, then the value supplied should be for time t.
6:     LDLX – INTEGERInput
On entry: the first dimension of the array LX as declared in the (sub)program from which G13EJF is called.
Constraint: LDLX=0 or LDLXMX.
7:     LYLDLY* – REAL (KIND=nag_wp) arrayInput
Note: the second dimension of the array LY must be at least MY.
On entry: Ly, such that LyLyT=Σy, i.e., the lower triangular part of a Cholesky decomposition of the observation noise covariance structure. Only the lower triangular part of LY is referenced.
If Σy is time dependent, then the value supplied should be for time t.
8:     LDLY – INTEGERInput
On entry: the first dimension of the array LY as declared in the (sub)program from which G13EJF is called.
Constraint: LDLYMY.
9:     XMX – REAL (KIND=nag_wp) arrayInput/Output
On initial entry: x^t-1 the state vector for the previous time point.
On intermediate exit: when
IREVCM=1
X is unchanged.
IREVCM=2
x^t_.
On intermediate re-entry: X must remain unchanged.
On final exit: x^t the updated state vector.
10:   STLDST* – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array ST must be at least MX.
On initial entry: St, such that St-1St-1T=Pt-1, i.e., the lower triangular part of a Cholesky decomposition of the state covariance matrix at the previous time point. Only the lower triangular part of ST is referenced.
On intermediate exit: when
IREVCM=1
ST is unchanged.
IREVCM=2
St_, the lower triangular part of a Cholesky factorization of Pt_.
On intermediate re-entry: ST must remain unchanged.
On final exit: St, the lower triangular part of a Cholesky factorization of the updated state covariance matrix.
11:   LDST – INTEGERInput
On entry: the first dimension of the array ST as declared in the (sub)program from which G13EJF is called.
Constraint: LDSTMX.
12:   N – INTEGERInput/Output
On initial entry: the value used in the sizing of the FXT and XT arrays. The value of N supplied must be at least as big as the maximum number of sigma points that the algorithm will use. G13EJF allows sigma points to be calculated in two ways during the measurement update; they can be redrawn or augmented. Which is used is controlled by ROPT.
If redrawn sigma points are used, then the maximum number of sigma points will be 2mx+1, otherwise the maximum number of sigma points will be 4mx+1.
On intermediate exit: the number of sigma points actually being used.
On intermediate re-entry: N must remain unchanged.
On final exit: reset to its value on initial entry.
Constraints:
if IREVCM=0 or 3,
  • if redrawn sigma points are used, N2×MX+1;
  • otherwise N4×MX+1.
13:   XTLDXT* – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array XT must be at least maxMY,N.
On initial entry: need not be set.
On intermediate exit: Xt when IREVCM=1, otherwise Yt.
For the jth sigma point, the value for the ith parameter is held in XTij, for i=1,2,,MX and j=1,2,,N.
On intermediate re-entry: XT must remain unchanged.
On final exit: the contents of XT are undefined.
14:   LDXT – INTEGERInput
On entry: the first dimension of the array XT as declared in the (sub)program from which G13EJF is called.
Constraint: LDXTMX.
15:   FXTLDFXT* – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array XT must be at least N+maxMX,MY.
On initial entry: need not be set.
On intermediate exit: the contents of FXT are undefined.
On intermediate re-entry: F Xt  when IREVCM=1, otherwise H Yt  for the values of Xt and Yt held in XT.
For the jth sigma point the value for the ith parameter should be held in FXTij, for j=1,2,,N. When IREVCM=1, i=1,2,,MX and when IREVCM=2, i=1,2,,MY.
On final exit: the contents of FXT are undefined.
16:   LDFXT – INTEGERInput
On entry: the first dimension of the array FXT as declared in the (sub)program from which G13EJF is called.
Constraint: LDFXTmaxMX,MY.
17:   ROPTLROPT – REAL (KIND=nag_wp) arrayInput
On entry: optional arguments. The default value will be used for ROPTi if LROPT<i. Setting LROPT=0 will use the default values for all optional arguments and ROPT need not be set.
ROPT1
If set to 1 then the second set of sigma points are redrawn, as given by equation (5). If set to 2 then the second set of sigma points are generated via augmentation, as given by equation (13).
Default is for the sigma points to be redrawn (i.e., ROPT1=1)
ROPT2
κx, value of κ used when constructing the first set of sigma points, Xt.
Defaults to 3-MX.
ROPT3
αx, value of α used when constructing the first set of sigma points, Xt.
Defaults to 1.
ROPT4
βx, value of β used when constructing the first set of sigma points, Xt.
Defaults to 2.
ROPT5
Value of κ used when constructing the second set of sigma points, Yt.
Defaults to 3-2×MX when LDLX0 and the second set of sigma points are augmented and κx otherwise.
ROPT6
Value of α used when constructing the second set of sigma points, Yt.
Defaults to αx.
ROPT7
Value of β used when constructing the second set of sigma points, Yt.
Defaults to βx.
Constraints:
  • ROPT1=1 or 2;
  • ROPT2>-MX;
  • ROPT5>-2×MX when LDLY0 and the second set of sigma points are augmented, otherwise ROPT5>-MX;
  • ROPTi>0, for i=3,6.
18:   LROPT – INTEGERInput
On entry: length of the options array ROPT.
Constraint: 0LROPT7.
19:   ICOMMLICOMM – INTEGER arrayCommunication Array
On initial entry: ICOMM need not be set.
On intermediate exit: ICOMM is used for storage between calls to G13EJF.
On intermediate re-entry: ICOMM must remain unchanged.
On final exit: ICOMM is not defined.
20:   LICOMM – INTEGERInput
On entry: the length of the array ICOMM. If LICOMM is too small and LICOMM2 then IFAIL=201 is returned and the minimum value for LICOMM and LRCOMM are given by ICOMM1 and ICOMM2 respectively.
Constraint: LICOMM30.
21:   RCOMMLRCOMM – REAL (KIND=nag_wp) arrayCommunication Array
On initial entry: RCOMM need not be set.
On intermediate exit: RCOMM is used for storage between calls to G13EJF.
On intermediate re-entry: RCOMM must remain unchanged.
On final exit: RCOMM is not defined.
22:   LRCOMM – INTEGERInput
On entry: the length of the array RCOMM. If LRCOMM is too small and LICOMM2 then IFAIL=202 is returned and the minimum value for LICOMM and LRCOMM are given by ICOMM1 and ICOMM2 respectively.
Suggested value: LRCOMM = 30 + MY + MX × MY + 1+nb × maxMX,MY , where nb is the optimal block size. In most cases a block size of 128 will be sufficient.
Constraint: LRCOMM30+MY+MX×MY+2×maxMX,MY.
23:   IFAIL – INTEGERInput/Output
On initial entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On final exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=11
On entry, IREVCM=value.
Constraint: IREVCM=0, 1, 2 or 3.
IFAIL=21
On entry, MX=value.
Constraint: MX1.
IFAIL=22
MX has changed between calls.
On intermediate entry, MX=value.
On initial entry, MX=value.
IFAIL=31
On entry, MY=value.
Constraint: MY1.
IFAIL=32
MY has changed between calls.
On intermediate entry, MY=value.
On initial entry, MY=value.
IFAIL=61
On entry, LDLX=value and MX=value.
Constraint: LDLX=0 or LDLXMX.
IFAIL=81
On entry, LDLY=value and MY=value.
Constraint: LDLYMY.
IFAIL=111
On entry, LDST=value and MX=value.
Constraint: LDSTMX.
IFAIL=121
On entry, augmented sigma points requested, N=value and MX=value.
Constraint: Nvalue.
IFAIL=122
On entry, redrawn sigma points requested, N=value and MX=value.
Constraint: Nvalue.
IFAIL=123
N has changed between calls.
On intermediate entry, N=value.
On intermediate exit, N=value.
IFAIL=141
On entry, LDXT=value and MX=value.
Constraint: LDXTMX.
IFAIL=161
On entry, LDFXT=value and MX=value.
Constraint: if IREVCM=1, LDFXTMX.
IFAIL=162
On entry, LDFXT=value and MY=value.
Constraint: if IREVCM=2, LDFXTMY.
IFAIL=171
On entry, ROPT1=value.
Constraint: ROPT1=1 or 2.
IFAIL=172
On entry, ROPTvalue=value.
Constraint: κ>value.
IFAIL=173
On entry, ROPTvalue=value.
Constraint: α>0.
IFAIL=181
On entry, LROPT=value.
Constraint: 0LROPT7.
IFAIL=191
ICOMM has been corrupted between calls.
IFAIL=201
On entry, LICOMM=value.
Constraint: LICOMM2.
ICOMM is too small to return the required array sizes.
IFAIL=202
On entry, LICOMM=value and LRCOMM=value.
Constraint: LICOMM30 and LRCOMM30+MY+MX×MY+2×maxMX,MY.
The minimum required values for LICOMM and LRCOMM are returned in ICOMM1 and ICOMM2 respectively.
IFAIL=211
RCOMM has been corrupted between calls.
IFAIL=301
A weight was negative and it was not possible to downdate the Cholesky factorization.
IFAIL=302
Unable to calculate the Kalman gain matrix.
IFAIL=303
Unable to calculate the Cholesky factorization of the updated state covariance matrix.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

Not applicable.

8  Parallelism and Performance

G13EJF is not threaded by NAG in any implementation.
G13EJF makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

As well as implementing the Unscented Kalman Filter, G13EJF can also be used to apply the Unscented Transform (see Julier (2002)) to the function F, by setting LDLX=0 and terminating the calling sequence when IREVCM=2 rather than IREVCM=3. In this situation, on initial entry, X and ST would hold the mean and Cholesky factorization of the covariance matrix of the untransformed sample and on exit (when IREVCM=2) they would hold the mean and Cholesky factorization of the covariance matrix of the transformed sample.

10  Example

This example implements the following nonlinear state space model, with the state vector x and state update function F given by:
mx =3 xt+1 = ξt+1 ηt+1 θt+1 T =Fxt+vt = xt+ cosθt -sinθt 0 sinθt cosθt 0 001 0.5r 0.5r 00 r/d -r/d ϕRt ϕLt +vt  
where r and d are known constants and ϕRt and ϕLt are time-dependent knowns. The measurement vector y and measurement function H is given by:
my =2 yt =δt,αtT =Hxt+ut = Δ-ξtcosA-ηtsinA θt-A +ut  
where A and Δ are known constants. The initial values, x0 and P0, are given by
x0 = 0 0 0 , P0 = 0.100 00.10 000.1  
and the Cholesky factorizations of the error covariance matrices, Lx and Lx by
Lx = 0.100 00.10 000.1 , Ly = 0.010 00.01 .  

10.1  Program Text

Program Text (g13ejfe.f90)

10.2  Program Data

Program Data (g13ejfe.d)

10.3  Program Results

Program Results (g13ejfe.r)

The example described above can be thought of as relating to the movement of a hypothetical robot. The unknown state, x, is the position of the robot (with respect to a reference frame) and facing, with ξ,η giving the x and y coordinates and θ the angle (with respect to the x-axis) that the robot is facing. The robot has two drive wheels, of radius r on an axle of length d. During time period t the right wheel is believed to rotate at a velocity of ϕRt and the left at a velocity of ϕLt. In this example, these velocities are fixed with ϕRt=0.4 and ϕLt=0.1. The state update function, F, calculates where the robot should be at each time point, given its previous position. However, in reality, there is some random fluctuation in the velocity of the wheels, for example, due to slippage. Therefore the actual position of the robot and the position given by equation F will differ.
In the area that the robot is moving there is a single wall. The position of the wall is known and defined by its distance, Δ, from the origin and its angle, A, from the x-axis. The robot has a sensor that is able to measure y, with δ being the distance to the wall and α the angle to the wall. The measurement function H gives the expected distance and angle to the wall if the robot's position is given by xt. Therefore the state space model allows the robot to incorporate the sensor information to update the estimate of its position.
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Example Program Illustration of Position and Orientation of Hypothetical Robot Wall Position gnuplot_plot_1 Initial gnuplot_plot_2 Actual gnuplot_plot_3 Updated gnuplot_plot_4 gnuplot_plot_5 gnuplot_plot_6 gnuplot_plot_7

G13EJF (PDF version)
G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015