@创建于:20210311
@修改于:20210311
文章目录
1、背景
DataFrame下的指数权重滑窗算法在时间序列预测中较常用。本文对其的使用方法、理论内容和具体例子进行阐述,以方便使用者最快速度掌握。
2、ewm介绍
2.1 DataFrame ewm API接口介绍
此处对应的pandas 版本号是 1.2.2:pandas.DataFrame.ewm
DataFrame.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0, times=None)
编号 | 英文 | 中文 |
---|---|---|
1 | Provide exponential weighted (EW) functions. | 提供指数加权函数 |
2 | Available EW functions: mean(), var(), std(), corr(), cov(). | 可供使用指数加权函数有:mean(), var(), std(), corr(), cov() |
3 | Exactly one parameter: com, span, halflife, or alpha must be provided. | 只能提供一个参数: com, span, halflife, 和 alpha 四个参数中有且仅有一个参数可被设置(不支持2个或2个以上的设置)。 |
4 | min_periods=0, adjust=True, ignore_na=False, axis=0, times=None | 这些可选。 |
2.2 参数介绍
(1)com : float, optional 可选
Specify decay in terms of center of mass,
根据质心指定衰减
:math:\alpha = 1 / (1 + com)
, for :math:com \geq 0
.
α
=
1
1
+
c
o
m
,
c
o
m
≥
0
\alpha = \frac{1}{1+com},com \geq 0
α=1+com1,com≥0
(2)span : float, optional 可选
Specify decay in terms of span,
根据范围指定衰减
:math:\alpha = 2 / (span + 1)
, for :math:span \geq 1
.
α
=
2
1
+
s
p
a
n
,
s
p
a
n
≥
1
\alpha = \frac{2}{1+span},span \geq 1
α=1+span2,span≥1
(3)halflife : float, str, timedelta, optional 可选
Specify decay in terms of half-life,
根据半衰期指定衰减
:math:\alpha = 1 - \exp\left(-\ln(2) / halflife\right)
, for
:math:halflife > 0
.
α
=
1
−
e
−
ln
2
h
a
l
f
l
i
f
e
,
h
a
l
f
l
i
f
e
>
0
\alpha = 1 - e^{\frac{-\ln2} {halflife}}, halflife > 0
α=1−ehalflife−ln2,halflife>0
(4)alpha : float, optional
Specify smoothing factor :math:\alpha
directly,
指定平滑因子:直接指定
α
\alpha
α
:math:0 < \alpha \leq 1
.
0
<
α
≤
1
0 < \alpha \leq 1
0<α≤1
当com=1,span=3,halflife=1, alpha=0.5时候,对应的权值 α \alpha α是0.5。
(5)adjust : bool, default True, 默认是True
Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
除以开始阶段的衰减调整因子,以解释相对权重的不平衡(将EWMA视为移动平均线)。
(6)ignore_na : bool, default False,默认值False
Ignore missing values when calculating weights; specify True
to reproduce pre-0.15.0 behavior.
计算权重时忽略缺失值;指定True可重现0.15.0之前的行为。
(7)axis : {0, 1}, default 0
The axis to use. The value 0 identifies the rows, and 1 identifies the columns.
要使用的轴。值0标识行,值1标识列。
(8)times : str, np.ndarray, Series, default None
If str, the name of the column in the DataFrame representing the times.
如果是str类型,DataFrame中的列名表示时间。
If 1-D array like, a sequence with the same shape as the observations.
如果是一维矩阵类型,输入时间序列个数与目标序列中一致。
Only applicable to mean()
.
只能使用 mean()
3、例子
3.1 必选参数比较
import pandas as pd
import numpy as np
df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
df.ewm(com=1).mean()
df.ewm(span=3).mean()
df.ewm(alpha=0.5).mean()
df.ewm(halflife=1).mean()
结果是一样的,具体参见数学原理。
B
0 0.000000
1 0.666667
2 1.428571
3 1.428571
4 3.217391
3.2 adjust参数比较
df.ewm(alpha=0.5, adjust=False).mean()
df.ewm(alpha=0.5, adjust=True).mean()
3.2 ignore_na参数比较
df.ewm(alpha=0.5, ignore_na=True).mean()
df.ewm(alpha=0.5, ignore_na=False).mean()
3.2 times参数使用
versionadded:: 1.1.0,pandas 1.1.0之后才增加该功能