-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yield_Curve
96 lines (79 loc) · 3.3 KB
/
Yield_Curve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
README
======
This file contains Python codes.
======
"""
""" Bootstrapping the yield curve """
import math
class BootstrapYieldCurve(object):
def __init__(self):
self.zero_rates = dict() # Map each T to a zero rate
self.instruments = dict() # Map each T to an instrument
def add_instrument(self, par, T, coup, price,
compounding_freq=2):
""" Save instrument info by maturity """
self.instruments[T] = (par, coup, price, compounding_freq)
def get_zero_rates(self):
""" Calculate a list of available zero rates """
self.__bootstrap_zero_coupons__()
self.__get_bond_spot_rates__()
return [self.zero_rates[T] for T in self.get_maturities()]
def get_maturities(self):
""" Return sorted maturities from added instruments. """
return sorted(self.instruments.keys())
def __bootstrap_zero_coupons__(self):
""" Get zero rates from zero coupon bonds """
for T in self.instruments.iterkeys():
(par, coup, price, freq) = self.instruments[T]
if coup == 0:
self.zero_rates[T] = \
self.zero_coupon_spot_rate(par, price, T)
def __get_bond_spot_rates__(self):
""" Get spot rates for every marurity available """
for T in self.get_maturities():
instrument = self.instruments[T]
(par, coup, price, freq) = instrument
if coup != 0:
self.zero_rates[T] = \
self.__calculate_bond_spot_rate__(
T, instrument)
def __calculate_bond_spot_rate__(self, T, instrument):
""" Get spot rate of a bond by bootstrapping """
try:
(par, coup, price, freq) = instrument
periods = T * freq # Number of coupon payments
value = price
per_coupon = coup / freq # Coupon per period
for i in range(int(periods)-1):
t = (i+1)/float(freq)
spot_rate = self.zero_rates[t]
discounted_coupon = per_coupon * \
math.exp(-spot_rate*t)
value -= discounted_coupon
# Derive spot rate for a particular maturity
last_period = int(periods)/float(freq)
spot_rate = -math.log(value /
(par+per_coupon))/last_period
return spot_rate
except:
print "Error: spot rate not found for T=%s" % t
def zero_coupon_spot_rate(self, par, price, T):
""" Get zero rate of a zero coupon bond """
spot_rate = math.log(par/price)/T
return spot_rate
if __name__ == "__main__":
yield_curve = BootstrapYieldCurve()
yield_curve.add_instrument(100, 0.25, 0., 97.5)
yield_curve.add_instrument(100, 0.5, 0., 94.9)
yield_curve.add_instrument(100, 1.0, 0., 90.)
yield_curve.add_instrument(100, 1.5, 8, 96., 2)
yield_curve.add_instrument(100, 2., 12, 101.6, 2)
y = yield_curve.get_zero_rates()
x = yield_curve.get_maturities()
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.title("Zero Curve")
plt.ylabel("Zero Rate (%)")
plt.xlabel("Maturity in Years")
plt.show()