5.5.1. Compounding returns¶
5.5.1.1. The math¶
Let \(r_t = (P_t+D_t)/P_{t-1}\) be the simple return for a single period with dividends included.
Let \(R_t = (1+r_t)\) be the gross (thus, the capital “R”) return for a single period.
The gross return for a number of periods is
and the simple return for the same set of periods is simply the above minus 1: $\( r[0,T] = \left(\prod_{t=1}^T R_t\right) - 1 \)$
We can compute this in pandas pretty easily:
5.5.1.2. The code¶
Assume you have simple periodic (e.g. daily) returns in a variable called “ret” in df
for various assets and you want to compound them to a longer period of time (e.g. monthly)
To adapt the code below to your use
replace “df” with the name of your dataframe
replace “asset” with the name of the variable(s) identifying the assets whose returns you want to compound
replace “timeperiod” with the name of the variable(s) containing the measurement of time that you want to compound to, for example, a year variable
Here are two ways to accomplish the compounding:
(df
# compute gross returns for each asset
.assign(R = 1+df['ret'])
# for each portfolio and time period...
.groupby(['asset','timeperiod'])
# get the gross returns, and cumulate by taking the product
['R'].prod()
# subtract one to get back to simple returns
-1
)
(df
# for each asset and time period
.groupby(["asset", "timeperiod"])
# agg() does the function inside for each group (asset-timeperiod combo)
# 1+x['ret'] gets gross return (R) for each observation in the data
# (1+x{'ret']).prod() takes the product of all R for that group
# and then subtract one
.agg(ret=lambda x: ((1 + x["ret"]).prod()) - 1)
)
5.5.1.3. Optional: Log returns¶
Just as an FYI, there is another way to compound returns you might hear about in other contexts: using log returns.
A “log return” is \(ln(1+r)\), using the natural log. It’s the_ continuously compounded rate of return. If you earned a simple return of \(r=10%\), one month, the continuously compounded rate of return over that month is \(ln(1+0.1)=9.5%\).
Of course, no one speaks in terms of continuously compounding returns, but log returns has its uses.
Note
You can not add the simple rates of returns across time to get the total compounded return: \(r_{[0,1]} +r_{[1,2]} \neq r_{[0,2]}\)
But you can add log returns across time to get the correctly compounded total log return: \(ln(1+r_{[0,1]}) +ln(1+r_{[1,2]}) = ln(1+ r_{[0,2]})\)
This makes it easy to compound returns in large datasets another way than what I showed above. Taking advantage of the fact that \(e(ln(x))=x\) (in step 2 below) and \(e^a*e^b*e^c=e^{a+b+c}\) (in step 3 below), we can rewrite \(R[0,T]\) as:
The last line shows that to compound returns, you can sum the log returns. Then to convert the compounded log return back to a simple return, just exponentiate and substract 1.
Warning
You can add log returns across time, but you can’t combine log returns across assets at the time time.
(df
.assign(logR = np.log(1+df.ret))
# for each asset and time period
.groupby(["asset", "timeperiod"])
# sum log returns
['logR'].sum()
.assign(ret = binlambda x: np.exp(x['logR'])-1
)