gleam_stats/distributions/binomial
Functions related to discrete binomial random variables.
- Available functions
Functions
pub fn binomial_cdf(x: Int, n: Int, p: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$x \in \mathbb{Z}$$, the cumulative distribution function (cdf) of a discrete binomial random variable with parameters $$n \in \mathbb{Z}_{\geq 0}$$ (number of trials) and $$p \in [0, 1]$$ (the success probability in each trial).
The cdf is defined as:
\[ F(x; n, p) = \begin{cases} \sum_{i=0}^{x} \binom{n}{i} \cdot p^{i} \cdot (1-p)^{n-i} &\text{if } x \geq 0,\\ 0 &\text{if } x < 0. \end{cases} \]
Example:
import gleam_stats/distributions/binomial
import gleeunit/should
pub fn example() {
let n: Float = 40.
let p: Float = 0.5
// For illustrational purposes, evaluate the cdf at the
// point -100.0
binomial.binomial_cdf(-100.0, n, p)
|> should.equal(Ok(0.0))
}
pub fn binomial_mean(n: Int, p: Float) -> Result(Float, String)
Analytically compute the mean of a discrete binomial random variable with parameters $$n \in \mathbb{Z}_{\geq 0}$$ (number of trials) and $$p \in [0, 1]$$ (the success probability in each trial).
The mean returned is: $$n \cdot p$$.
pub fn binomial_pmf(x: Int, n: Int, p: Float) -> Result(
Float,
String,
)
Evaluate, at a certain point $$x \in \mathbb{Z}$$, the probability mass function (pmf) of a discrete binomial random variable with parameters $$n \in \mathbb{Z}_{\geq 0}$$ (number of trials) and $$p \in [0, 1]$$ (the success probability in each trial).
The pmf is defined as:
\[ f(x; n, p) = \begin{cases} \binom{n}{x} \cdot p^{x} \cdot (1 - p)^{n - x} &\text{if } x \geq 0, \\ 0 &\text{if } x < 0. \end{cases} \]
Example:
import gleam_stats/distributions/binomial
import gleeunit/should
pub fn example() {
let n: Float = 40.
let p: Float = 0.5
// For illustrational purposes, evaluate the pmf at the
// point -100.0
binomial.binomial_pmf(-100.0, n, p)
|> should.equal(Ok(0.0))
}
pub fn binomial_random(stream: Iterator(Int), n: Int, p: Float, m: Int) -> Result(
#(List(Int), Iterator(Int)),
String,
)
Generate $$m \in \mathbb{Z}_{>0}$$ random numbers from a binomial distribution (discrete) with parameters $$n \in \mathbb{Z}_{\geq 0}$$ (number of trials) and $$p \in [0, 1]$$ (the success probability in each trial).
The random numbers are generated using the inverse transform method.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generators
import gleam_stats/distributions/binomial
pub fn example() {
let seed: Int = 5
let seq: Int = 1
let n: Float = 40.
let p: Float = 0.5
assert Ok(out) =
generators.seed_pcg32(seed, seq)
|> binomial.binomial_random(n, p, 5_000)
let rands: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn binomial_variance(n: Int, p: Float) -> Result(
Float,
String,
)
Analytically compute the variance of a discrete binomial random variable with parameters $$n \in \mathbb{Z}_{\geq 0}$$ (number of trials) and $$p \in [0, 1]$$ (the success probability in each trial).
The variance returned is: $$n \cdot p \cdot (1 - p)$$.