This function aims to provide a wrapper for constrained optimization of portfolios that specify constraints and objectives.
optimize.portfolio_v1(
R,
constraints,
optimize_method = c("DEoptim", "random", "ROI", "ROI_old", "pso", "GenSA"),
search_size = 20000,
trace = FALSE,
...,
rp = NULL,
momentFUN = "set.portfolio.moments_v1"
)optimize.portfolio(
R,
portfolio = NULL,
constraints = NULL,
objectives = NULL,
optimize_method = c("DEoptim", "random", "ROI", "pso", "GenSA", "Rglpk", "osqp", "mco",
"CVXR", ...),
search_size = 20000,
trace = FALSE,
...,
rp = NULL,
momentFUN = "set.portfolio.moments",
message = FALSE
)
a list containing the following elements
weights
:The optimal set weights.
objective_measures
:A list containing the value of each objective corresponding to the optimal weights.
opt_values
:A list containing the value of each objective corresponding to the optimal weights.
out
:The output of the solver.
call
:The function call.
portfolio
:The portfolio object.
R
:The asset returns.
data summary:
The first row and last row of R
.
elapsed_time:
The amount of time that elapses while the optimization is run.
end_t:
The date and time the optimization completed.
When Trace=TRUE is specified, the following elements will be returned in addition to the elements above. The output depends on the optimization method and is specific to each solver. Refer to the documentation of the desired solver for more information.
optimize_method="random"
random_portfolios
:A matrix of the random portfolios.
random_portfolio_objective_results
:A list of the following elements for each random portfolio.
optimize_method="DEoptim"
DEoutput:
A list (of length 2) containing the following elements:
optim
member
DEoptim_objective_results
:A list containing the following elements for each intermediate population.
out
: The output of the solver.
weights
: Population weights.
init_weights
: Initial population weights.
objective_measures
: A list of each objective measure corresponding to the weights
optimize_method="pso"
PSOoutput
: A list containing the following elements:
par
value
counts
convergence
message
stats
optimize_method="GenSA"
GenSAoutput
: A list containing the following elements:
value
par
trace.mat
counts
an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
default=NULL, a list of constraint objects. An object of class 'v1_constraint' can be passed in here.
one of "DEoptim", "random", "ROI", "pso", "GenSA", "osqp", "Rglpk", "mco", "CVXR", or a vector to specify CVXR solver. A solver of ROI or CVXR can also be specified and will be solved via ROI or CVXR. See details.
integer, how many portfolios to test, default 20,000
TRUE/FALSE if TRUE will attempt to return additional information on the path or portfolios searched
any other passthru parameters
matrix of random portfolio weights, default NULL, mostly for automated use by rebalancing optimization or repeated tests on same portfolios
the name of a function to call to set portfolio moments, default set.portfolio.moments_v2
an object of type "portfolio" specifying the constraints and objectives for the optimization
default=NULL, a list of objective objects.
TRUE/FALSE. The default is message=FALSE. Display messages if TRUE.
Kris Boudt, Peter Carl, Brian G. Peterson, Ross Bennett, Xiaokang Feng, Xinran Zhao
This function currently supports DEoptim, random portfolios, pso, GenSA, ROI, osqp, Rglpk, mco, and CVXR solvers as back ends. Additional back end contributions for Rmetrics, ghyp, etc. would be welcome.
When using random portfolios, search_size is precisely that, how many portfolios to test. You need to make sure to set your feasible weights in generatesequence to make sure you have search_size unique portfolios to test, typically by manipulating the 'by' parameter to select something smaller than .01 (I often use .002, as .001 seems like overkill)
When using DE, search_size is decomposed into two other parameters which it interacts with, NP and itermax.
NP, the number of members in each population, is set to cap at 2000 in DEoptim, and by default is the number of parameters (assets/weights) * 10.
itermax, if not passed in dots, defaults to the number of parameters (assets/weights) * 50.
When using GenSA and want to set verbose=TRUE
, instead use trace
.
If optimize_method="ROI"
is specified, a default solver will be
selected based on the optimization problem. The glpk
solver is the
default solver for LP and MILP optimization problems. The quadprog
solver is the default solver for QP optimization problems. For example,
optimize_method = "quadprog"
can be specified and the optimization
problem will be solved via ROI using the quadprog solver.
The extension to ROI solves a limited type of convex optimization problems:
Maxmimize portfolio return subject leverage, box, group, position limit, target mean return, and/or factor exposure constraints on weights.
Minimize portfolio variance subject to leverage, box, group, turnover, and/or factor exposure constraints (otherwise known as global minimum variance portfolio).
Minimize portfolio variance subject to leverage, box, group, and/or factor exposure constraints and a desired portfolio return.
Maximize quadratic utility subject to leverage, box, group, target mean return, turnover, and/or factor exposure constraints and risk aversion parameter.
(The risk aversion parameter is passed into optimize.portfolio
as an added argument to the portfolio
object).
Maximize portfolio mean return per unit standard deviation (i.e. the Sharpe Ratio) can be done by specifying maxSR=TRUE
in optimize.portfolio
.
If both mean and StdDev are specified as objective names, the default action is to maximize quadratic utility, therefore maxSR=TRUE
must be specified to maximize Sharpe Ratio.
Minimize portfolio ES/ETL/CVaR optimization subject to leverage, box, group, position limit, target mean return, and/or factor exposure constraints and target portfolio return.
Maximize portfolio mean return per unit ES/ETL/CVaR (i.e. the STARR Ratio) can be done by specifying maxSTARR=TRUE
in optimize.portfolio
.
If both mean and ES/ETL/CVaR are specified as objective names, the default action is to maximize mean return per unit ES/ETL/CVaR.
These problems also support a weight_concentration objective where concentration of weights as measured by HHI is added as a penalty term to the quadratic objective.
Because these convex optimization problem are standardized, there is no need for a penalty term.
The multiplier
argument in add.objective
passed into the complete constraint object are ignored by the ROI solver.
If optimize_method="CVXR"
is specified, a default solver will be selected based on the optimization problem.
The default solver for Quadratic Programming will be OSQP
,
and the default solver for Linear Problem and Second-Order Cone Programming will be SCS
.
Specified CVXR solver can be given by using optimize_method=c("CVXR", "CVXRsolver")
.
CVXR supports some commercial solvers, including CBC, CPLEX, GUROBI and MOSEK, and some open source solvers, including GLPK, GLPK_MI, OSQP, SCS and ECOS.
For example, optimize_method = c("CVXR", "ECOS")
can be specified and the optimization problem will be solved via CVXR using the ECOS solver.
The extension to CVXR solves a limited type of convex optimization problems:
Maxmimize portfolio mean return subject leverage, box, group, and/or target mean return constraints
Minimize portfolio variance subject to leverage, box, group, and/or target mean return constraints (otherwise known as global minimum variance portfolio).
Maximize quadratic utility subject to leverage, box, group, and/or target mean return constraints and risk aversion parameter.
(The default risk aversion is 1, and specified risk aversion could be given by risk_aversion = 1
.
The risk aversion parameter is passed into optimize.portfolio
as an added argument to the portfolio
object.)
Minimize portfolio ES/ETL/CVaR optimization subject to leverage, box, group, and/or target mean return constraints and tail probability parameter.
(The default tail probability is 0.05, and specified tail probability could be given by arguments = list(p=0.95)
.
The tail probability parameter is passed into optimize.portfolio
as an added argument to the portfolio
object.)
Minimize portfolio CSM optimization subject to leverage, box, group, and/or target mean return constraints and tail probability parameter.
(The default tail probability is 0.05, and specified tail probability could be given by arguments = list(p=0.95)
.
The tail probability parameter is passed into optimize.portfolio
as an added argument to the portfolio
object.)
Maximize portfolio mean return per unit standard deviation (i.e. the Sharpe Ratio) subject to leverage, box, group, and/or target mean return constraints.
It should be specified by maxSR=TRUE
in optimize.portfolio
with both mean and var/StdDev objectives.
Otherwise, the default action is to maximize quadratic utility.
Maximize portfolio mean return per unit ES (i.e. the ES ratio/STARR) subject to leverage, box, group, and/or target mean return constraints.
It could be specified by maxSTARR=TRUE
or ESratio=TRUE
in optimize.portfolio
with both mean and ES objectives.
The default action is to maximize ES ratio. If maxSTARR=FALSE
or ESratio=FALSE
is given, the action will be minimizing ES.
Maximize portfolio mean return per unit CSM (i.e. the CSM ratio) subject to leverage, box, group, and/or target mean return constraints.
It could be specified by CSMratio=TRUE
in optimize.portfolio
with both mean and CSM objectives.
The default action is to maximize CSM ratio. If CSMratio=FALSE
is given, the action will be minimizing CSM.
Because these convex optimization problem are standardized, there is no need for a penalty term.
The multiplier
argument in add.objective
passed into the complete constraint object are ignored by the CVXR solver.
portfolio.spec