File size: 33,215 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import Mathlib
import OAI.Probability.Contingency.ConcreteContexts

namespace OAI

/-! Conditional Transport. -/

noncomputable section

open scoped BigOperators
open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}
variable (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))

noncomputable def repairedChild (j : ℕ) (t : Fin a) : ℕ :=
  if (suffixSlot U r c k a h t.succ).val.1=(suffixSlot U r c k a h 0).val.1 then j+1 else j

abbrev FeasibleBranches (j : ℕ) (t : Fin a) :=
  {α : ∀ v, Fin (surplusCaps U a t v+1) // Feasible U r c (defectState U r c k a h σ j t α)}

noncomputable instance feasibleBranchesFintype (j : ℕ) (t : Fin a) :
    Fintype (FeasibleBranches U r c k a h σ j t) := by
  classical
  unfold FeasibleBranches
  infer_instance

noncomputable def repairedBranch (j : ℕ) (t : Fin a) (α : FeasibleBranches U r c k a h σ j t) :
    Fin a → Fin (U+1) :=
  stateSuffix U r c k a h
    (repair U r c (suffixSlot U r c k a h 0).val (suffixSlot U r c k a h t.succ).val
      (defectState U r c k a h σ j t α.val))
    (repair_feasible U r c _ _ _ α.property (defectState_defect U r c k a h σ j t α.val))

theorem repairedBranch_state (j : ℕ) (t : Fin a) (α : FeasibleBranches U r c k a h σ j t) :
    contextState U r c k a h σ (mixedDisplay a (fun _ => U)
      (childProfile U (repairedChild U r c k a h j t)) (repairedBranch U r c k a h σ j t α)) =
    repair U r c (suffixSlot U r c k a h 0).val (suffixSlot U r c k a h t.succ).val
      (defectState U r c k a h σ j t α.val) := by
  apply contextState_reconstruct
  · exact repair_transversal U r c _ _ _
  · intro v
    have hp := repair_prefix U r c _ _ _ (defectState_defect U r c k a h σ j t α.val)
      (suffix_before U r c k a h 0 t.succ (Fin.succ_pos t))
      (prefixSlot U r c k a h v).val (prefix_before_suffix U r c k a h v 0)
    exact hp.1.trans (contextState_prefix U r c k a h σ _ v).1
  · have hs := repair_special U r c _ _ _ (defectState_defect U r c k a h σ j t α.val)
      ((j:ℤ)+1) (by simp)
    simpa only [repairedChild, Nat.cast_ite,Nat.cast_add,Nat.cast_one,add_sub_cancel_right] using hs

theorem repairedBranch_injective (j : ℕ) (t : Fin a) :
    Function.Injective (repairedBranch U r c k a h σ j t) := by
  intro α β he
  apply Subtype.ext
  have hs := congrArg (fun v => contextState U r c k a h σ (mixedDisplay a (fun _ => U)
    (childProfile U (repairedChild U r c k a h j t)) v)) he
  rw [repairedBranch_state,repairedBranch_state] at hs
  have hx := repair_injective U r c _ _ _ _ (defectState_defect U r c k a h σ j t α.val)
    (defectState_defect U r c k a h σ j t β.val) hs
  exact mixedDisplay_injective a _ _ (contextState_injective U r c k a h σ hx)

 

theorem mixedBalance_repair (hrc : (∑ i, r i)=∑ j, c j)
    (hC : ∀ i, paddedRow U L r c i ≤ C) (j : ℕ) (t : Fin a) :
    mixedBalance a (surplusCaps U a t) (contextKernel 0 U L C r c k a h σ) (defectProfile U j) ≤
      nodeMass U a (contextKernel 0 U L C r c k a h σ) (repairedChild U r c k a h j t) := by
  classical
  let P : (∀ v, Fin (surplusCaps U a t v+1)) → Prop :=
    fun α => Feasible U r c (defectState U r c k a h σ j t α)
  let F := fun α => contextKernel 0 U L C r c k a h σ
    (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α)
  let G := fun β => contextKernel 0 U L C r c k a h σ
    (mixedDisplay a (fun _ => U) (childProfile U (repairedChild U r c k a h j t)) β)
  have h0 (α : ∀ v, Fin (surplusCaps U a t v+1)) (hα : ¬P α) : F α=0 :=
    smallKernel_zero_infeasible U L C r c _ hα
  have hsum : (∑ α : FeasibleBranches U r c k a h σ j t, F α.val) = ∑ α, F α := by
    have he := Fintype.sum_subtype_add_sum_subtype P F
    have hz : (∑ α : {α // ¬P α}, F α.val)=0 :=
      Finset.sum_eq_zero fun α _ => h0 α.val α.property
    rw [hz,add_zero] at he
    exact he
  have hle (α : FeasibleBranches U r c k a h σ j t) :
      F α.val ≤ G (repairedBranch U r c k a h σ j t α) := by
    dsimp [F,G]
    rw [contextKernel_weight U L C r c hrc hC,contextKernel_weight U L C r c hrc hC,
      repairedBranch_state]
    exact_mod_cast weight_repair_le U L r c _ _ _ (defectState_defect U r c k a h σ j t α.val)
  have hg (β : Fin a → Fin (U+1)) : 0 ≤ G β := contextKernel_nonneg U L C r c k a h σ _
  change (∑ α, F α) ≤ _
  rw [← hsum]
  change _ ≤ allBalance U a (contextKernel 0 U L C r c k a h σ)
    (childProfile U (repairedChild U r c k a h j t))
  rw [allBalance_mixed]
  change _ ≤ ∑ β, G β
  calc
    _ ≤ ∑ α : FeasibleBranches U r c k a h σ j t, G (repairedBranch U r c k a h σ j t α) :=
      Finset.sum_le_sum fun α _ => hle α
    _ = ∑ β ∈ Finset.univ.image (repairedBranch U r c k a h σ j t), G β := by
      rw [Finset.sum_image (fun x _ y _ he => repairedBranch_injective U r c k a h σ j t he)]
    _ ≤ ∑ β, G β := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _)
      (fun β _ _ => hg β)

theorem mixedBalance_repair_max (hrc : (∑ i, r i)=∑ j, c j)
    (hC : ∀ i, paddedRow U L r c i ≤ C) (j : ℕ) (t : Fin a) :
    mixedBalance a (surplusCaps U a t) (contextKernel 0 U L C r c k a h σ) (defectProfile U j) ≤
      max (nodeMass U a (contextKernel 0 U L C r c k a h σ) j)
        (nodeMass U a (contextKernel 0 U L C r c k a h σ) (j+1)) := by
  have hh := mixedBalance_repair U L C r c k a h σ hrc hC j t
  unfold repairedChild at hh
  split_ifs at hh
  · exact hh.trans (le_max_right _ _)
  · exact hh.trans (le_max_left _ _)

theorem context_auxiliary_bound (hrc : (∑ i, r i)=∑ j, c j)
    (hC : ∀ i, paddedRow U L r c i ≤ C) (j : ℕ) :
    auxiliaryMass a U (contextKernel 0 U L C r c k a h σ) (adjacentDisplay U j) ≤
      (a:ℝ)*max (nodeMass U a (contextKernel 0 U L C r c k a h σ) j)
        (nodeMass U a (contextKernel 0 U L C r c k a h σ) (j+1)) := by
  rw [auxiliaryMass,adjacent_removePair,allSurplus_sum_types]
  calc
    _ ≤ ∑ t : Fin a, max (nodeMass U a (contextKernel 0 U L C r c k a h σ) j)
        (nodeMass U a (contextKernel 0 U L C r c k a h σ) (j+1)) :=
      Finset.sum_le_sum fun t _ => mixedBalance_repair_max U L C r c k a h σ hrc hC j t
    _ = _ := by simp

theorem concrete_conditional_transport (hU : 2 ≤ U) (hC2 : 2 ≤ C)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : (PairCoords Bool a → ℤ) → ℝ) (j : ℕ) (hj : j<U) :
    min (nodeMass U a (contextKernel 0 U L C r c k a h σ) j)
      (nodeMass U a (contextKernel 0 U L C r c k a h σ) (j+1)) *
      (nodeMean U a (contextKernel 0 U L C r c k a h σ) H j-
        nodeMean U a (contextKernel 0 U L C r c k a h σ) H (j+1))^2 ≤
      (2+2*(a:ℝ)*((U:ℝ)+1)^2) * ∑ e ∈ smallUnitEdges U a,
        edgeEnergy (contextKernel 0 U L C r c k a h σ) H e :=
  node_adjacent_bound U hU a _ (contextKernel_continuous U L C r c k a h σ)
    (fun η hη hη' => (contextKernel_signature η hη hη' U L C hC2 r c k a h σ).1)
    (fun η hη hη' => (contextKernel_signature η hη hη' U L C hC2 r c k a h σ).2)
    (contextKernel_nonneg U L C r c k a h σ) H a (by positivity) j hj
    (context_auxiliary_bound U L C r c k a h σ hrc hC j)

 

