public class KalmanFilter extends Object implements Serializable, Cloneable
Class KalmanFilter
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 statespace model. The model is
specified stage by stage where the stages generally correspond to time
points at which the observations become available. KalmanFilter
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
(input in y
using method
update
) be the 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
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
statespace model is
Here, (input in z
using method
update
) is an known matrix and
is the state vector. The
state vector is allowed to change with time in
accordance with the state equation
starting with .
The change in the state vector from time k to
k + 1 is explained in part by the
transition matrix (the identity matrix
by default, or optionally using method setTransitionMatrix
), which is assumed known. It is
assumed that the qdimensional
are independently distributed multivariate normal with mean vector 0 and
variancecovariance matrix , that the
dimensional
are independently distributed multivariate normal with mean vector 0 and
variancecovariance matrix , and that the
and are independent of
each other. Here, is the mean of
and is assumed known,
is an unknown positive scalar. (input in
Q
) and (input in R
) are
assumed known.
Denote the estimator of the realization of the state vector given the observations by
By definition, the mean squared error matrix for
is
At the time of the kth invocation, we have
and
, which were computed
from the k1st invocation, input in b
and
covb
, respectively. During the kth invocation,
KalmanFilter
computes the filtered estimate
along with . These quantities are given by the update equations:
where
and where
Here, (stored in getPredictionError
) is the onestepahead
prediction error, and is the
variancecovariance matrix for .
is obtained from method getCovV
. The "startup values"
needed on the first invocation of KalmanFilter
are
and input via
b
and covb
, respectively. Computations for the
kth invocation are completed by KalmanFilter
computing
the onestepahead estimate
along with given by the prediction equations:
If both the filtered estimates and onestepahead estimates are needed
by the user at each time point, KalmanFilter
can be used twice
for each time pointfirst without methods SetTransitionMatrix
and
setQ
to produce
and , and second without method
update
to produce
and (Without methods
SetTransitionMatrix
and setQ
, the prediction equations are
skipped. Without method update
, the update equations are
skipped.).
Often, one desires the estimate of the state vector more than onestepahead, i.e., an estimate of
is needed where . At time j,
KalmanFilter
is invoked with method update
to
compute
Subsequent invocations of KalmanFilter
without method
update
can compute
Computations for
and assume the variancecovariance matrices of the errors in the observation equation and state equation are known up to an unknown positive scalar multiplier, . The maximum likelihood estimate of based on the observations , is given by
where
N and SS are input arguments rank
and
SumofSquares
. Updated values are obtained from methods getRank
and getSumofSquares
If is known, the and can be input as the variancecovariance matrices exactly. The earlier discussion is then simplified by letting .
In practice, the matrices ,
, and are
generally not completely known. They may be known functions of an unknown
parameter vector . In this case,
KalmanFilter
can be used in conjunction with an optimization
class (see MinUnconMultiVar
, JMSL Math package), to
obtain a maximum likelihood estimate of . The
natural logarithm of the likelihood function for
differs by no more than an
additive constant from
(Harvey 1981, page 14, equation 2.21).
Here,
(input in logDeterminant
, updated by getLogDeterminant
) is the natural logarithm of the determinant of
V where is the variancecovariance
matrix of the observations.
Minimization of over all and produces maximum likelihood estimates. Equivalently, minimization of where
produces maximum likelihood estimates
Minimization of instead of , reduces the dimension of the minimization problem by one. The two optimization problems are equivalent since
minimizes for all , consequently,
can be substituted for in to give a function that differs by no more than an additive constant from .
The earlier discussion assumed to be nonsingular. If is singular, a modification for singular distributions described by Rao (1973, pages 527528) is used. The necessary changes in the preceding discussion are as follows:
Maximum likelihood estimation of parameters in the Kalman filter is discussed by Sallas and Harville (1988) and Harvey (1981, pages 111113).
Constructor and Description 

KalmanFilter(double[] b,
double[][] covb,
int rank,
double sumOfSquaress,
double logDeterminant)
Constructor for
KalmanFilter . 
Modifier and Type  Method and Description 

