PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_tsa_multi_kalman_sqrt_var (g13ea)
Purpose
nag_tsa_multi_kalman_sqrt_var (g13ea) performs a combined measurement and time update of one iteration of the time-varying Kalman filter using a square root covariance filter.
Syntax
[
s,
k,
h,
ifail] = g13ea(
a,
b,
stq,
q,
c,
r,
s, 'n',
n, 'm',
m, 'l',
l, 'tol',
tol)
[
s,
k,
h,
ifail] = nag_tsa_multi_kalman_sqrt_var(
a,
b,
stq,
q,
c,
r,
s, 'n',
n, 'm',
m, 'l',
l, 'tol',
tol)
Description
The Kalman filter arises from the state space model given by:
where
is the state vector of length
at time
,
is the observation vector of length
at time
, and
of length
and
of length
are the independent state noise and measurement noise respectively.
The estimate of
given observations
to
is denoted by
with state covariance matrix
, while the estimate of
given observations
to
is denoted by
with covariance matrix
. The update of the estimate,
, from time
to time
, is computed in two stages. First, the measurement-update is given by
and
where
is the Kalman gain matrix. The second stage is the time-update for
which is given by
and
where
represents any deterministic control used.
The square root covariance filter algorithm provides a stable method for computing the Kalman gain matrix and the state covariance matrix. The algorithm can be summarised as
where
is an orthogonal transformation triangularizing the left-hand pre-array to produce the right-hand post-array. The relationship between the Kalman gain matrix,
, and
is given by
nag_tsa_multi_kalman_sqrt_var (g13ea) requires the input of the lower triangular Cholesky factors of the noise covariance matrices
and, optionally,
and the lower triangular Cholesky factor of the current state covariance matrix,
, and returns the product of the matrices
and
,
, the Cholesky factor of the updated state covariance matrix
and the matrix
used in the computation of the likelihood for the model.
References
Vanbegin M, van Dooren P and Verhaegen M H G (1989) Algorithm 675: FORTRAN subroutines for computing the square root covariance filter and square root information filter in dense or Hessenberg forms ACM Trans. Math. Software 15 243–256
Verhaegen M H G and van Dooren P (1986) Numerical aspects of different Kalman filter implementations IEEE Trans. Auto. Contr. AC-31 907–917
Parameters
Compulsory Input Parameters
- 1:
– double array
-
lds, the first dimension of the array, must satisfy the constraint
.
The state transition matrix, .
- 2:
– double array
-
lds, the first dimension of the array, must satisfy the constraint
.
The noise coefficient matrix .
- 3:
– logical scalar
-
If
, the state noise covariance matrix
is assumed to be the identity matrix. Otherwise the lower triangular Cholesky factor,
, must be provided in
q.
- 4:
– double array
-
The first dimension,
, of the array
q must satisfy
- if , ;
- otherwise .
The second dimension of the array
q must be at least
if
and at least
if
.
If
,
q must contain the lower triangular Cholesky factor of the state noise covariance matrix,
. Otherwise
q is not referenced.
- 5:
– double array
-
ldm, the first dimension of the array, must satisfy the constraint
.
The measurement coefficient matrix, .
- 6:
– double array
-
ldm, the first dimension of the array, must satisfy the constraint
.
The lower triangular Cholesky factor of the measurement noise covariance matrix .
- 7:
– double array
-
lds, the first dimension of the array, must satisfy the constraint
.
The lower triangular Cholesky factor of the state covariance matrix, .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the first dimension of the arrays
a,
b,
s and the second dimension of the arrays
a,
c,
s. (An error is raised if these dimensions are not equal.)
, the size of the state vector.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the first dimension of the arrays
c,
r and the second dimension of the array
r. (An error is raised if these dimensions are not equal.)
, the size of the observation vector.
Constraint:
.
- 3:
– int64int32nag_int scalar
-
Default:
the second dimension of the array
b.
, the dimension of the state noise.
Constraint:
.
- 4:
– double scalar
Default:
The tolerance used to test for the singularity of
. If
, then
is used instead. The inverse of the condition number of
is estimated by a call to
nag_lapack_dtrcon (f07tg). If this estimate is less than
tol then
is assumed to be singular.
Constraint:
.
Output Parameters
- 1:
– double array
-
The lower triangular Cholesky factor of the state covariance matrix, .
- 2:
– double array
-
The Kalman gain matrix, , premultiplied by the state transition matrix, , .
- 3:
– double array
-
The lower triangular matrix .
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | , |
or | , |
or | , |
or | , |
or | and , |
or | and , |
or | . |
-
-
The matrix is singular.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
The use of the square root algorithm improves the stability of the computations as compared with the direct coding of the Kalman filter. The accuracy will depend on the model.
Further Comments
For models with time-invariant
and
,
nag_tsa_multi_kalman_sqrt_invar (g13eb) can be used.
If
and
are independent multivariate
Normal variates then the log-likelihood for observations
is given by
where
is a constant.
The Cholesky factors of the covariance matrices can be computed using
nag_lapack_dpotrf (f07fd).
Note that the model
can be specified either with
b set to the identity matrix and
and the matrix
input in
q or with
and
b set to
.
The algorithm requires
operations and is backward stable (see
Verhaegen and van Dooren (1986)).
Example
This example first inputs the number of updates to be computed and the problem sizes. The initial state vector and state covariance matrix are input followed by the model matrices
and optionally
. The Cholesky factors of the covariance matrices can be computed if required. The model matrices can be input at each update or only once at the first step. At each update the observed values are input and the residuals are computed and printed and the estimate of the state vector,
, and the deviance are updated. The deviance is
log-likelihood ignoring the constant. After the final update the state covariance matrix is computed from
s and printed along with final estimate of the state vector and the value of the deviance.
The data is for a two-dimensional time series to which a VARMA
has been fitted. For the specification of a VARMA model as a state space model see the
G13 Chapter Introduction. The initial value of
,
, is the solution to
For convenience, the mean of each series is input before the first update and subtracted from the observations before the measurement update is computed.
Open in the MATLAB editor:
g13ea_example
function g13ea_example
fprintf('g13ea example results\n\n');
s = [ 8.2068 2.0599 1.4807 0.3627;
2.0599 7.9645 0.9703 0.2136;
1.4807 0.9703 0.9253 0.2236;
0.3627 0.2136 0.2236 0.0542];
a = [ 0.607, -0.033, 1, 0;
0, 0.543, 0, 1;
0, 0, 0, 0;
0, 0, 0, 0];
b = [ 1, 0;
0, 1;
0.543, 0.125;
0.134, 0.026];
stq = false;
q = [ 2.598, 0.56;
0.560, 5.33];
c = [ 1, 0, 0, 0;
0, 1, 0, 0];
r = [ 0, 0;
0, 0];
s = chol(s,'lower');
q = chol(q,'lower');
n = size(s,1);
m = 2;
x = zeros(n,1);
ym = [-1.49 7.34; -1.62 6.35; 5.20 6.96; 6.23 8.54; 6.21 6.62;
5.86 4.97; 4.09 4.55; 3.18 4.81; 2.62 4.75; 1.49 4.76;
1.17 10.88; 0.85 10.01; -0.35 11.62; 0.24 10.36; 2.44 6.40;
2.58 6.24; 2.04 7.93; 0.40 4.04; 2.26 3.73; 3.34 5.60;
5.09 5.35; 5.00 6.81; 4.78 8.27; 4.11 7.68; 3.45 6.65;
1.65 6.08; 1.29 10.25; 4.09 9.14; 6.32 17.75; 7.50 13.30;
3.89 9.63; 1.58 6.80; 5.21 4.08; 5.25 5.06; 4.93 4.94;
7.38 6.65; 5.87 7.94; 5.81 10.76; 9.68 11.89; 9.07 5.85;
7.29 9.01; 7.84 7.50; 7.55 10.02; 7.32 10.38; 7.97 8.15;
7.76 8.37; 7.00 10.73; 8.35 12.14];
ymean = [4.404; 7.991];
ny = size(ym,1);
fprintf(' Residuals\n\n');
dev = 0;
for j = 1:ny
y = ym(j,:)';
[s, K, H, ifail] = g13ea( ...
a, b, stq, q, c, r, s);
y = y - ymean;
y = y-c*x;
x = a*x + K*y;
fprintf('%10.4f%10.4f\n',y);
y = H\y;
dev = dev + dot(y,y);
for i = 1:m
dev = dev + 2*log(H(i,i));
end
end
p = s*s';
fprintf('\nFinal x\n\n')
disp (x');
[ifail] = x04ca(...
'Lower','N', p, 'Final Value of P');
fprintf('\nDeviance = %13.4e\n', dev);
g13ea example results
Residuals
-5.8940 -0.6510
-1.4710 -1.0407
5.1658 0.0447
-1.3280 0.4580
1.3652 -1.5066
-0.2337 -2.4192
-0.8685 -1.7065
-0.4624 -1.1519
-0.7510 -1.4218
-1.3526 -1.3335
-0.6707 4.8593
-1.7389 0.4138
-1.6376 2.7549
-0.6137 0.5463
0.9067 -2.8093
-0.8255 -0.9355
-0.7494 1.0247
-2.2922 -3.8441
1.8812 -1.7085
-0.7112 -0.2849
1.6747 -1.2400
-0.6619 0.0609
0.3271 1.0074
-0.8165 -0.5325
-0.2759 -1.0489
-1.9383 -1.1186
-0.3131 3.5855
1.3726 -0.1289
1.4153 8.9545
0.3672 -0.4126
-2.3659 -1.2823
-1.0130 -1.7306
3.2472 -3.0836
-1.1501 -1.1623
0.6855 -1.2751
2.3432 0.2570
-1.6892 0.3565
1.3871 3.0138
3.3840 2.1312
-0.5118 -4.7670
0.8569 2.3741
0.9558 -1.2209
0.6778 2.1993
0.4304 1.1393
1.4987 -1.2255
0.5361 0.1237
0.2649 2.4582
2.0095 2.5623
Final x
3.6698 2.5888 0 0
Final Value of P
1 2 3 4
1 2.5980
2 0.5600 5.3300
3 1.4807 0.9703 0.9253
4 0.3627 0.2136 0.2236 0.0542
Deviance = 2.2287e+02
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015