// // 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-2014 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. // using System; using System.Linq; using MathNet.Numerics.Distributions; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous { /// /// Exponential distribution tests. /// [TestFixture, Category("Distributions")] public class ExponentialTests { /// /// Can create exponential. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanCreateExponential(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(lambda, n.Rate); } /// /// Exponential create fails with bad parameter. /// /// Lambda value. [TestCase(Double.NaN)] [TestCase(-1.0)] [TestCase(-10.0)] public void ExponentialCreateFailsWithBadParameters(double lambda) { Assert.Throws(() => new Exponential(lambda)); } /// /// Validate ToString. /// [Test] public void ValidateToString() { var n = new Exponential(2d); Assert.AreEqual("Exponential(λ = 2)", n.ToString()); } /// /// Can set lambda. /// /// Lambda value. [TestCase(-0.0)] [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void CanSetLambda(double lambda) { new Exponential(1.0) { Rate = lambda }; } /// /// Set lambda fails with negative lambda. /// [Test] public void SetLambdaFailsWithNegativeLambda() { var n = new Exponential(1.0); Assert.Throws(() => n.Rate = -1.0); } /// /// Validate mean. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateMean(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(1.0 / lambda, n.Mean); } /// /// Validate variance. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateVariance(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(1.0 / (lambda * lambda), n.Variance); } /// /// Validate standard deviation. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateStdDev(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(1.0 / lambda, n.StdDev); } /// /// Validate entropy. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateEntropy(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(1.0 - Math.Log(lambda), n.Entropy); } /// /// Validate skewness. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateSkewness(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(2.0, n.Skewness); } /// /// Validate mode. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateMode(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(0.0, n.Mode); } /// /// Validate median. /// /// Lambda value. [TestCase(0.0)] [TestCase(0.1)] [TestCase(1.0)] [TestCase(10.0)] [TestCase(Double.PositiveInfinity)] public void ValidateMedian(double lambda) { var n = new Exponential(lambda); Assert.AreEqual(Math.Log(2.0) / lambda, n.Median); } /// /// Validate minimum. /// [Test] public void ValidateMinimum() { var n = new Exponential(1.0); Assert.AreEqual(0.0, n.Minimum); } /// /// Validate maximum. /// [Test] public void ValidateMaximum() { var n = new Exponential(1.0); Assert.AreEqual(Double.PositiveInfinity, n.Maximum); } /// /// Validate density. /// /// Lambda value. /// Input X value. [TestCase(0.0, 0.0)] [TestCase(0.1, 0.0)] [TestCase(1.0, 0.0)] [TestCase(10.0, 0.0)] [TestCase(Double.PositiveInfinity, 0.0)] [TestCase(0.0, 0.1)] [TestCase(0.1, 0.1)] [TestCase(1.0, 0.1)] [TestCase(10.0, 0.1)] [TestCase(Double.PositiveInfinity, 0.1)] [TestCase(0.0, 1.0)] [TestCase(0.1, 1.0)] [TestCase(1.0, 1.0)] [TestCase(10.0, 1.0)] [TestCase(Double.PositiveInfinity, 1.0)] [TestCase(0.0, Double.PositiveInfinity)] [TestCase(0.1, Double.PositiveInfinity)] [TestCase(1.0, Double.PositiveInfinity)] [TestCase(10.0, Double.PositiveInfinity)] [TestCase(Double.PositiveInfinity, Double.PositiveInfinity)] public void ValidateDensity(double lambda, double x) { var n = new Exponential(lambda); if (x >= 0) { Assert.AreEqual(lambda*Math.Exp(-lambda*x), n.Density(x)); Assert.AreEqual(lambda*Math.Exp(-lambda*x), Exponential.PDF(lambda, x)); } else { Assert.AreEqual(0.0, n.Density(lambda)); Assert.AreEqual(0.0, Exponential.PDF(lambda, lambda)); } } /// /// Validate density log. /// /// Lambda value. /// Input X value. [TestCase(0.0, 0.0)] [TestCase(0.1, 0.0)] [TestCase(1.0, 0.0)] [TestCase(10.0, 0.0)] [TestCase(Double.PositiveInfinity, 0.0)] [TestCase(0.0, 0.1)] [TestCase(0.1, 0.1)] [TestCase(1.0, 0.1)] [TestCase(10.0, 0.1)] [TestCase(Double.PositiveInfinity, 0.1)] [TestCase(0.0, 1.0)] [TestCase(0.1, 1.0)] [TestCase(1.0, 1.0)] [TestCase(10.0, 1.0)] [TestCase(Double.PositiveInfinity, 1.0)] [TestCase(0.0, Double.PositiveInfinity)] [TestCase(0.1, Double.PositiveInfinity)] [TestCase(1.0, Double.PositiveInfinity)] [TestCase(10.0, Double.PositiveInfinity)] [TestCase(Double.PositiveInfinity, Double.PositiveInfinity)] public void ValidateDensityLn(double lambda, double x) { var n = new Exponential(lambda); Assert.AreEqual(Math.Log(lambda) - (lambda*x), n.DensityLn(x)); Assert.AreEqual(Math.Log(lambda) - (lambda*x), Exponential.PDFLn(lambda, x)); } /// /// Can sample. /// [Test] public void CanSample() { var n = new Exponential(1.0); n.Sample(); } /// /// Can sample sequence. /// [Test] public void CanSampleSequence() { var n = new Exponential(1.0); var ied = n.Samples(); ied.Take(5).ToArray(); } /// /// Validate cumulative distribution. /// /// Lambda value. /// Input X value. [TestCase(0.0, 0.0)] [TestCase(0.1, 0.0)] [TestCase(1.0, 0.0)] [TestCase(10.0, 0.0)] [TestCase(Double.PositiveInfinity, 0.0)] [TestCase(0.0, 0.1)] [TestCase(0.1, 0.1)] [TestCase(1.0, 0.1)] [TestCase(10.0, 0.1)] [TestCase(Double.PositiveInfinity, 0.1)] [TestCase(0.0, 1.0)] [TestCase(0.1, 1.0)] [TestCase(1.0, 1.0)] [TestCase(10.0, 1.0)] [TestCase(Double.PositiveInfinity, 1.0)] [TestCase(0.0, Double.PositiveInfinity)] [TestCase(0.1, Double.PositiveInfinity)] [TestCase(1.0, Double.PositiveInfinity)] [TestCase(10.0, Double.PositiveInfinity)] [TestCase(Double.PositiveInfinity, Double.PositiveInfinity)] public void ValidateCumulativeDistribution(double lambda, double x) { var n = new Exponential(lambda); if (x >= 0.0) { Assert.AreEqual(1.0 - Math.Exp(-lambda*x), n.CumulativeDistribution(x)); Assert.AreEqual(1.0 - Math.Exp(-lambda*x), Exponential.CDF(lambda, x)); } else { Assert.AreEqual(0.0, n.CumulativeDistribution(x)); Assert.AreEqual(0.0, Exponential.CDF(lambda, x)); } } /// /// Validate inverse cumulative distribution. /// /// Lambda value. /// Input X value. [TestCase(0.1, 0.0)] [TestCase(1.0, 0.0)] [TestCase(10.0, 0.0)] [TestCase(10.0, 0.1)] [TestCase(1.0, 1.0)] [TestCase(0.1, Double.PositiveInfinity)] [TestCase(1.0, Double.PositiveInfinity)] [TestCase(10.0, Double.PositiveInfinity)] public void ValidateInverseCumulativeDistribution(double lambda, double x) { var n = new Exponential(lambda); Assert.AreEqual(x, n.InverseCumulativeDistribution(1.0 - Math.Exp(-lambda*x))); Assert.AreEqual(x, Exponential.InvCDF(lambda, 1.0 - Math.Exp(-lambda*x))); } } }