Skip to content

Stirling approximation

Approximate large factorials, distinguish relative from absolute error, and use Robbins bounds and log-factorials in probability calculations.

Stirling approximation replaces a large factorial by a smooth expression whose ratio to the factorial tends to one. It makes the size of a combinatorial search space readable and turns factorial terms in probability models into manageable logarithms. The approximation controls relative error; a small percentage error can still represent a vast difference in the underlying count.

For positive integers nn, define

An=2πn(ne)n.A_n=\sqrt{2\pi n}\left(\frac{n}{e}\right)^n.

Stirling's formula states

n!∼Anas n→∞,meaningn!An→1.n!\sim A_n\quad\text{as }n\to\infty, \qquad\text{meaning}\qquad \frac{n!}{A_n}\to1.
The symbol ∼\sim is an asymptotic statement, not an equality at any finite nn. This is also a different object from Stirling and Bell numbers, which count partitions and related combinatorial structures.

A good percentage can hide a large miss

Imagine ten distinct candidate research tasks, each performed once, with every ordering allowed. There are exactly

10!=3,628,80010!=3{,}628{,}800

possible orders. The leading Stirling expression gives

A10≈3,598,695.619.A_{10}\approx3{,}598{,}695.619.
Define signed relative error using the exact factorial as the denominator:
εn=An−n!n!.\varepsilon_n=\frac{A_n-n!}{n!}.
At n=10n=10, ε10≈−0.0082959604\varepsilon_{10}\approx-0.0082959604, or −0.829596%-0.829596\%. The negative sign indicates an underestimate. The absolute error, ∣A10−10!∣\lvert A_{10}-10!\rvert, is about 30,104.38130{,}104.381. A non-integer output is expected from an approximation to a count; rounding it does not restore exactness.
At n=100n=100, the relative shortfall shrinks to about 0.0833%0.0833\%, yet the absolute shortfall is about 7.77×101547.77\times10^{154}. Thus n!∼Ann!\sim A_n does not mean n!−An→0n!-A_n\to0. Whether that accuracy is useful depends on whether the task concerns an order of magnitude, a probability or an exact count.
Leading Stirling approximation understates n! by 0.829596% at n=10 and 0.0833% at n=100. A separate zoom shows Robbins bounds 3,628,560.142 and 3,628,810.051 enclosing exact 10!=3,628,800; the leading value 3,598,695.619 is outside the zoom.
Leading Stirling approximation understates n! by 0.829596% at n=10 and 0.0833% at n=100. A separate zoom shows Robbins bounds 3,628,560.142 and 3,628,810.051 enclosing exact 10!=3,628,800; the leading value 3,598,695.619 is outside the zoom.
Original deterministic calculations. The top panel uses logarithmic axes; the lower panel magnifies the factorial-value interval at n=10. Open circles mark strict bounds.

A finite guarantee: Robbins' bounds

For every integer n≥1n\ge1, Robbins' inequalities give
Anexp⁡(112n+1)<n!<Anexp⁡(112n).A_n\exp\left(\frac{1}{12n+1}\right) <n!< A_n\exp\left(\frac{1}{12n}\right).

These are finite bounds, stronger information than a limiting ratio. In particular, An<n!A_n<n! throughout this domain. At n=10n=10, the lower and upper endpoints are approximately

3,628,560.142<3,628,800<3,628,810.051.3{,}628{,}560.142 <3{,}628{,}800 <3{,}628{,}810.051.
The displayed decimals are rounded endpoint values; the exact inequalities refer to the exponential expressions above. Multiplying the leading term by exp⁡(1/(12n))\exp(1/(12n)) improves this example to roughly +0.000277%+0.000277\% relative error, but produces an upper bound, not an exact correction.

Keep the logarithm when the factorial is huge

Let all logarithms below be natural, and write

Ln=nlog⁡n−n+12log⁡(2πn).L_n=n\log n-n+\frac12\log(2\pi n).

Then log⁡(n!)≈Ln\log(n!)\approx L_n. The logarithmic asymptotic expansion is

log⁡(n!)=Ln+112n−1360n3+O(n−5),n→∞.\log(n!)= L_n+\frac{1}{12n}-\frac{1}{360n^3}+O(n^{-5}), \qquad n\to\infty.
For positive integers, stopping after 1/(12n)1/(12n) leaves a negative remainder whose magnitude is less than 1/(360n3)1/(360n^3). An asymptotic expansion is used after a finite truncation; adding terms indefinitely at fixed nn is not a convergence strategy. NIST DLMF §5.11 gives the expansions and positive-real remainder rule.

For example, L1000≈5912.128095L_{1000}\approx5912.128095, whereas log⁡(1000!)≈5912.128178\log(1000!)\approx5912.128178. These are tractable numbers even when directly storing the factorial as an ordinary machine float would overflow. Exponentiating the logarithm can reintroduce that overflow, so keep calculations in log space when possible. A library log-gamma routine is usually preferable for numerical evaluation: since Γ(n+1)=n!\Gamma(n+1)=n!, it evaluates log⁡(n!)\log(n!) directly without forming n!n!. For positive real xx, the extension is Γ(x+1)∼2πx(x/e)x\Gamma(x+1)\sim\sqrt{2\pi x}(x/e)^x as x→∞x\to\infty; the integer factorial bounds above have only been stated for integer inputs.

This matters for a Poisson distribution: for k≥0k\ge0 and λ>0\lambda>0,
log⁡P(X=k)=klog⁡λ−λ−log⁡(k!).\log\mathbb{P}(X=k)=k\log\lambda-\lambda-\log(k!).
A log-likelihood can therefore use log-factorials rather than unstable products. Handle k=0k=0 exactly using log⁡(0!)=0\log(0!)=0; LkL_k contains log⁡k\log k and is not defined there. Increasing λ\lambda alone does not make Stirling accurate at a fixed small observed kk.

Factorial ratios need their own error check

For a binomial coefficient, work with
log⁡(nk)=log⁡(n!)−log⁡(k!)−log⁡((n−k)!).\log\binom{n}{k}=\log(n!)-\log(k!)-\log((n-k)!).
Substituting the leading approximation into all three terms requires large nn, kk and n−kn-k for their individual asymptotics to apply. A large universe nn does not make a small selection size kk large. Use exact small factorials or log-gamma evaluations, and assess the error of the final ratio rather than assuming the numerator's error carries over unchanged. A factorial approximation alone is not a probability model.
The SelfMadeQuant lesson Counting and Combinatorics develops why permutations and combinations contain these factorials (public preview; full lesson requires membership).

Sources

Last updated
  • factorials
  • asymptotics
  • combinatorics
  • error bounds
  • log likelihood
← Back to the glossaryBack to top ↑