.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fixed_income/duration/plot_02_hicks_modified_duration.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_duration_plot_02_hicks_modified_duration.py: Hicks: duration as an elasticity (modified duration) ===================================================== John Hicks (1939) found Macaulay's average period again as the elasticity of a capital value with respect to the discount factor. Measured per unit of yield, that elasticity is *modified duration*, .. math:: D_{mod} = -\frac{1}{P}\frac{dP}{dy} = \frac{D_{mac}}{1 + y/m}, for a yield compounded :math:`m` times a year. The script checks the relation across compounding frequencies and shows that :math:`-D_{mod}` is the slope of the log-price curve. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.fixed_income import bond_cash_flows, macaulay_duration, modified_duration, price_from_yield cf = bond_cash_flows(100.0, 0.05, 15.0, freq=2) y0 = 0.05 .. GENERATED FROM PYTHON SOURCE LINES 26-28 Macaulay and modified duration by compounding frequency ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python for m in (1, 2, 4, 12, None): d_mac = macaulay_duration(cf, y0, m) d_mod = modified_duration(cf, y0, m) ratio = 1.0 if m is None else 1 + y0 / m print(f"m={str(m):>4}: D_mac={d_mac:.4f} D_mod={d_mod:.4f} D_mac/(1+y/m)={d_mac / ratio:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none m= 1: D_mac=10.7423 D_mod=10.2308 D_mac/(1+y/m)=10.2308 m= 2: D_mac=10.7268 D_mod=10.4651 D_mac/(1+y/m)=10.4651 m= 4: D_mac=10.7188 D_mod=10.5865 D_mac/(1+y/m)=10.5865 m= 12: D_mac=10.7134 D_mod=10.6689 D_mac/(1+y/m)=10.6689 m=None: D_mac=10.7107 D_mod=10.7107 D_mac/(1+y/m)=10.7107 .. GENERATED FROM PYTHON SOURCE LINES 36-38 Modified duration is the slope of log-price against yield --------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-50 .. code-block:: Python y = np.linspace(0.0, 0.10, 200) log_p = np.log(price_from_yield(cf, y, 2)) d_mod = modified_duration(cf, y0, 2) p0 = np.log(price_from_yield(cf, y0, 2)) fig, ax = plt.subplots() ax.plot(100 * y, log_p, label="log price") ax.plot(100 * y, p0 - d_mod * (y - y0), ls="--", label=f"slope $-D_{{mod}}$ = {-d_mod:.2f}") ax.set_xlabel("yield, semiannual (%)") ax.set_ylabel("log price") ax.legend() plt.show() .. image-sg:: /api/gallery/fixed_income/duration/images/sphx_glr_plot_02_hicks_modified_duration_001.png :alt: plot 02 hicks modified duration :srcset: /api/gallery/fixed_income/duration/images/sphx_glr_plot_02_hicks_modified_duration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.024 seconds) .. _sphx_glr_download_api_gallery_fixed_income_duration_plot_02_hicks_modified_duration.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/duration/plot_02_hicks_modified_duration.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hicks_modified_duration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hicks_modified_duration.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hicks_modified_duration.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_