Stirling's approximation

From testwiki
Jump to navigation Jump to search

Template:Short description

Comparison of Stirling's approximation with the factorial

In mathematics, Stirling's approximation (or Stirling's formula) is an asymptotic approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though a related but less precise result was first stated by Abraham de Moivre.Template:R

One way of stating the approximation involves the logarithm of the factorial: ln(n!)=nlnnn+O(lnn), where the big O notation means that, for all sufficiently large values of n, the difference between ln(n!) and nlnnn will be at most proportional to the logarithm of n. In computer science applications such as the worst-case lower bound for comparison sorting, it is convenient to instead use the binary logarithm, giving the equivalent form log2(n!)=nlog2nnlog2e+O(log2n). The error term in either base can be expressed more precisely as 12log(2πn)+O(1n), corresponding to an approximate formula for the factorial itself, n!2πn(ne)n. Here the sign means that the two quantities are asymptotic, that is, their ratio tends to 1 as n tends to infinity.

Derivation

Roughly speaking, the simplest version of Stirling's formula can be quickly obtained by approximating the sum ln(n!)=j=1nlnj with an integral: j=1nlnj1nlnxdx=nlnnn+1.

The full formula, together with precise estimates of its error, can be derived as follows. Instead of approximating n!, one considers its natural logarithm, as this is a slowly varying function: ln(n!)=ln1+ln2++lnn.

The right-hand side of this equation minus 12(ln1+lnn)=12lnn is the approximation by the trapezoid rule of the integral ln(n!)12lnn1nlnxdx=nlnnn+1,

and the error in this approximation is given by the Euler–Maclaurin formula: ln(n!)12lnn=12ln1+ln2+ln3++ln(n1)+12lnn=nlnnn+1+k=2m(1)kBkk(k1)(1nk11)+Rm,n,

where Bk is a Bernoulli number, and Template:Math is the remainder term in the Euler–Maclaurin formula. Take limits to find that limn(ln(n!)nlnn+n12lnn)=1k=2m(1)kBkk(k1)+limnRm,n.

Denote this limit as y. Because the remainder Template:Math in the Euler–Maclaurin formula satisfies Rm,n=limnRm,n+O(1nm),

where big-O notation is used, combining the equations above yields the approximation formula in its logarithmic form: ln(n!)=nln(ne)+12lnn+y+k=2m(1)kBkk(k1)nk1+O(1nm).

Taking the exponential of both sides and choosing any positive integer m, one obtains a formula involving an unknown quantity ey. For Template:Math, the formula is n!=eyn(ne)n(1+O(1n)).

The quantity ey can be found by taking the limit on both sides as n tends to infinity and using Wallis' product, which shows that ey=2π. Therefore, one obtains Stirling's formula: n!=2πn(ne)n(1+O(1n)).

Alternative derivations

An alternative formula for n! using the gamma function is n!=0xnexdx. (as can be seen by repeated integration by parts). Rewriting and changing variables Template:Math, one obtains n!=0enlnxxdx=enlnnn0en(lnyy)dy. Applying Laplace's method one has 0en(lnyy)dy2πnen, which recovers Stirling's formula: n!enlnnn2πnen=2πn(ne)n.

Higher orders

In fact, further corrections can also be obtained using Laplace's method. From previous result, we know that Γ(x)xxex, so we "peel off" this dominant term, then perform two changes of variables, to obtain:xxexΓ(x)=ex(1+tet)dtTo verify this: ex(1+tet)dt=tlntex0tx1extdt=tt/xxxex0ettx1dt=xxexΓ(x).

Now the function t1+tet is unimodal, with maximum value zero. Locally around zero, it looks like t2/2, 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 1+tet=τ2/2. This equation cannot be solved in closed form, but it can be solved by serial expansion, which gives us t=ττ2/6+τ3/36+a4τ4+O(τ5). Now plug back to the equation to obtainxxexΓ(x)=exτ2/2(1τ/3+τ2/12+4a4τ3+O(τ4))dτ=2π(x1/2+x3/2/12)+O(x5/2)notice how we don't need to actually find a4, since it is cancelled out by the integral. Higher orders can be achieved by computing more terms in t=τ+, which can be obtained programmatically.Template:NoteTag

Thus we get Stirling's formula to two orders:n!=2πn(ne)n(1+112n+O(1n2)).

Complex-analytic version

A complex-analysis version of this methodTemplate:R is to consider 1n! as a Taylor coefficient of the exponential function ez=n=0znn!, computed by Cauchy's integral formula as 1n!=12πi|z|=rezzn+1dz.

This line integral can then be approximated using the saddle-point method with an appropriate choice of contour radius r=rn. 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

An alternative version uses the fact that the Poisson distribution converges to a normal distribution by the Central Limit Theorem.[1]

Since the Poisson distribution with parameter λ converges to a normal distribution with mean λ and variance λ, their density functions will be approximately the same:

exp(μ)μxx!12πμexp(12(xμμ))

Evaluating this expression at the mean, at which the approximation is particularly accurate, simplifies this expression to:

exp(μ)μμμ!12πμ

Taking logs then results in:

μ+μlnμlnμ!12ln2πμ

which can easily be rearranged to give:

lnμ!μlnμμ+12ln2πμ

Evaluating at μ=n gives the usual, more precise form of Stirling's approximation.

Speed of convergence and error estimates

File:Stirling series relative error.svg
The relative error in a truncated Stirling series vs. n, for 0 to 5 terms. The kinks in the curves represent points where the truncated series coincides with Template:Math.

