FiniteDifference.lean

Lean 4 source, lean/DataMiningAsObservation/FiniteDifference.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
FiniteDifference.lean. Chapter 0 section 0.5 and equation 0.8, the finite difference.

The central difference of equation 0.8 recovers the derivative of a quadratic exactly at
every step size, while the one-sided difference is off by the curvature times the step,
which is why doing it in both directions is more accurate. Two evaluations per coordinate
make the price of one operating point `2d`.
-/
import Mathlib

namespace DMO.FiniteDifference

/-- A quadratic function of one variable. -/
def quad (a b c x : ℝ) : ℝ := a * x ^ 2 + b * x + c

/-- The central difference of equation 0.8. -/
noncomputable def central (f : ℝ → ℝ) (x h : ℝ) : ℝ := (f (x + h) - f (x - h)) / (2 * h)

/-- The one-sided difference. -/
noncomputable def forward (f : ℝ → ℝ) (x h : ℝ) : ℝ := (f (x + h) - f x) / h

/-- **The central difference is exact on a quadratic at every step.** -/
theorem central_quad (a b c x h : ℝ) (hh : h ≠ 0) :
    central (quad a b c) x h = 2 * a * x + b := by
  unfold central quad
  rw [div_eq_iff (mul_ne_zero two_ne_zero hh)]
  ring

/-- **The one-sided difference is off by the curvature times the step.** -/
theorem forward_quad (a b c x h : ℝ) (hh : h ≠ 0) :
    forward (quad a b c) x h = 2 * a * x + b + a * h := by
  unfold forward quad
  rw [div_eq_iff hh]
  ring

/-- **The one-sided error is the curvature times the step, in absolute value.** -/
theorem forward_error (a b c x h : ℝ) (hh : h ≠ 0) :
    |forward (quad a b c) x h - (2 * a * x + b)| = |a| * |h| := by
  rw [forward_quad a b c x h hh, show 2 * a * x + b + a * h - (2 * a * x + b) = a * h by ring,
    abs_mul]

/-- **Two evaluations per coordinate cost `2d` for `d` coordinates.** -/
theorem central_cost (d : ℕ) : ∑ _i : Fin d, (2 : ℕ) = 2 * d := by
  simp [mul_comm]

end DMO.FiniteDifference