NAG FL Interface
g13ejf (kalman_unscented_state_revcom)
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
Fortran Interface
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, Intent (In) |
:: |
mx, my, ldlx, ldly, ldst, ldxt, ldfxt, lropt, licomm, lrcomm |
Integer, Intent (Inout) |
:: |
irevcm, n, icomm(licomm), ifail |
Real (Kind=nag_wp), Intent (In) |
:: |
y(my), lx(ldlx,*), ly(ldly,*), ropt(lropt) |
Real (Kind=nag_wp), Intent (Inout) |
:: |
x(mx), st(ldst,*), xt(ldxt,*), fxt(ldfxt,*), rcomm(lrcomm) |
|
C Header Interface
#include <nag.h>
void |
g13ejf_ (Integer *irevcm, const Integer *mx, const Integer *my, const double y[], const double lx[], const Integer *ldlx, const double ly[], const Integer *ldly, double x[], double st[], const Integer *ldst, Integer *n, double xt[], const Integer *ldxt, double fxt[], const Integer *ldfxt, const double ropt[], const Integer *lropt, Integer icomm[], const Integer *licomm, double rcomm[], const Integer *lrcomm, Integer *ifail) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
g13ejf_ (Integer &irevcm, const Integer &mx, const Integer &my, const double y[], const double lx[], const Integer &ldlx, const double ly[], const Integer &ldly, double x[], double st[], const Integer &ldst, Integer &n, double xt[], const Integer &ldxt, double fxt[], const Integer &ldfxt, const double ropt[], const Integer &lropt, Integer icomm[], const Integer &licomm, double rcomm[], const Integer &lrcomm, Integer &ifail) |
}
|
The routine may be called by the names g13ejf or nagf_tsa_kalman_unscented_state_revcom.
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
, can be described by:
where
represents the unobserved state vector of length
and
the observed measurement vector of length
. The process noise is denoted
, which is assumed to have mean zero and covariance structure
, and the measurement noise by
, which is assumed to have mean zero and covariance structure
.
3.1
Unscented Kalman Filter Algorithm
Given
, an initial estimate of the state and
and initial estimate of the state covariance matrix, the UKF can be described as follows:
-
(a)Generate a set of sigma points (see Section 3.2):
-
(b)Evaluate the known model function :
The function is assumed to accept the matrix, and return an matrix, . The columns of both and correspond to different possible states. The notation is used to denote the th column of , hence the result of applying to the th possible state.
-
(c)Time Update:
-
(d)Redraw another set of sigma points (see Section 3.2):
-
(e)Evaluate the known model function :
The function is assumed to accept the matrix, and return an matrix, . The columns of both and correspond to different possible states. As above is used to denote the th column of .
-
(f)Measurement Update:
Here
is the Kalman gain matrix,
is the estimated state vector at time
and
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
random variables,
, with mean
and covariance matrix
, the sigma points are usually constructed as:
When calculating the weighted sample mean and covariance matrix two sets of weights are required, one used when calculating the weighted sample mean, denoted
and one used when calculating the weighted sample covariance matrix, denoted
. The weights and multiplier,
, are constructed as follows:
where, usually
and
and
are constants. The total number of sigma points,
, is given by
. The constant
is usually set to somewhere in the range
and for a Gaussian distribution, the optimal values of
and
are
and
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:
Augmenting the sigma points in this manner requires setting to (and hence to ) 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
Arguments
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 argument
irevcm. Between intermediate exits and re-entries,
all arguments other than fxt must remain unchanged.
-
1:
– Integer
Input/Output
-
On initial entry: must be set to
or
.
If , it is assumed that , otherwise it is assumed that and that g13ejf has been called at least once before at an earlier time step.
On intermediate exit:
or
. The value of
irevcm specifies what intermediate values are returned by this routine and what values the calling program must assign to arguments of
g13ejf before re-entering the routine. Details of the output and required input are given in the individual argument descriptions.
On intermediate re-entry:
irevcm must remain unchanged.
On final exit:
Constraint:
, , or .
Note: any values you return to g13ejf as part of the reverse communication procedure should not include floating-point NaN (Not a Number) or infinity values, since these are not handled by g13ejf. If your code does inadvertently return any NaNs or infinities, g13ejf is likely to produce unexpected results.
-
2:
– Integer
Input
-
On entry: , the number of state variables.
Constraint:
.
-
3:
– Integer
Input
-
On entry: , the number of observed variables.
Constraint:
.
-
4:
– Real (Kind=nag_wp) array
Input
-
On entry: , the observed data at the current time point.
-
5:
– Real (Kind=nag_wp) array
Input
-
Note: the second dimension of the array
lx
must be at least
.
On entry:
, such that
, 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
, there is no process noise (
for all
) and
lx is not referenced.
If is time dependent, the value supplied should be for time .
-
6:
– Integer
Input
-
On entry: the first dimension of the array
lx as declared in the (sub)program from which
g13ejf is called.
Constraint:
or .
-
7:
– Real (Kind=nag_wp) array
Input
-
Note: the second dimension of the array
ly
must be at least
.
On entry:
, such that
, 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 is time dependent, the value supplied should be for time .
-
8:
– Integer
Input
-
On entry: the first dimension of the array
ly as declared in the (sub)program from which
g13ejf is called.
Constraint:
.
-
9:
– Real (Kind=nag_wp) array
Input/Output
-
On initial entry: the state vector for the previous time point.
On intermediate exit:
when
- x is unchanged.
- .
On intermediate re-entry:
x must remain unchanged.
On final exit: the updated state vector.
-
10:
– Real (Kind=nag_wp) array
Input/Output
-
Note: the second dimension of the array
st
must be at least
.
On initial entry:
, such that
, 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
- st is unchanged.
- , the lower triangular part of a Cholesky factorization of .
On intermediate re-entry:
st must remain unchanged.
On final exit: , the lower triangular part of a Cholesky factorization of the updated state covariance matrix.
-
11:
– Integer
Input
-
On entry: the first dimension of the array
st as declared in the (sub)program from which
g13ejf is called.
Constraint:
.
-
12:
– Integer
Input/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 , otherwise the maximum number of sigma points will be .
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
or
,
- if redrawn sigma points are used, ;
- otherwise .
-
13:
– Real (Kind=nag_wp) array
Input/Output
-
Note: the second dimension of the array
xt
must be at least
.
On initial entry: need not be set.
On intermediate exit:
when
, otherwise
.
For the th sigma point, the value for the th parameter is held in
, for and .
On intermediate re-entry:
xt must remain unchanged.
On final exit: the contents of
xt are undefined.
-
14:
– Integer
Input
-
On entry: the first dimension of the array
xt as declared in the (sub)program from which
g13ejf is called.
Constraint:
.
-
15:
– Real (Kind=nag_wp) array
Input/Output
-
Note: the second dimension of the array
fxt
must be at least
.
On initial entry: need not be set.
On intermediate exit:
the contents of
fxt are undefined.
On intermediate re-entry:
when
, otherwise
for the values of
and
held in
xt.
For the th sigma point the value for the th parameter should be held in
, for . When , and when , .
On final exit: the contents of
fxt are undefined.
-
16:
– Integer
Input
-
On entry: the first dimension of the array
fxt as declared in the (sub)program from which
g13ejf is called.
Constraint:
.
-
17:
– Real (Kind=nag_wp) array
Input
-
On entry: optional parameters. The default value will be used for
if
. Setting
will use the default values for all optional parameters and
ropt need not be set.
- If set to then the second set of sigma points are redrawn, as given by equation (5). If set to 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., )
- , value of used when constructing the first set of sigma points, .
Defaults to .
- , value of used when constructing the first set of sigma points, .
Defaults to .
- , value of used when constructing the first set of sigma points, .
Defaults to .
- Value of used when constructing the second set of sigma points, .
Defaults to when and the second set of sigma points are augmented and otherwise.
- Value of used when constructing the second set of sigma points, .
Defaults to .
- Value of used when constructing the second set of sigma points, .
Defaults to .
Constraints:
- or ;
- ;
- when and the second set of sigma points are augmented, otherwise;
- , for .
-
18:
– Integer
Input
-
On entry: length of the options array
ropt.
Constraint:
.
-
19:
– Integer array
Communication 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:
– Integer
Input
-
On entry: the length of the array
icomm. If
licomm is too small and
then
is returned and the minimum value for
licomm and
lrcomm are given by
and
respectively.
Constraint:
.
-
21:
– Real (Kind=nag_wp) array
Communication 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:
– Integer
Input
-
On entry: the length of the array
rcomm. If
lrcomm is too small and
then
is returned and the minimum value for
licomm and
lrcomm are given by
and
respectively.
Suggested value:
, where is the optimal block size. In most cases a block size of will be sufficient.
Constraint:
.
-
23:
– Integer
Input/Output
-
On initial entry:
ifail must be set to
,
or
to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value
or
is recommended. If message printing is undesirable, then the value
is recommended. Otherwise, the value
is recommended since useful values can be provided in some output arguments even when
on exit.
When the value or is used it is essential to test the value of ifail on exit.
On final exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: , , or .
-
On entry, .
Constraint: .
-
mx has changed between calls.
On intermediate entry,
.
On initial entry,
.
-
On entry, .
Constraint: .
-
my has changed between calls.
On intermediate entry,
.
On initial entry,
.
-
On entry, and .
Constraint: or .
-
On entry, and .
Constraint: .
-
On entry, and .
Constraint: .
-
On entry, augmented sigma points requested, and .
Constraint: .
-
On entry, redrawn sigma points requested, and .
Constraint: .
-
n has changed between calls.
On intermediate entry,
.
On intermediate exit,
.
-
On entry, and .
Constraint: .
-
On entry, and .
Constraint: if , .
-
On entry, and .
Constraint: if , .
-
On entry, .
Constraint: or .
-
On entry, .
Constraint: .
-
On entry, .
Constraint: .
-
On entry, .
Constraint: .
-
icomm has been corrupted between calls.
-
On entry,
.
Constraint:
.
icomm is too small to return the required array sizes.
-
On entry,
and
.
Constraint:
and
.
The minimum required values for
licomm and
lrcomm are returned in
and
respectively.
-
rcomm has been corrupted between calls.
-
A weight was negative and it was not possible to downdate the Cholesky factorization.
-
Unable to calculate the Kalman gain matrix.
-
Unable to calculate the Cholesky factorization of the updated state covariance matrix.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See
Section 9 in the Introduction to the NAG Library FL Interface for further information.
7
Accuracy
Not applicable.
8
Parallelism and Performance
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.
As well as implementing the Unscented Kalman Filter,
g13ejf can also be used to apply the Unscented Transform (see
Julier (2002)) to the function
, by setting
and terminating the calling sequence when
rather than
. 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
) 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
and state update function
given by:
where
and
are known constants and
and
are time-dependent knowns. The measurement vector
and measurement function
is given by:
where
and
are known constants. The initial values,
and
, are given by
and the Cholesky factorizations of the error covariance matrices,
and
by
10.1
Program Text
10.2
Program Data
10.3
Program Results
The example described above can be thought of as relating to the movement of a hypothetical robot. The unknown state, , is the position of the robot (with respect to a reference frame) and facing, with giving the and coordinates and the angle (with respect to the -axis) that the robot is facing. The robot has two drive wheels, of radius on an axle of length . During time period the right wheel is believed to rotate at a velocity of and the left at a velocity of . In this example, these velocities are fixed with and . The state update function, , 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 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, , from the -axis. The robot has a sensor that is able to measure , with being the distance to the wall and the angle to the wall. The measurement function gives the expected distance and angle to the wall if the robot's position is given by . Therefore the state space model allows the robot to incorporate the sensor information to update the estimate of its position.