// // 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-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 // 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.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.Distributions { /// /// Discrete Univariate Uniform distribution. /// The discrete uniform distribution is a distribution over integers. The distribution /// is parameterized by a lower and upper bound (both inclusive). /// Wikipedia - Discrete uniform distribution. /// /// The distribution will use the by default. /// Users can set the random number generator by using the property. /// The statistics classes will check all the incoming parameters whether they are in the allowed /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters /// to false, all parameter checks can be turned off. public class DiscreteUniform : IDiscreteDistribution { System.Random _random; int _lower; int _upper; /// /// Initializes a new instance of the DiscreteUniform class. /// /// Lower bound. /// Upper bound; must be at least as large as . public DiscreteUniform(int lower, int upper) { _random = new System.Random(); SetParameters(lower, upper); } /// /// Initializes a new instance of the DiscreteUniform class. /// /// Lower bound. /// Upper bound; must be at least as large as . /// The random number generator which is used to draw random samples. public DiscreteUniform(int lower, int upper, System.Random randomSource) { _random = randomSource ?? new System.Random(); SetParameters(lower, upper); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return "DiscreteUniform(Lower = " + _lower + ", Upper = " + _upper + ")"; } /// /// Checks whether the parameters of the distribution are valid. /// /// Lower bound. /// Upper bound; must be at least as large as . /// true when the parameters are valid, false otherwise. static bool IsValidParameterSet(int lower, int upper) { return lower <= upper; } /// /// Sets the parameters of the distribution after checking their validity. /// /// Lower bound. /// Upper bound; must be at least as large as . /// When the parameters are out of range. void SetParameters(int lower, int upper) { if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } _lower = lower; _upper = upper; } /// /// Gets or sets the lower bound of the probability distribution. /// public int LowerBound { get { return _lower; } set { SetParameters(value, _upper); } } /// /// Gets or sets the upper bound of the probability distribution. /// public int UpperBound { get { return _upper; } set { SetParameters(_lower, value); } } /// /// Gets or sets the random number generator which is used to draw random samples. /// public System.Random RandomSource { get { return _random; } set { _random = value ?? new System.Random(); } } /// /// Gets the mean of the distribution. /// public double Mean { get { return (_lower + _upper)/2.0; } } /// /// Gets the standard deviation of the distribution. /// public double StdDev { get { return Math.Sqrt((((_upper - _lower + 1.0)*(_upper - _lower + 1.0)) - 1.0)/12.0); } } /// /// Gets the variance of the distribution. /// public double Variance { get { return (((_upper - _lower + 1.0)*(_upper - _lower + 1.0)) - 1.0)/12.0; } } /// /// Gets the entropy of the distribution. /// public double Entropy { get { return Math.Log(_upper - _lower + 1.0); } } /// /// Gets the skewness of the distribution. /// public double Skewness { get { return 0.0; } } /// /// Gets the smallest element in the domain of the distributions which can be represented by an integer. /// public int Minimum { get { return _lower; } } /// /// Gets the largest element in the domain of the distributions which can be represented by an integer. /// public int Maximum { get { return _upper; } } /// /// Gets the mode of the distribution; since every element in the domain has the same probability this method returns the middle one. /// public int Mode { get { return (int) Math.Floor((_lower + _upper)/2.0); } } /// /// Gets the median of the distribution. /// public int Median { get { return (int) Math.Floor((_lower + _upper)/2.0); } } /// /// Computes the probability mass (PMF) at k, i.e. P(X = k). /// /// The location in the domain where we want to evaluate the probability mass function. /// the probability mass at location . public double Probability(int k) { if (k >= _lower && k <= _upper) { return 1.0/(_upper - _lower + 1); } return 0.0; } /// /// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)). /// /// The location in the domain where we want to evaluate the log probability mass function. /// the log probability mass at location . public double ProbabilityLn(int k) { if (k >= _lower && k <= _upper) { return -Math.Log(_upper - _lower + 1); } return Double.NegativeInfinity; } /// /// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x). /// /// The location at which to compute the cumulative distribution function. /// the cumulative distribution at location . public double CumulativeDistribution(double x) { if (x < _lower) { return 0.0; } if (x >= _upper) { return 1.0; } return Math.Min(1.0, (Math.Floor(x) - _lower + 1)/(_upper - _lower + 1)); } /// /// Generates one sample from the discrete uniform distribution. This method does not do any parameter checking. /// /// The random source to use. /// The lower bound of the uniform random variable. /// The upper bound of the uniform random variable. /// A random sample from the discrete uniform distribution. static int SampleUnchecked(System.Random rnd, int lower, int upper) { return (rnd.Next()%(upper - lower + 1)) + lower; } /// /// Draws a random sample from the distribution. /// /// a sample from the distribution. public int Sample() { return SampleUnchecked(_random, _lower, _upper); } /// /// Samples an array of uniformly distributed random variables. /// /// a sequence of samples from the distribution. public IEnumerable Samples() { while (true) { yield return SampleUnchecked(_random, _lower, _upper); } } /// /// Samples a uniformly distributed random variable. /// /// The random number generator to use. /// The lower bound of the uniform random variable. /// The upper bound of the uniform random variable. /// A sample from the discrete uniform distribution. public static int Sample(System.Random rnd, int lower, int upper) { if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } return SampleUnchecked(rnd, lower, upper); } /// /// Samples a sequence of uniformly distributed random variables. /// /// The random number generator to use. /// The lower bound of the uniform random variable. /// The upper bound of the uniform random variable. /// a sequence of samples from the discrete uniform distribution. public static IEnumerable Samples(System.Random rnd, int lower, int upper) { if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } while (true) { yield return SampleUnchecked(rnd, lower, upper); } } } }