Skip to main content
9 events
when toggle format what by license comment
Feb 26 at 9:53 comment added Avraham @M.Beausoleil, it's a fast way of doing the "sweep". Consider that you are multiplying each entry in the correlation matrix by the product of the component standard deviations. For example, the covariance matrix entry at [1, 2] will be the SD[1] * SD[2] * cor[1, 2]. By turning the SD vector into a matrix and multiplying it by itself (tcrossprod(S) would be even faster), you are "pregenerating" the needed 4x4 matrix of multiplications of SD entries each by each other. All that is left to do is to scale them, elementwise, by the correlations, thus the * R. Which could be first or second.
Jun 8, 2022 at 16:28 history made wiki Post Made Community Wiki by kjetil b halvorsen
Jun 8, 2022 at 16:06 comment added M. Beausoleil What is the idea behind R * smat %*% t(smat)?
S Jun 8, 2022 at 16:06 history suggested M. Beausoleil CC BY-SA 4.0
added microbenchmark and reproducible answer and new way to compute the covariance matrix which is even more efficient!
Jun 8, 2022 at 16:00 review Suggested edits
S Jun 8, 2022 at 16:06
May 12, 2019 at 15:48 comment added whuber On my system (Microsoft's version of R), the outer solution is far faster for large dimensions. It is about eight times faster than the sweep implementation. (If you were to replace the inner sweep by R*S it would be almost twice as fast, but still four times slower than outer.) Replacing outer(S,S) by d <- length(S); (matrix(S,d,1) %*% matrix(S,1,d)) is a little faster. cc @BenBolker
May 12, 2019 at 15:00 comment added kjetil b halvorsen @Ben Bolker: It would be a good exercise to compare speedwise the efficiencies ...
May 12, 2019 at 14:13 comment added Ben Bolker also outer(S,S) * R since * performs the Hadamard (elementwise) product ...
May 12, 2019 at 13:13 history answered kjetil b halvorsen CC BY-SA 4.0