File size: 28,249 Bytes
9b6e014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import Mathlib
import OAI.Probability.Contingency.ConditionalTransport

namespace OAI

/-! Repair Variance. -/

noncomputable section

open scoped BigOperators
open scoped BigOperators
namespace Contingency

 
theorem variance_le_center {α : Type*} [Fintype α] (w H : α → ℝ)
    (hw : ∀ x, 0 ≤ w x) (a : ℝ) :
    weightedVariance w H ≤ ∑ x, w x*(H x-a)^2 := by
  by_cases h : totalWeight w = 0
  · have hz : ∀ x, w x=0 := by
      intro x
      have hh := Finset.single_le_sum (fun y _ => hw y) (Finset.mem_univ x)
      change w x ≤ totalWeight w at hh
      rw [h] at hh
      exact le_antisymm hh (hw x)
    simp [weightedVariance,hz]
  · rw [variance_translate w H h a]
    exact le_add_of_nonneg_right (mul_nonneg (Finset.sum_nonneg fun x _ => hw x) (sq_nonneg _))

 

theorem variance_repair_extension {T D P K : Type*}
    [Fintype T] [Fintype D] [Fintype P] [Fintype K]
    (wT hT : T → ℝ) (wD hD : D → ℝ) (wP hP : P → ℝ)
    (_hwT : ∀ t, 0 ≤ wT t) (hwD : ∀ x, 0 ≤ wD x) (hwP : ∀ p, 0 ≤ wP p)
    (f : T → P) (hf : Function.Injective f)
    (hTw : ∀ t, wT t=wP (f t)) (hTh : ∀ t, hT t=hP (f t))
    (g : D → K × P) (hg : Function.Injective g) (hDw : ∀ x, wD x≤wP (g x).2)
    (E : ℝ) (hE : (∑ x, wD x*(hD x-hP (g x).2)^2) ≤ E) :
    weightedVariance (Sum.elim wT wD) (Sum.elim hT hD) ≤
      (1+2*(Fintype.card K:ℝ))*weightedVariance wP hP+2*E := by
  classical
  let a := weightedMean wP hP
  have ht : (∑ t, wT t*(hT t-a)^2) ≤ weightedVariance wP hP := by
    simp_rw [hTw,hTh]
    rw [← Finset.sum_image (s := Finset.univ) (f := fun p => wP p*(hP p-a)^2)
      (fun x _ y _ h => hf h)]
    exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _)
      (fun x _ _ => mul_nonneg (hwP x) (sq_nonneg _))
  have hd : (∑ x, wD x*(hP (g x).2-a)^2) ≤
      (Fintype.card K:ℝ)*weightedVariance wP hP := by
    calc
      _ ≤ ∑ x, wP (g x).2*(hP (g x).2-a)^2 :=
        Finset.sum_le_sum fun x _ => mul_le_mul_of_nonneg_right (hDw x) (sq_nonneg _)
      _ = ∑ kp ∈ Finset.univ.image g, wP kp.2*(hP kp.2-a)^2 := by
        rw [Finset.sum_image (fun x _ y _ h => hg h)]
      _ ≤ ∑ kp : K × P, wP kp.2*(hP kp.2-a)^2 :=
        Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _)
          (fun x _ _ => mul_nonneg (hwP x.2) (sq_nonneg _))
      _ = _ := by simp [Fintype.sum_prod_type,weightedVariance,a]
  have hx : (∑ x, wD x*(hD x-a)^2) ≤
      2*(∑ x, wD x*(hD x-hP (g x).2)^2) +
      2*(∑ x, wD x*(hP (g x).2-a)^2) := by
    rw [Finset.mul_sum,Finset.mul_sum,← Finset.sum_add_distrib]
    apply Finset.sum_le_sum
    intro x _
    have hs : (hD x-a)^2 ≤ 2*(hD x-hP (g x).2)^2+2*(hP (g x).2-a)^2 := by
      nlinarith [sq_nonneg (hD x-2*hP (g x).2+a)]
    have hm := mul_le_mul_of_nonneg_left hs (hwD x)
    nlinarith only [hm]
  have hb := variance_le_center (Sum.elim wT wD) (Sum.elim hT hD)
    (by intro x; cases x <;> simp_all) a
  rw [Fintype.sum_sum_type] at hb
  simp only [Sum.elim_inl,Sum.elim_inr] at hb
  nlinarith only [hb,ht,hd,hx,hE]

