[Returnanalytics-commits] r2009 - pkg/PortfolioAnalytics/sandbox/attribution

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 12 16:28:28 CEST 2012


Author: ababii
Date: 2012-06-12 16:28:28 +0200 (Tue, 12 Jun 2012)
New Revision: 2009

Modified:
   pkg/PortfolioAnalytics/sandbox/attribution/Return.level.R
Log:
- documentation

Modified: pkg/PortfolioAnalytics/sandbox/attribution/Return.level.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/Return.level.R	2012-06-12 12:51:25 UTC (rev 2008)
+++ pkg/PortfolioAnalytics/sandbox/attribution/Return.level.R	2012-06-12 14:28:28 UTC (rev 2009)
@@ -1,26 +1,36 @@
-#' aggregate portfolio to the given level
+#' aggregate portfolio returns and weights up to the chosen level from the hierarchy
 #' 
-#' @aliases aggregate
-#' 
-#' Aggregates the portfoio up to the chosen level using returns, weights and
-#' portfolio hierarchy (from the buildHierarchy function)
+#' Aggregate returns and weights up to the chosen level from the hierarchy. 
+#' Hierarchy can be used from the buildHierarchy function or 
+#' defined manually in the same way as the buildHierarchy's output
 #'
-#' @aliases aggregate
+#' @aliases Weight.transform Return.level Weight.level
 #' @param Rp xts, data frame or matrix of portfolio returns
-#' @param wp vector, xts, data frame or matrix of portfolio weights.
-#' @param h  portfolio hierarchy returned by the buildHierarchy function
-#' @param level aggregation level from the hierarchy
+#' @param wp vector, xts, data frame or matrix of portfolio weights
+#' @param h  data.frame with portfolio hierarchy
+#' @param level level from the hierarchy to which returns and weights will be aggregated
 #' @author Andrii Babii
-#' @seealso  \code{\link{buildHierarchy}}
-#' @references
-#' @export
+#' @seealso  \code{\link{buildHierarchy}} \cr \code{\link{Attribution}} \cr \code{\link{Return.portfolio}}
+#' @references Christopherson, Jon A., Carino, David R., Ferson, Wayne E.  
+#' \emph{Portfolio Performance Measurement and Benchmarking}. McGraw-Hill. 2009. Chapter 17
+#' @keywords attribution
 #' @examples
 #' 
-
+#' data(attrib)
+#' Weight.transform(wp, Rp)
+#' Return.level(Rp, wp, h, level = "Sector")
+#' Weight.level(wp, h, level = "Sector")
+#' 
+#' @export
 Weight.transform <- 
 function(wp, Rp)
-{
-    # Transform weights to the xts object used by aggregation and attribution functions
+{   # @author Andrii Babii
+    # Function to transform weights to the xts object conformable with returns used by aggregation and attribution functions
+    #
+    # wp                wp vector, xts, data frame or matrix of portfolio weights
+    #
+    # Rp                xts, data frame or matrix of portfolio returns
+
     if (is.vector(wp)){
         wp = as.xts(matrix(rep(wp, nrow(Rp)), nrow(Rp), ncol(Rp), byrow = TRUE), index(Rp))
         colnames(wp) = colnames(Rp)
@@ -43,14 +53,23 @@
 
 Return.level <-
 function(Rp, wp, h, level = "Sector")
-{
+{   # @author Andrii Babii
+    # Function to aggregate returns up to the chosen level from the hierarchy
+    #
+    # Rp                xts, data frame or matrix of portfolio returns
+    #
+    # wp                wp vector, xts, data frame or matrix of portfolio weights
+    #
+    # h                 data.frame with portfolio hierarchy
+    #
+    # level             level from the hierarchy to which the aggregation will be performed
+    
     Rp = checkData(Rp, method = "xts")
     wp = Weight.transform(wp, Rp)
     if (nrow(wp) < nrow(Rp)){ # Rebalancing occurs next day
         Rp = Rp[2:nrow(Rp)]
     }
 
-    # Aggregate returns to the chosen level from the hierarchy
     h = split(h$primary_id, h[level])
     returns = as.xts(matrix(NA, ncol = length(h), nrow = nrow(Rp)), index(Rp))
     for(i in 1:length(h)){
@@ -62,7 +81,15 @@
 
 Weight.level <-
 function(wp, h, level = "Sector")
-{   
+{   # @author Andrii Babii
+    # Function to aggregate weights up to the chosen level from the hierarchy
+    #
+    # wp                wp vector, xts, data frame or matrix of portfolio weights
+    #
+    # h                 data.frame with portfolio hierarchy
+    #
+    # level             level from the hierarchy to which the aggregation will be performed
+    
     wp = Weight.transform(wp, Rp)
 
     h = split(h$primary_id, h[level])
@@ -73,22 +100,3 @@
     colnames(weights) = names(h)
     return(weights)
 }
-
-# Example
-# data(attrib) 
-Weight.transform(wp, Rp)
-Return.level(Rp, wp, h, level = "Sector")
-Return.level(Rp, wp, h, level = "type")
-Weight.level(wp, h, level = "Sector")
-
-###############################################################################
-# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
-#
-# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
-#
-# This R package is distributed under the terms of the GNU Public License (GPL)
-# for full details see the file COPYING
-#
-# $Id: CalmarRatio.R 1905 2012-04-21 19:23:13Z braverock $
-#
-###############################################################################
\ No newline at end of file



More information about the Returnanalytics-commits mailing list