From 02d40df9cfa1533da9fcedf91fde13086c1fd7d7 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Sun, 12 Jan 2025 16:46:37 +0530 Subject: [PATCH] chore: reorganize statistical methods --- eve/src/stats.rs | 81 ++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/eve/src/stats.rs b/eve/src/stats.rs index 765349e..a74cb31 100644 --- a/eve/src/stats.rs +++ b/eve/src/stats.rs @@ -112,43 +112,6 @@ impl Model { (mu, delta) } - /// Calculates the empirical mean of the random variable S from the given - /// sample data. - fn mean(&self, x: Score) -> f64 { - match *self { - Self::Pentanomial => { - 0.00 * x.ll + 0.25 * x.ld + 0.50 * (x.dd + x.wl) + 0.75 * x.wd + 1.00 * x.ww - } - Self::Traditional => 1.0 * x.w + 0.5 * x.d + 0.0 * x.l, - } - } - - /// Calculates the deviation (square root of the variance) of the given - /// sample data from the given pivot point. - fn deviation(&self, x: Score, mu: f64) -> f64 { - self.variance(x, mu).sqrt() - } - - /// Calculates the variance of the given sample data from the given pivot. - fn variance(&self, x: Score, mu: f64) -> f64 { - match *self { - Self::Pentanomial => { - ((x.dd + x.wl) * f64::powi(0.50 - mu, 2) - + x.ll * f64::powi(0.00 - mu, 2) - + x.ld * f64::powi(0.25 - mu, 2) - + x.wd * f64::powi(0.75 - mu, 2) - + x.ww * f64::powi(1.00 - mu, 2)) - / x.n - } - Self::Traditional => { - (x.w * f64::powi(1.0 - mu, 2) - + x.d * f64::powi(0.5 - mu, 2) - + x.l * f64::powi(0.0 - mu, 2)) - / (x.n * 2.0) - } - } - } - /// llh calculates the log-likelihood of the given sample `x` arising from /// the probability distribution specified by the parameter `theta`. /// @@ -183,6 +146,50 @@ impl Model { } } } + + /// Calculates the empirical mean of the random variable S from the given + /// sample data. + fn mean(&self, x: Score) -> f64 { + match *self { + Self::Pentanomial => { + 0.00 * x.ll + 0.25 * x.ld + 0.50 * (x.dd + x.wl) + 0.75 * x.wd + 1.00 * x.ww + } + Self::Traditional => 1.0 * x.w + 0.5 * x.d + 0.0 * x.l, + } + } + + /// Calculates the deviation (square root of the variance) of the given + /// sample data from the given pivot point. + fn deviation(&self, x: Score, mu: f64) -> f64 { + self.variance(x, mu).sqrt() + } + + /// Calculates the variance of the given sample data from the given pivot. + fn variance(&self, x: Score, mu: f64) -> f64 { + match *self { + Self::Pentanomial => self.sum_of_squares(x, mu) / x.n, + Self::Traditional => self.sum_of_squares(x, mu) / (x.n * 2.0), + } + } + + /// Calculates the sum of squares (variance * sample size) of the given + /// sample data from the given pivot point. + fn sum_of_squares(&self, x: Score, mu: f64) -> f64 { + match *self { + Self::Pentanomial => { + (x.dd + x.wl) * f64::powi(0.50 - mu, 2) + + x.ll * f64::powi(0.00 - mu, 2) + + x.ld * f64::powi(0.25 - mu, 2) + + x.wd * f64::powi(0.75 - mu, 2) + + x.ww * f64::powi(1.00 - mu, 2) + } + Self::Traditional => { + x.w * f64::powi(1.0 - mu, 2) + + x.d * f64::powi(0.5 - mu, 2) + + x.l * f64::powi(0.0 - mu, 2) + } + } + } } /// sprt_stopping bounds returns the upper and lower bounds of the llr for