Skip to content

做什么(One-liner)

滚动窗口均值,None 值 filter。库级 primitive。

怎么用

python
from services.algo.statistics import rolling_mean
rolling_mean([1, 2, 3, 4, 5], window=3)
# → [None, None, 2.0, 3.0, 4.0]

核心公式

out_t = mean([s for s in series[t-w+1..t] if s is not None])  if valid >= 1 else None

假设与适用场景

假设:左对齐 + None filter + w<=n。

适用:macro smoothing、rolling baseline、简单去噪。

不适用:weighted(用 EMA)、streaming。

已知局限

  1. 左对齐硬编码
  2. None filter(不 forward-fill)
  3. window=3 default

参考文献

Brockwell & Davis (2016) Time Series Forecasting。

Golden Test

tests/golden/fixtures/tier1/rolling_mean/,1e-12,2026-04-20 passing。

Changelog

  • 1.0.0 (2026-04-20) — 首次 Active

Verifiable intelligence for the decisions that demand scrutiny.