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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# figure 8.1 of Cover "Universal Portfolios" | |
library(logopt) | |
data(nyse.cover.1962.1984) | |
x <- coredata(nyse.cover.1962.1984) | |
xik <- x[,c("iroqu","kinar")] | |
nDays <- dim(xik)[1] | |
Days <- 1:nDays | |
pik <- apply(xik,2,cumprod) | |
plot(Days, pik[,"iroqu"], col="blue", type="l", | |
ylim=range(pik), main = '"iroqu" and "kinar"', ylab="") | |
lines(Days, pik[,"kinar"], col="red") | |
grid() | |
legend("topright",c('"iroqu"','"kinar"'), | |
col=c("blue","red"),lty=c(1,1)) |
![]() |
Performance of Iroquois brands and Kin Ark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fig 8.2 of Cover "Universal Portfolios" | |
library(logopt) | |
data(nyse.cover.1962.1984) | |
x <- coredata(nyse.cover.1962.1984) | |
xik <- x[,c("iroqu","kinar")] | |
nDays <- dim(xik)[1] | |
Days <- 1:nDays | |
alphas <- seq(0,1,by=0.05) | |
crps <- alphas | |
for (i in 1:length(crps)) { | |
crps[i] <- crp(xik, c(alphas[i], 1-alphas[i]))[nDays] | |
} | |
plot(alphas, crps, col="blue", type="l", ylab="", | |
main='20 Year Return vs. mix of "iroqu" and "kinar"', | |
xlab='Fraction of "iroqu" in Portfolio') | |
points(alphas, crps, pch=19, cex=0.5, col="red") | |
abline(h=mean(crps), col="green") | |
text(0.5,mean(crps)*1.05,labels="Return from Universal Portfolio") | |
grid() |
![]() |
Performance of rebalanced portfolio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fig 8.3 of Cover "Universal Portfolios" | |
library(logopt) | |
data(nyse.cover.1962.1984) | |
x <- coredata(nyse.cover.1962.1984) | |
xik <- x[,c("iroqu","kinar")] | |
nDays <- dim(xik)[1] | |
Days <- 1:nDays | |
pik <- apply(xik,2,cumprod) | |
alphas <- seq(0,1,by=0.05) | |
universal <- xik[,1] * 0 | |
for (i in 1:length(alphas)) { | |
universal <- universal + crp(xik, c(alphas[i], 1-alphas[i])) | |
} | |
universal <- universal/length(alphas) | |
plot(Days, pik[,"iroqu"], col="blue", type="l", ylim=range(pik, universal), | |
main = 'Universal Portfolios with "iroqu" and "kinar"', ylab="") | |
lines(Days, pik[,"kinar"], col="red") | |
lines(Days, universal, col="green") | |
legend("topleft",c('"iroqu"','"kinar"','"universal"'), | |
col=c("blue","red","green"),lty=c(1,1,1)) | |
grid() |
![]() |
Performance of universal portfolio |
Updated on 2012/07/25 to correct code errors and use github:gist as repository for the code examples.
> lines(Days, universal, col="green")
ReplyDeleteFehler in xy.coords(x, y) : 'x' and 'y' lengths differ
> universal
Data:
numeric(0)
Index:
NULL
Hello Horstwolf, can you try to add
Deletedata(nyse.cover.1962.1984) after library(logopt) and check if that works.
Thanks for reading the blog.
Thank you, I am not sure about the exact problem, but I found at least one error. You need to explicitly load nyse.cover.1962.1984 via command data(nyse.cover.1962.1984).
ReplyDeleteI'll update the code, and please feel free to indicate if that solves your problem.