# NAG FL Interfaceg13eaf (multi_​kalman_​sqrt_​var)

## 1Purpose

g13eaf performs a combined measurement and time update of one iteration of the time-varying Kalman filter using a square root covariance filter.

## 2Specification

Fortran Interface
 Subroutine g13eaf ( n, m, l, a, lds, b, stq, q, ldq, c, ldm, r, s, k, h, tol, iwk, wk,
 Integer, Intent (In) :: n, m, l, lds, ldq, ldm Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iwk(m) Real (Kind=nag_wp), Intent (In) :: a(lds,n), b(lds,l), q(ldq,*), c(ldm,n), r(ldm,m), tol Real (Kind=nag_wp), Intent (Inout) :: s(lds,n), k(lds,m), h(ldm,m) Real (Kind=nag_wp), Intent (Out) :: wk((n+m)*(n+m+l)) Logical, Intent (In) :: stq
#include <nag.h>
 void g13eaf_ (const Integer *n, const Integer *m, const Integer *l, const double a[], const Integer *lds, const double b[], const logical *stq, const double q[], const Integer *ldq, const double c[], const Integer *ldm, const double r[], double s[], double k[], double h[], const double *tol, Integer iwk[], double wk[], Integer *ifail)
The routine may be called by the names g13eaf or nagf_tsa_multi_kalman_sqrt_var.

## 3Description

The Kalman filter arises from the state space model given by:
 $Xi+1=AiXi+BiWi, VarWi=Qi Yi=CiXi+Vi, VarVi=Ri$
where ${X}_{i}$ is the state vector of length $n$ at time $i$, ${Y}_{i}$ is the observation vector of length $m$ at time $i$, and ${W}_{i}$ of length $l$ and ${V}_{i}$ of length $m$ are the independent state noise and measurement noise respectively.
The estimate of ${X}_{i}$ given observations ${Y}_{1}$ to ${Y}_{i-1}$ is denoted by ${\stackrel{^}{X}}_{i\mid i-1}$ with state covariance matrix $\mathrm{Var}\left({\stackrel{^}{X}}_{i\mid i-1}\right)={P}_{i\mid i-1}={S}_{i}{S}_{i}^{\mathrm{T}}$, while the estimate of ${X}_{i}$ given observations ${Y}_{1}$ to ${Y}_{i}$ is denoted by ${\stackrel{^}{X}}_{i\mid i}$ with covariance matrix $\mathrm{Var}\left({\stackrel{^}{X}}_{i\mid i}\right)={P}_{i\mid i}$. The update of the estimate, ${\stackrel{^}{X}}_{i\mid i-1}$, from time $i$ to time $\left(i+1\right)$, is computed in two stages. First, the measurement-update is given by
 $X^i∣i=X^i∣i-1+KiYi-CiX^i∣i-1$ (1)
and
 $Pi∣i=I-KiCiPi∣i-1$ (2)
where ${K}_{i}={P}_{i\mid i-1}{C}_{i}^{\mathrm{T}}{\left[{C}_{i}{P}_{i\mid i-1}{C}_{i}^{\mathrm{T}}+{R}_{i}\right]}^{-1}$ is the Kalman gain matrix. The second stage is the time-update for $X$ which is given by
 $X^i+1∣i=AiX^i∣i+DiUi$ (3)
and
 $Pi+1∣i=AiPi∣i AiT +BiQi BiT$ (4)
where ${D}_{i}{U}_{i}$ 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
 $Ri1/2 CiSi 0 0 AiSi BiQi1/2 U= Hi1/2 0 0 Gi Si+1 0$ (5)
where $U$ is an orthogonal transformation triangularizing the left-hand pre-array to produce the right-hand post-array. The relationship between the Kalman gain matrix, ${K}_{i}$, and ${G}_{i}$ is given by
 $AiKi=Gi Hi1/2 -1.$
g13eaf requires the input of the lower triangular Cholesky factors of the noise covariance matrices ${R}_{i}^{1/2}$ and, optionally, ${Q}_{i}^{1/2}$ and the lower triangular Cholesky factor of the current state covariance matrix, ${S}_{i}$, and returns the product of the matrices ${A}_{i}$ and ${K}_{i}$, ${A}_{i}{K}_{i}$, the Cholesky factor of the updated state covariance matrix ${S}_{i+1}$ and the matrix ${H}_{i}^{1/2}$ used in the computation of the likelihood for the model.
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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the size of the state vector.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{m}$Integer Input
On entry: $m$, the size of the observation vector.
Constraint: ${\mathbf{m}}\ge 1$.
3: $\mathbf{l}$Integer Input
On entry: $l$, the dimension of the state noise.
Constraint: ${\mathbf{l}}\ge 1$.
4: $\mathbf{a}\left({\mathbf{lds}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the state transition matrix, ${A}_{i}$.
5: $\mathbf{lds}$Integer Input
On entry: the first dimension of the arrays a, b, s and k as declared in the (sub)program from which g13eaf is called.
Constraint: ${\mathbf{lds}}\ge {\mathbf{n}}$.
6: $\mathbf{b}\left({\mathbf{lds}},{\mathbf{l}}\right)$Real (Kind=nag_wp) array Input
On entry: the noise coefficient matrix ${B}_{i}$.
7: $\mathbf{stq}$Logical Input
On entry: if ${\mathbf{stq}}=\mathrm{.TRUE.}$, the state noise covariance matrix ${Q}_{i}$ is assumed to be the identity matrix. Otherwise the lower triangular Cholesky factor, ${Q}_{i}^{1/2}$, must be provided in q.
8: $\mathbf{q}\left({\mathbf{ldq}},*\right)$Real (Kind=nag_wp) array Input
Note: the second dimension of the array q must be at least ${\mathbf{l}}$ if ${\mathbf{stq}}=\mathrm{.FALSE.}$.
On entry: if ${\mathbf{stq}}=\mathrm{.FALSE.}$, q must contain the lower triangular Cholesky factor of the state noise covariance matrix, ${Q}_{i}^{1/2}$. Otherwise q is not referenced.
9: $\mathbf{ldq}$Integer Input
On entry: the first dimension of the array q as declared in the (sub)program from which g13eaf is called.
Constraints:
• if ${\mathbf{stq}}=\mathrm{.FALSE.}$, ${\mathbf{ldq}}\ge {\mathbf{l}}$;
• otherwise ${\mathbf{ldq}}\ge 1$.
10: $\mathbf{c}\left({\mathbf{ldm}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the measurement coefficient matrix, ${C}_{i}$.
11: $\mathbf{ldm}$Integer Input
On entry: the first dimension of the arrays c, r and h as declared in the (sub)program from which g13eaf is called.
Constraint: ${\mathbf{ldm}}\ge {\mathbf{m}}$.
12: $\mathbf{r}\left({\mathbf{ldm}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: the lower triangular Cholesky factor of the measurement noise covariance matrix ${R}_{i}^{1/2}$.
13: $\mathbf{s}\left({\mathbf{lds}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: the lower triangular Cholesky factor of the state covariance matrix, ${S}_{i}$.
On exit: the lower triangular Cholesky factor of the state covariance matrix, ${S}_{i+1}$.
14: $\mathbf{k}\left({\mathbf{lds}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: the Kalman gain matrix, ${K}_{i}$, premultiplied by the state transition matrix, ${A}_{i}$, ${A}_{i}{K}_{i}$.
15: $\mathbf{h}\left({\mathbf{ldm}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: the lower triangular matrix ${H}_{i}^{1/2}$.
16: $\mathbf{tol}$Real (Kind=nag_wp) Input
On entry: the tolerance used to test for the singularity of ${H}_{i}^{1/2}$. If , then is used instead. The inverse of the condition number of ${H}^{1/2}$ is estimated by a call to f07tgf. If this estimate is less than tol then ${H}^{1/2}$ is assumed to be singular.
Suggested value: ${\mathbf{tol}}=0.0$.
Constraint: ${\mathbf{tol}}\ge 0.0$.
17: $\mathbf{iwk}\left({\mathbf{m}}\right)$Integer array Workspace
18: $\mathbf{wk}\left(\left({\mathbf{n}}+{\mathbf{m}}\right)×\left({\mathbf{n}}+{\mathbf{m}}+{\mathbf{l}}\right)\right)$Real (Kind=nag_wp) array Workspace
19: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l}}\ge 1$.
On entry, ${\mathbf{ldm}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldm}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{ldq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldq}}\ge 1$.
On entry, ${\mathbf{ldq}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldq}}\ge {\mathbf{l}}$.
On entry, ${\mathbf{lds}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lds}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}\ge 0.0$.
${\mathbf{ifail}}=2$
The matrix ${H}_{i}^{1/2}$ is singular.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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.

## 8Parallelism and Performance

g13eaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13eaf 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.

For models with time-invariant $A,B$ and $C$, g13ebf can be used.
The estimate of the state vector ${\stackrel{^}{X}}_{i+1\mid i}$ can be computed from ${\stackrel{^}{X}}_{i\mid i-1}$ by
 $X^i+1∣i=AiX^i∣i-1+AKiri$
where
 $ri=Yi-CiX^i∣i- 1$
are the independent one step prediction residuals. The required matrix-vector multiplications can be performed by f06paf.
If ${W}_{i}$ and ${V}_{i}$ are independent multivariate Normal variates then the log-likelihood for observations $i=1,2,\dots ,t$ is given by
 $lθ = κ - 12 ∑ i=1 t l n detHi - 12 ∑ i=1 t Yi - Ci X i∣i-1 T H i -1 Yi - Ci X i∣i-1$
where $\kappa$ is a constant.
The Cholesky factors of the covariance matrices can be computed using f07fdf.
Note that the model
 $Xi+1=AiXi+Wi, VarWi=Qi Yi=CiXi+Vi, VarVi=Ri$
can be specified either with b set to the identity matrix and ${\mathbf{stq}}=\mathrm{.FALSE.}$ and the matrix ${Q}^{1/2}$ input in q or with ${\mathbf{stq}}=\mathrm{.TRUE.}$ and b set to ${Q}^{1/2}$.
The algorithm requires $\frac{7}{6}{n}^{3}+{n}^{2}\left(\frac{5}{2}m+l\right)+n\left(\frac{1}{2}{l}^{2}+{m}^{2}\right)$ operations and is backward stable (see Verhaegen and van Dooren (1986)).

## 10Example

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 ${A}_{i},{B}_{i},{C}_{i},{R}_{i}$ and optionally ${Q}_{i}$. 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, ${\stackrel{^}{X}}_{i\mid i-1}$, and the deviance are updated. The deviance is $-2×\text{}$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$\left(1,1\right)$ has been fitted. For the specification of a VARMA model as a state space model see the G13 Chapter Introduction. The initial value of $P$, ${P}_{0}$, is the solution to
 $P0=A1P0 A1T +B1Q1 B1T .$
For convenience, the mean of each series is input before the first update and subtracted from the observations before the measurement update is computed.

### 10.1Program Text

Program Text (g13eafe.f90)

### 10.2Program Data

Program Data (g13eafe.d)

### 10.3Program Results

Program Results (g13eafe.r)