File size: 12,705 Bytes
e320e82 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | import OAI.Combinatorics.GraphThreshold.TreeReductionData
import Mathlib.Analysis.SpecialFunctions.Exp
import Mathlib.Tactic
namespace OAI
open scoped BigOperators
open Classical
noncomputable section
namespace LeanBlast.SecondKahnKalai
section RetainedLaw
variable {B X : Type*} [Fintype B] [DecidableEq X]
def retainedMass (w : B → ℝ) (G : B → Prop) : ℝ :=
∑ b, if G b then w b else 0
abbrev RetainedIndex (w : B → ℝ) (G : B → Prop) := {b : B // G b ∧ 0 < w b}
theorem sum_retained (w : B → ℝ) (G : B → Prop) (hw : ∀ b, 0 ≤ w b) (f : B → ℝ) :
(∑ b : RetainedIndex w G, w b * f b) = ∑ b, if G b then w b * f b else 0 := by
symm
apply Finset.sum_congr_set {b | G b ∧ 0 < w b}
· intro b hb
simp only [Set.mem_ofPred_eq] at hb
simp [hb.1]
· intro b hb
by_cases hg : G b
· have hnot : ¬ 0 < w b := fun hpos => hb ⟨hg, hpos⟩
have hz : w b = 0 := le_antisymm (le_of_not_gt hnot) (hw b)
simp [hg, hz]
· simp [hg]
theorem sum_retained_weights (w : B → ℝ) (G : B → Prop) (hw : ∀ b, 0 ≤ w b) :
(∑ b : RetainedIndex w G, w b) = retainedMass w G := by
simpa only [mul_one, retainedMass] using sum_retained w G hw (fun _ => 1)
theorem sum_retained_indicator (w : B → ℝ) (G Q : B → Prop) (hw : ∀ b, 0 ≤ w b) :
(∑ b : RetainedIndex w G, if Q b then w b else 0) =
∑ b, if G b ∧ Q b then w b else 0 := by
simpa only [mul_ite, mul_one, mul_zero, ite_and] using
sum_retained w G hw (fun b => if Q b then 1 else 0)
/-- The actual normalized child law, after removing all zero-mass targets. -/
def retainedLaw (w : B → ℝ) (G : B → Prop) (hw : ∀ b, 0 ≤ w b)
(hpos : 0 < retainedMass w G) : FiniteLaw (RetainedIndex w G) where
weight b := w b / retainedMass w G
nonneg b := div_nonneg (hw b) hpos.le
total := by
rw [← Finset.sum_div, sum_retained_weights w G hw]
exact div_self hpos.ne'
theorem retainedLaw_positive (w : B → ℝ) (G : B → Prop) (hw : ∀ b, 0 ≤ w b)
(hpos : 0 < retainedMass w G) : (retainedLaw w G hw hpos).IsPositive := by
intro b
exact div_pos b.property.2 hpos
theorem retainedLaw_prob (w : B → ℝ) (G Q : B → Prop) (hw : ∀ b, 0 ≤ w b)
(hpos : 0 < retainedMass w G) :
(retainedLaw w G hw hpos).prob (fun b => Q b) =
(∑ b, if G b ∧ Q b then w b else 0) / retainedMass w G := by
rw [← sum_retained_indicator w G Q hw, Finset.sum_div]
unfold FiniteLaw.prob
apply Finset.sum_congr rfl
intro b _
by_cases hq : Q b <;> simp [retainedLaw, hq]
theorem retainedLaw_isSpread (w : B → ℝ) (G : B → Prop) (hw : ∀ b, 0 ≤ w b)
(hpos : 0 < retainedMass w G) (F : B → Finset X) {a q : ℝ}
(ha : 0 ≤ a) (hq : 0 < q) (hq1 : q ≤ 1) (hretain : q ≤ retainedMass w G)
(hnum : ∀ J : Finset X, J.Nonempty →
(∑ b, if G b ∧ J ⊆ F b then w b else 0) ≤ a ^ J.card) :
IsSpread (retainedLaw w G hw hpos) (fun b => F b) (a / q) := by
classical
intro J hJ
change (retainedLaw w G hw hpos).prob (fun b => J ⊆ F b.val) ≤ (a / q) ^ J.card
rw [retainedLaw_prob w G (fun b => J ⊆ F b) hw hpos]
have hpower : q ^ J.card ≤ q :=
pow_le_of_le_one hq.le hq1 (Nat.ne_of_gt hJ.card_pos)
calc
_ ≤ a ^ J.card / retainedMass w G := by
apply div_le_div_of_nonneg_right _ hpos.le
convert hnum J hJ using 1
apply Finset.sum_congr rfl
intro b _
by_cases hb : G b ∧ J ⊆ F b <;> simp [hb]
_ ≤ a ^ J.card / q := div_le_div_of_nonneg_left (pow_nonneg ha _) hq hretain
_ ≤ a ^ J.card / q ^ J.card :=
div_le_div_of_nonneg_left (pow_nonneg ha _) (pow_pos hq _) hpower
_ = (a / q) ^ J.card := (div_pow a q J.card).symm
theorem target_fragment_mass_le_source
(μ : FiniteLaw B) (A F : B → Finset X) (joint : B × B → ℝ)
(event : B → B → Prop) (G : B → Prop)
(hjoint : ∀ c b, 0 ≤ joint (c, b))
(hsource : ∀ c, ∑ b, joint (c, b) = μ.weight c)
(hfragment : ∀ c b, 0 < joint (c, b) → F b ⊆ A c) (J : Finset X) :
(∑ b, if G b ∧ J ⊆ F b then ∑ c, if event c b then joint (c, b) else 0 else 0) ≤
μ.prob (fun c => J ⊆ A c) := by
calc
_ = ∑ b, ∑ c,
if G b ∧ J ⊆ F b then if event c b then joint (c, b) else 0 else 0 := by
apply Finset.sum_congr rfl
intro b _
by_cases hb : G b ∧ J ⊆ F b <;> simp [hb]
_ = ∑ c, ∑ b,
if G b ∧ J ⊆ F b then if event c b then joint (c, b) else 0 else 0 :=
Finset.sum_comm
_ ≤ ∑ c, ∑ b, if J ⊆ A c then joint (c, b) else 0 := by
apply Finset.sum_le_sum
intro c _
apply Finset.sum_le_sum
intro b _
by_cases hb : G b ∧ J ⊆ F b
· by_cases he : event c b
· by_cases hj : 0 < joint (c, b)
· have hJA : J ⊆ A c := hb.2.trans (hfragment c b hj)
simp [hb, he, hJA]
· have hz : joint (c, b) = 0 :=
le_antisymm (le_of_not_gt hj) (hjoint c b)
simp [hb, he, hz]
· simp only [ite_eq_left hb, ite_eq_right he]
split_ifs <;> linarith [hjoint c b]
· simp only [ite_eq_right hb]
split_ifs <;> linarith [hjoint c b]
_ = μ.prob (fun c => J ⊆ A c) := by
unfold FiniteLaw.prob
apply Finset.sum_congr rfl
intro c _
by_cases hc : J ⊆ A c <;> simp [hc, hsource]
end RetainedLaw
section Construction
variable {X : Type} [DecidableEq X]
theorem one_sub_exp_neg_nat_pos {m : ℕ} (hm : 0 < m) :
0 < 1 - Real.exp (-(m : ℝ)) := by
have hm' : 0 < (m : ℝ) := by exact_mod_cast hm
exact sub_pos.mpr (Real.exp_lt_one_iff.mpr (neg_neg_of_pos hm'))
theorem good_retainedMass_pos {d : ℕ} {B : Type} [Fintype B]
(μ : FiniteLaw B) (hμ : μ.IsPositive) (A : B → Finset X)
(child : B → ProbabilityTree X d) (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X)
(hm : 0 < m 0) (hg : Good ρ m W (.node B μ hμ A child)) :
0 < retainedMass (successfulTargetMass μ A ρ (m 0) W)
(fun b => Good ρ (fun j => m (j + 1)) W (child b)) := by
apply lt_of_lt_of_le (one_sub_exp_neg_nat_pos hm)
simpa only [Good, retainedMass] using hg
/-- Explicit good-node pruning and normalization. The depth is preserved in the
result type. Every retained label is the actual fragment of its original label. -/
def reduceTree (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) : ProbabilityTree X d :=
match T with
| .leaf => .leaf
| @ProbabilityTree.node _ n _B _inst μ hμ A child =>
let w := successfulTargetMass μ A ρ (m 0) W
let G := fun b => Good ρ (fun j => m (j + 1)) W (child b)
have hw : ∀ b, 0 ≤ w b := successfulTargetMass_nonneg μ A hρ.le (m 0) W
have hp : 0 < retainedMass w G :=
good_retainedMass_pos μ hμ A child ρ m W (hm 0 (Nat.succ_pos _)) hg
.node (RetainedIndex w G) (retainedLaw w G hw hp)
(retainedLaw_positive w G hw hp) (fun b => A b.val \ W)
(fun b => reduceTree ρ (fun j => m (j + 1)) W (child b.val) hρ
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1)
termination_by structural T
theorem reduceTree_support_subset (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) : (reduceTree ρ m W T hρ hm hg).support ⊆ T.support := by
induction T generalizing m with
| leaf => simp [reduceTree, ProbabilityTree.support]
| @node n B inst μ hμ A child ih =>
simp only [reduceTree, ProbabilityTree.support]
intro x hx
obtain ⟨b, _, hb⟩ := Finset.mem_biUnion.mp hx
refine Finset.mem_biUnion.mpr ⟨b.val, Finset.mem_univ _, ?_⟩
rcases Finset.mem_union.mp hb with hb | hb
· exact Finset.mem_union_left _ (Finset.mem_sdiff.mp hb).1
· exact Finset.mem_union_right _
(ih b.val (fun j => m (j + 1))
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1 hb)
theorem reduceTree_hasCapacities (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) :
(reduceTree ρ m W T hρ hm hg).HasCapacities (fun j => m j / 2) := by
induction T generalizing m with
| leaf => simp only [reduceTree, ProbabilityTree.HasCapacities]
| @node n B inst μ hμ A child ih =>
simp only [reduceTree, ProbabilityTree.HasCapacities]
constructor
· intro b
exact successfulTargetMass_pos_capacity μ A ρ (m 0) W b.val b.property.2
· intro b
exact ih b.val (fun j => m (j + 1))
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1
theorem reduceTree_isDisjoint (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) (hT : T.IsDisjoint) :
(reduceTree ρ m W T hρ hm hg).IsDisjoint := by
induction T generalizing m with
| leaf => simp only [reduceTree, ProbabilityTree.IsDisjoint]
| @node n B inst μ hμ A child ih =>
simp only [reduceTree, ProbabilityTree.IsDisjoint]
constructor
· intro b
exact (hT.1 b.val).mono Finset.sdiff_subset
(reduceTree_support_subset ρ (fun j => m (j + 1)) W (child b.val) hρ
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1)
· intro b
exact ih b.val (fun j => m (j + 1))
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1 (hT.2 b.val)
theorem reduceTree_path_recovery (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) :
∀ q : (reduceTree ρ m W T hρ hm hg).Path,
∃ p : T.Path, T.pathUnion p ⊆ W ∪ (reduceTree ρ m W T hρ hm hg).pathUnion q := by
induction T generalizing m with
| leaf =>
intro q
exact ⟨PUnit.unit, Finset.empty_subset _⟩
| @node n B inst μ hμ A child ih =>
rw [reduceTree]
simp only [ProbabilityTree.Path, ProbabilityTree.pathUnion]
intro q
rcases q with ⟨b, q⟩
obtain ⟨p, hp⟩ := ih b.val (fun j => m (j + 1))
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1 q
refine ⟨⟨b.val, p⟩, ?_⟩
intro x hx
rcases Finset.mem_union.mp hx with ha | hc
· by_cases hw : x ∈ W
· exact Finset.mem_union_left _ hw
· exact Finset.mem_union_right _
(Finset.mem_union_left _ (Finset.mem_sdiff.mpr ⟨ha, hw⟩))
· rcases Finset.mem_union.mp (hp hc) with hw | hq
· exact Finset.mem_union_left _ hw
· exact Finset.mem_union_right _ (Finset.mem_union_right _ hq)
theorem reduceTree_hasSpread (ρ : ℝ) (m : ℕ → ℕ) (W : Finset X) {d : ℕ}
(T : ProbabilityTree X d) (hρ : 0 < ρ) (hm : ∀ j < d, 0 < m j)
(hg : Good ρ m W T) (a : ℕ → ℝ) (ha : ∀ j < d, 0 ≤ a j)
(hT : T.HasSpread a) :
(reduceTree ρ m W T hρ hm hg).HasSpread
(fun j => a j / (1 - Real.exp (-(m j : ℝ)))) := by
induction T generalizing m a with
| leaf => simp only [reduceTree, ProbabilityTree.HasSpread]
| @node n B inst μ hμ A child ih =>
simp only [reduceTree, ProbabilityTree.HasSpread]
constructor
· refine retainedLaw_isSpread (successfulTargetMass μ A ρ (m 0) W)
(fun b => Good ρ (fun j => m (j + 1)) W (child b))
(successfulTargetMass_nonneg μ A hρ.le (m 0) W)
(good_retainedMass_pos μ hμ A child ρ m W (hm 0 (Nat.succ_pos _)) hg)
(fun b => A b \ W)
(ha 0 (Nat.succ_pos _))
(one_sub_exp_neg_nat_pos (hm 0 (Nat.succ_pos _))) ?_ ?_ ?_
· linarith [Real.exp_pos (-(m 0 : ℝ))]
· simpa only [retainedMass, Good] using hg
· intro J hJ
exact (target_fragment_mass_le_source μ A (fun b => A b \ W)
(resamplingWeight μ A ρ W) (LocalSuccess μ A ρ (m 0) W)
(fun b => Good ρ (fun j => m (j + 1)) W (child b))
(fun c b => resamplingWeight_nonneg μ A hρ.le W (c, b))
(resampling_source_marginal μ A hμ hρ W)
(fun c b hp => resamplingFragment_subset_source μ A ρ W c b hp) J).trans
(hT.1 J hJ)
· intro b
exact ih b.val (fun j => m (j + 1))
(fun j hj => hm (j + 1) (Nat.succ_lt_succ hj)) b.property.1
(fun j => a (j + 1)) (fun j hj => ha (j + 1) (Nat.succ_lt_succ hj))
(hT.2 b.val)
end Construction
end LeanBlast.SecondKahnKalai
end
end OAI
|