Path: blob/master/demo/demo_factor_exposure.R
1433 views
#' ---1#' title: "Factor Exposure Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to solve a portfolio optimization problem with7#' factor exposure constraints.89#' Load the required packages10library(PortfolioAnalytics)11library(ROI)12require(ROI.plugin.quadprog)13require(ROI.plugin.glpk)14library(Rglpk)15library(DEoptim)1617#' Load the data18data(edhec)19ret <- edhec[, 1:4]2021#' Create portfolio object22pspec <- portfolio.spec(assets=colnames(ret))2324#' Here we define individual constraint objects.25#' Leverage constraint.26lev_constr <- weight_sum_constraint(min_sum=1, max_sum=1)2728#' Box constraint29lo_constr <- box_constraint(assets=pspec$assets, min=c(0.01, 0.02, 0.03, 0.04), max=0.65)3031#' Group constraint'32grp_constr <- group_constraint(assets=pspec$assets, groups=list(1:2, 3, 4), group_min=0.1, group_max=0.4)3334#' Position limit constraint35pl_constr <- position_limit_constraint(assets=pspec$assets, max_pos=4)3637#' Make up a B matrix for an industry factor model.38#' dummyA, dummyB, and dummyC could be industries, sectors, etc.39B <- cbind(c(1, 1, 0, 0),40c(0, 0, 1, 0),41c(0, 0, 0, 1))42rownames(B) <- colnames(ret)43colnames(B) <- c("dummyA", "dummyB", "dummyC")44lower <- c(0.1, 0.1, 0.1)45upper <- c(0.4, 0.4, 0.4)4647#' Industry exposure constraint.48#' The exposure constraint and group constraint are equivalent to test that49#' they result in the same solution.50exp_constr <- factor_exposure_constraint(assets=pspec$assets, B=B, lower=lower, upper=upper)5152#' Here we define objectives.53#'54#' Objective to minimize variance.55var_obj <- portfolio_risk_objective(name="var")5657#' Objective to maximize return.58ret_obj <- return_objective(name="mean")5960#' Objective to minimize ETL.61etl_obj <- portfolio_risk_objective(name="ETL")6263#' Run optimization on minimum variance portfolio with leverage, long only,64#' and group constraints.65opta <- optimize.portfolio(R=ret, portfolio=pspec,66constraints=list(lev_constr, lo_constr, grp_constr),67objectives=list(var_obj),68optimize_method="ROI")69opta7071#' Run optimization on minimum variance portfolio with leverage, long only,72#' and factor exposure constraints.73optb <- optimize.portfolio(R=ret, portfolio=pspec,74constraints=list(lev_constr, lo_constr, exp_constr),75objectives=list(var_obj),76optimize_method="ROI")77optb7879#' Note that the portfolio with the group constraint and exposure constraint80#' should result in same solution.81all.equal(opta$weights, optb$weights)8283#' Run optimization on maximum return portfolio with leverage, long only,84#' and group constraints.85optc <- optimize.portfolio(R=ret, portfolio=pspec,86constraints=list(lev_constr, lo_constr, grp_constr),87objectives=list(ret_obj),88optimize_method="ROI")89optc9091#' Run optimization on maximum return portfolio with leverage, long only,92#' and factor exposure constraints.93optd <- optimize.portfolio(R=ret, portfolio=pspec,94constraints=list(lev_constr, lo_constr, exp_constr),95objectives=list(ret_obj),96optimize_method="ROI")97optd9899#' Note that the portfolio with the group constraint and exposure constraint100#' should result in same solution.101all.equal(optc$weights, optd$weights)102103#' Run optimization on minimum expected tail loss portfolio with leverage,104#' long only, and group constraints.105opte <- optimize.portfolio(R=ret, portfolio=pspec,106constraints=list(lev_constr, lo_constr, grp_constr),107objectives=list(etl_obj),108optimize_method="ROI")109opte110111#' Run optimization on minimum expected tail loss portfolio with leverage,112#' long only, and factor exposure constraints.113optf <- optimize.portfolio(R=ret, portfolio=pspec,114constraints=list(lev_constr, lo_constr, exp_constr),115objectives=list(etl_obj),116optimize_method="ROI")117optf118119#' Note that the portfolio with the group constraint and exposure constraint120#' should result in same solution.121all.equal(opte$weights, optf$weights)122123#' Run optimization on maximum return portfolio with leverage, long only,124#' and group constraints using DEoptim as the optimization engine.125set.seed(123)126optde1 <- optimize.portfolio(R=ret, portfolio=pspec,127constraints=list(lev_constr, lo_constr, grp_constr),128objectives=list(ret_obj),129optimize_method="DEoptim",130search_size=2000,131trace=TRUE)132optde1133134#' Run optimization on maximum return portfolio with leverage, long only,135#' and factor exposure constraints using DEoptim as the optimization engine.136set.seed(123)137optde2 <- optimize.portfolio(R=ret, portfolio=pspec,138constraints=list(lev_constr, lo_constr, exp_constr),139objectives=list(ret_obj),140optimize_method="DEoptim",141search_size=2000,142trace=TRUE)143optde2144145#' Note that the portfolio with the group constraint and exposure constraint146#' should result in same solution.147all.equal(optde1$weights, optde2$weights)148149#' Run optimization on maximum return portfolio with leverage, long only,150#' and group constraints using random portfolios as the optimization151#' engine.152optrp1 <- optimize.portfolio(R=ret, portfolio=pspec,153constraints=list(lev_constr, lo_constr, grp_constr),154objectives=list(ret_obj),155optimize_method="random",156search_size=2000,157trace=TRUE)158optrp1159160#' Run optimization on maximum return portfolio with leverage, long only,161#' and factor exposure constraints using random portfolios as the optimization162#' engine.163optrp2 <- optimize.portfolio(R=ret, portfolio=pspec,164constraints=list(lev_constr, lo_constr, exp_constr),165objectives=list(ret_obj),166optimize_method="random",167search_size=2000,168trace=TRUE)169optrp2170171#' Note that the portfolio with the group constraint and exposure constraint172#' should result in same solution.173all.equal(optrp1$weights, optrp2$weights)174175176