Documentation

Mathlib.Analysis.SpecialFunctions.Stirling

Stirling's formula #

This file proves Stirling's formula for the factorial. It states that $n!$ grows asymptotically like $\sqrt{2\pi n}(\frac{n}{e})^n$.

Also some global bounds on the factorial function and the Stirling sequence are proved.

Proof outline #

The proof follows: https://proofwiki.org/wiki/Stirling%27s_Formula.

We proceed in two parts.

Part 1: We consider the sequence $a_n$ of fractions $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$ and prove that this sequence converges to a real, positive number $a$. For this the two main ingredients are

Part 2: We use the fact that the series defined in part 1 converges against a real number $a$ and prove that $a = \sqrt{\pi}$. Here the main ingredient is the convergence of Wallis' product formula for ฯ€.

Part 1 #

https://proofwiki.org/wiki/Stirling%27s_Formula#Part_1

noncomputable def Stirling.stirlingSeq (n : โ„•) :

Define stirlingSeq n as $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$. Stirling's formula states that this sequence has limit $\sqrt(ฯ€)$.

Equations
    Instances For
      theorem Stirling.log_stirlingSeq_diff_hasSum (m : โ„•) :
      HasSum (fun (k : โ„•) => 1 / (2 * โ†‘(k + 1) + 1) * ((1 / (2 * โ†‘(m + 1) + 1)) ^ 2) ^ (k + 1)) (Real.log (stirlingSeq (m + 1)) - Real.log (stirlingSeq (m + 2)))

      The sequence log (stirlingSeq (m + 1)) - log (stirlingSeq (m + 2)) has the series expansion โˆ‘ 1 / (2 * (k + 1) + 1) * (1 / 2 * (m + 1) + 1)^(2 * (k + 1)).

      The sequence log โˆ˜ stirlingSeq โˆ˜ succ is monotone decreasing

      theorem Stirling.log_stirlingSeq_diff_le_geo_sum (n : โ„•) :
      Real.log (stirlingSeq (n + 1)) - Real.log (stirlingSeq (n + 2)) โ‰ค (1 / (2 * โ†‘(n + 1) + 1)) ^ 2 / (1 - (1 / (2 * โ†‘(n + 1) + 1)) ^ 2)

      We have a bound for successive elements in the sequence log (stirlingSeq k).

      We have the bound log (stirlingSeq n) - log (stirlingSeq (n+1)) โ‰ค 1/(4 n^2)

      For any n, we have log_stirlingSeq 1 - log_stirlingSeq n โ‰ค 1/4 * โˆ‘' 1/k^2

      The sequence log_stirlingSeq is bounded below for n โ‰ฅ 1.

      The sequence stirlingSeq is positive for n > 0

      The sequence stirlingSeq has a positive lower bound.

      The sequence stirlingSeq โˆ˜ succ is monotone decreasing

      Part 2 #

      https://proofwiki.org/wiki/Stirling%27s_Formula#Part_2

      theorem Stirling.tendsto_self_div_two_mul_self_add_one :
      Filter.Tendsto (fun (n : โ„•) => โ†‘n / (2 * โ†‘n + 1)) Filter.atTop (nhds (1 / 2))

      The sequence n / (2 * n + 1) tends to 1/2

      theorem Stirling.stirlingSeq_pow_four_div_stirlingSeq_pow_two_eq (n : โ„•) (hn : n โ‰  0) :
      stirlingSeq n ^ 4 / stirlingSeq (2 * n) ^ 2 * (โ†‘n / (2 * โ†‘n + 1)) = Real.Wallis.W n

      For any n โ‰  0, we have the identity (stirlingSeq n)^4 / (stirlingSeq (2*n))^2 * (n / (2 * n + 1)) = W n, where W n is the n-th partial product of Wallis' formula for ฯ€ / 2.

      Suppose the sequence stirlingSeq (defined above) has the limit a โ‰  0. Then the Wallis sequence W n has limit a^2 / 2.

      theorem Stirling.factorial_isEquivalent_stirling :
      Asymptotics.IsEquivalent Filter.atTop (fun (n : โ„•) => โ†‘n.factorial) fun (n : โ„•) => โˆš(2 * โ†‘n * Real.pi) * (โ†‘n / Real.exp 1) ^ n

      Stirling's Formula, formulated in terms of Asymptotics.IsEquivalent.

      Global bounds #

      The Stirling sequence is bounded below by โˆšฯ€, for all positive naturals. Note that this bound holds for all n > 0, rather than for sufficiently large n: it is effective.

      theorem Stirling.le_factorial_stirling (n : โ„•) :
      โˆš(2 * Real.pi * โ†‘n) * (โ†‘n / Real.exp 1) ^ n โ‰ค โ†‘n.factorial

      Stirling's approximation gives a lower bound for n! for all n. The left-hand side is formulated to mimic the usual informal description of the approximation. See also factorial_isEquivalent_stirling which says these are asymptotically equivalent. That statement gives an upper bound also, but requires sufficiently large n. In contrast, this one is only a lower bound, but holds for all n. Sharper bounds due to Robbins are available, but are not yet formalised.

      theorem Stirling.le_log_factorial_stirling {n : โ„•} (hn : n โ‰  0) :
      โ†‘n * Real.log โ†‘n - โ†‘n + Real.log โ†‘n / 2 + Real.log (2 * Real.pi) / 2 โ‰ค Real.log โ†‘n.factorial

      Stirling's approximation gives a lower bound for log n! for all positive n. The left-hand side is formulated in decreasing order in n: the higher order terms are first. This is a consequence of le_factorial_stirling, but is stated separately since the logarithmic version is sometimes more practical, and having this version eases algebraic calculations for applications. Sharper bounds due to Robbins are available, but are not yet formalised. These would add lower order terms (beginning with (12 * n)โปยน) to the left-hand side.