File size: 5,999 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
import OAI.Combinatorics.ContingencyTables.Model
import OAI.Combinatorics.MatchingCount.Model

namespace OAI

/-!
# Main algorithm statements for result 200

The samplers and counter use a finite transition table, independent fair
bits, binary inputs and explicit bit-operation runtime bounds. No counting or sampling oracle is part of the machine.
The exact sampler's expected runtime uses a summable tail series, avoiding
the default value of an undefined infinite sum.
-/

namespace ContingencyTables.Algorithms

open scoped BigOperators
open Filter

abbrev Machine := MatchingFPRAS.RandomMachine
abbrev Symbol := MatchingFPRAS.Symbol

def encodeMatrix {m n : β„•} (X : Fin m β†’ Fin n β†’ β„•) : List Symbol :=
  ((List.ofFn fun i => (List.ofFn fun j => MatchingFPRAS.encodeNat (X i j)).flatten)).flatten

def encodeMargins {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) : List Symbol :=
  MatchingFPRAS.encodeNat m ++ MatchingFPRAS.encodeNat n ++
    (List.ofFn fun i => MatchingFPRAS.encodeNat (r i)).flatten ++
    (List.ofFn fun j => MatchingFPRAS.encodeNat (c j)).flatten

def encodeSamplingInput {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) (k : β„•) : List Symbol :=
  encodeMargins r c ++ MatchingFPRAS.encodeNat k

def encodeCountingInput {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•)
    (b : Fin m β†’ Fin n β†’ β„•) (Ξ΅ Ξ΄ : β„š) : List Symbol :=
  encodeMargins r c ++ encodeMatrix b ++ MatchingFPRAS.encodeRat Ξ΅ ++ MatchingFPRAS.encodeRat Ξ΄

/-- The tape alphabet has positive cardinality. -/
theorem alphabetSize_neZero : NeZero (8 : β„•) := inferInstance

/-- A halted tape contains the row-major binary matrix encoding. -/
def OutputsTable (A : Machine) (input : List Symbol) {t m n : β„•}
    (bits : Fin t β†’ Bool) (X : Fin m β†’ Fin n β†’ β„•) : Prop :=
  (MatchingFPRAS.run A input bits).state = none ∧
    @Turing.Tape.rightβ‚€ MatchingFPRAS.Symbol (@Fin.instInhabited 8 alphabetSize_neZero)
      (MatchingFPRAS.run A input bits).tape =
    @Turing.ListBlank.mk Symbol (@Fin.instInhabited 8 alphabetSize_neZero) (encodeMatrix X)

noncomputable def tableMass (A : Machine) (input : List Symbol) (t : β„•)
    {m n : β„•} (X : Fin m β†’ Fin n β†’ β„•) : ℝ := by
  classical
  exact ((Finset.univ.filter fun bits : Fin t β†’ Bool => OutputsTable A input bits X).card : ℝ) /
    (2 : ℝ) ^ t

noncomputable def haltMass (A : Machine) (input : List Symbol) (t : β„•) : ℝ := by
  classical
  exact ((Finset.univ.filter fun bits : Fin t β†’ Bool =>
      (MatchingFPRAS.run A input bits).state = none).card : ℝ) / (2 : ℝ) ^ t

def samplingSize (n : β„•) {m : β„•} (r : Fin m β†’ β„•) (k : β„•) : β„• :=
  m + n + Nat.clog 2 ((βˆ‘ i, r i) + 1) + k + 1

/-- Paper 200-01, Theorem main(i): bounded runtime on every random tape
and total variation at most `2⁻ᡏ`, with freely varying dimensions. -/
def BoundedSamplingStatement : Prop :=
  βˆƒ (A : Machine) (C d : β„•), 0 < C ∧
    βˆ€ (m n : β„•) (r : Fin m β†’ β„•) (c : Fin n β†’ β„•),
      (βˆ‘ i, r i) = βˆ‘ j, c j β†’ βˆ€ k : β„•, 1 ≀ k β†’
        let input := encodeSamplingInput r c k
        let t := C * (samplingSize n r k) ^ d
        (βˆ€ bits : Fin t β†’ Bool, βˆƒ X : Table r c, OutputsTable A input bits X.val) ∧
        (βˆ‘ X : Table r c,
          |tableMass A input t X.val - (Fintype.card (Table r c) : ℝ)⁻¹|) / 2 ≀
            ((2 : ℝ) ^ k)⁻¹

