From 88e12686601c88e61b59df4be52d5aa64aa87999 Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Thu, 17 Feb 2022 13:52:30 -0500 Subject: [PATCH 1/6] Create fi.yield_curve module --- src/fixedincome/yield_curve.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/fixedincome/yield_curve.py diff --git a/src/fixedincome/yield_curve.py b/src/fixedincome/yield_curve.py new file mode 100644 index 0000000..772e199 --- /dev/null +++ b/src/fixedincome/yield_curve.py @@ -0,0 +1,21 @@ +""" +Fixed income analytics for the yield curve + +Implements various functions useful for yield curve analytics. +""" + + +def bootstrap(): + pass + + +def regression(): + pass + + +def spline(): + pass + + +def nelson_siegel(): + pass From 03ca2c4094d70fa31c1aa11f22376d3c30f938b3 Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Wed, 23 Feb 2022 14:12:25 -0500 Subject: [PATCH 2/6] Add yield_curve module --- src/fixedincome/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fixedincome/__init__.py b/src/fixedincome/__init__.py index c0ba094..1e88563 100644 --- a/src/fixedincome/__init__.py +++ b/src/fixedincome/__init__.py @@ -15,6 +15,6 @@ Functions useful for analyzing fixed rate bonds. """ -__all__ = ['utils', 'bonds'] +__all__ = ['utils', 'bonds', 'yield_curve'] from fixedincome import * From c0acf670ec05cc7bc529cb5493869aa815c11804 Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Tue, 22 Mar 2022 02:30:15 +0000 Subject: [PATCH 3/6] Implement fi.yield_curve.bootstrap() --- src/fixedincome/yield_curve.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/fixedincome/yield_curve.py b/src/fixedincome/yield_curve.py index 772e199..f581486 100644 --- a/src/fixedincome/yield_curve.py +++ b/src/fixedincome/yield_curve.py @@ -5,8 +5,31 @@ """ -def bootstrap(): - pass +import numpy as np + + +def bootstrap(cash_flows : np.ndarray, prices : np.ndarray) -> np.ndarray: + """ + Calculates the spot yield curve from a series of bonds. + + Parameters + ---------- + cash_flows : np.ndarray + Nonsingular payoff matrix of a series of bonds. + prices : np.ndarray + Corresponding price of each bond. + + Returns + ------- + np.ndarray + The corresponding spot yield curve derived from no-arbitrage relationships. + """ + + assert cash_flows.shape[1] == prices.shape[0], "Check shapes of input matrices." + + discount_factors = np.linalg.inv(cash_flows) @ prices + yields = np.power(discount_factors, np.reshape(-1 / np.arange(start=1, stop=discount_factors.shape[0]+1), newshape=discount_factors.shape)) - 1 # Formula: d = 1/(1 + y)^i + return yields def regression(): From 98c90209b3d1e1e6946c69a3063f1b61be86ff3e Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Tue, 22 Mar 2022 02:35:59 +0000 Subject: [PATCH 4/6] Temporarily implement yield_curve functions using NotImplementedError's --- src/fixedincome/yield_curve.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fixedincome/yield_curve.py b/src/fixedincome/yield_curve.py index f581486..5bf032a 100644 --- a/src/fixedincome/yield_curve.py +++ b/src/fixedincome/yield_curve.py @@ -33,12 +33,12 @@ def bootstrap(cash_flows : np.ndarray, prices : np.ndarray) -> np.ndarray: def regression(): - pass + raise NotImplementedError("Regression not yet implemented.") def spline(): - pass + raise NotImplementedError("Spline not yet implemented.") def nelson_siegel(): - pass + raise NotImplementedError("Nelson-Siegel not yet implemented.") From 76e68ab8915fb76654f22af329f6afc7ebe7ec53 Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Tue, 22 Mar 2022 02:36:18 +0000 Subject: [PATCH 5/6] Update to v0.1.2 for fi.yield_curve --- VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION.txt b/VERSION.txt index 6da28dd..8294c18 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -0.1.1 \ No newline at end of file +0.1.2 \ No newline at end of file From 725d9ec6ed69f527db2088f3dd183215af81f072 Mon Sep 17 00:00:00 2001 From: Shreyas Srinivasan Date: Tue, 22 Mar 2022 03:03:36 +0000 Subject: [PATCH 6/6] Flatten output of fi.yield_curve.bootstrap() --- src/fixedincome/yield_curve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fixedincome/yield_curve.py b/src/fixedincome/yield_curve.py index 5bf032a..6b8a894 100644 --- a/src/fixedincome/yield_curve.py +++ b/src/fixedincome/yield_curve.py @@ -29,7 +29,7 @@ def bootstrap(cash_flows : np.ndarray, prices : np.ndarray) -> np.ndarray: discount_factors = np.linalg.inv(cash_flows) @ prices yields = np.power(discount_factors, np.reshape(-1 / np.arange(start=1, stop=discount_factors.shape[0]+1), newshape=discount_factors.shape)) - 1 # Formula: d = 1/(1 + y)^i - return yields + return np.reshape(yields, newshape=(yields.shape[0])) # Flatten output def regression():