File size: 16,107 Bytes
e5a97c6 | 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | import Mathlib
import OAI.Combinatorics.MatchingCount.Probability.LocalVariance
import OAI.Combinatorics.MatchingCount.Sampling.Certify
namespace OAI
namespace MatchingFPRAS.Transport.Sparse
open Finset
variable {V E : Type*} [Fintype V] [DecidableEq V] [Fintype E] [DecidableEq E]
variable (ends : E → Sym2 V)
instance decideOverlay (U : Finset E) :
DecidableRel (overlayLineGraph (fun e v => v ∈ ends e) U).Adj := by
intro e f
change Decidable (e ≠ f ∧ e ∈ U ∧ f ∈ U ∧ ∃ v, v ∈ ends e ∧ v ∈ ends f)
infer_instance
def component (U : Finset E) (a : E) : Finset E :=
U ∩ ComputableReach.closure (overlayLineGraph (fun e v => v ∈ ends e) U) a
lemma component_eq (U : Finset E) (a : E) :
component ends U a = componentEdges (inc:=fun e v => v ∈ ends e) U a := by
ext e
simp only [component,mem_inter,ComputableReach.mem_closure,mem_componentEdges]
def allComponents (U : Finset E) : Finset (Finset E) := U.image (component ends U)
lemma allComponents_eq (U : Finset E) :
allComponents ends U = components (inc:=fun e v => v ∈ ends e) U := by
simp only [allComponents,components]
exact Finset.image_congr (fun a _ => component_eq ends U a)
def discrepancies (A B : PM (fun e v => v ∈ ends e)) : Finset (Finset E) :=
(allComponents ends (A.val ∪ B.val)).filter (fun S => ¬S ⊆ A.val ∩ B.val)
lemma discrepancies_eq (A B : PM (fun e v => v ∈ ends e)) :
discrepancies ends A B = discrepancyComponents A B := by
rw [discrepancies,allComponents_eq]
rfl
def cycle (p : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e))
(S : Finset E) : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e) :=
if h : S ∈ allComponents ends (p.1.val ∪ p.2.val) then
(⟨exchange p.1.val p.2.val S,exchange_matching p.1.property p.2.property
(component_exchange_compatible (by rwa [allComponents_eq] at h))⟩,
⟨exchange p.2.val p.1.val S,exchange_matching p.2.property p.1.property
(component_exchange_compatible (by rwa [allComponents_eq] at h)).symm⟩)
else p
lemma cycle_eq (p : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) (S : Finset E) :
cycle ends p S = cycleMove p S := by
unfold cycle cycleMove
simp only [allComponents_eq]
lemma discrepancies_card (A B : PM (fun e v => v ∈ ends e)) :
(discrepancies ends A B).card ≤ A.val.card+B.val.card := by
calc
_ ≤ (allComponents ends (A.val ∪ B.val)).card := card_filter_le _ _
_ ≤ (A.val ∪ B.val).card := card_image_le
_ ≤ _ := card_union_le _ _
end MatchingFPRAS.Transport.Sparse
namespace MatchingFPRAS
structure WeightedOptions (Ω : Type*) where
size : ℕ
out : Fin size → Ω
mass : Fin size → ℚ
nonneg : ∀ i, 0 ≤ mass i
total : ∑ i, mass i = 1
namespace WeightedOptions
open Finset
variable {Ω Ψ : Type*}
def pure (a : Ω) : WeightedOptions Ω where
size := 1
out := fun _ => a
mass := fun _ => 1
nonneg := by simp
total := by simp
def map (f : Ω → Ψ) (c : WeightedOptions Ω) : WeightedOptions Ψ where
size := c.size
out := f ∘ c.out
mass := c.mass
nonneg := c.nonneg
total := c.total
def mix (p : ℚ) (hp : 0 ≤ p) (hp1 : p ≤ 1)
(a b : WeightedOptions Ω) : WeightedOptions Ω where
size := a.size+b.size
out := Fin.addCases a.out b.out
mass := Fin.addCases (fun i => p*a.mass i) (fun i => (1-p)*b.mass i)
nonneg i := by
refine Fin.addCases (fun i => ?_) (fun i => ?_) i
· simp only [Fin.addCases_left]
exact mul_nonneg hp (a.nonneg i)
· simp only [Fin.addCases_right]
exact mul_nonneg (sub_nonneg.mpr hp1) (b.nonneg i)
total := by
simp only [Fin.sum_univ_add,Fin.addCases_left,Fin.addCases_right,←mul_sum,a.total,b.total]
ring
variable [DecidableEq Ω]
def law (c : WeightedOptions Ω) (b : Ω) : ℚ := ∑ i, if c.out i=b then c.mass i else 0
lemma law_nonneg (c : WeightedOptions Ω) (b : Ω) : 0 ≤ c.law b := by
apply sum_nonneg
intro i _
split_ifs <;> [exact c.nonneg i; exact le_rfl]
lemma law_sum [Fintype Ω] (c : WeightedOptions Ω) : ∑ b, c.law b=1 := by
unfold law
rw [sum_comm]
simpa using c.total
@[simp] lemma pure_law (a b : Ω) : (pure a).law b=if a=b then 1 else 0 := by
change (∑ _i : Fin 1, if a=b then (1:ℚ) else 0)=_
simp
lemma mix_law (p : ℚ) (hp : 0 ≤ p) (hp1 : p ≤ 1) (a b : WeightedOptions Ω) (x : Ω) :
(mix p hp hp1 a b).law x=p*a.law x+(1-p)*b.law x := by
change (∑ i : Fin (a.size+b.size), if Fin.addCases a.out b.out i=x then
Fin.addCases (fun i=>p*a.mass i) (fun i=>(1-p)*b.mass i) i else (0:ℚ))=_
rw [Fin.sum_univ_add]
simp only [Fin.addCases_left,Fin.addCases_right,law,mul_sum]
congr 1 <;> apply sum_congr rfl <;> intro i _ <;> split_ifs <;> simp
lemma sum_law_mul [Fintype Ω] (c : WeightedOptions Ω) (f : Ω → ℚ) :
∑ b, c.law b*f b = ∑ i, c.mass i*f (c.out i) := by
simp only [law,sum_mul]
rw [sum_comm]
apply sum_congr rfl
intro i _
simp only [ite_mul,zero_mul]
simp
lemma map_law [Fintype Ω] [DecidableEq Ψ] (f : Ω → Ψ) (c : WeightedOptions Ω) (b : Ψ) :
(c.map f).law b = ∑ a, if f a=b then c.law a else 0 := by
change (∑ i : Fin c.size, if f (c.out i)=b then c.mass i else (0:ℚ))=_
calc
_ = ∑ i, c.mass i*(if f (c.out i)=b then 1 else 0) := by
simp only [mul_ite,mul_one,mul_zero]
_ = ∑ a, c.law a*(if f a=b then 1 else 0) :=
(c.sum_law_mul (fun a => if f a=b then (1:ℚ) else 0)).symm
_ = _ := by simp
lemma map_first_law [DecidableEq Ψ] (c : WeightedOptions Ω) (a : Ψ) (b : Ω × Ψ) :
(c.map (fun x => (x,a))).law b = if a=b.2 then c.law b.1 else 0 := by
change (∑ i : Fin c.size, if (c.out i,a)=b then c.mass i else (0:ℚ))=_
rcases b with ⟨b,d⟩
by_cases h : a=d
· subst d
simp [law]
· simp [h]
def accept (c : WeightedOptions Ω) (hold : Ω) (p : Ω → ℚ)
(hp : ∀ a, 0 ≤ p a) (hp1 : ∀ a, p a ≤ 1) : WeightedOptions Ω where
size := c.size+c.size
out := Fin.addCases c.out (fun _ => hold)
mass := Fin.addCases (fun i => c.mass i*p (c.out i))
(fun i => c.mass i*(1-p (c.out i)))
nonneg i := by
refine Fin.addCases (fun i => ?_) (fun i => ?_) i
· simp only [Fin.addCases_left]
exact mul_nonneg (c.nonneg i) (hp _)
· simp only [Fin.addCases_right]
exact mul_nonneg (c.nonneg i) (sub_nonneg.mpr (hp1 _))
total := by
simp only [Fin.sum_univ_add,Fin.addCases_left,Fin.addCases_right,←sum_add_distrib]
simp only [←mul_add,add_sub_cancel,mul_one,c.total]
lemma accept_law (c : WeightedOptions Ω) (hold : Ω) (p : Ω → ℚ)
(hp : ∀ a, 0 ≤ p a) (hp1 : ∀ a, p a ≤ 1) (b : Ω) :
(c.accept hold p hp hp1).law b = c.law b*p b +
if hold=b then ∑ i, c.mass i*(1-p (c.out i)) else 0 := by
change (∑ i : Fin (c.size+c.size), if Fin.addCases c.out (fun _=>hold) i=b then
Fin.addCases (fun i=>c.mass i*p (c.out i)) (fun i=>c.mass i*(1-p (c.out i))) i
else (0:ℚ))=_
rw [Fin.sum_univ_add]
simp only [Fin.addCases_left,Fin.addCases_right]
congr 1
· rw [law,sum_mul]
apply sum_congr rfl
intro i _
split_ifs with h
· rw [h]
· simp
· split_ifs <;> simp_all
variable [LinearOrder Ω]
def numbering (s : Finset Ω) : Fin s.card ≃ ↥s := (s.orderIsoOfFin rfl).toEquiv
def uniform (s : Finset Ω) (hs : s.Nonempty) : WeightedOptions Ω where
size := s.card
out := fun i => (numbering s i).val
mass := fun _ => (s.card:ℚ)⁻¹
nonneg := by intro i; positivity
total := by
have h : (s.card:ℚ) ≠ 0 := by exact_mod_cast hs.card_pos.ne'
simp [h]
lemma uniform_law (s : Finset Ω) (hs : s.Nonempty) (b : Ω) :
(uniform s hs).law b=if b ∈ s then (s.card:ℚ)⁻¹ else 0 := by
unfold law uniform
change (∑ i, if (numbering s i).val=b then (s.card:ℚ)⁻¹ else 0)=_
rw [(numbering s).sum_comp (fun i : ↥s => if i.val=b then (s.card:ℚ)⁻¹ else 0)]
rw [Finset.sum_coe_sort s (fun x : Ω => if x=b then (s.card:ℚ)⁻¹ else 0)]
simp
def neighbors (a : Ω) (s : Finset Ω) (d : ℕ) (hd : s.card ≤ d) : WeightedOptions Ω where
size := s.card+1
out := Fin.lastCases a (fun i => (numbering s i).val)
mass := Fin.lastCases (1-(s.card:ℚ)/d) (fun _ => (d:ℚ)⁻¹)
nonneg i := by
refine Fin.lastCases ?_ (fun j => ?_) i
· simp only [Fin.lastCases_last]
by_cases h : d=0
· simp [h]
· have hh : (0:ℚ)<d := by exact_mod_cast Nat.pos_of_ne_zero h
exact sub_nonneg.mpr ((div_le_one hh).mpr (by exact_mod_cast hd))
· simp only [Fin.lastCases_castSucc]
positivity
total := by
rw [Fin.sum_univ_castSucc]
simp only [Fin.lastCases_castSucc,Fin.lastCases_last,sum_const,card_fin,nsmul_eq_mul]
ring
lemma neighbors_law (a : Ω) (s : Finset Ω) (d : ℕ) (hd : s.card ≤ d) (b : Ω) :
(neighbors a s d hd).law b=(if b ∈ s then (d:ℚ)⁻¹ else 0)+
(if a=b then 1-(s.card:ℚ)/d else 0) := by
unfold law neighbors
rw [Fin.sum_univ_castSucc]
simp only [Fin.lastCases_castSucc,Fin.lastCases_last]
congr 1
rw [(numbering s).sum_comp (fun i : ↥s => if i.val=b then (d:ℚ)⁻¹ else 0)]
rw [Finset.sum_coe_sort s (fun x : Ω => if x=b then (d:ℚ)⁻¹ else 0)]
simp
end WeightedOptions
end MatchingFPRAS
namespace MatchingFPRAS.Transport.Sparse
open Finset WeightedOptions
variable {Ω : Type*} [Fintype Ω] [LinearOrder Ω] [DecidableEq Ω]
omit [LinearOrder Ω] in
lemma erased_eq (R : Ω → Ω → Prop) (s : Finset Ω) (a : Ω)
(hs : ∀ b, b ∈ s ↔ R a b) : s.erase a = relationDestinations R a := by
classical
ext b
simp only [mem_erase,relationDestinations,mem_filter,mem_univ,true_and,hs,ne_comm]
lemma neighbors_real (R : Ω → Ω → Prop) (s : Finset Ω) (d : ℕ) (a b : Ω)
(hs : s=relationDestinations R a) (hd : s.card ≤ d) :
((WeightedOptions.neighbors a s d hd).law b : ℝ) = relationProposal R d a b := by
classical
rw [WeightedOptions.neighbors_law]
push_cast
subst s
rw [relationProposal,relationOff_sum]
by_cases hab : a=b
· subst b
simp [relationDestinations]
· simp only [hab,ite_false,relationOff]
split_ifs <;> push_cast <;> simp
variable {V E : Type*} [Fintype V] [DecidableEq V] [Fintype E] [LinearOrder E]
variable (ends : E → Sym2 V)
local instance finsetOrder : LinearOrder (Finset E) :=
LinearOrder.lift' toColex toColex.injective
local instance pmOrder : LinearOrder (PM (fun e v => v ∈ ends e)) :=
LinearOrder.lift' Subtype.val Subtype.val_injective
lemma color_degree (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A : PM (fun e v => v ∈ ends e)) : ((colors ends A).erase A).card ≤ Fintype.card V :=
(card_erase_le).trans ((colors_card ends hpar A).trans_eq (A.twice_card ends hloop))
lemma switch_degree (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A : PM (fun e v => v ∈ ends e)) :
((switches ends A).erase A).card ≤ 8*(Fintype.card V/2).choose 2 := by
have h : Fintype.card V/2=A.val.card := by have := A.twice_card ends hloop; omega
rw [h]
exact card_erase_le.trans (switches_card ends hpar A)
def colorTable (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A : PM (fun e v => v ∈ ends e)) : WeightedOptions (PM (fun e v => v ∈ ends e)) :=
WeightedOptions.neighbors A ((colors ends A).erase A) (Fintype.card V)
(color_degree ends hloop hpar A)
def switchTable (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A : PM (fun e v => v ∈ ends e)) : WeightedOptions (PM (fun e v => v ∈ ends e)) :=
WeightedOptions.neighbors A ((switches ends A).erase A) (8*(Fintype.card V/2).choose 2)
(switch_degree ends hloop hpar A)
lemma colorTable_law (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A B : PM (fun e v => v ∈ ends e)) :
((colorTable ends hloop hpar A).law B : ℝ)=colorProposal ends A B := by
exact neighbors_real (ColorChange ends) ((colors ends A).erase A) (Fintype.card V) A B
(erased_eq _ _ _ (mem_colors ends A)) (color_degree ends hloop hpar A)
lemma switchTable_law (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(A B : PM (fun e v => v ∈ ends e)) :
((switchTable ends hloop hpar A).law B : ℝ)=switchProposal ends A B := by
exact neighbors_real (fun A B => TwoEdgeSwitch A B) ((switches ends A).erase A)
(8*(Fintype.card V/2).choose 2) A B
(erased_eq _ _ _ (mem_switches ends A)) (switch_degree ends hloop hpar A)
def cycleTable (p : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :
WeightedOptions (PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :=
if h : (discrepancies ends p.1 p.2).Nonempty then
(WeightedOptions.uniform _ h).map (cycle ends p)
else WeightedOptions.pure p
lemma cycleTable_law (p q : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :
((cycleTable ends p).law q : ℝ)=cycleProposal p q := by
classical
by_cases h : (discrepancies ends p.1 p.2).Nonempty
· have h' : (discrepancyComponents p.1 p.2).Nonempty := by rwa [←discrepancies_eq]
rw [cycleTable,dite_eq_left h,cycleProposal,ite_eq_left h',WeightedOptions.map_law]
push_cast
apply sum_congr rfl
intro S _
rw [WeightedOptions.uniform_law,cycle_eq,discrepancies_eq]
unfold cycleTerm
split_ifs <;> simp_all
· have h' : ¬(discrepancyComponents p.1 p.2).Nonempty := by rwa [←discrepancies_eq]
rw [cycleTable,dite_eq_right h,cycleProposal,ite_eq_right h',WeightedOptions.pure_law]
split_ifs <;> norm_num
def pairTable (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(p : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :
WeightedOptions (PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :=
WeightedOptions.mix (1/3) (by norm_num) (by norm_num)
((switchTable ends hloop hpar p.1).map (fun x => (x,p.2)))
(WeightedOptions.mix (1/2) (by norm_num) (by norm_num)
((colorTable ends hloop hpar p.1).map (fun x => (x,p.2)))
(cycleTable ends p))
lemma pairTable_law (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(p q : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :
((pairTable ends hloop hpar p).law q : ℝ)=pairProposalRaw ends p q := by
unfold pairTable
rw [WeightedOptions.mix_law,WeightedOptions.mix_law,
WeightedOptions.map_first_law,WeightedOptions.map_first_law]
push_cast
unfold pairProposalRaw firstLift
by_cases h : p.2=q.2
· simp only [ite_eq_left h]
rw [switchTable_law,colorTable_law,cycleTable_law]
ring
· simp only [ite_eq_right h]
push_cast
rw [cycleTable_law]
ring
lemma pairTable_size (hloop : ∀ e, ¬(ends e).IsDiag)
(hpar : ∀ e, (univ.filter (fun f => ends f = ends e)).card ≤ 2)
(p : PM (fun e v => v ∈ ends e) × PM (fun e v => v ∈ ends e)) :
(pairTable ends hloop hpar p).size ≤
8*p.1.val.card.choose 2 + 2*p.1.val.card + (p.1.val.card+p.2.val.card)+3 := by
have hs : ((switches ends p.1).erase p.1).card ≤ 8*p.1.val.card.choose 2 := card_erase_le.trans (switches_card ends hpar p.1)
have hc : ((colors ends p.1).erase p.1).card ≤ 2*p.1.val.card := card_erase_le.trans (colors_card ends hpar p.1)
have hy := discrepancies_card ends p.1 p.2
simp only [pairTable,WeightedOptions.mix,WeightedOptions.map,switchTable,colorTable,
WeightedOptions.neighbors]
have hz : (cycleTable ends p).size ≤ p.1.val.card+p.2.val.card+1 := by
unfold cycleTable
split_ifs <;> simp only [WeightedOptions.map,WeightedOptions.uniform,WeightedOptions.pure]
· omega
· omega
omega
end MatchingFPRAS.Transport.Sparse
end OAI
|