[Returnanalytics-commits] r2146 - pkg/PortfolioAnalytics/sandbox/attribution/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jul 12 11:15:49 CEST 2012
Author: ababii
Date: 2012-07-12 11:15:48 +0200 (Thu, 12 Jul 2012)
New Revision: 2146
Modified:
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/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
Log:
- documentaion update (inline equations are supported)
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Attribution.geometric.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -4,40 +4,47 @@
#' geometric attribution effects over multiple periods. Used internally by the
#' \code{\link{Attribution}} function. Geometric attribution effects in the
#' contrast with arithmetic do naturally link over time multiplicatively:
-#' \deqn{\frac{(1+R_{p})}{1+R_{b}}-1=\prod^{n}_{t=1}(1+A_{t}^{G})\times\prod^{n}_{t=1}(1+S{}_{t}^{G})-1}
-#' Total allocation effect at time t:
+#' \deqn{\frac{(1+R_{p})}{1+R_{b}}-1=\prod^{n}_{t=1}(1+A_{t}^{G})\times
+#' \prod^{n}_{t=1}(1+S{}_{t}^{G})-1}
+#' Total allocation effect at time \eqn{t}:
#' \deqn{A_{t}^{G}=\frac{1+b_{S}}{1+R_{bt}}-1}
-#' Total selection effect at time t:
+#' Total selection effect at time \eqn{t}:
#' \deqn{S_{t}^{G}=\frac{1+R_{pt}}{1+b_{S}}-1}
#' Semi-notional fund:
#' \deqn{b_{S}=\sum^{n}_{i=1}w_{pi}\times R_{bi}}
+#' \eqn{w_{pt}}{wpt} - portfolio weights at time \eqn{t},
+#' \eqn{w_{bt}}{wbt} - benchmark weights at time \eqn{t},
+#' \eqn{r_{t}}{rt} - portfolio returns at time \eqn{t},
+#' \eqn{b_{t}}{bt} - benchmark returns at time \eqn{t},
+#' \eqn{r} - total portfolio returns
+#' \eqn{b} - total benchmark returns
+#' \eqn{n} - number of periods
#'
#' The multi-currency geometric attribution is handled following the Appendix A
#' (Bacon, 2004).
#'
#' The individual selection effects are computed using:
-#' \deqn{w_{pi}\times\left(\frac{1+R_{pLi}}{1+R_{bLi}}-1\right)\times\left(\frac{1+R_{bLi}}{1+b_{SL}}\right)}
+#' \deqn{w_{pi}\times\left(\frac{1+R_{pLi}}{1+R_{bLi}}-1\right)\times
+#' \left(\frac{1+R_{bLi}}{1+b_{SL}}\right)}
#'
#' The individual allocation effects are computed using:
#' \deqn{(w_{pi}-w_{bi})\times\left(\frac{1+R_{bHi}}{1+b_{L}}-1\right)}
#'
#' Where the total semi-notional returns hedged into the base currency were
#' used:
-#' \deqn{b_{SH} = \sum_{i}w_{pi}\times R_{bi}((w_{pi} - w_{bi})R_{bHi} + w_{bi}R_{bLi})}
+#' \deqn{b_{SH} = \sum_{i}w_{pi}\times R_{bi}((w_{pi} - w_{bi})R_{bHi} +
+#' w_{bi}R_{bLi})}
#' Total semi-notional returns in the local currency:
#' \deqn{b_{SL} = \sum_{i}w_{pi}R_{bLi}}
-#' Portfolio returns in the local currency:
-#' \deqn{R_{pLi}}
-#' Benchmark returns in the local currency:
-#' \deqn{R_{bLi}}
-#' Benchmark returns hedged into the base currency:
-#' \deqn{R_{bHi}}
-#' Total benchmark returns in the local currency:
-#' \deqn{b_{L}}
-#' Total portfolio returns in the local currency:
-#' \deqn{r_{L}}
+#' \eqn{R_{pLi}}{RpLi} - portfolio returns in the local currency
+#' \eqn{R_{bLi}}{RbLi} - benchmark returns in the local currency
+#' \eqn{R_{bHi}}{RbHi} - benchmark returns hedged into the base currency
+#' \eqn{b_{L}}{bL} - total benchmark returns in the local currency
+#' \eqn{r_{L}}{rL} - total portfolio returns in the local currency
#' The total excess returns are decomposed into:
-#' \deqn{\frac{(1+R_{p})}{1+R_{b}}-1=\frac{1+r_{L}}{1+b_{SL}}\times\frac{1+b_{SH}}{1+b_{L}}\times\frac{1+b_{SL}}{1+b_{SH}}\times\frac{1+R_{p}}{1+r_{L}}\times\frac{1+b_{L}}{1+R_{b}}-1}
+#' \deqn{\frac{(1+R_{p})}{1+R_{b}}-1=\frac{1+r_{L}}{1+b_{SL}}\times\frac{1+
+#' b_{SH}}{1+b_{L}}\times\frac{1+b_{SL}}{1+b_{SH}}\times\frac{1+R_{p}}{1+r_{L}}
+#' \times\frac{1+b_{L}}{1+R_{b}}-1}
#'
#' where the first term corresponds to the selection, second to the allocation,
#' third to the hedging cost transferred and the last two to the naive currency
@@ -101,9 +108,11 @@
names(rp) = "Total"
names(rb) = "Total"
- bs = reclass(rowSums((wp * coredata(Rb[, 1:ncol(wp)]))), rp) # Allocation notional fund returns
+ # Allocation notional fund returns
+ bs = reclass(rowSums((wp * coredata(Rb[, 1:ncol(wp)]))), rp)
if (!currency){
- allocation = ((1 + Rb) / (1 + rep(rb, ncol(Rp))) - 1) * coredata(wp - wb) # Geometric attribution effects for individual categories
+ # 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)))
} else{
@@ -116,7 +125,8 @@
rpl = reclass(rowSums(Rpl * wp), Rpl)
rbl = reclass(rowSums(Rbl * wp), Rpl)
allocation = (wp - wb) * ((1 + Rbh) / (1 + rep(rbl, ncol(Rbh))) - 1)
- selection = wp * ((1 + Rpl) / (1 + Rbl) - 1) * ((1 + Rbl) / (1 + rep(bsl, ncol(Rbl))))
+ selection = wp * ((1 + Rpl) / (1 + Rbl) - 1) * ((1 + Rbl) /
+ (1 + rep(bsl, ncol(Rbl))))
hedge = (1 + bsl) / (1 + bsh) - 1
currency.attr = (1 + rp) * (1 + rbl) / (1 + rpl) / (1 + rb) - 1
curr = cbind(hedge, currency.attr)
@@ -132,8 +142,10 @@
# Link single-period attribution effects
a = (apply(1 + allocation[, ncol(allocation)], 2, prod) - 1)
s = (apply(1 + selection[, ncol(selection)], 2, prod) - 1)
- allocation = rbind(as.data.frame(allocation), c(rep(NA, ncol(allocation) - 1), a))
- selection = rbind(as.data.frame(selection), c(rep(NA, ncol(selection) - 1), s))
+ allocation = rbind(as.data.frame(allocation),
+ c(rep(NA, ncol(allocation) - 1), a))
+ selection = rbind(as.data.frame(selection),
+ c(rep(NA, ncol(selection) - 1), s))
rownames(allocation)[nrow(allocation)] = "Total"
rownames(selection)[nrow(selection)] = "Total"
@@ -153,7 +165,8 @@
names(result) = c("Excess returns", "Allocation", "Selection")
} else{
result[[4]] = curr
- names(result) = c("Excess returns", "Allocation", "Selection", "Currency management")
+ names(result) = c("Excess returns", "Allocation", "Selection",
+ "Currency management")
}
return(result)
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/AttributionFixedIncome.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -10,27 +10,36 @@
#' is, in fact, a specialist form of risk-adjusted attribution.
#' The arithmetic attribution is handled using weighted duration approach
#' (Van Breukelen, 2000). The allocation, selection and currency allocation
-#' effects for category i are:
-#' \deqn{A_{i} = (D_{pi}\times w_{pi}-D_{\beta}\times D_{bi}\times w_{pi})\times (-\Delta y_{bi} + \Delta y_{b})}
+#' effects for category \eqn{i} are:
+#' \deqn{A_{i} = (D_{pi}\times w_{pi}-D_{\beta}\times D_{bi}\times w_{pi})
+#' \times (-\Delta y_{bi} + \Delta y_{b})}
#' \deqn{S_{i} = D_{i}\times w_{pi}\times (-\Delta y_{ri} + \Delta y_{bi})}
-#' \deqn{C_{i} = (w_{pi} - w_{bi})\times (c_{i} + R_{fi} - c')}
-#' where w_pi - portfolio weights, w_bi - benchmark weights, D_i - modified
-#' duration in bond category i.
+#' \deqn{C_{i} = (w_{pi} - w_{bi})\times (c_{i} + R_{fi} - c')}{Ci =
+#' (wpi - wbi) * (ci + Rfi - c')}
+#' where \eqn{w_{pi}}{wpi} - portfolio weights,
+#' \eqn{w_{bi}}{wbi} - benchmark weights,
+#' \eqn{D_{i}}{Di} - modified duration in bond category \eqn{i}.
#' Duration beta:
-#' \deqn{D_{\beta}=\frac{D_{r}}{D_{b}}}
-#' D_r - portfolio duration, D_b - benchmark duration, D_bi - benchmark
-#' duration for category i, D_pi - portfolio duration for category i,
-#' Delta y_ri - change in portfolio yield for category i,
-#' Delta y_bi - change in benchmark yield for category i,
-#' Delta y_b - change in benchmark yield,
-#' R_ci- currency returns for category i,
-#' R_fi - risk-free rate in currency of asset i,
+#' \deqn{D_{\beta}=\frac{D_{r}}{D_{b}}}{Dbeta = Dr / Db}
+#' \eqn{D_{r}}{Dr} - portfolio duration,
+#' \eqn{D_{b}}{Db} - benchmark duration,
+#' \eqn{D_{bi}}{Dbi} - benchmark duration for category \eqn{i},
+#' \eqn{D_{pi}}{Dpi} - portfolio duration for category \eqn{i},
+#' \eqn{\Delta y_{ri}}{Delta yri} - change in portfolio yield
+#' for category \eqn{i},
+#' \eqn{\Delta y_{bi}}{Delta ybi} - change in benchmark yield
+#' for category \eqn{i},
+#' \eqn{\Delta y_{b}}{Delta yb} - change in benchmark yield,
+#' \eqn{R_{ci}}{Rci} - currency returns for category \eqn{i},
+#' \eqn{R_{fi}}{Rfi} - risk-free rate in currency of asset \eqn{i},
#' \deqn{c'= \sum_{i}w_{bi}\times(R_{ci}+R_{fi})}
#' The geometric attribution is adapted using Van Breukelen (2000) approach for
#' the arithmetic attribution. The individual allocation and selection effects
#' are computed as follows:
-#' \deqn{A_{i}=D_{i}w_{pi}-D_{\beta}D_{bi}w_{bi}}
-#' \deqn{S_{i}=\frac{D_{pi}}{D_{bi}}\times (R_{bi} - R_{fi}) + R_{fi}}
+#' \deqn{A_{i}=D_{i}w_{pi}-D_{\beta}D_{bi}w_{bi}}{Ai =
+#' 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
#' @param Rp T x n xts, data frame or matrix of portfolio returns
#' @param wp vector, xts, data frame or matrix of portfolio weights
@@ -41,23 +50,26 @@
#' @param Db T x n xts, data frame or matrix with benchmark modified duration
#' @param wbf vector, xts, data frame or matrix with benchmark weights of
#' currency forward contracts
-#' @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 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
-#' @seealso \code{\link{Attribution.levels}}, \code{\link{Attribution.geometric}}
+#' @seealso \code{\link{Attribution.levels}},
+#' \code{\link{Attribution.geometric}}
#' @references Bacon, C. \emph{Practical Portfolio Performance Measurement
#' and Attribution}. Wiley. 2004. Chapter 7 \cr Van Breukelen, G. \emph{Fixed
-#' income attribution}. Journal of Performance Measurement. Sumer. p. 61-68. 2000 \cr
+#' income attribution}. Journal of Performance Measurement. Sumer.
+#' p. 61-68. 2000 \cr
#' @keywords attribution
#' @examples
#'
#' data(attrib)
-#' AttributionFixedIncome(Rp, wp, Rb, wb, Rf, Dp, Db, S, wbf, geometric = FALSE)
+#' AttributionFixedIncome(Rp, wp, Rb, wb, Rf, Dp, Db, S, wbf,
+#' geometric = FALSE)
#'
#' @export
AttributionFixedIncome <-
@@ -112,20 +124,27 @@
names(rp) = "Total"
names(rb) = "Total"
Dbeta = dp / coredata(db)
- DeltaYb = -(Rb - coredata(Rf)) / coredata(Db) # Implied benchmark yield changes
- DeltaYp = -(Rp - coredata(Rf)) / coredata(Dp) # Implied portfolio yield changes
- deltayb = rep(rb - coredata(rp), ncol(Dp)) / coredata(Dp) # Implied total benchmark yield changes
- Rc = lag(S, -1)[1:nrow(Rp), ] / S[1:nrow(Rp), ] - 1 # Currency returns
+ # Implied benchmark yield changes
+ DeltaYb = -(Rb - coredata(Rf)) / coredata(Db)
+ # Implied portfolio yield changes
+ DeltaYp = -(Rp - coredata(Rf)) / coredata(Dp)
+ # Implied total benchmark yield changes
+ deltayb = rep(rb - coredata(rp), ncol(Dp)) / coredata(Dp)
+ # Currency returns
+ Rc = lag(S, -1)[1:nrow(Rp), ] / S[1:nrow(Rp), ] - 1
rc = reclass(rowSums((wb + wbf) * (Rc + coredata(Rf))), Rc)
if (!geometric){
- allocation = (Dp * wp - rep(Dbeta, ncol(Dp)) * coredata(Db) * wb) * coredata(-DeltaYb + deltayb)
+ allocation = (Dp * wp - rep(Dbeta, ncol(Dp)) * coredata(Db) * wb) *
+ coredata(-DeltaYb + deltayb)
selection = Dp * coredata(wp) * coredata(-DeltaYp + coredata(DeltaYb))
currency = (wp - wb) * (Rc + coredata(Rf) - rep(rc, ncol(Rc)))
excess.returns = rp - coredata(rb)
} else{
rcprime = rowSums(wb * (Rc + Rf))
- bd = reclass(rowSums(rep(Dbeta, ncol(Db)) * Db * coredata(wb) * coredata(-DeltaYb)), Db) + rcprime # Overal duration notional fund
- allocation = Dp * wp - rep(Dbeta, ncol(Dp)) * coredata(Db) * wb * coredata(-DeltaYb + deltayb) / rep(bd, ncol(Db))
+ bd = reclass(rowSums(rep(Dbeta, ncol(Db)) * Db * coredata(wb) *
+ coredata(-DeltaYb)), Db) + rcprime # Overal duration notional fund
+ allocation = Dp * wp - rep(Dbeta, ncol(Dp)) * coredata(Db) * wb *
+ coredata(-DeltaYb + deltayb) / rep(bd, ncol(Db))
selection = Dp / coredata(Db) * coredata(Rb - coredata(Rf)) + Rf
excess.returns = (1 + rp) / (1 + coredata(rb)) - 1
}
@@ -147,7 +166,8 @@
currency = cbind(currency, rowSums(currency))
names(currency)[ncol(currency)] = "Total"
result[[4]] = currency
- names(result) = c("Excess returns", "Market allocation", "Issue selection", "Currency allocation")
+ names(result) = c("Excess returns", "Market allocation",
+ "Issue selection", "Currency allocation")
}
return(result)
}
\ No newline at end of file
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Carino.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -1,23 +1,28 @@
#' calculates total attribution effects using logarithmic smoothing
#'
#' Calculates total attribution effects over multiple periods using
-#' logarithmic linking method. Used internally by the \code{\link{Attribution}}
+#' logarithmic linking method. Used internally by the \code{\link{Attribution}}
#' function. Arithmetic attribution effects do not naturally link over time.
#' This function uses logarithmic smoothing to adjust attribution effects
#' so that they can be summed up over multiple periods. Attribution effect
#' are multiplied by the adjustment factor:
-#' \deqn{A_{t}' = A_{t} \times \frac{k_{t}}{k}}
-#' where \deqn{k_{t} = \frac{log(1 + R_{pt}) - log(1 + R_{bt})}{R_{pt} - R_{bt}}}
+#' \deqn{A_{t}' = A_{t} \times \frac{k_{t}}{k}}{At' = At * kt / k}
+#' where \deqn{k_{t} = \frac{log(1 + R_{pt}) -
+#' log(1 + R_{bt})}{R_{pt} - R_{bt}}}
#' \deqn{k = \frac{log(1 + R_{p}) - log(1 + R_{b})}{R_{p} - R_{b}}}
#' In case if portfolio and benchmark returns are equal:
-#' \deqn{k_{t} = \frac{1}{1 + R_{pt}}}
-#' where A_t' - adjusted attribution effects at period t, A_t - unadjusted
-#' attribution effects at period t, R_pt - portfolio returns at period t,
-#' R_bt - benchmark returns at period t, Rp - total portfolio returns,
-#' Rb - total benchmark returns, n - number of periods
+#' \deqn{k_{t} = \frac{1}{1 + R_{pt}}}{kt = 1 / (1 + Rpt)}
+#' where \eqn{A_{t}'}{At'} - adjusted attribution effects at period \eqn{t},
+#' \eqn{A_{t}}{At} - unadjusted attribution effects at period \eqn{t},
+#' \eqn{R_{pt}}{Rpt} - portfolio returns at period \eqn{t},
+#' \eqn{R_{bt}}{Rbt} - benchmark returns at period \eqn{t},
+#' \eqn{R_{p}}{Rp} - total portfolio returns,
+#' \eqn{R_{b}}{Rb} - total benchmark returns,
+#' \eqn{n} - number of periods
#' The total arithmetic excess returns can be explained in terms of the sum
#' of adjusted attribution effects:
-#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+Interaction_{t}\right)}
+#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+
+#' Interaction_{t}\right)}
#'
#' @aliases Carino
#' @param rp xts of portfolio returns
@@ -55,7 +60,7 @@
# Inputs:
# rp xts of portfolio returns
# rb xts of benchmark returns
- # attributions attribution effects (e.g. allocation, selection, interaction)
+ # attributions attribution effects (allocation, selection, interaction)
# Outputs:
# This function returns the data.frame with original attribution effects
@@ -68,12 +73,15 @@
kt = rp
for (t in 1:nrow(kt)){
if (rp[[t]] == rb[[t]]){
- kt[[t]] = 1 / (1 + rp[[t]]) # Carino factors if portfolio and benchmark returns are equal
+ # Carino factors if portfolio and benchmarkreturns are equal
+ kt[[t]] = 1 / (1 + rp[[t]])
} else{
- kt[[t]] = (log(1 + rp[[t]]) - log(1 + rb[[t]])) / (rp[[t]] - rb[[t]]) # if different
+ # if different
+ kt[[t]] = (log(1 + rp[[t]]) - log(1 + rb[[t]])) / (rp[[t]] - rb[[t]])
}
}
- kt = matrix(rep(kt, ncol(attributions)), nrow(attributions), ncol(attributions), byrow = FALSE)
+ kt = matrix(rep(kt, ncol(attributions)), nrow(attributions),
+ ncol(attributions), byrow = FALSE)
adj = attributions * kt / k
total = colSums(adj)
if (adjusted == FALSE){
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Conv.option.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -23,9 +23,11 @@
#' @keywords derivatives attribution, option attribution
#' @examples
#'
-#' option = matrix(c(1000, 1000, 1000, 300, 400, 10, 20, 30, 40, 50, 10, 11, 12, 13, 14, 12, 13,
-#' 14, 15, 16, 0.1, 0.2, 0.3, 0.4, 0.5, 0.1, 0.1, 0.2, 0.2, 0.3), 5, 6)
-#' colnames(option) = c("Strike", "Number", "Current option", "End option", "delta", "returns")
+#' option = matrix(c(1000, 1000, 1000, 300, 400, 10, 20, 30, 40, 50, 10, 11,
+#' 12, 13, 14, 12, 13, 14, 15, 16, 0.1, 0.2, 0.3, 0.4, 0.5, 0.1, 0.1, 0.2,
+#' 0.2, 0.3), 5, 6)
+#' colnames(option) = c("Strike", "Number", "Current option", "End option",
+#' "delta", "returns")
#' rownames(option) = c("CVX", "XOM", "GE", "WMT", "FB")
#' Conv.option(option)
#'
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/DaviesLaker.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -6,13 +6,17 @@
#' naturally link over time. This function uses Davies and Laker linking method
#' to compute total attribution effects.
#' Arithmetic excess returns are decomposed as follows:
-#' \deqn{R_{p} - R_{b} = Allocation + Selection + Interaction}
+#' \deqn{R_{p} - R_{b} = Allocation + Selection + Interaction}{Rp - Rb =
+#' Allocation + Selection + Interaction}
#' \deqn{Allocation = \prod^{T}_{t=1}(1+bs_{t})-\prod^{T}_{t=1}(1+R_{bt})}
#' \deqn{Selection = \prod^{T}_{t=1}(1+rs_{t})-\prod^{T}_{t=1}(1+R_{bt})}
-#' \deqn{Interaction = \prod^{T}_{t=1}(1+R_{pt})-\prod^{T}_{t=1}(1+rs_{t})-\prod^{T}_{t=1}(1+bs_{t})+\prod^{T}_{t=1}(1+R_{bt})}
-#' R_pi - portfolio returns at period i, Rb_i - benchmark returns at period i,
-#' rs_i - selection notional fund returns at period i, bs_i - allocation
-#' notional fund returns at period i, T - number of periods
+#' \deqn{Interaction = \prod^{T}_{t=1}(1+R_{pt})-\prod^{T}_{t=1}(1+rs_{t})-
+#' \prod^{T}_{t=1}(1+bs_{t})+\prod^{T}_{t=1}(1+R_{bt})}
+#' \eqn{R_{pi}}{Rpi} - portfolio returns at period \eqn{i},
+#' \eqn{R_{bi}}{Rbi} - benchmark returns at period \eqn{i},
+#' \eqn{rs_{i}}{rsi} - selection notional fund returns at period \eqn{i},
+#' \eqn{bs_{i}}{bsi} - allocation notional fund returns at period \eqn{i},
+#' \eqn{T} - number of periods
#'
#' @aliases DaviesLaker
#' @param Rp xts of portfolio returns
@@ -25,7 +29,7 @@
#' @seealso \code{\link{Attribution}} \cr \code{\link{Menchero}} \cr
#' \code{\link{Grap}} \cr \code{\link{Carino}} \cr
#' \code{\link{Attribution.geometric}} \cr \code{\link{Frongello}}
-#' @references Bacon, C. \emph{Practical Portfolio Performance Measurement and
+#' @references Bacon, C. \emph{Practical Portfolio Performance Measurement and
#' Attribution}. Wiley. 2004. p. 201-204 \cr Davies, O. and Laker, D. (2001)
#' \emph{Multiple-period performance attribution using the Brinson model}.
#' Journal of Performance Measurement. Fall. p. 12-22 \cr
@@ -68,11 +72,14 @@
}
colnames(rp) = "Total"
colnames(rb) = "Total"
- bs = reclass(rowSums((wp * coredata(Rb[, 1:ncol(wp)]))), Rp) # Allocation notional fund returns
- rs = reclass(rowSums((wb * coredata(Rp[, 1:ncol(wb)]))), Rp) # Selection notional fund returns
+ # Allocation notional fund returns
+ bs = reclass(rowSums((wp * coredata(Rb[, 1:ncol(wp)]))), Rp)
+ # Selection notional fund returns
+ rs = reclass(rowSums((wb * coredata(Rp[, 1:ncol(wb)]))), Rp)
a = apply(1 + bs, 2, prod) - apply(1 + rb, 2, prod)
s = apply(1 + rs, 2, prod) - apply(1 + rb, 2, prod)
- i = apply(1 + rp, 2, prod) - apply(1 + rs, 2, prod) - apply(1 + bs, 2, prod) + apply(1 + rb, 2, prod)
+ i = apply(1 + rp, 2, prod) - apply(1 + rs, 2, prod) -
+ apply(1 + bs, 2, prod) + apply(1 + rb, 2, prod)
# Compute attribution effects (Brinson, Hood and Beebower model)
allocation = (wp - wb) * coredata(Rb)
@@ -86,9 +93,12 @@
interaction = cbind(interaction, rowSums(interaction))
names(interaction)[n + 1] = "Total"
- allocation = rbind(as.data.frame(allocation), c(rep(NA, ncol(allocation) - 1), a))
- selection = rbind(as.data.frame(selection), c(rep(NA, ncol(selection) - 1), s))
- interaction = rbind(as.data.frame(interaction), c(rep(NA, ncol(interaction) - 1), i))
+ allocation = rbind(as.data.frame(allocation),
+ c(rep(NA, ncol(allocation) - 1), a))
+ selection = rbind(as.data.frame(selection),
+ c(rep(NA, ncol(selection) - 1), s))
+ interaction = rbind(as.data.frame(interaction),
+ c(rep(NA, ncol(interaction) - 1), i))
rownames(allocation)[nrow(allocation)] = "Total"
rownames(selection)[nrow(selection)] = "Total"
rownames(interaction)[nrow(allocation)] = "Total"
@@ -106,6 +116,7 @@
result[[2]] = allocation
result[[3]] = selection
result[[4]] = interaction
- names(result) = c("Excess returns", "Allocation", "Selection", "Interaction")
+ names(result) = c("Excess returns", "Allocation", "Selection",
+ "Interaction")
return(result)
}
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Frongello.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -1,16 +1,20 @@
#' calculates total attribution effects using Frongello smoothing
#'
#' Calculates total attribution effects over multiple periods using
-#' Frongello linking method. Used internally by the \code{\link{Attribution}}
+#' Frongello linking method. Used internally by the \code{\link{Attribution}}
#' function. Arithmetic attribution effects do not naturally link over time.
#' This function uses Frongello smoothing algorithm to adjust
#' attribution effects so that they can be summed up over multiple periods
#' Adjusted attribution effect at period t are:
-#' \deqn{A_{t}' = A_{t}\times\prod^{t-1}_{i=1}(1+r_{pi})+R_{bt}\times\sum^{t-1}_{i=1}A_{i}'}
-#' A_t' - adjusted attribution effects at period t, A_t - unadjusted
-#' attribution effects at period t, R_pi - portfolio returns at period i,
-#' R_bi - benchmark returns at period , Rp - total portfolio returns,
-#' Rb - total benchmark returns, n - number of periods
+#' \deqn{A_{t}' = A_{t}\times\prod^{t-1}_{i=1}(1+r_{pi})+R_{bt}
+#' \times\sum^{t-1}_{i=1}A_{i}'}
+#' \eqn{A_{t}'}{At'} - adjusted attribution effects at period \eqn{t},
+#' \eqn{A_{t}}{At} - unadjusted attribution effects at period \eqn{t},
+#' \eqn{R_{pi}}{Rpi} - portfolio returns at period \eqn{i},
+#' \eqn{R_{bi}}{Rbi} - benchmark returns at period,
+#' \eqn{R_{p}}{Rp} - total portfolio returns,
+#' \eqn{R_{b}}{Rb} - total benchmark returns,
+#' \eqn{n} - number of periods
#'
#' @aliases Frongello
#' @param rp xts of portfolio returns
@@ -55,11 +59,13 @@
# FUNCTION:
adj = attributions
if (nrow(rp) > 1){
- adj[2, ] = coredata(adj[2, ]) * drop((1 + rp[1, 1])) + drop(rb[2, 1]) * coredata(adj[1, ])
+ adj[2, ] = coredata(adj[2, ]) * drop((1 + rp[1, 1])) + drop(rb[2, 1]) *
+ coredata(adj[1, ])
}
if (nrow(rp) > 2){
for(i in 3:nrow(rp)){
- adj[i, ] = coredata(adj[i, ]) * drop(prod(1 + rp[1:(i-1), 1])) + drop(rb[i, ]) * coredata(colSums(adj[1:(i-1), ]))
+ adj[i, ] = coredata(adj[i, ]) * drop(prod(1 + rp[1:(i-1), 1])) +
+ drop(rb[i, ]) * coredata(colSums(adj[1:(i-1), ]))
}
}
total = colSums(adj)
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Grap.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -6,15 +6,19 @@
#' This function uses GRAP smoothing algorithm to adjust attribution effects
#' so that they can be summed up over multiple periods
#' Attribution effect are multiplied by the adjustment factor
-#' \deqn{A_{t}' = A_{t} \times G_{t}} where
+#' \deqn{A_{t}' = A_{t} \times G_{t}}{At' = At * Gt} where
#' \deqn{G_{t}=\prod^{t-1}_{i=1}(1+R_{pi})\times\prod^{n}_{t+1}(1+R_{bi})}
-#' A_t' - adjusted attribution effects at period t, A_t - unadjusted
-#' attribution effects at period t, R_pi - portfolio returns at period i,
-#' R_bi - benchmark returns at period i, Rp - total portfolio returns,
-#' Rb - total benchmark returns, n - number of periods
+#' \eqn{A_{t}'}{At'} - adjusted attribution effects at period \eqn{t},
+#' \eqn{A_{t}}{At} - unadjusted attribution effects at period \eqn{t},
+#' \eqn{R_{pi}}{Rpi} - portfolio returns at period \eqn{i},
+#' \eqn{R_{bi}}{Rbi} - benchmark returns at period \eqn{i},
+#' \eqn{R_{p}}{Rp} - total portfolio returns,
+#' \eqn{R_{b}}{Rb} - total benchmark returns,
+#' \eqn{n} - number of periods
#' The total arithmetic excess returns can be explained in terms of the sum
#' of adjusted attribution effects:
-#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+Interaction_{t}\right)}
+#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+
+#' Interaction_{t}\right)}
#'
#' @aliases Grap
#' @param rp xts of portfolio returns
@@ -51,7 +55,7 @@
# Inputs:
# rp xts of portfolio returns
# rb xts of benchmark returns
- # attributions attribution effects (e.g. allocation, selection, interaction)
+ # attributions attribution effects (allocation, selection, interaction)
# Outputs:
# This function returns the data.frame with original attribution effects
@@ -74,7 +78,8 @@
G[i] = apply(r, 2, prod) * apply(b, 2, prod)
}
}
- g = matrix(rep(G, ncol(attributions)), nrow(attributions), ncol(attributions), byrow = FALSE)
+ g = matrix(rep(G, ncol(attributions)), nrow(attributions),
+ ncol(attributions), byrow = FALSE)
adj = attributions * g
total = colSums(adj)
if (adjusted == FALSE){
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/HierarchyQuintiles.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -22,19 +22,24 @@
hnew = h[[level]]
quintiles = quantile(h[[level]], c(0, 0.2, 0.4, 0.6, 0.8, 1), na.rm = TRUE)
for (i in 1:length(h[[level]])){
- if (h[[level]][i] >= quintiles[1] & h[[level]][i] < quintiles[2] & !is.na(h[[level]][i])){
+ if (h[[level]][i] >= quintiles[1] & h[[level]][i] < quintiles[2]
+ & !is.na(h[[level]][i])){
hnew[i] = "Quintile 1"
}
- if (h[[level]][i] >= quintiles[2] & h[[level]][i] < quintiles[3] & !is.na(h[[level]][i])){
+ if (h[[level]][i] >= quintiles[2] & h[[level]][i] < quintiles[3]
+ & !is.na(h[[level]][i])){
hnew[i] = "Quintile 2"
}
- if (h[[level]][i] >= quintiles[3] & h[[level]][i] < quintiles[4] & !is.na(h[[level]][i])){
+ if (h[[level]][i] >= quintiles[3] & h[[level]][i] < quintiles[4]
+ & !is.na(h[[level]][i])){
hnew[i] = "Quintile 3"
}
- if (h[[level]][i] >= quintiles[4] & h[[level]][i] < quintiles[5] & !is.na(h[[level]][i])){
+ if (h[[level]][i] >= quintiles[4] & h[[level]][i] < quintiles[5]
+ & !is.na(h[[level]][i])){
hnew[i] = "Quintile 4"
}
- if (h[[level]][i] >= quintiles[5] & h[[level]][i] <= quintiles[6] & !is.na(h[[level]][i])){
+ if (h[[level]][i] >= quintiles[5] & h[[level]][i] <= quintiles[6]
+ & !is.na(h[[level]][i])){
hnew[i] = "Quintile 5"
}
}
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R 2012-07-11 17:56:29 UTC (rev 2145)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/Menchero.R 2012-07-12 09:15:48 UTC (rev 2146)
@@ -1,25 +1,31 @@
#' calculates total attribution effects using Menchero smoothing
#'
#' Calculates total attribution effects over multiple periods using
-#' Menchero linking method. Used internally by the \code{\link{Attribution}}
+#' Menchero linking method. Used internally by the \code{\link{Attribution}}
#' function. Arithmetic attribution effects do not naturally link over time.
#' This function uses Menchero smoothing algorithm to adjust
#' attribution effects so that they can be summed up over multiple periods
#' Attribution effect are multiplied by the adjustment factor
-#' \deqn{A_{t}' = A_{t} \times (M +a_{t})}
-#' where \deqn{M=\frac{\frac{1}{n}(R_{p}-R_{b})}{(1+R_{p})^{\frac{1}{n}}-(1+R_{b})^{\frac{1}{n}}}}
-#' \deqn{a_{t} = \left(\frac{R_{p}-R_{b}-M\times\sum^{n}_{t=1}(R_{pt}-R_{bt})}{\sum^{n}_{t=1}(R_{pt}-R_{bt})^{2}}\right)\times(R_{pt}-R_{bt})}
+#' \deqn{A_{t}' = A_{t} \times (M +a_{t})}{At' = At * (M + at)}
+#' where \deqn{M=\frac{\frac{1}{n}(R_{p}-R_{b})}{(1+R_{p})^{\frac{1}{n}}-
+#' (1+R_{b})^{\frac{1}{n}}}}
+#' \deqn{a_{t} = \left(\frac{R_{p}-R_{b}-M\times\sum^{n}_{t=1}(R_{pt}-
+#' R_{bt})}{\sum^{n}_{t=1}(R_{pt}-R_{bt})^{2}}\right)\times(R_{pt}-R_{bt})}
#' In case if portfolio and benchmark returns are equal the limit of the
#' above value is used:
-#' \deqn{M = (1 + r_{p})^\frac{n-1}{n}}
-#' A_t' - adjusted attribution effects at period t, A_t - unadjusted
-#' attribution effects at period t, R_pt - portfolio returns at period t,
-#' R_bt - benchmark returns at period t, Rp - total portfolio returns,
-#' Rb - total benchmark returns, n - number of periods
+#' \deqn{M = (1 + r_{p})^\frac{n-1}{n}}{M = (1 + rp)^((n - 1) / n)}
+#' \eqn{A_{t}'}{At'} - adjusted attribution effects at period \eqn{t},
+#' \eqn{A_{t}}{At} - unadjusted attribution effects at period \eqn{t},
+#' \eqn{R_{pt}}{Rpt} - portfolio returns at period \eqn{t},
+#' \eqn{R_{bt}}{Rbt} - benchmark returns at period \eqn{t},
+#' \eqn{R_{p}}{Rp} - total portfolio returns,
+#' \eqn{R_{b}}{Rb} - total benchmark returns,
+#' \eqn{n} - number of periods
#'
#' The total arithmetic excess returns can be explained in terms of the sum
#' of adjusted attribution effects:
-#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+Interaction_{t}\right)}
+#' \deqn{R_{p} - R_{b} = \sum^{n}_{t=1}\left(Allocation_{t}+Selection_{t}+
+#' Interaction_{t}\right)}
#'
#' @aliases Menchero
#' @param rp xts of portfolio returns
@@ -55,7 +61,7 @@
# Inputs:
# rp xts of portfolio returns
# rb xts of benchmark returns
- # attributions attribution effects (e.g. allocation, selection, interaction)
+ # attributions attribution effects (allocation, selection, interaction)
# Outputs:
# This function returns the data.frame with original attribution effects
@@ -70,9 +76,11 @@
at = 0
} else{
M = ((rpc - rbc) / T) / ((1 + rpc)^(1 / T) - (1 + rbc)^(1 / T))
- at = (rpc - rbc - M * sum(rp - rb)) * (rp - coredata(rb)) / sum((rp - coredata(rb))^2)
+ at = (rpc - rbc - M * sum(rp - rb)) * (rp - coredata(rb)) / sum((rp -
+ coredata(rb))^2)
}
- m = matrix(rep(M + at, ncol(attributions)), nrow(attributions), ncol(attributions), byrow = FALSE)
+ m = matrix(rep(M + at, ncol(attributions)), nrow(attributions),
+ ncol(attributions), byrow = FALSE)
adj = attributions * m
total = colSums(adj)
if (adjusted == FALSE){
Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/Return.annualized.excess.R
===================================================================
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 2146
More information about the Returnanalytics-commits
mailing list