|
1 | 1 | use super::SummaryStatisticsExt;
|
2 | 2 | use crate::errors::EmptyInput;
|
3 | 3 | use ndarray::{ArrayBase, Data, Dimension};
|
| 4 | +use num_integer::IterBinomial; |
4 | 5 | use num_traits::{Float, FromPrimitive, Zero};
|
5 | 6 | use std::ops::{Add, Div};
|
6 | 7 |
|
@@ -36,15 +37,161 @@ where
|
36 | 37 | {
|
37 | 38 | self.map(|x| x.ln()).mean().map(|x| x.exp())
|
38 | 39 | }
|
| 40 | + |
| 41 | + fn kurtosis(&self) -> Result<A, EmptyInput> |
| 42 | + where |
| 43 | + A: Float + FromPrimitive, |
| 44 | + { |
| 45 | + let central_moments = self.central_moments(4)?; |
| 46 | + Ok(central_moments[4] / central_moments[2].powi(2)) |
| 47 | + } |
| 48 | + |
| 49 | + fn skewness(&self) -> Result<A, EmptyInput> |
| 50 | + where |
| 51 | + A: Float + FromPrimitive, |
| 52 | + { |
| 53 | + let central_moments = self.central_moments(3)?; |
| 54 | + Ok(central_moments[3] / central_moments[2].sqrt().powi(3)) |
| 55 | + } |
| 56 | + |
| 57 | + fn central_moment(&self, order: u16) -> Result<A, EmptyInput> |
| 58 | + where |
| 59 | + A: Float + FromPrimitive, |
| 60 | + { |
| 61 | + if self.is_empty() { |
| 62 | + return Err(EmptyInput); |
| 63 | + } |
| 64 | + match order { |
| 65 | + 0 => Ok(A::one()), |
| 66 | + 1 => Ok(A::zero()), |
| 67 | + n => { |
| 68 | + let mean = self.mean().unwrap(); |
| 69 | + let shifted_array = self.mapv(|x| x - mean); |
| 70 | + let shifted_moments = moments(shifted_array, n); |
| 71 | + let correction_term = -shifted_moments[1]; |
| 72 | + |
| 73 | + let coefficients = central_moment_coefficients(&shifted_moments); |
| 74 | + Ok(horner_method(coefficients, correction_term)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn central_moments(&self, order: u16) -> Result<Vec<A>, EmptyInput> |
| 80 | + where |
| 81 | + A: Float + FromPrimitive, |
| 82 | + { |
| 83 | + if self.is_empty() { |
| 84 | + return Err(EmptyInput); |
| 85 | + } |
| 86 | + match order { |
| 87 | + 0 => Ok(vec![A::one()]), |
| 88 | + 1 => Ok(vec![A::one(), A::zero()]), |
| 89 | + n => { |
| 90 | + // We only perform these operations once, and then reuse their |
| 91 | + // result to compute all the required moments |
| 92 | + let mean = self.mean().unwrap(); |
| 93 | + let shifted_array = self.mapv(|x| x - mean); |
| 94 | + let shifted_moments = moments(shifted_array, n); |
| 95 | + let correction_term = -shifted_moments[1]; |
| 96 | + |
| 97 | + let mut central_moments = vec![A::one(), A::zero()]; |
| 98 | + for k in 2..=n { |
| 99 | + let coefficients = |
| 100 | + central_moment_coefficients(&shifted_moments[..=(k as usize)]); |
| 101 | + let central_moment = horner_method(coefficients, correction_term); |
| 102 | + central_moments.push(central_moment) |
| 103 | + } |
| 104 | + Ok(central_moments) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Returns a vector containing all moments of the array elements up to |
| 111 | +/// *order*, where the *p*-th moment is defined as: |
| 112 | +/// |
| 113 | +/// ```text |
| 114 | +/// 1 n |
| 115 | +/// ― ∑ xᵢᵖ |
| 116 | +/// n i=1 |
| 117 | +/// ``` |
| 118 | +/// |
| 119 | +/// The returned moments are ordered by power magnitude: 0th moment, 1st moment, etc. |
| 120 | +/// |
| 121 | +/// **Panics** if `A::from_usize()` fails to convert the number of elements in the array. |
| 122 | +fn moments<A, S, D>(a: ArrayBase<S, D>, order: u16) -> Vec<A> |
| 123 | +where |
| 124 | + A: Float + FromPrimitive, |
| 125 | + S: Data<Elem = A>, |
| 126 | + D: Dimension, |
| 127 | +{ |
| 128 | + let n_elements = |
| 129 | + A::from_usize(a.len()).expect("Converting number of elements to `A` must not fail"); |
| 130 | + let order = order as i32; |
| 131 | + |
| 132 | + // When k=0, we are raising each element to the 0th power |
| 133 | + // No need to waste CPU cycles going through the array |
| 134 | + let mut moments = vec![A::one()]; |
| 135 | + |
| 136 | + if order >= 1 { |
| 137 | + // When k=1, we don't need to raise elements to the 1th power (identity) |
| 138 | + moments.push(a.sum() / n_elements) |
| 139 | + } |
| 140 | + |
| 141 | + for k in 2..=order { |
| 142 | + moments.push(a.map(|x| x.powi(k)).sum() / n_elements) |
| 143 | + } |
| 144 | + moments |
| 145 | +} |
| 146 | + |
| 147 | +/// Returns the coefficients in the polynomial expression to compute the *p*th |
| 148 | +/// central moment as a function of the sample mean. |
| 149 | +/// |
| 150 | +/// It takes as input all moments up to order *p*, ordered by power magnitude - *p* is |
| 151 | +/// inferred to be the length of the *moments* array. |
| 152 | +fn central_moment_coefficients<A>(moments: &[A]) -> Vec<A> |
| 153 | +where |
| 154 | + A: Float + FromPrimitive, |
| 155 | +{ |
| 156 | + let order = moments.len(); |
| 157 | + IterBinomial::new(order) |
| 158 | + .zip(moments.iter().rev()) |
| 159 | + .map(|(binom, &moment)| A::from_usize(binom).unwrap() * moment) |
| 160 | + .collect() |
| 161 | +} |
| 162 | + |
| 163 | +/// Uses [Horner's method] to evaluate a polynomial with a single indeterminate. |
| 164 | +/// |
| 165 | +/// Coefficients are expected to be sorted by ascending order |
| 166 | +/// with respect to the indeterminate's exponent. |
| 167 | +/// |
| 168 | +/// If the array is empty, `A::zero()` is returned. |
| 169 | +/// |
| 170 | +/// Horner's method can evaluate a polynomial of order *n* with a single indeterminate |
| 171 | +/// using only *n-1* multiplications and *n-1* sums - in terms of number of operations, |
| 172 | +/// this is an optimal algorithm for polynomial evaluation. |
| 173 | +/// |
| 174 | +/// [Horner's method]: https://en.wikipedia.org/wiki/Horner%27s_method |
| 175 | +fn horner_method<A>(coefficients: Vec<A>, indeterminate: A) -> A |
| 176 | +where |
| 177 | + A: Float, |
| 178 | +{ |
| 179 | + let mut result = A::zero(); |
| 180 | + for coefficient in coefficients.into_iter().rev() { |
| 181 | + result = coefficient + indeterminate * result |
| 182 | + } |
| 183 | + result |
39 | 184 | }
|
40 | 185 |
|
41 | 186 | #[cfg(test)]
|
42 | 187 | mod tests {
|
43 | 188 | use super::SummaryStatisticsExt;
|
44 | 189 | use crate::errors::EmptyInput;
|
45 |
| - use approx::abs_diff_eq; |
46 |
| - use ndarray::{array, Array1}; |
| 190 | + use approx::assert_abs_diff_eq; |
| 191 | + use ndarray::{array, Array, Array1}; |
| 192 | + use ndarray_rand::RandomExt; |
47 | 193 | use noisy_float::types::N64;
|
| 194 | + use rand::distributions::Uniform; |
48 | 195 | use std::f64;
|
49 | 196 |
|
50 | 197 | #[test]
|
@@ -90,16 +237,116 @@ mod tests {
|
90 | 237 | // Computed using SciPy
|
91 | 238 | let expected_geometric_mean = 0.4345897639796527;
|
92 | 239 |
|
93 |
| - abs_diff_eq!(a.mean().unwrap(), expected_mean, epsilon = f64::EPSILON); |
94 |
| - abs_diff_eq!( |
| 240 | + assert_abs_diff_eq!(a.mean().unwrap(), expected_mean, epsilon = 1e-9); |
| 241 | + assert_abs_diff_eq!( |
95 | 242 | a.harmonic_mean().unwrap(),
|
96 | 243 | expected_harmonic_mean,
|
97 |
| - epsilon = f64::EPSILON |
| 244 | + epsilon = 1e-7 |
98 | 245 | );
|
99 |
| - abs_diff_eq!( |
| 246 | + assert_abs_diff_eq!( |
100 | 247 | a.geometric_mean().unwrap(),
|
101 | 248 | expected_geometric_mean,
|
102 |
| - epsilon = f64::EPSILON |
| 249 | + epsilon = 1e-12 |
103 | 250 | );
|
104 | 251 | }
|
| 252 | + |
| 253 | + #[test] |
| 254 | + fn test_central_moment_with_empty_array_of_floats() { |
| 255 | + let a: Array1<f64> = array![]; |
| 256 | + for order in 0..=3 { |
| 257 | + assert_eq!(a.central_moment(order), Err(EmptyInput)); |
| 258 | + assert_eq!(a.central_moments(order), Err(EmptyInput)); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + #[test] |
| 263 | + fn test_zeroth_central_moment_is_one() { |
| 264 | + let n = 50; |
| 265 | + let bound: f64 = 200.; |
| 266 | + let a = Array::random(n, Uniform::new(-bound.abs(), bound.abs())); |
| 267 | + assert_eq!(a.central_moment(0).unwrap(), 1.); |
| 268 | + } |
| 269 | + |
| 270 | + #[test] |
| 271 | + fn test_first_central_moment_is_zero() { |
| 272 | + let n = 50; |
| 273 | + let bound: f64 = 200.; |
| 274 | + let a = Array::random(n, Uniform::new(-bound.abs(), bound.abs())); |
| 275 | + assert_eq!(a.central_moment(1).unwrap(), 0.); |
| 276 | + } |
| 277 | + |
| 278 | + #[test] |
| 279 | + fn test_central_moments() { |
| 280 | + let a: Array1<f64> = array![ |
| 281 | + 0.07820559, 0.5026185, 0.80935324, 0.39384033, 0.9483038, 0.62516215, 0.90772261, |
| 282 | + 0.87329831, 0.60267392, 0.2960298, 0.02810356, 0.31911966, 0.86705506, 0.96884832, |
| 283 | + 0.2222465, 0.42162446, 0.99909868, 0.47619762, 0.91696979, 0.9972741, 0.09891734, |
| 284 | + 0.76934818, 0.77566862, 0.7692585, 0.2235759, 0.44821286, 0.79732186, 0.04804275, |
| 285 | + 0.87863238, 0.1111003, 0.6653943, 0.44386445, 0.2133176, 0.39397086, 0.4374617, |
| 286 | + 0.95896624, 0.57850146, 0.29301706, 0.02329879, 0.2123203, 0.62005503, 0.996492, |
| 287 | + 0.5342986, 0.97822099, 0.5028445, 0.6693834, 0.14256682, 0.52724704, 0.73482372, |
| 288 | + 0.1809703, |
| 289 | + ]; |
| 290 | + // Computed using scipy.stats.moment |
| 291 | + let expected_moments = vec![ |
| 292 | + 1., |
| 293 | + 0., |
| 294 | + 0.09339920262960291, |
| 295 | + -0.0026849636727735186, |
| 296 | + 0.015403769257729755, |
| 297 | + -0.001204176487006564, |
| 298 | + 0.002976822584939186, |
| 299 | + ]; |
| 300 | + for (order, expected_moment) in expected_moments.iter().enumerate() { |
| 301 | + assert_abs_diff_eq!( |
| 302 | + a.central_moment(order as u16).unwrap(), |
| 303 | + expected_moment, |
| 304 | + epsilon = 1e-8 |
| 305 | + ); |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + #[test] |
| 310 | + fn test_bulk_central_moments() { |
| 311 | + // Test that the bulk method is coherent with the non-bulk method |
| 312 | + let n = 50; |
| 313 | + let bound: f64 = 200.; |
| 314 | + let a = Array::random(n, Uniform::new(-bound.abs(), bound.abs())); |
| 315 | + let order = 10; |
| 316 | + let central_moments = a.central_moments(order).unwrap(); |
| 317 | + for i in 0..=order { |
| 318 | + assert_eq!(a.central_moment(i).unwrap(), central_moments[i as usize]); |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + #[test] |
| 323 | + fn test_kurtosis_and_skewness_is_none_with_empty_array_of_floats() { |
| 324 | + let a: Array1<f64> = array![]; |
| 325 | + assert_eq!(a.skewness(), Err(EmptyInput)); |
| 326 | + assert_eq!(a.kurtosis(), Err(EmptyInput)); |
| 327 | + } |
| 328 | + |
| 329 | + #[test] |
| 330 | + fn test_kurtosis_and_skewness() { |
| 331 | + let a: Array1<f64> = array![ |
| 332 | + 0.33310096, 0.98757449, 0.9789796, 0.96738114, 0.43545674, 0.06746873, 0.23706562, |
| 333 | + 0.04241815, 0.38961714, 0.52421271, 0.93430327, 0.33911604, 0.05112372, 0.5013455, |
| 334 | + 0.05291507, 0.62511183, 0.20749633, 0.22132433, 0.14734804, 0.51960608, 0.00449208, |
| 335 | + 0.4093339, 0.2237519, 0.28070469, 0.7887231, 0.92224523, 0.43454188, 0.18335111, |
| 336 | + 0.08646856, 0.87979847, 0.25483457, 0.99975627, 0.52712442, 0.41163279, 0.85162594, |
| 337 | + 0.52618733, 0.75815023, 0.30640695, 0.14205781, 0.59695813, 0.851331, 0.39524328, |
| 338 | + 0.73965373, 0.4007615, 0.02133069, 0.92899207, 0.79878191, 0.38947334, 0.22042183, |
| 339 | + 0.77768353, |
| 340 | + ]; |
| 341 | + // Computed using scipy.stats.kurtosis(a, fisher=False) |
| 342 | + let expected_kurtosis = 1.821933711687523; |
| 343 | + // Computed using scipy.stats.skew |
| 344 | + let expected_skewness = 0.2604785422878771; |
| 345 | + |
| 346 | + let kurtosis = a.kurtosis().unwrap(); |
| 347 | + let skewness = a.skewness().unwrap(); |
| 348 | + |
| 349 | + assert_abs_diff_eq!(kurtosis, expected_kurtosis, epsilon = 1e-12); |
| 350 | + assert_abs_diff_eq!(skewness, expected_skewness, epsilon = 1e-8); |
| 351 | + } |
105 | 352 | }
|
0 commit comments