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
use conv::prelude::*;

use crate::float_trait::Float;

pub trait Statistics<T>
where
    T: Float,
{
    fn sorted(&self) -> Vec<T>;
    fn minimum(&self) -> T;
    fn maximum(&self) -> T;
    fn min_from_sorted(&self) -> T;
    fn max_from_sorted(&self) -> T;
    fn mean(&self) -> T;
    fn median(&self) -> T;
    fn median_from_sorted(&self) -> T;
    fn ppf(&self, q: f32) -> T;
    fn ppf_from_sorted(&self, q: f32) -> T;
    fn ppf_many(&self, q: &[f32]) -> Vec<T>;
    fn ppf_many_from_sorted(&self, q: &[f32]) -> Vec<T>;
    fn peak_indices(&self) -> Vec<usize>;
    fn peak_indices_reverse_sorted(&self) -> Vec<usize>;
}

impl<T> Statistics<T> for [T]
where
    T: Float,
{
    fn sorted(&self) -> Vec<T> {
        let mut v = self.to_vec();
        // Replace with partition_at_index when it will be available
        v[..].sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        v
    }

    fn minimum(&self) -> T {
        *self
            .iter()
            .min_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap()
    }

    fn maximum(&self) -> T {
        *self
            .iter()
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap()
    }

    fn min_from_sorted(&self) -> T {
        self[0]
    }

    fn max_from_sorted(&self) -> T {
        self[self.len() - 1]
    }

    fn mean(&self) -> T {
        self.iter().cloned().sum::<T>() / self.len().value_as::<T>().unwrap()
    }

    fn median(&self) -> T {
        // Replace with partition_at_index when it will be available
        self.sorted().median_from_sorted()
    }

    fn median_from_sorted(&self) -> T {
        let i = (self.len() - 1) / 2;
        if self.len() % 2 == 0 {
            T::half() * (self[i] + self[i + 1])
        } else {
            self[i]
        }
    }

    fn ppf(&self, q: f32) -> T {
        // Replace with partition_at_index when it will be available
        self.sorted().ppf_from_sorted(q)
    }

    fn ppf_from_sorted(&self, q: f32) -> T {
        // R-5 from https://en.wikipedia.org/wiki/Quantile
        assert!(
            (q >= 0.0) && (q <= 1.0),
            "quantile should be between zero and unity"
        );
        let h = (self.len() as f32) * q - 0.5;
        let h_floor = h.floor();
        if h_floor < 0.0 {
            self[0]
        } else {
            let i = h_floor as usize;
            if i >= self.len() - 1 {
                self[self.len() - 1]
            } else {
                self[i] + (h - h_floor).value_as::<T>().unwrap() * (self[i + 1] - self[i])
            }
        }
    }

    fn ppf_many(&self, q: &[f32]) -> Vec<T> {
        self.sorted()[..].ppf_many_from_sorted(q)
    }

    fn ppf_many_from_sorted(&self, q: &[f32]) -> Vec<T> {
        q.iter().map(|&x| self.ppf_from_sorted(x)).collect()
    }

    /// Indices of local maxima, edge points are never included
    fn peak_indices(&self) -> Vec<usize> {
        self.iter()
            .enumerate()
            .fold(
                (vec![], T::infinity(), false),
                |(mut v, prev_x, prev_is_rising), (i, &x)| {
                    let is_rising = x > prev_x;
                    if prev_is_rising && (!is_rising) {
                        v.push(i - 1)
                    }
                    (v, x, is_rising)
                },
            )
            .0
    }

    fn peak_indices_reverse_sorted(&self) -> Vec<usize> {
        let mut v = self.peak_indices();
        v[..].sort_unstable_by(|&b, &a| self[a].partial_cmp(&self[b]).unwrap());
        v
    }
}

#[cfg(test)]
mod tests {
    use rand;

    use super::*;

    use light_curve_common::linspace;

    #[test]
    fn median_is_ppf_half() {
        for i in 0..10 {
            let a: Vec<f32> = (0..100 + i).map(|_| rand::random()).collect();
            assert_eq!(a[..].median(), a[..].ppf(0.5));
        }
    }

    #[test]
    fn minimum_is_ppf_zero() {
        for i in 0..10 {
            let a: Vec<f32> = (0..100 + i).map(|_| rand::random()).collect();
            assert_eq!(a[..].minimum(), a[..].ppf(0.0));
        }
    }

    #[test]
    fn maximum_is_ppf_unity() {
        for i in 0..10 {
            let a: Vec<f32> = (0..100 + i).map(|_| rand::random()).collect();
            assert_eq!(a[..].maximum(), a[..].ppf(1.0));
        }
    }

    macro_rules! peak_indices {
        ($name: ident, $desired: expr, $x: expr $(,)?) => {
            #[test]
            fn $name() {
                assert_eq!($x.peak_indices_reverse_sorted(), $desired);
            }
        };
    }

    peak_indices!(
        peak_indices_three_points_peak,
        [1_usize],
        [0.0_f32, 1.0, 0.0]
    );
    peak_indices!(
        peak_indices_three_points_plateau,
        [] as [usize; 0],
        [0.0_f32, 0.0, 0.0]
    );
    peak_indices!(
        peak_indices_three_points_dip,
        [] as [usize; 0],
        [0.0_f32, -1.0, 0.0]
    );
    peak_indices!(peak_indices_long_plateau, [] as [usize; 0], [0.0_f32; 100]);
    peak_indices!(
        peak_indices_sawtooth,
        (1..=99) // the first and the last point cannot be peak
            .filter(|i| i % 2 == 0)
            .collect::<Vec<_>>(),
        (0..=100)
            .map(|i| {
                if i % 2 == 0 {
                    1.0_f32
                } else {
                    0.0_f32
                }
            })
            .collect::<Vec<_>>(),
    );
    peak_indices!(
        peak_indices_one_peak,
        [50],
        linspace(-5.0_f32, 5.0, 101)
            .iter()
            .map(|&x| f32::exp(-0.5 * x * x))
            .collect::<Vec<_>>(),
    );
}