diff --git a/src/UnitTests/DistributionTests/CommonDistributionTests.cs b/src/UnitTests/DistributionTests/CommonDistributionTests.cs
index 843c4314..42021271 100644
--- a/src/UnitTests/DistributionTests/CommonDistributionTests.cs
+++ b/src/UnitTests/DistributionTests/CommonDistributionTests.cs
@@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
-// Copyright (c) 2009-2010 Math.NET
+//
+// 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
@@ -12,8 +14,10 @@
// 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
@@ -34,102 +38,58 @@ using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests
{
- using Random = System.Random;
-
///
- /// This class will perform various tests on discrete and continuous univariate distributions. The multivariate distributions
- /// will implement these respective tests in their local unit test classes as they do not adhere to the same interfaces.
+ /// This class will perform various tests on discrete and continuous univariate distributions.
+ /// The multivariate distributions will implement these respective tests in their local unit
+ /// test classes as they do not adhere to the same interfaces.
///
[TestFixture, Category("Distributions")]
public class CommonDistributionTests
{
- ///
- /// Gets or sets the number of samples we want.
- ///
- public static int NumberOfTestSamples
- {
- get;
- set;
- }
-
- ///
- /// Gets or sets the number of buckets in the histogram for the sampling function tests.
- ///
- public static int NumberOfBuckets
- {
- get;
- set;
- }
-
- ///
- /// Gets or sets the error we want to tolerate for sampling functions.
- ///
- public static double Error
- {
- get;
- set;
- }
-
- ///
- /// Gets or sets the error probability we want to tolerate for sampling functions.
- ///
- public static double ErrorProbability
- {
- get;
- set;
- }
-
- ///
- /// The list of discrete distributions which we test.
- ///
- private List _discreteDistributions;
-
- ///
- /// The list of continuous distributions which we test.
- ///
- private List _continuousDistributions;
-
- ///
- /// Initializes static members of the CommonDistributionTests class.
- ///
- static CommonDistributionTests()
- {
- NumberOfTestSamples = 3500000;
- NumberOfBuckets = 100;
- Error = 0.01;
- ErrorProbability = 0.001;
- }
-
- ///
- /// Set-up test parameters.
- ///
- [SetUp]
- public void SetupDistributions()
- {
- _discreteDistributions = new List
- {
- new Bernoulli(0.6),
- new Binomial(0.7, 10),
- new Categorical(new[] { 0.7, 0.3 }),
- new DiscreteUniform(1, 10)
- };
+ public const int NumberOfTestSamples = 3500000;
+ public const int NumberOfHistogramBuckets = 100;
+ public const double ErrorTolerance = 0.01;
+ public const double ErrorProbability = 0.001;
- _continuousDistributions = new List
- {
- new Beta(1.0, 1.0),
- new ContinuousUniform(0.0, 1.0),
- new Gamma(1.0, 1.0),
- new Normal(0.0, 1.0),
- new Weibull(1.0, 1.0),
- new LogNormal(1.0, 1.0),
- new Triangular(0, 1, 0.7),
- new StudentT(0.0, 1.0, 5.0)
- };
- }
+ readonly List _discreteDistributions =
+ new List
+ {
+ new Bernoulli(0.6),
+ new Binomial(0.7, 10),
+ new Categorical(new[] { 0.7, 0.3 }),
+ //new ConwayMaxwellPoisson(0.2, 0.4),
+ new DiscreteUniform(1, 10),
+ //new Geometric(0.1),
+ new Hypergeometric(20, 3, 5),
+ //new NegativeBinomial(4, 0.6),
+ //new Poisson(0.4),
+ new Zipf(0.6, 10),
+ };
+
+ readonly List _continuousDistributions =
+ new List
+ {
+ new Beta(1.0, 1.0),
+ new Cauchy(1.0, 1.0),
+ new Chi(3.0),
+ new ChiSquared(3.0),
+ new ContinuousUniform(0.0, 1.0),
+ new Erlang(3, 0.4),
+ new Exponential(0.4),
+ new FisherSnedecor(0.3, 0.4),
+ new Gamma(1.0, 1.0),
+ new InverseGamma(1.0, 1.0),
+ new Laplace(1.0, 0.5),
+ new LogNormal(1.0, 1.0),
+ new Normal(0.0, 1.0),
+ new Pareto(1.0, 0.5),
+ new Rayleigh(0.8),
+ new Stable(0.5, 1.0, 0.5, 1.0),
+ new StudentT(0.0, 1.0, 5.0),
+ new Triangular(0, 1, 0.7),
+ new Weibull(1.0, 1.0),
+ };
- ///
- /// Validate that univariate distributions have random source.
- ///
[Test]
public void ValidateThatUnivariateDistributionsHaveRandomSource()
{
@@ -144,9 +104,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
}
}
- ///
- /// Can set random source.
- ///
[Test]
public void CanSetRandomSource()
{
@@ -183,30 +140,28 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Test]
public void SampleFollowsCorrectDistribution()
{
- Random rnd = new SystemRandomSource(1);
-
foreach (var dd in _discreteDistributions)
{
- dd.RandomSource = rnd;
+ dd.RandomSource = new SystemRandomSource(1, false);
var samples = new double[NumberOfTestSamples];
for (var i = 0; i < NumberOfTestSamples; i++)
{
samples[i] = dd.Sample();
}
- VapnikChervonenkisTest(Error, ErrorProbability, samples, dd);
+ VapnikChervonenkisTest(ErrorTolerance, ErrorProbability, samples, dd);
}
foreach (var cd in _continuousDistributions)
{
- cd.RandomSource = rnd;
+ cd.RandomSource = new SystemRandomSource(1, false);
var samples = new double[NumberOfTestSamples];
for (var i = 0; i < NumberOfTestSamples; i++)
{
samples[i] = cd.Sample();
}
- VapnikChervonenkisTest(Error, ErrorProbability, samples, cd);
+ VapnikChervonenkisTest(ErrorTolerance, ErrorProbability, samples, cd);
}
}
@@ -216,18 +171,18 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Test]
public void SamplesFollowsCorrectDistribution()
{
- Random rnd = new SystemRandomSource(1);
-
foreach (var dd in _discreteDistributions)
{
- dd.RandomSource = rnd;
- VapnikChervonenkisTest(Error, ErrorProbability, dd.Samples().Select(x => (double)x).Take(NumberOfTestSamples), dd);
+ dd.RandomSource = new SystemRandomSource(1, false);
+ var samples = dd.Samples().Select(x => (double)x).Take(NumberOfTestSamples).ToArray();
+ VapnikChervonenkisTest(ErrorTolerance, ErrorProbability, samples, dd);
}
foreach (var cd in _continuousDistributions)
{
- cd.RandomSource = rnd;
- VapnikChervonenkisTest(Error, ErrorProbability, cd.Samples().Take(NumberOfTestSamples), cd);
+ cd.RandomSource = new SystemRandomSource(1, false);
+ var samples = cd.Samples().Take(NumberOfTestSamples).ToArray();
+ VapnikChervonenkisTest(ErrorTolerance, ErrorProbability, samples, cd);
}
}
@@ -238,20 +193,19 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
/// The error probability we are willing to tolerate.
/// The samples to use for testing.
/// The distribution we are testing.
- public static void VapnikChervonenkisTest(double epsilon, double delta, IEnumerable s, IUnivariateDistribution dist)
+ public static void VapnikChervonenkisTest(double epsilon, double delta, double[] s, IUnivariateDistribution dist)
{
// Using VC-dimension, we can bound the probability of making an error when estimating empirical probability
// distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics".
// http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .
// For intervals on the real line the VC-dimension is 2.
- double n = s.Count();
- Assert.Greater(n, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon));
+ Assert.Greater(s.Length, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon));
- var histogram = new Histogram(s, NumberOfBuckets);
- for (var i = 0; i < NumberOfBuckets; i++)
+ var histogram = new Histogram(s, NumberOfHistogramBuckets);
+ for (var i = 0; i < NumberOfHistogramBuckets; i++)
{
var p = dist.CumulativeDistribution(histogram[i].UpperBound) - dist.CumulativeDistribution(histogram[i].LowerBound);
- var pe = histogram[i].Count / n;
+ var pe = histogram[i].Count / s.Length;
Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString());
}
}
diff --git a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs
index 9ddf97ee..6e7462c8 100644
--- a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs
+++ b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs
@@ -343,21 +343,21 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
// Extract the mean and precisions.
- var means = samples.Select(mp => mp.Mean);
- var precs = samples.Select(mp => mp.Precision);
+ var means = samples.Select(mp => mp.Mean).ToArray();
+ var precs = samples.Select(mp => mp.Precision).ToArray();
var meanMarginal = cd.MeanMarginal();
var precMarginal = cd.PrecisionMarginal();
// Check the precision distribution.
CommonDistributionTests.VapnikChervonenkisTest(
- CommonDistributionTests.Error,
+ CommonDistributionTests.ErrorTolerance,
CommonDistributionTests.ErrorProbability,
precs,
precMarginal);
// Check the mean distribution.
CommonDistributionTests.VapnikChervonenkisTest(
- CommonDistributionTests.Error,
+ CommonDistributionTests.ErrorTolerance,
CommonDistributionTests.ErrorProbability,
means,
meanMarginal);
@@ -375,21 +375,21 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var samples = cd.Samples().Take(CommonDistributionTests.NumberOfTestSamples).ToArray();
// Extract the mean and precisions.
- var means = samples.Select(mp => mp.Mean);
- var precs = samples.Select(mp => mp.Precision);
+ var means = samples.Select(mp => mp.Mean).ToArray();
+ var precs = samples.Select(mp => mp.Precision).ToArray();
var meanMarginal = cd.MeanMarginal();
var precMarginal = cd.PrecisionMarginal();
// Check the precision distribution.
CommonDistributionTests.VapnikChervonenkisTest(
- CommonDistributionTests.Error,
+ CommonDistributionTests.ErrorTolerance,
CommonDistributionTests.ErrorProbability,
precs,
precMarginal);
// Check the mean distribution.
CommonDistributionTests.VapnikChervonenkisTest(
- CommonDistributionTests.Error,
+ CommonDistributionTests.ErrorTolerance,
CommonDistributionTests.ErrorProbability,
means,
meanMarginal);