In [1]:

```
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
```

In [2]:

```
# Python3 program to find compound
# interest for given values.
def compound_interest(principle, rate, years, monthly_cont, num_months):
"""
principle = the amount of money that you begin with
rate = the PERCENTAGE OF INTEREST (0-100) that you earn each time
years = the number of times you will gain interest (years)
monthly_contr = the amount you plan to input every month
num_months = number of months of contributions
"""
amount = principle
yearly_contr = monthly_cont * num_months
indiv_amounts = []
# Calculates compound interest
for ii in range(years):
amount += yearly_contr # Add a single years total contributions
year_interest = amount * (rate/100) # Calculate the interest on the new amount
amount += year_interest # Add the interest
indiv_amounts.append(amount) # Collect the yearly amounts for later plotting
total_cmp_int = amount - (principle + (monthly_cont * num_months * years)) # Calculate the total compound interest by subtracting the principle and total contributions
# Print info.
print("Compound interest = ${:,.2f}".format(total_cmp_int))
print("Total after {} years = ${:,.2f}".format(years,amount))
# return the final amount and individual amounts after each year
return amount, indiv_amounts
```

In [3]:

```
# First 5 years in PhD
total_value, yearlys = compound_interest(principle = 1,
rate = 5,
years = 5,
monthly_cont = 200,
num_months = 10) # We don't get paid over the summer
```

In [4]:

```
total, yearlys2 =compound_interest(principle = total_value,
rate = 5,
years = 30,
monthly_cont = 0,
num_months = 12)
```

In [5]:

```
# Combine the yearly amounts into one list
all_yearlys = yearlys + yearlys2
```

In [6]:

```
# Create a dataframe with years and values
yearly_values = pd.DataFrame({"Years" : np.arange(1,len(all_yearlys)+1,1),
"Value" : all_yearlys
})
```

In [7]:

```
yearly_values.head()
```

Out[7]:

In [8]:

```
import matplotlib as mpl
plt.figure(figsize=(15,7))
ax = sns.lineplot(data = yearly_values,
x = "Years",
y = "Value",
markers = "o",
size=100)
plt.vlines(x = 5,
ymax=20000,
ymin=0,
linestyles="--")
plt.title('Total = ${:20,.2f}'.format(yearly_values.iloc[-1]["Value"]))
plt.annotate(s = "Stop Contributions.",
xy = (5,20000))
formatter = mpl.ticker.FormatStrFormatter('$%1.2f')
ax.yaxis.set_major_formatter(formatter)
plt.grid()
sns.despine()
```

- Here, we calculate compound interest for contributing $200 every month for the next 35 years (5 Ph.D. years + 30 more, like last time)

In [9]:

```
# First 5 years in PhD
total_value, yearlys = compound_interest(principle = 1,
rate = 5,
years = 5,
monthly_cont = 200,
num_months = 10) # We don't get paid over the summer
# 200 for 30 more years
total_value, yearlys2 = compound_interest(principle = total_value,
rate = 5,
years = 30,
monthly_cont = 200,
num_months = 12) # Now we do get paid over the summer
all_yearlys = yearlys + yearlys2
# Create a dataframe with years and values
yearly_values = pd.DataFrame({"Years" : np.arange(1,len(all_yearlys)+1,1),
"Value" : all_yearlys
})
```

In [10]:

```
plt.figure(figsize=(15,7))
ax = sns.lineplot(data = yearly_values,
x = "Years",
y = "Value",
markers = "o",
size=100, legend=False)
plt.vlines(x = 5,
ymax=50000,
ymin=0,
linestyles="--")
plt.annotate(s = "End of Ph.D.",
xy = (5,50000))
plt.title('Total = ${:20,.2f}'.format(total_value))
formatter = mpl.ticker.FormatStrFormatter('$%1.2f')
ax.yaxis.set_major_formatter(formatter)
plt.grid()
sns.despine()
```

In [11]:

```
# First 5 years in PhD
total_value, yearlys = compound_interest(principle = 1,
rate = 5,
years = 5,
monthly_cont = 200,
num_months = 10)
```

In [12]:

```
# 100k / 12 months --> REMEMBER THESE ARE PRETAX CONTRIBUTIONS, SO WE DON'T NEED TO REMOVE INCOME TAX
monthly_payment = 100000 / 12
monthly_payment * .1
```

Out[12]:

In [13]:

```
total, yearlys2 =compound_interest(principle = total_value,
rate = 5,
years = 30,
monthly_cont = 800,
num_months = 12) # paid each month
```

In [14]:

```
# Combine the yearly amounts into one list
all_yearlys = yearlys + yearlys2
```

In [15]:

```
# Create a dataframe with years and values
yearly_values = pd.DataFrame({"Years" : np.arange(1,len(all_yearlys)+1,1),
"Value" : all_yearlys
})
```

In [16]:

```
yearly_values.head()
```

Out[16]:

In [17]:

```
import matplotlib as mpl
plt.figure(figsize=(15,7))
ax = sns.lineplot(data = yearly_values,
x = "Years",
y = "Value",
markers = "o",
size=100, legend=False)
plt.vlines(x = 5,
ymax=50000,
ymin=0,
linestyles="--")
plt.annotate(s = "End of Ph.D.",
xy = (5,50000))
plt.title('Total = ${:20,.2f}'.format(total))
formatter = mpl.ticker.FormatStrFormatter('$%1.2f')
ax.yaxis.set_major_formatter(formatter)
plt.grid()
sns.despine()
```

In [ ]:

```
s
```