File size: 3,385 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
import OAI.Combinatorics.ContingencyTables.Transport.RationalMapProducts
import OAI.Combinatorics.ContingencyTables.Transport.RejectionLaw

namespace OAI

/-! # Repeated rejection implemented by one finite independent Boolean array -/

namespace ContingencyTables.FirstSuccess

open scoped BigOperators Classical
open ResidualMixture FairBits

noncomputable section

theorem retryLaw_map {A B C : Type*} [Fintype A] [Fintype B] [Fintype C]
    [DecidableEq B] [DecidableEq C]
    (p : RationalLaw A) (draw : A β†’ B) (f : B β†’ Option C) (fallback : C) (n : β„•) :
    retryLaw (mapLaw p draw) f fallback n = retryLaw p (fun x => f (draw x)) fallback n := by
  have hm := mapLaw_comp p draw f
  have hg : successMass (mapLaw p draw) f = successMass p (fun x => f (draw x)) := by
    funext z
    exact congrArg (fun q : RationalLaw (Option C) => q.mass (some z)) hm
  have hf : failureMass (mapLaw p draw) f = failureMass p (fun x => f (draw x)) :=
    congrArg (fun q : RationalLaw (Option C) => q.mass none) hm
  apply RationalLaw.ext
  funext z
  simp only [retryLaw_mass, hg, hf]

variable {Ξ± : Type*} [Fintype Ξ±] [DecidableEq Ξ±]

def wordRetry {k : β„•} (f : (Fin k β†’ Bool) β†’ Option Ξ±) (fallback : Ξ±) :
    (n : β„•) β†’ (Fin n β†’ Fin k β†’ Bool) β†’ Ξ±
  | 0, _ => fallback
  | n + 1, bits => (f (bits 0)).getD (wordRetry f fallback n (Fin.tail bits))

def wordRetryLaw {k : β„•} (f : (Fin k β†’ Bool) β†’ Option Ξ±) (fallback : Ξ±) (n : β„•) : RationalLaw Ξ± :=
  mapLaw (uniformLaw (Ξ± := Fin n β†’ Fin k β†’ Bool)) (wordRetry f fallback n)

/-- The first successful output is computed from an actual fixed Boolean
array; unused suffix words do not change its distribution. -/
theorem wordRetryLaw_eq {k : β„•} (f : (Fin k β†’ Bool) β†’ Option Ξ±) (fallback : Ξ±) (n : β„•) :
    wordRetryLaw f fallback n = retryLaw (uniformLaw (Ξ± := Fin k β†’ Bool)) f fallback n := by
  induction n with
  | zero =>
    apply RationalLaw.ext
    funext z
    simp [wordRetryLaw, wordRetry, retryLaw, pointLaw, mapLaw, uniformLaw, uniformMass, eq_comm]
  | succ n ih =>
    let E : ((Fin k β†’ Bool) Γ— (Fin n β†’ Fin k β†’ Bool)) ≃ (Fin (n + 1) β†’ Fin k β†’ Bool) :=
      Fin.consEquiv (fun _ => Fin k β†’ Bool)
    have hc : (fun z => wordRetry f fallback (n + 1) (E z)) =
        fun z => (f z.1).getD (wordRetry f fallback n z.2) := by
      funext z
      rcases z with ⟨head, tail⟩
      change (f ((Fin.cons (Ξ± := fun _ : Fin (n+1) => Fin k β†’ Bool) head tail) 0)).getD
        (wordRetry f fallback n (Fin.tail (Fin.cons (Ξ± := fun _ : Fin (n+1) => Fin k β†’ Bool) head tail))) = _
      simp only [Fin.cons_zero, Fin.tail_cons]
    unfold wordRetryLaw
    rw [← uniform_equiv E, map_equiv E, hc, ← uniform_product]
    have hcomp := mapLaw_comp
      (productLaw (uniformLaw (Ξ± := Fin k β†’ Bool)) (uniformLaw (Ξ± := Fin n β†’ Fin k β†’ Bool)))
      (fun z => (id z.1, wordRetry f fallback n z.2)) (fun z => (f z.1).getD z.2)
    dsimp only [id_eq, Prod.fst, Prod.snd] at hcomp
    rw [← hcomp]
    have hp := mapLaw_product (uniformLaw (Ξ± := Fin k β†’ Bool))
      (uniformLaw (Ξ± := Fin n β†’ Fin k β†’ Bool)) id (wordRetry f fallback n)
    dsimp only [id_eq] at hp
    rw [hp, mapLaw_id]
    change retryStep (uniformLaw (Ξ± := Fin k β†’ Bool)) f (wordRetryLaw f fallback n) = _
    rw [ih]
    rfl

end

end ContingencyTables.FirstSuccess

end OAI