/-- Paper 200-01, Theorem main(ii): every halted output is feasible, each
table has limiting mass `1/|Ξ©|`, termination has probability one, and the
expected number of bit operations is bounded by a uniform polynomial. -/
def ExactSamplingStatement : Prop :=
  βˆƒ (A : Machine) (C d : β„•), 0 < C ∧
    βˆ€ (m n : β„•) (r : Fin m β†’ β„•) (c : Fin n β†’ β„•),
      (βˆ‘ i, r i) = βˆ‘ j, c j β†’
        let input := encodeMargins r c
        let tail : β„• β†’ ℝ := fun t => 1 - haltMass A input t
        (βˆ€ t (bits : Fin t β†’ Bool), (MatchingFPRAS.run A input bits).state = none β†’
          βˆƒ X : Table r c, OutputsTable A input bits X.val) ∧
        (βˆ€ X : Table r c, Tendsto (fun t => tableMass A input t X.val) atTop
          (nhds ((Fintype.card (Table r c) : ℝ)⁻¹))) ∧
        Tendsto (haltMass A input) atTop (nhds 1) ∧
        Summable tail ∧
        tsum tail ≀ C * (samplingSize n r 0) ^ d

noncomputable def count {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•)
    (b : Fin m β†’ Fin n β†’ β„•) : β„• := Fintype.card (BoundedTable r c b)

def countingTime (C d : β„•) {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•)
    (b : Fin m β†’ Fin n β†’ β„•) (Ξ΅ Ξ΄ : β„š) : β„• :=
  C * ((encodeCountingInput r c b Ξ΅ Ξ΄).length + βŒˆΞ΅β»ΒΉβŒ‰β‚Š +
    Nat.clog 2 βŒˆΞ΄β»ΒΉβŒ‰β‚Š + 1) ^ d

noncomputable def goodCountingTapes (A : Machine) {m n : β„•}
    (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) (b : Fin m β†’ Fin n β†’ β„•)
    (Ξ΅ Ξ΄ : β„š) (t : β„•) : Finset (Fin t β†’ Bool) := by
  classical
  exact Finset.univ.filter fun bits => βˆƒ q : β„š,
    MatchingFPRAS.Outputs A (encodeCountingInput r c b Ρ δ) bits q ∧
      (1 - Ξ΅) * count r c b ≀ q ∧ q ≀ (1 + Ξ΅) * count r c b

/-- Paper 200-02, Theorem main. The input length includes the rational
accuracy parameters, and infeasible inputs always produce exactly zero. -/
def CountingStatement : Prop :=
  βˆƒ (A : Machine) (C d : β„•), 0 < C ∧
    βˆ€ (m n : β„•), 0 < m β†’ 0 < n β†’
    βˆ€ (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) (b : Fin m β†’ Fin n β†’ β„•),
      (βˆ‘ i, r i) = βˆ‘ j, c j β†’
      βˆ€ Ξ΅ Ξ΄ : β„š, 0 < Ξ΅ β†’ Ξ΅ < 1 β†’ 0 < Ξ΄ β†’ Ξ΄ < 1 β†’
        let input := encodeCountingInput r c b Ξ΅ Ξ΄
        let t := countingTime C d r c b Ξ΅ Ξ΄
        (βˆ€ bits : Fin t β†’ Bool, βˆƒ q : β„š, 0 ≀ q ∧ MatchingFPRAS.Outputs A input bits q) ∧
        (count r c b = 0 β†’ βˆ€ bits : Fin t β†’ Bool, MatchingFPRAS.Outputs A input bits 0) ∧
        ((goodCountingTapes A r c b Ξ΅ Ξ΄ t).card : β„š) / (2 : β„š) ^ t β‰₯ 1 - Ξ΄

end ContingencyTables.Algorithms

end OAI