[Returnanalytics-commits] r3421 - pkg/PortfolioAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 16 19:50:14 CEST 2014
Author: rossbennett34
Date: 2014-06-16 19:50:13 +0200 (Mon, 16 Jun 2014)
New Revision: 3421
Added:
pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
Log:
Adding code to specify multiple layers and add sub portfolios for multi layer optimization
Added: pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/mult.layer.portfolio.R (rev 0)
+++ pkg/PortfolioAnalytics/R/mult.layer.portfolio.R 2014-06-16 17:50:13 UTC (rev 3421)
@@ -0,0 +1,119 @@
+
+
+# I am going to start with two levels. Once that is working, I will generalize
+# to any arbitrary number of levels.
+
+#' Multple Layer Portfolio Specification
+#'
+#' Create and specify a multiple layer portfolio
+#'
+#' 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.
+#'
+#' @param portfolio the "top level" portfolio
+#' @param levels number of levels of sub-portfolios
+#' @param \dots any additional parameters
+#' @return a \code{mult.portfolio.spec} object with the top level portfolio
+#' and sub portfolios with optimization parameters for each sub portfolio
+#' @author Ross Bennett
+#' @export
+mult.portfolio.spec <- function(portfolio, levels=2, ...){
+ structure(c(list(top.portfolio = portfolio,
+ sub.portfolios = list()),
+ list(...)),
+ class="mult.portfolio.spec")
+}
+
+# constructor for sub.portfolio object
+sub.portfolio <- function(portfolio,
+ optimize_method = c("DEoptim","random","ROI","pso","GenSA"),
+ search_size = 20000,
+ rp = NULL,
+ rebalance_on = NULL,
+ training_period = NULL,
+ trailing_periods = NULL,
+ ...){
+ # Check to make sure that the portfolio passed in is a portfolio object
+ if (!is.portfolio(portfolio)) stop("portfolio passed in is not of class 'portfolio'")
+
+ # structure and return
+ return(structure( c(list(portfolio = portfolio,
+ optimize_method = optimize_method[1],
+ search_size = search_size,
+ rp = rp,
+ rebalance_on = rebalance_on,
+ training_period = NULL,
+ trailing_periods= NULL),
+ list(...)),
+ class="sub.portfolio"
+ ) # end structure
+ )
+}
+
+#' Add sub-portfolio
+#'
+#' Add a sub-portfolio to a multiple layer portfolio specification object
+#'
+#' @param mult.portfolio a \code{mult.portfolio.spec} object
+#' @param portfolio a \code{portfolio} object to add as a sub portfolio.
+#' @param optimize_method optimization method for the sub portfolio
+#' @param search_size integer, how many portfolios to test, default 20,000
+#' @param rp matrix of random portfolio weights, default NULL, mostly for automated use by rebalancing optimization or repeated tests on same portfolios
+#' @param rebalance_on haracter string of period to rebalance on. See
+#' \code{\link[xts]{endpoints}} for valid names.
+#' @param training_period an integer of the number of periods to use as
+#' a training data in the front of the returns data
+#' @param 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
+#' @param \dots additonal passthrough parameters to \code{\link{optimize.portfolio.rebalancing}}
+#' @param 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.
+#' @seealso \code{\link{mult.portfolio.spec}} \code{\link{portfolio.spec}} \code{\link{optimize.portfolio}} \code{\link{optimize.portfolio.rebalancing}}
+#' @author
+#' @export
+add.sub.portfolio <- function(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){
+ # Check to make sure that the portfolio passed in is a portfolio mult.portfolio
+ if(!inherits(mult.portfolio, "mult.portfolio.spec")) stop("mult.portfolio must be of class 'mult.portfolio.spec'")
+
+ # construct a sub portfolio object
+ tmp_portfolio <- sub.portfolio(portfolio=portfolio,
+ optimize_method=optimize_method[1],
+ search_size=search_size,
+ rp=rp,
+ rebalance_on=rebalance_on,
+ training_period=training_period,
+ trailing_periods=trailing_periods,
+ ...=...)
+
+ if(inherits(tmp_portfolio, "sub.portfolio")){
+ if(!hasArg(indexnum) | (hasArg(indexnum) & is.null(indexnum))){
+ indexnum <- length(mult.portfolio$sub.portfolios)+1
+ }
+ mult.portfolio$sub.portfolios[[indexnum]] <- tmp_portfolio
+ }
+ return(mult.portfolio)
+}
+
+
+
More information about the Returnanalytics-commits
mailing list