Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
time-series
time-series-decomposition
benchmark
component-recovery
symbolic-regression
icml-2026
License:
File size: 3,136 Bytes
17b7ba4 | 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 | import numpy as np
from typing import Dict, Any, Optional
from ..core import DecompResult
from ..registry import MethodRegistry
@MethodRegistry.register("STL")
def stl_decompose(
y: np.ndarray,
params: Dict[str, Any],
) -> DecompResult:
"""
STL decomposition: y = trend + seasonal + resid.
"""
try:
from statsmodels.tsa.seasonal import STL
except ImportError as exc:
raise ImportError("statsmodels is required for STL decomposition.") from exc
# Copy params to avoid mutation
cfg = params.copy()
period = cfg.pop("period", None)
if period is None:
raise ValueError("STL requires 'period' in params.")
period = int(period)
stl = STL(y, period=period, **cfg)
res = stl.fit()
trend = np.asarray(res.trend)
seasonal = np.asarray(res.seasonal)
residual = np.asarray(res.resid)
return DecompResult(
trend=trend,
season=seasonal,
residual=residual,
meta={"method": "STL", "params": {"period": period, **cfg}},
)
@MethodRegistry.register("MSTL")
def mstl_decompose(
y: np.ndarray,
params: Dict[str, Any],
) -> DecompResult:
try:
from statsmodels.tsa.seasonal import MSTL
except ImportError as exc:
raise ImportError("statsmodels>=0.14 is required for MSTL decomposition.") from exc
cfg = params.copy()
periods = cfg.pop("periods", None)
if periods is None:
# Try to infer or require it
raise ValueError("MSTL requires 'periods' list in params.")
# Ensure periods are integers >= 2
periods = [int(p) for p in periods if p >= 2]
if not periods:
raise ValueError("MSTL 'periods' must contain at least one integer >= 2.")
mstl = MSTL(y, periods=periods, **cfg)
res = mstl.fit()
seasonal = res.seasonal
if seasonal.ndim == 2:
season = seasonal.sum(axis=1)
else:
season = seasonal
trend = res.trend
residual = res.resid
return DecompResult(
trend=trend,
season=season,
residual=residual,
meta={"method": "MSTL", "params": {"periods": periods, **cfg}}
)
@MethodRegistry.register("ROBUST_STL")
def robuststl_decompose(
y: np.ndarray,
params: Dict[str, Any],
) -> DecompResult:
try:
from statsmodels.tsa.seasonal import STL
except ImportError as exc:
raise ImportError("statsmodels is required for RobustSTL.") from exc
cfg = params.copy()
period = cfg.pop("period", None)
if period is None:
raise ValueError("RobustSTL requires 'period' in params.")
period = int(period)
# RobustSTL is just STL with robust=True by default and maybe some specific tuning
robust = cfg.pop("robust", True)
stl = STL(y, period=period, robust=robust, **cfg)
res = stl.fit()
return DecompResult(
trend=res.trend,
season=res.seasonal,
residual=res.resid,
meta={"method": "ROBUST_STL", "params": {"period": period, "robust": robust, **cfg}}
)
|