Browse Source

Statistics: introduce unsorted ArrayStatistics

pull/109/head
Christoph Ruegg 13 years ago
parent
commit
6861de3057
  1. 1
      src/Numerics/Numerics.csproj
  2. 96
      src/Numerics/Statistics/ArrayStatistics.cs
  3. 3
      src/Portable/Portable.csproj
  4. 46
      src/UnitTests/StatisticsTests/StatisticsTests.cs

1
src/Numerics/Numerics.csproj

@ -111,6 +111,7 @@
<Compile Include="SpecialFunctions\ModifiedStruve.cs" />
<Compile Include="SpecialFunctions\ModifiedBessel.cs" />
<Compile Include="SpecialFunctions\Logistic.cs" />
<Compile Include="Statistics\ArrayStatistics.cs" />
<Compile Include="Statistics\SortedArrayStatistics.cs" />
<Compile Include="TargetedPatchingOptOutAttribute.cs" />
<Compile Include="Distributions\Continuous\Cauchy.cs" />

96
src/Numerics/Statistics/ArrayStatistics.cs

@ -0,0 +1,96 @@
// <copyright file="ArrayStatistics.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Statistics
{
public static class ArrayStatistics
{
// TODO: Benchmark various options to find out the best approach (-> branch prediction)
// TODO: consider leveraging MKL
/// <summary>
/// Returns the smallest value from 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 Minimum(double[] data)
{
if (data == null || data.Length == 0) return double.NaN;
var min = double.PositiveInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] < min || double.IsNaN(data[i]))
{
min = data[i];
}
}
return min;
}
/// <summary>
/// Returns the smallest value from 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 Maximum(double[] data)
{
if (data == null || data.Length == 0) return double.NaN;
var max = double.NegativeInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] > max || double.IsNaN(data[i]))
{
max = data[i];
}
}
return max;
}
/// <summary>
/// Returns the sample mean from 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 Mean(double[] data)
{
if (data == null || data.Length == 0) return double.NaN;
double mean = 0;
ulong m = 0;
for (int i = 0; i < data.Length; i++)
{
mean += (data[i] - mean) / ++m;
}
return mean;
}
}
}

3
src/Portable/Portable.csproj

@ -1020,6 +1020,9 @@
<Compile Include="..\numerics\specialfunctions\Stability.cs">
<Link>SpecialFunctions\Stability.cs</Link>
</Compile>
<Compile Include="..\Numerics\Statistics\ArrayStatistics.cs">
<Link>Statistics\ArrayStatistics.cs</Link>
</Compile>
<Compile Include="..\Numerics\Statistics\Correlation.cs">
<Link>Statistics\Correlation.cs</Link>
</Compile>

46
src/UnitTests/StatisticsTests/StatisticsTests.cs

