Download data/lean/OAI/Combinatorics/EditDistance/WindowRecursion.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/EditDistance/WindowRecursion.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/EditDistance/WindowRecursion.lean
-
curl -L -o WindowRecursion.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/EditDistance/WindowRecursion.lean
11 kB
| import OAI.Combinatorics.EditDistance.CutModels | |
| import OAI.Combinatorics.EditDistance.DyadicScales | |
| import OAI.Combinatorics.EditDistance.Windows | |
| namespace OAI | |
| /-! | |
| # Dyadic-window recursion and insertion bounds | |
| -/ | |
| namespace EditUpper | |
| attribute [local instance] Classical.propDecidable | |
| open scoped BigOperators | |
| open Finset EditDistortion EditWindow EditCutModel | |
| variable {A : Type*} | |
| def samples (x : List A) (start l s : β) : Fin s β List (Option A) := | |
| fun i => window x (start+i.val) (l-s+1) | |
| noncomputable def piece (M : Model (List (Option A))) (x y : List A) (start l s : β) : β := | |
| EditCutHash.meanHistogram M.weight (EditCutHash.rate s) M.cut | |
| (samples x start l s) (samples y start l s) | |
| lemma piece_nonneg (M : Model (List (Option A))) (x y : List A) (start l s : β) (hs : 0 < s) : | |
| 0 β€ piece M x y start l s := | |
| EditCutHash.meanHistogram_nonneg M.nonneg (EditCutHash.rate_nonneg hs) _ _ _ | |
| noncomputable def lift (M : Model (List (Option A))) (b l : β) : Model (List A) := | |
| (Model.total (fun j : Fin b Γ Fin (Nat.log 2 l+1) => | |
| (M.histogram (2^j.2.val) (EditCutHash.rate (2^j.2.val)) | |
| (EditCutHash.rate_nonneg (pow_pos (by omega) _))).pullback | |
| (fun x => samples x (j.1.val*l) l (2^j.2.val)))).scale 16 (by norm_num) | |
| lemma lift_dist (M : Model (List (Option A))) (b l : β) (x y : List A) : | |
| (lift M b l).dist x y = | |
| 16 * β j : Fin b, β r : Fin (Nat.log 2 l+1), piece M x y (j.val*l) l (2^r.val) := by | |
| rw [lift,Model.scale_dist,Model.total_dist,Fintype.sum_prod_type] | |
| congr 1 | |
| apply sum_congr rfl | |
| intro j _ | |
| apply sum_congr rfl | |
| intro r _ | |
| rw [Model.pullback_dist,Model.histogram_dist] | |
| rfl | |
| lemma block_indel_bound (x y : List A) (start l : β) : | |
| indel (window x start l) (window y start l) β€ 2*l := by | |
| have h : indel (window x start l) (window y start l) β€ | |
| (window x start l).length+(window y start l).length := by | |
| exact cost_le ((Script.to_nil (s := false) _).trans (Script.to_nil _).symm) | |
| simpa only [window_length,two_mul] using h | |
| lemma block_lower (M : Model (List (Option A))) (l : β) (hl : 0 < l) | |
| (hM : β u v : List (Option A), u.length β€ l β v.length β€ l β | |
| (indel u v : β) β€ M.dist u v) (x y : List A) (start : β) : | |
| (indel (window x start l) (window y start l) : β) β€ | |
| 16 * β r : Fin (Nat.log 2 l+1), piece M x y start l (2^r.val) := by | |
| let delta := indel (window x start l) (window y start l) | |
| have hnn (r : Fin (Nat.log 2 l+1)) : 0 β€ piece M x y start l (2^r.val) := | |
| piece_nonneg _ _ _ _ _ _ (pow_pos (by omega) _) | |
| by_cases hd : delta = 0 | |
| Β· change (delta : β) β€ _ | |
| rw [hd,Nat.cast_zero] | |
| exact mul_nonneg (by norm_num) (sum_nonneg (fun r _ => hnn r)) | |
| Β· obtain β¨r,hs,hsl,hds,hsdβ© := EditDyadic.scale hl (Nat.pos_of_ne_zero hd) (block_indel_bound x y start l) | |
| have hcross (i j : Fin (2^r.val)) : ((2^r.val : β) : β) β€ | |
| EditCutHash.distance M.weight M.cut (samples x start l (2^r.val) i) (samples y start l (2^r.val) j) := by | |
| rw [Model.distance_eq] | |
| have htri := segment_cross (fun k => x[k]?) (fun k => y[k]?) start l (2^r.val) hs hsl i j | |
| change delta β€ 2*(2^r.val-1)+indel (samples x start l (2^r.val) i) | |
| (samples y start l (2^r.val) j) at htri | |
| have hloss : 2^r.val β€ indel (samples x start l (2^r.val) i) | |
| (samples y start l (2^r.val) j) := by omega | |
| calc | |
| _ β€ (indel (samples x start l (2^r.val) i) (samples y start l (2^r.val) j) : β) := by exact_mod_cast hloss | |
| _ β€ _ := hM _ _ (by simp only [samples,window_length]; omega) (by simp only [samples,window_length]; omega) | |
| have hh := EditCutHash.meanHistogram_separated (by omega : 0 < 2^r.val) M.nonneg M.cut | |
| (samples x start l (2^r.val)) (samples y start l (2^r.val)) hcross | |
| change ((2^r.val : β) : β) β€ piece M x y start l (2^r.val) at hh | |
| have hsum := single_le_sum (fun j _ => hnn j) (mem_univ r) | |
| have hdsr : (delta : β) β€ 16*((2^r.val : β) : β) := by exact_mod_cast hds | |
| change (delta : β) β€ _ | |
| nlinarith | |
| lemma lift_lower (M : Model (List (Option A))) (b l : β) (hl : 0 < l) | |
| (hM : β u v : List (Option A), u.length β€ l β v.length β€ l β | |
| (indel u v : β) β€ M.dist u v) (x y : List A) | |
| (hx : x.length β€ b*l) (hy : y.length β€ b*l) : | |
| (indel x y : β) β€ (lift M b l).dist x y := by | |
| have h1 := indel_le_window x y (b*l) hx hy | |
| have h2 := indel_segment_blocks (fun k => x[k]?) (fun k => y[k]?) l b | |
| change indel (window x 0 (b*l)) (window y 0 (b*l)) β€ _ at h2 | |
| have h3 : (indel x y : β) β€ | |
| β j : Fin b, (indel (window x (j.val*l) l) (window y (j.val*l) l) : β) := by | |
| rw [Fin.sum_univ_eq_sum_range (fun j => (indel (window x (j*l) l) (window y (j*l) l) : β)) b] | |
| exact_mod_cast h1.trans h2 | |
| rw [lift_dist,mul_sum] | |
| apply h3.trans | |
| apply sum_le_sum | |
| intro j _ | |
| exact block_lower M l hl hM x y (j.val*l) | |
| lemma piece_insert_general (M : Model (List (Option A))) {D : β} (hD : 0 β€ D) | |
| (hM : β u v, M.dist u v β€ D*(indel u v : β)) | |
| (p q : List A) (a : A) (start l s : β) (hs : 0 < s) : | |
| piece M (p++q) (p++a::q) start l s β€ 4*Real.log (2*(s : β)^2)*D := by | |
| have hup := EditCutHash.meanHistogram_upper M.nonneg (EditCutHash.rate_nonneg hs) M.cut | |
| (samples (p++q) start l s) (samples (p++a::q) start l s) | |
| have hsum : (β i : Fin s, EditCutHash.distance M.weight M.cut | |
| (samples (p++q) start l s i) (samples (p++a::q) start l s i)) β€ (s : β)*(2*D) := by | |
| calc | |
| _ β€ β _i : Fin s, 2*D := by | |
| apply sum_le_sum | |
| intro i _ | |
| rw [Model.distance_eq] | |
| have hcost := window_insert_cost p q a (start+i.val) (l-s+1) | |
| have hcr : (indel (samples (p++q) start l s i) (samples (p++a::q) start l s i) : β) β€ 2 := by exact_mod_cast hcost | |
| exact (hM _ _).trans (by nlinarith) | |
| _ = _ := by simp only [sum_const,card_univ,Fintype.card_fin,nsmul_eq_mul] | |
| have hr := EditCutHash.rate_nonneg hs | |
| have h := hup.trans (mul_le_mul_of_nonneg_left hsum (by positivity)) | |
| change piece M (p++q) (p++a::q) start l s β€ _ at h | |
| apply h.trans_eq | |
| unfold EditCutHash.rate | |
| have hs0 : (s : β) β 0 := by exact_mod_cast (Nat.ne_of_gt hs) | |
| field_simp | |
| ring | |
| lemma piece_before (M : Model (List (Option A))) (p q : List A) (a : A) | |
| (start l s : β) (_hs : 0 < s) (hsl : s β€ l) (hp : start+l β€ p.length) : | |
| piece M (p++q) (p++a::q) start l s = 0 := by | |
| have he : samples (p++q) start l s = samples (p++a::q) start l s := by | |
| funext i | |
| exact (window_before p q a (by omega)).symm | |
| unfold piece | |
| rw [βhe,EditCutHash.meanHistogram_self] | |
| lemma piece_after (M : Model (List (Option A))) (p q : List A) (a : A) | |
| (start l s : β) (hs : 0 < s) (hp : p.length β€ start) : | |
| piece M (p++q) (p++a::q) start l s β€ 2 := by | |
| apply EditCutHash.meanHistogram_shift hs M.nonneg (EditCutHash.rate_nonneg hs) | |
| intro i | |
| exact (window_after p q a (by omega)).symm | |
| end EditUpper | |
| namespace EditUpper | |
| attribute [local instance] Classical.propDecidable | |
| open scoped BigOperators | |
| open Finset EditDistortion EditWindow EditCutModel | |
| variable {A : Type*} | |
| lemma sum_block_insert (M : Model (List (Option A))) {D : β} (hD : 0 β€ D) | |
| (hM : β u v, M.dist u v β€ D*(indel u v : β)) | |
| (p q : List A) (a : A) (b l s : β) (hl : 0 < l) (hs : 0 < s) (hsl : s β€ l) : | |
| (β j : Fin b, piece M (p++q) (p++a::q) (j.val*l) l s) β€ | |
| 2*(b : β)+4*Real.log (2*(s : β)^2)*D := by | |
| let K := 4*Real.log (2*(s : β)^2)*D | |
| have hK : 0 β€ K := by | |
| dsimp [K] | |
| have hs1 : (1 : β) β€ s := by exact_mod_cast hs | |
| exact mul_nonneg (mul_nonneg (by norm_num) (Real.log_nonneg (by nlinarith))) hD | |
| have hj (j : Fin b) : piece M (p++q) (p++a::q) (j.val*l) l s β€ | |
| 2+(if j.val = p.length/l then K else 0) := by | |
| by_cases he : j.val = p.length/l | |
| Β· rw [ite_eq_left he] | |
| have hh := piece_insert_general M hD hM p q a (j.val*l) l s hs | |
| change _ β€ K at hh | |
| linarith | |
| Β· rw [ite_eq_right he,add_zero] | |
| by_cases hlt : j.val < p.length/l | |
| Β· have hp : j.val*l+l β€ p.length := by | |
| have h1 := Nat.mul_le_mul_right l (show j.val+1 β€ p.length/l by omega) | |
| have h2 := Nat.div_mul_le_self p.length l | |
| nlinarith | |
| rw [piece_before M p q a (j.val*l) l s hs hsl hp] | |
| norm_num | |
| Β· have hp : p.length β€ j.val*l := by | |
| have h1 := Nat.mul_le_mul_right l (show p.length/l+1 β€ j.val by omega) | |
| have h2 := Nat.lt_mul_div_succ p.length hl | |
| nlinarith | |
| exact piece_after M p q a (j.val*l) l s hs hp | |
| have hsum := sum_le_sum (fun j (_ : j β (univ : Finset (Fin b))) => hj j) | |
| rw [sum_add_distrib] at hsum | |
| have hc : (β j : Fin b, (if j.val = p.length/l then K else 0)) β€ K := by | |
| rw [Fin.sum_univ_eq_sum_range (fun j => if j = p.length/l then K else 0) b,sum_ite_eq'] | |
| split <;> linarith | |
| simp only [sum_const,card_univ,Fintype.card_fin,nsmul_eq_mul] at hsum | |
| dsimp [K] at hc hsum | |
| linarith | |
| lemma lift_insert_raw (M : Model (List (Option A))) {D : β} (hD : 0 β€ D) | |
| (hM : β u v, M.dist u v β€ D*(indel u v : β)) | |
| (p q : List A) (a : A) (b l : β) (hl : 0 < l) : | |
| (lift M b l).dist (p++q) (p++a::q) β€ | |
| 16*(Nat.log 2 l+1 : β)*(2*(b : β)+4*Real.log (2*(l : β)^2)*D) := by | |
| rw [lift_dist,sum_comm] | |
| have hscale (r : Fin (Nat.log 2 l+1)) : (β j : Fin b, | |
| piece M (p++q) (p++a::q) (j.val*l) l (2^r.val)) β€ | |
| 2*(b : β)+4*Real.log (2*(l : β)^2)*D := by | |
| have hr := EditDyadic.scale_le hl r | |
| have hp : 0 < 2^r.val := pow_pos (by omega) _ | |
| have h := sum_block_insert M hD hM p q a b l (2^r.val) hl hp hr | |
| apply h.trans | |
| have hrr : ((2^r.val : β) : β) β€ l := by exact_mod_cast hr | |
| have hlog : Real.log (2*(((2^r.val : β) : β))^2) β€ Real.log (2*(l : β)^2) := by | |
| apply Real.log_le_log (by positivity) | |
| gcongr | |
| nlinarith | |
| calc | |
| _ β€ 16 * β _r : Fin (Nat.log 2 l+1), (2*(b : β)+4*Real.log (2*(l : β)^2)*D) := | |
| mul_le_mul_of_nonneg_left (sum_le_sum (fun r _ => hscale r)) (by norm_num) | |
| _ = _ := by simp only [sum_const,card_univ,Fintype.card_fin,nsmul_eq_mul]; ring | |
| lemma model_indel_lipschitz (M : Model (List A)) {D : β} | |
| (hstep : β (p q : List A) (a : A), M.dist (p++q) (p++a::q) β€ D) : | |
| β x y, M.dist x y β€ D*(indel x y : β) := by | |
| have he {x y : List A} (h : Step false x y) : M.dist x y β€ D := by | |
| cases h with | |
| | insert p q a => exact hstep p q a | |
| | delete p q a => rw [Model.dist_symm]; exact hstep p q a | |
| | substitute h => contradiction | |
| have hs {n : β} {x y : List A} (h : Script false n x y) : M.dist x y β€ D*(n : β) := by | |
| induction h with | |
| | nil x => simp | |
| | @cons n x y z e h ih => | |
| have ht := M.dist_triangle x y z | |
| have hh := he e | |
| push_cast | |
| nlinarith | |
| intro x y | |
| exact hs (cost_spec x y) | |
| end EditUpper | |
| end OAI | |