简体   繁体   English

Pandas 在固定时间窗口内呈指数加权移动平均线

[英]Pandas exponentially weighted moving average over fixed time window

I am trying to do an exponentially-weighted moving average, where decay is specified in terms of halflife on a datetime column, using pandas ewm function.我正在尝试使用 pandas ewm 函数进行指数加权移动平均值,其中衰减是根据日期时间列上的半衰期指定的。 Pandas ewm function works similar to the pandas expand function in that it rolls over the whole dataframe. Pandas ewm 函数的工作原理与 pandas expand 函数类似,因为它滚动整个数据帧。 In my case however, I need to specify a fixed time window or offset over which the ewm function is applied.但是,在我的情况下,我需要指定一个固定的时间窗口或偏移量,在该时间窗口或偏移量上应用 ewm 函数。 In other words, an ewma with a cutoff or "max_periods" parameter.换句话说,一个带有截止或“max_periods”参数的 ewma。

My solution to this is as follows:我对此的解决方案如下:

df = pd.DataFrame({'a': np.random.randint(5, size=24),
                   'b': ["S", "A"] * 12,
                   'c': pd.date_range(start='1/1/2018', end='12/12/2018', freq='15D')})

df.groupby('b').rolling('60d', on='c')['a'].apply(lambda x: x.ewm(halflife='15d', times=x.index).mean().tail(1))

My solution is very inefficient.我的解决方案非常低效。 Looking for something faster.寻找更快的东西。

I came up with the following, but it's only slightly faster when I timed it.我想出了以下方法,但是当我计时时它只会稍微快一点。 I'd be curious to know if there's a better solution.我很想知道是否有更好的解决方案。

WEIGHTS = [pow(2, i) for i in range(10)]  # at least as many as the size of your window

def weighted_avg(df):
    weights = WEIGHTS[0:len(df)]
    return df.mul(weights).sum() / sum(weights)

df.groupby('b').rolling('60d', on='c')['a'].apply(weighted_avg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM