.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fixed_income/curves/plot_03_bootstrapping.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_fixed_income_curves_plot_03_bootstrapping.py: Carleton-Cooper bootstrapping ============================= With bonds that pay on common dates, Carleton and Cooper (1976) noted that prices are linear in the discount factors, :math:`P = C d`. Solving that system one bond at a time, shortest first, is bootstrapping: each bond adds one zero rate at its maturity. With more bonds than dates the same system is solved by least squares. The first cell reproduces Hull's bootstrapping table. The second strips a zero curve from par rates. The third recovers discount factors from a noisy, overdetermined set of prices. .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.fixed_income import bootstrap_par_curve, bootstrap_zero_curve, carleton_cooper_discount_factors .. GENERATED FROM PYTHON SOURCE LINES 23-25 Hull's Tables 4.3-4.4 --------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-31 .. code-block:: Python T = [0.25, 0.5, 1.0, 1.5, 2.0] curve = bootstrap_zero_curve(T, [0, 0, 0, 0.08, 0.12], [97.5, 94.9, 90.0, 96.0, 101.6]) for t, r in zip(curve.tenors, curve.rates, strict=True): print(f"{t:4.2f} years: {100 * r:.3f}%") .. rst-class:: sphx-glr-script-out .. code-block:: none 0.25 years: 10.127% 0.50 years: 10.469% 1.00 years: 10.536% 1.50 years: 10.681% 2.00 years: 10.808% .. GENERATED FROM PYTHON SOURCE LINES 32-34 A zero curve from par yields ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-46 .. code-block:: Python tenors = np.array([1, 2, 3, 5, 7, 10, 20, 30.0]) par = np.array([0.020, 0.024, 0.027, 0.031, 0.034, 0.037, 0.041, 0.042]) zc = bootstrap_par_curve(tenors, par) fig, ax = plt.subplots() ax.plot(tenors, 100 * par, "o", label="par yields (input)") ax.plot(zc.tenors, 100 * zc.rates, "s-", label="bootstrapped zero rates") ax.plot(tenors, 100 * zc.par_yield(tenors), "x", color="k", label="par yields repriced") ax.set_xlabel("maturity (years)") ax.set_ylabel("rate (%)") ax.legend() .. image-sg:: /api/gallery/fixed_income/curves/images/sphx_glr_plot_03_bootstrapping_001.png :alt: plot 03 bootstrapping :srcset: /api/gallery/fixed_income/curves/images/sphx_glr_plot_03_bootstrapping_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-49 Least squares with more bonds than dates ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-63 .. code-block:: Python rng = np.random.default_rng(1976) dates = np.arange(1, 6, dtype=float) d_true = np.exp(-0.035 * dates) C = np.zeros((30, dates.size)) for i in range(30): n = rng.integers(1, dates.size + 1) C[i, :n] = 100 * rng.uniform(0.01, 0.08) C[i, n - 1] += 100.0 prices = C @ d_true + rng.normal(0, 0.02, 30) res = carleton_cooper_discount_factors(dates, C, prices) print("estimated:", res.params.round(5)) print("true: ", d_true.round(5)) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none estimated: [0.96569 0.93238 0.90028 0.86937 0.83943] true: [0.96561 0.93239 0.90032 0.86936 0.83946] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.027 seconds) .. _sphx_glr_download_api_gallery_fixed_income_curves_plot_03_bootstrapping.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/fixed_income/curves/plot_03_bootstrapping.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_bootstrapping.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_bootstrapping.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_bootstrapping.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_