.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/portfolio/growth/plot_01_kelly_criterion.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_01_kelly_criterion.py: The Kelly criterion =================== John Kelly (1956), at Bell Labs, asked how a gambler with a private wire should bet. Staking the fraction :math:`f` of wealth on a bet won with probability :math:`p` at odds :math:`b`, wealth grows at the rate :math:`g(f) = p\log(1 + bf) + q\log(1 - f)` per bet, maximized by :math:`f^* = p - q/b`. The Kelly bettor eventually outgrows any other fixed-fraction strategy; betting more than twice Kelly makes growth negative even though every bet is favourable. .. GENERATED FROM PYTHON SOURCE LINES 15-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.portfolio import kelly_fraction, kelly_growth_rate, kelly_wealth_paths p = 0.55 f_star = kelly_fraction(p) print(f"Kelly fraction for an even-money bet with p = {p}: {f_star:.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Kelly fraction for an even-money bet with p = 0.55: 0.10 .. GENERATED FROM PYTHON SOURCE LINES 25-27 Growth rate against the fraction staked --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-39 .. code-block:: Python f = np.linspace(0, 0.25, 300) fig, ax = plt.subplots(figsize=(7, 3.5)) ax.plot(f, kelly_growth_rate(f, p)) ax.axvline(f_star, color="C2", ls="--", label="Kelly") ax.axvline(2 * f_star, color="C3", ls=":", label="twice Kelly") ax.axhline(0, color="0.3", lw=0.8) ax.set_xlabel("fraction of wealth staked") ax.set_ylabel("expected log growth per bet") ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/portfolio/growth/images/sphx_glr_plot_01_kelly_criterion_001.png :alt: plot 01 kelly criterion :srcset: /api/gallery/portfolio/growth/images/sphx_glr_plot_01_kelly_criterion_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-45 A thousand bets, three bettors ------------------------------ The same coin flips for each bettor. Half Kelly gives up a quarter of the growth for much smaller swings; double Kelly goes nowhere. .. GENERATED FROM PYTHON SOURCE LINES 45-58 .. code-block:: Python n_bets, n_paths = 1000, 200 fig, ax = plt.subplots(figsize=(7, 4)) for mult, color in [(0.5, "C0"), (1.0, "C2"), (2.0, "C3")]: W = kelly_wealth_paths(mult * f_star, p, n_bets, n_paths, rng=0) ax.semilogy(np.median(W, axis=0), color=color, lw=2, label=f"{mult:g} x Kelly (median)") ax.fill_between(np.arange(n_bets + 1), *np.percentile(W, [10, 90], axis=0), color=color, alpha=0.15) print(f"{mult:g} x Kelly: median final wealth {np.median(W[:, -1]):.3g}, P(lose money) {np.mean(W[:, -1] < 1):.2f}") ax.set_xlabel("bet") ax.set_ylabel("wealth") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/portfolio/growth/images/sphx_glr_plot_01_kelly_criterion_002.png :alt: plot 01 kelly criterion :srcset: /api/gallery/portfolio/growth/images/sphx_glr_plot_01_kelly_criterion_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5 x Kelly: median final wealth 47.1, P(lose money) 0.01 1 x Kelly: median final wealth 183, P(lose money) 0.04 2 x Kelly: median final wealth 1.31, P(lose money) 0.47 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.131 seconds) .. _sphx_glr_download_api_gallery_portfolio_growth_plot_01_kelly_criterion.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_01_kelly_criterion.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_kelly_criterion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_kelly_criterion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_kelly_criterion.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_