[Returnanalytics-commits] r2841 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Aug 20 23:08:55 CEST 2013
Author: rossbennett34
Date: 2013-08-20 23:08:55 +0200 (Tue, 20 Aug 2013)
New Revision: 2841
Added:
pkg/PortfolioAnalytics/man/scatterFUN.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/applyFUN.R
pkg/PortfolioAnalytics/R/chart.RiskReward.R
pkg/PortfolioAnalytics/R/charts.DE.R
pkg/PortfolioAnalytics/R/charts.GenSA.R
pkg/PortfolioAnalytics/R/charts.PSO.R
pkg/PortfolioAnalytics/R/charts.ROI.R
pkg/PortfolioAnalytics/R/charts.RP.R
pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
pkg/PortfolioAnalytics/man/charts.DE.Rd
pkg/PortfolioAnalytics/man/charts.GenSA.Rd
pkg/PortfolioAnalytics/man/charts.ROI.Rd
pkg/PortfolioAnalytics/man/charts.RP.Rd
pkg/PortfolioAnalytics/man/charts.pso.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.DEoptim.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.ROI.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.pso.Rd
pkg/PortfolioAnalytics/man/plot.optimize.portfolio.random.Rd
Log:
Adding functionality to chart.Scatter.* functions to plot a risk-reward scatter of the assets. Added 'Optimal' label to the optimal portfolio in the scatter chart.
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-08-20 21:08:55 UTC (rev 2841)
@@ -96,6 +96,7 @@
export(return_objective)
export(risk_budget_objective)
export(rp_transform)
+export(scatterFUN)
export(set.portfolio.moments_v1)
export(set.portfolio.moments_v2)
export(set.portfolio.moments)
Modified: pkg/PortfolioAnalytics/R/applyFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/applyFUN.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/applyFUN.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -81,4 +81,65 @@
out <- try(do.call(fun, .formals))
}
return(out)
-}
\ No newline at end of file
+}
+
+#' Apply a risk or return function to asset returns
+#'
+#' This function is used to calculate risk or return metrics given a matrix of
+#' asset returns and will be used for a risk-reward scatter plot of the assets
+#'
+#' @param R
+#' @param FUN
+#' @param ... any passthrough arguments to FUN
+#' @author Ross Bennett
+#' @export
+scatterFUN <- function(R, FUN, ...){
+ nargs <- list(...)
+
+ # match the FUN arg to a risk or return function
+ switch(FUN,
+ mean = {
+ return(as.numeric(apply(R, 2, mean)))
+ #fun = match.fun(mean)
+ #nargs$x = R
+ },
+ sd =,
+ StdDev = {
+ fun = match.fun(StdDev)
+ },
+ mVaR =,
+ VaR = {
+ fun = match.fun(VaR)
+ if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
+ if(is.null(nargs$invert)) nargs$invert = FALSE
+ },
+ es =,
+ mES =,
+ CVaR =,
+ cVaR =,
+ ETL =,
+ mETL =,
+ ES = {
+ fun = match.fun(ES)
+ if(is.null(nargs$portfolio_method)) nargs$portfolio_method='single'
+ if(is.null(nargs$invert)) nargs$invert = FALSE
+ },
+{ # see 'S Programming p. 67 for this matching
+ fun <- try(match.fun(FUN))
+}
+ ) # end switch block
+
+ # calculate FUN on R
+ out <- rep(0, ncol(R))
+ .formals <- formals(fun)
+ onames <- names(.formals)
+ for(i in 1:ncol(R)){
+ nargs$R <- R[, i]
+ dargs <- nargs
+ pm <- pmatch(names(dargs), onames, nomatch = 0L)
+ names(dargs[pm > 0L]) <- onames[pm]
+ .formals[pm] <- dargs[pm > 0L]
+ out[i] <- try(do.call(fun, .formals))
+ }
+ return(out)
+}
Modified: pkg/PortfolioAnalytics/R/chart.RiskReward.R
===================================================================
--- pkg/PortfolioAnalytics/R/chart.RiskReward.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/chart.RiskReward.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -8,6 +8,7 @@
#' @param rp TRUE/FALSE. If TRUE, random portfolios are generated by \code{\link{random_portfolios}} to view the feasible space
#' @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 chart.assets TRUE/FALSE. Includes a risk reward scatter of the assets in the chart
#' @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}}
Modified: pkg/PortfolioAnalytics/R/charts.DE.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.DE.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/charts.DE.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -77,7 +77,7 @@
#' @rdname chart.RiskReward
#' @export
-chart.Scatter.DE <- function(object, neighbors = NULL, ..., return.col='mean', risk.col='ES', element.color = "darkgray", cex.axis=0.8){
+chart.Scatter.DE <- function(object, neighbors = NULL, ..., return.col='mean', risk.col='ES', chart.assets=FALSE, element.color = "darkgray", cex.axis=0.8){
# more or less specific to the output of the DEoptim portfolio code with constraints
# will work to a point with other functions, such as optimize.porfolio.parallel
# there's still a lot to do to improve this.
@@ -138,8 +138,25 @@
}
# print(colnames(head(xtract)))
+ if(chart.assets){
+ # Include risk reward scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN=return.col, ...=...)
+ asset_risk <- scatterFUN(R=R, FUN=risk.col, ...=...)
+ rnames <- colnames(R)
+ } else {
+ asset_ret <- NULL
+ asset_risk <- NULL
+ }
+
+ # plot the portfolios from DEoptim_objective_results
plot(xtract[,risk.column],xtract[,return.column], xlab=risk.col, ylab=return.col, col="darkgray", axes=FALSE, ...)
+ # plot the risk-reward scatter of the assets
+ if(chart.assets){
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=colnames(R), pos=4, cex=0.8)
+ }
+
if(!is.null(neighbors)){
if(is.vector(neighbors)){
if(length(neighbors)==1){
@@ -188,7 +205,7 @@
w = w.traj[i,]
x = unlist(constrained_objective(w=w, R=R, portfolio=portfolio, trace=TRUE))
- names(x)<-name.replace(names(x))
+ names(x)<-PortfolioAnalytics:::name.replace(names(x))
if(is.null(trajnames)) trajnames<-names(x)
if(is.null(rsc)){
rtc = pmatch(return.col,trajnames)
@@ -224,7 +241,7 @@
result.slot<-'objective_measures'
}
objcols<-unlist(object[[result.slot]])
- names(objcols)<-name.replace(names(objcols))
+ names(objcols)<-PortfolioAnalytics:::name.replace(names(objcols))
return.column = pmatch(return.col,names(objcols))
if(is.na(return.column)) {
return.col = paste(return.col,return.col,sep='.')
@@ -245,8 +262,10 @@
ret <- as.numeric(applyFUN(R=R, weights=opt_weights, FUN=return.col))
risk <- as.numeric(applyFUN(R=R, weights=opt_weights, FUN=risk.col))
points(risk, ret, col="blue", pch=16) #optimal
+ text(x=risk, y=ret, labels="Optimal",col="blue", pos=4, cex=0.8)
} else {
points(objcols[risk.column], objcols[return.column], col="blue", pch=16) # optimal
+ text(x=objcols[risk.column], y=objcols[return.column], 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)
@@ -278,13 +297,13 @@
#' \code{\link{optimize.portfolio}}
#' \code{\link{extractStats}}
#' @export
-charts.DE <- function(DE, risk.col, return.col, neighbors=NULL, main="DEoptim.Portfolios", ...){
+charts.DE <- function(DE, risk.col, return.col, chart.assets, neighbors=NULL, main="DEoptim.Portfolios", ...){
# Specific to the output of the random portfolio code with constraints
# @TODO: check that DE is of the correct class
op <- par(no.readonly=TRUE)
layout(matrix(c(1,2)),height=c(2,1.5),width=1)
par(mar=c(4,4,4,2))
- chart.Scatter.DE(object=DE, risk.col=risk.col, return.col=return.col, neighbors=neighbors, main=main, ...)
+ chart.Scatter.DE(object=DE, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, neighbors=neighbors, main=main, ...)
par(mar=c(2,4,0,2))
chart.Weights.DE(object=DE, main="", neighbors=neighbors, ...)
par(op)
@@ -311,6 +330,6 @@
#' @param neighbors set of 'neighbor portfolios to overplot
#' @param main an overall title for the plot: see \code{\link{title}}
#' @export
-plot.optimize.portfolio.DEoptim <- function(x, ..., return.col='mean', risk.col='ES', neighbors=NULL, main='optimized portfolio plot') {
- charts.DE(DE=x, risk.col=risk.col, return.col=return.col, neighbors=neighbors, main=main, ...)
+plot.optimize.portfolio.DEoptim <- function(x, ..., return.col='mean', risk.col='ES', chart.assets=FALSE, neighbors=NULL, main='optimized portfolio plot') {
+ charts.DE(DE=x, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, neighbors=neighbors, main=main, ...)
}
Modified: pkg/PortfolioAnalytics/R/charts.GenSA.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.GenSA.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/charts.GenSA.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -65,7 +65,7 @@
#' @rdname chart.RiskReward
#' @export
-chart.Scatter.GenSA <- function(object, neighbors=NULL, ..., rp=FALSE, return.col="mean", risk.col="ES", element.color="darkgray", cex.axis=0.8){
+chart.Scatter.GenSA <- function(object, neighbors=NULL, ..., rp=FALSE, return.col="mean", risk.col="ES", chart.assets=FALSE, element.color="darkgray", cex.axis=0.8){
if(!inherits(object, "optimize.portfolio.GenSA")) stop("object must be of class 'optimize.portfolio.GenSA'")
@@ -75,6 +75,8 @@
permutations <- match.call(expand.dots=TRUE)$permutations
if(is.null(permutations)) permutations <- 2000
rp <- random_portfolios(portfolio=object$portfolio, permutations=permutations)
+ } else {
+ rp = NULL
}
# Get the optimal weights from the output of optimize.portfolio
@@ -86,8 +88,31 @@
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, ...)
+ if(chart.assets){
+ # Include risk reward scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN=return.col, ...=...)
+ asset_risk <- scatterFUN(R=R, FUN=risk.col, ...=...)
+ rnames <- colnames(R)
+ } else {
+ asset_ret <- NULL
+ asset_risk <- NULL
+ }
+
+ # get limits for x and y axis
+ ylim <- range(returnpoints, asset_ret)
+ xlim <- range(riskpoints, asset_risk)
+
+ # Plot the portfolios
+ plot(x=riskpoints, y=returnpoints, xlab=risk.col, ylab=return.col, col="darkgray", ylim=ylim, xlim=xlim, axes=FALSE, ...)
points(x=riskpoints[1], y=returnpoints[1], col="blue", pch=16) # optimal
+ text(x=riskpoints[1], y=returnpoints[1], labels="Optimal",col="blue", pos=4, cex=0.8)
+
+ # plot the risk-reward scatter of the assets
+ if(chart.assets){
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=colnames(R), 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)
@@ -114,13 +139,12 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-charts.GenSA <- function(GenSA, rp=FALSE, return.col="mean", risk.col="StdDev",
- cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
+charts.GenSA <- function(GenSA, rp=FALSE, return.col="mean", risk.col="ES", chart.assets=FALSE, cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
# Specific to the output of the optimize_method=GenSA
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(object=GenSA, rp=rp, return.col=return.col, risk.col=risk.col, element.color=element.color, cex.axis=cex.axis, main=main, ...=...)
+ chart.Scatter.GenSA(object=GenSA, rp=rp, return.col=return.col, risk.col=risk.col, chart.assets=chart.assets, element.color=element.color, cex.axis=cex.axis, main=main, ...=...)
par(mar=c(2,4,0,2))
chart.Weights.GenSA(object=GenSA, neighbors=neighbors, las=3, xlab=NULL, cex.lab=1, element.color=element.color, cex.axis=cex.axis, ...=..., main="")
par(op)
@@ -143,6 +167,6 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-plot.optimize.portfolio.GenSA <- function(GenSA, rp=FALSE, return.col="mean", risk.col="StdDev", cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
- charts.GenSA(GenSA=GenSA, rp=rp, return.col=return.col, risk.col=risk.col, cex.axis=cex.axis, element.color=element.color, neighbors=neighbors, main=main, ...=...)
+plot.optimize.portfolio.GenSA <- function(GenSA, rp=FALSE, return.col="mean", risk.col="ES", chart.assets=FALSE, cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="GenSA.Portfolios", ...){
+ charts.GenSA(GenSA=GenSA, rp=rp, return.col=return.col, risk.col=risk.col, chart.assets=chart.assets, cex.axis=cex.axis, element.color=element.color, neighbors=neighbors, main=main, ...=...)
}
Modified: pkg/PortfolioAnalytics/R/charts.PSO.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.PSO.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/charts.PSO.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -65,7 +65,7 @@
#' @rdname chart.RiskReward
#' @export
-chart.Scatter.pso <- function(object, neighbors=NULL, ..., return.col="mean", risk.col="ES", element.color = "darkgray", cex.axis=0.8){
+chart.Scatter.pso <- function(object, neighbors=NULL, ..., return.col="mean", risk.col="ES", chart.assets=FALSE, element.color = "darkgray", cex.axis=0.8){
if(!inherits(object, "optimize.portfolio.pso")) stop("object must be of class 'optimize.portfolio.pso'")
R <- object$R
# Object with the "out" value in the first column and the normalized weights
@@ -78,8 +78,31 @@
returnpoints <- applyFUN(R=R, weights=wts, FUN=return.col, ...=...)
riskpoints <- applyFUN(R=R, weights=wts, FUN=risk.col, ...=...)
- plot(x=riskpoints, y=returnpoints, xlab=risk.col, ylab=return.col, col="darkgray", axes=FALSE, ...)
+ if(chart.assets){
+ # Include risk reward scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN=return.col, ...=...)
+ asset_risk <- scatterFUN(R=R, FUN=risk.col, ...=...)
+ rnames <- colnames(R)
+ } else {
+ asset_ret <- NULL
+ asset_risk <- NULL
+ }
+
+ # get limits for x and y axis
+ ylim <- range(returnpoints, asset_ret)
+ xlim <- range(riskpoints, asset_risk)
+
+ # plot the portfolios
+ plot(x=riskpoints, y=returnpoints, xlab=risk.col, ylab=return.col, xlim=xlim, ylim=ylim, col="darkgray", axes=FALSE, ...)
points(x=riskpoints[1], y=returnpoints[1], col="blue", pch=16) # optimal
+ text(x=riskpoints[1], y=returnpoints[1], labels="Optimal",col="blue", pos=4, cex=0.8)
+
+ # plot the risk-reward scatter of the assets
+ if(chart.assets){
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=colnames(R), 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)
@@ -105,13 +128,12 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-charts.pso <- function(pso, return.col="mean", risk.col="StdDev",
- cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="PSO.Portfolios", ...){
+charts.pso <- function(pso, return.col="mean", risk.col="ES", chart.assets=FALSE, cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="PSO.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.pso(object=pso, return.col=return.col, risk.col=risk.col, element.color=element.color, cex.axis=cex.axis, main=main, ...=...)
+ chart.Scatter.pso(object=pso, return.col=return.col, risk.col=risk.col, chart.assets=chart.assets, element.color=element.color, cex.axis=cex.axis, main=main, ...=...)
par(mar=c(2,4,0,2))
chart.Weights.pso(object=pso, neighbors=neighbors, las=3, xlab=NULL, cex.lab=1, element.color=element.color, cex.axis=cex.axis, ...=..., main="")
par(op)
@@ -133,7 +155,6 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-plot.optimize.portfolio.pso <- function(pso, return.col="mean", risk.col="StdDev",
- cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="PSO.Portfolios", ...){
- charts.pso(pso=pso, return.col=return.col, risk.col=risk.col, cex.axis=cex.axis, element.color=element.color, neighbors=neighbors, main=main, ...=...)
+plot.optimize.portfolio.pso <- function(pso, return.col="mean", risk.col="ES", chart.assets=FALSE, cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="PSO.Portfolios", ...){
+ charts.pso(pso=pso, return.col=return.col, risk.col=risk.col, chart.assets=FALSE, cex.axis=cex.axis, element.color=element.color, neighbors=neighbors, main=main, ...=...)
}
Modified: pkg/PortfolioAnalytics/R/charts.ROI.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.ROI.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/charts.ROI.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -65,7 +65,7 @@
#' @rdname chart.RiskReward
#' @export
-chart.Scatter.ROI <- function(object, neighbors=NULL, ..., rp=FALSE, return.col="mean", risk.col="ES", element.color = "darkgray", cex.axis=0.8){
+chart.Scatter.ROI <- function(object, neighbors=NULL, ..., rp=FALSE, return.col="mean", risk.col="ES", chart.assets=FALSE, element.color = "darkgray", cex.axis=0.8){
if(!inherits(object, "optimize.portfolio.ROI")) stop("object must be of class 'optimize.portfolio.ROI'")
@@ -75,6 +75,8 @@
permutations <- match.call(expand.dots=TRUE)$permutations
if(is.null(permutations)) permutations <- 2000
rp <- random_portfolios(portfolio=object$portfolio, permutations=permutations)
+ } else {
+ rp = NULL
}
# Get the optimal weights from the output of optimize.portfolio
@@ -86,8 +88,32 @@
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, ...)
+ if(chart.assets){
+ # Include risk reward scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN=return.col, ...=...)
+ asset_risk <- scatterFUN(R=R, FUN=risk.col, ...=...)
+ rnames <- colnames(R)
+ } else {
+ asset_ret <- NULL
+ asset_risk <- NULL
+ }
+
+ # get limits for x and y axis
+ ylim <- range(returnpoints, asset_ret)
+ xlim <- range(riskpoints, asset_risk)
+
+ # Plot the portfolios
+ plot(x=riskpoints, y=returnpoints, xlab=risk.col, ylab=return.col, col="darkgray", ylim=ylim, xlim=xlim, axes=FALSE, ...)
+ # Plot the optimal portfolio
points(x=riskpoints[1], y=returnpoints[1], col="blue", pch=16) # optimal
+ text(x=riskpoints[1], y=returnpoints[1], labels="Optimal",col="blue", pos=4, cex=0.8)
+
+ # plot the risk-reward scatter of the assets
+ if(chart.assets){
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=colnames(R), 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)
@@ -117,14 +143,13 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-charts.ROI <- function(ROI, rp=FALSE, risk.col="StdDev", return.col="mean",
- cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="ROI.Portfolios", ...){
+charts.ROI <- function(ROI, rp=FALSE, risk.col="StdDev", return.col="mean", chart.assets=FALSE, cex.axis=0.8, element.color="darkgray", neighbors=NULL, main="ROI.Portfolios", ...){
# Specific to the output of the optimize_method=ROI
R <- ROI$R
op <- par(no.readonly=TRUE)
layout(matrix(c(1,2)),height=c(2,1.5),width=1)
par(mar=c(4,4,4,2))
- chart.Scatter.ROI(ROI, rp=rp, return.col=return.col, risk.col=risk.col, ..., element.color=element.color, cex.axis=cex.axis, main=main)
+ chart.Scatter.ROI(ROI, rp=rp, return.col=return.col, risk.col=risk.col, chart.assets=chart.assets, ..., element.color=element.color, cex.axis=cex.axis, main=main)
par(mar=c(2,4,0,2))
chart.Weights.ROI(ROI, neighbors=neighbors, ..., main="", las=3, xlab=NULL, cex.lab=1, element.color=element.color, cex.axis=cex.axis)
par(op)
@@ -150,6 +175,6 @@
#' @seealso \code{\link{optimize.portfolio}}
#' @author Ross Bennett
#' @export
-plot.optimize.portfolio.ROI <- function(ROI, rp=FALSE, risk.col="StdDev", return.col="mean", element.color="darkgray", neighbors=NULL, main="ROI.Portfolios", ...){
- charts.ROI(ROI=ROI, rp=rp, risk.col=risk.col, return.col=return.col, main=main, ...)
+plot.optimize.portfolio.ROI <- function(ROI, rp=FALSE, risk.col="ES", return.col="mean", chart.assets=chart.assets, element.color="darkgray", neighbors=NULL, main="ROI.Portfolios", ...){
+ charts.ROI(ROI=ROI, rp=rp, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, main=main, ...)
}
Modified: pkg/PortfolioAnalytics/R/charts.RP.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.RP.R 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/R/charts.RP.R 2013-08-20 21:08:55 UTC (rev 2841)
@@ -81,7 +81,7 @@
#' @rdname chart.RiskReward
#' @export
-chart.Scatter.RP <- function(object, neighbors = NULL, ..., return.col='mean', risk.col='ES', element.color = "darkgray", cex.axis=0.8){
+chart.Scatter.RP <- function(object, neighbors = NULL, ..., return.col='mean', risk.col='ES', chart.assets=FALSE, element.color = "darkgray", cex.axis=0.8){
# more or less specific to the output of the random portfolio code with constraints
# will work to a point with other functions, such as optimize.porfolio.parallel
# there's still a lot to do to improve this.
@@ -141,13 +141,30 @@
}
# print(colnames(head(xtract)))
+ if(chart.assets){
+ # Include risk reward scatter of asset returns
+ asset_ret <- scatterFUN(R=R, FUN=return.col, ...=...)
+ asset_risk <- scatterFUN(R=R, FUN=risk.col, ...=...)
+ rnames <- colnames(R)
+ } else {
+ asset_ret <- NULL
+ asset_risk <- NULL
+ }
+
+ # plot the random portfolios
plot(xtract[,risk.column],xtract[,return.column], xlab=risk.col, ylab=return.col, col="darkgray", axes=FALSE, ...)
+ # plot the risk-reward scatter of the assets
+ if(chart.assets){
+ points(x=asset_risk, y=asset_ret)
+ text(x=asset_risk, y=asset_ret, labels=colnames(R), pos=4, cex=0.8)
+ }
+
if(!is.null(neighbors)){
if(is.vector(neighbors)){
if(length(neighbors)==1){
# overplot nearby portfolios defined by 'out'
- orderx = order(xtract[,"out"]) #TODO this won't work if the objective is anything othchart.Scatter.er than mean
+ orderx = order(xtract[,"out"]) #TODO this won't work if the objective is anything other than mean
subsetx = head(xtract[orderx,], n=neighbors)
} else{
# assume we have a vector of portfolio numbers
@@ -205,8 +222,10 @@
ret <- as.numeric(applyFUN(R=R, weights=opt_weights, FUN=return.col))
risk <- as.numeric(applyFUN(R=R, weights=opt_weights, FUN=risk.col))
points(risk, ret, col="blue", pch=16) #optimal
+ text(x=risk, y=ret, labels="Optimal",col="blue", pos=4, cex=0.8)
} else {
points(objcols[risk.column], objcols[return.column], col="blue", pch=16) # optimal
+ text(x=objcols[risk.column], y=objcols[return.column], 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)
@@ -238,13 +257,13 @@
#' \code{\link{optimize.portfolio}}
#' \code{\link{extractStats}}
#' @export
-charts.RP <- function(RP, risk.col, return.col, neighbors=NULL, main="Random.Portfolios", ...){
+charts.RP <- function(RP, risk.col, return.col, chart.assets=FALSE, neighbors=NULL, main="Random.Portfolios", ...){
# Specific to the output of the random portfolio code with constraints
# @TODO: check that RP is of the correct class
op <- par(no.readonly=TRUE)
layout(matrix(c(1,2)),height=c(2,1.5),width=1)
par(mar=c(4,4,4,2))
- chart.Scatter.RP(object=RP, risk.col=risk.col, return.col=return.col, neighbors=neighbors, main=main, ...)
+ chart.Scatter.RP(object=RP, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, neighbors=neighbors, main=main, ...)
par(mar=c(2,4,0,2))
chart.Weights.RP(object=RP, main="", neighbors=neighbors, ...)
par(op)
@@ -271,8 +290,8 @@
#' @param neighbors set of 'neighbor portfolios to overplot
#' @param main an overall title for the plot: see \code{\link{title}}
#' @export
-plot.optimize.portfolio.random <- function(x, ..., R=NULL, return.col='mean', risk.col='ES', neighbors=NULL, main='optimized portfolio plot') {
- charts.RP(RP=x, risk.col=risk.col, return.col=return.col, neighbors=neighbors, main=main, ...)
+plot.optimize.portfolio.random <- function(x, ..., R=NULL, return.col='mean', risk.col='ES', chart.assets=FALSE, neighbors=NULL, main='optimized portfolio plot') {
+ charts.RP(RP=x, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, neighbors=neighbors, main=main, ...)
}
#' plot method for optimize.portfolio output
@@ -296,6 +315,6 @@
#' @param neighbors set of 'neighbor portfolios to overplot
#' @param main an overall title for the plot: see \code{\link{title}}
#' @export
-plot.optimize.portfolio <- function(x, ..., return.col='mean', risk.col='ES', neighbors=NULL, main='optimized portfolio plot') {
- charts.RP(RP=x, risk.col=risk.col, return.col=return.col, neighbors=neighbors, main=main, ...)
+plot.optimize.portfolio <- function(x, ..., return.col='mean', risk.col='ES', chart.assets=FALSE, neighbors=NULL, main='optimized portfolio plot') {
+ charts.RP(RP=x, risk.col=risk.col, return.col=return.col, chart.assets=chart.assets, neighbors=neighbors, main=main, ...)
}
\ No newline at end of file
Modified: pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskReward.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/chart.RiskReward.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -14,48 +14,53 @@
\usage{
chart.Scatter.DE(object, neighbors = NULL, ...,
return.col = "mean", risk.col = "ES",
- element.color = "darkgray", cex.axis = 0.8)
+ chart.assets = FALSE, element.color = "darkgray",
+ cex.axis = 0.8)
chart.RiskReward.optimize.portfolio.DEoptim(object,
neighbors = NULL, ..., return.col = "mean",
- risk.col = "ES", element.color = "darkgray",
- cex.axis = 0.8)
+ risk.col = "ES", chart.assets = FALSE,
+ element.color = "darkgray", cex.axis = 0.8)
chart.Scatter.RP(object, neighbors = NULL, ...,
return.col = "mean", risk.col = "ES",
- element.color = "darkgray", cex.axis = 0.8)
+ chart.assets = FALSE, element.color = "darkgray",
+ cex.axis = 0.8)
chart.RiskReward.optimize.portfolio.random(object,
neighbors = NULL, ..., return.col = "mean",
- risk.col = "ES", element.color = "darkgray",
- cex.axis = 0.8)
+ risk.col = "ES", chart.assets = FALSE,
+ element.color = "darkgray", cex.axis = 0.8)
chart.Scatter.ROI(object, neighbors = NULL, ...,
rp = FALSE, return.col = "mean", risk.col = "ES",
- element.color = "darkgray", cex.axis = 0.8)
+ chart.assets = FALSE, element.color = "darkgray",
+ cex.axis = 0.8)
chart.RiskReward.optimize.portfolio.ROI(object,
neighbors = NULL, ..., rp = FALSE, return.col = "mean",
- risk.col = "ES", element.color = "darkgray",
- cex.axis = 0.8)
+ risk.col = "ES", chart.assets = FALSE,
+ element.color = "darkgray", cex.axis = 0.8)
chart.Scatter.pso(object, neighbors = NULL, ...,
return.col = "mean", risk.col = "ES",
- element.color = "darkgray", cex.axis = 0.8)
+ chart.assets = FALSE, element.color = "darkgray",
+ cex.axis = 0.8)
chart.RiskReward.optimize.portfolio.pso(object,
neighbors = NULL, ..., return.col = "mean",
- risk.col = "ES", element.color = "darkgray",
- cex.axis = 0.8)
+ risk.col = "ES", chart.assets = FALSE,
+ element.color = "darkgray", cex.axis = 0.8)
chart.Scatter.GenSA(object, neighbors = NULL, ...,
rp = FALSE, return.col = "mean", risk.col = "ES",
- element.color = "darkgray", cex.axis = 0.8)
+ chart.assets = FALSE, element.color = "darkgray",
+ cex.axis = 0.8)
chart.RiskReward.optimize.portfolio.GenSA(object,
neighbors = NULL, ..., rp = FALSE, return.col = "mean",
- risk.col = "ES", element.color = "darkgray",
- cex.axis = 0.8)
+ risk.col = "ES", chart.assets = FALSE,
+ element.color = "darkgray", cex.axis = 0.8)
chart.RiskReward(object, neighbors, ..., rp = FALSE,
return.col = "mean", risk.col = "ES",
@@ -80,6 +85,9 @@
\item{risk.col}{string matching the objective of a 'risk'
objective, on horizontal axis}
+ \item{chart.assets}{TRUE/FALSE. Includes a risk reward
+ scatter of the assets in the chart}
+
\item{cex.axis}{The magnification to be used for axis
annotation relative to the current setting of \code{cex}}
Modified: pkg/PortfolioAnalytics/man/charts.DE.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.DE.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/charts.DE.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -2,8 +2,8 @@
\alias{charts.DE}
\title{scatter and weights chart for random portfolios}
\usage{
- charts.DE(DE, risk.col, return.col, neighbors = NULL,
- main = "DEoptim.Portfolios", ...)
+ charts.DE(DE, risk.col, return.col, chart.assets,
+ neighbors = NULL, main = "DEoptim.Portfolios", ...)
}
\arguments{
\item{DE}{set of random portfolios created by
Modified: pkg/PortfolioAnalytics/man/charts.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.GenSA.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/charts.GenSA.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -2,8 +2,8 @@
\alias{charts.GenSA}
\title{scatter and weights chart for portfolios}
\usage{
- charts.GenSA(GenSA, rp = NULL, return.col = "mean",
- risk.col = "StdDev", cex.axis = 0.8,
+ charts.GenSA(GenSA, rp = FALSE, return.col = "mean",
+ risk.col = "ES", chart.assets = FALSE, cex.axis = 0.8,
element.color = "darkgray", neighbors = NULL,
main = "GenSA.Portfolios", ...)
}
Modified: pkg/PortfolioAnalytics/man/charts.ROI.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.ROI.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/charts.ROI.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -2,10 +2,10 @@
\alias{charts.ROI}
\title{scatter and weights chart for portfolios}
\usage{
- charts.ROI(ROI, rp = NULL, risk.col = "StdDev",
- return.col = "mean", cex.axis = 0.8,
- element.color = "darkgray", neighbors = NULL,
- main = "ROI.Portfolios", ...)
+ charts.ROI(ROI, rp = FALSE, risk.col = "StdDev",
+ return.col = "mean", chart.assets = FALSE,
+ cex.axis = 0.8, element.color = "darkgray",
+ neighbors = NULL, main = "ROI.Portfolios", ...)
}
\arguments{
\item{ROI}{object created by
Modified: pkg/PortfolioAnalytics/man/charts.RP.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.RP.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/charts.RP.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -2,7 +2,7 @@
\alias{charts.RP}
\title{scatter and weights chart for random portfolios}
\usage{
- charts.RP(RP, R = NULL, risk.col, return.col,
+ charts.RP(RP, risk.col, return.col, chart.assets = FALSE,
neighbors = NULL, main = "Random.Portfolios", ...)
}
\arguments{
Modified: pkg/PortfolioAnalytics/man/charts.pso.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/charts.pso.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/charts.pso.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -2,9 +2,10 @@
\alias{charts.pso}
\title{scatter and weights chart for portfolios}
\usage{
- charts.pso(pso, return.col = "mean", risk.col = "StdDev",
- cex.axis = 0.8, element.color = "darkgray",
- neighbors = NULL, main = "PSO.Portfolios", ...)
+ charts.pso(pso, return.col = "mean", risk.col = "ES",
+ chart.assets = FALSE, cex.axis = 0.8,
+ element.color = "darkgray", neighbors = NULL,
+ main = "PSO.Portfolios", ...)
}
\arguments{
\item{pso}{object created by
Modified: pkg/PortfolioAnalytics/man/plot.optimize.portfolio.DEoptim.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/plot.optimize.portfolio.DEoptim.Rd 2013-08-20 19:32:30 UTC (rev 2840)
+++ pkg/PortfolioAnalytics/man/plot.optimize.portfolio.DEoptim.Rd 2013-08-20 21:08:55 UTC (rev 2841)
@@ -3,7 +3,8 @@
\title{plot method for optimize.portfolio.DEoptim output}
\usage{
plot.optimize.portfolio.DEoptim(x, ...,
- return.col = "mean", risk.col = "ES", neighbors = NULL,
+ return.col = "mean", risk.col = "ES",
+ chart.assets = FALSE, neighbors = NULL,
main = "optimized portfolio plot")
}
\arguments{
Modified: pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/plot.optimize.portfolio.GenSA.Rd 2013-08-20 19:32:30 UTC (rev 2840)
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 2841
More information about the Returnanalytics-commits
mailing list