简单移动平均 指数移动平均
參考自
簡單移動平均(Simple moving average)
數(shù)據(jù)p1,p2,...,pMp_1, p_2, ..., p_Mp1?,p2?,...,pM?的窗口為n的簡單移動平均:
p ̄SM=pM+pM?1+...+pM?(n?1)n=1n∑i=0n?1pM?i\overline{p}_{SM}=\frac{p_M+p_{M-1}+...+p_{M-(n-1)}}{n}=\frac{1}{n}\sum_{i=0}^{n-1}{p_{M-i}}p?SM?=npM?+pM?1?+...+pM?(n?1)??=n1?i=0∑n?1?pM?i?
迭代的形式:
p ̄SM=p ̄SM,prev+pMn?pM?Nn\overline{p}_{SM}=\overline{p}_{SM, prev}+\frac{p_M}{n}-\frac{p_{M-N}}{n}p?SM?=p?SM,prev?+npM???npM?N??
實(shí)現(xiàn)
簡單實(shí)現(xiàn)
import pandas as pd s = [1,2,3,5,6,10,12,14,12,30] # 等價(jià)于data = pd.DataFrame(s) data = pd.Series(s) # 窗口為3的簡單移動平均 smv_3=data.rolling(window=3).mean()print('時(shí)間序列數(shù)據(jù):') print(data) print('\n窗口為3的簡單移動平均:') print(smv_3)結(jié)果:
時(shí)間序列數(shù)據(jù): 0 1 1 2 2 3 3 5 4 6 5 10 6 12 7 14 8 12 9 30 dtype: int64窗口為3的簡單移動平均: 0 NaN 1 NaN 2 2.000000 3 3.333333 4 4.666667 5 7.000000 6 9.333333 7 12.000000 8 12.666667 9 18.666667 dtype: float64平均結(jié)果的NaN和數(shù)據(jù)中的NaN怎么辦?
min_periods字段
平均結(jié)果的NaN
import pandas as pd s = [1,2,3,5,6,10,12,14,12,30] data = pd.Series(s) # data = pd.DataFrame(s) # 窗口為3的簡單移動平均,min_periods指的是如ugo沒有3個(gè)數(shù),有2個(gè)數(shù)都可以做簡單移動平均 smv_3=data.rolling(window=3, min_periods=2).mean()print('時(shí)間序列數(shù)據(jù):') print(data) print('\n窗口為3的簡單移動平均:') print(smv_3)結(jié)果:
時(shí)間序列數(shù)據(jù): 0 1 1 2 2 3 3 5 4 6 5 10 6 12 7 14 8 12 9 30 dtype: int64窗口為3的簡單移動平均: 0 NaN 1 1.500000 2 2.000000 3 3.333333 4 4.666667 5 7.000000 6 9.333333 7 12.000000 8 12.666667 9 18.666667 dtype: float64數(shù)據(jù)中的NaN
import pandas as pd # 第三個(gè)數(shù)據(jù)是NaN s = [1,2,None,5,6,10,12,14,12,30] data = pd.Series(s) # data = pd.DataFrame(s) # 窗口為3的簡單移動平均 smv_3=data.rolling(window=3, min_periods=2).mean()print('時(shí)間序列數(shù)據(jù):') print(data) print('\n窗口為3的簡單移動平均:') print(smv_3)結(jié)果:
時(shí)間序列數(shù)據(jù): 0 1.0 1 2.0 2 NaN 3 5.0 4 6.0 5 10.0 6 12.0 7 14.0 8 12.0 9 30.0 dtype: float64窗口為3的簡單移動平均: 0 NaN 1 1.500000 2 1.500000 3 3.500000 4 5.500000 5 7.000000 6 9.333333 7 12.000000 8 12.666667 9 18.666667 dtype: float64指數(shù)移動平均(Exponential moving average)
數(shù)據(jù)p1,p2,...,pMp_1, p_2, ..., p_Mp1?,p2?,...,pM?的指數(shù)移動平均的迭代形式:
p ̄EM={p1M=1αpM+(1?α)p ̄SM,prevM≠1\overline{p}_{EM}=\left\{ \begin{array}{rcl} p_1 && {M=1}\\ \alpha p_M+(1-\alpha)\overline{p}_{SM, prev} && {M \neq 1} \end{array} \right. p?EM?={p1?αpM?+(1?α)p?SM,prev???M=1M??=1?
如何理解指數(shù)移動平均
把指數(shù)移動的公式展開:
p ̄EM=α(pM+(1?α)pM?1+(1?α)2pM?2+...+(1?α)M?1p1)\overline{p}_{EM}=\alpha(p_M+(1-\alpha)p_{M-1}+(1-\alpha)^2p_{M-2}+...+(1-\alpha)^{M-1}p_1)p?EM?=α(pM?+(1?α)pM?1?+(1?α)2pM?2?+...+(1?α)M?1p1?)
當(dāng)M>>1M>>1M>>1時(shí),他其實(shí)就是一個(gè)歷史數(shù)據(jù)的指數(shù)加權(quán)平均:
p ̄EM=∑i=0+∞α(1?α)ipM?i\overline{p}_{EM}=\sum_{i=0}^{+\infin}\alpha(1-\alpha)^{i}p_{M-i}p?EM?=i=0∑+∞?α(1?α)ipM?i?
我們可以發(fā)現(xiàn)權(quán)重pM?ip_{M-i}pM?i?對應(yīng)的權(quán)重wi=α(1?α)iw_i=\alpha(1-\alpha)^{i}wi?=α(1?α)i,我們計(jì)算所有權(quán)重之和看是否為1:∑i=0∞wi=α(1+(1?α)+(1?α)2+...)=α11?(1?α)=1\sum_{i=0}^{\infin}{w_i}=\alpha(1+(1-\alpha)+(1-\alpha)^2+...)=\alpha\frac{1}{1-(1-\alpha)}=1∑i=0∞?wi?=α(1+(1?α)+(1?α)2+...)=α1?(1?α)1?=1。
因此,我們用迭代的方式計(jì)算指數(shù)移動平均相當(dāng)于把權(quán)wi=α(1?α)iw_i=\alpha(1-\alpha)^iwi?=α(1?α)i賦給過去的值。
而下面考察權(quán)重的變化,如果α\alphaα足夠小,則
w1/α=α(1?α)1/α=α1e=36.8%αw_{1/\alpha}=\alpha(1-\alpha)^{1/\alpha}=\alpha\frac{1}{e}=36.8\%\alphaw1/α?=α(1?α)1/α=αe1?=36.8%α
指數(shù)移動平均與簡單移動平均的關(guān)系
可以通過指數(shù)移動平均與簡單移動平均的關(guān)系來知道α\alphaα的選取。
注:下面質(zhì)心的推導(dǎo)的坐標(biāo)系是這樣的:
0------>1------>2------>...p_M p_{M_1}選取α\alphaα的方法 × 兩者的質(zhì)心相同
簡單移動平均的質(zhì)心為1+N2\frac{1+N}{2}21+N?
指數(shù)移動平均的質(zhì)心為α[1+2(1?α)+3(1?α)2+...]=1α\alpha[1+2(1-\alpha)+3(1-\alpha)^2+...]=\frac{1}{\alpha}α[1+2(1?α)+3(1?α)2+...]=α1?
令二者的質(zhì)心相同,則1+N2=1α\frac{1+N}{2}=\frac{1}{\alpha}21+N?=α1?,即α=2N+1\alpha=\frac{2}{N+1}α=N+12?。當(dāng)我想我的指數(shù)移動平均的歷史加權(quán)的質(zhì)心和N窗口簡單移動平均質(zhì)心是一樣的。
選取α\alphaα的方法 × 指定指數(shù)移動平均的質(zhì)心
令質(zhì)心為c,則c=1αc=\frac{1}{\alpha}c=α1?,即α=1c\alpha=\frac{1}{c}α=c1?。
例子,我令最新的數(shù)據(jù)為質(zhì)心,則c=1,此時(shí)α=11=1\alpha=\frac{1}{1}=1α=11?=1;
注意如果質(zhì)心選擇的原點(diǎn)不一定會有影響
p_M對應(yīng)坐標(biāo)1是我們之前的推導(dǎo)方式
0------>1------>2------>...p_M p_{M_1}我們現(xiàn)在變?yōu)閜_M對應(yīng)坐標(biāo)0
0------>1------>2------>... p_M p_{M_1}例子,我令最新的數(shù)據(jù)為質(zhì)心,則c′=0c'=0c′=0,此時(shí)有c=c′+1c=c'+1c=c′+1,則α=1c=1c′+1=1\alpha=\frac{1}{c}=\frac{1}{c'+1}=1α=c1?=c′+11?=1。
選取α\alphaα的方法 × 最近的N個(gè)數(shù)所累計(jì)的權(quán)重和
拖尾的權(quán)重{wN,wN+1,...w_N, w_{N+1}, ...wN?,wN+1?,...}之和:
wn,...=α((1?α)N+(1?α)N+1,...)=a(1?α)N(1+(1?α)+...)=(1?α)Nw_{n, ...}=\alpha((1-\alpha)^N+(1-\alpha)^{N+1}, ...)=a(1-\alpha)^N(1+(1-\alpha)+...)=(1-\alpha)^Nwn,...?=α((1?α)N+(1?α)N+1,...)=a(1?α)N(1+(1?α)+...)=(1?α)N
最近的N個(gè)數(shù)所累計(jì)的權(quán)重和:
w0,..,n?1=1?wn,...=1?(1?α)Nw_{0, .., n-1}=1-w_{n, ...}=1-(1-\alpha)^Nw0,..,n?1?=1?wn,...?=1?(1?α)N
-
如何選取α\alphaα,我們可以說讓最近N個(gè)數(shù)所累計(jì)的權(quán)重和為剛剛12\frac{1}{2}21?,此時(shí)12=1?(1?α)N\frac{1}{2}=1-(1-\alpha)^N21?=1?(1?α)N,所以α=1?eln?(0.5)/N\alpha=1-e^{\ln(0.5)/N}α=1?eln(0.5)/N。
-
當(dāng)α\alphaα足夠小時(shí),w0,..,n?1=1?(1?α)N=1?((1?α)1/?α)?αN=1?e?αNw_{0, .., n-1}=1-(1-\alpha)^N=1-((1-\alpha)^{1/-\alpha})^{-\alpha N}=1-e^{-\alpha N}w0,..,n?1?=1?(1?α)N=1?((1?α)1/?α)?αN=1?e?αN
-
取α\alphaα(足夠小),則意味著1α\frac{1}{\alpha}α1?個(gè)最近的數(shù)據(jù)包含了約1?e?1=63.2%1-e^{-1}=63.2\%1?e?1=63.2%的權(quán)重
偏差修正
偏差修正的主要目的是為了提高指數(shù)加權(quán)平均的精確度,主要是針對前期的加權(quán)平均值的計(jì)算精度。
下面的例子中α=0.1;β=1?α=0.9\alpha=0.1;\beta=1-\alpha=0.9α=0.1;β=1?α=0.9。觀察到的序列為{40, 50},而的計(jì)算的指數(shù)平均為{4, 7.1},這樣的結(jié)果精度是很差的。
下面我們進(jìn)行修正偏差,得到修正指數(shù)平均vt′v_t'vt′?:
vt′=vt1?βtv_t'=\frac{v_t}{1-\beta^t}vt′?=1?βtvt??
因此,
原序列:{40, 50} 指數(shù)平均序列:{4, 7.1} 修正偏差后的指數(shù)平均序列:{40, 37.37}實(shí)現(xiàn)
Series.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0)α\alphaα選擇參數(shù)
span, com, halflife分別對應(yīng)前面所說的選擇方法
com : float. optional (注意,這里的com,第一個(gè)元素是對應(yīng)0的) Center of mass: \alpha = 1 / (1 + com),span : float, optional Specify decay in terms of span, \alpha = 2 / (span + 1)halflife : float, optional Specify decay in terms of halflife, \alpha = 1 - exp(log(0.5) / halflife)基本實(shí)現(xiàn)
# adjust=True的情況 import pandas as pd s = [40, 50] data = pd.Series(s) # data = pd.DataFrame(s) # span=19對應(yīng)a=0.1 emv=data.ewm(span=19).mean() ans_1=0.1*50+0.9*40 print('時(shí)間序列數(shù)據(jù):') print(data) print('\na=0.1指數(shù)移動平均:') print(emv) print('ans of time 1:', ans_1) 時(shí)間序列數(shù)據(jù): 0 40 1 50 dtype: int64a=0.1指數(shù)移動平均: 0 40.000000 1 45.263158 dtype: float64 ans of time 1: 41.0我們發(fā)現(xiàn)計(jì)算結(jié)果,跟我們推到的結(jié)果ans_1不一致。原因在于,我們之前關(guān)于移動評價(jià)的公式是假設(shè)權(quán)重之和為1,但這只有是無窮級數(shù)求和的結(jié)果。
- 真正的公式(即adjust=True)應(yīng)該是
yt=xt+(1?α)xt?1+(1?α)2xt?2+...+(1?α)tx01+(1?α)+(1?α)2+...+(1?α)ty_t = \frac{x_t+(1-\alpha)x_{t-1}+(1-\alpha)^2x_{t-2}+...+(1-\alpha)^tx_0}{1+(1-\alpha)+(1-\alpha)^2+...+(1-\alpha)^t}yt?=1+(1?α)+(1?α)2+...+(1?α)txt?+(1?α)xt?1?+(1?α)2xt?2?+...+(1?α)tx0?? - 快速迭代的公式(即adjust=False)應(yīng)該是
y0=x0,yt=(1?α)yt?1+αxty_0=x_0, \\ y_t=(1-\alpha)y_{t-1}+\alpha x_ty0?=x0?,yt?=(1?α)yt?1?+αxt? - 只有在t足夠大時(shí),adjust=False的結(jié)果才會接近adjust=True的結(jié)果,而adjust=False計(jì)算得快,但有一定精度損失
結(jié)果:
時(shí)間序列數(shù)據(jù): 0 40 1 50 dtype: int64a=0.1指數(shù)移動平均: 0 40.0 1 41.0 dtype: float64 ans of time 1: 41.0數(shù)據(jù)中有NaN怎么辦?
參考前面的公式
-
When ignore_na is False (default), weights are based on absolute positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and (1-alpha)**2 and alpha (if adjust is False).
-
When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based on relative positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is True), and 1-alpha and alpha (if adjust is False).
個(gè)人認(rèn)為,四種搭配中這兩種是比較合理的:
1. ignore_na=True, adjust=False 2. ignore_na=False, adjust=True下面的例子都考慮adjust=False的情況
# ignore_na=True,忽略NaN import pandas as pd s = [40, None, None, 50, None] data = pd.Series(s) # data = pd.DataFrame(s) # span=19對應(yīng)a=0.1 emv=data.ewm(span=19, adjust=False, ignore_na=True).mean() ans_1=0.1*50+0.9*40 print('時(shí)間序列數(shù)據(jù):') print(data) print('\na=0.1指數(shù)移動平均:') print(emv) print('ans of time 4:', ans_1)結(jié)果:
時(shí)間序列數(shù)據(jù): 0 40.0 1 NaN 2 NaN 3 50.0 4 NaN dtype: float64a=0.1指數(shù)移動平均: 0 40.0 1 40.0 2 40.0 3 41.0 4 41.0 dtype: float64 ans of time 4: 41.0 # ignore_na=False,不忽略NaN import pandas as pd s = [40, None, None, 50, None] data = pd.Series(s) # data = pd.DataFrame(s) # span=19對應(yīng)a=0.1 emv=data.ewm(span=19, adjust=False, ignore_na=False).mean() ans_1=(0.1*50+(0.9**3)*40)/(0.1+0.9**3) print('時(shí)間序列數(shù)據(jù):') print(data) print('\na=0.1指數(shù)移動平均:') print(emv) print('ans of time 4:', ans_1)結(jié)果:
時(shí)間序列數(shù)據(jù): 0 40.0 1 NaN 2 NaN 3 50.0 4 NaN dtype: float64a=0.1指數(shù)移動平均: 0 40.000000 1 40.000000 2 40.000000 3 41.206273 4 41.206273 dtype: float64 ans of time 4: 41.20627261761158二元移動窗口函數(shù)
一些統(tǒng)計(jì)計(jì)算符,比如相關(guān)性和協(xié)方差,需要在兩個(gè)時(shí)間序列上進(jìn)行計(jì)算。例如,經(jīng)濟(jì)分析通常喜歡比較一只股票與基礎(chǔ)指數(shù)標(biāo)普500之間的相關(guān)性。
更多
總結(jié)
以上是生活随笔為你收集整理的简单移动平均 指数移动平均的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信访分析 大数据_“大数据”有多火 化解
- 下一篇: echarts 折线图线条粗细_echa