本文介绍了通过滑动窗口计算事件的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 x = rnorm(100000) 而不是做一个 1000 长度的滑动窗口移动平均线,我想做一个 1000> 计算 x 高于 0.2 的所有时间的长度滑动窗口.

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.

例如

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

对于大length(x),这变得难以控制.什么是高效的方法?

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)

随着

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

表现正常

> 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

这些在如何表示结果的细节上有所不同(例如,filtttr 具有领先的 NA)并且只有 filter 交易带有嵌入式 NA

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

这篇关于通过滑动窗口计算事件的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:29