1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
//! Provides statistical computation utilities for data sets mod slice_statistics; /// Enumeration of possible tie-breaking strategies /// when computing ranks #[derive(Debug, Copy, Clone)] pub enum RankTieBreaker { /// Replaces ties with their mean Average, /// Replace ties with their minimum Min, /// Replace ties with their maximum Max, /// Permutation with increasing values at each index of ties First, } /// The statistics trait provides a host of statistical utilities for analzying /// data sets pub trait Statistics { /// Returns the minimum value in the data /// /// # Rermarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.min().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.min().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.min(), -2.0); /// ``` fn min(&self) -> f64; /// Returns the maximum value in the data /// /// # Remarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.max().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.max().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.max(), 3.0); /// ``` fn max(&self) -> f64; /// Returns the minimum absolute value in the data /// /// # Rermarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.abs_min().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.abs_min().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.abs_min(), 0.0); /// ``` fn abs_min(&self) -> f64; /// Returns the maximum absolute value in the data /// /// # Rermarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.abs_max().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.abs_max().is_nan()); /// /// let z = [0.0, 3.0, -2.0, -8.0]; /// assert_eq!(z.abs_max(), 8.0); /// ``` fn abs_max(&self) -> f64; /// Evaluates the sample mean, an estimate of the population /// mean. /// /// # Remarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use std::f64; /// use statrs::statistics::Statistics; /// /// # fn main() { /// let x = []; /// assert!(x.mean().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.mean().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_almost_eq!(z.mean(), 1.0 / 3.0, 1e-15); /// # } /// ``` fn mean(&self) -> f64; /// Evaluates the geometric mean of the data /// /// # Remarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN`. /// Returns `f64::NAN` if an entry is less than `0`. Returns `0` /// if no entry is less than `0` but there are entries equal to `0`. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use std::f64; /// use statrs::statistics::Statistics; /// /// # fn main() { /// let x = []; /// assert!(x.geometric_mean().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.geometric_mean().is_nan()); /// /// let mut z = [0.0, 3.0, -2.0]; /// assert!(z.geometric_mean().is_nan()); /// /// z = [0.0, 3.0, 2.0]; /// assert_eq!(z.geometric_mean(), 0.0); /// /// z = [1.0, 2.0, 3.0]; /// // test value from online calculator, could be more accurate /// assert_almost_eq!(z.geometric_mean(), 1.81712, 1e-5); /// # } /// ``` fn geometric_mean(&self) -> f64; /// Evaluates the harmonic mean of the data /// /// # Remarks /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN`, or if any value /// in data is less than `0`. Returns `0` if there are no values less than `0` but /// there exists values equal to `0`. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use std::f64; /// use statrs::statistics::Statistics; /// /// # fn main() { /// let x = []; /// assert!(x.harmonic_mean().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.harmonic_mean().is_nan()); /// /// let mut z = [0.0, 3.0, -2.0]; /// assert!(z.harmonic_mean().is_nan()); /// /// z = [0.0, 3.0, 2.0]; /// assert_eq!(z.harmonic_mean(), 0.0); /// /// z = [1.0, 2.0, 3.0]; /// // test value from online calculator, could be more accurate /// assert_almost_eq!(z.harmonic_mean(), 1.63636, 1e-5); /// # } /// ``` fn harmonic_mean(&self) -> f64; /// Estimates the unbiased population variance from the provided samples /// /// # Remarks /// /// On a dataset of size `N`, `N-1` is used as a normalizer (Bessel's correction). /// /// Returns `f64::NAN` if data has less than two entries or if any entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.variance().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.variance().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.variance(), 19.0 / 3.0); /// ``` fn variance(&self) -> f64; /// Evaluates the population variance from a full population. /// /// # Remarks /// /// On a dataset of size `N`, `N` is used as a normalizer and would thus /// be biased if applied to a subset /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.population_variance().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.population_variance().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.population_variance(), 38.0 / 9.0); /// ``` fn population_variance(&self) -> f64; /// Estimates the unbiased population standard deviation from the provided samples /// /// # Remarks /// /// On a dataset of size `N`, `N-1` is used as a normalizer (Bessel's correction). /// /// Returns `f64::NAN` if data has less than two entries or if any entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.std_dev().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.std_dev().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.std_dev(), (19f64 / 3.0).sqrt()); /// ``` fn std_dev(&self) -> f64; /// Evaluates the population standard deviation from a full population. /// /// # Remarks /// /// On a dataset of size `N`, `N` is used as a normalizer and would thus /// be biased if applied to a subset /// /// Returns `f64::NAN` if data is empty or an entry is `f64::NAN` /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.population_std_dev().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.population_std_dev().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// assert_eq!(z.population_std_dev(), (38f64 / 9.0).sqrt()); /// ``` fn population_std_dev(&self) -> f64; /// Estimates the unbiased population covariance between the two provided samples /// /// # Remarks /// /// On a dataset of size `N`, `N-1` is used as a normalizer (Bessel's correction). /// /// Returns `f64::NAN` if data has less than two entries or if any entry is `f64::NAN` /// /// # Panics /// /// If the two sample containers do not contain the same number of elements /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.covariance(&[]).is_nan()); /// /// let y1 = [0.0, f64::NAN, 3.0, -2.0]; /// let y2 = [-5.0, 4.0, 10.0, f64::NAN]; /// assert!(y1.covariance(&y2).is_nan()); /// /// let z1 = [0.0, 3.0, -2.0]; /// let z2 = [-5.0, 4.0, 10.0]; /// assert_eq!(z1.covariance(&z2), -5.5); /// ``` fn covariance(&self, other: &Self) -> f64; /// Evaluates the population covariance between the two provider populations /// /// # Remarks /// /// On a dataset of size `N`, `N` is used as a normalizer and would thus be /// biased if applied to a subset /// /// Returns `f64::NAN` if data is empty or any entry is `f64::NAN` /// /// # Panics /// /// If the two sample containers do not contain the same number of elements /// /// # Examples /// /// ``` /// use std::f64; /// use statrs::statistics::Statistics; /// /// let x = []; /// assert!(x.population_covariance(&[]).is_nan()); /// /// let y1 = [0.0, f64::NAN, 3.0, -2.0]; /// let y2 = [-5.0, 4.0, 10.0, f64::NAN]; /// assert!(y1.population_covariance(&y2).is_nan()); /// /// let z1 = [0.0, 3.0, -2.0]; /// let z2 = [-5.0, 4.0, 10.0]; /// assert_eq!(z1.population_covariance(&z2), -11.0 / 3.0); /// ``` fn population_covariance(&self, other: &Self) -> f64; /// Estimates the quadratic mean (Root Mean Square) of the data /// /// # Remarks /// /// Returns `f64::NAN` if data is empty or any entry is `f64::NAN` /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use std::f64; /// use statrs::statistics::Statistics; /// /// # fn main() { /// let x = []; /// assert!(x.quadratic_mean().is_nan()); /// /// let y = [0.0, f64::NAN, 3.0, -2.0]; /// assert!(y.quadratic_mean().is_nan()); /// /// let z = [0.0, 3.0, -2.0]; /// // test value from online calculator, could be more accurate /// assert_almost_eq!(z.quadratic_mean(), 2.08167, 1e-5); /// # } /// ``` fn quadratic_mean(&self) -> f64; /// Returns the order statistic `(order 1..N)` from the data /// /// # Remarks /// /// No sorting is assumed. Order must be one-based (between `1` and `N` inclusive) /// Returns `f64::NAN` if order is outside the viable range or data is empty. /// /// # Examples /// /// ``` /// use statrs::statistics::Statistics; /// /// let mut x = []; /// assert!(x.order_statistic(1).is_nan()); /// /// let mut y = [0.0, 3.0, -2.0]; /// assert!(y.order_statistic(0).is_nan()); /// assert!(y.order_statistic(4).is_nan()); /// assert_eq!(y.order_statistic(2), 0.0); /// assert!(y != [0.0, 3.0, -2.0]); /// ``` fn order_statistic(&mut self, order: usize) -> f64; /// Returns the median value from the data /// /// # Remarks /// /// Returns `f64::NAN` if data is empty /// /// # Examples /// /// ``` /// use statrs::statistics::Statistics; /// /// let mut x = []; /// assert!(x.median().is_nan()); /// /// let mut y = [0.0, 3.0, -2.0]; /// assert_eq!(y.median(), 0.0); /// assert!(y != [0.0, 3.0, -2.0]); fn median(&mut self) -> f64; /// Estimates the tau-th quantile from the data. The tau-th quantile /// is the data value where the cumulative distribution function crosses tau. /// /// # Remarks /// /// No sorting is assumed. Tau must be between `0` and `1` inclusive. /// Returns `f64::NAN` if data is empty or tau is outside the inclusive range. /// /// # Examples /// /// ``` /// use statrs::statistics::Statistics; /// /// let mut x = []; /// assert!(x.quantile(0.5).is_nan()); /// /// let mut y = [0.0, 3.0, -2.0]; /// assert!(y.quantile(-1.0).is_nan()); /// assert!(y.quantile(2.0).is_nan()); /// assert_eq!(y.quantile(0.5), 0.0); /// assert!(y != [0.0, 3.0, -2.0]); /// ``` fn quantile(&mut self, tau: f64) -> f64; /// Estimates the p-Percentile value from the data. /// /// # Remarks /// /// Use quantile for non-integer percentiles. `p` must be between `0` and `100` inclusive. /// Returns `f64::NAN` if data is empty or `p` is outside the inclusive range. /// /// # Examples /// /// ``` /// use statrs::Statistics; /// /// let mut x = []; /// assert!(x.percentile(0).is_nan()); /// /// let mut y = [1.0, 5.0, 3.0, 4.0, 10.0, 9.0, 6.0, 7.0, 8.0, 2.0]; /// assert_eq!(y.percentile(0), 1.0); /// assert_eq!(y.percentile(50), 5.5); /// assert_eq!(y.percentile(100), 10.0); /// assert!(y.percentile(105).is_nan()); /// assert!(y != [1.0, 5.0, 3.0, 4.0, 10.0, 9.0, 6.0, 7.0, 8.0, 2.0]); /// ``` fn percentile(&mut self, p: usize) -> f64; /// Estimates the first quartile value from the data. /// /// # Remarks /// /// Returns `f64::NAN` if data is empty /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use statrs::statistics::Statistics; /// /// # fn main() { /// let mut x = []; /// assert!(x.lower_quartile().is_nan()); /// /// let mut y = [2.0, 1.0, 3.0, 4.0]; /// assert_almost_eq!(y.lower_quartile(), 1.416666666666666, 1e-15); /// assert!(y != [2.0, 1.0, 3.0, 4.0]); /// # } /// ``` fn lower_quartile(&mut self) -> f64; /// Estimates the third quartile value from the data. /// /// # Remarks /// /// Returns `f64::NAN` if data is empty /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use statrs::statistics::Statistics; /// /// # fn main() { /// let mut x = []; /// assert!(x.lower_quartile().is_nan()); /// /// let mut y = [2.0, 1.0, 3.0, 4.0]; /// assert_almost_eq!(y.upper_quartile(), 3.5833333333333333, 1e-15); /// assert!(y != [2.0, 1.0, 3.0, 4.0]); /// # } /// ``` fn upper_quartile(&mut self) -> f64; /// Estimates the inter-quartile range from the data. /// /// # Remarks /// /// Returns `f64::NAN` if data is empty /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate statrs; /// /// use statrs::statistics::Statistics; /// /// # fn main() { /// let mut x = []; /// assert!(x.interquartile_range().is_nan()); /// /// let mut y = [2.0, 1.0, 3.0, 4.0]; /// assert_almost_eq!(y.interquartile_range(), 2.166666666666667, 1e-15); /// assert!(y != [2.0, 1.0, 3.0, 4.0]); /// # } /// ``` fn interquartile_range(&mut self) -> f64; /// Evaluates the rank of each entry of the data. /// /// # Examples /// /// ``` /// use statrs::statistics::{Statistics, RankTieBreaker}; /// /// let mut x = []; /// assert_eq!(x.ranks(RankTieBreaker::Average).len(), 0); /// /// let y = [1.0, 3.0, 2.0, 2.0]; /// assert_eq!((&mut y.clone()).ranks(RankTieBreaker::Average), [1.0, 4.0, 2.5, 2.5]); /// assert_eq!((&mut y.clone()).ranks(RankTieBreaker::Min), [1.0, 4.0, 2.0, 2.0]); /// ``` fn ranks(&mut self, tie_breaker: RankTieBreaker) -> Vec<f64>; }