Stirling's approximation

From testwiki
Jump to navigation Jump to search

Template:Short description Template:CS1 config Template:Dark mode invert 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!=nln⁡n−n+O(ln⁡n), where the big O notation means that, for all sufficiently large values of n, the difference between ln⁡n! and nln⁡n−n 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 log2n!=nlog2n−nlog2e+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.

History

The formula was first discovered by Abraham de MoivreTemplate:R in 1721 in the form n!∼[constant]⋅nn+12e−n.

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 2π.Template:R[1]

Derivation

The simplest version of Stirling's formula is n!=2πn(ne)n(1+O(1n)). It can be quickly obtained by approximating the sum ln⁡n!=∑j=1nln⁡j with an integral: ∑j=1nln⁡j≈∫1nln⁡xdx=nln⁡n−n+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!=ln⁡1+ln⁡2+⋯+ln⁡n.

The right-hand side of this equation minus 12(ln⁡1+ln⁡n)=12ln⁡n is the approximation by the trapezoid rule of the integral ln⁡n!−12ln⁡n≈∫1nln⁡xdx=nln⁡n−n+1,

and the error in this approximation is given by the Euler–Maclaurin formula: ln⁡n!−12ln⁡n=ln⁡1+ln⁡2+ln⁡3+⋯+ln⁡(n−1)+12ln⁡n=nln⁡n−n+1+∑k=2m(−1)kBkk(k−1)(1nk−1−1)+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!−nln⁡n+n−12ln⁡n)=1−∑k=2m(−1)kBkk(k−1)+limn→∞Rm,n.

Denote this limit as y. Because the remainder Template:Math in the Euler–Maclaurin formula satisfies Rm,n=limn→∞Rm,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)+12ln⁡n+y+∑k=2m(−1)kBkk(k−1)nk−1+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.

Alternative derivations

An alternative formula for n! using the gamma function is n!=∫0∞xne−xdx. (as can be seen by repeated integration by parts). Rewriting and changing variables Template:Math, one obtains n!=∫0∞enln⁡x−xdx=enln⁡nn∫0∞en(ln⁡y−y)dy. Applying Laplace's method one has ∫0∞en(ln⁡y−y)dy∼2πne−n, which recovers Stirling's formula: n!∼enln⁡nn2πne−n=2πn(ne)n.

Higher orders

Further corrections can also be obtained using Laplace's method. Stirling's formula to two orders is n!=2πn(ne)n(1+112n+O(1n2)).

From previous result, we know that Γ(x)∼xxe−x, so we "peel off" this dominant term, then perform two changes of variables, to obtain: x−xexΓ(x)=∫ℝex(1+t−et)dt To verify this: ∫ℝex(1+t−et)dt =t↦ln⁡t ex∫0∞tx−1e−xtdt =t↦tx x−xex∫0∞e−ttx−1dt=x−xexΓ(x).

Now the function t↦1+t−et is unimodal, with maximum value zero. Locally around zero, it looks like −t22, 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+t−et=−τ22. This equation cannot be solved in closed form, but it can be solved by serial expansion, which gives us t=τ−τ26+τ336+a4τ4+O(τ5).

Now plug back to the equation to obtain x−xexΓ(x)=∫ℝe−xτ22(1−τ3+τ212+4a4τ3+O(τ4))dτ=2π(x−12+x−3212)+O(x−52).

Notice how it is not actually necessary to 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

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=0∞znn!, 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.[2]

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−μμ)2)

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⁡μ!≈−12ln⁡(2πμ)

which can easily be rearranged to give:

ln⁡μ!≈μln⁡μ−μ+12ln⁡(2πμ)

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

Speed of convergence and error estimates

Template:Dark mode invert

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+1288n2−13951840n3−5712488320n4+163879209018880n5−⋯).

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. The coefficients have the following asymptotic formula:[3] 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.

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):Template:Cn n!∼2πn(ne)nexp⁡(112n−1360n3+11260n5−11680n7+11188n9−⋯). The kth coefficient (for the reciprocal of the (2k−1)th power of n) is directly calculated using Bernoulli numbers and ck=B2k2k(2k−1). Template:Dark mode invert

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⁡St(n)n!|, which, when small, is essentially the relative error.

Writing Stirling's series in the form ln⁡n!∼nln⁡n−n+12ln⁡(2πn)+112n−1360n3+11260n5−11680n7+⋯, 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 thatn!ennn+12∈(2π,e]for all n≥1.

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)=zln⁡z−z+12ln⁡2πz+∫0∞2arctan⁡(tz)e2πt−1dt.

Repeated integration by parts gives ln⁡Γ(z)∼zln⁡z−z+12ln⁡2πz+∑n=1N−1B2n2n(2n−1)z2n−1=zln⁡z−z+12ln⁡2πz+112z−1360z3+11260z5+…,

where Bn is the Template:Mvarth 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: ∫0∞2arctan⁡(tx)e2πt−1dt=ln⁡Γ(x)−xln⁡x+x−12ln⁡2πx.

