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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use tea_core::prelude::*;
use tea_dyn::{DynVec1, IntoDyn};
use crate::*;
pub trait RollingFeatureDyn: DynVec1 + Sized {
/// Calculates the rolling mean of valid elements within a window.
///
/// # Arguments
///
/// * `window` - The size of the rolling window.
/// * `min_periods` - The minimum number of observations in window required to have a value.
/// * `out` - Optional output buffer.
///
/// # Returns
///
/// A vector containing the rolling means.
fn ts_mean(&self, window: usize, min_periods: Option<usize>) -> TResult<Self>
where
f64: Cast<Self::F64Item> + Cast<Self::F32Item> + Cast<Self::I64Item> + Cast<Self::I32Item>,
{
let name = self.get_name();
let res = match self.get_dtype() {
DataType::F64 => self
.extract_f64()
.unwrap()
.ts_vmean::<Self::F64Vec, _>(window, min_periods)
.into_dyn(),
DataType::F32 => self
.extract_f32()
.unwrap()
.ts_vmean::<Self::F32Vec, _>(window, min_periods)
.into_dyn(),
DataType::I64 => self
.extract_i64()
.unwrap()
.ts_vmean::<Self::I64Vec, _>(window, min_periods)
.into_dyn(),
DataType::I32 => self
.extract_i32()
.unwrap()
.ts_vmean::<Self::I32Vec, _>(window, min_periods)
.into_dyn(),
dtype => tbail!("Unsupported dtype: {:?} in ts_mean", dtype),
};
if let Some(name) = name {
Ok(res.with_name(name))
} else {
Ok(res)
}
}
/// Calculates the exponentially weighted moving average.
///
/// # Arguments
/// * `window` - The size of the moving window.
/// * `min_periods` - The minimum number of observations in window required to have a value.
///
/// # Returns
/// A new Series with the calculated values.
fn ts_ewm(&self, window: usize, min_periods: Option<usize>) -> TResult<Self>
where
f64: Cast<Self::F64Item> + Cast<Self::F32Item> + Cast<Self::I64Item> + Cast<Self::I32Item>,
{
let name = self.get_name();
let res = match self.get_dtype() {
DataType::F64 => self
.extract_f64()
.unwrap()
.ts_vewm::<Self::F64Vec, _>(window, min_periods)
.into_dyn(),
DataType::F32 => self
.extract_f32()
.unwrap()
.ts_vewm::<Self::F32Vec, _>(window, min_periods)
.into_dyn(),
DataType::I64 => self
.extract_i64()
.unwrap()
.ts_vewm::<Self::I64Vec, _>(window, min_periods)
.into_dyn(),
DataType::I32 => self
.extract_i32()
.unwrap()
.ts_vewm::<Self::I32Vec, _>(window, min_periods)
.into_dyn(),
dtype => tbail!("Unsupported dtype: {:?} in ts_ewm", dtype),
};
if let Some(name) = name {
Ok(res.with_name(name))
} else {
Ok(res)
}
}
/// Calculates the rolling standard deviation of valid elements within a window.
///
/// # Arguments
///
/// * `window` - The size of the rolling window.
/// * `min_periods` - The minimum number of observations in window required to have a value.
///
/// # Returns
///
/// A vector containing the rolling standard deviations.
fn ts_std(&self, window: usize, min_periods: Option<usize>) -> TResult<Self>
where
f64: Cast<Self::F64Item> + Cast<Self::F32Item> + Cast<Self::I64Item> + Cast<Self::I32Item>,
{
let name = self.get_name();
let res = match self.get_dtype() {
DataType::F64 => self
.extract_f64()
.unwrap()
.ts_vstd::<Self::F64Vec, _>(window, min_periods)
.into_dyn(),
DataType::F32 => self
.extract_f32()
.unwrap()
.ts_vstd::<Self::F32Vec, _>(window, min_periods)
.into_dyn(),
DataType::I64 => self
.extract_i64()
.unwrap()
.ts_vstd::<Self::I64Vec, _>(window, min_periods)
.into_dyn(),
DataType::I32 => self
.extract_i32()
.unwrap()
.ts_vstd::<Self::I32Vec, _>(window, min_periods)
.into_dyn(),
dtype => tbail!("Unsupported dtype: {:?} in ts_std", dtype),
};
if let Some(name) = name {
Ok(res.with_name(name))
} else {
Ok(res)
}
}
/// Calculates the rolling skewness for valid elements within a window.
///
/// # Arguments
///
/// * `window` - The size of the rolling window.
/// * `min_periods` - The minimum number of observations in window required to have a value.
///
/// # Returns
///
/// A new Series with the calculated rolling skewness values.
fn ts_skew(&self, window: usize, min_periods: Option<usize>) -> TResult<Self>
where
f64: Cast<Self::F64Item> + Cast<Self::F32Item> + Cast<Self::I64Item> + Cast<Self::I32Item>,
{
let name = self.get_name();
let res = match self.get_dtype() {
DataType::F64 => self
.extract_f64()
.unwrap()
.ts_vskew::<Self::F64Vec, _>(window, min_periods)
.into_dyn(),
DataType::F32 => self
.extract_f32()
.unwrap()
.ts_vskew::<Self::F32Vec, _>(window, min_periods)
.into_dyn(),
DataType::I64 => self
.extract_i64()
.unwrap()
.ts_vskew::<Self::I64Vec, _>(window, min_periods)
.into_dyn(),
DataType::I32 => self
.extract_i32()
.unwrap()
.ts_vskew::<Self::I32Vec, _>(window, min_periods)
.into_dyn(),
dtype => tbail!("Unsupported dtype: {:?} in ts_skew", dtype),
};
if let Some(name) = name {
Ok(res.with_name(name))
} else {
Ok(res)
}
}
/// Calculates the rolling kurtosis for valid elements within a window.
///
/// # Arguments
///
/// * `window` - The size of the rolling window.
/// * `min_periods` - The minimum number of observations in window required to have a value.
///
/// # Returns
///
/// A new Series with the calculated rolling kurtosis values.
fn ts_kurt(&self, window: usize, min_periods: Option<usize>) -> TResult<Self>
where
f64: Cast<Self::F64Item> + Cast<Self::F32Item> + Cast<Self::I64Item> + Cast<Self::I32Item>,
{
let name = self.get_name();
let res = match self.get_dtype() {
DataType::F64 => self
.extract_f64()
.unwrap()
.ts_vkurt::<Self::F64Vec, _>(window, min_periods)
.into_dyn(),
DataType::F32 => self
.extract_f32()
.unwrap()
.ts_vkurt::<Self::F32Vec, _>(window, min_periods)
.into_dyn(),
DataType::I64 => self
.extract_i64()
.unwrap()
.ts_vkurt::<Self::I64Vec, _>(window, min_periods)
.into_dyn(),
DataType::I32 => self
.extract_i32()
.unwrap()
.ts_vkurt::<Self::I32Vec, _>(window, min_periods)
.into_dyn(),
dtype => tbail!("Unsupported dtype: {:?} in ts_kurt", dtype),
};
if let Some(name) = name {
Ok(res.with_name(name))
} else {
Ok(res)
}
}
}
impl<S: DynVec1> RollingFeatureDyn for S {}
#[cfg(test)]
mod tests {
use tea_deps::polars::prelude::*;
use super::*;
#[test]
fn test_ts_mean() {
let series = Series::from_vec("abc".into(), vec![1.0, 2.0, 3.0, 4.0, 5.0]);
let result = series.ts_mean(3, None).unwrap();
let expected = Series::from_vec("abc".into(), vec![1., 1.5, 2.0, 3.0, 4.0]);
assert_eq!(result, expected);
// Test with min_periods
let result_min_periods = series.ts_mean(3, Some(3)).unwrap();
let expected_min_periods: Float64Chunked =
vec![None, None, Some(2.0), Some(3.0), Some(4.0)].collect_trusted_vec1();
assert_eq!(result_min_periods, expected_min_periods.into_dyn());
}
}