gleam_stats/distributions/triangular
Functions related to continuous triangular random variables.
- Available Functions
Functions
pub fn triangular_cdf(x: Float, a: Float, b: Float, c: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$(-\infty, \infty)$$, the cumulative distribution function (cdf) of a continuous triangular random variable that takes values in the interval $$[a, b]$$ and has mode (a peak at value) $$c$$, $$a \leq c \leq b$$.
The cdf is defined as:
\[ F(x; a, b, c) = \begin{cases} 0 &\text{if } x \leq 0, \\ \frac{(x - a)^{2}}{(b - a) \cdot (c - a)} &\text{if } a < x \leq c, \\ 1 - \frac{(b - x)^{2}}{(b - a) \cdot (b - c)} &\text{if } c < x < b, \\ 1 &\text{if } b \leq x. \end{cases} \]
Example:
import gleam_stats/distributions/triangular
import gleeunit/should
pub fn example() {
// Min value
let a: Float = 0.
// Max value
let b: Float = 1.
// The mode of the distribution
let c: Float = 0.5
// For illustrational purposes, evaluate the cdf at the
// point -100.0
triangular.triangular_cdf(-100.0, a, b ,c)
|> should.equal(Ok(0.0))
}
pub fn triangular_mean(a: Float, b: Float, c: Float) -> Result(
Float,
String,
)
Analytically compute the mean of a continuous triangular random variable
that takes values in the interval $$[a, b]$$ and has mode (a peak at value)
$$c$$, $$a \leq c \leq b$$.
The mean returned is: $$\frac{a + b + c}{3}$$.
pub fn triangular_pdf(x: Float, a: Float, b: Float, c: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$x \in (-\infty, \infty)$$ the probability density function (pdf) of a continuous triangular random variable that takes values in the interval $$[a, b]$$ and has mode (a peak at value) $$c$$, $$a \leq c \leq b$$.
The pdf is defined as:
\[ f(x; a, b, c) = \begin{cases} 0 &\text{if } x < 0, \\ \frac{2 \cdot (x - a)}{(b - a) \cdot (c - a)} &\text{if } a \leq x < c, \\ \frac{2}{b - a} &\text{if } x = c, \\ \frac{2 \cdot (b - x)}{(b - a) \cdot (b - c)} &\text{if } c < x \leq b, \\ 0 &\text{if } b < x. \end{cases} \]
Example:
import gleam_stats/distributions/triangular
import gleeunit/should
pub fn example() {
// Min value
let a: Float = 0.
// Max value
let b: Float = 1.
// The mode of the distribution
let c: Float = 0.5
// For illustrational purposes, evaluate the pdf at the
// point -100.0
triangular.triangular_pdf(-100.0, a, b ,c)
|> should.equal(Ok(0.0))
}
pub fn triangular_random(stream: Iterator(Int), a: Float, b: Float, c: Float, m: Int) -> Result(
#(List(Float), Iterator(Int)),
String,
)
Generate $$m \in \mathbb{Z}_{>0}$$ random numbers in the interval $$[a, b]$$ from a continuous triangular distribution with mode (a peak at value) $$c$$, $$a \leq c \leq b$$.
The random numbers are generated using the inverse transform method.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generators
import gleam_stats/distributions/triangular
pub fn example() {
let seed: Int = 5
let seq: Int = 1
// Min value
let a: Float = 0.
// Max value
let b: Float = 1.
// The mode of the distribution
let c: Float = 0.5
assert Ok(out) =
generators.seed_pcg32(seed, seq)
|> triangular.triangular_random(a, b, c, 5_000)
let rands: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn triangular_variance(a: Float, b: Float, c: Float) -> Result(
Float,
String,
)
Analytically compute the variance of a continuous triangular random variable
that takes values in the intervalinterval $$[a, b]$$ and has mode (a peak at value)
$$c$$, $$a \leq c \leq b$$.
The variance returned is:
\[ \frac{a^{2} + b^{2} + c^{2} - a \cdot b - a\cdot c - b \cdot c}{18} \]