快速的方法来计算滑动窗口上的事件 [英] Fast way to count events over a sliding window
问题描述
x = rnorm(100000)
,而不是做一个 1000
长度滑动窗口移动平均,我想要做一个 1000
长度滑动窗口,它计算 x
高于 0.2
。 例如,
x < - rnorm
start< - 1:1000
record< - list()
while(start [length(start)]< = length(x)){
record [ [length(record)+ 1]] < - length(which(x [start]> 0.2))/ length(start)
start < - start + 1
print length(record)]]); flush.console()
}
对于大 length(x)
。什么是高效的方法?
我的贡献是计算条件的累积和之间的滞后差>
cumdiff = function(x)diff(c(0,cumsum(x> .2)),20)
以及
filt = function(x)filter(x> 0.2,rep(1,20),sides = 1)
library(TTR); ttr = function(x)runSum(x> .2,20)
cumsub = function(x){z- cumsum(c(0,x> 0.2)); tail(z,-20) - head(z,-20)}
>
>库(microbenchmark)
> set.seed(123); xx = rnorm(100000)
> microbenchmark(cumdiff(xx),filt(xx),ttr(xx),cumsub(xx))
单位:毫秒
expr min lq median uq max neval
cumdiff(xx)11.192005 12.387862 12.469253 12.77588 13.72404 100
filt(xx)20.979503 22.058045 22.442765 23.02612 62.91730 100
ttr(xx)8.390923 10.023934 10.119772 10.46309 11.04173 100
cumsub(xx)7.015654 8.483432 8.538171 8.73596 9.65421 100
这些不同在于如何表示结果的细节( filt
和 ttr
具有领先的NAs),并且只有过滤器
处理嵌入式NA的
> xx [22] = NA
> head(cumdiff(xx))#NA's propagate,silently
[1] 9 9 NA NA NA NA
> ttr(xx)
在runSum中的错误(x> 0.2,20):系列包含非领先的NAs
>尾(filt(xx),-19)
[1] 9 9 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 8 8 9
...
Suppose I have x = rnorm(100000)
and instead of doing a 1000
length sliding window moving average, I wanted to do a 1000
length sliding window that counts all the times that x
is above 0.2
.
For example,
x <- rnorm(1004)
start <- 1:1000
record <- list()
while(start[length(start)] <= length(x)) {
record[[length(record) + 1]] <- length(which(x[start] > 0.2))/length(start)
start <- start + 1
print(record[[length(record)]]);flush.console()
}
This becomes unmanegable for large length(x)
. What is a highly efficient method?
My contribution is to calculate the lagged difference between the cumulative sum of the condition
cumdiff = function(x) diff(c(0, cumsum( x > .2)), 20)
which along with
filt = function(x) filter(x > 0.2, rep(1, 20), sides=1)
library(TTR); ttr = function(x) runSum(x > .2, 20)
cumsub = function(x) { z <- cumsum(c(0, x>0.2)); tail(z,-20) - head(z,-20) }
performs ok
> library(microbenchmark)
> set.seed(123); xx = rnorm(100000)
> microbenchmark(cumdiff(xx), filt(xx), ttr(xx), cumsub(xx))
Unit: milliseconds
expr min lq median uq max neval
cumdiff(xx) 11.192005 12.387862 12.469253 12.77588 13.72404 100
filt(xx) 20.979503 22.058045 22.442765 23.02612 62.91730 100
ttr(xx) 8.390923 10.023934 10.119772 10.46309 11.04173 100
cumsub(xx) 7.015654 8.483432 8.538171 8.73596 9.65421 100
These differ in the specifics of how the result is represented (filt
and ttr
have leading NAs, for instance) and only filter
deals with embedded NA's
> xx[22] = NA
> head(cumdiff(xx)) # NA's propagate, silently
[1] 9 9 NA NA NA NA
> ttr(xx)
Error in runSum(x > 0.2, 20) : Series contains non-leading NAs
> tail(filt(xx), -19)
[1] 9 9 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 8 8 9
...
这篇关于快速的方法来计算滑动窗口上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!