math / data /lean /OAI /Probability /Ising /PinnedMarginal.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
12.4 kB
import OAI.Probability.Ising.SpinCorrelation
import OAI.Probability.Ising.Marginals
namespace OAI
open scoped BigOperators Classical
namespace BufferedIsing
/-- The actual contracted marginal retains arbitrary fields and all effects
of the common pins. It is distinct from the zero-field comparison weight. -/
noncomputable def contractedPinnedWeight {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (I J F : Finset (Fin n)) (Ο„ : Spins n)
(Οƒ : KeptVertex F β†’ Bool) : ℝ :=
if Respects (comparisonSetoid G I J F βˆ…) Οƒ then weight G h (pinExtension F Ο„ Οƒ) else 0
lemma respects_terminals_iff {n m : β„•} (G : FiniteGraph n m)
(I J F : Finset (Fin n)) (Οƒ : KeptVertex F β†’ Bool) :
Respects (comparisonSetoid G I J F βˆ…) Οƒ ↔
(βˆ€ x y, x.val ∈ I β†’ y.val ∈ I β†’ Οƒ x = Οƒ y) ∧
(βˆ€ x y, x.val ∈ J β†’ y.val ∈ J β†’ Οƒ x = Οƒ y) := by
constructor
Β· intro H
exact ⟨fun x y hx hy => H x y (Relation.EqvGen.rel _ _ (Or.inl ⟨hx, hy⟩)),
fun x y hx hy => H x y (Relation.EqvGen.rel _ _ (Or.inr (Or.inl ⟨hx, hy⟩)))⟩
· rintro ⟨hI, hJ⟩ x y hxy
induction hxy with
| rel x y h =>
rcases h with h | h | ⟨e, he, _⟩
Β· exact hI x y h.1 h.2
Β· exact hJ x y h.1 h.2
Β· exact (Finset.notMem_empty e he).elim
| refl => rfl
| symm _ _ _ ih => exact ih.symm
| trans _ _ _ _ _ ih ih' => exact ih.trans ih'
lemma contracted_prescription_iff {n m : β„•} (G : FiniteGraph n m)
(I J F : Finset (Fin n)) (hIF : Disjoint I F) (hJF : Disjoint J F)
(i j : KeptVertex F) (hi : i.val ∈ I) (hj : j.val ∈ J)
(Ο„ : Spins n) (Οƒ : KeptVertex F β†’ Bool) (s t : Bool) :
(Οƒ i = s ∧ Οƒ j = t ∧ Respects (comparisonSetoid G I J F βˆ…) Οƒ) ↔
AgreesOn I (pinExtension F Ο„ Οƒ) (fun _ => s) ∧
AgreesOn J (pinExtension F Ο„ Οƒ) (fun _ => t) := by
constructor
· rintro ⟨his, hjt, hresp⟩
obtain ⟨hI, hJ⟩ := (respects_terminals_iff G I J F Οƒ).mp hresp
constructor
Β· intro v hv
have hvF : v βˆ‰ F := fun hvF => Finset.disjoint_left.mp hIF hv hvF
exact (pinExtension_apply F Ο„ Οƒ ⟨v, hvF⟩).trans ((hI _ i hv hi).trans his)
Β· intro v hv
have hvF : v βˆ‰ F := fun hvF => Finset.disjoint_left.mp hJF hv hvF
exact (pinExtension_apply F Ο„ Οƒ ⟨v, hvF⟩).trans ((hJ _ j hv hj).trans hjt)
· rintro ⟨hI, hJ⟩
have hI' : βˆ€ v : KeptVertex F, v.val ∈ I β†’ Οƒ v = s := by
intro v hv
simpa only [pinExtension_apply] using hI v.val hv
have hJ' : βˆ€ v : KeptVertex F, v.val ∈ J β†’ Οƒ v = t := by
intro v hv
simpa only [pinExtension_apply] using hJ v.val hv
refine ⟨hI' i hi, hJ' j hj, (respects_terminals_iff G I J F Οƒ).mpr ?_⟩
exact ⟨fun x y hx hy => (hI' x hx).trans (hI' y hy).symm,
fun x y hx hy => (hJ' x hx).trans (hJ' y hy).symm⟩
/-- Identifying each terminal set separately is *exactly* restriction to its
constant configurations; no new model comparison enters this identity. -/
lemma pairTable_contractedPinned {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (I J F : Finset (Fin n))
(hIF : Disjoint I F) (hJF : Disjoint J F)
(i j : KeptVertex F) (hi : i.val ∈ I) (hj : j.val ∈ J)
(Ο„ : Spins n) (s t : Bool) :
pairTable (contractedPinnedWeight G h I J F Ο„) i j s t =
jointWeight G h I J F Ο„ (fun _ => s) (fun _ => t) := by
classical
have hdel := constrainedSum_delete_variables G h F Ο„
(fun ρ => AgreesOn I ρ (fun _ => s) ∧ AgreesOn J ρ (fun _ => t))
have hswap : jointWeight G h I J F Ο„ (fun _ => s) (fun _ => t) =
constrainedSum G h (fun ρ => AgreesOn F ρ Ο„ ∧
(AgreesOn I ρ (fun _ => s) ∧ AgreesOn J ρ (fun _ => t))) := by
unfold jointWeight constrainedSum
apply Finset.sum_congr rfl
intro ρ _
congr 1
exact propext and_rotate.symm
rw [hswap, hdel]
unfold pairTable
apply Finset.sum_congr rfl
intro Οƒ _
unfold contractedPinnedWeight
have H := contracted_prescription_iff G I J F hIF hJF i j hi hj Ο„ Οƒ s t
by_cases hc : Οƒ i = s ∧ Οƒ j = t
Β· rw [ite_eq_left hc]
by_cases hr : Respects (comparisonSetoid G I J F βˆ…) Οƒ
· rw [ite_eq_left hr, ite_eq_left (H.mp ⟨hc.1, hc.2, hr⟩)]
Β· rw [ite_eq_right hr, ite_eq_right (fun hp => hr (H.mpr hp).2.2)]
· rw [ite_eq_right hc, ite_eq_right (fun hp => hc ⟨(H.mpr hp).1, (H.mpr hp).2.1⟩)]
lemma extremeLogOdds_eq_effective {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (I J F : Finset (Fin n))
(hIF : Disjoint I F) (hJF : Disjoint J F)
(i j : KeptVertex F) (hi : i.val ∈ I) (hj : j.val ∈ J) (Ο„ : Spins n) :
extremeLogOdds G h I J F Ο„ =
4 * tableCoupling (pairTable (contractedPinnedWeight G h I J F Ο„) i j) := by
simp only [tableCoupling, pairTable_contractedPinned G h I J F hIF hJF i j hi hj]
unfold extremeLogOdds
change _ = 4 * ((Real.log (jointWeight G h I J F Ο„ ⊀ ⊀) +
Real.log (jointWeight G h I J F Ο„ βŠ₯ βŠ₯) - Real.log (jointWeight G h I J F Ο„ ⊀ βŠ₯) -
Real.log (jointWeight G h I J F Ο„ βŠ₯ ⊀)) / 4)
ring
lemma sum_pin_incidence {n m : β„•} (l r : Fin m β†’ Fin n) (F : Finset (Fin n)) (f : Fin m β†’ ℝ) (Οƒ : Fin n β†’ ℝ) :
(βˆ‘ v : Fin n, if v βˆ‰ F then (βˆ‘ e, if l e = v ∧ r e ∈ F then f e else 0) * Οƒ v else 0) =
βˆ‘ e, if l e βˆ‰ F ∧ r e ∈ F then f e * Οƒ (l e) else 0 := by
classical
calc
_ = βˆ‘ v : Fin n, βˆ‘ e, if v βˆ‰ F ∧ l e = v ∧ r e ∈ F then f e * Οƒ v else 0 := by
apply Finset.sum_congr rfl
intro v _
by_cases hv : v ∈ F
Β· simp [hv]
Β· simp only [hv, not_false_eq_true, ite_true, true_and, Finset.sum_mul]
apply Finset.sum_congr rfl
intro e _
split_ifs <;> simp
_ = _ := by
rw [Finset.sum_comm]
apply Finset.sum_congr rfl
intro e _
have H : βˆ€ v : Fin n, (v βˆ‰ F ∧ l e = v ∧ r e ∈ F) ↔
(l e = v ∧ l e βˆ‰ F ∧ r e ∈ F) := by
intro v
constructor
· rintro ⟨hv, he, hr⟩
exact ⟨he, he.symm β–Έ hv, hr⟩
· rintro ⟨he, hl, hr⟩
exact ⟨he β–Έ hl, he, hr⟩
simp_rw [H]
by_cases he : l e βˆ‰ F ∧ r e ∈ F
Β· simp [he]
Β· simp [he]
/-- The finite-field contribution induced by the actual prescribed spins.
Neither these induced fields nor the original fields survive in `qZero`. -/
noncomputable def pinField {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (F : Finset (Fin n)) (Ο„ : Spins n) (v : KeptVertex F) : ℝ :=
h v + (βˆ‘ e, if G.left e = v.val ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.right e)) else 0) +
βˆ‘ e, if G.right e = v.val ∧ G.left e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) else 0
/-- Field and bond terms depending only on fixed sites. They cancel from
all normalized laws, but recording them makes the elimination exact. -/
noncomputable def pinConstant {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (F : Finset (Fin n)) (Ο„ : Spins n) : ℝ :=
(βˆ‘ e, if G.left e ∈ F ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) * spin (Ο„ (G.right e)) else 0) +
βˆ‘ v ∈ F, h v * spin (Ο„ v)
lemma energy_pin_expansion {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (F : Finset (Fin n)) (Ο„ ρ : Spins n) (hρ : AgreesOn F ρ Ο„) :
energy G h ρ = pinConstant G h F Ο„ +
(βˆ‘ e, if G.left e βˆ‰ F ∧ G.right e βˆ‰ F then
G.coupling e * spin (ρ (G.left e)) * spin (ρ (G.right e)) else 0) +
βˆ‘ v : Fin n, if v βˆ‰ F then
(h v + (βˆ‘ e, if G.left e = v ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.right e)) else 0) +
βˆ‘ e, if G.right e = v ∧ G.left e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) else 0) * spin (ρ v) else 0 := by
classical
have hsplit : (βˆ‘ v : Fin n, if v βˆ‰ F then
(h v + (βˆ‘ e, if G.left e = v ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.right e)) else 0) +
βˆ‘ e, if G.right e = v ∧ G.left e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) else 0) * spin (ρ v) else 0) =
(βˆ‘ v : Fin n, if v βˆ‰ F then h v * spin (ρ v) else 0) +
(βˆ‘ e, if G.left e βˆ‰ F ∧ G.right e ∈ F then
(G.coupling e * spin (Ο„ (G.right e))) * spin (ρ (G.left e)) else 0) +
(βˆ‘ e, if G.right e βˆ‰ F ∧ G.left e ∈ F then
(G.coupling e * spin (Ο„ (G.left e))) * spin (ρ (G.right e)) else 0) := by
rw [← sum_pin_incidence G.left G.right F
(fun e => G.coupling e * spin (Ο„ (G.right e))) (fun v => spin (ρ v)),
← sum_pin_incidence G.right G.left F
(fun e => G.coupling e * spin (Ο„ (G.left e))) (fun v => spin (ρ v))]
rw [← Finset.sum_add_distrib, ← Finset.sum_add_distrib]
apply Finset.sum_congr rfl
intro v _
by_cases hv : v ∈ F <;> simp [hv, add_mul]
rw [hsplit]
unfold energy pinConstant
have he : (βˆ‘ e, G.coupling e * spin (ρ (G.left e)) * spin (ρ (G.right e))) =
(βˆ‘ e, if G.left e ∈ F ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) * spin (Ο„ (G.right e)) else 0) +
(βˆ‘ e, if G.left e βˆ‰ F ∧ G.right e βˆ‰ F then
G.coupling e * spin (ρ (G.left e)) * spin (ρ (G.right e)) else 0) +
(βˆ‘ e, if G.left e βˆ‰ F ∧ G.right e ∈ F then
(G.coupling e * spin (Ο„ (G.right e))) * spin (ρ (G.left e)) else 0) +
(βˆ‘ e, if G.right e βˆ‰ F ∧ G.left e ∈ F then
(G.coupling e * spin (Ο„ (G.left e))) * spin (ρ (G.right e)) else 0) := by
rw [← Finset.sum_add_distrib, ← Finset.sum_add_distrib, ← Finset.sum_add_distrib]
apply Finset.sum_congr rfl
intro e _
by_cases hl : G.left e ∈ F <;> by_cases hr : G.right e ∈ F
· simp [hl, hr, hρ _ hl, hρ _ hr]
· simp [hl, hr, hρ _ hl]
· simp [hl, hr, hρ _ hr, mul_comm, mul_left_comm]
Β· simp [hl, hr]
have hv : (βˆ‘ v, h v * spin (ρ v)) =
(βˆ‘ v ∈ F, h v * spin (Ο„ v)) +
(βˆ‘ v : Fin n, if v βˆ‰ F then h v * spin (ρ v) else 0) := by
rw [← Fintype.sum_extend_by_zero F (fun v => h v * spin (Ο„ v)),
← Finset.sum_add_distrib]
apply Finset.sum_congr rfl
intro v _
by_cases hv : v ∈ F <;> simp [hv, hρ v]
rw [he, hv]
ring
lemma sum_kept {n : β„•} (F : Finset (Fin n)) (f : Fin n β†’ ℝ) :
(βˆ‘ v : KeptVertex F, f v.val) = βˆ‘ v : Fin n, if v βˆ‰ F then f v else 0 := by
classical
rw [← Finset.sum_filter]
exact (Finset.sum_subtype _ (fun v => by simp only [Finset.mem_filter,
Finset.mem_univ, true_and]) f).symm
/-- Exact deletion of fixed sites: their incident bonds become finite real
fields, while only bonds with two free endpoints remain as interactions. -/
lemma energy_pinExtension {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (F : Finset (Fin n)) (Ο„ : Spins n) (Οƒ : KeptVertex F β†’ Bool) :
energy G h (pinExtension F Ο„ Οƒ) = pinConstant G h F Ο„ +
deletedEnergy G F Οƒ + βˆ‘ v : KeptVertex F, pinField G h F Ο„ v * spin (Οƒ v) := by
classical
rw [energy_pin_expansion G h F Ο„ _ (pinExtension_agrees F Ο„ Οƒ)]
congr 1
Β· congr 1
unfold deletedEnergy keptEdges
rw [Finset.sum_filter]
apply Finset.sum_congr rfl
intro e _
split_ifs with he
Β· simp only [pinExtension, extendKept, dite_eq_right he.1, dite_eq_right he.2, dite_eq_left he.1, dite_eq_left he.2]
Β· rfl
Β· have hs : (βˆ‘ v : KeptVertex F, pinField G h F Ο„ v * spin (Οƒ v)) =
βˆ‘ v : Fin n, if v βˆ‰ F then
(h v + (βˆ‘ e, if G.left e = v ∧ G.right e ∈ F then
G.coupling e * spin (Ο„ (G.right e)) else 0) +
βˆ‘ e, if G.right e = v ∧ G.left e ∈ F then
G.coupling e * spin (Ο„ (G.left e)) else 0) *
spin (pinExtension F Ο„ Οƒ v) else 0 := by
rw [← sum_kept]
simp only [pinField, pinExtension_apply]
exact hs.symm
lemma weight_pinExtension {n m : β„•} (G : FiniteGraph n m)
(h : Fin n β†’ ℝ) (F : Finset (Fin n)) (Ο„ : Spins n) (Οƒ : KeptVertex F β†’ Bool) :
weight G h (pinExtension F Ο„ Οƒ) = Real.exp (pinConstant G h F Ο„) *
Real.exp (deletedEnergy G F Οƒ + βˆ‘ v : KeptVertex F, pinField G h F Ο„ v * spin (Οƒ v)) := by
unfold weight
rw [energy_pinExtension]
rw [← Real.exp_add]
congr 1
ring
end BufferedIsing
end OAI