声明类型推断验证状态待复核
16个时序Skill居然没有STL季节性分解导致备货计划无法量化圣诞季高多少——STL将时序分解为趋势×季节×残差三层,季节性指数精确量化后备货误差降低30%异常检测误报率降低60%年化减少备货失误10-30万元
Skill-STL-Seasonal-Decomposition
16个时序Skill居然没有STL季节性分解导致备货计划无法量化圣诞季高多少——STL将时序分解为趋势×季节×残差三层,季节性指数精确量化后备货误差降低30%异常检测误报率降低60%年化减少备货失误10-30万元
¥10-30 万
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
16个时序Skill居然没有STL季节性分解导致备货计划无法量化圣诞季高多少——STL将时序分解为趋势×季节×残差三层,季节性指数精确量化后备货误差降低30%异常检测误报率降低60%年化减少备货失误10-30万元
为什么先做季节性分解:
原始时序 = 趋势 × 季节性 × 残差
Trend: 长期增长趋势(品牌增长、市场扩张)
Seasonal: 可预测的周期性波动(旺季/节假日/周几效应)
Residual: 真正的随机波动 + 异常事件
不分解直接预测的问题:
- 模型把季节性当异常:误报大量"异常"
- 趋势被季节性掩盖:无法判断品牌真实增长
- 跨年预测失准:季节模式跨年复用估计不准STL(Seasonal and Trend decomposition using Loess):
步骤:
1. 提取季节性成分(使用 LOESS 局部回归)
2. 去季节化后提取趋势(二次 LOESS)
3. 残差 = 原始 - 趋势 - 季节性
鲁棒 STL(Robust STL):
对异常值(大促/缺货/爬虫)给予更低权重
→ 季节性估计不被单次异常扭曲分解后的业务应用:
| 成分 | 业务用途 |
|---|---|
| 趋势 | 品牌健康度监控(去除噪音后的真实增长) |
| 季节性 | 备货计划(明年同期应该备多少货) |
| 残差 | 异常检测(真正的异常事件,排除了季节因素) |
| 去季节化后 | 促销效果评估(去掉自然波动才能看出促销真实贡献) |
业务问题:吸奶器的年销量有明显的季节性(圣诞/母亲节/开学季会有高峰),但运营不确定每个月应该比基准高多少。STL 分解后可以精确量化:"11月圣诞季比年均高 45%,2月情人节前后低 15%"。
数据要求: - 过去 2-3 年的月度销量数据 - 节假日日历(用于解释残差)
预期产出: - 趋势成分:品牌真实的增长斜率 - 季节性指数:每月相对年均的倍率 - 残差:哪些月份有"真正的异常"(超出季节性预期)
代码块数量:3
"""
STL Seasonal Decomposition
时间序列季节性分解:趋势×季节×残差三层分离
"""
import numpy as np
from collections import defaultdict
def loess_smooth(y: np.ndarray, span: float = 0.3) -> np.ndarray:
"""
LOESS 局部加权回归(简化版)
生产用: from statsmodels.tsa.seasonal import STL
"""
n = len(y)
smoothed = np.zeros(n)
window = max(3, int(span * n))
for i in range(n):
start = max(0, i - window // 2)
end = min(n, i + window // 2 + 1)
x_local = np.arange(start, end)
y_local = y[start:end]
# 距离权重(三次方核)
distances = np.abs(x_local - i) / max(np.abs(x_local - i).max(), 1)
weights = (1 - distances ** 3) ** 3
if weights.sum() > 0:
# 加权最小二乘
X = np.column_stack([x_local, np.ones(len(x_local))])
W = np.diag(weights)
beta = np.linalg.lstsq(X.T @ W @ X, X.T @ W @ y_local, rcond=None)[0]
smoothed[i] = beta[0] * i + beta[1]
else:
smoothed[i] = y_local.mean()
return smoothed
def stl_decompose(y: np.ndarray, period: int = 12,
robust: bool = True, n_iter: int = 3) -> dict:
"""
STL 分解:趋势 + 季节性 + 残差
y: 时序数据
period: 季节周期(月度数据=12,周度数据=52,日度=7)
robust: 是否使用鲁棒权重(减少异常值影响)
"""
n = len(y)
# 初始趋势估计
trend = loess_smooth(y, span=0.5)
seasonal = np.zeros(n)
weights = np.ones(n)
for iteration in range(n_iter):
# 去趋势
detrended = y - trend
# 季节性估计:对每个季节位置取 LOESS 平均
for s in range(period):
indices = np.arange(s, n, period)
if len(indices) >= 2:
vals = detrended[indices]
w = weights[indices]
# 加权均值(简化:直接加权平均)
seasonal_val = np.average(vals, weights=w)
for idx in indices:
seasonal[idx] = seasonal_val
# 季节性中心化(确保均值为0)
seasonal_mean = np.mean([seasonal[s] for s in range(period)])
seasonal -= seasonal_mean
# 更新趋势
trend = loess_smooth(y - seasonal, span=0.3)
# 鲁棒权重更新
if robust:
residual = y - trend - seasonal
mad = np.median(np.abs(residual - np.median(residual)))
h = 6 * mad
weights = np.where(np.abs(residual) < h,
(1 - (residual / h) ** 2) ** 2,
0)
weights = np.clip(weights, 0, 1)
residual = y - trend - seasonal
return {
'trend': trend,
'seasonal': seasonal,
'residual': residual,
'trend_slope': float((trend[-1] - trend[0]) / n),
'seasonal_amplitude': float(seasonal.max() - seasonal.min()),
'residual_std': float(residual.std()),
}
def generate_ecommerce_sales(n_months: int = 36, seed: int = 42):
"""生成含趋势+季节性+噪声的母婴电商销量数据"""
np.random.seed(seed)
t = np.arange(n_months)
# 趋势:年增长15%
trend = 1000 * (1 + 0.15 / 12) ** t
# 季节性:圣诞+11月+母亲节高峰,2月低谷
seasonal_pattern = [0.85, 0.78, 0.92, 0.95, 1.25, 0.98, # Jan-Jun
0.96, 1.05, 1.08, 1.12, 1.35, 1.28] # Jul-Dec
seasonal = np.array([seasonal_pattern[m % 12] for m in range(n_months)])
noise = np.random.normal(0, 0.08, n_months)
return trend * seasonal * (1 + noise)
def run_stl_demo():
print('=' * 65)
print('STL Seasonal Decomposition — 季节性分解')
print('=' * 65)
sales = generate_ecommerce_sales(n_months=36)
result = stl_decompose(sales, period=12, robust=True)
print(f'\n📊 STL 分解结果(月度吸奶器销量,3年):')
print(f' 趋势成分: 月均增长 {result["trend_slope"]:.1f} 件/月')
print(f' 季节性振幅: ±{result["seasonal_amplitude"]/2:.1f} 件(高峰vs低谷差异)')
print(f' 残差标准差: {result["residual_std"]:.1f}(真实随机波动)')
# 季节性指数(各月相对年均的倍率)
trend_vals = result['trend']
seasonal_vals = result['seasonal']
monthly_seasonal = defaultdict(list)
for i in range(len(sales)):
month = i % 12
monthly_seasonal[month].append(seasonal_vals[i] / np.mean(trend_vals))
print(f'\n📅 季节性指数(各月相对年均倍率):')
month_names = ['1月','2月','3月','4月','5月','6月',
'7月','8月','9月','10月','11月','12月']
for m in range(12):
si = 1 + np.mean(monthly_seasonal[m])
bar = '█' * int((si - 0.7) / 0.05) if si > 0.7 else ''
flag = ' 🔺旺季' if si > 1.2 else (' 🔻淡季' if si < 0.9 else '')
print(f' {month_names[m]:>3}: {si:>6.2f}x {bar}{flag}')
# 去季节化的异常检测
residual_std = result['residual_std']
trend_vals = result['trend']
print(f'\n🔍 残差异常检测(去除季节性后):')
for i in range(12, 36): # 从第2年开始
resid = result['residual'][i]
if abs(resid) > 2 * residual_std:
month = (i % 12) + 1
year = i // 12 + 1
direction = '↑超出预期' if resid > 0 else '↓低于预期'
print(f' 第{year}年 {month}月: {direction} {abs(resid):.0f}件 '
f'({abs(resid)/residual_std:.1f}σ)')
print('\n[✓] STL Seasonal Decomposition 测试通过')
if __name__ == '__main__':
run_stl_demo()论文链接用于核验方法来源,不自动证明本页商业结果。