python - 熊猫 ewm var 和 std
问题描述
我没有成功地尝试复制指数加权移动方差的计算。这是我使用的代码。
import pandas as pd
import numpy as np
l = [12., 12.5, 13.1, 14.6, 17.8, 19.1, 24.5]
df = pd.DataFrame(data=l, columns=['data'])
N = 5
a = 2./(1+N)
bias = (2-a)/2./(1-a)
ewma = df.ewm(span=N).mean()
var_pandas = df.ewm(span=N, adjust=False).var()
var_calculated = (1-a) * (var_pandas.shift(1) + bias * a * (df - ewma.shift(1))**2)
var_pandas
Out[100]:
data
0 NaN
1 0.125000
2 0.359231
3 1.582143
4 7.157121
5 10.080647
6 26.022245
var_calculated
Out[101]:
data
0 NaN
1 NaN
2 0.261111
3 1.264610
4 6.246149
5 9.135133
6 24.123265
如您所见,我仍然无法弄清楚。感谢您的见解!
我使用了上面的公式:pandas ewm.std 计算
解决方案
复制粘贴kosnik发布的代码并构建它来回答这个问题。以下:
# Import libraries
import numpy as np
import pandas as pd
# Create DataFrame
l = [12., 12.5, 13.1, 14.6, 17.8, 19.1, 24.5]
df = pd.DataFrame(data=l, columns=['data'])
# Initialize
N = 5 # Span
a = 2./(1+N) # Alpha
# Use .evm() to calculate 'exponential moving variance' directly
var_pandas = df.ewm(span=N).var()
# Initialize variable
varcalc=[]
# Calculate exponential moving variance
for i in range(0,len(df.data)):
# Get window
z = np.array(df.data.iloc[0:i+1].tolist())
# Get weights: w
n = len(z)
w = (1-a)**np.arange(n-1, -1, -1) # This is reverse order to match Series order
# Calculate exponential moving average
ewma = np.sum(w * z) / np.sum(w)
# Calculate bias
bias = np.sum(w)**2 / (np.sum(w)**2 - np.sum(w**2))
# Calculate exponential moving variance with bias
ewmvar = bias * np.sum(w * (z - ewma)**2) / np.sum(w)
# Calculate standard deviation
ewmstd = np.sqrt(ewmvar)
varcalc.append(ewmvar)
#print('ewmvar:',ewmvar)
#varcalc
df['var_pandas'] = var_pandas
df['varcalc'] = varcalc
df
推荐阅读
- php - 远程连接的 Mysql PDO 访问被拒绝,但 Mysql CLI 有效
- android - 是 JsonRequest
一个好主意? - android - 如何从 Cloud Firestore 制作可读的时间戳?
- python - 用多个变量替换熊猫中的多个列
- javascript - 可排序和新创建的内容
- java - 如何从 Android 中的 gridview 获取项目的背景颜色?
- sql - 使用复制语句插入现有表无效
- terraform - 如何在 terraform v0.12 上构造名称
- c++ - 将编写的类拆分为两个文件(C++)
- excel - 如果未找到值,则在 Excel 公式中添加空白字段