The IMSL_KALMAN procedure performs Kalman filtering and evaluates the likelihood function for the state-space model.

Routine IMSL_KALMAN is based on a recursive algorithm given by Kalman (1960), which has come to be known as the Kalman filter. The underlying model is known as the state-space model. The model is specified stage by stage where the stages generally correspond to time points at which the observations become available. The routine IMSL_KALMAN avoids many of the computations and storage requirements that would be necessary if one were to process all the data at the end of each stage in order to estimate the state vector. This is accomplished by using previous computations and retaining in storage only those items essential for processing of future observations.

The notation used here follows that of Sallas and Harville (1981). Let yk (input in keyword Y) be the nk ° 1 vector of observations that become available at time k. The subscript k is used here rather than t, which is more customary in time series, to emphasize that the model is expressed in stages k = 1, 2, ... and that these stages need not correspond to equally spaced time points. In fact, they need not correspond to time points of any kind. The observation equation for the state-space model is:

yk = Zkbk + ek k = 1, 2, ...

Here, Zk is an nk ° q known matrix and bk is the q ° 1 state vector. The state vector bk is allowed to change with time in accordance with the state equation:

bk+1 = Tk+1 bk + wk+1 k = 1, 2, ... starting with b1 = μ1 + w1.

The change in the state vector from time k to k + 1 is explained in part by the transition matrix Tk+1 (the identity matrix by default, or optionally input using keyword T_matrix), which is assumed known. It is assumed that the q-dimensional wks (k = 1, 2, ... K) are independently distributed multivariate normal with mean vector 0 and variance-covariance matrix σ2Qk, that the nk-dimensional eks (k = 1, 2, ... K) are independently distributed multivariate normal with mean vector 0 and variance-covariance matrix σ2Rk, and that the wks and eks are independent of each other. Here, μ1 is the mean of b1 and is assumed known, σ2 is an unknown positive scalar. Qk+1 (input in Q) and Rk (input in keyword R) are assumed known.

Denote the estimator of the realization of the state vector bk given the observations y1, y2, ..., yj by:

By definition, the mean squared error matrix for:

is:

At the time of the k-th invocation, we have:

and:

which were computed from the (k-1)-st invocation, input in b and Covb, respectively. During the k-th invocation, routine IMSL_KALMAN computes the filtered estimate:

along with Ck|k. These quantities are given by the update equations:

where:

and where:

Here, vk (stored in v) is the one-step-ahead prediction error, and σ2Hk is the variance- covariance matrix for vk. Hk is stored in COVV. The “start-up values” needed on the first invocation of IMSL_KALMAN are:

and C1|0 = Q1 input via b and Covb, respectively. Computations for the k-th invocation are completed by IMSL_KALMAN computing the one-step-ahead estimate:

along with Ck+1|k given by the prediction equations:

If both the filtered estimates and one-step-ahead estimates are needed at each time point, IMSL_KALMAN can be invoked twice for each time point, first without T_MATRIX and Q_MATRIX to produce:

and Ck|k, and second without keywords Y, Z, and R to produce:

and Ck+1|k (Without T_MATRIX and Q_MATRIX , prediction equations are skipped. Without keywords Y, Z, and R, update equations are skipped.).

Often, it is desired to the estimate of the state vector more than one-step-ahead, i.e., an estimate of:

is needed where k > j + 1. At time j, IMSL_KALMAN is invoked with keywords Y, Z, and R to compute:

Subsequent invocations of IMSL_KALMAN without Y, Z, and R can compute:

Computations for:

and Ck|j assume the variance-covariance matrices of the errors in the observation equation and state equation are known up to an unknown positive scalar multiplier, σ2. The maximum likelihood estimate of σ2 based on the observations y1, y2, …, ym, is given by:

where:

N and SS are the input/output arguments n and ss.

If σ2 is known, the Rks and Qks can be input as the variance-covariance matrices exactly. The earlier discussion is then simplified by letting σ2 = 1.

In practice, the matrices Tk, Qk, and Rk are generally not completely known. They may be known functions of an unknown parameter vector θ. In this case, IMSL_KALMAN can be used in conjunction with an optimization program (see IMSL_FMINV) to obtain a maximum likelihood estimate of θ. The natural logarithm of the likelihood function for y1, y2, ..., ym differs by no more than an additive constant from:

(Harvey 1981, page 14, equation 2.21).

Here:

(stored in Alndet) is the natural logarithm of the determinant of V where σ2V is the variance-covariance matrix of the observations.

Minimization of -2L(θ, σ2; y1, y2, ..., ym) over all θ and σ2 produces maximum likelihood estimates. Equivalently, minimization of -2Lc(θ; y1, y2, ..., ym) where:

produces maximum likelihood estimates:

The minimization of -2Lc(θ; y1, y2, ..., ym) instead of -2L(θ, σ2; y1, y2, ..., ym), reduces the dimension of the minimization problem by one. The two optimization problems are equivalent since:

minimizes -2L(θ, σ2; y1, y2, ..., ym) for all θ, consequently:

can be substituted for σ2 in L(θ, σ2; y1, y2, …, ym) to give a function that differs by no more than an additive constant from Lc(θ; y1, y2, ..., ym).

The earlier discussion assumed Hk to be non-singular. If Hk is singular, a modification for singular distributions described by Rao (1973, pages 527–528) is used. The changes in the preceding discussion are as follows:

