Example 1: MultiCrossCorrelation

Consider the Wolfer Sunspot Data (Y ) (Box and Jenkins 1976, page 530) along with data on northern light activity (X1) and earthquake activity (X2) (Robinson 1967, page 204) to be a three-channel time series. Methods getCrossCovariance and getCrossCorrelation are used to compute the cross-covariances and cross-correlations between X_1 and Y and between X_2 and Y with lags from -maximum_lag = -10 through lag maximum_lag = 10.

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

public class MultiCrossCorrelationEx1 {
    
    public static void main(String args[]) throws Exception {
        int i;
        double x[][] = {{  155.0,   66.0},
        {  113.0,   62.0},
        {    3.0,   66.0},
        {   10.0,  197.0},
        {    0.0,   63.0},
        {    0.0,    0.0},
        {   12.0,  121.0},
        {   86.0,    0.0},
        {  102.0,  113.0},
        {   20.0,   27.0},
        {   98.0,  107.0},
        {  116.0,   50.0},
        {   87.0,  122.0},
        {  131.0,  127.0},
        {  168.0,  152.0},
        {  173.0,  216.0},
        {  238.0,  171.0},
        {  146.0,   70.0},
        {    0.0,  141.0},
        {    0.0,   69.0},
        {    0.0,  160.0},
        {    0.0,   92.0},
        {   12.0,   70.0},
        {    0.0,   46.0},
        {   37.0,   96.0},
        {   14.0,   78.0},
        {   11.0,  110.0},
        {   28.0,   79.0},
        {   19.0,   85.0},
        {   30.0,  113.0},
        {   11.0,   59.0},
        {   26.0,   86.0},
        {    0.0,  199.0},
        {   29.0,   53.0},
        {   47.0,   81.0},
        {   36.0,   81.0},
        {   35.0,  156.0},
        {   17.0,   27.0},
        {    0.0,   81.0},
        {    3.0,  107.0},
        {    6.0,  152.0},
        {   18.0,   99.0},
        {   15.0,  177.0},
        {    0.0,   48.0},
        {    3.0,   70.0},
        {    9.0,  158.0},
        {   64.0,   22.0},
        {  126.0,   43.0},
        {   38.0,  102.0},
        {   33.0,  111.0},
        {   71.0,   90.0},
        {   24.0,   86.0},
        {   20.0,  119.0},
        {   22.0,   82.0},
        {   13.0,   79.0},
        {   35.0,  111.0},
        {   84.0,   60.0},
        {  119.0,  118.0},
        {   86.0,  206.0},
        {   71.0,  122.0},
        {  115.0,  134.0},
        {   91.0,  131.0},
        {   43.0,   84.0},
        {   67.0,  100.0},
        {   60.0,   99.0},
        {   49.0,   99.0},
        {  100.0,   69.0},
        {  150.0,   67.0},
        {  178.0,   26.0},
        {  187.0,  106.0},
        {   76.0,  108.0},
        {   75.0,  155.0},
        {  100.0,   40.0},
        {   68.0,   75.0},
        {   93.0,   99.0},
        {   20.0,   86.0},
        {   51.0,  127.0},
        {   72.0,  201.0},
        {  118.0,   76.0},
        {  146.0,   64.0},
        {  101.0,   31.0},
        {   61.0,  138.0},
        {   87.0,  163.0},
        {   53.0,   98.0},
        {   69.0,   70.0},
        {   46.0,  155.0},
        {   47.0,   97.0},
        {   35.0,   82.0},
        {   74.0,   90.0},
        {  104.0,  122.0},
        {   97.0,   70.0},
        {  106.0,   96.0},
        {  113.0,  111.0},
        {  103.0,   42.0},
        {   68.0,   97.0},
        {   67.0,   91.0},
        {   82.0,   64.0},
        {   89.0,   81.0},
        {  102.0,  162.0},
        {  110.0,  137.0}};
        
        double y[][] = {{ 101.0},
        {  82.0},
        {  66.0},
        {  35.0},
        {  31.0},
        {   7.0},
        {  20.0},
        {  92.0},
        { 154.0},
        { 126.0},
        {  85.0},
        {  68.0},
        {  38.0},
        {  23.0},
        {  10.0},
        {  24.0},
        {  83.0},
        { 132.0},
        { 131.0},
        { 118.0},
        {  90.0},
        {  67.0},
        {  60.0},
        {  47.0},
        {  41.0},
        {  21.0},
        {  16.0},
        {   6.0},
        {   4.0},
        {   7.0},
        {  14.0},
        {  34.0},
        {  45.0},
        {  43.0},
        {  48.0},
        {  42.0},
        {  28.0},
        {  10.0},
        {   8.0},
        {   2.0},
        {   0.0},
        {   1.0},
        {   5.0},
        {  12.0},
        {  14.0},
        {  35.0},
        {  46.0},
        {  41.0},
        {  30.0},
        {  24.0},
        {  16.0},
        {   7.0},
        {   4.0},
        {   2.0},
        {   8.0},
        {  17.0},
        {  36.0},
        {  50.0},
        {  62.0},
        {  67.0},
        {  71.0},
        {  48.0},
        {  28.0},
        {   8.0},
        {  13.0},
        {  57.0},
        { 122.0},
        { 138.0},
        { 103.0},
        {  86.0},
        {  63.0},
        {  37.0},
        {  24.0},
        {  11.0},
        {  15.0},
        {  40.0},
        {  62.0},
        {  98.0},
        { 124.0},
        {  96.0},
        {  66.0},
        {  64.0},
        {  54.0},
        {  39.0},
        {  21.0},
        {   7.0},
        {   4.0},
        {  23.0},
        {  55.0},
        {  94.0},
        {  96.0},
        {  77.0},
        {  59.0},
        {  44.0},
        {  47.0},
        {  30.0},
        {  16.0},
        {   7.0},
        {  37.0},
        {  74.0}};

        
      
        MultiCrossCorrelation mcc = new MultiCrossCorrelation(x, y, 10);
        
        new PrintMatrix("Mean of X :  ").print(mcc.getMeanX());
        new PrintMatrix("Variance of X :  ").print(mcc.getVarianceX());
        new PrintMatrix("Mean of Y :  ").print(mcc.getMeanY());
        new PrintMatrix("Variance of Y :  ").print(mcc.getVarianceY()); 
        double[][][] ccv = new double[21][2][1];
        double[][][] cc = new double[21][2][1];
        
        ccv = mcc.getCrossCovariance();
        System.out.println("Multichannel cross-covariance between X and Y");
        for (i=0; i<21; i++) {
            System.out.println("Lag K = "+(i-10));
            new PrintMatrix("CrossCovariances : ").print(ccv[i]);
        }
        cc = mcc.getCrossCorrelation();
        System.out.println("Multichannel cross-correlation between X and Y");
        for (i=0; i<21; i++) {
            System.out.println("Lag K = "+(i-10));
            new PrintMatrix("CrossCorrelations : ").print(cc[i]);
        }
    }
    
}

