use num_traits::{Float, FromPrimitive};
use crate::{Jackknife, SE};
use super::Statistic;
#[derive(Debug, Clone, Copy)]
pub struct Studentized<Estimator, SEE, T> {
pub statistic: Estimator,
pub se: SEE,
pub null_value: T,
}
impl<Estimator, SEE, T> Studentized<Estimator, SEE, T> {
pub fn new(statistic: Estimator, se: SEE, null_value: T) -> Self {
Self {
statistic,
se,
null_value,
}
}
}
impl<D, T, Estimator, SEE> Statistic<D, T> for Studentized<Estimator, SEE, T>
where
D: AsRef<[T]>,
T: Float + FromPrimitive + Copy,
Estimator: Statistic<D, T>,
SEE: Statistic<D, T>,
{
#[inline]
fn compute(&self, data: &D) -> T {
let estimate = self.statistic.compute(data);
let se_val = self.se.compute(data);
if se_val.is_zero() || estimate.is_nan() || se_val.is_nan() {
T::nan()
} else {
(estimate - self.null_value) / se_val
}
}
}