1. Replace:

by a generalized inverse.

2. Replace det(Hk) by the product of the nonzero eigenvalues of Hk.
3. Replace N by:

Maximum likelihood estimation of parameters in the Kalman filter is discussed by Sallas and Harville (1988) and Harvey (1981, pages 111–113).

## Example

Routine IMSL_KALMAN is used to compute the filtered estimates and one-step- ahead estimates for a scalar problem discussed by Harvey (1981, pages 116–117). The observation equation and state equation are given by:

where the eks are identically and independently distributed normal with mean 0 and variance σ2, the wks are identically and independently distributed normal with mean 0 and variance 4σ2, and b1 is distributed normal with mean 4 and variance 16σ2. Two invocations of IMSL_KALMAN are needed for each time point in order to compute the filtered estimate and the one-step-ahead estimate. The first invocation does not use the keywords T_MATRIX and Q_MATRIX so that the prediction equations are skipped in the computations. The update equations are skipped in the computations in the second invocation.

This example also computes the one-step-ahead prediction errors. Harvey (1981, page 117) contains a misprint for the value v4 that he gives as 1.197. The correct value of v4 = 1.003 is computed by IMSL_KALMAN.

Note that this example is in the form of an IDL Advanced Math and Stats procedure, with the output following the procedure.

`.RUN`
`PRO EX_KALMAN`
`  z = 1`
`  r = 1`
`  q = 4`
`  t = 1`
`  b = 4`
`  covb = 16`
`  ydata = [4.4, 4, 3.5, 4.6]`
`  n = 0`
`  ss = 0`
`  alndet = 0`
`  FORMAT = '(2I4, 2F8.3, I4, 4F8.3)'`
`  PRINT, '	k	j	b	covb	n	ss	alndet	v covv'`
`  FOR i = 0, 3 DO BEGIN`
`    y = ydata(i)`
`     `
`    ; Update`
`    IMSL_KALMAN, b, covb, n, ss, alndet, Y = y, Z = Z, R = r, \$`
`      v = v, covv = covv`
`    PRINT, i, i, b, covb, n, ss, alndet, v, covv, \$`
`      FORMAT = format`
`   `
`    ; Predict`
`    IMSL_KALMAN, b, covb, n, ss, alndet, t_matrix = t, q = q`
`    PRINT, i+1, i, b, covb, n, ss, alndet, v, covv, \$`
`      FORMAT = format`
`  END`
`END `

IDL prints:

## Syntax

IMSL_KALMAN, B, Covb, N, Ss, Alndet [, COVV=array] [, Q_MATRIX=array] [, R=array] [, T_MATRIX=array] [, TOLERANCE=value] [, V=array] [, Y=array]

## Arguments

### Alndet

Named variable containing the natural log of the product of the nonzero eigenvalues of P where P * σ2 is the variance-covariance matrix of the observations. Although Alndet is computed, IMSL_KALMAN avoids the explicit computation of P. alndet must be initialized to zero before the first call to IMSL_KALMAN. In the usual case when P is non-singular, Alndet is the natural log of the determinant of P.

### B

One dimensional array of containing the estimated state vector. The input is the estimated state vector at time k given the observations through time k – 1. The output is the estimated state vector at time k + 1 given the observations through time k. On the first call to IMSL_KALMAN, the input b must be the prior mean of the state vector at time.

### Covb

Two dimensional array of size N_ELEMENTS(b) by N_ELEMENTS(b) such that covb* σ2 is the mean squared error matrix for b. Before the first call to IMSL_KALMAN, covb* σ2 must equal the variance-covariance matrix of the state vector.

### N

Named variable containing the rank of the variance-covariance matrix for all the observations. n must be initialized to zero before the first call to IMSL_KALMAN. In the usual case when the variance-covariance matrix is non- singular, n equals the sum of the N_ELEMENTS(Y) from the invocations to IMSL_KALMAN. See the keyword section below for the definition of Y.

### Ss

Named variable containing the generalized sum of squares. ss must be initialized to zero before the first call to IMSL_KALMAN. The estimate of σ2 is given by:

## Keywords

### COVV (optional)

Two dimensional array if size N_ELEMENTS(Y) by N_ELEMENTS(Y) containing a matrix such that Covv * σ2 is the variance-covariance matrix of v.

### Q_MATRIX (optional)

Two dimensional array if size N_ELEMENTS(b) by N_ELEMENTS(b) matrix such that Q_matrix * σ2 is the variance-covariance matrix of the error vector in the state equation. Default: There is no error term in the state equation

### R (optional)

Two dimensional array if size N_ELEMENTS(Y) by N_ELEMENTS(Y) containing the matrix such that R * σ2 is the variance-covariance matrix of errors in the observation equation. Keywords Y, Z and R indicate an update step and must be used together.

### T_MATRIX (optional)

Two dimensional array if size N_ELEMENTS(b) by N_ELEMENTS(b) containing the transition matrix in the state equation. Default: identity matrix

### TOLERANCE (optional)

Tolerance used in determining linear dependence. Default: 100*eps where eps is machine precision.

### V (optional)

One dimensional array of length N_ELEMENTS(Y) containing the one-step-ahead prediction error.

### Y (optional)

One dimensional array containing the observations. Keywords Y, Z and R indicate an update step and must be used together.

## Version History

 6.4 Introduced