gleam_stats/distributions/normal

Functions related to continuous normal random variables.


Functions

pub fn normal_cdf(x: Float, mu: Float, sigma: Float) -> Result(
  Float,
  String,
)

Evaluate, at a certain point $$x \in (-\infty, \infty)$$, the cumulative distribution function (cdf) of a continuous normal random variable with mean $$\mu \in (-\infty, +\infty)$$ and standard deviation $$\sigma\in (-\infty, +\infty)$$.

The cdf is defined as:

\[ F(x; \mu, \sigma) = \frac{1}{2} \cdot \left[ 1 + \text{erf}\left(\frac{x - \mu}{\sigma \cdot 2^{\frac{1}{2}}}\right) \right] \]

In the formula $$\text{erf}(\dot)$$ is the error function.

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

 pub fn example() {
   let mean: Float = 0.
   let sigma: Float = 1.
   // For illustrational purposes, evaluate the cdf at the 
   // point -100.0
   normal.normal_cdf(-100.0, mu, sigma) 
   |> should.equal(Ok(0.0))
 }
pub fn normal_mean(mu: Float, sigma: Float) -> Result(
  Float,
  String,
)

Analytically compute the mean of a continuous normal random variable
with given mean $$\mu \in (-\infty, +\infty)$$ and standard deviation $$\sigma \in (-\infty, +\infty)$$ .

The mean returned is: $$\mu$$.

pub fn normal_pdf(x: Float, mu: Float, sigma: Float) -> Result(
  Float,
  String,
)

Evaluate, at a certain point $$x \in (-\infty, +\infty)$$, the probability density function (pdf) of a continuous normal random variable with given mean $$\mu \in (-\infty, +\infty)$$ and standard deviation $$\sigma\in (-\infty, +\infty)$$ .

The pdf is defined as:

\[ f(x; \mu, \sigma) = \frac{1}{\sigma \cdot \left(2 \cdot \pi \right)^{\frac{1}{2}}} \cdot e^{- \frac{1}{2} \cdot \left(\frac{x - \mu}{\sigma}\right)^{2}} \]

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

 pub fn example() {
   let mean: Float = 0.
   let sigma: Float = 1.
   // For illustrational purposes, evaluate the pdf at the 
   // point -100.0
   normal.normal_pdf(-100.0, mu, sigma) 
   |> should.equal(Ok(0.0))
 }
pub fn normal_random(stream: Iterator(Int), mu: Float, sigma: Float, m: Int) -> Result(
  #(List(Float), Iterator(Int)),
  String,
)

Generate $$m \in \in \mathbb{Z}_{>0}$$ random numbers from a continuous normal distribution with a given mean $$\mu \in (-\infty, +\infty)$$ and standard deviation $$\sigma\in (-\infty, +\infty)$$.

The random numbers are generated using Box–Muller transform.

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

 pub fn example() {
   let seed: Int = 5
   let seq: Int = 1
   let mean: Float = 0.
   let std: Float = 1.
   assert Ok(out) =
     generators.seed_pcg32(seed, seq)
     |> normal.normal_random(mean, std, 5_000)
   let rands: List(Float) = pair.first(out)
   let stream: Iterator(Int) = pair.second(out)
 }
pub fn normal_variance(mu: Float, sigma: Float) -> Result(
  Float,
  String,
)

Analytically compute the variance of a continuous normal random variable
with given mean $$\mu \in (-\infty, +\infty)$$ and standard deviation $$\sigma\in (-\infty, +\infty)$$ .

The variance returned is: $$\sigma^{2}$$.