Browse Source

Statistics: single precision support on geometric, harmonic mean #235

netstandard
Christoph Ruegg 11 years ago
parent
commit
516d2d71f0
  1. 48
      src/Numerics/Statistics/ArrayStatistics.cs
  2. 28
      src/Numerics/Statistics/Statistics.cs
  3. 20
      src/Numerics/Statistics/StreamingStatistics.cs

48
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);
}
/// <summary>
/// Evaluates the geometric mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
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);
}
/// <summary>
/// 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;
}
/// <summary>
/// Evaluates the harmonic mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
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;
}
/// <summary>
/// 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;

28
src/Numerics/Statistics/Statistics.cs

@ -174,6 +174,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.GeometricMean(data);
}
/// <summary>
/// Evaluates the geometric mean.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary>
/// <param name="data">The data to calculate the geometric mean of.</param>
/// <returns>The geometric mean of the sample.</returns>
public static double GeometricMean(this IEnumerable<float> data)
{
var array = data as float[];
return array != null
? ArrayStatistics.GeometricMean(array)
: StreamingStatistics.GeometricMean(data);
}
/// <summary>
/// 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);
}
/// <summary>
/// Evaluates the harmonic mean.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary>
/// <param name="data">The data to calculate the harmonic mean of.</param>
/// <returns>The harmonic mean of the sample.</returns>
public static double HarmonicMean(this IEnumerable<float> data)
{
var array = data as float[];
return array != null
? ArrayStatistics.HarmonicMean(array)
: StreamingStatistics.HarmonicMean(data);
}
/// <summary>
/// Estimates the unbiased population variance from the provided samples.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).

20
src/Numerics/Statistics/StreamingStatistics.cs

@ -185,6 +185,16 @@ namespace MathNet.Numerics.Statistics
return m > 0 ? Math.Exp(sum / m) : double.NaN;
}
/// <summary>
/// Evaluates the geometric mean of the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param>
public static double GeometricMean(IEnumerable<float> stream)
{
return GeometricMean(stream.Select(x => (double)x));
}
/// <summary>
/// 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;
}
/// <summary>
/// Evaluates the harmonic mean of the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="stream">Sample stream, no sorting is assumed.</param>
public static double HarmonicMean(IEnumerable<float> stream)
{
return HarmonicMean(stream.Select(x => (double)x));
}
/// <summary>
/// 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).

Loading…
Cancel
Save