Download data/lean/OAI/Probability/SKRatio/Quantization/Quantizer.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 6.54 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/SKRatio/Quantization/Quantizer.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Probability/SKRatio/Quantization/Quantizer.lean
-
curl -L -o Quantizer.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/SKRatio/Quantization/Quantizer.lean
6.54 kB
| 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<R) {N : β} (hN : 0<N) : | |
| StrictMono (gridCut R N) := by | |
| intro i j hij | |
| have hc : (i.val:β)<(j.val:β) := by exact_mod_cast hij | |
| exact add_lt_add_right (mul_lt_mul_of_pos_left hc (by positivity : 0<2*R/(N:β))) _ | |
| lemma gridCut_zero (R : β) (N : β) : gridCut R N 0= -R := by simp [gridCut] | |
| lemma gridCut_last (R : β) {N : β} (hN : 0<N) : gridCut R N (Fin.last N)=R := by | |
| simp only [gridCut,Fin.val_last,div_mul_cancelβ _ ((Nat.cast_ne_zero.mpr hN.ne' : (N:β)β 0))] | |
| ring | |
| lemma gridRep_zero (R : β) (N : β) : gridRep R N 0= -R := by simp [gridRep] | |
| lemma gridRep_last (R : β) {N : β} (hN : 0<N) : gridRep R N (Fin.last (N+1))=R := by | |
| simp only [gridRep,Fin.val_last,min_eq_right (show Nβ€N+1 from by omega),div_mul_cancelβ _ ((Nat.cast_ne_zero.mpr hN.ne' : (N:β)β 0))] | |
| ring | |
| lemma gridRep_middle (R : β) {N : β} (j : Fin (N+2)) (hj : j.val<N+1) : | |
| gridRep R N j=gridCut R N β¨j.val,hjβ© := by | |
| simp only [gridRep,gridCut,min_eq_left (show j.valβ€N from by omega)] | |
| lemma gridRep_abs_le {R : β} (hR : 0β€R) {N : β} (hN : 0<N) (j : Fin (N+2)) : | |
| |gridRep R N j|β€R := by | |
| have h0 : (0:β)β€(min j.val N:β) := Nat.cast_nonneg _ | |
| have h1 : ((min j.val N:β):β)β€(N:β) := by exact_mod_cast min_le_right j.val N | |
| have hΞ΄ : 0β€2*R/(N:β) := by positivity | |
| have hh := mul_le_mul_of_nonneg_left h1 hΞ΄ | |
| rw [div_mul_cancelβ _ ((Nat.cast_ne_zero.mpr hN.ne' : (N:β)β 0))] at hh | |
| unfold gridRep | |
| exact abs_le.mpr β¨by nlinarith [mul_nonneg hΞ΄ h0],by linarithβ© | |
| lemma gridQuant_left {R : β} (hR : 0<R) {N : β} (hN : 0<N) {x : β} (hx : xβ€-R) : | |
| gridQuant R N x= -R := by | |
| unfold gridQuant | |
| have he : intervalBin (gridCut R N) x=0 := (intervalBin_eq_zero (by omega) | |
| (gridCut_strictMono hR hN) x).mpr (by simpa [gridCut] using hx) | |
| rw [he,gridRep_zero] | |
| lemma gridQuant_right {R : β} (hR : 0<R) {N : β} (hN : 0<N) {x : β} (hx : R<x) : | |
| gridQuant R N x=R := by | |
| unfold gridQuant | |
| have he : intervalBin (gridCut R N) x=Fin.last (N+1) := (intervalBin_eq_last (by omega) | |
| (gridCut_strictMono hR hN) x).mpr (by simpa only [Nat.add_sub_cancel,gridCut,div_mul_cancelβ _ ((Nat.cast_ne_zero.mpr hN.ne' : (N:β)β 0)),show -R+2*R=R by ring] using hx) | |
| rw [he,gridRep_last R hN] | |
| lemma gridQuant_middle {R : β} (hR : 0<R) {N : β} (hN : 0<N) {x : β} | |
| (hxL : -R<x) (hxU : xβ€R) : |x-gridQuant R N x|β€2*R/N := by | |
| let j := intervalBin (gridCut R N) x | |
| have hj0 : jβ 0 := fun hh => 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<j.val := by have hh : j.valβ 0 := fun he => hj0 (Fin.ext he); omega | |
| have hjN : j.val<N+1 := by | |
| have hb := j.isLt | |
| have hh : j.valβ N+1 := fun he => 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<R) {N : β} (hN : 0<N) (x : β) : | |
| (x-gridQuant R N x)^2β€(2*R/N)^2 + squareTail R x := by | |
| by_cases hxL : xβ€-R | |
| Β· rw [gridQuant_left hR hN hxL] | |
| by_cases he : x= -R | |
| Β· simp only [he,sub_self,zero_pow (by decide : (2:β)β 0)] | |
| exact add_nonneg (sq_nonneg _) (squareTail_nonneg _ _) | |
| Β· have hlt : x < -R := lt_of_le_of_ne hxL he | |
| have hx : R < |x| := by rw [abs_of_neg (by linarith)]; linarith | |
| rw [squareTail,ite_eq_left hx] | |
| nlinarith [sq_nonneg (2*R/(N:β))] | |
| Β· by_cases hxU : xβ€R | |
| Β· have hb := gridQuant_middle hR hN (lt_of_not_ge hxL) hxU | |
| have hs : (x-gridQuant R N x)^2β€(2*R/N)^2 := by | |
| simpa only [sq_abs] using (sq_le_sqβ (abs_nonneg _) (by positivity : 0β€2*R/(N:β))).mpr hb | |
| linarith [squareTail_nonneg R x] | |
| Β· have hx := lt_of_not_ge hxU | |
| rw [gridQuant_right hR hN hx,squareTail,ite_eq_left (by rw [abs_of_pos (hR.trans hx)]; exact hx)] | |
| nlinarith [sq_nonneg (2*R/(N:β))] | |
| lemma measurable_gridQuant {R : β} (hR : 0<R) {N : β} (hN : 0<N) : | |
| Measurable (gridQuant R N) := | |
| (measurable_of_countable (gridRep R N)).comp (measurable_intervalBin (by omega) (gridCut_strictMono hR hN)) | |
| lemma gridQuant_compact_error {Ξ΄ R : β} (_ : 0<Ξ΄) (hR : 0<R) {N : β} (hN : 0<N) | |
| (hmesh : 2*R/(N:β)<Ξ΄) | |
| (htail : β x : β, Rβ€|x| β |tanh x-tanh (if 0β€x then R else -R)|<Ξ΄) (x : β) : | |
| dist (compactWeight x) (compactWeight (gridQuant R N x))<Ξ΄ := by | |
| change |w x-w (gridQuant R N x)|<Ξ΄ | |
| rw [w,w,sub_sub_sub_cancel_left,abs_sub_comm] | |
| change |tanh x-tanh (gridQuant R N x)|<Ξ΄ | |
| by_cases hxL : xβ€-R | |
| Β· rw [gridQuant_left hR hN hxL] | |
| have hh := htail x (by rw [abs_of_neg (by linarith)]; linarith) | |
| simpa only [ite_eq_right (show Β¬0β€x by linarith)] using hh | |
| Β· by_cases hxU : xβ€R | |
| Β· exact ((Calculus.tanh_lipschitz_abs _ _).trans | |
| (gridQuant_middle hR hN (lt_of_not_ge hxL) hxU)).trans_lt hmesh | |
| Β· have hx := lt_of_not_ge hxU | |
| rw [gridQuant_right hR hN hx] | |
| have hh := htail x (by rw [abs_of_pos (hR.trans hx)]; exact hx.le) | |
| simpa only [ite_eq_left (hR.trans hx).le] using hh | |
| end SKRatio.Bins | |
| end | |
| end OAI | |