Example 1: CrossCorrelation

Consider the Gas Furnace Data (Box and Jenkins 1976, pages 532-533) where X is the input gas rate in cubic feet/minute and Y is the percent CO_2 in the outlet gas. The CrossCorrelation methods getCrossCovariance and getCrossCorrelation are used to compute the cross-covariances and cross-correlations between time series X and Y with lags from -maximum_lag = -10 through lag maximum_lag = 10. In addition, the estimated standard errors of the estimated cross-correlations are computed. In the first invocation of method getStandardErrors stderrMethod = BARTLETTS_FORMULA, the standard errors are based on the assumption that autocorrelations and cross-correlations for lags greater than maximum_lag or less than -maximum_lag are zero, In the second invocation of method getStandardErrors with stderrMethod = BARTLETTS_FORMULA_NOCC, the standard errors are based on the additional assumption that all cross-correlations for X and Y are zero.

import java.text.*;
import com.imsl.stat.*;
import com.imsl.math.PrintMatrix;

public class CrossCorrelationEx1 {

    public static void main(String args[]) throws Exception {
        double[] x2 = {100.8, 81.6, 66.5, 34.8, 30.6, 7, 19.8, 92.5,
        154.4, 125.9, 84.8, 68.1, 38.5, 22.8, 10.2, 24.1, 82.9,
        132, 130.9, 118.1, 89.9, 66.6, 60, 46.9, 41, 21.3, 16,
        6.4, 4.1, 6.8, 14.5, 34, 45, 43.1, 47.5, 42.2, 28.1, 10.1,
        8.1, 2.5, 0, 1.4, 5, 12.2, 13.9, 35.4, 45.8, 41.1, 30.4,
        23.9, 15.7, 6.6, 4, 1.8, 8.5, 16.6, 36.3, 49.7, 62.5,
        67, 71, 47.8, 27.5, 8.5, 13.2, 56.9, 121.5, 138.3, 103.2,
        85.8, 63.2, 36.8, 24.2, 10.7, 15, 40.1, 61.5, 98.5,
        124.3, 95.9, 66.5, 64.5, 54.2, 39, 20.6, 6.7, 4.3, 22.8,
        54.8, 93.8, 95.7, 77.2, 59.1, 44, 47, 30.5, 16.3, 7.3,
        37.3, 73.9};
        
        double[] x = {-0.109, 0.0, 0.178, 0.339, 0.373, 0.441, 0.461,
          0.348, 0.127, -0.18, -0.588, -1.055, -1.421, -1.52, -1.302,
          -0.814, -0.475, -0.193, 0.088, 0.435, 0.771, 0.866, 0.875,
          0.891, 0.987, 1.263, 1.775, 1.976, 1.934, 1.866, 1.832,
          1.767, 1.608, 1.265, 0.79, 0.36, 0.115, 0.088, 0.331,
          0.645, 0.96, 1.409, 2.67, 2.834, 2.812, 2.483, 1.929,
          1.485, 1.214, 1.239, 1.608, 1.905, 2.023, 1.815, 0.535,
          0.122, 0.009, 0.164, 0.671, 1.019, 1.146, 1.155,
          1.112, 1.121, 1.223, 1.257, 1.157, 0.913, 0.62, 0.255,
          -0.28, -1.08, -1.551, -1.799, -1.825, -1.456, -0.944,
          -0.57, -0.431, -0.577, -0.96, -1.616, -1.875, -1.891,
          -1.746, -1.474, -1.201, -0.927, -0.524, 0.04, 0.788, 0.943,
          0.93, 1.006, 1.137, 1.198, 1.054, 0.595, -0.08, -0.314,
          -0.288, -0.153, -0.109, -0.187, -0.255, -0.229, -0.007,
          0.254, 0.33, 0.102, -0.423,
          -1.139, -2.275, -2.594, -2.716, -2.51, -1.79, -1.346,
          -1.081, -0.91, -0.876, -0.885, -0.8, -0.544, -0.416,
          -0.271, 0.0, 0.403, 0.841, 1.285, 1.607, 1.746, 1.683,
          1.485, 0.993, 0.648, 0.577, 0.577, 0.632, 0.747, 0.9,
          0.993, 0.968, 0.79, 0.399, -0.161, -0.553, -0.603, -0.424,
          -0.194, -0.049, 0.06, 0.161, 0.301, 0.517, 0.566, 0.56,
          0.573, 0.592, 0.671, 0.933, 1.337, 1.46, 1.353, 0.772,
          0.218,-0.237, -0.714, -1.099, -1.269, -1.175, -0.676,
          0.033, 0.556, 0.643, 0.484, 0.109, -0.31, -0.697, -1.047,
          -1.218, -1.183, -0.873, -0.336, 0.063, 0.084, 0.0, 0.001,
          0.209, 0.556, 0.782, 0.858, 0.918, 0.862, 0.416, -0.336,
          -0.959, -1.813, -2.378, -2.499, -2.473, -2.33, -2.053,
          -1.739, -1.261, -0.569, -0.137, -0.024, -0.05, -0.135,
          -0.276, -0.534, -0.871, -1.243, -1.439, -1.422, -1.175,
          -0.813, -0.634, -0.582, -0.625, -0.713,
          -0.848, -1.039, -1.346, -1.628, -1.619, -1.149,
          -0.488, -0.16, -0.007, -0.092, -0.62, -1.086, -1.525,
          -1.858, -2.029, -2.024, -1.961, -1.952, -1.794, -1.302,
          -1.03, -0.918, -0.798, -0.867, -1.047, -1.123, -0.876,
          -0.395, 0.185, 0.662, 0.709, 0.605, 0.501, 0.603, 0.943,
          1.223, 1.249, 0.824, 0.102, 0.025, 0.382,
          0.922, 1.032, 0.866, 0.527, 0.093, -0.458, -0.748,
          -0.947, -1.029, -0.928, -0.645, -0.424, -0.276, -0.158,
          -0.033, 0.102, 0.251, 0.28, 0.0, -0.493, -0.759, -0.824,
          -0.74, -0.528, -0.204, 0.034, 0.204, 0.253, 0.195, 0.131,
          0.017, -0.182, -0.262};
          double[] y = {53.8, 53.6, 53.5, 53.5, 53.4, 53.1, 52.7, 52.4, 52.2,
          52.0, 52.0, 52.4, 53.0, 54.0, 54.9, 56.0, 56.8, 56.8, 56.4,
          55.7, 55.0, 54.3, 53.2, 52.3, 51.6, 51.2, 50.8, 50.5, 50.0,
          49.2, 48.4, 47.9, 47.6, 47.5, 47.5, 47.6, 48.1, 49.0, 50.0,
          51.1, 51.8, 51.9, 51.7, 51.2, 50.0, 48.3, 47.0, 45.8, 45.6,
          46.0, 46.9, 47.8, 48.2, 48.3, 47.9, 47.2, 47.2,
          48.1, 49.4, 50.6, 51.5, 51.6, 51.2, 50.5, 50.1, 49.8, 49.6,
          49.4, 49.3, 49.2, 49.3, 49.7, 50.3, 51.3, 52.8, 54.4, 56.0,
          56.9, 57.5, 57.3, 56.6, 56.0, 55.4, 55.4, 56.4, 57.2, 58.0,
          58.4, 58.4, 58.1, 57.7, 57.0, 56.0, 54.7, 53.2, 52.1, 51.6,
          51.0, 50.5,50.4, 51.0, 51.8, 52.4, 53.0, 53.4, 53.6, 53.7,
          53.8, 53.8, 53.8, 53.3, 53.0, 52.9, 53.4, 54.6, 56.4, 58.0,
          59.4, 60.2, 60.0, 59.4, 58.4, 57.6, 56.9, 56.4, 56.0, 55.7,
          55.3, 55.0, 54.4, 53.7, 52.8, 51.6, 50.6, 49.4, 48.8, 48.5,
          48.7, 49.2, 49.8, 50.4, 50.7, 50.9, 50.7, 50.5, 50.4, 50.2,
          50.4, 51.2, 52.3, 53.2, 53.9, 54.1, 54.0, 53.6, 53.2, 53.0,
          52.8, 52.3,51.9, 51.6, 51.6, 51.4, 51.2, 50.7, 50.0, 49.4, 49.3,
          49.7, 50.6, 51.8, 53.0, 54.0, 55.3, 55.9, 55.9, 54.6, 53.5,
          52.4, 52.1, 52.3, 53.0, 53.8, 54.6, 55.4, 55.9, 55.9, 55.2,
          54.4, 53.7, 53.6, 53.6, 53.2, 52.5, 52.0, 51.4, 51.0, 50.9,
          52.4, 53.5, 55.6, 58.0, 59.5, 60.0, 60.4, 60.5, 60.2, 59.7,
          59.0, 57.6, 56.4, 55.2, 54.5, 54.1, 54.1, 54.4,
          55.5, 56.2, 57.0, 57.3, 57.4, 57.0, 56.4, 55.9, 55.5, 55.3,
          55.2, 55.4, 56.0, 56.5, 57.1, 57.3, 56.8, 55.6, 55.0, 54.1,
          54.3, 55.3, 56.4, 57.2, 57.8, 58.3, 58.6, 58.8, 58.8, 58.6,
          58.0, 57.4, 57.0, 56.4, 56.3, 56.4, 56.4, 56.0, 55.2, 54.0,
          53.0, 52.0,51.6, 51.6, 51.1, 50.4, 50.0, 50.0, 52.0, 54.0,
          55.1, 54.5, 52.8, 51.4, 50.8, 51.2, 52.0, 52.8, 53.8, 54.5,
          54.9, 54.9, 54.8, 54.4, 53.7, 53.3, 52.8, 52.6, 52.6, 53.0,
          54.3, 56.0, 57.0, 58.0, 58.6, 58.5, 58.3, 57.8, 57.3, 57.0};
          CrossCorrelation cc;
        
        System.out.println("*****************************");
        cc = new CrossCorrelation(x, y,10);
        System.out.println("Mean = "+cc.getMeanX());          
        System.out.println("Mean = "+cc.getMeanY());
        System.out.println("Xvariance  = "+cc.getVarianceX());
        System.out.println("Yvariance  = "+cc.getVarianceY());
        new PrintMatrix("CrossCovariances are:  ").print
                       (cc.getCrossCovariance());
        new PrintMatrix("CrossCorrelations are:  ").print
                       (cc.getCrossCorrelation()); 
        new PrintMatrix("Standard Errors using Bartlett are:  ").print
                       (cc.getStandardErrors(cc.BARTLETTS_FORMULA));
        new PrintMatrix("Standard Errors using Bartlett #2 are:  ").print
                       (cc.getStandardErrors(cc.BARTLETTS_FORMULA_NOCC));
        new PrintMatrix("AutoCovariances of X are:  ").print
                       (cc.getAutoCovarianceX());
        new PrintMatrix("AutoCovariances of Y are:  ").print
                       (cc.getAutoCovarianceY());
        new PrintMatrix("AutoCorrelations of X are:  ").print
                       (cc.getAutoCorrelationX());
        new PrintMatrix("AutoCorrelations of Y are:  ").print
                       (cc.getAutoCorrelationY());    
    }

}

