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}}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to