Thursday, June 7, 2012

Universal portfolio, part 4

The graph for figure 8.4 requires to be somehow careful about correctly lagging the CRP value when calculating the weights.


# fig 8.4 of Cover "Universal Portfolios"
require(logopt)
x <- nyse.cover.1962.1984
xik <- x[,c("iroqu","kinar")]
nDays <- dim(xik)[1]
Days <- 1:nDays
pik <- cumprod(xik)
alphas <- seq(0,1,by=0.05)
bk <- xik[,1] * 0
w <- xik[,1] * 0
for (i in 1:length(crps)) {
  # we calculate bk by weighting the b by the realized wealth lagged one
  weight <- lag(crp(xik, c(alphas[i], 1-alphas[i])), 1)
  bk <- bk + alphas[i] * weight
  w <- w + weight
}
bk <- bk / w
bk[1] <- 0.5
plot(Days, bk, col="blue", type="l", ylim = range(0.25, range(bk)), 
     main = 'Mix of "iroqu" and "kinar" in Universal Portfolio', 
     ylab='Fraction of "iroqu"')
grid()


The porfolio b ^ k
Figure 8.5, 8.6 and 8.7 share the same format, so we define a function to draw them.



# fig 8.5, 8.6 and 8.7 of Cover "Universal Portfolios"


require(logopt)
x <- nyse.cover.1962.1984


show_pair <- function (x,a,b) { 
  xab <- x[,c(a,b)]
  nDays <- dim(xab)[1]
  Days <- 1:nDays
  pab <- cumprod(xab)
  layout(matrix(c(1,3,4,2), 2, 2, byrow = TRUE))
  main_text <- sprintf("\"%s\" and \"%s\"",a,b)
  plot(Days, pab[,a], col="blue", type="l", ylim=range(pck), 
       main = main_text, ylab="")
  lines(Days, pab[,b], col="red")
  grid() 
  legend("topleft",c(sprintf("\"%s\"",a),
         sprintf("\"%s\"",b)),col=c("blue","red"),lty=c(1,1))


  alphas <- seq(0,1,by=0.05)
  crps <- alphas
  for (i in 1:length(crps)) {
    crps[i] <- crp(xab, c(alphas[i], 1-alphas[i]))[nDays]
  }
  main_text <- sprintf("20 Year Return vs. mix of \"%s\" and \"%s\"",a,b)
  xlab_text <- sprintf("Fraction of \"%s\" in Portfolio", a)
  plot(alphas, crps, col="blue", type="l", ylab="", 
       main=main_text, xlab=xlab_text)
  points(alphas, crps, pch=19, cex=0.5, col="red")
  abline(h=mean(crps), col="green")
  text(0.4,mean(crps)*1.05,labels="Return from Universal Portfolio")
  grid()


  universal <- xab[,1] * 0
  for (i in 1:length(crps)) {
    universal <- universal + crp(xab, c(alphas[i], 1-alphas[i]))
  }
  universal <- universal / length(alphas)
  main_text <- sprintf("Universal Portfolio with \"%s\" and \"%s\"",a,b)
  plot(Days, pab[,a], col="blue", type="l", ylim=range(pab, universal), 
       main = main_text, ylab="")
  lines(Days, pab[,b], col="red")
  lines(Days, universal, col="green",)
  legend("topleft",c(sprintf("\"%s\"",a), sprintf("\"%s\"",b),'"unversal"'),
         col=c("blue","red","green"),lty=c(1,1,1))
  grid()




  bk <- xab[,1] * 0
  w <- xab[,1] * 0
  for (i in 1:length(crps)) {
    # we calculate bk by weighting the b by the realized wealth lagged one
    weight <- lag(crp(xab, c(alphas[i], 1-alphas[i])), 1)
    bk <- bk + alphas[i] * weight
    w <- w + weight
  }
  bk <- bk / w
  bk[1] <- 0.5
  main_text <- sprintf("Mix of \"%s\" and \"%s\" in Universal Portfolio",a,b)
  ylab_text <- sprintf("Fraction of \"%s\"", a)
  plot(Days, bk, col="blue", type="l", ylim = range(range(bk)), 
       main=main_text, ylab=ylab_text)
  grid()
}


# fig 8.5
show_pair(x,"comme","kinar")



Commercial Metals and Kin Ark
# fig 8.6
show_pair(x,"comme","kinar")

Commercial Metals and Mei Corp.


# fig 8.7
show_pair(x,"coke","ibm")


IBM and Coca-Cola
All original figures are now reproduced.


Sunday, June 3, 2012

Universal portfolio, part 3

After the theoretical analysis, section 8 of Universal Portfolios provides examples.  We now use logopt and R to reproduce them, the first three in this post.

The examples of Universal Portfolios use a long time series of relative stock prices on the NYSE originally accumulated by Cover himself.  This series has been reused by many authors to allow for comparisons of algorithms, and the series is available in logopt itself (originally downloaded from this website).

