File size: 35,574 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Probability.SKFluctuations.FilledSquare

namespace OAI

/-! Feature Clock Response. -/

noncomputable section

open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter Set
open scoped Topology
open MeasureTheory ProbabilityTheory Filter Set
namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
variable {ΞΉ S : Type*} [Fintype ΞΉ] [Fintype S] [Nonempty S] {n : β„•}

noncomputable def featureClockResponse (w : S β†’ ℝ) (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ)
    (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ)) (steps : List (GaussianStep ΞΉ)) (m : ℝ) : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ :=
  linearClockDerivative L m (gaussianHierarchy w Ο† steps)
    (fun i => gaussianHierarchyMean w Ο† (fun Οƒ => Ο† Οƒ (L (Pi.single i 1))) steps)
    (fun i => gaussianHierarchyResponse w Ο† (fun Οƒ => Ο† Οƒ (L (Pi.single i 1))) (L (Pi.single i 1)) steps)

 theorem featureClockResponse_bound {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ)) {Q m : ℝ}
    (hQ : βˆ€ Οƒ, βˆ‘ i, (Ο† Οƒ (L (Pi.single i 1)))^2 = Q)
    (steps : List (GaussianStep ΞΉ)) (ha : OrderedSteps m steps) (t : ℝ) (x : ΞΉ β†’ ℝ) :
    |featureClockResponse w Ο† L steps m t x| ≀ Q/2 := by
  obtain ⟨C,hF⟩ := (gaussianHierarchy_regular hw Ο† steps ha 0).1
  have hi := (exponentialIncrement_linearGaussian L (Real.toNNReal t)).integrable_shift hF m x
  have hP := linearFeaturePenalty_smooth hw Ο† L steps ha 0
  have hp : 0 ≀ linearClockTest L m (gaussianHierarchy w Ο† steps) (linearFeaturePenalty w Ο† L m steps) t x :=
    shiftedMean_nonneg (fun y => (linearFeaturePenalty_bounds hw Ο† L hQ steps ha y).1) hi
  have hb : linearClockTest L m (gaussianHierarchy w Ο† steps) (linearFeaturePenalty w Ο† L m steps) t x ≀ Q := by
    have hh := shiftedMean_mono hP (BddSmooth.const 0 Q)
      (fun y => (le_abs_self _).trans (linearFeaturePenalty_abs_le hw Ο† L hQ steps ha y)) hi
    rwa [shiftedMean_const Q hi] at hh
  unfold featureClockResponse
  rw [linearClockDerivative_feature_trace hw Ο† L hQ steps ha t x]
  rw [abs_of_nonneg (mul_nonneg (by norm_num) (sub_nonneg.mpr hb))]
  linarith

 theorem parametric_featureClock {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ)) {Q m : ℝ}
    (hQ : βˆ€ Οƒ, βˆ‘ i, (Ο† Οƒ (L (Pi.single i 1)))^2 = Q)
    (steps : List (GaussianStep ΞΉ)) (ha : OrderedSteps m steps) (hm : 0 < m) (r : ℝ) :
    ParametricRegular 0 r (Q/2) (linearClockContinuation L m (gaussianHierarchy w Ο† steps))
      (featureClockResponse w Ο† L steps m) := by
  obtain ⟨C,hC⟩ := (gaussianHierarchy_regular hw Ο† steps ha 0).1
  obtain ⟨K,hK⟩ := parametric_linear_clock L hC
    (fun i => gaussianHierarchyMean_smooth hw Ο† _ steps ha 0)
    (fun i => gaussianHierarchyResponse_smooth hw Ο† _ _ steps ha 0)
    (fun y => (gaussianHierarchy_linear_feature_derivatives hw Ο† L steps ha y).1)
    (fun y => (gaussianHierarchy_linear_feature_derivatives hw Ο† L steps ha y).2) hm
  have hQ0 : 0 ≀ Q := by
    obtain βŸ¨ΟƒβŸ© := β€ΉNonempty Sβ€Ί
    rw [← hQ Οƒ]
    exact Finset.sum_nonneg fun i _ => sq_nonneg _
  exact ⟨by positivity,(hK r).spatial,(hK r).continuous,
    fun t _ x => featureClockResponse_bound hw Ο† L hQ steps ha t x,(hK r).derivative⟩

