import Mathlib /-! # Complementary-window duality and the cyclic Parseval identity solveathome twin-primes job #23 (formalize lane). Let `g : Fin D → ℕ` be a cyclic gap word with total `W = ∑ i, g i` (in the corpus: the gap word of a tile, `D` = census, `W` = width; positivity of the gaps is not needed below). 1. `windowSum g m i = g i + g (i+1) + ... + g (i+m-1)` with cyclic indexing by `Fin D` addition. For `m ≤ D` the window of length `m` starting at `i` and the window of length `D - m` starting at `i + m` partition `Fin D` (`windowSet_disjoint`, `windowSet_union`), so their sums add to `W` (`windowSum_add_complement`). The bijection on start positions is the rotation `i ↦ i + m` (`complementStart`). Hence `maxsum g m + minsum g (D - m) = W` (`maxsum_add_minsum`, `duality`). 2. With `mbar = W / D`, `c i = g i - mbar` and `gamma k = (1/D) ∑ i, c i * c (i + k)` over `ℚ`, `∑ k, gamma k = (1/D) (∑ i, c i)^2` (`sum_autocov_eq`) and `∑ i, c i = 0`, so `∑ k, gamma k = 0` (`parseval`). Scope. Both identities are elementary and not new: the duality is the complement identity of the circular scan statistic (Cressie 1977; `research/IMPORT-MAP.md` row 1). The implication once drawn from (2), "therefore Var(S_m)/m → 0", is REFUTED in `research/import-scanstat-01-identity.js` (B) by a permutation counterexample and is deliberately NOT formalized here. No statement about twin primes follows from either identity. -/ open Finset Fin.CommRing namespace ScanStat variable {D : ℕ} [NeZero D] /-! ## Windows and the complement bijection -/ /-- The cyclic window sum `S_m(i) = g_i + ... + g_{i+m-1}`, indices in `Fin D`. -/ def windowSum {M : Type*} [AddCommMonoid M] (g : Fin D → M) (m : ℕ) (i : Fin D) : M := ∑ j ∈ range m, g (i + (j : Fin D)) /-- The positions covered by the window of length `m` starting at `i`. -/ def windowSet (m : ℕ) (i : Fin D) : Finset (Fin D) := (range m).image fun j : ℕ => i + (j : Fin D) /-- The start of the complementary window: rotation by `m`. -/ def complementStart (D : ℕ) [NeZero D] (m : ℕ) : Fin D ≃ Fin D := Equiv.addRight (m : Fin D) lemma natCast_injOn_range : Set.InjOn (fun j : ℕ => (j : Fin D)) (range D) := by intro j hj k hk h simp only [coe_range, Set.mem_Iio] at hj hk have := congrArg Fin.val h simpa [Fin.val_natCast, Nat.mod_eq_of_lt hj, Nat.mod_eq_of_lt hk] using this lemma windowSet_disjoint {m : ℕ} (hm : m ≤ D) (i : Fin D) : Disjoint (windowSet m i) (windowSet (D - m) (complementStart D m i)) := by rw [Finset.disjoint_left] intro x hx hx' simp only [windowSet, complementStart, Equiv.coe_addRight, mem_image, mem_range] at hx hx' obtain ⟨j, hj, rfl⟩ := hx obtain ⟨k, hk, hjk⟩ := hx' have h : ((m + k : ℕ) : Fin D) = (j : Fin D) := by rw [Nat.cast_add] have := hjk rw [add_assoc] at this exact add_left_cancel this have := natCast_injOn_range (D := D) (by simp; omega) (by simp; omega) h omega lemma card_windowSet {m : ℕ} (hm : m ≤ D) (i : Fin D) : (windowSet m i).card = m := by rw [windowSet, card_image_of_injOn, card_range] intro j hj k hk h simp only [coe_range, Set.mem_Iio] at hj hk exact natCast_injOn_range (D := D) (by simp; omega) (by simp; omega) (add_left_cancel h) lemma windowSet_union {m : ℕ} (hm : m ≤ D) (i : Fin D) : windowSet m i ∪ windowSet (D - m) (complementStart D m i) = univ := by apply eq_univ_of_card rw [card_union_of_disjoint (windowSet_disjoint hm i), card_windowSet hm, card_windowSet (by omega), Fintype.card_fin] omega /-- The full window sums to the total. -/ lemma windowSum_self {M : Type*} [AddCommMonoid M] (g : Fin D → M) (i : Fin D) : windowSum g D i = ∑ x, g x := by unfold windowSum rw [← Fin.sum_univ_eq_sum_range (fun j => g (i + (j : Fin D)))] simp only [Fin.cast_val_eq_self] exact Equiv.sum_comp (Equiv.addLeft i) g /-- A window and its complementary window sum to the total `W`. -/ theorem windowSum_add_complement {M : Type*} [AddCommMonoid M] (g : Fin D → M) {m : ℕ} (hm : m ≤ D) (i : Fin D) : windowSum g m i + windowSum g (D - m) (complementStart D m i) = ∑ x, g x := by rw [← windowSum_self g i] simp only [windowSum, complementStart, Equiv.coe_addRight] rw [show range D = range (m + (D - m)) by rw [Nat.add_sub_of_le hm], sum_range_add] congr 1 refine sum_congr rfl fun k _ => ?_ rw [Nat.cast_add, add_assoc] /-! ## The duality `maxsum_m + minsum_{D-m} = W` -/ /-- `maxsum_m = max_i S_m(i)`. -/ def maxsum (g : Fin D → ℕ) (m : ℕ) : ℕ := univ.sup' univ_nonempty (windowSum g m) /-- `minsum_m = min_i S_m(i)`. -/ def minsum (g : Fin D → ℕ) (m : ℕ) : ℕ := univ.inf' univ_nonempty (windowSum g m) theorem maxsum_add_minsum (g : Fin D → ℕ) {m : ℕ} (hm : m ≤ D) : maxsum g m + minsum g (D - m) = ∑ x, g x := by apply le_antisymm · -- the maximising window's complement bounds the minimum from above obtain ⟨i, -, hi⟩ := exists_mem_eq_sup' univ_nonempty (windowSum g m) have h1 := windowSum_add_complement g hm i have h2 : minsum g (D - m) ≤ windowSum g (D - m) (complementStart D m i) := inf'_le _ (mem_univ _) rw [maxsum, hi] omega · -- the minimising window is the complement of the window starting `m` earlier obtain ⟨k, -, hk⟩ := exists_mem_eq_inf' univ_nonempty (windowSum g (D - m)) have h1 := windowSum_add_complement g hm ((complementStart D m).symm k) rw [Equiv.apply_symm_apply] at h1 have h2 : windowSum g m ((complementStart D m).symm k) ≤ maxsum g m := le_sup' _ (mem_univ _) rw [minsum, hk] omega /-- The statement as posed in the job: for `1 ≤ m ≤ D - 1`, `maxsum_m + minsum_{D-m} = W`. -/ theorem duality (g : Fin D → ℕ) (m : ℕ) (_h1 : 1 ≤ m) (h2 : m ≤ D - 1) : maxsum g m + minsum g (D - m) = ∑ x, g x := maxsum_add_minsum g (by omega) /-! ## The cyclic Parseval identity over `ℚ` -/ section Parseval variable (g : Fin D → ℚ) /-- `mbar = W / D`. -/ def mbar : ℚ := (∑ i, g i) / D /-- The centred gaps `c_i = g_i - mbar`. -/ def centred (i : Fin D) : ℚ := g i - mbar g /-- `gamma(k) = (1/D) ∑_i c_i c_{i+k}`, cyclic. -/ def gamma (k : Fin D) : ℚ := (1 / (D : ℚ)) * ∑ i, centred g i * centred g (i + k) /-- For any `f`, the cyclic autocovariance sums sum to `(1/D) (∑ f)^2`. -/ theorem sum_autocov_eq (f : Fin D → ℚ) : ∑ k, (1 / (D : ℚ)) * ∑ i, f i * f (i + k) = (1 / (D : ℚ)) * (∑ i, f i) ^ 2 := by rw [← mul_sum, sum_comm] congr 1 simp_rw [← mul_sum] rw [sq, sum_mul] refine sum_congr rfl fun i _ => ?_ congr 1 exact Equiv.sum_comp (Equiv.addLeft i) f theorem sum_centred : ∑ i, centred g i = 0 := by have hD : (D : ℚ) ≠ 0 := Nat.cast_ne_zero.mpr (NeZero.ne D) simp only [centred, mbar, sum_sub_distrib, sum_const, card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp ring /-- `∑_{k=0}^{D-1} gamma(k) = 0`. -/ theorem parseval : ∑ k, gamma g k = 0 := by simp only [gamma] rw [sum_autocov_eq (centred g), sum_centred] simp end Parseval /-- The same for a natural-number gap word, cast to `ℚ`. -/ theorem parseval_nat (g : Fin D → ℕ) : ∑ k, gamma (fun i => (g i : ℚ)) k = 0 := parseval _ end ScanStat /-! ## Axiom check and a small sanity example -/ #print axioms ScanStat.maxsum_add_minsum #print axioms ScanStat.duality #print axioms ScanStat.windowSet_union #print axioms ScanStat.parseval #print axioms ScanStat.parseval_nat /-! Evaluation-only spot checks (not used by any proof): the definitions compute the stated quantities on small words. -/ -- D = 5, g = 1..5, W = 15. Cyclic 2-windows: 3, 5, 7, 9, 6, so maxsum_2 = 9. -- Cyclic 3-windows: 6, 9, 12, 10, 8, so minsum_3 = 6. Expected (9, 6); 9 + 6 = 15. #eval (ScanStat.maxsum (D := 5) ![1, 2, 3, 4, 5] 2, ScanStat.minsum (D := 5) ![1, 2, 3, 4, 5] 3) -- D = 8, g = 3,1,4,1,5,9,2,6, W = 31: maxsum_m + minsum_{8-m} for m = 1..7. Expected all 31. #eval (List.range 7).map fun j => ScanStat.maxsum (D := 8) ![3, 1, 4, 1, 5, 9, 2, 6] (j + 1) + ScanStat.minsum (D := 8) ![3, 1, 4, 1, 5, 9, 2, 6] (8 - (j + 1)) -- The same word over ℚ: gamma(0), ..., gamma(7), then their sum. Expected sum 0. #eval let g : Fin 8 → ℚ := ![3, 1, 4, 1, 5, 9, 2, 6] ((List.finRange 8).map (ScanStat.gamma g), ∑ k, ScanStat.gamma g k)