Introduction
Time Value of Money¶
We have covered the present value and future value, so now I want to first introduce a function to find the value of different cash flows given different timings and rates of return. The function below is going to take as an argument a cashflows list where each element is a tuple with the nominal value, the discount rate for the period and the number of years in the future. This way we can make a function which returns the total TVM.
#Build a function to find TVM of cash flows
def TVM(cash_flows):
#Start with a present value of 0
PV = 0
#For each flow, calculate the PV and add it
for flow in cash_flows:
P = flow[0]
r = flow[1]
t = flow[2]
#Find the present value for the cashflow
PV_i = P / (1+r)**t
PV += PV_i
return PV
PV = TVM([(100,.01,1),(200,.015,2),(100,.02,3)])
print(PV)
Annuities¶
Annuities are financial instruments which return a set amount of money each year for a given number of years. An example is an annuity that rewards the holder with $100 every single year for 5 years. The way to calculate it's value is shown below given a discount rate of 5% held constant the whole time.
PV = TVM([(100, .05, 1), (100, .05, 2), (100, .05, 3), (100, .05, 4), (100, .05, 5)])
print(PV)
If we can assume that we are also able to invest each cash flow that we get at 5% after the fact, then our future value of the annuity can be found easily like below.
#In terms of future value, if we invested each cash flow at r=5% as we get it, we would have...
FV = PV * (1.05) ** 5
print(FV)
#Bringing back our timeline function
import matplotlib.pyplot as plt
def timelinePlot(periods,values):
#Create our plot
fig = plt.figure(figsize=(12, 4))
ax = fig.add_subplot(111)
#Set the limits of our plots (in terms of values, not dimensions)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
#Set the minimums that we will be plotting in (we give a 1 or 10% margin on each side)
xmin = 1
xmax = 9
#Set the y of where our plot will be centered around, 5 is the middle
y = 5
#Set height for our ticks
height = 1
#Get the spacing between each tick
delta = (xmax-xmin)/periods
#Create the positions for each tick
positions = [xmin+delta*x for x in list(range(periods+1))]
#Plot the horizontal line
plt.hlines(y, xmin, xmax, color='grey')
for t in range(periods+1):
#Plot the tick
plt.vlines(positions[t], y - .5, y + .5, color='grey')
#Plot the time period below
plt.text(positions[t], y-1.2, t, horizontalalignment='center',fontsize=14)
#Get rid of axes
plt.axis('off')
for value in values:
#Plot a red dot on the year that our value is at
plt.plot(positions[value[1]],y, 'ro', ms = 15, mfc = 'r')
#Plot the values above, use rounding to keep it clean
plt.text(positions[value[1]], y+1.2, "$"+str(round(value[0],1)), horizontalalignment='center',fontsize=18)
plt.show()
#Plot our the cash flows
payments = [(-PV,0)] + [(100,x) for x in range(1,6)]
timelinePlot(5,payments)