File size: 6,130 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
import OAI.Combinatorics.ContingencyTables.Counting.CompanionOracleEstimator

namespace OAI

/-! # Output range on every execution of the finite-word estimator -/

namespace ContingencyTables.CompanionOracleDensity

open CompanionFactorialApplication ForestAssignmentSplit GridBoundary ResidualMixture FirstSuccess
open CommonBasesFPRAS FiniteIndependentAverages
open scoped BigOperators Classical

noncomputable section

variable {I J : Type*} [Fintype I] [Fintype J]

lemma rationalAverage_bounds {X : Type*} (g : X β†’ β„š) (N : β„•) (hN : 0 < N)
    (L U : ℝ) (hg : βˆ€ x,L ≀ (g x:ℝ) ∧ (g x:ℝ) ≀ U) (draws : Fin N β†’ X) :
    L ≀ (rationalAverage g N draws:ℝ) ∧ (rationalAverage g N draws:ℝ) ≀ U := by
  have hNR : (0:ℝ) < N := by exact_mod_cast hN
  have hl := Finset.sum_le_sum (s := Finset.univ) (fun i _ => (hg (draws i)).1)
  have hu := Finset.sum_le_sum (s := Finset.univ) (fun i _ => (hg (draws i)).2)
  simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] at hl hu
  simp only [rationalAverage,Rat.cast_div,Rat.cast_sum,Rat.cast_natCast]
  exact ⟨(le_div_iffβ‚€ hNR).mpr (by linarith),(div_le_iffβ‚€ hNR).mpr (by linarith)⟩

lemma observed_ratio_bounds (A : Instance I J) (C : β„•) (hC : 0 < C) (z : View A β†’ β„•)
    (hz : BalancedBox.InBox (bounds A) z) (K : β„•) (hK : 1 ≀ K) (hW : 100*A.d*K ≀ A.W)
    (hB : (K:ℝ)=binWidth A C) (j : β„•) (delta failure : β„š) (hd : 0 < delta) (hf : 0 < failure)
    (v : Grid (I := Fin (dimension A)) K) (draws : EstimatorInput A C K j delta failure) (a : Fin j) :
    1/2 ≀ (observedFactor A C z K hK hW j delta failure v draws (some a):ℝ) ∧
      (observedFactor A C z K hK hW j delta failure v draws (some a):ℝ) ≀ 1 := by
  apply rationalAverage_bounds _ _ (sampleCount_positive j delta failure hd hf)
  intro bits
  rw [ratioSample_cast]
  exact ratioObservable_bounds A C (by exact_mod_cast hC) a.val z hz hB.le _

lemma observed_correction_bounds (A : Instance I J) (C : β„•) (hC : 0 < C) (z : View A β†’ β„•)
    (K : β„•) (hK : 1 ≀ K) (hW : 100*A.d*K ≀ A.W) (hB : (K:ℝ)=binWidth A C)
    (j : β„•) (hj : j ≀ rationalHeight A C) (delta failure : β„š) (hd : 0 < delta) (hd1 : delta < 1)
    (hf : 0 < failure) (v : Grid (I := Fin (dimension A)) K)
    (draws : EstimatorInput A C K j delta failure) :
    1/16 ≀ (observedFactor A C z K hK hW j delta failure v draws none:ℝ) ∧
      (observedFactor A C z K hK hW j delta failure v draws none:ℝ) ≀ 8 := by
  apply rationalAverage_bounds _ _ (sampleCount_positive j delta failure hd hf)
  intro bits
  apply computedCorrection_bounds A C j hC hj z K (by omega) hW hB (roundingTolerance delta)
  Β· unfold roundingTolerance
    positivity
  Β· unfold roundingTolerance
    linarith

