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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Oct 18 01:04:25 CEST 2013


Author: rossbennett34
Date: 2013-10-18 01:04:24 +0200 (Fri, 18 Oct 2013)
New Revision: 3226

Added:
   pkg/PortfolioAnalytics/man/inverse.volatility.weight.Rd
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/extractstats.R
   pkg/PortfolioAnalytics/man/extractStats.Rd
Log:
Adding extractStats and extractObjectiveMeasures methods for optimize.portfolio.rebalancing objects.

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2013-10-17 23:02:56 UTC (rev 3225)
+++ pkg/PortfolioAnalytics/NAMESPACE	2013-10-17 23:04:24 UTC (rev 3226)
@@ -95,6 +95,7 @@
 S3method(chart.Weights.EF,efficient.frontier)
 S3method(chart.Weights.EF,optimize.portfolio)
 S3method(extractObjectiveMeasures,opt.list)
+S3method(extractObjectiveMeasures,optimize.portfolio.rebalancing)
 S3method(extractObjectiveMeasures,optimize.portfolio)
 S3method(extractStats,optimize.portfolio.DEoptim)
 S3method(extractStats,optimize.portfolio.eqwt)
@@ -103,6 +104,7 @@
 S3method(extractStats,optimize.portfolio.parallel)
 S3method(extractStats,optimize.portfolio.pso)
 S3method(extractStats,optimize.portfolio.random)
+S3method(extractStats,optimize.portfolio.rebalancing)
 S3method(extractStats,optimize.portfolio.ROI)
 S3method(extractWeights,opt.list)
 S3method(extractWeights,optimize.portfolio.rebalancing)

Modified: pkg/PortfolioAnalytics/R/extractstats.R
===================================================================
--- pkg/PortfolioAnalytics/R/extractstats.R	2013-10-17 23:02:56 UTC (rev 3225)
+++ pkg/PortfolioAnalytics/R/extractstats.R	2013-10-17 23:04:24 UTC (rev 3226)
@@ -15,15 +15,32 @@
 #' This function will dispatch to the appropriate class handler based on the
 #' input class of the optimize.portfolio output object.
 #' 
+#' For \code{optimize.portfolio} objects:
+#' 
+#' In general, \code{extractStats} will extract the values objective measures 
+#' and weights at each iteration of a set of weights. This is the case for the
+#' DEoptim, random portfolios, and pso solvers that return trace information. 
+#' Note that \code{trace=TRUE} must be specified in \code{optimize.portfolio} 
+#' to return the trace information.
+#' 
 #' For \code{optimize.portfolio.pso} objects, this function will extract the 
 #' weights (swarm positions) from the PSO output and the out values
 #' (swarm fitness 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.
+#' to calculate the objective measures on the transformed weights.
 #' 
+#' For \code{optimize.portfolio.rebalancing} objects:
+#' 
+#' The \code{extractStats} function will return a list of the objective measures 
+#' and weights at each rebalance date for \code{optimize.portfolio.rebalancing}
+#' objects. The objective measures and weights of each iteration or permutation 
+#' will be returned if the optimization was done with DEoptim, random portfolios, 
+#' or pso. This could potentially result in a very large list object where each 
+#' list element has thousands of rows of at each rebalance period.
+#' 
 #' The output from the GenSA solver does not store weights evaluated at each iteration
 #' The GenSA output for trace.mat contains nb.steps, temperature, function.value, and current.minimum
-#'  
+#'
 #' @param object list returned by optimize.portfolio
 #' @param prefix prefix to add to output row names
 #' @param ... any other passthru parameters
@@ -327,6 +344,14 @@
   return(result)
 }
 
+#' @method extractStats optimize.portfolio.rebalancing
+#' @S3method extractStats optimize.portfolio.rebalancing
+#' @export 
+extractStats.optimize.portfolio.rebalancing <- function(object, prefix=NULL, ...) {
+  if(!inherits(object, "optimize.portfolio.rebalancing")) stop("object must be of class optimize.portfolio.rebalancing")
+  return(lapply(object, extractStats, ...))
+}
+
 #' Extract the objective measures
 #' 
 #' This function will extract the objective measures from the optimal portfolio
