Documentation |
X = filter(Mdl,Y) performs forward recursion of the fully specified, linear state-space model Mdl to estimate filtered states (X). That is, filter applies the Kalman filter using the state-space model Mdl and the observed responses Y to estimate X.
X = filter(Mdl,Y,Name,Value) performs forward recursion of the state-space model Mdl with additional options specified by one or more Name,Value pair arguments. If Mdl is not fully specified, then you must set the unknown parameters to known scalars using the Name,Value pair argument Params.
[X,logL,Output] = filter(___) additionally returns the loglikelihood value (logL) and an output structure array (Output) using any of the input arguments in the previous syntaxes. Output contains:
Filtered and forecasted states
Estimated covariance matrices of the filtered and forecasted states
Loglikelihood value
Forecasted observations and its estimated covariance matrix
Adjusted Kalman gain
Vector indicating which data the software used to filter
Consider obtaining the 1-step-ahead states forecast for period t + 1 using all information up to period t. The adjusted Kalman gain ($${K}_{adj,t}$$) is the amount of weight put on the estimated observation innovation for period t ($${\widehat{\epsilon}}_{t}$$) as compared to the 2-step-ahead state forecast ($${\widehat{x}}_{t+1|t-1}$$).
That is,
$${\widehat{x}}_{t+1|t}={A}_{t}{\widehat{x}}_{t|t}={A}_{t}{\widehat{x}}_{t|t-1}+{A}_{t}{K}_{t}{\widehat{\epsilon}}_{t}={\widehat{x}}_{t+1|t-1}+{K}_{adj,t}{\widehat{\epsilon}}_{t}.$$
States forecasts at period t, updated using all information (for example the observed responses) up to period t.
The m_{t}-by-1 vector of filtered states at period t is $${x}_{t|t}=E\left({x}_{t}|{y}_{t},\mathrm{...},{y}_{1}\right)$$. The estimated vector of filtered states is
$${\widehat{x}}_{t|t}={\widehat{x}}_{t|t-1}+{K}_{t}{\widehat{\epsilon}}_{t},$$
where:
$${\widehat{x}}_{t|t-1}$$ is the vector of state forecasts at period t using the observed responses from periods 1 through t – 1.
K_{t} is the m_{t}-by-h_{t} raw Kalman gain matrix for period t.
$${\widehat{\epsilon}}_{t}={y}_{t}-{C}_{t}{\widehat{x}}_{t|t-1}$$ is the h_{t}-by-1 vector of estimated observation innovations at period t.
In other words, the filtered states at period t are the forecasted states at period t plus an adjustment based on the trustworthiness of the observation. Trustworthy observations have very little corresponding observation innovation variance (for example, the maximum eigenvalue of D_{t}D_{t}′ is relatively small). Consequently, for a given estimated observation innovation, the term $${K}_{t}{\widehat{\epsilon}}_{t}$$ has a higher impact on the values of the filtered states than untrustworthy observations.
At period t, the filtered states have variance-covariance matrix
$${P}_{t|t}={P}_{t|t-1}-{K}_{t}{C}_{t}{P}_{t|t-1}^{\prime},$$
where $${P}_{t|t-1}$$ is the estimated variance-covariance matrix of the state forecasts at period t, given all information up to period t – 1.
s-step-ahead, forecasted observations are estimates of the observations at period t using all information (for example, the observed responses) up to period t – s.
The n_{t}-by-1 vector of 1-step-ahead, forecasted observations at period t is $${y}_{t|t-1}=E\left({y}_{t}|{y}_{t-1},\mathrm{...},{y}_{1}\right)$$. The estimated vector of forecasted observations is
$${\widehat{y}}_{t|t-1}={C}_{t}{\widehat{x}}_{t|t-1},$$
where $${\widehat{x}}_{t|t-1}$$ is the m_{t}-by-1 estimated vector of state forecasts at period t.
At period t, the 1-step-ahead, forecasted observations have variance-covariance matrix
$${V}_{t|t-1}=Var\left({y}_{t}|{y}_{t-1},\mathrm{...},{y}_{1}\right)={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime}+{D}_{t}{D}_{t}^{\prime}.$$
where $${P}_{t|t-1}$$ is the estimated variance-covariance matrix of the state forecasts at period t, given all information up to period t – 1.
In general, the s-step-ahead, vector of state forecasts is $${x}_{t|t-s}=E\left({x}_{t}|{y}_{t-s},\mathrm{...},{y}_{1}\right)$$. The s-step-ahead, forecasted observation vector is
$${\widehat{y}}_{t+s|t}={C}_{t+s}{\widehat{x}}_{t+s|t}.$$
In the state-space model framework, the Kalman filter estimates the values of a latent, linear, stochastic, dynamic process based on possibly mismeasured observations. Given distribution assumptions on the uncertainty, the Kalman filter also estimates model parameters via maximum likelihood.
Starting with initial values for states (x_{0|0}), the initial state variance-covariance matrix (P_{0|0}), and initial values for all unknown parameters (θ_{0}), the simple Kalman filter:
Estimates, for t = 1,...,T:
The 1-step-ahead vector of state forecasts vector for period t ($${\widehat{x}}_{t|t-1}$$) and its variance-covariance matrix ($${P}_{t|t-1}$$)
The 1-step-ahead vector of observation forecasts for period t ($${\widehat{y}}_{t|t-1}$$) and its estimated variance-covariance matrix ($${V}_{t|t-1}$$)
The filtered states for period t ($${\widehat{x}}_{t|t}$$) and its estimated variance-covariance matrix ($${P}_{t|t}$$)
Feeds the forecasted and filtered estimates into the data likelihood function
$$\mathrm{ln}p({y}_{T},\mathrm{...},{y}_{1})={\displaystyle \sum _{t=1}^{T}\mathrm{ln}\varphi ({y}_{t};{\widehat{y}}_{t|t-1},{V}_{t|t-1})},$$
where $$\varphi ({y}_{t};{\widehat{y}}_{t|t-1},{V}_{t|t-1})$$ is the multivariate normal probability density function with mean $${\widehat{y}}_{t|t-1}$$ and variance $${V}_{t|t-1}$$.
Feeds this procedure into an optimizer to maximize the likelihood with respect to the model parameters.
The raw Kalman gain is a matrix that indicates how much to weigh the observations during recursions of the Kalman filter.
The raw Kalman gain is an m_{t }-by-h_{t} matrix computed using
$${K}_{t}={P}_{t|t-1}{C}_{t}^{\prime}{\left({C}_{t}{P}_{t|t-1}{C}_{t}^{\prime}+{D}_{t}{D}_{t}^{\prime}\right)}^{-1},$$
where $${P}_{t|t-1}$$ is the estimated variance-covariance matrix of the state forecasts, given all information up to period t – 1.
The value of the raw Kalman gain determines how much weight to put on the observations. For a given estimated observation innovation, if the maximum eigenvalue of D_{t}D_{t}′ is relatively small, then the raw Kalman gain imparts a relatively large weight on the observations. If the maximum eigenvalue of D_{t}D_{t}′ is relatively large, then the raw Kalman gain imparts a relatively small weight on the observations. Consequently, the filtered states at period t are close to the corresponding state forecasts.
s-step-ahead, state forecasts are estimates of the states at period t using all information (for example, the observed responses) up to period t – s.
The m_{t}-by-1 vector of 1-step-ahead, state forecasts at period t is $${x}_{t|t-1}=E\left({x}_{t}|{y}_{t-1},\mathrm{...},{y}_{1}\right)$$. The estimated vector of state forecasts is
$${\widehat{x}}_{t|t-1}={A}_{t}{\widehat{x}}_{t-1|t-1},$$
where $${\widehat{x}}_{t-1|t-1}$$ is the m_{t – 1}-by-1 filtered state vector at period t – 1.
At period t, the 1-step-ahead, state forecasts have the variance-covariance matrix
$${P}_{t|t-1}={A}_{t}{P}_{t-1|t-1}{A}_{t}^{\prime}+{B}_{t}{B}_{t}^{\prime},$$
where$${P}_{t-1|t-1}$$ is the estimated variance-covariance matrix of the filtered states at period t – 1, given all information up to period t – 1.
The corresponding 1-step-ahead forecasted observation is $${\widehat{y}}_{t|t-1}={C}_{t}{\widehat{x}}_{t|t-1},$$, and its variance-covariance matrix is $${V}_{t|t-1}=Var\left({y}_{t}|{y}_{t-1},\mathrm{...},{y}_{1}\right)={C}_{t}{P}_{t|t-1}{C}_{t}^{\prime}+{D}_{t}{D}_{t}^{\prime}.$$
In general, the s-step-ahead, forecasted state vector is $${x}_{t|t-s}=E\left({x}_{t}|{y}_{t-s},\mathrm{...},{y}_{1}\right)$$. The s-step-ahead, vector of state forecasts is
$${\widehat{x}}_{t+s|t}=\left({\displaystyle \prod _{j=t+1}^{t+s}{A}_{j}}\right){x}_{t|t}$$
and the s-step-ahead, forecasted observation vector is
$${\widehat{y}}_{t+s|t}={C}_{t+s}{\widehat{x}}_{t+s|t}.$$
A state-space model is a discrete-time, stochastic model that contains two sets of equations:
One describing how a latent process transitions in time (the state equation)
Another describing how an observer measures the latent process at each period (the observation equation)
Symbolically, you can write a linear, multivariate, Gaussian state-space model using the following system of equations
$$\begin{array}{l}{x}_{t}={A}_{t}{x}_{t-1}+{B}_{t}{u}_{t}\\ {y}_{t}-{Z}_{t}\beta ={C}_{t}{x}_{t}+{D}_{t}{\epsilon}_{t},\end{array}$$
for t = 1,...,T.
$${x}_{t}=\left[{x}_{t1},\mathrm{...},{x}_{t{m}_{t}}\right]\prime $$ is an m_{t}-dimensional state vector describing the dynamics of some, possibly unobservable, phenomenon at period t.
$${y}_{t}=\left[{y}_{t1},\mathrm{...},{y}_{t{n}_{t}}\right]\prime $$ is an n_{t}-dimensional observation vector describing how the states are measured by observers at period t.
A_{t} is the m_{t}-by-m_{t – 1} state-transition matrix describing how the states at time t transition to the states at period t – 1.
B_{t} is the m_{t}-by-k_{t} state-disturbance-loading matrix describing how the states at period t combine with the innovations at period t.
C_{t} is the n_{t}-by-m_{t} measurement-sensitivity matrix describing how the observations at period t relate to the states at period t.
D_{t} is the n_{t}-by-h_{t} observation-innovation matrix describing how the observations at period t combine with the observation errors at period t.
The matrices A_{t}, B_{t}, C_{t}, and D_{t} are referred to as coefficient matrices, and might contain unknown parameters.
$${u}_{t}=\left[{u}_{t1},\mathrm{...},{u}_{t{k}_{t}}\right]\prime $$ is a k_{t}-dimensional, Gaussian, white-noise, unit-variance vector of state disturbances at period t.
$${\epsilon}_{t}=\left[{\epsilon}_{t1},\mathrm{...},{\epsilon}_{t{h}_{t}}\right]\prime $$ is an h_{t}-dimensional, Gaussian, white-noise, unit-variance vector of observation innovations at period t.
ε_{t} and u_{t} are uncorrelated.
For time-invariant models,
$${Z}_{t}=\left[\begin{array}{cccc}{z}_{t1}& {z}_{t2}& \cdots & {z}_{td}\end{array}\right]$$ is row t of a T-by-d matrix of predictors Z. Each column of Z corresponds to a predictor, and each successive row to a successive period. If the observations are multivariate, then all predictors deflate each observation.
β is a d-by-n matrix of regression coefficients for Z_{t}.
To write a time-invariant state-space model, drop the t subscripts of all coefficient matrices and dimensions.
In a time-invariant state-space model:
The coefficient matrices are equivalent for all periods.
The number of states, state disturbances, observations, and observation innovations are the same for all periods.
For example, for all t, the following system of equations
$$\begin{array}{c}\left[\begin{array}{c}{x}_{1,t}\\ {x}_{2,t}\end{array}\right]=\left[\begin{array}{cc}{\varphi}_{1}& 0\\ 0& {\varphi}_{2}\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+\left[\begin{array}{cc}0.5& 0\\ 0& 2\end{array}\right]\left[\begin{array}{c}{u}_{1,t}\\ {u}_{2,t}\end{array}\right]\\ {y}_{t}=\left[\begin{array}{cc}{\varphi}_{3}& 1\end{array}\right]\left[\begin{array}{c}{x}_{1,t}\\ {x}_{2,t}\end{array}\right]+0.2{\epsilon}_{t}\end{array}$$
represents a time-invariant state-space model.
In a time-varying state-space model:
The coefficient matrices might change from period to period.
The number of states, state disturbances, observations, and observation innovations might change from period to period. For example, this might happen if there is a regime shift or one of the states or observations cannot be measured during the sampling time frame. Also, you can model seasonality using time-varying models.
To illustrate a regime shift, suppose, for t = 1,..,10
$$\begin{array}{c}\left[\begin{array}{c}{x}_{1,t}\\ {x}_{2,t}\end{array}\right]=\left[\begin{array}{cc}{\varphi}_{1}& 0\\ 0& {\varphi}_{2}\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+\left[\begin{array}{cc}0.5& 0\\ 0& 2\end{array}\right]\left[\begin{array}{c}{u}_{1,t}\\ {u}_{2,t}\end{array}\right]\\ {y}_{t}=\left[\begin{array}{cc}{\varphi}_{3}& 1\end{array}\right]\left[\begin{array}{c}{x}_{1,t}\\ {x}_{2,t}\end{array}\right]+0.2{\epsilon}_{t}\end{array},$$
for t = 11
$$\begin{array}{c}{x}_{1,t}=\left[\begin{array}{cc}{\varphi}_{4}& 0\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+0.5{u}_{1,t}\\ {y}_{t}={\varphi}_{5}{x}_{1,t}+0.2{\epsilon}_{t}\end{array},$$
and for t = 12,..,T
$$\begin{array}{c}{x}_{1,t}={\varphi}_{4}+0.5{u}_{1,t}\\ {y}_{t}={\varphi}_{5}{x}_{1,t}+0.2{\epsilon}_{t}\end{array}.$$
There are three sets of state transition matrices, whereas there are only two sets of the other coefficient matrices.
The software accommodates missing data. Indicate missing data using NaN values in the observed responses (Y).
Mdl does not store the response data, predictor data, and the regression coefficients. Supply the data wherever necessary using the appropriate input or name-value pair arguments.
To forecast a state-space model, use forecast.