light_curve_feature/features/
cusum.rs1use crate::evaluator::*;
2
3macro_const! {
4 const DOC: &str = r"
5Cusum — a range of cumulative sums
6
7$$
8\mathrm{cusum} \equiv \max(S) - \min(S),
9$$
10where
11$$
12S_j \equiv \frac1{N\sigma_m} \sum_{i=0}^j{\left(m\_i - \langle m \rangle\right)},
13$$
14$N$ is the number of observations,
15$\langle m \rangle$ is the mean magnitude
16and $\sigma_m = \sqrt{\sum_i (m_i - \langle m \rangle)^2 / (N-1)}$ is the magnitude standard deviation.
17
18- Depends on: **magnitude**
19- Minimum number of observations: **2**
20- Number of features: **1**
21
22Kim et al. 2014, [DOI:10.1051/0004-6361/201323252](https://doi.org/10.1051/0004-6361/201323252)
23";
24}
25
26#[doc = DOC!()]
27#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
28pub struct Cusum {}
29
30impl Cusum {
31 pub fn new() -> Self {
32 Self {}
33 }
34
35 pub const fn doc() -> &'static str {
36 DOC
37 }
38}
39
40lazy_info!(
41 CUSUM_INFO,
42 Cusum,
43 size: 1,
44 min_ts_length: 2,
45 t_required: false,
46 m_required: true,
47 w_required: false,
48 sorting_required: true,
49);
50
51impl FeatureNamesDescriptionsTrait for Cusum {
52 fn get_names(&self) -> Vec<&str> {
53 vec!["cusum"]
54 }
55
56 fn get_descriptions(&self) -> Vec<&str> {
57 vec!["range of cumulative sums of magnitudes"]
58 }
59}
60
61impl<T> FeatureEvaluator<T> for Cusum
62where
63 T: Float,
64{
65 fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
66 self.check_ts_length(ts)?;
67 let m_std = get_nonzero_m_std(ts)?;
68 let m_mean = ts.m.get_mean();
69 let (_last_cusum, min_cusum, max_cusum) = ts.m.as_slice().iter().fold(
70 (T::zero(), T::infinity(), -T::infinity()),
71 |(mut cusum, min_cusum, max_cusum), &m| {
72 cusum += m - m_mean;
73 (cusum, T::min(min_cusum, cusum), T::max(max_cusum, cusum))
74 },
75 );
76 Ok(vec![(max_cusum - min_cusum) / (m_std * ts.lenf())])
77 }
78}
79
80#[cfg(test)]
81#[allow(clippy::unreadable_literal)]
82#[allow(clippy::excessive_precision)]
83mod tests {
84 use super::*;
85 use crate::tests::*;
86
87 check_feature!(Cusum);
88
89 feature_test!(
90 cumsum,
91 [Cusum::new()],
92 [0.3589213],
93 [1.0_f32, 1.0, 1.0, 5.0, 8.0, 20.0],
94 );
95}