Skip to main content

lib_jsl_random/distributions/
students_t.rs

1use crate::{distributions::gaussian::gaussian_distribution_box_muller_vec, uniform_generator::UniformGenerator};
2
3/// A Student's t sample with `k` degrees of freedom, `Z / √(χ²_k / k)`, from `k + 1` normal samples.
4/// The normals are drawn as `N(mean, sd²)`; with `mean = 0` the result does not depend on `sd`, and
5/// `mean = 0`, `sd = 1` is the standard case (a non-zero mean gives a noncentral variant).
6pub fn students_t_distribution<U>(uniform_generator: &mut U, k: usize, mean : f64, sd : f64) -> f64 
7where U : UniformGenerator{
8
9    let gaussian_samples = gaussian_distribution_box_muller_vec(k+1,uniform_generator,mean,sd);
10    let first = gaussian_samples.first().expect("checked above");
11    let chi_square: f64 = gaussian_samples.iter().skip(1).map(|x| x*x).sum();
12    first / (chi_square / k as f64).sqrt()
13}