Table of Contents

Class Statistics

Namespace
Utils.Mathematics
Assembly
Utils.Mathematics.dll

Descriptive statistics computed over sequences of floating-point values.

public static class Statistics
Inheritance
Statistics
Inherited Members

Remarks

Non-finite value policy: every operation here that constrains T to IFloatingPoint<TSelf> validates each input value with T.IsFinite(value) as it is enumerated and rejects the sequence with an ArgumentException on the first NaN or infinite value found, rather than letting it propagate into a NaN/infinite (and implementation-dependent, since propagation depends on where the value falls relative to sort order or accumulation order) result. This is a single consistent policy applied uniformly across Mean<T>(IEnumerable<T>), Variance<T>(IEnumerable<T>), Covariance<T>(IEnumerable<T>, IEnumerable<T>), and Correlation<T>(IEnumerable<T>, IEnumerable<T>).

Median<T>(IEnumerable<T>) is generic over any IComparable<T>, not just floating-point types, so it cannot apply the same finiteness check; its behavior for a type whose CompareTo(T) does not define a total order (floating-point NaN under the default comparer being the standard example) is therefore whatever Sort<T>(T[]) does with such values, which is unspecified. Validate finiteness before calling if that matters.

Methods

Correlation<T>(IEnumerable<T>, IEnumerable<T>)

Returns the Pearson correlation coefficient between two sequences of equal length.

public static T Correlation<T>(IEnumerable<T> x, IEnumerable<T> y) where T : struct, IFloatingPoint<T>, IRootFunctions<T>

Parameters

x IEnumerable<T>

First sequence, all values finite.

y IEnumerable<T>

Second sequence, all values finite.

Returns

T

The correlation coefficient in [āˆ’1, 1].

Type Parameters

T

Floating-point element type.

Remarks

Computes both variances and the covariance in a single online pass (rather than materializing both sequences and separately calling Covariance<T>(IEnumerable<T>, IEnumerable<T>) and StdDev<T>(IEnumerable<T>), each of which would re-enumerate and independently recompute its own mean).

Exceptions

ArgumentException

Thrown when the sequences have different lengths, fewer than two elements, or a value is non-finite.

InvalidOperationException

Thrown when either sequence has zero variance.

Covariance<T>(IEnumerable<T>, IEnumerable<T>)

Returns the sample covariance between two sequences of equal length.

public static T Covariance<T>(IEnumerable<T> x, IEnumerable<T> y) where T : struct, IFloatingPoint<T>

Parameters

x IEnumerable<T>

First sequence, all values finite.

y IEnumerable<T>

Second sequence, all values finite.

Returns

T

The sample covariance.

Type Parameters

T

Floating-point element type.

Exceptions

ArgumentException

Thrown when sequences have different lengths, fewer than two elements, or a value is non-finite.

Mean<T>(IEnumerable<T>)

Returns the arithmetic mean of the sequence. Uses Welford's online algorithm for numerical stability.

public static T Mean<T>(IEnumerable<T> values) where T : struct, IFloatingPoint<T>

Parameters

values IEnumerable<T>

Input sequence. Must contain at least one element, all finite.

Returns

T

The mean value.

Type Parameters

T

Floating-point element type.

Exceptions

ArgumentException

Thrown when values is empty or contains a non-finite value.

Median<T>(IEnumerable<T>)

Returns the median of the sequence. For an even number of elements, returns the lower of the two middle values.

public static T Median<T>(IEnumerable<T> values) where T : struct, IComparable<T>

Parameters

values IEnumerable<T>

Input sequence. Must contain at least one element.

Returns

T

The median value.

Type Parameters

T

Comparable element type.

Exceptions

ArgumentException

Thrown when values is empty.

StdDev<T>(IEnumerable<T>)

Returns the sample standard deviation of the sequence.

public static T StdDev<T>(IEnumerable<T> values) where T : struct, IFloatingPoint<T>, IRootFunctions<T>

Parameters

values IEnumerable<T>

Input sequence. Must contain at least two elements.

Returns

T

The standard deviation.

Type Parameters

T

Floating-point element type.

Variance<T>(IEnumerable<T>)

Returns the sample variance (divided by nāˆ’1) of the sequence. Uses Welford's online algorithm for numerical stability.

public static T Variance<T>(IEnumerable<T> values) where T : struct, IFloatingPoint<T>

Parameters

values IEnumerable<T>

Input sequence. Must contain at least two elements, all finite.

Returns

T

The sample variance.

Type Parameters

T

Floating-point element type.

Exceptions

ArgumentException

Thrown when fewer than two elements are provided, or a value is non-finite.