pandas.DataFrame.ewm¶
Exactly one parameter: com , span , halflife , or alpha must be provided.
Parameters com float, optional
Specify decay in terms of center of mass
\(\alpha = 1 / (1 + com)\) , for \(com \geq 0\) .
span float, optional
Specify decay in terms of span
\(\alpha = 2 / (span + 1)\) , for \(span \geq 1\) .
halflife float, str, timedelta, optional
Specify decay in terms of half-life
\(\alpha = 1 - \exp\left(-\ln(2) / halflife\right)\) , for \(halflife > 0\) .
If 指数移动平均线(EMA) times is specified, the time unit (str or timedelta) over which an observation decays to half its value. Only applicable to mean() , and halflife value will not apply to the other functions.
New 指数移动平均线(EMA) in version 1.1.0.
Specify smoothing factor \(\alpha\) directly
min_periods int, default 0
Minimum number of observations in window required 指数移动平均线(EMA) to have a value; otherwise, result is np.nan .
adjust bool, default True
Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
When adjust=True (default), the EW function is calculated using weights \(w_i = (1 - \alpha)^i\) . For example, the EW moving average of the series [ \(x_0, x_1, . x_t\) ] would be:
When adjust=False , the exponentially weighted function is calculated recursively:
Ignore missing values when calculating weights.
When ignore_na=False (default), weights are based on absolute positions. For example, the weights of \(x_0\) and \(x_2\) used in calculating the final weighted average of [ \(x_0\) , None, \(x_2\) ] are \((1-\alpha)^2\) and \(1\) if adjust=True , and \((1-\alpha)^2\) and \(\alpha\) if adjust=False .
When ignore_na=True , weights are based on relative positions. For example, the weights of \(x_0\) and \(x_2\) 指数移动平均线(EMA) used in calculating the final weighted average of [ \(x_0\) , None, \(x_2\) ] are \(1-\alpha\) and \(1\) if adjust=True , and \(1-\alpha\) and \(\alpha\) if adjust=False .
If 0 or 'index' , calculate across the rows.
If 1 or 'columns' , calculate across the columns.
times str, np.ndarray, Series, default None
New in version 1.1.0.
Only applicable to mean() .
Times corresponding to the 指数移动平均线(EMA) observations. Must be monotonically increasing and datetime64[ns] dtype.
If 1-D array like, a sequence with the same shape as the observations.
Deprecated since version 1.4.0: If str, the name of the column in the DataFrame representing the times.
New in version 1.4.0.
Execute the rolling operation per single column or row ( 'single' ) or over the entire object ( 'table' ).
This argument is only implemented when specifying engine='numba' in the method call.
【时间序列】从移动平均到指数平滑
AI蜗牛车 于 2020-10-12 18:22:00 发布 2568 收藏 15
简单移动平均
加权移动平均
移动平均线
移动平均最常见的使用就是在股市中了,将一定时期内的证券价格(指数)「加以平均」,并把不同时间的平均值连接起来,形成一根MA,用以观察证券价格变动趋势的一种技术指标
image-20200925174725577
「滞后性」
「趋势性」
指数加权移动平均
「公式:」
python代码
image-20200925181921625
一次指数平滑
二次指数平滑
三次指数平滑
「趋势调整」
α值是根据时间序列的变化特性来选取的。若时间序列的波动不大,比较平稳,则α应取小一些,如0.1 ~ 0.3 ;若时间序列具有迅速且明显的变动倾向, 则α应取大一些,如0.6 ~ 0.9。实质上,α是一个经验数据,通过多个值进行试算比较而定,哪个α值引起的预测误差小,就采用哪个。
Holt-Winters
「加法模型」
「乘法模型」
为i = 1,2,. ,L设置季节性指数
的初始估计会涉及更多。如果N是数据中存在的完整循环数(the number of complete cycles),则:
「衰减模型」
python代码
image-20200925184544024
「seasonal_periods」参数的作用就是季节波动,这里设置的是31,几乎完美拟合曲线
指数平滑法的组合分类
img
【量化投资-python & pandas技巧系列】使用python计算移动平均线
【量化小讲堂 – python & pandas技巧系列】使用python计算各类移动平均线
计算移动平均线是最常见的需求,下面这段代码将完成以下三件事情:
1. 从csv格式的文件中导入股票数据,数据例图如下:
—————- —————- —————- —————- —————- —————- —————- 指数移动平均线(EMA) —————- —————- —————- —————- —————- —————-
# -*- coding: utf-8 -*-
“””
@author: yucezhe
@contact: QQ:2089973054 email:[email protected]
“””
import pandas as pd
# ========== 从原始csv文件中导入股票数据,以浦发银行sh600000为例
# 导入数据 – 注意:这里请填写数据文件在您电脑中的路径
stock_data = pd.read_csv( ‘stock data/sh600000.csv’ , parse_dates =[ 1 ])
# 将数据按照交易日期从远到近排序
stock_data.sort( ‘date’ , inplace = True )
# ========== 计算移动平均线
# 分别计算5日、20日、60日的移动平均线
ma_list = [ 5 , 20 , 60 ]
# 计算简单算术移动平均线MA – 注意:stock_data[‘close’]为股票每天的收盘价
for ma in ma_list:
stock_data[ ‘MA_’ + str (ma)] = pd.rolling_mean(stock_data[ ‘close’ ] , ma)
# 计算指数平滑移动平均线EMA
for ma in ma_list:
stock_data[ ‘EMA_’ + str (ma)] = pd.ewma(stock_data[ ‘close’ ] , span =ma)
# 将数据按照交易日期从近到远排序
stock_data.sort( ‘date’ , ascending = False , inplace = True )
代码输出的数据截图如下:
现在想到的之后几期会讲的内容:
【量化小讲堂 – python & pandas技巧系列】如何在windows环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】如何在mac OSX环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】使用python计算KDJ指标
【量化小讲堂 – python & pandas技巧系列】使用python计算MACD指标
【量化小讲堂 – 投资策略系列】KDJ、MACD指标双金叉选股效果
【量化小讲堂 – python & pandas技巧系列】使用pytho将日线数据转换成周线、月线数据
【时间序列】从移动平均到指数平滑
AI蜗牛车 于 2020-10-12 18:22:00 发布 2569 收藏 15
简单移动平均
加权移动平均
移动平均线
移动平均最常见的使用就是在股市中了,将一定时期内的证券价格(指数)「加以平均」,并把不同时间的平均值连接起来,形成一根MA,用以观察证券价格变动趋势的一种技术指标
image-20200925174725577
「滞后性」
「趋势性」
指数加权移动平均
「公式:」
python代码
image-20200925181921625
一次指数平滑
二次指数平滑
三次指数平滑
「趋势调整」
α值是根据时间序列的变化特性来选取的。若时间序列的波动不大,比较平稳,则α应取小一些,如0.1 ~ 0.3 ;若时间序列具有迅速且明显的变动倾向, 则α应取大一些,如0.6 ~ 0.9。实质上,α是一个经验数据,通过多个值进行试算比较而定,哪个α值引起的预测误差小,就采用哪个。
Holt-Winters
「加法模型」
「乘法模型」
为i = 1,2,. ,L设置季节性指数
的初始估计会涉及更多。如果N是数据中存在的完整循环数(the number of complete cycles),则:
「衰减模型」
python代码
image-20200925184544024
「seasonal_periods」参数的作用就是季节波动,这里设置的是31,几乎完美拟合曲线
指数平滑法的组合分类
img