[Returnanalytics-commits] r1975 - pkg/PerformanceAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 4 09:13:26 CEST 2012
Author: ababii
Date: 2012-06-04 09:13:26 +0200 (Mon, 04 Jun 2012)
New Revision: 1975
Modified:
pkg/PerformanceAnalytics/R/aggregate.R
Log:
Corrected some mistakes. Added descriptions. The function returns aggregated returns and weights to the chosen level from the hierarchy.
Modified: pkg/PerformanceAnalytics/R/aggregate.R
===================================================================
--- pkg/PerformanceAnalytics/R/aggregate.R 2012-06-03 23:32:28 UTC (rev 1974)
+++ pkg/PerformanceAnalytics/R/aggregate.R 2012-06-04 07:13:26 UTC (rev 1975)
@@ -1,4 +1,4 @@
-#' aggregates portfolio up
+#' aggregate portfolio up
#'
#' @aliases aggregate
#'
@@ -6,31 +6,35 @@
#' portfolio hierarchy (from the buildHierarchy function)
#'
#' @aliases aggregate
-#' @param Rp portfolio returns
-#' @param wp portfolio weights
-#' @param h portfolio hierarchy
-#' @param level level of aggregation in the hierarchy
+#' @param Rp xts, data frame or matrix of portfolio returns
+#' @param wp xts, data frame or matrix of portfolio weights. Names of columns
+#' in weights should correspond to the names of columns in returns
+#' @param h portfolio hierarchy returned by buildHierarchy
+#' @param level aggregation level from the hierarchy
#' @author Andrii Babii
-#' @seealso
+#' @seealso \code{\link{buildHierarchy}}
+#' TODO Replace example using portfolio dataset. It should work with returns
+#' in the same manner as Return.portfolio function in future
#' @references
+#' @export
#' @examples
#'
-aggregate <-
-function(Rp, wp, h, level = "Sector"){
-
+aggregate.returns <-
+function(Rp, wp, h, level = "Sector")
+{
+
+ Rp = checkData(Rp, method = "xts")
+ wp = checkData(wp, method = "xts")
+
h = split(h$primary_id, h[level])
returns = as.xts(matrix(NA, ncol = length(h), nrow = nrow(Rp)), index(Rp))
for(j in 1:length(h)){
+ rp = as.xts(matrix(0, ncol = 1, nrow = nrow(Rp)), index(Rp))
for(i in 1:length(h[[j]])){
asset = h[[j]][i]
r = as.data.frame(Rp)[asset] * as.data.frame(wp)[asset]
r = as.xts(r)
-
- if (i == 1){
- rp = r
- } else{
- rp = rp + r
- }
+ rp = rp + r
}
returns[, j] = rp
colnames(returns) = names(h)
@@ -38,6 +42,29 @@
return(returns)
}
+aggregate.weights <-
+function(wp, h, level = "Sector")
+{
+ Rp = checkData(Rp, method = "xts")
+ wp = checkData(wp, method = "xts")
+
+ h = split(h$primary_id, h[level])
+ weights = wp[, 1:length(h)]
+
+ for(j in 1:length(h)){
+ W = as.xts(matrix(0, ncol = 1, nrow = nrow(wp)), index(wp))
+ for(i in 1:length(h[[j]])){
+ asset = h[[j]][i]
+ w = as.data.frame(wp)[asset]
+ w = as.xts(w)
+ W = W + w
+ }
+ weights[, j] = W
+ colnames(weights) = names(h)
+ }
+ return(weights)
+}
+
# Example
# 1. Generate data
@@ -54,11 +81,24 @@
Rp <- cbind(Rp, r)
}
}
-wp <- as.xts(matrix(rep(c(0.3, 0.1, 0.2, 0.1, 0.2), 5), 5, 5), index(Rp))
+wp <- as.xts(matrix(rep(c(0.3, 0.1, 0.2, 0.1, 0.2), 5), 5, 5, TRUE), index(Rp))
colnames(wp) <- colnames(Rp)
# 2. Aggregate portfolio
Rp
-aggregate(Rp, wp, hierarchy, level = "Sector")
-aggregate(Rp, wp, hierarchy, level = "type")
-aggregate(Rp, wp, hierarchy, level = "currency")
\ No newline at end of file
+aggregate.returns(Rp, wp, hierarchy, level = "Sector")
+aggregate.returns(Rp, wp, hierarchy, level = "type")
+aggregate.weights(wp, hierarchy, 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