gleam_stats/distributions/exponential
Functions related to continuous exponential random variables.
- Available functions
Functions
pub fn exponential_cdf(x: Float, lambda: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$x \in (-\infty, +\infty)$$, the cumulative distribution function (cdf) of a continuous exponential random variable with given rate parameter $$\lambda \in (0, +\infty)$$.
The cdf is defined as:
\[ F(x; \lambda) = \begin{cases} 1 - e^{-\lambda \cdot x} &\text{if } x \geq 0, \\ 0 &\text{if } x < 0. \end{cases} \]
Example:
import gleam_stats/distributions/exponential
import gleeunit/should
pub fn example() {
let lambda: Float = 1.
// For illustrational purposes, evaluate the cdf at the
// point -100.0
exponential.exponential_cdf(-100.0, lambda)
|> should.equal(Ok(0.0))
}
pub fn exponential_mean(lambda: Float) -> Result(Float, String)
Analytically compute the mean of a continuous exponential random variable
with given rate parameter $$\lambda \in (0, +\infty)$$.
The mean returned is: $$\frac{1}{\lambda}$$.
pub fn exponential_pdf(x: Float, lambda: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$x \in (-\infty, +\infty)$$, the probability density function (pdf) of a continuous exponential random variable with given rate parameter $$\lambda \in (0, +\infty)$$.
The pdf is defined as:
\[ f(x; \lambda) = \begin{cases} \lambda \cdot e^{-\lambda \cdot x} &\text{if } x \geq 0, \\ 0 &\text{if } x < 0. \end{cases} \]
Example:
import gleam_stats/distributions/exponential
import gleeunit/should
pub fn example() {
let lambda: Float = 1.
// For illustrational purposes, evaluate the pdf at the
// point -100.0
exponential.exponential_pdf(-100.0, lambda)
|> should.equal(Ok(0.0))
}
pub fn exponential_random(stream: Iterator(Int), lambda: Float, m: Int) -> Result(
#(List(Float), Iterator(Int)),
String,
)
Generate $$m \in \mathbb{Z}_{>0}$$ random numbers from a continuous exponential distribution with given rate parameter $$\lambda \in (0, +\infty)$$.
The random numbers are generated using the inverse transform method.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generators
import gleam_stats/distributions/exponential
pub fn example() {
let seed: Int = 5
let seq: Int = 1
let lambda: Float = 1.
assert Ok(out) =
generators.seed_pcg32(seed, seq)
|> exponential.exponential_random(lambda, 5_000)
let rands: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn exponential_variance(lambda: Float) -> Result(
Float,
String,
)
Analytically compute the variance of a continuous exponential random variable
with given rate parameter $$\lambda \in (0, +\infty)$$.
The variance returned is: $$\frac{1}{\lambda^{2}}$$.