theorem concrete_node_variance (hU : 2 ≤ U) (hC2 : 2 ≤ C)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : (PairCoords Bool a → ℤ) → ℝ) :
    weightedVariance (fun j : Fin (U+1) => nodeMass U a (contextKernel 0 U L C r c k a h σ) j)
      (fun j => nodeMean U a (contextKernel 0 U L C r c k a h σ) H j) ≤
      ((U:ℝ)+1)^4*(2+2*(a:ℝ)*((U:ℝ)+1)^2) * ∑ e ∈ smallUnitEdges U a,
        edgeEnergy (contextKernel 0 U L C r c k a h σ) H e := by
  apply node_variance_bound
  · intro j hj
    exact allBalance_nonneg U a _ (contextKernel_nonneg U L C r c k a h σ) _
  · positivity
  · exact Finset.sum_nonneg fun e _ =>
      edgeEnergy_nonneg _ H (contextKernel_nonneg U L C r c k a h σ) e
  · exact context_child_no_holes U L C hU hC2 r c k a h σ
  · exact concrete_conditional_transport U L C r c k a h σ hU hC2 hrc hC H

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency

noncomputable def profileEquiv (C : ℕ) : (n : ℕ) → Profiles C n ≃ (Fin n → Fin (C+1))
  | 0 => { toFun := fun _ i => Fin.elim0 i
           invFun := fun _ => PUnit.unit
           left_inv := fun x => by cases x; rfl
           right_inv := fun x => by funext i; exact Fin.elim0 i }
  | n+1 => ((Equiv.refl (Fin (C+1))).prodCongr (profileEquiv C n)).trans
      (piHeadEquiv (fun _ : Fin (n+1) => Fin (C+1))).symm

