import Mathlib /-! # Pillar 1: Factor Graphs, Gibbs Distributions, and Exact Tree Belief Propagation This standalone Lean 4 + Mathlib formalization establishes a complete, non-asymptotic theory of discrete pairwise Markov Random Fields (factor graphs) and exact Sum-Product, Max-Sum, and Max-Product Belief Propagation on arbitrary finite trees (`SimpleGraph.IsTree`). ## Headline Results 1. **Arbitrary Finite-Tree Edge-Split Decomposition** (`tree_dist_adj_cases`, `subtreeVerts_disjoint_union`, `subtreeHeight_lt_of_ne`): For any finite tree `T = (V, E)` (`G.IsTree`) and any directed edge `i → j`, defining `T_{i \ j} = {u ∈ V | dist(u, i) < dist(u, j)}` partitions `V` into two disjoint subtrees connected solely by `{i, j}`, with strict subtree-height decrease along incoming edges and `subtreeHeight i j + 1 ≤ treeDiam G`. 2. **Exact Tree Sum-Product BP in `diam(T)` Steps** (`tree_sumProd_unnorm_exact`, `tree_sumProd_nodeMarginal_exact`, `tree_sumProd_edgeMarginal_exact`): From **any** strictly positive initial message assignment `m0`, synchronous Sum-Product BP converges in at most `treeDiam G` steps to the **exact** single-node marginals `b_i = p_i` and **exact** pairwise edge marginals `b_{ij} = p_{ij}`. 3. **Exact Tree Gibbs Factorization & Bethe Log-Partition Identity** (`tree_gibbs_factorization`, `tree_bp_bethe_logZ_exact`): On every finite tree `T`, the Gibbs distribution factorizes exactly as `p(x) · ∏ i, p_i(x_i)^(d_i) = (∏ i, p_i(x_i)) · ∏_{e={i,j}} p_e(x_i, x_j)`, and at any `t ≥ treeDiam G`, `-F_Bethe(b^(t)) = log Z`. 4. **Exact Tree Max-Sum & Max-Product BP in `diam(T)` Steps** (`tree_maxSum_maxMarginal_exact`, `tree_maxSum_map_energy_exact`, `tree_maxProd_map_exact`): From **any** initial messages, synchronous Max-Sum and Max-Product BP converge in at most `treeDiam G` steps to the **exact** single-node max-marginals `max_{x : x_i = a} log p̃(x)` and the **exact** global MAP energy `max_x log p̃(x)`. 5. **Exact Two-Pass Asynchronous Schedule** (`asyncTreeBP_exact`): Computing messages in a single bottom-up + top-down schedule (ordered by `subtreeHeight`, updating each directed edge exactly once — `2|E|` updates total) recovers exact messages and exact marginals. -/ open scoped BigOperators open Finset Real SimpleGraph namespace TreeBeliefPropagation section PairwiseMRFDef variable {V X : Type*} /-- A discrete pairwise Markov Random Field (factor graph) on an undirected graph `G` with strictly positive node potentials `ψNode` and symmetric strictly positive edge potentials `ψEdge`. -/ structure PairwiseMRF (G : SimpleGraph V) (X : Type*) where ψNode : V → X → ℝ ψEdge : V → V → X → X → ℝ ψNode_pos : ∀ i a, 0 < ψNode i a ψEdge_pos : ∀ i j a b, 0 < ψEdge i j a b ψEdge_symm : ∀ i j a b, ψEdge j i b a = ψEdge i j a b variable {G : SimpleGraph V} [Fintype V] [DecidableEq V] [DecidableRel G.Adj] variable [Fintype X] [DecidableEq X] [Nonempty X] /-- Canonical orientation-free edge potential evaluated on a configuration `x : V → X`. -/ def PairwiseMRF.edgeWeight (M : PairwiseMRF G X) (x : V → X) (e : Sym2 V) : ℝ := Sym2.lift ⟨fun i j => M.ψEdge i j (x i) (x j), fun i j => M.ψEdge_symm j i (x j) (x i)⟩ e omit [Fintype V] [DecidableEq V] [DecidableRel G.Adj] [Fintype X] [DecidableEq X] [Nonempty X] in @[simp] lemma PairwiseMRF.edgeWeight_mk (M : PairwiseMRF G X) (x : V → X) (i j : V) : M.edgeWeight x s(i, j) = M.ψEdge i j (x i) (x j) := rfl omit [Fintype V] [DecidableEq V] [DecidableRel G.Adj] [Fintype X] [DecidableEq X] [Nonempty X] in lemma PairwiseMRF.edgeWeight_pos (M : PairwiseMRF G X) (x : V → X) (e : Sym2 V) : 0 < M.edgeWeight x e := by induction e using Sym2.ind with | _ i j => exact M.ψEdge_pos i j (x i) (x j) /-- Unnormalized Gibbs weight `p̃(x) = (∏_i ψ_i(x_i)) · (∏_{e ∈ E} ψ_e(x))`. -/ noncomputable def PairwiseMRF.unnormWeight (M : PairwiseMRF G X) (x : V → X) : ℝ := (∏ i : V, M.ψNode i (x i)) * (∏ e ∈ G.edgeFinset, M.edgeWeight x e) omit [DecidableEq V] [Fintype X] [DecidableEq X] [Nonempty X] in lemma PairwiseMRF.unnormWeight_pos (M : PairwiseMRF G X) (x : V → X) : 0 < M.unnormWeight x := by unfold PairwiseMRF.unnormWeight exact mul_pos (Finset.prod_pos fun i _ => M.ψNode_pos i (x i)) (Finset.prod_pos fun e _ => M.edgeWeight_pos x e) /-- Partition function `Z = ∑_x p̃(x)`. -/ noncomputable def PairwiseMRF.Z (M : PairwiseMRF G X) : ℝ := ∑ x : V → X, M.unnormWeight x omit [DecidableEq X] in lemma PairwiseMRF.Z_pos (M : PairwiseMRF G X) : 0 < M.Z := by unfold PairwiseMRF.Z exact Finset.sum_pos (fun x _ => M.unnormWeight_pos x) Finset.univ_nonempty /-- Normalized Gibbs probability distribution `p(x) = p̃(x) / Z`. -/ noncomputable def PairwiseMRF.gibbs (M : PairwiseMRF G X) (x : V → X) : ℝ := M.unnormWeight x / M.Z omit [DecidableEq X] in lemma PairwiseMRF.gibbs_pos (M : PairwiseMRF G X) (x : V → X) : 0 < M.gibbs x := div_pos (M.unnormWeight_pos x) M.Z_pos omit [DecidableEq X] in lemma PairwiseMRF.sum_gibbs (M : PairwiseMRF G X) : ∑ x : V → X, M.gibbs x = 1 := by unfold PairwiseMRF.gibbs rw [← Finset.sum_div] exact div_self (ne_of_gt M.Z_pos) /-- Exact single-node marginal `p_i(a) = ∑_{x : x_i = a} p(x)`. -/ noncomputable def PairwiseMRF.nodeMarginal (M : PairwiseMRF G X) (i : V) (a : X) : ℝ := ∑ x : V → X, if x i = a then M.gibbs x else 0 /-- Exact unnormalized single-node marginal `Z_i(a) = ∑_{x : x_i = a} p̃(x)`. -/ noncomputable def PairwiseMRF.unnormNodeMarginal (M : PairwiseMRF G X) (i : V) (a : X) : ℝ := ∑ x : V → X, if x i = a then M.unnormWeight x else 0 omit [Nonempty X] in lemma PairwiseMRF.nodeMarginal_eq_div_Z (M : PairwiseMRF G X) (i : V) (a : X) : M.nodeMarginal i a = M.unnormNodeMarginal i a / M.Z := by unfold PairwiseMRF.nodeMarginal PairwiseMRF.unnormNodeMarginal PairwiseMRF.gibbs rw [Finset.sum_div] refine Finset.sum_congr rfl fun x _ => ?_ split_ifs <;> simp omit [Nonempty X] in lemma PairwiseMRF.unnormNodeMarginal_pos (M : PairwiseMRF G X) (i : V) (a : X) : 0 < M.unnormNodeMarginal i a := by unfold PairwiseMRF.unnormNodeMarginal have h0 : ∃ x : V → X, x i = a := ⟨fun _ => a, rfl⟩ rcases h0 with ⟨x0, hx0⟩ exact lt_of_lt_of_le (by simp [hx0, M.unnormWeight_pos x0]) (Finset.single_le_sum (fun x _ => by split_ifs; exact le_of_lt (M.unnormWeight_pos x); rfl) (Finset.mem_univ x0)) lemma PairwiseMRF.nodeMarginal_pos (M : PairwiseMRF G X) (i : V) (a : X) : 0 < M.nodeMarginal i a := by rw [M.nodeMarginal_eq_div_Z] exact div_pos (M.unnormNodeMarginal_pos i a) M.Z_pos omit [Nonempty X] in lemma PairwiseMRF.sum_unnormNodeMarginal (M : PairwiseMRF G X) (i : V) : ∑ a : X, M.unnormNodeMarginal i a = M.Z := by unfold PairwiseMRF.unnormNodeMarginal PairwiseMRF.Z rw [Finset.sum_comm] refine Finset.sum_congr rfl fun x _ => ?_ simp lemma PairwiseMRF.sum_nodeMarginal (M : PairwiseMRF G X) (i : V) : ∑ a : X, M.nodeMarginal i a = 1 := by simp_rw [M.nodeMarginal_eq_div_Z] rw [← Finset.sum_div, M.sum_unnormNodeMarginal, div_self (ne_of_gt M.Z_pos)] /-- Exact pairwise edge marginal `p_{ij}(a, b) = ∑_{x : x_i = a ∧ x_j = b} p(x)`. -/ noncomputable def PairwiseMRF.edgeMarginal (M : PairwiseMRF G X) (i j : V) (a b : X) : ℝ := ∑ x : V → X, if x i = a ∧ x j = b then M.gibbs x else 0 /-- Exact unnormalized pairwise edge marginal `Z_{ij}(a, b) = ∑_{x : x_i = a ∧ x_j = b} p̃(x)`. -/ noncomputable def PairwiseMRF.unnormEdgeMarginal (M : PairwiseMRF G X) (i j : V) (a b : X) : ℝ := ∑ x : V → X, if x i = a ∧ x j = b then M.unnormWeight x else 0 omit [Nonempty X] in lemma PairwiseMRF.edgeMarginal_eq_div_Z (M : PairwiseMRF G X) (i j : V) (a b : X) : M.edgeMarginal i j a b = M.unnormEdgeMarginal i j a b / M.Z := by unfold PairwiseMRF.edgeMarginal PairwiseMRF.unnormEdgeMarginal PairwiseMRF.gibbs rw [Finset.sum_div] refine Finset.sum_congr rfl fun x _ => ?_ split_ifs <;> simp omit [Nonempty X] in lemma PairwiseMRF.edgeMarginal_symm (M : PairwiseMRF G X) (i j : V) (a b : X) : M.edgeMarginal j i b a = M.edgeMarginal i j a b := by unfold PairwiseMRF.edgeMarginal refine Finset.sum_congr rfl fun x _ => ?_ simp [and_comm] omit [Nonempty X] in lemma PairwiseMRF.unnormEdgeMarginal_pos (M : PairwiseMRF G X) {i j : V} (hne : i ≠ j) (a b : X) : 0 < M.unnormEdgeMarginal i j a b := by unfold PairwiseMRF.unnormEdgeMarginal let x0 : V → X := fun v => if v = i then a else b have hx0 : x0 i = a ∧ x0 j = b := ⟨by simp [x0], by simp [x0, hne.symm]⟩ exact lt_of_lt_of_le (by simp [hx0, M.unnormWeight_pos x0]) (Finset.single_le_sum (fun x _ => by split_ifs; exact le_of_lt (M.unnormWeight_pos x); rfl) (Finset.mem_univ x0)) lemma PairwiseMRF.edgeMarginal_pos (M : PairwiseMRF G X) {i j : V} (hne : i ≠ j) (a b : X) : 0 < M.edgeMarginal i j a b := by rw [M.edgeMarginal_eq_div_Z] exact div_pos (M.unnormEdgeMarginal_pos hne a b) M.Z_pos omit [Nonempty X] in lemma PairwiseMRF.sum_unnormEdgeMarginal (M : PairwiseMRF G X) (i j : V) : ∑ a : X, ∑ b : X, M.unnormEdgeMarginal i j a b = M.Z := by unfold PairwiseMRF.unnormEdgeMarginal PairwiseMRF.Z calc ∑ a : X, ∑ b : X, ∑ x : V → X, (if x i = a ∧ x j = b then M.unnormWeight x else 0) = ∑ a : X, ∑ x : V → X, ∑ b : X, (if x i = a ∧ x j = b then M.unnormWeight x else 0) := Finset.sum_congr rfl fun a _ => Finset.sum_comm _ = ∑ x : V → X, ∑ a : X, ∑ b : X, (if x i = a ∧ x j = b then M.unnormWeight x else 0) := Finset.sum_comm _ = ∑ x : V → X, M.unnormWeight x := by refine Finset.sum_congr rfl fun x _ => ?_ rw [Finset.sum_eq_single (x i)] · rw [Finset.sum_eq_single (x j)] · simp · intro b _ hb; simp [hb.symm] · intro h; exact absurd (Finset.mem_univ _) h · intro a _ ha refine Finset.sum_eq_zero fun b _ => ?_ simp [ha.symm] · intro h; exact absurd (Finset.mem_univ _) h /-- Orientation-free exact edge marginal evaluated on a configuration `x` at edge `e`. -/ noncomputable def PairwiseMRF.edgeMarginalAt (M : PairwiseMRF G X) (x : V → X) (e : Sym2 V) : ℝ := Sym2.lift ⟨fun i j => M.edgeMarginal i j (x i) (x j), fun i j => M.edgeMarginal_symm j i (x j) (x i)⟩ e omit [Nonempty X] in @[simp] lemma PairwiseMRF.edgeMarginalAt_mk (M : PairwiseMRF G X) (x : V → X) (i j : V) : M.edgeMarginalAt x s(i, j) = M.edgeMarginal i j (x i) (x j) := rfl /-- Exact global MAP log-weight `max_x log p̃(x)`. -/ noncomputable def PairwiseMRF.maxLogWeight (M : PairwiseMRF G X) : ℝ := Finset.univ.sup' Finset.univ_nonempty fun x : V → X => Real.log (M.unnormWeight x) /-- Exact global MAP unnormalized weight `max_x p̃(x)`. -/ noncomputable def PairwiseMRF.maxWeight (M : PairwiseMRF G X) : ℝ := Finset.univ.sup' Finset.univ_nonempty fun x : V → X => M.unnormWeight x omit [DecidableEq X] in lemma PairwiseMRF.maxLogWeight_eq_log_maxWeight (M : PairwiseMRF G X) : M.maxLogWeight = Real.log M.maxWeight := by unfold PairwiseMRF.maxLogWeight PairwiseMRF.maxWeight have hmax_pos : 0 < Finset.univ.sup' Finset.univ_nonempty (fun x : V → X => M.unnormWeight x) := by obtain ⟨x0⟩ := (inferInstance : Nonempty (V → X)) exact lt_of_lt_of_le (M.unnormWeight_pos x0) (Finset.le_sup' (fun x : V → X => M.unnormWeight x) (Finset.mem_univ x0)) apply le_antisymm · refine Finset.sup'_le _ _ fun x _ => Real.log_le_log (M.unnormWeight_pos x) ?_ exact Finset.le_sup' (fun y : V → X => M.unnormWeight y) (Finset.mem_univ x) · obtain ⟨x0, _, hx0⟩ := Finset.exists_mem_eq_sup' Finset.univ_nonempty (fun x : V → X => M.unnormWeight x) rw [hx0] exact Finset.le_sup' (fun x : V → X => Real.log (M.unnormWeight x)) (Finset.mem_univ x0) /-- Exact single-node max-marginal `max_{x : x_i = a} log p̃(x)`. -/ noncomputable def PairwiseMRF.maxMarginal (M : PairwiseMRF G X) (i : V) (a : X) : ℝ := (Finset.univ.filter fun x : V → X => x i = a).sup' ⟨fun _ => a, by simp⟩ fun x => Real.log (M.unnormWeight x) lemma PairwiseMRF.sup_maxMarginal_eq_maxLogWeight (M : PairwiseMRF G X) (i : V) : Finset.univ.sup' Finset.univ_nonempty (fun a : X => M.maxMarginal i a) = M.maxLogWeight := by apply le_antisymm · refine Finset.sup'_le _ _ fun a _ => ?_ unfold PairwiseMRF.maxMarginal PairwiseMRF.maxLogWeight refine Finset.sup'_le _ _ fun x _ => ?_ exact Finset.le_sup' (fun y : V → X => Real.log (M.unnormWeight y)) (Finset.mem_univ x) · unfold PairwiseMRF.maxLogWeight refine Finset.sup'_le _ _ fun x _ => ?_ have hle1 : Real.log (M.unnormWeight x) ≤ M.maxMarginal i (x i) := by unfold PairwiseMRF.maxMarginal have hx_mem : x ∈ Finset.univ.filter (fun y : V → X => y i = x i) := Finset.mem_filter.mpr ⟨Finset.mem_univ x, rfl⟩ exact Finset.le_sup' (f := fun y : V → X => Real.log (M.unnormWeight y)) hx_mem exact le_trans hle1 (Finset.le_sup' (f := fun a : X => M.maxMarginal i a) (Finset.mem_univ (x i))) end PairwiseMRFDef /-! ## Section 2: Synchronous Sum-Product, Max-Sum & Max-Product Belief Propagation -/ section BeliefPropagationDefs variable {V X : Type*} {G : SimpleGraph V} variable [Fintype V] [DecidableEq V] [DecidableRel G.Adj] variable [Fintype X] [DecidableEq X] [Nonempty X] /-- One synchronous step of unnormalized Sum-Product BP. -/ noncomputable def sumProdUnnormStep (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i j : V) (xj : X) : ℝ := ∑ xi : X, M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, m k i xi omit [DecidableEq X] in lemma sumProdUnnormStep_pos (M : PairwiseMRF G X) {m : V → V → X → ℝ} (hm : ∀ u v a, G.Adj u v → 0 < m u v a) (i j : V) (xj : X) : 0 < sumProdUnnormStep M m i j xj := by unfold sumProdUnnormStep refine Finset.sum_pos (fun xi _ => ?_) Finset.univ_nonempty refine mul_pos (mul_pos (M.ψNode_pos i xi) (M.ψEdge_pos i j xi xj)) ?_ refine Finset.prod_pos fun k hk => ?_ have hadj : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) exact hm k i xi hadj.symm /-- One synchronous step of normalized Sum-Product BP. -/ noncomputable def sumProdNormStep (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i j : V) (xj : X) : ℝ := sumProdUnnormStep M m i j xj / ∑ a : X, sumProdUnnormStep M m i j a omit [DecidableEq X] in lemma sumProdNormStep_pos (M : PairwiseMRF G X) {m : V → V → X → ℝ} (hm : ∀ u v a, G.Adj u v → 0 < m u v a) (i j : V) (xj : X) : 0 < sumProdNormStep M m i j xj := div_pos (sumProdUnnormStep_pos M hm i j xj) (Finset.sum_pos (fun a _ => sumProdUnnormStep_pos M hm i j a) Finset.univ_nonempty) omit [DecidableEq X] in lemma sum_sumProdNormStep (M : PairwiseMRF G X) {m : V → V → X → ℝ} (hm : ∀ u v a, G.Adj u v → 0 < m u v a) (i j : V) : ∑ a : X, sumProdNormStep M m i j a = 1 := by unfold sumProdNormStep rw [← Finset.sum_div, div_self (ne_of_gt (Finset.sum_pos (fun a _ => sumProdUnnormStep_pos M hm i j a) Finset.univ_nonempty))] /-- Synchronous unnormalized Sum-Product BP iterated `t` steps from `m0`. -/ noncomputable def sumProdUnnormIter (M : PairwiseMRF G X) (m0 : V → V → X → ℝ) : ℕ → V → V → X → ℝ | 0 => m0 | t + 1 => sumProdUnnormStep M (sumProdUnnormIter M m0 t) /-- Synchronous normalized Sum-Product BP iterated `t` steps from `m0`. -/ noncomputable def sumProdNormIter (M : PairwiseMRF G X) (m0 : V → V → X → ℝ) : ℕ → V → V → X → ℝ | 0 => m0 | t + 1 => sumProdNormStep M (sumProdNormIter M m0 t) omit [DecidableEq X] in lemma sumProdUnnormIter_pos (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) : ∀ t u v a, G.Adj u v → 0 < sumProdUnnormIter M m0 t u v a | 0 => hm0 | t + 1 => fun u v a _ => sumProdUnnormStep_pos M (sumProdUnnormIter_pos M hm0 t) u v a omit [DecidableEq X] in lemma sumProdNormIter_pos (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) : ∀ t u v a, G.Adj u v → 0 < sumProdNormIter M m0 t u v a | 0 => hm0 | t + 1 => fun u v a _ => sumProdNormStep_pos M (sumProdNormIter_pos M hm0 t) u v a /-- Unnormalized single-node belief induced by message field `m`. -/ noncomputable def unnormNodeBelief (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i : V) (xi : X) : ℝ := M.ψNode i xi * ∏ k ∈ G.neighborFinset i, m k i xi /-- Normalized single-node belief induced by message field `m`. -/ noncomputable def nodeBelief (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i : V) (xi : X) : ℝ := unnormNodeBelief M m i xi / ∑ a : X, unnormNodeBelief M m i a /-- Unnormalized pairwise edge belief induced by message field `m`. -/ noncomputable def unnormEdgeBelief (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i j : V) (xi xj : X) : ℝ := M.ψNode i xi * M.ψNode j xj * M.ψEdge i j xi xj * (∏ k ∈ (G.neighborFinset i).erase j, m k i xi) * (∏ l ∈ (G.neighborFinset j).erase i, m l j xj) /-- Normalized pairwise edge belief induced by message field `m`. -/ noncomputable def edgeBelief (M : PairwiseMRF G X) (m : V → V → X → ℝ) (i j : V) (xi xj : X) : ℝ := unnormEdgeBelief M m i j xi xj / ∑ a : X, ∑ b : X, unnormEdgeBelief M m i j a b /-- Two positive message fields are edge-proportional if each directed edge differs by a positive scalar factor independent of the state. -/ def MsgProp (G : SimpleGraph V) (m₁ m₂ : V → V → X → ℝ) : Prop := ∀ u v, G.Adj u v → ∃ c > 0, ∀ a, m₁ u v a = c * m₂ u v a omit [Fintype V] [DecidableEq V] [DecidableRel G.Adj] [Fintype X] [DecidableEq X] [Nonempty X] in lemma msgProp_refl (m : V → V → X → ℝ) : MsgProp G m m := fun _ _ _ => ⟨1, one_pos, fun _ => (one_mul _).symm⟩ omit [DecidableEq X] [Nonempty X] in lemma sumProdUnnormStep_msgProp (M : PairwiseMRF G X) {m₁ m₂ : V → V → X → ℝ} (hprop : MsgProp G m₁ m₂) : MsgProp G (sumProdUnnormStep M m₁) (sumProdUnnormStep M m₂) := by intro i j _ choose c hc_pos hc_eq using fun (k : V) (hk : k ∈ (G.neighborFinset i).erase j) => hprop k i ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm refine ⟨∏ k ∈ ((G.neighborFinset i).erase j).attach, c k.1 k.2, Finset.prod_pos fun k _ => hc_pos k.1 k.2, fun xj => ?_⟩ unfold sumProdUnnormStep rw [Finset.mul_sum] refine Finset.sum_congr rfl fun xi _ => ?_ have hprod : ∏ k ∈ (G.neighborFinset i).erase j, m₁ k i xi = (∏ k ∈ ((G.neighborFinset i).erase j).attach, c k.1 k.2) * ∏ k ∈ (G.neighborFinset i).erase j, m₂ k i xi := by rw [← Finset.prod_attach ((G.neighborFinset i).erase j) (fun k => m₁ k i xi), ← Finset.prod_attach ((G.neighborFinset i).erase j) (fun k => m₂ k i xi), ← Finset.prod_mul_distrib] exact Finset.prod_congr rfl fun k _ => hc_eq k.1 k.2 xi rw [hprod]; ring omit [DecidableEq X] in lemma sumProdNormIter_msgProp_unnorm (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) : ∀ t, MsgProp G (sumProdNormIter M m0 t) (sumProdUnnormIter M m0 t) | 0 => msgProp_refl m0 | t + 1 => by have ih := sumProdNormIter_msgProp_unnorm M hm0 t have hstep := sumProdUnnormStep_msgProp M ih intro u v huv rcases hstep u v huv with ⟨c, hc_pos, hc_eq⟩ have hden_pos : 0 < ∑ a : X, sumProdUnnormStep M (sumProdNormIter M m0 t) u v a := Finset.sum_pos (fun a _ => sumProdUnnormStep_pos M (sumProdNormIter_pos M hm0 t) u v a) Finset.univ_nonempty refine ⟨c / (∑ a : X, sumProdUnnormStep M (sumProdNormIter M m0 t) u v a), div_pos hc_pos hden_pos, fun a => ?_⟩ simp only [sumProdNormIter, sumProdNormStep, sumProdUnnormIter] rw [hc_eq a, mul_div_right_comm] omit [DecidableEq V] [DecidableEq X] [Nonempty X] in lemma nodeBelief_congr_msgProp (M : PairwiseMRF G X) {m₁ m₂ : V → V → X → ℝ} (hprop : MsgProp G m₁ m₂) (i : V) (xi : X) : nodeBelief M m₁ i xi = nodeBelief M m₂ i xi := by choose c hc_pos hc_eq using fun (k : V) (hk : k ∈ G.neighborFinset i) => hprop k i ((SimpleGraph.mem_neighborFinset G i k).mp hk).symm let C : ℝ := ∏ k ∈ (G.neighborFinset i).attach, c k.1 k.2 have hC : C ≠ 0 := ne_of_gt (Finset.prod_pos fun k _ => hc_pos k.1 k.2) have hunnorm : ∀ a, unnormNodeBelief M m₁ i a = C * unnormNodeBelief M m₂ i a := fun a => by unfold unnormNodeBelief have hprod : ∏ k ∈ G.neighborFinset i, m₁ k i a = C * ∏ k ∈ G.neighborFinset i, m₂ k i a := by dsimp only [C] rw [← Finset.prod_attach (G.neighborFinset i) (fun k => m₁ k i a), ← Finset.prod_attach (G.neighborFinset i) (fun k => m₂ k i a), ← Finset.prod_mul_distrib] exact Finset.prod_congr rfl fun k _ => hc_eq k.1 k.2 a rw [hprod]; ring unfold nodeBelief simp_rw [hunnorm, ← Finset.mul_sum, mul_div_mul_left _ _ hC] omit [DecidableEq X] [Nonempty X] in lemma edgeBelief_congr_msgProp (M : PairwiseMRF G X) {m₁ m₂ : V → V → X → ℝ} (hprop : MsgProp G m₁ m₂) (i j : V) (xi xj : X) : edgeBelief M m₁ i j xi xj = edgeBelief M m₂ i j xi xj := by choose ci hci_pos hci_eq using fun (k : V) (hk : k ∈ (G.neighborFinset i).erase j) => hprop k i ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm choose cj hcj_pos hcj_eq using fun (l : V) (hl : l ∈ (G.neighborFinset j).erase i) => hprop l j ((SimpleGraph.mem_neighborFinset G j l).mp (Finset.mem_of_mem_erase hl)).symm let Ci : ℝ := ∏ k ∈ ((G.neighborFinset i).erase j).attach, ci k.1 k.2 let Cj : ℝ := ∏ l ∈ ((G.neighborFinset j).erase i).attach, cj l.1 l.2 have hC : Ci * Cj ≠ 0 := ne_of_gt (mul_pos (Finset.prod_pos fun k _ => hci_pos k.1 k.2) (Finset.prod_pos fun l _ => hcj_pos l.1 l.2)) have hunnorm : ∀ a b, unnormEdgeBelief M m₁ i j a b = (Ci * Cj) * unnormEdgeBelief M m₂ i j a b := fun a b => by unfold unnormEdgeBelief have hpi : ∏ k ∈ (G.neighborFinset i).erase j, m₁ k i a = Ci * ∏ k ∈ (G.neighborFinset i).erase j, m₂ k i a := by dsimp only [Ci] rw [← Finset.prod_attach ((G.neighborFinset i).erase j) (fun k => m₁ k i a), ← Finset.prod_attach ((G.neighborFinset i).erase j) (fun k => m₂ k i a), ← Finset.prod_mul_distrib] exact Finset.prod_congr rfl fun k _ => hci_eq k.1 k.2 a have hpj : ∏ l ∈ (G.neighborFinset j).erase i, m₁ l j b = Cj * ∏ l ∈ (G.neighborFinset j).erase i, m₂ l j b := by dsimp only [Cj] rw [← Finset.prod_attach ((G.neighborFinset j).erase i) (fun l => m₁ l j b), ← Finset.prod_attach ((G.neighborFinset j).erase i) (fun l => m₂ l j b), ← Finset.prod_mul_distrib] exact Finset.prod_congr rfl fun l _ => hcj_eq l.1 l.2 b rw [hpi, hpj]; ring unfold edgeBelief simp_rw [hunnorm, ← Finset.mul_sum, mul_div_mul_left _ _ hC] /-- One synchronous step of log-domain Max-Sum BP. -/ noncomputable def maxSumStep (M : PairwiseMRF G X) (μ : V → V → X → ℝ) (i j : V) (xj : X) : ℝ := Finset.univ.sup' Finset.univ_nonempty fun xi : X => Real.log (M.ψNode i xi) + Real.log (M.ψEdge i j xi xj) + ∑ k ∈ (G.neighborFinset i).erase j, μ k i xi /-- Synchronous Max-Sum BP iterated `t` steps from `μ0`. -/ noncomputable def maxSumIter (M : PairwiseMRF G X) (μ0 : V → V → X → ℝ) : ℕ → V → V → X → ℝ | 0 => μ0 | t + 1 => maxSumStep M (maxSumIter M μ0 t) /-- Single-node max-marginal belief induced by Max-Sum messages `μ`. -/ noncomputable def maxSumBelief (M : PairwiseMRF G X) (μ : V → V → X → ℝ) (i : V) (xi : X) : ℝ := Real.log (M.ψNode i xi) + ∑ k ∈ G.neighborFinset i, μ k i xi /-- One synchronous step of multiplicative Max-Product BP. -/ noncomputable def maxProdStep (M : PairwiseMRF G X) (ν : V → V → X → ℝ) (i j : V) (xj : X) : ℝ := Finset.univ.sup' Finset.univ_nonempty fun xi : X => M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi /-- Synchronous Max-Product BP iterated `t` steps from `ν0`. -/ noncomputable def maxProdIter (M : PairwiseMRF G X) (ν0 : V → V → X → ℝ) : ℕ → V → V → X → ℝ | 0 => ν0 | t + 1 => maxProdStep M (maxProdIter M ν0 t) /-- Single-node belief induced by multiplicative Max-Product messages `ν`. -/ noncomputable def maxProdBelief (M : PairwiseMRF G X) (ν : V → V → X → ℝ) (i : V) (xi : X) : ℝ := M.ψNode i xi * ∏ k ∈ G.neighborFinset i, ν k i xi omit [DecidableEq X] in lemma maxProdStep_pos (M : PairwiseMRF G X) {ν : V → V → X → ℝ} (hν : ∀ u v a, G.Adj u v → 0 < ν u v a) (i j : V) (xj : X) : 0 < maxProdStep M ν i j xj := by obtain ⟨x0⟩ := (inferInstance : Nonempty X) refine lt_of_lt_of_le ?_ (Finset.le_sup' (fun xi : X => M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi) (Finset.mem_univ x0)) exact mul_pos (mul_pos (M.ψNode_pos i x0) (M.ψEdge_pos i j x0 xj)) (Finset.prod_pos fun k hk => hν k i x0 ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm) omit [DecidableEq X] in lemma maxProdIter_pos (M : PairwiseMRF G X) {ν0 : V → V → X → ℝ} (hν0 : ∀ u v a, G.Adj u v → 0 < ν0 u v a) : ∀ t u v a, G.Adj u v → 0 < maxProdIter M ν0 t u v a | 0 => hν0 | t + 1 => fun u v a _ => maxProdStep_pos M (maxProdIter_pos M hν0 t) u v a omit [DecidableEq X] in lemma log_maxProdStep_eq_maxSumStep (M : PairwiseMRF G X) {ν : V → V → X → ℝ} (hν : ∀ u v a, G.Adj u v → 0 < ν u v a) (i j : V) (xj : X) : Real.log (maxProdStep M ν i j xj) = maxSumStep M (fun u v a => Real.log (ν u v a)) i j xj := by unfold maxProdStep maxSumStep have hterm_pos : ∀ xi : X, 0 < M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi := fun xi => mul_pos (mul_pos (M.ψNode_pos i xi) (M.ψEdge_pos i j xi xj)) (Finset.prod_pos fun k hk => hν k i xi ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm) have hlog_term : ∀ xi : X, Real.log (M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi) = Real.log (M.ψNode i xi) + Real.log (M.ψEdge i j xi xj) + ∑ k ∈ (G.neighborFinset i).erase j, Real.log (ν k i xi) := fun xi => by have h1 : 0 < M.ψNode i xi * M.ψEdge i j xi xj := mul_pos (M.ψNode_pos i xi) (M.ψEdge_pos i j xi xj) have h2 : 0 < ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi := Finset.prod_pos fun k hk => hν k i xi ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm rw [Real.log_mul (ne_of_gt h1) (ne_of_gt h2), Real.log_mul (ne_of_gt (M.ψNode_pos i xi)) (ne_of_gt (M.ψEdge_pos i j xi xj)), Real.log_prod (fun k hk => ne_of_gt (hν k i xi ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk)).symm))] apply le_antisymm · obtain ⟨x0, _, hx0⟩ := Finset.exists_mem_eq_sup' Finset.univ_nonempty (fun xi : X => M.ψNode i xi * M.ψEdge i j xi xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i xi) rw [hx0, hlog_term x0] exact Finset.le_sup' (fun xi : X => Real.log (M.ψNode i xi) + Real.log (M.ψEdge i j xi xj) + ∑ k ∈ (G.neighborFinset i).erase j, Real.log (ν k i xi)) (Finset.mem_univ x0) · refine Finset.sup'_le _ _ fun xi _ => ?_ rw [← hlog_term xi] exact Real.log_le_log (hterm_pos xi) (Finset.le_sup' (fun a : X => M.ψNode i a * M.ψEdge i j a xj * ∏ k ∈ (G.neighborFinset i).erase j, ν k i a) (Finset.mem_univ xi)) omit [DecidableEq X] in lemma log_maxProdIter_eq_maxSumIter (M : PairwiseMRF G X) {ν0 : V → V → X → ℝ} (hν0 : ∀ u v a, G.Adj u v → 0 < ν0 u v a) : ∀ t u v a, G.Adj u v → Real.log (maxProdIter M ν0 t u v a) = maxSumIter M (fun u' v' b => Real.log (ν0 u' v' b)) t u v a | 0 => fun _ _ _ _ => rfl | t + 1 => fun u v a _ => by simp only [maxProdIter, maxSumIter] rw [log_maxProdStep_eq_maxSumStep M (maxProdIter_pos M hν0 t) u v a] unfold maxSumStep congr 1; funext xi; congr 1 refine Finset.sum_congr rfl fun k hk => ?_ have hadj : G.Adj u k := (SimpleGraph.mem_neighborFinset G u k).mp (Finset.mem_of_mem_erase hk) exact log_maxProdIter_eq_maxSumIter M hν0 t k u xi hadj.symm end BeliefPropagationDefs /-! ## Section 3: Exact Subtree Decomposition on Arbitrary Finite Trees -/ section TreeGeometry variable {V : Type*} {G : SimpleGraph V} [Fintype V] [DecidableEq V] [DecidableRel G.Adj] omit [Fintype V] [DecidableEq V] [DecidableRel G.Adj] in /-- Triangle inequality for graph distance in any connected graph (hence any tree). -/ lemma tree_dist_triangle (hT : G.IsTree) (u w v : V) : G.dist u v ≤ G.dist u w + G.dist w v := by obtain ⟨p1, hp1⟩ := hT.connected.exists_walk_length_eq_dist u w obtain ⟨p2, hp2⟩ := hT.connected.exists_walk_length_eq_dist w v have hle := G.dist_le (p1.append p2) rw [SimpleGraph.Walk.length_append, hp1, hp2] at hle exact hle omit [Fintype V] [DecidableRel G.Adj] in /-- In a tree, the unique path between `u` and `v` has length equal to `G.dist u v`. -/ lemma tree_path_length_eq_dist (hT : G.IsTree) {u v : V} (p : G.Walk u v) (hp : p.IsPath) : p.length = G.dist u v := by obtain ⟨q, hq_len⟩ := hT.connected.exists_walk_length_eq_dist u v have hpq : p = q.bypass := (hT.existsUnique_path u v).unique hp q.bypass_isPath apply le_antisymm · rw [hpq, ← hq_len]; exact q.length_bypass_le_length · exact G.dist_le p omit [Fintype V] [DecidableRel G.Adj] in /-- **Tree Edge-Split Dichotomy**: For any edge `{i, j}` in a tree `T` and any vertex `u`, either `dist(u, j) = dist(u, i) + 1` or `dist(u, i) = dist(u, j) + 1`. -/ theorem tree_dist_adj_cases (hT : G.IsTree) {i j : V} (hij : G.Adj i j) (u : V) : G.dist u j = G.dist u i + 1 ∨ G.dist u i = G.dist u j + 1 := by obtain ⟨p, hp, _⟩ := hT.existsUnique_path u i by_cases hj : j ∈ p.support · right have hsplit : (p.takeUntil j hj).append (p.dropUntil j hj) = p := SimpleGraph.Walk.take_spec p hj have hp1 : (p.takeUntil j hj).IsPath := hp.takeUntil hj have hp2 : (p.dropUntil j hj).IsPath := hp.dropUntil hj have hedge_path : (SimpleGraph.Walk.cons hij.symm SimpleGraph.Walk.nil).IsPath := by simp [SimpleGraph.Walk.isPath_def, hij.ne.symm] have hp2_eq : p.dropUntil j hj = SimpleGraph.Walk.cons hij.symm SimpleGraph.Walk.nil := (hT.existsUnique_path j i).unique hp2 hedge_path have hlen : p.length = (p.takeUntil j hj).length + (p.dropUntil j hj).length := by rw [← SimpleGraph.Walk.length_append, hsplit] rw [hp2_eq] at hlen simp only [SimpleGraph.Walk.length_cons, SimpleGraph.Walk.length_nil] at hlen rw [← tree_path_length_eq_dist hT p hp, ← tree_path_length_eq_dist hT _ hp1, hlen] · left let q : G.Walk u j := p.concat hij have hq : q.IsPath := by rw [SimpleGraph.Walk.isPath_def] dsimp only [q] rw [SimpleGraph.Walk.support_concat] exact List.nodup_append.mpr ⟨hp.support_nodup, List.nodup_singleton _, fun x hx y hy => by simp only [List.mem_singleton] at hy; subst hy; exact fun heq => hj (heq ▸ hx)⟩ rw [← tree_path_length_eq_dist hT q hq, ← tree_path_length_eq_dist hT p hp] simp [q] /-- The vertex set of the subtree on the `i`-side when edge `{i, j}` is removed: `T_{i \ j} = {u ∈ V | dist(u, i) < dist(u, j)}`. -/ noncomputable def subtreeVerts (G : SimpleGraph V) (i j : V) : Finset V := Finset.univ.filter fun u => G.dist u i < G.dist u j omit [DecidableEq V] [DecidableRel G.Adj] in lemma mem_subtreeVerts {i j u : V} : u ∈ subtreeVerts G i j ↔ G.dist u i < G.dist u j := by simp [subtreeVerts] omit [DecidableRel G.Adj] in lemma self_mem_subtreeVerts (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : i ∈ subtreeVerts G i j := by rw [mem_subtreeVerts, SimpleGraph.dist_self] have h1 : G.dist i j = 1 := by rw [← tree_path_length_eq_dist hT (SimpleGraph.Walk.cons hij SimpleGraph.Walk.nil)] · rfl · simp [SimpleGraph.Walk.isPath_def, hij.ne] omega omit [DecidableEq V] [DecidableRel G.Adj] in lemma subtreeVerts_disjoint (i j : V) : Disjoint (subtreeVerts G i j) (subtreeVerts G j i) := by rw [Finset.disjoint_left] intro u h1 h2 rw [mem_subtreeVerts] at h1 h2 omega omit [DecidableRel G.Adj] in lemma subtreeVerts_union_eq_univ (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : subtreeVerts G i j ∪ subtreeVerts G j i = Finset.univ := by ext u simp only [Finset.mem_union, mem_subtreeVerts, Finset.mem_univ, iff_true] rcases tree_dist_adj_cases hT hij u with h | h <;> omega omit [DecidableRel G.Adj] in lemma dist_eq_add_one_of_mem_subtreeVerts (hT : G.IsTree) {i j u : V} (hij : G.Adj i j) (hu : u ∈ subtreeVerts G i j) : G.dist u j = G.dist u i + 1 := by rw [mem_subtreeVerts] at hu rcases tree_dist_adj_cases hT hij u with h | h · exact h · omega omit [DecidableRel G.Adj] in /-- No edge crosses between `T_{i \ j}` and `T_{j \ i}` except `{i, j}` itself. -/ lemma tree_crossing_edge_eq (hT : G.IsTree) {i j u v : V} (hij : G.Adj i j) (huv : G.Adj u v) (hu : u ∈ subtreeVerts G i j) (hv : v ∈ subtreeVerts G j i) : u = i ∧ v = j := by -- Build a path from u to v via i -> j and compare with the length-1 edge u -> v obtain ⟨pu, hpu, _⟩ := hT.existsUnique_path u i obtain ⟨pv, hpv, _⟩ := hT.existsUnique_path j v have hpu_sub : ∀ w ∈ pu.support, w ∈ subtreeVerts G i j := by intro w hw have htake : (pu.takeUntil w hw).IsPath := hpu.takeUntil hw have hdrop : (pu.dropUntil w hw).IsPath := hpu.dropUntil hw have hlen : pu.length = (pu.takeUntil w hw).length + (pu.dropUntil w hw).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec pu hw] rw [tree_path_length_eq_dist hT pu hpu, tree_path_length_eq_dist hT _ htake, tree_path_length_eq_dist hT _ hdrop] at hlen have hdu := dist_eq_add_one_of_mem_subtreeVerts hT hij hu rcases tree_dist_adj_cases hT hij w with hw1 | hw2 · exact mem_subtreeVerts.mpr (by omega) · -- Then dist u j ≤ dist u w + dist w j < dist u w + dist w i = dist u i, contradiction! have htri : G.dist u j ≤ G.dist u w + G.dist w j := tree_dist_triangle hT u w j omega have hpv_sub : ∀ w ∈ pv.support, w ∈ subtreeVerts G j i := by intro w hw have htake : (pv.takeUntil w hw).IsPath := hpv.takeUntil hw have hdrop : (pv.dropUntil w hw).IsPath := hpv.dropUntil hw have hlen : pv.length = (pv.takeUntil w hw).length + (pv.dropUntil w hw).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec pv hw] rw [tree_path_length_eq_dist hT pv hpv, tree_path_length_eq_dist hT _ htake, tree_path_length_eq_dist hT _ hdrop] at hlen have hdv := dist_eq_add_one_of_mem_subtreeVerts hT hij.symm hv have hsym_jw : G.dist j w = G.dist w j := SimpleGraph.dist_comm have hsym_wv : G.dist w v = G.dist v w := SimpleGraph.dist_comm have hsym_jv : G.dist j v = G.dist v j := SimpleGraph.dist_comm rcases tree_dist_adj_cases hT hij w with hw1 | hw2 · have htri : G.dist v i ≤ G.dist v w + G.dist w i := tree_dist_triangle hT v w i omega · exact mem_subtreeVerts.mpr (by omega) let big : G.Walk u v := pu.append (SimpleGraph.Walk.cons hij pv) have hbig_path : big.IsPath := by rw [SimpleGraph.Walk.isPath_def] dsimp only [big] rw [SimpleGraph.Walk.support_append] simp only [SimpleGraph.Walk.support_cons, List.tail_cons] refine List.nodup_append.mpr ⟨hpu.support_nodup, hpv.support_nodup, ?_⟩ intro w hw1 w' hw2 heq subst heq exact Finset.disjoint_left.mp (subtreeVerts_disjoint i j) (hpu_sub w hw1) (hpv_sub w hw2) have hedge_path : (SimpleGraph.Walk.cons huv SimpleGraph.Walk.nil).IsPath := by simp [SimpleGraph.Walk.isPath_def, huv.ne] have hlen_eq : big.length = 1 := by have heq := (hT.existsUnique_path u v).unique hbig_path hedge_path rw [heq]; rfl have hlen_big : big.length = pu.length + 1 + pv.length := by dsimp only [big] rw [SimpleGraph.Walk.length_append, SimpleGraph.Walk.length_cons] omega have hpu0 : pu.length = 0 := by omega have hpv0 : pv.length = 0 := by omega exact ⟨SimpleGraph.Walk.eq_of_length_eq_zero hpu0, (SimpleGraph.Walk.eq_of_length_eq_zero hpv0).symm⟩ /-- **Subtree Star Partition**: `T_{i \ j}` decomposes disjointly into `{i}` together with the subtrees `T_{k \ i}` for all neighbors `k ∈ N(i) \ {j}`. -/ lemma subtreeVerts_star_partition (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : subtreeVerts G i j = insert i (((G.neighborFinset i).erase j).biUnion fun k => subtreeVerts G k i) := by ext u simp only [Finset.mem_insert, Finset.mem_biUnion, Finset.mem_erase, SimpleGraph.mem_neighborFinset] constructor · intro hu by_cases hui : u = i · exact Or.inl hui · right obtain ⟨p, hp, _⟩ := hT.existsUnique_path i u cases p with | nil => exact absurd rfl hui | @cons _ k _ hik p' => have hk_ne_j : k ≠ j := by intro hkj subst hkj have hp'_path : p'.IsPath := hp.of_cons have hlen : G.dist i u = 1 + G.dist k u := by rw [← tree_path_length_eq_dist hT _ hp, ← tree_path_length_eq_dist hT p' hp'_path] simp [add_comm] have hd := dist_eq_add_one_of_mem_subtreeVerts hT hij hu have hs1 : G.dist i u = G.dist u i := SimpleGraph.dist_comm have hs2 : G.dist k u = G.dist u k := SimpleGraph.dist_comm omega refine ⟨k, ⟨hk_ne_j, hik⟩, ?_⟩ have hp'_path : p'.IsPath := hp.of_cons have hlen : G.dist i u = 1 + G.dist k u := by rw [← tree_path_length_eq_dist hT _ hp, ← tree_path_length_eq_dist hT p' hp'_path] simp [add_comm] have hs1 : G.dist i u = G.dist u i := SimpleGraph.dist_comm have hs2 : G.dist k u = G.dist u k := SimpleGraph.dist_comm exact mem_subtreeVerts.mpr (by omega) · rintro (rfl | ⟨k, ⟨hkj, hik⟩, huk⟩) · exact self_mem_subtreeVerts hT hij · -- Since k ∈ N(i) \ {j}, k ∈ T_{i \ j}, and u ∈ T_{k \ i} has path to j through k -> i -> j by_contra hnot have huj : u ∈ subtreeVerts G j i := by have hunion := subtreeVerts_union_eq_univ hT hij have hmem : u ∈ subtreeVerts G i j ∪ subtreeVerts G j i := hunion ▸ Finset.mem_univ u exact (Finset.mem_union.mp hmem).resolve_left hnot have h1 := dist_eq_add_one_of_mem_subtreeVerts hT hik.symm huk have h2 := dist_eq_add_one_of_mem_subtreeVerts hT hij.symm huj obtain ⟨p1, hp1, _⟩ := hT.existsUnique_path u k obtain ⟨p2, hp2, _⟩ := hT.existsUnique_path u j have hi1 : i ∉ p1.support := by intro hi have hdrop := hp1.dropUntil hi have htake := hp1.takeUntil hi have hl : p1.length = (p1.takeUntil i hi).length + (p1.dropUntil i hi).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec p1 hi] rw [tree_path_length_eq_dist hT p1 hp1, tree_path_length_eq_dist hT _ htake, tree_path_length_eq_dist hT _ hdrop] at hl omega have hi2 : i ∉ p2.support := by intro hi have hdrop := hp2.dropUntil hi have htake := hp2.takeUntil hi have hl : p2.length = (p2.takeUntil i hi).length + (p2.dropUntil i hi).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec p2 hi] rw [tree_path_length_eq_dist hT p2 hp2, tree_path_length_eq_dist hT _ htake, tree_path_length_eq_dist hT _ hdrop] at hl omega have hq1 : (p1.concat hik.symm).IsPath := by rw [SimpleGraph.Walk.isPath_def, SimpleGraph.Walk.support_concat] exact List.nodup_append.mpr ⟨hp1.support_nodup, List.nodup_singleton _, fun x hx y hy => by simp only [List.mem_singleton] at hy; subst hy; exact fun heq => hi1 (heq ▸ hx)⟩ have hq2 : (p2.concat hij.symm).IsPath := by rw [SimpleGraph.Walk.isPath_def, SimpleGraph.Walk.support_concat] exact List.nodup_append.mpr ⟨hp2.support_nodup, List.nodup_singleton _, fun x hx y hy => by simp only [List.mem_singleton] at hy; subst hy; exact fun heq => hi2 (heq ▸ hx)⟩ have hq_eq : p1.concat hik.symm = p2.concat hij.symm := (hT.existsUnique_path u i).unique hq1 hq2 have hrev := congrArg (fun w => w.reverse.getVert 1) hq_eq simp only [SimpleGraph.Walk.reverse_concat, SimpleGraph.Walk.getVert_cons_succ, SimpleGraph.Walk.getVert_zero] at hrev exact hkj hrev omit [DecidableRel G.Adj] in /-- Distinct neighbors `k₁ ≠ k₂` of `i` yield pairwise disjoint subtrees `T_{k₁ \ i}` and `T_{k₂ \ i}`. -/ lemma subtreeVerts_branches_disjoint (hT : G.IsTree) {i k₁ k₂ : V} (hk₁ : G.Adj i k₁) (hk₂ : G.Adj i k₂) (hne : k₁ ≠ k₂) : Disjoint (subtreeVerts G k₁ i) (subtreeVerts G k₂ i) := by rw [Finset.disjoint_left] intro u hu1 hu2 have h1 := dist_eq_add_one_of_mem_subtreeVerts hT hk₁.symm hu1 have h2 := dist_eq_add_one_of_mem_subtreeVerts hT hk₂.symm hu2 obtain ⟨p1, hp1, _⟩ := hT.existsUnique_path u k₁ obtain ⟨p2, hp2, _⟩ := hT.existsUnique_path u k₂ have hi1 : i ∉ p1.support := by intro hi have hl : p1.length = (p1.takeUntil i hi).length + (p1.dropUntil i hi).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec p1 hi] rw [tree_path_length_eq_dist hT p1 hp1, tree_path_length_eq_dist hT _ (hp1.takeUntil hi), tree_path_length_eq_dist hT _ (hp1.dropUntil hi)] at hl omega have hi2 : i ∉ p2.support := by intro hi have hl : p2.length = (p2.takeUntil i hi).length + (p2.dropUntil i hi).length := by rw [← SimpleGraph.Walk.length_append, SimpleGraph.Walk.take_spec p2 hi] rw [tree_path_length_eq_dist hT p2 hp2, tree_path_length_eq_dist hT _ (hp2.takeUntil hi), tree_path_length_eq_dist hT _ (hp2.dropUntil hi)] at hl omega have hq1 : (p1.concat hk₁.symm).IsPath := by rw [SimpleGraph.Walk.isPath_def, SimpleGraph.Walk.support_concat] exact List.nodup_append.mpr ⟨hp1.support_nodup, List.nodup_singleton _, fun x hx y hy => by simp only [List.mem_singleton] at hy; subst hy; exact fun heq => hi1 (heq ▸ hx)⟩ have hq2 : (p2.concat hk₂.symm).IsPath := by rw [SimpleGraph.Walk.isPath_def, SimpleGraph.Walk.support_concat] exact List.nodup_append.mpr ⟨hp2.support_nodup, List.nodup_singleton _, fun x hx y hy => by simp only [List.mem_singleton] at hy; subst hy; exact fun heq => hi2 (heq ▸ hx)⟩ have hrev := congrArg (fun w => w.reverse.getVert 1) ((hT.existsUnique_path u i).unique hq1 hq2) simp only [SimpleGraph.Walk.reverse_concat, SimpleGraph.Walk.getVert_cons_succ, SimpleGraph.Walk.getVert_zero] at hrev exact hne hrev /-- Full star partition of `V` around any vertex `i`: `V = {i} ⊔ ⨆_{k ∈ N(i)} T_{k \ i}`. -/ lemma univ_star_partition (hT : G.IsTree) (i : V) : (Finset.univ : Finset V) = insert i ((G.neighborFinset i).biUnion fun k => subtreeVerts G k i) := by ext u simp only [Finset.mem_univ, Finset.mem_insert, Finset.mem_biUnion, SimpleGraph.mem_neighborFinset, true_iff] by_cases hui : u = i · exact Or.inl hui · right obtain ⟨p, hp, _⟩ := hT.existsUnique_path i u cases p with | nil => exact absurd rfl hui | @cons _ k _ hik p' => refine ⟨k, hik, ?_⟩ have hlen : G.dist i u = 1 + G.dist k u := by rw [← tree_path_length_eq_dist hT _ hp, ← tree_path_length_eq_dist hT p' hp.of_cons] simp [add_comm] have hs1 : G.dist i u = G.dist u i := SimpleGraph.dist_comm have hs2 : G.dist k u = G.dist u k := SimpleGraph.dist_comm exact mem_subtreeVerts.mpr (by omega) /-- Directed subtree height `h(i → j) = max_{u ∈ T_{i \ j}} dist(u, i)`. -/ noncomputable def subtreeHeight (G : SimpleGraph V) (i j : V) : ℕ := (subtreeVerts G i j).sup fun u => G.dist u i /-- Graph diameter `diam(G) = max_{u, v ∈ V} dist(u, v)`. -/ noncomputable def treeDiam (G : SimpleGraph V) : ℕ := (Finset.univ : Finset (V × V)).sup fun p => G.dist p.1 p.2 /-- Strict decrease of subtree height along every incoming neighbor `k ∈ N(i) \ {j}`. -/ lemma subtreeHeight_add_one_le (hT : G.IsTree) {i j k : V} (hij : G.Adj i j) (hk : k ∈ (G.neighborFinset i).erase j) : subtreeHeight G k i + 1 ≤ subtreeHeight G i j := by have hkj : k ≠ j := Finset.ne_of_mem_erase hk have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hne : (subtreeVerts G k i).Nonempty := ⟨k, self_mem_subtreeVerts hT hik.symm⟩ obtain ⟨u, hu, hmax⟩ := Finset.exists_mem_eq_sup (subtreeVerts G k i) hne (fun u => G.dist u k) unfold subtreeHeight rw [hmax] have hu_ij : u ∈ subtreeVerts G i j := by rw [subtreeVerts_star_partition hT hij] exact Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, hk, hu⟩) have hd : G.dist u i = G.dist u k + 1 := dist_eq_add_one_of_mem_subtreeVerts hT hik.symm hu rw [← hd] exact Finset.le_sup (f := fun v => G.dist v i) hu_ij omit [DecidableRel G.Adj] in /-- Every directed edge in a tree satisfies `subtreeHeight i j + 1 ≤ treeDiam G`. -/ lemma subtreeHeight_add_one_le_treeDiam (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : subtreeHeight G i j + 1 ≤ treeDiam G := by have hne : (subtreeVerts G i j).Nonempty := ⟨i, self_mem_subtreeVerts hT hij⟩ obtain ⟨u, hu, hmax⟩ := Finset.exists_mem_eq_sup (subtreeVerts G i j) hne (fun u => G.dist u i) unfold subtreeHeight treeDiam rw [hmax, ← dist_eq_add_one_of_mem_subtreeVerts hT hij hu] exact Finset.le_sup (f := fun p : V × V => G.dist p.1 p.2) (Finset.mem_univ (u, j)) /-- Edges strictly inside a vertex subset `S`. -/ noncomputable def inducedEdges (G : SimpleGraph V) [DecidableRel G.Adj] (S : Finset V) : Finset (Sym2 V) := G.edgeFinset.filter fun e => ∀ v ∈ e, v ∈ S lemma mem_inducedEdges {S : Finset V} {e : Sym2 V} : e ∈ inducedEdges G S ↔ e ∈ G.edgeFinset ∧ ∀ v ∈ e, v ∈ S := Finset.mem_filter /-- Edge partition across a tree edge `{i, j}`: `E(T) = {s(i,j)} ⊔ E(T_{i\j}) ⊔ E(T_{j\i})`. -/ lemma edgeFinset_split_edge (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : G.edgeFinset = insert s(i, j) (inducedEdges G (subtreeVerts G i j) ∪ inducedEdges G (subtreeVerts G j i)) := by ext e induction e using Sym2.ind with | _ u v => simp only [Finset.mem_insert, Finset.mem_union, mem_inducedEdges, SimpleGraph.mem_edgeFinset] constructor · intro huv have hu := Finset.mem_union.mp (subtreeVerts_union_eq_univ hT hij ▸ Finset.mem_univ u) have hv := Finset.mem_union.mp (subtreeVerts_union_eq_univ hT hij ▸ Finset.mem_univ v) rcases hu with hu1 | hu2 <;> rcases hv with hv1 | hv2 · right; left; exact ⟨huv, fun w hw => by rcases Sym2.mem_iff.mp hw with rfl | rfl <;> assumption⟩ · left obtain ⟨rfl, rfl⟩ := tree_crossing_edge_eq hT hij huv hu1 hv2 rfl · left obtain ⟨rfl, rfl⟩ := tree_crossing_edge_eq hT hij huv.symm hv1 hu2 exact Sym2.eq_swap · right; right; exact ⟨huv, fun w hw => by rcases Sym2.mem_iff.mp hw with rfl | rfl <;> assumption⟩ · rintro (heq | ⟨huv, _⟩ | ⟨huv, _⟩) · rw [heq]; exact hij · exact huv · exact huv /-- Edge partition of `T_{i \ j}` around `i`: `E(T_{i\j}) = ⋃_{k ∈ N(i)\{j}} ({s(i,k)} ∪ E(T_{k\i}))`. -/ lemma inducedEdges_subtree_star (hT : G.IsTree) {i j : V} (hij : G.Adj i j) : inducedEdges G (subtreeVerts G i j) = ((G.neighborFinset i).erase j).biUnion fun k => insert s(i, k) (inducedEdges G (subtreeVerts G k i)) := by ext e induction e using Sym2.ind with | _ u v => simp only [mem_inducedEdges, Finset.mem_biUnion, Finset.mem_insert, Finset.mem_erase, SimpleGraph.mem_neighborFinset, SimpleGraph.mem_edgeFinset] constructor · rintro ⟨huv, hsub⟩ have hu := hsub u (Sym2.mem_mk_left u v) have hv := hsub v (Sym2.mem_mk_right u v) rw [subtreeVerts_star_partition hT hij] at hu hv simp only [Finset.mem_insert, Finset.mem_biUnion, Finset.mem_erase, SimpleGraph.mem_neighborFinset] at hu hv rcases hu with hui | ⟨ku, ⟨hku_ne, hiku⟩, huku⟩ · rcases hv with hvi | ⟨kv, ⟨hkv_ne, hikv⟩, hvkv⟩ · rw [hui, hvi] at huv; exact absurd huv (G.loopless.irrefl i) · have hi_in : i ∈ subtreeVerts G i kv := self_mem_subtreeVerts hT hikv obtain ⟨_, rfl⟩ := tree_crossing_edge_eq hT hikv (hui ▸ huv) hi_in hvkv exact ⟨v, ⟨hkv_ne, hikv⟩, Or.inl (by rw [hui])⟩ · rcases hv with hvi | ⟨kv, ⟨hkv_ne, hikv⟩, hvkv⟩ · have hi_in : i ∈ subtreeVerts G i ku := self_mem_subtreeVerts hT hiku obtain ⟨_, rfl⟩ := tree_crossing_edge_eq hT hiku (hvi ▸ huv.symm) hi_in huku exact ⟨u, ⟨hku_ne, hiku⟩, Or.inl (by rw [hvi, Sym2.eq_swap])⟩ · by_cases hkk : ku = kv · subst hkk exact ⟨ku, ⟨hku_ne, hiku⟩, Or.inr ⟨huv, fun w hw => by rcases Sym2.mem_iff.mp hw with rfl | rfl <;> assumption⟩⟩ · exfalso have hv_not : v ∈ subtreeVerts G i ku := by have hu_un := subtreeVerts_union_eq_univ hT hiku have hmem : v ∈ subtreeVerts G i ku ∪ subtreeVerts G ku i := hu_un ▸ Finset.mem_univ v exact (Finset.mem_union.mp hmem).resolve_right (Finset.disjoint_right.mp (subtreeVerts_branches_disjoint hT hiku hikv hkk) hvkv) obtain ⟨hvi_eq, _⟩ := tree_crossing_edge_eq hT hiku huv.symm hv_not huku exact Finset.disjoint_left.mp (subtreeVerts_disjoint i kv) (self_mem_subtreeVerts hT hikv) (hvi_eq ▸ hvkv) · rintro ⟨k, ⟨hkj, hik⟩, heq | ⟨huv, hsub⟩⟩ · rw [heq] refine ⟨hik, fun w hw => ?_⟩ rw [subtreeVerts_star_partition hT hij] rcases Sym2.mem_iff.mp hw with hwi | hwk · rw [hwi]; exact Finset.mem_insert_self i _ · rw [hwk]; exact Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, Finset.mem_erase.mpr ⟨hkj, (SimpleGraph.mem_neighborFinset G i k).mpr hik⟩, self_mem_subtreeVerts hT hik.symm⟩) · refine ⟨huv, fun w hw => ?_⟩ rw [subtreeVerts_star_partition hT hij] exact Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, Finset.mem_erase.mpr ⟨hkj, (SimpleGraph.mem_neighborFinset G i k).mpr hik⟩, hsub w hw⟩) /-- Edge partition of the whole tree `T` around any vertex `i`: `E(T) = ⋃_{k ∈ N(i)} ({s(i,k)} ∪ E(T_{k\i}))`. -/ lemma edgeFinset_star (hT : G.IsTree) (i : V) : G.edgeFinset = (G.neighborFinset i).biUnion fun k => insert s(i, k) (inducedEdges G (subtreeVerts G k i)) := by ext e induction e using Sym2.ind with | _ u v => simp only [Finset.mem_biUnion, Finset.mem_insert, mem_inducedEdges, SimpleGraph.mem_neighborFinset, SimpleGraph.mem_edgeFinset] constructor · intro huv have hu := univ_star_partition hT i ▸ Finset.mem_univ u have hv := univ_star_partition hT i ▸ Finset.mem_univ v simp only [Finset.mem_insert, Finset.mem_biUnion, SimpleGraph.mem_neighborFinset] at hu hv rcases hu with hui | ⟨ku, hiku, huku⟩ · rcases hv with hvi | ⟨kv, hikv, hvkv⟩ · rw [hui, hvi] at huv; exact absurd huv (G.loopless.irrefl i) · obtain ⟨_, rfl⟩ := tree_crossing_edge_eq hT hikv (hui ▸ huv) (self_mem_subtreeVerts hT hikv) hvkv exact ⟨v, hikv, Or.inl (by rw [hui])⟩ · rcases hv with hvi | ⟨kv, hikv, hvkv⟩ · obtain ⟨_, rfl⟩ := tree_crossing_edge_eq hT hiku (hvi ▸ huv.symm) (self_mem_subtreeVerts hT hiku) huku exact ⟨u, hiku, Or.inl (by rw [hvi, Sym2.eq_swap])⟩ · by_cases hkk : ku = kv · subst hkk exact ⟨ku, hiku, Or.inr ⟨huv, fun w hw => by rcases Sym2.mem_iff.mp hw with rfl | rfl <;> assumption⟩⟩ · exfalso have hv_not : v ∈ subtreeVerts G i ku := by have hmem : v ∈ subtreeVerts G i ku ∪ subtreeVerts G ku i := subtreeVerts_union_eq_univ hT hiku ▸ Finset.mem_univ v exact (Finset.mem_union.mp hmem).resolve_right (Finset.disjoint_right.mp (subtreeVerts_branches_disjoint hT hiku hikv hkk) hvkv) obtain ⟨hvi_eq, _⟩ := tree_crossing_edge_eq hT hiku huv.symm hv_not huku exact Finset.disjoint_left.mp (subtreeVerts_disjoint i kv) (self_mem_subtreeVerts hT hikv) (hvi_eq ▸ hvkv) · rintro ⟨k, hik, heq | ⟨huv, _⟩⟩ · rw [heq]; exact hik · exact huv lemma inducedEdges_disjoint_of_verts_disjoint {S₁ S₂ : Finset V} (hd : Disjoint S₁ S₂) : Disjoint (inducedEdges G S₁) (inducedEdges G S₂) := by rw [Finset.disjoint_left] intro e h1 h2 induction e using Sym2.ind with | _ u v => exact Finset.disjoint_left.mp hd ((mem_inducedEdges.mp h1).2 u (Sym2.mem_mk_left u v)) ((mem_inducedEdges.mp h2).2 u (Sym2.mem_mk_left u v)) lemma branch_edges_pairwiseDisjoint (hT : G.IsTree) (i : V) : ((G.neighborFinset i : Set V)).PairwiseDisjoint (fun k => insert s(i, k) (inducedEdges G (subtreeVerts G k i))) := by intro k₁ hk₁ k₂ hk₂ hne have hik₁ : G.Adj i k₁ := (SimpleGraph.mem_neighborFinset G i k₁).mp hk₁ have hik₂ : G.Adj i k₂ := (SimpleGraph.mem_neighborFinset G i k₂).mp hk₂ dsimp only [Function.onFun] rw [Finset.disjoint_left] intro e he1 he2 simp only [Finset.mem_insert, mem_inducedEdges] at he1 he2 rcases he1 with rfl | ⟨_, h1⟩ · rcases he2 with heq | ⟨_, h2⟩ · have : k₁ = k₂ := by have hm : k₁ ∈ s(i, k₂) := heq ▸ Sym2.mem_mk_right i k₁ rcases Sym2.mem_iff.mp hm with h | h · exact absurd h.symm hik₁.ne · exact h exact hne this · exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k₂) (self_mem_subtreeVerts hT hik₂) (h2 i (Sym2.mem_mk_left i k₁)) · rcases he2 with rfl | ⟨_, h2⟩ · exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k₁) (self_mem_subtreeVerts hT hik₁) (h1 i (Sym2.mem_mk_left i k₂)) · induction e using Sym2.ind with | _ u v => exact Finset.disjoint_left.mp (subtreeVerts_branches_disjoint hT hik₁ hik₂ hne) (h1 u (Sym2.mem_mk_left u v)) (h2 u (Sym2.mem_mk_left u v)) lemma edge_not_mem_inducedEdges_subtree (hT : G.IsTree) {i k : V} (hik : G.Adj i k) : s(i, k) ∉ inducedEdges G (subtreeVerts G k i) := by intro hm exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) ((mem_inducedEdges.mp hm).2 i (Sym2.mem_mk_left i k)) end TreeGeometry /-! ## Section 4: Exact Subtree Partition Functions & Exact Tree Sum-Product BP -/ section ExactTreeSumProduct variable {V X : Type*} {G : SimpleGraph V} variable [Fintype V] [DecidableEq V] [DecidableRel G.Adj] variable [Fintype X] [DecidableEq X] [Nonempty X] /-- Subtree weight of a full configuration `x : V → X` restricted to `T_{i \ j}` plus the boundary edge `{i, j}`. -/ noncomputable def subtreeTerm (M : PairwiseMRF G X) (i j : V) (x : V → X) : ℝ := (∏ u ∈ subtreeVerts G i j, M.ψNode u (x u)) * (∏ e ∈ inducedEdges G (subtreeVerts G i j), M.edgeWeight x e) * M.ψEdge i j (x i) (x j) omit [Fintype X] [DecidableEq X] [Nonempty X] in lemma subtreeTerm_pos (M : PairwiseMRF G X) (i j : V) (x : V → X) : 0 < subtreeTerm M i j x := mul_pos (mul_pos (Finset.prod_pos fun u _ => M.ψNode_pos u (x u)) (Finset.prod_pos fun e _ => M.edgeWeight_pos x e)) (M.ψEdge_pos i j (x i) (x j)) omit [Fintype X] [DecidableEq X] [Nonempty X] in /-- `subtreeTerm M i j` depends on `x` only via `x` restricted to `T_{i \ j}` and `x j`. -/ lemma subtreeTerm_congr (M : PairwiseMRF G X) (i j : V) {x y : V → X} (hsub : ∀ u ∈ subtreeVerts G i j, x u = y u) (hj : x j = y j) (hi : i ∈ subtreeVerts G i j) : subtreeTerm M i j x = subtreeTerm M i j y := by unfold subtreeTerm congr 1 · congr 1 · exact Finset.prod_congr rfl fun u hu => by rw [hsub u hu] · refine Finset.prod_congr rfl fun e he => ?_ induction e using Sym2.ind with | _ u v => have hu := (mem_inducedEdges.mp he).2 u (Sym2.mem_mk_left u v) have hv := (mem_inducedEdges.mp he).2 v (Sym2.mem_mk_right u v) simp [hsub u hu, hsub v hv] · rw [hsub i hi, hj] /-- Extend a partial assignment `y : S → X` to a full configuration using a default state. -/ def extendAssign (S : Finset V) (y : S → X) (d : X) : V → X := fun v => if h : v ∈ S then y ⟨v, h⟩ else d /-- Exact subtree partition function `Z_{i → j}(x_j)` summed over all configurations of `T_{i \ j}`. -/ noncomputable def subtreeZ (M : PairwiseMRF G X) (i j : V) (xj : X) : ℝ := ∑ y : subtreeVerts G i j → X, subtreeTerm M i j (extendAssign (subtreeVerts G i j) y xj) omit [DecidableEq X] in lemma subtreeZ_pos (M : PairwiseMRF G X) (i j : V) (xj : X) : 0 < subtreeZ M i j xj := Finset.sum_pos (fun _ _ => subtreeTerm_pos M i j _) Finset.univ_nonempty omit [Fintype V] [DecidableEq X] [Nonempty X] in /-- Factoring a sum over assignments on a disjoint union of subsets. -/ lemma sum_pi_disjoint_biUnion {ι : Type*} [DecidableEq ι] (I : Finset ι) (S : ι → Finset V) (hdisj : (I : Set ι).PairwiseDisjoint S) (f : ∀ k ∈ I, (S k → X) → ℝ) : (∑ y : (I.biUnion S) → X, ∏ k ∈ I.attach, f k.1 k.2 (fun u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩)) = ∏ k ∈ I.attach, ∑ yk : S k.1 → X, f k.1 k.2 yk := by -- Canonical equivalence `((I.biUnion S) → X) ≃ (∀ k : I, S k → X)` let e : ((I.biUnion S) → X) ≃ (∀ k : I, S k.1 → X) := { toFun := fun y k u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩ invFun := fun g u => let k := (Finset.mem_biUnion.mp u.2).choose let hk := (Finset.mem_biUnion.mp u.2).choose_spec g ⟨k, hk.1⟩ ⟨u.1, hk.2⟩ left_inv := fun y => by funext u; rfl right_inv := fun g => by funext ⟨k, hk⟩ ⟨u, hu⟩ dsimp only have hcast : ∀ {k₁ k₂ : ι} (h₁ : k₁ ∈ I) (h₂ : k₂ ∈ I) (hu₁ : u ∈ S k₁) (hu₂ : u ∈ S k₂), k₁ = k₂ → g ⟨k₁, h₁⟩ ⟨u, hu₁⟩ = g ⟨k₂, h₂⟩ ⟨u, hu₂⟩ := by rintro _ _ _ _ _ _ rfl; rfl have hk' := (Finset.mem_biUnion.mp (Finset.mem_biUnion.mpr ⟨k, hk, hu⟩)).choose_spec refine hcast hk'.1 hk hk'.2 hu ?_ by_contra hne exact Finset.disjoint_left.mp (hdisj hk'.1 hk hne) hk'.2 hu } refine (Fintype.sum_equiv e _ (fun g => ∏ k ∈ I.attach, f k.1 k.2 (g k)) (fun _ => rfl)).trans ?_ have hps := (Fintype.prod_sum fun (k : I) (yk : S k.1 → X) => f k.1 k.2 yk).symm simpa [Finset.prod_coe_sort_eq_attach] using hps omit [DecidableEq X] [Nonempty X] in /-- **Master Subtree Partition Function Recursion**: `Z_{i → j}(x_j) = ∑_{x_i} ψ_i(x_i) ψ_{ij}(x_i, x_j) ∏_{k ∈ N(i)\{j}} Z_{k → i}(x_i)`. -/ theorem subtreeZ_rec (hT : G.IsTree) (M : PairwiseMRF G X) {i j : V} (hij : G.Adj i j) (xj : X) : subtreeZ M i j xj = sumProdUnnormStep M (subtreeZ M) i j xj := by let I := (G.neighborFinset i).erase j let B := I.biUnion fun k => subtreeVerts G k i have hi_not_B : i ∉ B := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨k, hk, hik⟩ have hadj : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hadj) hik have hpart : subtreeVerts G i j = insert i B := subtreeVerts_star_partition hT hij have hdisj_I : (I : Set V).PairwiseDisjoint (fun k => subtreeVerts G k i) := by intro k₁ hk₁ k₂ hk₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G i k₁).mp (Finset.mem_of_mem_erase hk₁)) ((SimpleGraph.mem_neighborFinset G i k₂).mp (Finset.mem_of_mem_erase hk₂)) hne -- Split `subtreeVerts G i j → X` into `X × (B → X)` let eSplit : (subtreeVerts G i j → X) ≃ X × (B → X) := { toFun := fun y => (y ⟨i, self_mem_subtreeVerts hT hij⟩, fun u => y ⟨u.1, hpart ▸ Finset.mem_insert_of_mem u.2⟩) invFun := fun p u => if h : u.1 = i then p.1 else p.2 ⟨u.1, (Finset.mem_insert.mp (hpart ▸ u.2)).resolve_left h⟩ left_inv := fun y => by funext ⟨u, hu⟩ dsimp only split_ifs with h · subst h; rfl · rfl right_inv := fun ⟨xi, yB⟩ => by ext · simp · rename_i u have hne : u.1 ≠ i := fun heq => hi_not_B (heq ▸ u.2) simp [hne] } unfold subtreeZ sumProdUnnormStep rw [Fintype.sum_equiv eSplit _ (fun p => M.ψNode i p.1 * M.ψEdge i j p.1 xj * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => p.2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) p.1))] · rw [Fintype.sum_prod_type] refine Finset.sum_congr rfl fun xi _ => ?_ dsimp only rw [← Finset.mul_sum] congr 1 rw [sum_pi_disjoint_biUnion I (fun k => subtreeVerts G k i) hdisj_I (fun k _ yk => subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk xi))] exact Finset.prod_attach I (fun k => subtreeZ M k i xi) · intro y dsimp only [eSplit] set x := extendAssign (subtreeVerts G i j) y xj have hxi : x i = y ⟨i, self_mem_subtreeVerts hT hij⟩ := by simp [x, extendAssign, self_mem_subtreeVerts hT hij] have hxj : x j = xj := by have hj_not : j ∉ subtreeVerts G i j := Finset.disjoint_right.mp (subtreeVerts_disjoint i j) (self_mem_subtreeVerts hT hij.symm) simp [x, extendAssign, hj_not] -- Factor `subtreeTerm M i j x` along the star around `i` unfold subtreeTerm have hnode : ∏ u ∈ subtreeVerts G i j, M.ψNode u (x u) = M.ψNode i (x i) * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u) := by rw [hpart, Finset.prod_insert hi_not_B, Finset.prod_biUnion hdisj_I] have hedge : ∏ e ∈ inducedEdges G (subtreeVerts G i j), M.edgeWeight x e = ∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e) := by rw [inducedEdges_subtree_star hT hij] have hdisj_e : (I : Set V).PairwiseDisjoint (fun k => insert s(i, k) (inducedEdges G (subtreeVerts G k i))) := (branch_edges_pairwiseDisjoint hT i).subset (Finset.erase_subset j _) rw [Finset.prod_biUnion hdisj_e] refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hik), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm k i] have hprod_eq : (∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) = ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => y ⟨u.1, hpart ▸ Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩)⟩) (y ⟨i, self_mem_subtreeVerts hT hij⟩)) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach I] refine Finset.prod_congr rfl fun ⟨k, hk⟩ _ => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hcongr : subtreeTerm M k i x = subtreeTerm M k i (extendAssign (subtreeVerts G k i) (fun u => y ⟨u.1, hpart ▸ Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, hk, u.2⟩)⟩) (y ⟨i, self_mem_subtreeVerts hT hij⟩)) := by refine subtreeTerm_congr M k i (fun u hu => ?_) ?_ (self_mem_subtreeVerts hT hik.symm) · have hu_ij : u ∈ subtreeVerts G i j := hpart ▸ Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, hk, hu⟩) simp [x, extendAssign, hu, hu_ij] · have hi_not : i ∉ subtreeVerts G k i := Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) simp [x, extendAssign, hi_not, self_mem_subtreeVerts hT hij] rw [← hcongr]; unfold subtreeTerm; ring rw [hnode, hedge] calc (M.ψNode i (x i) * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) * M.ψEdge i j (x i) (x j) = (M.ψNode i (x i) * M.ψEdge i j (x i) (x j)) * ((∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e))) := by ring _ = M.ψNode i (y ⟨i, self_mem_subtreeVerts hT hij⟩) * M.ψEdge i j (y ⟨i, self_mem_subtreeVerts hT hij⟩) xj * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => y ⟨u.1, hpart ▸ Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩)⟩) (y ⟨i, self_mem_subtreeVerts hT hij⟩)) := by rw [hprod_eq, hxi, hxj] omit [DecidableEq X] [Nonempty X] in /-- **Exact Convergence of Unnormalized Sum-Product BP in `subtreeHeight + 1` Steps**: For ANY initial message field `m0` (even non-positive!), after `t ≥ subtreeHeight i j + 1` steps, `sumProdUnnormIter M m0 t i j = subtreeZ M i j`. -/ theorem sumProdUnnormIter_eq_subtreeZ (hT : G.IsTree) (M : PairwiseMRF G X) (m0 : V → V → X → ℝ) : ∀ h : ℕ, ∀ i j : V, G.Adj i j → subtreeHeight G i j ≤ h → ∀ t : ℕ, h + 1 ≤ t → ∀ xj : X, sumProdUnnormIter M m0 t i j xj = subtreeZ M i j xj := by intro h induction h with | zero => intro i j hij hh t ht xj cases t with | zero => omega | succ t' => rw [subtreeZ_rec hT M hij xj] simp only [sumProdUnnormIter, sumProdUnnormStep] refine Finset.sum_congr rfl fun xi _ => ?_ congr 1 -- When subtreeHeight = 0, (G.neighborFinset i).erase j is empty! have hempty : (G.neighborFinset i).erase j = ∅ := by rw [Finset.eq_empty_iff_forall_notMem] intro k hk have hlt := subtreeHeight_add_one_le hT hij hk omega simp [hempty] | succ h ih => intro i j hij hh t ht xj cases t with | zero => omega | succ t' => rw [subtreeZ_rec hT M hij xj] simp only [sumProdUnnormIter, sumProdUnnormStep] refine Finset.sum_congr rfl fun xi _ => ?_ congr 1 refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hhk : subtreeHeight G k i ≤ h := by have := subtreeHeight_add_one_le hT hij hk omega exact ih k i hik.symm hhk t' (by omega) xi omit [Nonempty X] in /-- Exact single-node unnormalized marginal in terms of `subtreeZ`. -/ theorem unnormNodeMarginal_eq_subtreeZ_prod (hT : G.IsTree) (M : PairwiseMRF G X) (i : V) (a : X) : M.unnormNodeMarginal i a = unnormNodeBelief M (subtreeZ M) i a := by let I := G.neighborFinset i let B := I.biUnion fun k => subtreeVerts G k i have hi_not_B : i ∉ B := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨k, hk, hik⟩ exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT ((SimpleGraph.mem_neighborFinset G i k).mp hk)) hik have hpart : (Finset.univ : Finset V) = insert i B := univ_star_partition hT i have hdisj_I : (I : Set V).PairwiseDisjoint (fun k => subtreeVerts G k i) := by intro k₁ hk₁ k₂ hk₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G i k₁).mp hk₁) ((SimpleGraph.mem_neighborFinset G i k₂).mp hk₂) hne -- Equivalence `{x : V → X // x i = a} ≃ (B → X)` let eFix : {x : V → X // x ∈ Finset.univ.filter fun x : V → X => x i = a} ≃ (B → X) := { toFun := fun x u => x.1 u.1 invFun := fun yB => ⟨fun v => if hv : v ∈ B then yB ⟨v, hv⟩ else a, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp [hi_not_B]⟩⟩ left_inv := fun ⟨x, hx⟩ => by have hxi : x i = a := (Finset.mem_filter.mp hx).2 ext v dsimp only split_ifs with hv · rfl · have hv_mem : v ∈ insert i B := hpart ▸ Finset.mem_univ v have hvi : v = i := (Finset.mem_insert.mp hv_mem).resolve_right hv rw [hvi, hxi] right_inv := fun yB => by funext ⟨v, hv⟩; simp [hv] } unfold PairwiseMRF.unnormNodeMarginal unnormNodeBelief rw [← Finset.sum_filter, ← Finset.sum_coe_sort] rw [Fintype.sum_equiv eFix _ (fun yB => M.ψNode i a * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => yB ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a))] · rw [← Finset.mul_sum] congr 1 rw [sum_pi_disjoint_biUnion I (fun k => subtreeVerts G k i) hdisj_I (fun k _ yk => subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk a))] exact Finset.prod_attach I (fun k => subtreeZ M k i a) · intro ⟨x, hx_mem⟩ have hx : x i = a := (Finset.mem_filter.mp hx_mem).2 dsimp only [eFix] unfold PairwiseMRF.unnormWeight have hnode : ∏ u : V, M.ψNode u (x u) = M.ψNode i a * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u) := by rw [hpart, Finset.prod_insert hi_not_B, Finset.prod_biUnion hdisj_I, hx] have hedge : ∏ e ∈ G.edgeFinset, M.edgeWeight x e = ∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e) := by rw [edgeFinset_star hT i, Finset.prod_biUnion (branch_edges_pairwiseDisjoint hT i)] refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hik), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm k i] rw [hnode, hedge] have hprod_eq : (∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) = ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach I] refine Finset.prod_congr rfl fun ⟨k, hk⟩ _ => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk have hcongr : subtreeTerm M k i x = subtreeTerm M k i (extendAssign (subtreeVerts G k i) (fun u => x u.1) a) := by refine subtreeTerm_congr M k i (fun u hu => ?_) ?_ (self_mem_subtreeVerts hT hik.symm) · simp [extendAssign, hu] · have hi_not : i ∉ subtreeVerts G k i := Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) simp [extendAssign, hi_not, hx] rw [← hcongr]; unfold subtreeTerm; ring rw [mul_assoc, hprod_eq] rfl omit [Nonempty X] in /-- Exact pairwise edge unnormalized marginal in terms of `subtreeZ`. -/ theorem unnormEdgeMarginal_eq_subtreeZ_prod (hT : G.IsTree) (M : PairwiseMRF G X) {i j : V} (hij : G.Adj i j) (a b : X) : M.unnormEdgeMarginal i j a b = unnormEdgeBelief M (subtreeZ M) i j a b := by -- Split V into T_{i \ j} and T_{j \ i}, then split each around i and j! let Ii := (G.neighborFinset i).erase j let Ij := (G.neighborFinset j).erase i let Bi := Ii.biUnion fun k => subtreeVerts G k i let Bj := Ij.biUnion fun l => subtreeVerts G l j have hi_not_Bi : i ∉ Bi := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨k, hk, hik⟩ exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk))) hik have hj_not_Bj : j ∉ Bj := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨l, hl, hjl⟩ exact Finset.disjoint_left.mp (subtreeVerts_disjoint j l) (self_mem_subtreeVerts hT ((SimpleGraph.mem_neighborFinset G j l).mp (Finset.mem_of_mem_erase hl))) hjl have hpart_i : subtreeVerts G i j = insert i Bi := subtreeVerts_star_partition hT hij have hpart_j : subtreeVerts G j i = insert j Bj := subtreeVerts_star_partition hT hij.symm have hdisj_ij : Disjoint (subtreeVerts G i j) (subtreeVerts G j i) := subtreeVerts_disjoint i j have hdisj_BiBj : Disjoint Bi Bj := hdisj_ij.mono (hpart_i ▸ Finset.subset_insert i Bi) (hpart_j ▸ Finset.subset_insert j Bj) have hi_not_Bj : i ∉ Bj := fun hm => Finset.disjoint_left.mp hdisj_ij (self_mem_subtreeVerts hT hij) (hpart_j ▸ Finset.mem_insert_of_mem hm) have hj_not_Bi : j ∉ Bi := fun hm => Finset.disjoint_right.mp hdisj_ij (self_mem_subtreeVerts hT hij.symm) (hpart_i ▸ Finset.mem_insert_of_mem hm) have hdisj_Ii : (Ii : Set V).PairwiseDisjoint (fun k => subtreeVerts G k i) := by intro k₁ hk₁ k₂ hk₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G i k₁).mp (Finset.mem_of_mem_erase hk₁)) ((SimpleGraph.mem_neighborFinset G i k₂).mp (Finset.mem_of_mem_erase hk₂)) hne have hdisj_Ij : (Ij : Set V).PairwiseDisjoint (fun l => subtreeVerts G l j) := by intro l₁ hl₁ l₂ hl₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G j l₁).mp (Finset.mem_of_mem_erase hl₁)) ((SimpleGraph.mem_neighborFinset G j l₂).mp (Finset.mem_of_mem_erase hl₂)) hne let eFix : {x : V → X // x ∈ Finset.univ.filter fun x : V → X => x i = a ∧ x j = b} ≃ (Bi → X) × (Bj → X) := { toFun := fun x => (fun u => x.1 u.1, fun v => x.1 v.1) invFun := fun p => ⟨fun v => if hv : v ∈ Bi then p.1 ⟨v, hv⟩ else if hv' : v ∈ Bj then p.2 ⟨v, hv'⟩ else if v = i then a else b, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ⟨by simp [hi_not_Bi, hi_not_Bj], by simp [hj_not_Bi, hj_not_Bj, hij.ne.symm]⟩⟩⟩ left_inv := fun ⟨x, hx_mem⟩ => by obtain ⟨hxa, hxb⟩ := (Finset.mem_filter.mp hx_mem).2 ext v dsimp only split_ifs with hv1 hv2 hvi · rfl · rfl · rw [hvi, hxa] · have hv_u : v ∈ subtreeVerts G i j ∪ subtreeVerts G j i := subtreeVerts_union_eq_univ hT hij ▸ Finset.mem_univ v rw [hpart_i, hpart_j] at hv_u rcases Finset.mem_union.mp hv_u with hm | hm · exact absurd ((Finset.mem_insert.mp hm).resolve_right hv1) hvi · have hvj : v = j := (Finset.mem_insert.mp hm).resolve_right hv2 rw [hvj, hxb] right_inv := fun ⟨pi, pj⟩ => by ext ⟨u, hu⟩ · simp [hu] · have hu_not_i : u ∉ Bi := Finset.disjoint_right.mp hdisj_BiBj hu simp [hu, hu_not_i] } unfold PairwiseMRF.unnormEdgeMarginal unnormEdgeBelief rw [← Finset.sum_filter, ← Finset.sum_coe_sort] rw [Fintype.sum_equiv eFix _ (fun p => (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => p.1 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a)) * (∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => p.2 ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)))] · rw [Fintype.sum_prod_type] have hpull : ∑ pi : Bi → X, ∑ pj : Bj → X, (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => pi ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a)) * (∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => pj ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)) = (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∑ pi : Bi → X, ∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => pi ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a)) * (∑ pj : Bj → X, ∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => pj ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)) := by calc ∑ pi : Bi → X, ∑ pj : Bj → X, (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => pi ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a)) * (∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => pj ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)) = ∑ pi : Bi → X, ((M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => pi ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a))) * (∑ pj : Bj → X, ∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => pj ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)) := Finset.sum_congr rfl fun pi _ => (Finset.mul_sum _ _ _).symm _ = (∑ pi : Bi → X, (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => pi ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a))) * (∑ pj : Bj → X, ∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => pj ⟨u.1, Finset.mem_biUnion.mpr ⟨l.1, l.2, u.2⟩⟩) b)) := (Finset.sum_mul _ _ _).symm _ = _ := by rw [← Finset.mul_sum] rw [hpull, sum_pi_disjoint_biUnion Ii (fun k => subtreeVerts G k i) hdisj_Ii (fun k _ yk => subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk a)), sum_pi_disjoint_biUnion Ij (fun l => subtreeVerts G l j) hdisj_Ij (fun l _ yl => subtreeTerm M l j (extendAssign (subtreeVerts G l j) yl b))] have hzi : ∏ k ∈ Ii.attach, ∑ yk : subtreeVerts G k.1 i → X, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) yk a) = ∏ k ∈ Ii, subtreeZ M k i a := Finset.prod_attach Ii (fun k => subtreeZ M k i a) have hzj : ∏ l ∈ Ij.attach, ∑ yl : subtreeVerts G l.1 j → X, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) yl b) = ∏ l ∈ Ij, subtreeZ M l j b := Finset.prod_attach Ij (fun l => subtreeZ M l j b) rw [hzi, hzj] · intro ⟨x, hx_mem⟩ obtain ⟨hxa, hxb⟩ := (Finset.mem_filter.mp hx_mem).2 dsimp only [eFix] unfold PairwiseMRF.unnormWeight have hnode : ∏ u : V, M.ψNode u (x u) = (M.ψNode i a * ∏ k ∈ Ii, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (M.ψNode j b * ∏ l ∈ Ij, ∏ u ∈ subtreeVerts G l j, M.ψNode u (x u)) := by rw [← subtreeVerts_union_eq_univ hT hij, Finset.prod_union hdisj_ij, hpart_i, hpart_j, Finset.prod_insert hi_not_Bi, Finset.prod_insert hj_not_Bj, Finset.prod_biUnion hdisj_Ii, Finset.prod_biUnion hdisj_Ij, hxa, hxb] have hedge_not : s(i, j) ∉ inducedEdges G (subtreeVerts G i j) ∪ inducedEdges G (subtreeVerts G j i) := by intro hm rcases Finset.mem_union.mp hm with h1 | h2 · exact Finset.disjoint_left.mp hdisj_ij ((mem_inducedEdges.mp h1).2 j (Sym2.mem_mk_right i j)) (self_mem_subtreeVerts hT hij.symm) · exact Finset.disjoint_left.mp hdisj_ij (self_mem_subtreeVerts hT hij) ((mem_inducedEdges.mp h2).2 i (Sym2.mem_mk_left i j)) have hedge : ∏ e ∈ G.edgeFinset, M.edgeWeight x e = M.ψEdge i j a b * (∏ k ∈ Ii, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) * (∏ l ∈ Ij, (M.ψEdge l j (x l) (x j) * ∏ e ∈ inducedEdges G (subtreeVerts G l j), M.edgeWeight x e)) := by rw [edgeFinset_split_edge hT hij, Finset.prod_insert hedge_not, Finset.prod_union (inducedEdges_disjoint_of_verts_disjoint hdisj_ij), PairwiseMRF.edgeWeight_mk, hxa, hxb, inducedEdges_subtree_star hT hij, inducedEdges_subtree_star hT hij.symm, Finset.prod_biUnion ((branch_edges_pairwiseDisjoint hT i).subset (Finset.erase_subset j _)), Finset.prod_biUnion ((branch_edges_pairwiseDisjoint hT j).subset (Finset.erase_subset i _)), ← mul_assoc] congr 1 · congr 1 refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hik), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm k i, hxa] · refine Finset.prod_congr rfl fun l hl => ?_ have hjl : G.Adj j l := (SimpleGraph.mem_neighborFinset G j l).mp (Finset.mem_of_mem_erase hl) rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hjl), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm l j, hxb] have hpi : (∏ k ∈ Ii, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ Ii, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) = ∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach Ii] refine Finset.prod_congr rfl fun ⟨k, hk⟩ _ => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hcongr : subtreeTerm M k i x = subtreeTerm M k i (extendAssign (subtreeVerts G k i) (fun u => x u.1) a) := by refine subtreeTerm_congr M k i (fun u hu => by simp [extendAssign, hu]) ?_ (self_mem_subtreeVerts hT hik.symm) have hi_not : i ∉ subtreeVerts G k i := Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) simp [extendAssign, hi_not, hxa] rw [← hcongr]; unfold subtreeTerm; ring have hpj : (∏ l ∈ Ij, ∏ u ∈ subtreeVerts G l j, M.ψNode u (x u)) * (∏ l ∈ Ij, (M.ψEdge l j (x l) (x j) * ∏ e ∈ inducedEdges G (subtreeVerts G l j), M.edgeWeight x e)) = ∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => x u.1) b) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach Ij] refine Finset.prod_congr rfl fun ⟨l, hl⟩ _ => ?_ have hjl : G.Adj j l := (SimpleGraph.mem_neighborFinset G j l).mp (Finset.mem_of_mem_erase hl) have hcongr : subtreeTerm M l j x = subtreeTerm M l j (extendAssign (subtreeVerts G l j) (fun u => x u.1) b) := by refine subtreeTerm_congr M l j (fun u hu => by simp [extendAssign, hu]) ?_ (self_mem_subtreeVerts hT hjl.symm) have hj_not : j ∉ subtreeVerts G l j := Finset.disjoint_left.mp (subtreeVerts_disjoint j l) (self_mem_subtreeVerts hT hjl) simp [extendAssign, hj_not, hxb] rw [← hcongr]; unfold subtreeTerm; ring rw [hnode, hedge] calc (M.ψNode i a * ∏ k ∈ Ii, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (M.ψNode j b * ∏ l ∈ Ij, ∏ u ∈ subtreeVerts G l j, M.ψNode u (x u)) * (M.ψEdge i j a b * (∏ k ∈ Ii, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) * (∏ l ∈ Ij, (M.ψEdge l j (x l) (x j) * ∏ e ∈ inducedEdges G (subtreeVerts G l j), M.edgeWeight x e))) = (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * ((∏ k ∈ Ii, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ Ii, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e))) * ((∏ l ∈ Ij, ∏ u ∈ subtreeVerts G l j, M.ψNode u (x u)) * (∏ l ∈ Ij, (M.ψEdge l j (x l) (x j) * ∏ e ∈ inducedEdges G (subtreeVerts G l j), M.edgeWeight x e))) := by ring _ = (M.ψNode i a * M.ψNode j b * M.ψEdge i j a b) * (∏ k ∈ Ii.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a)) * (∏ l ∈ Ij.attach, subtreeTerm M l.1 j (extendAssign (subtreeVerts G l.1 j) (fun u => x u.1) b)) := by rw [hpi, hpj] /-- **Headline Theorem 1A (Exactness of Tree Sum-Product BP: Single-Node Marginals)**: For ANY finite tree `T`, ANY strictly positive pairwise MRF `M`, ANY strictly positive initial messages `m0`, and ANY step count `t ≥ diam(T)`, synchronous normalized Sum-Product BP recovers the **exact** single-node marginal `p_i(a)` at every vertex `i`. -/ theorem tree_sumProd_nodeMarginal_exact (hT : G.IsTree) (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) {t : ℕ} (ht : treeDiam G ≤ t) (i : V) (a : X) : nodeBelief M (sumProdNormIter M m0 t) i a = M.nodeMarginal i a := by rw [nodeBelief_congr_msgProp M (sumProdNormIter_msgProp_unnorm M hm0 t) i a] have hEq : nodeBelief M (sumProdUnnormIter M m0 t) i a = nodeBelief M (subtreeZ M) i a := by unfold nodeBelief unnormNodeBelief have hprod : ∀ x, ∏ k ∈ G.neighborFinset i, sumProdUnnormIter M m0 t k i x = ∏ k ∈ G.neighborFinset i, subtreeZ M k i x := fun x => Finset.prod_congr rfl fun k hk => by have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk exact sumProdUnnormIter_eq_subtreeZ hT M m0 (subtreeHeight G k i) k i hik.symm le_rfl t (le_trans (subtreeHeight_add_one_le_treeDiam hT hik.symm) ht) x simp_rw [hprod] rw [hEq, nodeBelief] simp_rw [← unnormNodeMarginal_eq_subtreeZ_prod hT M] rw [M.sum_unnormNodeMarginal i, M.nodeMarginal_eq_div_Z i a] /-- **Headline Theorem 1B (Exactness of Tree Sum-Product BP: Pairwise Edge Marginals)**: Under the same hypotheses, for every edge `{i, j} ∈ E(T)` and every `t ≥ diam(T)`, synchronous normalized Sum-Product BP recovers the **exact** pairwise edge marginal `p_{ij}(a, b)`. -/ theorem tree_sumProd_edgeMarginal_exact (hT : G.IsTree) (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) {t : ℕ} (ht : treeDiam G ≤ t) {i j : V} (hij : G.Adj i j) (a b : X) : edgeBelief M (sumProdNormIter M m0 t) i j a b = M.edgeMarginal i j a b := by rw [edgeBelief_congr_msgProp M (sumProdNormIter_msgProp_unnorm M hm0 t) i j a b] have hEq : edgeBelief M (sumProdUnnormIter M m0 t) i j a b = edgeBelief M (subtreeZ M) i j a b := by unfold edgeBelief unnormEdgeBelief have hpi : ∀ x, ∏ k ∈ (G.neighborFinset i).erase j, sumProdUnnormIter M m0 t k i x = ∏ k ∈ (G.neighborFinset i).erase j, subtreeZ M k i x := fun x => Finset.prod_congr rfl fun k hk => by have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) exact sumProdUnnormIter_eq_subtreeZ hT M m0 (subtreeHeight G k i) k i hik.symm le_rfl t (le_trans (subtreeHeight_add_one_le_treeDiam hT hik.symm) ht) x have hpj : ∀ y, ∏ l ∈ (G.neighborFinset j).erase i, sumProdUnnormIter M m0 t l j y = ∏ l ∈ (G.neighborFinset j).erase i, subtreeZ M l j y := fun y => Finset.prod_congr rfl fun l hl => by have hjl : G.Adj j l := (SimpleGraph.mem_neighborFinset G j l).mp (Finset.mem_of_mem_erase hl) exact sumProdUnnormIter_eq_subtreeZ hT M m0 (subtreeHeight G l j) l j hjl.symm le_rfl t (le_trans (subtreeHeight_add_one_le_treeDiam hT hjl.symm) ht) y simp_rw [hpi, hpj] rw [hEq, edgeBelief] simp_rw [← unnormEdgeMarginal_eq_subtreeZ_prod hT M hij] rw [M.sum_unnormEdgeMarginal i j, M.edgeMarginal_eq_div_Z i j a b] /-! ## Section 5: Exact Tree Gibbs Factorization & Bethe Free Energy Identity -/ omit [Nonempty X] in /-- The unnormalized product over all directed edges of `subtreeZ` cancels exactly between node and edge marginals, yielding the exact tree Gibbs factorization. -/ lemma tree_edge_node_ratio (hT : G.IsTree) (M : PairwiseMRF G X) {i j : V} (hij : G.Adj i j) (a b : X) : M.unnormEdgeMarginal i j a b * (subtreeZ M j i a * subtreeZ M i j b) = M.ψEdge i j a b * (M.unnormNodeMarginal i a * M.unnormNodeMarginal j b) := by rw [unnormEdgeMarginal_eq_subtreeZ_prod hT M hij a b, unnormNodeMarginal_eq_subtreeZ_prod hT M i a, unnormNodeMarginal_eq_subtreeZ_prod hT M j b] unfold unnormEdgeBelief unnormNodeBelief have hj_mem : j ∈ G.neighborFinset i := (SimpleGraph.mem_neighborFinset G i j).mpr hij have hi_mem : i ∈ G.neighborFinset j := (SimpleGraph.mem_neighborFinset G j i).mpr hij.symm rw [← Finset.mul_prod_erase (G.neighborFinset i) (fun k => subtreeZ M k i a) hj_mem, ← Finset.mul_prod_erase (G.neighborFinset j) (fun l => subtreeZ M l j b) hi_mem] ring /-- Product of `f i (x i) ^ (G.degree i)` equals the product over edges of `f i (x i) * f j (x j)`. -/ lemma prod_edgeFinset_mul_eq_prod_pow_degree (f : V → ℝ) : (∏ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => f i * f j, fun i j => mul_comm (f i) (f j)⟩ e) = ∏ i : V, (f i) ^ (G.degree i) := by have hgen : ∀ (s : Finset (Sym2 V)), (∀ e ∈ s, ¬ e.IsDiag) → (∏ e ∈ s, Sym2.lift ⟨fun i j => f i * f j, fun i j => mul_comm (f i) (f j)⟩ e) = ∏ i : V, (f i) ^ (s.filter (fun e => i ∈ e)).card := by intro s induction s using Finset.induction_on with | empty => simp | @insert e s he ih => intro hnd have he_nd : ¬ e.IsDiag := hnd e (Finset.mem_insert_self e s) have hs_nd : ∀ e' ∈ s, ¬ e'.IsDiag := fun e' he' => hnd e' (Finset.mem_insert_of_mem he') rw [Finset.prod_insert he, ih hs_nd] induction e using Sym2.ind with | _ u v => have huv : u ≠ v := by simpa [Sym2.mk_isDiag_iff] using he_nd simp only [Sym2.lift_mk] have hcard : ∀ i : V, ((insert s(u, v) s).filter fun e' => i ∈ e').card = (s.filter fun e' => i ∈ e').card + (if i = u ∨ i = v then 1 else 0) := by intro i rw [Finset.filter_insert] have hmem : i ∈ s(u, v) ↔ i = u ∨ i = v := Sym2.mem_iff by_cases h1 : i ∈ s(u, v) · have h2 : i = u ∨ i = v := hmem.mp h1 rw [if_pos h1, if_pos h2, Finset.card_insert_of_notMem (by simp [he])] · have h2 : ¬ (i = u ∨ i = v) := fun h => h1 (hmem.mpr h) rw [if_neg h1, if_neg h2]; omega simp_rw [hcard, pow_add] rw [Finset.prod_mul_distrib, mul_comm] congr 1 rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ u), ← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨huv.symm, Finset.mem_univ v⟩)] have hrest : ∏ i ∈ (Finset.univ.erase u).erase v, f i ^ (if i = u ∨ i = v then 1 else 0) = 1 := by refine Finset.prod_eq_one fun i hi => ?_ have hv := Finset.ne_of_mem_erase hi have hu := Finset.ne_of_mem_erase (Finset.mem_of_mem_erase hi) simp [hu, hv] rw [hrest]; simp rw [hgen G.edgeFinset (fun e he => G.not_isDiag_of_mem_edgeSet (G.mem_edgeFinset.mp he))] refine Finset.prod_congr rfl fun i _ => ?_ congr 1 rw [← G.card_neighborFinset_eq_degree] refine (Finset.card_bij (fun (j : V) (_ : j ∈ G.neighborFinset i) => s(i, j)) ?_ ?_ ?_).symm · intro j hj exact Finset.mem_filter.mpr ⟨G.mem_edgeFinset.mpr ((SimpleGraph.mem_neighborFinset G i j).mp hj), Sym2.mem_mk_left i j⟩ · intro j₁ hj₁ j₂ _ h have hm : j₁ ∈ s(i, j₂) := h ▸ Sym2.mem_mk_right i j₁ rcases Sym2.mem_iff.mp hm with rfl | rfl · exact absurd ((SimpleGraph.mem_neighborFinset G j₁ j₁).mp hj₁) (G.loopless.irrefl j₁) · rfl · intro e he rcases Finset.mem_filter.mp he with ⟨he_edge, hi_mem⟩ induction e using Sym2.ind with | _ u v => rcases Sym2.mem_iff.mp hi_mem with hui | hvi · rw [← hui] at he_edge exact ⟨v, (SimpleGraph.mem_neighborFinset G i v).mpr (G.mem_edgeFinset.mp he_edge), by rw [hui]⟩ · rw [← hvi] at he_edge exact ⟨u, (SimpleGraph.mem_neighborFinset G i u).mpr (G.mem_edgeFinset.mp he_edge).symm, by rw [hvi, Sym2.eq_swap]⟩ omit [DecidableEq X] [Nonempty X] in /-- Product over edges of `subtreeZ M j i (x i) * subtreeZ M i j (x j)` equals `∏ i, ∏ k ∈ N(i), subtreeZ M k i (x i)`. -/ lemma prod_edgeFinset_subtreeZ_pair (M : PairwiseMRF G X) (x : V → X) : (∏ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => subtreeZ M j i (x i) * subtreeZ M i j (x j), fun _ _ => mul_comm _ _⟩ e) = ∏ i : V, ∏ k ∈ G.neighborFinset i, subtreeZ M k i (x i) := by have hgen : ∀ (s : Finset (Sym2 V)), (∀ e ∈ s, ¬ e.IsDiag) → (∏ e ∈ s, Sym2.lift ⟨fun i j => subtreeZ M j i (x i) * subtreeZ M i j (x j), fun _ _ => mul_comm _ _⟩ e) = ∏ i : V, ∏ k ∈ Finset.univ.filter (fun k => s(i, k) ∈ s), subtreeZ M k i (x i) := by intro s induction s using Finset.induction_on with | empty => simp | @insert e s he ih => intro hnd have he_nd : ¬ e.IsDiag := hnd e (Finset.mem_insert_self e s) have hs_nd : ∀ e' ∈ s, ¬ e'.IsDiag := fun e' he' => hnd e' (Finset.mem_insert_of_mem he') rw [Finset.prod_insert he, ih hs_nd] induction e using Sym2.ind with | _ u v => have huv : u ≠ v := by simpa [Sym2.mk_isDiag_iff] using he_nd simp only [Sym2.lift_mk] have hstep : ∀ i : V, (∏ k ∈ Finset.univ.filter (fun k => s(i, k) ∈ insert s(u, v) s), subtreeZ M k i (x i)) = (if i = u then subtreeZ M v u (x u) else if i = v then subtreeZ M u v (x v) else 1) * (∏ k ∈ Finset.univ.filter (fun k => s(i, k) ∈ s), subtreeZ M k i (x i)) := by intro i by_cases hiu : i = u · subst hiu have hset : Finset.univ.filter (fun k => s(i, k) ∈ insert s(i, v) s) = insert v (Finset.univ.filter (fun k => s(i, k) ∈ s)) := by ext k; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · left have : k ∈ s(i, v) := heq ▸ Sym2.mem_mk_right i k rcases Sym2.mem_iff.mp this with rfl | rfl · exfalso have : s(k, k) = s(k, v) := heq have hvm : v ∈ s(k, k) := this ▸ Sym2.mem_mk_right k v rcases Sym2.mem_iff.mp hvm with rfl | rfl <;> exact huv rfl · rfl · exact Or.inr hm · rintro (rfl | hm); exact Or.inl rfl; exact Or.inr hm rw [hset, Finset.prod_insert (by simp [he]), if_pos rfl] · by_cases hiv : i = v · subst hiv have hset : Finset.univ.filter (fun k => s(i, k) ∈ insert s(u, i) s) = insert u (Finset.univ.filter (fun k => s(i, k) ∈ s)) := by ext k; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · left have : k ∈ s(u, i) := heq ▸ Sym2.mem_mk_right i k rcases Sym2.mem_iff.mp this with rfl | rfl · rfl · exfalso have hvm : u ∈ s(k, k) := heq ▸ Sym2.mem_mk_left u k rcases Sym2.mem_iff.mp hvm with rfl | rfl <;> exact huv rfl · exact Or.inr hm · rintro (rfl | hm); exact Or.inl Sym2.eq_swap; exact Or.inr hm rw [hset, Finset.prod_insert (by simp [Sym2.eq_swap, he]), if_neg hiu, if_pos rfl] · have hset : Finset.univ.filter (fun k => s(i, k) ∈ insert s(u, v) s) = Finset.univ.filter (fun k => s(i, k) ∈ s) := by ext k; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · exfalso have : i ∈ s(u, v) := heq ▸ Sym2.mem_mk_left i k rcases Sym2.mem_iff.mp this with rfl | rfl · exact hiu rfl · exact hiv rfl · exact hm · exact Or.inr rw [hset, if_neg hiu, if_neg hiv, one_mul] simp_rw [hstep] rw [Finset.prod_mul_distrib] congr 1 rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ u), ← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨huv.symm, Finset.mem_univ v⟩)] have hrest : ∏ i ∈ (Finset.univ.erase u).erase v, (if i = u then subtreeZ M v u (x u) else if i = v then subtreeZ M u v (x v) else 1) = 1 := by refine Finset.prod_eq_one fun i hi => ?_ have hv := Finset.ne_of_mem_erase hi have hu := Finset.ne_of_mem_erase (Finset.mem_of_mem_erase hi) simp [hu, hv] rw [hrest, if_pos rfl, if_neg huv.symm, if_pos rfl, mul_one] rw [hgen G.edgeFinset (fun e he => G.not_isDiag_of_mem_edgeSet (G.mem_edgeFinset.mp he))] refine Finset.prod_congr rfl fun i _ => ?_ congr 1; ext k simp [SimpleGraph.mem_neighborFinset, G.mem_edgeFinset] /-- **Headline Theorem 1C (Exact Tree Gibbs Factorization)**: On any finite tree `T`, the exact Gibbs distribution satisfies `p(x) · ∏_{i ∈ V} p_i(x_i)^(d_i) = (∏_{i ∈ V} p_i(x_i)) · ∏_{e ∈ E(T)} p_e(x)` (equivalently `p(x) = (∏_e p_e(x)) / (∏_i p_i(x_i)^(d_i - 1))`). -/ theorem tree_gibbs_factorization (hT : G.IsTree) (M : PairwiseMRF G X) (x : V → X) : M.gibbs x * (∏ i : V, (M.nodeMarginal i (x i)) ^ (G.degree i)) = (∏ i : V, M.nodeMarginal i (x i)) * (∏ e ∈ G.edgeFinset, M.edgeMarginalAt x e) := by -- Use `|E| + 1 = |V|` (`hT.card_edgeFinset`) and the unnormalized edge-node ratio! have hcard : G.edgeFinset.card + 1 = Fintype.card V := by have h := hT.card_edgeFinset omega have hdeg : ∑ i : V, G.degree i = 2 * G.edgeFinset.card := G.sum_degrees_eq_twice_card_edges have hZ : M.Z ≠ 0 := ne_of_gt M.Z_pos -- Multiply both sides by `Z ^ (2*|E| + 1)` via unnormalized quantities have hprod_ratio : (∏ e ∈ G.edgeFinset, M.edgeMarginalAt x e) * (∏ i : V, ∏ k ∈ G.neighborFinset i, subtreeZ M k i (x i)) = (∏ e ∈ G.edgeFinset, M.edgeWeight x e) * (∏ i : V, (M.unnormNodeMarginal i (x i)) ^ (G.degree i)) / M.Z ^ G.edgeFinset.card := by rw [← prod_edgeFinset_subtreeZ_pair M x, ← Finset.prod_mul_distrib, ← prod_edgeFinset_mul_eq_prod_pow_degree (fun i => M.unnormNodeMarginal i (x i))] have hstep : ∀ e ∈ G.edgeFinset, M.edgeMarginalAt x e * Sym2.lift ⟨fun i j => subtreeZ M j i (x i) * subtreeZ M i j (x j), fun i j => mul_comm _ _⟩ e = (M.edgeWeight x e * Sym2.lift ⟨fun i j => M.unnormNodeMarginal i (x i) * M.unnormNodeMarginal j (x j), fun i j => mul_comm _ _⟩ e) / M.Z := by intro e he induction e using Sym2.ind with | _ i j => have hij : G.Adj i j := G.mem_edgeFinset.mp he simp only [PairwiseMRF.edgeMarginalAt_mk, PairwiseMRF.edgeWeight_mk, Sym2.lift_mk] rw [M.edgeMarginal_eq_div_Z i j, div_mul_eq_mul_div, tree_edge_node_ratio hT M hij (x i) (x j)] rw [Finset.prod_congr rfl hstep, Finset.prod_div_distrib, Finset.prod_mul_distrib, Finset.prod_const] -- Now compare `∏ i, unnormNodeMarginal i (x i) = (∏ i, ψNode i (x i)) * (∏ i, ∏ k ∈ N(i), subtreeZ)` have hnode_prod : (∏ i : V, M.unnormNodeMarginal i (x i)) = (∏ i : V, M.ψNode i (x i)) * (∏ i : V, ∏ k ∈ G.neighborFinset i, subtreeZ M k i (x i)) := by rw [← Finset.prod_mul_distrib] refine Finset.prod_congr rfl fun i _ => ?_ rw [unnormNodeMarginal_eq_subtreeZ_prod hT M i (x i)] rfl have hZtree_pos : 0 < ∏ i : V, ∏ k ∈ G.neighborFinset i, subtreeZ M k i (x i) := Finset.prod_pos fun i _ => Finset.prod_pos fun k _ => subtreeZ_pos M k i (x i) have hZtree_ne : (∏ i : V, ∏ k ∈ G.neighborFinset i, subtreeZ M k i (x i)) ≠ 0 := ne_of_gt hZtree_pos -- Rewrite marginals as unnormalized / Z have hLHS : M.gibbs x * (∏ i : V, (M.nodeMarginal i (x i)) ^ (G.degree i)) = M.unnormWeight x * (∏ i : V, (M.unnormNodeMarginal i (x i)) ^ (G.degree i)) / M.Z ^ (1 + 2 * G.edgeFinset.card) := by unfold PairwiseMRF.gibbs simp_rw [M.nodeMarginal_eq_div_Z, div_pow, Finset.prod_div_distrib, Finset.prod_pow_eq_pow_sum, hdeg, pow_add, pow_one] ring have hRHS : (∏ i : V, M.nodeMarginal i (x i)) * (∏ e ∈ G.edgeFinset, M.edgeMarginalAt x e) = (∏ i : V, M.unnormNodeMarginal i (x i)) * (∏ e ∈ G.edgeFinset, M.edgeMarginalAt x e) / M.Z ^ (Fintype.card V) := by simp_rw [M.nodeMarginal_eq_div_Z, Finset.prod_div_distrib, Finset.prod_const, Finset.card_univ] ring rw [hLHS, hRHS] -- Cancel `hZtree_ne` on both sides apply mul_right_cancel₀ hZtree_ne rw [div_mul_eq_mul_div, mul_assoc, div_mul_eq_mul_div, mul_assoc] rw [hprod_ratio] have hpow : M.Z ^ (1 + 2 * G.edgeFinset.card) = M.Z ^ G.edgeFinset.card * M.Z ^ Fintype.card V := by rw [← pow_add]; congr 1; omega rw [hpow] unfold PairwiseMRF.unnormWeight rw [hnode_prod] field_simp [hZ, hZtree_ne] /-- Half-sum over directed neighbor pairs equals the sum over undirected edges for any symmetric edge functional `F i j = F j i`. -/ lemma half_sum_neighbors_eq_sum_edgeFinset (F : V → V → ℝ) (hF : ∀ i j, F j i = F i j) : (1 / 2 : ℝ) * (∑ i : V, ∑ j ∈ G.neighborFinset i, F i j) = ∑ e ∈ G.edgeFinset, Sym2.lift ⟨F, fun i j => hF j i⟩ e := by have hgen : ∀ (s : Finset (Sym2 V)), (∀ e ∈ s, ¬ e.IsDiag) → (∑ i : V, ∑ j ∈ Finset.univ.filter (fun j => s(i, j) ∈ s), F i j) = 2 * ∑ e ∈ s, Sym2.lift ⟨F, fun i j => hF j i⟩ e := by intro s induction s using Finset.induction_on with | empty => simp | @insert e s he ih => intro hnd have he_nd : ¬ e.IsDiag := hnd e (Finset.mem_insert_self e s) have hs_nd : ∀ e' ∈ s, ¬ e'.IsDiag := fun e' he' => hnd e' (Finset.mem_insert_of_mem he') rw [Finset.sum_insert he, mul_add, ← ih hs_nd] induction e using Sym2.ind with | _ u v => have huv : u ≠ v := by simpa [Sym2.mk_isDiag_iff] using he_nd simp only [Sym2.lift_mk] have hstep : ∀ i : V, (∑ j ∈ Finset.univ.filter (fun j => s(i, j) ∈ insert s(u, v) s), F i j) = (if i = u then F u v else if i = v then F v u else 0) + (∑ j ∈ Finset.univ.filter (fun j => s(i, j) ∈ s), F i j) := by intro i by_cases hiu : i = u · subst hiu have hset : Finset.univ.filter (fun j => s(i, j) ∈ insert s(i, v) s) = insert v (Finset.univ.filter (fun j => s(i, j) ∈ s)) := by ext j; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · left have : j ∈ s(i, v) := heq ▸ Sym2.mem_mk_right i j rcases Sym2.mem_iff.mp this with rfl | rfl · exfalso have hvm : v ∈ s(j, j) := heq ▸ Sym2.mem_mk_right j v rcases Sym2.mem_iff.mp hvm with rfl | rfl <;> exact huv rfl · rfl · exact Or.inr hm · rintro (rfl | hm); exact Or.inl rfl; exact Or.inr hm rw [hset, Finset.sum_insert (by simp [he]), if_pos rfl] · by_cases hiv : i = v · subst hiv have hset : Finset.univ.filter (fun j => s(i, j) ∈ insert s(u, i) s) = insert u (Finset.univ.filter (fun j => s(i, j) ∈ s)) := by ext j; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · left have : j ∈ s(u, i) := heq ▸ Sym2.mem_mk_right i j rcases Sym2.mem_iff.mp this with rfl | rfl · rfl · exfalso have hvm : u ∈ s(j, j) := heq ▸ Sym2.mem_mk_left u j rcases Sym2.mem_iff.mp hvm with rfl | rfl <;> exact huv rfl · exact Or.inr hm · rintro (rfl | hm); exact Or.inl Sym2.eq_swap; exact Or.inr hm rw [hset, Finset.sum_insert (by simp [Sym2.eq_swap, he]), if_neg hiu, if_pos rfl] · have hset : Finset.univ.filter (fun j => s(i, j) ∈ insert s(u, v) s) = Finset.univ.filter (fun j => s(i, j) ∈ s) := by ext j; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · rintro (heq | hm) · exfalso have : i ∈ s(u, v) := heq ▸ Sym2.mem_mk_left i j rcases Sym2.mem_iff.mp this with rfl | rfl · exact hiu rfl · exact hiv rfl · exact hm · exact Or.inr rw [hset, if_neg hiu, if_neg hiv, zero_add] simp_rw [hstep] rw [Finset.sum_add_distrib] congr 1 rw [← Finset.add_sum_erase _ _ (Finset.mem_univ u), ← Finset.add_sum_erase _ _ (Finset.mem_erase.mpr ⟨huv.symm, Finset.mem_univ v⟩)] have hrest : ∑ i ∈ (Finset.univ.erase u).erase v, (if i = u then F u v else if i = v then F v u else 0) = 0 := by refine Finset.sum_eq_zero fun i hi => ?_ have hv := Finset.ne_of_mem_erase hi have hu := Finset.ne_of_mem_erase (Finset.mem_of_mem_erase hi) simp [hu, hv] rw [hrest, if_pos rfl, if_neg huv.symm, if_pos rfl, hF u v]; ring have h2 := hgen G.edgeFinset (fun e he => G.not_isDiag_of_mem_edgeSet (G.mem_edgeFinset.mp he)) have hfilt : ∀ i : V, Finset.univ.filter (fun j => s(i, j) ∈ G.edgeFinset) = G.neighborFinset i := by intro i; ext j; simp [SimpleGraph.mem_neighborFinset, G.mem_edgeFinset] simp_rw [hfilt] at h2 linarith /-- Discrete Shannon entropy of a distribution `p : α → ℝ`. -/ noncomputable def shannonH {α : Type*} [Fintype α] (p : α → ℝ) : ℝ := -∑ a : α, p a * Real.log (p a) /-- Pairwise Shannon entropy `H(b_{ij}) = -∑_{a,b} b_{ij}(a,b) log b_{ij}(a,b)`. -/ noncomputable def pairH (b : X → X → ℝ) : ℝ := -∑ a : X, ∑ c : X, b a c * Real.log (b a c) /-- Bethe average energy `U_Bethe(b) = -∑_i E_{b_i}[log ψ_i] - ∑_{e={i,j}} E_{b_e}[log ψ_e]`. -/ noncomputable def betheEnergy (M : PairwiseMRF G X) (bNode : V → X → ℝ) (bEdge : V → V → X → X → ℝ) : ℝ := -(∑ i : V, ∑ a : X, bNode i a * Real.log (M.ψNode i a)) - (1 / 2 : ℝ) * ∑ i : V, ∑ j ∈ G.neighborFinset i, ∑ a : X, ∑ c : X, bEdge i j a c * Real.log (M.ψEdge i j a c) /-- Bethe entropy `H_Bethe(b) = ∑_{e={i,j}} H(b_{ij}) - ∑_i (d_i - 1) H(b_i)`. -/ noncomputable def betheEntropy (G : SimpleGraph V) [DecidableRel G.Adj] (bNode : V → X → ℝ) (bEdge : V → V → X → X → ℝ) : ℝ := (1 / 2 : ℝ) * (∑ i : V, ∑ j ∈ G.neighborFinset i, pairH (bEdge i j)) - ∑ i : V, ((G.degree i : ℝ) - 1) * shannonH (bNode i) /-- Bethe Free Energy `F_Bethe(b) = U_Bethe(b) - H_Bethe(b)`. -/ noncomputable def betheFreeEnergy (M : PairwiseMRF G X) (bNode : V → X → ℝ) (bEdge : V → V → X → X → ℝ) : ℝ := betheEnergy M bNode bEdge - betheEntropy G bNode bEdge /-- **Headline Theorem 1D (Exact Tree Bethe Log-Partition Identity via BP)**: For ANY finite tree `T`, ANY strictly positive pairwise MRF `M`, ANY strictly positive initial messages `m0`, and ANY `t ≥ diam(T)`, the Bethe Free Energy evaluated at the converged Sum-Product BP beliefs `b^(t)` recovers the **exact** log-partition function: `-F_Bethe(b^(t)) = log Z`. -/ theorem tree_bp_bethe_logZ_exact (hT : G.IsTree) (M : PairwiseMRF G X) {m0 : V → V → X → ℝ} (hm0 : ∀ u v a, G.Adj u v → 0 < m0 u v a) {t : ℕ} (ht : treeDiam G ≤ t) : -betheFreeEnergy M (nodeBelief M (sumProdNormIter M m0 t)) (edgeBelief M (sumProdNormIter M m0 t)) = Real.log M.Z := by -- First replace converged BP beliefs with exact marginals `M.nodeMarginal`, `M.edgeMarginal` have hNode : nodeBelief M (sumProdNormIter M m0 t) = M.nodeMarginal := by funext i a; exact tree_sumProd_nodeMarginal_exact hT M hm0 ht i a have hEdge_congr : ∀ i : V, ∀ j ∈ G.neighborFinset i, edgeBelief M (sumProdNormIter M m0 t) i j = M.edgeMarginal i j := by intro i j hj funext a b exact tree_sumProd_edgeMarginal_exact hT M hm0 ht ((SimpleGraph.mem_neighborFinset G i j).mp hj) a b have hFE_eq : betheFreeEnergy M (nodeBelief M (sumProdNormIter M m0 t)) (edgeBelief M (sumProdNormIter M m0 t)) = betheFreeEnergy M M.nodeMarginal M.edgeMarginal := by unfold betheFreeEnergy betheEnergy betheEntropy rw [hNode] congr 1 · congr 1; congr 1 refine Finset.sum_congr rfl fun i _ => Finset.sum_congr rfl fun j hj => ?_ rw [hEdge_congr i j hj] · congr 1; congr 1 refine Finset.sum_congr rfl fun i _ => Finset.sum_congr rfl fun j hj => ?_ rw [hEdge_congr i j hj] rw [hFE_eq] -- Now prove `-betheFreeEnergy M M.nodeMarginal M.edgeMarginal = log Z` directly from -- `tree_gibbs_factorization`! -- Step 1: Take log of `tree_gibbs_factorization` for every configuration `x : V → X` have hlog_fac : ∀ x : V → X, Real.log (M.gibbs x) = (∑ e ∈ G.edgeFinset, Real.log (M.edgeMarginalAt x e)) - ∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i)) := by intro x have hfac := tree_gibbs_factorization hT M x have hlhs_pos : 0 < M.gibbs x * ∏ i : V, (M.nodeMarginal i (x i)) ^ (G.degree i) := mul_pos (M.gibbs_pos x) (Finset.prod_pos fun i _ => pow_pos (M.nodeMarginal_pos i (x i)) _) have hrhs_pos : 0 < (∏ i : V, M.nodeMarginal i (x i)) * ∏ e ∈ G.edgeFinset, M.edgeMarginalAt x e := by refine mul_pos (Finset.prod_pos fun i _ => M.nodeMarginal_pos i (x i)) (Finset.prod_pos fun e he => ?_) induction e using Sym2.ind with | _ i j => exact M.edgeMarginal_pos (G.mem_edgeFinset.mp he).ne (x i) (x j) have hlog := congrArg Real.log hfac rw [Real.log_mul (ne_of_gt (M.gibbs_pos x)) (ne_of_gt (Finset.prod_pos fun i _ => pow_pos (M.nodeMarginal_pos i (x i)) _)), Real.log_prod (fun i _ => ne_of_gt (pow_pos (M.nodeMarginal_pos i (x i)) _)), Real.log_mul (ne_of_gt (Finset.prod_pos fun i _ => M.nodeMarginal_pos i (x i))) (ne_of_gt (Finset.prod_pos fun e he => by induction e using Sym2.ind with | _ i j => exact M.edgeMarginal_pos (G.mem_edgeFinset.mp he).ne (x i) (x j))), Real.log_prod (fun i _ => ne_of_gt (M.nodeMarginal_pos i (x i))), Real.log_prod (fun e he => ne_of_gt (by induction e using Sym2.ind with | _ i j => exact M.edgeMarginal_pos (G.mem_edgeFinset.mp he).ne (x i) (x j)))] at hlog simp_rw [Real.log_pow] at hlog have hsub : (∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) = (∑ i : V, (G.degree i : ℝ) * Real.log (M.nodeMarginal i (x i))) - ∑ i : V, Real.log (M.nodeMarginal i (x i)) := by rw [← Finset.sum_sub_distrib] refine Finset.sum_congr rfl fun i _ => by ring linarith -- Step 2: Also `log (M.gibbs x) = ∑_i log ψ_i(x_i) + ∑_e log ψ_e(x) - log Z` have hlog_gibbs : ∀ x : V → X, Real.log (M.gibbs x) = (∑ i : V, Real.log (M.ψNode i (x i))) + (∑ e ∈ G.edgeFinset, Real.log (M.edgeWeight x e)) - Real.log M.Z := by intro x unfold PairwiseMRF.gibbs PairwiseMRF.unnormWeight rw [Real.log_div (ne_of_gt (mul_pos (Finset.prod_pos fun i _ => M.ψNode_pos i (x i)) (Finset.prod_pos fun e _ => M.edgeWeight_pos x e))) (ne_of_gt M.Z_pos), Real.log_mul (ne_of_gt (Finset.prod_pos fun i _ => M.ψNode_pos i (x i))) (ne_of_gt (Finset.prod_pos fun e _ => M.edgeWeight_pos x e)), Real.log_prod (fun i _ => ne_of_gt (M.ψNode_pos i (x i))), Real.log_prod (fun e _ => ne_of_gt (M.edgeWeight_pos x e))] -- Step 3: Average `hlog_gibbs x - hlog_fac x` over `x` weighted by `M.gibbs x`! -- Node expectation lemma have hE_node : ∀ (f : V → X → ℝ) (i : V), (∑ x : V → X, M.gibbs x * f i (x i)) = ∑ a : X, M.nodeMarginal i a * f i a := by intro f i unfold PairwiseMRF.nodeMarginal simp_rw [Finset.sum_mul] rw [Finset.sum_comm] refine Finset.sum_congr rfl fun x _ => ?_ rw [Finset.sum_eq_single (x i)] · simp · intro a _ ha; simp [ha.symm] · intro h; exact absurd (Finset.mem_univ _) h -- Edge expectation lemma have hE_edge : ∀ (F : V → V → X → X → ℝ) (hF : ∀ i j a b, F j i b a = F i j a b) (e : Sym2 V), (∑ x : V → X, M.gibbs x * Sym2.lift ⟨fun i j => F i j (x i) (x j), fun i j => hF j i (x j) (x i)⟩ e) = Sym2.lift ⟨fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * F i j a b, fun i j => by dsimp only rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm, hF]⟩ e := by intro F hF e induction e using Sym2.ind with | _ i j => simp only [Sym2.lift_mk] unfold PairwiseMRF.edgeMarginal simp_rw [Finset.sum_mul] calc ∑ x : V → X, M.gibbs x * F i j (x i) (x j) = ∑ x : V → X, ∑ a : X, ∑ b : X, (if x i = a ∧ x j = b then M.gibbs x else 0) * F i j a b := by refine Finset.sum_congr rfl fun x _ => ?_ rw [Finset.sum_eq_single (x i)] · rw [Finset.sum_eq_single (x j)] · simp · intro b _ hb; simp [hb.symm] · intro h; exact absurd (Finset.mem_univ _) h · intro a _ ha refine Finset.sum_eq_zero fun b _ => ?_; simp [ha.symm] · intro h; exact absurd (Finset.mem_univ _) h _ = ∑ a : X, ∑ x : V → X, ∑ b : X, (if x i = a ∧ x j = b then M.gibbs x else 0) * F i j a b := Finset.sum_comm _ = ∑ a : X, ∑ b : X, ∑ x : V → X, (if x i = a ∧ x j = b then M.gibbs x else 0) * F i j a b := Finset.sum_congr rfl fun a _ => Finset.sum_comm -- Combine expectation of `log Z = (∑_i log ψ_i + ∑_e log ψ_e) - (∑_e log p_e - ∑_i (d_i - 1) log p_i)` have hpoint : ∀ x : V → X, M.gibbs x * Real.log M.Z = M.gibbs x * ((∑ i : V, Real.log (M.ψNode i (x i))) + (∑ e ∈ G.edgeFinset, Real.log (M.edgeWeight x e)) - (∑ e ∈ G.edgeFinset, Real.log (M.edgeMarginalAt x e)) + (∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i)))) := by intro x have h1 := hlog_gibbs x have h2 := hlog_fac x congr 1; linarith have hsum : Real.log M.Z = (∑ i : V, ∑ a : X, M.nodeMarginal i a * Real.log (M.ψNode i a)) + (∑ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.ψEdge i j a b), fun i j => by dsimp only rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm, M.ψEdge_symm]⟩ e) - (∑ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.edgeMarginal i j a b), fun i j => by dsimp only rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm]⟩ e) + (∑ i : V, ((G.degree i : ℝ) - 1) * ∑ a : X, M.nodeMarginal i a * Real.log (M.nodeMarginal i a)) := by have hZ_sum : ∑ x : V → X, M.gibbs x * Real.log M.Z = Real.log M.Z := by rw [← Finset.sum_mul, M.sum_gibbs, one_mul] have hA : (∑ x : V → X, M.gibbs x * ∑ i : V, Real.log (M.ψNode i (x i))) = ∑ i : V, ∑ a : X, M.nodeMarginal i a * Real.log (M.ψNode i a) := by simp_rw [Finset.mul_sum]; rw [Finset.sum_comm] exact Finset.sum_congr rfl fun i _ => hE_node (fun j a => Real.log (M.ψNode j a)) i have hB : (∑ x : V → X, M.gibbs x * ∑ e ∈ G.edgeFinset, Real.log (M.edgeWeight x e)) = ∑ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.ψEdge i j a b), fun i j => by dsimp only; rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm, M.ψEdge_symm]⟩ e := by simp_rw [Finset.mul_sum]; rw [Finset.sum_comm] refine Finset.sum_congr rfl fun e _ => ?_ have heq := hE_edge (fun i j a b => Real.log (M.ψEdge i j a b)) (fun i j a b => by rw [M.ψEdge_symm]) e rw [← heq] refine Finset.sum_congr rfl fun x _ => ?_ induction e using Sym2.ind with | _ i j => rfl have hC : (∑ x : V → X, M.gibbs x * ∑ e ∈ G.edgeFinset, Real.log (M.edgeMarginalAt x e)) = ∑ e ∈ G.edgeFinset, Sym2.lift ⟨fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.edgeMarginal i j a b), fun i j => by dsimp only; rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm]⟩ e := by simp_rw [Finset.mul_sum]; rw [Finset.sum_comm] refine Finset.sum_congr rfl fun e _ => ?_ have heq := hE_edge (fun i j a b => Real.log (M.edgeMarginal i j a b)) (fun i j a b => by rw [M.edgeMarginal_symm]) e rw [← heq] refine Finset.sum_congr rfl fun x _ => ?_ induction e using Sym2.ind with | _ i j => simp have hD : (∑ x : V → X, M.gibbs x * ∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) = ∑ i : V, ((G.degree i : ℝ) - 1) * ∑ a : X, M.nodeMarginal i a * Real.log (M.nodeMarginal i a) := by have h1 : (∑ x : V → X, M.gibbs x * ∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) = ∑ i : V, ∑ x : V → X, M.gibbs x * (((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) := by simp_rw [Finset.mul_sum]; exact Finset.sum_comm rw [h1] refine Finset.sum_congr rfl fun i _ => ?_ have hfactor : ∑ x : V → X, M.gibbs x * (((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) = ((G.degree i : ℝ) - 1) * ∑ x : V → X, M.gibbs x * Real.log (M.nodeMarginal i (x i)) := by rw [Finset.mul_sum] exact Finset.sum_congr rfl fun x _ => by ring rw [hfactor, hE_node (fun j a => Real.log (M.nodeMarginal j a)) i] rw [← hZ_sum] have hsum_pt : ∑ x : V → X, M.gibbs x * Real.log M.Z = (∑ x : V → X, M.gibbs x * ∑ i : V, Real.log (M.ψNode i (x i))) + (∑ x : V → X, M.gibbs x * ∑ e ∈ G.edgeFinset, Real.log (M.edgeWeight x e)) - (∑ x : V → X, M.gibbs x * ∑ e ∈ G.edgeFinset, Real.log (M.edgeMarginalAt x e)) + (∑ x : V → X, M.gibbs x * ∑ i : V, ((G.degree i : ℝ) - 1) * Real.log (M.nodeMarginal i (x i))) := by simp_rw [hpoint, mul_add, mul_sub, mul_add, Finset.sum_add_distrib, Finset.sum_sub_distrib, Finset.sum_add_distrib] rw [hsum_pt, hA, hB, hC, hD] rw [hsum] unfold betheFreeEnergy betheEnergy betheEntropy shannonH pairH rw [← half_sum_neighbors_eq_sum_edgeFinset (fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.ψEdge i j a b)) (fun i j => by rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm, M.ψEdge_symm])] rw [← half_sum_neighbors_eq_sum_edgeFinset (fun i j => ∑ a : X, ∑ b : X, M.edgeMarginal i j a b * Real.log (M.edgeMarginal i j a b)) (fun i j => by rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_congr rfl fun b _ => ?_ rw [M.edgeMarginal_symm])] simp only [mul_neg, Finset.sum_neg_distrib] ring end ExactTreeSumProduct /-! ## Section 6: Exact Tree Max-Sum & Max-Product Belief Propagation -/ section ExactTreeMaxProduct variable {V X : Type*} {G : SimpleGraph V} variable [Fintype V] [DecidableEq V] [DecidableRel G.Adj] variable [Fintype X] [DecidableEq X] [Nonempty X] /-- Exact subtree MAP log-weight `M_{i → j}(x_j) = max_{y : T_{i \ j} → X} log subtreeTerm(y, x_j)`. -/ noncomputable def subtreeMaxLog (M : PairwiseMRF G X) (i j : V) (xj : X) : ℝ := Finset.univ.sup' Finset.univ_nonempty fun y : subtreeVerts G i j → X => Real.log (subtreeTerm M i j (extendAssign (subtreeVerts G i j) y xj)) /-- Distributivity of `sup'` over a finite sum of independent functions on a product type. -/ lemma sup'_sum_pi {ι : Type*} [Fintype ι] [DecidableEq ι] {α : ι → Type*} [∀ k, Fintype (α k)] [∀ k, Nonempty (α k)] (f : ∀ k : ι, α k → ℝ) : (Finset.univ.sup' Finset.univ_nonempty fun g : ∀ k, α k => ∑ k : ι, f k (g k)) = ∑ k : ι, Finset.univ.sup' Finset.univ_nonempty (fun a : α k => f k a) := by apply le_antisymm · refine Finset.sup'_le _ _ fun g _ => ?_ exact Finset.sum_le_sum fun k _ => Finset.le_sup' (fun a : α k => f k a) (Finset.mem_univ (g k)) · choose gstar _ hgstar using fun k : ι => Finset.exists_mem_eq_sup' (Finset.univ_nonempty (α := α k)) (fun a : α k => f k a) have hsum : ∑ k : ι, Finset.univ.sup' Finset.univ_nonempty (fun a : α k => f k a) = ∑ k : ι, f k (gstar k) := Finset.sum_congr rfl fun k _ => hgstar k rw [hsum] exact Finset.le_sup' (fun g : ∀ k, α k => ∑ k : ι, f k (g k)) (Finset.mem_univ gstar) /-- Adding a constant commutes with `Finset.sup'`. -/ lemma const_add_sup' {α : Type*} {s : Finset α} (hs : s.Nonempty) (c : ℝ) (f : α → ℝ) : c + s.sup' hs f = s.sup' hs (fun a => c + f a) := by apply le_antisymm · obtain ⟨a, ha, hmax⟩ := Finset.exists_mem_eq_sup' hs f rw [hmax] exact Finset.le_sup' (fun x => c + f x) ha · refine Finset.sup'_le _ _ fun a ha => by linarith [Finset.le_sup' f ha] omit [Fintype V] [DecidableEq X] in /-- Factoring `sup'` over assignments on a disjoint union of subsets. -/ lemma sup'_pi_disjoint_biUnion {ι : Type*} [DecidableEq ι] (I : Finset ι) (S : ι → Finset V) (hdisj : (I : Set ι).PairwiseDisjoint S) (f : ∀ k ∈ I, (S k → X) → ℝ) : (Finset.univ.sup' Finset.univ_nonempty fun y : (I.biUnion S) → X => ∑ k ∈ I.attach, f k.1 k.2 (fun u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩)) = ∑ k ∈ I.attach, Finset.univ.sup' Finset.univ_nonempty (fun yk : S k.1 → X => f k.1 k.2 yk) := by let e : ((I.biUnion S) → X) ≃ (∀ k : I, S k.1 → X) := { toFun := fun y k u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩ invFun := fun g u => let k := (Finset.mem_biUnion.mp u.2).choose let hk := (Finset.mem_biUnion.mp u.2).choose_spec g ⟨k, hk.1⟩ ⟨u.1, hk.2⟩ left_inv := fun y => by funext u; rfl right_inv := fun g => by funext ⟨k, hk⟩ ⟨u, hu⟩ dsimp only have hcast : ∀ {k₁ k₂ : ι} (h₁ : k₁ ∈ I) (h₂ : k₂ ∈ I) (hu₁ : u ∈ S k₁) (hu₂ : u ∈ S k₂), k₁ = k₂ → g ⟨k₁, h₁⟩ ⟨u, hu₁⟩ = g ⟨k₂, h₂⟩ ⟨u, hu₂⟩ := by rintro _ _ _ _ _ _ rfl; rfl have hk' := (Finset.mem_biUnion.mp (Finset.mem_biUnion.mpr ⟨k, hk, hu⟩)).choose_spec refine hcast hk'.1 hk hk'.2 hu ?_ by_contra hne exact Finset.disjoint_left.mp (hdisj hk'.1 hk hne) hk'.2 hu } have h1 : (Finset.univ.sup' Finset.univ_nonempty fun y : (I.biUnion S) → X => ∑ k ∈ I.attach, f k.1 k.2 (fun u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩)) = (Finset.univ.sup' Finset.univ_nonempty fun g : ∀ k : I, S k.1 → X => ∑ k : I, f k.1 k.2 (g k)) := by apply le_antisymm · refine Finset.sup'_le _ _ fun y _ => ?_ exact Finset.le_sup' (fun g : ∀ k : I, S k.1 → X => ∑ k : I, f k.1 k.2 (g k)) (Finset.mem_univ (e y)) · refine Finset.sup'_le _ _ fun g _ => ?_ have hrep := Finset.le_sup' (fun y : (I.biUnion S) → X => ∑ k ∈ I.attach, f k.1 k.2 (fun u => y ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩)) (Finset.mem_univ (e.symm g)) have heq : (∑ k ∈ I.attach, f k.1 k.2 (fun u => e.symm g ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩)) = ∑ k : I, f k.1 k.2 (g k) := by rw [Finset.sum_coe_sort_eq_attach] refine Finset.sum_congr rfl fun k _ => ?_ have hright := congrFun (e.right_inv g) k exact congrArg (f k.1 k.2) hright linarith rw [h1, sup'_sum_pi] exact (Finset.sum_coe_sort_eq_attach I (fun k => Finset.univ.sup' Finset.univ_nonempty (fun yk : S k.1 → X => f k.1 k.2 yk))).symm omit [DecidableEq X] in /-- **Master Subtree Max-Sum Recursion**: `M_{i → j}(x_j) = max_{x_i} (log ψ_i(x_i) + log ψ_{ij}(x_i, x_j) + ∑_{k ∈ N(i)\{j}} M_{k → i}(x_i))`. -/ theorem subtreeMaxLog_rec (hT : G.IsTree) (M : PairwiseMRF G X) {i j : V} (hij : G.Adj i j) (xj : X) : subtreeMaxLog M i j xj = maxSumStep M (subtreeMaxLog M) i j xj := by let I := (G.neighborFinset i).erase j let B := I.biUnion fun k => subtreeVerts G k i have hi_not_B : i ∉ B := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨k, hk, hik⟩ exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT ((SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk))) hik have hpart : subtreeVerts G i j = insert i B := subtreeVerts_star_partition hT hij have hdisj_I : (I : Set V).PairwiseDisjoint (fun k => subtreeVerts G k i) := by intro k₁ hk₁ k₂ hk₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G i k₁).mp (Finset.mem_of_mem_erase hk₁)) ((SimpleGraph.mem_neighborFinset G i k₂).mp (Finset.mem_of_mem_erase hk₂)) hne let eSplit : (subtreeVerts G i j → X) ≃ X × (B → X) := { toFun := fun y => (y ⟨i, self_mem_subtreeVerts hT hij⟩, fun u => y ⟨u.1, hpart ▸ Finset.mem_insert_of_mem u.2⟩) invFun := fun p u => if h : u.1 = i then p.1 else p.2 ⟨u.1, (Finset.mem_insert.mp (hpart ▸ u.2)).resolve_left h⟩ left_inv := fun y => by funext ⟨u, hu⟩; dsimp only; split_ifs with h · subst h; rfl · rfl right_inv := fun ⟨xi, yB⟩ => by ext · simp · rename_i u have hne : u.1 ≠ i := fun heq => hi_not_B (heq ▸ u.2) simp [hne] } -- Additive log factorization of `subtreeTerm M i j` have hlog_split : ∀ y : subtreeVerts G i j → X, Real.log (subtreeTerm M i j (extendAssign (subtreeVerts G i j) y xj)) = Real.log (M.ψNode i (eSplit y).1) + Real.log (M.ψEdge i j (eSplit y).1 xj) + ∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) (eSplit y).1)) := by intro y set x := extendAssign (subtreeVerts G i j) y xj have hxi : x i = (eSplit y).1 := by simp [x, eSplit, extendAssign, self_mem_subtreeVerts hT hij] have hxj : x j = xj := by have hj_not : j ∉ subtreeVerts G i j := Finset.disjoint_right.mp (subtreeVerts_disjoint i j) (self_mem_subtreeVerts hT hij.symm) simp [x, extendAssign, hj_not] have hmul : subtreeTerm M i j x = (M.ψNode i (eSplit y).1 * M.ψEdge i j (eSplit y).1 xj) * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) (eSplit y).1) := by unfold subtreeTerm have hnode : ∏ u ∈ subtreeVerts G i j, M.ψNode u (x u) = M.ψNode i (x i) * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u) := by rw [hpart, Finset.prod_insert hi_not_B, Finset.prod_biUnion hdisj_I] have hedge : ∏ e ∈ inducedEdges G (subtreeVerts G i j), M.edgeWeight x e = ∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e) := by rw [inducedEdges_subtree_star hT hij, Finset.prod_biUnion ((branch_edges_pairwiseDisjoint hT i).subset (Finset.erase_subset j _))] refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hik), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm k i] have hprod_eq : (∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) = ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) (eSplit y).1) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach I] refine Finset.prod_congr rfl fun ⟨k, hk⟩ _ => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hcongr : subtreeTerm M k i x = subtreeTerm M k i (extendAssign (subtreeVerts G k i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k, hk, u.2⟩⟩) (eSplit y).1) := by refine subtreeTerm_congr M k i (fun u hu => ?_) ?_ (self_mem_subtreeVerts hT hik.symm) · have hu_ij : u ∈ subtreeVerts G i j := hpart ▸ Finset.mem_insert_of_mem (Finset.mem_biUnion.mpr ⟨k, hk, hu⟩) simp [x, eSplit, extendAssign, hu, hu_ij] · have hi_not : i ∉ subtreeVerts G k i := Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) simp [x, eSplit, extendAssign, hi_not, self_mem_subtreeVerts hT hij] rw [← hcongr]; unfold subtreeTerm; ring rw [hnode, hedge] calc (M.ψNode i (x i) * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) * M.ψEdge i j (x i) (x j) = (M.ψNode i (x i) * M.ψEdge i j (x i) (x j)) * ((∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e))) := by ring _ = (M.ψNode i (eSplit y).1 * M.ψEdge i j (eSplit y).1 xj) * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) (eSplit y).1) := by rw [hprod_eq, hxi, hxj] rw [hmul, Real.log_mul (ne_of_gt (mul_pos (M.ψNode_pos _ _) (M.ψEdge_pos _ _ _ _))) (ne_of_gt (Finset.prod_pos fun k _ => subtreeTerm_pos M k.1 i _)), Real.log_mul (ne_of_gt (M.ψNode_pos _ _)) (ne_of_gt (M.ψEdge_pos _ _ _ _)), Real.log_prod (fun k _ => ne_of_gt (subtreeTerm_pos M k.1 i _))] -- Now pull sup' over `eSplit` -> `X` then `B → X` unfold subtreeMaxLog maxSumStep apply le_antisymm · refine Finset.sup'_le _ _ fun y _ => ?_ rw [hlog_split y] refine le_trans ?_ (Finset.le_sup' (fun xi : X => Real.log (M.ψNode i xi) + Real.log (M.ψEdge i j xi xj) + ∑ k ∈ I, subtreeMaxLog M k i xi) (Finset.mem_univ (eSplit y).1)) have hsum_le : (∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => (eSplit y).2 ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) (eSplit y).1))) ≤ ∑ k ∈ I, subtreeMaxLog M k i (eSplit y).1 := by rw [← Finset.sum_attach I (fun k => subtreeMaxLog M k i (eSplit y).1)] refine Finset.sum_le_sum fun k _ => ?_ exact Finset.le_sup' (fun yk : subtreeVerts G k.1 i → X => Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) yk (eSplit y).1))) (Finset.mem_univ _) linarith · refine Finset.sup'_le _ _ fun xi _ => ?_ have hatt : ∑ k ∈ (G.neighborFinset i).erase j, Finset.univ.sup' Finset.univ_nonempty (fun yk : subtreeVerts G k i → X => Real.log (subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk xi))) = ∑ k ∈ I.attach, Finset.univ.sup' Finset.univ_nonempty (fun yk : subtreeVerts G k.1 i → X => Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) yk xi))) := (Finset.sum_attach I (fun k => subtreeMaxLog M k i xi)).symm rw [hatt] have hsup_eq := sup'_pi_disjoint_biUnion I (fun k => subtreeVerts G k i) hdisj_I (fun k _ yk => Real.log (subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk xi))) rw [← hsup_eq, const_add_sup'] refine Finset.sup'_le _ _ fun yB _ => ?_ let y := eSplit.symm (xi, yB) have hy : eSplit y = (xi, yB) := eSplit.apply_symm_apply (xi, yB) have hval := hlog_split y rw [hy] at hval rw [← hval] exact Finset.le_sup' (fun y' : subtreeVerts G i j → X => Real.log (subtreeTerm M i j (extendAssign (subtreeVerts G i j) y' xj))) (Finset.mem_univ y) omit [DecidableEq X] in /-- **Exact Convergence of Max-Sum BP in `subtreeHeight + 1` Steps**: For ANY initial messages `μ0`, after `t ≥ subtreeHeight i j + 1` steps, `maxSumIter M μ0 t i j = subtreeMaxLog M i j`. -/ theorem maxSumIter_eq_subtreeMaxLog (hT : G.IsTree) (M : PairwiseMRF G X) (μ0 : V → V → X → ℝ) : ∀ h : ℕ, ∀ i j : V, G.Adj i j → subtreeHeight G i j ≤ h → ∀ t : ℕ, h + 1 ≤ t → ∀ xj : X, maxSumIter M μ0 t i j xj = subtreeMaxLog M i j xj := by intro h induction h with | zero => intro i j hij hh t ht xj cases t with | zero => omega | succ t' => rw [subtreeMaxLog_rec hT M hij xj] simp only [maxSumIter, maxSumStep] have hempty : (G.neighborFinset i).erase j = ∅ := by rw [Finset.eq_empty_iff_forall_notMem] intro k hk have hlt := subtreeHeight_add_one_le hT hij hk omega simp [hempty] | succ h ih => intro i j hij hh t ht xj cases t with | zero => omega | succ t' => rw [subtreeMaxLog_rec hT M hij xj] simp only [maxSumIter, maxSumStep] congr 1; funext xi; congr 1 refine Finset.sum_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp (Finset.mem_of_mem_erase hk) have hhk : subtreeHeight G k i ≤ h := by have := subtreeHeight_add_one_le hT hij hk; omega exact ih k i hik.symm hhk t' (by omega) xi /-- Exact single-node max-marginal equals `maxSumBelief M (subtreeMaxLog M) i a`. -/ theorem maxMarginal_eq_subtreeMaxLog_sum (hT : G.IsTree) (M : PairwiseMRF G X) (i : V) (a : X) : M.maxMarginal i a = maxSumBelief M (subtreeMaxLog M) i a := by let I := G.neighborFinset i let B := I.biUnion fun k => subtreeVerts G k i have hi_not_B : i ∉ B := by intro hm rcases Finset.mem_biUnion.mp hm with ⟨k, hk, hik⟩ exact Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT ((SimpleGraph.mem_neighborFinset G i k).mp hk)) hik have hpart : (Finset.univ : Finset V) = insert i B := univ_star_partition hT i have hdisj_I : (I : Set V).PairwiseDisjoint (fun k => subtreeVerts G k i) := by intro k₁ hk₁ k₂ hk₂ hne exact subtreeVerts_branches_disjoint hT ((SimpleGraph.mem_neighborFinset G i k₁).mp hk₁) ((SimpleGraph.mem_neighborFinset G i k₂).mp hk₂) hne have hlog_x : ∀ x : V → X, x i = a → Real.log (M.unnormWeight x) = Real.log (M.ψNode i a) + ∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a)) := by intro x hx have hmul : M.unnormWeight x = M.ψNode i a * ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a) := by unfold PairwiseMRF.unnormWeight have hnode : ∏ u : V, M.ψNode u (x u) = M.ψNode i a * ∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u) := by rw [hpart, Finset.prod_insert hi_not_B, Finset.prod_biUnion hdisj_I, hx] have hedge : ∏ e ∈ G.edgeFinset, M.edgeWeight x e = ∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e) := by rw [edgeFinset_star hT i, Finset.prod_biUnion (branch_edges_pairwiseDisjoint hT i)] refine Finset.prod_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk rw [Finset.prod_insert (edge_not_mem_inducedEdges_subtree hT hik), PairwiseMRF.edgeWeight_mk, M.ψEdge_symm k i] have hprod_eq : (∏ k ∈ I, ∏ u ∈ subtreeVerts G k i, M.ψNode u (x u)) * (∏ k ∈ I, (M.ψEdge k i (x k) (x i) * ∏ e ∈ inducedEdges G (subtreeVerts G k i), M.edgeWeight x e)) = ∏ k ∈ I.attach, subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a) := by rw [← Finset.prod_mul_distrib, ← Finset.prod_attach I] refine Finset.prod_congr rfl fun ⟨k, hk⟩ _ => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk have hcongr : subtreeTerm M k i x = subtreeTerm M k i (extendAssign (subtreeVerts G k i) (fun u => x u.1) a) := by refine subtreeTerm_congr M k i (fun u hu => by simp [extendAssign, hu]) ?_ (self_mem_subtreeVerts hT hik.symm) have hi_not : i ∉ subtreeVerts G k i := Finset.disjoint_left.mp (subtreeVerts_disjoint i k) (self_mem_subtreeVerts hT hik) simp [extendAssign, hi_not, hx] rw [← hcongr]; unfold subtreeTerm; ring rw [hnode, hedge, mul_assoc, hprod_eq] rw [hmul, Real.log_mul (ne_of_gt (M.ψNode_pos i a)) (ne_of_gt (Finset.prod_pos fun k _ => subtreeTerm_pos M k.1 i _)), Real.log_prod (fun k _ => ne_of_gt (subtreeTerm_pos M k.1 i _))] unfold PairwiseMRF.maxMarginal maxSumBelief apply le_antisymm · refine Finset.sup'_le _ _ fun x hx => ?_ rw [hlog_x x (Finset.mem_filter.mp hx).2] have hsum_le : (∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a))) ≤ ∑ k ∈ I, subtreeMaxLog M k i a := by rw [← Finset.sum_attach I (fun k => subtreeMaxLog M k i a)] refine Finset.sum_le_sum fun k _ => ?_ exact Finset.le_sup' (fun yk : subtreeVerts G k.1 i → X => Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) yk a))) (Finset.mem_univ _) linarith · have hatt : ∑ k ∈ G.neighborFinset i, subtreeMaxLog M k i a = ∑ k ∈ I.attach, Finset.univ.sup' Finset.univ_nonempty (fun yk : subtreeVerts G k.1 i → X => Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) yk a))) := (Finset.sum_attach I (fun k => subtreeMaxLog M k i a)).symm rw [hatt] have hsup_eq := sup'_pi_disjoint_biUnion I (fun k => subtreeVerts G k i) hdisj_I (fun k _ yk => Real.log (subtreeTerm M k i (extendAssign (subtreeVerts G k i) yk a))) rw [← hsup_eq, const_add_sup'] refine Finset.sup'_le _ _ fun yB _ => ?_ let x : V → X := fun v => if hv : v ∈ B then yB ⟨v, hv⟩ else a have hxi : x i = a := by simp [x, hi_not_B] have hval := hlog_x x hxi have hagree : (∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => x u.1) a))) = ∑ k ∈ I.attach, Real.log (subtreeTerm M k.1 i (extendAssign (subtreeVerts G k.1 i) (fun u => yB ⟨u.1, Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩⟩) a)) := by refine Finset.sum_congr rfl fun k _ => ?_ congr 1; congr 1; congr 1 funext u have huB : u.1 ∈ B := Finset.mem_biUnion.mpr ⟨k.1, k.2, u.2⟩ simp [x, huB] rw [← hagree, ← hval] exact Finset.le_sup' (fun z : V → X => Real.log (M.unnormWeight z)) (Finset.mem_filter.mpr ⟨Finset.mem_univ x, hxi⟩) /-- **Headline Theorem 1E (Exactness of Tree Max-Sum BP: Max-Marginals)**: For ANY finite tree `T`, ANY initial messages `μ0`, and ANY `t ≥ diam(T)`, synchronous Max-Sum BP recovers the **exact** single-node max-marginal `max_{x : x_i = a} log p̃(x)`. -/ theorem tree_maxSum_maxMarginal_exact (hT : G.IsTree) (M : PairwiseMRF G X) (μ0 : V → V → X → ℝ) {t : ℕ} (ht : treeDiam G ≤ t) (i : V) (a : X) : maxSumBelief M (maxSumIter M μ0 t) i a = M.maxMarginal i a := by rw [maxMarginal_eq_subtreeMaxLog_sum hT M i a] unfold maxSumBelief congr 1 refine Finset.sum_congr rfl fun k hk => ?_ have hik : G.Adj i k := (SimpleGraph.mem_neighborFinset G i k).mp hk exact maxSumIter_eq_subtreeMaxLog hT M μ0 (subtreeHeight G k i) k i hik.symm le_rfl t (le_trans (subtreeHeight_add_one_le_treeDiam hT hik.symm) ht) a /-- **Headline Theorem 1F (Exactness of Tree Max-Sum BP: Global MAP Energy)**: At any vertex `i` and any `t ≥ diam(T)`, `max_a maxSumBelief_i(a) = max_x log p̃(x)`. -/ theorem tree_maxSum_map_energy_exact (hT : G.IsTree) (M : PairwiseMRF G X) (μ0 : V → V → X → ℝ) {t : ℕ} (ht : treeDiam G ≤ t) (i : V) : Finset.univ.sup' Finset.univ_nonempty (fun a : X => maxSumBelief M (maxSumIter M μ0 t) i a) = M.maxLogWeight := by have heq : (fun a : X => maxSumBelief M (maxSumIter M μ0 t) i a) = fun a => M.maxMarginal i a := by funext a; exact tree_maxSum_maxMarginal_exact hT M μ0 ht i a rw [heq, M.sup_maxMarginal_eq_maxLogWeight i] /-- **Headline Theorem 1G (Exactness of Multiplicative Tree Max-Product BP)**: For ANY strictly positive initial messages `ν0` and ANY `t ≥ diam(T)`, synchronous Max-Product BP recovers the **exact** global MAP weight `max_x p̃(x)`. -/ theorem tree_maxProd_map_exact (hT : G.IsTree) (M : PairwiseMRF G X) {ν0 : V → V → X → ℝ} (hν0 : ∀ u v a, G.Adj u v → 0 < ν0 u v a) {t : ℕ} (ht : treeDiam G ≤ t) (i : V) : Finset.univ.sup' Finset.univ_nonempty (fun a : X => maxProdBelief M (maxProdIter M ν0 t) i a) = M.maxWeight := by have hlog_bel : ∀ a : X, Real.log (maxProdBelief M (maxProdIter M ν0 t) i a) = maxSumBelief M (maxSumIter M (fun u v b => Real.log (ν0 u v b)) t) i a := by intro a unfold maxProdBelief maxSumBelief have hprod_pos : 0 < ∏ k ∈ G.neighborFinset i, maxProdIter M ν0 t k i a := Finset.prod_pos fun k hk => maxProdIter_pos M hν0 t k i a ((SimpleGraph.mem_neighborFinset G i k).mp hk).symm rw [Real.log_mul (ne_of_gt (M.ψNode_pos i a)) (ne_of_gt hprod_pos), Real.log_prod (fun k hk => ne_of_gt (maxProdIter_pos M hν0 t k i a ((SimpleGraph.mem_neighborFinset G i k).mp hk).symm))] congr 1 refine Finset.sum_congr rfl fun k hk => ?_ exact log_maxProdIter_eq_maxSumIter M hν0 t k i a ((SimpleGraph.mem_neighborFinset G i k).mp hk).symm have hpos_bel : ∀ a : X, 0 < maxProdBelief M (maxProdIter M ν0 t) i a := fun a => mul_pos (M.ψNode_pos i a) (Finset.prod_pos fun k hk => maxProdIter_pos M hν0 t k i a ((SimpleGraph.mem_neighborFinset G i k).mp hk).symm) have hsup_pos : 0 < Finset.univ.sup' Finset.univ_nonempty (fun a : X => maxProdBelief M (maxProdIter M ν0 t) i a) := by obtain ⟨a0⟩ := (inferInstance : Nonempty X) exact lt_of_lt_of_le (hpos_bel a0) (Finset.le_sup' (fun a : X => maxProdBelief M (maxProdIter M ν0 t) i a) (Finset.mem_univ a0)) have hmaxW_pos : 0 < M.maxWeight := by obtain ⟨x0⟩ := (inferInstance : Nonempty (V → X)) exact lt_of_lt_of_le (M.unnormWeight_pos x0) (Finset.le_sup' (fun x : V → X => M.unnormWeight x) (Finset.mem_univ x0)) apply Real.log_injOn_pos hsup_pos hmaxW_pos rw [← M.maxLogWeight_eq_log_maxWeight, ← tree_maxSum_map_energy_exact hT M (fun u v b => Real.log (ν0 u v b)) ht i] apply le_antisymm · obtain ⟨a0, _, ha0⟩ := Finset.exists_mem_eq_sup' Finset.univ_nonempty (fun a : X => maxProdBelief M (maxProdIter M ν0 t) i a) rw [ha0, hlog_bel a0] exact Finset.le_sup' (fun a : X => maxSumBelief M (maxSumIter M (fun u v b => Real.log (ν0 u v b)) t) i a) (Finset.mem_univ a0) · refine Finset.sup'_le _ _ fun a _ => ?_ rw [← hlog_bel a] exact Real.log_le_log (hpos_bel a) (Finset.le_sup' (fun b : X => maxProdBelief M (maxProdIter M ν0 t) i b) (Finset.mem_univ a)) end ExactTreeMaxProduct /-! ## Section 7: Exact Single-Pass Asynchronous Schedule (`2|E|` Updates) -/ section AsyncSchedule variable {V X : Type*} {G : SimpleGraph V} variable [Fintype V] [DecidableEq V] [DecidableRel G.Adj] variable [Fintype X] [DecidableEq X] [Nonempty X] /-- Single directed-edge asynchronous update of message field `m` along edge `e = (i, j)`. -/ noncomputable def asyncUpdateEdge (M : PairwiseMRF G X) (m : V → V → X → ℝ) (e : V × V) : V → V → X → ℝ := fun u v a => if u = e.1 ∧ v = e.2 then sumProdUnnormStep M m u v a else m u v a /-- Apply a sequential schedule (list of directed edges) asynchronously from `m0`. -/ noncomputable def asyncRun (M : PairwiseMRF G X) (m0 : V → V → X → ℝ) : List (V × V) → V → V → X → ℝ | [] => m0 | e :: es => asyncRun M (asyncUpdateEdge M m0 e) es /-- A schedule `sched : List (V × V)` is **topologically height-ordered** for a tree `T` if every directed edge `(i, j)` appears strictly after all its incoming tree edges `(k, i)` (`k ∈ N(i) \ {j}`), and every directed edge appears at most once (so length ≤ `2|E|`). -/ def IsValidTreeSchedule (G : SimpleGraph V) (sched : List (V × V)) : Prop := sched.Nodup ∧ (∀ e ∈ sched, G.Adj e.1 e.2) ∧ (∀ i j, G.Adj i j → (i, j) ∈ sched) ∧ (∀ p q : ℕ, ∀ (hp : p < sched.length) (hq : q < sched.length), let e₁ := sched[p] let e₂ := sched[q] (e₁.2 = e₂.1 ∧ e₁.1 ≠ e₂.2) → p < q) omit [DecidableEq X] [Nonempty X] in /-- **Headline Theorem 1H (Exact Asynchronous Tree BP in `2|E|` Updates)**: On any finite tree `T`, ANY valid topological schedule (e.g., sorting all `2|E|` directed edges by non-decreasing `subtreeHeight G i j`, or equivalently a two-pass bottom-up + top-down sweep) updates each directed edge **exactly once** (`sched.length = 2 * |E|`) and computes the **exact** subtree partition functions `subtreeZ M` (and hence exact node & edge marginals) from **any** initial messages `m0`. -/ theorem asyncTreeBP_exact (hT : G.IsTree) (M : PairwiseMRF G X) (m0 : V → V → X → ℝ) (sched : List (V × V)) (hsched : IsValidTreeSchedule G sched) : sched.length = 2 * G.edgeFinset.card ∧ (∀ i j, G.Adj i j → ∀ xj, asyncRun M m0 sched i j xj = subtreeZ M i j xj) := by rcases hsched with ⟨hnodup, hmem_adj, hall_adj, horder⟩ -- Part 1: Length equals `2 * |E|` have hlen : sched.length = 2 * G.edgeFinset.card := by have hcard_toFinset : sched.toFinset.card = sched.length := List.toFinset_card_of_nodup hnodup rw [← hcard_toFinset] have hset_eq : sched.toFinset = Finset.univ.biUnion (fun i : V => (G.neighborFinset i).map ⟨fun j => (i, j), fun _ _ h => (Prod.mk.inj h).2⟩) := by ext ⟨i, j⟩ simp only [List.mem_toFinset, Finset.mem_biUnion, Finset.mem_univ, Finset.mem_map, SimpleGraph.mem_neighborFinset, true_and] constructor · intro hm; exact ⟨i, j, hmem_adj (i, j) hm, rfl⟩ · rintro ⟨i', j', hadj, heq⟩ have h1 : i' = i := (Prod.mk.inj heq).1 have h2 : j' = j := (Prod.mk.inj heq).2 subst h1; subst h2; exact hall_adj i' j' hadj rw [hset_eq, Finset.card_biUnion] · simp_rw [Finset.card_map, G.card_neighborFinset_eq_degree] exact G.sum_degrees_eq_twice_card_edges · intro i₁ _ i₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro e he1 he2 rcases Finset.mem_map.mp he1 with ⟨j₁, _, rfl⟩ rcases Finset.mem_map.mp he2 with ⟨j₂, _, heq⟩ exact hne (Prod.mk.inj heq).1.symm refine ⟨hlen, ?_⟩ -- Part 2: Prove by induction on prefix length `n ≤ sched.length` that every edge at index `< n` -- has already attained `subtreeZ` and is never overwritten! have hprefix : ∀ (L : List (V × V)), L.Nodup → (∀ e ∈ L, G.Adj e.1 e.2) → (∀ (p q : ℕ) (hp : p < L.length) (hq : q < L.length), L[p].2 = L[q].1 ∧ L[p].1 ≠ L[q].2 → p < q) → (∀ (i j k : V), (i, j) ∈ L → k ∈ (G.neighborFinset i).erase j → (k, i) ∈ L) → ∀ m : V → V → X → ℝ, ∀ e ∈ L, ∀ a, asyncRun M m L e.1 e.2 a = subtreeZ M e.1 e.2 a := by -- Stronger invariant carrying already-exact edges `S`! suffices hgen : ∀ (L : List (V × V)) (S : Finset (V × V)) (m : V → V → X → ℝ), L.Nodup → (∀ e ∈ L, e ∉ S) → (∀ e ∈ L, G.Adj e.1 e.2) → (∀ e ∈ S, ∀ a, m e.1 e.2 a = subtreeZ M e.1 e.2 a) → (∀ (p q : ℕ) (hp : p < L.length) (hq : q < L.length), L[p].2 = L[q].1 ∧ L[p].1 ≠ L[q].2 → p < q) → (∀ (i j k : V), (i, j) ∈ L → k ∈ (G.neighborFinset i).erase j → (k, i) ∈ S ∨ (k, i) ∈ L) → (∀ e ∈ S, ∀ a, asyncRun M m L e.1 e.2 a = subtreeZ M e.1 e.2 a) ∧ (∀ e ∈ L, ∀ a, asyncRun M m L e.1 e.2 a = subtreeZ M e.1 e.2 a) by intro L hnd hadj hord hclosed m e he a exact (hgen L ∅ m hnd (by simp) hadj (by simp) hord (fun i j k hij hk => Or.inr (hclosed i j k hij hk))).2 e he a intro L induction L with | nil => intro S m _ _ _ hS _ _ exact ⟨hS, by simp⟩ | cons e₀ rest ih => intro S m hnd hdisj hadj hS hord hclosed rcases List.nodup_cons.mp hnd with ⟨he0_not_rest, hnd_rest⟩ have he0_adj : G.Adj e₀.1 e₀.2 := hadj e₀ (by simp) -- Every incoming neighbor `(k, e₀.1)` with `k ≠ e₀.2` MUST already be in `S` (cannot be in `rest` or equal to `e₀`)! have hin_S : ∀ k ∈ (G.neighborFinset e₀.1).erase e₀.2, (k, e₀.1) ∈ S := by intro k hk have hmem := hclosed e₀.1 e₀.2 k (by simp) hk rcases hmem with h | h · exact h · exfalso rcases List.mem_cons.mp h with heq | hrest · have h1 : k = e₀.1 := (Prod.mk.inj heq).1 exact (G.loopless.irrefl e₀.1) (h1 ▸ (SimpleGraph.mem_neighborFinset G e₀.1 k).mp (Finset.mem_of_mem_erase hk)) · obtain ⟨q, hq, hq_eq⟩ := List.getElem_of_mem hrest have hlt := hord (q + 1) 0 (Nat.succ_lt_succ hq) (Nat.zero_lt_succ _) ⟨by simp [hq_eq], by simp [hq_eq, Finset.ne_of_mem_erase hk]⟩ omega -- Therefore `asyncUpdateEdge M m e₀` sets `e₀` to exact `subtreeZ`! have hm'_e0 : ∀ a, asyncUpdateEdge M m e₀ e₀.1 e₀.2 a = subtreeZ M e₀.1 e₀.2 a := by intro a simp only [asyncUpdateEdge, true_and, ↓reduceIte] rw [subtreeZ_rec hT M he0_adj a] unfold sumProdUnnormStep refine Finset.sum_congr rfl fun xi _ => ?_ congr 1 refine Finset.prod_congr rfl fun k hk => hS (k, e₀.1) (hin_S k hk) xi have hS' : ∀ e ∈ insert e₀ S, ∀ a, asyncUpdateEdge M m e₀ e.1 e.2 a = subtreeZ M e.1 e.2 a := by intro e he a rcases Finset.mem_insert.mp he with rfl | heS · exact hm'_e0 a · have hne : ¬ (e.1 = e₀.1 ∧ e.2 = e₀.2) := by rintro ⟨h1, h2⟩ have heq : e = e₀ := Prod.ext h1 h2 exact hdisj e₀ (by simp) (heq ▸ heS) simp only [asyncUpdateEdge, hne, ↓reduceIte] exact hS e heS a have hrec := ih (insert e₀ S) (asyncUpdateEdge M m e₀) hnd_rest (fun e he hm => by rcases Finset.mem_insert.mp hm with rfl | hmS · exact he0_not_rest he · exact hdisj e (List.mem_cons_of_mem e₀ he) hmS) (fun e he => hadj e (List.mem_cons_of_mem e₀ he)) hS' (fun p q hp hq hcond => by have := hord (p + 1) (q + 1) (Nat.succ_lt_succ hp) (Nat.succ_lt_succ hq) hcond omega) (fun i j k hij hk => by rcases hclosed i j k (List.mem_cons_of_mem e₀ hij) hk with h | h · exact Or.inl (Finset.mem_insert_of_mem h) · rcases List.mem_cons.mp h with heq | hrest · exact Or.inl (heq ▸ Finset.mem_insert_self e₀ S) · exact Or.inr hrest) refine ⟨fun e he a => hrec.1 e (Finset.mem_insert_of_mem he) a, fun e he a => ?_⟩ rcases List.mem_cons.mp he with he_eq | hrest · subst he_eq; exact hrec.1 e (Finset.mem_insert_self e S) a · exact hrec.2 e hrest a intro i j hij xj exact hprefix sched hnodup hmem_adj horder (fun u v k huv hk => hall_adj k u ((SimpleGraph.mem_neighborFinset G u k).mp (Finset.mem_of_mem_erase hk)).symm) m0 (i, j) (hall_adj i j hij) xj /-- Constructive existence of a valid `2|E|`-step asynchronous schedule on any finite tree: sort all directed edges by `subtreeHeight G e.1 e.2`. -/ theorem exists_validTreeSchedule (hT : G.IsTree) : ∃ sched : List (V × V), IsValidTreeSchedule G sched := by let r : V × V → V × V → Prop := fun e₁ e₂ => subtreeHeight G e₁.1 e₁.2 ≤ subtreeHeight G e₂.1 e₂.2 have : DecidableRel r := fun e₁ e₂ => inferInstanceAs (Decidable (_ ≤ _)) have : Std.Total r := ⟨fun a b => le_total _ _⟩ have : IsTrans (V × V) r := ⟨fun _ _ _ hab hbc => le_trans hab hbc⟩ let dirEdges : Finset (V × V) := Finset.univ.filter fun e : V × V => G.Adj e.1 e.2 let sched : List (V × V) := dirEdges.toList.mergeSort (fun e₁ e₂ => decide (r e₁ e₂)) refine ⟨sched, ?_, ?_, ?_, ?_⟩ · exact (List.mergeSort_perm _ _).nodup_iff.mpr dirEdges.nodup_toList · intro e he have hm : e ∈ dirEdges.toList := (List.mergeSort_perm _ _).subset he exact (Finset.mem_filter.mp (Finset.mem_toList.mp hm)).2 · intro i j hij have hm : (i, j) ∈ dirEdges.toList := Finset.mem_toList.mpr (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hij⟩) exact (List.mergeSort_perm _ _).symm.subset hm · intro p q hp hq dsimp only rintro ⟨hmid, hne⟩ have hsorted : List.Pairwise r sched := List.pairwise_mergeSort' r dirEdges.toList have he1_adj : G.Adj sched[p].1 sched[p].2 := by have hm : sched[p] ∈ dirEdges.toList := (List.mergeSort_perm _ _).subset (List.getElem_mem hp) exact (Finset.mem_filter.mp (Finset.mem_toList.mp hm)).2 have he2_adj : G.Adj sched[q].1 sched[q].2 := by have hm : sched[q] ∈ dirEdges.toList := (List.mergeSort_perm _ _).subset (List.getElem_mem hq) exact (Finset.mem_filter.mp (Finset.mem_toList.mp hm)).2 have hmem_erase : sched[p].1 ∈ (G.neighborFinset sched[q].1).erase sched[q].2 := by refine Finset.mem_erase.mpr ⟨hne, (SimpleGraph.mem_neighborFinset _ _ _).mpr ?_⟩ rw [← hmid]; exact he1_adj.symm have hlt_h : subtreeHeight G sched[p].1 sched[p].2 < subtreeHeight G sched[q].1 sched[q].2 := by rw [hmid] have := subtreeHeight_add_one_le hT he2_adj hmem_erase omega by_contra hge have hq_le_p : q ≤ p := by omega rcases eq_or_lt_of_le hq_le_p with rfl | hlt · omega · have hle : r sched[q] sched[p] := (List.pairwise_iff_getElem.mp hsorted) q p hq hp hlt dsimp only [r] at hle omega end AsyncSchedule /-! ## Axiom Verification Audit -/ #print axioms tree_dist_adj_cases #print axioms subtreeZ_rec #print axioms sumProdUnnormIter_eq_subtreeZ #print axioms unnormNodeMarginal_eq_subtreeZ_prod #print axioms unnormEdgeMarginal_eq_subtreeZ_prod #print axioms tree_sumProd_nodeMarginal_exact #print axioms tree_sumProd_edgeMarginal_exact #print axioms tree_gibbs_factorization #print axioms tree_bp_bethe_logZ_exact #print axioms subtreeMaxLog_rec #print axioms maxSumIter_eq_subtreeMaxLog #print axioms maxMarginal_eq_subtreeMaxLog_sum #print axioms tree_maxSum_maxMarginal_exact #print axioms tree_maxSum_map_energy_exact #print axioms tree_maxProd_map_exact #print axioms asyncTreeBP_exact #print axioms exists_validTreeSchedule end TreeBeliefPropagation