File size: 4,376 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
import OAI.Combinatorics.ContingencyTables.Dense.DenseRational

namespace OAI

/-! # Natural arithmetic for the dense acceptance exponent

Each integral completion entry is represented by a difference of two
natural numbers. The rational floor in the geometric definition is then
an ordinary natural quotient, including zero denominators.
-/

namespace ContingencyTables.GridBoundary
open scoped BigOperators Classical

def componentExponent (d s positive negative : β„•) : β„• :=
  (d^4*d^8*negative-d^4*(d^8*positive+d*s))/(s*d^8)

lemma floor_signed_quotient (positive negative denominator : β„•) :
    ⌊((positive : β„š)-negative)/denominatorβŒ‹β‚Š =
      (positive-negative)/denominator := by
  rw [Nat.floor_div_natCast,Nat.floor_sub_natCast,Nat.floor_natCast]

lemma componentExponent_eq (d s positive negative : β„•) :
    componentExponent d s positive negative =
      ⌊(d : β„š)^4*(-((positive : β„š)-negative)/s-(d : β„š)/(d : β„š)^8)βŒ‹β‚Š := by
  by_cases hd : d=0
  Β· simp [hd,componentExponent]
  by_cases hs : s=0
  Β· simp only [hs,componentExponent,Nat.zero_mul,Nat.div_zero,Nat.cast_zero,
      div_zero,zero_sub]
    symm
    apply Nat.floor_of_nonpos
    apply mul_nonpos_of_nonneg_of_nonpos (by positivity)
    exact neg_nonpos.mpr (by positivity)
  have hdq : (d : β„š)β‰ 0 := by exact_mod_cast hd
  have hsq : (s : β„š)β‰ 0 := by exact_mod_cast hs
  have he : (d : β„š)^4*(-((positive : β„š)-negative)/s-(d : β„š)/(d : β„š)^8)=
      (((d^4*d^8*negative : β„•) : β„š)-((d^4*(d^8*positive+d*s) : β„•) : β„š))/
        ((s*d^8 : β„•) : β„š) := by
    push_cast
    field_simp [hdq,hsq]
    ring
  rw [he,floor_signed_quotient]
  rfl

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

def integerParts (R : Option I β†’ β„•) (P : Option J β†’ β„•) (z : IΓ—J β†’ β„•) :
    Option I β†’ Option J β†’ β„•Γ—β„•
  | some i,some j => (z (i,j),0)
  | some i,none => (R (some i),βˆ‘ j,z (i,j))
  | none,some j => (P (some j),βˆ‘ i,z (i,j))
  | none,none => (R none+βˆ‘ i,βˆ‘ j,z (i,j),βˆ‘ j,P (some j))

lemma integerParts_sub (R : Option I β†’ β„•) (P : Option J β†’ β„•)
    (z : IΓ—J β†’ β„•) (i : Option I) (j : Option J) :
    ((integerParts R P z i j).1 : β„€)-(integerParts R P z i j).2=
      DenseAffine.integerTable R P z i j := by
  cases i <;> cases j
  all_goals simp only [integerParts,DenseAffine.integerTable,Nat.cast_zero,sub_zero,
    Nat.cast_sum,Nat.cast_add]
  ring

lemma integerParts_sub_rat (R : Option I β†’ β„•) (P : Option J β†’ β„•)
    (z : IΓ—J β†’ β„•) (i : Option I) (j : Option J) :
    ((integerParts R P z i j).1 : β„š)-(integerParts R P z i j).2=
      (DenseAffine.integerTable R P z i j : β„š) := by
  exact_mod_cast integerParts_sub R P z i j

variable {q K : β„•}

def arithmeticExponentAt (R : Option I β†’ β„•) (P : Option J β†’ β„•) (d : β„•)
    (e : IΓ—J ≃ Fin q) (a : Grid (I := Fin q) K) :
    Option (Option IΓ—Option J) β†’ β„•
  | none => 0
  | some g =>
      let parts := integerParts R P (cornerFree R P d e a) g.1 g.2
      componentExponent d (min (R g.1) (P g.2)) parts.1 parts.2

lemma arithmeticExponentAt_eq (R : Option I β†’ β„•) (P : Option J β†’ β„•) (d : β„•)
    (e : IΓ—J ≃ Fin q) (a : Grid (I := Fin q) K)
    (g : Option (Option IΓ—Option J)) :
    arithmeticExponentAt R P d e a g=
      ⌊(d : β„š)^4*rationalComponent R P d e a gβŒ‹β‚Š := by
  cases g with
  | none => simp [arithmeticExponentAt,rationalComponent]
  | some g =>
    simp only [arithmeticExponentAt,componentExponent_eq,integerParts_sub_rat,
      rationalComponent]

/-- A finite maximum of explicitly computable natural quotients is exactly
the exponent used in the dense Metropolis acceptance probability. -/
theorem denseExponent_eq_arithmetic (R : Option I β†’ β„•) (P : Option J β†’ β„•) (d : β„•)
    (e : IΓ—J ≃ Fin q) (a : Grid (I := Fin q) K) :
    denseExponent R P d e a=
      Finset.univ.sup' Finset.univ_nonempty (arithmeticExponentAt R P d e a) := by
  have hm : Monotone (fun x : β„š => ⌊(d : β„š)^4*xβŒ‹β‚Š) := by
    intro x y hxy
    exact Nat.floor_mono (mul_le_mul_of_nonneg_left hxy (by positivity))
  unfold denseExponent rationalPenalty
  rw [Finset.apply_sup'_eq_sup'_comp _ (fun x : β„š => ⌊(d : β„š)^4*xβŒ‹β‚Š) hm.map_sup]
  apply Finset.sup'_congr Finset.univ_nonempty rfl
  intro g _
  exact (arithmeticExponentAt_eq R P d e a g).symm

end ContingencyTables.GridBoundary

end OAI