Accuracy of midpoint convention

We reproduce the chart of slide 14 of chapter 3.

In [1]:
import numpy as np
import matplotlib.pyplot as plt #graphing module with matlab-like properties
%matplotlib inline 
import math

dis1= lambda x:1/(1+x)**0.5 # this is the midpoint discounting approach
dis2= lambda x:1/x*(1-math.exp(-x)) # this is the exact discounting when cash-flows are uniform

rrange=np.arange(start=0.01, stop=0.5, step=0.05) # set a range of interest rates

plt.plot(rrange,[dis1(r) for r in rrange], label='midpoint')
plt.plot(rrange,[dis2(r) for r in rrange], label='exact')
plt.xlabel('interest rate')
plt.ylabel('discount factor')
plt.title('Accuracy of midpoint convention')
plt.legend()
Out[1]:
<matplotlib.legend.Legend at 0x2404fb05b80>