Comparison of Stirling's approximation (pink) with the factorial (blue)
Inmathematics,Stirling's approximation (orStirling's formula) is anasymptotic approximation forfactorials. It is a good approximation, leading to accurate results even for small values of. It is named afterJames Stirling, though a related but less precise result was first stated byAbraham de Moivre.[1][2][3]
One way of stating the approximation involves thelogarithm of the factorial:where thebigO notation means that, for all sufficiently large values of, the difference between and will be at most proportional to the logarithm of. In computer science applications such as theworst-case lower bound for comparison sorting, it is convenient to instead use thebinary logarithm, giving the equivalent form The error term in either base can be expressed more precisely as, corresponding to an approximate formula for the factorial itself,Here the sign means that the two quantities are asymptotic, that is, their ratio tends to 1 as tends to infinity.
De Moivre gave an approximate rational-number expression for the natural logarithm of the constant. Stirling's contribution in 1730 consisted of showing that the constant is precisely.[3][4]
The simplest version of Stirling's formula isIt can be quickly obtained by approximating the sumwith anintegral:
The full formula, together with precise estimates of its error, can be derived as follows. Instead of approximating, one considers itsnatural logarithm, as this is aslowly varying function:
The right-hand side of this equation minusis the approximation by thetrapezoid rule of the integral
where is aBernoulli number, andRm,n is the remainder term in the Euler–Maclaurin formula. Take limits to find that
Denote this limit as. Because the remainderRm,n in the Euler–Maclaurin formula satisfies
wherebig-O notation is used, combining the equations above yields the approximation formula in its logarithmic form:
Taking the exponential of both sides and choosing any positive integer, one obtains a formula involving an unknown quantity. Form = 1, the formula is
The quantity can be found by taking the limit on both sides as tends to infinity and usingWallis' product, which shows that. Therefore, one obtains Stirling's formula.
An alternative formula for using thegamma function is(as can be seen by repeated integration by parts). Rewriting and changing variablesx =ny, one obtainsApplyingLaplace's method one haswhich recovers Stirling's formula:
Further corrections can also be obtained using Laplace's method. Stirling's formula to two orders is
From previous result, we know that, so we "peel off" this dominant term, then perform two changes of variables, to obtain:To verify this:.
Now the function is unimodal, with maximum value zero. Locally around zero, it looks like, which is why we are able to perform Laplace's method. In order to extend Laplace's method to higher orders, we perform another change of variables by. This equation cannot be solved in closed form, but it can be solved by serial expansion, which gives us. Now plug back to the equation to obtainnotice how we don't need to actually find, since it is cancelled out by the integral. Higher orders can be achieved by computing more terms in, which can be obtained programmatically.[note 1]
This line integral can then be approximated using thesaddle-point method with an appropriate choice of contour radius. The dominant portion of the integral near the saddle point is then approximated by a real integral and Laplace's method, while the remaining portion of the integral can be bounded above to give an error term.
Using the Central Limit Theorem and the Poisson distribution
Since the Poisson distribution with parameter converges to a normal distribution with mean and variance, theirdensity functions will be approximately the same:
Evaluating this expression at the mean, at which the approximation is particularly accurate, simplifies this expression to:
Taking logs then results in
which can easily be rearranged to give:
Evaluating at gives the usual, more precise form of Stirling's approximation.
The relative error in a truncated Stirling series vs., for 0 to 5 terms. The kinks in the curves represent points where the truncated series coincides withΓ(n + 1).
Stirling's formula is in fact the first approximation to the following series (now called theStirling series):[7]
An explicit formula for the coefficients in this series was given by G. Nemes.[8] Further terms are listed in theOn-Line Encyclopedia of Integer Sequences asA001163 andA001164. The first graph in this section shows therelative error vs., for 1 through all 5 terms listed above. (Bender and Orszag[9] p. 218) gives the asymptotic formula for the coefficients:which shows that it grows superexponentially, and that by theratio test theradius of convergence is zero.
However, the representation obtained directly from the Euler–Maclaurin approximation, in which the correction term itself is the argument of the exponential function, converges much faster (needs half the number of correction terms for the same accuracy):Theth coefficient (for the reciprocal of theth power of) is directly calculated using Bernoulli numbers and
The relative error in a truncated Stirling series vs. the number of terms used
Asn → ∞, the error in the truncated series is asymptotically equal to the first omitted term. This is an example of anasymptotic expansion. It is not aconvergent series; for anyparticular value of there are only so many terms of the series that improve accuracy, after which accuracy worsens. This is shown in the next graph, which shows the relative error versus the number of terms in the series, for larger numbers of terms. More precisely, letSt (n) be the Stirling series to terms evaluated at . The graphs showwhich, when small, is essentially the relative error.
Writing Stirling's series in the formit is known that the error in truncating the series is always of the opposite sign and at most the same magnitude as the first omitted term.[citation needed]
Other bounds, due to Robbins,[10] valid for all positive integers areThis upper bound corresponds to stopping the above series for after the term.The lower bound is weaker than that obtained by stopping the series after the term. A looser version of this bound is that for all.
For all positive integers,whereΓ denotes thegamma function.
However, the gamma function, unlike the factorial, is more broadly defined for all complex numbers other than non-positive integers; nevertheless, Stirling's formula may still be applied. IfRe(z) > 0, then
Repeated integration by parts gives
where is thethBernoulli number (note that the limit of the sum as is not convergent, so this formula is just anasymptotic expansion). The formula is valid for large enough in absolute value, when|arg(z)| < π −ε, whereε is positive, with an error term ofO(z−2N+ 1). The corresponding approximation may now be written:
where the expansion is identical to that of Stirling's series above for, except that is replaced withz − 1.[11]
A further application of this asymptotic expansion is for complex argumentz with constantRe(z). See for example the Stirling formula applied inIm(z) =t of theRiemann–Siegel theta function on the straight line1/4 +it.
One way to do this is by means of a convergent series of invertedrising factorials. Ifthenwherewheres(n, k) denotes theStirling numbers of the first kind. From this one obtains a version of Stirling's serieswhich converges whenRe(x) > 0. Stirling's formula may also be given in convergent form as[13]where
The approximationand its equivalent formcan be obtained by rearranging Stirling's extended formula and observing a coincidence between the resultantpower series and theTaylor series expansion of thehyperbolic sine function. This approximation is good to more than 8 decimal digits forz with a real part greater than 8. Robert H. Windschitl suggested it in 2002 for computing the gamma function with fair accuracy on calculators with limited program or register memory.[14]
Gergő Nemes proposed in 2007 an approximation which gives the same number of exact digits as the Windschitl approximation but is much simpler:[15]or equivalently,
An alternative approximation for the gamma function stated bySrinivasa Ramanujan inRamanujan's lost notebook[16] isforx ≥ 0. The equivalent approximation forlnn! has an asymptotic error of1/1400n3 and is given by
The approximation may be made precise by giving paired upper and lower bounds; one such inequality is[17][18][19][20]
^abLe Cam, L. (1986), "The central limit theorem around 1935",Statistical Science,1 (1):78–96,doi:10.1214/ss/1177013818,JSTOR2245503,MR0833276; see p. 81, "The result, obtained using a formula originally proved by de Moivre but now called Stirling's formula, occurs in his 'Doctrine of Chances' of 1733."
^abPearson, Karl (1924), "Historical note on the origin of the normal curve of errors",Biometrika,16 (3/4): 402–404 [p. 403],doi:10.2307/2331714,JSTOR2331714,I consider that the fact that Stirling showed that De Moivre's arithmetical constant was does not entitle him to claim the theorem, [...]
^MacKay, David J. C. (2019),Information theory, inference, and learning algorithms (22nd printing ed.), Cambridge: Cambridge University Press,ISBN978-0-521-64298-9
^Olver, F. W. J.; Olde Daalhuis, A. B.; Lozier, D. W.; Schneider, B. I.; Boisvert, R. F.; Clark, C. W.; Miller, B. R. & Saunders, B. V.,"5.11 Gamma function properties: Asymptotic Expansions",NIST Digital Library of Mathematical Functions, Release 1.0.13 of 2016-09-16
^Nemes, Gergő (2010), "On the coefficients of the asymptotic expansion of",Journal of Integer Sequences,13 (6): 5
^Bender, Carl M.; Orszag, Steven A. (2009),Advanced mathematical methods for scientists and engineers. 1: Asymptotic methods and perturbation theory (Nachdr. ed.), New York, NY: Springer,ISBN978-0-387-98931-0
^Robbins, Herbert (1955), "A Remark on Stirling's Formula",The American Mathematical Monthly,62 (1):26–29,doi:10.2307/2308012,JSTOR2308012
^Spiegel, M. R. (1999),Mathematical handbook of formulas and tables, McGraw-Hill, p. 148
^Mortici, Cristinel (2011), "Ramanujan's estimate for the gamma function via monotonicity arguments",Ramanujan J.,25 (2):149–154,doi:10.1007/s11139-010-9265-y,S2CID119530041
^Mortici, Cristinel (2011), "Improved asymptotic formulas for the gamma function",Comput. Math. Appl.,61 (11):3364–3369,doi:10.1016/j.camwa.2011.04.036.
series=tau-tau^2/6+tau^3/36+tau^4*a+tau^5*b;(*pick the right a,b to make the series equal 0 at higher orders*)Series[tau^2/2+1+t-Exp[t]/.t->series,{tau,0,8}](*now do the integral*)integral=Integrate[Exp[-x*tau^2/2]*D[series/.a->0/.b->0,tau],{tau,-Infinity,Infinity}];Simplify[integral/Sqrt[2*Pi]*Sqrt[x]]