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
use crate::evaluator::*;

/// Inter-percentile range
///
/// $$
/// \mathrm{inter-percetile range} \equiv Q(1 - p) - Q(p),
/// $$
/// where $Q(p)$ is the $p$th quantile of the magnitude distribution.
///
/// Special cases are [the interquartile range](https://en.wikipedia.org/wiki/Interquartile_range)
/// which is inter-percentile range for $p = 0.25$ and
/// [the interdecile range](https://en.wikipedia.org/wiki/Interdecile_range) which is
/// inter-percentile range for $p = 0.1$.
///
/// - Depends on: **magnitude**
/// - Minimum number of observations: **1**
/// - Number of features: **1**
#[derive(Clone, Debug)]
pub struct InterPercentileRange {
    quantile: f32,
    name: String,
    description: String,
}

lazy_info!(
    INTER_PERCENTILE_RANGE_INFO,
    size: 1,
    min_ts_length: 1,
    t_required: false,
    m_required: true,
    w_required: false,
    sorting_required: false,
);

impl InterPercentileRange {
    pub fn new(quantile: f32) -> Self {
        assert!(
            (quantile > 0.0) && (quantile < 0.5),
            "Quanitle should be in range (0.0, 0.5)"
        );
        Self {
            quantile,
            name: format!("inter_percentile_range_{:.0}", 100.0 * quantile),
            description: format!(
                "range between {:.3e}% and {:.3e}% magnitude percentiles",
                100.0 * quantile,
                100.0 * (1.0 - quantile)
            ),
        }
    }

    #[inline]
    pub fn default_quantile() -> f32 {
        0.25
    }
}

impl Default for InterPercentileRange {
    fn default() -> Self {
        Self::new(Self::default_quantile())
    }
}

impl<T> FeatureEvaluator<T> for InterPercentileRange
where
    T: Float,
{
    fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
        self.check_ts_length(ts)?;
        let ppf_low = ts.m.get_sorted().ppf(self.quantile);
        let ppf_high = ts.m.get_sorted().ppf(1.0 - self.quantile);
        let value = ppf_high - ppf_low;
        Ok(vec![value])
    }

    fn get_info(&self) -> &EvaluatorInfo {
        &INTER_PERCENTILE_RANGE_INFO
    }

    fn get_names(&self) -> Vec<&str> {
        vec![self.name.as_str()]
    }

    fn get_descriptions(&self) -> Vec<&str> {
        vec![self.description.as_str()]
    }
}

#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
    use super::*;
    use crate::tests::*;

    eval_info_test!(inter_percentile_range_info, InterPercentileRange::default());

    feature_test!(
        inter_percentile_range,
        [
            Box::new(InterPercentileRange::default()),
            Box::new(InterPercentileRange::new(0.25)), // should be the same
            Box::new(InterPercentileRange::new(0.1)),
        ],
        [50.0, 50.0, 80.0],
        linspace(0.0, 99.0, 100),
    );
}