@ -89,7 +89,8 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void Mean(string dataSet)
{
var data = _data[dataSet];
AssertHelpers.AlmostEqual(data.Mean, data.Data.Mean(), 15);
AssertHelpers.AlmostEqual(data.Mean, Statistics.Mean(data.Data), 15);
AssertHelpers.AlmostEqual(data.Mean, ArrayStatistics.Mean(data.Data), 15);
}
/// <summary>
@ -107,7 +108,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void NullableMean(string dataSet)
{
var data = _data[dataSet];
AssertHelpers.AlmostEqual(data.Mean, data.DataWithNulls.Mean(), 15);
AssertHelpers.AlmostEqual(data.Mean, Statistics.Mean(data.DataWithNulls), 15);
}
/// <summary>
@ -117,7 +118,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void MeanThrowsArgumentNullException()
{
double[] data = null;
Assert.Throws<ArgumentNullException>(() => data.Mean());
Assert.Throws<ArgumentNullException>(() => Statistics.Mean(data));
}
/// <summary>
@ -136,7 +137,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void StandardDeviation(string dataSet, int digits)
{
var data = _data[dataSet];
AssertHelpers.AlmostEqual(data.StandardDeviation, data.Data.StandardDeviation(), digits);
AssertHelpers.AlmostEqual(data.StandardDeviation, Statistics.StandardDeviation(data.Data), digits);
}
/// <summary>
@ -155,7 +156,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void NullableStandardDeviation(string dataSet, int digits)
{
var data = _data[dataSet];
AssertHelpers.AlmostEqual(data.StandardDeviation, data.DataWithNulls.StandardDeviation(), digits);
AssertHelpers.AlmostEqual(data.StandardDeviation, Statistics.StandardDeviation(data.DataWithNulls), digits);
}
/// <summary>
@ -165,7 +166,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void StandardDeviationThrowsArgumentNullException()
{
double[] data = null;
Assert.Throws<ArgumentNullException>(() => data.StandardDeviation());
Assert.Throws<ArgumentNullException>(() => Statistics.StandardDeviation(data));
}
/// <summary>
@ -175,8 +176,16 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void ShortMinMax()
{
var samples = new[] {-1.0, 5, 0, -3, 10, -0.5, 4};
Assert.That(samples.Minimum(), Is.EqualTo(-3), "Min");
Assert.That(samples.Maximum(), Is.EqualTo(10), "Max");
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");
var sorted = new double[samples.Length];
Array.Copy(samples, sorted, samples.Length);
Array.Sort(sorted);
Assert.That(SortedArrayStatistics.Minimum(sorted), Is.EqualTo(-3), "Min");
Assert.That(SortedArrayStatistics.Maximum(sorted), Is.EqualTo(10), "Max");
}
/// <summary>
@ -187,11 +196,16 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
{
// -3 -1 -0.5 0 1 4 5 6 10
var samples = new[] {-1, 5, 0, -3, 10, -0.5, 4, 1, 6};
Assert.That(samples.Median(), Is.EqualTo(1), "Median");
Assert.That(Statistics.Median(samples), Is.EqualTo(1), "Median");
Assert.That(Statistics.OrderStatistic(samples, 1), Is.EqualTo(-3), "Order-1");
Assert.That(Statistics.OrderStatistic(samples, 3), Is.EqualTo(-0.5), "Order-3");
Assert.That(Statistics.OrderStatistic(samples, 7), Is.EqualTo(5), "Order-7");
Assert.That(Statistics.OrderStatistic(samples, 9), Is.EqualTo(10), "Order-9");
var sorted = new double[samples.Length];
Array.Copy(samples, sorted, samples.Length);
Array.Sort(sorted);
Assert.That(SortedArrayStatistics.Median(sorted), Is.EqualTo(1), "Median");
}
/// <summary>
@ -208,9 +222,9 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
RandomSource = new Numerics.Random.MersenneTwister(100)
};
AssertHelpers.AlmostEqual(1e+9, gaussian.Samples().Take(10000).Mean(), 11);
AssertHelpers.AlmostEqual(4d, gaussian.Samples().Take(10000).Variance(), 1);
AssertHelpers.AlmostEqual(2d, gaussian.Samples().Take(10000).StandardDeviation(), 2);
AssertHelpers.AlmostEqual(1e+9, Statistics.Mean(gaussian.Samples().Take(10000)), 11);
AssertHelpers.AlmostEqual(4d, Statistics.Variance(gaussian.Samples().Take(10000)), 1);
AssertHelpers.AlmostEqual(2d, Statistics.StandardDeviation(gaussian.Samples().Take(10000)), 2);
}
/// <summary>
@ -219,8 +233,12 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
[Test]
public void Median_CodeplexIssue5667()
{
var seq = File.ReadLines("./data/Codeplex-5667.csv").Select(s => double.Parse(s));
Assert.AreEqual(1.0, seq.Median());
var seq = File.ReadLines("./data/Codeplex-5667.csv").Select(double.Parse);
Assert.AreEqual(1.0, Statistics.Median(seq));
var sorted = seq.ToArray();
Array.Sort(sorted);
Assert.AreEqual(1.0, SortedArrayStatistics.Median(sorted));
}
}
#endif

Loading…
Cancel
Save