Legacy Documentation

Time Series (2011)

This is documentation for an obsolete product.
Current products and services

Previous section-----Next section

1.9.3 Applications of the Kalman Filter

The Kalman filtering technique can be used conveniently in the analysis of certain time series, once we write the time series model in a state-space form. In the following we will mention a few applications of the Kalman filter and illustrate some of them by examples. For a detailed treatment of the Kalman filter see, for example, Harvey (1989), Chapter 3.
The simple local level model (9.5) is in fact in the state-space form already with Xt=t, Gt=1, Ft=1, ct=0, and dt=0. It is easy to see that the local linear trend model (9.4) can be written in the state-space form as
and
with
Similarly, the basic structural model ((9.1) to (9.3)) when s=4 is equivalent to
where
ARMA models can also be cast into a state-space form, which in general is nonunique. For example, one particular representation of the AR(2) model in state-space form is (for the state-space form of an ARMA(p, q) model see Harvey (1989), Chapter 3)
and
An alternative representation for the state vector is

Initializing the Kalman Filter

The initial values of and Ptt-1 have to be given in order to start the Kalman filter. If the coefficients in (9.7) are all time independent and the eigenvalues of the transition matrix F are inside the unit circle, the state vector is stationary and the initial value can be solved as
and P10 obtained from
The solution to the above equation is
where vec(P) is the vector obtained by stacking the columns of P one below the other.
For example, the AR(2) model with 1=0.9, 2=-0.5, and 2=1 is stationary and the initial values are and P can be obtained using (9.14). The following Mathematica program solves for P.
In[1]:=
In[2]:=
In[3]:=
Out[3]=
Consider the case when the state equation is not stationary, and we do not have any prior information about the distribution of the state vector. In general, if the dimension of X is m, we can use the first m values of Y (assuming that Y is a scalar) to calculate and Pm+1m and start the recursion from there. To obtain and Pm+1m, we can use the Kalman filter ((9.8) to (9.11)) with E(X1I0)=E(X1)="0" and the so-called diffuse prior P10=I, where I is an identity matrix and is eventually set to infinity. For example, the local level model is not stationary. In the absence of any prior information, we use and P10=k and and P21 can be obtained as follows.
In[4]:=
Out[4]=
In[5]:=
Out[5]=
For the local linear trend model, X is a two-dimensional vector (m=2), and the calculation of the initial values using a diffuse prior, in general, needs the first two data points y1 and y2.
In[6]:=
In[7]:=
Out[7]=
In[8]:=
Out[8]=
Although in principle KalmanFilter can be used to calculate using the first m data points as illustrated above, it is infeasible when m is large because of the symbolic parameter k involved in the calculation. Alternatively, we can obtain the initial values by writing Xm in terms of first m data points of Y, and therefore, solve for . The following is a program that gives . Note that the program is only for the case where Y is a scalar and the coefficient matrices are independent of time. For other ways of calculating starting values, see Harvey (1989), Chapter 3.
In[9]:=
In[10]:=
In[11]:=
Out[11]=

Kalman Filtering and Kalman Smoothing

Often, the state variable X represents some quantity we are interested in knowing. The Kalman filter enables us to estimate the state variable X from the observations of Y. For example, in the local level model, is the trend and the estimation of t, given y1, y2, ..., yt, is given by the Kalman filter (9.8) to (9.11).
In[12]:=
In[13]:=
Out[13]=
In[14]:=
Out[14]=
In[15]:=
Out[15]=
In[16]:=
In[17]:=
In[18]:=
Out[18]=
Xtt is the estimate of Xt based on the information up to t. However, if we know Yt up to t=T, we can use the information up to T to improve our estimate of Xt. This is called Kalman smoothing.
In[19]:=
In[20]:=
Out[20]=
In[21]:=
In[22]:=
Out[22]=
In[23]:=
In[24]:=
Out[24]=
In[25]:=
In[26]:=
Out[26]=
In[27]:=
Out[27]=
In[28]:=
In[29]:=
Out[29]=
In[30]:=
Out[30]=
In[31]:=
In[32]:=
Out[32]=
In[33]:=
Out[33]=

Parameter Estimation and Prediction

The likelihood function of a state-space time series can be easily calculated using the Kalman filter technique. The joint density of {Y1, Y2, ..., YT} is L=p(YtIt-1), where and . The log likelihood is given by
Note that if the first m values of data are used to calculate the starting values, the lower limit of the summations is t=m+1 and the corresponding "Log"L is the conditional log likelihood, conditional on y1, y2, ..., ym being fixed.
If you wish to get the logarithm of the likelihood function of a state-space time series, you can use the function
LogLikelihood[data, init, F, G, Q, R, c, d]
Note that it has the same input arguments as those of KalmanFilter. When the first m points of the series are used to calculate the initial values init, data should start from ym+1; when any one of F, G, Q, R, c, d is time dependent, the above arguments to LogLikelihood, F, G, Q, R, c, d, should be replaced by {Fm+2, Fm+3, ..., FT+1}, {Gm+1, Gm+2, ..., GT}, etc. Again, if c=0 and d=0, the last two arguments can be omitted.
Example 9.3 We again look at the local level model (see Example 9.1). This time we try to estimate the variance parameters of the model from the given series.
In[34]:=
In[35]:=
Out[35]=
To get the maximum likelihood estimate of the parameters, we need to maximize the likelihood function. This is done using the built-in Mathematica function FindMinimum. Note that the function to be minimized is the negative of the log likelihood function. Also, we define a special function which evaluates only for numerical input to prevent FindMinimum from attempting symbolic preprocessing that can potentially take a long time.
In[36]:=
In[39]:=
Out[39]=
In[40]:=
In[41]:=
In[42]:=
Out[42]=
In[43]:=
In[46]:=
Out[46]=
In[47]:=
Having obtained the estimated parameters, we can now use them to predict the future trend by using KalmanPredictor. First, we calculate {Xt+1t, Pt+1t} from KalmanFilter.
In[48]:=
Out[48]=
In[49]:=
Out[49]=
In[50]:=
In[51]:=
Out[51]=
In[52]:=
Out[52]=
In[53]:=
In[54]:=
Out[54]=
FindMinimum finds the parameter values that maximize the likelihood function.
In[55]:=
In[58]:=
Out[58]=
In[59]:=
Out[59]=
In[60]:=
Out[60]=
On the other hand, had we used the first two data points to calculate the initial values, we would have effectively obtained the conditional maximum likelihood estimate, conditional on the first two data points being fixed.
In[61]:=
Out[61]=
In[62]:=
In[65]:=
Out[65]=
In[66]:=
In[67]:=
Out[67]=