# Finance Basics for Pandas




#### How to compound returns

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.

Then the gross return for a number of periods is

\begin{equation}
R[0,T] = \prod_{t=1}^T R_t
\end{equation}

and the return for the same set of periods is simply the above minus 1.

\begin{align}
r[0,T] = & R[0,T] - 1  \\
= & (\prod_{t=1}^T R_t) - 1
\end{align}

There is one more way to compute the gross return: "Sum the log returns, then exponentiate":

\begin{align}
R[0,T] = & \prod_{t=1}^T R_t \\
       = & \prod_{t=1}^T ( e(ln( R_t))        ) \\
       = & e(\sum_{t=1}^T ln(R_t)) \\
\end{align}
the second line works because $e(ln(x))=x$ and the last line works because $e^a*e^b*e^c=e^{a+b+c}$.







```python
(df
 .groupby(['asset','tiemperiod'])
 .agg(ret = lambda x: ((1+x['ret']).prod())-1 )
```

69c26026738efa1e1cda8206e90adc8cb2875e1d
ipynb
Jupyter Notebook
content/03/02g2_finance.ipynb
jjl222/ledatascifi-2021
894162e0a7e3c4222525abc164b606ca70d30bae
MIT
2021-05-28T17:24:51.000Z
2021-05-28T17:24:51.000Z
content/03/02g2_finance.ipynb
jjl222/ledatascifi-2021
894162e0a7e3c4222525abc164b606ca70d30bae
MIT
2021-02-01T06:23:48.000Z
2021-04-26T12:40:41.000Z
content/03/02g2_finance.ipynb
jjl222/ledatascifi-2021
894162e0a7e3c4222525abc164b606ca70d30bae
MIT
2021-02-01T17:41:34.000Z
2021-09-28T00:41:45.000Z
Qwen/Qwen-72B
1. YES
2. YES