namespace ReplicaTree

 theorem clockResponse_smooth0 {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    (P : T.Assignment β†’ ℝ) (t : ℝ) : BddSmooth 0 (T.clockResponse w Ο† L m P t) := by
  have hF := gaussianHierarchy_regular hw Ο† steps ha 0
  have hi := (exponentialIncrement_linearGaussian L (Real.toNNReal t)).integrable_shift hF.1.choose_spec m
  exact (((T.powerTrace_smooth hw Ο† L ha P 0).shiftedMean hF hi).sub
    (((T.expect_smooth hw Ο† ha P 0).shiftedMean hF hi).mul
      ((T.powerTrace_smooth hw Ο† L ha (fun _ => 1) 0).shiftedMean hF hi))).const_mul (1/2)

 theorem parametric_clockTest {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    {P : T.Assignment β†’ ℝ} {C : ℝ} (D : Fin (n+1) β†’ ℝ)
    (hC : 0 ≀ C) (hD : βˆ€ i, 0 ≀ D i) (hP : βˆ€ Οƒ, |P Οƒ| ≀ C)
    (hB : βˆ€ i Οƒ, |Ο† Οƒ (L (Pi.single i 1))| ≀ D i) (r : ℝ) :
    ParametricTest 0 r (C+4*(T.leaves:ℝ)*(T.leaves+1:ℝ)*C*βˆ‘ i, (D i)^2)
      (linearClockTest L m (gaussianHierarchy w Ο† steps) (T.expect w Ο† P))
      (T.clockResponse w Ο† L m P) := by
  have hK : 0 ≀ 4*(T.leaves:ℝ)*(T.leaves+1:ℝ)*C*βˆ‘ i, (D i)^2 := by positivity
  refine ⟨by positivity,?_,fun t _ => (T.clockResponse_smooth0 hw Ο† L ha P t).continuous,?_,?_,?_⟩
  Β· intro t _
    have hF := gaussianHierarchy_regular hw Ο† steps ha 0
    exact ((T.expect_smooth hw Ο† ha P 0).shiftedMean hF
      ((exponentialIncrement_linearGaussian L (Real.toNNReal t)).integrable_shift hF.1.choose_spec m)).continuous
  Β· intro t _ x
    exact (T.clock_abs_le hw Ο† L ha hP t x).trans (le_add_of_nonneg_right hK)
  Β· intro t _ x
    exact (T.clockResponse_abs_le hw Ο† L ha D hC hD hP hB t x).trans (le_add_of_nonneg_left hC)
  Β· intro t ht x
    exact (T.hasDerivWithinAt_linearClockTest hw Ο† L ha P x ht.1.le).hasDerivAt (Ici_mem_nhds ht.1)

end ReplicaTree
end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators

 theorem finite_law_variation_dual {ΞΊ : Type*} [Fintype ΞΊ] {R : ΞΊ β†’ ℝ} {K : ℝ}
    (h : βˆ€ P : ΞΊ β†’ ℝ, (βˆ€ Οƒ, |P Οƒ| ≀ 1) β†’ |βˆ‘ Οƒ, P Οƒ*R Οƒ| ≀ K) :
    βˆ‘ Οƒ, |R Οƒ| ≀ K := by
  classical
  let P : ΞΊ β†’ ℝ := fun Οƒ => if 0 ≀ R Οƒ then 1 else -1
  have hP : βˆ€ Οƒ, |P Οƒ| ≀ 1 := by intro Οƒ; dsimp [P]; split_ifs <;> norm_num
  have he : βˆ‘ Οƒ, P Οƒ*R Οƒ = βˆ‘ Οƒ, |R Οƒ| := by
    apply Finset.sum_congr rfl
    intro Οƒ _
    dsimp [P]
    split_ifs with hh
    Β· rw [one_mul,abs_of_nonneg hh]
    Β· rw [neg_one_mul,abs_of_neg (lt_of_not_ge hh)]
  have hh := h P hP
  rw [he,abs_of_nonneg (Finset.sum_nonneg fun Οƒ _ => abs_nonneg _)] at hh
  exact hh

variable {ΞΉ S : Type*} [Fintype ΞΉ] [Fintype S] [Nonempty S] {n : β„•}
namespace ReplicaTree

 theorem clockTest_atomic {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    [DecidableEq T.Assignment] (P : T.Assignment β†’ ℝ) (t : ℝ) (x : ΞΉ β†’ ℝ) :
    linearClockTest L m (gaussianHierarchy w Ο† steps) (T.expect w Ο† P) t x =
      βˆ‘ Οƒ, P Οƒ*linearClockTest L m (gaussianHierarchy w Ο† steps)
        (T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0)) t x := by
  have hF := gaussianHierarchy_regular hw Ο† steps ha 0
  have hi := (exponentialIncrement_linearGaussian L (Real.toNNReal t)).integrable_shift hF.1.choose_spec m x
  have he : T.expect w Ο† P = fun y => βˆ‘ Οƒ, P Οƒ*T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0) y := by
    funext y
    simp_rw [T.expect_indicator]
    unfold expect
    apply Finset.sum_congr rfl
    intro Οƒ _
    ring
  change shiftedMean _ _ _ _ _ = _
  rw [he,shiftedMean_finsetSum Finset.univ
    (fun Οƒ _ => (T.expect_smooth hw Ο† ha (fun Ο„ => if Ο„ = Οƒ then 1 else 0) 0).const_mul (P Οƒ)) hi]
  simp only [shiftedMean_const_mul,linearClockTest]

 theorem clockResponse_atomic {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    [DecidableEq T.Assignment] (P : T.Assignment β†’ ℝ) {t : ℝ} (ht : 0 < t) (x : ΞΉ β†’ ℝ) :
    T.clockResponse w Ο† L m P t x =
      βˆ‘ Οƒ, P Οƒ*T.clockResponse w Ο† L m (fun Ο„ => if Ο„ = Οƒ then 1 else 0) t x := by
  have hd (P : T.Assignment β†’ ℝ) :=
    (T.hasDerivWithinAt_linearClockTest hw Ο† L ha P x ht.le).hasDerivAt (Ici_mem_nhds ht)
  have hs := HasDerivAt.fun_sum (u := Finset.univ) (fun Οƒ _ => (hd (fun Ο„ => if Ο„ = Οƒ then 1 else 0)).const_mul (P Οƒ))
  have hh : HasDerivAt (fun s => linearClockTest L m (gaussianHierarchy w Ο† steps) (T.expect w Ο† P) s x)
      (βˆ‘ Οƒ, P Οƒ*T.clockResponse w Ο† L m (fun Ο„ => if Ο„ = Οƒ then 1 else 0) t x) t := by
    have he : (fun s => linearClockTest L m (gaussianHierarchy w Ο† steps) (T.expect w Ο† P) s x) =
        (fun s => βˆ‘ Οƒ, P Οƒ*linearClockTest L m (gaussianHierarchy w Ο† steps)
          (T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0)) s x) :=
      funext fun s => T.clockTest_atomic hw Ο† L ha P s x
    rw [he]
    exact hs
  exact (hd P).unique hh

 theorem atomic_clockResponse_variation {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    [DecidableEq T.Assignment] (D : Fin (n+1) β†’ ℝ)
    (hD : βˆ€ i, 0 ≀ D i) (hB : βˆ€ i Οƒ, |Ο† Οƒ (L (Pi.single i 1))| ≀ D i)
    {t : ℝ} (ht : 0 < t) (x : ΞΉ β†’ ℝ) :
    βˆ‘ Οƒ, |T.clockResponse w Ο† L m (fun Ο„ => if Ο„ = Οƒ then 1 else 0) t x| ≀
      4*(T.leaves:ℝ)*(T.leaves+1:ℝ)*βˆ‘ i, (D i)^2 := by
  apply finite_law_variation_dual
  intro P hP
  rw [← T.clockResponse_atomic hw Ο† L ha P ht x]
  simpa only [mul_one] using T.clockResponse_abs_le hw Ο† L ha D (by norm_num : (0:ℝ) ≀ 1) hD hP hB t x

 theorem atomic_clockTest_probability {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (T : ReplicaTree ΞΉ S steps) {m : ℝ} (ha : OrderedSteps m steps)
    [DecidableEq T.Assignment] (t : ℝ) (x : ΞΉ β†’ ℝ) :
    (βˆ€ Οƒ, 0 ≀ linearClockTest L m (gaussianHierarchy w Ο† steps)
      (T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0)) t x) ∧
    (βˆ‘ Οƒ, linearClockTest L m (gaussianHierarchy w Ο† steps)
      (T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0)) t x) = 1 := by
  have hF := gaussianHierarchy_regular hw Ο† steps ha 0
  have hi := (exponentialIncrement_linearGaussian L (Real.toNNReal t)).integrable_shift hF.1.choose_spec m x
  refine ⟨fun Οƒ => ?_,?_⟩
  Β· apply shiftedMean_nonneg _ hi
    intro y
    rw [T.expect_indicator]
    exact T.weight_nonneg hw Ο† ha Οƒ y
  Β· have hh := T.clockTest_atomic hw Ο† L ha (fun _ => 1) t x
    simp only [one_mul] at hh
    rw [← hh]
    have he : T.expect w Ο† (fun _ => 1) = fun _ => 1 := funext fun y => T.expect_const hw Ο† ha 1 y
    unfold linearClockTest
    rw [he,shiftedMean_const 1 hi]

end ReplicaTree
end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
universe uB vB wB

 

inductive BoundaryTree (ΞΉ : Type uB) (Ξ² : Type vB) : List (GaussianStep ΞΉ) β†’ Type (max uB vB) where
  | leaf (b : Ξ²) : BoundaryTree ΞΉ Ξ² []
  | step (st : GaussianStep ΞΉ) {tail : List (GaussianStep ΞΉ)}
      (T : BoundaryTree ΞΉ Ξ² tail) : BoundaryTree ΞΉ Ξ² (st::tail)
  | fork {front : List (GaussianStep ΞΉ)} (T U : BoundaryTree ΞΉ Ξ² front) : BoundaryTree ΞΉ Ξ² front

variable {ΞΉ : Type uB} {Ξ² : Type vB} {ΞΊ : Ξ² β†’ Type wB}

@[reducible] def BoundaryTree.Assignment (ΞΊ : Ξ² β†’ Type wB) :
    {front : List (GaussianStep ΞΉ)} β†’ BoundaryTree ΞΉ Ξ² front β†’ Type wB
  | _, .leaf b => ΞΊ b
  | _, .step _ T => T.Assignment ΞΊ
  | _, .fork T U => T.Assignment ΞΊ Γ— U.Assignment ΞΊ

@[instance_reducible] noncomputable def BoundaryTree.assignmentFintype [βˆ€ b, Fintype (ΞΊ b)] :
    {front : List (GaussianStep ΞΉ)} β†’ (T : BoundaryTree ΞΉ Ξ² front) β†’ Fintype (T.Assignment ΞΊ)
  | _, .leaf b => inferInstanceAs (Fintype (ΞΊ b))
  | _, .step _ T => T.assignmentFintype
  | _, .fork T U => @instFintypeProd _ _ T.assignmentFintype U.assignmentFintype
attribute [instance] BoundaryTree.assignmentFintype

variable [Fintype ΞΉ]

noncomputable def BoundaryTree.weight (F : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ)
    (W : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ) :
    {front : List (GaussianStep ΞΉ)} β†’ (T : BoundaryTree ΞΉ Ξ² front) β†’ T.Assignment ΞΊ β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ
  | _, .leaf b, Οƒ => W b Οƒ
  | _, @BoundaryTree.step _ _ (m,q) tail T, Οƒ => fun t =>
      shiftedMean (Measure.pi fun i => gaussianReal 0 (q i)) m
        (prefixContinuation tail (F t)) (fun x => T.weight F W Οƒ t x)
  | _, .fork T U, Οƒ => fun t x => T.weight F W Οƒ.1 t x*U.weight F W Οƒ.2 t x

noncomputable def BoundaryTree.response (F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ)
    (W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ) :
    {front : List (GaussianStep ΞΉ)} β†’ (T : BoundaryTree ΞΉ Ξ² front) β†’ T.Assignment ΞΊ β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ
  | _, .leaf b, Οƒ => B b Οƒ
  | _, @BoundaryTree.step _ _ (m,q) tail T, Οƒ => fun t =>
      tiltedResponse q m (prefixContinuation tail (F t)) (prefixTest tail (F t) (D t))
        (fun x => T.weight F W Οƒ t x) (fun x => T.response F D W B Οƒ t x)
  | _, .fork T U, Οƒ => fun t x => T.response F D W B Οƒ.1 t x*U.weight F W Οƒ.2 t x+
      T.weight F W Οƒ.1 t x*U.response F D W B Οƒ.2 t x

 theorem BoundaryTree.parametric {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (Οƒ : T.Assignment ΞΊ) :
    βˆƒ K, ParametricTest l r K (T.weight F W Οƒ) (T.response F D W B Οƒ) := by
  induction T with
  | leaf b => exact hW b Οƒ
  | @step st tail T ih =>
    rcases st with ⟨m,q⟩
    have ht := fun s hs => hm s (List.mem_cons_of_mem _ hs)
    obtain ⟨K,hK⟩ := ih ht Οƒ
    exact ⟨_,hK.step (hF.prefix tail ht) q m⟩
  | fork T U ihT ihU =>
    obtain ⟨K,hK⟩ := ihT hm Οƒ.1
    obtain ⟨J,hJ⟩ := ihU hm Οƒ.2
    exact ⟨_,hK.mul hJ⟩

 theorem BoundaryTree.weight_smooth0 {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (Οƒ : T.Assignment ΞΊ) {t : ℝ} (ht : t ∈ Ioo l r) :
    BddSmooth 0 (fun x => T.weight F W Οƒ t x) :=
  (T.parametric hF hW hm Οƒ).choose_spec.smooth0 ht

 theorem BoundaryTree.response_smooth0 {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (Οƒ : T.Assignment ΞΊ) {t : ℝ} (ht : t ∈ Ioo l r) :
    BddSmooth 0 (fun x => T.response F D W B Οƒ t x) :=
  (T.parametric hF hW hm Οƒ).choose_spec.response_smooth0 ht

 theorem BoundaryTree.probability [βˆ€ b, Fintype (ΞΊ b)]
    {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ} {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    (hp : βˆ€ b, βˆ€ t ∈ Ioo l r, βˆ€ x, (βˆ€ Οƒ, 0 ≀ W b Οƒ t x) ∧ βˆ‘ Οƒ, W b Οƒ t x = 1)
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) {t : ℝ} (ht : t ∈ Ioo l r) (x : ΞΉ β†’ ℝ) :
    (βˆ€ Οƒ : T.Assignment ΞΊ, 0 ≀ T.weight F W Οƒ t x) ∧ βˆ‘ Οƒ, T.weight F W Οƒ t x = 1 := by
  induction T generalizing x with
  | leaf b => exact hp b t ht x
  | @step st tail T ih =>
    rcases st with ⟨m,q⟩
    have htail := fun s hs => hm s (List.mem_cons_of_mem _ hs)
    have hi := (exponentialIncrement_gaussian q).integrable_shift
      ((hF.prefix tail htail).spatial t ht).choose_spec m x
    refine ⟨fun Οƒ => shiftedMean_nonneg (fun y => (ih htail y).1 Οƒ) hi,?_⟩
    change βˆ‘ Οƒ, shiftedMean _ _ _ (fun y => T.weight F W Οƒ t y) x = _
    rw [← shiftedMean_finsetSum Finset.univ (fun Οƒ _ => T.weight_smooth0 hF hW htail Οƒ ht) hi]
    simp_rw [(ih htail _).2]
    exact shiftedMean_const 1 hi
  | @fork front T U ihT ihU =>
    obtain ⟨hT,sT⟩ := ihT hm x
    obtain ⟨hU,sU⟩ := ihU hm x
    refine ⟨fun Οƒ => mul_nonneg (hT Οƒ.1) (hU Οƒ.2),?_⟩
    change βˆ‘ Οƒ : T.Assignment ΞΊ Γ— U.Assignment ΞΊ, T.weight F W Οƒ.1 t x*U.weight F W Οƒ.2 t x = _
    simp only [Fintype.sum_prod_type,← Finset.mul_sum,sU,mul_one,sT]

end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators

 theorem BddSmooth.abs_zero {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]
    {A : E β†’ ℝ} (h : BddSmooth 0 A) : BddSmooth 0 (fun x => |A x|) := by
  obtain ⟨K,hK,hb⟩ := h.bound
  exact ⟨h.continuous.abs,K,hK,fun x => by simpa only [abs_abs] using hb x⟩

 theorem sum_abs_shiftedMean_le {E ΞΊ : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]
    [MeasurableSpace E] [BorelSpace E] {Ξ½ : Measure E} [IsProbabilityMeasure Ξ½] [Fintype ΞΊ]
    {F : E β†’ ℝ} {m K : ℝ} {A : ΞΊ β†’ E β†’ ℝ}
    (hA : βˆ€ i, BddSmooth 0 (A i)) (hb : βˆ€ x, βˆ‘ i, |A i x| ≀ K) {x : E}
    (hi : Integrable (fun z => Real.exp (m*F (x+z))) Ξ½) :
    βˆ‘ i, |shiftedMean Ξ½ m F (A i) x| ≀ K := by
  calc
    _ ≀ βˆ‘ i, shiftedMean Ξ½ m F (fun y => |A i y|) x :=
      Finset.sum_le_sum fun i _ => shiftedMean_abs_mono (hA i) (hA i).abs_zero (fun y => le_refl _) hi
    _ = shiftedMean Ξ½ m F (fun y => βˆ‘ i, |A i y|) x :=
      (shiftedMean_finsetSum Finset.univ (fun i _ => (hA i).abs_zero) hi).symm
    _ ≀ shiftedMean Ξ½ m F (fun _ => K) x := shiftedMean_mono
      (BddSmooth.sum Finset.univ fun i _ => (hA i).abs_zero) (BddSmooth.const 0 K) hb hi
    _ = K := shiftedMean_const K hi

 theorem finite_law_score_fork {κ ρ : Type*} [Fintype κ] [Fintype ρ]
    {W : ΞΊ β†’ ℝ} {V : ρ β†’ ℝ} {B : ΞΊ β†’ ℝ} {C : ρ β†’ ℝ} {a D K L M : ℝ}
    (hW : βˆ€ i, 0 ≀ W i) (hV : βˆ€ j, 0 ≀ V j)
    (sW : βˆ‘ i, W i = 1) (sV : βˆ‘ j, V j = 1)
    (hB : βˆ‘ i, |B i+a*W i*D| ≀ K) (hC : βˆ‘ j, |C j+a*V j*D| ≀ L)
    (ha : 0 ≀ a) (hD : |D| ≀ M) :
    βˆ‘ z : ΞΊ Γ— ρ, |B z.1*V z.2+W z.1*C z.2+a*(W z.1*V z.2)*D| ≀ K+L+a*M := by
  have he (i : κ) (j : ρ) :
      B i*V j+W i*C j+a*(W i*V j)*D =
        (B i+a*W i*D)*V j+W i*(C j+a*V j*D)-a*W i*V j*D := by ring
  calc
    _ ≀ βˆ‘ z : ΞΊ Γ— ρ, (|B z.1+a*W z.1*D| *V z.2+W z.1*|C z.2+a*V z.2*D|+a*W z.1*V z.2*M) := by
      apply Finset.sum_le_sum
      intro z _
      rw [he]
      apply (abs_sub _ _).trans
      have h := abs_add_le ((B z.1+a*W z.1*D)*V z.2) (W z.1*(C z.2+a*V z.2*D))
      simp only [abs_mul,abs_of_nonneg (hW z.1),abs_of_nonneg (hV z.2),abs_of_nonneg ha] at h ⊒
      have hh := mul_le_mul_of_nonneg_left hD (mul_nonneg (mul_nonneg ha (hW z.1)) (hV z.2))
      linarith
    _ = (βˆ‘ i, |B i+a*W i*D|)+(βˆ‘ j, |C j+a*V j*D|)+a*M := by
      simp only [Fintype.sum_prod_type,Finset.sum_add_distrib,← Finset.sum_mul,← Finset.mul_sum,sV,sW,mul_one,one_mul]
    _ ≀ K+L+a*M := by linarith

 theorem finite_law_subtraction_bound {ΞΊ : Type*} [Fintype ΞΊ]
    {W R : ΞΊ β†’ ℝ} {c D K J : ℝ} (hW : βˆ€ i, 0 ≀ W i) (sW : βˆ‘ i, W i = 1)
    (hR : βˆ‘ i, |R i| ≀ K) (hc : 0 ≀ c) (hD : |D| ≀ J) :
    βˆ‘ i, |R i-c*W i*D| ≀ K+c*J := by
  calc
    _ ≀ βˆ‘ i, (|R i|+c*W i*J) := by
      apply Finset.sum_le_sum
      intro i _
      apply (abs_sub _ _).trans
      rw [abs_mul,abs_mul,abs_of_nonneg hc,abs_of_nonneg (hW i)]
      exact add_le_add (le_refl _) (mul_le_mul_of_nonneg_left hD (mul_nonneg hc (hW i)))
    _ = (βˆ‘ i, |R i|)+c*J := by
      simp only [Finset.sum_add_distrib,← Finset.sum_mul,← Finset.mul_sum,sW,mul_one]
    _ ≀ K+c*J := add_le_add hR (le_refl _)

end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
universe uB vB wB
variable {ΞΉ : Type uB} {Ξ² : Type vB} {ΞΊ : Ξ² β†’ Type wB}

def PrefixOrdered (a b : ℝ) : List (GaussianStep ΞΉ) β†’ Prop
  | [] => 0 ≀ a ∧ a ≀ b
  | (m,_)::tail => 0 ≀ a ∧ a ≀ m ∧ 0 < m ∧ PrefixOrdered m b tail

 theorem PrefixOrdered.positive {a b : ℝ} {front : List (GaussianStep ΞΉ)}
    (h : PrefixOrdered a b front) : βˆ€ st ∈ front, 0 < st.1 := by
  induction front generalizing a with
  | nil => simp
  | cons st tail ih =>
    rcases st with ⟨m,q⟩
    intro s hs
    rcases List.mem_cons.mp hs with hs|hs
    Β· simpa only [hs] using h.2.2.1
    Β· exact ih h.2.2.2 s hs

 theorem PrefixOrdered.bounds {a b : ℝ} {front : List (GaussianStep ΞΉ)}
    (h : PrefixOrdered a b front) : 0 ≀ a ∧ a ≀ b := by
  induction front generalizing a with
  | nil => exact h
  | cons st tail ih =>
    rcases st with ⟨m,q⟩
    exact ⟨h.1,h.2.1.trans (ih h.2.2.2).2⟩

def BoundaryTree.budget (V : Ξ² β†’ ℝ) : {front : List (GaussianStep ΞΉ)} β†’ BoundaryTree ΞΉ Ξ² front β†’ ℝ
  | _, .leaf b => V b
  | _, .step _ T => T.budget V
  | _, .fork T U => T.budget V+U.budget V

variable [Fintype ΞΉ] [βˆ€ b, Fintype (ΞΊ b)]

noncomputable def BoundaryTree.score (F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ)
    (W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ) {front : List (GaussianStep ΞΉ)}
    (T : BoundaryTree ΞΉ Ξ² front) (a : ℝ) (Οƒ : T.Assignment ΞΊ) (t : ℝ) (x : ΞΉ β†’ ℝ) : ℝ :=
  T.response F D W B Οƒ t x+a*T.weight F W Οƒ t x*prefixTest front (F t) (D t) x

omit [βˆ€ b, Fintype (ΞΊ b)] in
 theorem BoundaryTree.score_smooth0 {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (a : ℝ) (Οƒ : T.Assignment ΞΊ) {t : ℝ} (ht : t ∈ Ioo l r) :
    BddSmooth 0 (T.score F D W B a Οƒ t) :=
  (T.response_smooth0 hF hW hm Οƒ ht).add
    (((T.weight_smooth0 hF hW hm Οƒ ht).const_mul a).mul ((hF.prefix front hm).response_smooth0 ht))

omit [βˆ€ b, Fintype (ΞΊ b)] in
 theorem BoundaryTree.score_step {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (a m : ℝ) (q : ΞΉ β†’ ℝβ‰₯0)
    (Οƒ : T.Assignment ΞΊ) {t : ℝ} (ht : t ∈ Ioo l r) (x : ΞΉ β†’ ℝ) :
    (T.step (m,q)).score F D W B a Οƒ t x =
      shiftedMean (Measure.pi fun i => gaussianReal 0 (q i)) m (prefixContinuation front (F t))
        (T.score F D W B m Οƒ t) x-
      (m-a)*(T.step (m,q)).weight F W Οƒ t x*prefixTest ((m,q)::front) (F t) (D t) x := by
  let Ξ½ := Measure.pi fun i => gaussianReal 0 (q i)
  have hi := (exponentialIncrement_gaussian q).integrable_shift
    ((hF.prefix front hm).spatial t ht).choose_spec m x
  have hB := T.response_smooth0 hF hW hm Οƒ ht
  have hA := T.weight_smooth0 hF hW hm Οƒ ht
  have hD := (hF.prefix front hm).response_smooth0 ht
  have he : (T.score F D W B m Οƒ t) = fun y =>
      T.response F D W B Οƒ t y+m*(T.weight F W Οƒ t y*prefixTest front (F t) (D t) y) := by
    funext y
    unfold score
    ring
  rw [he,shiftedMean_add hB ((hA.mul hD).const_mul m) hi,shiftedMean_const_mul]
  simp only [score,response,weight,tiltedResponse,prefixTest]
  ring

 

 theorem BoundaryTree.score_variation {l r J m a : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ} {V : Ξ² β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    (hp : βˆ€ b, βˆ€ t ∈ Ioo l r, βˆ€ x, (βˆ€ Οƒ, 0 ≀ W b Οƒ t x) ∧ βˆ‘ Οƒ, W b Οƒ t x = 1)
    (hv : βˆ€ b, βˆ€ t ∈ Ioo l r, βˆ€ x, βˆ‘ Οƒ, |B b Οƒ t x+m*W b Οƒ t x*D t x| ≀ V b)
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (ha : PrefixOrdered a m front) {t : ℝ} (ht : t ∈ Ioo l r) (x : ΞΉ β†’ ℝ) :
    βˆ‘ Οƒ, |T.score F D W B a Οƒ t x| ≀ T.budget (fun b => V b+m*J)-a*J := by
  induction T generalizing a x with
  | leaf b =>
    have hh := finite_law_subtraction_bound (hp b t ht x).1 (hp b t ht x).2 (hv b t ht x)
      (sub_nonneg.mpr ha.2) (hF.bounded t ht x)
    have he (Οƒ : ΞΊ b) : (B b Οƒ t x+m*W b Οƒ t x*D t x)-(m-a)*W b Οƒ t x*D t x =
        (BoundaryTree.leaf b : BoundaryTree ΞΉ Ξ² []).score F D W B a Οƒ t x := by
      simp only [score,response,weight,prefixTest]
      ring
    simp_rw [he] at hh
    dsimp only [budget]
    linarith
  | @step st tail T ih =>
    rcases st with ⟨b,q⟩
    let Ξ½ := Measure.pi fun i => gaussianReal 0 (q i)
    have hm := ha.positive
    have hb := ha.2.2.2.positive
    have hi := (exponentialIncrement_gaussian q).integrable_shift
      ((hF.prefix tail hb).spatial t ht).choose_spec b x
    have hs := sum_abs_shiftedMean_le (fun Οƒ => T.score_smooth0 hF hW hb b Οƒ ht)
      (fun y => ih ha.2.2.2 y) hi
    have hp' := (T.step (b,q)).probability hF hW hp hm ht x
    have hd := (hF.prefix ((b,q)::tail) hm).bounded t ht x
    have hh := finite_law_subtraction_bound hp'.1 hp'.2 hs (sub_nonneg.mpr ha.2.1) hd
    simp_rw [← T.score_step hF hW hb a b q _ ht x] at hh
    dsimp only [budget]
    linarith
  | @fork front T U ihT ihU =>
    have hT := T.probability hF hW hp ha.positive ht x
    have hU := U.probability hF hW hp ha.positive ht x
    have hd := (hF.prefix front ha.positive).bounded t ht x
    have hh := finite_law_score_fork hT.1 hU.1 hT.2 hU.2 (ihT ha x) (ihU ha x) ha.bounds.1 hd
    change (βˆ‘ Οƒ : T.Assignment ΞΊ Γ— U.Assignment ΞΊ,
      |T.response F D W B Οƒ.1 t x*U.weight F W Οƒ.2 t x+
       T.weight F W Οƒ.1 t x*U.response F D W B Οƒ.2 t x+
       a*(T.weight F W Οƒ.1 t x*U.weight F W Οƒ.2 t x)*prefixTest front (F t) (D t) x|) ≀ _
    dsimp only [budget]
    exact hh.trans (by linarith)

end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
universe uB vB wB

 theorem finite_law_addition_bound {ΞΊ : Type*} [Fintype ΞΊ]
    {W R : ΞΊ β†’ ℝ} {c D K J : ℝ} (hW : βˆ€ i, 0 ≀ W i) (sW : βˆ‘ i, W i = 1)
    (hR : βˆ‘ i, |R i| ≀ K) (hc : 0 ≀ c) (hD : |D| ≀ J) :
    βˆ‘ i, |R i+c*W i*D| ≀ K+c*J := by
  have hh := finite_law_subtraction_bound hW sW hR hc (show |-D| ≀ J by simpa only [abs_neg] using hD)
  simpa only [mul_neg,sub_neg_eq_add] using hh

variable {ΞΉ : Type uB} {Ξ² : Type vB} {ΞΊ : Ξ² β†’ Type wB} [Fintype ΞΉ] [βˆ€ b, Fintype (ΞΊ b)]

noncomputable def BoundaryTree.expect (F : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ)
    (W : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ) {front : List (GaussianStep ΞΉ)}
    (T : BoundaryTree ΞΉ Ξ² front) (P : T.Assignment ΞΊ β†’ ℝ) (t : ℝ) (x : ΞΉ β†’ ℝ) : ℝ :=
  βˆ‘ Οƒ, P Οƒ*T.weight F W Οƒ t x

 theorem BoundaryTree.expect_hasDerivAt {l r J : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (hF : ParametricRegular l r J F D)
    (hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest l r K (W b Οƒ) (B b Οƒ))
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front)
    (hm : βˆ€ st ∈ front, 0 < st.1) (P : T.Assignment ΞΊ β†’ ℝ) {t : ℝ} (ht : t ∈ Ioo l r) (x : ΞΉ β†’ ℝ) :
    HasDerivAt (fun s => T.expect F W P s x) (βˆ‘ Οƒ, P Οƒ*T.response F D W B Οƒ t x) t := by
  exact HasDerivAt.fun_sum (u := Finset.univ) (fun Οƒ _ =>
    ((T.parametric hF hW hm Οƒ).choose_spec.derivative t ht x).const_mul (P Οƒ))

 theorem BoundaryTree.tested_response_bound {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    {W B : βˆ€ b, ΞΊ b β†’ ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ} {front : List (GaussianStep ΞΉ)}
    (T : BoundaryTree ΞΉ Ξ² front) {P : T.Assignment ΞΊ β†’ ℝ} {C K : ℝ} {t : ℝ} {x : ΞΉ β†’ ℝ}
    (hC : 0 ≀ C) (hP : βˆ€ Οƒ, |P Οƒ| ≀ C)
    (hR : βˆ‘ Οƒ, |T.response F D W B Οƒ t x| ≀ K) :
    |βˆ‘ Οƒ, P Οƒ*T.response F D W B Οƒ t x| ≀ C*K := by
  apply (Finset.abs_sum_le_sum_abs _ _).trans
  calc
    _ ≀ βˆ‘ Οƒ, C*|T.response F D W B Οƒ t x| := Finset.sum_le_sum fun Οƒ _ => by
      rw [abs_mul]
      exact mul_le_mul_of_nonneg_right (hP Οƒ) (abs_nonneg _)
    _ = C*βˆ‘ Οƒ, |T.response F D W B Οƒ t x| := (Finset.mul_sum _ _ _).symm
    _ ≀ C*K := mul_le_mul_of_nonneg_left hR hC

end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
variable {ΞΉ S Ξ² : Type*} [Fintype ΞΉ] [Fintype S] [Nonempty S] {n : β„•}

noncomputable def ReplicaTree.atomicClock (w : S β†’ ℝ) (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ)
    (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ)) {steps : List (GaussianStep ΞΉ)}
    (T : ReplicaTree ΞΉ S steps) (m : ℝ) (Οƒ : T.Assignment) : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ := by
  classical
  exact linearClockTest L m (gaussianHierarchy w Ο† steps) (T.expect w Ο† (fun Ο„ => if Ο„ = Οƒ then 1 else 0))

noncomputable def ReplicaTree.atomicResponse (w : S β†’ ℝ) (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ)
    (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ)) {steps : List (GaussianStep ΞΉ)}
    (T : ReplicaTree ΞΉ S steps) (m : ℝ) (Οƒ : T.Assignment) : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ := by
  classical
  exact T.clockResponse w Ο† L m (fun Ο„ => if Ο„ = Οƒ then 1 else 0)

 

 theorem BoundaryTree.actual_inserted_response_variation {w : S β†’ ℝ} (hw : βˆ€ Οƒ, 0 < w Οƒ)
    (Ο† : S β†’ (ΞΉ β†’ ℝ) β†’L[ℝ] ℝ) (L : (Fin (n+1) β†’ ℝ) β†’L[ℝ] (ΞΉ β†’ ℝ))
    {steps : List (GaussianStep ΞΉ)} (R : Ξ² β†’ ReplicaTree ΞΉ S steps)
    {m Q : ℝ} (ha : OrderedSteps m steps) (hm : 0 < m)
    (hQ : βˆ€ Οƒ, βˆ‘ i, (Ο† Οƒ (L (Pi.single i 1)))^2 = Q)
    (D : Fin (n+1) β†’ ℝ) (hD : βˆ€ i, 0 ≀ D i)
    (hB : βˆ€ i Οƒ, |Ο† Οƒ (L (Pi.single i 1))| ≀ D i)
    {front : List (GaussianStep ΞΉ)} (T : BoundaryTree ΞΉ Ξ² front) (hO : PrefixOrdered 0 m front)
    {t : ℝ} (ht : 0 < t) (x : ΞΉ β†’ ℝ) :
    βˆ‘ Οƒ, |T.response (linearClockContinuation L m (gaussianHierarchy w Ο† steps))
      (featureClockResponse w Ο† L steps m)
      (fun b => (R b).atomicClock w Ο† L m) (fun b => (R b).atomicResponse w Ο† L m) Οƒ t x| ≀
      T.budget (fun b => 4*((R b).leaves:ℝ)*((R b).leaves+1:ℝ)*(βˆ‘ i, (D i)^2)+m*Q) := by
  classical
  let F := linearClockContinuation L m (gaussianHierarchy w Ο† steps)
  let J := featureClockResponse w Ο† L steps m
  let W := fun b => (R b).atomicClock w Ο† L m
  let B := fun b => (R b).atomicResponse w Ο† L m
  have hF : ParametricRegular 0 (t+1) (Q/2) F J := parametric_featureClock hw Ο† L hQ steps ha hm (t+1)
  have hW : βˆ€ b Οƒ, βˆƒ K, ParametricTest 0 (t+1) K (W b Οƒ) (B b Οƒ) := by
    intro b Οƒ
    refine ⟨_,(R b).parametric_clockTest hw Ο† L ha D (by norm_num : (0:ℝ) ≀ 1) hD ?_ hB (t+1)⟩
    intro Ο„
    split_ifs <;> norm_num
  have hp : βˆ€ b, βˆ€ s ∈ Ioo 0 (t+1), βˆ€ y, (βˆ€ Οƒ, 0 ≀ W b Οƒ s y) ∧ βˆ‘ Οƒ, W b Οƒ s y = 1 := by
    intro b s _ y
    exact (R b).atomic_clockTest_probability hw Ο† L ha s y
  let V : Ξ² β†’ ℝ := fun b => 4*((R b).leaves:ℝ)*((R b).leaves+1:ℝ)*(βˆ‘ i, (D i)^2)+m*(Q/2)
  have hv : βˆ€ b, βˆ€ s ∈ Ioo 0 (t+1), βˆ€ y, βˆ‘ Οƒ, |B b Οƒ s y+m*W b Οƒ s y*J s y| ≀ V b := by
    intro b s hs y
    exact finite_law_addition_bound (hp b s hs y).1 (hp b s hs y).2
      ((R b).atomic_clockResponse_variation hw Ο† L ha D hD hB hs.1 y) hm.le (hF.bounded s hs y)
  have hs := T.score_variation hF hW hp hv hO (show t ∈ Ioo 0 (t+1) by exact ⟨ht,by linarith⟩) x
  have he : (fun b => V b+m*(Q/2)) = (fun b => 4*((R b).leaves:ℝ)*((R b).leaves+1:ℝ)*(βˆ‘ i, (D i)^2)+m*Q) := by
    funext b
    dsimp [V]
    ring
  rw [he] at hs
  simpa only [BoundaryTree.score,zero_mul,add_zero,sub_zero] using hs

end SKFluctuations.Hierarchy

namespace SKFluctuations.Hierarchy
open MeasureTheory ProbabilityTheory Filter Set
open scoped Topology NNReal BigOperators
variable {ΞΉ : Type*} [Fintype ΞΉ]

 def interpolate (F G : (ΞΉ β†’ ℝ) β†’ ℝ) (t : ℝ) (x : ΞΉ β†’ ℝ) : ℝ :=
  (1-t)*F x+t*G x

 omit [Fintype ΞΉ] in
 theorem interpolate_derivative (F G : (ΞΉ β†’ ℝ) β†’ ℝ) (t : ℝ) (x : ΞΉ β†’ ℝ) :
    HasDerivAt (fun s => interpolate F G s x) (G x-F x) t := by
  apply ((((hasDerivAt_const t (1:ℝ)).sub (hasDerivAt_id t)).mul_const (F x)).add
    ((hasDerivAt_id t).mul_const (G x))).congr_deriv
  ring

 theorem parametric_interpolate {F G : (ΞΉ β†’ ℝ) β†’ ℝ} {Cβ‚€ C₁ : ℝβ‰₯0} {K l r : ℝ}
    (hF : LipschitzWith Cβ‚€ F) (hG : LipschitzWith C₁ G) (hK : 0 ≀ K)
    (hb : βˆ€ x, |G x-F x| ≀ K) :
    ParametricRegular l r K (interpolate F G) (fun _ x => G x-F x) := by
  refine ⟨hK,?_,fun _ _ => hG.continuous.sub hF.continuous,fun _ _ x => hb x,fun t _ x => interpolate_derivative F G t x⟩
  intro t _
  exact ⟨_,((lipschitzWith_smul (1-t)).comp hF).add ((lipschitzWith_smul t).comp hG)⟩

 omit [Fintype ΞΉ] in
 theorem parametricTest_interpolate {F G : (ΞΉ β†’ ℝ) β†’ ℝ} {K : ℝ}
    (hF : Continuous F) (hG : Continuous G) (hK : 0 ≀ K)
    (hbF : βˆ€ x, |F x| ≀ K) (hbG : βˆ€ x, |G x| ≀ K) :
    ParametricTest (-1) 2 (5*K) (interpolate F G) (fun _ x => G x-F x) := by
  refine ⟨by positivity,fun t _ => (hF.const_mul (1-t)).add (hG.const_mul t),
    fun _ _ => hG.sub hF,?_,?_,fun t _ x => interpolate_derivative F G t x⟩
  Β· intro t ht x
    have ht1 : |1-t| ≀ 3 := by rw [abs_le]; constructor <;> linarith [ht.1,ht.2]
    have ht2 : |t| ≀ 2 := by rw [abs_le]; constructor <;> linarith [ht.1,ht.2]
    unfold interpolate
    apply (abs_add_le _ _).trans
    rw [abs_mul,abs_mul]
    have h1 := mul_le_mul ht1 (hbF x) (abs_nonneg _) (by norm_num : (0:ℝ) ≀ 3)
    have h2 := mul_le_mul ht2 (hbG x) (abs_nonneg _) (by norm_num : (0:ℝ) ≀ 2)
    linarith
  Β· intro t _ x
    exact (abs_sub _ _).trans (by linarith [hbF x,hbG x])

 theorem ParametricRegular.restrict {l r l' r' K : ℝ} {F D : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (h : ParametricRegular l r K F D) (hs : Ioo l' r' βŠ† Ioo l r) : ParametricRegular l' r' K F D :=
  ⟨h.nonneg,fun t ht => h.spatial t (hs ht),fun t ht => h.continuous t (hs ht),
    fun t ht => h.bounded t (hs ht),fun t ht => h.derivative t (hs ht)⟩

 omit [Fintype ΞΉ] in
 theorem ParametricTest.restrict {l r l' r' K : ℝ} {A B : ℝ β†’ (ΞΉ β†’ ℝ) β†’ ℝ}
    (h : ParametricTest l r K A B) (hs : Ioo l' r' βŠ† Ioo l r) : ParametricTest l' r' K A B :=
  ⟨h.nonneg,fun t ht => h.continuous t (hs ht),fun t ht => h.response_continuous t (hs ht),
    fun t ht => h.bounded t (hs ht),fun t ht => h.response_bounded t (hs ht),
    fun t ht => h.derivative t (hs ht)⟩

end SKFluctuations.Hierarchy

end

end OAI