@@ -350,6 +375,26 @@
   return(out)
 }
 
+#' @method extractObjectiveMeasures optimize.portfolio.rebalancing
+#' @S3method extractObjectiveMeasures optimize.portfolio.rebalancing
+extractObjectiveMeasures.optimize.portfolio.rebalancing <- function(object){
+  if(!inherits(object, "optimize.portfolio.rebalancing")) stop("object must be of class 'optimize.portfolio.rebalancing'")
+  
+  num.columns <- length(unlist(extractObjectiveMeasures(object[[1]])))
+  num.rows <- length(object)
+  
+  result <- matrix(nrow=num.rows, ncol=num.columns)
+  
+  for(i in 1:num.rows){
+    result[i,] <- unlist(extractObjectiveMeasures(object[[i]]))
+  }
+  
+  colnames(result) <- name.replace(names(unlist(extractObjectiveMeasures(object[[1]]))))
+  rownames(result) <- names(object)
+  result <- as.xts(result)
+  return(result)
+}
+
 #' Extract the group and/or category weights
 #' 
 #' This function extracts the weights by group and/or category from an object

Modified: pkg/PortfolioAnalytics/man/extractStats.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extractStats.Rd	2013-10-17 23:02:56 UTC (rev 3225)
+++ pkg/PortfolioAnalytics/man/extractStats.Rd	2013-10-17 23:04:24 UTC (rev 3226)
@@ -23,13 +23,36 @@
   optimize.portfolio output object.
 }
 \details{
+  For \code{optimize.portfolio} objects:
+
+  In general, \code{extractStats} will extract the values
+  objective measures and weights at each iteration of a set
+  of weights. This is the case for the DEoptim, random
+  portfolios, and pso solvers that return trace
+  information. Note that \code{trace=TRUE} must be
+  specified in \code{optimize.portfolio} to return the
+  trace information.
+
   For \code{optimize.portfolio.pso} objects, this function
   will extract the weights (swarm positions) from the PSO
   output and the out values (swarm fitness 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.
+  calculate the objective measures on the transformed
+  weights.
 
+  For \code{optimize.portfolio.rebalancing} objects:
+
+  The \code{extractStats} function will return a list of
+  the objective measures and weights at each rebalance date
+  for \code{optimize.portfolio.rebalancing} objects. The
+  objective measures and weights of each iteration or
+  permutation will be returned if the optimization was done
+  with DEoptim, random portfolios, or pso. This could
+  potentially result in a very large list object where each
+  list element has thousands of rows of at each rebalance
+  period.
+
   The output from the GenSA solver does not store weights
   evaluated at each iteration The GenSA output for
   trace.mat contains nb.steps, temperature, function.value,

Added: pkg/PortfolioAnalytics/man/inverse.volatility.weight.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/inverse.volatility.weight.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/inverse.volatility.weight.Rd	2013-10-17 23:04:24 UTC (rev 3226)
@@ -0,0 +1,35 @@
+\name{inverse.volatility.weight}
+\alias{inverse.volatility.weight}
+\title{Create an inverse volatility weighted portfolio}
+\usage{
+  inverse.volatility.weight(R, portfolio, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{portfolio}{an object of type "portfolio" specifying
+  the constraints and objectives for the optimization}
+
+  \item{\dots}{any other passthru parameters to
+  \code{constrained_objective}}
+}
+\value{
+  a list containing the returns, weights, objective
+  measures, call, and portfolio object
+}
+\description{
+  This function calculates objective measures for an equal
+  weight portfolio.
+}
+\details{
+  This function is simply a wrapper around
+  \code{\link{constrained_objective}} to calculate the
+  objective measures in the given \code{portfolio} object
+  of an inverse volatility weight portfolio. The portfolio
+  object should include all objectives to be calculated.
+}
+\author{
+  Peter Carl
+}
+



More information about the Returnanalytics-commits mailing list