gleam_stats/distributions/negbinomial

Functions related to discrete negative binomial random variables.


Functions

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

Evaluate, at a certain point $$x \in \mathbb{Z}$$, the cumulative distribution function (cdf) of a discrete negative binomial random variable with parameters $$r \in \mathbb{Z}_{>0}$$ (number of failures until the experiment is stopped) and $$p \in [0, 1]$$ (the success probability in each experiment).

The cdf is defined as:

\[ F(x; r, p) = \begin{cases} \sum_{i=0}^{x} \binom{i + r - 1}{i} \cdot (1 - p)^{r} \cdot p^{i} &\text{if } x \geq 0, \\ 0 &\text{if } x < 0. \end{cases} \]

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

 pub fn example() {
   let r: Float = 40.
   let p: Float = 0.5
   // For illustrational purposes, evaluate the cdf at the 
   // point -100.0
   negbinomial.negbinomial_cdf(-100.0, r, p) 
   |> should.equal(Ok(0.0))
 }
pub fn negbinomial_mean(r: Int, p: Float) -> Result(Float, String)

Analytically compute the mean of a discrete negative binomial random variable with parameters $$r \in \mathbb{Z}_{>0}$$ (number of failures until the experiment is stopped) and $$p \in [0, 1]$$ (the success probability in each experiment).

The mean returned is: $$\frac{r \cdot p}{1 - p}$$.

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

Evaluate, at a certain point $$x \in \mathbb{Z}$$, the probability mass function (pmf) of a discrete negative binomial random variable with parameters $$r \in \mathbb{Z}_{>0}$$ (number of failures until the experiment is stopped) and $$p \in [0, 1]$$ (the success probability in each experiment).

The pmf is defined as:

\[ f(x; r, p) = \begin{cases} \binom{x + r - 1}{x} \cdot (1 - p)^{r} \cdot p^{x} &\text{if } x \geq 0, \\ 0 &\text{if } x < 0. \end{cases} \]

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

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

Generate $$m \in \mathbb{Z}_{>0}$$ random numbers from a discrete negative binomial distribution with parameters $$r \in \mathbb{Z}_{>0}$$ (number of failures until the experiment is stopped) and $$p \in [0, 1]$$ (the success probability in each experiment).

The random numbers are generated using the inverse transform method.

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

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

Analytically compute the variance of a discrete negative binomial random variiable with parameters $$r \in \mathbb{Z}_{>0}$$ (number of failures until the experiment is stopped) and $$p \in [0, 1]$$ (the success probability in each experiment).

The variance returned is: $$\frac{r \cdot p}{(1 - p)^2}$$.