| | @@ -0,0 +1,202 @@ |
| 1 | +import ErdosSwarm.Erdos530Bonferroni |
| 2 | +import ErdosSwarm.Erdos530DigitTuple |
| 3 | + |
| 4 | +/-! |
| 5 | +# A second moment of image size from one extra coordinate |
| 6 | + |
| 7 | +Place two samples in the slices with first coordinate zero and one. A uniform |
| 8 | +linear map on the enlarged space acts on the second slice by a uniform |
| 9 | +translation of the original map. Averaging the overlap gives the mixed |
| 10 | +second moment of the two original image sizes. For equal samples this |
| 11 | +computes the second moment from two mean image sizes. |
| 12 | +-/ |
| 13 | + |
| 14 | +namespace ErdosSwarm.Erdos530TaggedProjection |
| 15 | + |
| 16 | +open Finset Erdos530Bonferroni Erdos530DigitTuple |
| 17 | +open scoped BigOperators |
| 18 | + |
| 19 | +section Translates |
| 20 | + |
| 21 | +variable {W : Type*} [AddCommGroup W] [Fintype W] [DecidableEq W] |
| 22 | + |
| 23 | +/-- Each ordered pair in the two sets contributes to exactly one translated overlap. -/ |
| 24 | +theorem sum_card_inter_translate (S T : Finset W) : |
| 25 | + ∑ w : W, (S ∩ T.image (w + ·)).card = S.card * T.card := by |
| 26 | + classical |
| 27 | + have hinter (w : W) : S ∩ T.image (w + ·) = S.filter (fun x => x - w ∈ T) := by |
| 28 | + ext x |
| 29 | + simp only [mem_inter, mem_filter, and_congr_right_iff] |
| 30 | + intro _ |
| 31 | + constructor |
| 32 | + · intro h |
| 33 | + obtain ⟨y, hy, rfl⟩ := mem_image.mp h |
| 34 | + simpa using hy |
| 35 | + · intro h |
| 36 | + exact mem_image.mpr ⟨x - w, h, by abel⟩ |
| 37 | + have hshift (x : W) : (∑ w : W, if x - w ∈ T then 1 else 0) = T.card := by |
| 38 | + change (∑ w : W, (fun y => if y ∈ T then 1 else 0) (Equiv.subLeft x w)) = T.card |
| 39 | + rw [(Equiv.subLeft x).sum_comp (fun y : W => if y ∈ T then (1 : ℕ) else 0)] |
| 40 | + simp |
| 41 | + simp_rw [hinter, card_filter] |
| 42 | + rw [sum_comm] |
| 43 | + simp [hshift] |
| 44 | + |
| 45 | +/-- Mean union size under a uniform translation of the second set. -/ |
| 46 | +theorem expect_card_union_translate (S T : Finset W) : |
| 47 | + (𝔼 w : W, ((S ∪ T.image (w + ·)).card : ℝ)) = |
| 48 | + S.card + T.card - (S.card : ℝ) * T.card / Fintype.card W := by |
| 49 | + simp_rw [cast_card_union, card_image_of_injective _ (add_right_injective _)] |
| 50 | + rw [expect_sub_distrib, Fintype.expect_const, Fintype.expect_eq_sum_div_card, |
| 51 | + ← Nat.cast_sum, sum_card_inter_translate, Nat.cast_mul] |
| 52 | + |
| 53 | +end Translates |
| 54 | + |
| 55 | +section LinearMaps |
| 56 | + |
| 57 | +variable (F : Type*) {V : Type*} (W : Type*) [Field F] [AddCommGroup V] [Module F V] |
| 58 | + [AddCommGroup W] [Module F W] |
| 59 | + |
| 60 | +/-- Put the two samples on the zero and one slices of a new coordinate. -/ |
| 61 | +noncomputable def taggedSet (A B : Finset V) : Finset (F × V) := by |
| 62 | + classical |
| 63 | + exact A.image (Prod.mk 0) ∪ B.image (Prod.mk 1) |
| 64 | + |
| 65 | +private def extensionEquiv : ((V →ₗ[F] W) × W) ≃ (F × V →ₗ[F] W) := |
| 66 | + (Equiv.prodComm _ _).trans |
| 67 | + (((LinearMap.ringLmapEquivSelf F F W).symm.toEquiv.prodCongr (Equiv.refl _)).trans |
| 68 | + (LinearMap.coprodEquiv F).toEquiv) |
| 69 | + |
| 70 | +variable [DecidableEq W] |
| 71 | + |
| 72 | +private theorem image_taggedSet (A B : Finset V) (L : V →ₗ[F] W) (w : W) : |
| 73 | + (taggedSet F A B).image (extensionEquiv F W (L, w)) = |
| 74 | + A.image L ∪ (B.image L).image (w + ·) := by |
| 75 | + classical |
| 76 | + simp only [taggedSet, image_union, image_image] |
| 77 | + change A.image (fun v => (0 : F) • w + L v) ∪ B.image (fun v => (1 : F) • w + L v) = _ |
| 78 | + simp [Function.comp_def] |
| 79 | + |
| 80 | +variable [Fintype W] [Fintype (V →ₗ[F] W)] [Fintype (F × V →ₗ[F] W)] |
| 81 | + |
| 82 | +/-- The mean on the tagged sample determines the mixed image-size moment. |
| 83 | +Both expectations use all linear maps, with no rank restriction. -/ |
| 84 | +theorem meanImageSize_taggedSet (A B : Finset V) : |
| 85 | + meanImageSize F W (taggedSet F A B) = |
| 86 | + meanImageSize F W A + meanImageSize F W B - |
| 87 | + (𝔼 L : V →ₗ[F] W, ((A.image L).card : ℝ) * (B.image L).card) / Fintype.card W := by |
| 88 | + calc |
| 89 | + _ = 𝔼 z : (V →ₗ[F] W) × W, ((A.image z.1 ∪ (B.image z.1).image (z.2 + ·)).card : ℝ) := by |
| 90 | + rw [meanImageSize, ← Fintype.expect_eq_sum_div_card] |
| 91 | + symm |
| 92 | + apply Fintype.expect_equiv (extensionEquiv F W) |
| 93 | + rintro ⟨L, w⟩ |
| 94 | + rw [image_taggedSet] |
| 95 | + _ = 𝔼 L : V →ₗ[F] W, 𝔼 w : W, ((A.image L ∪ (B.image L).image (w + ·)).card : ℝ) := by |
| 96 | + rw [← univ_product_univ, expect_product] |
| 97 | + _ = _ := by |
| 98 | + simp_rw [expect_card_union_translate] |
| 99 | + rw [expect_sub_distrib, expect_add_distrib, ← expect_div] |
| 100 | + simp only [meanImageSize, Fintype.expect_eq_sum_div_card] |
| 101 | + |
| 102 | +/-- For two copies of one sample, solve the tagged mean formula for the second moment. -/ |
| 103 | +theorem expect_sq_card_image (A : Finset V) : |
| 104 | + (𝔼 L : V →ₗ[F] W, ((A.image L).card : ℝ) ^ 2) = |
| 105 | + Fintype.card W * (2 * meanImageSize F W A - meanImageSize F W (taggedSet F A A)) := by |
| 106 | + have h := meanImageSize_taggedSet F W A A |
| 107 | + have hq : (Fintype.card W : ℝ) ≠ 0 := by exact_mod_cast Fintype.card_ne_zero |
| 108 | + field_simp [hq] at h |
| 109 | + linear_combination h |
| 110 | + |
| 111 | +end LinearMaps |
| 112 | + |
| 113 | +section DigitRows |
| 114 | + |
| 115 | +/-- Prefix the first digit sample by zero and the second by one. -/ |
| 116 | +def taggedRows {r : ℕ} (X Y : Finset (Fin r → Fin 8)) : Finset (Fin (r + 1) → Fin 8) := |
| 117 | + X.image (Fin.cons 0) ∪ Y.image (Fin.cons 1) |
| 118 | + |
| 119 | +/-- A common row receives a different first coordinate in each slice. -/ |
| 120 | +theorem card_taggedRows {r : ℕ} (X Y : Finset (Fin r → Fin 8)) : |
| 121 | + (taggedRows X Y).card = X.card + Y.card := by |
| 122 | + rw [taggedRows, card_union_of_disjoint, card_image_of_injective _ (Fin.cons_right_injective _), |
| 123 | + card_image_of_injective _ (Fin.cons_right_injective _)] |
| 124 | + refine disjoint_left.mpr ?_ |
| 125 | + intro x hx hy |
| 126 | + obtain ⟨a, _, rfl⟩ := mem_image.mp hx |
| 127 | + obtain ⟨b, _, h⟩ := mem_image.mp hy |
| 128 | + exact one_ne_zero (Fin.cons_inj.mp h).1 |
| 129 | + |
| 130 | +private theorem modRow_cons {r : ℕ} (p : ℕ) (d : Fin 8) (x : Fin r → Fin 8) : |
| 131 | + modRow p (Fin.cons d x) = Fin.cons (d.val : ZMod p) (modRow p x) := by |
| 132 | + funext i |
| 133 | + refine Fin.cases ?_ (fun j => ?_) i <;> simp [modRow] |
| 134 | + |
| 135 | +private theorem meanImageSize_image_equiv {F V V' W : Type*} [Field F] [AddCommGroup V] |
| 136 | + [Module F V] [AddCommGroup V'] [Module F V'] [AddCommGroup W] [Module F W] |
| 137 | + [DecidableEq V'] [DecidableEq W] [Fintype (V →ₗ[F] W)] [Fintype (V' →ₗ[F] W)] |
| 138 | + (e : V ≃ₗ[F] V') (A : Finset V) : |
| 139 | + meanImageSize F W (A.image e) = meanImageSize F W A := by |
| 140 | + let changeDomain : (V →ₗ[F] W) ≃ₗ[F] (V' →ₗ[F] W) := e.arrowCongr (LinearEquiv.refl F W) |
| 141 | + simp only [meanImageSize, ← Fintype.expect_eq_sum_div_card] |
| 142 | + symm |
| 143 | + apply Fintype.expect_equiv changeDomain.toEquiv |
| 144 | + intro L |
| 145 | + have himage : (A.image e).image (changeDomain L) = A.image L := by |
| 146 | + rw [image_image] |
| 147 | + apply image_congr |
| 148 | + intro v _ |
| 149 | + simp [changeDomain, LinearEquiv.arrowCongr_apply] |
| 150 | + exact congrArg (fun S : Finset W => (S.card : ℝ)) himage.symm |
| 151 | + |
| 152 | +variable {r p : ℕ} [Fact p.Prime] |
| 153 | + [Fintype ((Fin r → ZMod p) →ₗ[ZMod p] ZMod p × ZMod p)] |
| 154 | + [Fintype ((Fin (r + 1) → ZMod p) →ₗ[ZMod p] ZMod p × ZMod p)] |
| 155 | + |
| 156 | +/-- Mixed image-size moment for the two digit samples, expressed by the same |
| 157 | +mean image size used for plane projections of the eight-digit box. -/ |
| 158 | +theorem meanImageSize_taggedRows (X Y : Finset (Fin r → Fin 8)) : |
| 159 | + meanImageSize (ZMod p) (ZMod p × ZMod p) ((taggedRows X Y).image (modRow p)) = |
| 160 | + meanImageSize (ZMod p) (ZMod p × ZMod p) (X.image (modRow p)) + |
| 161 | + meanImageSize (ZMod p) (ZMod p × ZMod p) (Y.image (modRow p)) - |
| 162 | + (𝔼 L : (Fin r → ZMod p) →ₗ[ZMod p] ZMod p × ZMod p, |
| 163 | + (((X.image (modRow p)).image L).card : ℝ) * ((Y.image (modRow p)).image L).card) / |
| 164 | + (p : ℝ) ^ 2 := by |
| 165 | + classical |
| 166 | + let e := Fin.consLinearEquiv (ZMod p) (fun _ : Fin (r + 1) => ZMod p) |
| 167 | + let _ := Fintype.ofEquiv _ (extensionEquiv (ZMod p) (V := Fin r → ZMod p) (ZMod p × ZMod p)) |
| 168 | + have htag : (taggedRows X Y).image (modRow p) = |
| 169 | + (taggedSet (ZMod p) (X.image (modRow p)) (Y.image (modRow p))).image e := by |
| 170 | + simp only [taggedRows, taggedSet, image_union, image_image] |
| 171 | + congr 1 |
| 172 | + · apply image_congr |
| 173 | + intro x _ |
| 174 | + change modRow p (Fin.cons 0 x) = Fin.cons 0 (modRow p x) |
| 175 | + simpa using modRow_cons p 0 x |
| 176 | + · apply image_congr |
| 177 | + intro x _ |
| 178 | + change modRow p (Fin.cons 1 x) = Fin.cons 1 (modRow p x) |
| 179 | + simpa using modRow_cons p 1 x |
| 180 | + rw [htag, meanImageSize_image_equiv, meanImageSize_taggedSet] |
| 181 | + simp [Fintype.card_prod, ZMod.card, sq] |
| 182 | + |
| 183 | +/-- The mean squared empty fraction equals the mean empty fraction after duplicating |
| 184 | +the sample with a binary prefix. The formula includes the empty sample. -/ |
| 185 | +theorem expect_sq_emptyFraction (X : Finset (Fin r → Fin 8)) : |
| 186 | + (𝔼 L : (Fin r → ZMod p) →ₗ[ZMod p] ZMod p × ZMod p, |
| 187 | + (1 - (((X.image (modRow p)).image L).card : ℝ) / (p : ℝ) ^ 2) ^ 2) = |
| 188 | + 1 - meanImageSize (ZMod p) (ZMod p × ZMod p) ((taggedRows X X).image (modRow p)) / |
| 189 | + (p : ℝ) ^ 2 := by |
| 190 | + let q := (p : ℝ) ^ 2 |
| 191 | + let J (L : (Fin r → ZMod p) →ₗ[ZMod p] ZMod p × ZMod p) : ℝ := |
| 192 | + ((X.image (modRow p)).image L).card |
| 193 | + change (𝔼 L, (1 - J L / q) ^ 2) = 1 - _ / q |
| 194 | + have hpoly (j : ℝ) : (1 - j / q) ^ 2 = 1 - (j + j - j * j / q) / q := by ring |
| 195 | + simp_rw [hpoly] |
| 196 | + rw [expect_sub_distrib, Fintype.expect_const, ← expect_div, expect_sub_distrib, |
| 197 | + expect_add_distrib, ← expect_div, meanImageSize_taggedRows] |
| 198 | + simp only [J, q, meanImageSize, Fintype.expect_eq_sum_div_card] |
| 199 | + |
| 200 | +end DigitRows |
| 201 | + |
| 202 | +end ErdosSwarm.Erdos530TaggedProjection |