Skip to content

做什么(One-liner)

numpy.polyfit 一阶拟合的 slope。用来判断序列方向。库级 primitive。

怎么用

python
from services.algo.statistics import trend_slope
s = trend_slope([10, 12, 14, 16, 18])   # 2.0

核心公式

x = [0..n-1]
slope = polyfit(x, filter_none(series), deg=1)[0]

假设与适用场景

假设:等距索引 + None filter + n≥3 有效点。

适用:趋势方向 / earnings 打分 / macro drift。

不适用:非线性、structural break、n<3。

已知局限

  1. Outlier 敏感
  2. 无 R²/p-value 输出
  3. 单位 = value per index step(要 caller 理解)

参考文献

Standard OLS + numpy.polyfit + Greene (2017) Econometric Analysis.

Golden Test

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

Changelog

  • 1.0.0 (2026-04-20) — BUG-GOLDEN-001 修复 + 首次 Active

Verifiable intelligence for the decisions that demand scrutiny.