.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/portfolio/growth/plot_02_merton_portfolio.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_portfolio_growth_plot_02_merton_portfolio.py: Merton's portfolio problem ========================== Robert Merton (1969) solved the lifetime consumption and investment problem in continuous time. With a stock following geometric Brownian motion, a riskless rate :math:`r` and constant relative risk aversion :math:`\gamma`, the optimal policy is to keep the constant fraction .. math:: \pi^* = \frac{\mu - r}{\gamma\sigma^2} of wealth in the stock, rebalancing continuously, and to consume a constant fraction of wealth. The Kelly rule is the case :math:`\gamma = 1`. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.portfolio import merton_certainty_equivalent, merton_consumption_rate, merton_fraction, merton_wealth_paths mu, r, sigma = 0.07, 0.02, 0.18 .. GENERATED FROM PYTHON SOURCE LINES 27-29 The certainty-equivalent return of each constant mix ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-44 .. code-block:: Python pi = np.linspace(-0.5, 2.5, 300) fig, ax = plt.subplots(figsize=(7, 4)) for gamma in [1.0, 2.0, 4.0, 8.0]: star = merton_fraction(mu, r, sigma, gamma) ax.plot(pi, merton_certainty_equivalent(pi, mu, r, sigma, gamma), label=f"gamma = {gamma:g}: pi* = {star:.2f}") ax.plot(star, merton_certainty_equivalent(star, mu, r, sigma, gamma), "ko", ms=4) ax.set_ylim(0.0, 0.06) ax.set_xlabel("fraction of wealth in the stock") ax.set_ylabel("certainty-equivalent return") ax.legend(fontsize=8) fig.tight_layout() for gamma in [2.0, 4.0]: print(f"gamma = {gamma:g}: consume {merton_consumption_rate(mu, r, sigma, gamma, 0.03):.2%} of wealth a year") .. image-sg:: /api/gallery/portfolio/growth/images/sphx_glr_plot_02_merton_portfolio_001.png :alt: plot 02 merton portfolio :srcset: /api/gallery/portfolio/growth/images/sphx_glr_plot_02_merton_portfolio_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none gamma = 2: consume 3.46% of wealth a year gamma = 4: consume 2.97% of wealth a year .. GENERATED FROM PYTHON SOURCE LINES 45-51 Monte Carlo check: no other mix gives a higher expected utility --------------------------------------------------------------- Terminal wealth after 10 years for :math:`\gamma = 4`, simulated exactly through :func:`financialkit.integrators.gbm_paths` on common random numbers. .. GENERATED FROM PYTHON SOURCE LINES 51-67 .. code-block:: Python gamma, T = 4.0, 10.0 mixes = np.linspace(0.0, 1.0, 11) eu = [] for m in mixes: # The same seed for every mix: common random numbers. _, W = merton_wealth_paths(m, mu, r, sigma, T, 1, 100_000, rng=0) eu.append(np.mean(W[:, -1] ** (1 - gamma)) / (1 - gamma)) fig, ax = plt.subplots(figsize=(7, 3.5)) ax.plot(mixes, eu, "o-", label="simulated expected CRRA utility") ax.axvline(merton_fraction(mu, r, sigma, gamma), color="C2", ls="--", label="Merton's fraction") ax.set_xlabel("fraction in the stock") ax.set_ylabel("E[U(W_T)]") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/portfolio/growth/images/sphx_glr_plot_02_merton_portfolio_002.png :alt: plot 02 merton portfolio :srcset: /api/gallery/portfolio/growth/images/sphx_glr_plot_02_merton_portfolio_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.098 seconds) .. _sphx_glr_download_api_gallery_portfolio_growth_plot_02_merton_portfolio.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/portfolio/growth/plot_02_merton_portfolio.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_merton_portfolio.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_merton_portfolio.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_merton_portfolio.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_