Example: TimeSeriesClassFilter

For illustration purposes, the time series in this example consists of the integers 1, 2, ..., 10, organized into two classes. Of course, it is assumed that these data are sorted in chronologically descending order. That is for each class, the first number is the latest value and the last number in that class is the earliest.

The values 1-4 are in class 1, and the values 5-10 are in class 2. These values represent two separate time series, one for each class. If you were to list them in chronologically ascending order, starting with time = T0, the values would be:

Class 1: T0=4, T1=3, T2=2, T3=1
Class 2: T0=10, T1=9, T2=8, T3=7, T4=6, T5=5

This example requests lag calculations for lags 0, 1, 2, 3. For lag=0, no lagging is performed. For lag=1, the value at time = t replaced with the value at time = t-1, the previous value in that class. If t-1 \lt 0, then a missing value is placed in that position.

For example, the first lag of a time series at time=t are the values at time=t-1. For the time series values of Class 1 (lag=1), these values are:

Class 1, lag 1: T0=NaN, T1=4, T2=3, T3=2

The second lag for time=t consists of the values at time=t-2:

Class 1, lag 2: T0=NaN, T1=NaN, T2=4, T3=3

Notice that the second lag now has two missing observations. In general, lag=n will have n missing values. In some cases this can result in all missing values for classes with few observations. A class will have all missing values in any of its lag columns that have a lag value larger than or equal to the number of observations in that class.


import com.imsl.math.*;
import com.imsl.datamining.neural.*;

public class TimeSeriesClassFilterEx1 {

    private static int nClasses = 2;
    private static int nObs = 10;
    private static int nLags = 4;

    public static void main(String args[]) {
        double[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        double[] time = {3, 2, 1, 0, 5, 4, 3, 2, 1, 0};
        int[] iClass = {1, 1, 1, 1, 2, 2, 2, 2, 2, 2};
        int[] lag = {0, 1, 2, 3};
        String[] colLabels = {
            "Class", "Time", "Lag=0", "Lag=1", "Lag=2", "Lag=3"
        };

        // Filter Classified Time Series Data
        TimeSeriesClassFilter filter = new TimeSeriesClassFilter(nClasses);
        double[][] y = filter.computeLags(lag, iClass, x);
        double[][] z = new double[nObs][nLags + 2];
        for (int i = 0; i < nObs; i++) {
            z[i][0] = (double) iClass[i];
            z[i][1] = time[i];
            for (int j = 0; j < nLags; j++) {
                z[i][j + 2] = y[i][j];
            }
        }

        // Print result without row/column labels.
        PrintMatrix pm = new PrintMatrix();
        PrintMatrixFormat mf;
        mf = new PrintMatrixFormat();
        mf.setNoRowLabels();
        mf.setColumnLabels(colLabels);
        pm.setTitle("Lagged data");

        pm.print(mf, z);
    }
}

Output

                Lagged data
  Class  Time  Lag=0  Lag=1  Lag=2  Lag=3  
  1     3      1      2      3      4    
  1     2      2      3      4      ?    
  1     1      3      4      ?      ?    
  1     0      4      ?      ?      ?    
  2     5      5      6      7      8    
  2     4      6      7      8      9    
  2     3      7      8      9     10    
  2     2      8      9     10      ?    
  2     1      9     10      ?      ?    
  2     0     10      ?      ?      ?    

Link to Java source.