.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/classical/plot_02_st_petersburg_paradox.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_probability_classical_plot_02_st_petersburg_paradox.py: The St. Petersburg paradox ======================================================== The game pays :math:`2^k` if the first head comes on toss :math:`k`. Its expected payoff is infinite, so the running average of simulated payoffs never settles; it keeps jumping upward whenever a rare long run of tails occurs. Daniel Bernoulli's logarithmic utility values the game at a small, finite amount. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import st_petersburg_certainty_equivalent, st_petersburg_payoffs .. GENERATED FROM PYTHON SOURCE LINES 19-21 Running average payoff ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-33 .. code-block:: Python n = np.arange(1, 1000001) fig, ax = plt.subplots() for seed in range(4): payoffs = st_petersburg_payoffs(n.size, seed=seed) ax.semilogx(n, np.cumsum(payoffs) / n, lw=1) ax.semilogx(n, np.log2(n), "k--", label=r"$\log_2 n$ growth") ax.set_xlabel("games played") ax.set_ylabel("average payoff") ax.legend() ax.set_title("St. Petersburg game: the average never converges") .. image-sg:: /api/gallery/probability/classical/images/sphx_glr_plot_02_st_petersburg_paradox_001.png :alt: St. Petersburg game: the average never converges :srcset: /api/gallery/probability/classical/images/sphx_glr_plot_02_st_petersburg_paradox_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'St. Petersburg game: the average never converges') .. GENERATED FROM PYTHON SOURCE LINES 34-36 Bernoulli's certainty equivalent under log utility ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python for wealth in (0.0, 10.0, 100.0, 1000.0, 1e6): print(f"wealth {wealth:>9.0f}: worth {st_petersburg_certainty_equivalent(wealth):.3f} ducats") .. rst-class:: sphx-glr-script-out .. code-block:: none wealth 0: worth 4.000 ducats wealth 10: worth 5.466 ducats wealth 100: worth 7.892 ducats wealth 1000: worth 10.969 ducats wealth 1000000: worth 20.874 ducats .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.540 seconds) .. _sphx_glr_download_api_gallery_probability_classical_plot_02_st_petersburg_paradox.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_st_petersburg_paradox.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_st_petersburg_paradox.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_st_petersburg_paradox.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_