//
// 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 TruncatedPareto : IContinuousDistribution
{
System.Random _random;
///
/// Initializes a new instance of the TruncatedPareto class.
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// The random number generator which is used to draw random samples.
/// If or are non-positive or if T ≤ xm.
public TruncatedPareto(double scale, double shape, double truncation, System.Random randomSource = null)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
_random = randomSource ?? SystemRandomSource.Default;
Scale = scale;
Shape = shape;
Truncation = truncation;
}
///
/// A string representation of the distribution.
///
/// a string representation of the distribution.
public override string ToString()
{
return $"Truncated Pareto(Scale = {Scale}, Shape = {Shape}, Truncation = {Truncation})";
}
///
/// Tests whether the provided values are valid parameters for this distribution.
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
public static bool IsValidParameterSet(double scale, double shape, double truncation)
{
var allFinite = scale.IsFinite() && shape.IsFinite() && truncation.IsFinite();
return allFinite && scale > 0.0 && shape > 0.0 && truncation > scale;
}
///
/// 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 scale (xm) of the distribution. Range: xm > 0.
///
public double Scale { get; }
///
/// Gets the shape (α) of the distribution. Range: α > 0.
///
public double Shape { get; }
///
/// Gets the truncation (T) of the distribution. Range: T > 0.
///
public double Truncation { get; }
///
/// 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(int n)
{
double moment;
if (Shape.AlmostEqual(n))
{
moment = ((Shape * Math.Pow(Scale, n)) / (1 - Math.Pow(Scale / Truncation, Shape))) * Math.Log(Truncation / Scale);
}
else
{
moment = ((Shape * Math.Pow(Scale, n)) / (Shape - n)) * ((1 - Math.Pow((Scale / Truncation), (Shape - n))) / (1 - Math.Pow(Scale / Truncation, Shape)));
}
return moment;
}
///
/// Gets the mean of the truncated Pareto distribution.
///
public double Mean => GetMoment(1);
///
/// Gets the variance of the truncated Pareto distribution.
///
public double Variance => GetMoment(2) - Math.Pow(GetMoment(1), 2);
///
/// Gets the standard deviation of the truncated Pareto distribution.
///
public double StdDev => Math.Sqrt(Variance);
///
/// Gets the mode of the truncated Pareto distribution (not supported).
///
public double Mode => throw new NotSupportedException();
///
/// Gets the minimum of the truncated Pareto distribution.
///
public double Minimum => Scale;
///
/// Gets the maximum of the truncated Pareto distribution.
///
public double Maximum => Truncation;
///
/// Gets the entropy of the truncated Pareto distribution (not supported).
///
public double Entropy => throw new NotSupportedException();
///
/// Gets the skewness of the truncated Pareto distribution.
///
public double Skewness
{
get
{
var mean = Mean;
var variance = Variance;
var std = StdDev;
return (GetMoment(3) - 3.0 * mean * variance - mean * mean * mean) / (std * std * std);
}
}
///
/// Gets the median of the truncated Pareto distribution.
///
public double Median => Scale * Math.Pow(1.0 - (1.0 / 2.0) * (1.0 - Math.Pow(Scale / Truncation, Shape)), -(1.0 / Shape));
///
/// Generates a sample from the truncated Pareto distribution.
///
/// a sample from the distribution.
public double Sample()
{
return SampleUnchecked(_random, Scale, Shape, Truncation);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The array to fill with the samples.
public void Samples(double[] values)
{
SamplesUnchecked(_random, values, Scale, Shape, Truncation);
}
///
/// Generates a sequence of samples from the truncated Pareto distribution.
///
/// a sequence of samples from the distribution.
public IEnumerable Samples()
{
return SamplesUnchecked(_random, Scale, Shape, Truncation);
}
///
/// Generates a sample from the truncated Pareto distribution.
///
/// The random number generator to use.
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// a sample from the distribution.
public static double Sample(System.Random rnd, double scale, double shape, double truncation)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SampleUnchecked(rnd, scale, shape, truncation);
}
///
/// Fills an array with samples generated from the distribution.
///
/// The random number generator to use.
/// The array to fill with the samples.
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
public static void Samples(System.Random rnd, double[] values, double scale, double shape, double truncation)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
SamplesUnchecked(rnd, values, scale, shape, truncation);
}
///
/// Generates a sequence of samples from the truncated Pareto distribution.
///
/// The random number generator to use.
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// a sequence of samples from the distribution.
public static IEnumerable Samples(System.Random rnd, double scale, double shape, double truncation)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return SamplesUnchecked(rnd, scale, shape, truncation);
}
internal static double SampleUnchecked(System.Random rnd, double scale, double shape, double truncation)
{
double uniform = rnd.NextDouble();
return InvCDFUncheckedImpl(scale, shape, truncation, uniform);
}
internal static void SamplesUnchecked(System.Random rnd, double[] values, double scale, double shape, double truncation)
{
if (values.Length == 0)
{
return;
}
double[] uniforms = rnd.NextDoubles(values.Length);
for (var j = 0; j < values.Length; ++j)
{
values[j] = InvCDFUncheckedImpl(scale, shape, truncation, uniforms[j]);
}
}
internal static IEnumerable SamplesUnchecked(System.Random rnd, double scale, double shape, double truncation)
{
while (true)
{
yield return SampleUnchecked(rnd, scale, shape, truncation);
}
}
///
/// 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(Scale, Shape, Truncation, 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(Scale, Shape, Truncation, 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(Scale, Shape, Truncation, x);
}
///
/// Computes the inverse cumulative distribution (CDF) of the distribution at p, i.e. solving for P(X ≤ x) = p.
///
/// The location at which to compute the inverse cumulative distribution function.
/// the inverse cumulative distribution at location .
public double InvCDF(double p)
{
return InvCDFUncheckedImpl(Scale, Shape, Truncation, p);
}
///
/// Computes the inverse cumulative distribution (CDF) of the distribution at p, i.e. solving for P(X ≤ x) = p.
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// The location at which to compute the inverse cumulative distribution function.
/// the inverse cumulative distribution at location .
///
public static double ICDF(double scale, double shape, double truncation, double p)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return InvCDFUncheckedImpl(scale, shape, truncation, p);
}
///
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// The location at which to compute the density.
/// the density at .
///
public static double PDF(double scale, double shape, double truncation, double x)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return DensityImpl(scale, shape, truncation, x);
}
///
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// The location at which to compute the log density.
/// the log density at .
///
public static double PDFLn(double scale, double shape, double truncation, double x)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return DensityLnImpl(scale, shape, truncation, x);
}
///
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x).
///
/// The scale (xm) of the distribution. Range: xm > 0.
/// The shape (α) of the distribution. Range: α > 0.
/// The truncation (T) of the distribution. Range: T > xm.
/// The location at which to compute the cumulative distribution function.
/// the cumulative distribution at location .
///
public static double CDF(double scale, double shape, double truncation, double x)
{
if (!IsValidParameterSet(scale, shape, truncation))
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
return CumulativeDistributionImpl(scale, shape, truncation, x);
}
internal static double DensityImpl(double scale, double shape, double truncation, double x)
{
if (x < scale || x > truncation)
return 0;
else
return (shape * Math.Pow(scale, shape) * Math.Pow(x, -shape - 1)) / (1 - Math.Pow(scale / truncation, shape));
}
internal static double DensityLnImpl(double scale, double shape, double truncation, double x)
{
return Math.Log(DensityImpl(scale, shape, truncation, x));
}
internal static double CumulativeDistributionImpl(double scale, double shape, double truncation, double x)
{
if (x <= scale)
return 0;
else if (x >= truncation)
return 1;
else
return (1 - Math.Pow(scale, shape) * Math.Pow(x, -shape)) / (1 - Math.Pow(scale / truncation, shape));
}
internal static double InvCDFUncheckedImpl(double scale, double shape, double truncation, double p)
{
var numerator = p * Math.Pow(truncation, shape) - p * Math.Pow(scale, shape) - Math.Pow(truncation, shape);
var denominator = Math.Pow(truncation, shape) * Math.Pow(scale, shape);
return Math.Pow(-numerator / denominator, -(1 / shape));
}
}
}