简体   繁体   English

滚动加权移动平均线 pandas

[英]rolling weighted moving average pandas

I've scoured stackoverflow, and I can't find exactly what fits the bill for me.我搜索了 stackoverflow,但找不到适合我的东西。

I'm calculating a weighted moving average for a rolling window.我正在计算滚动 window 的加权移动平均值。

The equation is:等式是:

 #weighted average temp with smoothing factor, a
 #T_w = sum[k=1,24](a^(k-1)*T(t-k)) / sum[k=1,24]a^(k-1)

Seems easy enough, but I need to apply this average to a rolling window. I can do rolling mean (simple moving average):看起来很简单,但我需要将此平均值应用于滚动 window。我可以做滚动平均值(简单移动平均值):

 T_ = pd.DataFrame()
 T_ = temps['T'].rolling(window=24).mean()

But now I want to apply weights for ONLY the window I'm averaging over.但现在我只想对我平均计算的 window 应用权重。 Python's.ewm() doesn't cut the mustard, because I want the weights to be just for the window I'm "rolling" over. Python 的 ewm() 并没有削减芥末,因为我希望权重仅适用于我正在“滚动”的 window。

I've found a few snippets that seem like they might work, but components fail:我发现了一些看起来可能有效的片段,但组件失败了:

from functools import partial

window = 13
alpha = 1-ln(2)/3    # This is ewma's decay factor.
weights = list(reversed([(1-alpha)**n for n in range(window)]))
ewma = partial(average, weights=weight)
rolling_average = series.rolling(window).apply(ewma)

Here, the problem I've run into is how partial() calls average() -- this was introduced here - Create a rolling custom EWMA on a pandas dataframe - but I can't comment yet (newb), and I don't know where to take this.在这里,我遇到的问题是 partial() 调用 average() 的方式——这是在这里介绍的——Create a rolling custom EWMA on a pandas dataframe——但我还不能评论(newb),我不不知道在哪里拿这个。

Another solution I have implemented, but it doesn't do exactly what I need:我已经实施的另一个解决方案,但它并不能完全满足我的需要:

alpha = 0.1    # This is my smoothing parameter
weights = list(reversed([(1-alpha)**n for n in range(window)]))
def f(w):
    def g(x):
        return (w*x).mean()
    return g
T_ = pd.DataFrame()
T_ = temps['T'].rolling(window=24).apply(f(weights))

Based on a proposed solution here: Calculating weighted moving average using pandas Rolling method The problem with this approach is that it calculates the mean, whereas I need effectively something like this:基于这里提出的解决方案: Calculating weighted moving average using pandas Rolling method这种方法的问题是它计算平均值,而我实际上需要这样的东西:

return (w*x).sum() / w.sum()

But that doesn't work, because但这是行不通的,因为

AttributeError: 'list' object has no attribute 'sum'

How do I calculate a rolling weighted moving average with a specified window (here, the last 24 hours), and a specified smoothing parameter a (which is only applied to the last 24 hours)?如何使用指定的 window(此处为过去 24 小时)和指定的平滑参数 a(仅适用于过去 24 小时)计算滚动加权移动平均值?

Instead of return (w*x).sum() / w.sum() , try sum(w*x) / sum(w) . 尝试使用sum(w*x) / sum(w)而不是return (w*x).sum() / w.sum() sum(w*x) / sum(w) This will get you past the AttributeError at least. 这至少可以使您摆脱AttributeError的困扰。

sum is a Python built-in that will work on any iterable holding objects that can be summed (ie added objects that can be added together with + ). sum是Python的内置函数,可用于任何可求和的可迭代持有对象(即可以与+一起添加的添加对象)。 Examples of these objects include int , float , etc. 这些对象的示例包括intfloat等。

you can set the raw=True in.apply function. it makes the input as ndarray.您可以设置 raw=True in.apply function。它使输入为 ndarray。 then you can multiply that with normalize weights: temps.rolling(24, axis=1).apply(lambda x: np.sum(x * w), raw=True)然后你可以将其与归一化权重相乘: temps.rolling(24, axis=1).apply(lambda x: np.sum(x * w), raw=True)

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

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