Browse Source

Statistics: sample/unibiased variance of empty set should be NaN #101

v2
Christoph Ruegg 14 years ago
parent
commit
669005b509
  1. 8
      src/Numerics/Statistics/Statistics.cs
  2. 16
      src/UnitTests/StatisticsTests/StatisticsTests.cs

8
src/Numerics/Statistics/Statistics.cs

@ -88,7 +88,7 @@ namespace MathNet.Numerics.Statistics
} }
/// <summary> /// <summary>
/// Calculates the unbiased population variance estimator (on a dataset of size N will use an N-1 normalizer). /// Calculates the unbiased population (sample) variance estimator (on a dataset of size N will use an N-1 normalizer).
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="data">The data to calculate the variance of.</param>
/// <returns>The unbiased population variance of the sample.</returns> /// <returns>The unbiased population variance of the sample.</returns>
@ -121,11 +121,11 @@ namespace MathNet.Numerics.Statistics
} }
} }
return variance / (j - 1); return j > 1 ? variance/(j - 1) : double.NaN;
} }
/// <summary> /// <summary>
/// Computes the unbiased population variance estimator (on a dataset of size N will use an N-1 normalizer) for nullable data. /// Computes the unbiased population (sample) variance estimator (on a dataset of size N will use an N-1 normalizer) for nullable data.
/// </summary> /// </summary>
/// <param name="data">The data to calculate the variance of.</param> /// <param name="data">The data to calculate the variance of.</param>
/// <returns>The population variance of the sample.</returns> /// <returns>The population variance of the sample.</returns>
@ -171,7 +171,7 @@ namespace MathNet.Numerics.Statistics
} }
} }
return variance / (j - 1); return j > 1 ? variance/(j - 1) : double.NaN;
} }
/// <summary> /// <summary>

16
src/UnitTests/StatisticsTests/StatisticsTests.cs

@ -227,6 +227,22 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
AssertHelpers.AlmostEqual(2d, Statistics.StandardDeviation(gaussian.Samples().Take(10000)), 2); AssertHelpers.AlmostEqual(2d, Statistics.StandardDeviation(gaussian.Samples().Take(10000)), 2);
} }
[Test]
public void SampleVarianceOfEmptyAndSingleMustBeNaN()
{
Assert.That(Statistics.Variance(new double[0]), Is.NaN);
Assert.That(Statistics.Variance(new[] { 2d }), Is.NaN);
Assert.That(Statistics.Variance(new[] { 2d, 3d }), Is.Not.NaN);
}
[Test]
public void PopulationVarianceOfEmptyMustBeNaN()
{
Assert.That(Statistics.PopulationVariance(new double[0]), Is.NaN);
Assert.That(Statistics.PopulationVariance(new[] { 2d }), Is.Not.NaN);
Assert.That(Statistics.PopulationVariance(new[] { 2d, 3d }), Is.Not.NaN);
}
/// <summary> /// <summary>
/// URL http://mathnetnumerics.codeplex.com/workitem/5667 /// URL http://mathnetnumerics.codeplex.com/workitem/5667
/// </summary> /// </summary>

Loading…
Cancel
Save