Click or drag to resize

StatCovariance Method (ReadOnlyDoubleMatrix, Boolean, DataOperation)

Returns the covariances among the rows or the columns of the specified data, eventually adjusted for bias.

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

Parameters

data
Type: Novacta.AnalyticsReadOnlyDoubleMatrix
The data.
adjustForBias
Type: SystemBoolean
If set to true signals that the covariances are adjusted for bias.
dataOperation
Type: Novacta.AnalyticsDataOperation
A constant to specify if the covariances are to be computed among the rows or among the columns.

Return Value

Type: DoubleMatrix
The covariances among the rows or the columns of the specified data, eventually adjusted for bias.
Exceptions
ExceptionCondition
ArgumentNullExceptiondata is null.
ArgumentExceptiondataOperation is not a field of DataOperation.
-or-
adjustForBias is true, dataOperation is OnRows and the data number of columns is less than 2.
-or-
adjustForBias is true, dataOperation is OnColumns and the data number of rows is less than 2.
Remarks

Given a sequence of LaTeX equation random variables LaTeX equation, their covariance 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 covariance 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 covariance matrix among the LaTeX equation rows of data.

If adjustForBias is set to false, then LaTeX equation is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample mean of the i-th row.

Such estimator is biased. If adjustForBias is set to true, then the estimator is corrected for bias and LaTeX equation is evaluated through the coefficient

LaTeX equation

provided that LaTeX equation is greater than 1; otherwise, an exception is thrown.

Operating on columns

If dataOperation is OnColumns, then the method returns the estimated covariance matrix among the LaTeX equation columns of data.

If adjustForBias is set to false, then LaTeX equation is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample mean of the j-th column.

Such estimator is biased. If adjustForBias is set to true, then the estimator is corrected for bias and LaTeX equation is evaluated through the coefficient

LaTeX equation

provided that LaTeX equation is greater than 1; otherwise, an exception is thrown.

Examples

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

C#
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class CovarianceExample0  
    {
        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);

            // Covariance can be adjusted for bias.
            bool adjustForBias = true; 

            // Compute the covariances among columns.
            var covarianceOnColumns = Stat.Covariance(matrix, adjustForBias, DataOperation.OnColumns);

            Console.WriteLine();
            Console.WriteLine("Covariances among columns:");
            Console.WriteLine(covarianceOnColumns);

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

            Console.WriteLine();
            Console.WriteLine("Covariances among rows:");
            Console.WriteLine(covarianceOnRows);
        }
    }
}

// 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               
// 
// 
// 
// Covariances among columns:
// 10.9166667       0                5.25             -4.58333333      -15.8333333      
// 0                0                0                0                0                
// 5.25             0                16.9166667       -11.5833333      -1.16666667      
// -4.58333333      0                -11.5833333      9.58333333       -2.5             
// -15.8333333      0                -1.16666667      -2.5             41.6666667       
// 
// 
// 
// Covariances among rows:
// 12.7             -6.1             -6.1             -4.3             
// -6.1             6.8              8.3              -5.85            
// -6.1             8.3              18.3             -12.85           
// -4.3             -5.85            -12.85           19.7             
// 

See Also