# Universal portfolio, part 7

**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)[1]

nStocks <- dim(x)[2]

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[1]], colnames(x)[BestIdx[2]]))

- 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).

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

**leave a comment**for the author, please follow the link and comment on their blog:

**logopt: a journey in R, finance and open source**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.