-
Compound Interest Part 1 6
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
-
Compound Interest Part 2 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Present Value 4
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
-
Annuities 6
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
Lecture4.5
-
Lecture4.6
-
-
Perpetuities 2
-
Lecture5.1
-
Lecture5.2
-
-
Bonds 6
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
Lecture6.5
-
Lecture6.6
-
-
Dividend Discount Model 3
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
-
Risk 8
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
Lecture8.6
-
Lecture8.7
-
Lecture8.8
-
-
Capital Asset Pricing Model 6
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Changing r
Changing Rates¶
It is not always the case that we have a constant rate applied. Sometimes we may deal with multiple rates, and for this case we will introduce two ways to find the total return at each point in time.
Using a Loop¶
Let’s begin with a value of $100 and say that the first 15 years have a rate of 10% while the last 15 years have a rate of 2%. First, create the rates list, then we can simply iterate through each yearly return!
#Create the rates
rates = [.1]*15+[.02]*15
print(rates)
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
#Also a list describing the years we will represent
years = list(range(31))
#Start with the principal at time 0
A = 100
A_list = [A]
#Go through the rates and update the value for each year
for r in rates:
A = (1 + r) * A
A_list.append(A)
print(A_list)
[100, 110.00000000000001, 121.00000000000003, 133.10000000000005, 146.41000000000008, 161.0510000000001, 177.15610000000012, 194.87171000000015, 214.3588810000002, 235.79476910000022, 259.37424601000026, 285.3116706110003, 313.84283767210036, 345.2271214393104, 379.74983358324147, 417.7248169415656, 426.07931328039695, 434.6008995460049, 443.29291753692496, 452.15877588766347, 461.20195140541676, 470.4259904335251, 479.8345102421956, 489.43120044703954, 499.21982445598036, 509.20422094509996, 519.388305364002, 529.7760714712821, 540.3715929007077, 551.1790247587219, 562.2026052538963]
import matplotlib.pyplot as plt
#Plot the compound interest
plt.plot(years, A_list)
plt.xlabel("Year")
plt.ylabel("Account Value")
plt.title("Account Value with Differing Rates")
plt.show()
Using the cumprod function¶
One of the functions that can make this process very easy is the cumprod function from numpy. For a list of values, it returns the multiplication of all values up to and including that value. So the formula for the ith value is:
$ Y_{i} = X_{0} * X_{1} * .... X_{i}$
For finding the value for the rates, we can add 1 to each and then use this function on it. Look below for the example.
import numpy as np
#Turn the rates into a numpy array
rates = np.array(rates)
#Add 1 to each rate
rates = rates + 1
#Find the cummulative product
cummulative_product = np.cumprod(rates)
print(cummulative_product)
[1.1 1.21 1.331 1.4641 1.61051 1.771561
1.9487171 2.14358881 2.35794769 2.59374246 2.85311671 3.13842838
3.45227121 3.79749834 4.17724817 4.26079313 4.346009 4.43292918
4.52158776 4.61201951 4.7042599 4.7983451 4.894312 4.99219824
5.09204221 5.19388305 5.29776071 5.40371593 5.51179025 5.62202605]
For this array, we also are going to need the value 1 in the beginning to represent the principal at time 0. The insert function from numpy takes an array, the index to insert at, and the value to insert then returns this new array.
#We also want to insert the value 1 in the beginning
cummulative_product = np.insert(cummulative_product, 0, 1)
print(cummulative_product)
[1. 1.1 1.21 1.331 1.4641 1.61051
1.771561 1.9487171 2.14358881 2.35794769 2.59374246 2.85311671
3.13842838 3.45227121 3.79749834 4.17724817 4.26079313 4.346009
4.43292918 4.52158776 4.61201951 4.7042599 4.7983451 4.894312
4.99219824 5.09204221 5.19388305 5.29776071 5.40371593 5.51179025
5.62202605]
Now that we have the cummulative product we can multiply the principal in to find the account value at each point. It will match our results from using the loop.
#Multiply in the value of principal
A_list = cummulative_product * 100
print(A_list)
[100. 110. 121. 133.1 146.41
161.051 177.1561 194.87171 214.358881 235.7947691
259.37424601 285.31167061 313.84283767 345.22712144 379.74983358
417.72481694 426.07931328 434.60089955 443.29291754 452.15877589
461.20195141 470.42599043 479.83451024 489.43120045 499.21982446
509.20422095 519.38830536 529.77607147 540.3715929 551.17902476
562.20260525]
#Plot the results again
plt.plot(years, A_list)
plt.xlabel("Year")
plt.ylabel("Account Value")
plt.title("Account Value with Differing Rates")
plt.show()
One thing you will notice is that if we have the same rates applied but in different order, our ending account value will still be the same. Let's try it now.
#Find the rates
rates1 = [.1]*15 + [.02]*15
rates2 = [.02]*15 + [.1]*15
#Convert the rates to numpy arrays
rates1 = np.array(rates1)
rates2 = np.array(rates2)
#Add 1 to the rates
rates1 = rates1 + 1
rates2 = rates2 + 1
#Find the cummulative product
cummulative_product1 = np.cumprod(rates1)
cummulative_product2 = np.cumprod(rates2)
#Insert the value of 1 in both of the arrays
cummulative_product1 = np.insert(cummulative_product1, 0, 1)
cummulative_product2 = np.insert(cummulative_product2, 0, 1)
#Find the account values
A_list1 = cummulative_product1 * 100
A_list2 = cummulative_product2 * 100
#Plot the values
plt.plot(years, A_list1)
plt.plot(years, A_list2)
plt.xlabel("Year")
plt.ylabel("Account Value")
plt.title("Account Value with Differing Rates")
plt.legend(['Account 1', 'Account 2'])
plt.show()