/-- These bounds hold for every Boolean input, including executions on
which the empirical averages are statistically inaccurate. -/
theorem estimateValue_range (A : Instance I J) (C : β„•) (hC : 0 < C) (z : View A β†’ β„•)
    (hz : BalancedBox.InBox (bounds A) z) (K : β„•) (hK : 1 ≀ K) (hW : 100*A.d*K ≀ A.W)
    (hB : (K:ℝ)=binWidth A C) (j : β„•) (hj : j ≀ rationalHeight A C)
    (delta failure : β„š) (hd : 0 < delta) (hd1 : delta < 1) (hf : 0 < failure)
    (v : Grid (I := Fin (dimension A)) K) (draws : EstimatorInput A C K j delta failure) :
    (assignmentCount A:ℝ)*(1/2:ℝ)^j/16 ≀ (estimateValue A C z K hK hW j delta failure v draws:ℝ) ∧
      (estimateValue A C z K hK hW j delta failure v draws:ℝ) ≀ 8*assignmentCount A := by
  let r := fun a : Fin j => (observedFactor A C z K hK hW j delta failure v draws (some a):ℝ)
  let q := (observedFactor A C z K hK hW j delta failure v draws none:ℝ)
  have hr := observed_ratio_bounds A C hC z hz K hK hW hB j delta failure hd hf v draws
  have hq := observed_correction_bounds A C hC z K hK hW hB j hj delta failure hd hd1 hf v draws
  have hrlow : (1/2:ℝ)^j ≀ ∏ a,r a := by
    have hh := Finset.prod_le_prodβ‚€ (s := Finset.univ)
      (fun _ _ => by norm_num : βˆ€ a : Fin j,a∈Finset.univ β†’ (0:ℝ) ≀ 1/2)
      (fun a _ => (hr a).1)
    simpa only [Finset.prod_const,Finset.card_univ,Fintype.card_fin] using hh
  have hrup : (∏ a,r a) ≀ 1 := by
    have hh := Finset.prod_le_prodβ‚€ (s := Finset.univ)
      (fun a _ => show (0:ℝ) ≀ r a from (by have h := (hr a).1; linarith)) (fun a _ => (hr a).2)
    simpa only [Finset.prod_const_one] using hh
  have hr0 : (0:ℝ) ≀ ∏ a,r a := Finset.prod_nonneg (fun a _ => by have h := (hr a).1; linarith)
  have hq0 : 0 ≀ q := by dsimp only [q]; linarith [hq.1]
  have hlow : (1/16:ℝ)*(1/2:ℝ)^j ≀ q*(∏ a,r a) :=
    mul_le_mul hq.1 hrlow (by positivity) hq0
  have hupp : q*(∏ a,r a) ≀ 8 := by
    calc
      _ ≀ 8*1 := mul_le_mul hq.2 hrup hr0 (by norm_num)
      _ = _ := by norm_num
  have hMf : (0:ℝ) ≀ assignmentCount A := Nat.cast_nonneg _
  have he : (estimateValue A C z K hK hW j delta failure v draws:ℝ)=
      (assignmentCount A:ℝ)*(q*∏ a,r a) := by
    simp only [estimateValue,Rat.cast_mul,Rat.cast_natCast,Rat.cast_prod,Fintype.prod_option]
    rfl
  rw [he]
  constructor
  Β· have hh := mul_le_mul_of_nonneg_left hlow hMf
    nlinarith
  Β· have hh := mul_le_mul_of_nonneg_left hupp hMf
    nlinarith

theorem estimateValue_positive (A : Instance I J) (C : β„•) (hC : 0 < C) (z : View A β†’ β„•)
    (hz : BalancedBox.InBox (bounds A) z) (K : β„•) (hK : 1 ≀ K) (hW : 100*A.d*K ≀ A.W)
    (hB : (K:ℝ)=binWidth A C) (j : β„•) (hj : j ≀ rationalHeight A C)
    (delta failure : β„š) (hd : 0 < delta) (hd1 : delta < 1) (hf : 0 < failure)
    (v : Grid (I := Fin (dimension A)) K) (draws : EstimatorInput A C K j delta failure) :
    0 < estimateValue A C z K hK hW j delta failure v draws := by
  have hMf : (0:ℝ) < assignmentCount A := by exact_mod_cast assignmentCount_positive A
  have hh := (estimateValue_range A C hC z hz K hK hW hB j hj delta failure hd hd1 hf v draws).1
  have hp : (0:ℝ) < (assignmentCount A:ℝ)*(1/2:ℝ)^j/16 := by positivity
  exact_mod_cast lt_of_lt_of_le hp hh

end

end ContingencyTables.CompanionOracleDensity

end OAI