29 March 2011

K-step-ahead Kalman predictions

by Jaromir Benes

This post is about the 'ahead' option available in the IRIS Kalman filter, and the associated plotpred function.

When running the filter function, we get the so-called smoothed data as the standard output. The smoother produces the estimates of the model's unobservables and shocks in each period t based on all observations supplied t = 1,...,T:

[m,smooth] = filter(m,d,range);

Sometimes, especially when diagnosing the model properties, we may also be interested in the Kalman prediction step. By default, the Kalman predictor produces the estimates of the unobservables in each period t based on information up to time t-1: That's in fact what the Kalman filter needs to evaluate the likelihood function. We can use the 'data' option to request the prediction step. The option can be set to 'smooth' (which is the default), 'pred', or 'smooth,pred'. Try the following calls to filter, and see the changing structure of the output database:

[m,f1] = filter(m,d,range);
[m,f2] = filter(m,d,range,'data','pred');
[m,f3] = filter(m,d,range,'data','pred','meanonly',true);
[m,f4] = filter(m,d,range,'data','smooth,pred');
[m,f5] = filter(m,d,range,'data','smooth,pred','meanonly',true);

In addition to the default 1-step-ahead predictions, we can also request k-step-predictions using the 'ahead' option. By default, 'ahead' is 1; we can change it to any positive integerk. In that case, the Kalman filter will compute all predictions between 1 and k steps ahead. In other words,

[m,f2] = filter(m,d,range,'data','pred','ahead',4,'meanonly',true);
will return a database f2 in which all variables will be tseries objects with four columns. The first column will be prediction t|t-1, the second column will be prediction t|t-2, and so on. Notice that the four data in one row correspond always to predictions for that particular period conditioned upon different times.

When plotting the time series from the prediction database, we obviously get four lines. The first line will represent 1-step-ahead predictions for each particular period, the second line will represent 2-step-ahead prediction for that period (i.e. prediciton based on a more distant past), and so on. This might be useful, but usually we would like to see a different picture: A central line representing the actual observations, with little threads shooting from the individual data points showing the t+1, t+2, etc. predictions based on that period's information. To create such a graph, we need to re-arrange a little bit the data. But don't worry – that's exactly what plotpred function can do for us:

[m,f2] = filter(m,d,range,'data','pred','ahead',4,'meanonly',true);
[h1,h2] = plotpred([d.x,f2.x]);
The input tseries object passed in to the plotpred function is assumed to have the central line (i.e. the actually observed data) in the first column, and then the 1-, 2-, ... k-step-ahead predictions in the other columns. Note also that the function returns two sets of graphics handles: h1 is the handle to the central line (the first column), whereas h2 is the handles to the prediction threads. This allows us to style the prediction graphs easily. Try, for instance, the following:
set(h1,'lineWidth',1.5,'marker','.');
set(h2,'lineWidth',1,'lineStyle',':');

The distinction between the central line and the prediction threads is also available in the style structures used in the qstyle function or in the 'style' option of report functions:

[m,f2] = filter(m,d,range,'data','pred','ahead',4,'meanonly',true);
plotpred([d.x,f2.x]);

s = struct();

s.line.lineWidth = 1.5;
s.line.marker = '.';

s.plotpred.lineStyle = ':';
s.plotpred.lineWidth = 1;
s.plotpred.color = {'red','blue','magenta'};

qstyle(gca(),s);
In other words, to style the central line, we simply refer to the standard line type of objects. To define the style for the prediction threads, we then refer to plotpred. Note also that in the example above, we only define three colours for the prediction threads – while typically plotting more than three of them. In that case, the colours will be cycled over (red-blue-magenta-red-blue-magenta-etc.). This is how any of the style structure elements (not only colours, of course) behave when the number of values supplied is smaller than the number of graphics objects of its kind on the screen.

No comments:

Post a Comment