[Returnanalytics-commits] r2859 - in pkg/PortfolioAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 23 00:46:07 CEST 2013
Author: rossbennett34
Date: 2013-08-23 00:46:06 +0200 (Fri, 23 Aug 2013)
New Revision: 2859
Added:
pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.Rd
Modified:
pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
pkg/PortfolioAnalytics/man/extractStats.optimize.portfolio.pso.Rd
Log:
Adding function to chart the efficient frontier for optimize.portfolio objects.
Modified: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-22 22:33:48 UTC (rev 2858)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-22 22:46:06 UTC (rev 2859)
@@ -78,4 +78,85 @@
box(col = element.color)
}
-
+#' 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 efficient frontier plotted is based on the the trace information (sets of
+#' portfolios tested by the solver at each iteration) in objects created by
+#' \code{optimize.portfolio}. When running \code{optimize.portfolio},
+#' \code{trace=TRUE} must be specified.
+#'
+#' @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 <- function(object, match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", ...){
+ # This function will work with objects of class optimize.portfolio.DEoptim,
+ # optimize.portfolio.random, and optimize.portfolio.pso
+
+ if(inherits(object, "optimize.portfolio.GenSA")){
+ stop("GenSA does not return any useable trace information for portfolios tested, thus we cannot extract an efficient portfolio")
+ }
+
+ if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio")
+
+ portf <- object$portfolio
+ R <- object$R
+ if(is.null(R)) stop(paste("Not able to get asset returns from", object))
+ wts <- object$weights
+
+ # get the stats from the object
+ xtract <- extractStats(object=object)
+ columnames <- colnames(xtract)
+
+ # Check if match.col is in extractStats output
+ if(!(match.col %in% columnames)){
+ stop(paste(match.col, "is not a column in extractStats output"))
+ }
+
+ # check if 'mean' is in extractStats output
+ if(!("mean" %in% columnames)){
+ stop("mean is not a column in extractStats output")
+ }
+
+ # get the stats of the optimal portfolio
+ optstats <- xtract[which.min(xtract[, "out"]), ]
+ opt_ret <- optstats["mean"]
+ opt_risk <- optstats[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)
+
+ # get the data of the efficient frontier
+ frontier <- extract.efficient.frontier(object=object, match.col=match.col)
+
+ # 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.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.Efficient.Frontier.optimize.portfolio.Rd 2013-08-22 22:46:06 UTC (rev 2859)
@@ -0,0 +1,39 @@
+\name{chart.Efficient.Frontier.optimize.portfolio}
+\alias{chart.Efficient.Frontier.optimize.portfolio}
+\title{chart the efficient frontier and risk-reward scatter plot of the assets}
+\usage{
+ chart.Efficient.Frontier.optimize.portfolio(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 efficient
+ frontier plotted is based on the the trace information
+ (sets of portfolios tested by the solver at each
+ iteration) in objects created by
+ \code{optimize.portfolio}. When running
+ \code{optimize.portfolio}, \code{trace=TRUE} must be
+ specified.
+}
+
Modified: pkg/PortfolioAnalytics/man/extractStats.optimize.portfolio.pso.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extractStats.optimize.portfolio.pso.Rd 2013-08-22 22:33:48 UTC (rev 2858)
+++ pkg/PortfolioAnalytics/man/extractStats.optimize.portfolio.pso.Rd 2013-08-22 22:46:06 UTC (rev 2859)
@@ -16,7 +16,10 @@
\description{
This function will extract the weights (swarm positions)
from the PSO output and the out value (swarm fitness
- values) for each iteration of the optimization.
+ values) for each iteration of the optimization. This
+ function can be slow because we need to run
+ \code{constrained_objective} to calculate the objective
+ measures on the weights.
}
\author{
Ross Bennett
More information about the Returnanalytics-commits
mailing list