[This article was first published on logopt: a journey in R, finance and open source, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

After reproducing all original figures and tables from Universal Portfolios, R coupled with modern processors allows to perform some more analysis.

First we calculate the final wealth of the universal portfolio for all possible pairs of stocks, and show the corresponding cumulative probability function, and which pair is the best.  This shows how concise R code can be.

library(logopt)
x <- coredata(nyse.cover.1962.1984)
w <- logopt:::x2w(x)
nDays <- dim(x)
nStocks <- dim(x)
Days <- 1:nDays

FinalWealth <- function(cols) {
xij <- x[,cols]
uc <- universal.cover(xij, 20)
return(uc[length(uc)])
}

ws <- combn(1:nStocks, 2, FinalWealth)
plot(ecdf(ws),col=”blue”,pch=19,cex=0.5)
grid()

# show the best pair
BestIdx <- combn(1:nStocks,2)[,which.max(ws)]
cat(sprintf(“Max final wealth: %.4f for pair (%s,%s)\n”, max(ws), colnames(x)[BestIdx], colnames(x)[BestIdx]))

The result is the figure below, plus this line

Max final wealth: 78.4742 for pair (comme,kinar)

There are two interesting observations to make:
• There is a long, thin tail.  In other words, there are some pairs giving good results, but they are relatively rare.
• The best pair happens to be one of the examples in Cover’s article, probably not a coincidence.
An even better metric is the ratio between the final wealth and the wealth of the best stock in the pair.  It is expected that the universal portfolio gets better performance when the underlying stocks perform better.  The code below is very similar to the code above, but calculating the ratio of wealth as a final step.  It needs the same prolog as above (not shown).

WealthRatio <- function(cols) {
xij <- x[,cols]
uc <- universal.cover(xij, 20)
return(uc[length(uc)]/max(w[length(uc),cols]))
}

wrs <- combn(1:nStocks, 2, WealthRatio)
plot(ecdf(wrs),col=”blue”,pch=19,cex=0.5)
grid()

# show the best and worst pairs
BestIdx <- combn(1:nStocks,2)[,which.max(wrs)]
cat(sprintf(“Max increase from best stock: %.4f for pair (%s,%s)\n”, max(wrs), colnames(x)[BestIdx], colnames(x)[BestIdx]))
WorstIdx <- combn(1:nStocks,2)[,which.min(wrs)]
cat(sprintf(“Min ratio from best stock: %.4f for pair (%s,%s)\n”, min(wrs), colnames(x)[WorstIdx], colnames(x)[WorstIdx]))

The result is the figure below, plus these lines

Max increase from best stock: 4.3379 for pair (iroqu,kinar)
Min ratio from best stock: 0.3773 for pair (dupont,morris)
And we can do similar observations:
• There is a long, thin tail, even thinner than for the wealth itself.  In other words, there are some pairs giving good results, but they are relatively rare.
• In the majority of the case (about 60%), the wealth of universal portfolio is below the wealth of the best stock in the pair, sometimes significantly so.
• The best pair happens to be another one of the examples in Cover’s article, probably not a coincidence either.
The general feeling is that universal portfolio is a valid approach, but maybe not as performing as could be deduced from the original article that cherry picked the examples.