Click or drag to resize

StatCorrelation Method (ReadOnlyDoubleMatrix, DataOperation)

Returns the correlations among the rows or the columns of the specified data.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static DoubleMatrix Correlation(
	ReadOnlyDoubleMatrix data,
	DataOperation dataOperation
)

Parameters

data
Type: Novacta.AnalyticsReadOnlyDoubleMatrix
The data.
dataOperation
Type: Novacta.AnalyticsDataOperation
A constant to specify if the correlations are to be computed among the rows or among the columns.

Return Value

Type: DoubleMatrix
The correlations among the rows or the columns of the specified data.
Exceptions
ExceptionCondition
ArgumentNullExceptiondata is null.
ArgumentExceptiondataOperation is not a field of DataOperation.
Remarks

Given a sequence of LaTeX equation random variables LaTeX equation, their correlation matrix can be defined as that having generic entry:

LaTeX equation

By interpreting the rows or the columns of data as samples drawn from random variables, this method returns estimates of the correlation matrix of such variables. Let LaTeX equation and LaTeX equation be the data number of rows and columns, respectively, and define

LaTeX equation

Operating on rows

If dataOperation is OnRows, then the method returns the estimated correlation matrix, say LaTeX equation, among the LaTeX equation rows of data. For LaTeX equation entry LaTeX equation is NaN if at least one among the l-th and k-th rows has zero variance. Otherwise, LaTeX equation is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample mean of the i-th row.

Operating on columns

If dataOperation is OnColumns, then the method returns the estimated correlation matrix among the LaTeX equation columns of data. For LaTeX equation entry LaTeX equation is NaN if at least one among the l-th and k-th columns has zero variance. Otherwise, LaTeX equation is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample mean of the j-th column.

Examples

In the following example, the correlations among the rows and those among the columns of a data matrix are computed.

C#
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class CorrelationExample0  
    {
        public void Main()
        {
            // Create a matrix.
            var data = new double[20] {
                1, 2, -3,  6, -2,
                2, 2,  2,  0,  7,
               -3, 2,  3,  2,  9,
                5, 2,  7, -1, -4
            };
            var matrix = DoubleMatrix.Dense(4, 5, data, StorageOrder.RowMajor);
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Compute the correlations among columns.
            var correlationOnColumns = Stat.Correlation(matrix, DataOperation.OnColumns);

            Console.WriteLine();
            Console.WriteLine("Correlations among columns:");
            Console.WriteLine(correlationOnColumns);

            // Correlation is overloaded to accept data as a read-only matrix:
            // compute the correlations among rows using a read-only wrapper of the data matrix.
            ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly();
            var correlationOnRows = Stat.Correlation(readOnlyMatrix,  DataOperation.OnRows);

            Console.WriteLine();
            Console.WriteLine("Correlations among rows:");
            Console.WriteLine(correlationOnRows);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
// 1                2                -3               6                -2               
// 2                2                2                0                7                
// -3               2                3                2                9                
// 5                2                7                -1               -4               
// 
// 
// 
// Correlations among columns:
// 1                NaN              0.38632862       -0.448103279     -0.742391432     
// NaN              NaN              NaN              NaN              NaN              
// 0.38632862       NaN              1                -0.909741195     -0.0439435374    
// -0.448103279     NaN              -0.909741195     1                -0.125108648     
// -0.742391432     NaN              -0.0439435374    -0.125108648     1                
// 
// 
// 
// Correlations among rows:
// 1                -0.656407475     -0.400131212     -0.271852594     
// -0.656407475     1                0.744043096      -0.505438473     
// -0.400131212     0.744043096      1                -0.676775253     
// -0.271852594     -0.505438473     -0.676775253     1                
// 

See Also