[Returnanalytics-commits] r2176 - in pkg/PortfolioAnalytics/sandbox/attribution: . R data man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jul 18 22:17:31 CEST 2012
Author: ababii
Date: 2012-07-18 22:17:30 +0200 (Wed, 18 Jul 2012)
New Revision: 2176
Added:
pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE
pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.level.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.transform.R
pkg/PortfolioAnalytics/sandbox/attribution/data/attrib.rda
pkg/PortfolioAnalytics/sandbox/attribution/man/AppraisalRatio.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/CAPM.dynamic.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/MarketTiming.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Return.level.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Weight.level.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Weight.transform.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/attrib.Rd
Removed:
pkg/PortfolioAnalytics/sandbox/attribution/data/attrib.RData
pkg/PortfolioAnalytics/sandbox/attribution/man/logLinking.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/logLinking.zoo.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/period.apply.EZ.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/relativeAttribution.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/relativeAttributionWithoutFactors.Rd
Modified:
pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION
pkg/PortfolioAnalytics/sandbox/attribution/R/AppraisalRatio.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R
pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R
pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R
pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R
pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Return.annualized.excess.R
pkg/PortfolioAnalytics/sandbox/attribution/R/Return.level.R
pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.R
pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.levels.R
pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.geometric.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.levels.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/AttributionFixedIncome.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Carino.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Conv.option.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/DaviesLaker.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Frongello.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Grap.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/HierarchyQuintiles.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Menchero.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Modigliani.Rd
pkg/PortfolioAnalytics/sandbox/attribution/man/Return.annualized.excess.Rd
Log:
- passed R CMD check without warnings (assuming 5 Eric's functions are deleted)
- split Return.level function into 3
- changed the format of the dataset
- documented dataset
- some minor corrections
Modified: pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION 2012-07-18 20:17:30 UTC (rev 2176)
@@ -1,26 +1,46 @@
-Package: attribution
-Type: Package
-Title: Econometric tools for performance and risk analysis.
-Version: 0.1
-Date: $Date: 2012-06-06 15:18:48 -0500 (Wed, 06 Jun 2012) $
-Author: Andrii Babii
-Maintainer: Brian G. Peterson <brian at braverock.com>
-Description: GSoC stub for attribution
-Depends:
- R (>= 2.14.0),
- zoo,
- xts (>= 0.8),
- PerformanceAnalytics(>= 1.0.4.3)
-Suggests:
- Hmisc,
- MASS,
- tseries,
- quadprog,
- sn,
- robustbase,
- quantreg,
- gplots,
- ff
-License: GPL
-URL: http://r-forge.r-project.org/projects/returnanalytics/
-Copyright: (c) 2004-2012
+Package: attribution
+Type: Package
+Title: Econometric tools for performance and risk analysis.
+Version: 0.1
+Date: $Date: 2012-06-06 15:18:48 -0500 (Wed, 06 Jun 2012) $
+Author: Andrii Babii
+Maintainer: Brian G. Peterson <brian at braverock.com>
+Description: GSoC stub for attribution
+Depends:
+ R (>= 2.14.0),
+ zoo,
+ xts (>= 0.8),
+ PerformanceAnalytics(>= 1.0.4.3)
+Suggests:
+ Hmisc,
+ MASS,
+ tseries,
+ quadprog,
+ sn,
+ robustbase,
+ quantreg,
+ gplots,
+ ff
+License: GPL
+URL: http://r-forge.r-project.org/projects/returnanalytics/
+Copyright: (c) 2004-2012
+Collate:
+ 'AppraisalRatio.R'
+ 'Attribution.geometric.R'
+ 'attribution.levels.R'
+ 'attribution.R'
+ 'AttributionFixedIncome.R'
+ 'CAPM.dynamic.R'
+ 'Carino.R'
+ 'Conv.option.R'
+ 'DaviesLaker.R'
+ 'Frongello.R'
+ 'Grap.R'
+ 'HierarchyQuintiles.R'
+ 'Menchero.R'
+ 'Modigliani.R'
+ 'Return.annualized.excess.R'
+ 'Return.level.R'
+ 'MarketTiming.R'
+ 'Weight.level.R'
+ 'Weight.transform.R'
Added: pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE 2012-07-18 20:17:30 UTC (rev 2176)
@@ -0,0 +1,19 @@
+export(AppraisalRatio)
+export(Attribution)
+export(Attribution.geometric)
+export(Attribution.levels)
+export(AttributionFixedIncome)
+export(CAPM.dynamic)
+export(Carino)
+export(Conv.option)
+export(DaviesLaker)
+export(Frongello)
+export(Grap)
+export(HierarchyQuintiles)
+export(MarketTiming)
+export(Menchero)
+export(Modigliani)
+export(Return.annualized.excess)
+export(Return.level)
+export(Weight.level)
+export(Weight.transform)
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/AppraisalRatio.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/AppraisalRatio.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/AppraisalRatio.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -52,8 +52,8 @@
{
alpha = CAPM.alpha(Ra, Rb, Rf)
beta = CAPM.beta(Ra, Rb, Rf)
- epsilon = Ra - Rf - rep(alpha, nrow(Ra)) - beta %x% Rb
- sigma = diag(t(epsilon) %*% epsilon) / (length(managers) - 2)
+ epsilon = Ra - Rf - matrix(rep(alpha, nrow(Ra)), nrow(Ra), ncol(Ra), byrow = TRUE) - beta %x% (Rb - Rf)
+ sigma = diag(t(epsilon) %*% epsilon) / (length(epsilon) - 2)
AR = alpha / sigma
return(AR)
}
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -71,11 +71,12 @@
#' @examples
#'
#' data(attrib)
-#' Attribution.geometric(Rp, wp, Rb, wb)
+#' Attribution.geometric(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ],
+#' Rb = attrib.returns[, 11:20], wb = attrib.weights[2, ])
#'
#' @export
Attribution.geometric <-
-function(Rp, wp, Rb, wb, Rpl, Rbl, Rbh, currency = FALSE)
+function(Rp, wp, Rb, wb, Rpl = NA, Rbl = NA, Rbh = NA)
{ # @author Andrii Babii
# DESCRIPTION:
@@ -96,6 +97,7 @@
WB = wb
wp = Weight.transform(wp, Rp)
wb = Weight.transform(wb, Rb)
+ currency = !(is.null(dim(Rpl)) & is.null(dim(Rpl)) & is.null(dim(Rpl)))
# Get total portfolio returns
if (is.vector(WP) & is.vector(WB)){
@@ -114,6 +116,7 @@
# Geometric attribution effects for individual categories
allocation = ((1 + Rb) / (1 + rep(rb, ncol(Rp))) - 1) * coredata(wp - wb)
selection = wp * (Rp - coredata(Rb)) / (1 + rep(bs, ncol(Rp)))
+ colnames(allocation) = colnames(Rp)
} else{
Rpl = checkData(Rpl)
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -40,7 +40,7 @@
#' Di * wpi - Dbeta * Dbi * wbi}
#' \deqn{S_{i}=\frac{D_{pi}}{D_{bi}}\times (R_{bi} - R_{fi}) + R_{fi}}{Si =
#' Dpi / Dbi * (Rbi - Rfi) + Rfi}
-#' @aliases fixed income attribution
+#' @aliases AttributionFixedIncome
#' @param Rp T x n xts, data frame or matrix of portfolio returns
#' @param wp vector, xts, data frame or matrix of portfolio weights
#' @param Rb T x n xts, data frame or matrix of benchmark returns
@@ -53,8 +53,6 @@
#' @param S (T + 1) x n xts, data frame or matrix with spot rates. The first
#' date should coincide with the first date of portfolio returns
#' @param geometric - TRUE/FALSE for geometric/arithmetic attribution
-#' @param wbf vector, xts, data frame or matrix with benchmark weights of
-#' currency forward contracts
#' @return list with total excess returns decomposed into allocation, selection
#' (and currency effects)
#' @author Andrii Babii
@@ -68,8 +66,9 @@
#' @examples
#'
#' data(attrib)
-#' AttributionFixedIncome(Rp, wp, Rb, wb, Rf, Dp, Db, S, wbf,
-#' geometric = FALSE)
+#' AttributionFixedIncome(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+#' wb = attrib.weights[2, ], Rf = attrib.returns[, 23:32], Dp = attrib.returns[, 63:72], Db = attrib.returns[, 73:82],
+#' S = attrib.currency[, 11:20], wbf = attrib.weights[4, ], geometric = FALSE)
#'
#' @export
AttributionFixedIncome <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -46,7 +46,7 @@
#' @examples
#'
#' data(attrib)
-#' Carino(rp, rb, allocation, adjusted = FALSE)
+#' Carino(rp = attrib.returns[, 21], rb = attrib.returns[, 22], attributions = attrib.allocation, adjusted = FALSE)
#'
#' @export
Carino <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -9,8 +9,8 @@
#' must be achieved again by using notional assets: \emph{option economic
#' exposure = Delta x number of options x strike price = option valuation +
#' notional exposure}
-#' @aliases options attribution
-#' @param \emph{nx8} matrix containing option ID (as rownames), and columns
+#' @aliases Conv.option
+#' @param option \emph{nx8} matrix containing option ID (as rownames), and columns
#' corresponding to (in particular order): strike price, number of options,
#' current option price, end option price, option's delta, returns on the
#' underlying
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -37,7 +37,7 @@
#' @examples
#'
#' data(attrib)
-#' DaviesLaker(Rp, wp, Rb, wb)
+#' DaviesLaker(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20], wb = attrib.weights[2, ])
#'
#' @export
DaviesLaker <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -36,7 +36,7 @@
#' @examples
#'
#' data(attrib)
-#' Frongello(rp, rb, allocation, adjusted = FALSE)
+#' Frongello(rp = attrib.returns[, 21], rb = attrib.returns[, 22], attributions = attrib.allocation, adjusted = FALSE)
#'
#' @export
Frongello <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -40,7 +40,7 @@
#' @examples
#'
#' data(attrib)
-#' Grap(rp, rb, allocation, adjusted = FALSE)
+#' Grap(rp = attrib.returns[, 21], rb = attrib.returns[, 22], attributions = attrib.allocation, adjusted = FALSE)
#'
#' @export
Grap <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -7,12 +7,12 @@
#' @param h data.frame with portfolio hierarchy
#' @param level level from the hierarchy for which there are numeric values
#' @author Andrii Babii
-#' @seealso \code{\link{buildHierarchy}}
+#' @seealso \code{buildHierarchy}
#' @keywords attribution, hierarchy
#' @examples
#'
#' data(attrib)
-#' HierarchyQuintiles(h, "MarketCap")
+#' HierarchyQuintiles(h = attrib.hierarchy, "MarketCap")
#'
#' @export
HierarchyQuintiles <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -30,10 +30,10 @@
#' @param Rf risk free rate, in same period as your returns
#' @param method used to select between Treynor-Mazuy and Henriksson-Merton
#' models. May be any of: \itemize{ \item TM - Treynor-Mazuy model,
-#' \item HM - Henriksson-Merton model}}
+#' \item HM - Henriksson-Merton model}. By default Treynor-Mazuy is selected
#' @param \dots any other passthrough parameters
-#' @author Andrii Babii
-#' @seealso \code{\link{CAPM.beta}}, \code{\link{MertonHendriksson}}
+#' @author Andrii Babii, Peter Carl
+#' @seealso \code{\link{CAPM.beta}}
#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio
#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill, p. 127-133.
#' \cr J. L. Treynor and K. Mazuy, "Can Mutual Funds Outguess the Market?"
@@ -50,7 +50,7 @@
#'
#' @export
MarketTiming <- function (Ra, Rb, Rf = 0, method = c("TM", "HM"))
-{ # @author Andrii Babii
+{ # @author Andrii Babii, Peter Carl
# FUNCTION
@@ -65,7 +65,6 @@
xRa = Return.excess(Ra, Rf)
xRb = Return.excess(Rb, Rf)
-
mt <- function (xRa, xRb)
{
switch(method,
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -47,7 +47,7 @@
#' @examples
#'
#' data(attrib)
-#' Menchero(rp, rb, allocation, adjusted = FALSE)
+#' Menchero(rp = attrib.returns[, 21], rb = attrib.returns[, 22], attributions = attrib.allocation, adjusted = FALSE)
#'
#' @export
Menchero <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Return.annualized.excess.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Return.annualized.excess.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Return.annualized.excess.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -38,7 +38,7 @@
#' @examples
#'
#' data(attrib)
-#' Return.annualized.excess(rp, rb)
+#' Return.annualized.excess(Rp = attrib.returns[, 21], Rb = attrib.returns[, 22])
#'
#' @export
Return.annualized.excess <-
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Return.level.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Return.level.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Return.level.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -1,23 +1,22 @@
-#' aggregates portfolio returns and weights up to the chosen level from the
-#' hierarchy
+#' aggregates portfolio returns up to the chosen level from the hierarchy
#'
#' Aggregates returns and weights up to the chosen level from the hierarchy.
-#' Hierarchy can be used from the \code{\link{buildHierarchy}} function or
-#' defined manually in the same way as the \code{\link{buildHierarchy}}'s
+#' Hierarchy can be used from the \code{buildHierarchy} function or
+#' defined manually in the same way as the \code{buildHierarchy}'s
#' output. If for the selected level the values in the hierarchy are numeric,
#' the aggregation of returns or weights is performed by quintiles.
#' \code{Weight.transform} makes transformation of weights to the xts object
#' conformable with returns.
#'
-#' @aliases Weight.transform Return.level Weight.level
+#' @aliases Return.level
#' @param Rp xts, data frame or matrix of portfolio returns
#' @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}} \cr \code{\link{Attribution}} \cr
-#' \code{\link{Return.portfolio}}
+#' @seealso \code{buildHierarchy} \cr \code{\link{Attribution}} \cr
+#' \code{\link{Weight.level}}
#' @references Christopherson, Jon A., Carino, David R., Ferson, Wayne E.
#' \emph{Portfolio Performance Measurement and Benchmarking}. McGraw-Hill.
#' 2009. Chapter 17
@@ -25,9 +24,7 @@
#' @examples
#'
#' data(attrib)
-#' Weight.transform(wp, Rp)
-#' Return.level(Rp, wp, h, level = "MarketCap")
-#' Weight.level(wp, h, level = "Sector")
+#' Return.level(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], h = attrib.hierarchy, level = "MarketCap")
#'
#' @export
Return.level <-
@@ -62,78 +59,4 @@
}
colnames(returns) = names(h)
return(returns)
-}
-
-Weight.level <-
-function(wp, h, level = "Sector")
-{ # @author Andrii Babii
-
- # DESCRIPTION:
- # Function to aggregate weights up to the chosen level from the hierarchy
-
- # Inputs:
- # 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 done
-
- # Outputs:
- # This function returns portfolio weights at the chosen level
-
- # FUNCTION:
- # Transform data to the xts objects
- wp = Weight.transform(wp, Rp)
-
- # If level has numeric values we replace numeric values by quintiles
- if (is.numeric(h[[level]])){
- h = HierarchyQuintiles(h, level)
- }
- h = split(h$primary_id, h[level])
- weights = wp[, 1:length(h)]
- for(i in 1:length(h)){
- weights[, i] = rowSums(wp[, h[[i]]])
- }
- colnames(weights) = names(h)
- return(weights)
-}
-
-Weight.transform <-
- function(wp, Rp)
- { # @author Andrii Babii
-
- # DESCRIPTION:
- # Function to transform weights to the xts object conformable with returns
- # used by aggregation and attribution functions
-
- # Inputs:
- # wp vector, xts, data frame or matrix of portfolio weights
- # Rp xts, data frame or matrix of portfolio returns
-
- # Outputs:
- # This function returns the xts object with weights conformable with
- # returns
-
- # FUNCTION:
- if (is.vector(wp)){
- wp = as.xts(matrix(rep(wp, nrow(Rp)), nrow(Rp), ncol(Rp), byrow = TRUE),
- index(Rp))
- colnames(wp) = colnames(Rp)
- } else{
- if(as.Date(last(index(Rp))) < (as.Date(index(wp[1, ])) + 1)){
- stop(paste('last date in series', as.Date(last(index(Rp))),
- 'occurs before beginning of first rebalancing period',
- as.Date(first(index(wp))) + 1))
- }
- wp = checkData(wp, method = "xts")
- wp = merge(wp, xts(, index(Rp)))
- wp = na.locf(wp)
- if(as.Date(first(index(Rp))) > (as.Date(index(wp[1,]))+1)) {
- warning(paste('data series starts on', as.Date(first(index(Rp))), ',
- which is after the first rebalancing period',
- as.Date(first(index(wp)))+1))
- wp = wp
- } else{
- wp = wp[2:nrow(wp)]
- }
- }
- return(wp)
}
\ No newline at end of file
Added: pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.level.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.level.R (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.level.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -0,0 +1,58 @@
+#' aggregates portfolio weights up to the chosen level from the hierarchy
+#'
+#' Aggregates weights up to the chosen level from the hierarchy.
+#' Hierarchy can be used from the \code{buildHierarchy} function or
+#' defined manually in the same way as the \code{buildHierarchy}'s
+#' output. If for the selected level the values in the hierarchy are numeric,
+#' the aggregation of returns or weights is performed by quintiles.
+#'
+#' @aliases 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 data.frame with portfolio hierarchy
+#' @param level level from the hierarchy to which returns and weights will be
+#' aggregated
+#' @author Andrii Babii
+#' @seealso \code{buildHierarchy} \cr \code{\link{Attribution}} \cr
+#' \code{\link{Return.level}}
+#' @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.level(wp = attrib.weights[1, ], Rp = attrib.returns[, 1:10], attrib.hierarchy, level = "Sector")
+#'
+#' @export
+Weight.level <-
+ function(wp, Rp, h, level = "Sector")
+ { # @author Andrii Babii
+
+ # DESCRIPTION:
+ # Function to aggregate weights up to the chosen level from the hierarchy
+
+ # Inputs:
+ # 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 done
+
+ # Outputs:
+ # This function returns portfolio weights at the chosen level
+
+ # FUNCTION:
+ # Transform data to the xts objects
+ wp = Weight.transform(wp, Rp)
+
+ # If level has numeric values we replace numeric values by quintiles
+ if (is.numeric(h[[level]])){
+ h = HierarchyQuintiles(h, level)
+ }
+ h = split(h$primary_id, h[level])
+ weights = wp[, 1:length(h)]
+ for(i in 1:length(h)){
+ weights[, i] = rowSums(wp[, h[[i]]])
+ }
+ colnames(weights) = names(h)
+ return(weights)
+}
\ No newline at end of file
Added: pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.transform.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.transform.R (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Weight.transform.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -0,0 +1,62 @@
+#' transforms weights for the attribution functions
+#'
+#' Makes transformation of weights to the xts object conformable with returns
+#' and to be taken by the attribution functions
+#'
+#' @aliases Weight.transform
+#' @param Rp xts, data frame or matrix of portfolio returns
+#' @param wp vector, xts, data frame or matrix of portfolio weights
+#' @author Andrii Babii
+#' @seealso \code{buildHierarchy} \cr \code{\link{Attribution}} \cr
+#' \code{\link{Weight.level}}
+#' @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 = attrib.weights[1, ], Rp = attrib.returns[, 1:10])
+#'
+#' @export
+Weight.transform <-
+ function(wp, Rp)
+ { # @author Andrii Babii
+
+ # DESCRIPTION:
+ # Function to transform weights to the xts object conformable with returns
+ # used by aggregation and attribution functions
+
+ # Inputs:
+ # wp vector, xts, data frame or matrix of portfolio weights
+ # Rp xts, data frame or matrix of portfolio returns
+
+ # Outputs:
+ # This function returns the xts object with weights conformable with
+ # returns
+
+ # FUNCTION:
+ if (is.vector(wp)){
+ wp = as.xts(matrix(rep(wp, nrow(Rp)), nrow(Rp), ncol(Rp), byrow = TRUE),
+ index(Rp))
+ colnames(wp) = colnames(Rp)
+ } else{
+ if(as.Date(last(index(Rp))) < (as.Date(index(wp[1, ])) + 1)){
+ stop(paste('last date in series', as.Date(last(index(Rp))),
+ 'occurs before beginning of first rebalancing period',
+ as.Date(first(index(wp))) + 1))
+ }
+ wp = checkData(wp, method = "xts")
+ wp = merge(wp, xts(, index(Rp)))
+ wp = na.locf(wp)
+ if(as.Date(first(index(Rp))) > (as.Date(index(wp[1,]))+1)) {
+ warning(paste('data series starts on', as.Date(first(index(Rp))), ',
+ which is after the first rebalancing period',
+ as.Date(first(index(wp)))+1))
+ wp = wp
+ } else{
+ wp = wp[2:nrow(wp)]
+ }
+ }
+ return(wp)
+}
\ No newline at end of file
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -153,7 +153,8 @@
#' @examples
#'
#' data(attrib)
-#' Attribution(Rp, wp, Rb, wb, method = "top.down", linking = "carino")
+#' Attribution(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+#' wb = attrib.weights[2, ], method = "top.down", linking = "carino")
#'
#' @export
Attribution <-
@@ -207,8 +208,10 @@
method = method[1]
linking = linking[1]
- currency = !(is.na(wpf)[1] & is.na(wbf)[1] & is.na(S)[1] & is.na(F)[1] &
- is.na(Rpl)[1] & is.na(Rbl)[1] & is.na(Rbh)[1])
+ currency = !(is.null(dim(wpf)) & is.null(dim(wbf)) &
+ is.null(dim(S)) & is.null(dim(F)) &
+ is.null(dim(Rpl)) & is.null(dim(Rpl)) &
+ is.null(dim(Rpl)))
if (geometric == FALSE & linking != "davies.laker"){
# The function makes all computations for the arithmetic attribution
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.levels.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.levels.R 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/attribution.levels.R 2012-07-18 20:17:30 UTC (rev 2176)
@@ -20,7 +20,7 @@
#' \deqn{^{d}w_{pi}\times\left(\frac{1+R_{pi}}{1+^{d}R_{bi}}-1\right)
#' \times\frac{1+^{d}R_{bi}}{1+^{d}bs}}
#'
-#' @aliases Attribution
+#' @aliases Attribution.levels
#' @param Rp xts, data frame or matrix of portfolio returns
#' @param wp vector, xts, data frame or matrix of portfolio weights
#' @param Rb xts, data frame or matrix of benchmark returns
@@ -28,6 +28,7 @@
#' @param h data.frame with the hierarchy obtained from the buildHierarchy
#' function or defined manually in the same style as buildHierarchy's
#' output
+#' @param \dots any other passthrough parameters
#' @return returns the list with geometric excess returns including annualized
#' geometric excess returns, total attribution effects (allocation, selection
#' and total) including total multi-period attribution effects, attribution
@@ -40,8 +41,10 @@
#' @examples
#'
#' data(attrib)
-#' Attribution.levels(Rp, wp, Rb, wb, h, c("type", "MarketCap", "Sector"))
-#' Attribution.levels(Rp, wp, Rb, wb, h, c("type", "Sector"))
+#' Attribution.levels(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+#' wb = attrib.weights[2, ], h = attrib.hierarchy, c("type", "MarketCap", "Sector"))
+#' Attribution.levels(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+#' wb = attrib.weights[2, ], h = attrib.hierarchy, c("type", "Sector"))
#'
#' @export
Attribution.levels <-
@@ -119,9 +122,9 @@
bs = list()
for(i in 1:length(levels)){
returns.p[[i]] = Return.level(Rp, WP, h, level = levels[i])
- weights.p[[i]] = Weight.level(WP, h, level = levels[i])
+ weights.p[[i]] = Weight.level(WP, Rp, h, level = levels[i])
returns.b[[i]] = Return.level(Rb, WB, h, level = levels[i])
- weights.b[[i]] = Weight.level(WB, h, level = levels[i])
+ weights.b[[i]] = Weight.level(WB, Rp, h, level = levels[i])
# semi-notional funds returns
bs[[i]] = reclass(rowSums(returns.b[[i]] * weights.p[[i]]), rp)
}
Deleted: pkg/PortfolioAnalytics/sandbox/attribution/data/attrib.RData
===================================================================
(Binary files differ)
Added: pkg/PortfolioAnalytics/sandbox/attribution/data/attrib.rda
===================================================================
(Binary files differ)
Property changes on: pkg/PortfolioAnalytics/sandbox/attribution/data/attrib.rda
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Added: pkg/PortfolioAnalytics/sandbox/attribution/man/AppraisalRatio.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/AppraisalRatio.Rd (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/AppraisalRatio.Rd 2012-07-18 20:17:30 UTC (rev 2176)
@@ -0,0 +1,53 @@
+\name{AppraisalRatio}
+\alias{AppraisalRatio}
+\title{Apraisal ratio = alpha / residual return}
+\usage{
+ AppraisalRatio(Ra, Rb, Rf = 0, ...)
+}
+\arguments{
+ \item{Ra}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of the asset returns}
+
+ \item{Rb}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of the benchmark asset return}
+
+ \item{Rf}{risk free rate, in same period as your returns}
+
+ \item{\dots}{any other passthrough parameters}
+}
+\description{
+ The alpha from CAPM divided by the standard deviation of
+ residuals
+}
+\details{
+ \deqn{A_{p}=\frac{\alpha_{p}}{\sigma(\varepsilon_{p})}}{Ap
+ = alphap / sigmap(epsilonp)}
+
+ Is appropriate for evaluating security selection ability.
+ It captures the idea that an active portfolio manager has
+ t odepart from the benchmark, that is, take on residual
+ risk, in order to produce alpha. The more alpha the
+ investor produces for a given amout of residual risk, the
+ higher the ratio.
+}
+\examples{
+data(managers)
+AppraisalRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12)
+AppraisalRatio(managers[80:120,1:6], managers[80:120,7,drop=FALSE], managers[80:120,10,drop=FALSE])
+AppraisalRatio(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE])
+}
+\author{
+ Andrii Babii
+}
+\references{
+ J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio
+ Performance Measurement and Benchmarking}. 2009.
+ McGraw-Hill, p. 101. \cr Jack Treynor and Fischer Black,
+ "How t oUse Security Analysis to Improve Portfolio
+ Selection," \emph{Journal of Business}, vol.46, no.1,
+ January 1973, pp. 66-86. \cr
+}
+\seealso{
+ \code{\link{InformationRatio}}
+}
+
Modified: pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.Rd 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.Rd 2012-07-18 20:17:30 UTC (rev 2176)
@@ -190,7 +190,8 @@
}
\examples{
data(attrib)
-Attribution(Rp, wp, Rb, wb, method = "top.down", linking = "carino")
+Attribution(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+wb = attrib.weights[2, ], method = "top.down", linking = "carino")
}
\author{
Andrii Babii
Modified: pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.geometric.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.geometric.Rd 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.geometric.Rd 2012-07-18 20:17:30 UTC (rev 2176)
@@ -2,8 +2,8 @@
\alias{Attribution.geometric}
\title{performs geometric attribution}
\usage{
- Attribution.geometric(Rp, wp, Rb, wb, Rpl, Rbl, Rbh,
- currency = FALSE)
+ Attribution.geometric(Rp, wp, Rb, wb, Rpl = NA, Rbl = NA,
+ Rbh = NA)
}
\arguments{
\item{Rp}{xts of portfolio returns}
@@ -82,7 +82,8 @@
}
\examples{
data(attrib)
-Attribution.geometric(Rp, wp, Rb, wb)
+Attribution.geometric(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ],
+Rb = attrib.returns[, 11:20], wb = attrib.weights[2, ])
}
\author{
Andrii Babii
Modified: pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.levels.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.levels.Rd 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/Attribution.levels.Rd 2012-07-18 20:17:30 UTC (rev 2176)
@@ -1,5 +1,4 @@
\name{Attribution.levels}
-\alias{Attribution}
\alias{Attribution.levels}
\title{provides multi-level geometric performance attribution}
\usage{
@@ -19,6 +18,8 @@
\item{h}{data.frame with the hierarchy obtained from the
buildHierarchy function or defined manually in the same
style as buildHierarchy's output}
+
+ \item{\dots}{any other passthrough parameters}
}
\value{
returns the list with geometric excess returns including
@@ -53,8 +54,10 @@
}
\examples{
data(attrib)
-Attribution.levels(Rp, wp, Rb, wb, h, c("type", "MarketCap", "Sector"))
-Attribution.levels(Rp, wp, Rb, wb, h, c("type", "Sector"))
+Attribution.levels(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+wb = attrib.weights[2, ], h = attrib.hierarchy, c("type", "MarketCap", "Sector"))
+Attribution.levels(Rp = attrib.returns[, 1:10], wp = attrib.weights[1, ], Rb = attrib.returns[, 11:20],
+wb = attrib.weights[2, ], h = attrib.hierarchy, c("type", "Sector"))
}
\author{
Andrii Babii
Modified: pkg/PortfolioAnalytics/sandbox/attribution/man/AttributionFixedIncome.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/AttributionFixedIncome.Rd 2012-07-18 15:43:15 UTC (rev 2175)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/AttributionFixedIncome.Rd 2012-07-18 20:17:30 UTC (rev 2176)
@@ -1,8 +1,5 @@
\name{AttributionFixedIncome}
-\alias{attribution}
\alias{AttributionFixedIncome}
-\alias{fixed}
-\alias{income}
\title{fixed income attribution}
\usage{
AttributionFixedIncome(Rp, wp, Rb, wb, Rf, Dp, Db, S,
@@ -39,9 +36,6 @@
\item{geometric}{- TRUE/FALSE for geometric/arithmetic
attribution}
-
- \item{wbf}{vector, xts, data frame or matrix with
- benchmark weights of currency forward contracts}
}
\value{
list with total excess returns decomposed into
@@ -93,8 +87,9 @@
}
\examples{
data(attrib)
-AttributionFixedIncome(Rp, wp, Rb, wb, Rf, Dp, Db, S, wbf,
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 2176
More information about the Returnanalytics-commits
mailing list