MATLAB MEX Function Reference
Kalman Filtering Example 1:
Likelihood Function Evaluation

In this example, the log-likelihood function of the SSM is computed using prediction error decomposition. The annual real GNP series, yt, can be decomposed as

y_t = \mu_t + \epsilon_t
where \mu_t is a trend component and \epsilon_t is a white noise error with \epsilon_t \sim (0, \sigma^2_\epsilon). Refer to Nelson and Plosser (1982) for more details on these data. The trend component is assumed to be generated from the following stochastic equations:
\mu_t & = & \mu_{t-1} + \beta_{t-1} + \eta_{1t} \
\beta_t & = & \beta_{t-1} + \eta_{2t}
where \eta_{1t} and \eta_{2t} are independent white noise disturbances with \eta_{1t} \sim (0, \sigma^2_{\eta_1}) and \eta_{2t} \sim (0, \sigma^2_{\eta_2}).

It is straightforward to construct the SSM of the real GNP series.

y_t & = & {H}{z}_t + \epsilon_t \
{z}_t & = & {F}{z}_{t-1} + \eta_t
where
{H}& = & (1, 0) \
{F}& = & [ 1 & 1 \
 0 & 1
 ] \
{z}_t & = & (\mu_t, \beta...
 ...\eta 1} & 0 & 0 \
 0 & \sigma^2_{\eta 2} & 0 \
 0 & 0 & \sigma^2_\epsilon
 ]
When the observation noise \epsilon_t is normally distributed, the average log-likelihood function of the SSM is
\ell & = & \frac{1}T \sum_{t=1}^T \ell_t \
\ell_t & = & -\frac{N_y}2\log(2\pi)...
 ...1}2\log(|{C}_t|)
 - \frac{1}2 \hat{\epsilon}^'_t
 {C}^{-1}_t \hat{\epsilon}_t
where Ct is the mean square error matrix of the prediction error \hat{\epsilon}_t, such that {C}_t = {H}{P}_{t| t-1} {H}^' + {{R}}_t.

The following example MATLAB script computes the average log-likelihood function. First, the average log-likelihood function is computed using the default initial values: Z0=0 and VZ0=106I. The second call of function KALCVF produces the average log-likelihood function with the given initial conditions: Z0=0 and VZ0=10-3I. You can notice a sizable difference between the uncertain initial condition (VZ0=106I) and the almost deterministic initial condition (VZ0=10-3I) in Output 1.

Finally, the first 16 observations of one-step predictions, filtered values, and real GNP series are produced under the moderate initial condition (VZ0=10I). The data are the annual real GNP for the years 1909 to 1969.


y = [ 116.8 120.1 123.2 130.2 131.4 125.6 124.5 134.3 ...
      135.2 151.8 146.4 139.0 127.8 147.0 165.9 165.5 ...
      179.4 190.0 189.8 190.9 203.6 183.5 169.3 144.2 ...
      141.5 154.3 169.5 193.0 203.2 192.9 209.4 227.2 ...
      263.7 297.8 337.1 361.3 355.2 312.6 309.9 323.7 ...
      324.1 355.3 383.4 395.1 412.8 406.0 438.0 446.1 ...
      452.5 447.3 475.9 487.7 497.2 529.8 551.0 581.1 ...
      617.8 658.1 675.2 706.6 724.7 ];
T = size(y,2);
lead = 0;
Nz = 2;
a = zeros(Nz,1);
F = [1 1; 0 1];
Ny = 1;
b = zeros(Ny,1);
H = [1 0];
var = eye(Ny+Nz)*1e-3;

fprintf('\nLikelihood Evaluation of SSM\n');
fprintf('DATA: Annual Real GNP 1909-1969\n\n');

logl = kalcvf(y, lead, a, F, b, H, var);
fprintf('No initial values are given\n');
fprintf('LOGL = %.2f\n\n', logl);

z0 = a;
vz0 = eye(Nz)*1e-3;
logl = kalcvf(y, lead, a, F, b, H, var, z0, vz0);
fprintf('Initial values are given\n');
fprintf('LOGL = %.2f\n\n', logl);

lead = 1;
z0 = a;
vz0 = eye(Nz)*10;
[logl, pred, vpred, filt, vfilt] = kalcvf(y, lead, a, F, b, H, var, z0, vz0);

fprintf('   Actual        Predicted            Filtered\n')
format,disp([y(1:16); pred(:,1:16); filt(:,1:16)]')

Output 1: Average Log Likelihood of SSM
    Likelihood Evaluation of SSM
    DATA: Annual Real GNP 1909-1969
    
    No initial values are given
    LOGL = -1605137.95
    
    Initial values are given
    LOGL = -3426718.43
    

Output 2 shows the observed data, the predicted state vectors, and the filtered state vectors for the first 16 observations.

Output 2: Filtering and One-Step Prediction
       Actual        Predicted            Filtered
      116.8000         0         0  116.7942   58.3942
      120.1000  175.1883   58.3942  120.1110    3.3444
      123.2000  123.4554    3.3444  123.2284    3.2025
      130.2000  126.4309    3.2025  129.5942    4.8851
      131.4000  134.4793    4.8851  131.9389    3.5764
      125.6000  135.5153    3.5764  127.3627   -0.6101
      124.5000  126.7527   -0.6101  124.9013   -1.5608
      134.3000  123.3404   -1.5608  132.3475    3.0650
      135.2000  135.4125    3.0650  135.2379    2.9753
      151.8000  138.2132    2.9753  149.3795    8.7101
      146.4000  158.0895    8.7101  148.4825    3.7761
      139.0000  152.2587    3.7761  141.3621   -1.8201
      127.8000  139.5420   -1.8201  129.8919   -6.7762
      147.0000  123.1157   -6.7762  142.7449    3.3050
      165.9000  146.0499    3.3050  162.3636   11.6833
      165.5000  174.0470   11.6833  167.0227    8.0758
    

See also

KALCVF performs covariance filtering and prediction

KALCVS performs fixed-interval smoothing

Getting Started with State Space Models

Kalman Filtering Example 2: Estimating an SSM Using the EM Algorithm

References

[1]  Nelson, C.P. and Plosser, C.I., Trends and Random Walks in Macroeconomic Time Series: Some Evidence and Implications, Journal of Monetary Economics, vol 10, pp. 139-162, 1982.

Return to main page