TriangularDistribution

Represents the triangular distribution, a continuous probability distribution defined by a lower limit a, an upper limit b, and a mode (peak) c.

The triangular distribution has a piecewise linear density that rises from zero at a to a peak at c, then falls back to zero at b. It is commonly used in simulation and risk analysis when only the minimum, maximum, and most likely value of a quantity are known, making it a practical alternative to the beta distribution when limited information is available.

The PDF consists of two linear segments joined at the mode c, the CDF is piecewise quadratic, and the quantile function uses the inverse of those quadratic segments. Sampling is performed via the inverse CDF method.

Example:

// Model project duration: min 2 weeks, most likely 5 weeks, max 10 weeks
val dist = TriangularDistribution(a = 2.0, b = 10.0, c = 5.0)
dist.mean // 5.6667 (shifted toward the longer tail)
dist.variance // 2.7222
dist.pdf(5.0) // 0.25 (peak density at the mode)
dist.cdf(5.0) // 0.375
dist.quantile(0.5) // 5.2915 (the median)
dist.sample(Random(42)) // a single random draw from Triangular(2, 10, 5)

// Symmetric triangular distribution
val sym = TriangularDistribution(a = 0.0, b = 1.0, c = 0.5)
sym.skewness // 0.0

Constructors

Link copied to clipboard
constructor(a: Double, b: Double, c: Double)

Properties

Link copied to clipboard
val a: Double

the lower limit (minimum) of the distribution's support. Must be strictly less than b.

Link copied to clipboard
val b: Double

the upper limit (maximum) of the distribution's support. Must be strictly greater than a.

Link copied to clipboard
val c: Double

the mode (peak) of the distribution. Must satisfy a <= c <= b.

Link copied to clipboard
open override val entropy: Double

The differential entropy of this distribution in nats, equal to 0.5 + ln((b - a) / 2).

Link copied to clipboard
open override val kurtosis: Double

The excess kurtosis of this distribution, always -0.6 for any triangular distribution regardless of parameters.

Link copied to clipboard
open override val mean: Double

The mean of this distribution, equal to (a + b + c) / 3.

Link copied to clipboard
open override val skewness: Double

The skewness of this distribution, computed from a, b, and c. Zero when the distribution is symmetric (c = (a + b) / 2).

Link copied to clipboard
open override val variance: Double

The variance of this distribution, equal to (a^2 + b^2 + c^2 - ab - ac - bc) / 18.

Functions

Link copied to clipboard
open override fun cdf(x: Double): Double

Computes the cumulative distribution function at x using the piecewise quadratic CDF.

Link copied to clipboard
open override fun logPdf(x: Double): Double

Computes the natural logarithm of the probability density at x.

Link copied to clipboard
open override fun pdf(x: Double): Double

Computes the probability density at x using the piecewise linear density function.

Link copied to clipboard
open override fun quantile(p: Double): Double

Computes the quantile (inverse CDF) for the given probability p.

Link copied to clipboard
open override fun sample(random: Random): Double

Draws a single random value from this triangular distribution using inverse CDF sampling.

Link copied to clipboard
open override fun sf(x: Double): Double

Computes the survival function (1 - CDF) at x.