math / data /lean /OAI /Combinatorics /BalancedRyser /Local /ChildAlignment.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
14.3 kB
import OAI.Combinatorics.BalancedRyser.Local.TupleAlignment
import OAI.Combinatorics.BalancedRyser.Probability.FiniteUniformConditioning
namespace OAI
namespace Balanced.TupleAlignment
variable {F : Type*} [Field F]
abbrev ChildOffsets (a : Bool β†’ F) :=
{o : Fin 4 β†’ Bool β†’ F // o 0 = a}
def childFreshEquiv (a : Bool β†’ F) : ChildOffsets a ≃ (Fin 3 β†’ Bool β†’ F) where
toFun o i := o.1 i.succ
invFun f := ⟨Fin.cases a f, rfl⟩
left_inv o := by
apply Subtype.ext
funext i
refine Fin.cases ?_ (fun j => ?_) i
Β· exact o.2.symm
Β· rfl
right_inv f := rfl
@[simp] theorem tuplePositionEquiv_symm_apply
(d : Fin 4 β†’ Direction F) (hd : βˆ€ i, d i β‰  d (i + 1))
(Οƒ : Fin 4 β†’ Bool) (p : Fin 4 β†’ Point F) (i : Fin 4) (b : Bool) :
(tuplePositionEquiv d hd Οƒ).symm p i b =
if b = Οƒ i then lineEval (d i) (p i) else lineEval (d i) (p (i - 1)) := by
simp [tuplePositionEquiv, offsetPairsEquiv, lineEvalEquiv]
@[simp] theorem lineEvalEquiv_symm_fst (d e : Direction F) (hne : d β‰  e) (a : F Γ— F) :
lineEval d ((lineEvalEquiv d e hne).symm a) = a.1 :=
congrArg Prod.fst ((lineEvalEquiv d e hne).apply_symm_apply a)
@[simp] theorem lineEvalEquiv_symm_snd (d e : Direction F) (hne : d β‰  e) (a : F Γ— F) :
lineEval e ((lineEvalEquiv d e hne).symm a) = a.2 :=
congrArg Prod.snd ((lineEvalEquiv d e hne).apply_symm_apply a)
def childAlignedPositions (dβ‚€ g : Direction F) (hg : dβ‚€ β‰  g)
(Οƒβ‚€ : Bool) (c : F) (a : Bool β†’ F) (x : F Γ— F) : Fin 4 β†’ Point F :=
![(lineEvalEquiv dβ‚€ g hg).symm (a Οƒβ‚€, c),
((linePointEquiv g c).symm x.1).1,
((linePointEquiv g c).symm x.2).1,
(lineEvalEquiv dβ‚€ g hg).symm (a (!Οƒβ‚€), c)]
theorem childAlignedPositions_onLine (dβ‚€ g : Direction F) (hg : dβ‚€ β‰  g)
(Οƒβ‚€ : Bool) (c : F) (a : Bool β†’ F) (x : F Γ— F) (i : Fin 4) :
OnLine (childAlignedPositions dβ‚€ g hg Οƒβ‚€ c a x i) (g,c) := by
fin_cases i
Β· exact lineEvalEquiv_symm_snd dβ‚€ g hg (a Οƒβ‚€,c)
Β· exact ((linePointEquiv g c).symm x.1).2
Β· exact ((linePointEquiv g c).symm x.2).2
Β· exact lineEvalEquiv_symm_snd dβ‚€ g hg (a (!Οƒβ‚€),c)
abbrev AlignedChildOffsets (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (c : F) (a : Bool β†’ F) :=
{o : ChildOffsets a // βˆ€ i,
OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)}
def alignedChildFromPair (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) (x : F Γ— F) :
AlignedChildOffsets d hd Οƒ g c a := by
let p := childAlignedPositions (d 0) g hg (Οƒ 0) c a x
refine ⟨⟨(tuplePositionEquiv d hd Οƒ).symm p, ?_⟩, ?_⟩
Β· funext b
rw [tuplePositionEquiv_symm_apply]
by_cases hb : b = Οƒ 0
Β· rw [ite_eq_left hb]
change lineEval (d 0) ((lineEvalEquiv (d 0) g hg).symm (a (Οƒ 0), c)) = a b
rw [lineEvalEquiv_symm_fst, hb]
Β· rw [ite_eq_right hb]
change lineEval (d 0) ((lineEvalEquiv (d 0) g hg).symm (a (!(Οƒ 0)), c)) = a b
rw [lineEvalEquiv_symm_fst]
cases b <;> cases hσ : σ 0 <;> simp_all
Β· intro i
change OnLine ((tuplePositionEquiv d hd Οƒ)
((tuplePositionEquiv d hd Οƒ).symm p) i) (g,c)
rw [Equiv.apply_symm_apply]
exact childAlignedPositions_onLine (d 0) g hg (Οƒ 0) c a x i
@[simp] theorem alignedChildFromPair_positions (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) (x : F Γ— F) :
tuplePositionEquiv d hd Οƒ (alignedChildFromPair d hd Οƒ g hg c a x).1.1 =
childAlignedPositions (d 0) g hg (Οƒ 0) c a x := by
exact (tuplePositionEquiv d hd Οƒ).apply_symm_apply _
@[simp] theorem tuplePosition_lineEval_left (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(o : Fin 4 β†’ Bool β†’ F) (i : Fin 4) :
lineEval (d i) (tuplePositionEquiv d hd Οƒ o i) = o i (Οƒ i) :=
lineEvalEquiv_symm_fst (d i) (d (i + 1)) (hd i) _
@[simp] theorem tuplePosition_lineEval_right (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(o : Fin 4 β†’ Bool β†’ F) (i : Fin 4) :
lineEval (d (i + 1)) (tuplePositionEquiv d hd Οƒ o i) =
o (i + 1) (!(Οƒ (i + 1))) :=
lineEvalEquiv_symm_snd (d i) (d (i + 1)) (hd i) _
def alignedChildToPair (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (c : F) (a : Bool β†’ F)
(o : AlignedChildOffsets d hd Οƒ g c a) : F Γ— F :=
((linePointEquiv g c) ⟨tuplePositionEquiv d hd Οƒ o.1.1 1, o.2 1⟩,
(linePointEquiv g c) ⟨tuplePositionEquiv d hd Οƒ o.1.1 2, o.2 2⟩)
def alignedChildOffsetsEquiv (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) :
AlignedChildOffsets d hd Οƒ g c a ≃ F Γ— F where
toFun := alignedChildToPair d hd Οƒ g c a
invFun := alignedChildFromPair d hd Οƒ g hg c a
left_inv o := by
apply Subtype.ext
apply Subtype.ext
apply (tuplePositionEquiv d hd Οƒ).injective
rw [alignedChildFromPair_positions]
funext i
fin_cases i
Β· change (lineEvalEquiv (d 0) g hg).symm (a (Οƒ 0), c) = _
apply (lineEvalEquiv (d 0) g hg).injective
rw [Equiv.apply_symm_apply]
apply Prod.ext
Β· change a (Οƒ 0) = lineEval (d 0) (tuplePositionEquiv d hd Οƒ o.1.1 0)
rw [tuplePosition_lineEval_left, o.1.2]
Β· exact (o.2 0).symm
Β· exact congrArg Subtype.val ((linePointEquiv g c).symm_apply_apply
⟨tuplePositionEquiv d hd Οƒ o.1.1 1, o.2 1⟩)
Β· exact congrArg Subtype.val ((linePointEquiv g c).symm_apply_apply
⟨tuplePositionEquiv d hd Οƒ o.1.1 2, o.2 2⟩)
Β· change (lineEvalEquiv (d 0) g hg).symm (a (!(Οƒ 0)), c) = _
apply (lineEvalEquiv (d 0) g hg).injective
rw [Equiv.apply_symm_apply]
apply Prod.ext
Β· change a (!(Οƒ 0)) = lineEval (d 0) (tuplePositionEquiv d hd Οƒ o.1.1 3)
have h := tuplePosition_lineEval_right d hd Οƒ o.1.1 3
simpa only [show (3 : Fin 4) + 1 = 0 from rfl, o.1.2] using h.symm
Β· exact (o.2 3).symm
right_inv x := by
simp only [alignedChildToPair, alignedChildFromPair_positions]
change ((linePointEquiv g c) ((linePointEquiv g c).symm x.1),
(linePointEquiv g c) ((linePointEquiv g c).symm x.2)) = x
simp only [Equiv.apply_symm_apply, Prod.mk.eta]
abbrev ChildOffsetsAt (j : Fin 4) (a : Bool β†’ F) :=
{o : Fin 4 β†’ Bool β†’ F // o j = a}
def childRotationEquiv (j : Fin 4) (a : Bool β†’ F) :
ChildOffsetsAt j a ≃ ChildOffsets a where
toFun o := ⟨fun i => o.1 (i + j), by simpa using o.2⟩
invFun o := ⟨fun i => o.1 (i - j), by simpa using o.2⟩
left_inv o := by
apply Subtype.ext
funext i
simp
right_inv o := by
apply Subtype.ext
funext i
simp
omit [Field F] in
theorem rotated_adjacent (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (j : Fin 4) :
βˆ€ i, d (i + j) β‰  d (i + 1 + j) := by
intro i
simpa only [add_assoc, add_comm, add_left_comm] using hd (i + j)
theorem tuplePosition_rotation (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(o : Fin 4 β†’ Bool β†’ F) (j i : Fin 4) :
tuplePositionEquiv (fun k => d (k + j)) (rotated_adjacent d hd j)
(fun k => Οƒ (k + j)) (fun k => o (k + j)) i =
tuplePositionEquiv d hd Οƒ o (i + j) := by
simp only [tuplePositionEquiv_apply, add_assoc, add_comm]
abbrev AlignedChildOffsetsAt (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (c : F) (j : Fin 4) (a : Bool β†’ F) :=
{o : ChildOffsetsAt j a // βˆ€ i,
OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)}
def alignedChildRotationEquiv (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (c : F) (j : Fin 4) (a : Bool β†’ F) :
AlignedChildOffsetsAt d hd Οƒ g c j a ≃
AlignedChildOffsets (fun i => d (i + j)) (rotated_adjacent d hd j)
(fun i => Οƒ (i + j)) g c a :=
Equiv.subtypeEquiv (childRotationEquiv j a) (fun o => by
constructor
Β· intro ho i
change OnLine (tuplePositionEquiv (fun k => d (k + j))
(rotated_adjacent d hd j) (fun k => Οƒ (k + j))
(fun k => o.1 (k + j)) i) (g,c)
rw [tuplePosition_rotation]
exact ho (i + j)
Β· intro ho i
have h := ho (i - j)
change OnLine (tuplePositionEquiv (fun k => d (k + j))
(rotated_adjacent d hd j) (fun k => Οƒ (k + j))
(fun k => o.1 (k + j)) (i - j)) (g,c) at h
rw [tuplePosition_rotation] at h
simpa only [sub_add_cancel] using h)
def alignedChildOffsetsAtEquiv (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (j : Fin 4) (hg : d j β‰  g) (c : F) (a : Bool β†’ F) :
AlignedChildOffsetsAt d hd Οƒ g c j a ≃ F Γ— F :=
(alignedChildRotationEquiv d hd Οƒ g c j a).trans
(alignedChildOffsetsEquiv (fun i => d (i + j)) (rotated_adjacent d hd j)
(fun i => Οƒ (i + j)) g (by simpa using hg) c a)
instance (a : Bool β†’ F) : Nonempty (ChildOffsets a) :=
⟨(childFreshEquiv a).symm (fun _ _ => 0)⟩
instance (j : Fin 4) (a : Bool β†’ F) : Nonempty (ChildOffsetsAt j a) :=
⟨(childRotationEquiv j a).symm ((childFreshEquiv a).symm (fun _ _ => 0))⟩
section Finite
variable [Fintype F] [DecidableEq F]
omit [Field F] in
theorem card_childOffsets (a : Bool β†’ F) :
Fintype.card (ChildOffsets a) = Fintype.card F ^ 6 := by
rw [Fintype.card_congr (childFreshEquiv a)]
simp [← pow_mul]
theorem card_alignedChildOffsets (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) :
Fintype.card (AlignedChildOffsets d hd Οƒ g c a) = Fintype.card F ^ 2 := by
rw [Fintype.card_congr (alignedChildOffsetsEquiv d hd Οƒ g hg c a)]
simp [pow_two]
theorem alignedChildOffsets_probability (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) :
(Fintype.card (AlignedChildOffsets d hd Οƒ g c a) : ℝ) /
Fintype.card (ChildOffsets a) = (Fintype.card F : ℝ)⁻¹ ^ 4 := by
rw [card_alignedChildOffsets d hd Οƒ g hg, card_childOffsets]
simp only [Nat.cast_pow]
have hq : (Fintype.card F : ℝ) β‰  0 := by exact_mod_cast Fintype.card_ne_zero
field_simp
theorem alignedChildOffsets_pmf_probability (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (hg : d 0 β‰  g) (c : F) (a : Bool β†’ F) :
((PMF.uniformOfFintype (ChildOffsets a)).toOuterMeasure
{o | βˆ€ i, OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)}).toReal =
(Fintype.card F : ℝ)⁻¹ ^ 4 := by
rw [PMF.toOuterMeasure_uniformOfFintype_apply]
rw [ENNReal.toReal_div, ENNReal.toReal_natCast, ENNReal.toReal_natCast]
exact alignedChildOffsets_probability d hd Οƒ g hg c a
omit [Field F] in
theorem card_childOffsetsAt (j : Fin 4) (a : Bool β†’ F) :
Fintype.card (ChildOffsetsAt j a) = Fintype.card F ^ 6 := by
rw [Fintype.card_congr (childRotationEquiv j a), card_childOffsets]
theorem card_alignedChildOffsetsAt (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (j : Fin 4) (hg : d j β‰  g) (c : F) (a : Bool β†’ F) :
Fintype.card (AlignedChildOffsetsAt d hd Οƒ g c j a) = Fintype.card F ^ 2 := by
rw [Fintype.card_congr (alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a)]
simp [pow_two]
theorem alignedChildOffsetsAt_pmf_probability (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (j : Fin 4) (hg : d j β‰  g) (c : F) (a : Bool β†’ F) :
((PMF.uniformOfFintype (ChildOffsetsAt j a)).toOuterMeasure
{o | βˆ€ i, OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)}).toReal =
(Fintype.card F : ℝ)⁻¹ ^ 4 := by
rw [PMF.toOuterMeasure_uniformOfFintype_apply]
change ((Fintype.card (AlignedChildOffsetsAt d hd Οƒ g c j a) : ENNReal) /
(Fintype.card (ChildOffsetsAt j a) : ENNReal)).toReal = _
rw [card_alignedChildOffsetsAt d hd Οƒ g j hg, card_childOffsetsAt]
simp only [Nat.cast_pow, ENNReal.toReal_div, ENNReal.toReal_pow, ENNReal.toReal_natCast]
have hq : (Fintype.card F : ℝ) β‰  0 := by exact_mod_cast Fintype.card_ne_zero
field_simp
theorem alignedChildOffsetsAt_supported (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (j : Fin 4) (hg : d j β‰  g) (c : F) (a : Bool β†’ F) :
βˆƒ o ∈ {o : ChildOffsetsAt j a |
βˆ€ i, OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)},
o ∈ (PMF.uniformOfFintype (ChildOffsetsAt j a)).support := by
let o := (alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a).symm (0,0)
exact ⟨o.1, o.2, PMF.mem_support_uniformOfFintype _⟩
theorem alignedChildOffsetsAt_conditional_law (d : Fin 4 β†’ Direction F)
(hd : βˆ€ i, d i β‰  d (i + 1)) (Οƒ : Fin 4 β†’ Bool)
(g : Direction F) (j : Fin 4) (hg : d j β‰  g) (c : F) (a : Bool β†’ F) :
(PMF.uniformOfFintype (ChildOffsetsAt j a)).filter
{o | βˆ€ i, OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)}
(alignedChildOffsetsAt_supported d hd Οƒ g j hg c a) =
(PMF.uniformOfFintype (F Γ— F)).map
(fun x => ((alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a).symm x).1) := by
let : Nonempty (AlignedChildOffsetsAt d hd Οƒ g c j a) :=
⟨(alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a).symm (0,0)⟩
let : Nonempty ({o : ChildOffsetsAt j a |
βˆ€ i, OnLine (tuplePositionEquiv d hd Οƒ o.1 i) (g,c)} : Set (ChildOffsetsAt j a)) :=
⟨(alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a).symm (0,0)⟩
rw [Balanced.uniform_filter_eq_subtype_map]
change (PMF.uniformOfFintype (AlignedChildOffsetsAt d hd Οƒ g c j a)).map Subtype.val = _
have h := uniform_map_equiv (alignedChildOffsetsAtEquiv d hd Οƒ g j hg c a).symm
rw [← h, PMF.map_comp]
rfl
end Finite
end Balanced.TupleAlignment
end OAI