[Returnanalytics-commits] r3067 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Sep 12 05:58:14 CEST 2013
Author: rossbennett34
Date: 2013-09-12 05:58:13 +0200 (Thu, 12 Sep 2013)
New Revision: 3067
Added:
pkg/PortfolioAnalytics/man/chart.RiskBudget.opt.list.Rd
pkg/PortfolioAnalytics/man/chart.RiskBudget.optimize.portfolio.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/charts.risk.R
pkg/PortfolioAnalytics/man/chart.RiskBudget.Rd
pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
pkg/PortfolioAnalytics/man/chart.Weights.Rd
Log:
Making chart.RiskBudget a generic function. Adding chart.RiskBudget.opt.list function to chart risk contribution for opt.list objects.
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-09-12 01:05:36 UTC (rev 3066)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-09-12 03:58:13 UTC (rev 3067)
@@ -82,6 +82,8 @@
S3method(chart.EfficientFrontier,efficient.frontier)
S3method(chart.EfficientFrontier,optimize.portfolio.ROI)
S3method(chart.EfficientFrontier,optimize.portfolio)
+S3method(chart.RiskBudget,opt.list)
+S3method(chart.RiskBudget,optimize.portfolio)
S3method(chart.RiskReward,opt.list)
S3method(chart.RiskReward,optimize.portfolio.DEoptim)
S3method(chart.RiskReward,optimize.portfolio.GenSA)
Modified: pkg/PortfolioAnalytics/R/charts.risk.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.risk.R 2013-09-12 01:05:36 UTC (rev 3066)
+++ pkg/PortfolioAnalytics/R/charts.risk.R 2013-09-12 03:58:13 UTC (rev 3067)
@@ -1,6 +1,18 @@
-#' Chart risk contribution or percent contribution
+#' Generic method to chart risk contribution
#'
+#' This function is the generic method to chart risk budget objectives for
+#' \code{optimize.portfolio} and \code{opt.list} objects.
+#'
+#' @param object optimal portfolio object created by \code{\link{optimize.portfolio}}
+#' @param ... passthrough parameters to \code{\link{plot}}
+#' @export
+chart.RiskBudget <- function(object, ...){
+ UseMethod("chart.RiskBudget")
+}
+
+#' Chart risk contribution of an \code{optimize.portfolio} object
+#'
#' This function charts the contribution or percent contribution of the resulting
#' objective measures in \code{risk_budget_objectives}.
#'
@@ -32,8 +44,9 @@
#' }
#' @param ylim set the y-axis limit, same as in \code{\link{plot}}
#' @author Ross Bennett
-#' @export
-chart.RiskBudget <- function(object, neighbors=NULL, ..., risk.type="absolute", main="Risk Contribution", ylab="", xlab=NULL, cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, ylim=NULL){
+#' @method chart.RiskBudget optimize.portfolio
+#' @S3method chart.RiskBudget optimize.portfolio
+chart.RiskBudget.optimize.portfolio <- function(object, ..., neighbors=NULL, risk.type="absolute", main="Risk Contribution", ylab="", xlab=NULL, cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, ylim=NULL){
if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio")
portfolio <- object$portfolio
# class of each objective
@@ -186,6 +199,136 @@
box(col = element.color)
} # end for loop of risk_budget_objective
} # end plot for pct_contrib risk.type
+}
+
+#' Chart risk contribution of an \code{opt.list} object
+#'
+#' This function charts the absolute contribution or percent contribution of
+#' the resulting objective measures in the \code{opt.list} object.
+#'
+#' @param object list of optimal portfolio objects created by \code{\link{optimizations.combine}}
+#' @param \dots any other passthru parameter
+#' @param match.col string of risk column to match. The \code{opt.list} object
+#' may contain risk budgets for ES or StdDev and this will match the proper
+#' column names (e.g. ES.contribution).
+#' @param risk.type "absolute" or "percentage" plot risk contribution in absolute terms or percentage contribution
+#' @param main main title for the chart
+#' @param plot.type "line" or "barplot"
+#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}
+#' @param cex.lab 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 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 ylim set the y-axis limit, same as in \code{\link{plot}}
+#' @param colorset color palette or vector of colors to use
+#' @param legend.loc legend.loc NULL, "topright", "right", or "bottomright". If legend.loc is NULL, the legend will not be plotted
+#' @param cex.legend The magnification to be used for the legend relative to the current setting of \code{cex}
+#' @author Ross Bennett
+#' @method chart.RiskBudget opt.list
+#' @S3method chart.RiskBudget opt.list
+chart.RiskBudget.opt.list <- function(object, ..., match.col="ES", risk.type="absolute", main="Risk Budget", plot.type="line", cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, ylim=NULL, colorset=NULL, legend.loc=NULL, cex.legend=0.8){
+ if(!inherits(object, "opt.list")) stop("object must be of class 'opt.list'")
+ xtract <- extractObjectiveMeasures(object)
+ if(risk.type == "absolute"){
+ # get the index of columns with risk budget
+ rbcols <- grep(paste(match.col, "contribution", sep="."), colnames(xtract))
+ dat <- na.omit(xtract[, rbcols])
+ opt_names <- rownames(dat)
+ # remove everything up to the last dot (.) to extract the names
+ colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
+
+ # set the colors
+ if(is.null(colorset)) colorset <- 1:nrow(dat)
+ columnnames <- colnames(dat)
+ numassets <- length(columnnames)
+
+ xlab <- NULL
+ 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)
+
+ if(is.null(ylim)) ylim <- range(dat)
+
+ plot(dat[1,], type="n", ylim=ylim, xlab='', ylab=paste(match.col, "Contribution", sep=" "), main=main, cex.lab=cex.lab, axes=FALSE)
+ for(i in 1:nrow(dat)){
+ points(dat[i, ], type="b", col=colorset[i], ...) # add dots here
+ }
+
+ # set the axis
+ 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)
+
+ # Add a legend
+ if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, col=colorset, lty=1, bty="n", cex=cex.legend)
+ }
+
+ if(risk.type %in% c("percent", "percentage", "pct_contrib")){
+ # get the index of columns with risk budget
+ rbcols <- grep(paste(match.col, "pct_contrib", sep="."), colnames(xtract))
+ dat <- na.omit(xtract[, rbcols])
+ opt_names <- rownames(dat)
+ # remove everything up to the last dot (.) to extract the names
+ colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
+
+ # set the colors
+ if(is.null(colorset)) colorset <- 1:nrow(dat)
+
+ columnnames <- colnames(dat)
+ numassets <- length(columnnames)
+
+ xlab <- NULL
+ 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)
+
+ if(is.null(ylim)) ylim <- range(dat)
+
+ plot(dat[1,], type="n", ylim=ylim, xlab='', ylab=paste(match.col, "% Contribution", sep=" "), main=main, cex.lab=cex.lab, axes=FALSE)
+ for(i in 1:nrow(dat)){
+ points(dat[i, ], type="b", col=colorset[i], ...) # add dots here
+ }
+
+ 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)
+
+ # Add a legend
+ if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, col=colorset, lty=1, bty="n", cex=cex.legend)
+ }
}
Modified: pkg/PortfolioAnalytics/man/chart.RiskBudget.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskBudget.Rd 2013-09-12 01:05:36 UTC (rev 3066)
+++ pkg/PortfolioAnalytics/man/chart.RiskBudget.Rd 2013-09-12 03:58:13 UTC (rev 3067)
@@ -1,67 +1,18 @@
\name{chart.RiskBudget}
\alias{chart.RiskBudget}
-\title{Chart risk contribution or percent contribution}
+\title{Generic method to chart risk contribution}
\usage{
- chart.RiskBudget(object, neighbors = NULL, ...,
- risk.type = "absolute", main = "Risk Contribution",
- ylab = "", xlab = NULL, cex.axis = 0.8, cex.lab = 0.8,
- element.color = "darkgray", las = 3, ylim = NULL)
+ chart.RiskBudget(object, ...)
}
\arguments{
\item{object}{optimal portfolio object created by
\code{\link{optimize.portfolio}}}
- \item{neighbors}{risk contribution or pct_contrib of
- neighbor portfolios to be plotted}
-
\item{...}{passthrough parameters to \code{\link{plot}}}
-
- \item{risk.type}{plot risk contribution in absolute terms
- or percentage contribution}
-
- \item{main}{main title for the chart}
-
- \item{ylab}{label for the y-axis}
-
- \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{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{ylim}{set the y-axis limit, same as in
- \code{\link{plot}}}
}
\description{
- This function charts the contribution or percent
- contribution of the resulting objective measures in
- \code{risk_budget_objectives}.
+ This function is the generic method to chart risk budget
+ objectives for \code{optimize.portfolio} and
+ \code{opt.list} objects.
}
-\details{
- \code{neighbors} may be specified in three ways. The
- first is as a single number of neighbors. This will
- extract the \code{neighbors} closest portfolios in terms
- of the \code{out} numerical statistic. The second method
- consists of a numeric vector for \code{neighbors}. This
- will extract the \code{neighbors} with portfolio index
- numbers that correspond to the vector contents. The third
- method for specifying \code{neighbors} is to pass in a
- matrix. This matrix should look like the output of
- \code{\link{extractStats}}, and should contain properly
- named contribution and pct_contrib columns.
-}
-\author{
- Ross Bennett
-}
Added: pkg/PortfolioAnalytics/man/chart.RiskBudget.opt.list.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskBudget.opt.list.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.RiskBudget.opt.list.Rd 2013-09-12 03:58:13 UTC (rev 3067)
@@ -0,0 +1,65 @@
+\name{chart.RiskBudget.opt.list}
+\alias{chart.RiskBudget.opt.list}
+\title{Chart risk contribution of an \code{opt.list} object}
+\usage{
+ \method{chart.RiskBudget}{opt.list} (object, ...,
+ match.col = "ES", risk.type = "absolute",
+ main = "Risk Budget", plot.type = "line",
+ cex.axis = 0.8, cex.lab = 0.8,
+ element.color = "darkgray", las = 3, ylim = NULL,
+ colorset = NULL, legend.loc = NULL, cex.legend = 0.8)
+}
+\arguments{
+ \item{object}{list of optimal portfolio objects created
+ by \code{\link{optimizations.combine}}}
+
+ \item{\dots}{any other passthru parameter}
+
+ \item{match.col}{string of risk column to match. The
+ \code{opt.list} object may contain risk budgets for ES or
+ StdDev and this will match the proper column names (e.g.
+ ES.contribution).}
+
+ \item{risk.type}{"absolute" or "percentage" plot risk
+ contribution in absolute terms or percentage
+ contribution}
+
+ \item{main}{main title for the chart}
+
+ \item{plot.type}{"line" or "barplot"}
+
+ \item{cex.axis}{The magnification to be used for axis
+ annotation relative to the current setting of \code{cex}}
+
+ \item{cex.lab}{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{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{ylim}{set the y-axis limit, same as in
+ \code{\link{plot}}}
+
+ \item{colorset}{color palette or vector of colors to use}
+
+ \item{legend.loc}{legend.loc NULL, "topright", "right",
+ or "bottomright". If legend.loc is NULL, the legend will
+ not be plotted}
+
+ \item{cex.legend}{The magnification to be used for the
+ legend relative to the current setting of \code{cex}}
+}
+\description{
+ This function charts the absolute contribution or percent
+ contribution of the resulting objective measures in the
+ \code{opt.list} object.
+}
+\author{
+ Ross Bennett
+}
+
Added: pkg/PortfolioAnalytics/man/chart.RiskBudget.optimize.portfolio.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskBudget.optimize.portfolio.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.RiskBudget.optimize.portfolio.Rd 2013-09-12 03:58:13 UTC (rev 3067)
@@ -0,0 +1,68 @@
+\name{chart.RiskBudget.optimize.portfolio}
+\alias{chart.RiskBudget.optimize.portfolio}
+\title{Chart risk contribution of an \code{optimize.portfolio} object}
+\usage{
+ \method{chart.RiskBudget}{optimize.portfolio} (object,
+ ..., neighbors = NULL, risk.type = "absolute",
+ main = "Risk Contribution", ylab = "", xlab = NULL,
+ cex.axis = 0.8, cex.lab = 0.8,
+ element.color = "darkgray", las = 3, ylim = NULL)
+}
+\arguments{
+ \item{object}{optimal portfolio object created by
+ \code{\link{optimize.portfolio}}}
+
+ \item{neighbors}{risk contribution or pct_contrib of
+ neighbor portfolios to be plotted}
+
+ \item{...}{passthrough parameters to \code{\link{plot}}}
+
+ \item{risk.type}{plot risk contribution in absolute terms
+ or percentage contribution}
+
+ \item{main}{main title for the chart}
+
+ \item{ylab}{label for the y-axis}
+
+ \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{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{ylim}{set the y-axis limit, same as in
+ \code{\link{plot}}}
+}
+\description{
+ This function charts the contribution or percent
+ contribution of the resulting objective measures in
+ \code{risk_budget_objectives}.
+}
+\details{
+ \code{neighbors} may be specified in three ways. The
+ first is as a single number of neighbors. This will
+ extract the \code{neighbors} closest portfolios in terms
+ of the \code{out} numerical statistic. The second method
+ consists of a numeric vector for \code{neighbors}. This
+ will extract the \code{neighbors} with portfolio index
+ numbers that correspond to the vector contents. The third
+ method for specifying \code{neighbors} is to pass in a
+ matrix. This matrix should look like the output of
+ \code{\link{extractStats}}, and should contain properly
+ named contribution and pct_contrib columns.
+}
+\author{
+ Ross Bennett
+}
+
Modified: pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskReward.Rd 2013-09-12 01:05:36 UTC (rev 3066)
+++ pkg/PortfolioAnalytics/man/chart.RiskReward.Rd 2013-09-12 03:58:13 UTC (rev 3067)
@@ -8,6 +8,8 @@
\alias{chart.RiskReward.optimize.portfolio.ROI}
\title{classic risk reward scatter}
\usage{
+ chart.RiskReward(object, ...)
+
\method{chart.RiskReward}{optimize.portfolio.DEoptim} (object, ..., neighbors = NULL, return.col = "mean",
risk.col = "ES", chart.assets = FALSE,
element.color = "darkgray", cex.axis = 0.8,
@@ -33,8 +35,6 @@
element.color = "darkgray", cex.axis = 0.8,
ylim = NULL, xlim = NULL, rp = FALSE)
- chart.RiskReward(object, ...)
-
\method{chart.RiskReward}{opt.list} (object, ...,
risk.col = "ES", return.col = "mean", main = "",
ylim = NULL, xlim = NULL, labels.assets = TRUE,
Modified: pkg/PortfolioAnalytics/man/chart.Weights.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Weights.Rd 2013-09-12 01:05:36 UTC (rev 3066)
+++ pkg/PortfolioAnalytics/man/chart.Weights.Rd 2013-09-12 03:58:13 UTC (rev 3067)
@@ -9,6 +9,10 @@
\alias{chart.Weights.optimize.portfolio.RP}
\title{boxplot of the weights of the optimal portfolios}
\usage{
+ chart.Weights(object, neighbors = NULL, ...,
+ main = "Weights", las = 3, xlab = NULL, cex.lab = 1,
+ element.color = "darkgray", cex.axis = 0.8)
+
\method{chart.Weights}{optimize.portfolio.DEoptim} (object, neighbors = NULL, ..., main = "Weights",
las = 3, xlab = NULL, cex.lab = 1,
element.color = "darkgray", cex.axis = 0.8,
@@ -42,10 +46,6 @@
legend.loc = "topright", cex.legend = 0.8,
plot.type = "line")
- chart.Weights(object, neighbors = NULL, ...,
- main = "Weights", las = 3, xlab = NULL, cex.lab = 1,
- element.color = "darkgray", cex.axis = 0.8)
-
\method{chart.Weights}{opt.list} (object,
neighbors = NULL, ..., main = "Weights", las = 3,
xlab = NULL, cex.lab = 1, element.color = "darkgray",
More information about the Returnanalytics-commits
mailing list