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 X1 and Y and between X2 and Y with lags from -maximumLag = -10 through lag maximumLag = 10.

using System;
using Imsl.Stat;
using PrintMatrix = Imsl.Math.PrintMatrix;
using Matrix = Imsl.Math.Matrix;

public class MultiCrossCorrelationEx1
{
    public static void  Main(String[] args)
    {
        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[,] tmpArr = new double[x.GetLength(1), y.GetLength(1)];
        double[,,] ccv = mcc.GetCrossCovariance();
        Console.Out.WriteLine
            ("Multichannel cross-covariance between X and Y");
        for (i = 0; i < 21; i++)
        {
            for (int j=0;j<x.GetLength(1);j++)
                for (int k=0;k<y.GetLength(1);k++)
                    tmpArr[j,k] = ccv[i,j,k];
            Console.Out.WriteLine("Lag K = " + (i - 10));
            new PrintMatrix("CrossCovariances : ").Print(tmpArr);
        }

        double[,,] cc = mcc.GetCrossCorrelation();
        Console.Out.WriteLine
            ("Multichannel cross-correlation between X and Y");
        for (i = 0; i < 21; i++)
        {
            for (int j=0;j<x.GetLength(1);j++)
                for (int k=0;k<y.GetLength(1);k++)
                    tmpArr[j,k] = cc[i,j,k];
            Console.Out.WriteLine("Lag K = " + (i - 10));
            new PrintMatrix("CrossCorrelations : ").Print(tmpArr);
        }
    }
}

Output

Mean of X :  
     0    
0  63.43  
1  97.97  

Variance of X :  
       0      
0  2643.6851  
1  1978.4291  

Mean of Y :  
     0    
0  46.94  

Variance of Y :  
       0      
0  1383.7564  

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

Lag K = -9
 CrossCovariances : 
          0          
0  65.0243098901099  
1  38.1363054945055  

Lag K = -8
 CrossCovariances : 
          0          
0  216.637243478261  
1  135.57832173913   

Lag K = -7
 CrossCovariances : 
          0          
0  246.793769892473  
1  100.362230107527  

Lag K = -6
 CrossCovariances : 
           0          
0  142.127923404255   
1   44.9678638297872  

Lag K = -5
 CrossCovariances : 
           0          
0   50.6970421052632  
1  -11.8094631578948  

Lag K = -4
 CrossCovariances : 
          0          
0  72.6846166666667  
1  32.6926333333334  

Lag K = -3
 CrossCovariances : 
           0          
0  217.854096907217   
1  -40.1185092783505  

Lag K = -2
 CrossCovariances : 
           0          
0   355.820628571429  
1  -152.649118367347  

Lag K = -1
 CrossCovariances : 
           0          
0   579.653492929293  
1  -212.95022020202   

Lag K = 0
CrossCovariances : 
       0      
0   821.6258  
1  -104.7518  

Lag K = 1
 CrossCovariances : 
           0          
0  810.131371717171   
1   55.1601838383839  

Lag K = 2
 CrossCovariances : 
           0          
0  628.385118367347   
1   84.7751673469388  

Lag K = 3
 CrossCovariances : 
           0          
0  438.271931958763   
1   75.9630371134021  

Lag K = 4
 CrossCovariances : 
          0          
0  238.792741666667  
1  200.383466666667  

Lag K = 5
 CrossCovariances : 
          0          
0  143.621147368421  
1  282.986431578947  

Lag K = 6
 CrossCovariances : 
          0          
0  252.973774468085  
1  234.393289361702  

Lag K = 7
 CrossCovariances : 
          0          
0  479.468286021505  
1  223.033735483871  

Lag K = 8
 CrossCovariances : 
          0          
0  724.912243478261  
1  124.456582608696  

Lag K = 9
 CrossCovariances : 
           0          
0  924.971232967034   
1  -79.5174307692309  

Lag K = 10
 CrossCovariances : 
           0          
0   922.759311111112  
1  -279.286422222222  

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

Lag K = -9
 CrossCorrelations : 
           0           
0  0.0339970370656115  
1  0.023048812287829   

Lag K = -8
 CrossCorrelations : 
           0           
0  0.113265706453004   
1  0.0819407975561327  

Lag K = -7
 CrossCorrelations : 
           0           
0  0.129032618058936   
1  0.0606569035081169  

Lag K = -6
 CrossCorrelations : 
           0           
0  0.074309566502109   
1  0.0271776680765982  

Lag K = -5
  CrossCorrelations : 
            0            
0   0.0265062285548632   
1  -0.00713740085770933  

Lag K = -4
 CrossCorrelations : 
           0           
0  0.0380021196855836  
1  0.0197587668528454  

Lag K = -3
  CrossCorrelations : 
            0           
0   0.11390192098873    
1  -0.0242468161934945  

Lag K = -2
  CrossCorrelations : 
            0           
0   0.186035762912295   
1  -0.0922580420292281  

Lag K = -1
 CrossCorrelations : 
           0           
0   0.303063597562697  
1  -0.128702809263875  

Lag K = 0
  CrossCorrelations : 
            0           
0   0.429575382251174   
1  -0.0633098708358119  

Lag K = 1
 CrossCorrelations : 
           0           
0  0.423565683647071   
1  0.0333377002981115  

Lag K = 2
 CrossCorrelations : 
           0          
0  0.328542235922487  
1  0.051236397797642  

Lag K = 3
 CrossCorrelations : 
           0           
0  0.22914425606054    
1  0.0459105243818767  

Lag K = 4
 CrossCorrelations : 
           0          
0  0.124849394067548  
1  0.121107717407232  

Lag K = 5
 CrossCorrelations : 
           0          
0  0.075090277447643  
1  0.171031279954621  

Lag K = 6
 CrossCorrelations : 
           0          
0  0.132263745693782  
1  0.141662566889261  

Lag K = 7
 CrossCorrelations : 
           0          
0  0.250683184784367  
1  0.134797082107539  

Lag K = 8
 CrossCorrelations : 
           0           
0  0.37901007257894    
1  0.0752190432013873  

Lag K = 9
  CrossCorrelations : 
            0           
0   0.48360807434863    
1  -0.0480587280714567  

Lag K = 10
 CrossCorrelations : 
           0           
0   0.48245160241607   
1  -0.168795069078383  


Link to C# source.