Flip.lean
Lean 4 source, lean/DataMiningAsObservation/Flip.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Flip.lean. The four-to-one flip of chapter 3, equation 3.1, and the cover.
Two error covariances diag(a, b) and diag(b, a) have the same trace. A reader that reads
the single direction (c, s) experiences each as the variance along that direction. The
difference between the two readings is (a − b)(c² − s²), so swapping the reader's two
coordinates reverses the ranking. With the book's numbers, a = 0.3, b = 1.7, and the
reader at 15 degrees, the two readings are 1 − 0.35√3 and 1 + 0.35√3, which are 0.394
and 1.606 to three decimals, a ratio of a little over four to one.
-/
import Mathlib
namespace DMO.Flip
/-- The read distortion of the diagonal error covariance diag(a, b) along the reader (c, s),
the variance of the error along that direction, `u'Σu` of equation 0.4. -/
def readDiag (a b c s : ℝ) : ℝ := a * c ^ 2 + b * s ^ 2
/-- Both errors have the same trace. -/
theorem trace_eq (a b : ℝ) : a + b = b + a := add_comm a b
/-- Equation 3.1 in general. The two readings differ by (a − b)(c² − s²). -/
theorem reading_difference (a b c s : ℝ) :
readDiag a b c s - readDiag b a c s = (a - b) * (c ^ 2 - s ^ 2) := by
unfold readDiag; ring
/-- Swapping the reader's coordinates reverses the sign of the difference, so the second
reader ranks the two errors the opposite way from the first. -/
theorem reader_swap_reverses (a b c s : ℝ) :
readDiag a b s c - readDiag b a s c = -(readDiag a b c s - readDiag b a c s) := by
unfold readDiag; ring
/-- The flip, stated as an ordering. If a < b and the reader leans toward the first axis,
c² > s², it ranks diag(a, b) below diag(b, a), and the swapped reader ranks them the other
way, while the traces agree. -/
theorem flip (a b c s : ℝ) (hab : a < b) (hcs : s ^ 2 < c ^ 2) :
readDiag a b c s < readDiag b a c s ∧ readDiag b a s c < readDiag a b s c := by
constructor
· have := reading_difference a b c s
nlinarith
· have := reading_difference a b s c
nlinarith
/-! ### The book's numbers -/
/-- cos²(15°) = 1/2 + √3/4. -/
theorem cos_sq_pi_div_twelve : Real.cos (Real.pi / 12) ^ 2 = 1 / 2 + Real.sqrt 3 / 4 := by
rw [Real.cos_sq]
have : 2 * (Real.pi / 12) = Real.pi / 6 := by ring
rw [this, Real.cos_pi_div_six]
ring
/-- sin²(15°) = 1/2 − √3/4. -/
theorem sin_sq_pi_div_twelve : Real.sin (Real.pi / 12) ^ 2 = 1 / 2 - Real.sqrt 3 / 4 := by
rw [Real.sin_sq, cos_sq_pi_div_twelve]
ring
/-- The reader at 15 degrees reads diag(0.3, 1.7) as 1 − 0.35√3. -/
theorem reading_15_first :
readDiag 0.3 1.7 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12))
= 1 - 0.35 * Real.sqrt 3 := by
unfold readDiag
rw [cos_sq_pi_div_twelve, sin_sq_pi_div_twelve]
ring
/-- The reader at 15 degrees reads diag(1.7, 0.3) as 1 + 0.35√3. -/
theorem reading_15_second :
readDiag 1.7 0.3 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12))
= 1 + 0.35 * Real.sqrt 3 := by
unfold readDiag
rw [cos_sq_pi_div_twelve, sin_sq_pi_div_twelve]
ring
/-- √3 lies between 1.732 and 1.7321. -/
theorem sqrt_three_bounds : 1.732 < Real.sqrt 3 ∧ Real.sqrt 3 < 1.7321 := by
constructor
· rw [Real.lt_sqrt (by norm_num)]; norm_num
· rw [Real.sqrt_lt' (by norm_num)]; norm_num
/-- The two readings at 15 degrees are 0.394 and 1.606 to three decimals, as the book prints. -/
theorem readings_15_to_three_decimals :
|readDiag 0.3 1.7 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12)) - 0.394| < 0.0005 ∧
|readDiag 1.7 0.3 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12)) - 1.606| < 0.0005 := by
rw [reading_15_first, reading_15_second]
obtain ⟨h1, h2⟩ := sqrt_three_bounds
constructor <;> rw [abs_lt] <;> constructor <;> linarith
/-- The reader at 75 degrees is the reader at 15 degrees with its coordinates swapped,
since cos(75°) = sin(15°) and sin(75°) = cos(15°). -/
theorem reader_75_is_swap :
Real.cos (5 * Real.pi / 12) = Real.sin (Real.pi / 12) ∧
Real.sin (5 * Real.pi / 12) = Real.cos (Real.pi / 12) := by
have h : 5 * Real.pi / 12 = Real.pi / 2 - Real.pi / 12 := by ring
rw [h, Real.cos_pi_div_two_sub, Real.sin_pi_div_two_sub]
exact ⟨rfl, rfl⟩
/-- The four-to-one flip with the book's numbers. The reader at 15 degrees ranks the first
error below the second, the reader at 75 degrees ranks the second below the first, and the
traces are equal. -/
theorem four_to_one_flip :
readDiag 0.3 1.7 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12))
< readDiag 1.7 0.3 (Real.cos (Real.pi / 12)) (Real.sin (Real.pi / 12)) ∧
readDiag 1.7 0.3 (Real.cos (5 * Real.pi / 12)) (Real.sin (5 * Real.pi / 12))
< readDiag 0.3 1.7 (Real.cos (5 * Real.pi / 12)) (Real.sin (5 * Real.pi / 12)) ∧
(0.3 : ℝ) + 1.7 = 1.7 + 0.3 := by
obtain ⟨hc, hs⟩ := reader_75_is_swap
rw [hc, hs]
have hcs : Real.sin (Real.pi / 12) ^ 2 < Real.cos (Real.pi / 12) ^ 2 := by
rw [cos_sq_pi_div_twelve, sin_sq_pi_div_twelve]
have := Real.sqrt_pos.mpr (show (0 : ℝ) < 3 by norm_num)
linarith
obtain ⟨h1, h2⟩ := flip 0.3 1.7 _ _ (by norm_num) hcs
exact ⟨h1, h2, by norm_num⟩
end DMO.Flip