Skip to content

Instantly share code, notes, and snippets.

@tlmaloney
Created December 8, 2011 12:40
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/1446881 to your computer and use it in GitHub Desktop.
Save tlmaloney/1446881 to your computer and use it in GitHub Desktop.
Annuity class
'''
Created on Dec 8, 2011
@author: tlmaloney
'''
import Asset
class Annuity(Asset.Asset):
'''
An annuity is a collection of Payment objects. Let the collection
be a dictionary keyed by payment_date.
'''
def __init__(self, asset_id, name, payments):
'''
Constructor
Keyword arguments:
asset_id -- Unique identifier
name -- Name of the asset
payments -- A dictionary of Payment objects
'''
super(Annuity, self).__init__(asset_id, name)
self.payments = payments
def set_payment(self, payment):
'''
Add/changes a payment
Keyword arguments:
payment -- Currency payment instance
'''
self.payments[payment.payment_date] = payment
def get_payment(self, payment_date):
'''
Returns a payment on a specificpayment_date
Keyword arguments:
payment_date -- SerialDate instance
'''
return self.payments[payment_date]
def pv(self, market, slice_date):
'''
Returns the present value of a payment
Keyword arguments:
market -- the market which holds the discount factor curve, Market instance
slice_date -- the date of transfer of ownership
'''
pv = 0.0
for payment_date, payment in self.payments.iteritems():
if slice_date.serial_day_number <= payment_date.serial_day_number:
pv = pv + self.units * self.asset.pv(market, payment_date)
return pv
def fv(self, market, slice_date):
'''
Returns the future value of a payment
Keyword arguments:
market -- the market which holds the discount factor curve, Market instance
slice_date -- the date of transfer of ownership
'''
fv = 0.0
for payment_date, payment in self.payments.iteritems():
pv = payment.pv(market, slice_date)
# we only have one discount factor curve
dfc = market.get_observable('DFC.USD')
df = dfc.calc_df(slice_date)
fv = fv + pv / df.discount_factor
return fv
def make(asset_id, name, payments={}):
'''
Makes a new Annuity instance
Keyword arguments:
asset_id -- Unique identifier
name -- Name of the asset
payments -- a dictionary of Payment objects
'''
return Annuity(asset_id, name, payments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment