gleam_stats/distributions/bernoulli

Functions related to discrete Bernoulli random variables.


Functions

pub fn bernoulli_cdf(x: Int, p: Float) -> Result(Float, String)

Evaluate, at a certain point $$x \in \mathbb{Z}$$, the cumulative distribution function (cdf) of a discrete Bernoulli random variable with parameter $$p \in [0, 1]$$ (the success probability of a trial).

The cdf is defined as:

\[ F(x; p) = \begin{cases} 0 &\text{if } x < 0, \\ 1 - p &\text{if } x \leq < 1, \\ 1 &\text{if } x \geq 1. \end{cases} \]

Example:
 import gleam_stats/distributions/bernoulli
 import gleeunit/should

 pub fn example() {
   let p: Float = 0.5
   // For illustrational purposes, evaluate the cdf at the 
   // point -100.0
   bernoulli.bernoulli_cdf(-100.0, p) 
   |> should.equal(Ok(0.0))
 }
pub fn bernoulli_mean(p: Float) -> Result(Float, String)

Analytically compute the mean of a discrete Bernoulli random variable with parameter $$p \in [0, 1]$$ (the success probability of a trial).

The mean returned is: $$p$$.

pub fn bernoulli_pmf(x: Int, p: Float) -> Result(Float, String)

Evaluate, at a certain point $$x \in {0, 1}$$, the probability mass function (pmf) of a discrete Bernoulli random variable with parameter $$p \in [0, 1]$$ (the success probability of a trial).

The pmf is defined as:

\[ f(x; p) = \begin{cases} p &\text{if } x = 0, \\ 1 - p &\text{if } x = 1. \end{cases} \]

Example:
 import gleam_stats/distributions/bernoulli
 import gleeunit/should

 pub fn example() {
   let p: Float = 0.5
   // For illustrational purposes, evaluate the pmf at the 
   // point -100.0
   bernoulli.bernoulli_pmf(-100.0, p) 
   |> should.equal(Ok(0.0))
 }
pub fn bernoulli_random(stream: Iterator(Int), p: Float, m: Int) -> Result(
  #(List(Int), Iterator(Int)),
  String,
)

Generate $$m \in \mathbb{Z}_{>0}$$ random numbers from a Bernoulli distribution (discrete) with parameter $$p \in [0, 1]$$ (the success probability of a trial).

The random numbers are generated using the inverse transform method.

Example:
 import gleam/iterator.{Iterator}
 import gleam_stats/generators
 import gleam_stats/distributions/bernoulli

 pub fn example() {
   let seed: Int = 5
   let seq: Int = 1
   let p: Float = 0.5
   assert Ok(out) =
     generators.seed_pcg32(seed, seq)
     |> bernoulli.bernoulli_random(p, 5_000)
   let rands: List(Float) = pair.first(out)
   let stream: Iterator(Int) = pair.second(out)
 }
pub fn bernoulli_variance(p: Float) -> Result(Float, String)

Analytically compute the variance of a discrete Bernoulli random variable with parameter $$p \in [0, 1]$$ (the success probability of a trial).

The variance returned is: $$p \cdot (1 - p)$$.