.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/markov_chain/plot_01_gamblers_ruin.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_markov_chain_plot_01_gamblers_ruin.py: Markov chains: gambler's ruin as an absorbing chain ====================================================================== A Markov chain moves between states with probabilities that depend only on the current state, not on how it got there, so the whole process is described by one transition matrix. In gambler's ruin, a gambler with capital ``i`` (out of a target ``N``) makes fair even-money bets until reaching 0 (ruin) or ``N`` (target); the capital after each bet is a Markov chain on the states 0..N with two absorbing ends. Both the absorption probabilities and the expected number of steps have simple closed forms for a fair game, used here to check the numerical solve. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python import numpy as np from mathematicskit.probability import MarkovChain from mathematicskit.probability.visualizers.plots import plot_transition_matrix .. GENERATED FROM PYTHON SOURCE LINES 22-24 Build the transition matrix for capital 0..N ------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 24-37 .. code-block:: Python n_capital = 6 size = n_capital + 1 p = np.zeros((size, size)) p[0, 0] = 1.0 p[-1, -1] = 1.0 for i in range(1, n_capital): p[i, i - 1] = 0.5 p[i, i + 1] = 0.5 chain = MarkovChain(p) plot_transition_matrix(chain) .. image-sg:: /api/gallery/probability/markov_chain/images/sphx_glr_plot_01_gamblers_ruin_001.png :alt: Transition matrix :srcset: /api/gallery/probability/markov_chain/images/sphx_glr_plot_01_gamblers_ruin_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-40 Absorption probabilities and expected duration ------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 40-50 .. code-block:: Python transient = list(range(1, n_capital)) b = chain.absorption_probabilities(transient, absorbing=[0, n_capital]) t = chain.expected_steps_to_absorption(transient) for i, (row, steps) in zip(transient, zip(b, t)): print( f"start at {i}: P(ruin)={row[0]:.4f}, P(reach {n_capital})={row[1]:.4f} " f"(closed form {i / n_capital:.4f}), E[steps]={steps:.2f} (closed form {i * (n_capital - i)})" ) .. rst-class:: sphx-glr-script-out .. code-block:: none start at 1: P(ruin)=0.8333, P(reach 6)=0.1667 (closed form 0.1667), E[steps]=5.00 (closed form 5) start at 2: P(ruin)=0.6667, P(reach 6)=0.3333 (closed form 0.3333), E[steps]=8.00 (closed form 8) start at 3: P(ruin)=0.5000, P(reach 6)=0.5000 (closed form 0.5000), E[steps]=9.00 (closed form 9) start at 4: P(ruin)=0.3333, P(reach 6)=0.6667 (closed form 0.6667), E[steps]=8.00 (closed form 8) start at 5: P(ruin)=0.1667, P(reach 6)=0.8333 (closed form 0.8333), E[steps]=5.00 (closed form 5) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.021 seconds) .. _sphx_glr_download_api_gallery_probability_markov_chain_plot_01_gamblers_ruin.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_gamblers_ruin.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_gamblers_ruin.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_gamblers_ruin.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_