.. _intRates: Building in-house libraries: Python =================================================== .. contents:: :depth: 3 :local: :backlinks: entry The purpose of this section is to demonstrate how to build financial models using the Python functional programming paradigm. It is *not* to derive or implement models. Thus we focus on two "toy" models - deterministic interest rates and 1-factor Gaussian short rate with constant coefficients. Models ------------------- We consider *models* for the future evolution of discount factors. We restrict our implementation to the case when today's interest rate curve is flat - the same rate for all (bond/deposit) maturities. (In an actual analytics library, today's rates input would be a curve - list of rates or discount factors and maturities, maybe along with an interpolation method.) The simple discounting "model" (not really a model) assumes today's rates stay the same in the future. Thus the discount factors are given by .. math:: :label: discSimple D(t,T) = \exp (-r (T-t) ) where - :math:`r` is today's spot interest rate, - :math:`t` is the valuation time (today - :math:`t-0`, future :math:`t>0`), and - :math:`T` is the maturity. The Linear Gaussian model is .. math:: :label: discLinGauss D(t,T,x) = A(t,T) \, e^{-B(t,T) \, x}, where .. math:: :label: discLinGaussA A(t,T) = \exp(-r (T-t)) \exp \Big( \frac{1}{2} \int_0^t \sigma^2 \big( B(s,T)^2 - B(s,t)^2 \big) ds \Big), .. math:: :label: discLinGaussB B(t,T) = \frac{ 1 - e^{-a (T-t)}}{a}, and - :math:`\sigma` is the spot rate volatility and - :math:`a` is the spot rate mean reversion speed. We can write down the price of any (interest rates) instrument in terms of discount factors. For example, the price of a coupon paying bond is .. math:: :label: cpnBond P(t, x) = \sum_{k=0}^N C_k \, D(t,T_k,x), where :math:`C_k` are the coupon amounts (:math:`C_N` the principal), :math:`T_k` the coupon dates, and :math:`D(t,T_k,x)` the discount factors, given by either :eq:`discSimple` or :eq:`discLinGauss`. (:math:`D(t,T,x)=D(t,T)` in :eq:`discSimple`) The point is that we do just that in the implementation: Pass the functions calculating the discount factors to the (model agnostic) function that calculates the price of the coupon paying bond. Similarly, any other (interest rates) model that can be plugged in :eq:`cpnBond`. Likewise, other financial instruments (that can be written in terms of discount factors) can be priced in a similar way. Implementation ----------------- .. automodule:: LibPythonExample.intRates :members: Tests ----- .. plot:: analytics/intRatesTest.py :include-source: .. literalinclude:: intRatesOut1.txt