Lean / Reader
Lean readerWallisLimit.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports WallisBounds, WallisAverages, WallisTriples, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import WallisBoundsimport WallisAveragesimport WallisTriplesnamespace WallisEncodingopen Finset WallisGap Filteropen scoped Topologynoncomputable sectionopen Classicaldef wallis (m : ℕ) : ℝ := ∫ x in 0..Real.pi / 2, Real.sin x ^ (2 * m + 1)/-- The fixed-point rate among encodings differs from the disjoint triple rateby at most the probability of coinciding marked positions. -/theorem encoding_rate {n m : ℕ} (hm : m ≤ n) (hn : 0 < n) (w : ℝ)(hw₀ : 0 ≤ w) (hw₁ : w ≤ 1)(hr : ((univ.filter (fun t : Triple n m => DisjointTriple t ∧ MatchingRank t)).card : ℝ) =((univ.filter (fun t : Triple n m => DisjointTriple t)).card : ℝ) * w) :|frequency (univ : Finset (MarkedCode n m)) (fun z => fixed (markedDecode z)) - w|≤ ((univ.filter (fun t : Triple n m => ¬DisjointTriple t)).card : ℝ) /Fintype.card (Triple n m) := byhave liftRat : (((univ : Finset (MarkedCode n m)).filter fun z =>DisjointTriple (triple z) ∧ MatchingRank (triple z)).card : ℝ) =(((univ : Finset (MarkedCode n m)).filter fun z => DisjointTriple (triple z)).card : ℝ) * w := byrw [mark_count_lift (fun t => DisjointTriple t ∧ MatchingRank t), mark_count_lift DisjointTriple,Nat.cast_mul, Nat.cast_mul, hr]ringhave h := ratio_from_restriction (univ : Finset (MarkedCode n m))(fun z => DisjointTriple (triple z)) (fun z => fixed (markedDecode z))(fun z => MatchingRank (triple z)) (fun z _ => fixed_on_good z) w hw₀ hw₁ (by convert liftRat using 1 <;> congr)(exists_marked hm hn)have eqbad : (univ.filter (fun z : MarkedCode n m => ¬DisjointTriple (triple z))).card =(univ.filter (fun t : Triple n m => ¬DisjointTriple t)).card * Fintype.card (Equiv.Perm (Fin m)) :=mark_count_lift (fun t => ¬DisjointTriple t)have h' : |frequency (univ : Finset (MarkedCode n m)) (fun z => fixed (markedDecode z)) - w| ≤ ((univ.filter (fun z : MarkedCode n m => ¬DisjointTriple (triple z))).card : ℝ) /(Fintype.card (MarkedCode n m)) := byconvert h using 1 <;> congrrw [eqbad,count_marked,Nat.cast_mul,Nat.cast_mul] at h'simpa only [mul_div_mul_right _ _ (Nat.cast_ne_zero.mpr (Fintype.card_ne_zero : Fintype.card (Equiv.Perm (Fin m)) ≠ 0) : (_ : ℝ) ≠ 0)] using h'lemma exists_target_of_sparse {n m k : ℕ} (hm : m ≤ n) (hn : 0 < n) (hk : k < n)(hs : n * (univ.filter (fun c : Code n m => ¬Separated c.1 c.2.1)).card ≤ k * Fintype.card (Code n m)) :(target n m).Nonempty := byobtain ⟨z,_⟩ := exists_marked hm hnletI : Nonempty (Code n m) := ⟨z.1⟩have hlt : (univ.filter (fun c : Code n m => ¬Separated c.1 c.2.1)).card < Fintype.card (Code n m) := byby_contra! hexact (not_lt_of_ge (hs.trans' (Nat.mul_le_mul_left n h))) (Nat.mul_lt_mul_of_pos_right hk Fintype.card_pos)have hf : ∃ c : Code n m, Separated c.1 c.2.1 := byby_contra! hsimp [h] at hltobtain ⟨c,hc⟩ := hfexact ⟨(decode c, z.2), mem_filter.2 ⟨mem_univ _,defect_of_separated c hc⟩⟩lemma exceptional_fraction {n x k t : ℕ} (hn : 0 < n) (h : n*x ≤ k*t) :(x : ℝ)/t ≤ (k : ℝ)/n := byby_cases ht : t = 0· simp [ht]; positivityhave hp : (0:ℝ) < n := by exact_mod_cast hnhave ht' : (0:ℝ) < t := by exact_mod_cast Nat.pos_of_ne_zero htapply (div_le_div_iff₀ ht' hp).2exact_mod_cast (Nat.mul_comm n x ▸ h)theorem finite_bound_of_counts {m n k l : ℕ} (hm : m ≤ n) (hn : 0 < n)(hk : k < n)(hs : n * (univ.filter (fun c : Code n m => ¬Separated c.1 c.2.1)).card ≤ k * Fintype.card (Code n m))(hd : n * (univ.filter (fun t : Triple n m => ¬DisjointTriple t)).card ≤ l * Fintype.card (Triple n m))(w : ℝ) (hw₀ : 0 ≤ w) (hw₁ : w ≤ 1)(hr : ((univ.filter (fun t : Triple n m => DisjointTriple t ∧ MatchingRank t)).card : ℝ) =((univ.filter (fun t : Triple n m => DisjointTriple t)).card : ℝ) * w) :|expected n m - w| ≤ ((k+l : ℕ) : ℝ)/n := byhave h₁ := (transfer_marked hm (exists_target_of_sparse hm hn hk hs)).trans (exceptional_fraction hn hs)have h₂ := (encoding_rate hm hn w hw₀ hw₁ hr).trans (exceptional_fraction hn hd)let enc := frequency (univ : Finset (MarkedCode n m)) (fun z => fixed (markedDecode z))calc|expected n m - w| ≤ |expected n m - enc| + |enc - w| := abs_sub_le _ _ __ ≤ (k : ℝ)/n + (l : ℝ)/n := add_le_add h₁ h₂_ = _ := by rw [← add_div,Nat.cast_add]/-- Erickson's Problem 1.6: the expectation for a uniform permutation with alongest increasing subsequence of length n-m tends to the odd Wallis integral. -/theorem erickson_problem_1_6 (m : ℕ) :Tendsto (fun n => expected n m) atTop (𝓝 (∫ x in 0..Real.pi / 2, Real.sin x ^ (2 * m + 1))) := bylet k := m*m*(2*m+1)let l := m*m+2*mapply tendsto_iff_dist_tendsto_zero.2apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds(tendsto_const_div_atTop_nhds_zero_nat ((k+l : ℕ) : ℝ))(Eventually.of_forall (fun _ => dist_nonneg))filter_upwards [eventually_ge_atTop (k+m+1)] with n hnhave hm : m ≤ n := by omegahave hn' : 0 < n := by omegahave hk : k < n := by omegahave H := finite_bound_of_counts hm hn' hk (bad_separated_bound n m) (bad_triple_bound n m)(wallisValue m) (wallisValue_bounds m).1 (wallisValue_bounds m).2 (disjointTripleRankCount_real n m)exact (show dist _ _ ≤ _ from by simpa only [Real.dist_eq] using H)#print axioms erickson_problem_1_6endend WallisEncoding