gleam_stats/distributions/uniform

Functions related to continuous uniform random variables.


Functions

pub fn uniform_cdf(x: Float, a: Float, b: Float) -> Result(
  Float,
  String,
)

Evaluate, at a certain point $$x \in (-\infty, +\infty)$$, the cumulative distribution function (cdf) of a continuous uniform random variable that takes values in the interval $$[a, b]$$.

The cdf is defined as:

\[ F(x; a, b) = \begin{cases} 0 &\text{if } x < a, \\ \frac{x - a}{b - a} &\text{if } a \leq x \leq b, \\ 0 &\text{if } x > b. \end{cases} \]

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

 pub fn example() {
   let a: Float = 0.
   let b: Float = 1.
   // For illustrational purposes, evaluate the cdf at points
   // 0.0 and 1.0
   uniform.uniform_cdf(0.0, a, b)
   |> should.equal(Ok(0.0))

   uniform.uniform_cdf(1.0, a, b)
   |> should.equal(Ok(1.0))
 }
pub fn uniform_mean(a: Float, b: Float) -> Result(Float, String)

Analytically compute the mean of a continuous uniform random variable
that takes values in the interval $$[a, b]$$.

The mean returned is: $$\frac{a + b}{2}$$.

pub fn uniform_pdf(x: Float, a: Float, b: Float) -> Result(
  Float,
  String,
)

Evaluate, at a certain point $$x \in (-\infty, +\infty)$$, the probability density function (pdf) of a continuous uniform random variable that takes values in the interval $$[a, b]$$.

The pdf is defined as:

\[ f(x; a, b) = \begin{cases} \frac{1}{b - a} &\text{if } a \leq x \leq b, \\ 0 &\text{if } x < a \text{ or } x > b. \end{cases} \]

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

 pub fn example() {
   let a: Float = 0.
   let b: Float = 1.
   // For illustrational purposes, evaluate the pdf at points 
   // 0.0 and 1.0
   uniform.uniform_cdf(0.0, a, b)
   |> should.equal(Ok(0.5))

   uniform.uniform_cdf(1.0, a, b) 
   |> should.equal(Ok(1.0))
 }
pub fn uniform_random(stream: Iterator(Int), a: Float, b: Float, m: Int) -> Result(
  #(List(Float), Iterator(Int)),
  String,
)

Generate $$m \in \mathbb{Z}_{>0}$$ random numbers in the interval $$[a, b]$$ from a continuous uniform distribution.

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

 pub fn example() {
   let seed: Int = 5
   let seq: Int = 1
   // Min value
   let a: Float = 0.
   // Max value
   let b: Float = 1.
   assert Ok(out) =
     generators.seed_pcg32(seed, seq)
     |> uniform.uniform_random(a, b, 5_000)
   let rands: List(Float) = pair.first(out)
   let stream: Iterator(Int) = pair.second(out)
 }
pub fn uniform_variance(a: Float, b: Float) -> Result(
  Float,
  String,
)

Analytically compute the variance of a continuous uniform random variable
that takes values in the interval $$[a, b]$$.

The variance returned is: $$\frac{\left(b - a\right)^{2}}{12}$$.