Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
time-series
time-series-decomposition
benchmark
component-recovery
symbolic-regression
icml-2026
License:
| import numpy as np | |
| from typing import Dict, Any, Optional | |
| from ..core import DecompResult | |
| from ..registry import MethodRegistry | |
| 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}}, | |
| ) | |
| 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}} | |
| ) | |
| 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}} | |
| ) | |