[Returnanalytics-commits] r2875 - in pkg/PortfolioAnalytics: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Aug 24 21:38:41 CEST 2013


Author: rossbennett34
Date: 2013-08-24 21:38:40 +0200 (Sat, 24 Aug 2013)
New Revision: 2875

Added:
   pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
   pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
Log:
Adding function to extract efficient frontier from objects created by optimize.portfolio. Modifying chart.Weights.EF to work better with efficient.frontier objects.

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2013-08-24 17:52:50 UTC (rev 2874)
+++ pkg/PortfolioAnalytics/NAMESPACE	2013-08-24 19:38:40 UTC (rev 2875)
@@ -45,6 +45,7 @@
 export(diversification_constraint)
 export(diversification)
 export(extract.efficient.frontier)
+export(extractEfficientFrontier)
 export(extractObjectiveMeasures)
 export(extractStats.optimize.portfolio.DEoptim)
 export(extractStats.optimize.portfolio.GenSA)

Modified: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	2013-08-24 17:52:50 UTC (rev 2874)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	2013-08-24 19:38:40 UTC (rev 2875)
@@ -198,8 +198,16 @@
   # using ideas from weightsPlot.R in fPortfolio package
   
   if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'")
-  frontier <- object$frontier
   
+  if(is.list(object)){
+    # Objects created with create.EfficientFrontier will be a list of 2 elements
+    frontier <- object$frontier
+  } else {
+    # Objects created with extractEfficientFrontier will only be an efficient.frontier object
+    frontier <- object
+  }
+  
+  
   # get the columns with weights
   cnames <- colnames(frontier)
   wts_idx <- grep(pattern="^w\\.", cnames)
@@ -227,7 +235,8 @@
   # plot the positive weights
   barplot(t(pos.weights), col = colorset, space = 0, ylab = "",
           xlim = c(xmin, xmax), ylim = c(ymin, ymax),
-          border = element.color, cex.axis=cex.axis, ...)
+          border = element.color, cex.axis=cex.axis, 
+          axisnames=FALSE,...)
   
   # set the legend information
   if(is.null(legend.labels)){
@@ -236,7 +245,8 @@
   legend("topright", legend = legend.labels, bty = "n", cex = cex.legend, fill = colorset)
   
   # plot the negative weights
-  barplot(t(neg.weights), col = colorset, space = 0, add = TRUE, border = element.color, cex.axis=cex.axis, axes=FALSE, ...)
+  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

Modified: pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-24 17:52:50 UTC (rev 2874)
+++ pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-24 19:38:40 UTC (rev 2875)
@@ -331,3 +331,66 @@
                         R=R), class="efficient.frontier"))
 }
 
+#' Extract the efficient frontier data points
+#' 
+#' This function extracts the efficient frontier from an object created by
+#' \code{\link{optimize.portfolio}}.
+#' 
+#' If the object is an \code{optimize.portfolio.ROI} object and \code{match.col}
+#'  is "ES", "ETL", or "CVaR", then the mean-ETL efficient frontier will be 
+#'  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}.
+#' 
+#' For objects created by \code{optimize.portfolo} with the DEoptim, random, or
+#' pso solvers, the efficient frontier will be extracted from the object via
+#' \code{extract.efficient.frontier}. This means that \code{optimize.portfolio} must
+#' be run with \code{trace=TRUE}
+#' 
+#' @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.
+#' @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
+#' @export
+extractEfficientFrontier <- function(object, match.col="ES", n.portfolios=25){
+  # extract the efficient frontier from an optimize.portfolio output object
+  
+  if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")
+  
+  if(inherits(object, "optimize.portfolio.GenSA")){
+    stop("GenSA does not return any useable trace information for portfolios tested, thus we cannot extract an efficient frontier.")
+  }
+  
+  # get the portfolio and returns 
+  portf <- object$portfolio
+  R <- object$R
+  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")
+  }
+  
+  # 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"){
+      frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
+    }
+  } # end optimize.portfolio.ROI
+  
+  # use extract.efficient.frontier for otpimize.portfolio output objects with global solvers
+  if(inherits(object, c("optimize.portfolio.random", "optimize.portfolio.DEoptim", "optimize.portfolio.pso"))){
+    frontier <- extract.efficient.frontier(object=object, match.col=match.col, n.portfolios=n.portfolios)
+  }
+  return(frontier)
+}
+

Added: pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/extractEfficientFrontier.Rd	2013-08-24 19:38:40 UTC (rev 2875)
@@ -0,0 +1,48 @@
+\name{extractEfficientFrontier}
+\alias{extractEfficientFrontier}
+\title{Extract the efficient frontier data points}
+\usage{
+  extractEfficientFrontier(object, match.col = "ES",
+    n.portfolios = 25)
+}
+\arguments{
+  \item{object}{an optimal portfolio object created by
+  \code{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{n.portfolios}{number of portfolios to use to plot
+  the efficient frontier}
+}
+\value{
+  an \code{efficient.frontier} object with weights and
+  other metrics along the efficient frontier
+}
+\description{
+  This function extracts the efficient frontier from an
+  object created by \code{\link{optimize.portfolio}}.
+}
+\details{
+  If the object is an \code{optimize.portfolio.ROI} object
+  and \code{match.col} is "ES", "ETL", or "CVaR", then the
+  mean-ETL efficient frontier will be 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}.
+
+  For objects created by \code{optimize.portfolo} with the
+  DEoptim, random, or pso solvers, the efficient frontier
+  will be extracted from the object via
+  \code{extract.efficient.frontier}. This means that
+  \code{optimize.portfolio} must be run with
+  \code{trace=TRUE}
+}
+\author{
+  Ross Bennett
+}
+



More information about the Returnanalytics-commits mailing list