Source code for LibPythonExample.intRates

"""
Interest rates module - proof of concept
"""

import math

[docs]def discFactorDeterministic(t, T, r): """ This function implements :eq:`discSimple` """ return math.exp(-r*T)
[docs]def discFactorLinearGaussian(t, T, r, x, a, v): r""" This function implements :eq:`discLinGauss`. We calculate the integral in :eq:`discLinGaussA`, .. math:: A(t,T) = \exp ( -r (T-t) ) \exp \bigg( \frac{\sigma^2}{4 a^3} \Big( - 4 e^{-a(T-t)} + 4 e^{-a T} + e^{-2a(T-t)} - e^{- 2 a T} + 4 - 4 e^{-a t} - 1 + e^{- 2 a t} \Big) \bigg) and implement this formula. """ B = (1.0 - math.exp(-a*(T-t)))/a temp = -4*math.exp(-a*(T-t)) + 4*math.exp(-a*T) + math.exp(-2*a*(T-t)) - math.exp(-2*a*T) temp += 4.0 - 4*math.exp(-a*t) - 1.0 + math.exp(-2*a*t) A = math.exp(-r*(T-t)) * math.exp( 0.25*v*v*temp/(a*a*a) ) return A*math.exp(-B*x)
[docs]def cpnBondPrice(t, Tlist, Clist, x, discFactFun): r""" This function implements :eq:`cpnBond`. Tlist: - :math:`T_k`, Clist - :math:`C_k`, equal length. Usage: discFactFun = lambda t, T, x: discFactorDeterministic(t, T, r) or discFactFun = lambda t, T, x: discFactorLinearGaussian(t, T, r, x, a, v) Then P = cpnBondPrice(t, Tlist, Clist, x, disFactFun) """ if len(Clist) != len(Tlist): raise Exception("Tlist and Clist must have the same length!") P = 0.0 for (C,T) in zip(Clist,Tlist): P += C*T*discFactFun(t,T,x) return P