【炼丹技巧】指数移动平均(EMA)的原理及PyTorch实现
Nicolas
EMA的定义
指数移动平均(Exponential Moving Average)也叫权重移动平均(Weighted Moving Average),是一种给予近期数据更高权重的平均方法。
假设我们有n个数据: [\theta_1, \theta_2, . \theta_n]
- 普通的平均数: \overline=\frac\sum_^n \theta_i
- EMA: v_t = \beta\cdot v_ + (1-\beta)\cdot \theta_t ,其中, v_t 表示前 t 条的平均值 ( v_0=0 ), \beta 是加权权重值 (一般设为0.9-0.999)。
Andrew Ng在Course 2 Improving Deep Neural Networks中讲到,EMA可以近似看成过去 1/(1-\beta) 个时刻 v 值的平均。
普通的过去 n 时刻的平均是这样的:
类比EMA,可以发现当 \beta=\frac 时,两式形式上相等。需要注意的是,两个平均并不是严格相等的,这里只是为了帮助理解。
实际上,EMA计算时,过去 1/(1-\beta) 个时刻之前的数值平均会decay到 \frac 的加权比例,证明如下。
如果将这里的 v_t 展开,可以得到:
v_t = \alpha^n v_ + (1-\alpha)(\alpha^\theta_+ . +\alpha^0\theta_t)
在深度学习的优化中的EMA
上面讲的是广义的ema定义和计算方法,特别的,在深度学习的优化过程中, \theta_t 是t时刻的模型权重weights, v_t 是t时刻的影子权重(shadow weights)。在梯度下降的过程中,会一直维护着这个影子权重,但是这个影子权重并不会参与训练。基本的假设是,模型权重在最后的n步内,会在实际的最优点处抖动,所以我们取最后n步的平均,能使得模型更加的鲁棒。
EMA的偏差修正
实际使用中,如果令 v_0=0 ,且步数较少,ema的计算结果会有一定偏差。
EMA为什么有效
令第n时刻的模型权重(weights)为 v_n ,梯度为 g_n ,可得:
令第n时刻EMA的影子权重为 v_n ,可得:
\begin v_n &= \alpha v_+(1-\alpha)\theta_n \\ &= \alpha (\alpha v_+(1-\alpha)\theta_)+(1-\alpha)\theta_n \\ &= . \\ &= \alpha^n v_0+(1-\alpha)(\theta_n+\alpha\theta_+\alpha^2\theta_+. +\alpha^\theta_) \end
代入上面 \theta_n 的表达,令 v_0=\theta_1 展开上面的公式,可得:
\begin v_n &= \alpha^n v_0+(1-\alpha)(\theta_n+\alpha\theta_+\alpha^2\theta_+. +\alpha^\theta_)\\ &= \alpha^n v_0+(1-\alpha)(\theta_1-\sum_^g_i+\alpha(\theta_1-\sum_^g_i)+. + \alpha^(\theta_1-\sum_^g_i)+\alpha^\theta_)\\ &= \alpha^n v_0+(1-\alpha)(\frac\theta_1-\sum_^\frac>g_i) \\ &= \alpha^n v_0+(1-\alpha^n)\theta_1 -\sum_^(1-\alpha^)g_i\\ &= \theta_1 移动平均线与EMA 移动平均线与EMA -\sum_^(1-\alpha^)g_i \end
\begin \theta_n &= \theta_1-\sum_^g_i \\ v_n &= \theta_1 -\sum_^(1-\alpha^)g_i \end
多种移动平均计算总结(MA,EMA,SMA,DMA,TMA,WMA)
diaohuaidi6355 于 2017-06-23 14:43:00 发布 1050 收藏 3
当前函数值= 当前权重 X 当前价格 + 当前权重的互补值 X 上一次函数值;
06-25 9220
01-26 3万+
06-22 2892
02-26 1020
10-07 4387
一直对EMA的理解都比较模糊,总是不能完全把握,因此,凡是牵涉到EMA的公式都搞不清其内在的数学模型是什么。刚好看到个文章,觉得写的很好。 参考内容:https://www.codeleading.com/article/9441142281/ EMA 公式:EMAtoday=α * Pricetoday + ( 1 - α ) * EMAyesterday; 其中,α为平滑指数,一般取作2/(N+1) 推导公式:EMA(X,N)=[2*X+(N-1)Y’]/(N+1) 下面是对于公式的拆解,分别解出当N=
MOVING_AVERAGE(X,F) 使用大小为 2F+1 的厢式车窗对向量数据 移动平均线与EMA X 进行平滑处理,即,通过对每个元素取其右侧 F 元素和左侧 F 元素的平均值。 极端元素也被平均,但显然数据较少。 保持边缘完好无损。 这个方法真的很快。 MOVING_AVERAGE2(X,M,N) 使用大小为 (2M+1)x(2N+1) 移动平均线与EMA 的 boxcar 窗口平滑矩阵 X,即,通过将每个元素与其周围元素平均它。 这也真的很快。 边缘的元素也被平均,但角落保持完整。 NANMOVING_AVERAGE(X,F)或NANMOVING_AVERAGE(X,F,1)接受向量X中NaN的元素; 后者还插入那些被数字元素包围的 NaN 元素。 NANMOVING_AVERAGE2(X,M,N) 或 NANMOVING_AVERAGE2(X,M,N,1) 接受矩阵 X 中的元素 NaN; 后者还插入那些被数字元
10-25 7万+
____tz_zs 注:本博客概念解释部分均来自 MBA智库百科 一、移动平均法(Moving average,MA) 移动平均法 - MBA智库百科 移动平均法又称滑动平均法、滑动平均模型法 移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量、公司产能等的一种常用方法。移动平均法适用于即期预测。当产品需求既不快速增长也不快速下降,且不存在季节性因素
10-22 4014
已知a[n]为整数数组,设计一个递归算法求这n个元素的平均值 #include #include using namespace std; int num[100]; float fun(int num[],int n) < if(n==0) return num[n]; else return (num[n-1]+(n-1)*fun(num,n-
02-04 2858
双重指数移动均线(DEMA)
Hi everyone! CAUTION. This is only an indicator. Do not rely 100% on it. I made this 移动平均线与EMA indicator hoping to help everyone with this specific Pull Back Scalping Strategy. RULES: Time Chart of 5minuts Long Condition - "EMA Red Line" below the "EMA Blue Line" and wait for a green long signal. Short Condition - "EMA Red Line" below the "EMA Blue Line" and wait for a.
Indicator that shows buy/sell signals based on price action and volume as it relates to a double EMA. If the candle is above the double EMA, we look for candles with long wicks on the top indicating selling pressure. If the candle is below the double EMA , we look for candles with a long 移动平均线与EMA bottom wick indicating buying pressure. The user defined parameters are the.
Este indicador gráfica la medias móviles (SMA, EMA, DEMA ) más utilizadas, pero ajustables a la longitud que se desee analizar sus cruces, junto con las bandas Bollinger también ajustable. This is for evaluation only, and it is not recommended to use with real money. It is a work in progress. I read your comments.
Hi everyone! CAUTION. This is only an indicator. Do not rely 100% on it. I made this indicator hoping to help everyone with this specific Pull Back Scalping Strategy. RULES: Time Chart of 5minuts LONG Condition - "EMA Red Line" below the "EMA Blue Line" and wait for a green long signal. SHORT Condition - "EMA Red Line" below the "EMA Blue Line" and wait for a.
2 EMAs Based on Hi and Lo are plotted and breakouts are checked
3 EMAs 48 High, 48 Low and 10 Close Trade can be taken when purple line crosses the high (green)
Pine Script version=3 Author CryptoJoncis Heikin-Ashi Smoothed The Heikin-Ashi Smoothed study is based upon the standard Heikin-Ashi study with additional moving average calculations. The following is the calculation formula for the bars: 1. The current bar Open, High, Low, Close values are smoothed individually by using the moving average type specified by the.
This version is the same as DEMARSI with following differences I add take profit to short and long when DEMA MTF 1 is crossing DEMA MTF 2 (they are calculated different that why when you increase int2 in min to longer time the difference between them increse) if you 移动平均线与EMA want the TP to be on signal of fast and slow DEMA RSI 2 (just change the code inside) by putting.
Moving averages are used to determine trend. These moving averages are designed to determine trend with reactive coloring and utilizes a unique version of the Hull moving average. This is one of many iterations of fishnet MAs I have made in the past. I first found fishnet MAs, which is a lot of moving averages of increasing length, from @TusenPix YMMV on.
Nice Dema with TF function design to help you to see trends better and to use it as a backbone for indicator that you choose with DEMA can help you to see trend or filter bad signals make the TF =to the graph timset or higher The mTF is non repainting
ACTION ZONE-ATR MOD v0.1 DOCUMENTATION Overview This tradingview pine script strategy is mainly created to enrich my coding skill. It is a combination of “CDC-ACTIONZONE” and my personal studies of trading techniques in various sources e.g.book, course 移动平均线与EMA or blog. This strategy purposefully built to connect with my automatic trading bot. However, It will be very.