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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 25 02:27:59 CEST 2013


Author: rossbennett34
Date: 2013-07-25 02:27:59 +0200 (Thu, 25 Jul 2013)
New Revision: 2640

Added:
   pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing_v1.Rd
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
   pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing.Rd
Log:
Changing old version of optimize.portfolio.rebalancing to _v1 and adding optimize.portfolio.rebalancing to accept new portfolio interface

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2013-07-24 14:06:04 UTC (rev 2639)
+++ pkg/PortfolioAnalytics/NAMESPACE	2013-07-25 00:27:59 UTC (rev 2640)
@@ -40,6 +40,7 @@
 export(optimize.portfolio_v1)
 export(optimize.portfolio_v2)
 export(optimize.portfolio.parallel)
+export(optimize.portfolio.rebalancing_v1)
 export(optimize.portfolio.rebalancing)
 export(optimize.portfolio)
 export(plot.optimize.portfolio.DEoptim)

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-24 14:06:04 UTC (rev 2639)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-25 00:27:59 UTC (rev 2640)
@@ -34,7 +34,6 @@
 #' @author Ross Bennett
 #' @export
 fn_map <- function(weights, portfolio, relax=FALSE, ...){
-  
   if(!is.portfolio(portfolio)) stop("portfolio passed in is not of class 'portfolio'")
   
   nassets <- length(portfolio$assets)

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-07-24 14:06:04 UTC (rev 2639)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-07-25 00:27:59 UTC (rev 2640)
@@ -686,7 +686,7 @@
     weights <- as.vector(minw$optim$bestmem)
     print(weights)
     # is it necessary to normalize the weights here?
-    weights <- normalize_weights(weights)
+    # weights <- normalize_weights(weights)
     names(weights) <- colnames(R)
     
     out <- list(weights=weights, objective_measures=constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures, out=minw$optim$bestval, call=call)
@@ -899,7 +899,7 @@
 #' @export
 optimize.portfolio <- optimize.portfolio_v2
 
-#' portfolio optimization with support for rebalancing or rolling periods
+#' version 1 portfolio optimization with support for rebalancing or rolling periods
 #' 
 #' This function may eventually be wrapped into optimize.portfolio
 #' 
@@ -920,7 +920,7 @@
 #' @return a list containing the optimal weights, some summary statistics, the function call, and optionally trace information 
 #' @author Kris Boudt, Peter Carl, Brian G. Peterson
 #' @export
-optimize.portfolio.rebalancing <- function(R,constraints,optimize_method=c("DEoptim","random","ROI"), search_size=20000, trace=FALSE, ..., rp=NULL, rebalance_on=NULL, training_period=NULL, trailing_periods=NULL)
+optimize.portfolio.rebalancing_v1 <- function(R,constraints,optimize_method=c("DEoptim","random","ROI"), search_size=20000, trace=FALSE, ..., rp=NULL, rebalance_on=NULL, training_period=NULL, trailing_periods=NULL)
 {
     stopifnot("package:foreach" %in% search() || require("foreach",quietly=TRUE))
     start_t<-Sys.time()
@@ -959,6 +959,66 @@
     return(out_list)
 }
 
+#' portfolio optimization with support for rebalancing or rolling periods
+#' 
+#' This function may eventually be wrapped into optimize.portfolio
+#' 
+#' For now, we'll set the rebalancing periods here, though I think they should eventually be part of the constraints object
+#' 
+#' This function is massively parallel, and will require 'foreach' and we suggest that you register a parallel backend.
+#' 
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
+#' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization
+#' @param optimize_method one of "DEoptim", "random", or "ROI"
+#' @param search_size integer, how many portfolios to test, default 20,000
+#' @param trace TRUE/FALSE if TRUE will attempt to return additional information on the path or portfolios searched
+#' @param \dots any other passthru parameters
+#' @param rp a set of random portfolios passed into the function, to prevent recalculation
+#' @param rebalance_on a periodicity as returned by xts function periodicity and usable by endpoints
+#' @param training_period period to use as training in the front of the data
+#' @param trailing_periods if set, an integer with the number of periods to roll over, default NULL will run from inception 
+#' @return a list containing the optimal weights, some summary statistics, the function call, and optionally trace information 
+#' @author Kris Boudt, Peter Carl, Brian G. Peterson
+#' @export
+optimize.portfolio.rebalancing <- function(R, portfolio, optimize_method=c("DEoptim","random","ROI"), search_size=20000, trace=FALSE, ..., rp=NULL, rebalance_on=NULL, training_period=NULL, trailing_periods=NULL)
+{
+  stopifnot("package:foreach" %in% search() || require("foreach",quietly=TRUE))
+  start_t<-Sys.time()
+  
+  #store the call for later
+  call <- match.call()
+  if(optimize_method=="random"){
+    #' call random_portfolios() with constraints and search_size to create matrix of portfolios
+    if(is.null(rp))
+      rp<-random_portfolios(portfolio=portfolio, permutations=search_size)
+  } else {
+    rp=NULL
+  }    
+  
+  if(is.null(training_period)) {if(nrow(R)<36) training_period=nrow(R) else training_period=36}
+  if (is.null(trailing_periods)){
+    # define the index endpoints of our periods
+    ep.i<-endpoints(R,on=rebalance_on)[which(endpoints(R, on = rebalance_on)>=training_period)]
+    # now apply optimize.portfolio to the periods, in parallel if available
+    out_list<-foreach(ep=iter(ep.i), .errorhandling='pass', .packages='PortfolioAnalytics') %dopar% {
+      optimize.portfolio(R[1:ep,], portfolio=portfolio, optimize_method=optimize_method, search_size=search_size, trace=trace, rp=rp, parallel=FALSE, ...=...)
+    }
+  } else {
+    # define the index endpoints of our periods
+    ep.i<-endpoints(R,on=rebalance_on)[which(endpoints(R, on = rebalance_on)>=training_period)]
+    # now apply optimize.portfolio to the periods, in parallel if available
+    out_list<-foreach(ep=iter(ep.i), .errorhandling='pass', .packages='PortfolioAnalytics') %dopar% {
+      optimize.portfolio(R[(ifelse(ep-trailing_periods>=1,ep-trailing_periods,1)):ep,], portfolio=portfolio, optimize_method=optimize_method, search_size=search_size, trace=trace, rp=rp, parallel=FALSE, ...=...)
+    }
+  }
+  names(out_list)<-index(R[ep.i])
+  
+  end_t<-Sys.time()
+  message(c("overall elapsed time:",end_t-start_t))
+  class(out_list)<-c("optimize.portfolio.rebalancing")
+  return(out_list)
+}
+
 #'execute multiple optimize.portfolio calls, presumably in parallel
 #' 
 #' TODO write function to check sensitivity of optimal results by using optimize.portfolio.parallel results

Modified: pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing.Rd	2013-07-24 14:06:04 UTC (rev 2639)
+++ pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing.Rd	2013-07-25 00:27:59 UTC (rev 2640)
@@ -2,7 +2,7 @@
 \alias{optimize.portfolio.rebalancing}
 \title{portfolio optimization with support for rebalancing or rolling periods}
 \usage{
-  optimize.portfolio.rebalancing(R, constraints,
+  optimize.portfolio.rebalancing(R, portfolio,
     optimize_method = c("DEoptim", "random", "ROI"),
     search_size = 20000, trace = FALSE, ..., rp = NULL,
     rebalance_on = NULL, training_period = NULL,
@@ -12,11 +12,11 @@
   \item{R}{an xts, vector, matrix, data frame, timeSeries
   or zoo object of asset returns}
 
-  \item{constraints}{an object of type "constraints"
-  specifying the constraints for the optimization, see
-  \code{\link{constraint}}}
+  \item{portfolio}{an object of type "portfolio" specifying
+  the constraints and objectives for the optimization}
 
-  \item{optimize_method}{one of "DEoptim" or "random"}
+  \item{optimize_method}{one of "DEoptim", "random", or
+  "ROI"}
 
   \item{search_size}{integer, how many portfolios to test,
   default 20,000}

Added: pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing_v1.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing_v1.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/optimize.portfolio.rebalancing_v1.Rd	2013-07-25 00:27:59 UTC (rev 2640)
@@ -0,0 +1,64 @@
+\name{optimize.portfolio.rebalancing_v1}
+\alias{optimize.portfolio.rebalancing_v1}
+\title{version 1 portfolio optimization with support for rebalancing or rolling periods}
+\usage{
+  optimize.portfolio.rebalancing_v1(R, constraints,
+    optimize_method = c("DEoptim", "random", "ROI"),
+    search_size = 20000, trace = FALSE, ..., rp = NULL,
+    rebalance_on = NULL, training_period = NULL,
+    trailing_periods = NULL)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{constraints}{an object of type "constraints"
+  specifying the constraints for the optimization, see
+  \code{\link{constraint}}}
+
+  \item{optimize_method}{one of "DEoptim" or "random"}
+
+  \item{search_size}{integer, how many portfolios to test,
+  default 20,000}
+
+  \item{trace}{TRUE/FALSE if TRUE will attempt to return
+  additional information on the path or portfolios
+  searched}
+
+  \item{\dots}{any other passthru parameters}
+
+  \item{rp}{a set of random portfolios passed into the
+  function, to prevent recalculation}
+
+  \item{rebalance_on}{a periodicity as returned by xts
+  function periodicity and usable by endpoints}
+
+  \item{training_period}{period to use as training in the
+  front of the data}
+
+  \item{trailing_periods}{if set, an integer with the
+  number of periods to roll over, default NULL will run
+  from inception}
+}
+\value{
+  a list containing the optimal weights, some summary
+  statistics, the function call, and optionally trace
+  information
+}
+\description{
+  This function may eventually be wrapped into
+  optimize.portfolio
+}
+\details{
+  For now, we'll set the rebalancing periods here, though I
+  think they should eventually be part of the constraints
+  object
+
+  This function is massively parallel, and will require
+  'foreach' and we suggest that you register a parallel
+  backend.
+}
+\author{
+  Kris Boudt, Peter Carl, Brian G. Peterson
+}
+



More information about the Returnanalytics-commits mailing list