// // 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-2010 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. // namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous { using System; using System.Linq; using Distributions; using NUnit.Framework; /// /// Normal distribution tests. /// [TestFixture] public class NormalTests { /// /// Set-up parameters. /// [SetUp] public void SetUp() { Control.CheckDistributionParameters = true; } /// /// Can create standard normal. /// [Test] public void CanCreateStandardNormal() { var n = new Normal(); Assert.AreEqual(0.0, n.Mean); Assert.AreEqual(1.0, n.StdDev); } /// /// Can create normal. /// /// Mean value. /// Standard deviation value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void CanCreateNormal(double mean, double sdev) { var n = new Normal(mean, sdev); Assert.AreEqual(mean, n.Mean); Assert.AreEqual(sdev, n.StdDev); } /// /// Normal create fails with bad parameters. /// /// Mean value. /// Standard deviation value. [TestCase(Double.NaN, 1.0)] [TestCase(1.0, Double.NaN)] [TestCase(Double.NaN, Double.NaN)] [TestCase(1.0, -1.0)] public void NormalCreateFailsWithBadParameters(double mean, double sdev) { Assert.Throws(() => new Normal(mean, sdev)); } /// /// Can create normal from mean and standard deviation. /// /// Mean value. /// Standard deviation value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void CanCreateNormalFromMeanAndStdDev(double mean, double sdev) { var n = Normal.WithMeanStdDev(mean, sdev); Assert.AreEqual(mean, n.Mean); Assert.AreEqual(sdev, n.StdDev); } /// /// Can create normal from mean and variance. /// /// Mean value. /// Variance value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void CanCreateNormalFromMeanAndVariance(double mean, double var) { var n = Normal.WithMeanVariance(mean, var); AssertHelpers.AlmostEqual(mean, n.Mean, 16); AssertHelpers.AlmostEqual(var, n.Variance, 16); } /// /// Can create normal from mean and precision. /// /// Mean value. /// Precision value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void CanCreateNormalFromMeanAndPrecision(double mean, double prec) { var n = Normal.WithMeanPrecision(mean, prec); AssertHelpers.AlmostEqual(mean, n.Mean, 15); AssertHelpers.AlmostEqual(prec, n.Precision, 15); } /// /// Validate ToString. /// [Test] public void ValidateToString() { var n = new Normal(1.0, 2.0); Assert.AreEqual("Normal(Mean = 1, StdDev = 2)", n.ToString()); } /// /// Can set precision. /// /// Precision value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanSetPrecision(double prec) { new Normal { Precision = prec }; } /// /// Set precision fails with negative value. /// [Test] public void SetPrecisionFailsWithNegativePrecision() { var n = new Normal(); Assert.Throws(() => n.Precision = -1.0); } /// /// Can set variance. /// /// Variance value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanSetVariance(double var) { new Normal { Variance = var }; } /// /// Set variance fails with negative value. /// [Test] public void SetVarianceFailsWithNegativeVariance() { var n = new Normal(); Assert.Throws(() => n.Variance = -1.0); } /// /// Can set standard deviation. /// /// Standard deviation value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanSetStdDev(double sdev) { new Normal { StdDev = sdev }; } /// /// Set standard deviation fails with negative value. /// [Test] public void SetStdDevFailsWithNegativeStdDev() { var n = new Normal(); Assert.Throws(() => n.StdDev = -1.0); } /// /// Can set mean. /// /// Mean value. [TestCase(Double.NegativeInfinity)] [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanSetMean(double mean) { new Normal { Mean = mean }; } /// /// Validate entropy. /// /// Standard deviation value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateEntropy(double sdev) { var n = new Normal(1.0, sdev); Assert.AreEqual(Constants.LogSqrt2PiE + Math.Log(n.StdDev), n.Entropy); } /// /// Validate skewness. /// /// Standard deviation value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateSkewness(double sdev) { var n = new Normal(1.0, sdev); Assert.AreEqual(0.0, n.Skewness); } /// /// Validate mean. /// /// Mean value. [TestCase(Double.NegativeInfinity)] [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateMode(double mean) { var n = new Normal(mean, 1.0); Assert.AreEqual(mean, n.Mode); } /// /// Validate median. /// /// Mean value. [TestCase(Double.NegativeInfinity)] [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateMedian(double mean) { var n = new Normal(mean, 1.0); Assert.AreEqual(mean, n.Median); } /// /// Validate minimum. /// [Test] public void ValidateMinimum() { var n = new Normal(); Assert.AreEqual(Double.NegativeInfinity, n.Minimum); } /// /// Validate maximum. /// [Test] public void ValidateMaximum() { var n = new Normal(); Assert.AreEqual(Double.PositiveInfinity, n.Maximum); } /// /// Validate density. /// /// Mean value. /// Standard deviation value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void ValidateDensity(double mean, double sdev) { var n = Normal.WithMeanStdDev(mean, sdev); for (var i = 0; i < 11; i++) { var x = i - 5.0; var d = (mean - x) / sdev; var pdf = Math.Exp(-0.5 * d * d) / (sdev * Constants.Sqrt2Pi); Assert.AreEqual(pdf, n.Density(x)); } } /// /// Validate density log. /// /// Mean value. /// Standard deviation value. [TestCase(0.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(-5.0, 1.0)] [TestCase(0.0, 10.0)] [TestCase(10.0, 100.0)] [TestCase(-5.0, Double.PositiveInfinity)] public void ValidateDensityLn(double mean, double sdev) { var n = Normal.WithMeanStdDev(mean, sdev); for (var i = 0; i < 11; i++) { var x = i - 5.0; var d = (mean - x) / sdev; var pdfln = (-0.5 * (d * d)) - Math.Log(sdev) - Constants.LogSqrt2Pi; Assert.AreEqual(pdfln, n.DensityLn(x)); } } /// /// Can sample static. /// [Test] public void CanSampleStatic() { Normal.Sample(new Random(), 0.0, 1.0); } /// /// Can sample sequence static. /// [Test] public void CanSampleSequenceStatic() { var ied = Normal.Samples(new Random(), 0.0, 1.0); ied.Take(5).ToArray(); } /// /// Fail sample static with bad parameters. /// [Test] public void FailSampleStatic() { Assert.Throws(() => { var d = Normal.Sample(new Random(), 0.0, -1.0); }); } /// /// Fail sample sequence static with bad parameters. /// [Test] public void FailSampleSequenceStatic() { Assert.Throws(() => { var ied = Normal.Samples(new Random(), 0.0, -1.0).First(); }); } /// /// Can sample. /// [Test] public void CanSample() { var n = new Normal(); n.Sample(); } /// /// Can sample sequence. /// [Test] public void CanSampleSequence() { var n = new Normal(); var ied = n.Samples(); ied.Take(5).ToArray(); } /// /// Validate cumulative distribution. /// /// Input X value. /// Expected value. [TestCase(Double.NegativeInfinity, 0.0)] [TestCase(-5.0, 0.00000028665157187919391167375233287464535385442301361187883)] [TestCase(-2.0, 0.0002326290790355250363499258867279847735487493358890356)] [TestCase(-0.0, 0.0062096653257761351669781045741922211278977469230927036)] [TestCase(0.0, 0.0062096653257761351669781045741922211278977469230927036)] [TestCase(4.0, 0.30853753872598689636229538939166226011639782444542207)] [TestCase(5.0, 0.5)] [TestCase(6.0, 0.69146246127401310363770461060833773988360217555457859)] [TestCase(10.0, 0.9937903346742238648330218954258077788721022530769078)] [TestCase(Double.PositiveInfinity, 1.0)] public void ValidateCumulativeDistribution(double x, double f) { var n = Normal.WithMeanStdDev(5.0, 2.0); AssertHelpers.AlmostEqual(f, n.CumulativeDistribution(x), 10); } /// /// Validate inverse cumulative distribution. /// /// Input X value. /// Expected value. [TestCase(Double.NegativeInfinity, 0.0)] [TestCase(-5.0, 0.00000028665157187919391167375233287464535385442301361187883)] [TestCase(-2.0, 0.0002326290790355250363499258867279847735487493358890356)] [TestCase(-0.0, 0.0062096653257761351669781045741922211278977469230927036)] [TestCase(0.0, .0062096653257761351669781045741922211278977469230927036)] [TestCase(4.0, .30853753872598689636229538939166226011639782444542207)] [TestCase(5.0, .5)] [TestCase(6.0, .69146246127401310363770461060833773988360217555457859)] [TestCase(10.0, 0.9937903346742238648330218954258077788721022530769078)] [TestCase(Double.PositiveInfinity, 1.0)] public void ValidateInverseCumulativeDistribution(double x, double f) { var n = Normal.WithMeanStdDev(5.0, 2.0); AssertHelpers.AlmostEqual(x, n.InverseCumulativeDistribution(f), 15); } } }