.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/branching/plot_01_galton_watson.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_branching_plot_01_galton_watson.py: Galton-Watson: the probability a family name dies out ======================================================== Each man has 0, 1, or 2 sons with probabilities 0.2, 0.3, 0.5. The extinction probability is the smallest fixed point of the generating function :math:`G(s) = 0.2 + 0.3s + 0.5s^2`, namely :math:`q = 0.4`, even though the population grows by 30% per generation on average. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import galton_watson_extinction_probability, galton_watson_simulate pmf = [0.2, 0.3, 0.5] .. GENERATED FROM PYTHON SOURCE LINES 20-22 The fixed point of the generating function ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python q = galton_watson_extinction_probability(pmf) s = np.linspace(0.0, 1.0, 200) fig, ax = plt.subplots() ax.plot(s, np.polynomial.polynomial.polyval(s, pmf), label="G(s)") ax.plot(s, s, "k--", label="s") ax.plot(q, q, "o", color="tab:red", label=f"q = {q:.4f}") ax.set_xlabel("s") ax.legend() ax.set_title("Extinction probability = smallest root of G(s) = s") .. image-sg:: /api/gallery/probability/branching/images/sphx_glr_plot_01_galton_watson_001.png :alt: Extinction probability = smallest root of G(s) = s :srcset: /api/gallery/probability/branching/images/sphx_glr_plot_01_galton_watson_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Extinction probability = smallest root of G(s) = s') .. GENERATED FROM PYTHON SOURCE LINES 34-36 Simulated families ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: Python result = galton_watson_simulate(pmf, n_generations=25, n_runs=10000, seed=0) print(f"simulated extinction fraction: {result.extinct_fraction:.4f} (theory {q:.4f})") fig, ax = plt.subplots() for sizes in result.generation_sizes[:40]: ax.semilogy(np.where(sizes > 0, sizes, np.nan), color="tab:blue", alpha=0.4) ax.set_xlabel("generation") ax.set_ylabel("population") ax.set_title("40 simulated families (dying lines stop)") .. image-sg:: /api/gallery/probability/branching/images/sphx_glr_plot_01_galton_watson_002.png :alt: 40 simulated families (dying lines stop) :srcset: /api/gallery/probability/branching/images/sphx_glr_plot_01_galton_watson_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none simulated extinction fraction: 0.3951 (theory 0.4000) Text(0.5, 1.0, '40 simulated families (dying lines stop)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.087 seconds) .. _sphx_glr_download_api_gallery_probability_branching_plot_01_galton_watson.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_galton_watson.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_galton_watson.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_galton_watson.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_