From 8fb1d36f73a88a3cc52402fae7d7a1a8444561e2 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 21 Mar 2013 14:03:32 +0100 Subject: [PATCH] Statistics: never throw on empty data (instead use NaN) --- src/Numerics/Statistics/Statistics.cs | 43 ++++++------------- .../StatisticsTests/StatisticsTests.cs | 16 +++++-- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 9a957609..ec992a11 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -344,12 +344,7 @@ namespace MathNet.Numerics.Statistics } } - if (count == 0) - { - throw new ArgumentException(Resources.CollectionEmpty, "data"); - } - - return min; + return count == 0 ? double.NaN : min; } /// @@ -375,12 +370,7 @@ namespace MathNet.Numerics.Statistics } } - if (count == 0) - { - throw new ArgumentException(Resources.CollectionEmpty, "data"); - } - - return max; + return count == 0 ? double.NaN : max; } /// @@ -403,12 +393,7 @@ namespace MathNet.Numerics.Statistics count++; } - if (count == 0) - { - throw new ArgumentException(Resources.CollectionEmpty, "data"); - } - - return min; + return count == 0 ? double.NaN : min; } /// @@ -431,12 +416,7 @@ namespace MathNet.Numerics.Statistics count++; } - if (count == 0) - { - throw new ArgumentException(Resources.CollectionEmpty, "data"); - } - - return max; + return count == 0 ? double.NaN : max; } /// @@ -452,6 +432,11 @@ namespace MathNet.Numerics.Statistics } var dataArray = new List(data); + if (dataArray.Count == 0) + { + return double.NaN; + } + int index = (dataArray.Count / 2) + 1; if (dataArray.Count % 2 == 0) { @@ -484,11 +469,6 @@ namespace MathNet.Numerics.Statistics } } - if (nonNull.Count == 0) - { - throw new ArgumentException(Resources.CollectionEmpty, "data"); - } - return nonNull.Median(); } @@ -507,6 +487,11 @@ namespace MathNet.Numerics.Statistics } var list = new List(samples); + if (list.Count == 0) + { + return double.NaN; + } + if (order < 1 || order > list.Count) { throw new ArgumentOutOfRangeException("order", Resources.ArgumentInIntervalXYInclusive); diff --git a/src/UnitTests/StatisticsTests/StatisticsTests.cs b/src/UnitTests/StatisticsTests/StatisticsTests.cs index 9e924303..22e772bc 100644 --- a/src/UnitTests/StatisticsTests/StatisticsTests.cs +++ b/src/UnitTests/StatisticsTests/StatisticsTests.cs @@ -63,6 +63,11 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests Assert.Throws(() => Statistics.Minimum(data)); Assert.Throws(() => Statistics.Maximum(data)); Assert.Throws(() => Statistics.Mean(data)); + Assert.Throws(() => Statistics.Median(data)); + Assert.Throws(() => Statistics.Variance(data)); + Assert.Throws(() => Statistics.StandardDeviation(data)); + Assert.Throws(() => Statistics.PopulationVariance(data)); + Assert.Throws(() => Statistics.PopulationStandardDeviation(data)); Assert.Throws(() => SortedArrayStatistics.Minimum(data)); Assert.Throws(() => SortedArrayStatistics.Maximum(data)); @@ -88,9 +93,14 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests { double[] data = new double[0]; - //Assert.DoesNotThrow(() => Statistics.Minimum(data)); - //Assert.DoesNotThrow(() => Statistics.Maximum(data)); - //Assert.DoesNotThrow(() => Statistics.Mean(data)); + Assert.DoesNotThrow(() => Statistics.Minimum(data)); + Assert.DoesNotThrow(() => Statistics.Maximum(data)); + Assert.DoesNotThrow(() => Statistics.Mean(data)); + Assert.DoesNotThrow(() => Statistics.Median(data)); + Assert.DoesNotThrow(() => Statistics.Variance(data)); + Assert.DoesNotThrow(() => Statistics.StandardDeviation(data)); + Assert.DoesNotThrow(() => Statistics.PopulationVariance(data)); + Assert.DoesNotThrow(() => Statistics.PopulationStandardDeviation(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.Minimum(data)); Assert.DoesNotThrow(() => SortedArrayStatistics.Maximum(data));