[Returnanalytics-commits] r1965 - pkg/PerformanceAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri May 25 12:25:23 CEST 2012
Author: ababii
Date: 2012-05-25 12:25:23 +0200 (Fri, 25 May 2012)
New Revision: 1965
Modified:
pkg/PerformanceAnalytics/R/contribution.R
Log:
Added support of matrix input
Modified: pkg/PerformanceAnalytics/R/contribution.R
===================================================================
--- pkg/PerformanceAnalytics/R/contribution.R 2012-05-24 23:05:43 UTC (rev 1964)
+++ pkg/PerformanceAnalytics/R/contribution.R 2012-05-25 10:25:23 UTC (rev 1965)
@@ -27,20 +27,31 @@
# This is a wrapper for calculating contribution to returns.
# Inputs:
- # Rp: vector of portfolio returns
- # wp: vector of portfolio weights
+ # Rp: a matrix, data frame, or timeSeries of returns
+ # wp: a matrix, data frame, or timeSeries of weights
# Outputs:
# This function returns the vector of contribution to returns
# FUNCTION:
- Rp = checkData(Rp)
- wp = checkData(wp)
-
- contr = Rp * wp
- colnames(contr) = c(rep("contribution",ncol(contr)))
- return(contr)
+ Rp = checkData(Rp, method = "zoo")
+ wp = checkData(wp, method = "zoo")
+
+ columns = ncol(Rp)
+ rows = nrow(Rp)
+ columnames = colnames(Rp)
+
+ for(i in 1:columns){
+ contr = Rp[, i] * wp[, i]
+ if(i == 1){
+ result.contr = contr
+ } else{
+ result.contr = merge(result.contr, contr)
+ }
+ }
+ colnames(result.contr) = columnames
+ return(result.contr)
}
#' @export
More information about the Returnanalytics-commits
mailing list