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
use crate::evaluator::*;
use ndarray::Zip;
macro_const! {
const DOC: &str = r#"
Stetson $K$ coefficient described light curve shape
$$
\mathrm{Stetson}~K \equiv \frac{\sum_i\left|\frac{m_i - \bar{m}}{\delta_i}\right|}{\sqrt{N\\,\chi^2}},
$$
where N is the number of observations,
$\bar{m}$ is the weighted mean magnitude
and $\chi^2 = \sum_i\left(\frac{m_i - \langle m \rangle}{\delta\_i}\right)^2$.
- Depends on: **magnitude**, **magnitude error**
- Minimum number of observations: **2**
- Number of features: **1**
P. B. Statson, 1996. [DOI:10.1086/133808](https://doi.org/10.1086/133808)
"#;
}
#[doc = DOC!()]
#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
pub struct StetsonK {}
lazy_info!(
STETSON_K_INFO,
size: 1,
min_ts_length: 2,
t_required: false,
m_required: true,
w_required: true,
sorting_required: false,
);
impl StetsonK {
pub fn new() -> Self {
Self {}
}
pub fn doc() -> &'static str {
DOC
}
}
impl<T> FeatureEvaluator<T> for StetsonK
where
T: Float,
{
fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
self.check_ts_length(ts)?;
let chi2 = get_nonzero_reduced_chi2(ts)? * (ts.lenf() - T::one());
let mean = ts.get_m_weighted_mean();
let value = Zip::from(&ts.m.sample)
.and(&ts.w.sample)
.fold(T::zero(), |acc, &y, &w| acc + T::abs(y - mean) * T::sqrt(w))
/ T::sqrt(ts.lenf() * chi2);
Ok(vec![value])
}
fn get_info(&self) -> &EvaluatorInfo {
&STETSON_K_INFO
}
fn get_names(&self) -> Vec<&str> {
vec!["stetson_K"]
}
fn get_descriptions(&self) -> Vec<&str> {
vec!["normalized weighted deviation of magnitude from its weighted mean"]
}
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
use super::*;
use crate::tests::*;
use std::f64::consts::*;
check_feature!(StetsonK);
feature_test!(
stetson_k_square_wave,
[StetsonK::new()],
[1.0],
[1.0; 1000],
(0..1000)
.map(|i| {
if i < 500 {
1.0
} else {
-1.0
}
})
.collect::<Vec<_>>(),
[1.0; 1000],
);
feature_test!(
stetson_k_sinus,
[StetsonK::new()],
[8_f64.sqrt() / PI],
[1.0; 1000],
linspace(0.0, 2.0 * PI, 1000)
.iter()
.map(|&x| f64::sin(x))
.collect::<Vec<_>>(),
[1.0; 1000],
1e-3,
);
feature_test!(
stetson_k_sawtooth,
[StetsonK::new()],
[12_f64.sqrt() / 4.0],
[1.0; 1000],
linspace(0.0, 1.0, 1000),
);
feature_test!(
stetson_k_single_peak,
[StetsonK::new()],
[2.0 * 99.0_f64.sqrt() / 100.0],
[1.0; 100],
(0..100)
.map(|i| {
if i == 0 {
1.0
} else {
-1.0
}
})
.collect::<Vec<_>>(),
);
#[test]
fn stetson_k_plateau() {
let eval = StetsonK::new();
let x = [0.0; 10];
let mut ts = TimeSeries::new_without_weight(&x, &x);
assert_eq!(eval.eval(&mut ts), Err(EvaluatorError::FlatTimeSeries));
}
}