[Returnanalytics-commits] r3076 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Sep 13 07:18:31 CEST 2013
Author: rossbennett34
Date: 2013-09-13 07:18:31 +0200 (Fri, 13 Sep 2013)
New Revision: 3076
Added:
pkg/PortfolioAnalytics/man/rp_simplex.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/random_portfolios.R
Log:
Adding rp_simplex function to generate random portfolios based on Shaw method.
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-09-13 02:15:21 UTC (rev 3075)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-09-13 05:18:31 UTC (rev 3076)
@@ -61,6 +61,7 @@
export(return_constraint)
export(return_objective)
export(risk_budget_objective)
+export(rp_simplex)
export(rp_transform)
export(scatterFUN)
export(set.portfolio.moments_v1)
Modified: pkg/PortfolioAnalytics/R/random_portfolios.R
===================================================================
--- pkg/PortfolioAnalytics/R/random_portfolios.R 2013-09-13 02:15:21 UTC (rev 3075)
+++ pkg/PortfolioAnalytics/R/random_portfolios.R 2013-09-13 05:18:31 UTC (rev 3076)
@@ -358,6 +358,68 @@
#' @export
random_portfolios <- random_portfolios_v2
+#' Generate random portfolios using the simplex method
+#'
+#' This function generates random portfolios based on the method outlined in the
+#' Shaw paper. Need to add reference.
+#'
+#' @details
+#' The simplex method is useful to generate random portfolios with the full
+#' investment constraint where the sum of the weights is equal to 1 and min and
+#' max box constraints. All other constraints such as group and position limit
+#' constraints will be handled by elimination. If the constraints are very
+#' restrictive, this may result in very few feasible portfolios remaining.
+#'
+#' The random portfolios are created by first generating a set of uniform
+#' random numbers.
+#' \deqn{U \sim [0, 1]}
+#' The portfolio weights are then transformed to satisfy the min of the
+#' box constraints.
+#' \deqn{w_{i} = min_{i} + (1 - \sum_{j=1}^{N} min_{j}) \frac{log(U_{i}^{q}}{\sum_{k=1}^{N}log(U_{k}^{q}}}
+#'
+#' \code{p} controls the Face-Edge-Vertex (FEV) biasing where \deqn{q=2^p}. As
+#' \code{q} approaches infinity, the set of weights will be concentrated in a
+#' single asset. To sample the interior and exterior, \code{p} can be passed
+#' in as a vector. The number of portfolios, \code{permutations}, and the
+#' length of \code{p} affect how the random portfolios are generated. For
+#' example if \code{permutations=10000} and \code{p=0:4}, 2000 portfolios will
+#' be generated for each value of \code{p}.
+#'
+#' @param portfolio an object of type "portfolio" specifying the constraints for the optimization, see \code{\link{portfolio.spec}}
+#' @param permutations integer: number of unique constrained random portfolios to generate
+#' @param p scalar or vector for FEV biasing
+#' @param \dots any other passthru parameters
+#' @return a matrix of random portfolios
+#' @export
+rp_simplex <- function(portfolio, permutations, p=0:5, ...){
+ # get the assets from the portfolio
+ assets <- portfolio$assets
+ nassets <- length(assets)
+
+ # get the constraints
+ # the simplex method for generating random portfolios requires that the sum of weights is equal to 1
+ constraints <- get_constraints(portfolio)
+ L <- constraints$min
+
+ # number of portfolios for each p to generate
+ k <- floor(permutations / length(p))
+
+ # generate uniform[0, 1] random numbers
+ U <- runif(n=k*permutations, 0, 1)
+ Umat <- matrix(data=U, nrow=k, ncol=nassets)
+
+ # do the transformation to the set of weights to satisfy lower bounds
+ stopifnot("package:foreach" %in% search() || require("foreach",quietly = TRUE))
+ out <- foreach(j = 1:length(p), .combine=c) %:% foreach(i=1:nrow(Umat)) %dopar% {
+ q <- 2^p[j]
+ tmp <- L + (1 - sum(L)) * log(Umat[i,])^q / sum(log(Umat[i,])^q)
+ tmp
+ }
+ # the foreach loop returns a list of each random portfolio
+ out <- do.call(rbind, out)
+ return(out)
+}
+
# EXAMPLE: start_t<- Sys.time(); x=random_walk_portfolios(rep(1/5,5), generatesequence(min=0.01, max=0.30, by=0.01), max_permutations=500, permutations=5000, min_sum=.99, max_sum=1.01); end_t<-Sys.time(); end_t-start_t;
# > nrow(unique(x))
# [1] 4906
Added: pkg/PortfolioAnalytics/man/rp_simplex.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/rp_simplex.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/rp_simplex.Rd 2013-09-13 05:18:31 UTC (rev 3076)
@@ -0,0 +1,53 @@
+\name{rp_simplex}
+\alias{rp_simplex}
+\title{Generate random portfolios using the simplex method}
+\usage{
+ rp_simplex(portfolio, permutations, p = 0:5, ...)
+}
+\arguments{
+ \item{portfolio}{an object of type "portfolio" specifying
+ the constraints for the optimization, see
+ \code{\link{portfolio.spec}}}
+
+ \item{permutations}{integer: number of unique constrained
+ random portfolios to generate}
+
+ \item{p}{scalar or vector for FEV biasing}
+
+ \item{\dots}{any other passthru parameters}
+}
+\value{
+ a matrix of random portfolios
+}
+\description{
+ This function generates random portfolios based on the
+ method outlined in the Shaw paper. Need to add reference.
+}
+\details{
+ The simplex method is useful to generate random
+ portfolios with the full investment constraint where the
+ sum of the weights is equal to 1 and min and max box
+ constraints. All other constraints such as group and
+ position limit constraints will be handled by
+ elimination. If the constraints are very restrictive,
+ this may result in very few feasible portfolios
+ remaining.
+
+ The random portfolios are created by first generating a
+ set of uniform random numbers. \deqn{U \sim [0, 1]} The
+ portfolio weights are then transformed to satisfy the min
+ of the box constraints. \deqn{w_{i} = min_{i} + (1 -
+ \sum_{j=1}^{N} min_{j})
+ \frac{log(U_{i}^{q}}{\sum_{k=1}^{N}log(U_{k}^{q}}}
+
+ \code{p} controls the Face-Edge-Vertex (FEV) biasing
+ where \deqn{q=2^p}. As \code{q} approaches infinity, the
+ set of weights will be concentrated in a single asset. To
+ sample the interior and exterior, \code{p} can be passed
+ in as a vector. The number of portfolios,
+ \code{permutations}, and the length of \code{p} affect
+ how the random portfolios are generated. For example if
+ \code{permutations=10000} and \code{p=0:4}, 2000
+ portfolios will be generated for each value of \code{p}.
+}
+
More information about the Returnanalytics-commits
mailing list