gleam_stats/distributions/chisquared

Functions related to continuous chi-squared random variables.


Functions

pub fn chisquared_cdf(x: Float, d: Int) -> Result(Float, String)

Evaluate, at a certain point $$x \in [0, +\infty]$$, the cumulative distribution function (cdf) of a continuous chi-squared random variable with given degrees of freedom $$d \in \mathbb{Z}_{>0}$$.

The cdf is defined as:

\[ F(x; d) = \frac{\gamma\left(\frac{d}{2}, \frac{x}{2}\right)}{\Gamma\left(\frac{d}{2}\right)} \]

In the formula, $$\gamma(\cdot, \cdot)$$ is the lower incomplete gamma function.

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

 pub fn example() {
   let ddof: Float = 1.
   // For illustrational purposes, evaluate the cdf at the 
   // point -100.0
   chisquared.chisquared_cdf(-100.0, mu, sigma) 
   |> should.equal(Ok(0.0))
 }
pub fn chisquared_mean(d: Int) -> Result(Float, String)

Analytically compute the mean of a continuous chi-squared random variable
with given degrees of freedom $$d \in \mathbb{N}$$.

The mean returned is: $$d$$.

pub fn chisquared_pdf(x: Float, d: Int) -> Result(Float, String)

Evaluate, at a certain point $$x \in [0, +\infty]$$ the probability density function (pdf) of a continuous chi-squared random variable with given degrees of freedom $$d \in \mathbb{Z}_{>0}$$.

The pdf is defined as:

\[ f(x; d) = \begin{cases} \frac{x^{\frac{d}{2} - 1}\cdot e^{-\frac{x}{2}}}{2^{\frac{d}{2}} \cdot \Gamma\left(\frac{d}{2}\right)} &\text{if } x > 0, \\ 0 &\text{if } x \leq 0. \end{cases} \]

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

 pub fn example() {
   let ddof: Float = 1.
   // For illustrational purposes, evaluate the pdf at the 
   // point -100.0
   chisquared.chisquared_pdf(-100.0, ddof) 
   |> should.equal(Ok(0.0))
 }
pub fn chisquared_random(stream: Iterator(Int), d: Int, m: Int) -> Result(
  #(List(Float), Iterator(Int)),
  String,
)

Generate $$m \in \mathbb{Z}_{>0}$$ random numbers from a continuous chi-squared distribution with given degrees of freedom $$d \in \mathbb{Z}_{>0}$$.

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

 pub fn example() {
   let seed: Int = 5
   let seq: Int = 1
   let ddof: Float = 1.
   assert Ok(out) =
     generators.seed_pcg32(seed, seq)
     |> chisquared.chisquared_random(ddof, 5_000)
   let rands: List(Float) = pair.first(out)
   let stream: Iterator(Int) = pair.second(out)
 }
pub fn chisquared_variance(d: Int) -> Result(Float, String)

Analytically compute the variance of a continuous chi-squared random variable
with given degrees of freedom $$d \in \mathbb{Z}_{>0}$$.

The variance returned is: $$2 \cdot d$$.