Investments can be divided into two types:
-
One-time investment, with returns received in batches. For example, installment payments, mortgage home purchases, etc.
-
Batch investments, with a one-time return. For example, regular investment in index funds, etc.
Regardless of the type, the internal rate of return (IRR) can be used to calculate the annualized return on investment.
The entire process is divided into two parts: the first part is data entry, which can only be done manually. The second part is data computation, which can be assisted by Python.
In Python, create two columns: “time” representing time, and “cash_flow” representing cash flow.
Record the amount of each cash flow and its corresponding time. The time format is “year-month-day”. Cash outflows are recorded as negative, and inflows as positive.
- If it is a one-time investment with returns received in batches, record it as follows:
| time | cash_flow |
|---|---|
| 2021-09-01 | -100 |
| 2021-09-03 | 20 |
| 2021-09-07 | 30 |
| 2021-09-20 | 51 |
- If it is a batch investment with a one-time return, record it as follows:
| time | cash_flow |
|---|---|
| 2021-09-01 | -70 |
| 2021-09-03 | -20 |
| 2021-09-07 | -10 |
| 2021-09-20 | 101 |
Import the data into Python for computation, with the complete code as follows:
import pandas as pd
import numpy_financial as npf
df = pd.read_excel(r"Excel file path")
df = df.dropna(subset=["time"])
cash_flows = pd.Series(0,
index = pd.date_range(start = str(df["time"].min()),
end = str(df["time"].max()) ))
cash_flows.update(df.set_index('time')['cash_flow'])
day_r = npf.irr(cash_flows.values.tolist())
print(f"Daily return = {day_r:.2%}")
year_r = day_r * 365
print(f"Annualized return = {year_r:.2%}")
- One-time investment with returns received in batches.
| time | cash_flow |
|---|---|
| 2021-09-01 | -100 |
| 2021-09-03 | 20 |
| 2021-09-07 | 30 |
| 2021-09-20 | 51 |
The daily return can be calculated
You can substitute values into the equation for verification
- Batch investment with a one-time return.
| time | cash_flow |
|---|---|
| 2021-09-01 | -70 |
| 2021-09-03 | -20 |
| 2021-09-07 | -10 |
| 2021-09-20 | 101 |
The daily return can be calculated
You can substitute values into the equation for verification
In this way, calculating the annualized return on investment becomes much simpler. All one needs to do is enter the data, and for the computation, just leave it to the computer.