Stirling's formula is in fact the first approximation to the following series (now called the Stirling series):Template:R n!2πn(ne)n(1+112n+1288n213951840n35712488320n4+).

An explicit formula for the coefficients in this series was given by G. Nemes.Template:R Further terms are listed in the On-Line Encyclopedia of Integer Sequences as Template:OEIS link and Template:OEIS link. The first graph in this section shows the relative error vs. n, for 1 through all 5 terms listed above. (Bender and Orszag[2] p. 218) gives the asymptotic formula for the coefficients:A2j+1(1)j2(2j)!/(2π)2(j+1)which shows that it grows superexponentially, and that by the ratio test the radius of convergence is zero.

File:Stirling error vs number of terms.svg
The relative error in a truncated Stirling series vs. the number of terms used

As Template:Math, the error in the truncated series is asymptotically equal to the first omitted term. This is an example of an asymptotic expansion. It is not a convergent series; for any particular value of n 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, let Template:Math be the Stirling series to t terms evaluated at n. The graphs show |ln(S(n,t)n!)|, which, when small, is essentially the relative error.

Writing Stirling's series in the form ln(n!)nlnnn+12ln(2πn)+112n1360n3+11260n511680n7+, it 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.Template:Citation needed

Other bounds, due to Robbins,Template:R valid for all positive integers n are 2πn(ne)ne112n+1<n!<2πn(ne)ne112n. This upper bound corresponds to stopping the above series for ln(n!) after the 1n term. The lower bound is weaker than that obtained by stopping the series after the 1n3 term. A looser version of this bound is that n!ennn+12(2π,e] for all n1.

Stirling's formula for the gamma function

For all positive integers, n!=Γ(n+1), where Template:Math denotes the gamma 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. If Template:Math, then lnΓ(z)=zlnzz+12ln2πz+02arctan(tz)e2πt1dt.

Repeated integration by parts gives lnΓ(z)zlnzz+12ln2πz+n=1N1B2n2n(2n1)z2n1=zlnzz+12ln2πz+112z1360z3+11260z5+,

where Bn is the nth Bernoulli number (note that the limit of the sum as N is not convergent, so this formula is just an asymptotic expansion). The formula is valid for z large enough in absolute value, when Template:Math, where Template:Mvar is positive, with an error term of Template:Math. The corresponding approximation may now be written: Γ(z)=2πz(ze)z(1+O(1z)).

where the expansion is identical to that of Stirling's series above for n!, except that n is replaced with Template:Math.Template:R

A further application of this asymptotic expansion is for complex argument Template:Mvar with constant Template:Math. See for example the Stirling formula applied in Template:Math of the Riemann–Siegel theta function on the straight line Template:Math.

A convergent version of Stirling's formula

Thomas Bayes showed, in a letter to John Canton published by the Royal Society in 1763, that Stirling's formula did not give a convergent series.Template:R Obtaining a convergent version of Stirling's formula entails evaluating Binet's formula: 02arctan(tx)e2πt1dt=lnΓ(x)xlnx+x12ln2πx.

One way to do this is by means of a convergent series of inverted rising factorials. If zn¯=z(z+1)(z+n1), then 02arctan(tx)e2πt1dt=n=1cn(x+1)n¯, where cn=1n01xn¯(x12)dx=12nk=1nk|s(n,k)|(k+1)(k+2), where Template:Math denotes the Stirling numbers of the first kind. From this one obtains a version of Stirling's series lnΓ(x)=xlnxx+12ln2πx+112(x+1)+112(x+1)(x+2)++59360(x+1)(x+2)(x+3)+2960(x+1)(x+2)(x+3)(x+4)+, which converges when Template:Math. Stirling's formula may also be given in convergent form as[3] Γ(x)=2πxx12ex+μ(x) where μ(x)=n=0((x+n+12)ln(1+1x+n)1).

Versions suitable for calculators

The approximation Γ(z)2πz(zezsinh1z+1810z6)z and its equivalent form 2lnΓ(z)ln(2π)lnz+z(2lnz+ln(zsinh1z+1810z6)2) can be obtained by rearranging Stirling's extended formula and observing a coincidence between the resultant power series and the Taylor series expansion of the hyperbolic sine function. This approximation is good to more than 8 decimal digits for Template:Mvar 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.Template:R

Gergő Nemes proposed in 2007 an approximation which gives the same number of exact digits as the Windschitl approximation but is much simpler:Template:R Γ(z)2πz(1e(z+112z110z))z, or equivalently, lnΓ(z)12(ln(2π)lnz)+z(ln(z+112z110z)1).

An alternative approximation for the gamma function stated by Srinivasa Ramanujan in Ramanujan's lost notebook[4] is Γ(1+x)π(xe)x(8x3+4x2+x+130)16 for Template:Math. The equivalent approximation for Template:Math has an asymptotic error of Template:Math and is given by lnn!nlnnn+16ln(8n3+4n2+n+130)+12lnπ.

The approximation may be made precise by giving paired upper and lower bounds; one such inequality isTemplate:R π(xe)x(8x3+4x2+x+1100)1/6<Γ(1+x)<π(xe)x(8x3+4x2+x+130)1/6.

History

The formula was first discovered by Abraham de MoivreTemplate:R in the form n![constant]nn+12en.

De Moivre gave an approximate rational-number expression for the natural logarithm of the constant. Stirling's contribution consisted of showing that the constant is precisely 2π.Template:R

See also

References

Template:Reflist

Further reading


Template:Notelist

Template:Commons category

Template:Calculus topics