//
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 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.Distributions
{
using System;
using System.Collections.Generic;
using Properties;
///
/// The Bernoulli distribution is a distribution over bits. The parameter
/// p specifies the probability that a 1 is generated.
///
/// 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 Bernoulli : IDiscreteDistribution
{
///
/// The probability of generating a one.
///
private double _p;
///
/// The distribution's random number generator.
///
private Random _random;
///
/// Construct a new Bernoulli distribution.
///
/// The probability of generating one.
/// If the Bernoulli parameter is not in the range [0,1].
public Bernoulli(double p)
{
SetParameters(p);
RandomSource = new System.Random();
}
///
/// A string representation of the distribution.
///
public override string ToString()
{
return "Bernoulli(P = " + _p + ")";
}
///
/// Checks whether the parameters of the distribution are valid.
///
/// The probability of generating a one.
/// True when the parameters are valid, false otherwise.
private static bool IsValidParameterSet(double p)
{
if (p >= 0.0 && p <= 1.0)
{
return true;
}
return false;
}
///
/// Sets the parameters of the distribution after checking their validity.
///
/// The probability of generating a one.
/// When the parameters don't pass the function.
private void SetParameters(double p)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
}
///
/// Gets or sets the probability of generating a one.
///
public double P
{
get
{
return _p;
}
set
{
SetParameters(value);
}
}
#region IDistribution Members
///
/// Gets or sets the random number generator which is used to draw random samples.
///
public Random RandomSource
{
get
{
return _random;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
///
/// Gets the mean of the distribution.
///
public double Mean
{
get { return _p; }
}
///
/// Gets the standard deviation of the distribution.
///
public double StdDev
{
get { return Math.Sqrt(_p * (1.0 - _p)); }
}
///
/// Gets the variance of the distribution.
///
public double Variance
{
get { return _p * (1.0 - _p); }
}
///
/// Gets the entropy of the distribution.
///
public double Entropy
{
get { return -_p * Math.Log(_p) - (1.0 - _p) * Math.Log(1.0 - _p); }
}
///
/// Gets the skewness of the distribution.
///
public double Skewness
{
get { return (1.0 - 2.0 * _p) / Math.Sqrt(_p * (1.0 - _p)); }
}
///
/// Gets the smallest element in the domain of the distributions which can be represented by an integer.
///
public int Minimum { get { return 0; } }
///
/// Gets the largest element in the domain of the distributions which can be represented by an integer.
///
public int Maximum { get { return 1; } }
///
/// Computes the cumulative distribution function of the Bernoulli distribution.
///
/// The location at which to compute the cumulative density.
/// the cumulative density at .
public double CumulativeDistribution(double x)
{
if (x < 0)
{
return 0.0;
}
if (x == 0)
{
return 1.0 - _p;
}
return 1.0;
}
#endregion
#region IDiscreteDistribution Members
///
/// The mode of the distribution.
///
public int Mode
{
get { return _p > 0.5 ? 1 : 0; }
}
///
/// The median of the distribution.
///
public int Median
{
get { throw new Exception("The median of the Bernoulli distribution is undefined."); }
}
///
/// Computes the probability of a specific value.
///
public double Probability(int val)
{
if (val == 0)
{
return 1.0 - _p;
}
if (val == 1)
{
return _p;
}
return 0.0;
}
///
/// Computes the probability of a specific value.
///
public double ProbabilityLn(int val)
{
if (val == 0)
{
return Math.Log(1.0 - _p);
}
if (val == 1)
{
return Math.Log(_p);
}
return Double.NegativeInfinity;
}
///
/// Samples a Bernoulli distributed random variable.
///
/// A sample from the Bernoulli distribution.
public int Sample()
{
return DoSample(RandomSource, _p);
}
///
/// Samples an array of Bernoulli distributed random variables.
///
/// a sequence of samples from the distribution.
public IEnumerable Samples()
{
while (true)
{
yield return DoSample(RandomSource, _p);
}
}
#endregion
///
/// Samples a Bernoulli distributed random variable.
///
/// The random number generator to use.
/// The probability of generating a 1.
/// A sample from the Bernoulli distribution.
public static int Sample(System.Random rnd, double p)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return DoSample(rnd, p);
}
///
/// Samples an array of Bernoulli distributed random variables.
///
/// The random number generator to use.
/// The probability of generating a 1.
/// a sequence of samples from the distribution.
public static IEnumerable Samples(System.Random rnd, double p)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return DoSample(rnd, p);
}
}
///
/// Generates one sample from the Bernoulli distribution.
///
/// The random source to use.
/// The probability of generating a one.
/// A random sample from the Bernoulli distribution.
private static int DoSample(System.Random rnd, double p)
{
if (rnd.NextDouble() < p)
{
return 1;
}
return 0;
}
}
}