[Returnanalytics-commits] r2734 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Aug 7 00:17:15 CEST 2013
Author: rossbennett34
Date: 2013-08-07 00:17:14 +0200 (Wed, 07 Aug 2013)
New Revision: 2734
Added:
pkg/PortfolioAnalytics/R/charts.GenSA.R
pkg/PortfolioAnalytics/man/chart.Scatter.GenSA.Rd
pkg/PortfolioAnalytics/man/chart.Weights.GenSA.Rd
pkg/PortfolioAnalytics/man/charts.GenSA.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd
Modified:
pkg/PortfolioAnalytics/DESCRIPTION
pkg/PortfolioAnalytics/NAMESPACE
Log:
adding chart methods for GenSA optimization method
Modified: pkg/PortfolioAnalytics/DESCRIPTION
===================================================================
--- pkg/PortfolioAnalytics/DESCRIPTION 2013-08-06 21:47:01 UTC (rev 2733)
+++ pkg/PortfolioAnalytics/DESCRIPTION 2013-08-06 22:17:14 UTC (rev 2734)
@@ -49,3 +49,4 @@
'charts.ROI.R'
'applyFUN.R'
'charts.PSO.R'
+ 'charts.GenSA.R'
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-08-06 21:47:01 UTC (rev 2733)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-08-06 22:17:14 UTC (rev 2734)
@@ -6,14 +6,17 @@
export(box_constraint)
export(CCCgarch.MM)
export(chart.Scatter.DE)
+export(chart.Scatter.GenSA)
export(chart.Scatter.pso)
export(chart.Scatter.ROI)
export(chart.Scatter.RP)
export(chart.Weights.DE)
+export(chart.Weights.GenSA)
export(chart.Weights.pso)
export(chart.Weights.ROI)
export(chart.Weights.RP)
export(charts.DE)
+export(charts.GenSA)
export(charts.pso)
export(charts.ROI)
export(charts.RP)
@@ -56,6 +59,7 @@
export(optimize.portfolio.rebalancing)
export(optimize.portfolio)
export(plot.optimize.portfolio.DEoptim)
+export(plot.optimize.portfolio.GenSA)
export(plot.optimize.portfolio.pso)
export(plot.optimize.portfolio.random)
export(plot.optimize.portfolio.ROI)
Added: pkg/PortfolioAnalytics/R/charts.GenSA.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.GenSA.R (rev 0)
+++ pkg/PortfolioAnalytics/R/charts.GenSA.R 2013-08-06 22:17:14 UTC (rev 2734)
@@ -0,0 +1,173 @@
+#' boxplot of the weights in the portfolio
+#'
+#' @param GenSA object created by \code{\link{optimize.portfolio}}
+#' @param neighbors set of 'neighbor' portfolios to overplot
+#' @param las numeric in \{0,1,2,3\}; the style of axis labels
+#' \describe{
+#' \item{0:}{always parallel to the axis [\emph{default}],}
+#' \item{1:}{always horizontal,}
+#' \item{2:}{always perpendicular to the axis,}
+#' \item{3:}{always vertical.}
+#' }
+#' @param xlab a title for the x axis: see \code{\link{title}}
+#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of \code{cex}
+#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}
+#' @param element.color color for the default plot lines
+#' @param ... any other passthru parameters
+#' @param main an overall title for the plot: see \code{\link{title}}
+#' @seealso \code{\link{optimize.portfolio}}
+#' @author Ross Bennett
+#' @export
+chart.Weights.GenSA <- function(GenSA, neighbors = NULL, ..., main="Weights", las = 3, xlab=NULL, cex.lab = 1, element.color = "darkgray", cex.axis=0.8){
+
+ if(!inherits(GenSA, "optimize.portfolio.GenSA")) stop("GenSA must be of class 'optimize.portfolio.GenSA'")
+
+ columnnames = names(GenSA$weights)
+ numassets = length(columnnames)
+
+ constraints <- get_constraints(GenSA$portfolio)
+
+ if(is.null(xlab))
+ minmargin = 3
+ else
+ minmargin = 5
+ if(main=="") topmargin=1 else topmargin=4
+ if(las > 1) {# set the bottom border to accommodate labels
+ bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
+ if(bottommargin > 10 ) {
+ bottommargin<-10
+ columnnames<-substr(columnnames,1,19)
+ # par(srt=45) #TODO figure out how to use text() and srt to rotate long labels
+ }
+ }
+ else {
+ bottommargin = minmargin
+ }
+ par(mar = c(bottommargin, 4, topmargin, 2) +.1)
+ plot(GenSA$weights, type="b", col="blue", axes=FALSE, xlab='', ylim=c(0,max(constraints$max)), ylab="Weights", main=main, pch=16, ...)
+ points(constraints$min, type="b", col="darkgray", lty="solid", lwd=2, pch=24)
+ points(constraints$max, type="b", col="darkgray", lty="solid", lwd=2, pch=25)
+ # if(!is.null(neighbors)){
+ # if(is.vector(neighbors)){
+ # xtract=extractStats(ROI)
+ # weightcols<-grep('w\\.',colnames(xtract)) #need \\. to get the dot
+ # if(length(neighbors)==1){
+ # # overplot nearby portfolios defined by 'out'
+ # orderx = order(xtract[,"out"])
+ # subsetx = head(xtract[orderx,], n=neighbors)
+ # for(i in 1:neighbors) points(subsetx[i,weightcols], type="b", col="lightblue")
+ # } else{
+ # # assume we have a vector of portfolio numbers
+ # subsetx = xtract[neighbors,weightcols]
+ # for(i in 1:length(neighbors)) points(subsetx[i,], type="b", col="lightblue")
+ # }
+ # }
+ # if(is.matrix(neighbors) | is.data.frame(neighbors)){
+ # # the user has likely passed in a matrix containing calculated values for risk.col and return.col
+ # nbweights<-grep('w\\.',colnames(neighbors)) #need \\. to get the dot
+ # for(i in 1:nrow(neighbors)) points(as.numeric(neighbors[i,nbweights]), type="b", col="lightblue")
+ # # note that here we need to get weight cols separately from the matrix, not from xtract
+ # # also note the need for as.numeric. points() doesn't like matrix inputs
+ # }
+ # }
+ # points(ROI$weights, type="b", col="blue", pch=16)
+ axis(2, cex.axis = cex.axis, col = element.color)
+ axis(1, labels=columnnames, at=1:numassets, las=las, cex.axis = cex.axis, col = element.color)
+ box(col = element.color)
+}
+
+#' classic risk return scatter of random portfolios
+#'
+#' The GenSA optimizer does not store the portfolio weights like DEoptim or random
+#' portfolios so we will generate random portfolios for the scatter plot.
+#'
+#' \code{return.col} must be the name of a function used to compute the return metric on the random portfolio weights
+#' \code{risk.col} must be the name of a function used to compute the risk metric on the random portfolio weights
+#'
+#' @param ROI object created by \code{\link{optimize.portfolio}}
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns, used to recalulate the risk and return metric
+#' @param rp set of weights generated by \code{\link{random_portfolio}}
+#' @param return.col string matching the objective of a 'return' objective, on vertical axis
+#' @param risk.col string matching the objective of a 'risk' objective, on horizontal axis
+#' @param ... any other passthru parameters
+#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}
+#' @param element.color color for the default plot scatter points
+#' @seealso \code{\link{optimize.portfolio}}
+#' @author Ross Bennett
+#' @export
+chart.Scatter.GenSA <- function(GenSA, R, rp=NULL, return.col="mean", risk.col="StdDev", ..., element.color = "darkgray", cex.axis=0.8, main=""){
+
+ # If the user does not pass in rp, then we will generate random portfolios
+ if(is.null(rp)){
+ permutations <- match.call(expand.dots=TRUE)$permutations
+ if(is.null(permutations)) permutations <- 2000
+ rp <- random_portfolios(portfolio=GenSA$portfolio, permutations=permutations)
+ }
+
+ # Get the optimal weights from the output of optimize.portfolio
+ wts <- GenSA$weights
+
+ # cbind the optimal weights and random portfolio weights
+ rp <- rbind(wts, rp)
+
+ returnpoints <- applyFUN(R=R, weights=rp, FUN=return.col, ...=...)
+ riskpoints <- applyFUN(R=R, weights=rp, FUN=risk.col, ...=...)
+
+ plot(x=riskpoints, y=returnpoints, xlab=risk.col, ylab=return.col, col="darkgray", axes=FALSE, main=main)
+ points(x=riskpoints[1], y=returnpoints[1], col="blue", pch=16) # optimal
+ axis(1, cex.axis = cex.axis, col = element.color)
+ axis(2, cex.axis = cex.axis, col = element.color)
+ box(col = element.color)
+}
+
+#' scatter and weights chart for portfolios
+#'
+#' \code{return.col} must be the name of a function used to compute the return metric on the random portfolio weights
+#' \code{risk.col} must be the name of a function used to compute the risk metric on the random portfolio weights
+#'
+#' @param GenSA object created by \code{\link{optimize.portfolio}}
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns, used to recalulate the risk and return metric
+#' @param rp set of weights generated by \code{\link{random_portfolio}}
+#' @param return.col string matching the objective of a 'return' objective, on vertical axis
+#' @param risk.col string matching the objective of a 'risk' objective, on horizontal axis
+#' @param ... any other passthru parameters
+#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}
+#' @param element.color color for the default plot scatter points
+#' @param neighbors set of 'neighbor' portfolios to overplot
+#' @param main an overall title for the plot: see \code{\link{title}}
+#' @seealso \code{\link{optimize.portfolio}}
+#' @author Ross Bennett
+#' @export
+charts.GenSA <- function(GenSA, R, rp=NULL, return.col="mean", risk.col="StdDev",
+ cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
+ # Specific to the output of the optimize_method=pso
+ op <- par(no.readonly=TRUE)
+ layout(matrix(c(1,2)),height=c(2,2),width=1)
+ par(mar=c(4,4,4,2))
+ chart.Scatter.GenSA(GenSA=GenSA, R=R, rp=rp, return.col=return.col, risk.col=risk.col, element.color=element.color, cex.axis=cex.axis, main=main, ...=...)
+ par(mar=c(2,4,0,2))
+ chart.Weights.GenSA(GenSA=GenSA, neighbors=neighbors, las=3, xlab=NULL, cex.lab=1, element.color=element.color, cex.axis=cex.axis, ...=..., main="")
+ par(op)
+}
+
+#' scatter and weights chart for portfolios
+#'
+#' \code{return.col} must be the name of a function used to compute the return metric on the random portfolio weights
+#' \code{risk.col} must be the name of a function used to compute the risk metric on the random portfolio weights
+#'
+#' @param GenSA object created by \code{\link{optimize.portfolio}}
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns, used to recalulate the risk and return metric
+#' @param rp set of weights generated by \code{\link{random_portfolio}}
+#' @param return.col string matching the objective of a 'return' objective, on vertical axis
+#' @param risk.col string matching the objective of a 'risk' objective, on horizontal axis
+#' @param ... any other passthru parameters
+#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}
+#' @param element.color color for the default plot scatter points
+#' @param neighbors set of 'neighbor' portfolios to overplot
+#' @param main an overall title for the plot: see \code{\link{title}}
+#' @seealso \code{\link{optimize.portfolio}}
+#' @author Ross Bennett
+#' @export
+plot.optimize.portfolio.GenSA <- function(GenSA, R, rp=NULL, return.col="mean", risk.col="StdDev", cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
+ charts.GenSA(GenSA=GenSA, R=R, rp=rp, return.col=return.col, risk.col=risk.col, cex.axis=cex.axis, element.color=element.color, neighbors=neighbors, main=main, ...=...)
+}
Added: pkg/PortfolioAnalytics/man/chart.Scatter.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Scatter.GenSA.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.Scatter.GenSA.Rd 2013-08-06 22:17:14 UTC (rev 2734)
@@ -0,0 +1,51 @@
+\name{chart.Scatter.GenSA}
+\alias{chart.Scatter.GenSA}
+\title{classic risk return scatter of random portfolios}
+\usage{
+ chart.Scatter.GenSA(GenSA, R, rp = NULL,
+ return.col = "mean", risk.col = "StdDev", ...,
+ element.color = "darkgray", cex.axis = 0.8, main = "")
+}
+\arguments{
+ \item{ROI}{object created by
+ \code{\link{optimize.portfolio}}}
+
+ \item{R}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of asset returns, used to recalulate the
+ risk and return metric}
+
+ \item{rp}{set of weights generated by
+ \code{\link{random_portfolio}}}
+
+ \item{return.col}{string matching the objective of a
+ 'return' objective, on vertical axis}
+
+ \item{risk.col}{string matching the objective of a 'risk'
+ objective, on horizontal axis}
+
+ \item{...}{any other passthru parameters}
+
+ \item{cex.axis}{The magnification to be used for axis
+ annotation relative to the current setting of \code{cex}}
+
+ \item{element.color}{color for the default plot scatter
+ points}
+}
+\description{
+ The GenSA optimizer does not store the portfolio weights
+ like DEoptim or random portfolios so we will generate
+ random portfolios for the scatter plot.
+}
+\details{
+ \code{return.col} must be the name of a function used to
+ compute the return metric on the random portfolio weights
+ \code{risk.col} must be the name of a function used to
+ compute the risk metric on the random portfolio weights
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{optimize.portfolio}}
+}
+
Added: pkg/PortfolioAnalytics/man/chart.Weights.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Weights.GenSA.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.Weights.GenSA.Rd 2013-08-06 22:17:14 UTC (rev 2734)
@@ -0,0 +1,47 @@
+\name{chart.Weights.GenSA}
+\alias{chart.Weights.GenSA}
+\title{boxplot of the weights in the portfolio}
+\usage{
+ chart.Weights.GenSA(GenSA, neighbors = NULL, ...,
+ main = "Weights", las = 3, xlab = NULL, cex.lab = 1,
+ element.color = "darkgray", cex.axis = 0.8)
+}
+\arguments{
+ \item{GenSA}{object created by
+ \code{\link{optimize.portfolio}}}
+
+ \item{neighbors}{set of 'neighbor' portfolios to
+ overplot}
+
+ \item{las}{numeric in \{0,1,2,3\}; the style of axis
+ labels \describe{ \item{0:}{always parallel to the axis
+ [\emph{default}],} \item{1:}{always horizontal,}
+ \item{2:}{always perpendicular to the axis,}
+ \item{3:}{always vertical.} }}
+
+ \item{xlab}{a title for the x axis: see
+ \code{\link{title}}}
+
+ \item{cex.lab}{The magnification to be used for x and y
+ labels relative to the current setting of \code{cex}}
+
+ \item{cex.axis}{The magnification to be used for axis
+ annotation relative to the current setting of \code{cex}}
+
+ \item{element.color}{color for the default plot lines}
+
+ \item{...}{any other passthru parameters}
+
+ \item{main}{an overall title for the plot: see
+ \code{\link{title}}}
+}
+\description{
+ boxplot of the weights in the portfolio
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{optimize.portfolio}}
+}
+
Added: pkg/PortfolioAnalytics/man/charts.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.GenSA.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/charts.GenSA.Rd 2013-08-06 22:17:14 UTC (rev 2734)
@@ -0,0 +1,53 @@
+\name{charts.GenSA}
+\alias{charts.GenSA}
+\title{scatter and weights chart for portfolios}
+\usage{
+ charts.GenSA(GenSA, R, rp = NULL, return.col = "mean",
+ risk.col = "StdDev", cex.axis = 0.8,
+ element.color = "darkgray", neighbors = NULL,
+ main = "GenSA.Portfolios", ...)
+}
+\arguments{
+ \item{GenSA}{object created by
+ \code{\link{optimize.portfolio}}}
+
+ \item{R}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of asset returns, used to recalulate the
+ risk and return metric}
+
+ \item{rp}{set of weights generated by
+ \code{\link{random_portfolio}}}
+
+ \item{return.col}{string matching the objective of a
+ 'return' objective, on vertical axis}
+
+ \item{risk.col}{string matching the objective of a 'risk'
+ objective, on horizontal axis}
+
+ \item{...}{any other passthru parameters}
+
+ \item{cex.axis}{The magnification to be used for axis
+ annotation relative to the current setting of \code{cex}}
+
+ \item{element.color}{color for the default plot scatter
+ points}
+
+ \item{neighbors}{set of 'neighbor' portfolios to
+ overplot}
+
+ \item{main}{an overall title for the plot: see
+ \code{\link{title}}}
+}
+\description{
+ \code{return.col} must be the name of a function used to
+ compute the return metric on the random portfolio weights
+ \code{risk.col} must be the name of a function used to
+ compute the risk metric on the random portfolio weights
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{optimize.portfolio}}
+}
+
Added: pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd 2013-08-06 22:17:14 UTC (rev 2734)
@@ -0,0 +1,53 @@
+\name{plot.optimize.portfolio.GenSA}
+\alias{plot.optimize.portfolio.GenSA}
+\title{scatter and weights chart for portfolios}
+\usage{
+ plot.optimize.portfolio.GenSA(GenSA, R, rp = NULL,
+ return.col = "mean", risk.col = "StdDev",
+ cex.axis = 0.8, element.color = "darkgray",
+ neighbors = NULL, main = "GenSA.Portfolios", ...)
+}
+\arguments{
+ \item{GenSA}{object created by
+ \code{\link{optimize.portfolio}}}
+
+ \item{R}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of asset returns, used to recalulate the
+ risk and return metric}
+
+ \item{rp}{set of weights generated by
+ \code{\link{random_portfolio}}}
+
+ \item{return.col}{string matching the objective of a
+ 'return' objective, on vertical axis}
+
+ \item{risk.col}{string matching the objective of a 'risk'
+ objective, on horizontal axis}
+
+ \item{...}{any other passthru parameters}
+
+ \item{cex.axis}{The magnification to be used for axis
+ annotation relative to the current setting of \code{cex}}
+
+ \item{element.color}{color for the default plot scatter
+ points}
+
+ \item{neighbors}{set of 'neighbor' portfolios to
+ overplot}
+
+ \item{main}{an overall title for the plot: see
+ \code{\link{title}}}
+}
+\description{
+ \code{return.col} must be the name of a function used to
+ compute the return metric on the random portfolio weights
+ \code{risk.col} must be the name of a function used to
+ compute the risk metric on the random portfolio weights
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{optimize.portfolio}}
+}
+
More information about the Returnanalytics-commits
mailing list