diff --git a/src/Numerics/Statistics/ArrayStatistics.cs b/src/Numerics/Statistics/ArrayStatistics.cs index 590bc016..df9e7c91 100644 --- a/src/Numerics/Statistics/ArrayStatistics.cs +++ b/src/Numerics/Statistics/ArrayStatistics.cs @@ -172,7 +172,7 @@ namespace MathNet.Numerics.Statistics { if (data.Length == 0) { - return float.NaN; + return double.NaN; } double mean = 0; @@ -206,6 +206,27 @@ namespace MathNet.Numerics.Statistics return Math.Exp(sum/data.Length); } + /// + /// Evaluates the geometric mean of the unsorted data array. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample array, no sorting is assumed. + public static double GeometricMean(float[] data) + { + if (data.Length == 0) + { + return double.NaN; + } + + double sum = 0; + for (int i = 0; i < data.Length; i++) + { + sum += Math.Log(data[i]); + } + + return Math.Exp(sum / data.Length); + } + /// /// Evaluates the harmonic mean of the unsorted data array. /// Returns NaN if data is empty or any entry is NaN. @@ -227,6 +248,27 @@ namespace MathNet.Numerics.Statistics return data.Length/sum; } + /// + /// Evaluates the harmonic mean of the unsorted data array. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample array, no sorting is assumed. + public static double HarmonicMean(float[] data) + { + if (data.Length == 0) + { + return double.NaN; + } + + double sum = 0; + for (int i = 0; i < data.Length; i++) + { + sum += 1.0 / data[i]; + } + + return data.Length / sum; + } + /// /// Estimates the unbiased population variance from the provided samples as unsorted array. /// On a dataset of size N will use an N-1 normalizer (Bessel's correction). @@ -262,7 +304,7 @@ namespace MathNet.Numerics.Statistics { if (samples.Length <= 1) { - return float.NaN; + return double.NaN; } double variance = 0; @@ -312,7 +354,7 @@ namespace MathNet.Numerics.Statistics { if (population.Length == 0) { - return float.NaN; + return double.NaN; } double variance = 0; diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 0134e070..6d78f174 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -174,6 +174,20 @@ namespace MathNet.Numerics.Statistics : StreamingStatistics.GeometricMean(data); } + /// + /// Evaluates the geometric mean. + /// Returns NaN if data is empty or if any entry is NaN. + /// + /// The data to calculate the geometric mean of. + /// The geometric mean of the sample. + public static double GeometricMean(this IEnumerable data) + { + var array = data as float[]; + return array != null + ? ArrayStatistics.GeometricMean(array) + : StreamingStatistics.GeometricMean(data); + } + /// /// Evaluates the harmonic mean. /// Returns NaN if data is empty or if any entry is NaN. @@ -188,6 +202,20 @@ namespace MathNet.Numerics.Statistics : StreamingStatistics.HarmonicMean(data); } + /// + /// Evaluates the harmonic mean. + /// Returns NaN if data is empty or if any entry is NaN. + /// + /// The data to calculate the harmonic mean of. + /// The harmonic mean of the sample. + public static double HarmonicMean(this IEnumerable data) + { + var array = data as float[]; + return array != null + ? ArrayStatistics.HarmonicMean(array) + : StreamingStatistics.HarmonicMean(data); + } + /// /// Estimates the unbiased population variance from the provided samples. /// On a dataset of size N will use an N-1 normalizer (Bessel's correction). diff --git a/src/Numerics/Statistics/StreamingStatistics.cs b/src/Numerics/Statistics/StreamingStatistics.cs index f258e915..4076c66e 100644 --- a/src/Numerics/Statistics/StreamingStatistics.cs +++ b/src/Numerics/Statistics/StreamingStatistics.cs @@ -185,6 +185,16 @@ namespace MathNet.Numerics.Statistics return m > 0 ? Math.Exp(sum / m) : double.NaN; } + /// + /// Evaluates the geometric mean of the enumerable, in a single pass without memoization. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample stream, no sorting is assumed. + public static double GeometricMean(IEnumerable stream) + { + return GeometricMean(stream.Select(x => (double)x)); + } + /// /// Evaluates the harmonic mean of the enumerable, in a single pass without memoization. /// Returns NaN if data is empty or any entry is NaN. @@ -204,6 +214,16 @@ namespace MathNet.Numerics.Statistics return m > 0 ? m/sum : double.NaN; } + /// + /// Evaluates the harmonic mean of the enumerable, in a single pass without memoization. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample stream, no sorting is assumed. + public static double HarmonicMean(IEnumerable stream) + { + return HarmonicMean(stream.Select(x => (double)x)); + } + /// /// Estimates the unbiased population variance from the provided samples as enumerable sequence, in a single pass without memoization. /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).