//
// 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;
///
/// Implements the Beta distribution. For details about this distribution, see
/// Wikipedia - Beta distribution.
///
/// The distribution will use the by default.
/// Users can get/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 Beta : IContinuousDistribution
{
///
/// Beta shape parameter a.
///
private double _shapeA;
///
/// Beta shape parameter b.
///
private double _shapeB;
///
/// The distribution's random number generator.
///
private Random _random;
///
/// Initializes a new instance of the Beta class.
///
/// The a shape parameter of the Beta distribution.
/// The b shape parameter of the Beta distribution.
/// If any of the Beta parameters are negative.
public Beta(double a, double b)
{
SetParameters(a, b);
RandomSource = new Random();
}
///
/// A string representation of the distribution.
///
/// A string representation of the Beta distribution.
public override string ToString()
{
return "Beta(A = " + _shapeA + ", B = " + _shapeB + ")";
}
///
/// Checks whether the parameters of the distribution are valid.
///
/// The a shape parameter of the Beta distribution.
/// The b shape parameter of the Beta distribution.
/// True when the parameters are valid, false otherwise.
private static bool IsValidParameterSet(double a, double b)
{
if (a < 0.0 || b < 0.0 || Double.IsNaN(a) || Double.IsNaN(b))
{
return false;
}
return true;
}
///
/// Sets the parameters of the distribution after checking their validity.
///
/// The a shape parameter of the Beta distribution.
/// The b shape parameter of the Beta distribution.
/// When the parameters don't pass the function.
private void SetParameters(double a, double b)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_shapeA = a;
_shapeB = b;
}
///
/// Gets or sets the A shape parameter of the Beta distribution.
///
public double A
{
get { return _shapeA; }
set { SetParameters(value, _shapeB); }
}
///
/// Gets or sets the B shape parameter of the Beta distribution.
///
public double B
{
get { return _shapeB; }
set { SetParameters(_shapeA, value); }
}
#region IDistribution implementation
///
/// 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 Beta distribution.
///
public double Mean
{
get
{
if(_shapeA == 0.0 && _shapeB == 0.0)
{
return 0.5;
}
else if(_shapeA == 0.0)
{
return 0.0;
}
else if(_shapeB == 0.0)
{
return 1.0;
}
else if(Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
{
return 0.5;
}
else if (Double.IsPositiveInfinity(_shapeA))
{
return 1.0;
}
else if (Double.IsPositiveInfinity(_shapeB))
{
return 0.0;
}
else
{
return _shapeA / (_shapeA + _shapeB);
}
}
}
///
/// Gets the variance of the Beta distribution.
///
public double Variance
{
get
{
return (_shapeA * _shapeB) / ((_shapeA + _shapeB) * (_shapeA + _shapeB) * (_shapeA + _shapeB + 1.0));
}
}
///
/// Gets the standard deviation of the Beta distribution.
///
public double StdDev
{
get { return Math.Sqrt((_shapeA * _shapeB) / ((_shapeA + _shapeB) * (_shapeA + _shapeB) * (_shapeA + _shapeB + 1.0))); }
}
///
/// Gets the entropy of the Beta distribution.
///
public double Entropy
{
get
{
return SpecialFunctions.BetaLn(_shapeA, _shapeB)
- ((_shapeA - 1.0) * SpecialFunctions.DiGamma(_shapeA))
- ((_shapeB - 1.0) * SpecialFunctions.DiGamma(_shapeB))
+ ((_shapeA + _shapeB - 2.0) * SpecialFunctions.DiGamma(_shapeA + _shapeB));
}
}
///
/// Gets the skewness of the Beta distribution.
///
public double Skewness
{
get
{
if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
{
return 0.0;
}
else if (Double.IsPositiveInfinity(_shapeA))
{
return -2.0;
}
else if (Double.IsPositiveInfinity(_shapeB))
{
return 2.0;
}
else if (_shapeA == 0.0 && _shapeB == 0.0)
{
return 0.0;
}
else if (_shapeA == 0.0)
{
return 2.0;
}
else if (_shapeB == 0.0)
{
return -2.0;
}
else
{
return 2.0 * (_shapeB - _shapeA) * Math.Sqrt(_shapeA + _shapeB + 1.0)
/ ((_shapeA + _shapeB + 2.0) * Math.Sqrt(_shapeA * _shapeB));
}
}
}
#endregion
#region IContinuousDistribution implementation
///
/// Gets the mode of the Beta distribution; when there are multiple answers, this routine will return 0.5.
///
public double Mode
{
get
{
if (_shapeA == 0.0 && _shapeB == 0.0)
{
return 0.5;
}
else if (_shapeA == 0.0)
{
return 0.0;
}
else if (_shapeB == 0.0)
{
return 1.0;
}
else if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
{
return 0.5;
}
else if (Double.IsPositiveInfinity(_shapeA))
{
return 1.0;
}
else if (Double.IsPositiveInfinity(_shapeB))
{
return 0.0;
}
else if(_shapeA == 1.0 && _shapeB == 1.0)
{
return 0.5;
}
else
{
return (_shapeA - 1) / (_shapeA + _shapeB - 2);
}
}
}
///
/// Gets the median of the Beta distribution.
///
public double Median
{
get { throw new NotSupportedException(); }
}
///
/// Gets the minimum of the Beta distribution.
///
public double Minimum
{
get { return 0.0; }
}
///
/// Gets the maximum of the Beta distribution.
///
public double Maximum
{
get { return 1.0; }
}
///
/// Computes the density of the Beta distribution.
///
/// The location at which to compute the density.
/// the density at .
public double Density(double x)
{
if (x < 0.0 || x > 1.0)
{
return 0.0;
}
if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
{
if (x == 0.5)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (Double.IsPositiveInfinity(_shapeA))
{
if (x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (Double.IsPositiveInfinity(_shapeB))
{
if (x == 0.0)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (_shapeA == 0.0 && _shapeB == 0.0)
{
if (x == 0.0 || x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (_shapeA == 0.0)
{
if (x == 0.0)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (_shapeB == 0.0)
{
if (x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return 0.0;
}
}
else if (_shapeA == 1.0 && _shapeB == 1.0)
{
return 1.0;
}
else
{
double b = SpecialFunctions.Gamma(_shapeA + _shapeB) / (SpecialFunctions.Gamma(_shapeA) * SpecialFunctions.Gamma(_shapeB));
return b * Math.Pow(x, _shapeA - 1.0) * Math.Pow(1.0 - x, _shapeB - 1.0);
}
}
///
/// Computes the log density of the Beta distribution.
///
/// The location at which to compute the log density.
/// the log density at .
public double DensityLn(double x)
{
if (x < 0.0 || x > 1.0)
{
return Double.NegativeInfinity;
}
if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
{
if (x == 0.5)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (Double.IsPositiveInfinity(_shapeA))
{
if (x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (Double.IsPositiveInfinity(_shapeB))
{
if (x == 0.0)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (_shapeA == 0.0 && _shapeB == 0.0)
{
if (x == 0.0 || x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (_shapeA == 0.0)
{
if (x == 0.0)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (_shapeB == 0.0)
{
if (x == 1.0)
{
return Double.PositiveInfinity;
}
else
{
return Double.NegativeInfinity;
}
}
else if (_shapeA == 1.0 && _shapeB == 1.0)
{
return 0.0;
}
else
{
double a = SpecialFunctions.GammaLn(_shapeA + _shapeB) - SpecialFunctions.GammaLn(_shapeA) - SpecialFunctions.GammaLn(_shapeB);
double b = x == 0.0 ? (_shapeA == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeA - 1.0) * Math.Log(x);
double c = x == 1.0 ? (_shapeB == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeB - 1.0) * Math.Log(1.0 - x);
return a + b + c;
}
}
///
/// Computes the cumulative distribution function of the Beta distribution.
///
/// The location at which to compute the cumulative density.
/// the cumulative density at .
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_shapeA, _shapeB, x);
}
///
/// Generates a sample from the Beta distribution.
///
/// a sample from the distribution.
public double Sample()
{
return SampleBeta(RandomSource, _shapeA, _shapeB);
}
///
/// Generates a sequence of samples from the Beta distribution.
///
/// a sequence of samples from the distribution.
public IEnumerable Samples()
{
while (true)
{
yield return SampleBeta(RandomSource, _shapeA, _shapeB);
}
}
#endregion
///
/// Generates a sample from the normal distribution using the Box-Muller algorithm.
///
/// The random number generator to use.
/// The a shape parameter of the Beta distribution.
/// The b shape parameter of the Beta distribution.
/// a sample from the distribution.
public static double Sample(Random rng, double a, double b)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleBeta(rng, a, b);
}
///
/// Generates a sequence of samples from the normal distribution using the Box-Muller algorithm.
///
/// The random number generator to use.
/// The a shape parameter of the Beta distribution.
/// The b shape parameter of the Beta distribution.
/// a sequence of samples from the distribution.
public static IEnumerable Samples(Random rng, double a, double b)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleBeta(rng, a, b);
}
}
///
/// Samples Beta distributed random variables by sampling two Gamma variables and normalizing.
///
/// The random number generator to use.
/// The A shape parameter.
/// The B shape parameter.
/// a random number from the Beta distribution.
internal static double SampleBeta(Random rnd, double a, double b)
{
double x = Gamma.SampleGamma(rnd, a, 1.0);
double y = Gamma.SampleGamma(rnd, b, 1.0);
return x / (x + y);
}
}
}