fPortfolio-package {fPortfolio} | R Documentation |
The Rmetrics "fPortfolio" package is a very powerful collection of functions to optimize portfolios and to analyze them from different points ov view.
The implemented portfolio models include the traditional mean–variance Markowitz portfolio, robust variants of the Markowitz portfoio, the mean–LPM lower partial moment portfolio, and the mean-CVaR conditional value-at-Risk portfolio.
Optimization is possible either by minimizing the risk if the return is specified, or by maximizing the return if the risk is specified.
Linear box/group constraints and quadratic covariance risk budgets and/or copulae tail risk budgets can be specified.
Depending on the model of the portfolio and the constraints powerful solvers are used for optimization including a linear programming solver, a quadratic programming solver, a sequential quadratic programming solver, and a second order programming solver.
Several kinds of charts can be produced using powerful graphics tools to visualize the results.
Package: | fPortfolio |
Type: | Package |
Version: | 261.73.1 |
Date: | 2008 |
License: | GPL Version 2 or later |
Copyright: | (c) 1999-2008 Diethelm Wuertz and Rmetrics Foundation |
URL: | http://www.rmetrics.org |
The following chapters give a brief introduction how to optimize and analyze portfolios.
Table of Content:
1. | How to Specify a Portfolio ? |
2. | How to Prepare the Data Input ? |
3. | How to Define Portfolio Constraints ? |
4. | How to Optimize a Portfolio ? |
5. | Implemented Portfolios |
6. | How to Analyze an Optimized Portfolio ? |
7. | How to Analyze the Efficient Frontier ? |
8. | How to Backtest a Rolling Portfolio ? |
To optimize a portfolio of assets we first have to specify it. All
settings which specify a portfolio of assets are respresented by a
S4 object of class-fPFOLIOSPEC
:
setClass("fPFOLIOSPEC", representation( model = "list", portfolio = "list", optim = "list") )
An object of class fPFOLIOSPEC
has three slots, named
@model
, @code{portfolio}, and @optim
The first slot @model
holds the model information, the second
slot @portfolio
the portfolio information, and the last slot
@optim
the information about the solver used for optimization.
The default settings are as follows:
model = list( type = "MV", optimize = "minRisk", estimator = "covEstimator", tailRisk = list(), params = list(alpha=0.05, a=2)), portfolio = list( weights = NULL, targetReturn = NULL, targetRisk = NULL, riskFreeRate = 0, nFrontierPoints = 50, status = NA), optim = list( solver = NULL, trace = FALSE)
To retrieve or modify portfolio specifications several
getSpec
and setSpec
function are
available. To set a portfolio specification from scratch
one can also use the function portfolioSpec
.
Note, there is a generic print function to print information from
portfolio specifications. For example print(portfolioSpec()
prints the default portfolio specification.
To optimize a portfolio or to compute the efficient frontier of
a portfolio the assets which compose the portfolio must be available
as a set of multivariate time series. These must be available in
form of an S4 object of class "timeSeries"
, the preferred
time series representation in Rmetrics.
Internally, the portfolio functions use the portfolioData
function to generate an S4 object of class-fPFOLIODATA
:
setClass("fPFOLIODATA", representation( data = "list", statistics = "list", tailRisk = "list") )
An object of class fPFOLIODATA
has three slots repfresented
by lists, named @data
, @code{@statistics}, and @tailrisk
.
The first slot @data
holds the data information, the second
slot @statistics
the estimated mean and covariance together
with optional robust estimates, and the last slot @tailRisk
optional information if we include copulae tail risk baskets into
portfolio optimization.
The function
portfolioData(data, spec = portfolioSpec())
creates an S4 object of class fPFOLIODATA
using the
portfolio specification to get the required information how to
estimate mean and covariance matrix of the assets.
To retrieve information about the portfolios data set of assets
several getData
functions are available.
Note, there is a generic print function to print information from a portfolio data object.
Portfolio optimization functions expect information about the
constraints. These may be either box constraints, group constraints,
covariance risk budget constraints or copulae tail risk budgets.
In addition "LongOnly"
constraints generate box constraints
for all weights ranging between zero and one, "Short"
constraints allow for unlimited short selling.
Weight constrained portfolios may be specified by a vector of
character strings which describe executable code, setting values to
vectors minW
, maxW
, minsumW
, and maxsumW
.
The individual string elements of such vectors have the following
form for box and group constraints
description{
"minW[Asset(s)]=Value(s)"
, and/or "maxW[Asset(s)]=Value(s)"
.
"minsumW[Asset(s)]=Value(s)"
, and/or "maxsumW[Asset(s)]=Value(s)"
.
Asset(s)
is an index of one or more assets, and Value(s)
a numeric value or vector assigning the desired value.
By default, risk budgets are not included in the portfolio optimization.
Covariance risk budget constraints have to be added explicitely, and
have the following form:
"minB[Asset(s)]=Value(s)"
, and/or "minB[Asset(s)]=Value(s)"
.
Again, Asset(s)
is an index of one or more assets, and
Value(s)
a numeric value or vector with numbers ranging between
zero and one, assigning the desired risk budgets.
Internally, the portfolio functions use the
portfolioConstraints
function to generate an S4 object
of class-fPFOLIOCON
:
setClass("fPFOLIOCON", representation( stringConstraints = "character", boxgroupConstraints = "matrix", riskbudgetConstraints = "matrix") )
An object of class fPFOLIOCON
has three slots represented
by lists, named @stringConstraints
, @code{@boxgroupConstraints},
and @riskbudgetConstraints
.
The first slot @stringConstraints
holds the constraints
information given by the string specification, the second
slot @boxgroupConstraints
holds the box and group constraints
in form of a matrix, and the last slot @riskbudgetConstraints
holds the covariance risk budget constraint also in form of a numerical
matrix.
The function which returns the constraints as an S4 object
portfolioConstraints(data, spec = portfolioSpec(), constraints = "LongOnly")
requires as input the portfolio data set, the portfolio specification, and the string of constraints.
To retrieve information about the portfolios constraints use the
function getConstraints
.
Note, there is a generic print function to print information from a portfolio data object.
Rmetrics offers several functions to compute and optimize portfolios.
These include the functions
feasiblePortfolio(data, spec, constraints) efficientPortfolio(data, spec, constraints) tangencyPortfolio(data, spec, constraints) minvariancePortfolio(data, spec, constraints) portfolioFrontier(data, spec, constraints)
For all functions the user has to specifiy the data
as an
S4 object of class timeSeries
. The remaining two arguments
spec=portfolioSpec()
, and constraints="LongOnly"
have
default values.
The portfolio functions return an S4 object of class-fPORTFOLIO
which is represented by the following slots:
setClass("fPFOLIOCON", representation( call = "call", data = "list", spec = "list", constraints = "character", portfolio = "list", title = "character", description = "character") )
To retrieve information from a portfolio object use one of the
extractor function, see getPortfolio
.
Note, there is a generic print function to print information from a portfolio data object and a plot function to display charts.
The computation of a feasiblePortfolio
doesn't require
portfolio optimization at all. Given the weights through portfolio
specifiaction, use setWeights
, the function
feasiblePortfolio
directly computes the target return
and the target risk. The results are returned as an S4 object of
class fPortfolio
.
The optimization of an efficient portfolio is done by the function
efficientPortfolio
. By default a mean–variance Markowitz
optimization is performed with "LongOnly". If neither the target
return is specified by the function setTargetReturn
nor
the risk is pecified by the function setTargetRisk
, then the
results for the tangency portfolio with given risk free rate, will
be returned.
The tangencyPortfolio
and minvariancePortfolio
are two special efficient portfolios, the first yields the tangency
point on the efficient frontier with respect to the rosk free rate,
and the second the point on the efficient frontier with the lowest
risk.
The function portfolioFrontier
allows to compute points
along the efficient frontier, use setNFrontierPoints
to modify the default value of 50. The points cover the whole range
of feasible points on the efficient frontier with returns in equidistant
steps.
The solver used for portfolio optimization is selected automatically,
but can also chosen by the experienced user. It is in the responsibility
of the user that he select a solver which is not in conflict with
the specified portfolio model. Provided interfaces and solvers are:
solveRshortExact
for unlimited short selling,
solveRquadprog
for quadratic objective with linear constraints,
solveRglpk
for linear objective with linear constraints,
solveRsocp
for linear objective with linear/quadratic constraints,
solveRdonlp2
for non-linear objective with non-linear constraints.
Optimize a Mean-Variance Portfolio:
By default the portfolio functions minimize the risk given the return of a mean-variance portfolio with long-only constraints. Optinonally we can maximize the return given the risk, constraints are allowed for short selling if specified. Furthermore box, group, and covariance risk budget constraints can be added.
Optimize a Mean-Variance Long-Only Portfolio:
This is the default case:
spec = portfolioSpec()
constraints = "Short"
Optimize a Mean-Variance Short Selling Portfolio:
spec = portfolioSpec()
constraints = "LongOnly"
Optimize a Mean-Variance Portfolio with Box Constraints:
spec = portfolioSpec()
constraints = c(minW, maxW)
Optimize a Mean-Variance Portfolio with Group Constraints:
spec = portfolioSpec()
constraints =
Optimize a Mean-Variance Portfolio with Risk Budget Constraints:
spec = portfolioSpec()
constraints =
Optimize a Robust Mean-Variance Portfolio:
Mean-Variance portfolios can be robustified by replace the man and/or covariance matrix by robust estimators. Not only everal choices are implemented, the user can also add his own estimator for the mean and covariance. In any case the use has tpo specify the name of the function which estimates mean /and or covaraince.
spec = portfolioSpec() setEstimator <- "robEstimator"
Optimize a Mean-LPM Portfolio:
The optimization of a "Mean - Lower Partial Moment" portfolio is done mapping the Mean-LPM portfolio on a quadratic form which looks formally identical to a Mean-Variance portfolio.
Type: We set the portfolio type to "LPM", and automatically the default LPM estimator "lpmEstimator" will be chosen. Note you can overwrite this choice using a robust LPM estimator for the portfolios "mu" and Sigma".
setType <- "LPM" setEstimator <- "lpmEstimator"
Constraints: Concerning the constraints, they can be set in the same way as for a Mean-Variance portfolio. This includes box, group and LPM risk budget constraints.
Optimize: By default the "LPM" risk will be minimized for a given "Mean" return. Optionally one can maximize the target return for a predetermined value of the risk. For risk minimization the solver "solveRquadprog" is used, the default, for return maximization the solver "solverRdonlp2" is used.
Optimize a Mean-CVaR Portfolio:
The optimization of a "Mean - CVaR" portfolio is done using the approach of Uryasev and Rockafeller. The portfolio is mapped to a linear programming problem.
Type: We set the portfolio type to "CVaR",
setType <- "CVaR"
Constraints: Concerning the constraints, they can be set in the same way as for a Mean-Variance portfolio. This includes box, group and LPM risk budget constraints.
Optimize: By default the "CVaR" risk will be minimized for a given "Mean" return. Optionally one can maximize the target return for a predetermined value of the risk. For risk minimization the solver "solveRglpk" is used, the default, for return maximization the solver "solverRsocp" is used.
There exist several printouts and plots to display the results of an optimized portfolio. For an individual portfolio ...
Plot
weightsPie
Plot
There exist several plots to display the results of an optimized portfolio or to display the portfolio frontier.
For the efficient frontier ...
plot
frontierPlot
weightsPlot
frontierSlider
weightsSlider
Rmetrics offers functions for a rolling backtesting of
historical portfolios:
rollingPortfolio
portfolioBacktesting
rollingPortfolio
portfolioBacktesting
With Rmetrics version 2.7.0 the function cmlPortfolio
has been deprecated. Setting the risk free rate, sing the
function setRiskFreeRate
as before and calling the
function tangencyPortfolio
will yield the same results.
The following functions and group of functions have been moved
to the fAssets
package, since they rather deal with multivariate
assets data and not with portfolios. covEllipsesPlot
,
VarModelling
.
The function maxxdd
which computes the theoretical density
of drawdowns for random walks has been moved to the fBasics
package which covers the field of distributions.
The packages Rdonlp2
with the non-linear programming solver
and Rsocp
swith the second order cone programming solver
are suggested packages. The first can be downloaded from the website
http://arumat.net/Rdonlp2/ or from the Rmetrics repository,
for the second package please contact wuertz@phys.ethz.ch.