Surprisingly Slow Standard Deviation in R

后端 未结 3 2089
死守一世寂寞
死守一世寂寞 2021-01-06 09:52

I am calculating standard deviations on an expanding window where at each point I recalculate the standard deviation. This seems like a fairly straightforward thing to do t

3条回答
  •  攒了一身酷
    2021-01-06 10:20

    You might also try an algorithm that updates the standard deviation (well, actually, the sum of squares of differences from the mean) as you go. On my system this reduces the time from ~0.8 seconds to ~0.002 seconds.

    n <- length(x)
    m <- cumsum(x)/(1:n)
    m1 <- c(NA,m[1:(n-1)])
    ssd <- (x-m)*(x-m1)
    v <- c(0,cumsum(ssd[-1])/(1:(n-1)))
    z <- sqrt(v)
    

    See http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance for details.

    Also see the answers to this question: Efficient calculation of matrix cumulative standard deviation in r

提交回复
热议问题