[Returnanalytics-commits] r2402 - pkg/PortfolioAnalytics/sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 22 18:55:54 CEST 2013


Author: rossbennett34
Date: 2013-06-22 18:55:53 +0200 (Sat, 22 Jun 2013)
New Revision: 2402

Added:
   pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R
Log:
adding testing script for ROI to match examples from Prof Martin's slides.

Added: pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R	2013-06-22 16:55:53 UTC (rev 2402)
@@ -0,0 +1,252 @@
+# Testing for replicating professor Martin's examples
+# The numbered examples corresopond to 1. theory review weights constrained mvo v5.pdf
+
+# data = crsp.short.Rdata
+# returns = midcap.ts[, 1:10]
+
+rm(list=ls())
+
+# Load packages
+library(PerformanceAnalytics)
+library(PortfolioAnalytics)
+library(ROI)
+library(ROI.plugin.glpk)
+library(ROI.plugin.quadprog)
+
+# Use edhec data set from PerformanceAnalytics for reproducing if user does not
+# have the crsp.short.Rdata data
+# data(edhec)
+# returns <- edhec[, 1:10]
+
+# Use crsp.short.Rdata from Prof Martin
+# data file should be in working directory or specify path
+# Can we include this as a data set in the PortfolioAnalytics package?
+load("/Users/rossbennett/Desktop/Testing/crsp.short.Rdata")
+
+returns <- midcap.ts[, 1:10]
+funds <- colnames(returns)
+
+# Set up initial constraint object
+# Here we specify the minimum weight of any asset is -Inf and the maximum
+# weight of any asset is Inf. This is essentially an unconstrained GMV portfolio
+# We specify the full investment constraint (w' 1 = 1) by setting min_sum=1 
+# and max_sum=1.
+gen.constr <- constraint(assets=funds, min=-Inf, max=Inf, min_sum=1, max_sum=1)
+
+# Add objective to minimize variance
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=TRUE)
+
+##### Example 1.1: Global Minimum Variance (GMV) Portfolio #####
+# Global Minimum variance portfolio
+gmv.constr <- gen.constr
+
+# Call the optimizer to minimize portfolio variance
+gmv.opt <- optimize.portfolio(R=returns, constraints=gmv.constr, optimize_method="ROI")
+
+# Optimal weights
+round(gmv.opt$weights, 3)
+
+# Portfolio standard deviation
+sqrt(gmv.opt$out)
+
+##### Example 1.2: Long Only GMV Portfolio #####
+gmv.longonly.constr <- gen.constr
+
+# Set the min and max vectors for long only constraints
+min <- rep(0, length(funds))
+max <- rep(1, length(funds))
+
+# Modify the min and max vectors in gmv.longonly.constr
+gmv.longonly.constr$min <- min
+gmv.longonly.constr$max <- max
+
+# Call the optimizer
+gmv.longonly.opt <- optimize.portfolio(R=returns, constraints=gmv.longonly.constr, optimize_method="ROI")
+
+# Optimal weights
+round(gmv.longonly.opt$weights, 3)
+
+# Portfolio standard deviation
+sqrt(gmv.longonly.opt$out)
+
+##### Example 1.3: GMV Box Constraints #####
+gmv.box.constr <- gen.constr
+
+# Set the min and max vectors for box constraints
+# The box constraints are such that the minimum weight of any asset is 0.03
+# and the maximum weight of any asset is 0.25
+min <- rep(0.03, length(funds))
+max <- rep(0.25, length(funds))
+
+# Modify the min and max vectors in gmv.longonly.constr
+gmv.box.constr$min <- min
+gmv.box.constr$max <- max
+
+# Call the optimizer
+gmv.box.opt <- optimize.portfolio(R=returns, constraints=gmv.box.constr, optimize_method="ROI")
+
+# Optimal weights
+round(gmv.box.opt$weights, 3)
+
+# Portfolio standard deviation
+sqrt(gmv.box.opt$out)
+
+##### Example 1.3a: GMV Box Constraints #####
+gmv.box.constr <- gen.constr
+
+# As an alternative to box constriants, we can also linear inequality
+# constraints for the minimum and maximum asset weights
+
+# Set the min and max vectors for box constraints
+# The box constraints are such that the minimum weight of any asset is 0.03
+# and the maximum weight of any asset is 0.25
+min <- c(0.02, 0.02, 0.02, 0.04, 0.05, 0.05, 0.02, 0, 0, 0.1)
+max <- c(0.2, 0.4, 0.4, 0.45, 0.3, 0.5, 0.4, 0.4, 0.4, 0.4)
+
+# Modify the min and max vectors in gmv.longonly.constr
+gmv.box.constr$min <- min
+gmv.box.constr$max <- max
+
+# Mean variance optimization (MVO) seeks to minimize portfolio variance
+gmv.box.opt <- optimize.portfolio(R=returns, constraints=gmv.box.constr, optimize_method="ROI")
+
+# Optimal weights
+round(gmv.box.opt$weights, 3)
+
+# Portfolio standard deviation
+sqrt(gmv.box.opt$out)
+
+##### Example 1.4: GMV long only with Group Constraints #####
+# Combine returns from different market cap groups
+returns.cap <- cbind(microcap.ts[, 1:2],
+                     smallcap.ts[, 1:2],
+                     midcap.ts[, 1:2],
+                     largecap.ts[, 1:2])
+
+funds.cap <- colnames(returns.cap)
+
+# Set up constraints object for the market caps
+lo.group.constr <- constraint(assets=funds.cap, min=0, max=1, min_sum=1, max_sum=1)
+
+# Add group constraints to gmv.box.group.constr
+# Market cap constraints
+# At least 10% and no more than 25% in micro-caps
+# At least 15% and no more than 35% in small-caps
+# At least 0% and no more than 35% in mid-caps
+# At least 0% and no more than 45% in large-caps
+lo.group.constr$groups <- c(2, 2, 2, 2)
+lo.group.constr$cLO <- c(0.1, 0.15, 0, 0)
+lo.group.constr$cUP <- c(0.25, .35, 0.35, 0.45)
+
+# Add objective to minimize variance
+gmv.lo.group.constr <- add.objective(constraints=lo.group.constr, type="risk", name="var", enabled=TRUE)
+
+# Call optimizer
+gmv.lo.group.opt <- optimize.portfolio(R=returns.cap, constraints=gmv.lo.group.constr, optimize_method="ROI")
+
+# Optimal weights
+round(gmv.lo.group.opt$weights, 3)
+
+# Group weights
+gmv.lo.group.opt$weights[c(1, 3, 5, 7)] + gmv.lo.group.opt$weights[c(2, 4, 6, 8)]
+
+# In the previous examples, we were solving global minimum variance with optmize_method="ROI". 
+# The solve.QP plugin is selected automatically by optimize.portfolio when "var" is the objective
+
+##### Example 1.6: Maximize mean-return with box constraints #####
+# Set up initial constraint object
+# Here we specify the minimum weight of any asset is 0.03 and the maximum weight of any asset is 0.25
+# We specify the full investment constraint (w' 1 = 1) by setting min_sum=1 and max_sum=1
+gen.constr <- constraint(assets=funds, min=0.03, max=0.25, min_sum=1, max_sum=1)
+
+# Add objective to maximize return
+gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=TRUE)
+
+maxret.constr <- gen.constr
+
+# Call optimizer to maximize return subject to given constraints
+maxret.opt <- optimize.portfolio(R=returns, constraints=maxret.constr, optimize_method="ROI")
+
+# Optimal weights
+maxret.opt$weights
+
+##### Example 1.7 Maximize mean-return Long Only with Group Constraints #####
+# Re-use lo.group.constr from Example 1.5
+maxret.lo.group.constr <- lo.group.constr
+
+maxret.lo.group.constr <- add.objective(constraints=maxret.lo.group.constr, type="return", name="mean", enabled=TRUE)
+
+maxret.lo.group.opt <- optimize.portfolio(R=returns.cap, constraints=maxret.lo.group.constr, optimize_method="ROI")
+
+# Optimal weights
+maxret.lo.group.opt$weights
+
+# Group weights
+maxret.lo.group.opt$weights[c(1, 3, 5, 7)] + maxret.lo.group.opt$weights[c(2, 4, 6, 8)]
+
+##### Example 1.X: Maximize Quadratic Utility #####
+# Quadratic utility maximize return penalizing variance
+qu.constr <- constraint(assets=funds, min=0, max=1, min_sum=1, max_sum=1)
+
+# Add mean return as an objective
+qu.constr <- add.objective(constraints=qu.constr, type="return", name="mean", enabled=TRUE)
+
+# Add variance as an objective
+qu.constr <- add.objective(constraints=qu.constr, type="risk", name="var", enabled=TRUE, risk_aversion=20)
+
+qu.opt <- optimize.portfolio(R=returns, constraints=qu.constr, optimize_method="ROI")
+
+wts1 <- round(qu.opt$weights, 4)
+wts1
+
+# Check results for quadratic utility with manual code
+p <- ncol(returns)
+V <- var(returns)
+mu <- colMeans(returns)
+lambda <- 20
+min_wt <- 0
+max_wt <- 1
+
+# parameters for solve.QP
+A <- cbind(rep(1, p), diag(p), -diag(p))
+b <- c(1, rep(min_wt, p), rep(-max_wt, p))
+d <- mu
+res <- quadprog:::solve.QP(Dmat=2*lambda*V, dvec=d, Amat=A, bvec=b, meq=1)
+wts2 <- round(res$solution, 4)
+names(wts2) <- colnames(returns)
+wts2
+
+all.equal(wts1, wts2)
+
+# Note that target mean return CANNOT be specified as a constraint currently
+# It is specified as a target in the return objective
+# Can do quadratic utility optimization with target return
+
+##### Example 1.X: Maximize Quadratic Utility #####
+# Quadratic utility maximize return penalizing variance
+qu.constr <- constraint(assets=funds, min=0, max=1, min_sum=1, max_sum=1)
+
+# Add mean return as an objective
+qu.constr <- add.objective(constraints=qu.constr, type="return", name="mean", target=0.025, enabled=TRUE)
+
+# Add variance as an objective
+# Set risk aversion parameter high to approximate mvo
+qu.constr <- add.objective(constraints=qu.constr, type="risk", name="var", enabled=TRUE, risk_aversion=1e6)
+
+qu.opt <- optimize.portfolio(R=returns, constraints=qu.constr, optimize_method="ROI")
+
+round(qu.opt$weights, 4)
+
+##### Example X: Mean Variance Optimization (MVO) with target mean return constraint #####
+# TODO Add type="return" for constraint to solve the mean-return constrained mvo
+# Will also need to modify optimize.portfolio for optimize_method="ROI"
+
+##### Example X: Mean Variance Optimization (MVO) with target mean return and long only constraints #####
+# TODO Add type="return" for constraint to solve the mean-return constrained mvo
+# Will also need to modify optimize.portfolio for optimize_method="ROI"
+
+##### Example X: Mean Variance Optimization (MVO) with target mean return and box constraints #####
+# TODO Add type="return" for constraint to solve the mean-return constrained mvo
+# Will also need to modify optimize.portfolio for optimize_method="ROI"
+
+# ROI only solves mean, var, or sample CVaR type business objectives
\ No newline at end of file



More information about the Returnanalytics-commits mailing list