Path: blob/master/sandbox/testing_ROI_Martin.R
1433 views
# Testing for replicating professor Martin's examples1# The numbered examples corresopond to 1. theory review weights constrained mvo v5.pdf23# data = crsp.short.Rdata4# returns = midcap.ts[, 1:10]56rm(list=ls())78# Load packages9library(PortfolioAnalytics)10library(ROI)11library(ROI.plugin.glpk)12library(ROI.plugin.quadprog)1314# Use edhec data set from PerformanceAnalytics for reproducing if user does not15# have the crsp.short.Rdata data16# data(edhec)17# returns <- edhec[, 1:10]1819# Use crsp.short.Rdata from Prof Martin20# data file should be in working directory or specify path21# Can we include this as a data set in the PortfolioAnalytics package?22load("/Users/rossbennett/Desktop/Testing/crsp.short.Rdata")2324returns <- midcap.ts[, 1:10]25funds <- colnames(returns)262728##### Example 1.1: Global Minimum Variance (GMV) Portfolio #####2930# GMV portfolio using new interface31pspec <- portfolio.spec(assets=funds)32pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)33pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)34pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)3536opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")3738# Optimal weights39round(opt$weights, 3)4041# Portfolio standard deviation42sqrt(opt$out)4344##### Example 1.2: Long Only GMV Portfolio #####4546# GMV long only portfolio using new interface47pspec <- portfolio.spec(assets=funds)48pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)49pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)50pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)5152opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")5354# Optimal weights55round(opt$weights, 3)5657# Portfolio standard deviation58sqrt(opt$out)5960##### Example 1.3: GMV Box Constraints #####6162# GMV box constraints portfolio using new interface63pspec <- portfolio.spec(assets=funds)64pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)65pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)66pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)6768opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")6970# Optimal weights71round(opt$weights, 3)7273# Portfolio standard deviation74sqrt(opt$out)7576##### Example 1.4: GMV long only with Group Constraints #####77# Combine returns from different market cap groups78returns.cap <- cbind(microcap.ts[, 1:2],79smallcap.ts[, 1:2],80midcap.ts[, 1:2],81largecap.ts[, 1:2])8283funds.cap <- colnames(returns.cap)8485# GMV group constraints portfolio using new interface86pspec <- portfolio.spec(assets=funds.cap)87pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)88pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)89pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,90groups=c(2, 2, 2, 2),91group_min=c(0.1, 0.15, 0, 0),92group_max=c(0.25, .35, 0.35, 0.45),93group_labels=c("MICRO", "SMALL", "MID", "LARGE"))94pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)9596opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")9798# Optimal weights99round(opt$weights, 3)100101# Get the group weights102# This is something I will work to include in the summary.optimize.portfolio.ROI103groups <- pspec$constraints[[3]]$groups104group_labels <- pspec$constraints[[3]]$group_labels105n.groups <- length(groups)106group_weights <- rep(0, n.groups)107k <- 1108l <- 0109for(i in 1:n.groups){110j <- groups[i]111group_weights[i] <- sum(opt$weights[k:(l+j)])112k <- k + j113l <- k - 1114}115names(group_weights) <- group_labels116group_weights117118# Portfolio standard deviation119sqrt(opt$out)120121# In the previous examples, we were solving global minimum variance with optmize_method="ROI".122# The solve.QP plugin is selected automatically by optimize.portfolio when "var" is the objective123124##### Example 1.6: Maximize mean-return with box constraints #####125126# Maximize mean return with box constraints portfolio using new interface127pspec <- portfolio.spec(assets=funds)128pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)129pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)130pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)131132opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")133134# Optimal weights135round(opt$weights, 3)136137# Portfolio standard deviation138sqrt(opt$out)139140##### Example 1.7 Maximize mean-return Long Only with Group Constraints #####141142# GMV group constraints portfolio using new interface143pspec <- portfolio.spec(assets=funds.cap)144pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)145pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)146pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,147groups=c(2, 2, 2, 2),148group_min=c(0.1, 0.15, 0, 0),149group_max=c(0.25, .35, 0.35, 0.45),150group_labels=c("MICRO", "SMALL", "MID", "LARGE"))151pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)152153opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")154155# Optimal weights156round(opt$weights, 3)157158# Get the group weights159# This is something I will work to include in the summary.optimize.portfolio.ROI160groups <- pspec$constraints[[3]]$groups161group_labels <- pspec$constraints[[3]]$group_labels162group_weights <- rep(0, n.groups)163n.groups <- length(groups)164k <- 1165l <- 0166for(i in 1:n.groups){167j <- groups[i]168group_weights[i] <- sum(opt$weights[k:(l+j)])169k <- k + j170l <- k - 1171}172names(group_weights) <- group_labels173group_weights174175# Portfolio standard deviation176sqrt(opt$out)177178# Check results for quadratic utility with manual code179p <- ncol(returns)180V <- var(returns)181mu <- colMeans(returns)182lambda <- 20183min_wt <- 0184max_wt <- 1185186# parameters for solve.QP187A <- cbind(rep(1, p), diag(p), -diag(p))188b <- c(1, rep(min_wt, p), rep(-max_wt, p))189d <- mu190res <- quadprog:::solve.QP(Dmat=2*lambda*V, dvec=d, Amat=A, bvec=b, meq=1)191wts2 <- round(res$solution, 4)192names(wts2) <- colnames(returns)193wts2194195# Note that target mean return CANNOT be specified as a constraint currently196# It is specified as a target in the return objective197# Can do quadratic utility optimization with target return198199##### Example X: Mean Variance Optimization (MVO) with target mean return constraint #####200201# MVO with target mean return202pspec <- portfolio.spec(assets=funds)203pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)204pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)205pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)206pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)207208opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")209210# Optimal weights211round(opt$weights, 3)212213# Portfolio return214t(opt$weights) %*% colMeans(returns)215216217##### Example X: Mean Variance Optimization (MVO) with target mean return and long only constraints #####218219# MVO with long only and target mean return220pspec <- portfolio.spec(assets=funds)221pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)222pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)223pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)224pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)225226opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")227228# Optimal weights229round(opt$weights, 3)230231# Portfolio return232t(opt$weights) %*% colMeans(returns)233234##### Example X: Mean Variance Optimization (MVO) with target mean return and box constraints #####235236# MVO with box constraints and target mean return237pspec <- portfolio.spec(assets=funds)238pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)239pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)240pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)241pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)242243opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")244245# Optimal weights246round(opt$weights, 3)247248# Portfolio return249t(opt$weights) %*% colMeans(returns)250251##### Example X: ETL Long Only #####252253pspec <- portfolio.spec(assets=funds)254pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)255pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)256# This can be specified with ETL, ES, or CVaR for name257pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)258259opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")260261# Optimal weights262round(opt$weights, 3)263264##### Example X: ETL with box constraints #####265266pspec <- portfolio.spec(assets=funds)267pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)268pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)269# This can be specified with ETL, ES, or CVaR for name270pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)271272opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")273274# Optimal weights275round(opt$weights, 3)276277##### Example X: ETL long only with group constraints #####278279# GMV group constraints portfolio using new interface280pspec <- portfolio.spec(assets=funds.cap)281pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)282pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)283pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,284groups=c(2, 2, 2, 2),285group_min=c(0.1, 0.15, 0, 0),286group_max=c(0.25, .35, 0.35, 0.45),287group_labels=c("MICRO", "SMALL", "MID", "LARGE"))288pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)289290opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")291292# Optimal weights293round(opt$weights, 3)294295# Get the group weights296# This is something I will work to include in the summary.optimize.portfolio.ROI297groups <- pspec$constraints[[3]]$groups298group_labels <- pspec$constraints[[3]]$group_labels299group_weights <- rep(0, n.groups)300n.groups <- length(groups)301k <- 1302l <- 0303for(i in 1:n.groups){304j <- groups[i]305group_weights[i] <- sum(opt$weights[k:(l+j)])306k <- k + j307l <- k - 1308}309names(group_weights) <- group_labels310group_weights311312313314