Created
August 12, 2012 00:17
-
-
Save tlmaloney/3328083 to your computer and use it in GitHub Desktop.
Rate class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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