首页 > 解决方案 > 熊猫 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 计算

标签: pythonpandasdataframe

解决方案


复制粘贴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

在此处输入图像描述


推荐阅读