[Returnanalytics-commits] r2890 - in pkg/PortfolioAnalytics: . R man sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Aug 26 06:54:04 CEST 2013
Author: rossbennett34
Date: 2013-08-26 06:54:04 +0200 (Mon, 26 Aug 2013)
New Revision: 2890
Added:
pkg/PortfolioAnalytics/man/chart.EfficientFrontierOverlay.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
pkg/PortfolioAnalytics/man/chart.EfficientFrontier.Rd
pkg/PortfolioAnalytics/man/create.EfficientFrontier.Rd
pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd
pkg/PortfolioAnalytics/man/optimize.portfolio.Rd
pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R
Log:
Modifying efficient frontier code to plot efficient frontier in mean-sd space instead of mean-var space. Adding function to overlay multiple efficient frontiers of portfolios with different constraints.
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-08-26 04:54:04 UTC (rev 2890)
@@ -7,6 +7,7 @@
export(chart.EfficientFrontier.optimize.portfolio.ROI)
export(chart.EfficientFrontier.optimize.portfolio)
export(chart.EfficientFrontier)
+export(chart.EfficientFrontierOverlay)
export(chart.RiskReward.optimize.portfolio.DEoptim)
export(chart.RiskReward.optimize.portfolio.GenSA)
export(chart.RiskReward.optimize.portfolio.pso)
Modified: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-26 04:54:04 UTC (rev 2890)
@@ -15,10 +15,10 @@
#' For objects created by optimize.portfolio with 'ROI' specified as the
#' optimize_method:
#' \itemize{
-#' \item The mean-var or mean-etl efficient frontier can be plotted for optimal
+#' \item The mean-StdDev or mean-etl efficient frontier can be plotted for optimal
#' portfolio objects created by \code{optimize.portfolio}.
#'
-#' \item If \code{match.col="var"}, the mean-variance efficient frontier is plotted.
+#' \item If \code{match.col="StdDev"}, the mean-StdDev efficient frontier is plotted.
#'
#' \item If \code{match.col="ETL"} (also "ES" or "CVaR"), the mean-etl efficient frontier is plotted.
#' }
@@ -29,9 +29,10 @@
#' each iteration, therfore we cannot extract and chart an efficient frontier.
#'
#' @param object optimal portfolio created by \code{\link{optimize.portfolio}}
-#' @param match.col string name of column to use for risk (horizontal axis).
-#' \code{match.col} must match the name of an objective in the \code{portfolio}
-#' object.
+#' @param string name of column to use for risk (horizontal axis).
+#' \code{match.col} must match the name of an objective measure in the
+#' \code{objective_measures} or \code{opt_values} slot in the object created
+#' by \code{\link{optimize.portfolio}}.
#' @param n.portfolios number of portfolios to use to plot the efficient frontier
#' @param xlim set the x-axis limit, same as in \code{\link{plot}}
#' @param ylim set the y-axis limit, same as in \code{\link{plot}}
@@ -56,21 +57,27 @@
wts <- object$weights
objectclass <- class(object)[1]
- objnames <- unlist(lapply(portf$objectives, function(x) x$name))
- if(!(match.col %in% objnames)){
- stop("match.col must match an objective name")
- }
+ # objnames <- unlist(lapply(portf$objectives, function(x) x$name))
+ # if(!(match.col %in% objnames)){
+ # stop("match.col must match an objective name")
+ # }
# get the optimal return and risk metrics
xtract <- extractStats(object=object)
- columnames <- colnames(xtract)
+ columnames <- names(xtract)
if(!(("mean") %in% columnames)){
# we need to calculate the mean given the optimal weights
opt_ret <- applyFUN(R=R, weights=wts, FUN="mean")
} else {
opt_ret <- xtract["mean"]
}
- opt_risk <- xtract[match.col]
+ # get the match.col column
+ mtc <- pmatch(match.col, columnames)
+ if(is.na(mtc)) {
+ mtc <- pmatch(paste(match.col,match.col,sep='.'), columnames)
+ }
+ if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+ opt_risk <- xtract[mtc]
# get the data to plot scatter of asset returns
asset_ret <- scatterFUN(R=R, FUN="mean")
@@ -80,7 +87,7 @@
if(match.col %in% c("ETL", "ES", "CVaR")){
frontier <- meanetl.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
}
- if(match.col %in% objnames){
+ if(match.col == "StdDev"){
frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
}
# data points to plot the frontier
@@ -221,6 +228,22 @@
wts_idx <- grep(pattern="^w\\.", cnames)
wts <- frontier[, wts_idx]
+ # return along the efficient frontier
+ # get the "mean" column
+ mean.mtc <- pmatch("mean", cnames)
+ if(is.na(mean.mtc)) {
+ mean.mtc <- pmatch("mean.mean", cnames)
+ }
+ if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
+
+ # risk along the efficient frontier
+ # get the match.col column
+ mtc <- pmatch(match.col, cnames)
+ if(is.na(mtc)) {
+ mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames)
+ }
+ if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+
# compute the weights for the barplot
pos.weights <- +0.5 * (abs(wts) + wts)
neg.weights <- -0.5 * (abs(wts) - wts)
@@ -256,22 +279,7 @@
barplot(t(neg.weights), col = colorset, space = 0, add = TRUE, border = element.color,
cex.axis=cex.axis, axes=FALSE, axisnames=FALSE, ...)
- # return along the efficient frontier
- # get the "mean" column
- mean.mtc <- pmatch("mean", cnames)
- if(is.na(mean.mtc)) {
- mean.mtc <- pmatch("mean.mean", cnames)
- }
- if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
- # risk along the efficient frontier
- # get the match.col column
- mtc <- pmatch(match.col, cnames)
- if(is.na(mtc)) {
- mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames)
- }
- if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
-
# Add labels
ef.return <- frontier[, mean.mtc]
ef.risk <- frontier[, mtc]
@@ -322,14 +330,14 @@
if(is.na(mean.mtc)) {
mean.mtc <- pmatch("mean.mean", cnames)
}
- if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
+ if(is.na(mean.mtc)) stop("could not match 'mean' with column name of efficient frontier")
# get the match.col column
mtc <- pmatch(match.col, cnames)
if(is.na(mtc)) {
mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames)
}
- if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+ if(is.na(mtc)) stop("could not match match.col with column name of efficient frontier")
if(chart.assets){
# get the data to plot scatter of asset returns
@@ -358,3 +366,78 @@
box(col = element.color)
}
+#' Plot multiple efficient frontiers
+#'
+#' Overlay the efficient frontiers of multiple portfolio objects on a single plot
+#'
+#' @param R an xts object of asset returns
+#' @param portfolio_list list of portfolio objects created by \code{\link{portfolio.spec}}
+#' @param type type of efficient frontier, see \code{\link{create.EfficientFrontier}}
+#' @param n.portfolios number of portfolios to extract along the efficient frontier.
+#' This is only used for objects of class \code{optimize.portfolio}
+#' @param match.col match.col string name of column to use for risk (horizontal axis).
+#' Must match the name of an objective.
+#' @param seach_size passed to optimize.portfolio for type="DEoptim" or type="random"
+#' @param main main title used in the plot.
+#' @param cex.axis The magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}.
+#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc.
+#' @param legend.loc location of the legend; NULL, "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center"
+#' @param legend.labels character vector to use for the legend labels
+#' @param cex.legend The magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}.
+#' @param xlim set the x-axis limit, same as in \code{\link{plot}}
+#' @param ylim set the y-axis limit, same as in \code{\link{plot}}
+#' @param ... passthrough parameters to \code{\link{plot}}
+#' @author Ross Bennett
+#' @export
+chart.EfficientFrontierOverlay <- function(R, portfolio_list, type, n.portfolios=25, match.col="ES", search_size=2000, main="Efficient Frontiers", cex.axis=0.8, element.color="darkgray", legend.loc=NULL, legend.labels=NULL, cex.legend=0.8, xlim=NULL, ylim=NULL, ...){
+ # create multiple efficient frontier objects (one per portfolio in portfolio_list)
+ if(!is.list(portfolio_list)) stop("portfolio_list must be passed in as a list")
+ if(length(portfolio_list) == 1) warning("Only one portfolio object in portfolio_list")
+ # store in out
+ out <- list()
+ for(i in 1:length(portfolio_list)){
+ if(!is.portfolio(portfolio_list[[i]])) stop("portfolio in portfolio_list must be of class 'portfolio'")
+ out[[i]] <- create.EfficientFrontier(R=R, portfolio=portfolio_list[[i]], type=type, n.portfolios=n.portfolios, match.col=match.col, search_size=search_size)
+ }
+ # get the data to plot scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN="mean")
+ asset_risk <- scatterFUN(R=R, FUN=match.col)
+ rnames <- colnames(R)
+ # plot the assets
+ plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...)
+ axis(1, cex.axis = cex.axis, col = element.color)
+ axis(2, cex.axis = cex.axis, col = element.color)
+ box(col = element.color)
+ # risk-return scatter of the assets
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=0.8)
+
+ for(i in 1:length(out)){
+ tmp <- out[[i]]
+ tmpfrontier <- tmp$frontier
+ cnames <- colnames(tmpfrontier)
+
+ # get the "mean" column
+ mean.mtc <- pmatch("mean", cnames)
+ if(is.na(mean.mtc)) {
+ mean.mtc <- pmatch("mean.mean", cnames)
+ }
+ if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
+
+ # get the match.col column
+ mtc <- pmatch(match.col, cnames)
+ if(is.na(mtc)) {
+ mtc <- pmatch(paste(match.col, match.col, sep='.'),cnames)
+ }
+ if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+ lines(x=tmpfrontier[, mtc], y=tmpfrontier[, mean.mtc], col=i, lty=i, lwd=2)
+ }
+ if(!is.null(legend.loc)){
+ if(is.null(legend.labels)){
+ legend.labels <- paste("Portfolio", 1:length(out), sep=".")
+ }
+ legend(legend.loc, legend=legend.labels, col=1:length(out), lty=1:length(out), lwd=2, cex=cex.legend, bty="n")
+ }
+ return(invisible(out))
+}
+
Modified: pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/extract.efficient.frontier.R 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/R/extract.efficient.frontier.R 2013-08-26 04:54:04 UTC (rev 2890)
@@ -255,21 +255,23 @@
#'
#' @details currently there are 4 'types' supported to create an efficient frontier
#' \itemize{
-#' \item{"mean-var":}{ This is a special case for an efficient frontier that
-#' can be created by a QP solver. The \code{portfolio} object should have two
+#' \item{"mean-var", "mean-sd", or "mean-StdDev":}{ This is a special case for
+#' an efficient frontier that can be created by a QP solver.
+#' The \code{portfolio} object should have two
#' objectives: 1) mean and 2) var. The efficient frontier will be created via
#' \code{\link{meanvar.efficient.frontier}}.}
-#' \item{"mean-etl"}{ This is a special case for an efficient frontier that
-#' can be created by an LP solver. The \code{portfolio} object should have two
-#' objectives: 1) mean and 2) etl The efficient frontier will be created via
+#' \item{"mean-ETL", "mean-ES", "mean-CVaR", "mean-etl"}{ This is a special
+#' case for an efficient frontier that can be created by an LP solver.
+#' The \code{portfolio} object should have two objectives: 1) mean
+#' and 2) ETL/ES/CVaR. The efficient frontier will be created via
#' \code{\link{meanetl.efficient.frontier}}.}
#' \item{"DEoptim"}{ This can handle more complex constraints and objectives
-#' than the simple mean-var and mean-etl cases. For this type, we actually
+#' than the simple mean-var and mean-ETL cases. For this type, we actually
#' call \code{\link{optimize.portfolio}} with \code{optimize_method="DEoptim"}
#' and then extract the efficient frontier with
#' \code{\link{extract.efficient.frontier}}.}
#' \item{"random"}{ This can handle more complex constraints and objectives
-#' than the simple mean-var and mean-etl cases. For this type, we actually
+#' than the simple mean-var and mean-ETL cases. For this type, we actually
#' call \code{\link{optimize.portfolio}} with \code{optimize_method="random"}
#' and then extract the efficient frontier with
#' \code{\link{extract.efficient.frontier}}.}
@@ -291,17 +293,22 @@
#' \code{\link{meanetl.efficient.frontier}},
#' \code{\link{extract.efficient.frontier}}
#' @export
-create.EfficientFrontier <- function(R, portfolio, type=c("mean-var", "mean-etl", "random", "DEoptim"), n.portfolios=25, match.col="ES", search_size=2000, ...){
+create.EfficientFrontier <- function(R, portfolio, type, n.portfolios=25, match.col="ES", search_size=2000, ...){
# This is just a wrapper around a few functions to easily create efficient frontiers
# given a portfolio object and other parameters
if(!is.portfolio(portfolio)) stop("portfolio must be of class 'portfolio'")
type <- type[1]
switch(type,
+ "mean-sd"=,
+ "mean-StdDev"=,
"mean-var" = {frontier <- meanvar.efficient.frontier(portfolio=portfolio,
R=R,
n.portfolios=n.portfolios)
},
+ "mean-ETL"=,
+ "mean-CVaR"=,
+ "mean-ES"=,
"mean-etl" = {frontier <- meanetl.efficient.frontier(portfolio=portfolio,
R=R,
n.portfolios=n.portfolios)
@@ -341,8 +348,9 @@
#' created via \code{meanetl.efficient.frontier}.
#'
#' If the object is an \code{optimize.portfolio.ROI} object and \code{match.col}
-#' is "var", then the mean-var efficient frontier will be created via
-#' \code{meanvar.efficient.frontier}.
+#' is "StdDev", then the mean-StdDev efficient frontier will be created via
+#' \code{meanvar.efficient.frontier}. Note that if 'var' is specified as the
+#' name of an objective, the value returned will be 'StdDev'.
#'
#' For objects created by \code{optimize.portfolo} with the DEoptim, random, or
#' pso solvers, the efficient frontier will be extracted from the object via
@@ -351,8 +359,9 @@
#'
#' @param object an optimal portfolio object created by \code{optimize.portfolio}
#' @param match.col string name of column to use for risk (horizontal axis).
-#' \code{match.col} must match the name of an objective in the \code{portfolio}
-#' object.
+#' \code{match.col} must match the name of an objective measure in the
+#' \code{objective_measures} or \code{opt_values} slot in the object created
+#' by \code{\link{optimize.portfolio}}.
#' @param n.portfolios number of portfolios to use to plot the efficient frontier
#' @return an \code{efficient.frontier} object with weights and other metrics along the efficient frontier
#' @author Ross Bennett
@@ -372,17 +381,17 @@
if(is.null(R)) stop(paste("Not able to get asset returns from", object, "run optimize.portfolio with trace=TRUE"))
# get the objective names and check if match.col is an objective name
- objnames <- unlist(lapply(portf$objectives, function(x) x$name))
- if(!(match.col %in% objnames)){
- stop("match.col must match an objective name")
- }
+ # objnames <- unlist(lapply(portf$objectives, function(x) x$name))
+ # if(!(match.col %in% objnames)){
+ # stop("match.col must match an objective name")
+ # }
# We need to create the efficient frontier if object is of class optimize.portfolio.ROI
if(inherits(object, "optimize.portfolio.ROI")){
if(match.col %in% c("ETL", "ES", "CVaR")){
frontier <- meanetl.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
}
- if(match.col == "var"){
+ if(match.col == "StdDev"){
frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
}
} # end optimize.portfolio.ROI
Modified: pkg/PortfolioAnalytics/man/chart.EfficientFrontier.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.EfficientFrontier.Rd 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/man/chart.EfficientFrontier.Rd 2013-08-26 04:54:04 UTC (rev 2890)
@@ -32,9 +32,11 @@
\item{object}{optimal portfolio created by
\code{\link{optimize.portfolio}}}
- \item{match.col}{string name of column to use for risk
- (horizontal axis). \code{match.col} must match the name
- of an objective in the \code{portfolio} object.}
+ \item{string}{name of column to use for risk (horizontal
+ axis). \code{match.col} must match the name of an
+ objective measure in the \code{objective_measures} or
+ \code{opt_values} slot in the object created by
+ \code{\link{optimize.portfolio}}.}
\item{n.portfolios}{number of portfolios to use to plot
the efficient frontier}
@@ -68,11 +70,11 @@
For objects created by optimize.portfolio with 'ROI'
specified as the optimize_method: \itemize{ \item The
- mean-var or mean-etl efficient frontier can be plotted
+ mean-StdDev or mean-etl efficient frontier can be plotted
for optimal portfolio objects created by
\code{optimize.portfolio}.
- \item If \code{match.col="var"}, the mean-variance
+ \item If \code{match.col="StdDev"}, the mean-StdDev
efficient frontier is plotted.
\item If \code{match.col="ETL"} (also "ES" or "CVaR"),
Added: pkg/PortfolioAnalytics/man/chart.EfficientFrontierOverlay.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.EfficientFrontierOverlay.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.EfficientFrontierOverlay.Rd 2013-08-26 04:54:04 UTC (rev 2890)
@@ -0,0 +1,68 @@
+\name{chart.EfficientFrontierOverlay}
+\alias{chart.EfficientFrontierOverlay}
+\title{Plot multiple efficient frontiers}
+\usage{
+ chart.EfficientFrontierOverlay(R, portfolio_list, type,
+ n.portfolios = 25, match.col = "ES",
+ search_size = 2000, main = "Efficient Frontiers",
+ cex.axis = 0.8, element.color = "darkgray",
+ legend.loc = NULL, legend.labels = NULL,
+ cex.legend = 0.8, xlim = NULL, ylim = NULL, ...)
+}
+\arguments{
+ \item{R}{an xts object of asset returns}
+
+ \item{portfolio_list}{list of portfolio objects created
+ by \code{\link{portfolio.spec}}}
+
+ \item{type}{type of efficient frontier, see
+ \code{\link{create.EfficientFrontier}}}
+
+ \item{n.portfolios}{number of portfolios to extract along
+ the efficient frontier. This is only used for objects of
+ class \code{optimize.portfolio}}
+
+ \item{match.col}{match.col string name of column to use
+ for risk (horizontal axis). Must match the name of an
+ objective.}
+
+ \item{seach_size}{passed to optimize.portfolio for
+ type="DEoptim" or type="random"}
+
+ \item{main}{main title used in the plot.}
+
+ \item{cex.axis}{The magnification to be used for sizing
+ the axis text relative to the current setting of 'cex',
+ similar to \code{\link{plot}}.}
+
+ \item{element.color}{provides the color for drawing
+ less-important chart elements, such as the box lines,
+ axis lines, etc.}
+
+ \item{legend.loc}{location of the legend; NULL,
+ "bottomright", "bottom", "bottomleft", "left", "topleft",
+ "top", "topright", "right" and "center"}
+
+ \item{legend.labels}{character vector to use for the
+ legend labels}
+
+ \item{cex.legend}{The magnification to be used for sizing
+ the legend relative to the current setting of 'cex',
+ similar to \code{\link{plot}}.}
+
+ \item{xlim}{set the x-axis limit, same as in
+ \code{\link{plot}}}
+
+ \item{ylim}{set the y-axis limit, same as in
+ \code{\link{plot}}}
+
+ \item{...}{passthrough parameters to \code{\link{plot}}}
+}
+\description{
+ Overlay the efficient frontiers of multiple portfolio
+ objects on a single plot
+}
+\author{
+ Ross Bennett
+}
+
Modified: pkg/PortfolioAnalytics/man/create.EfficientFrontier.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/create.EfficientFrontier.Rd 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/man/create.EfficientFrontier.Rd 2013-08-26 04:54:04 UTC (rev 2890)
@@ -2,8 +2,7 @@
\alias{create.EfficientFrontier}
\title{create an efficient frontier}
\usage{
- create.EfficientFrontier(R, portfolio,
- type = c("mean-var", "mean-etl", "random", "DEoptim"),
+ create.EfficientFrontier(R, portfolio, type,
n.portfolios = 25, match.col = "ES",
search_size = 2000, ...)
}
@@ -39,26 +38,27 @@
}
\details{
currently there are 4 'types' supported to create an
- efficient frontier \itemize{ \item{"mean-var":}{ This is
- a special case for an efficient frontier that can be
- created by a QP solver. The \code{portfolio} object
- should have two objectives: 1) mean and 2) var. The
- efficient frontier will be created via
- \code{\link{meanvar.efficient.frontier}}.}
- \item{"mean-etl"}{ This is a special case for an
- efficient frontier that can be created by an LP solver.
+ efficient frontier \itemize{ \item{"mean-var", "mean-sd",
+ or "mean-StdDev":}{ This is a special case for an
+ efficient frontier that can be created by a QP solver.
The \code{portfolio} object should have two objectives:
- 1) mean and 2) etl The efficient frontier will be created
- via \code{\link{meanetl.efficient.frontier}}.}
+ 1) mean and 2) var. The efficient frontier will be
+ created via \code{\link{meanvar.efficient.frontier}}.}
+ \item{"mean-ETL", "mean-ES", "mean-CVaR", "mean-etl"}{
+ This is a special case for an efficient frontier that can
+ be created by an LP solver. The \code{portfolio} object
+ should have two objectives: 1) mean and 2) ETL/ES/CVaR.
+ The efficient frontier will be created via
+ \code{\link{meanetl.efficient.frontier}}.}
\item{"DEoptim"}{ This can handle more complex
constraints and objectives than the simple mean-var and
- mean-etl cases. For this type, we actually call
+ mean-ETL cases. For this type, we actually call
\code{\link{optimize.portfolio}} with
\code{optimize_method="DEoptim"} and then extract the
efficient frontier with
\code{\link{extract.efficient.frontier}}.}
\item{"random"}{ This can handle more complex constraints
- and objectives than the simple mean-var and mean-etl
+ and objectives than the simple mean-var and mean-ETL
cases. For this type, we actually call
\code{\link{optimize.portfolio}} with
\code{optimize_method="random"} and then extract the
Modified: pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd 2013-08-26 04:54:04 UTC (rev 2890)
@@ -11,7 +11,9 @@
\item{match.col}{string name of column to use for risk
(horizontal axis). \code{match.col} must match the name
- of an objective in the \code{portfolio} object.}
+ of an objective measure in the \code{objective_measures}
+ or \code{opt_values} slot in the object created by
+ \code{\link{optimize.portfolio}}.}
\item{n.portfolios}{number of portfolios to use to plot
the efficient frontier}
@@ -31,9 +33,11 @@
\code{meanetl.efficient.frontier}.
If the object is an \code{optimize.portfolio.ROI} object
- and \code{match.col} is "var", then the mean-var
+ and \code{match.col} is "StdDev", then the mean-StdDev
efficient frontier will be created via
- \code{meanvar.efficient.frontier}.
+ \code{meanvar.efficient.frontier}. Note that if 'var' is
+ specified as the name of an objective, the value returned
+ will be 'StdDev'.
For objects created by \code{optimize.portfolo} with the
DEoptim, random, or pso solvers, the efficient frontier
Modified: pkg/PortfolioAnalytics/man/optimize.portfolio.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/optimize.portfolio.Rd 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/man/optimize.portfolio.Rd 2013-08-26 04:54:04 UTC (rev 2890)
@@ -68,6 +68,8 @@
\item{\code{weights}:}{ The optimal set weights.}
\item{\code{objective_measures}:}{ A list containing the
value of each objective corresponding to the optimal
+ weights.} \item{\code{opt_values}:}{ A list containing
+ the value of each objective corresponding to the optimal
weights.} \item{\code{out}:}{ The output of the solver.}
\item{\code{call}:}{ The function call.}
\item{\code{portfolio}:}{ The portfolio object.}
Modified: pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R 2013-08-26 01:04:28 UTC (rev 2889)
+++ pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R 2013-08-26 04:54:04 UTC (rev 2890)
@@ -36,22 +36,22 @@
# create efficient frontiers
# mean-var efficient frontier
-meanvar.ef <- create.EfficientFrontier(R=R, portfolio=meanvar.portf, type="mean-var")
-chart.EfficientFrontier(meanvar.ef, match.col="var", type="b")
-chart.Weights.EF(meanvar.ef, colorset=bluemono, match.col="var")
+meanvar.ef <- create.EfficientFrontier(R=R, portfolio=meanvar.portf, type="mean-StdDev")
+chart.EfficientFrontier(meanvar.ef, match.col="StdDev", type="b")
+chart.Weights.EF(meanvar.ef, colorset=bluemono, match.col="StdDev")
# run optimize.portfolio and chart the efficient frontier for that object
opt_meanvar <- optimize.portfolio(R=R, portfolio=meanvar.portf, optimize_method="ROI", trace=TRUE)
-chart.EfficientFrontier(opt_meanvar, match.col="var", n.portfolios=50)
+chart.EfficientFrontier(opt_meanvar, match.col="StdDev", n.portfolios=50)
# The weights along the efficient frontier can be plotted by passing in the
# optimize.portfolio output object
-chart.Weights.EF(opt_meanvar, match.col="var")
+chart.Weights.EF(opt_meanvar, match.col="StdDev")
# or we can extract the efficient frontier and then plot it
-ef <- extractEfficientFrontier(object=opt_meanvar, match.col="var", n.portfolios=15)
+ef <- extractEfficientFrontier(object=opt_meanvar, match.col="StdDev", n.portfolios=15)
chart.Weights.EF(ef, match.col="var", colorset=bluemono)
# mean-etl efficient frontier
-meanetl.ef <- create.EfficientFrontier(R=R, portfolio=meanetl.portf, type="mean-etl")
+meanetl.ef <- create.EfficientFrontier(R=R, portfolio=meanetl.portf, type="mean-ES")
chart.EfficientFrontier(meanetl.ef, match.col="ES", main="mean-ETL Efficient Frontier", type="l", col="blue")
chart.Weights.EF(meanetl.ef, colorset=bluemono, match.col="ES")
@@ -83,62 +83,10 @@
group.portf <- add.constraint(portfolio=group.portf, type="long_only")
# optimize.portfolio(R=R, portfolio=group.portf, optimize_method="ROI")
-foo <- function(R, portfolio_list, type, match.col="ES", main="Efficient Frontiers", cex.axis=0.8, element.color="darkgray", legend.loc=NULL, legend.labels=NULL, cex.legend=0.8, xlim=NULL, ylim=NULL, ...){
-
- # create multiple efficient frontier objects (one per portfolio in portfolio_list)
- # store in out
- out <- list()
- for(i in 1:length(portfolio_list)){
- if(!is.portfolio(portfolio_list[[i]])) stop("portfolio in portfolio_list must be of class 'portfolio'")
- out[[i]] <- create.EfficientFrontier(R=R, portfolio=portfolio_list[[i]], type=type)
- }
- # get the data to plot scatter of asset returns
- asset_ret <- scatterFUN(R=R, FUN="mean")
- asset_risk <- scatterFUN(R=R, FUN=match.col)
- rnames <- colnames(R)
- # plot the assets
- plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...)
- axis(1, cex.axis = cex.axis, col = element.color)
- axis(2, cex.axis = cex.axis, col = element.color)
- box(col = element.color)
- # risk-return scatter of the assets
- points(x=asset_risk, y=asset_ret)
- text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=0.8)
-
- for(i in 1:length(out)){
- tmp <- out[[i]]
- tmpfrontier <- tmp$frontier
- cnames <- colnames(tmpfrontier)
-
- # get the "mean" column
- mean.mtc <- pmatch("mean", cnames)
- if(is.na(mean.mtc)) {
- mean.mtc <- pmatch("mean.mean", cnames)
- }
- if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
-
- # get the match.col column
- mtc <- pmatch(match.col, cnames)
- if(is.na(mtc)) {
- mtc <- pmatch(paste(match.col, match.col, sep='.'),cnames)
- }
- if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
- lines(x=tmpfrontier[, mtc], y=tmpfrontier[, mean.mtc], col=i, lty=i, lwd=2)
- }
- if(!is.null(legend.loc)){
- if(is.null(legend.labels)){
- legend.labels <- paste("Portfolio", 1:length(out), sep=".")
- }
- legend(legend.loc, legend=legend.labels, col=1:length(out), lty=1:length(out), lwd=2, cex=cex.legend, bty="n")
- }
- return(invisible(out))
-}
-
portf.list <- list(lo.portf, box.portf, group.portf)
legend.labels <- c("Long Only", "Box", "Group + Long Only")
-foo(R=R, portfolio_list=portf.list, type="mean-var", match.col="var",
- ylim=c(0.0055, 0.0085), xlim=c(0, 0.0025), legend.loc="bottomright",
- legend.labels=legend.labels)
+chart.EfficientFrontierOverlay(R=R, portfolio_list=portf.list, type="mean-StdDev", match.col="StdDev",
+ legend.loc="right", legend.labels=legend.labels)
# TODO add the following methods for objects of class efficient.frontier
# - print
More information about the Returnanalytics-commits
mailing list