Example 2: TimeSeriesOperations

This example illustrates merging two time series using different combine methods.


import com.imsl.stat.*;
import java.util.Date;
import java.text.*;

public class TimeSeriesOperationsEx2 {

    public static void main(String args[]) throws ParseException {
        double data1[] = {
            0.940543272, 2.664279727, 0.252045652, 1.221098464, 0.270575973,
            0.446882726, 0.073672969, -4.450030305, 0.007699415, -0.215808015
        };

        double data2[] = {
            0.42010331, -1.687159586, 0.212950629, 1.147614577, 0.748805609,
            -1.311175287, -1.574960025, 0.484204788, 0.178009897, -0.869311816,
            1.062785115, 0.098566846, 0.342257263, -0.47633197
        };

        String datestrings1[] = {
            "4/5/1988", "4/6/1988", "4/7/1988", "4/8/1988", "4/11/1988",
            "4/12/1988", "4/13/1988", "4/14/1988", "4/15/1988", "4/18/1988"
        };

        String datestrings2[] = {
            "4/13/1988", "4/14/1988", "4/15/1988", "4/19/1988", "4/20/1988",
            "4/21/1988", "4/22/1988", "4/25/1988", "4/26/1988", "4/27/1988",
            "4/28/1988", "4/29/1988", "5/2/1988", "5/3/1988"
        };

        SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/y");

        TimeSeries ts1 = new TimeSeries();
        ts1.setSeriesValues(data1);
        Date dates1[] = new Date[ts1.getLength()];

        for (int i = 0; i < ts1.getLength(); i++) {
            dates1[i] = dateFormat.parse(datestrings1[i]);
        }
        ts1.setDates(dates1);

        TimeSeries ts2 = new TimeSeries();
        ts2.setSeriesValues(data2);
        Date dates2[] = new Date[ts2.getLength()];

        for (int i = 0; i < ts2.getLength(); i++) {
            dates2[i] = dateFormat.parse(datestrings2[i]);
        }
        ts2.setDates(dates2);

        TimeSeriesOperations tsOps = new TimeSeriesOperations();
        TimeSeries ts3 = tsOps.merge(ts1, ts2);
        double[][] values3 = ts3.getSeriesValues();

        System.out.println("Result using MergeRule.UNION and "
                + "CombineFunction.AVERAGE (defaults):");
        for (int i = 0; i < ts3.getLength(); i++) {
            System.out.println(dateFormat.format(ts3.getDates()[i])
                    + "\t" + values3[i][0]);
        }

        tsOps.setMergeRule(TimeSeriesOperations.MergeRule.INTERSECTION);
        tsOps.setCombineMethod(TimeSeriesOperations.CombineMethod.ABS_DIFF);
        ts3 = tsOps.merge(ts1, ts2);
        values3 = ts3.getSeriesValues();

        System.out.println("Result using MergeRule.INTERSECTION and "
                + "CombineFunction.ABS_DIFF:");
        for (int i = 0; i < ts3.getLength(); i++) {
            System.out.println(dateFormat.format(ts3.getDates()[i])
                    + "\t" + values3[i][0]);
        }
    }
}

Output

Result using MergeRule.UNION and CombineFunction.AVERAGE (defaults):
4/5/1988	0.940543272
4/6/1988	2.664279727
4/7/1988	0.252045652
4/8/1988	1.221098464
4/11/1988	0.270575973
4/12/1988	0.446882726
4/13/1988	0.2468881395
4/14/1988	-3.0685949455
4/15/1988	0.110325022
4/18/1988	-0.215808015
4/19/1988	1.147614577
4/20/1988	0.748805609
4/21/1988	-1.311175287
4/22/1988	-1.574960025
4/25/1988	0.484204788
4/26/1988	0.178009897
4/27/1988	-0.869311816
4/28/1988	1.062785115
4/29/1988	0.098566846
5/2/1988	0.342257263
5/3/1988	-0.47633197
Result using MergeRule.INTERSECTION and CombineFunction.ABS_DIFF:
4/13/1988	0.34643034100000003
4/14/1988	2.7628707190000004
4/15/1988	0.20525121400000002
Link to Java source.