.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/classical/plot_01_buffon_needle.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_01_buffon_needle.py: Buffon's needle: estimating pi by dropping needles ======================================================== A needle as long as the gap between parallel lines crosses a line with probability :math:`2/\pi`, so counting crossings estimates :math:`\pi`. The estimate's error shrinks like :math:`1/\sqrt{n}`. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import buffon_needle .. GENERATED FROM PYTHON SOURCE LINES 17-19 One large experiment ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: Python result = buffon_needle(n_drops=1000000, seed=0) print(f"crossing fraction {result.crossing_fraction:.5f} (exact 2/pi = {result.exact_crossing_probability:.5f})") print(f"pi estimate {result.pi_estimate:.5f}") .. rst-class:: sphx-glr-script-out .. code-block:: none crossing fraction 0.63669 (exact 2/pi = 0.63662) pi estimate 3.14125 .. GENERATED FROM PYTHON SOURCE LINES 25-27 Convergence with the number of drops ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python n_values = np.logspace(2, 6, 9).astype(int) errors = [np.mean([abs(buffon_needle(n, seed=s).pi_estimate - np.pi) for s in range(20)]) for n in n_values] fig, ax = plt.subplots() ax.loglog(n_values, errors, "o-", label="mean |error| over 20 runs") ax.loglog(n_values, errors[0] * np.sqrt(n_values[0] / n_values), "--", color="0.5", label=r"$\propto 1/\sqrt{n}$") ax.set_xlabel("needles dropped") ax.set_ylabel(r"error in $\hat\pi$") ax.legend() ax.set_title("Buffon's needle (1777)") .. image-sg:: /api/gallery/probability/classical/images/sphx_glr_plot_01_buffon_needle_001.png :alt: Buffon's needle (1777) :srcset: /api/gallery/probability/classical/images/sphx_glr_plot_01_buffon_needle_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, "Buffon's needle (1777)") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.567 seconds) .. _sphx_glr_download_api_gallery_probability_classical_plot_01_buffon_needle.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_buffon_needle.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_buffon_needle.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_buffon_needle.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_