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
use super::RankTieBreaker;

/// The `OrderStatistics` trait provides statistical utilities
/// having to do with ordering. All the algorithms are in-place thus requiring
/// a mutable borrow.
pub trait OrderStatistics<T> {
    /// 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::OrderStatistics;
    ///
    /// 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) -> T;

    /// Returns the median value from the data
    ///
    /// # Remarks
    ///
    /// Returns `f64::NAN` if data is empty
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::statistics::OrderStatistics;
    ///
    /// 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) -> T;

    /// 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::OrderStatistics;
    ///
    /// 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) -> T;

    /// 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::OrderStatistics;
    ///
    /// 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) -> T;

    /// 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::OrderStatistics;
    ///
    /// # 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) -> T;

    /// 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::OrderStatistics;
    ///
    /// # fn main() {
    /// let mut x = [];
    /// assert!(x.upper_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) -> T;

    /// 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::OrderStatistics;
    ///
    /// # 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) -> T;

    /// Evaluates the rank of each entry of the data.
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::statistics::{OrderStatistics, 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<T>;
}