%% \VignetteIndexEntry{Return contribution}
\documentclass[a4paper,11pt]{article}
\usepackage[left = 3cm, top = 2cm, bottom = 2cm, right = 4cm]{geometry}
\usepackage[noae,nogin]{Sweave}
\usepackage{libertine}
\usepackage[scaled=0.9]{inconsolata}
\renewcommand*\familydefault{\sfdefault}
\usepackage{amsmath,amstext}
\usepackage{sfmath}
\usepackage{hyperref}
\usepackage{natbib}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{fancyvrb}
\usepackage[flushmargin]{footmisc}
\definecolor{grau2}{rgb}{.2,.2,.2}
\definecolor{grau7}{rgb}{.7,.7,.7}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=single,xleftmargin=0em,%
  formatcom=\color{grau2},rulecolor=\color{grau7}}
\DefineVerbatimEnvironment{Scode}{Verbatim}{xleftmargin=2em}
\fvset{listparameters={\setlength{\topsep}{0pt}}}
\renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
\SweaveOpts{keep.source = TRUE}

<<echo=false>>=
options(continue = "  ", digits = 3, width = 60, useFancyQuotes = FALSE)
pv <- packageVersion("PMwR")
pv <- gsub("(.*)[.](.*)", "\\1-\\2", pv)
@

\begin{document}
\title{Return contribution}
\author{Enrico Schumann\\\url{mailto:es@enricoschumann.net}}

{\raggedright{\LARGE Return contribution}}\hspace*{\fill}
{\footnotesize Package version \Sexpr{pv}}\medskip

\noindent Enrico Schumann\\
\noindent \url{mailto:es@enricoschumann.net}\\
\bigskip

% \noindent This vignette shows how function \texttt{rc}
% can be used.


% \section{Return contribution}

\noindent Start with a simple case: three assets, one period.
The vector~$w$ stores the beginning-of-period
weights, and the vector~$R$ holds the returns
for the period.
<<>>=
w <- c( 0.1, 0.5, 0.4)
R <- c(-0.1, 0.1, 0.2)
@
\noindent You get the return contributions of the single
assets by multiplying $wR$ element by element (as is the default in R).
Summing the contributions gives the total return.
<<echo=false>>=
names(w) <- c("A", "B", "C")
w*R
sum(w*R)
@
\noindent With \texttt{rc}:
<<>>=
library("PMwR")
rc(R, w)
@

\noindent Suppose the weights of some reference
portfolio (a.\/k.\/a.\ a benchmark) are stored in
\texttt{w.bm}.
<<>>=
w.bm <- c(A = 0.2, B = 0.4, C = 0.4)
@
\noindent One way to define relative return
contributions is to multiply returns by active weights,
i.\,e. differences between portfolio and benchmark
weights.
<<>>=
(w - w.bm)*R
@
\noindent Summing those relative contributions gives
the total active return.
<<>>=
sum((w - w.bm)*R)
sum(w*R) - sum(w.bm*R)
@

\noindent If you provide benchmark weights or
returns to \texttt{rc}, then this will trigger a
split into allocation, selection and interaction \citep{brinson1986}.
Since returns for the portfolio and benchmark are the same for all segments,
the total effect is captured by `allocation'.
<<>>=
rc(R, w, weights.bm = w.bm)
@
\noindent But the result is not the same as above: now all
relative return of 2\,\% is attributed to \textsc{a},
when before \textsc{a} and \textsc{b} both contributed
1\,\%.  The reason is that rc, per default, computes
allocation as
$(w - w_{\mathrm{bm}})(R - R_{\mathrm{bm}})$, i.\,e. it
subtracts the benchmark return.  This setting is
controlled by argument \texttt{allocation.minus.bm}.


An example from \citet{christopherson2009}, Tables 18.4--18.6.
<<>>=
segments <- c("Large Cap", "Small Cap", "Fixed Income")

w    <- matrix(c(60, 10, 30,
                 60, 10, 30,
                 60, 10, 30), nrow = 3, byrow = TRUE)/100
w.bm <- matrix(c(55,  5, 40,
                 55,  5, 40,
                 55,  5, 40), nrow = 3, byrow = TRUE)/100

R    <- matrix(c(-3.7,-3.5,-0.1,
                 +4.1,+3.7,+2.9,
                 +3.5,+1.2,+1.0), nrow = 3, byrow = TRUE)/100

R.bm <- matrix(c(-1.9,-5.3,-0.9,
                 +2.1,+4.3,+2.4,
                 +2.1,+1.4,+0.4), nrow = 3, byrow = TRUE)/100


rc(R = R, weights = w, R.bm = R.bm, weights.bm = w.bm,
   method = "attribution", segments = segments)
@

\bibliographystyle{plainnat}
\bibliography{PMwR}

\end{document}
