Click or drag to resize

StatVariance Method (ReadOnlyDoubleMatrix, Boolean)

Returns the variance 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 double Variance(
	ReadOnlyDoubleMatrix data,
	bool adjustForBias
)

Parameters

data
Type: Novacta.AnalyticsReadOnlyDoubleMatrix
The data.
adjustForBias
Type: SystemBoolean
If set to true signals that the variance is adjusted for bias.

Return Value

Type: Double
The variance of the specified data, eventually adjusted for bias.
Exceptions
ExceptionCondition
ArgumentNullExceptiondata is null.
ArgumentExceptionadjustForBias is true and the number of entries in data 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.

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

LaTeX equation

where

LaTeX equation

is the sample central moment of order LaTeX equation, LaTeX equation is the data length and

LaTeX equation

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

LaTeX equation

Examples

In the following example, the variance of a data matrix is computed.

C#
using System;

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

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

            // Compute the data variance.
            var variance = Stat.Variance(matrix, adjustForBias);

            Console.WriteLine();
            Console.WriteLine("Data variance is:");
            Console.WriteLine(variance);

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

            Console.WriteLine();
            Console.WriteLine("Using read-only data. The variance is:");
            Console.WriteLine(varianceOfReadOnlyData);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
// 1                2                
// 2                -3               
// 3                4                
// 
// 
// 
// Data variance is:
// 4.916666666666667
// 
// Using read-only data. The variance is:
// 4.916666666666667

See Also