Download data/lean/OAI/Combinatorics/BinaryEdit/CodeNumerics.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 8.85 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/BinaryEdit/CodeNumerics.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/BinaryEdit/CodeNumerics.lean
-
curl -L -o CodeNumerics.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/BinaryEdit/CodeNumerics.lean
8.85 kB
| import Mathlib.Analysis.SpecialFunctions.Log.Base | |
| import Mathlib.Tactic | |
| namespace OAI | |
| /-! Numerical estimates used in the fixed-length binary conversion. -/ | |
| open Filter Asymptotics | |
| namespace TreeEdit.BinaryLower.Code | |
| /-- The segment threshold used in the prescribed-length code construction. -/ | |
| noncomputable def segmentThreshold (M : β) : β := | |
| Nat.ceil (120 * Real.logb 2 (M : β)) | |
| /-- Eventually the error from the short block-pair spans fits within half a block. -/ | |
| theorem eventually_short_pair_error : | |
| βαΆ M : β in atTop, | |
| 0 < M β§ 2 * (M : β) * segmentThreshold M β€ (M : β) ^ 2 / 2 := by | |
| have hlog : (fun M : β => Real.logb 2 (M : β)) =o[atTop] | |
| (fun M : β => (M : β)) := | |
| (Real.isLittleO_logb_id_atTop (b := 2)).comp_tendsto | |
| tendsto_natCast_atTop_atTop | |
| have hb := hlog.bound (show (0 : β) < 1 / 960 by norm_num) | |
| filter_upwards [hb, eventually_ge_atTop (8 : β)] with M hM h8 | |
| have hpos : 0 < M := by omega | |
| have hM0 : (0 : β) β€ M := Nat.cast_nonneg M | |
| have hM8 : (8 : β) β€ M := by exact_mod_cast h8 | |
| have hM1 : (1 : β) β€ M := by linarith | |
| have hlog0 : 0 β€ Real.logb 2 (M : β) := | |
| Real.logb_nonneg (by norm_num) hM1 | |
| have hlogbound : Real.logb 2 (M : β) β€ (1 / 960 : β) * M := by | |
| simpa only [Real.norm_eq_abs, abs_of_nonneg hlog0, abs_of_nonneg hM0] using hM | |
| have hceil : (segmentThreshold M : β) < 120 * Real.logb 2 (M : β) + 1 := by | |
| exact Nat.ceil_lt_add_one (by positivity) | |
| have hsmall : (segmentThreshold M : β) β€ (M : β) / 4 := by | |
| linarith | |
| refine β¨hpos, ?_β© | |
| have := mul_le_mul_of_nonneg_left hsmall (show 0 β€ 2 * (M : β) by positivity) | |
| nlinarith | |
| /-- An absolute integer threshold exists, as required in the manuscript. -/ | |
| theorem exists_short_pair_threshold : | |
| β Mβ : β, β M : β, Mβ β€ M β | |
| 0 < M β§ 2 * (M : β) * segmentThreshold M β€ (M : β) ^ 2 / 2 := by | |
| exact eventually_atTop.1 eventually_short_pair_error | |
| /-- Removing the additive error in the block-matching estimate gives the | |
| constant 402. The nonzero original deficit is integral, hence at least one. -/ | |
| theorem deficit_from_additive {P r Ξ΄ e : β} | |
| (hP : 0 β€ P) (hr : 1 β€ r) | |
| (hadd : P * r β€ 201 * Ξ΄ + e) (herr : e β€ P / 2) : | |
| P / 402 * r β€ Ξ΄ := by | |
| have hPr : P β€ P * r := by nlinarith | |
| nlinarith | |
| /-- Combining the deficit inequality with the two ordinary-edit comparisons | |
| produces exactly the constant 804, without changing the edit metric. -/ | |
| theorem edit_lower_from_deficit {P r r' d d' : β} | |
| (hP : 0 β€ P) (hdeficit : P / 402 * r β€ r') | |
| (hleft : r' β€ d') (hright : d β€ 2 * r) : | |
| P / 804 * d β€ d' := by | |
| have := mul_le_mul_of_nonneg_left hright (show 0 β€ P / 804 by positivity) | |
| nlinarith | |
| /-- The equal-label and unequal-label counts combine into the additive | |
| estimate. All quantities are natural cardinalities, so no rounding is hidden. -/ | |
| theorem block_counts_to_additive {M P aβ L r matched equal unequal Ξ΄ : β} | |
| (hpartition : matched = equal + unequal) | |
| (htotal : matched + Ξ΄ = M * P) | |
| (hdeficit : r + L = M) | |
| (hequal : equal β€ P * L) | |
| (hunequal : unequal β€ 200 * Ξ΄ + 2 * M * aβ) : | |
| P * r β€ 201 * Ξ΄ + 2 * M * aβ := by | |
| nlinarith | |
| /-- Integral form of the error absorption, including the zero-deficit case. -/ | |
| theorem natural_deficit_from_additive {P r Ξ΄ e : β} | |
| (hadd : P * r β€ 201 * Ξ΄ + e) (herr : 2 * e β€ P) : | |
| P * r β€ 402 * Ξ΄ := by | |
| by_cases hr : r = 0 | |
| Β· simp [hr] | |
| have hr1 : 1 β€ r := Nat.one_le_iff_ne_zero.mpr hr | |
| have hPr : P β€ P * r := by nlinarith | |
| nlinarith | |
| /-- Cast the integral matching conclusion into the exact real-valued lower | |
| bound used in the statement of the fixed-length conversion theorem. -/ | |
| theorem real_deficit_of_natural {P r Ξ΄ : β} | |
| (h : P * r β€ 402 * Ξ΄) : | |
| (P : β) / 402 * r β€ Ξ΄ := by | |
| have h' : (P : β) * r β€ 402 * Ξ΄ := by exact_mod_cast h | |
| linarith | |
| /-- The two common formulas for the segment threshold agree at positive M. -/ | |
| theorem segmentThreshold_eq (M : β) : | |
| segmentThreshold M = | |
| Nat.ceil (40 * Real.logb 2 ((M : β) ^ 3)) := by | |
| unfold segmentThreshold | |
| rw [Real.logb_pow] | |
| congr 1 | |
| ring | |
| /-- Elementary failure tail at the source logarithmic segment threshold. -/ | |
| theorem code_failure_power_bound {M : β} (hM : 1 β€ M) : | |
| (9 / 10 : β) ^ segmentThreshold M * (M : β) ^ 12 β€ 1 := by | |
| have hM1 : (1 : β) β€ M := by exact_mod_cast hM | |
| have hMpos : (0 : β) < M := by linarith | |
| have hlog0 := Real.log_nonneg hM1 | |
| have hlog2pos : 0 < Real.log 2 := Real.log_pos (by norm_num) | |
| have hlog2le : Real.log 2 β€ 1 := by | |
| have := Real.log_le_sub_one_of_pos (show (0 : β) < 2 by norm_num) | |
| norm_num at this β’ | |
| exact this | |
| have hlogbase : Real.log (M : β) β€ Real.logb 2 (M : β) := by | |
| rw [Real.logb, le_div_iffβ hlog2pos] | |
| nlinarith | |
| have hthreshold : 120 * Real.log (M : β) β€ (segmentThreshold M : β) := by | |
| have hceil := Nat.le_ceil (120 * Real.logb 2 (M : β)) | |
| change 120 * Real.logb 2 (M : β) β€ (segmentThreshold M : β) at hceil | |
| linarith | |
| have hbase : (9 / 10 : β) β€ Real.exp (-(1 / 10 : β)) := by | |
| have := Real.add_one_le_exp (-(1 / 10 : β)) | |
| linarith | |
| have htail : (9 / 10 : β) ^ segmentThreshold M β€ | |
| Real.exp (-(12 * Real.log (M : β))) := by | |
| calc | |
| (9 / 10 : β) ^ segmentThreshold M β€ | |
| Real.exp (-(1 / 10 : β)) ^ segmentThreshold M := | |
| pow_le_pow_leftβ (by norm_num) hbase _ | |
| _ = Real.exp ((segmentThreshold M : β) * (-(1 / 10 : β))) := | |
| (Real.exp_nat_mul _ _).symm | |
| _ β€ Real.exp (-(12 * Real.log (M : β))) := | |
| Real.exp_monotone (by linarith) | |
| have hexp : Real.exp (-(12 * Real.log (M : β))) = ((M : β) ^ 12)β»ΒΉ := by | |
| rw [Real.exp_neg] | |
| congr 1 | |
| have := Real.exp_nat_mul (Real.log (M : β)) 12 | |
| norm_num only [Nat.cast_ofNat] at this | |
| rw [this, Real.exp_log hMpos] | |
| rw [hexp] at htail | |
| have hmul := mul_le_mul_of_nonneg_right htail | |
| (show 0 β€ (M : β) ^ 12 by positivity) | |
| simpa [ne_of_gt hMpos] using hmul | |
| /-- The segment-comparison count times the failure bound is below one. -/ | |
| theorem code_union_bound_lt_one {M : β} (hM : 2 β€ M) : | |
| (M : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 * | |
| (9 / 10 : β) ^ segmentThreshold M < 1 := by | |
| have hx : (2 : β) β€ M := by exact_mod_cast hM | |
| have hpos : (0 : β) < M := by linarith | |
| have hsq : (1 : β) β€ (M : β) ^ 2 := by nlinarith | |
| have hpoly : (M : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 β€ 8 * (M : β) ^ 8 := by | |
| calc | |
| (M : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 β€ | |
| (M : β) ^ 2 * (2 * (M : β) ^ 2) ^ 3 := by | |
| gcongr | |
| linarith | |
| _ = 8 * (M : β) ^ 8 := by ring | |
| have hfour : (8 : β) < (M : β) ^ 4 := by | |
| have h := pow_le_pow_leftβ (show (0 : β) β€ 2 by norm_num) hx 4 | |
| norm_num at h | |
| linarith | |
| have hpoly' : (M : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 < (M : β) ^ 12 := by | |
| apply hpoly.trans_lt | |
| have h := mul_lt_mul_of_pos_right hfour (show (0 : β) < (M : β) ^ 8 by positivity) | |
| convert h using 1; ring | |
| have hmul := mul_lt_mul_of_pos_right hpoly' | |
| (show (0 : β) < (9 / 10 : β) ^ segmentThreshold M by positivity) | |
| exact hmul.trans_le (by simpa [mul_comm] using code_failure_power_bound (by omega : 1 β€ M)) | |
| /-- One absolute threshold supplies both probabilistic and deterministic estimates. -/ | |
| theorem exists_code_numerical_threshold : | |
| β Mβ : β, β M : β, Mβ β€ M β | |
| 2 β€ M β§ | |
| 2 * (M : β) * segmentThreshold M β€ (M : β) ^ 2 / 2 β§ | |
| (M : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 * | |
| (9 / 10 : β) ^ segmentThreshold M < 1 := by | |
| rcases exists_short_pair_threshold with β¨Mβ, hMββ© | |
| refine β¨max Mβ 2, fun M hM => ?_β© | |
| have hMtwo : 2 β€ M := (le_max_right Mβ 2).trans hM | |
| exact β¨hMtwo, (hMβ M ((le_max_left Mβ 2).trans hM)).2, | |
| code_union_bound_lt_one hMtwoβ© | |
| /-- Alphabet sizes below M only decrease the comparison count. -/ | |
| theorem code_union_bound_for_alphabet {K M : β} (hK : K β€ M) (hM : 2 β€ M) : | |
| (K : β) ^ 2 * ((M : β) ^ 2 + 1) ^ 3 * | |
| (9 / 10 : β) ^ segmentThreshold M < 1 := by | |
| apply lt_of_le_of_lt _ (code_union_bound_lt_one hM) | |
| gcongr | |
| /-- The short-pair threshold is no larger than the block width once its | |
| error-absorption inequality holds. -/ | |
| theorem segmentThreshold_le_square {M : β} (hM : 1 β€ M) | |
| (h : 2 * (M : β) * segmentThreshold M β€ (M : β) ^ 2 / 2) : | |
| segmentThreshold M β€ M ^ 2 := by | |
| have hM' : (1 : β) β€ M := by exact_mod_cast hM | |
| have hnon : (0 : β) β€ segmentThreshold M := Nat.cast_nonneg _ | |
| have h' : (segmentThreshold M : β) β€ (M : β) ^ 2 := by nlinarith | |
| exact_mod_cast h' | |
| end TreeEdit.BinaryLower.Code | |
| end OAI | |