end Contingency

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

abbrev TransverseState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :=
  {X : State m n // Feasible U r c X ∧ Transversal X}
abbrev DefectiveState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :=
  {X : State m n // Feasible U r c X ∧ ∃ s t, DefectAt U r c s t X}
 
abbrev GraphState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :=
  TransverseState U r c ⊕ DefectiveState U r c

instance transverseFinite (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finite (TransverseState U r c) :=
  Finite.of_injective (fun X => (⟨X.val,X.property.1⟩ : {X // Feasible U r c X}))
    (fun _ _ h => Subtype.ext (congrArg (fun Y : {X // Feasible U r c X} => Y.val) h))
instance defectiveFinite (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finite (DefectiveState U r c) :=
  Finite.of_injective (fun X => (⟨X.val,X.property.1⟩ : {X // Feasible U r c X}))
    (fun _ _ h => Subtype.ext (congrArg (fun Y : {X // Feasible U r c X} => Y.val) h))
noncomputable instance transverseFintype (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Fintype (TransverseState U r c) := Fintype.ofFinite _
noncomputable instance defectiveFintype (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Fintype (DefectiveState U r c) := Fintype.ofFinite _

noncomputable def defectPair (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) : Cell m n × Cell m n :=
  (X.property.2.choose, X.property.2.choose_spec.choose)

theorem defectPair_spec (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) :
    DefectAt U r c (defectPair U r c X).1 (defectPair U r c X).2 X.val :=
  X.property.2.choose_spec.choose_spec

theorem defect_not_transversal (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (s t : Cell m n) (X : State m n) (hd : DefectAt U r c s t X) : ¬Transversal X := by
  intro hT
  have h := congrFun hd.2.2.2 s
  change X.q=X.x at hT
  rw [hT] at h
  simp [unit,hd.2.2.1] at h

noncomputable def repairedState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) : TransverseState U r c :=
  ⟨repair U r c (defectPair U r c X).1 (defectPair U r c X).2 X.val,
    repair_feasible U r c _ _ _ X.property.1 (defectPair_spec U r c X),rfl⟩

theorem coordinates_state_inverse (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : State m n) (hX : ∀ w, IsLarge U r c w → X.x w=0 ∧ X.q w=0) :
    coordinatesState U r c (stateCoordinates U r c X)=X := by
  have hx : (coordinatesState U r c (stateCoordinates U r c X)).x=X.x := by
    funext w
    by_cases hw : IsSmall U r c w
    · simp [coordinatesState,stateCoordinates,hw]
    · simp [coordinatesState,hw,(hX w (Classical.not_not.mp hw)).1]
  have hq : (coordinatesState U r c (stateCoordinates U r c X)).q=X.q := by
    funext w
    by_cases hw : IsSmall U r c w
    · simp [coordinatesState,stateCoordinates,hw]
    · simp [coordinatesState,hw,(hX w (Classical.not_not.mp hw)).2]
  exact congrArg₂ State.mk hx hq

noncomputable def transverseTuple (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : TransverseState U r c) : SmallSlot U r c → Fin (U+1) :=
  fun s => ⟨(X.val.x s.val).toNat,by have h := X.property.1.1 s.val; omega⟩

@[simp] theorem tuple_transverseTuple (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : TransverseState U r c) : tupleState U r c (transverseTuple U r c X)=X.val := by
  have hh : (fun w : SmallCoord U r c => if w.2 then
      ((transverseTuple U r c X w.1:ℕ):ℤ) else (U:ℤ)-(transverseTuple U r c X w.1:ℕ)) =
      stateCoordinates U r c X.val := by
    funext ⟨s,b⟩
    have hnn := (X.property.1.1 s.val).1
    have hT : X.val.q=X.val.x := X.property.2
    cases b <;> simp [transverseTuple,stateCoordinates,Int.toNat_of_nonneg hnn,hT]
  unfold tupleState
  rw [hh]
  exact coordinates_state_inverse U r c X.val X.property.1.2.1

noncomputable def rootProfile (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (α : SmallSlot U r c → Fin (U+1)) : Profiles U (Fintype.card (SmallSlot U r c)) :=
  (profileEquiv U _).symm (α ∘ smallIndex U r c)

@[simp] theorem rootProfile_state (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (α : SmallSlot U r c → Fin (U+1)) :
    profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0
      (rootProfile U r c α)=tupleState U r c α := by
  unfold profileState rootProfile
  rw [Equiv.apply_symm_apply]
  congr 1
  funext s
  simp [appendAssignment]

noncomputable def transverseProfile (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : TransverseState U r c) : Profiles U (Fintype.card (SmallSlot U r c)) :=
  rootProfile U r c (transverseTuple U r c X)

@[simp] theorem transverseProfile_state (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : TransverseState U r c) :
    profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0
      (transverseProfile U r c X)=X.val := by
  simp [transverseProfile]

theorem transverseProfile_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Function.Injective (transverseProfile U r c) := by
  intro X Y he
  have hh := congrArg (profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0) he
  simpa only [transverseProfile_state,Subtype.val_inj] using hh

noncomputable def taggedRepair (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) : (Cell m n × Cell m n) × Profiles U (Fintype.card (SmallSlot U r c)) :=
  (defectPair U r c X,transverseProfile U r c (repairedState U r c X))

theorem taggedRepair_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Function.Injective (taggedRepair U r c) := by
  intro X Y he
  have hp : defectPair U r c X=defectPair U r c Y := congrArg Prod.fst he
  have ht := transverseProfile_injective U r c (congrArg Prod.snd he)
  have ht' := congrArg Subtype.val ht
  change repair U r c _ _ X.val=repair U r c _ _ Y.val at ht'
  rw [← hp] at ht'
  apply Subtype.ext
  apply repair_injective U r c _ _ X.val Y.val (defectPair_spec U r c X) _ ht'
  rw [hp]
  exact defectPair_spec U r c Y

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

theorem defect_type_unique (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : State m n) (s t s' t' : Cell m n)
    (h : DefectAt U r c s t X) (h' : DefectAt U r c s' t' X) : s=s' ∧ t=t' := by
  have hs := congrFun h.2.2.2 s
  have hs' := congrFun h'.2.2.2 s
  have ht := congrFun h.2.2.2 t
  have ht' := congrFun h'.2.2.2 t
  simp [unit,h.2.2.1,Ne.symm h.2.2.1] at hs ht
  constructor
  · by_contra he
    simp only [unit,ite_eq_right he] at hs'
    split_ifs at hs' <;> omega
  · by_contra he
    simp only [unit,ite_eq_right he] at ht'
    split_ifs at ht' <;> omega

theorem stateCoordinates_box (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : State m n) (hX : Feasible U r c X) :
    InBox (fun _ => (U:ℤ)) (stateCoordinates U r c X) := by
  intro ⟨s,b⟩
  have h := hX.1 s.val
  cases b <;> simp only [stateCoordinates,Bool.false_eq_true,ite_false,ite_true] <;> omega

@[simp] theorem fullOccupancy_state (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : State m n) (s : SmallSlot U r c) :
    fullOccupancy U r c (stateCoordinates U r c X) s = (U:ℤ)+X.x s.val-X.q s.val := by
  simp [fullOccupancy,stateCoordinates]
  ring

theorem stateCoordinates_transverse_vertex (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : TransverseState U r c) : stateCoordinates U r c X.val∈fullVertices U r c := by
  apply (mem_fullVertices U r c _).mpr
  refine ⟨stateCoordinates_box U r c X.val X.property.1,Or.inl ?_⟩
  intro s
  rw [fullOccupancy_state,show X.val.q=X.val.x from X.property.2]
  omega

theorem stateCoordinates_defect_vertex (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) : stateCoordinates U r c X.val∈fullVertices U r c := by
  obtain ⟨s,t,hd⟩ := X.property.2
  apply (mem_fullVertices U r c _).mpr
  refine ⟨stateCoordinates_box U r c X.val X.property.1,Or.inr ?_⟩
  refine ⟨⟨s,hd.1⟩,⟨t,hd.2.1⟩,?_,?_,?_,?_⟩
  · intro he; exact hd.2.2.1 (congrArg Subtype.val he)
  · rw [fullOccupancy_state]
    change (U:ℤ)+X.val.x s-X.val.q s=(U:ℤ)-1
    rw [hd.2.2.2]
    simp [unit,hd.2.2.1]
    ring
  · rw [fullOccupancy_state]
    change (U:ℤ)+X.val.x t-X.val.q t=(U:ℤ)+1
    rw [hd.2.2.2]
    simp [unit,Ne.symm hd.2.2.1]
    ring
  · intro v hvs hvt
    have hvs' : v.val≠s := fun he => hvs (Subtype.ext he)
    have hvt' : v.val≠t := fun he => hvt (Subtype.ext he)
    rw [fullOccupancy_state,hd.2.2.2]
    simp [unit,hvs',hvt']

theorem state_kernel_weight (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (X : State m n) (hX : Feasible U r c X) :
    smallKernel 0 U L C r c (stateCoordinates U r c X)=(weight U L r c X:ℝ) := by
  classical
  rw [weight,ite_eq_left hX]
  exact smallKernel_zero_count U L C r c hrc X hX hC

noncomputable def repairEdge (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X : DefectiveState U r c) : Sym2 (SmallCoord U r c → ℤ) :=
  s(stateCoordinates U r c X.val,stateCoordinates U r c (repairedState U r c X).val)

theorem stateCoordinates_inj_feasible (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (X Y : State m n) (hX : Feasible U r c X) (hY : Feasible U r c Y)
    (he : stateCoordinates U r c X=stateCoordinates U r c Y) : X=Y := by
  have hh := congrArg (coordinatesState U r c) he
  simpa only [coordinates_state_inverse U r c X hX.2.1,
    coordinates_state_inverse U r c Y hY.2.1] using hh

theorem repairEdge_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Function.Injective (repairEdge U r c) := by
  intro X Y he
  rcases Sym2.eq_iff.mp he with ⟨h,_⟩ | ⟨h,_⟩
  · exact Subtype.ext (stateCoordinates_inj_feasible U r c _ _ X.property.1 Y.property.1 h)
  · have hh := stateCoordinates_inj_feasible U r c _ _ X.property.1
      (repairedState U r c Y).property.1 h
    have ht : Transversal X.val := hh ▸ (repairedState U r c Y).property.2
    exact (defect_not_transversal U r c _ _ X.val (defectPair_spec U r c X) ht).elim

noncomputable def fullRepairEdges (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finset (Sym2 (SmallCoord U r c → ℤ)) := by
  classical
  exact Finset.univ.image (repairEdge U r c)

 

noncomputable def fullEdges (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finset (Sym2 (SmallCoord U r c → ℤ)) := by
  classical
  exact fullUnitEdges U r c ∪ fullRepairEdges U r c

noncomputable def graphEnergy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : ℝ :=
  ∑ e ∈ fullEdges U r c, edgeEnergy (smallKernel 0 U L C r c)
    (H ∘ coordinatesState U r c) e

theorem graphEnergy_nonneg (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : 0≤graphEnergy U L C r c H :=
  Finset.sum_nonneg fun e _ => edgeEnergy_nonneg _ _ (smallKernel_nonneg 0 (by positivity) U L C r c) e

theorem unitEnergy_le_graphEnergy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : fullUnitEnergy U L C r c H ≤ graphEnergy U L C r c H := by
  classical
  apply Finset.sum_le_sum_of_subset_of_nonneg
  · exact Finset.subset_union_left
  · intro e _ _
    exact edgeEnergy_nonneg _ _ (smallKernel_nonneg 0 (by positivity) U L C r c) e

theorem repairEdge_energy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) (X : DefectiveState U r c) :
    edgeEnergy (smallKernel 0 U L C r c) (H ∘ coordinatesState U r c) (repairEdge U r c X) =
      (weight U L r c X.val:ℝ)*(H X.val-H (repairedState U r c X).val)^2 := by
  change min (smallKernel 0 U L C r c (stateCoordinates U r c X.val))
    (smallKernel 0 U L C r c (stateCoordinates U r c (repairedState U r c X).val)) *
    (H (coordinatesState U r c (stateCoordinates U r c X.val))-
      H (coordinatesState U r c (stateCoordinates U r c (repairedState U r c X).val)))^2 = _
  rw [state_kernel_weight U L C r c hrc hC X.val X.property.1,
    state_kernel_weight U L C r c hrc hC _ (repairedState U r c X).property.1,
    coordinates_state_inverse U r c _ X.property.1.2.1,
    coordinates_state_inverse U r c _ (repairedState U r c X).property.1.2.1]
  rw [min_eq_left]
  exact_mod_cast weight_repair_le U L r c _ _ X.val (defectPair_spec U r c X)

theorem repairEnergy_le_graphEnergy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) :
    (∑ X : DefectiveState U r c,
      (weight U L r c X.val:ℝ)*(H X.val-H (repairedState U r c X).val)^2) ≤
        graphEnergy U L C r c H := by
  classical
  simp_rw [← repairEdge_energy U L C r c hrc hC H]
  rw [← Finset.sum_image (fun X _ Y _ he => repairEdge_injective U r c he)]
  apply Finset.sum_le_sum_of_subset_of_nonneg
  · exact Finset.subset_union_right
  · intro e _ _
    exact edgeEnergy_nonneg _ _ (smallKernel_nonneg 0 (by positivity) U L C r c) e

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

def graphUnderlying (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    GraphState U r c → State m n := Sum.elim Subtype.val Subtype.val

theorem graphUnderlying_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Function.Injective (graphUnderlying U r c) := by
  intro X Y h
  cases X with
  | inl X =>
    cases Y with
    | inl Y => exact congrArg Sum.inl (Subtype.ext h)
    | inr Y =>
      change X.val=Y.val at h
      have ht : Transversal Y.val := h ▸ X.property.2
      exact (defect_not_transversal U r c _ _ Y.val (defectPair_spec U r c Y) ht).elim
  | inr X =>
    cases Y with
    | inl Y =>
      change X.val=Y.val at h
      have ht : Transversal X.val := h.symm ▸ Y.property.2
      exact (defect_not_transversal U r c _ _ X.val (defectPair_spec U r c X) ht).elim
    | inr Y => exact congrArg Sum.inr (Subtype.ext h)

noncomputable def graphVariance (U L : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : ℝ :=
  weightedVariance (fun X : GraphState U r c => (weight U L r c (graphUnderlying U r c X):ℝ))
    (H ∘ graphUnderlying U r c)

 

theorem graph_poincare_exact (U L C d : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hU : 2 ≤ U) (hC2 : 2 ≤ C) (hd : 1 ≤ d) (hmn : m*n≤d)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) :
    graphVariance U L r c H ≤ ((1+2*(d:ℝ)^2)*4*(d:ℝ)^2*((U:ℝ)+1)^6+2)*
      graphEnergy U L C r c H := by
  let P := profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0
  let wP := fun p => (weight U L r c (P p):ℝ)
  let hP := H ∘ P
  have hs : Fintype.card (SmallSlot U r c)≤d := by
    have hh := Fintype.card_le_of_injective (smallRank U r c) (smallRank_injective U r c)
    simp only [Fintype.card_fin] at hh
    omega
  have hv := concrete_transversal_variance U L C d r c hU hC2 hd hs hrc hC H
  change weightedVariance wP hP ≤ _ at hv
  have he := unitEnergy_le_graphEnergy U L C r c H
  have hv' : weightedVariance wP hP ≤ 4*(d:ℝ)^2*((U:ℝ)+1)^6*graphEnergy U L C r c H :=
    hv.trans (mul_le_mul_of_nonneg_left he (by positivity))
  have hb := variance_repair_extension
    (fun X : TransverseState U r c => (weight U L r c X.val:ℝ)) (fun X => H X.val)
    (fun X : DefectiveState U r c => (weight U L r c X.val:ℝ)) (fun X => H X.val)
    wP hP (fun X => by positivity) (fun X => by positivity) (fun p => by positivity)
    (transverseProfile U r c) (transverseProfile_injective U r c)
    (fun X => by simp [wP,P]) (fun X => by simp [hP,P])
    (taggedRepair U r c) (taggedRepair_injective U r c)
    (fun X => by
      dsimp [wP,P,taggedRepair]
      rw [transverseProfile_state]
      exact_mod_cast weight_repair_le U L r c _ _ X.val (defectPair_spec U r c X))
    (graphEnergy U L C r c H) (by
      simpa only [hP,P,taggedRepair,Function.comp_apply,transverseProfile_state] using
        repairEnergy_le_graphEnergy U L C r c hrc hC H)
  have heq : graphVariance U L r c H = weightedVariance
      (Sum.elim (fun X : TransverseState U r c => (weight U L r c X.val:ℝ))
        (fun X : DefectiveState U r c => (weight U L r c X.val:ℝ)))
      (Sum.elim (fun X => H X.val) (fun X => H X.val)) := by
    unfold graphVariance
    congr 1 <;> funext X <;> cases X <;> rfl
  rw [← heq] at hb
  have hc : (Fintype.card (Cell m n × Cell m n):ℝ) ≤ (d:ℝ)^2 := by
    simp only [Fintype.card_prod,Fintype.card_fin]
    exact_mod_cast (show m*n*(m*n)≤d^2 by
      simpa only [pow_two] using Nat.mul_self_le_mul_self hmn)
  have hn := variance_nonneg wP hP (fun p => by positivity)
  have hm := mul_le_mul_of_nonneg_right hc hn
  have hv'' := mul_le_mul_of_nonneg_left hv' (show 0≤1+2*(d:ℝ)^2 by positivity)
  nlinarith only [hb,hm,hv'']

theorem poincare_constant_le (d U : ℕ) (hd : 1≤d) (hU : 1≤U) :
    (1+2*(d:ℝ)^2)*4*(d:ℝ)^2*((U:ℝ)+1)^6+2 ≤ 770*(d:ℝ)^4*(U:ℝ)^6 := by
  have hd' : (1:ℝ)≤d := by exact_mod_cast hd
  have hU' : (1:ℝ)≤U := by exact_mod_cast hU
  have hd2 : (1:ℝ)≤(d:ℝ)^2 := one_le_pow₀ hd'
  have hd4 : (1:ℝ)≤(d:ℝ)^4 := one_le_pow₀ hd'
  have hU6 : (1:ℝ)≤(U:ℝ)^6 := one_le_pow₀ hU'
  have hu : ((U:ℝ)+1)^6≤64*(U:ℝ)^6 := by
    calc
      _ ≤ (2*(U:ℝ))^6 := by gcongr; linarith
      _ = _ := by ring
  calc
    _ ≤ (3*(d:ℝ)^2)*4*(d:ℝ)^2*(64*(U:ℝ)^6)+2 := by
      gcongr
      nlinarith only [hd2]
    _ ≤ _ := by nlinarith [mul_le_mul hd4 hU6 (by positivity) (by positivity)]

 
theorem graph_poincare (U L C d : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hU : 2≤U) (hC2 : 2≤C) (hd : 1≤d) (hmn : m*n≤d)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i≤C)
    (H : State m n → ℝ) :
    graphVariance U L r c H ≤ 770*(d:ℝ)^4*(U:ℝ)^6*graphEnergy U L C r c H :=
  (graph_poincare_exact U L C d r c hU hC2 hd hmn hrc hC H).trans
    (mul_le_mul_of_nonneg_right (poincare_constant_le d U hd (by omega))
      (graphEnergy_nonneg U L C r c H))

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
noncomputable section
variable {m n : ℕ} (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)

def graphCoordinates (X : GraphState U r c) : SmallCoord U r c → ℤ :=
  stateCoordinates U r c (graphUnderlying U r c X)

theorem graph_feasible (X : GraphState U r c) : Feasible U r c (graphUnderlying U r c X) := by
  cases X with
  | inl X => exact X.property.1
  | inr X => exact X.property.1

theorem graphCoordinates_injective : Function.Injective (graphCoordinates U r c) := by
  intro X Y h
  apply graphUnderlying_injective U r c
  exact stateCoordinates_inj_feasible U r c _ _ (graph_feasible U r c X) (graph_feasible U r c Y) h

def GraphAdj (X Y : GraphState U r c) : Prop :=
  s(graphCoordinates U r c X,graphCoordinates U r c Y)∈fullEdges U r c

noncomputable instance graphAdjDecidable : DecidableRel (GraphAdj U r c) := fun _ _ => Classical.propDecidable _

theorem GraphAdj.symm {X Y : GraphState U r c} (h : GraphAdj U r c X Y) : GraphAdj U r c Y X := by
  simpa only [GraphAdj,Sym2.eq_swap] using h

theorem GraphAdj.irrefl (X : GraphState U r c) : ¬GraphAdj U r c X X := by
  classical
  intro hh
  rcases Finset.mem_union.mp hh with hu | hr
  · obtain ⟨_,D,i,j,hij,he⟩ := Finset.mem_filter.mp hu
    have he' : removeOne D i=removeOne D j := by
      rcases Sym2.eq_iff.mp he with ⟨h1,h2⟩ | ⟨h1,h2⟩
      · exact h1.symm.trans h2
      · exact h2.symm.trans h1
    have h := congrFun he' i
    simp [removeOne,hij] at h
  · obtain ⟨Y,_,hy⟩ := Finset.mem_image.mp hr
    have he' : stateCoordinates U r c Y.val=stateCoordinates U r c (repairedState U r c Y).val := by
      rcases Sym2.eq_iff.mp hy with ⟨h1,h2⟩ | ⟨h1,h2⟩ <;> exact h1.trans h2.symm
    have h := stateCoordinates_inj_feasible U r c _ _ Y.property.1 (repairedState U r c Y).property.1 he'
    exact defect_not_transversal U r c _ _ Y.val (defectPair_spec U r c Y) (h ▸ (repairedState U r c Y).property.2)

def unitCandidates (X : GraphState U r c) : Finset (SmallCoord U r c → ℤ) := by
  classical
  exact Finset.univ.image (fun ij : SmallCoord U r c × SmallCoord U r c =>
    fun w => graphCoordinates U r c X w + (if w=ij.1 then 1 else 0) - (if w=ij.2 then 1 else 0))

def outgoing (X : GraphState U r c) : Finset (GraphState U r c) := by
  classical
  exact match X with | .inl _ => ∅ | .inr Y => {Sum.inl (repairedState U r c Y)}

def incomingDefects (X : GraphState U r c) : Finset (DefectiveState U r c) := by
  classical
  exact Finset.univ.filter (fun Y => Sum.inl (repairedState U r c Y)=X)

theorem incoming_card (X : GraphState U r c) : (incomingDefects U r c X).card≤(m*n)^2 := by
  classical
  have hi : Set.InjOn (defectPair U r c) (incomingDefects U r c X : Set _) := by
    intro Y hY Z hZ h
    apply taggedRepair_injective U r c
    apply Prod.ext h
    have hY' := (Finset.mem_filter.mp hY).2
    have hZ' := (Finset.mem_filter.mp hZ).2
    have he := Sum.inl.inj (hY'.trans hZ'.symm)
    exact congrArg (transverseProfile U r c) he
  have hh := Finset.card_le_card_of_injOn (defectPair U r c) (s:=incomingDefects U r c X)
    (t:=Finset.univ) (fun _ _ => Finset.mem_univ _) hi
  simpa [Cell,Fintype.card_prod,pow_two] using hh

theorem neighbors_cover (X Y : GraphState U r c) (h : GraphAdj U r c X Y) :
    graphCoordinates U r c Y∈unitCandidates U r c X ∨
    Y∈outgoing U r c X ∨ Y∈(incomingDefects U r c X).image Sum.inr := by
  classical
  rcases Finset.mem_union.mp h with hu | hr
  · left
    obtain ⟨_,D,i,j,hij,he⟩ := Finset.mem_filter.mp hu
    rcases Sym2.eq_iff.mp he with ⟨hx,hy⟩ | ⟨hx,hy⟩
    · apply Finset.mem_image.mpr
      refine ⟨(i,j),Finset.mem_univ _,?_⟩
      funext w
      rw [hx,hy]
      simp only [removeOne]
      ring
    · apply Finset.mem_image.mpr
      refine ⟨(j,i),Finset.mem_univ _,?_⟩
      funext w
      rw [hx,hy]
      simp only [removeOne]
      ring
  · obtain ⟨Z,_,he⟩ := Finset.mem_image.mp hr
    rcases Sym2.eq_iff.mp he with ⟨hx,hy⟩ | ⟨hx,hy⟩
    · have hx' : Sum.inr Z=X := graphCoordinates_injective U r c hx
      have hy' : Sum.inl (repairedState U r c Z)=Y := graphCoordinates_injective U r c hy
      right; left
      rw [← hx',← hy']; simp [outgoing]
    · have hx' : Sum.inr Z=Y := graphCoordinates_injective U r c hx
      have hy' : Sum.inl (repairedState U r c Z)=X := graphCoordinates_injective U r c hy
      right; right
      exact Finset.mem_image.mpr ⟨Z,Finset.mem_filter.mpr ⟨Finset.mem_univ _,hy'⟩,hx'⟩

 

theorem graph_degree (d : ℕ) (hmn : m*n≤d) (X : GraphState U r c) :
    (Finset.univ.filter (GraphAdj U r c X)).card≤5*d^2+1 := by
  classical
  let A := Finset.univ.filter (fun Y => graphCoordinates U r c Y∈unitCandidates U r c X)
  let B := outgoing U r c X
  let C : Finset (GraphState U r c) := (incomingDefects U r c X).image Sum.inr
  have hsub : Finset.univ.filter (GraphAdj U r c X) ⊆ A ∪ B ∪ C := by
    intro Y hY
    rcases neighbors_cover U r c X Y (Finset.mem_filter.mp hY).2 with h | h | h
    · exact Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_filter.mpr ⟨Finset.mem_univ _,h⟩))
    · exact Finset.mem_union_left _ (Finset.mem_union_right _ h)
    · exact Finset.mem_union_right _ h
  have ha : A.card≤4*d^2 := by
    have hh := Finset.card_le_card_of_injOn (graphCoordinates U r c)
      (s:=A) (t:=unitCandidates U r c X) (fun _ h => (Finset.mem_filter.mp h).2)
      (fun _ _ _ _ h => graphCoordinates_injective U r c h)
    have hc : (unitCandidates U r c X).card≤(Fintype.card (SmallCoord U r c))^2 := by
      simpa [unitCandidates,Fintype.card_prod,pow_two] using (Finset.card_image_le
        (s:=(Finset.univ : Finset (SmallCoord U r c × SmallCoord U r c)))
        (f:=fun ij => fun w => graphCoordinates U r c X w+(if w=ij.1 then (1:ℤ) else 0)-(if w=ij.2 then 1 else 0)))
    have hs : Fintype.card (SmallSlot U r c)≤d := by
      have hh := Fintype.card_le_of_injective (smallRank U r c) (smallRank_injective U r c)
      simp only [Fintype.card_fin] at hh
      omega
    have hc' : Fintype.card (SmallCoord U r c)≤2*d := by
      simp only [SmallCoord,Fintype.card_prod,Fintype.card_bool]
      omega
    exact hh.trans (hc.trans (by nlinarith))
  have hb : B.card≤1 := by cases X <;> simp [B,outgoing]
  have hc : C.card≤d^2 := by
    exact Finset.card_image_le.trans ((incoming_card U r c X).trans (Nat.pow_le_pow_left hmn 2))
  exact (Finset.card_le_card hsub).trans ((Finset.card_union_le _ _).trans (by
    have hh := Finset.card_union_le A B
    omega))

end
end Contingency.PaddedGraph

end

end OAI