light_curve_feature/features/
eta.rs

1use crate::evaluator::*;
2use itertools::Itertools;
3
4macro_const! {
5    const DOC: &str = r"
6Von Neummann $\eta$
7
8$$
9\eta \equiv \frac1{(N - 1)\\,\sigma_m^2} \sum_{i=0}^{N-2}(m_{i+1} - m_i)^2,
10$$
11where $N$ is the number of observations,
12$\sigma_m = \sqrt{\sum_i (m_i - \langle m \rangle)^2 / (N-1)}$ is the magnitude standard deviation.
13
14- Depends on: **magnitude**
15- Minimum number of observations: **2**
16- Number of features: **1**
17
18Kim et al. 2014, [DOI:10.1051/0004-6361/201323252](https://doi.org/10.1051/0004-6361/201323252)
19";
20}
21
22#[doc = DOC!()]
23#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
24pub struct Eta {}
25
26impl Eta {
27    pub fn new() -> Self {
28        Self {}
29    }
30
31    pub const fn doc() -> &'static str {
32        DOC
33    }
34}
35
36lazy_info!(
37    ETA_INFO,
38    Eta,
39    size: 1,
40    min_ts_length: 2,
41    t_required: false,
42    m_required: true,
43    w_required: false,
44    sorting_required: true,
45);
46
47impl FeatureNamesDescriptionsTrait for Eta {
48    fn get_names(&self) -> Vec<&str> {
49        vec!["eta"]
50    }
51
52    fn get_descriptions(&self) -> Vec<&str> {
53        vec!["Von Neummann eta-coefficient for magnitude sample"]
54    }
55}
56
57impl<T> FeatureEvaluator<T> for Eta
58where
59    T: Float,
60{
61    fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
62        self.check_ts_length(ts)?;
63        let m_std2 = get_nonzero_m_std2(ts)?;
64        let value =
65            ts.m.as_slice()
66                .iter()
67                .tuple_windows()
68                .map(|(&a, &b)| (b - a).powi(2))
69                .sum::<T>()
70                / (ts.lenf() - T::one())
71                / m_std2;
72        Ok(vec![value])
73    }
74}
75
76#[cfg(test)]
77#[allow(clippy::unreadable_literal)]
78#[allow(clippy::excessive_precision)]
79mod tests {
80    use super::*;
81    use crate::tests::*;
82
83    check_feature!(Eta);
84
85    feature_test!(
86        eta,
87        [Eta::new()],
88        [1.11338],
89        [1.0_f32, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 109.0],
90    );
91}