.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/special_functions/gamma_beta/plot_02_stirling_formula.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_special_functions_gamma_beta_plot_02_stirling_formula.py: Stirling's formula and its asymptotic series =============================================== Shows that sqrt(2 pi n) (n/e)^n approximates n! with relative error about 1/(12n), and that Stirling's full series for log Gamma(x) is asymptotic: at small x, adding terms first helps and then hurts. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import math import matplotlib.pyplot as plt import numpy as np from mathematicskit.special_functions import log_gamma_function, stirling_factorial, stirling_log_gamma .. GENERATED FROM PYTHON SOURCE LINES 19-21 The leading term: n! ~ sqrt(2 pi n) (n/e)^n ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python for n in (1, 5, 10, 50, 100): ratio = math.factorial(n) / stirling_factorial(float(n)) print(f"n = {n:3d}: n!/Stirling = {ratio:.6f}, 1 + 1/(12n) = {1 + 1 / (12 * n):.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none n = 1: n!/Stirling = 1.084438, 1 + 1/(12n) = 1.083333 n = 5: n!/Stirling = 1.016784, 1 + 1/(12n) = 1.016667 n = 10: n!/Stirling = 1.008365, 1 + 1/(12n) = 1.008333 n = 50: n!/Stirling = 1.001668, 1 + 1/(12n) = 1.001667 n = 100: n!/Stirling = 1.000834, 1 + 1/(12n) = 1.000833 .. GENERATED FROM PYTHON SOURCE LINES 27-29 Error of the truncated series versus number of terms ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: Python terms = np.arange(0, 16) fig, ax = plt.subplots() for x in (1.0, 2.0, 5.0): errors = [abs(stirling_log_gamma(x, terms=int(k)) - log_gamma_function(x)) for k in terms] ax.semilogy(terms, errors, "o-", label=f"x = {x:g}") print(f"x = {x:g}: best with {int(np.argmin(errors))} terms, error {min(errors):.2e}") ax.set_xlabel("number of Bernoulli correction terms") ax.set_ylabel(r"$|\,\mathrm{series} - \ln\Gamma(x)\,|$") ax.set_title("Stirling's series is asymptotic, not convergent") ax.legend() .. image-sg:: /api/gallery/special_functions/gamma_beta/images/sphx_glr_plot_02_stirling_formula_001.png :alt: Stirling's series is asymptotic, not convergent :srcset: /api/gallery/special_functions/gamma_beta/images/sphx_glr_plot_02_stirling_formula_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none x = 1: best with 3 terms, error 2.88e-04 x = 2: best with 7 terms, error 3.90e-07 x = 5: best with 15 terms, error 8.88e-16 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.043 seconds) .. _sphx_glr_download_api_gallery_special_functions_gamma_beta_plot_02_stirling_formula.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_stirling_formula.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_stirling_formula.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_stirling_formula.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_