void 
filter()
Performs Kalman filtering and evaluates the likelihood function for
the statespace model.

double[][] 
getCovB()
Returns the mean squared error matrix for
b divided by sigma squared. 
double[][] 
getCovV()
Returns the variancecovariance matrix of v divided by sigma squared.

double 
getLogDeterminant()
Returns the natural log of the product of the nonzero eigenvalues of P
where P * sigma^{2} is the variancecovariance matrix
of the observations.

double[] 
getPredictionError()
Returns the onestepahead prediction error.

int 
getRank()
Returns the rank of the variancecovariance matrix for all the observations.

double[] 
getStateVector()
Returns the estimated state vector at time k + 1 given the observations
through time k.

double 
getSumOfSquares()
Returns the generalized sum of squares.

void 
resetQ()
Removes the Q matrix.

void 
resetTransitionMatrix()
Removes the transition matrix.

void 
resetUpdate()
Do not perform computation of the update equations.

void 
setQ(double[][] q)
Sets the Q matrix.

void 
setTolerance(double tolerance)
Sets the tolerance used in determining linear dependence.

void 
setTransitionMatrix(double[][] t)
Sets the transition matrix.

void 
update(double[] y,
double[][] z,
double[][] r)
Performs computation of the update equations.

public KalmanFilter(double[] b, double[][] covb, int rank, double sumOfSquaress, double logDeterminant)
KalmanFilter
.b
 A double
array containing the estimated state vector.
b
is the estimated state vector at time k
given
the observations through time k
1.covb
 A double
matrix of size b.length
by b.length
such that covb
*
is
the mean squared error matrix for b
.rank
 An int
scalar containing the rank of the
variancecovariance matrix for all the observations.sumOfSquaress
 A double
scalar containing the
generalized sum of squares.logDeterminant
 A double
scalar containing the natural log
of the product of the nonzero eigenvalues of P where
P
* is the variancecovariance
matrix of the observations.IllegalArgumentException
 is thrown if the dimensions of b
,
and covb
are not consistent.public final void filter()
public double[][] getCovB()
b
divided by sigma squared.double
matrix of size b.length
by b.length
such that covb
*
is the mean squared error
matrix for b
.public double[][] getCovV()
double
matrix containing a
y.length by y.length
matrix such that
covv
* is the
variancecovariance matrix of the onestepahead prediction error, getPredictionError
.public double getLogDeterminant()
double
scalar containing the natural log
of the product of the nonzero eigenvalues of P where
P
* is the variancecovariance
matrix of the observations. In the usual case when P is
nonsingular, logDeterminant
is the natural log of the
determinant of P.public double[] getPredictionError()
double
array of size y.length
containing the onestepahead prediction error.public int getRank()
int
scalar containing the rank of the
variancecovariance matrix for all the observations.public double[] getStateVector()
double
array containing the estimated
state vector at time k + 1 given the observations through
time k.public double getSumOfSquares()
double
scalar containing the generalized
sum of squares. The estimate of is given by
sumOfSquares / rank
.public void resetQ()
public void resetTransitionMatrix()
public void resetUpdate()
public void setQ(double[][] q)
q
 A double
matrix containing the
b.length by b.length
matrix such that
q
* is the variancecovariance matrix of the
error vector in the state equation.
Default: There is no error term in the state equation.public void setTolerance(double tolerance)
tolerance
 A double
scalar containing the tolerance
used in determining linear dependence.
Default: tolerance
= 100.0*2.2204460492503131e16.public void setTransitionMatrix(double[][] t)
t
 A double
matrix containing the
b.length by b.length
transition matrix in the
state equation. Default: t
= identity matrixpublic void update(double[] y, double[][] z, double[][] r)
y
 A double
array containing the observations.z
 A double
matrix containing the
y.length by b.length
matrix relating the observations
to the state vector in the observation equation.r
 A double
matrix containing the
y.length by y.length
matrix such that
r
* is the variancecovariance matrix of
errors in the observation equation.
is a positive unknown scalar. Only elements in the upper
triangle of r
are referenced.Copyright © 19702015 Rogue Wave Software
Built October 13 2015.