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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 22 17:36:03 CEST 2013


Author: rossbennett34
Date: 2013-08-22 17:36:03 +0200 (Thu, 22 Aug 2013)
New Revision: 2854

Added:
   pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
   pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.ROI.Rd
Modified:
   pkg/PortfolioAnalytics/DESCRIPTION
   pkg/PortfolioAnalytics/R/applyFUN.R
Log:
adding chart.Efficient.Frontier function for optimize.portfolio.ROI objects

Modified: pkg/PortfolioAnalytics/DESCRIPTION
===================================================================
--- pkg/PortfolioAnalytics/DESCRIPTION	2013-08-22 13:02:02 UTC (rev 2853)
+++ pkg/PortfolioAnalytics/DESCRIPTION	2013-08-22 15:36:03 UTC (rev 2854)
@@ -52,3 +52,4 @@
     'charts.GenSA.R'
     'chart.Weights.R'
     'chart.RiskReward.R'
+    'charts.efficient.frontier.R'

Modified: pkg/PortfolioAnalytics/R/applyFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/applyFUN.R	2013-08-22 13:02:02 UTC (rev 2853)
+++ pkg/PortfolioAnalytics/R/applyFUN.R	2013-08-22 15:36:03 UTC (rev 2854)
@@ -103,6 +103,11 @@
            #fun = match.fun(mean)
            #nargs$x = R
          },
+         var = {
+           return(as.numeric(apply(R, 2, var)))
+           #fun = match.fun(mean)
+           #nargs$x = R
+         },
          sd =,
          StdDev = { 
            fun = match.fun(StdDev)

Added: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	2013-08-22 15:36:03 UTC (rev 2854)
@@ -0,0 +1,81 @@
+
+#' chart the efficient frontier and risk-reward scatter plot of the assets
+#' 
+#' This function charts the efficient frontier and risk-reward scatter plot of the assets.
+#' The mean-var or mean-etl efficient frontier can be plotted for optimal
+#' portfolio objects created by \code{optimize.portfolio}.
+#' 
+#' If \code{match.col="var"}, the mean-variance efficient frontier is plotted.
+#' 
+#' If \code{match.col="ETL"} (also "ES" or "CVaR"), the mean-etl efficient frontier is plotted.
+#' 
+#' @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 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 main a main title for the plot
+#' @param ... passthrough parameters to \code{\link{plot}}
+chart.Efficient.Frontier.optimize.portfolio.ROI <- function(object, match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", ...){
+  if(!inherits(object, "optimize.portfolio.ROI")) stop("object must be of class optimize.portfolio.ROI")
+  
+  portf <- object$portfolio
+  R <- object$R
+  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")
+  }
+  
+  # get the optimal return and risk metrics
+  xtract <- extractStats(object=object)
+  columnames <- colnames(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 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)
+  
+  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){
+    frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios)
+  }
+  # data points to plot the frontier
+  x.f <- frontier[, match.col]
+  y.f <- frontier[, "mean"]
+  
+  # set the x and y limits
+  if(is.null(xlim)){
+    xlim <- range(c(x.f, asset_risk))
+  }
+  if(is.null(ylim)){
+    ylim <- range(c(y.f, asset_ret))
+  }
+  
+  # plot a scatter of the assets
+  plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, pch=5, axes=FALSE, ...)
+  text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=0.8)
+  # plot the efficient line
+  lines(x=x.f, y=y.f, col="darkgray", lwd=2)
+  # plot the optimal portfolio
+  points(opt_risk, opt_ret, col="blue", pch=16) # optimal
+  text(x=opt_risk, y=opt_ret, labels="Optimal",col="blue", pos=4, cex=0.8)
+  axis(1, cex.axis = cex.axis, col = element.color)
+  axis(2, cex.axis = cex.axis, col = element.color)
+  box(col = element.color)
+}
+
+

Added: pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.ROI.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.ROI.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.ROI.Rd	2013-08-22 15:36:03 UTC (rev 2854)
@@ -0,0 +1,42 @@
+\name{chart.Efficient.Frontier.optimize.portfolio.ROI}
+\alias{chart.Efficient.Frontier.optimize.portfolio.ROI}
+\title{chart the efficient frontier and risk-reward scatter plot of the assets}
+\usage{
+  chart.Efficient.Frontier.optimize.portfolio.ROI(object,
+    match.col = "ES", n.portfolios = 25, xlim = NULL,
+    ylim = NULL, cex.axis = 0.8,
+    element.color = "darkgray",
+    main = "Efficient Frontier", ...)
+}
+\arguments{
+  \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{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{main}{a main title for the plot}
+
+  \item{...}{passthrough parameters to \code{\link{plot}}}
+}
+\description{
+  This function charts the efficient frontier and
+  risk-reward scatter plot of the assets. The mean-var or
+  mean-etl efficient frontier can be plotted for optimal
+  portfolio objects created by \code{optimize.portfolio}.
+}
+\details{
+  If \code{match.col="var"}, the mean-variance efficient
+  frontier is plotted.
+
+  If \code{match.col="ETL"} (also "ES" or "CVaR"), the
+  mean-etl efficient frontier is plotted.
+}
+



More information about the Returnanalytics-commits mailing list