light_curve_feature/features/
inter_percentile_range.rs1use crate::evaluator::*;
2
3macro_const! {
4 const DOC: &'static str = r#"
5Inter-percentile range
6
7$$
8Q(1 - p) - Q(p),
9$$
10where $Q(p)$ is the $p$th quantile of the magnitude distribution.
11
12Special cases are [the interquartile range](https://en.wikipedia.org/wiki/Interquartile_range)
13which is inter-percentile range for $p = 0.25$ and
14[the interdecile range](https://en.wikipedia.org/wiki/Interdecile_range) which is
15inter-percentile range for $p = 0.1$.
16
17- Depends on: **magnitude**
18- Minimum number of observations: **1**
19- Number of features: **1**
20"#;
21}
22
23#[doc = DOC!()]
24#[cfg_attr(test, derive(PartialEq))]
25#[derive(Clone, Debug, Serialize, Deserialize)]
26#[serde(
27 from = "InterPercentileRangeParameters",
28 into = "InterPercentileRangeParameters"
29)]
30pub struct InterPercentileRange {
31 quantile: f32,
32 name: String,
33 description: String,
34}
35
36lazy_info!(
37 INTER_PERCENTILE_RANGE_INFO,
38 InterPercentileRange,
39 size: 1,
40 min_ts_length: 1,
41 t_required: false,
42 m_required: true,
43 w_required: false,
44 sorting_required: false,
45);
46
47impl InterPercentileRange {
48 pub fn new(quantile: f32) -> Self {
49 assert!(
50 (quantile > 0.0) && (quantile < 0.5),
51 "Quantile should be in range (0.0, 0.5)"
52 );
53 Self {
54 quantile,
55 name: format!("inter_percentile_range_{:.0}", 100.0 * quantile),
56 description: format!(
57 "range between {:.3e}% and {:.3e}% magnitude percentiles",
58 100.0 * quantile,
59 100.0 * (1.0 - quantile)
60 ),
61 }
62 }
63
64 #[inline]
65 pub fn default_quantile() -> f32 {
66 0.25
67 }
68
69 pub const fn doc() -> &'static str {
70 DOC
71 }
72}
73
74impl Default for InterPercentileRange {
75 fn default() -> Self {
76 Self::new(Self::default_quantile())
77 }
78}
79
80impl FeatureNamesDescriptionsTrait for InterPercentileRange {
81 fn get_names(&self) -> Vec<&str> {
82 vec![self.name.as_str()]
83 }
84
85 fn get_descriptions(&self) -> Vec<&str> {
86 vec![self.description.as_str()]
87 }
88}
89
90impl<T> FeatureEvaluator<T> for InterPercentileRange
91where
92 T: Float,
93{
94 fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
95 self.check_ts_length(ts)?;
96 let ppf_low = ts.m.get_sorted().ppf(self.quantile);
97 let ppf_high = ts.m.get_sorted().ppf(1.0 - self.quantile);
98 let value = ppf_high - ppf_low;
99 Ok(vec![value])
100 }
101}
102
103#[derive(Serialize, Deserialize, JsonSchema)]
104#[serde(rename = "InterPercentileRange")]
105struct InterPercentileRangeParameters {
106 quantile: f32,
107}
108
109impl From<InterPercentileRange> for InterPercentileRangeParameters {
110 fn from(f: InterPercentileRange) -> Self {
111 Self {
112 quantile: f.quantile,
113 }
114 }
115}
116
117impl From<InterPercentileRangeParameters> for InterPercentileRange {
118 fn from(p: InterPercentileRangeParameters) -> Self {
119 Self::new(p.quantile)
120 }
121}
122
123impl JsonSchema for InterPercentileRange {
124 json_schema!(InterPercentileRangeParameters, false);
125}
126
127#[cfg(test)]
128#[allow(clippy::unreadable_literal)]
129#[allow(clippy::excessive_precision)]
130mod tests {
131 use super::*;
132 use crate::tests::*;
133
134 use serde_test::{Token, assert_tokens};
135
136 check_feature!(InterPercentileRange);
137
138 feature_test!(
139 inter_percentile_range,
140 [
141 InterPercentileRange::default(),
142 InterPercentileRange::new(0.25), InterPercentileRange::new(0.1),
144 ],
145 [50.0, 50.0, 80.0],
146 linspace(0.0, 99.0, 100),
147 );
148
149 #[test]
150 fn serialization() {
151 const QUANTILE: f32 = 0.256;
152 let beyond_n_std = InterPercentileRange::new(QUANTILE);
153 assert_tokens(
154 &beyond_n_std,
155 &[
156 Token::Struct {
157 len: 1,
158 name: "InterPercentileRange",
159 },
160 Token::String("quantile"),
161 Token::F32(QUANTILE),
162 Token::StructEnd,
163 ],
164 )
165 }
166}