pandas 算出ewm错误吗? [英] Does Pandas calculate ewm wrong?

查看:131
本文介绍了 pandas 算出ewm错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从数据框中的财务数据计算指数移动平均值(EMA)时,似乎熊猫的ewm方法不正确.

When trying to calculate the exponential moving average (EMA) from financial data in a dataframe it seems that Pandas' ewm approach is incorrect.

在以下链接中对基本知识进行了很好的解释: http://stockcharts.com/school/doku.php?id=chart_school :technical_indicators:moving_averages

The basics are well explained in the following link: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages

在对Pandas进行解释时,采用的方法如下(使用"adjust"参数为False):

When going to Pandas explanation, the approach taken is as follows (using the "adjust" parameter as False):

   weighted_average[0] = arg[0];
   weighted_average[i] = (1-alpha) * weighted_average[i-1] + alpha * arg[i]

在我看来,这是不正确的. "arg"应为(例如)结束值,但是arg [0]是第一个平均值(即,所选时间段长度的第一组数据的简单平均值),而不是第一个结束值.因此,arg [0]和arg [i]永远不能来自同一数据.使用"min_periods"参数似乎无法解决此问题.

This in my view is incorrect. The "arg" should be (for example) the closing values, however, arg[0] is the first average (i.e. the simple average of the first series of data of the length of the period selected), but NOT the first closing value. arg[0] and arg[i] can therefore never be from the same data. Using the "min_periods" parameter does not seem to resolve this.

任何人都可以向我解释如何(或是否可以)使用熊猫来正确计算数据的EMA吗?

Can anyone explain me how (or if) Pandas can be used to properly calculate the EMA of data?

推荐答案

有几种初始化指数移动平均线的方法,所以我不会说熊猫做错了,只是有所不同.

There are several ways to initialize an exponential moving average, so I wouldn't say pandas is doing it wrong, just different.

这将是您想要的一种计算方法:

Here would be a way to calculate it like you want:

In [20]: s.head()
Out[20]: 
0    22.27
1    22.19
2    22.08
3    22.17
4    22.18
Name: Price, dtype: float64

In [21]: span = 10

In [22]: sma = s.rolling(window=span, min_periods=span).mean()[:span]

In [24]: rest = s[span:]

In [25]: pd.concat([sma, rest]).ewm(span=span, adjust=False).mean()
Out[25]: 
0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
5           NaN
6           NaN
7           NaN
8           NaN
9     22.221000
10    22.208091
11    22.241165
12    22.266408
13    22.328879
14    22.516356
15    22.795200
16    22.968800
17    23.125382
18    23.275312
19    23.339801
20    23.427110
21    23.507635
22    23.533520
23    23.471062
24    23.403596
25    23.390215
26    23.261085
27    23.231797
28    23.080561
29    22.915004
Name: Price, dtype: float64

这篇关于 pandas 算出ewm错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