Path: blob/master/sandbox/RFinance2014/optimization_analysis.R
1433 views
library(PortfolioAnalytics)1library(methods)23# rCharts charting functions4source("R/charting.R")56# Set the directory where the optimization results are saved7results.dir <- "optimization_results"8figures.dir <- "figures"910fig.height <- 45011fig.width <- 9501213# mix of blue, green, and red hues14my_colors <- c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c")151617##### Ledoit-Wolf Shrinkage Example #####18load(paste(results.dir, "opt.minVarSample.rda", sep="/"))19load(paste(results.dir, "opt.minVarLW.rda", sep="/"))2021n <- length(opt.minVarSample$portfolio$assets)22# tmp_colors <- sample(colorRampPalette(c("dodgerblue", "gray", "orange"))(n), n, FALSE)23tmp_colors <- colorRampPalette(c("lightgreen", "dodgerblue", "#fff7bc"))(n)24# Chart the weights through time25png(paste(figures.dir, "weights_minVarSample.png", sep="/"))26chart.Weights(opt.minVarSample, main="minVarSample Weights", legend.loc=NULL,27col=tmp_colors)28dev.off()2930w1 <- nvd3WeightsPlot(opt.minVarSample)31w1$chart(color = tmp_colors)32save(w1, file=paste(figures.dir, "w1.rda", sep="/"))333435png(paste(figures.dir, "weights_minVarLW.png", sep="/"))36chart.Weights(opt.minVarLW, main="minVarLW Weights", legend.loc=NULL,37col=tmp_colors)38dev.off()3940w2 <- nvd3WeightsPlot(opt.minVarLW)41w2$chart(color = tmp_colors)42save(w2, file=paste(figures.dir, "w2.rda", sep="/"))4344# Compute the returns and chart the performance summary45ret.minVarSample <- summary(opt.minVarSample)$portfolio_returns46ret.minVarRobust <- summary(opt.minVarLW)$portfolio_returns47ret.minVar <- cbind(ret.minVarSample, ret.minVarRobust)48colnames(ret.minVar) <- c("Sample", "LW")4950png(paste(figures.dir, "ret_minVar.png", sep="/"), height = fig.height, width = fig.width)51charts.PerformanceSummary(ret.minVar, colorset=bluemono)52dev.off()5354##### Market Neutral Example #####55load(paste(results.dir, "opt.dn.rda", sep="/"))5657png(paste(figures.dir, "opt_dn.png", sep="/"), height = fig.height, width = fig.width)58plot(opt.dn, main="Dollar Neutral Portfolio", risk.col="StdDev", neighbors=10)59dev.off()606162# chart.RiskReward(opt, risk.col="StdDev", neighbors=25)63# chart.Weights(opt, plot.type="bar", legend.loc=NULL)64# wts <- extractWeights(opt)65# t(wts) %*% betas66# sum(abs(wts))67# sum(wts[wts > 0])68# sum(wts[wts < 0])69# sum(wts != 0)7071##### Example 3 #####72load(file=paste(results.dir, "opt.minES.rda", sep="/"))73load(file=paste(results.dir, "bt.opt.minES.rda", sep="/"))7475# ES(R, portfolio_method="component", weights=extractWeights(opt.minES[[1]]))76# extractObjectiveMeasures(opt.minES)7778# extract objective measures, out, and weights79xtract <- extractStats(opt.minES)8081# get the 'mean' and 'ES' columns from each element of the list82xtract.mean <- unlist(lapply(xtract, function(x) x[,"mean"]))83xtract.ES <- unlist(lapply(xtract, function(x) x[,"ES"]))848586png(paste(figures.dir, "opt_minES.png", sep="/"), height = fig.height, width = fig.width)87# plot the feasible space88par(mar=c(7,4,4,1)+0.1)89plot(xtract.ES, xtract.mean, col="gray",90main="Minimum ES Portfolios",91xlab="ES", ylab="Mean",92ylim=c(0.005, 0.007),93xlim=c(0.015, 0.085))9495# min ES96points(x=opt.minES[[1]]$objective_measures$ES$MES,97y=opt.minES[[1]]$objective_measures$mean,98pch=15, col="purple")99text(x=opt.minES[[1]]$objective_measures$ES$MES,100y=opt.minES[[1]]$objective_measures$mean,101labels="Min ES", pos=1, col="purple", cex=0.8)102103# min ES with risk budget upper limit on component contribution to risk104points(x=opt.minES[[2]]$objective_measures$ES$MES,105y=opt.minES[[2]]$objective_measures$mean,106pch=15, col="black")107text(x=opt.minES[[2]]$objective_measures$ES$MES,108y=opt.minES[[2]]$objective_measures$mean,109labels="Min ES RB", pos=4, col="black", cex=0.8)110111# min ES with equal (i.e. min concentration) component contribution to risk112points(x=opt.minES[[3]]$objective_measures$ES$MES,113y=opt.minES[[3]]$objective_measures$mean,114pch=15, col="darkgreen")115text(x=opt.minES[[3]]$objective_measures$ES$MES,116y=opt.minES[[3]]$objective_measures$mean,117labels="Min ES EqRB", pos=4, col="darkgreen", cex=0.8)118par(mar=c(5,4,4,1)+0.1)119dev.off()120121# Chart the risk contribution122#chart.RiskBudget(opt.minES[[1]], risk.type="percentage", neighbors=10)123png(paste(figures.dir, "rb_minES.png", sep="/"))124chart.RiskBudget(opt.minES[[2]], main="Risk Budget Limit",125risk.type="percentage", neighbors=10)126dev.off()127128png(paste(figures.dir, "eqrb_minES.png", sep="/"))129chart.RiskBudget(opt.minES[[3]], main="Equal ES Component Contribution",130risk.type="percentage", neighbors=10)131dev.off()132133# Plot the risk contribution of portfolio 1 through time134png(paste(figures.dir, "risk_minES.png", sep="/"))135chart.RiskBudget(bt.opt.minES[[1]], main="Min ES Risk Contribution",136risk.type="percentage", col=my_colors)137dev.off()138# Plot the risk contribution of portfolio 1 through time139png(paste(figures.dir, "weights_minES.png", sep="/"))140chart.Weights(bt.opt.minES[[1]], main="Min ES Weights", col=my_colors)141dev.off()142143# Plot the risk contribution of portfolio 3 through time144png(paste(figures.dir, "risk_minESRB.png", sep="/"))145chart.RiskBudget(bt.opt.minES[[2]], main="Min ES RB Risk Contribution",146risk.type="percentage", col=my_colors)147dev.off()148# Plot the weights of portfolio 2 through time149png(paste(figures.dir, "weights_minESRB.png", sep="/"))150chart.Weights(bt.opt.minES[[2]], main="Min ES RB Weights", col=my_colors)151dev.off()152153# Plot the risk contribution of portfolio 3 through time154png(paste(figures.dir, "risk_minESEqRB.png", sep="/"))155chart.RiskBudget(bt.opt.minES[[3]], main="Min ES EqRB Risk Contribution",156risk.type="percentage", col=my_colors)157dev.off()158# Plot the weights of portfolio 3 through time159png(paste(figures.dir, "weights_minESEqRB.png", sep="/"))160chart.Weights(bt.opt.minES[[3]], main="Min ES EqRB Weights", col=my_colors)161dev.off()162163bt_w3 <- nvd3WeightsPlot(bt.opt.minES[[3]], "multiBarChart")164bt_w3$chart(color = my_colors)165save(bt_w3, file=paste(figures.dir, "bt_w3.rda", sep="/"))166167bt_rb3 <- nvd3RiskPlot(bt.opt.minES[[3]], "multiBarChart")168bt_rb3$chart(color = my_colors)169save(bt_rb3, file=paste(figures.dir, "bt_rb3.rda", sep="/"))170171# Extract the returns from each element and chart the performance summary172ret.bt.opt <- do.call(cbind, lapply(bt.opt.minES,173function(x) summary(x)$portfolio_returns))174colnames(ret.bt.opt) <- c("min ES", "min ES RB", "min ES Eq RB")175176png(paste(figures.dir, "ret_minES.png", sep="/"), height = fig.height, width = fig.width)177charts.PerformanceSummary(ret.bt.opt, colorset=my_colors[c(2,4,6)])178dev.off()179180###181# interactive plot of risk budgets through time using nvd3182# nvd3RiskPlot(bt.opt.minES[[1]])183# nvd3RiskPlot(bt.opt.minES[[2]])184# nvd3RiskPlot(bt.opt.minES[[3]])185###186187##### Example 4 #####188load(file=paste(results.dir, "opt.crra.rda", sep="/"))189load(file=paste(results.dir, "bt.opt.crra.rda", sep="/"))190191CRRA <- function(R, weights, lambda, sigma, m3, m4){192weights <- matrix(weights, ncol=1)193M2.w <- t(weights) %*% sigma %*% weights194M3.w <- t(weights) %*% m3 %*% (weights %x% weights)195M4.w <- t(weights) %*% m4 %*% (weights %x% weights %x% weights)196term1 <- 0.5 * lambda * M2.w197term2 <- (1 / 6) * lambda * (lambda + 1) * M3.w198term3 <- (1 / 24) * lambda * (lambda + 1) * (lambda + 2) * M4.w199out <- -term1 + term2 - term3200out201}202203# Chart the optimization in Risk-Reward space204png(paste(figures.dir, "crra_RR_ES.png", sep="/"))205chart.RiskReward(opt.crra, risk.col="ES")206dev.off()207208png(paste(figures.dir, "crra_RR_StdDev.png", sep="/"))209chart.RiskReward(opt.crra, risk.col="StdDev")210dev.off()211212png(paste(figures.dir, "weights_crra.png", sep="/"), height = fig.height, width = fig.width)213chart.Weights(bt.opt.crra, main="CRRA Weights", col=my_colors)214dev.off()215216# Compute the portfolio returns with rebalancing217ret.crra <- summary(bt.opt.crra)$portfolio_returns218colnames(ret.crra) <- "CRRA"219220# Plot the performance summary of the returns from example 3 and CRRA221png(paste(figures.dir, "ret_crra.png", sep="/"), height = fig.height, width = fig.width)222charts.PerformanceSummary(cbind(ret.bt.opt, ret.crra),223main="Optimization Performance",224colorset=c(my_colors[c(2,4,6)], "black"))225dev.off()226227228229