@[simp] theorem profileEquiv_succ (C n : ℕ) (j : Fin (C+1)) (α : Profiles C n) :
    profileEquiv C (n+1) (j,α) = Fin.cons j (profileEquiv C n α) := rfl

namespace PaddedGraph
variable {m n : ℕ}

noncomputable def tupleState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (α : SmallSlot U r c → Fin (U+1)) : State m n :=
  coordinatesState U r c (fun w => if w.2 then (α w.1:ℕ) else (U:ℤ)-(α w.1:ℕ))

@[simp] theorem tupleState_x (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (α : SmallSlot U r c → Fin (U+1)) (s : SmallSlot U r c) :
    (tupleState U r c α).x s.val=(α s:ℕ) := by simp [tupleState]

@[simp] theorem tupleState_q (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (α : SmallSlot U r c → Fin (U+1)) (s : SmallSlot U r c) :
    (tupleState U r c α).q s.val=(α s:ℕ) := by simp [tupleState]

noncomputable def appendAssignment (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+a)
    (σ : Fin k → Fin (U+1)) (τ : Fin a → Fin (U+1)) : SmallSlot U r c → Fin (U+1) :=
  Fin.append σ τ ∘ finCongr h ∘ (smallIndex U r c).symm

@[simp] theorem appendAssignment_prefix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (τ : Fin (a+1) → Fin (U+1)) (s : Fin k) :
    appendAssignment U r c k (a+1) h σ τ (prefixSlot U r c k a h s)=σ s := by
  simp only [appendAssignment,Function.comp_apply,prefixSlot,splitSlots,Equiv.symm_trans_apply,
    Equiv.symm_symm,Equiv.symm_apply_apply,Equiv.apply_symm_apply]
  exact Fin.append_left σ τ s

@[simp] theorem appendAssignment_suffix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (τ : Fin (a+1) → Fin (U+1)) (s : Fin (a+1)) :
    appendAssignment U r c k (a+1) h σ τ (suffixSlot U r c k a h s)=τ s := by
  simp only [appendAssignment,Function.comp_apply,suffixSlot,splitSlots,Equiv.symm_trans_apply,
    Equiv.symm_symm,Equiv.symm_apply_apply,Equiv.apply_symm_apply]
  exact Fin.append_right σ τ s

@[simp] theorem appendAssignment_cons (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (j : Fin (U+1)) (τ : Fin a → Fin (U+1)) :
    appendAssignment U r c k (a+1) h σ (Fin.cons j τ) =
      appendAssignment U r c (k+1) a (by omega) (Fin.snoc σ j) τ := by
  funext s
  simp only [appendAssignment,Function.comp_apply,Fin.append_right_cons]
  rfl

 

noncomputable def profileState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+a) (σ : Fin k → Fin (U+1))
    (τ : Profiles U a) : State m n :=
  tupleState U r c (appendAssignment U r c k a h σ (profileEquiv U a τ))

@[simp] theorem profileState_succ (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (j : Fin (U+1)) (τ : Profiles U a) :
    profileState U r c k (a+1) h σ (j,τ) =
      profileState U r c (k+1) a (by omega) (Fin.snoc σ j) τ := by
  simp only [profileState,profileEquiv_succ,appendAssignment_cons]

 

theorem contextState_profile (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (j : Fin (U+1)) (τ : Profiles U a) :
    contextState U r c k a h σ
      (mixedDisplay a (fun _ => U) (childProfile U j) (profileEquiv U a τ)) =
    profileState U r c k (a+1) h σ (j,τ) := by
  apply State.ext <;> funext w
  all_goals
    by_cases hw : IsSmall U r c w
    · rcases slot_cases U r c k a h ⟨w,hw⟩ with ⟨s,hs⟩ | ⟨s,hs⟩
      · have he : w = _ := congrArg Subtype.val hs
        subst w
        simp only [profileState,profileEquiv_succ,tupleState_x,tupleState_q,
          appendAssignment_prefix]
        first | exact (contextState_prefix U r c k a h σ _ s).1 |
          exact (contextState_prefix U r c k a h σ _ s).2
      · have he : w = _ := congrArg Subtype.val hs
        subst w
        simp only [profileState,profileEquiv_succ,tupleState_x,tupleState_q,
          appendAssignment_suffix]
        first
        | rw [(contextState_suffix U r c k a h σ _ s).1]
        | rw [(contextState_suffix U r c k a h σ _ s).2]
        all_goals
          refine Fin.cases ?_ (fun v => ?_) s
          · simp [mixedDisplay_current,childProfile]
          · simp [mixedDisplay_ordinary]
    · have hl : IsLarge U r c w := Classical.not_not.mp hw
      have h1 := contextState_supported U r c k a h σ
        (mixedDisplay a (fun _ => U) (childProfile U j) (profileEquiv U a τ)) w hl
      have h2 := coordinatesState_supported U r c
        (fun w => if w.2 then
          (appendAssignment U r c k (a+1) h σ (profileEquiv U (a+1) (j,τ)) w.1:ℕ)
          else (U:ℤ)-(appendAssignment U r c k (a+1) h σ (profileEquiv U (a+1) (j,τ)) w.1:ℕ)) w hl
      first | exact h1.1.trans h2.1.symm | exact h1.2.trans h2.2.symm

theorem nodeMass_profile (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)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (j : Fin (U+1)) :
    nodeMass U a (contextKernel 0 U L C r c k a h σ) j =
      totalWeight (fun τ : Profiles U a =>
        (weight U L r c (profileState U r c k (a+1) h σ (j,τ)):ℝ)) := by
  rw [nodeMass,allBalance_mixed]
  unfold mixedBalance totalWeight
  rw [← Equiv.sum_comp (profileEquiv U a)]
  apply Finset.sum_congr rfl
  intro τ hτ
  rw [contextKernel_weight U L C r c hrc hC,contextState_profile]

theorem nodeMean_profile (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)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (H : State m n → ℝ) (j : Fin (U+1)) :
    nodeMean U a (contextKernel 0 U L C r c k a h σ)
      (H ∘ contextState U r c k a h σ) j =
    weightedMean (fun τ : Profiles U a =>
      (weight U L r c (profileState U r c k (a+1) h σ (j,τ)):ℝ))
      (fun τ => H (profileState U r c k (a+1) h σ (j,τ))) := by
  rw [nodeMean,nodeMass_profile U L C r c hrc hC]
  unfold weightedMean
  congr 1
  rw [nodeMass,allBalance_mixed]
  unfold mixedBalance
  rw [← Equiv.sum_comp (profileEquiv U a)]
  apply Finset.sum_congr rfl
  intro τ hτ
  simp only [Function.comp_apply,contextKernel_weight U L C r c hrc hC,contextState_profile]

end PaddedGraph
end Contingency

 

 

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

noncomputable def contextEnergy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (H : State m n → ℝ) : ℝ :=
  ∑ e ∈ smallUnitEdges U a, edgeEnergy (contextKernel 0 U L C r c k a h σ)
    (H ∘ contextState U r c k a h σ) e

theorem contextEnergy_nonneg (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (H : State m n → ℝ) : 0 ≤ contextEnergy U L C r c k a h σ H :=
  Finset.sum_nonneg fun e _ => edgeEnergy_nonneg _ _ (contextKernel_nonneg U L C r c k a h σ) e

noncomputable def contextTree (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : (a k : ℕ) →
      (h : Fintype.card (SmallSlot U r c)=k+a) → (Fin k → Fin (U+1)) → ExposureEnergies U a
  | 0, _, _, _ => PUnit.unit
  | a+1, k, h, σ => (contextEnergy U L C r c k a h σ H,
      fun j => contextTree U L C r c H a (k+1) (by omega) (Fin.snoc σ j))

theorem local_constant_le (U d a : ℕ) (_hd : 1 ≤ d) (ha : a<d) :
    ((U:ℝ)+1)^4*(2+2*(a:ℝ)*((U:ℝ)+1)^2) ≤ 4*(d:ℝ)*((U:ℝ)+1)^6 := by
  have hU : 1 ≤ ((U:ℝ)+1)^2 := by nlinarith [show 0 ≤ (U:ℝ) by positivity]
  have had : (a:ℝ)+1 ≤ d := by exact_mod_cast ha
  have h1 : 2+2*(a:ℝ)*((U:ℝ)+1)^2 ≤ 4*(d:ℝ)*((U:ℝ)+1)^2 := by
    have ha0 : 0 ≤ (a:ℝ) := by positivity
    have hmul := mul_le_mul_of_nonneg_right had (show 0 ≤ ((U:ℝ)+1)^2 by positivity)
    nlinarith
  calc
    _ ≤ ((U:ℝ)+1)^4*(4*(d:ℝ)*((U:ℝ)+1)^2) := mul_le_mul_of_nonneg_left h1 (by positivity)
    _ = _ := by ring

theorem contextTree_childBounds (U L C d : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hU : 2 ≤ U) (hC2 : 2 ≤ C) (hd : 1 ≤ d) (hslots : Fintype.card (SmallSlot U r c) ≤ d)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) (a k : ℕ) (h : Fintype.card (SmallSlot U r c)=k+a)
    (σ : Fin k → Fin (U+1)) :
    ChildVarianceBounds U (4*(d:ℝ)*((U:ℝ)+1)^6) a
      (fun τ => (weight U L r c (profileState U r c k a h σ τ):ℝ))
      (fun τ => H (profileState U r c k a h σ τ))
      (contextTree U L C r c H a k h σ) := by
  induction a generalizing k with
  | zero => trivial
  | succ a ih =>
    constructor
    · change weightedVariance
        (fun j => totalWeight (fun τ : Profiles U a =>
          (weight U L r c (profileState U r c k (a+1) h σ (j,τ)):ℝ)))
        (fun j => weightedMean
          (fun τ : Profiles U a => (weight U L r c (profileState U r c k (a+1) h σ (j,τ)):ℝ))
          (fun τ => H (profileState U r c k (a+1) h σ (j,τ)))) ≤
            (4*(d:ℝ)*((U:ℝ)+1)^6)*contextEnergy U L C r c k a h σ H
      simp_rw [← nodeMass_profile U L C r c hrc hC, ← nodeMean_profile U L C r c hrc hC]
      have hb := concrete_node_variance U L C r c k a h σ hU hC2 hrc hC
        (H ∘ contextState U r c k a h σ)
      exact hb.trans (mul_le_mul_of_nonneg_right (local_constant_le U d a hd (by omega))
        (contextEnergy_nonneg U L C r c k a h σ H))
    · intro j
      simpa only [profileState_succ,contextTree] using ih (k+1) (by omega) (Fin.snoc σ j)

 

theorem concrete_exposure_variance (U L C d : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hU : 2 ≤ U) (hC2 : 2 ≤ C) (hd : 1 ≤ d) (hslots : Fintype.card (SmallSlot U r c) ≤ d)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) (a k : ℕ) (h : Fintype.card (SmallSlot U r c)=k+a)
    (σ : Fin k → Fin (U+1)) :
    weightedVariance
      (fun τ => (weight U L r c (profileState U r c k a h σ τ):ℝ))
      (fun τ => H (profileState U r c k a h σ τ)) ≤
      4*(d:ℝ)*((U:ℝ)+1)^6 * exposureEnergy U a (contextTree U L C r c H a k h σ) :=
  exposure_variance_bound U _ a _ _ (fun τ => by positivity) _
    (contextTree_childBounds U L C d r c hU hC2 hd hslots hrc hC H a k h σ)

end Contingency.PaddedGraph

 

 

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

noncomputable def fullOccupancy (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (p : SmallCoord U r c → ℤ) (s : SmallSlot U r c) : ℤ := p (s,true)+p (s,false)

def fullPattern (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (p : SmallCoord U r c → ℤ) : Prop :=
  (∀ s, fullOccupancy U r c p s=U) ∨
  ∃ s t, s≠t ∧ fullOccupancy U r c p s=(U:ℤ)-1 ∧ fullOccupancy U r c p t=(U:ℤ)+1 ∧
    ∀ v, v≠s → v≠t → fullOccupancy U r c p v=U

noncomputable def fullVertices (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finset (SmallCoord U r c → ℤ) := by
  classical
  exact (Fintype.piFinset (fun _ => Finset.Icc 0 (U:ℤ))).filter (fullPattern U r c)

theorem mem_fullVertices (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (p : SmallCoord U r c → ℤ) :
    p∈fullVertices U r c ↔ InBox (fun _ => (U:ℤ)) p ∧ fullPattern U r c p := by
  classical
  simp [fullVertices,Fintype.mem_piFinset,InBox]

 

noncomputable def fullUnitEdges (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Finset (Sym2 (SmallCoord U r c → ℤ)) := by
  classical
  exact (fullVertices U r c).sym2.filter (fun e => ∃ D i j, i≠j ∧
    e=s(removeOne D i,removeOne D j))

noncomputable def contextDisplay (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) : SmallCoord U r c → ℤ :=
  Sum.elim (balanceProfile (fun _ => U) σ) p ∘ contextCoordinates U r c k a h

@[simp] theorem contextDisplay_prefix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (s : Fin k) (b : Bool) :
    contextDisplay U r c k a h σ p (prefixSlot U r c k a h s,b) =
      if b then ((σ s:ℕ):ℤ) else (U:ℤ)-(σ s:ℕ) := by simp [contextDisplay,balanceProfile]

@[simp] theorem contextDisplay_suffix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (s : Fin (a+1)) (b : Bool) :
    contextDisplay U r c k a h σ p (suffixSlot U r c k a h s,b) =
      p ((smallCoords a).symm (s,b)) := by simp [contextDisplay]

theorem contextDisplay_joint_inj (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1))
    (σ τ : Fin k → Fin (U+1)) (p q : PairCoords Bool a → ℤ)
    (he : contextDisplay U r c k a h σ p=contextDisplay U r c k a h τ q) : σ=τ ∧ p=q := by
  constructor
  · funext s
    have hh := congrFun he (prefixSlot U r c k a h s,true)
    simp only [contextDisplay_prefix,ite_true] at hh
    apply Fin.ext
    exact_mod_cast hh
  · funext i
    have hh := congrFun he ((contextCoordinates U r c k a h).symm (.inr i))
    simpa [contextDisplay] using hh

theorem contextDisplay_box (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (hp : InBox (fun _ => (U:ℤ)) p) :
    InBox (fun _ => (U:ℤ)) (contextDisplay U r c k a h σ p) := by
  intro w
  obtain ⟨z,rfl⟩ := (contextCoordinates U r c k a h).symm.surjective w
  cases z with
  | inl z => simpa [contextDisplay] using balanceProfile_box U k σ z
  | inr z => simpa [contextDisplay] using hp z

theorem contextDisplay_pattern (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (hp : smallPattern U a p) :
    fullPattern U r c (contextDisplay U r c k a h σ p) := by
  have hpre (s : Fin k) : fullOccupancy U r c (contextDisplay U r c k a h σ p)
      (prefixSlot U r c k a h s)=U := by simp [fullOccupancy]
  have hsuf (s : Fin (a+1)) : fullOccupancy U r c (contextDisplay U r c k a h σ p)
      (suffixSlot U r c k a h s)=smallOccupancy a p s := by simp [fullOccupancy,smallOccupancy]
  rcases hp with ht | ⟨s,t,hst,hs,ht,hrest⟩
  · left
    intro w
    rcases slot_cases U r c k a h w with ⟨s,rfl⟩ | ⟨s,rfl⟩
    · exact hpre s
    · rw [hsuf,ht]
  · right
    refine ⟨suffixSlot U r c k a h s,suffixSlot U r c k a h t,?_,?_,?_,?_⟩
    · intro he
      have hh := congrArg Subtype.val he
      exact hst ((suffixSlot_val_inj U r c k a h s t).mp hh)
    · rw [hsuf,hs]
    · rw [hsuf,ht]
    · intro w hws hwt
      rcases slot_cases U r c k a h w with ⟨v,rfl⟩ | ⟨v,rfl⟩
      · exact hpre v
      · rw [hsuf]
        exact hrest v (fun he => hws (congrArg _ he)) (fun he => hwt (congrArg _ he))

theorem contextDisplay_vertex (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (hp : p∈smallVertexSet U a) :
    contextDisplay U r c k a h σ p∈fullVertices U r c := by
  rw [mem_smallVertexSet] at hp
  exact (mem_fullVertices U r c _).mpr ⟨contextDisplay_box U r c k a h σ p hp.1,
    contextDisplay_pattern U r c k a h σ p hp.2⟩

theorem contextDisplay_removeOne (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (i : PairCoords Bool a) :
    contextDisplay U r c k a h σ (removeOne p i) =
      removeOne (contextDisplay U r c k a h σ p) ((contextCoordinates U r c k a h).symm (.inr i)) := by
  funext w
  obtain ⟨z,rfl⟩ := (contextCoordinates U r c k a h).symm.surjective w
  cases z with
  | inl z => simp [contextDisplay,removeOne]
  | inr z => simp [contextDisplay,removeOne]

noncomputable def contextEdge (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1))
    (e : (Fin k → Fin (U+1)) × Sym2 (PairCoords Bool a → ℤ)) : Sym2 (SmallCoord U r c → ℤ) :=
  Sym2.map (contextDisplay U r c k a h e.1) e.2

theorem contextEdge_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) :
    Function.Injective (contextEdge U r c k a h) := by
  rintro ⟨σ,e⟩ ⟨τ,f⟩ he
  refine Sym2.inductionOn₂ e f (fun x y z w => ?_) he
  intro he
  change s(contextDisplay U r c k a h σ x,contextDisplay U r c k a h σ y)=
    s(contextDisplay U r c k a h τ z,contextDisplay U r c k a h τ w) at he
  rcases Sym2.eq_iff.mp he with ⟨hx,hy⟩ | ⟨hx,hy⟩
  · obtain ⟨rfl,rfl⟩ := contextDisplay_joint_inj U r c k a h σ τ x z hx
    obtain ⟨_,rfl⟩ := contextDisplay_joint_inj U r c k a h σ σ y w hy
    rfl
  · obtain ⟨rfl,rfl⟩ := contextDisplay_joint_inj U r c k a h σ τ x w hx
    obtain ⟨_,rfl⟩ := contextDisplay_joint_inj U r c k a h σ σ y z hy
    exact Prod.ext rfl Sym2.eq_swap

theorem contextEdge_mem (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (e : Sym2 (PairCoords Bool a → ℤ)) (he : e∈smallUnitEdges U a) :
    contextEdge U r c k a h (σ,e)∈fullUnitEdges U r c := by
  classical
  obtain ⟨hv,D,i,j,hij,rfl⟩ := Finset.mem_filter.mp he
  rw [Finset.mk_mem_sym2_iff] at hv
  apply Finset.mem_filter.mpr
  constructor
  · change s(contextDisplay U r c k a h σ (removeOne D i),
        contextDisplay U r c k a h σ (removeOne D j)) ∈ (fullVertices U r c).sym2
    exact Finset.mk_mem_sym2_iff.mpr ⟨contextDisplay_vertex U r c k a h σ _ hv.1,
      contextDisplay_vertex U r c k a h σ _ hv.2⟩
  · refine ⟨contextDisplay U r c k a h σ D,
      (contextCoordinates U r c k a h).symm (.inr i),
      (contextCoordinates U r c k a h).symm (.inr j),?_,?_⟩
    · intro he
      exact hij (Sum.inr.inj ((contextCoordinates U r c k a h).symm.injective he))
    · change s(contextDisplay U r c k a h σ (removeOne D i),
        contextDisplay U r c k a h σ (removeOne D j)) = _
      rw [contextDisplay_removeOne,contextDisplay_removeOne]

@[simp] theorem contextEdge_energy (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (H : State m n → ℝ) (e : Sym2 (PairCoords Bool a → ℤ)) :
    edgeEnergy (smallKernel 0 U L C r c) (H ∘ coordinatesState U r c)
      (contextEdge U r c k a h (σ,e)) =
    edgeEnergy (contextKernel 0 U L C r c k a h σ)
      (H ∘ contextState U r c k a h σ) e := by
  induction e using Sym2.ind with
  | _ x y => rfl

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

theorem fullUnitEnergy_nonneg (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) : 0 ≤ fullUnitEnergy 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 contextEnergy_sum_le (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (H : State m n → ℝ) :
    (∑ σ : Fin k → Fin (U+1), contextEnergy U L C r c k a h σ H) ≤ fullUnitEnergy U L C r c H := by
  classical
  let E := edgeEnergy (smallKernel 0 U L C r c) (H ∘ coordinatesState U r c)
  have hh : (∑ σ : Fin k → Fin (U+1), contextEnergy U L C r c k a h σ H) =
      ∑ q ∈ Finset.univ ×ˢ smallUnitEdges U a, E (contextEdge U r c k a h q) := by
    rw [Finset.sum_product]
    simp only [E,contextEdge_energy,contextEnergy]
  rw [hh]
  rw [← Finset.sum_image (fun x _ y _ he => contextEdge_injective U r c k a h he)]
  apply Finset.sum_le_sum_of_subset_of_nonneg
  · intro e he
    obtain ⟨⟨σ,e⟩,hq,rfl⟩ := Finset.mem_image.mp he
    exact contextEdge_mem U r c k a h σ e (Finset.mem_product.mp hq).2
  · intro e he hne
    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 : ℕ}

theorem sum_snoc (U k : ℕ) (F : (Fin (k+1) → Fin (U+1)) → ℝ) :
    (∑ σ : Fin k → Fin (U+1), ∑ j : Fin (U+1), F (Fin.snoc σ j)) = ∑ τ, F τ := by
  rw [Finset.sum_comm]
  have he := Equiv.sum_comp (Fin.snocEquiv (fun _ : Fin (k+1) => Fin (U+1))) F
  rw [Fintype.sum_prod_type] at he
  exact he

 

theorem contextTree_level_sum_le (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) (l a k : ℕ) (h : Fintype.card (SmallSlot U r c)=k+a) (hl : l<a) :
    (∑ σ : Fin k → Fin (U+1), exposureLevel U a (contextTree U L C r c H a k h σ) l) ≤
      fullUnitEnergy U L C r c H := by
  induction l generalizing a k with
  | zero =>
    cases a with
    | zero => omega
    | succ a => exact contextEnergy_sum_le U L C r c k a h H
  | succ l ih =>
    cases a with
    | zero => omega
    | succ a =>
      simp only [contextTree,exposureLevel]
      have h' : Fintype.card (SmallSlot U r c) = (k+1)+a := by omega
      change (∑ σ : Fin k → Fin (U+1), ∑ j : Fin (U+1),
        exposureLevel U a (contextTree U L C r c H a (k+1) h' (Fin.snoc σ j)) l) ≤ _
      rw [sum_snoc U k (fun τ => exposureLevel U a
        (contextTree U L C r c H a (k+1) h' τ) l)]
      exact ih a (k+1) h' (by omega)

theorem root_level_le (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (H : State m n → ℝ) (l : ℕ) (hl : l<Fintype.card (SmallSlot U r c)) :
    exposureLevel U (Fintype.card (SmallSlot U r c))
      (contextTree U L C r c H (Fintype.card (SmallSlot U r c)) 0 (by omega) Fin.elim0) l ≤
      fullUnitEnergy U L C r c H := by
  have hh := contextTree_level_sum_le U L C r c H l (Fintype.card (SmallSlot U r c)) 0 (by omega) hl
  simp only [Fintype.sum_unique] at hh
  convert hh using 1
  congr 2

 

theorem concrete_transversal_variance (U L C d : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hU : 2 ≤ U) (hC2 : 2 ≤ C) (hd : 1 ≤ d) (hslots : Fintype.card (SmallSlot U r c) ≤ d)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (H : State m n → ℝ) :
    weightedVariance
      (fun τ => (weight U L r c (profileState U r c 0 (Fintype.card (SmallSlot U r c))
        (by omega) Fin.elim0 τ):ℝ))
      (fun τ => H (profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0 τ)) ≤
      4*(d:ℝ)^2*((U:ℝ)+1)^6 * fullUnitEnergy U L C r c H := by
  have hb := exposure_variance_level_bound U (4*(d:ℝ)*((U:ℝ)+1)^6) (by positivity)
    (Fintype.card (SmallSlot U r c))
    (fun τ => (weight U L r c (profileState U r c 0 (Fintype.card (SmallSlot U r c))
      (by omega) Fin.elim0 τ):ℝ))
    (fun τ => H (profileState U r c 0 (Fintype.card (SmallSlot U r c)) (by omega) Fin.elim0 τ))
    (fun τ => by positivity) _
    (contextTree_childBounds U L C d r c hU hC2 hd hslots hrc hC H _ 0 (by omega) Fin.elim0)
    (fullUnitEnergy U L C r c H) (root_level_le U L C r c H)
  refine hb.trans ?_
  have hs : (Fintype.card (SmallSlot U r c):ℝ) ≤ d := by exact_mod_cast hslots
  have hm := mul_le_mul_of_nonneg_right hs
    (show 0 ≤ (4*(d:ℝ)*((U:ℝ)+1)^6)*fullUnitEnergy U L C r c H from
      mul_nonneg (by positivity) (fullUnitEnergy_nonneg U L C r c H))
  nlinarith only [hm]

end Contingency.PaddedGraph

end

end OAI