kalmanFilter {pmhtutorial} | R Documentation |
Kalman filter for state estimate in a linear Gaussian state space model
Description
Estimates the filtered state and the log-likelihood for a linear Gaussian
state space model of the form
and
, where
and
denote
independent standard Gaussian random variables, i.e.
.
Usage
kalmanFilter(y, theta, initialState, initialStateCovariance)
Arguments
y |
Observations from the model for |
theta |
The parameters |
initialState |
The initial state. |
initialStateCovariance |
The initial covariance of the state. |
Value
The function returns a list with the elements:
xHatFiltered: The estimate of the filtered state at time
.
logLikelihood: The estimate of the log-likelihood.
Note
See Section 3 in the reference for more details.
Author(s)
Johan Dahlin uni@johandahlin.com
References
Dahlin, J. & Schon, T. B. "Getting Started with Particle Metropolis-Hastings for Inference in Nonlinear Dynamical Models." Journal of Statistical Software, Code Snippets, 88(2): 1–41, 2019.
Examples
# Generates 500 observations from a linear state space model with
# (phi, sigma_e, sigma_v) = (0.5, 1.0, 0.1) and zero initial state.
theta <- c(0.5, 1.0, 0.1)
d <- generateData(theta, noObservations=500, initialState=0.0)
# Estimate the filtered state using Kalman filter
kfOutput <- kalmanFilter(d$y, theta,
initialState=0.0, initialStateCovariance=0.01)
# Plot the estimate and the true state
par(mfrow=c(3, 1))
plot(d$x, type="l", xlab="time", ylab="true state", bty="n",
col="#1B9E77")
plot(kfOutput$xHatFiltered, type="l", xlab="time",
ylab="Kalman filter estimate", bty="n", col="#D95F02")
plot(d$x-kfOutput$xHatFiltered, type="l", xlab="time",
ylab="difference", bty="n", col="#7570B3")