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

jim green student.northwestern at gmail.com
Mon Apr 30 23:35:27 CEST 2012


Hi Gabor, it seems I can't reproduce your calculation and had problem at

system.time(corr.rapply <- rapply(cbind(x,y), 30, cor, by.column =
FALSE)[,3])

Could you pls advise?

Thanks!
Jim.

---- code ----
> set.seed(1)
> n <- 10000
x <- zoo(rnorm(n))
> x <- zoo(rnorm(n))

system.time({
        x. <- rollmean(x, 30)
        y. <- rollmean(y, 30)

        xx. <- rollmean(x*x, 30)
        yy. <- rollmean(y*y, 30)

        xy. <- rollmean(x*y, 30)
> y <- zoo(rnorm(n))

        corr <- (xy. - x. * y.) / sqrt((xx. - x.^2) * (yy. - y.^2))
})

system.time(corr.rapply <- rapply(cbind(x,y), 30, cor, by.column =
FALSE)[,3])

all.equal(corr, corr.rapply)

>
> system.time({
+         x. <- rollmean(x, 30)
+         y. <- rollmean(y, 30)
+
+         xx. <- rollmean(x*x, 30)
+         yy. <- rollmean(y*y, 30)
+
+         xy. <- rollmean(x*y, 30)
+
+         corr <- (xy. - x. * y.) / sqrt((xx. - x.^2) * (yy. - y.^2))
+ })
   user  system elapsed
  0.427   0.000   0.427
>
> system.time(corr.rapply <- rapply(cbind(x,y), 30, cor, by.column =
> FALSE)[,3])
Error in rapply(cbind(x, y), 30, cor, by.column = FALSE) :
  'object' must be a list
Timing stopped at: 0.034 0 0.034
>
> all.equal(corr, corr.rapply)
Error in mode(current) : object 'corr.rapply' not found


---- session info -------
> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base

other attached packages:
 [1] PerformanceAnalytics_1.0.3.2 quantstrat_0.6.3
 [3] blotter_0.8.4                FinancialInstrument_0.13.1
 [5] RPostgreSQL_0.3-2            DBI_0.2-5
 [7] quantmod_0.3-17              TTR_0.21-0
 [9] Defaults_1.1-1               xts_0.8-6
[11] zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.14.2    lattice_0.20-0





--
View this message in context: http://r.789695.n4.nabble.com/Rolling-correlations-with-zoo-object-tp926073p4599459.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list