pandas ewm var和std [英] pandas ewm var and std

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

问题描述

我尝试复制指数加权移动方差的计算失败。
这是我使用的代码。

 将pandas导入为pd 
将numpy导入为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)
偏差=(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)+偏差* a *(df-ewma.shift(1))** 2)

var_pandas
Out [100]:
数据
0 NaN
1 0.125000
2 0.359231
3 1.582143
4 7.157121
5 10.080647
6 26.022245

var_calculated
Out [101]:
数据
0 NaN
1 NaN
2 0.261111
3 1.264610
4 6.246149
5 9.135133
6 24.123265

您会看到我仍然无法理解。
感谢您的见解!



我使用以下公式来自:


I unsuccessfully tried to replicate the calculation of exponential weighted moving variance. here is the code I used.

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

as you would see I still have a difference that I couldn't figure out. Grateful for your insights!

I used the above formula from: pandas ewm.std calculation

解决方案

Copy-pasted the code posted by kosnik and build it up to answer this question. below:

# 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

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

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