//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2019 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 IStation.Numerics.Random;
using System;
using System.Collections.Generic;
namespace IStation.Numerics.Distributions
{
public class Burr : IContinuousDistribution
{
System.Random _random;
///
/// Gets the scale (a) of the distribution. Range: a > 0.
///
public double a { get; }
///
/// Gets the first shape parameter (c) of the distribution. Range: c > 0.
///
public double c { get; }
///
/// Gets the second shape parameter (k) of the distribution. Range: k > 0.
///
public double k { get; }
///
/// Initializes a new instance of the Burr Type XII class.
///
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// The random number generator which is used to draw random samples. Optional, can be null.
public Burr(double a, double c, double k, System.Random randomSource = null)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
_random = randomSource ?? SystemRandomSource.Default;
this.a = a;
this.c = c;
this.k = k;
}
///
/// A string representation of the distribution.
///
/// a string representation of the distribution.
public override string ToString()
{
return $"Burr(a = {a}, c = {c}, k = {k})";
}
///
/// Tests whether the provided values are valid parameters for this distribution.
///
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
public static bool IsValidParameterSet(double a, double c, double k)
{
var allFinite = a.IsFinite() && c.IsFinite() && k.IsFinite();
return allFinite && a > 0.0 && c > 0.0 && k > 0.0;
}
///
/// Gets 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 Burr distribution.
///
public double Mean => (1 / SpecialFunctions.Gamma(k)) * a * SpecialFunctions.Gamma(1 + 1 / c) * SpecialFunctions.Gamma(k - 1 / c);
///
/// Gets the variance of the Burr distribution.
///
public double Variance =>
(1 / SpecialFunctions.Gamma(k)) * Math.Pow(a, 2) * SpecialFunctions.Gamma(1 + 2 / c) * SpecialFunctions.Gamma(k - 2 / c)
- Math.Pow((1 / SpecialFunctions.Gamma(k)) * a * SpecialFunctions.Gamma(1 + 1 / c) * SpecialFunctions.Gamma(k - 1 / c), 2);
///
/// Gets the standard deviation of the Burr distribution.
///
public double StdDev => Math.Sqrt(Variance);
///
/// Gets the mode of the Burr distribution.
///
public double Mode => a * Math.Pow((c - 1) / (c * k + 1), 1 / c);
///
/// Gets the minimum of the Burr distribution.
///
public double Minimum => 0.0;
///
/// Gets the maximum of the Burr distribution.
///
public double Maximum => double.PositiveInfinity;
///
/// Gets the entropy of the Burr distribution (currently not supported).
///
public double Entropy => throw new NotSupportedException();
///
/// Gets the skewness of the Burr distribution.
///
public double Skewness
{
get
{
var mean = Mean;
var variance = Variance;
var std = StdDev;
return (GetMoment(3) - 3 * mean * variance - mean * mean * mean) / (std * std * std);
}
}
///
/// Gets the median of the Burr distribution.
///
public double Median => a * Math.Pow(Math.Pow(2, 1 / k) - 1, 1 / c);
///
/// Generates a sample from the Burr distribution.
///
/// a sample from the distribution.
public double Sample()
{
return SampleUnchecked(_random, a, c, k);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The array to fill with the samples.
public void Samples(double[] values)
{
SamplesUnchecked(_random, values, a, c, k);
}
///
/// Generates a sequence of samples from the Burr distribution.
///
/// a sequence of samples from the distribution.
public IEnumerable Samples()
{
return SamplesUnchecked(_random, a, c, k);
}
///
/// Generates a sample from the Burr distribution.
///
/// The random number generator to use.
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// a sample from the distribution.
public static double Sample(System.Random rnd, double a, double c, double k)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SampleUnchecked(rnd, a, c, k);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The random number generator to use.
/// The array to fill with the samples.
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
public static void Samples(System.Random rnd, double[] values, double a, double c, double k)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
SamplesUnchecked(rnd, values, a, c, k);
}
///
/// Generates a sequence of samples from the Burr distribution.
///
/// The random number generator to use.
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// a sequence of samples from the distribution.
public static IEnumerable Samples(System.Random rnd, double a, double c, double k)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SamplesUnchecked(rnd, a, c, k);
}
internal static double SampleUnchecked(System.Random rnd, double a, double c, double k)
{
var k_inv = 1 / k;
var c_inv = 1 / c;
double u = rnd.NextDouble();
return a * Math.Pow(Math.Pow(1 - u, -k_inv) - 1, c_inv);
}
internal static void SamplesUnchecked(System.Random rnd, double[] values, double a, double c, double k)
{
if (values.Length == 0)
{
return;
}
var k_inv = 1 / k;
var c_inv = 1 / c;
double[] u = rnd.NextDoubles(values.Length);
for (var j = 0; j < values.Length; ++j)
{
values[j] = a * Math.Pow(Math.Pow(1 - u[j], -k_inv) - 1, c_inv);
}
}
internal static IEnumerable SamplesUnchecked(System.Random rnd, double a, double c, double k)
{
while (true)
{
yield return SampleUnchecked(rnd, a, c, k);
}
}
///
/// Gets the n-th raw moment of the distribution.
///
/// The order (n) of the moment. Range: n ≥ 1.
/// the n-th moment of the distribution.
public double GetMoment(double n)
{
if (n > k * c)
{
throw new ArgumentException("The chosen parameter set is invalid (probably some value is out of range).");
}
var lambda_n = (n / c) * SpecialFunctions.Gamma(n / c) * SpecialFunctions.Gamma(k - n / c);
return Math.Pow(a, n) * lambda_n / SpecialFunctions.Gamma(k);
}
///
/// 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 DensityImpl(a, c, k, 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 DensityLnImpl(a, c, k, 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 CumulativeDistributionImpl(a, c, k, x);
}
///
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
///
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// The location at which to compute the density.
/// the density at .
///
public static double PDF(double a, double c, double k, double x)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return DensityImpl(a, c, k, x);
}
///
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
///
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// The location at which to compute the log density.
/// the log density at .
///
public static double PDFLn(double a, double c, double k, double x)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return DensityLnImpl(a, c, k, x);
}
///
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x).
///
/// The scale parameter a of the Burr distribution. Range: a > 0.
/// The first shape parameter c of the Burr distribution. Range: c > 0.
/// The second shape parameter k of the Burr distribution. Range: k > 0.
/// The location at which to compute the cumulative distribution function.
/// the cumulative distribution at location .
///
public static double CDF(double a, double c, double k, double x)
{
if (!IsValidParameterSet(a, c, k))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return CumulativeDistributionImpl(a, c, k, x);
}
internal static double DensityImpl(double a, double c, double k, double x)
{
var numerator = (k * c / a) * Math.Pow(x / a, c - 1);
var denominator = Math.Pow(1 + Math.Pow(x / a, c), k + 1);
return numerator / denominator;
}
internal static double DensityLnImpl(double a, double c, double k, double x)
{
return Math.Log(DensityImpl(a, c, k, x));
}
internal static double CumulativeDistributionImpl(double a, double c, double k, double x)
{
var denominator = Math.Pow(1 + Math.Pow(x / a, c), k);
return 1 - 1 / denominator;
}
}
}