Output

*****************************
Mean = -0.05683445945945951
Mean = 53.50912162162156
Xvariance  = 1.1469379016503833
Yvariance  = 10.218937066289259
CrossCovariances are:  
      0     
 0  -0.405  
 1  -0.508  
 2  -0.614  
 3  -0.705  
 4  -0.776  
 5  -0.831  
 6  -0.891  
 7  -0.981  
 8  -1.125  
 9  -1.347  
10  -1.659  
11  -2.049  
12  -2.482  
13  -2.885  
14  -3.165  
15  -3.253  
16  -3.131  
17  -2.839  
18  -2.453  
19  -2.053  
20  -1.695  

CrossCorrelations are:  
      0     
 0  -0.118  
 1  -0.149  
 2  -0.179  
 3  -0.206  
 4  -0.227  
 5  -0.243  
 6  -0.26   
 7  -0.286  
 8  -0.329  
 9  -0.393  
10  -0.484  
11  -0.598  
12  -0.725  
13  -0.843  
14  -0.925  
15  -0.95   
16  -0.915  
17  -0.829  
18  -0.717  
19  -0.6    
20  -0.495  

Standard Errors using Bartlett are:  
      0    
 0  0.158  
 1  0.156  
 2  0.153  
 3  0.149  
 4  0.145  
 5  0.141  
 6  0.138  
 7  0.136  
 8  0.132  
 9  0.124  
