I have only recently started learning python and the below is my attempt to create an 'interest rate' object which, given one of several types of interest/discount rate, gives the user access to all of the other corresponding rates that are equivalent to this. This looks as follows:
import math
class InterestRate:
def __init__(self, effectiveInterestRate=0, nominalInterestRate=0, effectiveDiscountRate=0, nominalDiscountRate=0, discountFactor=0, forceOfInterest=0, paymentsPerPeriod=1):
self.effectiveInterestRate = effectiveInterestRate
self.nominalInterestRate = nominalInterestRate
self.effectiveDiscountRate = effectiveDiscountRate
self.nominalDiscountRate = nominalDiscountRate
self.discountFactor = discountFactor
self.forceOfInterest = forceOfInterest
self.paymentsPerPeriod = paymentsPerPeriod
numberOfRatesProvided = 0
for attr, value in vars(self).items():
if value != 0 and attr != 'paymentsPerPeriod':
numberOfRatesProvided += 1
rateGiven = attr
if numberOfRatesProvided != 1:
raise Exception("Incorrect number of inputs passed to InterestRate object")
if rateGiven == 'nominalInterestRate': self.effectiveInterestRate = (nominalInterestRate/paymentsPerPeriod + 1)**paymentsPerPeriod - 1
elif rateGiven == 'effectiveDiscountRate': self.effectiveInterestRate = effectiveDiscountRate / (1 - effectiveDiscountRate)
elif rateGiven == 'nominalDiscountRate': self.effectiveInterestRate = (1 - nominalDiscountRate/paymentsPerPeriod)**-paymentsPerPeriod - 1
elif rateGiven == 'discountFactor': self.effectiveInterestRate = 1/discountFactor - 1
elif rateGiven == 'forceOfInterest': self.effectiveInterestRate = math.exp(forceOfInterest) - 1
self.nominalInterestRate = self.paymentsPerPeriod*((1+self.effectiveInterestRate)**(1/self.paymentsPerPeriod) - 1)
self.effectiveDiscountRate = self.effectiveInterestRate/(1+self.effectiveInterestRate)
self.nominalDiscountRate = self.paymentsPerPeriod*(1 - (1 + self.effectiveInterestRate)**(-1/self.paymentsPerPeriod))
self.discountFactor = 1 / (1 + self.effectiveInterestRate)
self.forceOfInterest = math.log(1 + self.effectiveInterestRate)
def outputInterestRateData(self):
print("\n ----------------------------------------------------")
print(" Interest Rate Data ")
print(" ----------------------------------------------------")
print(" Payments per Period: ", self.paymentsPerPeriod)
print(" Effective Interest Rate: ", self.effectiveInterestRate)
print(" Effective Discount Rate: ", self.effectiveDiscountRate)
print(" Nominal Interest Rate: ", self.nominalInterestRate)
print(" Nominal Discount Rate: ", self.nominalDiscountRate)
print(" Discount Factor: ", self.discountFactor)
print(" Force Of Interest: ", self.forceOfInterest)
print(" ----------------------------------------------------")
def shiftedPaymentPV(paymentAmmount, periodsShifted):
return paymentAmmount*(1 + self.effectiveInterestRate)**periodsShifted
Assuming that it is clear enough what I have attempted to do here, can anyone offer some constructive criticism on my first attempt at a python class module?