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
//! # A histogram consisting of a fixed number of intervals within a fixed range

use std::{cmp, fmt, ops::Deref};

use num::traits::{
    cast::{FromPrimitive, ToPrimitive},
    NumAssign, NumOps,
};

use crate::traits::{Collecting, ToIterator};

/// The Histogram consists of `num_intervals` intervals between the `min` and the `max` value.
pub struct Histogram<T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display, {
    boundaries: Vec<T>,
    counters: Vec<usize>,
    num_less_than_min: usize,
    num_larger_than_max: usize,
}

impl<T> Histogram<T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display,
{
    /// # Initializing with Known Boundaries
    /// Creates a Histogram consisting of `num_intervals` intervals between the values `min` and
    /// `max`, and inserts the values from the vector of `elements` into the histogram.
    /// Values smaller than `min` will increment the counter `num_less_than_min`,
    /// while values larger than `max` will increment the counter `num_larger_than_max`
    ///
    /// # Example
    /// ```
    /// use analytic::histogram::Histogram;
    ///
    /// let histogram = Histogram::new(&vec![2, -1, 3, 5, 8], 5, 0, 10).unwrap();
    /// assert_eq!(histogram.get_boundaries().len(), 6);
    /// assert_eq!(histogram.get_num_less_than_min(), 1);
    /// assert_eq!(histogram.get_num_larger_than_max(), 0);
    /// ```
    pub fn new<'a>(
        elements: &'a Vec<T>,
        num_intervals: usize,
        min: T,
        max: T,
    ) -> Result<Histogram<T>, String>
    where
        &'a T: Deref, {
        if num_intervals == 0 {
            return Err(format!(
                "num_intervals should be positive, received {}",
                num_intervals
            ));
        }
        if max < min {
            return Err(format!("max ({}) has to be >= min ({})", max, min));
        }
        let n = match T::from_usize(num_intervals) {
            Some(n) => n,
            None => {
                return Err(format!(
                    "failed to convert num_intervals: usize ({}) to type T",
                    num_intervals
                ))
            }
        };
        let delta = (max - min) / n;
        if delta <= T::zero() {
            return Err(format!(
                "cannot create positive interval legnths for the given \
                min({}) max({}) and num_intervals({})",
                min, max, num_intervals
            ));
        }

        let mut boundaries = vec![min];
        let mut acc = min;
        for _ in 1..num_intervals {
            acc += delta;
            boundaries.push(acc);
        }
        boundaries.push(max);

        let mut num_larger_than_max = 0;
        let mut num_less_than_min = 0;
        let mut counters = vec![0usize; num_intervals];
        for a in elements.iter() {
            if *a < min {
                num_less_than_min += 1;
            } else if *a > max {
                num_larger_than_max += 1;
            } else {
                let i = match ((*a - min) / delta).to_usize() {
                    Some(i) => i,
                    None => {
                        return Err(format!(
                            "failed to convert {} to an usize index",
                            (*a - min) / delta
                        ))
                    }
                };
                counters[cmp::min(i, num_intervals - 1)] += 1;
            }
        }
        Ok(Histogram {
            boundaries,
            counters,
            num_less_than_min,
            num_larger_than_max,
        })
    }

    #[inline]
    pub fn num_intervals(&self) -> usize {
        self.boundaries.len() - 1
    }

    pub fn get_ratios(&self) -> Vec<f64> {
        let mut ratio_distribution = vec![0f64; self.num_intervals()];
        let total = self.counters.iter().sum::<usize>() as f64;
        for (i, ratio) in ratio_distribution.iter_mut().enumerate() {
            *ratio = self.counters[i] as f64 / total;
        }
        ratio_distribution
    }

    pub fn new_with_auto_range<'a>(
        elements: &'a Vec<T>,
        num_intervals: usize,
    ) -> Result<Histogram<T>, String>
    where
        &'a T: Deref,
        T: Ord, {
        let min = match elements.iter().min() {
            None => {
                return Err(format!(
                    "failed to extract the min elements when range is set to auto"
                ))
            }
            Some(min) => min,
        };
        let max = match elements.iter().max() {
            None => {
                return Err(format!(
                    "failed to extract the max elements when range is set to auto"
                ))
            }
            Some(max) => max,
        };
        Histogram::new(elements, num_intervals, *min, *max)
    }

    #[inline]
    pub fn get_boundaries(&self) -> &Vec<T> {
        &self.boundaries
    }

    #[inline]
    pub fn get_counters(&self) -> &Vec<usize> {
        &self.counters
    }

    pub fn get_num_less_than_min(&self) -> usize {
        self.num_less_than_min
    }

    pub fn get_num_larger_than_max(&self) -> usize {
        self.num_larger_than_max
    }

    #[inline]
    pub fn min_boundary(&self) -> T {
        *self.boundaries.first().unwrap()
    }

    #[inline]
    pub fn max_boundary(&self) -> T {
        *self.boundaries.last().unwrap()
    }
}

impl<T> Collecting<T> for Histogram<T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display,
{
    fn collect(&mut self, item: T) {
        let delta = self.boundaries[1] - self.boundaries[0];
        let num_intervals = self.num_intervals();
        let min_boundary = self.min_boundary();
        if item < min_boundary {
            self.num_less_than_min += 1;
        } else if item > self.max_boundary() {
            self.num_larger_than_max += 1;
        } else {
            let i = ((item - min_boundary) / delta).to_usize().unwrap();
            self.counters[cmp::min(i, num_intervals - 1)] += 1;
        }
    }
}

impl<T> fmt::Display for Histogram<T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let ratios = self.get_ratios();
        let mut cum = 0usize;
        let mut ratio_cum = 0f64;
        let mut reverse_ratio_cum = 1f64;
        let last_i = self.num_intervals() - 1;
        writeln!(
            f,
            "{:>11.2}  {:>12.2} {:>16} {:>16} {:>16} {:>16} {:>16}",
            "", "", "count", "cum_count", "ratio", "cum_ratio", "rev_cum_ratio"
        )?;
        for i in 0..last_i {
            cum += self.counters[i];
            ratio_cum += ratios[i];
            writeln!(
                f,
                "[{:>10.2}, {:>10.2}): {:>16.2} {:>16.2} {:>16.4} {:>16.4} {:>16.4}",
                self.boundaries[i],
                self.boundaries[i + 1],
                self.counters[i],
                cum,
                ratios[i],
                ratio_cum,
                reverse_ratio_cum
            )?;
            reverse_ratio_cum -= ratios[i];
        }
        cum += self.counters[last_i];
        ratio_cum += ratios[last_i];
        writeln!(
            f,
            "[{:>10.2}, {:>10.2}]: {:>16.2} {:>16.2} {:>16.4} {:>16.4} {:>16.4}",
            self.boundaries[last_i],
            self.boundaries[self.num_intervals()],
            self.counters[last_i],
            cum,
            ratios[last_i],
            ratio_cum,
            reverse_ratio_cum
        )?;
        writeln!(
            f,
            "\n\
            ({:>10}, {:>10.2}) count: {}\n\
            ({:>10.2}, {:>10}) count: {}",
            "-inf",
            self.min_boundary(),
            self.num_less_than_min,
            self.max_boundary(),
            "inf",
            self.num_larger_than_max,
        )?;
        Ok(())
    }
}

pub type HistogramEntry<T> = (T, T, usize);

impl<'a, T> ToIterator<'a, HistogramIter<'a, T>, HistogramEntry<T>> for Histogram<T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display,
{
    fn to_iter(&'a self) -> HistogramIter<'a, T> {
        HistogramIter {
            histogram: &self,
            cursor: 0,
        }
    }
}

/// An iterator that iterates through the entries of the histogram
/// ```
/// use analytic::{histogram::Histogram, traits::ToIterator};
/// let histogram = Histogram::new(&vec![4., 0., 3.5], 2, 0., 7.).unwrap();
/// let mut iter = histogram.to_iter();
/// assert_eq!(Some((0., 3.5, 1)), iter.next());
/// assert_eq!(Some((3.5, 7., 2)), iter.next());
/// assert_eq!(None, iter.next());
/// ```
pub struct HistogramIter<'a, T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display, {
    histogram: &'a Histogram<T>,
    cursor: usize,
}

impl<'a, T> Iterator for HistogramIter<'a, T>
where
    T: PartialOrd + NumAssign + NumOps + FromPrimitive + ToPrimitive + Copy + fmt::Display,
{
    type Item = HistogramEntry<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let i = self.cursor;
        if i >= self.histogram.num_intervals() {
            None
        } else {
            self.cursor += 1;
            Some((
                self.histogram.boundaries[i],
                self.histogram.boundaries[i + 1],
                self.histogram.counters[i],
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::traits::{Collecting, ToIterator};

    use super::Histogram;

    #[test]
    fn test_histogram() {
        let elements = vec![4., 0., 3.5];
        let num_intervals = 2;
        let mut histogram = match Histogram::new(&elements, num_intervals, 0., 7.) {
            Ok(h) => h,
            Err(why) => {
                eprintln!("{}", why);
                assert!(false, why);
                return;
            }
        };
        histogram.collect(4.);
        let mut iter = histogram.to_iter();
        assert_eq!(Some((0., 3.5, 1)), iter.next());
        assert_eq!(Some((3.5, 7., 3)), iter.next());
        assert_eq!(None, iter.next());
        assert_eq!(num_intervals + 1, histogram.boundaries.len());
        assert_eq!(num_intervals, histogram.counters.len());
        assert_eq!(histogram.counters[0], 1);
        assert_eq!(histogram.counters[1], 3);
    }
}