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, 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’.
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’.
pub fn triangular_pdf(x: Float, a: Float, b: Float, c: Float) -> Result(
Float,
String,
)
Evaluate 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’.
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’ random numbers in the interval ‘[a, b]’ from a continuous triangular distribution with mode ‘c’.
The random numbers are generated using the inverse transform method.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
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)
|> 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 interval ‘[a, b]’ and has mode (a peak at value) ‘c’.