One way to do this is by means of a convergent series of inverted rising factorials. If zn¯=z(z+1)⋯(z+n−1), then ∫0∞2arctan⁡(tx)e2πt−1dt=∑n=1∞cn(x+1)n¯, where cn=1n∫01xn¯(x−12)dx=12n∑k=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)=xln⁡x−x+12ln⁡2π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[4] Γ(x)=2πxx−12e−x+μ(x) where μ(x)=∑n=0∞((x+n+12)ln⁡(1+1x+n)−1).

Versions suitable for calculators

The approximation Γ(z)≈2πz(zezsinh⁡1z+1810z6)z and its equivalent form 2ln⁡Γ(z)≈ln⁡(2π)−ln⁡z+z(2ln⁡z+ln⁡(zsinh⁡1z+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+112z−110z))z, or equivalently, ln⁡Γ(z)≈12(ln⁡(2π)−ln⁡z)+z(ln⁡(z+112z−110z)−1).

An alternative approximation for the gamma function stated by Srinivasa Ramanujan in Ramanujan's lost notebook[5] 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 ln⁡n!≈nln⁡n−n+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)16<Γ(1+x)<π(xe)x(8x3+4x2+x+130)16.

Equation for discrete cases

For discrete cases, rather than the asymptotic formula described above, a more accurate equation, and much easier to prove result has been derived by T. S. Nanjundiah:[6]

n!=2πn(ne)neθn12n,θn∈[0,1]

or further, we can deduce that:

n!=2πn(ne)n⋅exp⁡(112n−θn′360n3)(θn′∈(0,1))

To prove this, we first assume that:

an=n!⋅n−(n+12)⋅enln=ln⁡an

It is easy to calculate that:

anan+1=n!⋅n−(n+12)⋅en(n+1)!⋅(n+1)−(n+32)⋅en+1=1e(n+1n)n+12

⟹ln−ln+1=(n+12)ln⁡(1+1n)−1

and, by combining the Taylor's series of logarithms:

ln⁡(1+x)=x−x22+x33−x44+x55−…ln⁡(1−x)=−x−x22−x33−x44−x55−…

subtracting them,

ln⁡(1+x1−x)=2(x+x33+x55+…)

let the two variables be equal,

1+x1−x=n+1n⟹x=12n+1

replacing Template:Mvar with Template:Mvar, we get:

ln⁡(1+1n)=2⋅12n+1[1+13(12n+1)2+15(12n+1)4+…]

thus, for ln we have that:

ln−ln+1=13(2n+1)2+15(2n+1)4+…

To calculate the sum, we force them to be a geometric series:

ln−ln+1<13(2n+1)2∑k=0∞[1(2n+1)2]k=13(2n+1)2⋅11−1(2n+1)2=112n(n+1)

and, it is also evident that ln−ln+1>0,

⟹ln−112n<ln+1−112(n+1)

we can let xn=ln−112n, thus we have that

xn<xn+1,ln<ln+1,∀n,xn<ln,

according to monotone convergence theorem, we know that ln is convergent, we assume that limn→+∞ln=λ

then limn→+∞an=eλ=α,

α=limn→+∞an2a2n=limn→+∞(n!)2⋅n−2n−1⋅e2n(2n)!⋅(2n)−2n−12⋅e2n=limn→+∞(n!)2⋅22n+12⋅n2n+12(2n)!⋅n2n+1=limn→+∞22n(n!)2(2n)!2n=limn→+∞2n(2n)!!(2n−1)!!

combined with Wallis Formula, we can finally derive that the limit is 2π, which finalize our proof of the asymptotic case.[6]

And notice that for every Template:Mvar, we have that ln−112n<λ<ln, rewrite the formula by substituting Template:Mvar with ln⟹ln=λ+θn12n, we get:

n!=2πn(ne)neθn12n

and, if we further elaborate on the inequality we used, by keeping the first term as original and using another geometric series with ration 13(2n+1)2, we have:[6]

ln−ln+1>13(2n+1)2(1−13(2n+1)2)−1+(15−132)1(2n+1)4=112n(n+1)(1+16n(n+1))−1+1180n2(n+1)2(1+14n(n+1))−2>112n(n+1)(1−16n(n+1))+1180n2(n+1)2(1−12n(n+1))=112n(n+1)−3n(n+1)+1360n3(n+1)3 Here, we can derive that:

ln−112n+1360n3>ln+1−112(n+1)+1360(n+1)3

since it is trivial that ln−112n<ln−112n+1360n3<ln, and limn→+∞112n=limn→+∞1360n3=0

we know that: ln−112n<λ<ln−112n+1360n3, and rewrite the equation above using ln=λ+112n−θn360n3, finally we prove that:

n!=2πn(ne)nexp⁡(112n−θn360n3)

See also

References

Template:Reflist

Further reading


Template:Notelist

Template:Commons category

Template:Calculus topics