-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYTM.py
63 lines (52 loc) · 2.52 KB
/
YTM.py
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
import numpy as np
from scipy import optimize, interpolate
import datetime
class YTMCalculator(object):
def __init__(self, calcDate=None,monthConvention=None, yearConvention=None):
self.calcDate = calcDate or datetime.date.today()
self.daysInMonth = monthConvention or 30
self.daysInYear = yearConvention or 360
self.ytms=None
def calculate( self, paymentsDict, price):
def f(ytm):
return price-self.npv(paymentsDict, ytm)
return optimize.brentq(f,-1,1)
def npv(self, paymentsDict, ytm):
result = 0
for date, payment in paymentsDict.items():
if date<self.calcDate:
continue
yearsToDate = self.daysToDate( self.calcDate, date )/self.daysInYear
result += payment*np.exp(-ytm*yearsToDate)
return result
def macaulayDuration(self, paymentsDict, ytm, bondPrice):
result = 0
for date, payment in paymentsDict.items():
if date<self.calcDate:
continue
yearsToDate = self.daysToDate( self.calcDate, date )/self.daysInYear
result += payment*yearsToDate*np.exp(-ytm*yearsToDate)
return result/bondPrice
@classmethod
def daysToDate(cls, startDate, endDate):
return np.busday_count(startDate, endDate)
def dateToYears(self, date):
days = self.daysToDate(self.calcDate,date)
return days/self.daysInYear
def getDurationsYTMsAndMaturities(self, bondCalendars, bondPrices):
maturitiesInYears = []
durationsInYears = []
yields = []
for i, calendar in enumerate(bondCalendars):
maturitiesInYears.append(self.dateToYears(calendar.dates[-1]))
bondPrice = bondPrices.loc[calendar.bondName].Price
ytm = self.calculate(calendar.paymentsDict, bondPrice)
durationsInYears.append(self.macaulayDuration(calendar.paymentsDict, ytm, bondPrice))
yields.append(ytm)
return tuple([np.array(x) for x in (durationsInYears, yields, maturitiesInYears)]) #Cast to numpy arrays so no need to cast in the future
def getInterpolatedYieldCurve(self, bondCalendars, bondPrices):
"""Returned variable is an interpolated object"""
_, yields, maturitiesInYears = self.getDurationsYTMsAndMaturities(bondCalendars, bondPrices)
maturitiesInYears = [0] + maturitiesInYears
yields = [0] + yields
return interpolate.interp1d( maturitiesInYears, yields, bounds_error=False, fill_value='extrapolate',kind='linear' )