[Returnanalytics-commits] r3458 - in pkg/PortfolioAnalytics: . R man sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jul 2 04:03:41 CEST 2014
Author: rossbennett34
Date: 2014-07-02 04:03:40 +0200 (Wed, 02 Jul 2014)
New Revision: 3458
Added:
pkg/PortfolioAnalytics/man/centroid.buckets.Rd
pkg/PortfolioAnalytics/man/centroid.complete.mc.Rd
pkg/PortfolioAnalytics/man/centroid.sectors.Rd
pkg/PortfolioAnalytics/man/centroid.sign.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/ac_ranking.R
pkg/PortfolioAnalytics/man/ac.ranking.Rd
pkg/PortfolioAnalytics/sandbox/centroids.R
Log:
Adding centroid functions to replicate the examples from the Almgren and Chriss paper
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2014-07-01 22:11:50 UTC (rev 3457)
+++ pkg/PortfolioAnalytics/NAMESPACE 2014-07-02 02:03:40 UTC (rev 3458)
@@ -81,6 +81,10 @@
export(black.litterman)
export(box_constraint)
export(center)
+export(centroid.buckets)
+export(centroid.complete.mc)
+export(centroid.sectors)
+export(centroid.sign)
export(chart.Concentration)
export(chart.EfficientFrontier)
export(chart.EfficientFrontierOverlay)
Modified: pkg/PortfolioAnalytics/R/ac_ranking.R
===================================================================
--- pkg/PortfolioAnalytics/R/ac_ranking.R 2014-07-01 22:11:50 UTC (rev 3457)
+++ pkg/PortfolioAnalytics/R/ac_ranking.R 2014-07-02 02:03:40 UTC (rev 3458)
@@ -26,6 +26,8 @@
#' data(edhec)
#' R <- edhec[,1:4]
#' ac.ranking(R, c(2, 3, 1, 4))
+#' @seealso \code{\link{centroid.complete.mc}} \code{\link{centroid.sectors}}
+#' \code{\link{centroid.sign}} \code{\link{centroid.buckets}}
#' @export
ac.ranking <- function(R, order, ...){
if(length(order) != ncol(R)) stop("The length of the order vector must equal the number of assets")
@@ -74,3 +76,165 @@
new.range <- new.max - new.min
((x - min(x)) * new.range) / old.range + new.min
}
+
+# Numerically compute the centroid for different cases as described in
+# the Almgren and Chriss paper.
+
+#' Complete Cases Centroid
+#'
+#' Numerical method to estimate complete cases centroid
+#' @param order a vector of indexes of the relative ranking of expected asset
+#' returns in ascending order. For example, \code{order = c(2, 3, 1, 4)}
+#' expresses a view on the expected returns such that
+#' R_2 < R_3 < R_1 < R_4
+#' @param simulations number of simulations
+#' @return the centroid vector
+#' @examples
+#' # Express a view on the assets such that
+#' # R_2 < R_1 < R_3 < R_4
+#' centroid.complete.mc(c(2, 1, 3, 4))
+#' @author Ross Bennett
+#' @export
+centroid.complete.mc <- function(order, simulations=1000){
+ n <- length(order)
+ c_hat <- matrix(0, simulations, n)
+ for(i in 1:simulations){
+ c_hat[i,] <- sort(rnorm(n), decreasing=TRUE)
+ }
+ out <- vector("numeric", n)
+ out[rev(order)] <- colMeans(c_hat)
+ return(out)
+}
+
+#' Multiple Sectors Centroid
+#'
+#' Compute the centroid for expressing views on the relative ranking of assets
+#' within sectors.
+#'
+#' @param sectors a list where each list element contains the order of each
+#' asset in the given sector
+#' @param simulations number of simulations
+#' @return the centroid vector
+#' @examples
+#' # Express a view on the assets in two sectors
+#' # Sector 1 View: R_2 < R_1 < R_3
+#' # Sector 2 View: R_5 < R_4
+#' x <- list()
+#' x[[1]] <- c(2, 1, 3)
+#' x[[2]] <- c(5, 4)
+#' centroid.sectors(x)
+#' @author Ross Bennett
+#' @export
+centroid.sectors <- function(sectors, simulations=1000){
+ if(!is.list(sectors)) stop("sectors must be a list")
+
+ # Get the number of assets and number of sectors
+ nassets <- length(unlist(sectors))
+ nsectors <- length(sectors)
+
+ # Compute the centroid for each sector and combine at the end
+ sim.list <- vector("list", nsectors)
+ for(j in 1:nsectors){
+ # number of assets in sector j
+ n <- length(sectors[[j]])
+ out <- matrix(0, simulations, n)
+ for(i in 1:simulations){
+ out[i,] <- sort(rnorm(n), decreasing=TRUE)
+ }
+ sim.list[[j]] <- out
+ }
+ c_hat <- lapply(sim.list, colMeans)
+ out <- vector("numeric", nassets)
+ for(i in 1:length(c_hat)){
+ out[rev(sectors[[i]])] <- c_hat[[i]]
+ }
+ return(out)
+}
+
+#' Positive and Negative View Centroid
+#'
+#' Compute the centroid for expressing a view on assets with positive or
+#' negative expected returns
+#'
+#' @param positive a vector of the index of assets with positive expected
+#' return in ascending order
+#' @param negative a vector of the index of assets with negative expected
+#' return in ascending order.
+#' @param simulations number of simulations
+#' @return the centroid vector
+#' @examples
+#' # Express a view that
+#' # R_1 < R_2 < 0 < R_3 < R_4
+#' centroid.sign(c(1, 2), c(4, 3))
+#' @author Ross Bennett
+#' @export
+centroid.sign <- function(positive, negative, simulations=1000){
+
+ # Number of positive and negative assets
+ pos <- length(positive)
+ neg <- length(negative)
+ nassets <- pos + neg
+
+ c_hat <- matrix(0, simulations, nassets)
+ for(i in 1:simulations){
+ tmp <- rnorm(nassets)
+ # subset the positive and negative assets
+ tmp.pos <- tmp[1:pos]
+ tmp.neg <- tmp[(pos+1):(pos+neg)]
+
+ # Sign correct the positive assets
+ idx <- which(tmp.pos < 0)
+ if(length(idx) != 0){
+ tmp.pos[idx] <- -1 * tmp.pos[idx]
+ }
+
+ # Sign correct the negative assets
+ idx <- which(tmp.neg > 0)
+ if(length(idx) != 0){
+ tmp.neg[idx] <- -1 * tmp.neg[idx]
+ }
+ c_hat[i,] <- sort(c(tmp.pos, tmp.neg), decreasing=TRUE)
+ }
+ xx <- colMeans(c_hat)
+ out <- vector("numeric", nassets)
+ out[rev(positive)] <- xx[1:pos]
+ out[rev(negative)] <- xx[(1+pos):(pos+neg)]
+ return(out)
+}
+
+#' Buckets Centroid
+#'
+#' Compute the centroid for buckets of assets
+#'
+#' A common use of buckets is to divide the assets into quartiles or deciles,
+#' but is generalized here for an arbitrary number of buckets and arbitrary
+#' number of assets in each bucket.
+#'
+#' @param buckets a list where each element contains the index of the assets in
+#' the respective bucket. The assets within each bucket have no order.
+#' The bucket elements are in ascending order such that
+#' R_bucket_1 < ... < R_bucket_n
+#' @param simulations number of simulations
+#' @return the centroid vector
+#' @author Ross Bennett
+#' @export
+centroid.buckets <- function(buckets, simulations=1000){
+ if(!is.list(buckets)) stop("buckets must be a list")
+
+ # number of assets and buckets
+ nassets <- length(unlist(buckets))
+ nbuckets <- length(buckets)
+
+ # Run simulations so we simulate n values for n buckets and then replicate
+ # that value for the number of assets in the given bucket
+ c_hat <- matrix(0, simulations, nbuckets)
+ for(i in 1:simulations){
+ c_hat[i,] <- sort(rnorm(nbuckets), decreasing=TRUE)
+ }
+ xx <- colMeans(c_hat)
+ out <- vector("numeric", nassets)
+ for(j in 1:nbuckets){
+ out[buckets[[j]]] <- xx[j]
+ }
+ return(out)
+}
Modified: pkg/PortfolioAnalytics/man/ac.ranking.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/ac.ranking.Rd 2014-07-01 22:11:50 UTC (rev 3457)
+++ pkg/PortfolioAnalytics/man/ac.ranking.Rd 2014-07-02 02:03:40 UTC (rev 3458)
@@ -37,4 +37,8 @@
R. Almgren and N. Chriss, "Portfolios from Sorts"
\url{http://papers.ssrn.com/sol3/papers.cfm?abstract_id=720041}
}
+\seealso{
+\code{\link{centroid.complete.mc}} \code{\link{centroid.sectors}}
+\code{\link{centroid.sign}} \code{\link{centroid.buckets}}
+}
Added: pkg/PortfolioAnalytics/man/centroid.buckets.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/centroid.buckets.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/centroid.buckets.Rd 2014-07-02 02:03:40 UTC (rev 3458)
@@ -0,0 +1,30 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{centroid.buckets}
+\alias{centroid.buckets}
+\title{Buckets Centroid}
+\usage{
+centroid.buckets(buckets, simulations = 1000)
+}
+\arguments{
+\item{buckets}{a list where each element contains the index of the assets in
+the respective bucket. The assets within each bucket have no order.
+The bucket elements are in ascending order such that
+R_bucket_1 < ... < R_bucket_n}
+
+\item{simulations}{number of simulations}
+}
+\value{
+the centroid vector
+}
+\description{
+Compute the centroid for buckets of assets
+}
+\details{
+A common use of buckets is to divide the assets into quartiles or deciles,
+but is generalized here for an arbitrary number of buckets and arbitrary
+number of assets in each bucket.
+}
+\author{
+Ross Bennett
+}
+
Added: pkg/PortfolioAnalytics/man/centroid.complete.mc.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/centroid.complete.mc.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/centroid.complete.mc.Rd 2014-07-02 02:03:40 UTC (rev 3458)
@@ -0,0 +1,30 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{centroid.complete.mc}
+\alias{centroid.complete.mc}
+\title{Complete Cases Centroid}
+\usage{
+centroid.complete.mc(order, simulations = 1000)
+}
+\arguments{
+\item{order}{a vector of indexes of the relative ranking of expected asset
+returns in ascending order. For example, \code{order = c(2, 3, 1, 4)}
+expresses a view on the expected returns such that
+R_2 < R_3 < R_1 < R_4}
+
+\item{simulations}{number of simulations}
+}
+\value{
+the centroid vector
+}
+\description{
+Numerical method to estimate complete cases centroid
+}
+\examples{
+# Express a view on the assets such that
+# R_2 < R_1 < R_3 < R_4
+centroid.complete.mc(c(2, 1, 3, 4))
+}
+\author{
+Ross Bennett
+}
+
Added: pkg/PortfolioAnalytics/man/centroid.sectors.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/centroid.sectors.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/centroid.sectors.Rd 2014-07-02 02:03:40 UTC (rev 3458)
@@ -0,0 +1,33 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{centroid.sectors}
+\alias{centroid.sectors}
+\title{Multiple Sectors Centroid}
+\usage{
+centroid.sectors(sectors, simulations = 1000)
+}
+\arguments{
+\item{sectors}{a list where each list element contains the order of each
+asset in the given sector}
+
+\item{simulations}{number of simulations}
+}
+\value{
+the centroid vector
+}
+\description{
+Compute the centroid for expressing views on the relative ranking of assets
+within sectors.
+}
+\examples{
+# Express a view on the assets in two sectors
+# Sector 1 View: R_2 < R_1 < R_3
+# Sector 2 View: R_5 < R_4
+x <- list()
+x[[1]] <- c(2, 1, 3)
+x[[2]] <- c(5, 4)
+centroid.sectors(x)
+}
+\author{
+Ross Bennett
+}
+
Added: pkg/PortfolioAnalytics/man/centroid.sign.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/centroid.sign.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/centroid.sign.Rd 2014-07-02 02:03:40 UTC (rev 3458)
@@ -0,0 +1,32 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{centroid.sign}
+\alias{centroid.sign}
+\title{Positive and Negative View Centroid}
+\usage{
+centroid.sign(positive, negative, simulations = 1000)
+}
+\arguments{
+\item{positive}{a vector of the index of assets with positive expected
+return in ascending order}
+
+\item{negative}{a vector of the index of assets with negative expected
+return in ascending order.}
+
+\item{simulations}{number of simulations}
+}
+\value{
+the centroid vector
+}
+\description{
+Compute the centroid for expressing a view on assets with positive or
+negative expected returns
+}
+\examples{
+# Express a view that
+# R_1 < R_2 < 0 < R_3 < R_4
+centroid.sign(c(1, 2), c(4, 3))
+}
+\author{
+Ross Bennett
+}
+
Modified: pkg/PortfolioAnalytics/sandbox/centroids.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/centroids.R 2014-07-01 22:11:50 UTC (rev 3457)
+++ pkg/PortfolioAnalytics/sandbox/centroids.R 2014-07-02 02:03:40 UTC (rev 3458)
@@ -1,89 +1,39 @@
-# Numerically compute the centroid for different cases as described in
-# the Almgren and Chriss paper.
-# These replicate the paper, now I just need to functionalize them
+# Complete cases centroid computed numerically
+centroid.complete.mc(order = c(3, 1, 2, 4))
+barplot(centroid.complete.mc(50:1))
+# Express a view on the assets in two sectors
+# Sector 1 View: R_2 < R_1 < R_4
+# Sector 2 View: R_5 < R_3
+x <- list()
+x[[1]] <- c(2, 1, 4)
+x[[2]] <- c(5, 3)
+barplot(centroid.sectors(x))
-# Complete sort
-nsim <- 1000
-nassets <- 50
-out <- matrix(0, nsim, nassets)
-for(i in 1:nsim){
- out[i,] <- sort(rnorm(nassets), decreasing=TRUE)
-}
+y <- list()
+y[[1]] <- 10:1
+y[[2]] <- 40:11
+barplot(centroid.sectors(y))
-barplot(colMeans(out))
+# Express a view that
+# R_1 < R_2 < 0 < R_3 < R_4
+centroid.sign(c(1, 2), c(4, 3))
-# Complete sort with multiple sectors
-sectors <- list()
-sectors[[1]] <- 1:10
-sectors[[2]] <- 11:40
-nassets <- length(unlist(sectors))
-nsectors <- length(sectors)
+# The centroid values of 16:50 are negative
+barplot(centroid.sign(15:1, 50:16))
-sim.list <- vector("list", nsectors)
-for(j in 1:nsectors){
- nassets <- length(sectors[[j]])
- out <- matrix(0, nsim, nassets)
- for(i in 1:nsim){
- out[i,] <- sort(rnorm(nassets), decreasing=TRUE)
- }
- sim.list[[j]] <- out
-}
-barplot(unlist(lapply(sim.list, colMeans)))
+z <- list()
+z[[1]] <- c(1, 3)
+z[[2]] <- c(2, 4)
+barplot(centroid.buckets(z))
-# Complete sort with comparison to 0
-my.list <- list()
-my.list$pos <- c(1, 2, 3, 4)
-my.list$neg <- c(5, 6, 7, 8 , 9, 10)
-pos <- length(my.list$pos)
-neg <- length(my.list$neg)
+zz <- list()
+zz[[1]] <- 10:1
+zz[[2]] <- 20:11
+zz[[3]] <- 30:21
+zz[[4]] <- 40:31
+zz[[5]] <- 50:41
+barplot(centroid.buckets(zz))
-nsim <- 1000
-nassets <- pos + neg
-
-out <- matrix(0, nsim, nassets)
-for(i in 1:nsim){
- tmp <- rnorm(nassets)
- tmp.pos <- tmp[1:pos]
- tmp.neg <- tmp[(pos+1):(pos+neg)]
-
- # Sign correct the pos assets
- idx <- which(tmp.pos < 0)
- if(length(idx) != 0){
- tmp.pos[idx] <- -1 * tmp.pos[idx]
- }
-
- # Sign correct the neg assets
- idx <- which(tmp.neg > 0)
- if(length(idx) != 0){
- tmp.neg[idx] <- -1 * tmp.neg[idx]
- }
- out[i,] <- sort(c(tmp.pos, tmp.neg), decreasing=TRUE)
-}
-
-barplot(colMeans(out))
-
-
-# Complete sort with "buckets"
-qlist <- list()
-qlist[[1]] <- c(1, 2, 3, 4)
-qlist[[2]] <- c(5, 6, 7, 8)
-qlist[[3]] <- c(9, 10, 11, 12)
-qlist[[4]] <- c(13, 14, 15, 16)
-
-nsim <- 1000
-nassets <- length(unlist(qlist))
-nbuckets <- length(qlist)
-out <- matrix(0, nsim, nassets)
-for(i in 1:nsim){
- tmp <- sort(rnorm(nbuckets), decreasing=TRUE)
- vec <- c()
- for(j in 1:nbuckets){
- vec <- c(vec, rep(tmp[j], length(qlist[[j]])))
- }
- out[i,] <- vec
-}
-
-barplot(colMeans(out))
More information about the Returnanalytics-commits
mailing list