- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的原始数据框是这样的:
Date C
0 a
1 b
2 c
3 d
这是股票数据。0,1,2,3 是时间,C:Close 是 float 。
我需要能够将 EMA(指数移动平均线)列添加到原始数据帧中,该数据帧是通过当前列 C 计算得到的和之前的新列 ('EMA')。
Chrome :http://investexcel.net/how-to-calculate-ema-in-excel/
所以结果应该是这样的
C EMA
0 a start value as ema0
1 b (ema0*alpha) + (b * (1-alpha)) as ema1
2 c (ema1*alpha) + (c * (1-alpha)) as ema2
3 d (ema2*alpha) + (d * (1-alpha)) as ema3
4 e (ema3*alpha) + (e * (1-alpha)) as ema4
... ... ....
起始值将是一个简单的平均值,因此我尝试了以下方法。创造起始值(value)是第一个条件但在计算 EMA 值时,它不适用于第二种情况。
ema_period = 30
myalpha = 2/(ema_period+1)
data['EMA'] = np.where(data['index'] < ema_period,data['C'].rolling(window=ema_period, min_periods=ema_period).mean(), data['C']*myalpha +data['EMA'].shift(1)*(1-myalpha) )
最佳答案
您所附图像中所需的 EWMA:
代码:
ema_period = 12 # change it to ema_period = 30 for your case
myalpha = 2/(ema_period+1)
# concise form : df.expanding(min_periods=12).mean()
df['Expand_Mean'] = df.rolling(window=len(df), min_periods=ema_period).mean()
# obtain the very first index after nulls
idx = df['Expand_Mean'].first_valid_index()
# Make all the subsequent values after this index equal to NaN
df.loc[idx:, 'Expand_Mean'].iloc[1:] = np.NaN
# Let these rows now take the corresponding values in the Close column
df.loc[idx:, 'Expand_Mean'] = df['Expand_Mean'].combine_first(df['Close'])
# Perform EMA by turning off adjustment
df['12 Day EMA'] = df['Expand_Mean'].ewm(alpha=myalpha, adjust=False).mean()
df
获得 EWMA:
DF
构造:
index = ['1/2/2013','1/3/2013','1/4/2013','1/7/2013','1/8/2013','1/9/2013', '1/10/2013','1/11/2013',
'1/14/2013','1/15/2013','1/16/2013','1/17/2013','1/18/2013','1/22/2013','1/23/2013',
'1/24/2013','1/25/2013','1/28/2013','1/29/2013','1/30/2013']
data = [42.42, 43.27, 43.66, 43.4, 43.4, 44.27, 45.01, 44.48, 44.34,
44.44, 44.08, 44.16, 44.04, 43.74, 44.27, 44.11, 43.93, 44.35,
45.21,44.92]
df = pd.DataFrame(dict(Close=data), index)
df.index = pd.to_datetime(df.index)
关于python - 添加一个列(EMA),这是 pandas 中先前新列值的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40782248/
我的应用将 SceneKit 内容的“页面”与图像和文本交替。当我从图像页面前进到新的 SceneKit 页面时,前一个 SceneKit 页面中的内容会短暂显示,然后被新内容替换。时髦。 我只使用一
我正在尝试处理(在 C# 中)包含一些数字数据的大型数据文件。给定一个整数数组,如何对其进行拆分/分组,以便如果下一个 n(两个或更多)是负数,则前一个 n 元素被分组。例如,在下面的数组中,应该使用
刚接触promises,研究过。所以我的代码和我的理解: sql.connect(config).then(function(connection) { return connection.req
目前我在 if (roobaf) block 中有一些代码,这取决于 foo 和 bar 是否为假。我可以在 block 内再次检查这些条件,但感觉像是不必要的代码重复。 if (foo) {
我是一名优秀的程序员,十分优秀!