简体   繁体   中英

Using pandas Exponentially-weighted moving std as a sliding window with *fixed* window size

I would like to use the pandas ewmstd function as a rolling window (using only last N elements) instead of as a expanding window (using all elements). Anyone has a clue how to do this? Here is an example:

set = np.array([0,0,0,0,0,0,0,10,10,10])
set2 = np.array([1,0,0,0,0,0,0,10,10,10])

set_op = pd.ewmstd(set, span=5)
set2_op = pd.ewmstd(set2, span=5)

print 'set_op', set_op
print 'set2_op', set2_op

This outputs:

set_op [ nan 0. 0. 0. 0. 0. 0. 5.37639567  5.57181916  5.06335969]
set_op2 [ nan 0.70 0.51 0.39 0.30 0.24 0.19   5.36   5.55 5.04]

If the operation only uses last 5 elements, set_op[7] should yield the same result as set_op2[7], and ignore the 1 at the beginning of set2.

Thx JohnE, I came up with this and works:

def onetime_ewmstd(arr):
    return pd.ewmstd(arr, span=5)[-1]

set_rollop = pd.rolling_apply(set,5,onetime_ewmstd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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