Plotting Cashflows
Present Value¶
Present value is an idea that money now is worth more than money in the future because you can use it to do things like invest or consume. In this lesson we work through all the basics of present value and use some plots to drive home these points. Below I define a function for plotting cash flows. Do not worry about how it works, it might be a little too advanced, and I just need to define it to more easily show these cashflows on a timeline.
import matplotlib.pyplot as plt
def timelinePlot(periods):
#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')
plt.show()
#Now let's call our helper function to see what a timeline for a 5 year period looks like
timelinePlot(5)
Now I am going to add the ability to add a value. It will take the form of a tuple where the first value is the cashflow and the second value is the time that it happens at.
def timelinePlot(periods,value):
#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')
#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 value 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()
timelinePlot(5,(100,0))
Remember from the last lesson how compounding works. In the code below we are going to have our investment at time 0 grow by 5% each period. We will plot the value expanding over the timeframe.
#Plot comoound growth
#Define parameters
periods = 5
principal = 100
r = .05
#Get the value and plot each period
for t in range(periods+1):
A = principal*(1+r)**t
timelinePlot(periods,(A,t))