# Performance of Universal compared to some references

library(logopt)
x <- coredata(nyse.cover.1962.1984)
w <- logopt:::x2w(x)
nDays <- dim(x)[1]
nStocks <- dim(x)[2] 
Days <- 1:nDays
iWin <- 1 ; plot(1:10)

TupleSizes <- c(2,3,4)
# rewrite the different functions to use assign
# the first one creates a list by evaluating on all tuples
# it uses frames to not do anything if the variable exists in the parent frame

EvaluateOnAllTuples <- function(ListName, TupleSizes, fFinalWealth, ...) {
	if (exists(ListName) == FALSE) {
		LocalList <- list()
		for (i in 1:length(TupleSizes)) {
			TupleSize <- TupleSizes[i]
			ws <- combn(x=(1:nStocks), m=(TupleSize), FUN=fFinalWealth, simplify=TRUE, ...)
			LocalList[[i]] <- ws
		}
		assign(ListName, LocalList, pos=parent.frame())
	}
}
UniversalFinalWealth <- function(cols, ...) {
	x <- list(...)[[1]] ; n <- list(...)[[2]]
	uc <- universal.cover(x[,cols], 20)
	return(uc[length(uc)])
}
EvaluateOnAllTuples("lUniversalFinalWealth", TupleSizes, UniversalFinalWealth, x, 20)

# show best absolute wealth and its composition
for (i in 1:length(TupleSizes)) {
	TupleSize <- TupleSizes[i]
	BestTuple <- which.max(lUniversalFinalWealth[[i]])
	BestStocks <- combn(1:nStocks, TupleSize)[,BestTuple]
	cat(sprintf("Max final wealth %.4f for stocks: ", lUniversalFinalWealth[[i]][BestTuple]))
	cat(colnames(x)[BestStocks]) ; cat("\n")
}


BestStockFinalWealth <- function(cols, ...) { 
	w <- list(...)[[1]]
	return(max(w[nDays,cols])) 
}
EvaluateOnAllTuples("lBestStockFinalWealth", TupleSizes, BestStockFinalWealth, w)

UcrpFinalWealth <- function(cols, ...) {
	x <- list(...)[[1]]
	ucrp <- crp(x[,cols])
	return(ucrp[length(ucrp)])	
}
EvaluateOnAllTuples("lUcrpFinalWealth", TupleSizes, UcrpFinalWealth, x)

BhFinalWealth <- function(cols, ...) {
	x <- list(...)[[1]]
	ubh <- bh(x[,cols])
	return(ubh[length(ubh)])	
}
EvaluateOnAllTuples("lBhFinalWealth", TupleSizes, BhFinalWealth, x)
Colors <- c("blue","green","red")
# a function to compare the ECDF of two lists of final wealths
CompareFinalWealth <- function( L0, L1, MainString, TupleSizes=TupleSizes, 
		                    Colors=c("blue","green","red"), PlotChar = ".",
	                          XLabel="Ratio of final wealths", YLabel="Cumulative probability") {
	nLines <- min(length(L0),length(L1))
	LR <- list() ; XLims = c()
	for(i in 1:nLines) { LR[[i]] <- L0[[i]]/L1[[i]] ; XLims <- range(XLims, LR[[i]]) }
	plot(ecdf(L0[[1]]/L1[[1]]), pch=PlotChar, col=Colors[1], main=MainString,
		xlab=XLabel, ylab=YLabel, xlim= XLims)
	abline(v=1,col="gray",lwd=2)
	for (i in 1:nLines) {
		lines(ecdf(L0[[i]]/L1[[i]]), pch=PlotChar, col=Colors[i])
	}
	legend("bottomright", legend=c("2 stocks","3 stocks","4 stocks"), fill=Colors)
	grid()
	# show best relative wealth and its composition
	for (i in 1:length(TupleSizes)) {
		TupleSize <- TupleSizes[i]
		BestTuple <- which.max(LR[[i]])
		BestStocks <- combn(1:nStocks, TupleSize)[,BestTuple]
		cat(sprintf("Max final relative wealth %.4f for stocks: ", LR[[i]][BestTuple]))
		cat(colnames(x)[BestStocks]) ; cat("\n")
	}
}

if(length(dev.list()) < iWin) { x11() } ; iWin <- iWin + 1 ; dev.set(iWin) ;
CompareFinalWealth(lUniversalFinalWealth, lBestStockFinalWealth, "Universal relative to best stock final wealth", TupleSizes) 

if(length(dev.list()) < iWin) { x11() } ; iWin <- iWin + 1 ; dev.set(iWin) ;
CompareFinalWealth(lUniversalFinalWealth, lBhFinalWealth, "Universal relative to uniform buy and hold final wealth", TupleSizes) 

if(length(dev.list()) < iWin) { x11() } ; iWin <- iWin + 1 ; dev.set(iWin) ;
CompareFinalWealth(lUniversalFinalWealth, lUcrpFinalWealth, "Universal relative to uniform CRP final wealth", TupleSizes)