10  0.108  
11  0.087  
12  0.064  
13  0.047  
14  0.044  
15  0.048  
16  0.049  
17  0.048  
18  0.053  
19  0.072  
20  0.094  

Standard Errors using Bartlett #2 are:  
      0    
 0  0.163  
 1  0.162  
 2  0.162  
 3  0.162  
 4  0.162  
 5  0.161  
 6  0.161  
 7  0.161  
 8  0.161  
 9  0.16   
10  0.16   
11  0.16   
12  0.161  
13  0.161  
14  0.161  
15  0.161  
16  0.162  
17  0.162  
18  0.162  
19  0.162  
20  0.163  

AutoCovariances of X are:  
      0    
 0  1.147  
 1  1.092  
 2  0.957  
 3  0.782  
 4  0.609  
 5  0.467  
 6  0.365  
 7  0.298  
 8  0.261  
 9  0.244  
10  0.239  

AutoCovariances of Y are:  
      0     
 0  10.219  
 1   9.92   
 2   9.157  
 3   8.099  
 4   6.949  
 5   5.871  
 6   4.961  
 7   4.252  
 8   3.736  
 9   3.376  
10   3.132  

AutoCorrelations of X are:  
      0    
 0  1      
 1  0.952  
 2  0.834  
 3  0.682  
 4  0.531  
 5  0.408  
 6  0.318  
 7  0.26   
 8  0.228  
 9  0.213  
10  0.208  

AutoCorrelations of Y are:  
      0    
 0  1      
 1  0.971  
 2  0.896  
 3  0.793  
 4  0.68   
 5  0.574  
 6  0.485  
 7  0.416  
 8  0.366  
 9  0.33   
10  0.307  

Link to Java source.