Path: blob/master/sandbox/rp_method_comparison.R
1433 views
library(PortfolioAnalytics)12data(edhec)3R <- edhec[, 1:4]45# set up simple portfolio with leverage and box constraints6pspec <- portfolio.spec(assets=colnames(R))7pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)8pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1)910# generate random portfolios using the 3 methods11rp1 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='sample')12rp2 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='simplex')13rp3 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='grid')1415# show feasible portfolios in mean-StdDev space16tmp1.mean <- apply(rp1, 1, function(x) mean(R %*% x))17tmp1.StdDev <- apply(rp1, 1, function(x) StdDev(R=R, weights=x))18tmp2.mean <- apply(rp2, 1, function(x) mean(R %*% x))19tmp2.StdDev <- apply(rp2, 1, function(x) StdDev(R=R, weights=x))20tmp3.mean <- apply(rp3, 1, function(x) mean(R %*% x))21tmp3.StdDev <- apply(rp3, 1, function(x) StdDev(R=R, weights=x))2223# plot feasible portfolios24plot(x=tmp1.StdDev, y=tmp1.mean, col="gray", main="Random Portfolio Methods")25points(x=tmp2.StdDev, y=tmp2.mean, col="red", pch=2)26points(x=tmp3.StdDev, y=tmp3.mean, col="lightgreen", pch=5)27legend("bottomright", legend=c("sample", "simplex", "grid"), col=c("gray", "red", "lightgreen"),28pch=c(1, 2, 5), bty="n")2930# sample has pretty even coverage of feasible space31# simplex is concentrated around the assets32# grid is 'pushed'/concentrated to the interior due to normalization3334# demonstrate how different values of fev influence the random portfolios of35# the simplex method36# This could be a really good example with Shiny for an interactive example37fev <- 0:538par(mfrow=c(2, 3))39for(i in 1:length(fev)){40rp <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='simplex', fev=fev[i])41tmp.mean <- apply(rp, 1, function(x) mean(R %*% x))42tmp.StdDev <- apply(rp, 1, function(x) StdDev(R=R, weights=x))43plot(x=tmp.StdDev, y=tmp.mean, main=paste("FEV =", fev[i]),44ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))45}46par(mfrow=c(1,1))4748# charts to compare simplex and sample random portfolio generation49par(mfrow=c(1, 2))50# simplex51rp_simplex <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='simplex', fev=0:5)52tmp.mean <- apply(rp_simplex, 1, function(x) mean(R %*% x))53tmp.StdDev <- apply(rp_simplex, 1, function(x) StdDev(R=R, weights=x))54plot(x=tmp.StdDev, y=tmp.mean, main="rp_method=simplex fev=0:5",55ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))56#sample57rp_sample <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='sample')58tmp.mean <- apply(rp_sample, 1, function(x) mean(R %*% x))59tmp.StdDev <- apply(rp_sample, 1, function(x) StdDev(R=R, weights=x))60plot(x=tmp.StdDev, y=tmp.mean, main="rp_method=sample",61ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))62par(mfrow=c(1,1))636465