use alloc::vec;
use alloc::vec::Vec;
mod mstl;
mod period;
pub use mstl::{MSTL_ITERATIONS, MultiDecomposition, mstl};
pub use period::{PeriodCandidate, find_period};
pub const SEASONAL_STRENGTH_THRESHOLD: f64 = 0.64;
#[derive(Debug, Clone, PartialEq)]
pub struct Decomposition {
pub seasonal: Vec<f64>,
pub trend: Vec<f64>,
pub remainder: Vec<f64>,
}
impl Decomposition {
#[must_use]
pub fn seasonal_strength(&self) -> f64 {
let detrended: Vec<f64> = self
.seasonal
.iter()
.zip(&self.remainder)
.map(|(s, r)| s + r)
.collect();
let total = variance(&detrended);
if total == 0.0 {
return 0.0;
}
(1.0 - variance(&self.remainder) / total).max(0.0)
}
}
#[must_use]
pub fn stl(values: &[f64], period: usize) -> Option<Decomposition> {
stl_with(values, period, DEFAULT_SEASONAL_WINDOW)
}
#[must_use]
pub fn stl_with(values: &[f64], period: usize, seasonal_window: usize) -> Option<Decomposition> {
let n = values.len();
let window_ok = seasonal_window >= 3 && seasonal_window % 2 == 1;
if !window_ok || period < 2 || n < 2 * period || !crate::math::all_finite(values) {
return None;
}
if crate::math::is_constant(values) {
return Some(Decomposition {
seasonal: vec![0.0; n],
trend: values.to_vec(),
remainder: vec![0.0; n],
});
}
let windows = Windows::new(period, seasonal_window);
let mut seasonal = vec![0.0; n];
let mut trend = vec![0.0; n];
for _ in 0..INNER_PASSES {
inner_pass(values, period, &windows, &mut seasonal, &mut trend);
}
let remainder = (0..n).map(|i| values[i] - seasonal[i] - trend[i]).collect();
Some(Decomposition {
seasonal,
trend,
remainder,
})
}
const DEFAULT_SEASONAL_WINDOW: usize = 7;
const INNER_PASSES: usize = 5;
struct Windows {
seasonal: usize,
trend: usize,
low_pass: usize,
}
impl Windows {
fn new(period: usize, seasonal: usize) -> Self {
let trend_minimum = 1.5 * period as f64 / (1.0 - 1.5 / seasonal as f64);
let next_odd_above = |x: f64| {
let mut candidate = x as usize + 1;
if candidate % 2 == 0 {
candidate += 1;
}
candidate
};
let truncated = trend_minimum as usize;
let mut trend = truncated + usize::from(trend_minimum > truncated as f64);
if trend % 2 == 0 {
trend += 1;
}
Self {
seasonal,
trend,
low_pass: next_odd_above(period as f64),
}
}
}
fn inner_pass(
y: &[f64],
period: usize,
windows: &Windows,
seasonal: &mut [f64],
trend: &mut [f64],
) {
let n = y.len();
let detrended: Vec<f64> = y.iter().zip(trend.iter()).map(|(y, t)| y - t).collect();
let cycle = cycle_subseries(&detrended, period, windows.seasonal);
let low_pass_input = low_pass_averages(&cycle, period);
let low_pass = loess_smooth(&low_pass_input, windows.low_pass);
for i in 0..n {
seasonal[i] = cycle[period + i] - low_pass[i];
}
let deseasonalized: Vec<f64> = y.iter().zip(seasonal.iter()).map(|(y, s)| y - s).collect();
trend.copy_from_slice(&loess_smooth(&deseasonalized, windows.trend));
}
fn cycle_subseries(y: &[f64], period: usize, window: usize) -> Vec<f64> {
let n = y.len();
let mut out = vec![0.0; n + 2 * period];
let mut weights = Vec::new();
for j in 0..period {
let sub: Vec<f64> = y.iter().skip(j).step_by(period).copied().collect();
let k = sub.len();
let smooth = loess_smooth(&sub, window);
let before =
loess_estimate(&sub, window, 0.0, 1, window.min(k), &mut weights).unwrap_or(smooth[0]);
let left = (k + 1).saturating_sub(window).max(1);
let after = loess_estimate(&sub, window, (k + 1) as f64, left, k, &mut weights)
.unwrap_or(smooth[k - 1]);
out[j] = before;
for (m, &value) in smooth.iter().enumerate() {
out[(m + 1) * period + j] = value;
}
out[(k + 1) * period + j] = after;
}
out
}
fn low_pass_averages(x: &[f64], period: usize) -> Vec<f64> {
moving_average(&moving_average(&moving_average(x, period), period), 3)
}
fn moving_average(x: &[f64], len: usize) -> Vec<f64> {
let count = x.len() - len + 1;
let mut out = Vec::with_capacity(count);
let mut sum: f64 = x[..len].iter().sum();
out.push(sum / len as f64);
for j in 1..count {
sum = sum - x[j - 1] + x[j + len - 1];
out.push(sum / len as f64);
}
out
}
fn loess_smooth(y: &[f64], len: usize) -> Vec<f64> {
let n = y.len();
if n < 2 {
return y.to_vec();
}
let mut weights = Vec::new();
if len >= n {
return (1..=n)
.map(|i| loess_estimate(y, len, i as f64, 1, n, &mut weights).unwrap_or(y[i - 1]))
.collect();
}
let half = len.div_ceil(2);
let (mut left, mut right) = (1, len);
(1..=n)
.map(|i| {
if i > half && right != n {
left += 1;
right += 1;
}
loess_estimate(y, len, i as f64, left, right, &mut weights).unwrap_or(y[i - 1])
})
.collect()
}
fn loess_estimate(
y: &[f64],
len: usize,
xs: f64,
left: usize,
right: usize,
weights: &mut Vec<f64>,
) -> Option<f64> {
let n = y.len();
let range = (right - left) as f64;
let mut h = (xs - left as f64).max(right as f64 - xs);
if len > n {
h += ((len - n) / 2) as f64;
}
let (h9, h1) = (0.999 * h, 0.001 * h);
weights.clear();
weights.resize(right - left + 1, 0.0);
let mut total = 0.0;
for (w, j) in weights.iter_mut().zip(left..=right) {
let r = (j as f64 - xs).abs();
if r <= h9 {
*w = if r <= h1 {
1.0
} else {
let u = r / h;
let cube = 1.0 - u * u * u;
cube * cube * cube
};
total += *w;
}
}
if total <= 0.0 {
return None;
}
for w in weights.iter_mut() {
*w /= total;
}
if h > 0.0 {
let centre: f64 = weights
.iter()
.zip(left..=right)
.map(|(w, j)| w * j as f64)
.sum();
let spread: f64 = weights
.iter()
.zip(left..=right)
.map(|(w, j)| w * (j as f64 - centre) * (j as f64 - centre))
.sum();
if crate::math::sqrt(spread) > 0.001 * range {
let slope = (xs - centre) / spread;
for (w, j) in weights.iter_mut().zip(left..=right) {
*w *= slope * (j as f64 - centre) + 1.0;
}
}
}
Some(
weights
.iter()
.zip(&y[left - 1..right])
.map(|(w, v)| w * v)
.sum(),
)
}
fn variance(x: &[f64]) -> f64 {
let mean = crate::math::mean(x);
x.iter().map(|v| (v - mean) * (v - mean)).sum::<f64>() / x.len() as f64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_windows_follow_statsmodels() {
let w = Windows::new(12, 7);
assert_eq!((w.seasonal, w.trend, w.low_pass), (7, 23, 13));
let w = Windows::new(4, 7);
assert_eq!((w.trend, w.low_pass), (9, 5));
let w = Windows::new(7, 7);
assert_eq!((w.trend, w.low_pass), (15, 9));
let w = Windows::new(11, 7);
assert_eq!((w.trend, w.low_pass), (21, 13));
assert_eq!(Windows::new(33, 7).trend, 63);
}
#[test]
fn moving_average_matches_hand_computation() {
assert_eq!(
moving_average(&[1.0, 2.0, 3.0, 4.0, 6.0], 2),
[1.5, 2.5, 3.5, 5.0]
);
}
#[test]
fn rejects_short_or_non_finite_input() {
assert!(stl(&[1.0; 7], 4).is_none());
assert!(stl(&[1.0; 8], 1).is_none());
let mut y = [1.0; 12];
y[3] = f64::NAN;
assert!(stl(&y, 4).is_none());
}
#[test]
fn constant_series_has_no_seasonality() {
let parts = stl(&[0.1; 48], 4).unwrap();
assert_eq!(parts.seasonal_strength(), 0.0);
assert!(parts.trend.iter().all(|&t| t == 0.1));
}
}