[R] manipulation of list of matrices

murali.menon at uk.abnamro.com murali.menon at uk.abnamro.com
Fri Dec 7 12:06:07 CET 2007


Folks,

I have a list of correlation matrices of equities, say, and I need to 
manipulate each matrix: I weight each equity by its median absolute 
correlation against all the other equities, such that the equity with the 
least such correlation gets the highest weight. Something like min(over 
all average correlations) / average correlation of equity

So I do the following:

> a <- lapply(1:1000, function(n) {b <- matrix(runif(45*45, min = -1, max 
= 1), ncol = 45); diag(b) <- 1; b})

I write the following function:

constructCorrelationWeights <- function(rollCorr, FUN = median)
{
    corWt <- sapply(rollCorr,
                    function(corMat)
                    {
                        wt <- apply(corMat, 1, function(x) FUN(abs(x)))
                        min(wt) / wt
                    })
    t(corWt)
}

> system.time(corWt <- constructCorrelationWeights(a))
[1] 33.42 18.03 79.55

Is there a more efficient and cleverer way to accomplish this? 

Many thanks,

Murali


---------------------------------------------------------------------------
This message (including any attachments) is confidential...{{dropped:3}}



More information about the R-help mailing list