[Returnanalytics-commits] r3422 - in pkg/PortfolioAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 16 23:23:55 CEST 2014
Author: rossbennett34
Date: 2014-06-16 23:23:54 +0200 (Mon, 16 Jun 2014)
New Revision: 3422
Added:
pkg/PortfolioAnalytics/man/add.sub.portfolio.Rd
pkg/PortfolioAnalytics/man/mult.portfolio.spec.Rd
Modified:
pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
pkg/PortfolioAnalytics/R/optimize.portfolio.R
Log:
Adding support for mult layer optimization in optimize.portfolio and optimize.portfolio.R. Adding .Rd files for multi layer optimization.
Modified: pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/mult.layer.portfolio.R 2014-06-16 17:50:13 UTC (rev 3421)
+++ pkg/PortfolioAnalytics/R/mult.layer.portfolio.R 2014-06-16 21:23:54 UTC (rev 3422)
@@ -51,8 +51,8 @@
search_size = search_size,
rp = rp,
rebalance_on = rebalance_on,
- training_period = NULL,
- trailing_periods= NULL),
+ training_period = training_period,
+ trailing_periods = trailing_periods),
list(...)),
class="sub.portfolio"
) # end structure
@@ -81,7 +81,7 @@
#' sub portfolios in the \code{mult.portfolio} object. If \code{indexnum} is
#' specified, the portfolio in that index number is overwritten.
#' @seealso \code{\link{mult.portfolio.spec}} \code{\link{portfolio.spec}} \code{\link{optimize.portfolio}} \code{\link{optimize.portfolio.rebalancing}}
-#' @author
+#' @author Ross Bennett
#' @export
add.sub.portfolio <- function(mult.portfolio,
portfolio,
@@ -115,5 +115,51 @@
return(mult.portfolio)
}
+# This function calls optimize.portfolio.rebalancing on each sub portfolio
+# according to the given optimization parameters and returns an xts object
+# representing the proxy returns of each sub portfolio
+proxy.mult.portfolio <- function(R, mult.portfolio, ...){
+ # Check to make sure that the mult.portfolio passed in is a
+ # mult.portfolio.spec object
+ if(!inherits(mult.portfolio, "mult.portfolio.spec")){
+ stop("mult.portfolio must be of class 'mult.portfolio.spec'")
+ }
+
+ n.sub.portfolios <- length(mult.portfolio$sub.portfolios)
+ ret <- vector("list", n.sub.portfolios)
+
+ # Loop through the sub portfolios and call optimize.portfolio.rebalancing
+ # on each sub portfolio and its optimization parameters
+ for(i in 1:n.sub.portfolios){
+ #print(paste("sub portfolio", i))
+ tmp <- mult.portfolio$sub.portfolios[[i]]
+
+ # We need to subset the R object based on the names of portfolio$assets in
+ # the sub portfolio
+ # This requires that asset names match colnames(R)
+ R.tmp <- R[,names(tmp$portfolio$assets)]
+ if(ncol(R.tmp) != length(tmp$portfolio$assets)){
+ stop("R object of returns not subset correctly. Make sure the names of
+ the assets in the sub portfolio match the column names of the R object")
+ }
+ # This needs to support
+ .formals <- formals(optimize.portfolio.rebalancing)
+ .formals <- PortfolioAnalytics:::modify.args(formals=.formals, arglist=NULL, R=R, dots=TRUE)
+ .formals <- PortfolioAnalytics:::modify.args(formals=.formals, arglist=tmp, dots=TRUE)
+ .formals$... <- NULL
+ #print(.formals)
+ opt <- try(do.call(optimize.portfolio.rebalancing, .formals), silent=TRUE)
+ if(inherits(opt, "try-error")) {
+ message(paste("optimize.portfolio.rebalancing for sub portfolio", i, "generated an error or warning:", opt))
+ next()
+ }
+ ret.tmp <- Return.rebalancing(R.tmp, extractWeights(opt))
+ colnames(ret.tmp) <- paste("proxy", i, sep=".")
+ ret[[i]] <- ret.tmp
+ #print(ret[[i]])
+ }
+ proxy.ret <- na.omit(do.call(cbind, ret))
+ return(proxy.ret)
+}
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2014-06-16 17:50:13 UTC (rev 3421)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2014-06-16 21:23:54 UTC (rev 3422)
@@ -493,6 +493,19 @@
regime.switching <- FALSE
}
+ # This is the case where the user has passed in a mult.portfolio.spec
+ # object for multiple layer portfolio optimization.
+ if(inherits(portfolio, "mult.portfolio.spec")){
+ # This function calls optimize.portfolio.rebalancing on each sub portfolio
+ # according to the given optimization parameters and returns an xts object
+ # representing the proxy returns of each sub portfolio.
+ R <- proxy.mult.portfolio(R=R, mult.portfolio=portfolio)
+
+ # The optimization is controlled by the constraints and objectives in the
+ # top level portfolio so now set the 'portfolio' to the top level portfolio
+ portfolio <- portfolio$top.portfolio
+ }
+
optimize_method <- optimize_method[1]
tmptrace <- NULL
start_t <- Sys.time()
@@ -1406,6 +1419,18 @@
return(out)
}
+ # This is the case where the user has passed in a mult.portfolio.spec
+ # object for multiple layer portfolio optimization.
+ if(inherits(portfolio, "mult.portfolio.spec")){
+ # The optimization is controlled by the constraints and objectives in the
+ # top level portfolio
+ portfolio <- portfolio$top.portfolio
+ # This function calls optimize.portfolio.rebalancing on each sub portfolio
+ # according to the given optimization parameters and returns an xts object
+ # representing the proxy returns of each sub portfolio.
+ R <- proxy.mult.portfolio(R=R, mult.portfolio=portfolio)
+ }
+
# Store the call to return later
call <- match.call()
Added: pkg/PortfolioAnalytics/man/add.sub.portfolio.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/add.sub.portfolio.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/add.sub.portfolio.Rd 2014-06-16 21:23:54 UTC (rev 3422)
@@ -0,0 +1,48 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{add.sub.portfolio}
+\alias{add.sub.portfolio}
+\title{Add sub-portfolio}
+\usage{
+add.sub.portfolio(mult.portfolio, portfolio, optimize_method = c("DEoptim",
+ "random", "ROI", "pso", "GenSA"), search_size = 20000, rp = NULL,
+ rebalance_on = NULL, training_period = NULL, trailing_periods = NULL,
+ ..., indexnum = NULL)
+}
+\arguments{
+\item{mult.portfolio}{a \code{mult.portfolio.spec} object}
+
+\item{portfolio}{a \code{portfolio} object to add as a sub portfolio.}
+
+\item{optimize_method}{optimization method for the sub portfolio}
+
+\item{search_size}{integer, how many portfolios to test, default 20,000}
+
+\item{rp}{matrix of random portfolio weights, default NULL, mostly for automated use by rebalancing optimization or repeated tests on same portfolios}
+
+\item{rebalance_on}{haracter string of period to rebalance on. See
+\code{\link[xts]{endpoints}} for valid names.}
+
+\item{training_period}{an integer of the number of periods to use as
+a training data in the front of the returns data}
+
+\item{trailing_periods}{an integer with the number of periods to roll over
+(i.e. width of the moving or rolling window), the default is NULL will
+run using the returns data from inception}
+
+\item{\dots}{additonal passthrough parameters to \code{\link{optimize.portfolio.rebalancing}}}
+
+\item{indexnum}{the index number of the sub portfolio. If \code{indexnum=NULL}
+(the default), then the sub portfolio object is appended to the list of
+sub portfolios in the \code{mult.portfolio} object. If \code{indexnum} is
+specified, the portfolio in that index number is overwritten.}
+}
+\description{
+Add a sub-portfolio to a multiple layer portfolio specification object
+}
+\author{
+Ross Bennett
+}
+\seealso{
+\code{\link{mult.portfolio.spec}} \code{\link{portfolio.spec}} \code{\link{optimize.portfolio}} \code{\link{optimize.portfolio.rebalancing}}
+}
+
Added: pkg/PortfolioAnalytics/man/mult.portfolio.spec.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/mult.portfolio.spec.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/mult.portfolio.spec.Rd 2014-06-16 21:23:54 UTC (rev 3422)
@@ -0,0 +1,38 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{mult.portfolio.spec}
+\alias{mult.portfolio.spec}
+\title{Multple Layer Portfolio Specification}
+\usage{
+mult.portfolio.spec(portfolio, levels = 2, ...)
+}
+\arguments{
+\item{portfolio}{the "top level" portfolio}
+
+\item{levels}{number of levels of sub-portfolios}
+
+\item{\dots}{any additional parameters}
+}
+\value{
+a \code{mult.portfolio.spec} object with the top level portfolio
+and sub portfolios with optimization parameters for each sub portfolio
+}
+\description{
+Create and specify a multiple layer portfolio
+}
+\details{
+The \code{sub.portfolios} slot is a list where each element contains the
+portfolio object and rebalancing parameters for the optimization of the
+sub portfolio.
+This allows, for example, each sub portfolio to have different rebalancing
+frequencies (i.e. monthly or quarterly), optimization methods, etc.
+
+Each sub portfolio is optimized with \code{optimize.portfolio.rebalancing}
+to create a time series of proxy returns.
+
+The "top level" portfolio is used to specify the constraints and objectives
+to control the optimization given the proxy returns of each sub portfolio.
+}
+\author{
+Ross Bennett
+}
+
More information about the Returnanalytics-commits
mailing list