繁体   English   中英

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

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

我正在尝试使用 pandas ewm 函数进行指数加权移动平均值,其中衰减是根据日期时间列上的半衰期指定的。 Pandas ewm 函数的工作原理与 pandas expand 函数类似,因为它滚动整个数据帧。 但是,在我的情况下,我需要指定一个固定的时间窗口或偏移量,在该时间窗口或偏移量上应用 ewm 函数。 换句话说,一个带有截止或“max_periods”参数的 ewma。

我对此的解决方案如下:

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))

我的解决方案非常低效。 寻找更快的东西。

我想出了以下方法,但是当我计时时它只会稍微快一点。 我很想知道是否有更好的解决方案。

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