//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// 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 System.Linq;
using IStation.Numerics.Random;
using IStation.Numerics.Threading;
namespace IStation.Numerics.Distributions
{
///
/// Triangular distribution.
/// For details, see Wikipedia - Triangular 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 whether all the incoming parameters 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 Triangular : IContinuousDistribution
{
System.Random _random;
readonly double _lower;
readonly double _upper;
readonly double _mode;
///
/// Initializes a new instance of the Triangular class with the given lower bound, upper bound and mode.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// If the upper bound is smaller than the mode or if the mode is smaller than the lower bound.
public Triangular(double lower, double upper, double mode)
{
if (!IsValidParameterSet(lower, upper, mode))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
_random = SystemRandomSource.Default;
_lower = lower;
_upper = upper;
_mode = mode;
}
///
/// Initializes a new instance of the Triangular class with the given lower bound, upper bound and mode.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// The random number generator which is used to draw random samples.
/// If the upper bound is smaller than the mode or if the mode is smaller than the lower bound.
public Triangular(double lower, double upper, double mode, System.Random randomSource)
{
if (!IsValidParameterSet(lower, upper, mode))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
_random = randomSource ?? SystemRandomSource.Default;
_lower = lower;
_upper = upper;
_mode = mode;
}
///
/// A string representation of the distribution.
///
/// a string representation of the distribution.
public override string ToString()
{
return $"Triangular(Lower = {_lower}, Upper = {_upper}, Mode = {_mode})";
}
///
/// Tests whether the provided values are valid parameters for this distribution.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
public static bool IsValidParameterSet(double lower, double upper, double mode)
{
return upper >= mode && mode >= lower && !double.IsInfinity(upper) && !double.IsInfinity(lower) && !double.IsInfinity(mode);
}
///
/// Gets the lower bound of the distribution.
///
public double LowerBound => _lower;
///
/// Gets the upper bound of the distribution.
///
public double UpperBound => _upper;
///
/// Gets or sets the random number generator which is used to draw random samples.
///
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
}
///
/// Gets the mean of the distribution.
///
public double Mean => (_lower + _upper + _mode)/3.0;
///
/// Gets the variance of the distribution.
///
public double Variance
{
get
{
var a = _lower;
var b = _upper;
var c = _mode;
return (a*a + b*b + c*c - a*b - a*c - b*c)/18.0;
}
}
///
/// Gets the standard deviation of the distribution.
///
public double StdDev => Math.Sqrt(Variance);
///
/// Gets the entropy of the distribution.
///
///
public double Entropy => 0.5 + Math.Log((_upper - _lower)/2);
///
/// Gets the skewness of the distribution.
///
public double Skewness
{
get
{
var a = _lower;
var b = _upper;
var c = _mode;
var q = Math.Sqrt(2)*(a + b - 2*c)*(2*a - b - c)*(a - 2*b + c);
var d = 5*Math.Pow(a*a + b*b + c*c - a*b - a*c - b*c, 3.0/2);
return q/d;
}
}
///
/// Gets or sets the mode of the distribution.
///
public double Mode => _mode;
///
/// Gets the median of the distribution.
///
///
public double Median
{
get
{
var a = _lower;
var b = _upper;
var c = _mode;
return c >= (a + b)/2
? a + Math.Sqrt((b - a)*(c - a)/2)
: b - Math.Sqrt((b - a)*(b - c)/2);
}
}
///
/// Gets the minimum of the distribution.
///
public double Minimum => _lower;
///
/// Gets the maximum of the distribution.
///
public double Maximum => _upper;
///
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
///
/// The location at which to compute the density.
/// the density at .
///
public double Density(double x)
{
return PDF(_lower, _upper, _mode, x);
}
///
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
///
/// The location at which to compute the log density.
/// the log density at .
///
public double DensityLn(double x)
{
return PDFLn(_lower, _upper, _mode, x);
}
///
/// 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)
{
return CDF(_lower, _upper, _mode, x);
}
///
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability. This is also known as the quantile or percent point function.
///
/// The location at which to compute the inverse cumulative density.
/// the inverse cumulative density at .
///
public double InverseCumulativeDistribution(double p)
{
return InvCDF(_lower, _upper, _mode, p);
}
///
/// Generates a sample from the Triangular distribution.
///
/// a sample from the distribution.
public double Sample()
{
return SampleUnchecked(_random, _lower, _upper, _mode);
}
///
/// Fills an array with samples generated from the distribution.
///
public void Samples(double[] values)
{
SamplesUnchecked(_random, values, _lower, _upper, _mode);
}
///
/// Generates a sequence of samples from the Triangular distribution.
///
/// a sequence of samples from the distribution.
public IEnumerable Samples()
{
return SamplesUnchecked(_random, _lower, _upper, _mode);
}
static double SampleUnchecked(System.Random rnd, double lower, double upper, double mode)
{
var u = rnd.NextDouble();
return u < (mode - lower)/(upper - lower)
? lower + Math.Sqrt(u*(upper - lower)*(mode - lower))
: upper - Math.Sqrt((1 - u)*(upper - lower)*(upper - mode));
}
static IEnumerable SamplesUnchecked(System.Random rnd, double lower, double upper, double mode)
{
double ml = mode - lower, ul = upper - lower, um = upper - mode;
double u = ml/ul, v = ul*ml, w = ul*um;
return rnd.NextDoubleSequence().Select(x => x < u ? lower + Math.Sqrt(x*v) : upper - Math.Sqrt((1 - x)*w));
}
static void SamplesUnchecked(System.Random rnd, double[] values, double lower, double upper, double mode)
{
double ml = mode - lower, ul = upper - lower, um = upper - mode;
double u = ml/ul, v = ul*ml, w = ul*um;
rnd.NextDoubles(values);
CommonParallel.For(0, values.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
values[i] = values[i] < u
? lower + Math.Sqrt(values[i]*v)
: upper - Math.Sqrt((1 - values[i])*w);
}
});
}
///
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// The location at which to compute the density.
/// the density at .
///
public static double PDF(double lower, double upper, double mode, double x)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
var a = lower;
var b = upper;
var c = mode;
if (a <= x && x <= c)
{
return 2*(x - a)/((b - a)*(c - a));
}
if (c < x & x <= b)
{
return 2*(b - x)/((b - a)*(b - c));
}
return 0;
}
///
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// The location at which to compute the density.
/// the log density at .
///
public static double PDFLn(double lower, double upper, double mode, double x)
{
return Math.Log(PDF(lower, upper, mode, x));
}
///
/// 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.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// the cumulative distribution at location .
///
public static double CDF(double lower, double upper, double mode, double x)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
var a = lower;
var b = upper;
var c = mode;
if (x < a)
{
return 0;
}
if (a <= x && x <= c)
{
return (x - a)*(x - a)/((b - a)*(c - a));
}
if (c < x & x <= b)
{
return 1 - (b - x)*(b - x)/((b - a)*(b - c));
}
return 1;
}
///
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability. This is also known as the quantile or percent point function.
///
/// The location at which to compute the inverse cumulative density.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// the inverse cumulative density at .
///
public static double InvCDF(double lower, double upper, double mode, double p)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
var a = lower;
var b = upper;
var c = mode;
if (p <= 0)
{
return lower;
}
// Taken from http://www.ntrand.com/triangular-distribution/
if (p < (c - a)/(b - a))
{
return a + Math.Sqrt(p*(c - a)*(b - a));
}
if (p < 1)
{
return b - Math.Sqrt((1 - p)*(b - c)*(b - a));
}
return upper;
}
///
/// Generates a sample from the Triangular distribution.
///
/// The random number generator to use.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sample from the distribution.
public static double Sample(System.Random rnd, double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SampleUnchecked(rnd, lower, upper, mode);
}
///
/// Generates a sequence of samples from the Triangular distribution.
///
/// The random number generator to use.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sequence of samples from the distribution.
public static IEnumerable Samples(System.Random rnd, double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SamplesUnchecked(rnd, lower, upper, mode);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The random number generator to use.
/// The array to fill with the samples.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sequence of samples from the distribution.
public static void Samples(System.Random rnd, double[] values, double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
SamplesUnchecked(rnd, values, lower, upper, mode);
}
///
/// Generates a sample from the Triangular distribution.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sample from the distribution.
public static double Sample(double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SampleUnchecked(SystemRandomSource.Default, lower, upper, mode);
}
///
/// Generates a sequence of samples from the Triangular distribution.
///
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sequence of samples from the distribution.
public static IEnumerable Samples(double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SamplesUnchecked(SystemRandomSource.Default, lower, upper, mode);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The array to fill with the samples.
/// Lower bound. Range: lower ≤ mode ≤ upper
/// Upper bound. Range: lower ≤ mode ≤ upper
/// Mode (most frequent value). Range: lower ≤ mode ≤ upper
/// a sequence of samples from the distribution.
public static void Samples(double[] values, double lower, double upper, double mode)
{
if (!(upper >= mode && mode >= lower))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
SamplesUnchecked(SystemRandomSource.Default, values, lower, upper, mode);
}
}
}