import Mathlib import OAI.Probability.SKRatio.Matrices.GaussianSquareTails import OAI.Probability.SKRatio.Variational.CompactField import OAI.Probability.SKRatio.Quantization.IntervalBins namespace OAI noncomputable section open scoped BigOperators NNReal ENNReal Topology open MeasureTheory ProbabilityTheory Filter Real Set namespace SKRatio.Bins open Planted Scalar SKRatioClock.Regression attribute [local instance] Classical.propDecidable def gridCut (R : ℝ) (N : ℕ) (j : Fin (N+1)) : ℝ := -R+(2*R/N)*(j.val:ℝ) def gridRep (R : ℝ) (N : ℕ) (j : Fin (N+2)) : ℝ := -R+(2*R/N)*((min j.val N:ℕ):ℝ) def gridQuant (R : ℝ) (N : ℕ) (x : ℝ) : ℝ := gridRep R N (intervalBin (gridCut R N) x) lemma gridCut_strictMono {R : ℝ} (hR : 0 not_le.mpr hxL (by simpa only [gridCut,Fin.val_mk,Nat.cast_zero,mul_zero,add_zero] using (intervalBin_eq_zero (by omega) (gridCut_strictMono hR hN) x).mp hh) have hjLast : j≠Fin.last (N+1) := fun hh => not_lt.mpr hxU (by simpa only [Nat.add_sub_cancel,gridCut,Fin.val_mk,div_mul_cancel₀ _ ((Nat.cast_ne_zero.mpr hN.ne' : (N:ℝ)≠0)), show -R+2*R=R by ring] using (intervalBin_eq_last (by omega) (gridCut_strictMono hR hN) x).mp hh) have hj0' : 0 hj0 (Fin.ext he); omega have hjN : j.val hjLast (Fin.ext he) omega have hbounds := (intervalBin_eq_middle (gridCut_strictMono hR hN) x j hj0' hjN).mp rfl change |x-gridRep R N j|≤_ rw [gridRep_middle R j hjN] simp only [gridCut] at hbounds ⊢ have hcast : ((j.val-1:ℕ):ℝ)=(j.val:ℝ)-1 := by rw [Nat.cast_sub (by omega),Nat.cast_one] rw [hcast] at hbounds rw [abs_of_nonpos (sub_nonpos.mpr hbounds.2)] linarith only [hbounds.1] lemma gridQuant_square_error {R : ℝ} (hR : 0