How to use df.rolling(window, min_periods, win_type='exponential').sum()

自古美人都是妖i 提交于 2021-02-07 08:36:37

问题


I would like to calculate the rolling exponentially weighted mean with df.rolling().mean(). I get stuck at the win_type = 'exponential'.

I have tried other *win_types such as 'gaussian'. I think there would be sth a little different from 'exponential'.

dfTemp.rolling(window=21, min_periods=10, win_type='gaussian').mean(std=1)
# works fine

but when it comes to 'exponential',

dfTemp.rolling(window=21, min_periods=10, win_type='exponential').mean(tau=10)
# ValueError: The 'exponential' window needs one or more parameters -- pass a tuple.

How to use win_type='exponential'... Thanks~~~


回答1:


I faced same issue and asked it on Russian SO:

Got the following answer:

x.rolling(window=(2,10), min_periods=1, win_type='exponential').mean(std=0.1)

You should pass tau value to window=(2, 10) parameter directly where 10 is a value for tau.

I hope it will help! Thanks to @MaxU




回答2:


You can easily implement any kind of window by definining your kernel function.

Here's an example for a backward-looking exponential average:

import pandas as pd
import numpy as np

# Kernel function ( backward-looking exponential )
def K(x): 
    return np.exp(-np.abs(x)) * np.where(x<=0,1,0)

# Exponenatial average function
def exp_average(values):
    N = len(values)
    exp_weights = list(map(K, np.arange(-N,0) / N ))
    return values.dot(exp_weights) / N

# Create a sample DataFrame
df = pd.DataFrame({
    'date': [pd.datetime(2020,1,1)]*50 + [pd.datetime(2020,1,2)]*50,
    'x'   : np.random.randn(100)
})

# Finally, compute the exponenatial moving average using `rolling` and `apply`
df['mu'] = df.groupby(['date'])['x'].rolling(5).apply(exp_average, raw=True).values
df.head(10)

Notice that, if N is fixed, you can significantly reduce the execution time by keeping the weights constant:

N = 10
exp_weights = list(map(K, np.arange(-N,0) / N ))

def exp_average(values):
    return values.dot(exp_weights) / N



回答3:


Short answer: you should use pass tau to the applied function, e.g., rolling(d, win_type='exponential').sum(tau=10). Note that the mean function does not respect the exponential window as expected, so you may need to use sum(tau=10)/window_size to calculate the exponential mean. This is a BUG of current version Pandas (1.0.5).

Full example:

# To calculate the rolling exponential mean
import numpy as np
import pandas as pd

window_size = 10
tau = 5
a = pd.Series(np.random.rand(100))
rolling_mean_a = a.rolling(window_size, win_type='exponential').sum(tau=tau) / window_size

The answer of @Илья Митусов is not correct. With pandas 1.0.5, running the following code raises ValueError: exponential window requires tau:

import pandas as pd
import numpy as np

pd.Series(np.arange(10)).rolling(window=(4, 10), min_periods=1, win_type='exponential').mean(std=0.1)

This code has many problems. First, the 10 in window=(4, 10) is not tau, and will lead to wrong answers. Second, exponential window does not need the parameter std -- only gaussian window needs. Last, the tau should be provided to mean (although mean does not respect the win_type).



来源:https://stackoverflow.com/questions/57518576/how-to-use-df-rollingwindow-min-periods-win-type-exponential-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!