The two first figures introduce two specific stocks (8.1), then a series of CRP and Universal Portfolio for these two stocks (8.2).  Each figure is drawn using a standalone snippet of code.



Performance of Iroquois brands and Kin Ark



Performance of rebalanced portfolio




Performance of universal portfolio
Updated on 2012/07/25 to correct code errors and use github:gist as repository for the code examples.

Saturday, June 2, 2012

Universal portfolio, part 2

Universal Portfolios has a classical structure: introduction of the problem, defining then proving a proposition, provide some illustration on real data, conclusion.

The introduction first defines a reference portfolio, the best constant rebalanced portfolio (BCRP).  The BCRP is found after all prices are known, so it is a rather tough reference to meet.  In particular, by definition, Cover shows that the BRCP:

  • exceeds the best stock (proposition 2.1 in the article)
  • exceeds the value line (proposition 2.2)
  • exceeds the arithmetic mean (proposition 2.3) 
And still Cover then proves that a specific portofolio selection algorithm, called Universal Portfolio (UP) can asymptotically match the growth rate of the BCRP.  This comes by showing that the ratio between BCRP and UP goes asymptotically to zero but slower than as an exponential, and thus the ratio of the growth rates tends to 1.  The exact expression (6.1 in the article) for the ratio is

S n ^ S n * ( 2 Π n ) m - 1 m - 1 J * 1/2
S n ^ is the value of the Universal Portfolio after n periods
S n * is the value of the BCRP after n periods
m is the number of stocks considered
J * is a measure of "curvature" of the time series of price relatives
The important aspect is that the ratio is not an exponential function of n and so the difference in growth rates between the two portfolios is asymptotically 0.

Unfortunately, the Universal Portfolio is a little bit disappointing, it effectively splits the initial investment across all possible CRP, so that it is sure to hit the best one.  This is only done on paper, there is only one real portfolio but its composition is matched to the combination of all possible CRP at any moment in time.  This happens to be difficult to do (exponential in the number of stocks for the obvious approach), with a number of simpler approximations available.

Universal Portfolios then uses a long sequence of historical prices to show real applications.  This will be the subject of the next post in this series.

Wednesday, May 30, 2012

Correcting the package on R-forge

Writing a blog did help, it forced me to look at R-forge and discover that logopt was not building correctly. After installing back all necessary tools, I have (almost) recreated the original environment and was able to solve the problem, committing the first change in a long time.  Hopefully R-forge will have a valid build tomorrow.

One of the reasons I stopped working on logopt was a disk crash, in which I lost some files used in building the package.  Obviously the source files committed on the server were safe (an other reason to open source your code), but I lost some local files used to build the vignette and currently I cannot build the vignette anymore, even if I recovered the source code.  This will be one pending task after finishing the planned sequence of write-ups on Universal Portfolios.

Sunday, May 27, 2012

Universal portfolio, part 1

Thomas M. Cover was a well known Stanford professor working in the field of information theory.  He studied portfolio theory from an information theory standpoint, in a tradition started by John L. Kelly, Jr in A New Interpretation of Information Rate, Bell System Technical Journal 35: 917–926, 1956.  A large number of his portfolio theory articles are available on line.

Universal Portfolios. Mathematical Finance, 1(1): 1-29, January 1991 is the most interesting for me:

  • It is highly didactic and relatively easy to understand.
  • It includes a number of interesting graphs, contrary to many articles that restrict themselves to numeric tables.
  • It presents a portfolio approach that is guaranteed to work "reasonably well" under all conditions.
What not to like?  Reading this article led to some R code that eventually became the seed for logopt.

But what does "reasonably well"?  In Cover own words
We exhibit an algorithm for portfolio selection that asymptotically outperforms the best stock in the market.
This seems sensational, and is also slightly misleading as I'll discuss later on.


Constant rebalanced portfolio (CRP)

In Cover's words again,
"Universal Portfolios," Cover [1991] introduces a portfolio that does as well to first order in the exponent as the best constant rebalanced portfolio
A constant rebalanced portfolio buys and sells shares so that the ratio of each stock in the portfolio is a constant.  Intrinsically this corresponds to sell winners to buy losers.  The evolution of the value of a constant rebalanced portfolio is easy to describe mathematically, and this is why Cover is able to derive a portfolio selection algorithm that is guaranteed to track (in a mathematical sense) the best possible constant rebalanced portfolio.

Saturday, May 26, 2012

logopt is a R package that I have neglected for too long a time for a variety of reasons.  Logopt comes from log optimal portfolio, a part of the more general portfolio theory.  This blog is an attempt at reviving it through an increase in exposure, with these goals:

  • Clean up the existing code and discuss its implementation
  • Present applications of the code, generally by recreating the results of academic articles
  • Write new code
The base idea will be "don't break the chain", post and commit code on a regular base, in small chunks.  The first set of posts will discuss the initial motivation for logopt. a seminal article by Thomas M. Cover. Universal Portfolios. Mathematical Finance, 1(1): 1-29, January 1991.