Skip to content

Instantly share code, notes, and snippets.

@tlmaloney
Created August 12, 2012 00:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlmaloney/3328083 to your computer and use it in GitHub Desktop.
Save tlmaloney/3328083 to your computer and use it in GitHub Desktop.
Rate class
class Rate(object):
''' Abstract concept of a market rate '''
def __init__(self, level, measure, periods):
"""Constructor
Keyword arguments:
level -- Float, the rate level (e.g. 0.05 means 5%)
measure -- Measure
periods -- Integer, the number of times the rate compounds in a year
(e.g. 2 means semiannual). If periods==0, then rate
compounds continuously.
"""
self.level = level
self.measure = measure
self.periods = periods
def calc_compounding_factor(self, date, schedule):
"""Returns the factor by which the rate grows (compounds) over the
time interval from schedule.prev_date(date) to date.
Keyword arguments:
date -- SerialDate
schedule -- Schedule
"""
if self.periods == 0:
# Continuos compounding
dcf = self.measure.calc_dcf(date, schedule)
return math.exp(self.level * dcf)
else:
# Discrete compounding
dcf = self.measure.calc_dcf(date. schedule)
total_periods = dcf * self.periods
period_rate = self.level / self.period
return math.pow(1 + period_rate, total_periods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment