Skip to content
Prev 22758 / 398502 Next

Running median

I have a Date x Stock (223 x 520) matrix of "trading volume".  I can calculate
a 5-day (past) average in about 1 second using: 

R> apply(vol, 1, filter, filter=c(0, rep(1/5,5)), sides=1)

I would like to do the same with a 5-day median, e.g.:

R> mymed <- function(x, n=5) {
R>   r <- rep(NA, length(x))
R>   for (i in (n+1):length(x)) r[i] <- median(x[i-(1:n)])
R>   return(r)
R> }
R> apply(vol, 1, mymed)

only faster (the above takes 65 seconds).  Is there already a function (or some
C code) to do this?  Any clever way to vectorize it?

smooth() in package "eda" with kind="3" calculates a running median of 3
values, so I may start with the code in library/eda/src/smooth.c, but it
doesn't generalize easily to N values.  Also, decmedian() in package "pastecs"
may be relevant, but doesn't seem any faster than my naive code.

Thanks!