Click or drag to resize

StatVariance Method (DoubleMatrix, Boolean, DataOperation)

Returns the variance of each row or column in 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 Variance(
	DoubleMatrix data,
	bool adjustForBias,
	DataOperation dataOperation
)

Parameters

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

Return Value

Type: DoubleMatrix
The variance of each row or column in 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

The variance of a random variable LaTeX equation can be defined as follows:

LaTeX equation

where

LaTeX equation

is the LaTeX equation central moment of order LaTeX equation.

By interpreting rows or columns of data as samples drawn from random variables, this method returns the variance estimates 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 a column vector whose length equals the number of rows of data. The i-th entry of such column returns the variance of the i-th data row.

If adjustForBias is set to false, then the variance of the i-th row is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample LaTeX equation-th central moment of the i-th row.

Such estimator is biased. If adjustForBias is set to true, then the estimator is corrected for bias and the i-th variance is evaluated through the coefficient

LaTeX equation

Operating on columns

If dataOperation is OnColumns, then the method returns a row vector whose length is the data number of columns. The j-th entry of the returned row exposes the variance of the j-th data column.

If adjustForBias is set to false, then the variance of the j-th column is estimated through the coefficient

LaTeX equation

where

LaTeX equation

is the sample LaTeX equation-th central moment of the j-th column.

Such estimator is biased. If adjustForBias is set to true, then the estimator is corrected for bias and the j-th variance is evaluated through the coefficient

LaTeX equation

Examples

In the following example, row and column variance estimates in a data matrix are computed.

C#
using System;

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

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

            // Compute the variance on columns.
            var varianceOnColumns = Stat.Variance(matrix, adjustForBias, DataOperation.OnColumns);

            Console.WriteLine();
            Console.WriteLine("Variance on columns:");
            Console.WriteLine(varianceOnColumns);

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

            Console.WriteLine();
            Console.WriteLine("Variance on rows:");
            Console.WriteLine(varianceOnRows);
        }
    }
}

// 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               
// 
// 
// 
// Variance on columns:
// 10.9166667       0                16.9166667       9.58333333       41.6666667       
// 
// 
// 
// Variance on rows:
// 12.7             
// 6.8              
// 18.3             
// 19.7             
// 

See Also