Output

Mean of X :  
     0    
0  63.43  
1  97.97  

Variance of X :  
       0      
0  2,643.685  
1  1,978.429  

Mean of Y :  
     0    
0  46.94  

Variance of Y :  
       0      
0  1,383.756  

Multichannel cross-covariance between X and Y
Lag K = -10
CrossCovariances : 
      0     
0  -20.512  
1   70.713  

Lag K = -9
CrossCovariances : 
     0     
0  65.024  
1  38.136  

Lag K = -8
CrossCovariances : 
      0     
0  216.637  
1  135.578  

Lag K = -7
CrossCovariances : 
      0     
0  246.794  
1  100.362  

Lag K = -6
CrossCovariances : 
      0     
0  142.128  
1   44.968  

Lag K = -5
CrossCovariances : 
      0     
0   50.697  
1  -11.809  

Lag K = -4
CrossCovariances : 
     0     
0  72.685  
1  32.693  

Lag K = -3
CrossCovariances : 
      0     
0  217.854  
1  -40.119  

Lag K = -2
CrossCovariances : 
      0      
0   355.821  
1  -152.649  

Lag K = -1
CrossCovariances : 
      0      
0   579.653  
1  -212.95   

Lag K = 0
CrossCovariances : 
      0      
0   821.626  
1  -104.752  

Lag K = 1
CrossCovariances : 
      0     
0  810.131  
1   55.16   

Lag K = 2
CrossCovariances : 
      0     
0  628.385  
1   84.775  

Lag K = 3
CrossCovariances : 
      0     
0  438.272  
1   75.963  

Lag K = 4
CrossCovariances : 
      0     
0  238.793  
1  200.383  

Lag K = 5
CrossCovariances : 
      0     
0  143.621  
1  282.986  

Lag K = 6
CrossCovariances : 
      0     
0  252.974  
1  234.393  

Lag K = 7
CrossCovariances : 
      0     
0  479.468  
1  223.034  

Lag K = 8
CrossCovariances : 
      0     
0  724.912  
1  124.457  

Lag K = 9
CrossCovariances : 
      0     
0  924.971  
1  -79.517  

Lag K = 10
CrossCovariances : 
      0      
0   922.759  
1  -279.286  

Multichannel cross-correlation between X and Y
Lag K = -10
CrossCorrelations : 
     0     
0  -0.011  
1   0.043  

Lag K = -9
CrossCorrelations : 
     0    
0  0.034  
1  0.023  

Lag K = -8
CrossCorrelations : 
     0    
0  0.113  
1  0.082  

Lag K = -7
CrossCorrelations : 
     0    
0  0.129  
1  0.061  

Lag K = -6
CrossCorrelations : 
     0    
0  0.074  
1  0.027  

Lag K = -5
CrossCorrelations : 
     0     
0   0.027  
1  -0.007  

Lag K = -4
CrossCorrelations : 
     0    
0  0.038  
1  0.02   

Lag K = -3
CrossCorrelations : 
     0     
0   0.114  
1  -0.024  

Lag K = -2
CrossCorrelations : 
     0     
0   0.186  
1  -0.092  

Lag K = -1
CrossCorrelations : 
     0     
0   0.303  
1  -0.129  

Lag K = 0
CrossCorrelations : 
     0     
0   0.43   
1  -0.063  

Lag K = 1
CrossCorrelations : 
     0    
0  0.424  
1  0.033  

Lag K = 2
CrossCorrelations : 
     0    
0  0.329  
1  0.051  

Lag K = 3
CrossCorrelations : 
     0    
0  0.229  
1  0.046  

Lag K = 4
CrossCorrelations : 
     0    
0  0.125  
1  0.121  

Lag K = 5
CrossCorrelations : 
     0    
0  0.075  
1  0.171  

Lag K = 6
CrossCorrelations : 
     0    
0  0.132  
1  0.142  

Lag K = 7
CrossCorrelations : 
     0    
0  0.251  
1  0.135  

Lag K = 8
CrossCorrelations : 
     0    
0  0.379  
1  0.075  

Lag K = 9
CrossCorrelations : 
     0     
0   0.484  
1  -0.048  

Lag K = 10
CrossCorrelations : 
     0     
0   0.482  
1  -0.169  

Link to Java source.