[R-sig-Finance] Rolling correlations with zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 22 17:24:23 CEST 2006


rollmean is much faster than rapply so use the fact that we
can calculate correlations in terms of means, means of squares
and means of cross products.   Doing this appears to be 4.6x
faster for this example:

> library(zoo)
> set.seed(1)
> n <- 10000
> x <- zoo(rnorm(n))
> y <- zoo(rnorm(n))
>
> x. <- rollmean(x, 30)
> y. <- rollmean(y, 30)
>
> xx. <- rollmean(x*x, 30)
> yy. <- rollmean(y*y, 30)
>
> xy. <- rollmean(x*y, 30)
>
> system.time(out <- (xy. - x. * y.) / sqrt((xx. - x.^2) * (yy. - y.^2)))
[1] 0.94 0.12 1.45   NA   NA
>
> system.time(out.rapply <- rapply(cbind(x,y), 30, cor, by.column = FALSE)[,3])
[1]  4.35  0.34 16.21    NA    NA
>
> all.equal(out, out.rapply)
[1] TRUE
>
> R.version.string # XP
[1] "R version 2.2.1, 2005-12-20"


On 5/22/06, jladekarl at worldbank.org <jladekarl at worldbank.org> wrote:
> I'm a novice user of R so, please, excuse me for asking a basic question I hope
> there is a basic answer to:
>
> I have tried to created a correlation matrix using a "zoo object".  I don't have
> any issues using the cor("zoo data") function, but when I try to expand the
> process by introducing rolling correlations I run into problems. I have created
> a workaround using "rapply" specifying the exact variables to use, rather than
> the general zoo object, but it's not very practical solution for large matrixes.
> Any hints?
>
> Jeppe
>
> ##Connect to Bloomberg through session "conn"
> conn <- blpConnect ()
>
> ##download currency data for the last 3 years into a zoo object called "data"
> data <- blpGetData(conn, c("EUR Curncy", "CHF Curncy"), "PX Last",
> start=as.chron(Sys.time() - 86400 * (3*365)))
>
> ##End Bloomberg Session "conn"
> blpDisconnect(conn)
>
> #plot running correlation estimated by last 30 observations rolling by "1"
> EUR.chf.cor <- rapply(data[,c("EUR CURNCY", "CHF CURNCY")], 30,
> function(x) cor(x[,1], x[,2]), by = 1, by.column = FALSE)
> plot(EUR.chf.cor)
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>



More information about the R-SIG-Finance mailing list