package com.imsl.test.example.datamining.neural; import com.imsl.math.*; import com.imsl.datamining.neural.*; /** *
* Applies a time series filter to a classification * variable.
*
* The variable has the values 1, 2,..., 10 corresponding to one of 2 classes.
* The values 1-4 are associated with class 1, and the values 5-10 are associated with
* class 2. These values represent two separate time series, one for each class.
* The time index for each series is provided in the array time
.
* Listed 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\) is 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 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. *
* * @see Code * @see Output * */ 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 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]; System.arraycopy(y[i], 0, z[i], 2, nLags); } // 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); } }