Planar scattering definitions and support in WordChain
DefinitionMarkovScatteringPlanar_WordChainmarkov-scattering
Defines the finite word states, append and erase transitions, their kernel, and the stationary mass. Includes the proofs needed to construct the certified rooted model.
Definition code
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Field.Defs
import Mathlib.Algebra.Order.BigOperators.Group.Finset
import Mathlib.Algebra.Order.Field.Basic
import Mathlib.Data.Finset.Dedup
import Mathlib.Data.Finset.Powerset
import Mathlib.Data.Finset.Union
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Fintype.Card
import Mathlib.Data.List.Permutation
import Mathlib.Logic.Relation
import Definitions.Def_MarkovScatteringPlanar_Orderings
open scoped BigOperators
namespace MarkovScattering.WordChain
/-- Words in reverse chronological order. The head is the most recently appended letter. -/
abbrev State (m : ℕ) := {w : List (Fin m) // w.Nodup ∧ w.length < m}
variable {m : ℕ}
noncomputable instance : Fintype (State m) :=
Fintype.ofFinset ((Finset.univ : Finset (Fin m)).powerset.biUnion (fun S =>
(orders S).filter fun w => w.length < m)) (by
intro w
change _ ↔ w.Nodup ∧ w.length < m
constructor
· intro hw
obtain ⟨S, _, hw⟩ := Finset.mem_biUnion.mp hw
obtain ⟨hw, hlen⟩ := Finset.mem_filter.mp hw
exact ⟨((mem_orders S w).mp hw).1, hlen⟩
· rintro ⟨hn, hl⟩
exact Finset.mem_biUnion.mpr ⟨w.toFinset, by simp,
Finset.mem_filter.mpr ⟨(mem_orders _ _).mpr ⟨hn, rfl⟩, hl⟩⟩)
def root (hm : 0 < m) : State m := ⟨[], List.nodup_nil, hm⟩
/-- The maximum length allowed in the host is one less than the alphabet size. -/
def leaf (w : State m) : Prop := w.val.length = m - 1
instance : DecidablePred (leaf (m := m)) :=
fun _ => inferInstanceAs (Decidable (_ = _))
/-- The letter set of a state. -/
def letters (w : State m) : Finset (Fin m) := w.val.toFinset
@[simp] theorem card_letters (w : State m) : (letters w).card = w.val.length := by
rw [letters, List.card_toFinset, w.property.1.dedup]
theorem letters_ne_univ (w : State m) : (letters w) ≠ Finset.univ := by
intro h
have hw := w.property.2
rw [← card_letters, h, Finset.card_univ, Fintype.card_fin] at hw
exact (lt_irrefl _ hw)
theorem letters_nonempty {w : State m} (hw : w.val ≠ []) : (letters w).Nonempty := by
rw [← Finset.card_pos, card_letters, List.length_pos_iff]
exact hw
/-- Erasing the most recent letter, with the empty word as a fixed point. -/
def parent (w : State m) : State m :=
⟨w.val.tail, w.property.1.tail, (by have hw := w.property.2; rw [List.length_tail]; omega)⟩
/-- Extend by an unused letter when the result is still shorter than the alphabet size. -/
def extend (w : State m) (a : Fin m) (ha : a ∉ w.val)
(hw : w.val.length + 1 < m) : State m :=
⟨a :: w.val, List.nodup_cons.mpr ⟨ha, w.property.1⟩, hw⟩
/-- The directed edge given by one append operation. -/
def Child (w v : State m) : Prop := v.val ≠ [] ∧ parent v = w
instance (w v : State m) : Decidable (Child w v) :=
inferInstanceAs (Decidable (_ ∧ _))
theorem child_length {w v : State m} (h : Child w v) :
v.val.length = w.val.length + 1 := by
have ht := congrArg Subtype.val h.2
change v.val.tail = w.val at ht
rcases hv : v.val with _ | ⟨a, t⟩
· exact (h.1 hv).elim
· simp [hv] at ht ⊢
rw [ht]
theorem child_ne {w v : State m} (h : Child w v) : w ≠ v := by
intro he
have := child_length h
rw [he] at this
omega
theorem child_asymm {w v : State m} (h : Child w v) : ¬ Child v w := by
intro h'
have := child_length h
have := child_length h'
omega
@[simp] theorem child_extend (w : State m) (a : Fin m) (ha : a ∉ w.val)
(hw : w.val.length + 1 < m) : Child w (extend w a ha hw) :=
⟨List.cons_ne_nil _ _, Subtype.ext rfl⟩
theorem parent_child (w : State m) (hw : w.val ≠ []) : Child (parent w) w := ⟨hw, rfl⟩
noncomputable def children (w : State m) : Finset (State m) :=
Finset.univ.filter (Child w)
theorem children_card (w : State m) :
(children w).card = if w.val.length + 1 < m then m - w.val.length else 0 := by
classical
split_ifs with hw
· have hcard : (Finset.univ \ (letters w)).card = m - w.val.length := by
rw [Finset.card_sdiff_of_subset (Finset.subset_univ _), Finset.card_univ,
Fintype.card_fin, card_letters]
rw [← hcard]
symm
refine Finset.card_bij (fun a ha =>
extend w a (fun hm => (Finset.mem_sdiff.mp ha).2 (List.mem_toFinset.mpr hm)) hw)
(fun a ha => Finset.mem_filter.mpr ⟨Finset.mem_univ _, child_extend ..⟩) ?_ ?_
· intro a ha b hb hab
exact (List.cons.inj (congrArg Subtype.val hab)).1
· intro v hv
have hc := (Finset.mem_filter.mp hv).2
have ht := congrArg Subtype.val hc.2
change v.val.tail = w.val at ht
rcases hval : v.val with _ | ⟨a, t⟩
· exact (hc.1 hval).elim
· have ht' : t = w.val := by simpa [hval] using ht
have hn := v.property.1
rw [hval, ht'] at hn
have ha : a ∈ Finset.univ \ (letters w) := Finset.mem_sdiff.mpr
⟨Finset.mem_univ _, fun hm => (List.nodup_cons.mp hn).1 (List.mem_toFinset.mp hm)⟩
exact ⟨a, ha, Subtype.ext (by simpa [extend, hval] using congrArg (List.cons a) ht'.symm)⟩
· rw [Finset.card_eq_zero]
apply Finset.eq_empty_iff_forall_notMem.mpr
intro v hv
have hh := child_length (Finset.mem_filter.mp hv).2
exact hw (hh ▸ v.property.2)
/-- All maximal words; each is one letter short of a full ordering. -/
noncomputable def leaves (m : ℕ) : Finset (State m) := Finset.univ.filter leaf
@[simp] theorem mem_leaves (w : State m) : w ∈ leaves m ↔ leaf w := by simp [leaves]
theorem root_not_leaf (hm : 0 < m) (hm₂ : 2 ≤ m) : ¬ leaf (root hm) := by
change ¬ (0 = m - 1)
omega
/-- Record a visit at the empty word or at any maximal word. -/
noncomputable def window (hm : 0 < m) : Finset (State m) := insert (root hm) (leaves m)
@[simp] theorem root_mem_window (hm : 0 < m) : root hm ∈ window hm := by simp [window]
theorem full_order_length {l : List (Fin m)} (hl : l ∈ orders Finset.univ) :
l.length = m := by
obtain ⟨hn, hs⟩ := (mem_orders _ _).mp hl
have hc := congrArg Finset.card hs
rw [List.card_toFinset, hn.dedup, Finset.card_univ, Fintype.card_fin] at hc
exact hc
def dropLast (hm : 0 < m) (l : List (Fin m)) (hl : l ∈ orders Finset.univ) :
State m :=
⟨l.tail, ((mem_orders _ _).mp hl).1.tail, by
rw [List.length_tail, full_order_length hl]
omega⟩
theorem dropLast_leaf (hm : 0 < m) (l : List (Fin m))
(hl : l ∈ orders Finset.univ) : leaf (dropLast hm l hl) := by
simp [leaf, dropLast, full_order_length hl]
theorem full_order_ext {l₁ l₂ : List (Fin m)}
(h₁ : l₁ ∈ orders Finset.univ) (h₂ : l₂ ∈ orders Finset.univ)
(ht : l₁.tail = l₂.tail) : l₁ = l₂ := by
cases l₁ with
| nil =>
cases l₂ with
| nil => rfl
| cons a t =>
have h := full_order_length h₁
have h' := full_order_length h₂
simp at h h'
omega
| cons a t =>
cases l₂ with
| nil =>
have h := full_order_length h₁
have h' := full_order_length h₂
simp at h h'
omega
| cons b u =>
change t = u at ht
subst u
have ha : a ∉ t := (List.nodup_cons.mp ((mem_orders _ _).mp h₁).1).1
have hb : a ∈ b :: t := List.mem_toFinset.mp (by
rw [((mem_orders _ _).mp h₂).2]
exact Finset.mem_univ _)
have hab : a = b := (List.mem_cons.mp hb).resolve_right ha
rw [hab]
theorem exists_full_order (w : State m) (hw : leaf w) :
∃ l, l ∈ orders Finset.univ ∧ l.tail = w.val := by
have hmissing : ∃ a : Fin m, a ∉ letters w := by
by_contra! h
exact letters_ne_univ w (Finset.eq_univ_of_forall h)
obtain ⟨a, ha⟩ := hmissing
have hn : (a :: w.val).Nodup := List.nodup_cons.mpr
⟨fun h => ha (List.mem_toFinset.mpr h), w.property.1⟩
refine ⟨a :: w.val, (mem_orders _ _).mpr ⟨hn, ?_⟩, rfl⟩
apply Finset.eq_univ_of_card
rw [List.card_toFinset, hn.dedup, Fintype.card_fin, List.length_cons]
have hlt := w.property.2
unfold leaf at hw
omega
section Weights
variable {K : Type*} [Field K]
/-- The uniform chance for any permitted append. -/
def appendRate (m : ℕ) : K := 1 / (m : K)
/-- The erasure rate of a nonempty word. -/
def eraseRate (D : Finset (Fin m) → K) (C : K) (w : State m) : K :=
D (letters w) / ((m : K) * C)
/-- Local off-diagonal moves of the host. -/
def moves (D : Finset (Fin m) → K) (C : K) (w v : State m) : K :=
(if Child w v then appendRate m else 0) + (if Child v w then eraseRate D C w else 0)
/-- Append uniformly, erase with the scaled slack, and hold with the remaining probability. -/
noncomputable def kernel (D : Finset (Fin m) → K) (C : K) (w v : State m) : K :=
moves D C w v + (if w = v then 1 - ∑ u, moves D C w u else 0)
theorem kernel_append (D : Finset (Fin m) → K) (C : K) {w v : State m} (h : Child w v) :
kernel D C w v = appendRate m := by
simp [kernel, moves, h, child_asymm h, child_ne h]
theorem kernel_erase (D : Finset (Fin m) → K) (C : K) {w v : State m} (h : Child v w) :
kernel D C w v = eraseRate D C w := by
simp [kernel, moves, h, child_asymm h, (child_ne h).symm]
theorem kernel_off_edge (D : Finset (Fin m) → K) (C : K) {w v : State m}
(hne : w ≠ v) (ha : ¬ Child w v) (he : ¬ Child v w) :
kernel D C w v = 0 := by simp [kernel, moves, hne, ha, he]
theorem kernel_hold (D : Finset (Fin m) → K) (C : K) (w : State m) :
kernel D C w w = 1 - ∑ u, moves D C w u := by
have hn : ¬ Child w w := fun h => child_ne h rfl
simp [kernel, moves, hn]
theorem kernel_sum (D : Finset (Fin m) → K) (C : K) (w : State m) :
∑ v, kernel D C w v = 1 := by
simp [kernel, Finset.sum_add_distrib, Finset.sum_ite_eq]
theorem moves_sum (D : Finset (Fin m) → K) (C : K) (w : State m) :
∑ v, moves D C w v =
(if w.val.length + 1 < m then ((m - w.val.length : ℕ) : K) / (m : K) else 0) +
(if w.val = [] then 0 else eraseRate D C w) := by
classical
unfold moves
rw [Finset.sum_add_distrib]
congr 1
· rw [← Finset.sum_filter]
change (∑ _ ∈ children w, appendRate m) = _
rw [Finset.sum_const, nsmul_eq_mul, children_card]
split_ifs <;> simp [appendRate, div_eq_mul_inv]
· by_cases hw : w.val = []
· have nh (v : State m) : ¬ Child v w := fun h => h.1 hw
simp [hw, nh]
· simp [Child, hw, eq_comm]
/-- The root-normalized reversible mass, as a product on nonempty prefix sets. -/
def mass (D : Finset (Fin m) → K) (C : K) (w : State m) : K :=
orderWeight (fun S => D S / C) w.val
@[simp] theorem mass_root (D : Finset (Fin m) → K) (C : K) (hm : 0 < m) :
mass D C (root hm) = 1 := rfl
theorem mass_child (D : Finset (Fin m) → K) (C : K) {w v : State m} (h : Child w v) :
mass D C v = (D (letters v) / C)⁻¹ * mass D C w := by
have ht := congrArg Subtype.val h.2
change v.val.tail = w.val at ht
rcases hv : v.val with _ | ⟨a, t⟩
· exact (h.1 hv).elim
· simp only [mass, letters, hv, orderWeight, List.tail_cons] at ht ⊢
rw [ht]
theorem eraseRate_eq (D : Finset (Fin m) → K) (C : K) (w : State m) :
eraseRate D C w = (D (letters w) / C) * appendRate m := by
simp [eraseRate, appendRate, div_eq_mul_inv, mul_assoc, mul_left_comm, mul_comm]
/-- A positive scale and strictly positive proper nonempty subset slacks. -/
def PositiveSlacks [LT K] (D : Finset (Fin m) → K) (C : K) : Prop :=
0 < C ∧ ∀ S, S.Nonempty → S ≠ Finset.univ → 0 < D S
theorem detailedBalance (D : Finset (Fin m) → K) (C : K)
(hC : C ≠ 0) (hD : ∀ S, S.Nonempty → S ≠ Finset.univ → D S ≠ 0)
(w v : State m) :
mass D C w * kernel D C w v = mass D C v * kernel D C v w := by
have edge {x y : State m} (h : Child x y) :
mass D C x * kernel D C x y = mass D C y * kernel D C y x := by
have hn := div_ne_zero (hD _ (letters_nonempty h.1) (letters_ne_univ y)) hC
rw [kernel_append D C h, kernel_erase D C h, mass_child D C h, eraseRate_eq]
calc
_ = ((D (letters y) / C)⁻¹ * (D (letters y) / C)) *
(mass D C x * appendRate m) := by rw [inv_mul_cancel₀ hn, one_mul]
_ = _ := by ac_rfl
by_cases h : w = v
· rw [h]
by_cases ha : Child w v
· exact edge ha
by_cases he : Child v w
· exact (edge he).symm
rw [kernel_off_edge D C h ha he, kernel_off_edge D C (Ne.symm h) he ha, mul_zero, mul_zero]
theorem mass_stationary (D : Finset (Fin m) → K) (C : K)
(hC : C ≠ 0) (hD : ∀ S, S.Nonempty → S ≠ Finset.univ → D S ≠ 0) (v : State m) :
∑ w, mass D C w * kernel D C w v = mass D C v := by
simp_rw [detailedBalance D C hC hD _ v]
rw [← Finset.mul_sum, kernel_sum, mul_one]
theorem orderWeight_scale (D : Finset (Fin m) → K) (C : K) (w : List (Fin m)) :
orderWeight (fun S => D S / C) w = C ^ w.length * orderWeight D w := by
induction w with
| nil => simp [orderWeight]
| cons a t ih =>
rw [orderWeight, orderWeight, ih, List.length_cons, pow_succ, inv_div, div_eq_mul_inv]
ac_rfl
theorem mass_scale (D : Finset (Fin m) → K) (C : K) (w : State m) :
mass D C w = C ^ w.val.length * orderWeight D w.val :=
orderWeight_scale D C w.val
/-- Summing the maximal-word weights is the scaled sum over proper prefixes of
all full orderings. Deleting the final letter bijects those orderings with maximal words. -/
theorem sum_leaf_mass (D : Finset (Fin m) → K) (C : K) (hm : 0 < m) :
∑ w ∈ leaves m, mass D C w = C ^ (m - 1) * rootOrderSum D Finset.univ := by
unfold rootOrderSum
rw [Finset.mul_sum]
symm
refine Finset.sum_bij (dropLast hm) (fun l hl => (mem_leaves _).mpr (dropLast_leaf hm l hl))
?_ ?_ ?_
· intro l₁ h₁ l₂ h₂ he
exact full_order_ext h₁ h₂ (congrArg Subtype.val he)
· intro w hw
obtain ⟨l, hl, hv⟩ := exists_full_order w ((mem_leaves _).mp hw)
exact ⟨l, hl, Subtype.ext hv⟩
· intro l hl
rw [mass_scale]
exact congrArg (fun k : ℕ => C ^ k * orderWeight D l.tail)
(dropLast_leaf hm l hl).symm
/-- The full observation window has root mass 1 plus the masses of the maximal words. -/
theorem sum_window_mass (D : Finset (Fin m) → K) (C : K) (hm : 0 < m) (hm₂ : 2 ≤ m) :
∑ w ∈ window hm, mass D C w = 1 + C ^ (m - 1) * rootOrderSum D Finset.univ := by
unfold window
rw [Finset.sum_insert (fun h => root_not_leaf hm hm₂ ((mem_leaves _).mp h)),
mass_root, sum_leaf_mass D C hm]
section Ordered
variable [LinearOrder K] [IsStrictOrderedRing K]
theorem kernel_nonneg (D : Finset (Fin m) → K) (C : K) (hC : 0 < C)
(hD : ∀ S, S.Nonempty → S ≠ Finset.univ → 0 ≤ D S)
(hupper : ∀ S, S.Nonempty → S ≠ Finset.univ → D S ≤ C) (w v : State m) :
0 ≤ kernel D C w v := by
have hm : (0 : K) < (m : K) := Nat.cast_pos.mpr (Nat.zero_lt_of_lt w.property.2)
have ha : 0 ≤ (appendRate m : K) := div_nonneg zero_le_one hm.le
have herase (hw : w.val ≠ []) : 0 ≤ eraseRate D C w :=
div_nonneg (hD _ (letters_nonempty hw) (letters_ne_univ w)) (mul_nonneg hm.le hC.le)
by_cases heq : w = v
· subst v
rw [kernel_hold, sub_nonneg, moves_sum]
by_cases hw : w.val = []
· simp only [hw, List.length_nil, Nat.sub_zero, if_pos, add_zero]
split_ifs <;> simp [ne_of_gt hm]
· have hrate : eraseRate D C w ≤ (1 : K) / (m : K) := by
rw [eraseRate_eq]
exact mul_le_of_le_one_left ha
((div_le_one hC).mpr (hupper _ (letters_nonempty hw) (letters_ne_univ w)))
rw [if_neg hw]
split_ifs
· apply (add_le_add_right hrate _).trans
rw [← add_div]
apply (div_le_iff₀ hm).mpr
rw [one_mul, ← Nat.cast_one, ← Nat.cast_add]
exact Nat.cast_le.mpr (by have := List.length_pos_iff.mpr hw; omega)
· rw [zero_add]
apply hrate.trans
exact (div_le_one hm).mpr (Nat.one_le_cast.mpr (Nat.zero_lt_of_lt w.property.2))
· by_cases h : Child w v
· rw [kernel_append D C h]
exact ha
· by_cases h' : Child v w
· rw [kernel_erase D C h']
exact herase h'.1
· rw [kernel_off_edge D C heq h h']
theorem mass_pos (D : Finset (Fin m) → K) (C : K) (h : PositiveSlacks D C)
(w : State m) : 0 < mass D C w := by
by_cases hw : w.val = []
· simp [mass, hw, orderWeight]
· rw [mass_child D C (parent_child w hw)]
exact mul_pos (inv_pos.mpr (div_pos (h.2 _ (letters_nonempty hw) (letters_ne_univ w)) h.1))
(mass_pos D C h (parent w))
termination_by w.val.length
decreasing_by
have := child_length (parent_child w hw)
omega
/-- Each state reaches the empty word through strictly positive erasure transitions. -/
theorem reaches_root (D : Finset (Fin m) → K) (C : K) (h : PositiveSlacks D C)
(hm : 0 < m) (w : State m) :
Relation.ReflTransGen (fun x y => 0 < kernel D C x y) w (root hm) := by
by_cases hw : w.val = []
· have he : w = root hm := Subtype.ext hw
rw [he]
· refine (reaches_root D C h hm (parent w)).head ?_
rw [kernel_erase D C (parent_child w hw)]
exact div_pos (h.2 _ (letters_nonempty hw) (letters_ne_univ w))
(mul_pos (Nat.cast_pos.mpr hm) h.1)
termination_by w.val.length
decreasing_by
have := child_length (parent_child w hw)
omega
end Ordered
end Weights
end MarkovScattering.WordChain
Source