Browse Source

Statistics: single precision support on Minimum/Maximum #235

netstandard
Christoph Ruegg 11 years ago
parent
commit
1a05798c53
  1. 28
      src/Numerics/Statistics/SortedArrayStatistics.cs
  2. 29
      src/Numerics/Statistics/Statistics.cs
  3. 46
      src/Numerics/Statistics/StreamingStatistics.cs
  4. 16
      src/UnitTests/StatisticsTests/StatisticsTests.cs

28
src/Numerics/Statistics/SortedArrayStatistics.cs

@ -54,6 +54,20 @@ namespace MathNet.Numerics.Statistics
return data[0];
}
/// <summary>
/// Returns the smallest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Minimum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[0];
}
/// <summary>
/// Returns the largest value from the sorted data array (ascending).
/// </summary>
@ -68,6 +82,20 @@ namespace MathNet.Numerics.Statistics
return data[data.Length - 1];
}
/// <summary>
/// Returns the largest value from the sorted data array (ascending).
/// </summary>
/// <param name="data">Sample array, must be sorted ascendingly.</param>
public static float Maximum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
return data[data.Length - 1];
}
/// <summary>
/// Returns the order statistic (order 1..N) from the sorted data array (ascending).
/// </summary>

29
src/Numerics/Statistics/Statistics.cs

@ -53,6 +53,21 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.Minimum(data);
}
/// <summary>
/// Returns the minimum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary>
/// <param name="data">The sample data.</param>
/// <returns>The minimum value in the sample data.</returns>
public static float Minimum(this IEnumerable<float> data)
{
var array = data as float[];
return array != null
? ArrayStatistics.Minimum(array)
: StreamingStatistics.Minimum(data);
}
/// <summary>
/// Returns the minimum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
@ -79,6 +94,20 @@ namespace MathNet.Numerics.Statistics
: StreamingStatistics.Maximum(data);
}
/// <summary>
/// Returns the maximum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.
/// </summary>
/// <param name="data">The sample data.</param>
/// <returns>The maximum value in the sample data.</returns>
public static float Maximum(this IEnumerable<float> data)
{
var array = data as float[];
return array != null
? ArrayStatistics.Maximum(array)
: StreamingStatistics.Maximum(data);
}
/// <summary>
/// Returns the maximum value in the sample data.
/// Returns NaN if data is empty or if any entry is NaN.

46
src/Numerics/Statistics/StreamingStatistics.cs

@ -66,6 +66,29 @@ namespace MathNet.Numerics.Statistics
return any ? min : double.NaN;
}
/// <summary>
/// Returns the smallest value from 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 float Minimum(IEnumerable<float> stream)
{
var min = float.PositiveInfinity;
bool any = false;
foreach (var d in stream)
{
if (d < min || float.IsNaN(d))
{
min = d;
}
any = true;
}
return any ? min : float.NaN;
}
/// <summary>
/// Returns the largest value from the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.
@ -89,6 +112,29 @@ namespace MathNet.Numerics.Statistics
return any ? max : double.NaN;
}
/// <summary>
/// Returns the largest value from 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 float Maximum(IEnumerable<float> stream)
{
var max = float.NegativeInfinity;
bool any = false;
foreach (var d in stream)
{
if (d > max || float.IsNaN(d))
{
max = d;
}
any = true;
}
return any ? max : float.NaN;
}
/// <summary>
/// Estimates the arithmetic sample mean from the enumerable, in a single pass without memoization.
/// Returns NaN if data is empty or any entry is NaN.

16
src/UnitTests/StatisticsTests/StatisticsTests.cs

@ -291,6 +291,22 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max");
}
[Test]
public void MinimumMaximumOnShortSequence32()
{
var samples = new[] { -1.0f, 5f, 0f, -3f, 10f, -0.5f, 4f };
Assert.That(Statistics.Minimum(samples), Is.EqualTo(-3), "Min");
Assert.That(Statistics.Maximum(samples), Is.EqualTo(10), "Max");
Assert.That(ArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
Assert.That(ArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max");
Assert.That(StreamingStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
Assert.That(StreamingStatistics.Maximum(samples), Is.EqualTo(10), "Max");
Array.Sort(samples);
Assert.That(SortedArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max");
}
[Test]
public void OrderStatisticsOnShortSequence()
{

Loading…
Cancel
Save