diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Cast.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Cast.lean index 60e3ec83fdc8cde3f97b3d13a62350b7cdafce76..f4c4531fc67f726f5e6fcee69917d7ed8724cc7d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Cast.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Cast.lean @@ -1,185 +1,185 @@ -import OAI.Analysis.FourierLLogL.Assembly.Receipt -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace CastShow -open Troupe - -def tailE (s : Scene M) (J : ℕ) (x u : Grid M) := - ∑ n ∈ Finset.Ico s.here.lev J, ∑ k ∈ Finset.range (2^n), - FinJump ⟨n,k⟩ (finMod s.wage u) x - -private lemma lone (s : Scene M) (p : Node) (hv : p.valid) (x u : Grid M) (h : x∈p.block M) : - ∑ k∈Finset.range (2^p.lev), FinJump ⟨p.lev,k⟩ (finMod s.wage u) x = FinJump p (finMod s.wage u) x := by - rw [Finset.sum_eq_single p.ix] - · intro b hb hh - rw [FinJump, Gi_out _ (show pos x∉ Node.cells ⟨p.lev,b⟩ from by - intro H - exact Set.disjoint_left.mp (cells_dis hh) H h),zero_smul] - intro H; exact (H (Finset.mem_range.mpr hv)).elim - -lemma tail_move {s : Scene M} (x u : Grid M) (hh : s.here.valid) - (hp : s.here.lev < J) (h : x∈s.here.block M) (b : Bool) : - tailE s J x u=FinJump s.here (finMod s.wage u) x+tailE (s.smaller b) J x u := by - unfold tailE - let f (n) := ∑ k∈Finset.range (2^n),FinJump ⟨n,k⟩ (finMod s.wage u) x - let n := s.here.lev - have he : Finset.Ico s.here.lev J=insert n (Finset.Ico (n+1) J) := by - ext i; simp only [Finset.mem_insert,Finset.mem_Ico,n]; omega - rw [he,Finset.sum_insert (by simp),lone s s.here hh x u h] - cases b <;> rfl - -lemma tail_attempt {s : Scene M} (v : Update j M s.here) (x u : Grid M) (J : ℕ) : - tailE s J x u=tailE (s.lose v) J x u + tailE (s.win v) J x u := by - let f := finMod s.wage u; let g := finMod (s.lose v).wage u - let w := finMod (s.win v).wage u - have H : f=g+w := by - funext i - unfold f g w lose win finMod; simp only [Pi.add_apply] - by_cases HH : v.take s.fund.mood i - all_goals simp [HH] - have HV (p : Node) : - FinJump p (finMod s.wage u) x=FinJump p g x+FinJump p w x := by - unfold FinJump - have Hx (x : Grid M) : finMod s.wage u x=g x+w x := congrFun H x - simp_rw [Hx,smul_add,avg_sum,Finset.sum_add_distrib,smul_add] - unfold tailE; simp_rw [HV,Finset.sum_add_distrib]; rfl - -lemma tail_fail (s : Scene M) (u x : Grid M) (h : Safe s j J f) - (hp : finGood (cutoff j) J f x) (hh : x∈s.here.block M) - (ht : ¬s.openHere j J f) : - tailE s J x u=0 := by - by_cases hb : s.here.lev < J - · have hx := hp s.here h.within.1 hb hh - have he : s.mass=0 := le_antisymm (le_of_not_gt (fun hv => ht ⟨hb,hx,hv⟩)) h.mass_lower - have H (i) (hf : i∈s.here.block M) : s.wage i=0 := by - have HE := mass_actor (s:=s) h.within h.positive i - rw [he,zero_mul] at HE - exact (mul_eq_zero.mp HE.symm).resolve_left ((dice_positive h.within).2 hf).ne' - unfold tailE; apply Finset.sum_eq_zero; intro n hn - apply Finset.sum_eq_zero; intro k hk - let p := Node.mk n k - by_cases hm : x∈p.block M - · have HI (y : Grid M) (hy : y∈p.block M) : finMod s.wage u y=0 := by - unfold finMod; rw [H y]; simp - have hf := coarse_const (Finset.mem_Ico.mp hn).1 (q:=p) (p:=s.here) (x:=pos x) (y:=pos y) hm hy - exact hf.mp hh - rw [FinJump,avg_sum] - rw [Finset.sum_eq_zero (fun i hi => by rw [HI _ ((mem_group _ _).1 hi),smul_zero])] - simp - · rw [FinJump, Gi_out p hm,zero_smul] - · unfold tailE; simp [Finset.Ico_eq_empty hb] - -lemma FinJump_align (s : Scene M) (hv : s.here.valid) (hm : s.here.lev < M) - (he : 0 < s.norm) (u x : Grid M) : - FinJump s.here (finMod s.wage u) x= - (s.norm:ℂ)*proceeds x u (Thread.Beat.mk s s ((step s).bit x)) := by - let p := s.here; let f := finMod s.wage u - let g (y : Grid M) := p.Hi (pos y) • f y - have H (b : Bool) : - (p.child b).avg g=((curl b:ℝ)*s.norm:ℝ)*scalar (s.smaller b) u := by - rw [avg_cx (child_ok p hv hm b)] - unfold Cx scalar; rw [Finset.mul_sum] - refine Finset.sum_congr rfl fun i _ => ?_ - let q := p.child b - by_cases hh : i∈q.block M - · change (q.dice M i:ℂ)*g i=(((curl b*s.norm:ℝ):ℝ):ℂ)*((↑(wt q s.wage s.norm i):ℂ)*_) - unfold g p wt - rw [heav_bit _ _ _ hh,Complex.real_smul] - have HR : (s.norm:ℂ) ≠ 0 := by exact_mod_cast he.ne' - unfold f finMod - push_cast; field_simp - have hx : q.dice M i=0 := by rw [dice_apply (child_ok p hv hm b),ite_eq_right hh] - have hi : wt (s.smaller b).here (s.smaller b).wage (s.smaller b).norm i=0 := by - unfold wt; rw [show (s.smaller b).here=q from rfl,hx]; simp - rw [hx,hi]; simp - rw [FinJump, Complex.real_smul] - have hi : p.avg g=(s.norm:ℂ)*gain s u := by - rw [avg_split_complex] - rw [show p.L=p.child false from rfl,show p.R=p.child true from rfl,H,H] - unfold gain; simp only [curl,Bool.false_eq_true,↓reduceIte]; push_cast - ring - change (p.Gi (pos x):ℂ)*p.avg g=_*((p.Gi (pos x):ℂ)*_) - rw [hi]; ring - -/-- Payment from the already open plateau, without its children plateaus. -/ -def ordinary (u x : Grid M) (t : Play j M s) := - (s.norm:ℂ)*subtotal (proceeds x u) ((project t).stream x) - -def summons (u x : Grid M) {s : Scene M} (t : Play j M s) : ℂ := match t with -| .rest _ => 0 -| .ticket _ t => ordinary u x t + summons u x t -| .freeze _ t => summons u x t -| .attempt _ _ l r => summons u x l+summons u x r -| .jump s l r => if (step s).bit x then summons u x r else summons u x l - -lemma ordinary_jump (s : Scene M) (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) (u x : Grid M) : - let b := (step s).bit x - ordinary u x (.jump s l r)=(s.norm:ℂ)*proceeds x u ⟨s,s,b⟩+ - if b=true then ordinary u x r else ordinary u x l := by - intro b - unfold ordinary b - simp only [project,stream,subtotal_cons] - cases (step s).bit x <;> - simp only [smaller,Bool.false_eq_true,↓reduceIte,mul_add] - -/-- common bit selection is correct for the shifted Haar step -/ -lemma stage_shift (s : Scene M) (t : Play j M s) (u x : Grid M) - (h : t.Follows J f) (hp : x∈ s.here.block M) - (hh : finGood (cutoff j) J f x) : tailE s J x u=ordinary u x t+summons u x t := by - induction t with - | rest s => - rw [tail_fail s u x h.1 hh hp h.2] - unfold ordinary; simp [project,stream,subtotal,summons] - | ticket s t ih => - have HH := ih h.2.2.2.2 hp - change tailE s.reset _ _ _ = _ - rw [HH] - simp [ordinary,project,stream,subtotal,summons] - | freeze s t ih => - have H := ih h.2.2.2 hp - change tailE s.out _ _ _ = _ - rw [H] - simp [ordinary,project,stream,subtotal,summons,out] - | attempt s v l r il ir => - rw [tail_attempt v x u J, il h.2.2.2.2.1 hp, ir h.2.2.2.2.2 hp] - rw [show ordinary u x (.attempt s v l r) = ordinary u x l by - unfold ordinary; rw [project] - cases project l <;> rfl] - rw [summons] - rw [show ordinary u x r=0 by unfold ordinary; rw [show (s.win v).norm=0 from rfl]; simp]; ring - | jump s l r il ir => - obtain ⟨ha,hb,hc,hd,he⟩ := h - let b := (step s).bit x - have hM : s.here.lev < M := (Play.first hd).within.2 - have hs := share_small hb - have Hm := FinJump_align s ha.within.1 hM hs.1 u x - have HE := stream_child (less_self _) hp - rw [tail_move x u ha.within.1 hb.1.1 hp b,Hm] - change x ∈ (s.smaller b).here.block M at HE - unfold b at * - rcases HA : (step s).bit x - · rw [ordinary_jump s l r u x, summons,HA,ite_eq_right (by simp)] - rw [HA] at HE - rw [il hd HE]; simp only [Bool.false_eq_true,↓reduceIte,add_assoc] - · rw [ordinary_jump s l r u x, summons,HA,ite_eq_left rfl] - rw [HA] at HE - rw [ir he HE]; simp only [↓reduceIte,add_assoc] - -end CastShow - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Receipt +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace CastShow +open Troupe + +def tailE (s : Scene M) (J : ℕ) (x u : Grid M) := + ∑ n ∈ Finset.Ico s.here.lev J, ∑ k ∈ Finset.range (2^n), + FinJump ⟨n,k⟩ (finMod s.wage u) x + +private lemma lone (s : Scene M) (p : Node) (hv : p.valid) (x u : Grid M) (h : x∈p.block M) : + ∑ k∈Finset.range (2^p.lev), FinJump ⟨p.lev,k⟩ (finMod s.wage u) x = FinJump p (finMod s.wage u) x := by + rw [Finset.sum_eq_single p.ix] + · intro b hb hh + rw [FinJump, Gi_out _ (show pos x∉ Node.cells ⟨p.lev,b⟩ from by + intro H + exact Set.disjoint_left.mp (cells_dis hh) H h),zero_smul] + intro H; exact (H (Finset.mem_range.mpr hv)).elim + +lemma tail_move {s : Scene M} (x u : Grid M) (hh : s.here.valid) + (hp : s.here.lev < J) (h : x∈s.here.block M) (b : Bool) : + tailE s J x u=FinJump s.here (finMod s.wage u) x+tailE (s.smaller b) J x u := by + unfold tailE + let f (n) := ∑ k∈Finset.range (2^n),FinJump ⟨n,k⟩ (finMod s.wage u) x + let n := s.here.lev + have he : Finset.Ico s.here.lev J=insert n (Finset.Ico (n+1) J) := by + ext i; simp only [Finset.mem_insert,Finset.mem_Ico,n]; omega + rw [he,Finset.sum_insert (by simp),lone s s.here hh x u h] + cases b <;> rfl + +lemma tail_attempt {s : Scene M} (v : Update j M s.here) (x u : Grid M) (J : ℕ) : + tailE s J x u=tailE (s.lose v) J x u + tailE (s.win v) J x u := by + let f := finMod s.wage u; let g := finMod (s.lose v).wage u + let w := finMod (s.win v).wage u + have H : f=g+w := by + funext i + unfold f g w lose win finMod; simp only [Pi.add_apply] + by_cases HH : v.take s.fund.mood i + all_goals simp [HH] + have HV (p : Node) : + FinJump p (finMod s.wage u) x=FinJump p g x+FinJump p w x := by + unfold FinJump + have Hx (x : Grid M) : finMod s.wage u x=g x+w x := congrFun H x + simp_rw [Hx,smul_add,avg_sum,Finset.sum_add_distrib,smul_add] + unfold tailE; simp_rw [HV,Finset.sum_add_distrib]; rfl + +lemma tail_fail (s : Scene M) (u x : Grid M) (h : Safe s j J f) + (hp : finGood (cutoff j) J f x) (hh : x∈s.here.block M) + (ht : ¬s.openHere j J f) : + tailE s J x u=0 := by + by_cases hb : s.here.lev < J + · have hx := hp s.here h.within.1 hb hh + have he : s.mass=0 := le_antisymm (le_of_not_gt (fun hv => ht ⟨hb,hx,hv⟩)) h.mass_lower + have H (i) (hf : i∈s.here.block M) : s.wage i=0 := by + have HE := mass_actor (s:=s) h.within h.positive i + rw [he,zero_mul] at HE + exact (mul_eq_zero.mp HE.symm).resolve_left ((dice_positive h.within).2 hf).ne' + unfold tailE; apply Finset.sum_eq_zero; intro n hn + apply Finset.sum_eq_zero; intro k hk + let p := Node.mk n k + by_cases hm : x∈p.block M + · have HI (y : Grid M) (hy : y∈p.block M) : finMod s.wage u y=0 := by + unfold finMod; rw [H y]; simp + have hf := coarse_const (Finset.mem_Ico.mp hn).1 (q:=p) (p:=s.here) (x:=pos x) (y:=pos y) hm hy + exact hf.mp hh + rw [FinJump,avg_sum] + rw [Finset.sum_eq_zero (fun i hi => by rw [HI _ ((mem_group _ _).1 hi),smul_zero])] + simp + · rw [FinJump, Gi_out p hm,zero_smul] + · unfold tailE; simp [Finset.Ico_eq_empty hb] + +lemma FinJump_align (s : Scene M) (hv : s.here.valid) (hm : s.here.lev < M) + (he : 0 < s.norm) (u x : Grid M) : + FinJump s.here (finMod s.wage u) x= + (s.norm:ℂ)*proceeds x u (Thread.Beat.mk s s ((step s).bit x)) := by + let p := s.here; let f := finMod s.wage u + let g (y : Grid M) := p.Hi (pos y) • f y + have H (b : Bool) : + (p.child b).avg g=((curl b:ℝ)*s.norm:ℝ)*scalar (s.smaller b) u := by + rw [avg_cx (child_ok p hv hm b)] + unfold Cx scalar; rw [Finset.mul_sum] + refine Finset.sum_congr rfl fun i _ => ?_ + let q := p.child b + by_cases hh : i∈q.block M + · change (q.dice M i:ℂ)*g i=(((curl b*s.norm:ℝ):ℝ):ℂ)*((↑(wt q s.wage s.norm i):ℂ)*_) + unfold g p wt + rw [heav_bit _ _ _ hh,Complex.real_smul] + have HR : (s.norm:ℂ) ≠ 0 := by exact_mod_cast he.ne' + unfold f finMod + push_cast; field_simp + have hx : q.dice M i=0 := by rw [dice_apply (child_ok p hv hm b),ite_eq_right hh] + have hi : wt (s.smaller b).here (s.smaller b).wage (s.smaller b).norm i=0 := by + unfold wt; rw [show (s.smaller b).here=q from rfl,hx]; simp + rw [hx,hi]; simp + rw [FinJump, Complex.real_smul] + have hi : p.avg g=(s.norm:ℂ)*gain s u := by + rw [avg_split_complex] + rw [show p.L=p.child false from rfl,show p.R=p.child true from rfl,H,H] + unfold gain; simp only [curl,Bool.false_eq_true,↓reduceIte]; push_cast + ring + change (p.Gi (pos x):ℂ)*p.avg g=_*((p.Gi (pos x):ℂ)*_) + rw [hi]; ring + +/-- Payment from the already open plateau, without its children plateaus. -/ +def ordinary (u x : Grid M) (t : Play j M s) := + (s.norm:ℂ)*subtotal (proceeds x u) ((project t).stream x) + +def summons (u x : Grid M) {s : Scene M} (t : Play j M s) : ℂ := match t with +| .rest _ => 0 +| .ticket _ t => ordinary u x t + summons u x t +| .freeze _ t => summons u x t +| .attempt _ _ l r => summons u x l+summons u x r +| .jump s l r => if (step s).bit x then summons u x r else summons u x l + +lemma ordinary_jump (s : Scene M) (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) (u x : Grid M) : + let b := (step s).bit x + ordinary u x (.jump s l r)=(s.norm:ℂ)*proceeds x u ⟨s,s,b⟩+ + if b=true then ordinary u x r else ordinary u x l := by + intro b + unfold ordinary b + simp only [project,stream,subtotal_cons] + cases (step s).bit x <;> + simp only [smaller,Bool.false_eq_true,↓reduceIte,mul_add] + +/-- common bit selection is correct for the shifted Haar step -/ +lemma stage_shift (s : Scene M) (t : Play j M s) (u x : Grid M) + (h : t.Follows J f) (hp : x∈ s.here.block M) + (hh : finGood (cutoff j) J f x) : tailE s J x u=ordinary u x t+summons u x t := by + induction t with + | rest s => + rw [tail_fail s u x h.1 hh hp h.2] + unfold ordinary; simp [project,stream,subtotal,summons] + | ticket s t ih => + have HH := ih h.2.2.2.2 hp + change tailE s.reset _ _ _ = _ + rw [HH] + simp [ordinary,project,stream,subtotal,summons] + | freeze s t ih => + have H := ih h.2.2.2 hp + change tailE s.out _ _ _ = _ + rw [H] + simp [ordinary,project,stream,subtotal,summons,out] + | attempt s v l r il ir => + rw [tail_attempt v x u J, il h.2.2.2.2.1 hp, ir h.2.2.2.2.2 hp] + rw [show ordinary u x (.attempt s v l r) = ordinary u x l by + unfold ordinary; rw [project] + cases project l <;> rfl] + rw [summons] + rw [show ordinary u x r=0 by unfold ordinary; rw [show (s.win v).norm=0 from rfl]; simp]; ring + | jump s l r il ir => + obtain ⟨ha,hb,hc,hd,he⟩ := h + let b := (step s).bit x + have hM : s.here.lev < M := (Play.first hd).within.2 + have hs := share_small hb + have Hm := FinJump_align s ha.within.1 hM hs.1 u x + have HE := stream_child (less_self _) hp + rw [tail_move x u ha.within.1 hb.1.1 hp b,Hm] + change x ∈ (s.smaller b).here.block M at HE + unfold b at * + rcases HA : (step s).bit x + · rw [ordinary_jump s l r u x, summons,HA,ite_eq_right (by simp)] + rw [HA] at HE + rw [il hd HE]; simp only [Bool.false_eq_true,↓reduceIte,add_assoc] + · rw [ordinary_jump s l r u x, summons,HA,ite_eq_left rfl] + rw [HA] at HE + rw [ir he HE]; simp only [↓reduceIte,add_assoc] + +end CastShow + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Chill.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Chill.lean index 4e8b2f7a2be23f282aaf7a8e17f7829999bab04e..0740bf98d25e3af19fd16c9fbc26c94ac09a0b8f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Chill.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Chill.lean @@ -1,179 +1,179 @@ -import OAI.Analysis.FourierLLogL.Assembly.Gross -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace CastShow -open Troupe - -def drought (m : ℝ) := max (-log m) 0 -lemma drought_pos (m : ℝ) : 0 ≤ drought m := le_max_right _ _ -def chargeStatic (j : ℕ) (m : ℝ) := - 10^150*(Thermal.H j +1+drought m)*(height j+2+drought m) -lemma charge_pos (j : ℕ) (m : ℝ) : 0 ≤ chargeStatic j m := by - unfold chargeStatic - have := H_pos j - have := d_pos j - have := drought_pos m - positivity - -def coldLump (v : Journal j M) := if v.froze then chargeStatic j v.done.mass else 0 -def coldFee {s : Scene M} (t : Road j M s) := - expHike t (fun l _ => coldLump l) - -lemma chill_descendants : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, - ∀ s : Scene M, ∀ t : Play j M s, Play.Follows J f t → - s.live=false → s.norm=0 → 0 < s.mass → - netGross t ≤ chargeStatic j s.mass := by - filter_upwards [cutoff_log] with j h M J f s t hF he hx hm - let x := s.mass - let A := height j+2+drought x - let v := Thermal.H j; let U := 20000*v - have HV : 1 ≤ v := H_ge j - have hA : 0 ≤ A := by unfold A; have := d_pos j; have := drought_pos x; positivity - have HE (q : Scene M) (hh : Safe q j J f) (h : 0 < q.mass) : - log q.mass ≤ U := - le_trans (log_le_log h hh.budget) (by assumption) - let K : ℝ := 10^140*A - have hK : 0 ≤ K := by unfold K; positivity - have HT (s : Scene M) (hh : x ≤ s.mass) (ht : s.live=false) : - gateTax s.reset j ≤ K := by - unfold gateTax amount; simp only [reset,ht, Bool.false_eq_true, ↓reduceIte] - have hi : max (-log s.mass) 0 ≤ drought x := max_le_max_right 0 (neg_le_neg (log_le_log hm hh)) - unfold K A; linarith - - have HI {q : Scene M} (t : Play j M q) (h : Play.Follows J f t) (he : q.live=false) - (hE : x ≤ q.norm) (hL : log q.norm ≤ U) : - netGross t ≤ 2*K*(U-log q.norm) := by - induction t with - | rest q => - rw [netGross,trace,flat_finish]; nlinarith - | ticket q z ih => - obtain ⟨ha,hb,hc,hd,hf⟩ := h - have he₁ := hb.2.2 - have he₂ : 0 < q.norm := hm.trans_le hE - have hv : x ≤ q.mass := by unfold enter at hd; linarith - have HE' := ih hf he hv (HE q ha he₁) - unfold netGross - rw [trace,flat_push] - change gateTax _ j+netGross z ≤ _ - have he₃ : log q.norm+1/2 ≤ log q.mass := by - have HH := log_le_log (show 0 < 2*q.norm by positivity) (le_of_lt (show 2*q.norm < q.mass from hd)) - rw [log_mul (by norm_num) he₂.ne'] at HH - linarith [Real.log_two_gt_d9] - change netGross z ≤ 2*K*(U-log q.mass) at HE' - linarith [mul_le_mul_of_nonneg_left he₃ hK,HT q hv he] - | jump q l r il ir => - have HG := traced _ true h - rw [netGross,trace,flat_fork _ _ HG] - obtain ⟨ha,hb,hc,hd,hf⟩ := h - rw [← Ex_const (Turn.fork (Turn.move (j:=j) (s:=q))).probs (2*K*(U-log q.norm))] - apply Ex_mono; intro b - cases b - · change normStamp q j+netGross l ≤ _ - rw [normStamp,ite_eq_right (by rw [he]; simp),zero_add] - exact il hd he hE hL - change normStamp q j+netGross r ≤ _ - rw [normStamp,ite_eq_right (by rw [he]; simp),zero_add] - exact ir hf he hE hL - | attempt q u l r => - have HH := h.2.2.1 - rw [he] at HH; cases HH - | freeze q w ih => - have HH := h.2.2.1.1 - rw [he] at HH; cases HH - cases t with - | rest s => - rw [netGross,trace,flat_finish]; exact charge_pos _ _ - | freeze s w => - have HH := hF.2.2.1.1 - rw [he] at HH; cases HH - | attempt s u l r => - have HH := hF.2.2.1 - rw [he] at HH; cases HH - | jump s l r => - have HH := share_small hF.2.1 - rw [hx] at HH - linarith [HH.1] - | ticket s w => - rw [netGross,trace,flat_push] - have HC := HI w hF.2.2.2.2 he (le_refl _) (HE s hF.1 hm) - change netGross w ≤ 2*K*(U-log x) at HC - change gateTax s.reset j+netGross w ≤ 10^150*(v+1+drought x)*A - have hz := drought_pos x - have hx' : -log x ≤ drought x := le_max_left _ _ - apply le_trans (add_le_add (HT s le_rfl he) HC) - unfold K U; nlinarith - -/-- Every coast including virtual ends of the true live processing bills only once for static -descendants. -/ -lemma frostCompare : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s : Scene M, - ∀ t : Play j M s, Play.Follows J f t → s.live=true → - - netGross t ≤ (trace false t).flatFee billOf + coldFee (trace false t) := by - filter_upwards [chill_descendants] with j h M J f s t hh he - induction t with - | rest s => - unfold coldFee netGross; simp only [trace,flat_finish,zero_add] - simp [expHike,hike,Ex_const,coldLump] - | freeze s t ih => - have HX := hh.2.2.2 - have H := h M J f s.out t HX rfl rfl hh.2.1.2.2 - unfold netGross coldFee - rw [trace,ite_eq_left rfl, flat_push] - simp only [trace,Bool.false_eq_true,ite_false,flat_finish,zero_add] - simp only [expHike,hike,coldLump, ↓reduceIte,Ex_const] - change 0+netGross t ≤ _ - rw [zero_add]; exact H - | ticket s t ih => - have H := ih hh.2.2.2.2 he - unfold netGross coldFee - rw [trace,flat_push] - simp only [trace,flat_push] - rw [Ex_hike_push] - apply le_trans (add_le_add_right H _) - rw [← add_assoc]; exact le_rfl - | jump s l r il ir => - have HF := traced _ true hh - have HG := traced _ false hh - unfold netGross coldFee - rw [trace,flat_fork _ _ HF] - simp only [trace,flat_fork _ _ HG] - rw [Ex_hike_fork HG, ← Ex_add] - apply Ex_mono; intro b - cases b - · apply le_trans (add_le_add_right (il hh.2.2.2.1 he) _) - rw [←add_assoc]; exact le_rfl - · apply le_trans (add_le_add_right (ir hh.2.2.2.2 he) _) - rw [←add_assoc]; exact le_rfl - | attempt s u l r il ir => - have HF := traced _ true hh - have HG := traced _ false hh - unfold netGross coldFee; rw [trace,flat_fork _ _ HF] - simp only [trace,flat_fork _ _ HG] - rw [Ex_hike_fork HG, ← Ex_add] - apply Ex_mono; intro b - cases b - · apply le_trans (add_le_add_right (il hh.2.2.2.2.1 he) _) - rw [← add_assoc]; exact le_rfl - · apply le_trans (add_le_add_right (ir hh.2.2.2.2.2 he) _) - rw [← add_assoc]; exact le_rfl - -end CastShow - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Gross +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace CastShow +open Troupe + +def drought (m : ℝ) := max (-log m) 0 +lemma drought_pos (m : ℝ) : 0 ≤ drought m := le_max_right _ _ +def chargeStatic (j : ℕ) (m : ℝ) := + 10^150*(Thermal.H j +1+drought m)*(height j+2+drought m) +lemma charge_pos (j : ℕ) (m : ℝ) : 0 ≤ chargeStatic j m := by + unfold chargeStatic + have := H_pos j + have := d_pos j + have := drought_pos m + positivity + +def coldLump (v : Journal j M) := if v.froze then chargeStatic j v.done.mass else 0 +def coldFee {s : Scene M} (t : Road j M s) := + expHike t (fun l _ => coldLump l) + +lemma chill_descendants : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, + ∀ s : Scene M, ∀ t : Play j M s, Play.Follows J f t → + s.live=false → s.norm=0 → 0 < s.mass → + netGross t ≤ chargeStatic j s.mass := by + filter_upwards [cutoff_log] with j h M J f s t hF he hx hm + let x := s.mass + let A := height j+2+drought x + let v := Thermal.H j; let U := 20000*v + have HV : 1 ≤ v := H_ge j + have hA : 0 ≤ A := by unfold A; have := d_pos j; have := drought_pos x; positivity + have HE (q : Scene M) (hh : Safe q j J f) (h : 0 < q.mass) : + log q.mass ≤ U := + le_trans (log_le_log h hh.budget) (by assumption) + let K : ℝ := 10^140*A + have hK : 0 ≤ K := by unfold K; positivity + have HT (s : Scene M) (hh : x ≤ s.mass) (ht : s.live=false) : + gateTax s.reset j ≤ K := by + unfold gateTax amount; simp only [reset,ht, Bool.false_eq_true, ↓reduceIte] + have hi : max (-log s.mass) 0 ≤ drought x := max_le_max_right 0 (neg_le_neg (log_le_log hm hh)) + unfold K A; linarith + + have HI {q : Scene M} (t : Play j M q) (h : Play.Follows J f t) (he : q.live=false) + (hE : x ≤ q.norm) (hL : log q.norm ≤ U) : + netGross t ≤ 2*K*(U-log q.norm) := by + induction t with + | rest q => + rw [netGross,trace,flat_finish]; nlinarith + | ticket q z ih => + obtain ⟨ha,hb,hc,hd,hf⟩ := h + have he₁ := hb.2.2 + have he₂ : 0 < q.norm := hm.trans_le hE + have hv : x ≤ q.mass := by unfold enter at hd; linarith + have HE' := ih hf he hv (HE q ha he₁) + unfold netGross + rw [trace,flat_push] + change gateTax _ j+netGross z ≤ _ + have he₃ : log q.norm+1/2 ≤ log q.mass := by + have HH := log_le_log (show 0 < 2*q.norm by positivity) (le_of_lt (show 2*q.norm < q.mass from hd)) + rw [log_mul (by norm_num) he₂.ne'] at HH + linarith [Real.log_two_gt_d9] + change netGross z ≤ 2*K*(U-log q.mass) at HE' + linarith [mul_le_mul_of_nonneg_left he₃ hK,HT q hv he] + | jump q l r il ir => + have HG := traced _ true h + rw [netGross,trace,flat_fork _ _ HG] + obtain ⟨ha,hb,hc,hd,hf⟩ := h + rw [← Ex_const (Turn.fork (Turn.move (j:=j) (s:=q))).probs (2*K*(U-log q.norm))] + apply Ex_mono; intro b + cases b + · change normStamp q j+netGross l ≤ _ + rw [normStamp,ite_eq_right (by rw [he]; simp),zero_add] + exact il hd he hE hL + change normStamp q j+netGross r ≤ _ + rw [normStamp,ite_eq_right (by rw [he]; simp),zero_add] + exact ir hf he hE hL + | attempt q u l r => + have HH := h.2.2.1 + rw [he] at HH; cases HH + | freeze q w ih => + have HH := h.2.2.1.1 + rw [he] at HH; cases HH + cases t with + | rest s => + rw [netGross,trace,flat_finish]; exact charge_pos _ _ + | freeze s w => + have HH := hF.2.2.1.1 + rw [he] at HH; cases HH + | attempt s u l r => + have HH := hF.2.2.1 + rw [he] at HH; cases HH + | jump s l r => + have HH := share_small hF.2.1 + rw [hx] at HH + linarith [HH.1] + | ticket s w => + rw [netGross,trace,flat_push] + have HC := HI w hF.2.2.2.2 he (le_refl _) (HE s hF.1 hm) + change netGross w ≤ 2*K*(U-log x) at HC + change gateTax s.reset j+netGross w ≤ 10^150*(v+1+drought x)*A + have hz := drought_pos x + have hx' : -log x ≤ drought x := le_max_left _ _ + apply le_trans (add_le_add (HT s le_rfl he) HC) + unfold K U; nlinarith + +/-- Every coast including virtual ends of the true live processing bills only once for static +descendants. -/ +lemma frostCompare : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s : Scene M, + ∀ t : Play j M s, Play.Follows J f t → s.live=true → + + netGross t ≤ (trace false t).flatFee billOf + coldFee (trace false t) := by + filter_upwards [chill_descendants] with j h M J f s t hh he + induction t with + | rest s => + unfold coldFee netGross; simp only [trace,flat_finish,zero_add] + simp [expHike,hike,Ex_const,coldLump] + | freeze s t ih => + have HX := hh.2.2.2 + have H := h M J f s.out t HX rfl rfl hh.2.1.2.2 + unfold netGross coldFee + rw [trace,ite_eq_left rfl, flat_push] + simp only [trace,Bool.false_eq_true,ite_false,flat_finish,zero_add] + simp only [expHike,hike,coldLump, ↓reduceIte,Ex_const] + change 0+netGross t ≤ _ + rw [zero_add]; exact H + | ticket s t ih => + have H := ih hh.2.2.2.2 he + unfold netGross coldFee + rw [trace,flat_push] + simp only [trace,flat_push] + rw [Ex_hike_push] + apply le_trans (add_le_add_right H _) + rw [← add_assoc]; exact le_rfl + | jump s l r il ir => + have HF := traced _ true hh + have HG := traced _ false hh + unfold netGross coldFee + rw [trace,flat_fork _ _ HF] + simp only [trace,flat_fork _ _ HG] + rw [Ex_hike_fork HG, ← Ex_add] + apply Ex_mono; intro b + cases b + · apply le_trans (add_le_add_right (il hh.2.2.2.1 he) _) + rw [←add_assoc]; exact le_rfl + · apply le_trans (add_le_add_right (ir hh.2.2.2.2 he) _) + rw [←add_assoc]; exact le_rfl + | attempt s u l r il ir => + have HF := traced _ true hh + have HG := traced _ false hh + unfold netGross coldFee; rw [trace,flat_fork _ _ HF] + simp only [trace,flat_fork _ _ HG] + rw [Ex_hike_fork HG, ← Ex_add] + apply Ex_mono; intro b + cases b + · apply le_trans (add_le_add_right (il hh.2.2.2.2.1 he) _) + rw [← add_assoc]; exact le_rfl + · apply le_trans (add_le_add_right (ir hh.2.2.2.2.2 he) _) + rw [← add_assoc]; exact le_rfl + +end CastShow + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Duration.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Duration.lean index 7ee523f86d49d15c18025a84cbb03a50d473eb6c..4b5c4af9850348e8ca44599b4e0aca55cf57d599 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Duration.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Duration.lean @@ -1,241 +1,241 @@ -import OAI.Analysis.FourierLLogL.Assembly.Slab -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot Run Streak Beam - -def tagM (i : ℕ) : Grade := ⟨true,i⟩ -def tagL (i : ℕ) : Grade := ⟨false,i⟩ -def depthRate (i : ℕ) := √(side i) -def mainRatio (i : ℕ) (j : ℕ) (L : Path M) := - let g := tagM i - (1+fullKills L+ (rose g 0).flagged L) / depthRate i + - depthRate i*timex g j L -def mainSticker (i : ℕ) (j : ℕ) (L : Path M) := - mainRatio i j L+(billWait (tagM (i-1)) j L:ℝ) -def scalarSticker (i : ℕ) (j : ℕ) (L : Path M) := - let g := tagL i - depthRate i*(1+fullKills L+timex g j L) - -lemma rate_easy (i) : 0 < depthRate i ∧ depthRate i ≤ 1 ∧ (depthRate i)^2=side i := - thick_rate (M:=0) (rose (tagM i) 0) -lemma mainRatio_one (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} - (h : Trip x s L) : 1 ≤ mainRatio i j L := by - let g := tagM i; let p : Streak M := rose g 0 - have hv := trip_K h; have ht := flagged_pos p h - have hi := (timex_scores g j L).1 - let a := depthRate i; have hx : 0 < a ∧ a ≤ 1 := ⟨(rate_easy _).1,(rate_easy i).2.1⟩ - change 1 ≤ (1+fullKills L+p.flagged L)/a+a*_ - exact le_trans ((one_le_div hx.1).mpr (by linarith)) - (le_add_of_nonneg_right (mul_nonneg hx.1.le hi)) -lemma mainSticker_pos (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} - (h : Trip x s L) : 0 ≤ mainSticker i j L := - add_nonneg (le_trans (by norm_num) (mainRatio_one i j h)) (by positivity) -lemma scalarSticker_pos (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} - (h : Trip x s L) : 0 ≤ scalarSticker i j L := - mul_nonneg (rate_easy i).1.le - (add_nonneg (add_nonneg (by norm_num) (trip_K h)) (timex_scores (tagL i) j L).1) - -/-- Bounding a suffix without bigger main activations. -/ -lemma ride : - ∀ᶠ j : ℕ in atTop, - ∀ M J i : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ l r m : Path M, - ∀ (b : Beat M) (x u : Grid M), - let g := tagM i - let D := b::r - let L := l++(D++m) - let p := heroPlot g L - Cast j J f s L → Trip x s L → entitled g s j → - ringing u p j l b → - (0 < i → stall u (tagM (i-1)) j L (l++D)) → - ‖subtotal (proceeds x u) D‖ ≤ 10^20*(fullFax D+mainSticker i j L) := by - filter_upwards [live_rec,channel_inner,near_size] with j h₁ h₂ h₃ - M J i s f l r m b x u g D L p hh he hg hp hf - let g' := tagM (i-1) - let n := billWait g' j L - obtain ⟨z,hz⟩ := bound_stage (u:=u) L hh g' - obtain ⟨v,hA,l₀,l₁,hL,hT⟩ := h₁ M J s f u g l (r++m) b hh hg hp - let t := timex g j L - let S := rose g v - have HT : 2*log (sumbling S none (l₁++(b::(r++m)))) ≤ t := by - apply le_trans _ hT - unfold S g Troupe.rose Troupe.tagM recital - simp - exact le_rfl - have hI : i ≤ mainIx j := (hg rfl).1 - have H : ∃ k, delta (dye b.post) k ≤ sepRad j ∧ rad (u-v-k) ≤ 2*spotLimit b.post j i true := by - change aligned p b.post j u v at hA - unfold aligned at hA - rw [ite_eq_left (show p.main=true from rfl)] at hA; exact hA - obtain ⟨k,hK,hD⟩ := H - let a := depthRate i - obtain ⟨_,q,hq⟩ := trip_cut (show Trip x s (l++(D++m)) from he) - obtain ⟨hX,_,_⟩ := trip_cut hq - have hY : b.post.here=q.here := hX.2.1.lower.here.trans (congrArg Scene.here hX.1) - have hy : dye b.post=dye q := (trip_attend hX b List.mem_cons_self).2.2.2.2 - have HV (c : Beat M) (hc : c∈D) (hA : ¬betweenS z n c) : - innerV S c.post := by - have hr := trip_attend hX c hc - refine h₂ i hI M b.post c.post (by rw [hY]; exact hr.2.2.2.1) - (hr.2.2.2.2.trans hy.symm) hr.1.climb u v k hK hD ?_ - intro hi hx - have HX := hf hi - exact hA (hz (l++D) m (by unfold L; rw [List.append_assoc]) HX c (by simp [hc]) hx) - let P := proceeds x u - have HA : 1 ≤ mainRatio i j L := mainRatio_one i j he - have hc (v' w q' : Path M) (hiD : D=v'++(w++q')) (hiA : ∀ b∈w, ¬betweenS z n b) : - ‖subtotal P w‖ ≤ 10^16*(fullFax w+mainRatio i j L) := by - have hp₁ : Trip x q (v'++(w++q')) := hiD ▸ hX - have hp₂ : L=l₀++(((l₁++v')++(w++(q'++m)))) := by - unfold L; rw [hL,hiD]; simp only [List.append_assoc] - have hp₃ : l₁++(b::(r++m))=(l₁++v')++(w++(q'++m)) := by - change l₁++(D++m)=_ - rw [hiD]; simp only [List.append_assoc] - let K := (l₁++v')++(w++(q'++m)) - obtain ⟨hU,c₀,hc₀⟩ := trip_cut (show Trip x s (l₀++K) from hp₂ ▸ he) - have hi₁ := possible S (l₁++v') w (q'++m) hc₀ - (fun b hb => HV b (by rw [hiD]; simp [hb]) (hiA b hb)) t (hp₃ ▸ HT) - have hi₂ : yieldW (subtotal (duties x v) w) ≤ 10^15*(fullFax w + mainRatio i j L) := by - apply hi₁.trans; unfold mainRatio - change 10^15*(fullFax w+(1+fullKills K+flagged S K)/a+a*t) ≤ - 10^15*(fullFax w+((1+fullKills L+flagged S L)/a+a*t)) - have ht₁ : fullKills K ≤ fullKills L := by - rw [hp₂,show fullKills (l₀++K)=fullKills l₀+fullKills K from subtotal_app killed _ _] - exact le_add_of_nonneg_left (trip_K hU) - have ht₂ : flagged S K ≤ flagged S L := by - rw [hp₂,show flagged S (l₀++K)=flagged S l₀+flagged S K from subtotal_app _ _ _] - exact le_add_of_nonneg_left (flagged_pos S hU) - have ha : 0 < a := (rate_easy i).1 - have HE : (1+fullKills K+flagged S K)/a ≤ (1+fullKills L+flagged S L)/a := - div_le_div_of_nonneg_right (by linarith) ha.le - linarith - have hi₃ := h₃ i hI - have hga : 64*q.here.len*rad (u-v-k) ≤ 4 := by - have HE : b.post.here.len*rad (u-v-k) ≤ 2*sig j := - halo_limit b.post j i true _ (below_refl _) hD - rw [←hY]; nlinarith [hi₃.2, side_le i, side_pos i] - have HB := below_decode u v k x (dye b.post) v' w q' hp₁ hy (hK.trans (hi₃.1.trans (side_le i))) - apply HB.trans - obtain ⟨_,C,Hc⟩ := trip_cut hp₁ - linarith [trip_fax (trip_cut Hc).1] - obtain ⟨w,v,q,hD',hw,hp,hq⟩ := slab_on _ _ (matrix_trip D hX) z n - let K := mainRatio i j L - have Heq : mainSticker i j L=K+(n:ℝ) := rfl - have HW : D=w++(v++q) := by rw [hD',List.append_assoc] - have HA₁ := hc [] w (v++q) (by simpa using HW) hw - have HA₂ := hc (w++v) q [] (by simpa using hD') hp - obtain ⟨hw',q₀,Hh⟩ := trip_cut (show Trip x _ (w++(v++q)) from HW ▸ hX) - obtain ⟨hv',q₁,hq₁⟩ := trip_cut Hh - rw [Heq,hD',subtotal_app,subtotal_app] - change ‖subtotal P w + subtotal P v + subtotal P q‖ ≤ _ - apply (norm_add_le _ _).trans - have he' : (v.length:ℝ) ≤ n := by exact_mod_cast hq - have HE := straight_fee u v hv' - change ‖subtotal P v‖ ≤ _ at HE - change 1 ≤ K at HA - change _ ≤ 10^16* (fullFax w+K) at HA₁ - change _ ≤ 10^16*(fullFax q+K) at HA₂ - have HE : fullFax (w++v++q)=fullFax w+fullFax v+fullFax q := by - rw [fullFax,subtotal_app,subtotal_app]; rfl - rw [HE] - linarith [norm_add_le (subtotal P w) (subtotal P v),trip_fax hw',trip_fax hv',trip_fax hq₁] - -/-- Scalar suffix exact part with mask specified by chosen representative. -/ -def guardedV (i : ℕ) (v : Grid M) (b : Beat M) := guarded v (side i) b.post - -def ampU (s : Scene M) (u : Grid M) := - max ‖scalar (s.smaller false) u‖ ‖scalar (s.smaller true) u‖ - -lemma mask_estimate : - ∀ᶠ j : ℕ in atTop, ∀ M J i : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ l r m : Path M, ∀ b : Beat M, ∀ x u : Grid M, - let g := tagL i - let D := b::r - let L := l++(D++m) - let p := heroPlot g L - Cast j J f s L → Trip x s L → ringing u p j l b → - ∃ v : Grid M, let μ := guardedV i v - ‖subtotal (aired μ x u) D‖ ≤ 20000*scalarSticker i j L ∧ - ∀ z∈D, ¬μ z → 4*side i < ampU z.post u := by - filter_upwards [live_rec] with j hj M J i s f l r m b x u g D L p h ht hp - let y := side i - have Hg : entitled g s j := fun H => False.elim (Bool.noConfusion H) - obtain ⟨v,hA,l₀,l₁,hL,hT⟩ := hj M J s f u g l (r++m) b h Hg hp - have HK : rad (u-v) ≤ 2*spotLimit b.post j i false := by - change aligned p b.post j u v at hA - unfold aligned at hA; rw [ite_eq_right (show p.main ≠ true from Bool.false_ne_true)] at hA; exact hA - obtain ⟨_,q,hq⟩ := trip_cut (show Trip x s (l++(D++m)) from ht) - have HX := (trip_cut hq).1 - have HE : b.post.here=q.here := HX.2.1.lower.here.trans (congrArg Scene.here HX.1) - have hb : b.post.here.len*rad (u-v) ≤ 2*(y^4/65536) := halo_limit _ j i false _ (below_refl _) HK - let a := depthRate i - have ha : 0 < a ∧ a ≤ 1 ∧ a^2=y := rate_easy i - have hy : 0 < y := side_pos i - have hf : y^4 ≤ y := by simpa using pow_le_pow_of_le_one hy.le (side_le i) (by omega : 1≤4) - refine ⟨v,?_⟩ - intro μ - let t := timex g j L - have hp₂ : L=l₀++(l₁++(D++m)) := by unfold L; rw [hL,List.append_assoc] - constructor - · have ht₀ : 2*log (coldScore v y (l₁++D++m)) ≤ t := by - apply le_trans _ hT - simp only [g,tagL,recital,Bool.false_eq_true,↓reduceIte,List.append_assoc] - exact le_rfl - obtain ⟨hU,c₀,hc₀⟩ := trip_cut (show Trip x s (l₀++(l₁++(D++m))) from hp₂ ▸ ht) - have He : Trip x c₀ (l₁++D++m) := by rwa [List.append_assoc] - have HR : aired μ x v = frosty x v y := by - funext b; unfold aired frosty prize - split_ifs with h1 h2 h2 - · rfl - · exact (h2 h1).elim - · exact (h1 h2).elim - simp - have Hx := rail_fee u v μ [] D [] (s:=q) (by simpa using HX) - have HV := frosty_portion v hy (side_le i) l₁ D m He t ht₀ - have H := norm_add_le (subtotal (aired μ x u) D-chi (u-v) x*subtotal (aired μ x v) D) - (chi (u-v) x*subtotal (aired μ x v) D) - rw [sub_add_cancel,norm_mul,norm_chi,one_mul,HR] at H - have hE' : fullKills (l₁++D++m) ≤ fullKills L := by - rw [hp₂,List.append_assoc,show fullKills (l₀++(l₁++(D++m)))=fullKills l₀+fullKills (l₁++(D++m)) from subtotal_app killed _ _] - exact le_add_of_nonneg_left (trip_K hU) - have hV : 0 ≤ fullKills L := trip_K ht - have hY : 0 ≤ t := (timex_scores g j _).1 - apply H.trans - rw [HR,← HE] at Hx - change _ ≤ 12000*a*_ at HV - change _ ≤ 20000*(a*(1+fullKills L+t)) - nlinarith - · intro z hz hμ - obtain ⟨hz₁,hz₂,hz₃,hz₄,hz₅⟩ := trip_attend HX z hz - rw [←HE] at hz₄ - change ¬∀ k, ‖scalar (z.post.smaller k) v‖ ≤ 10*y at hμ - push Not at hμ - obtain ⟨c,hc⟩ := hμ - let d := z.post.smaller c - have H1 := hz₁.climb.child c - have H2 := scalar_compare d u v H1.ready H1.pos.le 4 H1.lift - have H3 : d.here.len ≤ b.post.here.len := - node_compress (below_trans (child_below _ _) hz₄) - have HH' : ‖scalar d u‖ ≤ ampU z.post u := by unfold d ampU; cases c; exact le_max_left _ _; exact le_max_right _ _ - change _ < ‖scalar d v‖ at hc - change 4*y < _ - nlinarith [(abs_le.mp H2).1,rad_nonneg (u-v)] - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Slab +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot Run Streak Beam + +def tagM (i : ℕ) : Grade := ⟨true,i⟩ +def tagL (i : ℕ) : Grade := ⟨false,i⟩ +def depthRate (i : ℕ) := √(side i) +def mainRatio (i : ℕ) (j : ℕ) (L : Path M) := + let g := tagM i + (1+fullKills L+ (rose g 0).flagged L) / depthRate i + + depthRate i*timex g j L +def mainSticker (i : ℕ) (j : ℕ) (L : Path M) := + mainRatio i j L+(billWait (tagM (i-1)) j L:ℝ) +def scalarSticker (i : ℕ) (j : ℕ) (L : Path M) := + let g := tagL i + depthRate i*(1+fullKills L+timex g j L) + +lemma rate_easy (i) : 0 < depthRate i ∧ depthRate i ≤ 1 ∧ (depthRate i)^2=side i := + thick_rate (M:=0) (rose (tagM i) 0) +lemma mainRatio_one (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} + (h : Trip x s L) : 1 ≤ mainRatio i j L := by + let g := tagM i; let p : Streak M := rose g 0 + have hv := trip_K h; have ht := flagged_pos p h + have hi := (timex_scores g j L).1 + let a := depthRate i; have hx : 0 < a ∧ a ≤ 1 := ⟨(rate_easy _).1,(rate_easy i).2.1⟩ + change 1 ≤ (1+fullKills L+p.flagged L)/a+a*_ + exact le_trans ((one_le_div hx.1).mpr (by linarith)) + (le_add_of_nonneg_right (mul_nonneg hx.1.le hi)) +lemma mainSticker_pos (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} + (h : Trip x s L) : 0 ≤ mainSticker i j L := + add_nonneg (le_trans (by norm_num) (mainRatio_one i j h)) (by positivity) +lemma scalarSticker_pos (i j : ℕ) {L : Path M} {x : Grid M} {s : Scene M} + (h : Trip x s L) : 0 ≤ scalarSticker i j L := + mul_nonneg (rate_easy i).1.le + (add_nonneg (add_nonneg (by norm_num) (trip_K h)) (timex_scores (tagL i) j L).1) + +/-- Bounding a suffix without bigger main activations. -/ +lemma ride : + ∀ᶠ j : ℕ in atTop, + ∀ M J i : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ l r m : Path M, + ∀ (b : Beat M) (x u : Grid M), + let g := tagM i + let D := b::r + let L := l++(D++m) + let p := heroPlot g L + Cast j J f s L → Trip x s L → entitled g s j → + ringing u p j l b → + (0 < i → stall u (tagM (i-1)) j L (l++D)) → + ‖subtotal (proceeds x u) D‖ ≤ 10^20*(fullFax D+mainSticker i j L) := by + filter_upwards [live_rec,channel_inner,near_size] with j h₁ h₂ h₃ + M J i s f l r m b x u g D L p hh he hg hp hf + let g' := tagM (i-1) + let n := billWait g' j L + obtain ⟨z,hz⟩ := bound_stage (u:=u) L hh g' + obtain ⟨v,hA,l₀,l₁,hL,hT⟩ := h₁ M J s f u g l (r++m) b hh hg hp + let t := timex g j L + let S := rose g v + have HT : 2*log (sumbling S none (l₁++(b::(r++m)))) ≤ t := by + apply le_trans _ hT + unfold S g Troupe.rose Troupe.tagM recital + simp + exact le_rfl + have hI : i ≤ mainIx j := (hg rfl).1 + have H : ∃ k, delta (dye b.post) k ≤ sepRad j ∧ rad (u-v-k) ≤ 2*spotLimit b.post j i true := by + change aligned p b.post j u v at hA + unfold aligned at hA + rw [ite_eq_left (show p.main=true from rfl)] at hA; exact hA + obtain ⟨k,hK,hD⟩ := H + let a := depthRate i + obtain ⟨_,q,hq⟩ := trip_cut (show Trip x s (l++(D++m)) from he) + obtain ⟨hX,_,_⟩ := trip_cut hq + have hY : b.post.here=q.here := hX.2.1.lower.here.trans (congrArg Scene.here hX.1) + have hy : dye b.post=dye q := (trip_attend hX b List.mem_cons_self).2.2.2.2 + have HV (c : Beat M) (hc : c∈D) (hA : ¬betweenS z n c) : + innerV S c.post := by + have hr := trip_attend hX c hc + refine h₂ i hI M b.post c.post (by rw [hY]; exact hr.2.2.2.1) + (hr.2.2.2.2.trans hy.symm) hr.1.climb u v k hK hD ?_ + intro hi hx + have HX := hf hi + exact hA (hz (l++D) m (by unfold L; rw [List.append_assoc]) HX c (by simp [hc]) hx) + let P := proceeds x u + have HA : 1 ≤ mainRatio i j L := mainRatio_one i j he + have hc (v' w q' : Path M) (hiD : D=v'++(w++q')) (hiA : ∀ b∈w, ¬betweenS z n b) : + ‖subtotal P w‖ ≤ 10^16*(fullFax w+mainRatio i j L) := by + have hp₁ : Trip x q (v'++(w++q')) := hiD ▸ hX + have hp₂ : L=l₀++(((l₁++v')++(w++(q'++m)))) := by + unfold L; rw [hL,hiD]; simp only [List.append_assoc] + have hp₃ : l₁++(b::(r++m))=(l₁++v')++(w++(q'++m)) := by + change l₁++(D++m)=_ + rw [hiD]; simp only [List.append_assoc] + let K := (l₁++v')++(w++(q'++m)) + obtain ⟨hU,c₀,hc₀⟩ := trip_cut (show Trip x s (l₀++K) from hp₂ ▸ he) + have hi₁ := possible S (l₁++v') w (q'++m) hc₀ + (fun b hb => HV b (by rw [hiD]; simp [hb]) (hiA b hb)) t (hp₃ ▸ HT) + have hi₂ : yieldW (subtotal (duties x v) w) ≤ 10^15*(fullFax w + mainRatio i j L) := by + apply hi₁.trans; unfold mainRatio + change 10^15*(fullFax w+(1+fullKills K+flagged S K)/a+a*t) ≤ + 10^15*(fullFax w+((1+fullKills L+flagged S L)/a+a*t)) + have ht₁ : fullKills K ≤ fullKills L := by + rw [hp₂,show fullKills (l₀++K)=fullKills l₀+fullKills K from subtotal_app killed _ _] + exact le_add_of_nonneg_left (trip_K hU) + have ht₂ : flagged S K ≤ flagged S L := by + rw [hp₂,show flagged S (l₀++K)=flagged S l₀+flagged S K from subtotal_app _ _ _] + exact le_add_of_nonneg_left (flagged_pos S hU) + have ha : 0 < a := (rate_easy i).1 + have HE : (1+fullKills K+flagged S K)/a ≤ (1+fullKills L+flagged S L)/a := + div_le_div_of_nonneg_right (by linarith) ha.le + linarith + have hi₃ := h₃ i hI + have hga : 64*q.here.len*rad (u-v-k) ≤ 4 := by + have HE : b.post.here.len*rad (u-v-k) ≤ 2*sig j := + halo_limit b.post j i true _ (below_refl _) hD + rw [←hY]; nlinarith [hi₃.2, side_le i, side_pos i] + have HB := below_decode u v k x (dye b.post) v' w q' hp₁ hy (hK.trans (hi₃.1.trans (side_le i))) + apply HB.trans + obtain ⟨_,C,Hc⟩ := trip_cut hp₁ + linarith [trip_fax (trip_cut Hc).1] + obtain ⟨w,v,q,hD',hw,hp,hq⟩ := slab_on _ _ (matrix_trip D hX) z n + let K := mainRatio i j L + have Heq : mainSticker i j L=K+(n:ℝ) := rfl + have HW : D=w++(v++q) := by rw [hD',List.append_assoc] + have HA₁ := hc [] w (v++q) (by simpa using HW) hw + have HA₂ := hc (w++v) q [] (by simpa using hD') hp + obtain ⟨hw',q₀,Hh⟩ := trip_cut (show Trip x _ (w++(v++q)) from HW ▸ hX) + obtain ⟨hv',q₁,hq₁⟩ := trip_cut Hh + rw [Heq,hD',subtotal_app,subtotal_app] + change ‖subtotal P w + subtotal P v + subtotal P q‖ ≤ _ + apply (norm_add_le _ _).trans + have he' : (v.length:ℝ) ≤ n := by exact_mod_cast hq + have HE := straight_fee u v hv' + change ‖subtotal P v‖ ≤ _ at HE + change 1 ≤ K at HA + change _ ≤ 10^16* (fullFax w+K) at HA₁ + change _ ≤ 10^16*(fullFax q+K) at HA₂ + have HE : fullFax (w++v++q)=fullFax w+fullFax v+fullFax q := by + rw [fullFax,subtotal_app,subtotal_app]; rfl + rw [HE] + linarith [norm_add_le (subtotal P w) (subtotal P v),trip_fax hw',trip_fax hv',trip_fax hq₁] + +/-- Scalar suffix exact part with mask specified by chosen representative. -/ +def guardedV (i : ℕ) (v : Grid M) (b : Beat M) := guarded v (side i) b.post + +def ampU (s : Scene M) (u : Grid M) := + max ‖scalar (s.smaller false) u‖ ‖scalar (s.smaller true) u‖ + +lemma mask_estimate : + ∀ᶠ j : ℕ in atTop, ∀ M J i : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ l r m : Path M, ∀ b : Beat M, ∀ x u : Grid M, + let g := tagL i + let D := b::r + let L := l++(D++m) + let p := heroPlot g L + Cast j J f s L → Trip x s L → ringing u p j l b → + ∃ v : Grid M, let μ := guardedV i v + ‖subtotal (aired μ x u) D‖ ≤ 20000*scalarSticker i j L ∧ + ∀ z∈D, ¬μ z → 4*side i < ampU z.post u := by + filter_upwards [live_rec] with j hj M J i s f l r m b x u g D L p h ht hp + let y := side i + have Hg : entitled g s j := fun H => False.elim (Bool.noConfusion H) + obtain ⟨v,hA,l₀,l₁,hL,hT⟩ := hj M J s f u g l (r++m) b h Hg hp + have HK : rad (u-v) ≤ 2*spotLimit b.post j i false := by + change aligned p b.post j u v at hA + unfold aligned at hA; rw [ite_eq_right (show p.main ≠ true from Bool.false_ne_true)] at hA; exact hA + obtain ⟨_,q,hq⟩ := trip_cut (show Trip x s (l++(D++m)) from ht) + have HX := (trip_cut hq).1 + have HE : b.post.here=q.here := HX.2.1.lower.here.trans (congrArg Scene.here HX.1) + have hb : b.post.here.len*rad (u-v) ≤ 2*(y^4/65536) := halo_limit _ j i false _ (below_refl _) HK + let a := depthRate i + have ha : 0 < a ∧ a ≤ 1 ∧ a^2=y := rate_easy i + have hy : 0 < y := side_pos i + have hf : y^4 ≤ y := by simpa using pow_le_pow_of_le_one hy.le (side_le i) (by omega : 1≤4) + refine ⟨v,?_⟩ + intro μ + let t := timex g j L + have hp₂ : L=l₀++(l₁++(D++m)) := by unfold L; rw [hL,List.append_assoc] + constructor + · have ht₀ : 2*log (coldScore v y (l₁++D++m)) ≤ t := by + apply le_trans _ hT + simp only [g,tagL,recital,Bool.false_eq_true,↓reduceIte,List.append_assoc] + exact le_rfl + obtain ⟨hU,c₀,hc₀⟩ := trip_cut (show Trip x s (l₀++(l₁++(D++m))) from hp₂ ▸ ht) + have He : Trip x c₀ (l₁++D++m) := by rwa [List.append_assoc] + have HR : aired μ x v = frosty x v y := by + funext b; unfold aired frosty prize + split_ifs with h1 h2 h2 + · rfl + · exact (h2 h1).elim + · exact (h1 h2).elim + simp + have Hx := rail_fee u v μ [] D [] (s:=q) (by simpa using HX) + have HV := frosty_portion v hy (side_le i) l₁ D m He t ht₀ + have H := norm_add_le (subtotal (aired μ x u) D-chi (u-v) x*subtotal (aired μ x v) D) + (chi (u-v) x*subtotal (aired μ x v) D) + rw [sub_add_cancel,norm_mul,norm_chi,one_mul,HR] at H + have hE' : fullKills (l₁++D++m) ≤ fullKills L := by + rw [hp₂,List.append_assoc,show fullKills (l₀++(l₁++(D++m)))=fullKills l₀+fullKills (l₁++(D++m)) from subtotal_app killed _ _] + exact le_add_of_nonneg_left (trip_K hU) + have hV : 0 ≤ fullKills L := trip_K ht + have hY : 0 ≤ t := (timex_scores g j _).1 + apply H.trans + rw [HR,← HE] at Hx + change _ ≤ 12000*a*_ at HV + change _ ≤ 20000*(a*(1+fullKills L+t)) + nlinarith + · intro z hz hμ + obtain ⟨hz₁,hz₂,hz₃,hz₄,hz₅⟩ := trip_attend HX z hz + rw [←HE] at hz₄ + change ¬∀ k, ‖scalar (z.post.smaller k) v‖ ≤ 10*y at hμ + push Not at hμ + obtain ⟨c,hc⟩ := hμ + let d := z.post.smaller c + have H1 := hz₁.climb.child c + have H2 := scalar_compare d u v H1.ready H1.pos.le 4 H1.lift + have H3 : d.here.len ≤ b.post.here.len := + node_compress (below_trans (child_below _ _) hz₄) + have HH' : ‖scalar d u‖ ≤ ampU z.post u := by unfold d ampU; cases c; exact le_max_left _ _; exact le_max_right _ _ + change _ < ‖scalar d v‖ at hc + change 4*y < _ + nlinarith [(abs_le.mp H2).1,rad_nonneg (u-v)] + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Endpoint.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Endpoint.lean index 7ae30c297c3d59d6615738af1feaae8689c31d9e..37bed589d655bc75538c57ae4adf3174cd3a3422 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Endpoint.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Endpoint.lean @@ -1,62 +1,62 @@ -import OAI.Analysis.FourierLLogL.Assembly.Weighted - -/-! -# The grid bound --/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Law Node Real Period Compress.Params Thermal -open Procedure Procedure.Scene Procedure.Paths Procedure.Paths.Troupe Procedure.Paths.CastShow -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -theorem gridProof : GridBound := by - refine ⟨10^222,by positivity,?_⟩ - filter_upwards [grossDry,grossOur,plant,annual_valid] with j h1 h2 hp h J - filter_upwards [eventually_ge_atTop (J+20)] with M hM f hF hm hG - let m := expU f - let b := decide (1/Thermal.H j ≤ m) - let s := beginPlay f b - have he : J ≤ M := by omega - obtain ⟨t,ht⟩ := hp M J he f hF hm hG b - let W (x : Grid M) := annual x t - have hX (x : Grid M) : x ∈ s.here.block M := (mem_group _ _).mp - (by change x∈root.group M; rw [group_top]; simp) - refine ⟨W,fun x => ?_, ?_,?_⟩ - · exact le_trans (norm_nonneg _) (h M J he s f t ht 0 x (hX x)) - · rw [← top_avg W,avg_ex (root_ok M)] - change yieldA t ≤ _ - suffices H : ∀ b : Bool, decide (1/Thermal.H j ≤ m)=b → ∀ t : Play j M (beginPlay f b), t.Follows J f → yieldA t ≤ (10:ℝ)^222*height j from H b rfl t ht - intro b ha t ht - have hd := d_pos j - cases b - · have hh := le_of_not_ge (of_decide_eq_false ha) - exact le_trans (h1 M J f hG t ht (by rw [show (beginPlay f false).mass=m from top_avg _]; exact hh - )) (by linarith) - · have hh := of_decide_eq_true ha - have HE : 1000*(2:ℝ)^J ≤ 2^M := by - apply le_trans _ (pow_le_pow_right₀ (by norm_num) hM) - rw [pow_add] - nlinarith [show 0 < (2:ℝ)^J by positivity] - have HX : (beginPlay f true).mass=m := top_avg f - have HV := h2 M J HE f hG t ht (HX.symm ▸ hh) - rw [HX] at HV - have hc : m ≤ 2 := hm - change _ ≤ (10:ℝ)^222* height j - nlinarith - intro x hx u - have HE := stage_shift s t u x ht (hX x) hx - have HI : tailE s J x u=FinShift J (finMod f u) x := by - unfold tailE; rw [show s.here.lev=0 from rfl,Nat.Ico_zero_eq_range] - rfl - rw [HI,ordinary,show s.norm=0 from rfl,Complex.ofReal_zero,zero_mul,zero_add] at HE - rw [HE]; exact h M J he s f t ht u x (hX x) - -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Weighted + +/-! +# The grid bound +-/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Law Node Real Period Compress.Params Thermal +open Procedure Procedure.Scene Procedure.Paths Procedure.Paths.Troupe Procedure.Paths.CastShow +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +theorem gridProof : GridBound := by + refine ⟨10^222,by positivity,?_⟩ + filter_upwards [grossDry,grossOur,plant,annual_valid] with j h1 h2 hp h J + filter_upwards [eventually_ge_atTop (J+20)] with M hM f hF hm hG + let m := expU f + let b := decide (1/Thermal.H j ≤ m) + let s := beginPlay f b + have he : J ≤ M := by omega + obtain ⟨t,ht⟩ := hp M J he f hF hm hG b + let W (x : Grid M) := annual x t + have hX (x : Grid M) : x ∈ s.here.block M := (mem_group _ _).mp + (by change x∈root.group M; rw [group_top]; simp) + refine ⟨W,fun x => ?_, ?_,?_⟩ + · exact le_trans (norm_nonneg _) (h M J he s f t ht 0 x (hX x)) + · rw [← top_avg W,avg_ex (root_ok M)] + change yieldA t ≤ _ + suffices H : ∀ b : Bool, decide (1/Thermal.H j ≤ m)=b → ∀ t : Play j M (beginPlay f b), t.Follows J f → yieldA t ≤ (10:ℝ)^222*height j from H b rfl t ht + intro b ha t ht + have hd := d_pos j + cases b + · have hh := le_of_not_ge (of_decide_eq_false ha) + exact le_trans (h1 M J f hG t ht (by rw [show (beginPlay f false).mass=m from top_avg _]; exact hh + )) (by linarith) + · have hh := of_decide_eq_true ha + have HE : 1000*(2:ℝ)^J ≤ 2^M := by + apply le_trans _ (pow_le_pow_right₀ (by norm_num) hM) + rw [pow_add] + nlinarith [show 0 < (2:ℝ)^J by positivity] + have HX : (beginPlay f true).mass=m := top_avg f + have HV := h2 M J HE f hG t ht (HX.symm ▸ hh) + rw [HX] at HV + have hc : m ≤ 2 := hm + change _ ≤ (10:ℝ)^222* height j + nlinarith + intro x hx u + have HE := stage_shift s t u x ht (hX x) hx + have HI : tailE s J x u=FinShift J (finMod f u) x := by + unfold tailE; rw [show s.here.lev=0 from rfl,Nat.Ico_zero_eq_range] + rfl + rw [HI,ordinary,show s.norm=0 from rfl,Complex.ofReal_zero,zero_mul,zero_add] at HE + rw [HE]; exact h M J he s f t ht u x (hX x) + +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Festival.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Festival.lean index 79b01bc32928c952c04cecd352c9762b8fae7148..6a96a8fc22ae40b3647da1e0d0c1cc841f23c45e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Festival.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Festival.lean @@ -1,213 +1,213 @@ -import OAI.Analysis.FourierLLogL.Assembly.Tokens -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe - -lemma masked_split (m : Beat M → Prop) (x u : Grid M) (l : Path M) : - subtotal (proceeds x u) l=subtotal (aired m x u) l+subtotal (aired (fun b => ¬m b) x u) l := by - induction l with - | nil => simp [subtotal] - | cons b l ih => - simp only [subtotal_cons,ih] - by_cases h : m b <;> unfold aired <;> simp only [h,not_false_eq_true,ite_false,ite_true,zero_add,not_true] - all_goals abel - -lemma subt_le {l : Path M} {f g : Beat M → ℝ} (h : ∀ b∈l, f b ≤ g b) : - subtotal f l ≤ subtotal g l := by - induction l with - | nil => exact le_rfl - | cons b l ih => - exact add_le_add (h b List.mem_cons_self) - (ih (fun i hi => h i (List.mem_cons_of_mem _ hi))) - -def crumb (n T j : ℕ) (L : Path M) := - 20000*∑ i∈Finset.Ico n T, scalarSticker i j L - -lemma start_ico (t n : ℕ) (h : n < t) (f : ℕ → ℝ) : - ∑ i∈Finset.Ico n t,f i = f n+∑ i∈Finset.Ico (n+1) t,f i := by - have he : Finset.Ico n t=insert n (Finset.Ico (n+1) t) := by - ext i; simp only [Finset.mem_insert, Finset.mem_Ico]; omega - rw [he, Finset.sum_insert (by simp)] - -lemma scalar_stage : - ∀ᶠ j : ℕ in atTop, - ∀ (M J : ℕ) (s : Scene M) (c : Bool) (f : Grid M → ℝ) (x u : Grid M), - let t := lowI j c - let T := highI j M c - ∀ (L : Path M) (a : Grade → ℕ), Cast j J f s L → Trip x s L → shades a u L j → - ∀ (l r : Path M), L=l++r → (c=true→stall u (tagM (mainIx j)) j L l) → - ‖subtotal (proceeds x u) l‖ ≤ - subtotal (token a L j c) l + crumb t T j L := by - filter_upwards [mask_estimate] with j hj M J s c f x u t T L a h hh hV - let P := proceeds x u - let q := token a L j c - suffices HE : ∀ k n : ℕ, t ≤ n → n+k=T → ∀ l r : Path M, L=l++r → - (c=true → stall u (tagM (mainIx j)) j L l) → - (∀ i∈Finset.Ico t n, stall u (tagL i) j L l) → - ‖subtotal P l‖ ≤ subtotal q l+crumb n T j L by - intro l r hx hf - have hT : t ≤ T := Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_right _ _) - exact HE (T-t) t (le_refl _) (by omega) l r hx hf (by simp) - intro k - induction k with - | zero => - intro n hn hE l r hl hf he - obtain rfl := (show T=n by omega) - rw [show crumb T T j L=0 by simp [crumb],add_zero] - apply (subt_norm l P).trans - exact subt_le (fun b hb => direct_point hh j c a hV T l r b hf he ⟨hn,le_rfl⟩ hl hb (by omega)) - | succ k ih => - intro n hn hE l r hl hf he - have hT : n < T := by omega - have HV : crumb n T j L = 20000*scalarSticker n j L + crumb (n+1) T j L := by - rw [crumb, start_ico T n hT,mul_add]; rfl - rw [HV] - let g := tagL n - have HP (p w : Path M) (hL : l=p++w) (hh : stall u g j L p) : - ‖subtotal P p‖ ≤ subtotal q p + crumb (n+1) T j L := by - have hx (g) (hh : stall u g j L l) : stall u g j L p := by - rw [hL] at hh - exact ((mute_cat _ _ _ _ _ _).1 hh).1 - apply ih (n+1) (hn.trans (Nat.le_add_right n 1)) (by omega) p (w++r) ?_ - (fun H => hx _ (hf H)) - · intro i hi - obtain ⟨h₁,h₂⟩ := Finset.mem_Ico.mp hi - by_cases heq : i=n - · subst heq; exact hh - · exact hx _ (he _ (Finset.mem_Ico.mpr ⟨h₁,by omega⟩)) - rw [hl,hL,List.append_assoc] - let p := heroPlot g L - rcases first_on u p (vacant M) j l with H|⟨d,b,m,H,h₁,h₂⟩ - · have Hx := HP l [] (by simp) H - have HE := scalarSticker_pos n j hh; linarith - · let w := b::m - let L' := d++(w++r) - have HB : L=L' := by unfold L' w; rw [hl,H,List.append_assoc] - have HC : ringing u (heroPlot g L') j d b := HB ▸ h₂ - have Hw : l=d++w := H - obtain ⟨v,hX⟩ := hj M J n s f d m r b x u (show Cast j J f s L' from HB ▸ h) (show Trip x s L' from HB ▸ hh) HC - let μ := guardedV n v; let Q := aired μ x u; let R := aired (fun z => ¬μ z) x u - change ‖subtotal Q w‖ ≤ _*scalarSticker n j L' ∧ _ at hX - have hx₁ : ‖subtotal Q w‖ ≤ 20000* scalarSticker n j L := by rw [HB]; exact hX.1 - have hx₂ : ‖subtotal R w‖ ≤ subtotal q w := by - apply (subt_norm w R).trans; apply subt_le - intro z hz - unfold R aired; split - · rename_i HH - apply direct_point hh j c a hV n l r z hf he ⟨hn,hT.le⟩ hl (by rw [Hw]; simp [hz]) - intro _; exact hX.2 z hz HH - · rw [norm_zero]; exact tokens_nonneg a L z j c - rw [Hw,subtotal_app,subtotal_app, - show subtotal P w=subtotal Q w+subtotal R w from masked_split μ x u w] - apply (norm_add_le _ _).trans - linarith [HP d w Hw h₁,norm_add_le (subtotal Q w) (subtotal R w)] - -def bigCrumb (n t j : ℕ) (L : Path M) := - 10^20* ∑ i∈Finset.Ico n t, mainSticker i j L - -/-- Main handles a contour interval, consuming the small leading prefix only once. -/ -lemma main_stage : - ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ) (s : Scene M) (f : Grid M → ℝ) (x u : Grid M) - (L : Path M), s.live=true → Cast j J f s L → Trip x s L → - ∀ (v : ℝ), - (∀ l r : Path M, L=l++r → (∀ i, i ≤ mainIx j → stall u (tagM i) j L l) → - ‖subtotal (proceeds x u) l‖ ≤ v) → - ‖subtotal (proceeds x u) L‖ ≤ v+10^20*fullFax L+ - bigCrumb 0 (mainIx j+1) j L := by - filter_upwards [ride] with j hj M J s f x u L hs hc ht v hv - let P := proceeds x u - let T := mainIx j+1 - suffices H : ∀ k n : ℕ, n+k=T → ∀ (l r : Path M), L=l++r → - (∀ i, i < n → stall u (tagM i) j L l) → - ‖subtotal P l‖ ≤ v+10^20*fullFax l+bigCrumb n T j L from - H T 0 (by omega) L [] (by simp) (by omega) - intro k; induction k with - | zero => - intro n hh l r he hf - have HX : Trip x s (l++r) := he ▸ ht - have hT : n=T := by omega - cases hT - have HH := hv l r he (fun i hi => hf i (Nat.lt_succ_of_le hi)) - have HJ := trip_fax (trip_cut HX).1 - simp only [bigCrumb,Finset.Ico_self,Finset.sum_empty,mul_zero,add_zero] - unfold P; linarith - | succ k ih => - intro n hn l r hl hf - let g := tagM n; let p := heroPlot g L - have hE : n < mainIx j+1 := Nat.lt_of_lt_of_le (by omega : n < T) (le_refl _) - have hX : Cast j J f s (l++r) := hl ▸ hc - have hY : Trip x s (l++r) := hl ▸ ht - have HI (d w : Path M) (hD : l=d++w) (hh : stall u g j L d) : - ‖subtotal P d‖ ≤ v+10^20*fullFax d+bigCrumb (n+1) T j L := by - apply ih _ (by omega) d (w++r) ?_ - · intro i hi - by_cases he : i=n; subst i; exact hh - have H := hf i (by omega) - rw [hD] at H - exact ((mute_cat _ _ _ _ _ _).1 H).1 - rw [hl,hD,List.append_assoc] - rw [show bigCrumb n T j L=10^20*mainSticker n j L+bigCrumb (n+1) T j L by - unfold bigCrumb; rw [start_ico _ n hE,mul_add]] - rcases first_on u p (vacant M) j l with H|⟨d,b,w,hD,hp,ha⟩ - · have HE := HI l [] (by simp) H - linarith [mainSticker_pos n j ht] - · let D := b::w; let X := d++(D++r) - have heq : X = L := by unfold X D; rw [hl,hD,List.append_assoc] - have H : Cast j J f s X := heq.symm ▸ hc; have HR : Trip x s X := heq.symm ▸ ht - have HF : ringing u (heroPlot g X) j d b := by rw [heq]; exact ha - have Ht (hh : 0 < n) : stall u (tagM (n-1)) j X (d++D) := by rw [heq]; exact (hD ▸ hf (n-1) (by omega)) - have HH := hj M J n s f d w r b x u H HR (fun _ => ⟨by change n ≤ mainIx j; omega,hs⟩) HF Ht - change ‖subtotal P D‖ ≤ _*(fullFax D+mainSticker n j X) at HH - rw [heq] at HH - rw [show l=d++D from hD,subtotal_app,show fullFax (d++D)=fullFax d+fullFax D from subtotal_app billFax d D] - apply (norm_add_le _ _).trans; linarith [HI d D hD hp] - -def souvenir (c : Bool) (j : ℕ) (L : Path M) := - let t := lowI j c; let T := highI j M c - tokenCost j c L+crumb t T j L+ - (if c then 10^20*fullFax L+bigCrumb 0 (mainIx j+1) j L else 0) - -lemma gala : - ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ), J ≤ M → ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ x u : Grid M, ∀ L : Path M, Cast j J f s L → Trip x s L → - ‖subtotal (proceeds x u) L‖ ≤ souvenir s.live j L := by - filter_upwards [scalar_stage, main_stage] with j hs hm M J hJ s f x u L h hh - obtain ⟨a,ha⟩ := sheds h u - let V (c : Bool) := tokenCost j c L+ crumb (lowI j c) (highI j M c) j L - have HV (b : Bool) (l r : Path M) (he : L=l++r) (ht : b=true→stall u (tagM (mainIx j)) j L l) : - ‖subtotal (proceeds x u) l‖ ≤ V b := by - apply (hs M J s b f x u L a h hh ha l r he ht).trans - apply add_le_add_left - have hM : L.length ≤ M := by have HE := trip_depth_limit h; omega - apply le_trans _ (max_tokens L hh a j b hM) - obtain ⟨hx,V,hR⟩ := trip_cut (show Trip x s (l++r) from he ▸ hh) - nth_rw 3 [he] - exact le_trans (le_add_of_nonneg_right (trip_nonneg hR (f:=token a L j b) - (fun i _ => tokens_nonneg _ _ _ _ _))) (le_of_eq (subtotal_app _ l r).symm) - unfold souvenir - by_cases HB : s.live=true - · have HH := hm M J s f x u L HB h hh (V true) (fun l r hL he => - HV _ _ _ hL (fun _ => he _ (le_refl _))) - rw [HB,ite_eq_left rfl] - unfold V at HH; linarith - rw [show s.live=false from Bool.eq_false_iff.mpr HB,ite_eq_right (by simp),add_zero] - exact HV false L [] (by simp) fun h => Bool.noConfusion h - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Tokens +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe + +lemma masked_split (m : Beat M → Prop) (x u : Grid M) (l : Path M) : + subtotal (proceeds x u) l=subtotal (aired m x u) l+subtotal (aired (fun b => ¬m b) x u) l := by + induction l with + | nil => simp [subtotal] + | cons b l ih => + simp only [subtotal_cons,ih] + by_cases h : m b <;> unfold aired <;> simp only [h,not_false_eq_true,ite_false,ite_true,zero_add,not_true] + all_goals abel + +lemma subt_le {l : Path M} {f g : Beat M → ℝ} (h : ∀ b∈l, f b ≤ g b) : + subtotal f l ≤ subtotal g l := by + induction l with + | nil => exact le_rfl + | cons b l ih => + exact add_le_add (h b List.mem_cons_self) + (ih (fun i hi => h i (List.mem_cons_of_mem _ hi))) + +def crumb (n T j : ℕ) (L : Path M) := + 20000*∑ i∈Finset.Ico n T, scalarSticker i j L + +lemma start_ico (t n : ℕ) (h : n < t) (f : ℕ → ℝ) : + ∑ i∈Finset.Ico n t,f i = f n+∑ i∈Finset.Ico (n+1) t,f i := by + have he : Finset.Ico n t=insert n (Finset.Ico (n+1) t) := by + ext i; simp only [Finset.mem_insert, Finset.mem_Ico]; omega + rw [he, Finset.sum_insert (by simp)] + +lemma scalar_stage : + ∀ᶠ j : ℕ in atTop, + ∀ (M J : ℕ) (s : Scene M) (c : Bool) (f : Grid M → ℝ) (x u : Grid M), + let t := lowI j c + let T := highI j M c + ∀ (L : Path M) (a : Grade → ℕ), Cast j J f s L → Trip x s L → shades a u L j → + ∀ (l r : Path M), L=l++r → (c=true→stall u (tagM (mainIx j)) j L l) → + ‖subtotal (proceeds x u) l‖ ≤ + subtotal (token a L j c) l + crumb t T j L := by + filter_upwards [mask_estimate] with j hj M J s c f x u t T L a h hh hV + let P := proceeds x u + let q := token a L j c + suffices HE : ∀ k n : ℕ, t ≤ n → n+k=T → ∀ l r : Path M, L=l++r → + (c=true → stall u (tagM (mainIx j)) j L l) → + (∀ i∈Finset.Ico t n, stall u (tagL i) j L l) → + ‖subtotal P l‖ ≤ subtotal q l+crumb n T j L by + intro l r hx hf + have hT : t ≤ T := Nat.le_trans (Nat.le_add_right _ _) (Nat.le_add_right _ _) + exact HE (T-t) t (le_refl _) (by omega) l r hx hf (by simp) + intro k + induction k with + | zero => + intro n hn hE l r hl hf he + obtain rfl := (show T=n by omega) + rw [show crumb T T j L=0 by simp [crumb],add_zero] + apply (subt_norm l P).trans + exact subt_le (fun b hb => direct_point hh j c a hV T l r b hf he ⟨hn,le_rfl⟩ hl hb (by omega)) + | succ k ih => + intro n hn hE l r hl hf he + have hT : n < T := by omega + have HV : crumb n T j L = 20000*scalarSticker n j L + crumb (n+1) T j L := by + rw [crumb, start_ico T n hT,mul_add]; rfl + rw [HV] + let g := tagL n + have HP (p w : Path M) (hL : l=p++w) (hh : stall u g j L p) : + ‖subtotal P p‖ ≤ subtotal q p + crumb (n+1) T j L := by + have hx (g) (hh : stall u g j L l) : stall u g j L p := by + rw [hL] at hh + exact ((mute_cat _ _ _ _ _ _).1 hh).1 + apply ih (n+1) (hn.trans (Nat.le_add_right n 1)) (by omega) p (w++r) ?_ + (fun H => hx _ (hf H)) + · intro i hi + obtain ⟨h₁,h₂⟩ := Finset.mem_Ico.mp hi + by_cases heq : i=n + · subst heq; exact hh + · exact hx _ (he _ (Finset.mem_Ico.mpr ⟨h₁,by omega⟩)) + rw [hl,hL,List.append_assoc] + let p := heroPlot g L + rcases first_on u p (vacant M) j l with H|⟨d,b,m,H,h₁,h₂⟩ + · have Hx := HP l [] (by simp) H + have HE := scalarSticker_pos n j hh; linarith + · let w := b::m + let L' := d++(w++r) + have HB : L=L' := by unfold L' w; rw [hl,H,List.append_assoc] + have HC : ringing u (heroPlot g L') j d b := HB ▸ h₂ + have Hw : l=d++w := H + obtain ⟨v,hX⟩ := hj M J n s f d m r b x u (show Cast j J f s L' from HB ▸ h) (show Trip x s L' from HB ▸ hh) HC + let μ := guardedV n v; let Q := aired μ x u; let R := aired (fun z => ¬μ z) x u + change ‖subtotal Q w‖ ≤ _*scalarSticker n j L' ∧ _ at hX + have hx₁ : ‖subtotal Q w‖ ≤ 20000* scalarSticker n j L := by rw [HB]; exact hX.1 + have hx₂ : ‖subtotal R w‖ ≤ subtotal q w := by + apply (subt_norm w R).trans; apply subt_le + intro z hz + unfold R aired; split + · rename_i HH + apply direct_point hh j c a hV n l r z hf he ⟨hn,hT.le⟩ hl (by rw [Hw]; simp [hz]) + intro _; exact hX.2 z hz HH + · rw [norm_zero]; exact tokens_nonneg a L z j c + rw [Hw,subtotal_app,subtotal_app, + show subtotal P w=subtotal Q w+subtotal R w from masked_split μ x u w] + apply (norm_add_le _ _).trans + linarith [HP d w Hw h₁,norm_add_le (subtotal Q w) (subtotal R w)] + +def bigCrumb (n t j : ℕ) (L : Path M) := + 10^20* ∑ i∈Finset.Ico n t, mainSticker i j L + +/-- Main handles a contour interval, consuming the small leading prefix only once. -/ +lemma main_stage : + ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ) (s : Scene M) (f : Grid M → ℝ) (x u : Grid M) + (L : Path M), s.live=true → Cast j J f s L → Trip x s L → + ∀ (v : ℝ), + (∀ l r : Path M, L=l++r → (∀ i, i ≤ mainIx j → stall u (tagM i) j L l) → + ‖subtotal (proceeds x u) l‖ ≤ v) → + ‖subtotal (proceeds x u) L‖ ≤ v+10^20*fullFax L+ + bigCrumb 0 (mainIx j+1) j L := by + filter_upwards [ride] with j hj M J s f x u L hs hc ht v hv + let P := proceeds x u + let T := mainIx j+1 + suffices H : ∀ k n : ℕ, n+k=T → ∀ (l r : Path M), L=l++r → + (∀ i, i < n → stall u (tagM i) j L l) → + ‖subtotal P l‖ ≤ v+10^20*fullFax l+bigCrumb n T j L from + H T 0 (by omega) L [] (by simp) (by omega) + intro k; induction k with + | zero => + intro n hh l r he hf + have HX : Trip x s (l++r) := he ▸ ht + have hT : n=T := by omega + cases hT + have HH := hv l r he (fun i hi => hf i (Nat.lt_succ_of_le hi)) + have HJ := trip_fax (trip_cut HX).1 + simp only [bigCrumb,Finset.Ico_self,Finset.sum_empty,mul_zero,add_zero] + unfold P; linarith + | succ k ih => + intro n hn l r hl hf + let g := tagM n; let p := heroPlot g L + have hE : n < mainIx j+1 := Nat.lt_of_lt_of_le (by omega : n < T) (le_refl _) + have hX : Cast j J f s (l++r) := hl ▸ hc + have hY : Trip x s (l++r) := hl ▸ ht + have HI (d w : Path M) (hD : l=d++w) (hh : stall u g j L d) : + ‖subtotal P d‖ ≤ v+10^20*fullFax d+bigCrumb (n+1) T j L := by + apply ih _ (by omega) d (w++r) ?_ + · intro i hi + by_cases he : i=n; subst i; exact hh + have H := hf i (by omega) + rw [hD] at H + exact ((mute_cat _ _ _ _ _ _).1 H).1 + rw [hl,hD,List.append_assoc] + rw [show bigCrumb n T j L=10^20*mainSticker n j L+bigCrumb (n+1) T j L by + unfold bigCrumb; rw [start_ico _ n hE,mul_add]] + rcases first_on u p (vacant M) j l with H|⟨d,b,w,hD,hp,ha⟩ + · have HE := HI l [] (by simp) H + linarith [mainSticker_pos n j ht] + · let D := b::w; let X := d++(D++r) + have heq : X = L := by unfold X D; rw [hl,hD,List.append_assoc] + have H : Cast j J f s X := heq.symm ▸ hc; have HR : Trip x s X := heq.symm ▸ ht + have HF : ringing u (heroPlot g X) j d b := by rw [heq]; exact ha + have Ht (hh : 0 < n) : stall u (tagM (n-1)) j X (d++D) := by rw [heq]; exact (hD ▸ hf (n-1) (by omega)) + have HH := hj M J n s f d w r b x u H HR (fun _ => ⟨by change n ≤ mainIx j; omega,hs⟩) HF Ht + change ‖subtotal P D‖ ≤ _*(fullFax D+mainSticker n j X) at HH + rw [heq] at HH + rw [show l=d++D from hD,subtotal_app,show fullFax (d++D)=fullFax d+fullFax D from subtotal_app billFax d D] + apply (norm_add_le _ _).trans; linarith [HI d D hD hp] + +def souvenir (c : Bool) (j : ℕ) (L : Path M) := + let t := lowI j c; let T := highI j M c + tokenCost j c L+crumb t T j L+ + (if c then 10^20*fullFax L+bigCrumb 0 (mainIx j+1) j L else 0) + +lemma gala : + ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ), J ≤ M → ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ x u : Grid M, ∀ L : Path M, Cast j J f s L → Trip x s L → + ‖subtotal (proceeds x u) L‖ ≤ souvenir s.live j L := by + filter_upwards [scalar_stage, main_stage] with j hs hm M J hJ s f x u L h hh + obtain ⟨a,ha⟩ := sheds h u + let V (c : Bool) := tokenCost j c L+ crumb (lowI j c) (highI j M c) j L + have HV (b : Bool) (l r : Path M) (he : L=l++r) (ht : b=true→stall u (tagM (mainIx j)) j L l) : + ‖subtotal (proceeds x u) l‖ ≤ V b := by + apply (hs M J s b f x u L a h hh ha l r he ht).trans + apply add_le_add_left + have hM : L.length ≤ M := by have HE := trip_depth_limit h; omega + apply le_trans _ (max_tokens L hh a j b hM) + obtain ⟨hx,V,hR⟩ := trip_cut (show Trip x s (l++r) from he ▸ hh) + nth_rw 3 [he] + exact le_trans (le_add_of_nonneg_right (trip_nonneg hR (f:=token a L j b) + (fun i _ => tokens_nonneg _ _ _ _ _))) (le_of_eq (subtotal_app _ l r).symm) + unfold souvenir + by_cases HB : s.live=true + · have HH := hm M J s f x u L HB h hh (V true) (fun l r hL he => + HV _ _ _ hL (fun _ => he _ (le_refl _))) + rw [HB,ite_eq_left rfl] + unfold V at HH; linarith + rw [show s.live=false from Bool.eq_false_iff.mpr HB,ite_eq_right (by simp),add_zero] + exact HV false L [] (by simp) fun h => Bool.noConfusion h + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Gross.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Gross.lean index a99a4d6be76f693a9adf970b0e1ec2ccd1a47bd3..9220622a3479cb0144891eadc5f88cedf264577b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Gross.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Gross.lean @@ -1,225 +1,225 @@ -import OAI.Analysis.FourierLLogL.Assembly.Cast -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace CastShow -open Troupe - -def jackpot (x : Grid M) {s : Scene M} (t : Play j M s) := - s.norm*souvenir s.live j ((project t).stream x) -def annual (x : Grid M) {s : Scene M} (t : Play j M s) : ℝ := match t with -| .rest _ => 0 -| .ticket _ t => jackpot x t+annual x t -| .freeze _ t => annual x t -| .attempt _ _ l r => annual x l+annual x r -| .jump s l r => if (step s).bit x then annual x r else annual x l - -lemma fit_ticket {s : Scene M} {t : Play j M s.reset} (h : (Play.ticket s t).Follows J f) : - Fit s.reset ∧ entry s.reset ∧ (s.live=true → exp (-height j) ≤ s.reset.norm) := by - obtain ⟨ha,hb,hc,hd,he⟩ := h - have hh := hb.2.2 - refine ⟨⟨⟨ha.within,ha.positive⟩,hh,?_⟩,rfl, fun H => le_of_not_gt (fun h => hc ⟨H,Or.inr h⟩)⟩ - change s.mass/s.mass ≤ 4; rw [div_self hh.ne']; norm_num - -lemma annual_valid : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, J ≤ M → ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Play j M s, t.Follows J f → ∀ u x : Grid M, x∈s.here.block M → - ‖summons u x t‖ ≤ annual x t := by - filter_upwards [gala] with j hj M J hh s f t h u x hx - induction t with - | rest s => simp [summons,annual] - | ticket s t ih => - rw [summons,annual] - have HT := fit_ticket h - obtain ⟨ha,hb,hc,hd,he⟩ := h - apply le_trans (norm_add_le _ _) - apply add_le_add _ (ih he hx) - let q := t - unfold ordinary jackpot; rw [norm_scaleC,abs_of_nonneg HT.1.pos.le] - apply mul_le_mul_of_nonneg_left _ HT.1.pos.le - have H := Thread.projected _ he - exact hj _ _ hh _ f _ _ _ (stream_cast _ H x) (trip_stream _ H HT.1 hx) - | freeze s t ih => exact ih h.2.2.2 hx - | attempt s v l r il ir => - rw [summons,annual] - exact (norm_add_le _ _).trans (add_le_add (il h.2.2.2.2.1 hx) (ir h.2.2.2.2.2 hx)) - | jump s l r il ir => - rw [summons,annual] - have HE := stream_child (less_self _) hx - split_ifs with HH - · exact ir h.2.2.2.2 (HH ▸ HE) - · rw [show (step s).bit x=false from Bool.eq_false_iff.mpr HH] at HE - exact il h.2.2.2.1 HE - -/-- Prices in true history. -/ -def normStamp (s : Scene M) (j : ℕ) := - if s.live then (s.norm/s.mass)*stamp s j else 0 -def billOf : Event j M → ℝ -| .pace s .renew => gateTax s.reset j -| .march s .move _ => normStamp s j -| _ => 0 - -def netGross {s : Scene M} (t : Play j M s) := (trace true t).flatFee billOf -def yieldA {s : Scene M} (t : Play j M s) := - (s.here.dice M).Ex (fun x => annual x t) -def overdue {s : Scene M} (t : Play j M s) := - if s.live then s.norm*stats (project t) (subtotal (fun b => stamp b.post j)) else 0 - -lemma dueEmpty {s : Scene M} {t : Play j M s} (h : project t=blank s) : overdue t=0 := by - unfold overdue; rw [h] - split; simp only [stats_nil,subtotal_nil,mul_zero]; rfl -lemma due_restart {s : Scene M} {t : Play j M s} (h : s.norm=0) : overdue t=0 := by simp [overdue,h] - -lemma cost_jump (s : Scene M) (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) - (h : (Play.jump s l r).Follows J f) : - let a := s.norm*(if s.live then stamp s j else 0) - yieldA (Play.jump s l r) + overdue (Play.jump s l r) = - ((yieldA l+overdue l)+(yieldA r+overdue r))/2+a := -by - intro a - let t := Play.jump s l r - - let k (b : Bool) : Thread M (s.smaller b) := match b with | false => project l | true => project r - have HG : Following j J f (ran s s k) := projected _ h - obtain ⟨ha,hb,hc,hd,he⟩ := h - let v (b : Thread.Beat M) := stamp b.post j - let U (q : Scene M) := q.here.dice M - have hM : s.here.lev < M := (Play.first hd).within.2 - have HH : yieldA t=(yieldA l+yieldA r)/2 := by - unfold yieldA - rw [dice_child s.here ha.within.1 hM] - have H (b : Bool) (x : Grid M) (hx : 0 < (U (s.smaller b)) x) : - (step s).bit x=b := step_dice_child s.here ha.within.1 hM b hx - have HE : (U (s.smaller false)).Ex (fun x => annual x t)=yieldA l := by - apply Ex_congr_mem; intro x hx; unfold t; rw [annual,H false x hx]; rfl - have HF : (U (s.smaller true)).Ex (fun x => annual x t)=yieldA r := by - apply Ex_congr_mem; intro x hx; unfold t; rw [annual,H true x hx]; rfl - change ((U (s.smaller false)).Ex (fun x => annual x t) + (U (s.smaller true)).Ex (fun x => annual x t))/2=(yieldA l+yieldA r)/2 - rw [HE,HF] - have HF : (project t).fee v=stamp s j+((project l).fee v+(project r).fee v)/2 := by - change (ran s s k).fee v=_ - rw [Thread.fee_ran k HG,fair_av] - change (stamp s j+_+(stamp s j+_))/2=_ - unfold k; ring - change yieldA t+_ = _ - rw [HH] - unfold a overdue - change (yieldA l+yieldA r)/2 +(if s.live=true then s.norm*(project t).fee v else 0) = - ((yieldA l+(if s.live=true then s.norm*(project l).fee v else 0))+ - (yieldA r+(if s.live=true then s.norm*(project r).fee v else 0)))/2+ - s.norm*(if s.live then _ else 0) - rw [HF]; split <;> ring - -lemma cost_attempt (s : Scene M) (u : Update j M s.here) - (l : Play j M (s.lose u)) (r : Play j M (s.win u)) : - yieldA (Play.attempt s u l r)+overdue (Play.attempt s u l r) = - yieldA l+overdue l+(yieldA r+overdue r) := by - rw [due_restart (s:=s.win u) rfl] - have h₁ : yieldA (Play.attempt s u l r)=yieldA l+yieldA r := - Ex_add (s.here.dice M) _ _ - have h₂ : overdue (Play.attempt s u l r)=overdue l := by - unfold overdue; rw [project] - cases project l <;> rfl - rw [h₁,h₂]; ring - -lemma cost_overall : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s : Scene M, - ∀ t : Play j M s, t.Follows J f → (∀ x, f x ≤ exp (height j)) → - yieldA t+overdue t ≤ s.mass*netGross t := by - filter_upwards [receipt_paid] with j hj M J f s t h hf - induction t with - | rest s => - rw [netGross,trace,flat_finish,dueEmpty rfl] - simp_rw [yieldA,annual,Ex_const]; simp - | ticket s t ih => - let v := s.reset - have hs := fit_ticket h - obtain ⟨ha,hb,hc,hd,he⟩ := h - have HE := hj M J v (project t) f (projected _ he) hs.1 hs.2.1 hs.2.2 hf - have HA : yieldA (Play.ticket s t)= - v.norm*stats (project t) (souvenir v.live j)+yieldA t := by - unfold yieldA - simp_rw [annual,jackpot]; rw [Ex_add,Ex_lmul] - rfl - rw [HA, netGross, trace, flat_push,dueEmpty rfl,add_zero] - let x := stats (project t) (souvenir v.live j) - change v.norm*x+_ ≤ s.mass*(gateTax s.reset j+netGross t) - let a : ℝ := if v.live then stats (project t) (subtotal (fun b => stamp b.post j)) else 0 - change x ≤ gateTax v j+a at HE - have Hi := ih he - rw [show overdue t = s.mass*a by unfold overdue a v; split <;> first|rfl|simp] at Hi - change yieldA t+s.mass*a ≤ s.mass*_ at Hi - change s.mass*x+_ ≤ s.mass*(gateTax v j+_) - nlinarith [hb.2.2] - | freeze s t ih => - rw [dueEmpty rfl,netGross,trace] - rw [ite_eq_left rfl,flat_push] - have HH := ih h.2.2.2 - rw [due_restart (s:=s.out) rfl] at HH - change yieldA t+0 ≤ s.mass*(0+netGross t) - rw [zero_add]; exact HH - | jump s l r il ir => - let t := Play.jump s l r - rw [cost_jump _ _ _ h] - have H₁ := traced t true h - unfold netGross; rw [trace,flat_fork _ _ H₁] - obtain ⟨ha,hb,hc,hd,he⟩ := h - let g (b : Bool) := if b then netGross r else netGross l - - let v : Bool → ℝ := fun b => match b with - | false => netGross l - | true => netGross r - let w : ∀ b, Road j M (s.smaller b) := fun b => match b with - | false => trace true l - | true => trace true r - let P := (step s).probs - let m := s.mass; let x := s.norm*(if s.live then stamp s j else 0) - have HV (b : Bool) : (w b).flatFee billOf = v b := by cases b <;> rfl - have heq : P.Ex (fun b : Bool => normStamp s j+(w b).flatFee billOf) = normStamp s j+P.Ex v := by - simp_rw [HV,Ex_add _ (f:=fun _ => _),Ex_const] - change _ + x ≤ s.mass*P.Ex (fun b => normStamp s j+(w b).flatFee billOf) - rw [heq,mul_add] - have hM : s.here.lev < M := (Play.first hd).within.2 - have hi := output_split s ha.within.1 hM ha.positive hb.1.2.2 v - have hm := hb.1.2.2 - rw [show s.mass*normStamp s j=x by unfold x normStamp; split; field_simp; simp] - change s.mass*P.Ex v=_ at hi - rw [hi,fair_av] - have HI : (fun b => (s.smaller b).mass*v b) false = (s.smaller false).mass*netGross l := rfl - change _/2+x ≤ x+ ((s.smaller false).mass*netGross l+(s.smaller true).mass*netGross r)/2 - linarith [il hd,ir he] - | attempt s u l r il ir => - let t := Play.attempt s u l r - let v := toss s u; let P := v.probs - have H := traced t true h - rw [cost_attempt,netGross,trace,flat_fork _ _ H] - obtain ⟨ha,hb,hc,hd,he,hx⟩ := h - let w : ∀ b, Road j M (v.dst b) := fun b => match b with - | false => trace true l - | true => trace true r - change _ ≤ s.mass*P.Ex (fun b => 0+(w b).flatFee billOf) - simp only [zero_add,Ex,Fintype.sum_bool] - have Hi := toss_true u ha.within ha.positive hb.1.2.2 - have HH : (s.lose u).mass = P false*s.mass := toss_mass Hi false - have HJ : (s.win u).mass = P true*s.mass := toss_mass Hi true - change _ ≤ s.mass*(P true*netGross r+P false*netGross l) - have Hi₁ := il he; have Hi₂ := ir hx - rw [HH] at Hi₁; rw [HJ] at Hi₂; linarith - -end CastShow - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Cast +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace CastShow +open Troupe + +def jackpot (x : Grid M) {s : Scene M} (t : Play j M s) := + s.norm*souvenir s.live j ((project t).stream x) +def annual (x : Grid M) {s : Scene M} (t : Play j M s) : ℝ := match t with +| .rest _ => 0 +| .ticket _ t => jackpot x t+annual x t +| .freeze _ t => annual x t +| .attempt _ _ l r => annual x l+annual x r +| .jump s l r => if (step s).bit x then annual x r else annual x l + +lemma fit_ticket {s : Scene M} {t : Play j M s.reset} (h : (Play.ticket s t).Follows J f) : + Fit s.reset ∧ entry s.reset ∧ (s.live=true → exp (-height j) ≤ s.reset.norm) := by + obtain ⟨ha,hb,hc,hd,he⟩ := h + have hh := hb.2.2 + refine ⟨⟨⟨ha.within,ha.positive⟩,hh,?_⟩,rfl, fun H => le_of_not_gt (fun h => hc ⟨H,Or.inr h⟩)⟩ + change s.mass/s.mass ≤ 4; rw [div_self hh.ne']; norm_num + +lemma annual_valid : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, J ≤ M → ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Play j M s, t.Follows J f → ∀ u x : Grid M, x∈s.here.block M → + ‖summons u x t‖ ≤ annual x t := by + filter_upwards [gala] with j hj M J hh s f t h u x hx + induction t with + | rest s => simp [summons,annual] + | ticket s t ih => + rw [summons,annual] + have HT := fit_ticket h + obtain ⟨ha,hb,hc,hd,he⟩ := h + apply le_trans (norm_add_le _ _) + apply add_le_add _ (ih he hx) + let q := t + unfold ordinary jackpot; rw [norm_scaleC,abs_of_nonneg HT.1.pos.le] + apply mul_le_mul_of_nonneg_left _ HT.1.pos.le + have H := Thread.projected _ he + exact hj _ _ hh _ f _ _ _ (stream_cast _ H x) (trip_stream _ H HT.1 hx) + | freeze s t ih => exact ih h.2.2.2 hx + | attempt s v l r il ir => + rw [summons,annual] + exact (norm_add_le _ _).trans (add_le_add (il h.2.2.2.2.1 hx) (ir h.2.2.2.2.2 hx)) + | jump s l r il ir => + rw [summons,annual] + have HE := stream_child (less_self _) hx + split_ifs with HH + · exact ir h.2.2.2.2 (HH ▸ HE) + · rw [show (step s).bit x=false from Bool.eq_false_iff.mpr HH] at HE + exact il h.2.2.2.1 HE + +/-- Prices in true history. -/ +def normStamp (s : Scene M) (j : ℕ) := + if s.live then (s.norm/s.mass)*stamp s j else 0 +def billOf : Event j M → ℝ +| .pace s .renew => gateTax s.reset j +| .march s .move _ => normStamp s j +| _ => 0 + +def netGross {s : Scene M} (t : Play j M s) := (trace true t).flatFee billOf +def yieldA {s : Scene M} (t : Play j M s) := + (s.here.dice M).Ex (fun x => annual x t) +def overdue {s : Scene M} (t : Play j M s) := + if s.live then s.norm*stats (project t) (subtotal (fun b => stamp b.post j)) else 0 + +lemma dueEmpty {s : Scene M} {t : Play j M s} (h : project t=blank s) : overdue t=0 := by + unfold overdue; rw [h] + split; simp only [stats_nil,subtotal_nil,mul_zero]; rfl +lemma due_restart {s : Scene M} {t : Play j M s} (h : s.norm=0) : overdue t=0 := by simp [overdue,h] + +lemma cost_jump (s : Scene M) (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) + (h : (Play.jump s l r).Follows J f) : + let a := s.norm*(if s.live then stamp s j else 0) + yieldA (Play.jump s l r) + overdue (Play.jump s l r) = + ((yieldA l+overdue l)+(yieldA r+overdue r))/2+a := +by + intro a + let t := Play.jump s l r + + let k (b : Bool) : Thread M (s.smaller b) := match b with | false => project l | true => project r + have HG : Following j J f (ran s s k) := projected _ h + obtain ⟨ha,hb,hc,hd,he⟩ := h + let v (b : Thread.Beat M) := stamp b.post j + let U (q : Scene M) := q.here.dice M + have hM : s.here.lev < M := (Play.first hd).within.2 + have HH : yieldA t=(yieldA l+yieldA r)/2 := by + unfold yieldA + rw [dice_child s.here ha.within.1 hM] + have H (b : Bool) (x : Grid M) (hx : 0 < (U (s.smaller b)) x) : + (step s).bit x=b := step_dice_child s.here ha.within.1 hM b hx + have HE : (U (s.smaller false)).Ex (fun x => annual x t)=yieldA l := by + apply Ex_congr_mem; intro x hx; unfold t; rw [annual,H false x hx]; rfl + have HF : (U (s.smaller true)).Ex (fun x => annual x t)=yieldA r := by + apply Ex_congr_mem; intro x hx; unfold t; rw [annual,H true x hx]; rfl + change ((U (s.smaller false)).Ex (fun x => annual x t) + (U (s.smaller true)).Ex (fun x => annual x t))/2=(yieldA l+yieldA r)/2 + rw [HE,HF] + have HF : (project t).fee v=stamp s j+((project l).fee v+(project r).fee v)/2 := by + change (ran s s k).fee v=_ + rw [Thread.fee_ran k HG,fair_av] + change (stamp s j+_+(stamp s j+_))/2=_ + unfold k; ring + change yieldA t+_ = _ + rw [HH] + unfold a overdue + change (yieldA l+yieldA r)/2 +(if s.live=true then s.norm*(project t).fee v else 0) = + ((yieldA l+(if s.live=true then s.norm*(project l).fee v else 0))+ + (yieldA r+(if s.live=true then s.norm*(project r).fee v else 0)))/2+ + s.norm*(if s.live then _ else 0) + rw [HF]; split <;> ring + +lemma cost_attempt (s : Scene M) (u : Update j M s.here) + (l : Play j M (s.lose u)) (r : Play j M (s.win u)) : + yieldA (Play.attempt s u l r)+overdue (Play.attempt s u l r) = + yieldA l+overdue l+(yieldA r+overdue r) := by + rw [due_restart (s:=s.win u) rfl] + have h₁ : yieldA (Play.attempt s u l r)=yieldA l+yieldA r := + Ex_add (s.here.dice M) _ _ + have h₂ : overdue (Play.attempt s u l r)=overdue l := by + unfold overdue; rw [project] + cases project l <;> rfl + rw [h₁,h₂]; ring + +lemma cost_overall : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s : Scene M, + ∀ t : Play j M s, t.Follows J f → (∀ x, f x ≤ exp (height j)) → + yieldA t+overdue t ≤ s.mass*netGross t := by + filter_upwards [receipt_paid] with j hj M J f s t h hf + induction t with + | rest s => + rw [netGross,trace,flat_finish,dueEmpty rfl] + simp_rw [yieldA,annual,Ex_const]; simp + | ticket s t ih => + let v := s.reset + have hs := fit_ticket h + obtain ⟨ha,hb,hc,hd,he⟩ := h + have HE := hj M J v (project t) f (projected _ he) hs.1 hs.2.1 hs.2.2 hf + have HA : yieldA (Play.ticket s t)= + v.norm*stats (project t) (souvenir v.live j)+yieldA t := by + unfold yieldA + simp_rw [annual,jackpot]; rw [Ex_add,Ex_lmul] + rfl + rw [HA, netGross, trace, flat_push,dueEmpty rfl,add_zero] + let x := stats (project t) (souvenir v.live j) + change v.norm*x+_ ≤ s.mass*(gateTax s.reset j+netGross t) + let a : ℝ := if v.live then stats (project t) (subtotal (fun b => stamp b.post j)) else 0 + change x ≤ gateTax v j+a at HE + have Hi := ih he + rw [show overdue t = s.mass*a by unfold overdue a v; split <;> first|rfl|simp] at Hi + change yieldA t+s.mass*a ≤ s.mass*_ at Hi + change s.mass*x+_ ≤ s.mass*(gateTax v j+_) + nlinarith [hb.2.2] + | freeze s t ih => + rw [dueEmpty rfl,netGross,trace] + rw [ite_eq_left rfl,flat_push] + have HH := ih h.2.2.2 + rw [due_restart (s:=s.out) rfl] at HH + change yieldA t+0 ≤ s.mass*(0+netGross t) + rw [zero_add]; exact HH + | jump s l r il ir => + let t := Play.jump s l r + rw [cost_jump _ _ _ h] + have H₁ := traced t true h + unfold netGross; rw [trace,flat_fork _ _ H₁] + obtain ⟨ha,hb,hc,hd,he⟩ := h + let g (b : Bool) := if b then netGross r else netGross l + + let v : Bool → ℝ := fun b => match b with + | false => netGross l + | true => netGross r + let w : ∀ b, Road j M (s.smaller b) := fun b => match b with + | false => trace true l + | true => trace true r + let P := (step s).probs + let m := s.mass; let x := s.norm*(if s.live then stamp s j else 0) + have HV (b : Bool) : (w b).flatFee billOf = v b := by cases b <;> rfl + have heq : P.Ex (fun b : Bool => normStamp s j+(w b).flatFee billOf) = normStamp s j+P.Ex v := by + simp_rw [HV,Ex_add _ (f:=fun _ => _),Ex_const] + change _ + x ≤ s.mass*P.Ex (fun b => normStamp s j+(w b).flatFee billOf) + rw [heq,mul_add] + have hM : s.here.lev < M := (Play.first hd).within.2 + have hi := output_split s ha.within.1 hM ha.positive hb.1.2.2 v + have hm := hb.1.2.2 + rw [show s.mass*normStamp s j=x by unfold x normStamp; split; field_simp; simp] + change s.mass*P.Ex v=_ at hi + rw [hi,fair_av] + have HI : (fun b => (s.smaller b).mass*v b) false = (s.smaller false).mass*netGross l := rfl + change _/2+x ≤ x+ ((s.smaller false).mass*netGross l+(s.smaller true).mass*netGross r)/2 + linarith [il hd,ir he] + | attempt s u l r il ir => + let t := Play.attempt s u l r + let v := toss s u; let P := v.probs + have H := traced t true h + rw [cost_attempt,netGross,trace,flat_fork _ _ H] + obtain ⟨ha,hb,hc,hd,he,hx⟩ := h + let w : ∀ b, Road j M (v.dst b) := fun b => match b with + | false => trace true l + | true => trace true r + change _ ≤ s.mass*P.Ex (fun b => 0+(w b).flatFee billOf) + simp only [zero_add,Ex,Fintype.sum_bool] + have Hi := toss_true u ha.within ha.positive hb.1.2.2 + have HH : (s.lose u).mass = P false*s.mass := toss_mass Hi false + have HJ : (s.win u).mass = P true*s.mass := toss_mass Hi true + change _ ≤ s.mass*(P true*netGross r+P false*netGross l) + have Hi₁ := il he; have Hi₂ := ir hx + rw [HH] at Hi₁; rw [HJ] at Hi₂; linarith + +end CastShow + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Phase.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Phase.lean index 0b16c00cf5805819ca73152f2e8068e4295110ed..3da9060e06083b2e00438e60778cc8ecac94ad63 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Phase.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Phase.lean @@ -1,215 +1,215 @@ -import OAI.Analysis.FourierLLogL.Paths.Screen -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot - -/-- channel perturbation estimate. -/ -private lemma ray_channel (A : Form M) (f : Grid M → ℂ) (x : Grid M) : - (channel A x).Cx f = (label A).Cx fun z => f (x+z) := by - apply Complex.ext - · rw [re_Cx,re_Cx,ch_ex] - · have H (P : Law (Grid M)) (f) : (P.Cx f).im=P.Ex (fun i => (f i).im) := by - simp [Cx,Ex,Complex.im_sum] - rw [H,H,ch_ex] - -lemma duet_decode (s : Scene M) (v k : Grid M) : - ∑ z, chi k z*letter s v z = (cred (dye s) k:ℂ)*scalar s (v+k) := by - let A := dye s - let L := channel A - have HE (x : Grid M) : - ∑ z, (L x z:ℂ)*chi k z=chi k x*(cred A k:ℂ) := by - change (channel A x).Cx _=_ - rw [ray_channel,cred_eq] - simp_rw [chi_add,Cx_lmul] - unfold letter fz scalar; unfold omegaObs - simp_rw [Finset.mul_sum] - rw [Finset.sum_comm] - apply Finset.sum_congr rfl; intro i _ - let p : ℂ := ante s i - calc - _ = p*chi v i*∑ z,(L i z:ℂ)*chi k z := by - rw [Finset.mul_sum] - exact Finset.sum_congr rfl fun z _ => by unfold L p A ante; push_cast; ring - _ = _ := by rw [HE i,chi_add']; unfold p ante A; ring - -lemma gain_decode (s : Scene M) (v k : Grid M) : - ∑ z, chi k z*factor s v z = (cred (dye s) k:ℂ)*gain s (v+k) := by - unfold factor - simp_rw [← mul_div_assoc,mul_sub]; rw [←Finset.sum_div,Finset.sum_sub_distrib, - duet_decode,duet_decode,gain] - simp only [dye,smaller]; ring - -lemma energy_near_channel (s : Scene M) (h : Fit s) (v k : Grid M) : - energy s v ≤ 2*energy s (v+k)+2*(share s*delta (dye s) k^2) := by - let A := dye s; let P := channel A; let f := ante s - let W (x y : Grid M) := f x * P x y - let a := letter s v; let b := letter s (v+k) - let m := matter s - let T (x y : Grid M) := chi k x-chi k y - let G (y : Grid M) := ∑ i,W i y* ‖T i y‖^2 - have hw (x y) : 0 ≤ W x y := mul_nonneg (ante_nonneg _ h.ready.2 h.pos.le _) ((P x).nonneg _) - have H (z : Grid M) : - b z-chi k z*a z = ∑ x, (W x z:ℂ)*(T x z*chi v x) := by - unfold a b letter fz; rw [Finset.mul_sum,←Finset.sum_sub_distrib] - exact Finset.sum_congr rfl fun x _ => by - rw [chi_add'] - unfold W T f P A ante omegaObs - ring - have HI (y) : ‖b y-chi k y*a y‖^2 / m y ≤ G y := by - have HH : 0 ≤ m y := matter_nonneg h _ - rcases eq_or_lt_of_le HH with HH|HH - · rw [←HH,div_zero]; exact Finset.sum_nonneg fun _ _ => mul_nonneg (hw _ _) (sq_nonneg _) - apply (div_le_iff₀ HH).mpr - rw [H y] - have hi (x : Grid M) : - ‖(W x y:ℂ)*(T x y*chi v x)‖= W x y* ‖T x y‖ := by - rw [norm_scaleC,norm_mul,abs_of_nonneg (hw _ _),norm_chi,mul_one] - apply le_trans (pow_le_pow_left₀ (norm_nonneg _) (norm_sum_le _ _) _) - simp_rw [hi] - have HX : m y=∑ x,W x y := rfl - rw [HX]; unfold G - apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ - (fun i _ => mul_nonneg (hw _ _) (sq_nonneg _)) (fun i _ => hw _ _) (fun i _ => ?_) - exact le_of_eq (by ring) - have HB (x : Grid M) : ∑ y,P x y* ‖T x y‖^2=delta A k ^ 2 := by - change (channel A x).Ex _=_ - rw [ch_ex] - have he (z) : T x (x+z)=chi k x *(1-chi k z) := by unfold T; rw [chi_add]; ring - simp_rw [he,norm_mul,norm_chi,one_mul] - exact (sq_sqrt ((label A).Ex_nonneg fun i => sq_nonneg _)).symm - have HL : ∑ z,G z=share s*delta A k ^ 2 := by - unfold G; rw [Finset.sum_comm] - simp_rw [W,mul_assoc,← Finset.mul_sum,HB] - rw [← Finset.sum_mul,ante_total _ h.ready.1] - unfold energy energyZ - change ∑ z, ‖a z‖^2/m z ≤ 2*(∑ z, ‖b z‖^2/m z)+2*(share s*_) - rw [show dye s=A from rfl,←HL, Finset.mul_sum, Finset.mul_sum,← Finset.sum_add_distrib] - apply Finset.sum_le_sum; intro z _ - have he : ‖a z‖ ≤ ‖b z‖+‖b z-chi k z*a z‖ := by - have H := norm_sub_le (b z) (b z-chi k z*a z) - rw [sub_sub_cancel,norm_mul,norm_chi,one_mul] at H; exact H - have hev : ‖a z‖^2 ≤ 2*‖b z‖^2+2*‖b z-chi k z*a z‖^2 := by - have H := pow_le_pow_left₀ (norm_nonneg _) he 2 - nlinarith [sq_nonneg (‖b z‖-‖b z-chi k z*a z‖)] - apply le_trans (div_le_div_of_nonneg_right hev (matter_nonneg h _)) - rw [add_div,mul_div_assoc,mul_div_assoc]; linarith [HI z] - -lemma near_size : - ∀ᶠ j : ℕ in atTop, - ∀ i : ℕ, i ≤ mainIx j → sepRad j ≤ side i ∧ 256*sig j ≤ side i^2 := by - filter_upwards [h_large 1000] with j h i hi - let a := Thermal.H j - have H : 1/a^16 ≤ side i := - le_trans (bottom_level j).ge (pow_le_pow_of_le_one (by norm_num) (by norm_num) hi) - have hx : 1000 ≤ a := h - have hH : 1 ≤ a := by linarith - change 1/a^30 ≤ _ ∧ 256*(1/a^40) ≤ _ - constructor - · apply le_trans _ H; gcongr; omega - · apply le_trans (b:=(1/a^16)^2) _ (pow_le_pow_left₀ (by positivity) H 2) - have hv : a ≤ a^8 := by simpa using pow_le_pow_right₀ hH (by omega : 1≤8) - field_simp; nlinarith - -lemma node_compress {p q : Node} (h : p.below q) : - p.len ≤ q.len := pow_le_pow_of_le_one (by norm_num) (by norm_num) h.1 - -lemma halo_limit {p : Scene M} (s : Scene M) (j i : ℕ) (main : Bool) (d : Grid M) - (hh : s.here.below p.here) (h : rad d ≤ 2*spotLimit p j i main) : - s.here.len*rad d ≤ 2*tolerance j i main := by - calc - _ ≤ p.here.len*(2*spotLimit p j i main) := - mul_le_mul (node_compress hh) h (rad_nonneg _) p.here.len_pos.le - _ = _ := by rw [spotLimit,rinv]; field_simp [p.here.len_pos.ne'] - -/-- Combining errors after activation. First main interval may use b=1, then inner always. -/ -lemma channel_inner : - ∀ᶠ j : ℕ in atTop, ∀ (i : ℕ), i ≤ mainIx j → ∀ (M : ℕ) - (p q : Scene M), q.here.below p.here → dye q=dye p → Climb q → - ∀ u v k : Grid M, delta (dye p) k ≤ sepRad j → - rad (u-v-k) ≤ 2*spotLimit p j i true → - (0 < i → ¬ snag ⟨true,i-1⟩ u (⟨q,q,false⟩:Beat M)) → - Run.Streak.innerV (rose ⟨true,i⟩ v) q := by - filter_upwards [near_size] with j hj i hI M p q h1 h2 h u v k hx hk hp - - by_cases hi : i=0 - · subst i - apply Or.inr; intro b - let s := q.smaller b - apply ((energy_small (h.child b) v).2.trans (h.child b).lift).trans - change (4:ℝ) ≤ 64*side 0^2; norm_num [side] - let A := rose ⟨true,i⟩ v - apply or_iff_not_imp_left.mpr - intro HH - have hf := hp (Nat.pos_of_ne_zero hi) - have HV (b : Bool) : energy (q.smaller b) u ≤ (2*side i)^2 := by - have HH' : ¬ hazard q true (i-1) := fun hh => HH ⟨Nat.pos_of_ne_zero hi, hh⟩ - have Ht : energy (q.smaller b) u < side (i-1)^2 := - lt_of_not_ge fun hz => hf ⟨⟨b,hz⟩,HH'⟩ - have hc := side_succ (i-1) - rw [Nat.sub_add_cancel (by omega : 1 ≤ i)] at hc - rw [hc]; nlinarith - intro b - let s := q.smaller b - have hS : below s.here p.here := below_trans (child_below _ _) h1 - let x := side i - have HP := h.child b - have H : energy s (v+k) ≤ energy s u+4*(16*s.here.len*rad (v+k-u)) := - magnitude_near true s (v+k) u HP.ready HP.pos.le 4 HP.lift - rw [show v+k-u= -(u-v-k) by ring,rad_even] at H - have he₁ : s.here.len*rad (u-v-k) ≤ 2*sig j := - halo_limit s j i true _ hS hk - have hd := hj i hI - have he₂ : delta (dye s) k^2 ≤ side i^2 := by - change delta (dye q) k^2 ≤ _ - rw [h2]; exact pow_le_pow_left₀ (sqrt_nonneg _) (hx.trans hd.1) _ - change energy s v ≤ 64*side i^2 - have ht₁ : share s*delta (dye s) k^2 ≤ 4*(side i)^2 := by - apply le_trans (mul_le_mul_of_nonneg_right HP.lift (sq_nonneg _)); nlinarith - have hT : energy s u ≤ (2*side i)^2 := HV b - nlinarith [energy_near_channel s HP v k, hd.2] - -lemma gain_point {s : Scene M} (hp : Climb s) - (u v x : Grid M) (hh : x∈s.here.block M) : - ‖gain s u-chi (u-v) x*gain s v‖ ≤ 32*(s.here.len*rad (u-v)) := by - let a B := scalar (s.smaller B) u- chi (u-v) x * scalar (s.smaller B) v - let k := s.here.len*rad (u-v) - have H (b : Bool) : ‖a b‖ ≤ 32*k := by - have he := hp.child b - apply (scalar_point _ u he.ready he.pos.le (child_below s.here b) x hh v).trans - rw [mul_assoc 8] - nlinarith [show 0 ≤ k from mul_nonneg s.here.len_pos.le (rad_nonneg _),he.lift] - rw [show gain s u-chi (u-v) x*gain s v=(a false-a true)/2 by unfold gain a; ring] - apply (norm_halfcomplex _ _).trans; linarith [H false,H true] - -/-- big unlabelled coefficients force labelled high -/ -lemma scalar_energy (s : Scene M) (h : Fit s) (u) : - ‖scalar s u‖^2 ≤ 4*energy s u := by - rw [scalar_label] - apply le_trans (pow_le_pow_left₀ (norm_nonneg _) (norm_sum_le _ _) _) - apply le_trans (b:=share s*energy s u) - · rw [←matter_sum h,sun_energy h] - apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => matter_nonneg h _) - (fun i _ => (sun_rays h _ _).1) (fun i _ => ?_) - rw [← (mean_hold _ h _ _).1,norm_scaleC,abs_of_nonneg (matter_nonneg h _)] - unfold sun; exact le_of_eq (by ring) - exact mul_le_mul_of_nonneg_right h.lift (energy_small h u).1 - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Screen +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot + +/-- channel perturbation estimate. -/ +private lemma ray_channel (A : Form M) (f : Grid M → ℂ) (x : Grid M) : + (channel A x).Cx f = (label A).Cx fun z => f (x+z) := by + apply Complex.ext + · rw [re_Cx,re_Cx,ch_ex] + · have H (P : Law (Grid M)) (f) : (P.Cx f).im=P.Ex (fun i => (f i).im) := by + simp [Cx,Ex,Complex.im_sum] + rw [H,H,ch_ex] + +lemma duet_decode (s : Scene M) (v k : Grid M) : + ∑ z, chi k z*letter s v z = (cred (dye s) k:ℂ)*scalar s (v+k) := by + let A := dye s + let L := channel A + have HE (x : Grid M) : + ∑ z, (L x z:ℂ)*chi k z=chi k x*(cred A k:ℂ) := by + change (channel A x).Cx _=_ + rw [ray_channel,cred_eq] + simp_rw [chi_add,Cx_lmul] + unfold letter fz scalar; unfold omegaObs + simp_rw [Finset.mul_sum] + rw [Finset.sum_comm] + apply Finset.sum_congr rfl; intro i _ + let p : ℂ := ante s i + calc + _ = p*chi v i*∑ z,(L i z:ℂ)*chi k z := by + rw [Finset.mul_sum] + exact Finset.sum_congr rfl fun z _ => by unfold L p A ante; push_cast; ring + _ = _ := by rw [HE i,chi_add']; unfold p ante A; ring + +lemma gain_decode (s : Scene M) (v k : Grid M) : + ∑ z, chi k z*factor s v z = (cred (dye s) k:ℂ)*gain s (v+k) := by + unfold factor + simp_rw [← mul_div_assoc,mul_sub]; rw [←Finset.sum_div,Finset.sum_sub_distrib, + duet_decode,duet_decode,gain] + simp only [dye,smaller]; ring + +lemma energy_near_channel (s : Scene M) (h : Fit s) (v k : Grid M) : + energy s v ≤ 2*energy s (v+k)+2*(share s*delta (dye s) k^2) := by + let A := dye s; let P := channel A; let f := ante s + let W (x y : Grid M) := f x * P x y + let a := letter s v; let b := letter s (v+k) + let m := matter s + let T (x y : Grid M) := chi k x-chi k y + let G (y : Grid M) := ∑ i,W i y* ‖T i y‖^2 + have hw (x y) : 0 ≤ W x y := mul_nonneg (ante_nonneg _ h.ready.2 h.pos.le _) ((P x).nonneg _) + have H (z : Grid M) : + b z-chi k z*a z = ∑ x, (W x z:ℂ)*(T x z*chi v x) := by + unfold a b letter fz; rw [Finset.mul_sum,←Finset.sum_sub_distrib] + exact Finset.sum_congr rfl fun x _ => by + rw [chi_add'] + unfold W T f P A ante omegaObs + ring + have HI (y) : ‖b y-chi k y*a y‖^2 / m y ≤ G y := by + have HH : 0 ≤ m y := matter_nonneg h _ + rcases eq_or_lt_of_le HH with HH|HH + · rw [←HH,div_zero]; exact Finset.sum_nonneg fun _ _ => mul_nonneg (hw _ _) (sq_nonneg _) + apply (div_le_iff₀ HH).mpr + rw [H y] + have hi (x : Grid M) : + ‖(W x y:ℂ)*(T x y*chi v x)‖= W x y* ‖T x y‖ := by + rw [norm_scaleC,norm_mul,abs_of_nonneg (hw _ _),norm_chi,mul_one] + apply le_trans (pow_le_pow_left₀ (norm_nonneg _) (norm_sum_le _ _) _) + simp_rw [hi] + have HX : m y=∑ x,W x y := rfl + rw [HX]; unfold G + apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ + (fun i _ => mul_nonneg (hw _ _) (sq_nonneg _)) (fun i _ => hw _ _) (fun i _ => ?_) + exact le_of_eq (by ring) + have HB (x : Grid M) : ∑ y,P x y* ‖T x y‖^2=delta A k ^ 2 := by + change (channel A x).Ex _=_ + rw [ch_ex] + have he (z) : T x (x+z)=chi k x *(1-chi k z) := by unfold T; rw [chi_add]; ring + simp_rw [he,norm_mul,norm_chi,one_mul] + exact (sq_sqrt ((label A).Ex_nonneg fun i => sq_nonneg _)).symm + have HL : ∑ z,G z=share s*delta A k ^ 2 := by + unfold G; rw [Finset.sum_comm] + simp_rw [W,mul_assoc,← Finset.mul_sum,HB] + rw [← Finset.sum_mul,ante_total _ h.ready.1] + unfold energy energyZ + change ∑ z, ‖a z‖^2/m z ≤ 2*(∑ z, ‖b z‖^2/m z)+2*(share s*_) + rw [show dye s=A from rfl,←HL, Finset.mul_sum, Finset.mul_sum,← Finset.sum_add_distrib] + apply Finset.sum_le_sum; intro z _ + have he : ‖a z‖ ≤ ‖b z‖+‖b z-chi k z*a z‖ := by + have H := norm_sub_le (b z) (b z-chi k z*a z) + rw [sub_sub_cancel,norm_mul,norm_chi,one_mul] at H; exact H + have hev : ‖a z‖^2 ≤ 2*‖b z‖^2+2*‖b z-chi k z*a z‖^2 := by + have H := pow_le_pow_left₀ (norm_nonneg _) he 2 + nlinarith [sq_nonneg (‖b z‖-‖b z-chi k z*a z‖)] + apply le_trans (div_le_div_of_nonneg_right hev (matter_nonneg h _)) + rw [add_div,mul_div_assoc,mul_div_assoc]; linarith [HI z] + +lemma near_size : + ∀ᶠ j : ℕ in atTop, + ∀ i : ℕ, i ≤ mainIx j → sepRad j ≤ side i ∧ 256*sig j ≤ side i^2 := by + filter_upwards [h_large 1000] with j h i hi + let a := Thermal.H j + have H : 1/a^16 ≤ side i := + le_trans (bottom_level j).ge (pow_le_pow_of_le_one (by norm_num) (by norm_num) hi) + have hx : 1000 ≤ a := h + have hH : 1 ≤ a := by linarith + change 1/a^30 ≤ _ ∧ 256*(1/a^40) ≤ _ + constructor + · apply le_trans _ H; gcongr; omega + · apply le_trans (b:=(1/a^16)^2) _ (pow_le_pow_left₀ (by positivity) H 2) + have hv : a ≤ a^8 := by simpa using pow_le_pow_right₀ hH (by omega : 1≤8) + field_simp; nlinarith + +lemma node_compress {p q : Node} (h : p.below q) : + p.len ≤ q.len := pow_le_pow_of_le_one (by norm_num) (by norm_num) h.1 + +lemma halo_limit {p : Scene M} (s : Scene M) (j i : ℕ) (main : Bool) (d : Grid M) + (hh : s.here.below p.here) (h : rad d ≤ 2*spotLimit p j i main) : + s.here.len*rad d ≤ 2*tolerance j i main := by + calc + _ ≤ p.here.len*(2*spotLimit p j i main) := + mul_le_mul (node_compress hh) h (rad_nonneg _) p.here.len_pos.le + _ = _ := by rw [spotLimit,rinv]; field_simp [p.here.len_pos.ne'] + +/-- Combining errors after activation. First main interval may use b=1, then inner always. -/ +lemma channel_inner : + ∀ᶠ j : ℕ in atTop, ∀ (i : ℕ), i ≤ mainIx j → ∀ (M : ℕ) + (p q : Scene M), q.here.below p.here → dye q=dye p → Climb q → + ∀ u v k : Grid M, delta (dye p) k ≤ sepRad j → + rad (u-v-k) ≤ 2*spotLimit p j i true → + (0 < i → ¬ snag ⟨true,i-1⟩ u (⟨q,q,false⟩:Beat M)) → + Run.Streak.innerV (rose ⟨true,i⟩ v) q := by + filter_upwards [near_size] with j hj i hI M p q h1 h2 h u v k hx hk hp + + by_cases hi : i=0 + · subst i + apply Or.inr; intro b + let s := q.smaller b + apply ((energy_small (h.child b) v).2.trans (h.child b).lift).trans + change (4:ℝ) ≤ 64*side 0^2; norm_num [side] + let A := rose ⟨true,i⟩ v + apply or_iff_not_imp_left.mpr + intro HH + have hf := hp (Nat.pos_of_ne_zero hi) + have HV (b : Bool) : energy (q.smaller b) u ≤ (2*side i)^2 := by + have HH' : ¬ hazard q true (i-1) := fun hh => HH ⟨Nat.pos_of_ne_zero hi, hh⟩ + have Ht : energy (q.smaller b) u < side (i-1)^2 := + lt_of_not_ge fun hz => hf ⟨⟨b,hz⟩,HH'⟩ + have hc := side_succ (i-1) + rw [Nat.sub_add_cancel (by omega : 1 ≤ i)] at hc + rw [hc]; nlinarith + intro b + let s := q.smaller b + have hS : below s.here p.here := below_trans (child_below _ _) h1 + let x := side i + have HP := h.child b + have H : energy s (v+k) ≤ energy s u+4*(16*s.here.len*rad (v+k-u)) := + magnitude_near true s (v+k) u HP.ready HP.pos.le 4 HP.lift + rw [show v+k-u= -(u-v-k) by ring,rad_even] at H + have he₁ : s.here.len*rad (u-v-k) ≤ 2*sig j := + halo_limit s j i true _ hS hk + have hd := hj i hI + have he₂ : delta (dye s) k^2 ≤ side i^2 := by + change delta (dye q) k^2 ≤ _ + rw [h2]; exact pow_le_pow_left₀ (sqrt_nonneg _) (hx.trans hd.1) _ + change energy s v ≤ 64*side i^2 + have ht₁ : share s*delta (dye s) k^2 ≤ 4*(side i)^2 := by + apply le_trans (mul_le_mul_of_nonneg_right HP.lift (sq_nonneg _)); nlinarith + have hT : energy s u ≤ (2*side i)^2 := HV b + nlinarith [energy_near_channel s HP v k, hd.2] + +lemma gain_point {s : Scene M} (hp : Climb s) + (u v x : Grid M) (hh : x∈s.here.block M) : + ‖gain s u-chi (u-v) x*gain s v‖ ≤ 32*(s.here.len*rad (u-v)) := by + let a B := scalar (s.smaller B) u- chi (u-v) x * scalar (s.smaller B) v + let k := s.here.len*rad (u-v) + have H (b : Bool) : ‖a b‖ ≤ 32*k := by + have he := hp.child b + apply (scalar_point _ u he.ready he.pos.le (child_below s.here b) x hh v).trans + rw [mul_assoc 8] + nlinarith [show 0 ≤ k from mul_nonneg s.here.len_pos.le (rad_nonneg _),he.lift] + rw [show gain s u-chi (u-v) x*gain s v=(a false-a true)/2 by unfold gain a; ring] + apply (norm_halfcomplex _ _).trans; linarith [H false,H true] + +/-- big unlabelled coefficients force labelled high -/ +lemma scalar_energy (s : Scene M) (h : Fit s) (u) : + ‖scalar s u‖^2 ≤ 4*energy s u := by + rw [scalar_label] + apply le_trans (pow_le_pow_left₀ (norm_nonneg _) (norm_sum_le _ _) _) + apply le_trans (b:=share s*energy s u) + · rw [←matter_sum h,sun_energy h] + apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => matter_nonneg h _) + (fun i _ => (sun_rays h _ _).1) (fun i _ => ?_) + rw [← (mean_hold _ h _ _).1,norm_scaleC,abs_of_nonneg (matter_nonneg h _)] + unfold sun; exact le_of_eq (by ring) + exact mul_le_mul_of_nonneg_right h.lift (energy_small h u).1 + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Rail.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Rail.lean index d69772fbed2bcfda891e89d9e7e3709f381c797f..18d6275aeff96587528ec05d3ba8d5c45b52f131 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Rail.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Rail.lean @@ -1,190 +1,190 @@ -import OAI.Analysis.FourierLLogL.Assembly.Phase -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe - -lemma blend (l : Path M) {E : Type*} [AddCommMonoid E] (f g : Beat M → E) : - subtotal (fun b => f b+g b) l = subtotal f l + subtotal g l := by - induction l with - | nil => change 0=0+(0:E); simp - | cons b l ih => simp only [subtotal_cons,ih]; ac_rfl -lemma subtC (l : Path M) (f : Beat M → ℂ) (c : ℂ) : - subtotal (fun b => c*f b) l=c*subtotal f l := by - induction l with - | nil => change 0=c*0; simp - | cons b l ih => rw [subtotal_cons,subtotal_cons,ih,mul_add] -lemma subtR (l : Path M) (f : Beat M → ℝ) (c : ℝ) : - subtotal (fun b => c*f b) l=c*subtotal f l := by - induction l with - | nil => change 0=c*0; simp - | cons b l ih => rw [subtotal_cons,subtotal_cons,ih,mul_add] -lemma subt_constant (l : Path M) (c : ℝ) : - subtotal (fun _ => c) l = c*l.length := by - induction l with - | nil => simp [subtotal] - | cons b l ih => rw [subtotal_cons,List.length_cons,ih]; push_cast; ring -lemma subt_neg (l : Path M) (f g : Beat M → ℂ) : - subtotal f l-subtotal g l=subtotal (fun b => f b-g b) l := by - induction l with - | nil => simp [subtotal] - | cons b l ih => rw [subtotal_cons,subtotal_cons,subtotal_cons,← ih]; abel -lemma subt_norm (l : Path M) (f : Beat M → ℂ) : - ‖subtotal f l‖ ≤ subtotal (fun b => ‖f b‖) l := by - induction l with - | nil => change ‖(0:ℂ)‖ ≤ 0; simp - | cons b l ih => exact (norm_add_le (f b) _).trans (add_le_add_right ih _) - -lemma trip_attend {x : Grid M} {l : Path M} (h : Trip x s l) : - ∀ b∈l, Ideal b.pre b.post ∧ x∈b.post.here.block M ∧ |blow x b|=1 ∧ - b.post.here.below s.here ∧ dye b.post=dye s := by - induction l generalizing s with - | nil => simp - | cons b l ih => - intro z hz - rcases List.mem_cons.mp hz with rfl|hz - · refine ⟨h.2.1,?_,trip_final h,?_,?_⟩ - · exact block_of_below (child_below _ _) _ (trip_head h.2.2).2 - · rw [h.2.1.lower.here,h.1]; exact below_refl _ - rw [dye,h.2.1.lower.fund,h.1]; rfl - obtain ⟨hp,hj,hi,h₁,h₂⟩ := ih h.2.2 z hz - refine ⟨hp,hj,hi,?_,?_⟩ - · rw [← h.1,← h.2.1.lower.here]; exact below_trans h₁ (child_below _ _) - change dye z.post=dye b.post at h₂; rw [h₂,dye,h.2.1.lower.fund,h.1]; rfl - -def spaceRent (b : Beat M) := b.post.here.len -lemma length_fee {x : Grid M} {l : Path M} (h : Trip x s l) : - subtotal spaceRent l ≤ 2*s.here.len := by - induction l generalizing s with - | nil => change 0 ≤ 2*_; exact mul_nonneg (by norm_num) s.here.len_pos.le - | cons b l ih => - have H := ih h.2.2 - rw [show (winds b).here.len=b.post.here.len/2 from child_len _ b.bit] at H - rw [←h.1,←h.2.1.lower.here] - change b.post.here.len+subtotal spaceRent l ≤ _ - linarith - -def aired (m : Beat M → Prop) (x u : Grid M) (b : Beat M) := - if m b then proceeds x u b else 0 - -lemma rail_fee {x : Grid M} (u v : Grid M) (μ : Beat M → Prop) (p l r : Path M) - (h : Trip x s (p++(l++r))) : - ‖subtotal (aired μ x u) l-chi (u-v) x*subtotal (aired μ x v) l‖ ≤ - 64*s.here.len*rad (u-v) := by - rw [← subtC,subt_neg] - let F (b : Beat M) := aired μ x u b-chi (u-v) x * aired μ x v b - apply (subt_norm l F).trans - have ht := trip_cut h - obtain ⟨hs,w,hk⟩ := ht - obtain ⟨hL,t,hT⟩ := trip_cut hk - have HH : subtotal (fun b => ‖F b‖) l ≤ subtotal (fun b => 32*rad (u-v)*spaceRent b) l := by - suffices HX : ∀ l s, Trip x s l → subtotal (fun b => ‖F b‖) l ≤ - subtotal (fun b => 32*rad (u-v)*spaceRent b) l from HX _ w hL - intro l; induction l with - | nil => intro s h; exact le_rfl - | cons b l ih => - intro s H - rw [subtotal_cons,subtotal_cons] - apply add_le_add _ (ih (winds b) H.2.2) - unfold F aired; split_ifs - · rw [proceeds,proceeds,mul_left_comm,← mul_sub,norm_scaleC,trip_final H,one_mul] - have HC := (trip_attend H b List.mem_cons_self) - exact (gain_point H.2.1.climb u v x HC.2.1).trans (le_of_eq (by unfold spaceRent; ring)) - · simp; exact mul_nonneg (mul_nonneg (by norm_num) (rad_nonneg _)) b.post.here.len_pos.le - apply HH.trans - rw [subtR] - have hp' : subtotal spaceRent l ≤ 2*s.here.len := by - have HH := length_fee h - simp_rw [subtotal_app] at HH - linarith [trip_nonneg hs (f:=spaceRent) (fun b _ => b.post.here.len_pos.le), - trip_nonneg hT (f:=spaceRent) (fun b _ => b.post.here.len_pos.le)] - nlinarith [rad_nonneg (u-v)] - -lemma straight_fee {x : Grid M} (u : Grid M) (l : Path M) - (h : Trip x s l) : - ‖subtotal (proceeds x u) l‖ ≤ 4*l.length := by - induction l generalizing s with - | nil => change ‖(0:ℂ)‖ ≤ 4*(↑([]:Path M).length:ℝ); simp - | cons b l ih => - rw [subtotal_cons,List.length_cons]; push_cast - apply (norm_add_le _ _).trans - have he := h.2.1.climb - rw [proceeds,norm_scaleC,trip_final h,one_mul] - linarith [(gain_norm he u).trans he.fit.lift,ih h.2.2] - -lemma decode_track (l : Path M) (x v k : Grid M) {s : Scene M} - (h : Trip x s l) : - let w := subtotal (duties x v) l - ∑ z,chi k z*w z=(cred (dye s) k:ℂ)*subtotal (proceeds x (v+k)) l := by - induction l generalizing s with - | nil => simp [subtotal] - | cons b l ih => - have H := gain_decode b.post v k - let C : ℂ := cred (dye b.post) k - have HH := ih h.2.2 - intro w - have hr : dye b.post=dye s := (trip_attend h b List.mem_cons_self).2.2.2.2 - rw [←hr] - let t := duties x v b; let u := subtotal (duties x v) l - have hw (z) : w z=t z+u z := rfl - simp_rw [hw,mul_add,Finset.sum_add_distrib] - rw [subtotal_cons,mul_add] - congr 1 - · unfold t duties; simp_rw [mul_left_comm (chi k _),←Finset.mul_sum] - rw [H]; unfold proceeds; ring - -lemma below_decode (u v k x : Grid M) (A : Form M) (p l r : Path M) - (h : Trip x s (p++(l++r))) (hx : A=dye s) - (hh : delta A k ≤ 1) : - ‖subtotal (proceeds x u) l‖ ≤ - 2*Beam.yieldW (subtotal (duties x v) l)+64*s.here.len*rad (u-v-k) := by - obtain ⟨_,q,h₁⟩ := trip_cut h - obtain ⟨hp,d,h₂⟩ := trip_cut h₁ - let a := v+k - let f := subtotal (proceeds x a) l; let W := subtotal (duties x v) l - have HI : ‖f‖ ≤ 2*Beam.yieldW W := by - by_cases ht : l=[] - · unfold f W; rw [ht,subtotal_nil,subtotal_nil,norm_zero,Beam.yield_zero]; norm_num - have hz := decode_track l x v k hp - have HV : dye q=A := by - obtain ⟨b,hb⟩ := List.exists_mem_of_ne_nil _ ht - have hT : b∈p++(l++r) := by simp [hb] - rw [← (trip_attend hp b hb).2.2.2.2, (trip_attend h b hT).2.2.2.2,hx] - change ∑ z,chi k z*W z=(cred (dye q) k:ℂ)*f at hz - rw [HV] at hz - have hC : 1/2 ≤ cred A k := by - nlinarith [delta_sq A k,show 0 ≤ delta A k from sqrt_nonneg _] - have H : ‖(↑(cred A k):ℂ)*f‖ ≤ Beam.yieldW W := by - rw [← hz] - exact (norm_sum_le _ _).trans (by simp only [norm_mul,norm_chi,one_mul]; exact le_rfl) - rw [norm_scaleC,abs_of_nonneg (by linarith)] at H - nlinarith [norm_nonneg f] - have he := rail_fee u a (fun _ => True) p l r h - have hA (v) : aired (fun _ => True) x v=proceeds x v := by funext b; simp [aired] - rw [hA,hA] at he - let f' := subtotal (proceeds x u) l - have HH : ‖f'‖ ≤ ‖f'-chi (u-a) x*f‖ + ‖f‖ := by - have H := norm_add_le (f'-chi (u-a) x*f) (chi (u-a) x*f) - rwa [sub_add_cancel,norm_mul,norm_chi,one_mul] at H - rw [show u-v-k=u-a by unfold a; ring] - change ‖f'-chi (u-a) x*f‖ ≤ _ at he - change ‖f'‖ ≤ 2*Beam.yieldW W+_ - linarith - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Phase +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe + +lemma blend (l : Path M) {E : Type*} [AddCommMonoid E] (f g : Beat M → E) : + subtotal (fun b => f b+g b) l = subtotal f l + subtotal g l := by + induction l with + | nil => change 0=0+(0:E); simp + | cons b l ih => simp only [subtotal_cons,ih]; ac_rfl +lemma subtC (l : Path M) (f : Beat M → ℂ) (c : ℂ) : + subtotal (fun b => c*f b) l=c*subtotal f l := by + induction l with + | nil => change 0=c*0; simp + | cons b l ih => rw [subtotal_cons,subtotal_cons,ih,mul_add] +lemma subtR (l : Path M) (f : Beat M → ℝ) (c : ℝ) : + subtotal (fun b => c*f b) l=c*subtotal f l := by + induction l with + | nil => change 0=c*0; simp + | cons b l ih => rw [subtotal_cons,subtotal_cons,ih,mul_add] +lemma subt_constant (l : Path M) (c : ℝ) : + subtotal (fun _ => c) l = c*l.length := by + induction l with + | nil => simp [subtotal] + | cons b l ih => rw [subtotal_cons,List.length_cons,ih]; push_cast; ring +lemma subt_neg (l : Path M) (f g : Beat M → ℂ) : + subtotal f l-subtotal g l=subtotal (fun b => f b-g b) l := by + induction l with + | nil => simp [subtotal] + | cons b l ih => rw [subtotal_cons,subtotal_cons,subtotal_cons,← ih]; abel +lemma subt_norm (l : Path M) (f : Beat M → ℂ) : + ‖subtotal f l‖ ≤ subtotal (fun b => ‖f b‖) l := by + induction l with + | nil => change ‖(0:ℂ)‖ ≤ 0; simp + | cons b l ih => exact (norm_add_le (f b) _).trans (add_le_add_right ih _) + +lemma trip_attend {x : Grid M} {l : Path M} (h : Trip x s l) : + ∀ b∈l, Ideal b.pre b.post ∧ x∈b.post.here.block M ∧ |blow x b|=1 ∧ + b.post.here.below s.here ∧ dye b.post=dye s := by + induction l generalizing s with + | nil => simp + | cons b l ih => + intro z hz + rcases List.mem_cons.mp hz with rfl|hz + · refine ⟨h.2.1,?_,trip_final h,?_,?_⟩ + · exact block_of_below (child_below _ _) _ (trip_head h.2.2).2 + · rw [h.2.1.lower.here,h.1]; exact below_refl _ + rw [dye,h.2.1.lower.fund,h.1]; rfl + obtain ⟨hp,hj,hi,h₁,h₂⟩ := ih h.2.2 z hz + refine ⟨hp,hj,hi,?_,?_⟩ + · rw [← h.1,← h.2.1.lower.here]; exact below_trans h₁ (child_below _ _) + change dye z.post=dye b.post at h₂; rw [h₂,dye,h.2.1.lower.fund,h.1]; rfl + +def spaceRent (b : Beat M) := b.post.here.len +lemma length_fee {x : Grid M} {l : Path M} (h : Trip x s l) : + subtotal spaceRent l ≤ 2*s.here.len := by + induction l generalizing s with + | nil => change 0 ≤ 2*_; exact mul_nonneg (by norm_num) s.here.len_pos.le + | cons b l ih => + have H := ih h.2.2 + rw [show (winds b).here.len=b.post.here.len/2 from child_len _ b.bit] at H + rw [←h.1,←h.2.1.lower.here] + change b.post.here.len+subtotal spaceRent l ≤ _ + linarith + +def aired (m : Beat M → Prop) (x u : Grid M) (b : Beat M) := + if m b then proceeds x u b else 0 + +lemma rail_fee {x : Grid M} (u v : Grid M) (μ : Beat M → Prop) (p l r : Path M) + (h : Trip x s (p++(l++r))) : + ‖subtotal (aired μ x u) l-chi (u-v) x*subtotal (aired μ x v) l‖ ≤ + 64*s.here.len*rad (u-v) := by + rw [← subtC,subt_neg] + let F (b : Beat M) := aired μ x u b-chi (u-v) x * aired μ x v b + apply (subt_norm l F).trans + have ht := trip_cut h + obtain ⟨hs,w,hk⟩ := ht + obtain ⟨hL,t,hT⟩ := trip_cut hk + have HH : subtotal (fun b => ‖F b‖) l ≤ subtotal (fun b => 32*rad (u-v)*spaceRent b) l := by + suffices HX : ∀ l s, Trip x s l → subtotal (fun b => ‖F b‖) l ≤ + subtotal (fun b => 32*rad (u-v)*spaceRent b) l from HX _ w hL + intro l; induction l with + | nil => intro s h; exact le_rfl + | cons b l ih => + intro s H + rw [subtotal_cons,subtotal_cons] + apply add_le_add _ (ih (winds b) H.2.2) + unfold F aired; split_ifs + · rw [proceeds,proceeds,mul_left_comm,← mul_sub,norm_scaleC,trip_final H,one_mul] + have HC := (trip_attend H b List.mem_cons_self) + exact (gain_point H.2.1.climb u v x HC.2.1).trans (le_of_eq (by unfold spaceRent; ring)) + · simp; exact mul_nonneg (mul_nonneg (by norm_num) (rad_nonneg _)) b.post.here.len_pos.le + apply HH.trans + rw [subtR] + have hp' : subtotal spaceRent l ≤ 2*s.here.len := by + have HH := length_fee h + simp_rw [subtotal_app] at HH + linarith [trip_nonneg hs (f:=spaceRent) (fun b _ => b.post.here.len_pos.le), + trip_nonneg hT (f:=spaceRent) (fun b _ => b.post.here.len_pos.le)] + nlinarith [rad_nonneg (u-v)] + +lemma straight_fee {x : Grid M} (u : Grid M) (l : Path M) + (h : Trip x s l) : + ‖subtotal (proceeds x u) l‖ ≤ 4*l.length := by + induction l generalizing s with + | nil => change ‖(0:ℂ)‖ ≤ 4*(↑([]:Path M).length:ℝ); simp + | cons b l ih => + rw [subtotal_cons,List.length_cons]; push_cast + apply (norm_add_le _ _).trans + have he := h.2.1.climb + rw [proceeds,norm_scaleC,trip_final h,one_mul] + linarith [(gain_norm he u).trans he.fit.lift,ih h.2.2] + +lemma decode_track (l : Path M) (x v k : Grid M) {s : Scene M} + (h : Trip x s l) : + let w := subtotal (duties x v) l + ∑ z,chi k z*w z=(cred (dye s) k:ℂ)*subtotal (proceeds x (v+k)) l := by + induction l generalizing s with + | nil => simp [subtotal] + | cons b l ih => + have H := gain_decode b.post v k + let C : ℂ := cred (dye b.post) k + have HH := ih h.2.2 + intro w + have hr : dye b.post=dye s := (trip_attend h b List.mem_cons_self).2.2.2.2 + rw [←hr] + let t := duties x v b; let u := subtotal (duties x v) l + have hw (z) : w z=t z+u z := rfl + simp_rw [hw,mul_add,Finset.sum_add_distrib] + rw [subtotal_cons,mul_add] + congr 1 + · unfold t duties; simp_rw [mul_left_comm (chi k _),←Finset.mul_sum] + rw [H]; unfold proceeds; ring + +lemma below_decode (u v k x : Grid M) (A : Form M) (p l r : Path M) + (h : Trip x s (p++(l++r))) (hx : A=dye s) + (hh : delta A k ≤ 1) : + ‖subtotal (proceeds x u) l‖ ≤ + 2*Beam.yieldW (subtotal (duties x v) l)+64*s.here.len*rad (u-v-k) := by + obtain ⟨_,q,h₁⟩ := trip_cut h + obtain ⟨hp,d,h₂⟩ := trip_cut h₁ + let a := v+k + let f := subtotal (proceeds x a) l; let W := subtotal (duties x v) l + have HI : ‖f‖ ≤ 2*Beam.yieldW W := by + by_cases ht : l=[] + · unfold f W; rw [ht,subtotal_nil,subtotal_nil,norm_zero,Beam.yield_zero]; norm_num + have hz := decode_track l x v k hp + have HV : dye q=A := by + obtain ⟨b,hb⟩ := List.exists_mem_of_ne_nil _ ht + have hT : b∈p++(l++r) := by simp [hb] + rw [← (trip_attend hp b hb).2.2.2.2, (trip_attend h b hT).2.2.2.2,hx] + change ∑ z,chi k z*W z=(cred (dye q) k:ℂ)*f at hz + rw [HV] at hz + have hC : 1/2 ≤ cred A k := by + nlinarith [delta_sq A k,show 0 ≤ delta A k from sqrt_nonneg _] + have H : ‖(↑(cred A k):ℂ)*f‖ ≤ Beam.yieldW W := by + rw [← hz] + exact (norm_sum_le _ _).trans (by simp only [norm_mul,norm_chi,one_mul]; exact le_rfl) + rw [norm_scaleC,abs_of_nonneg (by linarith)] at H + nlinarith [norm_nonneg f] + have he := rail_fee u a (fun _ => True) p l r h + have hA (v) : aired (fun _ => True) x v=proceeds x v := by funext b; simp [aired] + rw [hA,hA] at he + let f' := subtotal (proceeds x u) l + have HH : ‖f'‖ ≤ ‖f'-chi (u-a) x*f‖ + ‖f‖ := by + have H := norm_add_le (f'-chi (u-a) x*f) (chi (u-a) x*f) + rwa [sub_add_cancel,norm_mul,norm_chi,one_mul] at H + rw [show u-v-k=u-a by unfold a; ring] + change ‖f'-chi (u-a) x*f‖ ≤ _ at he + change ‖f'‖ ≤ 2*Beam.yieldW W+_ + linarith + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Receipt.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Receipt.lean index 747a8ddd53baac073d66470d5713a27e6b4ef2d4..febcfb43bc27ca7555ccf7439bb2663ba83bfe6e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Receipt.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Receipt.lean @@ -1,182 +1,182 @@ -import OAI.Analysis.FourierLLogL.Assembly.Series -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Run Streak - -def parody (s : Scene M) (i : ℕ) := (rose (tagM i) (0:Grid M)).flag s -def stampAux (s : Scene M) (j : ℕ) := - 4*parody s (mainIx j+1)+10^20*(fax s+ - (∑ i∈Finset.Ico 0 (mainIx j+1), parody s i / depthRate i)) - -def stamp (s : Scene M) (j : ℕ) := 512*mutedR s j true + stampAux s j - -lemma stamp_nonneg {s : Scene M} (h : Climb s) (j : ℕ) : 0 ≤ stamp s j := by - unfold stamp stampAux - have H := surcharge_nonneg s false (priciest s j true) - have HH (i : ℕ) : 0 ≤ parody s i := flag_nonneg _ _ - have HP : 0 ≤ ∑ i∈Finset.Ico 0 (mainIx j+1), parody s i / depthRate i := - Finset.sum_nonneg (fun _ _ => div_nonneg (HH _) (rate_easy _).1.le) - unfold mutedR - linarith [(fax_floor h).1,HH (mainIx j+1)] - -private lemma chamber_fee (c j) (L : Path M) : - tokenCost j c L + crumb (lowI j c) (highI j M c) j L = - rooms c j L+512*subtotal (fun b => mutedR b.post j c) L+ - (if c then 4*(subtotal (special j) L+(billWait (tagM (mainIx j)) j L:ℝ)) else 0) := by - unfold tokenCost crumb rooms miniStage playsL - rw [Finset.sum_add_distrib,← Finset.mul_sum] - ring - -lemma receipt_main (j : ℕ) (L : Path M) : - souvenir true j L=rooms true j L+palaces j L + subtotal (fun b => stamp b.post j) L := by - simp only [souvenir,chamber_fee] - simp only [↓reduceIte] - unfold palaces stamp stampAux bigCrumb - simp_rw [blend _ (f:=fun _ => _) (g:=fun _ => _),subtR] - let m := Finset.Ico 0 (mainIx j+1) - let F (b : Beat M) (i : ℕ) := parody b.post i / depthRate i - have H (i : ℕ) : mainSticker i j L = nakedStage i j L + subtotal (fun b => F b i) L := by - unfold F mainSticker nakedStage mainRatio parody flagged - simp_rw [div_eq_mul_inv,mul_comm _ (depthRate i)⁻¹,subtR] - ring - simp_rw [H,Finset.sum_add_distrib] - have HH := cross_subtotal L m F - unfold F m at HH - rw [blend L (fun b => fax b.post),HH]; unfold F - rw [show subtotal (fun b => fax b.post) L=fullFax L from rfl] - - have HV : special j = fun b : Beat M => parody b.post (mainIx j+1) := rfl - rw [HV] - - linarith -lemma receipt_static (j : ℕ) (L : Path M) : - souvenir false j L=rooms false j L+512*subtotal (fun b => mutedR b.post j false) L := by - simp only [souvenir,chamber_fee]; simp - -def gateTax (s : Scene M) (j : ℕ) := - if s.live then 10^140*height j/Thermal.H j else 10^140*amount s j - -lemma tax_pos (s : Scene M) (j : ℕ) : 0 ≤ gateTax s j := by - have := d_pos j; have := H_pos j; have HA := (amount_limits s j).1 - unfold gateTax; split <;> positivity - -lemma receipt_paid : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ t : Thread M s, ∀ f : Grid M → ℝ, - t.Following j J f → Fit s → entry s → - (s.live=true → exp (-height j) ≤ s.norm) → - (∀ x, f x ≤ exp (height j)) → - stats t (souvenir s.live j) ≤ gateTax s j + - (if s.live then stats t (subtotal (fun b => stamp b.post j)) else 0) := by - filter_upwards [rooms_fee,palaces_fee,segment_cost,hpoly 1 1] with j hr hp hv hT - M J s t f h hh hx he hf - let d := height j - let k := amount s j; let v := Thermal.H j - have hV : 1 ≤ v := H_ge _ - have hk : 1 ≤ k := (amount_limits _ _).1 - have hd : 1 ≤ d := one_le_pow₀ (by norm_num) - have HG := stats_plus t - have HS := hr M J s f t h hh hx hf - unfold gateTax - split - · rename_i hE - have Ht := HS true - have HH := hp M J s f t h hh hx hE (he hE) hf - have hK : k ≤ 4*d := by - have H := log_le_log (exp_pos _) (he hE) - rw [log_exp] at H - change d+max _ 0+2 ≤ _ - have he' : max (-log s.norm) 0 ≤ d := max_le (by unfold d; linarith) (by linarith) - linarith - rw [hE] - rw [show souvenir (M:=M) true j= - fun L => rooms true j L+palaces j L+subtotal (fun b => stamp b.post j) L from funext (receipt_main _)] - rw [HG (f:=fun L => _),HG (f:=fun L => _)] - have heq : lowI j true=mainIx j := rfl - rw [heq,depth_at_main] at Ht - have hi : 0 < v := H_pos j - have HE : 1/v^8 ≤ 1/v := one_div_le_one_div_of_le hi - (by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 8)) - have HT : 1+10^30*(1/v^8)*k ≤ 10^31*(d/v) := by - have hΦ : 1 ≤ d/v := (one_le_div hi).mpr (by simpa using hT) - have hh := mul_le_mul HE hK (by positivity) (by positivity) - rw [show 1/v*(4*d)=4*(d/v) by ring] at hh; nlinarith - change stats t _ ≤ 1+10^30*(1/v^8)*k at Ht - unfold v d at * - nlinarith [show 0 ≤ height j/Thermal.H j by have := H_pos j; have := d_pos j; positivity, - show 10^140*height j/Thermal.H j=10^140*(height j/Thermal.H j) by ring, - show 10^120*height j/Thermal.H j=10^120*(height j/Thermal.H j) by ring] - · rename_i hE - have Ht := HS false - rw [show lowI j false=0 from rfl,show depthRate 0=1 by simp [depthRate,side]] at Ht - have HM := hv M J s f t h hf (show 0 < s.mass from hx ▸ hh.pos) hh.pos (fun q => priciest q j false) false - rw [show s.mass/s.norm=1 from entry_share hh hx, one_mul] at HM - rw [show s.live=false from Bool.eq_false_iff.mpr hE] - simp only [add_zero] - rw [show souvenir (M:=M) false j = - fun L => rooms false j L+512* subtotal (fun b => mutedR b.post j false) L from funext (receipt_static _)] - change stats t _ ≤ _*k - rw [HG] - have HE : stats t (fun L => 512*subtotal (fun b => mutedR b.post j false) L) = - 512* t.fee (fun b => mutedR b.post j false) := - Ex_lmul (s.here.dice M) _ 512 - rw [HE] - change _≤ 1+_*_*k at Ht - have hp : height j ≤ k := (amount_limits _ _).2 - have hh0 : -log s.mass ≤ k := by unfold entry at hx; rw [← hx]; unfold k amount; linarith [le_max_left (-log s.norm) 0,d_pos j] - unfold mutedR; linarith - -/-- pointwise price comparison for expected flags. -/ -lemma parody_fee (s : Scene M) (j : ℕ) (i : ℕ) (h : i ≤ mainIx j+1) : - parody s i ≤ 10^6*Thermal.H j^32*surcharge s true (i-1) := by - have hE : 0 < Thermal.H j := H_pos _ - by_cases he : (rose (tagM i) (0:Grid M)).rogue s - · have hx := he.1 - have hp : 0 < i := hx - have HH := he.2 - have HC := (bottom_level j) - have HT : side (mainIx j) ≤ side (i-1) := - side_ant _ _ (by omega) - rw [HC] at HT - change hazard s true (i-1) at HH - rw [show parody s i=1 from ite_eq_left he,surcharge,ite_eq_left HH] - change side (i-1)^2/128 ≤ _ at HH - let v := Thermal.H j - have Hb : 1/v^32/128 ≤ edgeW s (i-1) := by - have Hi : 0 ≤ 1/(Thermal.H j)^16 := by positivity - apply le_trans _ HH - have HT := div_le_div_of_nonneg_right (pow_le_pow_left₀ Hi HT 2) (by norm_num : (0:ℝ)≤128) - rw [show (1/Thermal.H j^16)^2=1/v^32 by unfold v; ring] at HT; exact HT - have hx' : 0 < v^32 := pow_pos hE _ - change 1 ≤ 10^6*v^32*_ - nlinarith [mul_le_mul_of_nonneg_left Hb hx'.le, - show v^32*(1/v^32/128)=1/128 by have Hx : 0 < v := hE; field_simp] - · rw [show parody s i=0 from ite_eq_right he] - exact mul_nonneg (by positivity) (surcharge_nonneg _ _ _) - -lemma depth_rate_fee (i j : ℕ) (h : i ≤ mainIx j) : - 1/depthRate i ≤ Thermal.H j^8 := by - have H : depthRate (mainIx j) ≤ depthRate i := sqrt_le_sqrt (side_ant i _ h) - rw [depth_at_main] at H - apply (div_le_iff₀ (rate_easy _).1).mpr - have HE := (div_le_iff₀ (show 0 < Thermal.H j^8 by have := H_pos j; positivity)).mp H - linarith - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Series +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Run Streak + +def parody (s : Scene M) (i : ℕ) := (rose (tagM i) (0:Grid M)).flag s +def stampAux (s : Scene M) (j : ℕ) := + 4*parody s (mainIx j+1)+10^20*(fax s+ + (∑ i∈Finset.Ico 0 (mainIx j+1), parody s i / depthRate i)) + +def stamp (s : Scene M) (j : ℕ) := 512*mutedR s j true + stampAux s j + +lemma stamp_nonneg {s : Scene M} (h : Climb s) (j : ℕ) : 0 ≤ stamp s j := by + unfold stamp stampAux + have H := surcharge_nonneg s false (priciest s j true) + have HH (i : ℕ) : 0 ≤ parody s i := flag_nonneg _ _ + have HP : 0 ≤ ∑ i∈Finset.Ico 0 (mainIx j+1), parody s i / depthRate i := + Finset.sum_nonneg (fun _ _ => div_nonneg (HH _) (rate_easy _).1.le) + unfold mutedR + linarith [(fax_floor h).1,HH (mainIx j+1)] + +private lemma chamber_fee (c j) (L : Path M) : + tokenCost j c L + crumb (lowI j c) (highI j M c) j L = + rooms c j L+512*subtotal (fun b => mutedR b.post j c) L+ + (if c then 4*(subtotal (special j) L+(billWait (tagM (mainIx j)) j L:ℝ)) else 0) := by + unfold tokenCost crumb rooms miniStage playsL + rw [Finset.sum_add_distrib,← Finset.mul_sum] + ring + +lemma receipt_main (j : ℕ) (L : Path M) : + souvenir true j L=rooms true j L+palaces j L + subtotal (fun b => stamp b.post j) L := by + simp only [souvenir,chamber_fee] + simp only [↓reduceIte] + unfold palaces stamp stampAux bigCrumb + simp_rw [blend _ (f:=fun _ => _) (g:=fun _ => _),subtR] + let m := Finset.Ico 0 (mainIx j+1) + let F (b : Beat M) (i : ℕ) := parody b.post i / depthRate i + have H (i : ℕ) : mainSticker i j L = nakedStage i j L + subtotal (fun b => F b i) L := by + unfold F mainSticker nakedStage mainRatio parody flagged + simp_rw [div_eq_mul_inv,mul_comm _ (depthRate i)⁻¹,subtR] + ring + simp_rw [H,Finset.sum_add_distrib] + have HH := cross_subtotal L m F + unfold F m at HH + rw [blend L (fun b => fax b.post),HH]; unfold F + rw [show subtotal (fun b => fax b.post) L=fullFax L from rfl] + + have HV : special j = fun b : Beat M => parody b.post (mainIx j+1) := rfl + rw [HV] + + linarith +lemma receipt_static (j : ℕ) (L : Path M) : + souvenir false j L=rooms false j L+512*subtotal (fun b => mutedR b.post j false) L := by + simp only [souvenir,chamber_fee]; simp + +def gateTax (s : Scene M) (j : ℕ) := + if s.live then 10^140*height j/Thermal.H j else 10^140*amount s j + +lemma tax_pos (s : Scene M) (j : ℕ) : 0 ≤ gateTax s j := by + have := d_pos j; have := H_pos j; have HA := (amount_limits s j).1 + unfold gateTax; split <;> positivity + +lemma receipt_paid : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ t : Thread M s, ∀ f : Grid M → ℝ, + t.Following j J f → Fit s → entry s → + (s.live=true → exp (-height j) ≤ s.norm) → + (∀ x, f x ≤ exp (height j)) → + stats t (souvenir s.live j) ≤ gateTax s j + + (if s.live then stats t (subtotal (fun b => stamp b.post j)) else 0) := by + filter_upwards [rooms_fee,palaces_fee,segment_cost,hpoly 1 1] with j hr hp hv hT + M J s t f h hh hx he hf + let d := height j + let k := amount s j; let v := Thermal.H j + have hV : 1 ≤ v := H_ge _ + have hk : 1 ≤ k := (amount_limits _ _).1 + have hd : 1 ≤ d := one_le_pow₀ (by norm_num) + have HG := stats_plus t + have HS := hr M J s f t h hh hx hf + unfold gateTax + split + · rename_i hE + have Ht := HS true + have HH := hp M J s f t h hh hx hE (he hE) hf + have hK : k ≤ 4*d := by + have H := log_le_log (exp_pos _) (he hE) + rw [log_exp] at H + change d+max _ 0+2 ≤ _ + have he' : max (-log s.norm) 0 ≤ d := max_le (by unfold d; linarith) (by linarith) + linarith + rw [hE] + rw [show souvenir (M:=M) true j= + fun L => rooms true j L+palaces j L+subtotal (fun b => stamp b.post j) L from funext (receipt_main _)] + rw [HG (f:=fun L => _),HG (f:=fun L => _)] + have heq : lowI j true=mainIx j := rfl + rw [heq,depth_at_main] at Ht + have hi : 0 < v := H_pos j + have HE : 1/v^8 ≤ 1/v := one_div_le_one_div_of_le hi + (by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 8)) + have HT : 1+10^30*(1/v^8)*k ≤ 10^31*(d/v) := by + have hΦ : 1 ≤ d/v := (one_le_div hi).mpr (by simpa using hT) + have hh := mul_le_mul HE hK (by positivity) (by positivity) + rw [show 1/v*(4*d)=4*(d/v) by ring] at hh; nlinarith + change stats t _ ≤ 1+10^30*(1/v^8)*k at Ht + unfold v d at * + nlinarith [show 0 ≤ height j/Thermal.H j by have := H_pos j; have := d_pos j; positivity, + show 10^140*height j/Thermal.H j=10^140*(height j/Thermal.H j) by ring, + show 10^120*height j/Thermal.H j=10^120*(height j/Thermal.H j) by ring] + · rename_i hE + have Ht := HS false + rw [show lowI j false=0 from rfl,show depthRate 0=1 by simp [depthRate,side]] at Ht + have HM := hv M J s f t h hf (show 0 < s.mass from hx ▸ hh.pos) hh.pos (fun q => priciest q j false) false + rw [show s.mass/s.norm=1 from entry_share hh hx, one_mul] at HM + rw [show s.live=false from Bool.eq_false_iff.mpr hE] + simp only [add_zero] + rw [show souvenir (M:=M) false j = + fun L => rooms false j L+512* subtotal (fun b => mutedR b.post j false) L from funext (receipt_static _)] + change stats t _ ≤ _*k + rw [HG] + have HE : stats t (fun L => 512*subtotal (fun b => mutedR b.post j false) L) = + 512* t.fee (fun b => mutedR b.post j false) := + Ex_lmul (s.here.dice M) _ 512 + rw [HE] + change _≤ 1+_*_*k at Ht + have hp : height j ≤ k := (amount_limits _ _).2 + have hh0 : -log s.mass ≤ k := by unfold entry at hx; rw [← hx]; unfold k amount; linarith [le_max_left (-log s.norm) 0,d_pos j] + unfold mutedR; linarith + +/-- pointwise price comparison for expected flags. -/ +lemma parody_fee (s : Scene M) (j : ℕ) (i : ℕ) (h : i ≤ mainIx j+1) : + parody s i ≤ 10^6*Thermal.H j^32*surcharge s true (i-1) := by + have hE : 0 < Thermal.H j := H_pos _ + by_cases he : (rose (tagM i) (0:Grid M)).rogue s + · have hx := he.1 + have hp : 0 < i := hx + have HH := he.2 + have HC := (bottom_level j) + have HT : side (mainIx j) ≤ side (i-1) := + side_ant _ _ (by omega) + rw [HC] at HT + change hazard s true (i-1) at HH + rw [show parody s i=1 from ite_eq_left he,surcharge,ite_eq_left HH] + change side (i-1)^2/128 ≤ _ at HH + let v := Thermal.H j + have Hb : 1/v^32/128 ≤ edgeW s (i-1) := by + have Hi : 0 ≤ 1/(Thermal.H j)^16 := by positivity + apply le_trans _ HH + have HT := div_le_div_of_nonneg_right (pow_le_pow_left₀ Hi HT 2) (by norm_num : (0:ℝ)≤128) + rw [show (1/Thermal.H j^16)^2=1/v^32 by unfold v; ring] at HT; exact HT + have hx' : 0 < v^32 := pow_pos hE _ + change 1 ≤ 10^6*v^32*_ + nlinarith [mul_le_mul_of_nonneg_left Hb hx'.le, + show v^32*(1/v^32/128)=1/128 by have Hx : 0 < v := hE; field_simp] + · rw [show parody s i=0 from ite_eq_right he] + exact mul_nonneg (by positivity) (surcharge_nonneg _ _ _) + +lemma depth_rate_fee (i j : ℕ) (h : i ≤ mainIx j) : + 1/depthRate i ≤ Thermal.H j^8 := by + have H : depthRate (mainIx j) ≤ depthRate i := sqrt_le_sqrt (side_ant i _ h) + rw [depth_at_main] at H + apply (div_le_iff₀ (rate_easy _).1).mpr + have HE := (div_le_iff₀ (show 0 < Thermal.H j^8 by have := H_pos j; positivity)).mp H + linarith + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Series.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Series.lean index fe6f3f19afd6eae7edb54e207087215bb45a35e0..f8a4e3f0e19fd77c8b0cb386bb471dc1a9f585c1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Series.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Series.lean @@ -1,273 +1,273 @@ -import OAI.Analysis.FourierLLogL.Assembly.Festival -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot - -def miniStage (j i : ℕ) (L : Path M) := - 20000*scalarSticker i j L+ - 4*side i*(billWait (tagL i) j L:ℝ) -def rooms (c : Bool) (j : ℕ) (L : Path M) := - 1+ ∑ i∈playsL j M c, miniStage j i L - -def nakedStage (i j : ℕ) (L : Path M) := - (1+fullKills L)/depthRate i + depthRate i*timex (tagM i) j L+ - (billWait (tagM (i-1)) j L:ℝ) -def palaces (j : ℕ) (L : Path M) := - 4*(billWait (tagM (mainIx j)) j L:ℝ)+ - 10^20* ∑ i∈Finset.Ico 0 (mainIx j+1), nakedStage i j L - -lemma decayed (n : ℕ) : depthRate (n+1) ≤ 3/4*depthRate n := by - let x := depthRate (n+1); let y := depthRate n - have h := rate_easy (n+1); have hv := rate_easy n - obtain ⟨ha,hb,hc⟩ := h - rw [side_succ] at hc - nlinarith - -lemma weight_decay (x : ℝ) (hx : 0 ≤ x) (n m : ℕ) (h : n ≤ m) : - ∑ i∈Finset.Ico n m, depthRate i*(x+1+i) ≤ 10*depthRate n*(x+10+n) := by - suffices hh : ∀ k n : ℕ, n+k=m → - ∑ i∈Finset.Ico n m, depthRate i*(x+1+i) ≤ 10*depthRate n*(x+10+n) from hh (m-n) n (by omega) - intro k; induction k with - | zero => - intro n he; simp only [Nat.add_zero] at he - rw [he]; simp only [Finset.Ico_self,Finset.sum_empty] - have := (rate_easy m).1; positivity - | succ k ih => - intro n he - rw [start_ico _ n (by omega)] - apply le_trans (add_le_add_right (ih (n+1) (by omega)) (depthRate n*(x+1+n))) - push_cast - have H := decayed n; have hp := (rate_easy n).1 - nlinarith [Nat.cast_nonneg (α:=ℝ) n] - -lemma waiting_fee {g : Grade} {t : Thread M s} {j : ℕ} - (h : stats t (fun l => (chooseCap g l:ℝ)) ≤ 10^6*dBase g s j) : - stats t (fun l => (billWait g j l:ℝ)) ≤ 10^10*(dBase g s j+Thermal.H j) := by - let U := s.here.dice M; let f (x : Grid M) := (↑(chooseCap g (t.stream x)):ℝ) - have HE : stats t (fun l => (billWait g j l:ℝ)) = - 1000*(1+U.Ex f+g.i+(ell j:ℝ)) := by - unfold stats billWait latency - push_cast - simp_rw [heroPlot,stage] - rw [Ex_lmul,Ex_add,Ex_add,Ex_add (f:=fun _ => 1)] - simp only [Ex_const] - rfl - rw [HE] - change U.Ex f ≤ _ at h - have Hi : (ell j:ℝ) ≤ Thermal.H j := by unfold Thermal.H; exact_mod_cast ell_le_h j - have H := log_nonneg (amount_limits s j).1 - unfold dBase at *; linarith [H_pos j,Nat.cast_nonneg (α:=ℝ) g.i] - -lemma size_stat {t : Thread M s} (h : t.Following j J f) (hh : Fit s) (hv : entry s) : - stats t fullKills ≤ 1 := - le_trans (deaths t h hh.pos) (entry_share hh hv).le - -lemma rooms_fee : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - (∀ x, f x ≤ exp (height j)) → - ∀ c : Bool, stats t (rooms c j) ≤ 1 + 10^30*depthRate (lowI j c)*amount s j := by - filter_upwards [paid_levels,hpoly 20 1] with j hj hv M J s f t h hh hx hf c - let n := lowI j c; let m := highI j M c - let A := amount s j - have hP : 1 ≤ A ∧ height j ≤ A := amount_limits _ _ - have hz : 20*Thermal.H j ≤ height j := by rw [pow_one] at hv; exact hv - have H (i : ℕ) : stats t (miniStage j i) ≤ 10^20*depthRate i*(A+1+i) := by - let g := tagL i; let a := depthRate i - let H := dBase g s j - have hi := hj M J s f t h hh hx hf g (fun x => Bool.noConfusion x) - obtain ⟨h₁,h₂⟩ := hi - have HC : H ≤ 2*(A+1+i) := by - change 1+log A+(i:ℝ) ≤ _ - linarith [Real.log_le_sub_one_of_pos (by linarith : 0 20000*(a*(1+_+_)) +4*side i * _) = _ - unfold stats - rw [Ex_add _ (f:=fun l => _),Ex_lmul,Ex_lmul,Ex_lmul,Ex_add,Ex_add (f:=fun l=>1),Ex_const] - rw [hE] - have ha : 0 < a ∧ a ≤ 1 ∧ a^2=side i := rate_easy _ - have HA : side i ≤ a := by nlinarith - have hv := size_stat h hh hx - have HL : side i * stats t f ≤ a*(10^11*(A+1+i)) := - (mul_le_mul_of_nonneg_left HJ (side_pos i).le).trans - (mul_le_mul_of_nonneg_right HA (by have hh := le_trans (by norm_num : (0:ℝ)≤1) hP.1; positivity)) - change _ ≤ 10^20*a*_ - nlinarith [mul_le_mul_of_nonneg_left HE ha.1.le,Nat.cast_nonneg (α:=ℝ) i, - mul_nonneg ha.1.le (Nat.cast_nonneg (α:=ℝ) i)] - have Hn : (n:ℝ) ≤ 19*Thermal.H j := by - unfold n lowI - split - · exact (main_bounds j (Nat.le_add_right _ 3)).2.1 - · simp only [Nat.cast_zero]; have := H_pos j; positivity - calc - _ = 1+∑ i∈Finset.Ico n m, stats t (miniStage j i) := by - unfold stats rooms; rw [Ex_add (f:=fun _ => 1),Ex_const,Ex_finset_sum] - rfl - _ ≤ 1+∑ i∈Finset.Ico n m, (10^20*depthRate i*(A+1+i)) := - add_le_add_right (Finset.sum_le_sum (fun i _ => H i)) _ - _ ≤ _ := by - simp_rw [mul_assoc (a:=(10:ℝ)^20) (b:=depthRate _), - ← Finset.mul_sum] - have HC := weight_decay A (by linarith) n m (by unfold m highI n; omega) - have H : A+10+n ≤ 1000*A := by linarith - change _ ≤ 1+10^30*depthRate n*A - nlinarith [mul_le_mul_of_nonneg_left H (rate_easy n).1.le,show - 0 ≤ depthRate n*A by apply mul_nonneg (rate_easy n).1.le; linarith] - -lemma depth_at_main (j : ℕ) : depthRate (mainIx j) = 1/(Thermal.H j)^8 := by - unfold depthRate; rw [bottom_level] - rw [show 1/Thermal.H j^16=(1/Thermal.H j^8)^2 by ring]; rw [Real.sqrt_sq (by positivity)] - -lemma palaces_fee : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - s.live=true → exp (-height j) ≤ s.norm → - (∀ x, f x ≤ exp (height j)) → - stats t (palaces j) ≤ 10^120*height j/Thermal.H j := by - filter_upwards [paid_levels,hpoly_mix 1 11 1,eventually_ge_atTop 1] with j hj hT Hj - M J s f t h hh hx he hhx hf - let d := height j; let v := Thermal.H j; let k := mainIx j+1 - have hd : 1 ≤ d := one_le_pow₀ (by norm_num) - have hV : 1 ≤ v := H_ge j - have hv : 0 < v := by linarith - have hk : (k:ℝ) ≤ 20*v := by - unfold k; push_cast - linarith [(main_bounds j (Nat.le_add_right (mainIx j) 3)).2.1,show Thermal.H j=v from rfl] - let A := amount s j - have hl : A ≤ 4*d := by - have H := log_le_log (exp_pos _) hhx - rw [log_exp] at H - change d+max _ 0 + 2 ≤ _ - have he' : max (-log s.norm) 0 ≤ d := max_le (by unfold d; linarith) (by linarith) - linarith - let m := (j:ℝ) - have hm : 1 ≤ m := by unfold m; exact_mod_cast Hj - have HP : v^11*m ≤ d := by simpa only [pow_one,one_mul] using hT - have hA : log A ≤ 4+m := by - have Hx : 1 ≤ A := (amount_limits _ _).1 - apply (log_le_log (show 0 < A by linarith) hl).trans - unfold d; rw [log_mul (by positivity) (d_pos j).ne',height, log_pow] - unfold m - have hU : log 2 ≤ 1 := by linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ) < 2)] - nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4),Nat.cast_nonneg (α:=ℝ) j] - have hP (i : ℕ) (hK : i < k) := hj M J s f t h hh hx hf (tagM i) (fun _ => - ⟨(by change i ≤ mainIx j; unfold k at hK; omega),he⟩) - have HR (i : ℕ) (hi : i (billWait (tagM i) j l:ℝ)) ≤ 10^12*(v+m) := by - apply (waiting_fee (hP i hi).2).trans - change (10:ℝ)^10*(1+log A+(i:ℝ)+v) ≤ _ - have HH : (i:ℝ) ≤ k := Nat.cast_le.mpr hi.le - linarith - have HX : m ≤ d/v := (le_div_iff₀ hv).mpr (by - have HL : v ≤ v^11 := by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 11) - nlinarith) - have HU (i : ℕ) (hi : i (↑(billWait (tagM (i-1)) j L):ℝ)) := by - unfold stats nakedStage - simp_rw [Ex_add,Ex_lmul,div_eq_mul_inv,Ex_mul, - Ex_add (f:=fun _ => 1),Ex_const] - rfl - have hb : 1/a ≤ v^8 := by - have hn : i ≤ mainIx j := by unfold k at hi; omega - have HH : depthRate (mainIx j) ≤ a := sqrt_le_sqrt (side_ant _ _ hn) - rw [depth_at_main] at HH - apply (div_le_iff₀ ha).mpr - have H' := (div_le_iff₀ (show 0 < v^8 by positivity)).mp HH - linarith - rw [HE] - have HH : (1+stats t fullKills)/a ≤ 2/a := div_le_div_of_nonneg_right (by linarith [size_stat h hh hx]) ha.le - have ht := HR (i-1) (by omega) - unfold a at *; linarith [mul_le_mul_of_nonneg_left (HU i hi) ha.le,show - (2:ℝ)/depthRate i=2*(1/depthRate i) by ring] - let F (i) := depthRate i*(1+(i:ℝ)) - let V := 2*v^8+10^12*(v+m); let S := 10^35*(d/v) - have HS : 0 ≤ S := by unfold S; positivity - let R := Finset.Ico 0 k - have HI : ∑ i∈R,stats t (nakedStage i j) ≤ k*V+100*S := by - have H : ∑ i∈R,F i ≤ 100 := by - have HI := weight_decay 0 le_rfl 0 k (by omega) - unfold F R - norm_num [depthRate,side] at HI ⊢; exact HI - calc - _ ≤ ∑ i∈R,(V+S*F i) := Finset.sum_le_sum fun i hi => hcost i (Finset.mem_Ico.mp hi).2 - _ ≤ _ := by - unfold R; rw [Finset.sum_add_distrib,←Finset.mul_sum,Finset.sum_const,Nat.card_Ico,nsmul_eq_mul,Nat.sub_zero] - unfold R at H; nlinarith - have hΦ : (v:ℝ)^10 *m ≤ d/v := (le_div_iff₀ hv).mpr (by apply le_trans _ HP; exact le_of_eq (by ring)) - have HQ : k*V ≤ 10^16*(d/v) := by - have he' : v^9 ≤ v^10*m := le_trans (pow_le_pow_right₀ hV (by omega)) - (le_mul_of_one_le_right (by positivity) hm) - have ht' : v^2 ≤ v^9 := pow_le_pow_right₀ hV (by omega) - have hh' : v*m ≤ v^10*m := mul_le_mul_of_nonneg_right - (by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 10)) (by linarith) - have hv' : 0 ≤ V := by unfold V; positivity - apply (mul_le_mul_of_nonneg_right hk hv').trans - unfold V; nlinarith - have H₁ : stats t (fun l => (billWait (tagM (mainIx j)) j l:ℝ)) ≤ 10^14*(d/v) := by - apply (HR (mainIx j) (by unfold k; omega)).trans - have HH : v ≤ d/v := le_trans (by - have HE : v ≤ v^10 := by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 10) - exact le_trans HE (le_mul_of_one_le_right (by positivity) hm)) hΦ - linarith - have HHH : stats t (palaces j) = - 4*stats t (fun l => (billWait (tagM (mainIx j)) j l:ℝ))+ - 10^20*∑ i∈R,stats t (nakedStage i j) := by - unfold stats palaces; rw [Ex_add _ (f:=fun _ => _),Ex_lmul,Ex_lmul,Ex_finset_sum] - rw [HHH] - apply le_trans (add_le_add (mul_le_mul_of_nonneg_left H₁ (by norm_num)) (mul_le_mul_of_nonneg_left HI (by norm_num))) - change _ ≤10^120*d/v - unfold S at *; nlinarith [show 10^120*d/v=10^120*(d/v) by ring] - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Festival +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot + +def miniStage (j i : ℕ) (L : Path M) := + 20000*scalarSticker i j L+ + 4*side i*(billWait (tagL i) j L:ℝ) +def rooms (c : Bool) (j : ℕ) (L : Path M) := + 1+ ∑ i∈playsL j M c, miniStage j i L + +def nakedStage (i j : ℕ) (L : Path M) := + (1+fullKills L)/depthRate i + depthRate i*timex (tagM i) j L+ + (billWait (tagM (i-1)) j L:ℝ) +def palaces (j : ℕ) (L : Path M) := + 4*(billWait (tagM (mainIx j)) j L:ℝ)+ + 10^20* ∑ i∈Finset.Ico 0 (mainIx j+1), nakedStage i j L + +lemma decayed (n : ℕ) : depthRate (n+1) ≤ 3/4*depthRate n := by + let x := depthRate (n+1); let y := depthRate n + have h := rate_easy (n+1); have hv := rate_easy n + obtain ⟨ha,hb,hc⟩ := h + rw [side_succ] at hc + nlinarith + +lemma weight_decay (x : ℝ) (hx : 0 ≤ x) (n m : ℕ) (h : n ≤ m) : + ∑ i∈Finset.Ico n m, depthRate i*(x+1+i) ≤ 10*depthRate n*(x+10+n) := by + suffices hh : ∀ k n : ℕ, n+k=m → + ∑ i∈Finset.Ico n m, depthRate i*(x+1+i) ≤ 10*depthRate n*(x+10+n) from hh (m-n) n (by omega) + intro k; induction k with + | zero => + intro n he; simp only [Nat.add_zero] at he + rw [he]; simp only [Finset.Ico_self,Finset.sum_empty] + have := (rate_easy m).1; positivity + | succ k ih => + intro n he + rw [start_ico _ n (by omega)] + apply le_trans (add_le_add_right (ih (n+1) (by omega)) (depthRate n*(x+1+n))) + push_cast + have H := decayed n; have hp := (rate_easy n).1 + nlinarith [Nat.cast_nonneg (α:=ℝ) n] + +lemma waiting_fee {g : Grade} {t : Thread M s} {j : ℕ} + (h : stats t (fun l => (chooseCap g l:ℝ)) ≤ 10^6*dBase g s j) : + stats t (fun l => (billWait g j l:ℝ)) ≤ 10^10*(dBase g s j+Thermal.H j) := by + let U := s.here.dice M; let f (x : Grid M) := (↑(chooseCap g (t.stream x)):ℝ) + have HE : stats t (fun l => (billWait g j l:ℝ)) = + 1000*(1+U.Ex f+g.i+(ell j:ℝ)) := by + unfold stats billWait latency + push_cast + simp_rw [heroPlot,stage] + rw [Ex_lmul,Ex_add,Ex_add,Ex_add (f:=fun _ => 1)] + simp only [Ex_const] + rfl + rw [HE] + change U.Ex f ≤ _ at h + have Hi : (ell j:ℝ) ≤ Thermal.H j := by unfold Thermal.H; exact_mod_cast ell_le_h j + have H := log_nonneg (amount_limits s j).1 + unfold dBase at *; linarith [H_pos j,Nat.cast_nonneg (α:=ℝ) g.i] + +lemma size_stat {t : Thread M s} (h : t.Following j J f) (hh : Fit s) (hv : entry s) : + stats t fullKills ≤ 1 := + le_trans (deaths t h hh.pos) (entry_share hh hv).le + +lemma rooms_fee : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + (∀ x, f x ≤ exp (height j)) → + ∀ c : Bool, stats t (rooms c j) ≤ 1 + 10^30*depthRate (lowI j c)*amount s j := by + filter_upwards [paid_levels,hpoly 20 1] with j hj hv M J s f t h hh hx hf c + let n := lowI j c; let m := highI j M c + let A := amount s j + have hP : 1 ≤ A ∧ height j ≤ A := amount_limits _ _ + have hz : 20*Thermal.H j ≤ height j := by rw [pow_one] at hv; exact hv + have H (i : ℕ) : stats t (miniStage j i) ≤ 10^20*depthRate i*(A+1+i) := by + let g := tagL i; let a := depthRate i + let H := dBase g s j + have hi := hj M J s f t h hh hx hf g (fun x => Bool.noConfusion x) + obtain ⟨h₁,h₂⟩ := hi + have HC : H ≤ 2*(A+1+i) := by + change 1+log A+(i:ℝ) ≤ _ + linarith [Real.log_le_sub_one_of_pos (by linarith : 0 20000*(a*(1+_+_)) +4*side i * _) = _ + unfold stats + rw [Ex_add _ (f:=fun l => _),Ex_lmul,Ex_lmul,Ex_lmul,Ex_add,Ex_add (f:=fun l=>1),Ex_const] + rw [hE] + have ha : 0 < a ∧ a ≤ 1 ∧ a^2=side i := rate_easy _ + have HA : side i ≤ a := by nlinarith + have hv := size_stat h hh hx + have HL : side i * stats t f ≤ a*(10^11*(A+1+i)) := + (mul_le_mul_of_nonneg_left HJ (side_pos i).le).trans + (mul_le_mul_of_nonneg_right HA (by have hh := le_trans (by norm_num : (0:ℝ)≤1) hP.1; positivity)) + change _ ≤ 10^20*a*_ + nlinarith [mul_le_mul_of_nonneg_left HE ha.1.le,Nat.cast_nonneg (α:=ℝ) i, + mul_nonneg ha.1.le (Nat.cast_nonneg (α:=ℝ) i)] + have Hn : (n:ℝ) ≤ 19*Thermal.H j := by + unfold n lowI + split + · exact (main_bounds j (Nat.le_add_right _ 3)).2.1 + · simp only [Nat.cast_zero]; have := H_pos j; positivity + calc + _ = 1+∑ i∈Finset.Ico n m, stats t (miniStage j i) := by + unfold stats rooms; rw [Ex_add (f:=fun _ => 1),Ex_const,Ex_finset_sum] + rfl + _ ≤ 1+∑ i∈Finset.Ico n m, (10^20*depthRate i*(A+1+i)) := + add_le_add_right (Finset.sum_le_sum (fun i _ => H i)) _ + _ ≤ _ := by + simp_rw [mul_assoc (a:=(10:ℝ)^20) (b:=depthRate _), + ← Finset.mul_sum] + have HC := weight_decay A (by linarith) n m (by unfold m highI n; omega) + have H : A+10+n ≤ 1000*A := by linarith + change _ ≤ 1+10^30*depthRate n*A + nlinarith [mul_le_mul_of_nonneg_left H (rate_easy n).1.le,show + 0 ≤ depthRate n*A by apply mul_nonneg (rate_easy n).1.le; linarith] + +lemma depth_at_main (j : ℕ) : depthRate (mainIx j) = 1/(Thermal.H j)^8 := by + unfold depthRate; rw [bottom_level] + rw [show 1/Thermal.H j^16=(1/Thermal.H j^8)^2 by ring]; rw [Real.sqrt_sq (by positivity)] + +lemma palaces_fee : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + s.live=true → exp (-height j) ≤ s.norm → + (∀ x, f x ≤ exp (height j)) → + stats t (palaces j) ≤ 10^120*height j/Thermal.H j := by + filter_upwards [paid_levels,hpoly_mix 1 11 1,eventually_ge_atTop 1] with j hj hT Hj + M J s f t h hh hx he hhx hf + let d := height j; let v := Thermal.H j; let k := mainIx j+1 + have hd : 1 ≤ d := one_le_pow₀ (by norm_num) + have hV : 1 ≤ v := H_ge j + have hv : 0 < v := by linarith + have hk : (k:ℝ) ≤ 20*v := by + unfold k; push_cast + linarith [(main_bounds j (Nat.le_add_right (mainIx j) 3)).2.1,show Thermal.H j=v from rfl] + let A := amount s j + have hl : A ≤ 4*d := by + have H := log_le_log (exp_pos _) hhx + rw [log_exp] at H + change d+max _ 0 + 2 ≤ _ + have he' : max (-log s.norm) 0 ≤ d := max_le (by unfold d; linarith) (by linarith) + linarith + let m := (j:ℝ) + have hm : 1 ≤ m := by unfold m; exact_mod_cast Hj + have HP : v^11*m ≤ d := by simpa only [pow_one,one_mul] using hT + have hA : log A ≤ 4+m := by + have Hx : 1 ≤ A := (amount_limits _ _).1 + apply (log_le_log (show 0 < A by linarith) hl).trans + unfold d; rw [log_mul (by positivity) (d_pos j).ne',height, log_pow] + unfold m + have hU : log 2 ≤ 1 := by linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ) < 2)] + nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4),Nat.cast_nonneg (α:=ℝ) j] + have hP (i : ℕ) (hK : i < k) := hj M J s f t h hh hx hf (tagM i) (fun _ => + ⟨(by change i ≤ mainIx j; unfold k at hK; omega),he⟩) + have HR (i : ℕ) (hi : i (billWait (tagM i) j l:ℝ)) ≤ 10^12*(v+m) := by + apply (waiting_fee (hP i hi).2).trans + change (10:ℝ)^10*(1+log A+(i:ℝ)+v) ≤ _ + have HH : (i:ℝ) ≤ k := Nat.cast_le.mpr hi.le + linarith + have HX : m ≤ d/v := (le_div_iff₀ hv).mpr (by + have HL : v ≤ v^11 := by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 11) + nlinarith) + have HU (i : ℕ) (hi : i (↑(billWait (tagM (i-1)) j L):ℝ)) := by + unfold stats nakedStage + simp_rw [Ex_add,Ex_lmul,div_eq_mul_inv,Ex_mul, + Ex_add (f:=fun _ => 1),Ex_const] + rfl + have hb : 1/a ≤ v^8 := by + have hn : i ≤ mainIx j := by unfold k at hi; omega + have HH : depthRate (mainIx j) ≤ a := sqrt_le_sqrt (side_ant _ _ hn) + rw [depth_at_main] at HH + apply (div_le_iff₀ ha).mpr + have H' := (div_le_iff₀ (show 0 < v^8 by positivity)).mp HH + linarith + rw [HE] + have HH : (1+stats t fullKills)/a ≤ 2/a := div_le_div_of_nonneg_right (by linarith [size_stat h hh hx]) ha.le + have ht := HR (i-1) (by omega) + unfold a at *; linarith [mul_le_mul_of_nonneg_left (HU i hi) ha.le,show + (2:ℝ)/depthRate i=2*(1/depthRate i) by ring] + let F (i) := depthRate i*(1+(i:ℝ)) + let V := 2*v^8+10^12*(v+m); let S := 10^35*(d/v) + have HS : 0 ≤ S := by unfold S; positivity + let R := Finset.Ico 0 k + have HI : ∑ i∈R,stats t (nakedStage i j) ≤ k*V+100*S := by + have H : ∑ i∈R,F i ≤ 100 := by + have HI := weight_decay 0 le_rfl 0 k (by omega) + unfold F R + norm_num [depthRate,side] at HI ⊢; exact HI + calc + _ ≤ ∑ i∈R,(V+S*F i) := Finset.sum_le_sum fun i hi => hcost i (Finset.mem_Ico.mp hi).2 + _ ≤ _ := by + unfold R; rw [Finset.sum_add_distrib,←Finset.mul_sum,Finset.sum_const,Nat.card_Ico,nsmul_eq_mul,Nat.sub_zero] + unfold R at H; nlinarith + have hΦ : (v:ℝ)^10 *m ≤ d/v := (le_div_iff₀ hv).mpr (by apply le_trans _ HP; exact le_of_eq (by ring)) + have HQ : k*V ≤ 10^16*(d/v) := by + have he' : v^9 ≤ v^10*m := le_trans (pow_le_pow_right₀ hV (by omega)) + (le_mul_of_one_le_right (by positivity) hm) + have ht' : v^2 ≤ v^9 := pow_le_pow_right₀ hV (by omega) + have hh' : v*m ≤ v^10*m := mul_le_mul_of_nonneg_right + (by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 10)) (by linarith) + have hv' : 0 ≤ V := by unfold V; positivity + apply (mul_le_mul_of_nonneg_right hk hv').trans + unfold V; nlinarith + have H₁ : stats t (fun l => (billWait (tagM (mainIx j)) j l:ℝ)) ≤ 10^14*(d/v) := by + apply (HR (mainIx j) (by unfold k; omega)).trans + have HH : v ≤ d/v := le_trans (by + have HE : v ≤ v^10 := by simpa using pow_le_pow_right₀ hV (by omega : 1 ≤ 10) + exact le_trans HE (le_mul_of_one_le_right (by positivity) hm)) hΦ + linarith + have HHH : stats t (palaces j) = + 4*stats t (fun l => (billWait (tagM (mainIx j)) j l:ℝ))+ + 10^20*∑ i∈R,stats t (nakedStage i j) := by + unfold stats palaces; rw [Ex_add _ (f:=fun _ => _),Ex_lmul,Ex_lmul,Ex_finset_sum] + rw [HHH] + apply le_trans (add_le_add (mul_le_mul_of_nonneg_left H₁ (by norm_num)) (mul_le_mul_of_nonneg_left HI (by norm_num))) + change _ ≤10^120*d/v + unfold S at *; nlinarith [show 10^120*d/v=10^120*(d/v) by ring] + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Slab.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Slab.lean index ecba4d941c3260608032525d9144ae08e3d352a0..3f14ccb51fa4272fe2973901194b3a15a3ae30d2 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Slab.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Slab.lean @@ -1,138 +1,138 @@ -import OAI.Analysis.FourierLLogL.Assembly.Rail -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe - -/-- Depth of a sequence (no missing jumps within plateau) -/ -def matrix (n : ℕ) : Path M → Prop -| [] => True -| b::l => depthB b=n ∧ matrix (n+1) l - -lemma matrix_trip {x : Grid M} (l : Path M) (h : Trip x s l) : - matrix s.here.lev l := by - induction l generalizing s with - | nil => exact True.intro - | cons b l ih => - have He : depthB b=s.here.lev := by unfold depthB; rw [h.2.1.lower.here,h.1] - have HH := ih h.2.2 - rw [depth_next,He] at HH; exact ⟨He,HH⟩ - -lemma matrix_cut (l : Path M) (n : ℕ) (k : ℕ) (h : matrix n l) : - ∃ p q : Path M, l=p++q ∧ matrix (n+p.length) q ∧ - (∀ b∈p, depthB b < k) ∧ (∀ b∈q, k ≤ depthB b) ∧ - n+p.length = min (max n k) (n+l.length) := by - induction l generalizing n with - | nil => refine ⟨[],[],rfl,True.intro,?_⟩; simp - | cons b l ih => - obtain ⟨hp,hq,hh,hE,h₁,h₂,h₃⟩ := ih _ h.2 - have HI : depthB b=n := h.1 - by_cases hv : n < k - · refine ⟨b::hp,hq, by rw [hh]; rfl,?_,?_,h₂,?_⟩ - · rw [List.length_cons,show n+(List.length hp+1)=n+1+List.length hp by omega] - exact hE - · intro x hx; rcases List.mem_cons.mp hx with rfl|hx - · omega - exact h₁ _ hx - · simp only [List.length_cons] - omega - · have H : hp.length=0 := by omega - have hL : hp=[] := List.eq_nil_of_length_eq_zero H - rw [hL,List.nil_append] at hh; cases hh - refine ⟨[],b::l,rfl,h,fun b hb => ?_,fun z hz => ?_,?_⟩ - · simp at hb - · rcases List.mem_cons.mp hz with rfl|hz; omega - exact h₂ _ hz - · simp; omega - -lemma slab_on (l : Path M) (n : ℕ) (h : matrix n l) (a k : ℕ) : - ∃ p q r : Path M, l=p++q++r ∧ - (∀ b∈p, ¬betweenS a k b) ∧ (∀ b∈r, ¬betweenS a k b) ∧ - q.length ≤ k := by - obtain ⟨p,s,H,h₁,h₂,h₃,h₄⟩ := matrix_cut l n a h - obtain ⟨q,r,hr,hx,hp,he,hk⟩ := matrix_cut s _ (a+k) h₁ - refine ⟨p,q,r,by rw [H,hr,List.append_assoc],fun b hi hh => ?_,fun b hi hh => ?_,?_⟩ - exact (h₂ b hi).not_ge hh.1 - exact hh.2.not_ge (he b hi) - have ht₁ : l.length=p.length+s.length := by rw [H,List.length_append] - have ht₂ : s.length=q.length+r.length := by rw [hr,List.length_append] - omega - -/-- quality trip coupons that cover all high nonbad jumps before activation. -/ -def shades (m : Grade → ℕ) (u : Grid M) (l : Path M) (j : ℕ) := - ∀ g : Grade, ∀ p q : Path M, l=p++q → stall u g j l p → - ∀ b∈p, snag g u b → betweenS (m g) (billWait g j l) b - -lemma sheds {s : Scene M} {l : Path M} (h : Cast j J f s l) (u : Grid M) : - ∃ m : Grade → ℕ, shades m u l j := by - choose m hm using fun g => bound_stage (u:=u) l h g - exact ⟨m,hm⟩ - -def stripe (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (b : Beat M) := - if betweenS (a g) (billWait g j l) b then (1:ℝ) else 0 - -lemma stripe_pos (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (b : Beat M) : - 0 ≤ stripe a g j l b := by unfold stripe; split <;> norm_num - -lemma stripe_bill (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (x : Grid M) - {s : Scene M} (h : Trip x s l) : subtotal (stripe a g j l) l ≤ billWait g j l := by - let f := stripe a g j l - obtain ⟨v,w,p,hl,hv,hp,hw⟩ := slab_on l _ (matrix_trip l h) (a g) (billWait g j l) - have HF (m : Path M) (hi : ∀ b∈m, ¬ betweenS (a g) (billWait g j l) b) : - subtotal f m=0 := by - induction m with - | nil => rfl - | cons b r ih => - have HH : f b=0 := ite_eq_right (hi b List.mem_cons_self) - rw [subtotal_cons,HH,ih fun y hy => hi y (List.mem_cons_of_mem _ hy),zero_add] - have HX (m : Path M) : - subtotal f m ≤ m.length := by - induction m with - | nil => simp [subtotal] - | cons b r ih => - have he : f b≤ 1 := by unfold f stripe; split <;> norm_num - rw [subtotal_cons, List.length_cons]; push_cast; linarith - change subtotal f l ≤ _ - nth_rw 1 [hl] - rw [subtotal_app,subtotal_app,HF v hv,HF p hp,zero_add,add_zero] - apply (HX w).trans - exact_mod_cast hw - - -lemma trim_cost (g : Beat M → ℝ) (hg : ∀ b, Ideal b.pre b.post → 0 ≤ g b) - {x : Grid M} (l p r : Path M) (h : Trip x s (p++(l++r))) : - subtotal g l ≤ subtotal g (p++(l++r)) := by - rw [subtotal_app,subtotal_app] - obtain ⟨ha,q,hq⟩ := trip_cut h - obtain ⟨hc,t,ht⟩ := trip_cut hq - linarith [trip_nonneg ht hg,trip_nonneg ha hg] - -lemma trip_depth_limit {l : Path M} {s : Scene M} (h : Cast j J f s l) : - s.here.lev+l.length ≤ J := by - induction l generalizing s with - | nil => exact (show s.Safe j J f from h).depth_ok - | cons b l ih => - obtain ⟨rfl,⟨t,ht⟩,hh⟩ := h - have hi := ih hh - rw [List.length_cons] - have hX := ht.2.1.1.here - rw [depth_next,depthB,hX] at hi - omega - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Rail +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe + +/-- Depth of a sequence (no missing jumps within plateau) -/ +def matrix (n : ℕ) : Path M → Prop +| [] => True +| b::l => depthB b=n ∧ matrix (n+1) l + +lemma matrix_trip {x : Grid M} (l : Path M) (h : Trip x s l) : + matrix s.here.lev l := by + induction l generalizing s with + | nil => exact True.intro + | cons b l ih => + have He : depthB b=s.here.lev := by unfold depthB; rw [h.2.1.lower.here,h.1] + have HH := ih h.2.2 + rw [depth_next,He] at HH; exact ⟨He,HH⟩ + +lemma matrix_cut (l : Path M) (n : ℕ) (k : ℕ) (h : matrix n l) : + ∃ p q : Path M, l=p++q ∧ matrix (n+p.length) q ∧ + (∀ b∈p, depthB b < k) ∧ (∀ b∈q, k ≤ depthB b) ∧ + n+p.length = min (max n k) (n+l.length) := by + induction l generalizing n with + | nil => refine ⟨[],[],rfl,True.intro,?_⟩; simp + | cons b l ih => + obtain ⟨hp,hq,hh,hE,h₁,h₂,h₃⟩ := ih _ h.2 + have HI : depthB b=n := h.1 + by_cases hv : n < k + · refine ⟨b::hp,hq, by rw [hh]; rfl,?_,?_,h₂,?_⟩ + · rw [List.length_cons,show n+(List.length hp+1)=n+1+List.length hp by omega] + exact hE + · intro x hx; rcases List.mem_cons.mp hx with rfl|hx + · omega + exact h₁ _ hx + · simp only [List.length_cons] + omega + · have H : hp.length=0 := by omega + have hL : hp=[] := List.eq_nil_of_length_eq_zero H + rw [hL,List.nil_append] at hh; cases hh + refine ⟨[],b::l,rfl,h,fun b hb => ?_,fun z hz => ?_,?_⟩ + · simp at hb + · rcases List.mem_cons.mp hz with rfl|hz; omega + exact h₂ _ hz + · simp; omega + +lemma slab_on (l : Path M) (n : ℕ) (h : matrix n l) (a k : ℕ) : + ∃ p q r : Path M, l=p++q++r ∧ + (∀ b∈p, ¬betweenS a k b) ∧ (∀ b∈r, ¬betweenS a k b) ∧ + q.length ≤ k := by + obtain ⟨p,s,H,h₁,h₂,h₃,h₄⟩ := matrix_cut l n a h + obtain ⟨q,r,hr,hx,hp,he,hk⟩ := matrix_cut s _ (a+k) h₁ + refine ⟨p,q,r,by rw [H,hr,List.append_assoc],fun b hi hh => ?_,fun b hi hh => ?_,?_⟩ + exact (h₂ b hi).not_ge hh.1 + exact hh.2.not_ge (he b hi) + have ht₁ : l.length=p.length+s.length := by rw [H,List.length_append] + have ht₂ : s.length=q.length+r.length := by rw [hr,List.length_append] + omega + +/-- quality trip coupons that cover all high nonbad jumps before activation. -/ +def shades (m : Grade → ℕ) (u : Grid M) (l : Path M) (j : ℕ) := + ∀ g : Grade, ∀ p q : Path M, l=p++q → stall u g j l p → + ∀ b∈p, snag g u b → betweenS (m g) (billWait g j l) b + +lemma sheds {s : Scene M} {l : Path M} (h : Cast j J f s l) (u : Grid M) : + ∃ m : Grade → ℕ, shades m u l j := by + choose m hm using fun g => bound_stage (u:=u) l h g + exact ⟨m,hm⟩ + +def stripe (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (b : Beat M) := + if betweenS (a g) (billWait g j l) b then (1:ℝ) else 0 + +lemma stripe_pos (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (b : Beat M) : + 0 ≤ stripe a g j l b := by unfold stripe; split <;> norm_num + +lemma stripe_bill (a : Grade → ℕ) (g : Grade) (j : ℕ) (l : Path M) (x : Grid M) + {s : Scene M} (h : Trip x s l) : subtotal (stripe a g j l) l ≤ billWait g j l := by + let f := stripe a g j l + obtain ⟨v,w,p,hl,hv,hp,hw⟩ := slab_on l _ (matrix_trip l h) (a g) (billWait g j l) + have HF (m : Path M) (hi : ∀ b∈m, ¬ betweenS (a g) (billWait g j l) b) : + subtotal f m=0 := by + induction m with + | nil => rfl + | cons b r ih => + have HH : f b=0 := ite_eq_right (hi b List.mem_cons_self) + rw [subtotal_cons,HH,ih fun y hy => hi y (List.mem_cons_of_mem _ hy),zero_add] + have HX (m : Path M) : + subtotal f m ≤ m.length := by + induction m with + | nil => simp [subtotal] + | cons b r ih => + have he : f b≤ 1 := by unfold f stripe; split <;> norm_num + rw [subtotal_cons, List.length_cons]; push_cast; linarith + change subtotal f l ≤ _ + nth_rw 1 [hl] + rw [subtotal_app,subtotal_app,HF v hv,HF p hp,zero_add,add_zero] + apply (HX w).trans + exact_mod_cast hw + + +lemma trim_cost (g : Beat M → ℝ) (hg : ∀ b, Ideal b.pre b.post → 0 ≤ g b) + {x : Grid M} (l p r : Path M) (h : Trip x s (p++(l++r))) : + subtotal g l ≤ subtotal g (p++(l++r)) := by + rw [subtotal_app,subtotal_app] + obtain ⟨ha,q,hq⟩ := trip_cut h + obtain ⟨hc,t,ht⟩ := trip_cut hq + linarith [trip_nonneg ht hg,trip_nonneg ha hg] + +lemma trip_depth_limit {l : Path M} {s : Scene M} (h : Cast j J f s l) : + s.here.lev+l.length ≤ J := by + induction l generalizing s with + | nil => exact (show s.Safe j J f from h).depth_ok + | cons b l ih => + obtain ⟨rfl,⟨t,ht⟩,hh⟩ := h + have hi := ih hh + rw [List.length_cons] + have hX := ht.2.1.1.here + rw [depth_next,depthB,hX] at hi + omega + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Tokens.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Tokens.lean index a1f7133d9f3a0e7ac16ede2cef7344a5765fd25e..0ff3b2fd2acf02026f5b1c51d5eb09d17e4d08c4 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Tokens.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Tokens.lean @@ -1,223 +1,223 @@ -import OAI.Analysis.FourierLLogL.Assembly.Duration -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Run Streak - -def lowI (j : ℕ) (b : Bool) := if b then mainIx j else 0 -def highI (j M : ℕ) (b : Bool) := lowI j b+M+1 -def playsL (j M : ℕ) (b : Bool) := Finset.Ico (lowI j b) (highI j M b) -def picks (s : Scene M) (i j : ℕ) (b : Bool) := - i ∈ playsL j M b ∧ hazard s false i - -def priciest (s : Scene M) (j : ℕ) (b : Bool) := - if h : ∃ i, picks s i j b then Nat.find h else lowI j b -def mutedR (s : Scene M) (j : ℕ) (b : Bool) := - surcharge s false (priciest s j b) - -/-- scalar edge charges pay the largest bad level, chosen locally at the pre-bit moment. -/ -lemma pricey (s : Scene M) (j : ℕ) (b : Bool) (i : ℕ) (h : picks s i j b) : - side i ≤ 128*mutedR s j b := by - let n := priciest s j b - have hi : ∃ i, picks s i j b := ⟨i,h⟩ - have hn : n=Nat.find hi := dite_eq_left hi - have HE : hazard s false n := hn.symm ▸ (Nat.find_spec hi).2 - have H : side i ≤ side n := pow_le_pow_of_le_one (by norm_num) (by norm_num) - (hn.le.trans (Nat.find_min' hi h)) - change side n / 128 ≤ edgeW s n at HE - change _ ≤ 128*surcharge s false n - rw [surcharge,ite_eq_left (show hazard s false n from HE)]; linarith - -def special (j : ℕ) (b : Beat M) := (rose (tagM (mainIx j +1)) (0:Grid M)).flag b.post - -def token (a : Grade → ℕ) (L : Path M) (j : ℕ) (s : Bool) (b : Beat M) := - side M+512*mutedR b.post j s+ - (∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b) + - (if s then 4*(special j b+stripe a (tagM (mainIx j)) j L b) else 0) - -lemma token_floor (a : Grade → ℕ) (j : ℕ) (s : Bool) (L : Path M) (b : Beat M) : - side M ≤ token a L j s b ∧ 512*mutedR b.post j s ≤ token a L j s b ∧ - (∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b) ≤ token a L j s b ∧ - (s=true → 4*(special j b+stripe a (tagM (mainIx j)) j L b) ≤ token a L j s b) := by - let z := ∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b - have hZ : 0 ≤ z := Finset.sum_nonneg fun i _ => mul_nonneg (mul_nonneg (by norm_num) - (side_pos _).le) (stripe_pos _ _ _ _ _) - have hx : 0 ≤ mutedR b.post j s := surcharge_nonneg _ _ _ - have hi : 0 ≤ special j b := flag_nonneg _ _ - have hv := stripe_pos a (tagM (mainIx j)) j L b - have hh := side_pos M - unfold token - unfold z at * - cases s <;> simp only [↓reduceIte,Bool.false_eq_true] <;> - refine ⟨?_,?_,?_,fun H => ?_⟩ <;> first | linarith | contradiction - -lemma tokens_nonneg (a : Grade → ℕ) (L : Path M) (b : Beat M) (j : ℕ) (s : Bool) : - 0 ≤ token a L j s b := (side_pos M).le.trans (token_floor a j s L b).1 - -lemma amp_test {s : Scene M} (h : Climb s) (u : Grid M) : - ‖gain s u‖ ≤ ampU s u ∧ ampU s u ≤ 4 ∧ - ∃ b : Bool, ampU s u=‖scalar (s.smaller b) u‖ := by - have H (b) := (scalar_norm (h.child b) u).trans (h.child b).lift - constructor - · apply (norm_halfcomplex _ _).trans - have h1 : ‖scalar (s.smaller false) u‖ ≤ ampU s u := le_max_left _ _ - have h2 : ‖scalar (s.smaller true) u‖ ≤ ampU s u := le_max_right _ _ - linarith - · unfold ampU - refine ⟨max_le (H false) (H true),?_⟩ - by_cases hh : ‖scalar (s.smaller true) u‖ ≤ ‖scalar (s.smaller false) u‖ - exact ⟨false,max_eq_left hh⟩ - exact ⟨true,max_eq_right (le_of_not_ge hh)⟩ - -lemma side_ant (i j : ℕ) (h : i ≤ j) : side j ≤ side i := - pow_le_pow_of_le_one (by norm_num) (by norm_num) h - -/-- Each unmasked direct jump outside the main intervals can be paid by its amplitude-selected -coupons. During the recursion node n hasn't entered or is unmasked for its scalar level. -/ -lemma direct_point {s : Scene M} {L : Path M} {x u : Grid M} (h : Trip x s L) - (j : ℕ) (c : Bool) (a : Grade → ℕ) (hh : shades a u L j) - (n : ℕ) (l r : Path M) (b : Beat M) - (hR : c=true → stall u (tagM (mainIx j)) j L l) - (hc : ∀ i ∈ Finset.Ico (lowI j c) n, stall u (tagL i) j L l) - (_hn : n ∈ Set.Icc (lowI j c) (highI j M c)) (hl : L=l++r) - (hm : b∈l) (hb : n < highI j M c → 4*side n < ampU b.post u) : - ‖proceeds x u b‖ ≤ token a L j c b := by - let t := lowI j c; let B := side t; let q := b.post; let s := playsL j M c - let K := t+M - let v := token a L j c b - let p := ampU q u - have ht := trip_attend h b (by rw [hl]; simp [hm]) - have H := token_floor a j c L b - change _ ≤ v ∧ _ ≤ v ∧ _ ≤ v ∧ (c=true→_ ≤ v) at H - obtain ⟨hP,hB,z,hz⟩ := amp_test ht.1.climb u - change p ≤ 4 at hB - change p=_ at hz - rw [proceeds,norm_scaleC,ht.2.2.1,one_mul] - apply hP.trans - by_cases hi : 4*B < p - · have he : c=true := by - by_contra he - have HE : t=0 := ite_eq_right he - unfold B at hi; rw [HE,show side 0=1 by simp [side]] at hi; linarith - have hI : mainIx j=t := by unfold t lowI; rw [ite_eq_left he] - apply le_trans _ (H.2.2.2 he) - by_cases ha : hazard q true t - · have HF : special j b=1 := - ite_eq_left ⟨(show 0 < mainIx j + 1 by omega),by - change hazard q true (mainIx j+1-1); rw [hI]; exact ha⟩ - rw [HF] - linarith [stripe_pos a (tagM (mainIx j)) j L b] - · have HV : snag (tagM (mainIx j)) u b := by - rw [hI]; refine ⟨⟨z,?_⟩,ha⟩ - change B^2 ≤ energy (q.smaller z) u - have hi' := scalar_energy (q.smaller z) (ht.1.climb.child z) u - change p=‖scalar (q.smaller z) u‖ at hz - rw [←hz] at hi' - nlinarith [show 0 < B from side_pos _] - have HW := hh _ l r hl (hR he) b hm HV - rw [show stripe a (tagM (mainIx j)) j L b=1 from ite_eq_left HW] - have hx : 0 ≤ special j b := flag_nonneg _ _ - linarith - have hk : p ≤ 4*B := le_of_not_gt hi - by_cases h : p < side K - · apply le_trans _ H.1 - exact h.le.trans (side_ant M K (by unfold K; omega)) - have hq : side K ≤ p := le_of_not_gt h - let goodN (i : ℕ) := t ≤ i ∧ side i ≤ p - have HX : goodN K := ⟨by unfold K; omega,hq⟩ - have HF : ∃ i,goodN i := ⟨K,HX⟩ - let i := Nat.find HF - have hE : goodN i := Nat.find_spec HF - have hV : i ≤ K := Nat.find_min' HF HX - have hS : i∈s := Finset.mem_Ico.mpr ⟨hE.1,Nat.lt_succ_of_le hV⟩ - have HI : p ≤ 4*side i := by - by_cases HC : t=i - · unfold B at hk; rwa [← HC] - have HH := Nat.find_min HF (m:=i-1) (by have hi : t < i := lt_of_le_of_ne hE.1 HC; change i-1 < i; omega) - have hx' : p < side (i-1) := lt_of_not_ge fun H => - HH ⟨Nat.le_sub_one_of_lt (lt_of_le_of_ne hE.1 HC), H⟩ - have H := side_succ (i-1) - have HV : 0 < i := by have hi := hE.1; omega - rw [Nat.sub_add_cancel HV] at H - nlinarith [side_pos i] - have hj : i < n := by - by_cases hz : n < highI j M c - · by_contra! he - exact not_lt_of_ge (HI.trans (mul_le_mul_of_nonneg_left (side_ant n i he) (by norm_num))) - (hb hz) - exact (Finset.mem_Ico.mp hS).2.trans_le (le_of_not_gt hz) - apply HI.trans - by_cases hv : hazard q false i - · apply le_trans _ H.2.1 - linarith [pricey b.post j c i (show picks q i j c from ⟨hS,hv⟩)] - · let g := tagL i - have hb : stall u g j L l := hc _ (Finset.mem_Ico.mpr ⟨hE.1,hj⟩) - have hi : betweenS (a g) (billWait g j L) b := - hh g l r hl hb b hm ⟨⟨z,(show side i ≤ ‖scalar _ _‖ from le_trans hE.2 hz.le)⟩,hv⟩ - have hx' : stripe a g j L b=1 := ite_eq_left hi - apply le_trans _ H.2.2.1 - calc - _ = 4*side i*stripe a (tagL i) j L b := by rw [hx']; ring - _ ≤ _ := Finset.single_le_sum (f:=fun i => 4*side i*stripe a (tagL i) j L b) (fun k _ => - mul_nonneg (mul_nonneg (by norm_num) (side_pos _).le) (stripe_pos _ _ _ _ _)) hS - -def tokenCost (j : ℕ) (s : Bool) (L : Path M) := - 1 + 512*subtotal (fun b => mutedR b.post j s) L+ - (∑ i∈playsL j M s,4*side i*(billWait (tagL i) j L:ℝ))+ - (if s then 4*(subtotal (special j) L+(billWait (tagM (mainIx j)) j L:ℝ)) else 0) - -lemma cross_subtotal (l : Path M) (s : Finset ℕ) (f : Beat M → ℕ → ℝ) : - subtotal (fun b => ∑ i∈s,f b i) l=∑ i∈s,subtotal (fun b => f b i) l := by - induction l with - | nil => simp only [subtotal_nil,Finset.sum_const_zero] - | cons b l ih => - rw [subtotal_cons,ih]; simp_rw [subtotal_cons,Finset.sum_add_distrib] - -lemma max_tokens {x : Grid M} (L : Path M) (h : Trip x s L) - (a : Grade→ℕ) (j : ℕ) (p : Bool) - (hh : L.length ≤ M) : - subtotal (token a L j p) L ≤ tokenCost j p L := by - let C i := 4*side i - have h1 : subtotal (fun _ => side M) L ≤ 1 := by - rw [subt_constant] - have H : (L.length:ℝ) ≤ 2^M := by exact_mod_cast (hh.trans Nat.lt_two_pow_self.le) - apply le_trans (mul_le_mul_of_nonneg_left H (side_pos _).le) - rw [side_card] - have h2 (T : Finset ℕ) : - subtotal (fun b => ∑ i∈T,4*side i *stripe a (tagL i) j L b) L ≤ - ∑ i∈T,4*side i*(billWait (tagL i) j L:ℝ) := by - rw [cross_subtotal _ _ (fun b i => _)] - apply Finset.sum_le_sum; intro i _ - rw [subtR]; exact mul_le_mul_of_nonneg_left (stripe_bill _ _ _ _ x h) - (mul_nonneg (by norm_num) (side_pos _).le) - have HE (b : Bool) : - subtotal (fun B : Beat M => 512*mutedR B.post j b) L=512*subtotal (fun B => mutedR B.post j b) L := - subtR _ _ _ - unfold token tokenCost - have h3 := stripe_bill a (tagM (mainIx j)) j L x h - cases p <;> - simp only [Bool.false_eq_true,↓reduceIte,add_zero] - · rw [blend _ (f:=fun _ => _) (g:=fun _ => _),blend _ (f:=fun _=>_) (g:=fun _=>_),HE] - linarith [h2 (playsL j M false)] - · simp_rw [blend _ (f:=fun b => _) (g:=fun b => _)] - rw [HE,subtR _ _ 4,blend _ (f:=special j)] - linarith [h2 (playsL j M true)] - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Duration +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Run Streak + +def lowI (j : ℕ) (b : Bool) := if b then mainIx j else 0 +def highI (j M : ℕ) (b : Bool) := lowI j b+M+1 +def playsL (j M : ℕ) (b : Bool) := Finset.Ico (lowI j b) (highI j M b) +def picks (s : Scene M) (i j : ℕ) (b : Bool) := + i ∈ playsL j M b ∧ hazard s false i + +def priciest (s : Scene M) (j : ℕ) (b : Bool) := + if h : ∃ i, picks s i j b then Nat.find h else lowI j b +def mutedR (s : Scene M) (j : ℕ) (b : Bool) := + surcharge s false (priciest s j b) + +/-- scalar edge charges pay the largest bad level, chosen locally at the pre-bit moment. -/ +lemma pricey (s : Scene M) (j : ℕ) (b : Bool) (i : ℕ) (h : picks s i j b) : + side i ≤ 128*mutedR s j b := by + let n := priciest s j b + have hi : ∃ i, picks s i j b := ⟨i,h⟩ + have hn : n=Nat.find hi := dite_eq_left hi + have HE : hazard s false n := hn.symm ▸ (Nat.find_spec hi).2 + have H : side i ≤ side n := pow_le_pow_of_le_one (by norm_num) (by norm_num) + (hn.le.trans (Nat.find_min' hi h)) + change side n / 128 ≤ edgeW s n at HE + change _ ≤ 128*surcharge s false n + rw [surcharge,ite_eq_left (show hazard s false n from HE)]; linarith + +def special (j : ℕ) (b : Beat M) := (rose (tagM (mainIx j +1)) (0:Grid M)).flag b.post + +def token (a : Grade → ℕ) (L : Path M) (j : ℕ) (s : Bool) (b : Beat M) := + side M+512*mutedR b.post j s+ + (∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b) + + (if s then 4*(special j b+stripe a (tagM (mainIx j)) j L b) else 0) + +lemma token_floor (a : Grade → ℕ) (j : ℕ) (s : Bool) (L : Path M) (b : Beat M) : + side M ≤ token a L j s b ∧ 512*mutedR b.post j s ≤ token a L j s b ∧ + (∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b) ≤ token a L j s b ∧ + (s=true → 4*(special j b+stripe a (tagM (mainIx j)) j L b) ≤ token a L j s b) := by + let z := ∑ i∈playsL j M s,4*side i*stripe a (tagL i) j L b + have hZ : 0 ≤ z := Finset.sum_nonneg fun i _ => mul_nonneg (mul_nonneg (by norm_num) + (side_pos _).le) (stripe_pos _ _ _ _ _) + have hx : 0 ≤ mutedR b.post j s := surcharge_nonneg _ _ _ + have hi : 0 ≤ special j b := flag_nonneg _ _ + have hv := stripe_pos a (tagM (mainIx j)) j L b + have hh := side_pos M + unfold token + unfold z at * + cases s <;> simp only [↓reduceIte,Bool.false_eq_true] <;> + refine ⟨?_,?_,?_,fun H => ?_⟩ <;> first | linarith | contradiction + +lemma tokens_nonneg (a : Grade → ℕ) (L : Path M) (b : Beat M) (j : ℕ) (s : Bool) : + 0 ≤ token a L j s b := (side_pos M).le.trans (token_floor a j s L b).1 + +lemma amp_test {s : Scene M} (h : Climb s) (u : Grid M) : + ‖gain s u‖ ≤ ampU s u ∧ ampU s u ≤ 4 ∧ + ∃ b : Bool, ampU s u=‖scalar (s.smaller b) u‖ := by + have H (b) := (scalar_norm (h.child b) u).trans (h.child b).lift + constructor + · apply (norm_halfcomplex _ _).trans + have h1 : ‖scalar (s.smaller false) u‖ ≤ ampU s u := le_max_left _ _ + have h2 : ‖scalar (s.smaller true) u‖ ≤ ampU s u := le_max_right _ _ + linarith + · unfold ampU + refine ⟨max_le (H false) (H true),?_⟩ + by_cases hh : ‖scalar (s.smaller true) u‖ ≤ ‖scalar (s.smaller false) u‖ + exact ⟨false,max_eq_left hh⟩ + exact ⟨true,max_eq_right (le_of_not_ge hh)⟩ + +lemma side_ant (i j : ℕ) (h : i ≤ j) : side j ≤ side i := + pow_le_pow_of_le_one (by norm_num) (by norm_num) h + +/-- Each unmasked direct jump outside the main intervals can be paid by its amplitude-selected +coupons. During the recursion node n hasn't entered or is unmasked for its scalar level. -/ +lemma direct_point {s : Scene M} {L : Path M} {x u : Grid M} (h : Trip x s L) + (j : ℕ) (c : Bool) (a : Grade → ℕ) (hh : shades a u L j) + (n : ℕ) (l r : Path M) (b : Beat M) + (hR : c=true → stall u (tagM (mainIx j)) j L l) + (hc : ∀ i ∈ Finset.Ico (lowI j c) n, stall u (tagL i) j L l) + (_hn : n ∈ Set.Icc (lowI j c) (highI j M c)) (hl : L=l++r) + (hm : b∈l) (hb : n < highI j M c → 4*side n < ampU b.post u) : + ‖proceeds x u b‖ ≤ token a L j c b := by + let t := lowI j c; let B := side t; let q := b.post; let s := playsL j M c + let K := t+M + let v := token a L j c b + let p := ampU q u + have ht := trip_attend h b (by rw [hl]; simp [hm]) + have H := token_floor a j c L b + change _ ≤ v ∧ _ ≤ v ∧ _ ≤ v ∧ (c=true→_ ≤ v) at H + obtain ⟨hP,hB,z,hz⟩ := amp_test ht.1.climb u + change p ≤ 4 at hB + change p=_ at hz + rw [proceeds,norm_scaleC,ht.2.2.1,one_mul] + apply hP.trans + by_cases hi : 4*B < p + · have he : c=true := by + by_contra he + have HE : t=0 := ite_eq_right he + unfold B at hi; rw [HE,show side 0=1 by simp [side]] at hi; linarith + have hI : mainIx j=t := by unfold t lowI; rw [ite_eq_left he] + apply le_trans _ (H.2.2.2 he) + by_cases ha : hazard q true t + · have HF : special j b=1 := + ite_eq_left ⟨(show 0 < mainIx j + 1 by omega),by + change hazard q true (mainIx j+1-1); rw [hI]; exact ha⟩ + rw [HF] + linarith [stripe_pos a (tagM (mainIx j)) j L b] + · have HV : snag (tagM (mainIx j)) u b := by + rw [hI]; refine ⟨⟨z,?_⟩,ha⟩ + change B^2 ≤ energy (q.smaller z) u + have hi' := scalar_energy (q.smaller z) (ht.1.climb.child z) u + change p=‖scalar (q.smaller z) u‖ at hz + rw [←hz] at hi' + nlinarith [show 0 < B from side_pos _] + have HW := hh _ l r hl (hR he) b hm HV + rw [show stripe a (tagM (mainIx j)) j L b=1 from ite_eq_left HW] + have hx : 0 ≤ special j b := flag_nonneg _ _ + linarith + have hk : p ≤ 4*B := le_of_not_gt hi + by_cases h : p < side K + · apply le_trans _ H.1 + exact h.le.trans (side_ant M K (by unfold K; omega)) + have hq : side K ≤ p := le_of_not_gt h + let goodN (i : ℕ) := t ≤ i ∧ side i ≤ p + have HX : goodN K := ⟨by unfold K; omega,hq⟩ + have HF : ∃ i,goodN i := ⟨K,HX⟩ + let i := Nat.find HF + have hE : goodN i := Nat.find_spec HF + have hV : i ≤ K := Nat.find_min' HF HX + have hS : i∈s := Finset.mem_Ico.mpr ⟨hE.1,Nat.lt_succ_of_le hV⟩ + have HI : p ≤ 4*side i := by + by_cases HC : t=i + · unfold B at hk; rwa [← HC] + have HH := Nat.find_min HF (m:=i-1) (by have hi : t < i := lt_of_le_of_ne hE.1 HC; change i-1 < i; omega) + have hx' : p < side (i-1) := lt_of_not_ge fun H => + HH ⟨Nat.le_sub_one_of_lt (lt_of_le_of_ne hE.1 HC), H⟩ + have H := side_succ (i-1) + have HV : 0 < i := by have hi := hE.1; omega + rw [Nat.sub_add_cancel HV] at H + nlinarith [side_pos i] + have hj : i < n := by + by_cases hz : n < highI j M c + · by_contra! he + exact not_lt_of_ge (HI.trans (mul_le_mul_of_nonneg_left (side_ant n i he) (by norm_num))) + (hb hz) + exact (Finset.mem_Ico.mp hS).2.trans_le (le_of_not_gt hz) + apply HI.trans + by_cases hv : hazard q false i + · apply le_trans _ H.2.1 + linarith [pricey b.post j c i (show picks q i j c from ⟨hS,hv⟩)] + · let g := tagL i + have hb : stall u g j L l := hc _ (Finset.mem_Ico.mpr ⟨hE.1,hj⟩) + have hi : betweenS (a g) (billWait g j L) b := + hh g l r hl hb b hm ⟨⟨z,(show side i ≤ ‖scalar _ _‖ from le_trans hE.2 hz.le)⟩,hv⟩ + have hx' : stripe a g j L b=1 := ite_eq_left hi + apply le_trans _ H.2.2.1 + calc + _ = 4*side i*stripe a (tagL i) j L b := by rw [hx']; ring + _ ≤ _ := Finset.single_le_sum (f:=fun i => 4*side i*stripe a (tagL i) j L b) (fun k _ => + mul_nonneg (mul_nonneg (by norm_num) (side_pos _).le) (stripe_pos _ _ _ _ _)) hS + +def tokenCost (j : ℕ) (s : Bool) (L : Path M) := + 1 + 512*subtotal (fun b => mutedR b.post j s) L+ + (∑ i∈playsL j M s,4*side i*(billWait (tagL i) j L:ℝ))+ + (if s then 4*(subtotal (special j) L+(billWait (tagM (mainIx j)) j L:ℝ)) else 0) + +lemma cross_subtotal (l : Path M) (s : Finset ℕ) (f : Beat M → ℕ → ℝ) : + subtotal (fun b => ∑ i∈s,f b i) l=∑ i∈s,subtotal (fun b => f b i) l := by + induction l with + | nil => simp only [subtotal_nil,Finset.sum_const_zero] + | cons b l ih => + rw [subtotal_cons,ih]; simp_rw [subtotal_cons,Finset.sum_add_distrib] + +lemma max_tokens {x : Grid M} (L : Path M) (h : Trip x s L) + (a : Grade→ℕ) (j : ℕ) (p : Bool) + (hh : L.length ≤ M) : + subtotal (token a L j p) L ≤ tokenCost j p L := by + let C i := 4*side i + have h1 : subtotal (fun _ => side M) L ≤ 1 := by + rw [subt_constant] + have H : (L.length:ℝ) ≤ 2^M := by exact_mod_cast (hh.trans Nat.lt_two_pow_self.le) + apply le_trans (mul_le_mul_of_nonneg_left H (side_pos _).le) + rw [side_card] + have h2 (T : Finset ℕ) : + subtotal (fun b => ∑ i∈T,4*side i *stripe a (tagL i) j L b) L ≤ + ∑ i∈T,4*side i*(billWait (tagL i) j L:ℝ) := by + rw [cross_subtotal _ _ (fun b i => _)] + apply Finset.sum_le_sum; intro i _ + rw [subtR]; exact mul_le_mul_of_nonneg_left (stripe_bill _ _ _ _ x h) + (mul_nonneg (by norm_num) (side_pos _).le) + have HE (b : Bool) : + subtotal (fun B : Beat M => 512*mutedR B.post j b) L=512*subtotal (fun B => mutedR B.post j b) L := + subtR _ _ _ + unfold token tokenCost + have h3 := stripe_bill a (tagM (mainIx j)) j L x h + cases p <;> + simp only [Bool.false_eq_true,↓reduceIte,add_zero] + · rw [blend _ (f:=fun _ => _) (g:=fun _ => _),blend _ (f:=fun _=>_) (g:=fun _=>_),HE] + linarith [h2 (playsL j M false)] + · simp_rw [blend _ (f:=fun b => _) (g:=fun b => _)] + rw [HE,subtR _ _ 4,blend _ (f:=special j)] + linarith [h2 (playsL j M true)] + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Assembly/Verdict.lean b/lean/OAI/Analysis/FourierLLogL/Assembly/Verdict.lean index 5a051e3086211d53002df0659ea08151465902a9..c0426768e6be4428c304e9bb53ab6f8fb6a58de6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Assembly/Verdict.lean +++ b/lean/OAI/Analysis/FourierLLogL/Assembly/Verdict.lean @@ -1,215 +1,215 @@ -import OAI.Analysis.FourierLLogL.Assembly.Chill -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace CastShow -open Troupe - -def holders (j : ℕ) := Finset.Ico 0 (mainIx j+2) -def picker (j M : ℕ) : Policy M := fun s => priciest s j true -def bouts (i _j : ℕ) (s : Scene M) := surcharge s true (i-1) -def aura (i : ℕ) (e : Event j M) := edgeFee (fun _ => i-1) true e - -def upperFare (e : Event j M) := - 10^140*(height j/Thermal.H j)*beginFee e+ - 512*edgeFee (picker j M) false e+ - 4*10^20*infoFee e + 10^40*Thermal.H j^40* (∑ i∈holders j, aura i e) - -lemma stamp_upper (s : Scene M) (j : ℕ) : - stamp s j ≤ 512*mutedR s j true + 10^20*fax s+ - 10^40*Thermal.H j^40*(∑ i∈holders j,bouts i j s) := by - let R := holders j; let n := mainIx j; let a := Thermal.H j - let f (i) := bouts i j s - have he : 1 ≤ a := H_ge j - have hp : 0 < a := H_pos j - have hi (i) : 0 ≤ f i := surcharge_nonneg _ _ _ - have HR : 0 ≤ ∑ i∈R,f i := Finset.sum_nonneg fun i _ => hi _ - let K := Finset.Ico 0 (n+1) - have HH : - ∑ i∈K, parody s i/depthRate i ≤ (10^6*a^40)*∑ i∈R, f i := by - have he' : 0 ≤ 10^6*a^40 := by positivity - apply le_trans (b:=(10^6*a^40)*∑ i∈K,f i) - · rw [Finset.mul_sum] - apply Finset.sum_le_sum; intro i hv - have hI : i ≤ n := by - have hx : i < n+1 := (Finset.mem_Ico.mp hv).2; omega - have Hx : parody s i ≤ 10^6*a^32*f i := parody_fee s j i (hI.trans (Nat.le_add_right _ _)) - have Hv := depth_rate_fee i j hI - rw [div_eq_mul_one_div] - apply le_trans (mul_le_mul Hx Hv (by have := (rate_easy i).1; positivity) - (by have := hi i; positivity)); change 10^6*a^32*f i*a^8 ≤ _ - exact le_of_eq (by ring) - · apply mul_le_mul_of_nonneg_left _ he' - refine Finset.sum_le_sum_of_subset_of_nonneg ?_ fun i _ _ => hi i - unfold K R holders n; intro i hi - obtain ⟨ha,hb⟩ := Finset.mem_Ico.mp hi; exact Finset.mem_Ico.mpr ⟨ha,by omega⟩ - have HK : f (n+1) ≤ ∑ i∈R,f i := Finset.single_le_sum (fun i _ => hi i) - (Finset.mem_Ico.mpr ⟨by omega,by unfold n; omega⟩) - have HT : parody s (n+1) ≤ (10^6*a^40)*∑ i∈R, f i := by - apply (parody_fee s j (n+1) le_rfl).trans - change 10^6*a^32*f (n+1) ≤ _ - gcongr <;> first| omega| apply hi - unfold stamp stampAux - change _ + (4*parody s (n+1)+10^20*(fax s+∑ i∈K,_)) ≤ _+_+10^40*a^40*(∑ i∈R,f i) - nlinarith [mul_nonneg (show 0 ≤ a^40 by positivity) HR] - -private lemma bound_bit (s : Scene M) (k : Bool) - (h : Safe s j J f) (he : s.live=true) (hp : work s j J f) : - let b := Event.march s (Turn.move (j:=j)) k - billOf b ≤ upperFare b := by - intro b - unfold b upperFare - let r := s.norm/s.mass - have ht : normStamp s j=r*stamp s j := ite_eq_left he - have h1 : edgeFee (picker j M) false b=r*mutedR s j true := by - unfold b edgeFee toll - simp only [true_and,ite_eq_left hp.2.2, r, picker,mutedR] - have h2 (i) : aura i b=r*bouts i j s := by - unfold aura b edgeFee toll - simp only [true_and,ite_eq_left hp.2.2,r,bouts] - have HH : 0 ≤ r := div_nonneg h.norm_ok h.mass_lower - change normStamp s j ≤ _*0+512*edgeFee (picker j M) false b+ - 4*10^20*(1/4*r*fax s)+ 10^40* Thermal.H j^40*(∑ i∈holders j,aura i b) - simp_rw [h2,h1,ht,← Finset.mul_sum] - apply le_trans (mul_le_mul_of_nonneg_left (stamp_upper s j) HH) - exact le_of_eq (by ring) - -lemma short_trace_bound (t : Play j M s) (h : t.Follows J f) (he : s.live=true) : - let v := trace false t - v.flatFee billOf ≤ v.flatFee upperFare := by - induction t with - | rest s => simp only [trace,flat_finish,le_refl] - | freeze s t ih => - intro v - change flatFee (.finish s true : Road j M s) billOf ≤ flatFee (.finish s true) upperFare - rw [flat_finish,flat_finish] - | ticket s t ih => - simp only [trace,flat_push] - apply add_le_add _ (ih h.2.2.2.2 he) - unfold upperFare billOf beginFee edgeFee infoFee gateTax - have H : ∀ i, aura i (.pace s .renew : Event j M)=0 := fun i => rfl - simp only [H,reset,he,↓reduceIte,Finset.sum_const_zero] - exact le_of_eq (by ring) - | jump s l r il ir => - simp only [trace, flat_fork _ _ (traced _ false h)] - apply Ex_mono; intro b - apply add_le_add (bound_bit s b h.1 he h.2.1) - cases b - exact il h.2.2.2.1 he - exact ir h.2.2.2.2 he - | attempt s u l r il ir => - simp only [trace,flat_fork _ _ (traced _ false h)] - apply Ex_mono; intro b - apply add_le_add - · have H : ∀ i, aura i (.march s (.ask u) b)=0 := fun i => rfl - simp [upperFare,H,beginFee,edgeFee,infoFee,billOf] - · cases b; exact il h.2.2.2.2.1 he; exact ir h.2.2.2.2.2 he - -lemma linear_fee (t : Road j M s) (A B : Event j M → ℝ) (a : ℝ) : - flatFee t (fun i => A i+B i)=flatFee t A+flatFee t B ∧ - flatFee t (fun i => a*A i)=a*flatFee t A := by - have HE (l : List (Event j M)) : - (l.map (fun i => A i+B i)).sum=(l.map A).sum+(l.map B).sum ∧ - (l.map (fun i => a*A i)).sum=a*(l.map A).sum := by - induction l with - | nil => simp - | cons b l ih => - simp only [List.map_cons,List.sum_cons, ih.1, ih.2] - constructor <;> ring - constructor - · unfold flatFee expHike Journal.fee; simp_rw [(HE _).1, Ex_add] - unfold flatFee expHike Journal.fee; simp_rw [(HE _).2, Ex_lmul] - -lemma sum_fee (t : Road j M s) (p : Finset ℕ) (f : ℕ → Event j M → ℝ) : - t.flatFee (fun i => ∑ z∈p,f z i)=∑ z∈p,t.flatFee (f z) := by - induction p using Finset.induction_on with - | empty => - have H (s : Journal j M) : s.fee (fun _ => 0)=0 := by - unfold Journal.fee; induction s.events with - | nil => simp - | cons a l ih => simp - simp only [Finset.sum_empty,flatFee,expHike,H,Ex_const] - | @insert a p ha ih => - simp only [Finset.sum_insert ha] - rw [(linear_fee t (f a) (fun i => _) 0).1,ih] - -lemma upper_total : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, - (∀ x,f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Road j M s, t.Trusted J f → 1/Thermal.H j ≤ s.mass → - t.flatFee upperFare ≤ 10^190*height j := by - filter_upwards [global_edges, fisher_bound, final_coarse,hpoly 1 42] with j he hq ht hk - M J f hF s t h hp - let a := Thermal.H j; let d := height j; let phi := t.flatFee - let x := limitC - obtain ⟨H1,H2⟩ := he M J f hF t h hp - have H₃ := hq M J f hF t h hp - have H₄ := (ht M J s f t h hp).2.2 rfl - let R := holders j - have HH : ∑ i∈R,phi (aura i) ≤ 300000*x*a^2 := by - calc - _ ≤ ∑ _i∈R,10000*x*a := Finset.sum_le_sum fun i hi => by - have hh := (Finset.mem_Ico.mp hi).2 - apply H2 - unfold testIx; omega - _ ≤ _ := by - unfold R holders - simp only [Finset.sum_const,Nat.card_Ico,Nat.sub_zero,nsmul_eq_mul] - have he : ((mainIx j:ℕ):ℝ) ≤ 19*a := (main_bounds j (Nat.le_add_right _ 3)).2.1 - push_cast - have HX : 1 ≤ a := H_ge _ - unfold x limitC; nlinarith - let f₁ := edgeFee (picker j M) false (j:=j) - let C := 10^140*(d/a) - let Y := 10^40*a^40 - have HY : 0 ≤ C ∧ 0 ≤ Y := by - have hi : 0 < a := H_pos _ - have hh : 0 < d := d_pos j - unfold C Y; constructor <;> positivity - - have HE (A B : Event j M → ℝ) : phi (fun i => A i+B i)=phi A+phi B := (linear_fee t A B 1).1 - have HM (A : Event j M → ℝ) (c : ℝ) : phi (fun i => c*A i)=c*phi A := (linear_fee t A A c).2 - have H := H1 (picker j M) - unfold upperFare - change phi (fun e => C*beginFee e+512*f₁ e+4*10^20*infoFee e + Y*(∑ i∈R,aura i e)) ≤ _ - simp_rw [HE (fun _ => _) (fun _ => _),HM] - rw [show phi (fun e => ∑ i∈R,aura i e) = ∑ i∈R,phi (aura i) from sum_fee t R aura] - have Ha : 1 ≤ a := H_ge _ - have hd : 0 ≤ d := (d_pos j).le - change phi f₁ ≤ 1000*d at H - change phi infoFee ≤ x*d at H₃ - change phi beginFee ≤ 100*x*a at H₄ - have HZ : a^42 ≤ d := by simpa using hk - have HC : C*(100*x*a) ≤ 10^180*d := by - have He : 0 priciest s j true +def bouts (i _j : ℕ) (s : Scene M) := surcharge s true (i-1) +def aura (i : ℕ) (e : Event j M) := edgeFee (fun _ => i-1) true e + +def upperFare (e : Event j M) := + 10^140*(height j/Thermal.H j)*beginFee e+ + 512*edgeFee (picker j M) false e+ + 4*10^20*infoFee e + 10^40*Thermal.H j^40* (∑ i∈holders j, aura i e) + +lemma stamp_upper (s : Scene M) (j : ℕ) : + stamp s j ≤ 512*mutedR s j true + 10^20*fax s+ + 10^40*Thermal.H j^40*(∑ i∈holders j,bouts i j s) := by + let R := holders j; let n := mainIx j; let a := Thermal.H j + let f (i) := bouts i j s + have he : 1 ≤ a := H_ge j + have hp : 0 < a := H_pos j + have hi (i) : 0 ≤ f i := surcharge_nonneg _ _ _ + have HR : 0 ≤ ∑ i∈R,f i := Finset.sum_nonneg fun i _ => hi _ + let K := Finset.Ico 0 (n+1) + have HH : + ∑ i∈K, parody s i/depthRate i ≤ (10^6*a^40)*∑ i∈R, f i := by + have he' : 0 ≤ 10^6*a^40 := by positivity + apply le_trans (b:=(10^6*a^40)*∑ i∈K,f i) + · rw [Finset.mul_sum] + apply Finset.sum_le_sum; intro i hv + have hI : i ≤ n := by + have hx : i < n+1 := (Finset.mem_Ico.mp hv).2; omega + have Hx : parody s i ≤ 10^6*a^32*f i := parody_fee s j i (hI.trans (Nat.le_add_right _ _)) + have Hv := depth_rate_fee i j hI + rw [div_eq_mul_one_div] + apply le_trans (mul_le_mul Hx Hv (by have := (rate_easy i).1; positivity) + (by have := hi i; positivity)); change 10^6*a^32*f i*a^8 ≤ _ + exact le_of_eq (by ring) + · apply mul_le_mul_of_nonneg_left _ he' + refine Finset.sum_le_sum_of_subset_of_nonneg ?_ fun i _ _ => hi i + unfold K R holders n; intro i hi + obtain ⟨ha,hb⟩ := Finset.mem_Ico.mp hi; exact Finset.mem_Ico.mpr ⟨ha,by omega⟩ + have HK : f (n+1) ≤ ∑ i∈R,f i := Finset.single_le_sum (fun i _ => hi i) + (Finset.mem_Ico.mpr ⟨by omega,by unfold n; omega⟩) + have HT : parody s (n+1) ≤ (10^6*a^40)*∑ i∈R, f i := by + apply (parody_fee s j (n+1) le_rfl).trans + change 10^6*a^32*f (n+1) ≤ _ + gcongr <;> first| omega| apply hi + unfold stamp stampAux + change _ + (4*parody s (n+1)+10^20*(fax s+∑ i∈K,_)) ≤ _+_+10^40*a^40*(∑ i∈R,f i) + nlinarith [mul_nonneg (show 0 ≤ a^40 by positivity) HR] + +private lemma bound_bit (s : Scene M) (k : Bool) + (h : Safe s j J f) (he : s.live=true) (hp : work s j J f) : + let b := Event.march s (Turn.move (j:=j)) k + billOf b ≤ upperFare b := by + intro b + unfold b upperFare + let r := s.norm/s.mass + have ht : normStamp s j=r*stamp s j := ite_eq_left he + have h1 : edgeFee (picker j M) false b=r*mutedR s j true := by + unfold b edgeFee toll + simp only [true_and,ite_eq_left hp.2.2, r, picker,mutedR] + have h2 (i) : aura i b=r*bouts i j s := by + unfold aura b edgeFee toll + simp only [true_and,ite_eq_left hp.2.2,r,bouts] + have HH : 0 ≤ r := div_nonneg h.norm_ok h.mass_lower + change normStamp s j ≤ _*0+512*edgeFee (picker j M) false b+ + 4*10^20*(1/4*r*fax s)+ 10^40* Thermal.H j^40*(∑ i∈holders j,aura i b) + simp_rw [h2,h1,ht,← Finset.mul_sum] + apply le_trans (mul_le_mul_of_nonneg_left (stamp_upper s j) HH) + exact le_of_eq (by ring) + +lemma short_trace_bound (t : Play j M s) (h : t.Follows J f) (he : s.live=true) : + let v := trace false t + v.flatFee billOf ≤ v.flatFee upperFare := by + induction t with + | rest s => simp only [trace,flat_finish,le_refl] + | freeze s t ih => + intro v + change flatFee (.finish s true : Road j M s) billOf ≤ flatFee (.finish s true) upperFare + rw [flat_finish,flat_finish] + | ticket s t ih => + simp only [trace,flat_push] + apply add_le_add _ (ih h.2.2.2.2 he) + unfold upperFare billOf beginFee edgeFee infoFee gateTax + have H : ∀ i, aura i (.pace s .renew : Event j M)=0 := fun i => rfl + simp only [H,reset,he,↓reduceIte,Finset.sum_const_zero] + exact le_of_eq (by ring) + | jump s l r il ir => + simp only [trace, flat_fork _ _ (traced _ false h)] + apply Ex_mono; intro b + apply add_le_add (bound_bit s b h.1 he h.2.1) + cases b + exact il h.2.2.2.1 he + exact ir h.2.2.2.2 he + | attempt s u l r il ir => + simp only [trace,flat_fork _ _ (traced _ false h)] + apply Ex_mono; intro b + apply add_le_add + · have H : ∀ i, aura i (.march s (.ask u) b)=0 := fun i => rfl + simp [upperFare,H,beginFee,edgeFee,infoFee,billOf] + · cases b; exact il h.2.2.2.2.1 he; exact ir h.2.2.2.2.2 he + +lemma linear_fee (t : Road j M s) (A B : Event j M → ℝ) (a : ℝ) : + flatFee t (fun i => A i+B i)=flatFee t A+flatFee t B ∧ + flatFee t (fun i => a*A i)=a*flatFee t A := by + have HE (l : List (Event j M)) : + (l.map (fun i => A i+B i)).sum=(l.map A).sum+(l.map B).sum ∧ + (l.map (fun i => a*A i)).sum=a*(l.map A).sum := by + induction l with + | nil => simp + | cons b l ih => + simp only [List.map_cons,List.sum_cons, ih.1, ih.2] + constructor <;> ring + constructor + · unfold flatFee expHike Journal.fee; simp_rw [(HE _).1, Ex_add] + unfold flatFee expHike Journal.fee; simp_rw [(HE _).2, Ex_lmul] + +lemma sum_fee (t : Road j M s) (p : Finset ℕ) (f : ℕ → Event j M → ℝ) : + t.flatFee (fun i => ∑ z∈p,f z i)=∑ z∈p,t.flatFee (f z) := by + induction p using Finset.induction_on with + | empty => + have H (s : Journal j M) : s.fee (fun _ => 0)=0 := by + unfold Journal.fee; induction s.events with + | nil => simp + | cons a l ih => simp + simp only [Finset.sum_empty,flatFee,expHike,H,Ex_const] + | @insert a p ha ih => + simp only [Finset.sum_insert ha] + rw [(linear_fee t (f a) (fun i => _) 0).1,ih] + +lemma upper_total : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, + (∀ x,f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, t.Trusted J f → 1/Thermal.H j ≤ s.mass → + t.flatFee upperFare ≤ 10^190*height j := by + filter_upwards [global_edges, fisher_bound, final_coarse,hpoly 1 42] with j he hq ht hk + M J f hF s t h hp + let a := Thermal.H j; let d := height j; let phi := t.flatFee + let x := limitC + obtain ⟨H1,H2⟩ := he M J f hF t h hp + have H₃ := hq M J f hF t h hp + have H₄ := (ht M J s f t h hp).2.2 rfl + let R := holders j + have HH : ∑ i∈R,phi (aura i) ≤ 300000*x*a^2 := by + calc + _ ≤ ∑ _i∈R,10000*x*a := Finset.sum_le_sum fun i hi => by + have hh := (Finset.mem_Ico.mp hi).2 + apply H2 + unfold testIx; omega + _ ≤ _ := by + unfold R holders + simp only [Finset.sum_const,Nat.card_Ico,Nat.sub_zero,nsmul_eq_mul] + have he : ((mainIx j:ℕ):ℝ) ≤ 19*a := (main_bounds j (Nat.le_add_right _ 3)).2.1 + push_cast + have HX : 1 ≤ a := H_ge _ + unfold x limitC; nlinarith + let f₁ := edgeFee (picker j M) false (j:=j) + let C := 10^140*(d/a) + let Y := 10^40*a^40 + have HY : 0 ≤ C ∧ 0 ≤ Y := by + have hi : 0 < a := H_pos _ + have hh : 0 < d := d_pos j + unfold C Y; constructor <;> positivity + + have HE (A B : Event j M → ℝ) : phi (fun i => A i+B i)=phi A+phi B := (linear_fee t A B 1).1 + have HM (A : Event j M → ℝ) (c : ℝ) : phi (fun i => c*A i)=c*phi A := (linear_fee t A A c).2 + have H := H1 (picker j M) + unfold upperFare + change phi (fun e => C*beginFee e+512*f₁ e+4*10^20*infoFee e + Y*(∑ i∈R,aura i e)) ≤ _ + simp_rw [HE (fun _ => _) (fun _ => _),HM] + rw [show phi (fun e => ∑ i∈R,aura i e) = ∑ i∈R,phi (aura i) from sum_fee t R aura] + have Ha : 1 ≤ a := H_ge _ + have hd : 0 ≤ d := (d_pos j).le + change phi f₁ ≤ 1000*d at H + change phi infoFee ≤ x*d at H₃ + change phi beginFee ≤ 100*x*a at H₄ + have HZ : a^42 ≤ d := by simpa using hk + have HC : C*(100*x*a) ≤ 10^180*d := by + have He : 0 simp_all only [k,Bool.false_eq_true,↓reduceIte,le_refl] - have HH : P.Ex (fun i => y i^2) ≤ (c*a)^2 := h2 M J s f t h hh - let L := fun i => w i*y i - let K := fun i => w i*y i^2 - have hw (i) : w i ≤ 1 ∧ w i^2=w i := by unfold w; split <;> norm_num - have H : P.Ex K ≤ (c*a)^2 := le_trans (Ex_mono _ fun i => - mul_le_of_le_one_left (sq_nonneg (y i)) (hw _).1) HH - have HC : P.Ex L ≤ c := by - have Heq := Law.sq_Ex_mul P w y - simp_rw [(hw _).2] at Heq - have HJ : P.Ex w * P.Ex (fun i => y i^2) ≤ (1/a^10)*(c*a)^2 := - mul_le_mul HX HH (Ex_nonneg _ (fun _ => sq_nonneg _)) (by positivity) - have HF : (1/a^10)*(c*a)^2 ≤ c^2 := by - apply le_trans (b:=(1/a^2)*(c*a)^2) - · apply mul_le_mul_of_nonneg_right _ (sq_nonneg _) - exact one_div_le_one_div_of_le (by positivity) (pow_le_pow_right₀ hp (by omega)) - exact le_of_eq (by field_simp) - change (P.Ex L)^2 ≤ _ at Heq - have hP : 0 < c := by unfold c limitC; norm_num - nlinarith - have heq (x : Grid M) : coldLump (k x) = - 10^150* ((a+1)*(d+2)*w x+(a+d+3)*L x+K x) := by - unfold coldLump chargeStatic - unfold L K w y - split_ifs <;> unfold d a <;> ring - unfold coldFee expHike - change P.Ex (fun i => coldLump (k i)) ≤ 10^220*d - simp_rw [heq,Ex_lmul,Ex_add,Ex_lmul] - have HR : a ≤ d := le_trans (by simpa using pow_le_pow_right₀ hp (by omega : 1≤4)) he - have Hs : a^2 ≤ d := le_trans (pow_le_pow_right₀ hp (by omega)) he - have HS : (a+1)*(d+2)*P.Ex w ≤ 6*d := - le_trans (mul_le_mul_of_nonneg_left HX (by positivity)) (by - have H : 1/a^10 ≤ 1/a := one_div_le_one_div_of_le ha (by simpa using pow_le_pow_right₀ hp (by omega : 1≤10)) - apply (mul_le_mul_of_nonneg_left H (show 0 ≤ (a+1)*(d+2) by positivity)).trans - rw [mul_one_div] - apply (div_le_iff₀ ha).mpr; nlinarith) - have HT : (a+d+3)*P.Ex L ≤ 5*d*c := - le_trans (mul_le_mul_of_nonneg_left HC (by positivity)) (by - have H : 0 ≤ c := by unfold c limitC; norm_num - nlinarith) - unfold c limitC at * - nlinarith - -lemma grossOur : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → - ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Play j M s, t.Follows J f → 1/Thermal.H j ≤ s.mass → - yieldA t ≤ s.mass*(10^221*height j) := by - filter_upwards [cost_overall,burn_rate,upper_total,frostCompare] with j h1 h2 h3 h4 M J hC f hf s t h hm - have H := traced t false h - have HP := (Play.first h).mass_lower - let v := trace false t - have hE := h1 M J f s t h hf - rw [due_restart rfl,add_zero] at hE - apply le_trans hE (mul_le_mul_of_nonneg_left ?_ HP) - have hc := h2 M J hC f hf v H hm - have he := h3 M J f hf v H hm - have HH := short_trace_bound t h rfl - apply (h4 M J f s t h rfl).trans - change v.flatFee billOf ≤ v.flatFee upperFare at HH - change v.flatFee billOf+coldFee v ≤ _ - linarith [d_pos j] - -private lemma scale_drought (m : ℝ) (hm : 0 < m) (h : m ≤ 1) : - m*drought m ≤ 4 ∧ m*(drought m)^2 ≤ 4 := by - have hh' : drought m= -log m := max_eq_left (by linarith [log_nonpos hm.le h]) - let x := drought m - have heq : m= (exp x)⁻¹ := by unfold x; rw [hh',exp_neg,inv_inv,exp_log hm] - have H : x^2 /4 ≤ exp x := by - have h0 : exp x=(exp (x/2))^2 := by rw [pow_two,←exp_add]; ring_nf - have hi := add_one_le_exp (x/2) - rw [h0] - nlinarith [show 0 ≤ x from drought_pos m] - have HH := mul_le_mul_of_nonneg_left H hm.le - have Hx : m*exp x=1 := by rw [heq,inv_mul_cancel₀ (exp_pos x).ne'] - change m*x≤4 ∧ m*x^2≤4 - constructor - · nlinarith [mul_nonneg hm.le (sq_nonneg (x-1))] - linarith - -lemma grossDry : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, - (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f false - ∀ t : Play j M s, t.Follows J f → s.mass ≤ 1/Thermal.H j → - yieldA t ≤ 10^152*height j := by - filter_upwards [cost_overall,chill_descendants,hpoly 1 1] with j h1 h2 h3 M J f hf s t h hm - have H := h1 _ _ f s t h hf - rw [due_restart rfl,add_zero] at H - apply H.trans - let m := s.mass; let d := height j; let a := Thermal.H j - have hh : 0 ≤ m := (Play.first h).mass_lower - have ha : 1 ≤ a := H_ge _ - have hd : 1 ≤ d := one_le_pow₀ (by norm_num) - have hp : a ≤ d := by simpa using h3 - have hz : 0 < a := by linarith - change m≤1/a at hm - have hi : m*a ≤ 1 := (le_div_iff₀ hz).mp hm - have hr : m ≤ 1 := by nlinarith - change m*_ ≤ 10^152*d - rcases eq_or_lt_of_le hh with hh|hh - · rw [←hh,zero_mul]; positivity - apply le_trans (mul_le_mul_of_nonneg_left (h2 M J f s t h rfl rfl hh) hh.le) - let x := drought m - change m*(10^150*(a+1+x)*(d+2+x)) ≤ _ - have he : m*x ≤ 4 ∧ m*x^2 ≤ 4 := scale_drought m hh hr - nlinarith [mul_le_mul_of_nonneg_right hi (show 0 ≤ d by linarith),mul_le_mul_of_nonneg_right he.1 - (show 0 ≤ a by linarith),mul_le_mul_of_nonneg_right he.1 (show 0 ≤ d by linarith)] - -end CastShow - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Assembly.Verdict +import OAI.Analysis.FourierLLogL.Procedure.Rare +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace CastShow +open Troupe + +lemma burn_rate : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → + ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, Trusted J f t → 1/Thermal.H j ≤ s.mass → + coldFee t ≤ 10^220*height j := by + filter_upwards [rare,log_mass_budget,hpoly 1 4] with j h1 h2 hd M J hm f hf s t h hh + let a := Thermal.H j; let d := height j; let P := s.actor + let c := 10*limitC + have hp : 1 ≤ a := H_ge j + have ha : 0 < a := H_pos j + have he : a^4 ≤ d := by simpa using hd + have hD : 1 ≤ d := one_le_pow₀ (by norm_num) + let k (x : Grid M) := t.hike x + let w (x : Grid M) := if (k x).froze then (1:ℝ) else 0 + let y (x : Grid M) := drought (k x).done.mass + have HX : P.Ex w ≤ 1/a^10 := by + apply le_trans _ (h1 M J hm f hf t h hh) + apply Ex_mono; intro x; unfold w + cases he : (k x).froze <;> simp_all only [k,Bool.false_eq_true,↓reduceIte,le_refl] + have HH : P.Ex (fun i => y i^2) ≤ (c*a)^2 := h2 M J s f t h hh + let L := fun i => w i*y i + let K := fun i => w i*y i^2 + have hw (i) : w i ≤ 1 ∧ w i^2=w i := by unfold w; split <;> norm_num + have H : P.Ex K ≤ (c*a)^2 := le_trans (Ex_mono _ fun i => + mul_le_of_le_one_left (sq_nonneg (y i)) (hw _).1) HH + have HC : P.Ex L ≤ c := by + have Heq := Law.sq_Ex_mul P w y + simp_rw [(hw _).2] at Heq + have HJ : P.Ex w * P.Ex (fun i => y i^2) ≤ (1/a^10)*(c*a)^2 := + mul_le_mul HX HH (Ex_nonneg _ (fun _ => sq_nonneg _)) (by positivity) + have HF : (1/a^10)*(c*a)^2 ≤ c^2 := by + apply le_trans (b:=(1/a^2)*(c*a)^2) + · apply mul_le_mul_of_nonneg_right _ (sq_nonneg _) + exact one_div_le_one_div_of_le (by positivity) (pow_le_pow_right₀ hp (by omega)) + exact le_of_eq (by field_simp) + change (P.Ex L)^2 ≤ _ at Heq + have hP : 0 < c := by unfold c limitC; norm_num + nlinarith + have heq (x : Grid M) : coldLump (k x) = + 10^150* ((a+1)*(d+2)*w x+(a+d+3)*L x+K x) := by + unfold coldLump chargeStatic + unfold L K w y + split_ifs <;> unfold d a <;> ring + unfold coldFee expHike + change P.Ex (fun i => coldLump (k i)) ≤ 10^220*d + simp_rw [heq,Ex_lmul,Ex_add,Ex_lmul] + have HR : a ≤ d := le_trans (by simpa using pow_le_pow_right₀ hp (by omega : 1≤4)) he + have Hs : a^2 ≤ d := le_trans (pow_le_pow_right₀ hp (by omega)) he + have HS : (a+1)*(d+2)*P.Ex w ≤ 6*d := + le_trans (mul_le_mul_of_nonneg_left HX (by positivity)) (by + have H : 1/a^10 ≤ 1/a := one_div_le_one_div_of_le ha (by simpa using pow_le_pow_right₀ hp (by omega : 1≤10)) + apply (mul_le_mul_of_nonneg_left H (show 0 ≤ (a+1)*(d+2) by positivity)).trans + rw [mul_one_div] + apply (div_le_iff₀ ha).mpr; nlinarith) + have HT : (a+d+3)*P.Ex L ≤ 5*d*c := + le_trans (mul_le_mul_of_nonneg_left HC (by positivity)) (by + have H : 0 ≤ c := by unfold c limitC; norm_num + nlinarith) + unfold c limitC at * + nlinarith + +lemma grossOur : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → + ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Play j M s, t.Follows J f → 1/Thermal.H j ≤ s.mass → + yieldA t ≤ s.mass*(10^221*height j) := by + filter_upwards [cost_overall,burn_rate,upper_total,frostCompare] with j h1 h2 h3 h4 M J hC f hf s t h hm + have H := traced t false h + have HP := (Play.first h).mass_lower + let v := trace false t + have hE := h1 M J f s t h hf + rw [due_restart rfl,add_zero] at hE + apply le_trans hE (mul_le_mul_of_nonneg_left ?_ HP) + have hc := h2 M J hC f hf v H hm + have he := h3 M J f hf v H hm + have HH := short_trace_bound t h rfl + apply (h4 M J f s t h rfl).trans + change v.flatFee billOf ≤ v.flatFee upperFare at HH + change v.flatFee billOf+coldFee v ≤ _ + linarith [d_pos j] + +private lemma scale_drought (m : ℝ) (hm : 0 < m) (h : m ≤ 1) : + m*drought m ≤ 4 ∧ m*(drought m)^2 ≤ 4 := by + have hh' : drought m= -log m := max_eq_left (by linarith [log_nonpos hm.le h]) + let x := drought m + have heq : m= (exp x)⁻¹ := by unfold x; rw [hh',exp_neg,inv_inv,exp_log hm] + have H : x^2 /4 ≤ exp x := by + have h0 : exp x=(exp (x/2))^2 := by rw [pow_two,←exp_add]; ring_nf + have hi := add_one_le_exp (x/2) + rw [h0] + nlinarith [show 0 ≤ x from drought_pos m] + have HH := mul_le_mul_of_nonneg_left H hm.le + have Hx : m*exp x=1 := by rw [heq,inv_mul_cancel₀ (exp_pos x).ne'] + change m*x≤4 ∧ m*x^2≤4 + constructor + · nlinarith [mul_nonneg hm.le (sq_nonneg (x-1))] + linarith + +lemma grossDry : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, + (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f false + ∀ t : Play j M s, t.Follows J f → s.mass ≤ 1/Thermal.H j → + yieldA t ≤ 10^152*height j := by + filter_upwards [cost_overall,chill_descendants,hpoly 1 1] with j h1 h2 h3 M J f hf s t h hm + have H := h1 _ _ f s t h hf + rw [due_restart rfl,add_zero] at H + apply H.trans + let m := s.mass; let d := height j; let a := Thermal.H j + have hh : 0 ≤ m := (Play.first h).mass_lower + have ha : 1 ≤ a := H_ge _ + have hd : 1 ≤ d := one_le_pow₀ (by norm_num) + have hp : a ≤ d := by simpa using h3 + have hz : 0 < a := by linarith + change m≤1/a at hm + have hi : m*a ≤ 1 := (le_div_iff₀ hz).mp hm + have hr : m ≤ 1 := by nlinarith + change m*_ ≤ 10^152*d + rcases eq_or_lt_of_le hh with hh|hh + · rw [←hh,zero_mul]; positivity + apply le_trans (mul_le_mul_of_nonneg_left (h2 M J f s t h rfl rfl hh) hh.le) + let x := drought m + change m*(10^150*(a+1+x)*(d+2+x)) ≤ _ + have he : m*x ≤ 4 ∧ m*x^2 ≤ 4 := scale_drought m hh hr + nlinarith [mul_le_mul_of_nonneg_right hi (show 0 ≤ d by linarith),mul_le_mul_of_nonneg_right he.1 + (show 0 ≤ a by linarith),mul_le_mul_of_nonneg_right he.1 (show 0 ≤ d by linarith)] + +end CastShow + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Append.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Append.lean index 5b3701e0603cdc4614d0e2eee0f75b380a25f8eb..f8df22a1aad212943031b2db8c4519f8a1a6e585 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Append.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Append.lean @@ -1,180 +1,180 @@ -import OAI.Analysis.FourierLLogL.Capture.Book - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Chart - -variable {M : ℕ} -open scoped Classical - -def offset (i n : ℕ) := 16+i*2+n*2 - -def price (b : Bool) (i : ℕ) := amp i b^2/2048 - -private lemma leak_gate (i n : ℕ) (main : Bool) : - side (offset i n) ≤ amp i main/128 ∧ - 2^n*side (offset i n) ≤ side n*(amp i main/128) := by - have hi : side i ^2 ≤ amp i main := (strength_bounds main (side_pos _) (side_le _)).2.2 - have he' := side_le n - have he := side_pos n - have hs : side (offset i n) = side 16 * side i^2*side n^2 := by simp [offset,side,pow_add,pow_mul] - have hP : side 16 ≤ 1/128 := by norm_num [side] - have hQ : 2^n*side n=(1:ℝ) := by rw [mul_comm,side_card] - rw [hs] - have hn' : side 16*side i^2 ≤ amp i main/128 := by - calc - _ ≤ (1/128)*amp i main := mul_le_mul hP hi (sq_nonneg _) (by norm_num) - _ = _ := by ring - constructor - · refine le_trans (mul_le_of_le_one_right (mul_nonneg (side_pos _).le (sq_nonneg _)) ?_) hn'; nlinarith - calc - _ = side n*(side 16*side i^2) := by - rw [show 2^n * (side 16*side i^2*side n^2) = - (2^n * side n)*(side n*(side 16*side i^2)) by ring,hQ,one_mul] - _ ≤ _ := by gcongr - -private lemma boundary_leak (p : Node) (hp : p.ok M) (F g : Grid M → ℝ) - (hF : ∀ x, 0 ≤ g x ∧ |F x| ≤ g x) (hG : expU F=0) (L : ℝ) - (he : expU (fun x => if x∈p.block M then 0 else g x) ≤ L*p.len) : - (p.dice M).Ex F ≤ L := by - let f (x : Grid M) := if x∈p.block M then F x else 0 - let g' (x : Grid M) := if x∈p.block M then 0 else g x - have hE (x : Grid M) : f x ≤ F x+g' x := by - unfold f g'; split_ifs - · rw [add_zero] - · linarith [(abs_le.mp (hF x).2).1] - have hX : expU f ≤ L*p.len := by - apply le_trans (b:=expU (fun x => F x+g' x)) - · rw [expU_ex,expU_ex]; apply Ex_mono _ hE - rw [expU_add,hG,zero_add]; exact he - have hh : (p.dice M).Ex F = expU f / p.len := by - rw [← avg_ex hp,avg]; rw [div_eq_inv_mul,smul_eq_mul] - congr 2 - rw [hh] - exact (div_le_iff₀ p.len_pos).mpr hX - -theorem append_choose (main : Bool) (i : ℕ) {q : Scene M} - (hp : q.here.valid) (he : q.here.lev < M) (ha : 0 ≤ q.wage) (hn : 0 < q.norm) - (hm : 0 < q.mass) (hM : q.mass ≤ 2*q.norm) (hE : ¬ hazard q main i) - (D : Chart M) (hD : D.Sound) - (x : Grid M) (hx : intense main q (side i) x) - (hX : ¬ atReach D.items (D.wide+band q.here i (offset i D.size)) x) : - ∃ C : Chart M, C.items=insert x D.items ∧ D.size+1=C.size ∧ - C.wide=D.wide+band q.here i (offset i D.size) ∧ C.Sound ∧ - flex q main C.refs + price main i ≤ flex q main D.refs := by - let r := offset i D.size - let p := band q.here i r - let a := amp i main; let t := a/128 - let P := tracker q main; let U := q.here.dice M - have hR : 0 ≤ p := by unfold p band; positivity - have h₀ : 0 < a := amp_pos _ _ - have hT : 0 < t := by unfold t; positivity - have h₁ : a ≤ 1 := amp_hi _ _ - obtain ⟨g,hg,f,hf,hY,hZ,hW⟩ := masked_bias main x hp he ha hn hm hM hx hE r (leak_gate i D.size main).1 - let B := D.refs - let Y (z y : Grid M) := f y * (g z * chi x y).re - let L (z y : Grid M) := 1+t*Y z y - let C : Chart M := ⟨fun z y => B z y*L z y, insert x D.items, D.wide+p⟩ - have HT (z y) : |Y z y| ≤ 1 := by - unfold Y; rw [abs_mul] - have hfy : |f y| ≤ 1 := by simpa only [abs_of_nonneg (hY y).1] using (hY y).2 - have hgy : |(g z * chi x y).re| ≤ 1 := - (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact hg _) - exact (mul_le_mul_of_nonneg_right hfy (abs_nonneg _)).trans (by simpa using hgy) - have HF (z y) : 1/2 ≤ L z y ∧ L z y ≤ 2 := by - unfold L t; rcases abs_le.mp (HT z y) with ⟨hh1,hh2⟩; constructor <;> nlinarith - have HH (z) : - x∉D.items ∧ tinted C.items C.wide (C.refs z) := by - convert tinted_extend _ _ _ _ _ (hD.radius z) hf hD.bound hR - x hX ((t:ℂ)*g z) using 2 - · dsimp only [C,B,L,Y] - funext y; rw [mul_assoc (a:=(t:ℂ)),Complex.re_ofReal_mul]; ring - have HX : C.size=D.size+1 := Finset.card_insert_of_notMem (HH 0).1 - have HL : C.Sound := by - constructor - · exact add_nonneg hD.bound hR - · intro z y - rcases HF z y with ⟨hh1,hh2⟩ - rcases hD.total z y with ⟨HE1,HE2⟩ - have he := side_pos D.size - rw [HX, side_succ,pow_succ] - unfold C B - dsimp only - constructor <;> nlinarith - intro z; exact (HH z).2 - refine ⟨C,rfl,HX.symm,rfl,HL,?_⟩ - have HE (z : Grid M) : log (normQ q C.refs z) - log (normQ q B z) ≤ t*(a/128) := by - have hv (y) : |B z y*Y z y| ≤ 2^D.size * f y := by - have he : 0 ≤ B z y := ((Chart.Sound.pos D hD) _ _).le - have hM := hY y - unfold Y; simp_rw [abs_mul,abs_of_nonneg he, abs_of_nonneg hM.1] - rw [← mul_assoc] - refine le_trans (mul_le_of_le_one_right (mul_nonneg he hM.1) ?_) ?_ - · exact (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact hg _) - exact mul_le_mul_of_nonneg_right (hD.total _ _).2 hM.1 - have hh : U.Ex (fun y => B z y*Y z y) ≤ 2^D.size * side r := by - apply boundary_leak _ ⟨hp,he.le⟩ _ (fun y => 2^D.size*f y) - (fun i => ⟨by have hx := (hY i).1; positivity,hv i⟩) - · unfold Y B; simp_rw [← mul_assoc] - exact tinted_cancel (tinted_mul (hD.radius z) hf) hX _ - · rw [expU_ex]; rw [expU_ex] at hZ - simp_rw [show ∀ x : Grid M, (if x∈q.here.block M then (0:ℝ) else 2^D.size*f x) = - 2^D.size*(if x∈q.here.block M then 0 else f x) from fun x => by split_ifs <;> ring, Ex_lmul] - apply le_trans (mul_le_mul_of_nonneg_left hZ (by positivity)) - exact le_of_eq (by ring) - have hn : normQ q C.refs z=normQ q B z+t* U.Ex (fun y => B z y*Y z y) := by - change U.Ex _ = U.Ex _ + _ - rw [← Ex_lmul,← Ex_add] - congr 1; funext i - change B z i*(1+t*Y z i) = _ - ring - have hG : side D.size ≤ normQ q B z := - le_trans (Ex_const U _).ge (Ex_mono U fun i => (hD.total _ _).1) - have h₃ : 0 < normQ q B z := normQ_pos _ _ (Chart.Sound.pos D hD) _ - have h₄ : 0 < normQ q C.refs z := normQ_pos _ _ (Chart.Sound.pos C HL) _ - rw [← log_div h₄.ne' h₃.ne'] - apply le_trans (Real.log_le_sub_one_of_pos (div_pos h₄ h₃)) - have hl : 2^D.size * side r ≤ side D.size*(a/128) := (leak_gate _ _ main).2 - refine sub_le_iff_le_add.mpr ((div_le_iff₀ h₃).mpr ?_) - nlinarith [hh.trans (hl.trans (mul_le_mul_of_nonneg_right hG (by positivity : 0 ≤ a/128)))] - have h₃ : t*(a/8) - 2*t^2 ≤ P.Ex (fun i => log (L i.2 i.1)) := by - let W (i : Grid M × Grid M) := Y i.2 i.1 - have hi (i : Grid M × Grid M) : - t*W i - 2*t^2 ≤ log (L i.2 i.1) := by - have hW := HT i.2 i.1 - have hX : |t* W i| ≤ t := by - rw [abs_mul,abs_of_nonneg hT.le] - exact mul_le_of_le_one_right hT.le hW - have hF : log (1+t*W i) ≥ t*W i -2*(t*W i)^2 := - log_shift_bound _ (hX.trans (by unfold t; linarith)) - change _ ≤ log (1+t*W i) - have hE := pow_le_pow_left₀ (abs_nonneg _) hX 2; rw [sq_abs] at hE - linarith - have HT := P.Ex_mono hi - rw [Ex_sub,Ex_lmul,Ex_const] at HT - change a/8 ≤ P.Ex W at hW - nlinarith - unfold flex - let delta := fun (F : ProductRef M) (i : Grid M × Grid M) => log (normQ q F i.2)-log (F i.2 i.1) - have HX (i : Grid M × Grid M) : - delta C.refs i+log (L i.2 i.1) ≤ delta B i+t*(a/128) := by - unfold delta - have heq : C.refs i.2 i.1=B i.2 i.1*L i.2 i.1 := rfl - rw [heq,Real.log_mul ((Chart.Sound.pos D hD) _ _).ne' - (lt_of_lt_of_le (by norm_num : (0:ℝ)<1/2) (HF _ _).1).ne'] - linarith [HE i.2] - have He := Ex_mono P HX - rw [Ex_add,Ex_add,Ex_const] at He - unfold price - change _+ P.Ex (delta C.refs)+ a^2/2048 ≤ _ + P.Ex (delta D.refs) - unfold t B at * - nlinarith - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Book + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Chart + +variable {M : ℕ} +open scoped Classical + +def offset (i n : ℕ) := 16+i*2+n*2 + +def price (b : Bool) (i : ℕ) := amp i b^2/2048 + +private lemma leak_gate (i n : ℕ) (main : Bool) : + side (offset i n) ≤ amp i main/128 ∧ + 2^n*side (offset i n) ≤ side n*(amp i main/128) := by + have hi : side i ^2 ≤ amp i main := (strength_bounds main (side_pos _) (side_le _)).2.2 + have he' := side_le n + have he := side_pos n + have hs : side (offset i n) = side 16 * side i^2*side n^2 := by simp [offset,side,pow_add,pow_mul] + have hP : side 16 ≤ 1/128 := by norm_num [side] + have hQ : 2^n*side n=(1:ℝ) := by rw [mul_comm,side_card] + rw [hs] + have hn' : side 16*side i^2 ≤ amp i main/128 := by + calc + _ ≤ (1/128)*amp i main := mul_le_mul hP hi (sq_nonneg _) (by norm_num) + _ = _ := by ring + constructor + · refine le_trans (mul_le_of_le_one_right (mul_nonneg (side_pos _).le (sq_nonneg _)) ?_) hn'; nlinarith + calc + _ = side n*(side 16*side i^2) := by + rw [show 2^n * (side 16*side i^2*side n^2) = + (2^n * side n)*(side n*(side 16*side i^2)) by ring,hQ,one_mul] + _ ≤ _ := by gcongr + +private lemma boundary_leak (p : Node) (hp : p.ok M) (F g : Grid M → ℝ) + (hF : ∀ x, 0 ≤ g x ∧ |F x| ≤ g x) (hG : expU F=0) (L : ℝ) + (he : expU (fun x => if x∈p.block M then 0 else g x) ≤ L*p.len) : + (p.dice M).Ex F ≤ L := by + let f (x : Grid M) := if x∈p.block M then F x else 0 + let g' (x : Grid M) := if x∈p.block M then 0 else g x + have hE (x : Grid M) : f x ≤ F x+g' x := by + unfold f g'; split_ifs + · rw [add_zero] + · linarith [(abs_le.mp (hF x).2).1] + have hX : expU f ≤ L*p.len := by + apply le_trans (b:=expU (fun x => F x+g' x)) + · rw [expU_ex,expU_ex]; apply Ex_mono _ hE + rw [expU_add,hG,zero_add]; exact he + have hh : (p.dice M).Ex F = expU f / p.len := by + rw [← avg_ex hp,avg]; rw [div_eq_inv_mul,smul_eq_mul] + congr 2 + rw [hh] + exact (div_le_iff₀ p.len_pos).mpr hX + +theorem append_choose (main : Bool) (i : ℕ) {q : Scene M} + (hp : q.here.valid) (he : q.here.lev < M) (ha : 0 ≤ q.wage) (hn : 0 < q.norm) + (hm : 0 < q.mass) (hM : q.mass ≤ 2*q.norm) (hE : ¬ hazard q main i) + (D : Chart M) (hD : D.Sound) + (x : Grid M) (hx : intense main q (side i) x) + (hX : ¬ atReach D.items (D.wide+band q.here i (offset i D.size)) x) : + ∃ C : Chart M, C.items=insert x D.items ∧ D.size+1=C.size ∧ + C.wide=D.wide+band q.here i (offset i D.size) ∧ C.Sound ∧ + flex q main C.refs + price main i ≤ flex q main D.refs := by + let r := offset i D.size + let p := band q.here i r + let a := amp i main; let t := a/128 + let P := tracker q main; let U := q.here.dice M + have hR : 0 ≤ p := by unfold p band; positivity + have h₀ : 0 < a := amp_pos _ _ + have hT : 0 < t := by unfold t; positivity + have h₁ : a ≤ 1 := amp_hi _ _ + obtain ⟨g,hg,f,hf,hY,hZ,hW⟩ := masked_bias main x hp he ha hn hm hM hx hE r (leak_gate i D.size main).1 + let B := D.refs + let Y (z y : Grid M) := f y * (g z * chi x y).re + let L (z y : Grid M) := 1+t*Y z y + let C : Chart M := ⟨fun z y => B z y*L z y, insert x D.items, D.wide+p⟩ + have HT (z y) : |Y z y| ≤ 1 := by + unfold Y; rw [abs_mul] + have hfy : |f y| ≤ 1 := by simpa only [abs_of_nonneg (hY y).1] using (hY y).2 + have hgy : |(g z * chi x y).re| ≤ 1 := + (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact hg _) + exact (mul_le_mul_of_nonneg_right hfy (abs_nonneg _)).trans (by simpa using hgy) + have HF (z y) : 1/2 ≤ L z y ∧ L z y ≤ 2 := by + unfold L t; rcases abs_le.mp (HT z y) with ⟨hh1,hh2⟩; constructor <;> nlinarith + have HH (z) : + x∉D.items ∧ tinted C.items C.wide (C.refs z) := by + convert tinted_extend _ _ _ _ _ (hD.radius z) hf hD.bound hR + x hX ((t:ℂ)*g z) using 2 + · dsimp only [C,B,L,Y] + funext y; rw [mul_assoc (a:=(t:ℂ)),Complex.re_ofReal_mul]; ring + have HX : C.size=D.size+1 := Finset.card_insert_of_notMem (HH 0).1 + have HL : C.Sound := by + constructor + · exact add_nonneg hD.bound hR + · intro z y + rcases HF z y with ⟨hh1,hh2⟩ + rcases hD.total z y with ⟨HE1,HE2⟩ + have he := side_pos D.size + rw [HX, side_succ,pow_succ] + unfold C B + dsimp only + constructor <;> nlinarith + intro z; exact (HH z).2 + refine ⟨C,rfl,HX.symm,rfl,HL,?_⟩ + have HE (z : Grid M) : log (normQ q C.refs z) - log (normQ q B z) ≤ t*(a/128) := by + have hv (y) : |B z y*Y z y| ≤ 2^D.size * f y := by + have he : 0 ≤ B z y := ((Chart.Sound.pos D hD) _ _).le + have hM := hY y + unfold Y; simp_rw [abs_mul,abs_of_nonneg he, abs_of_nonneg hM.1] + rw [← mul_assoc] + refine le_trans (mul_le_of_le_one_right (mul_nonneg he hM.1) ?_) ?_ + · exact (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact hg _) + exact mul_le_mul_of_nonneg_right (hD.total _ _).2 hM.1 + have hh : U.Ex (fun y => B z y*Y z y) ≤ 2^D.size * side r := by + apply boundary_leak _ ⟨hp,he.le⟩ _ (fun y => 2^D.size*f y) + (fun i => ⟨by have hx := (hY i).1; positivity,hv i⟩) + · unfold Y B; simp_rw [← mul_assoc] + exact tinted_cancel (tinted_mul (hD.radius z) hf) hX _ + · rw [expU_ex]; rw [expU_ex] at hZ + simp_rw [show ∀ x : Grid M, (if x∈q.here.block M then (0:ℝ) else 2^D.size*f x) = + 2^D.size*(if x∈q.here.block M then 0 else f x) from fun x => by split_ifs <;> ring, Ex_lmul] + apply le_trans (mul_le_mul_of_nonneg_left hZ (by positivity)) + exact le_of_eq (by ring) + have hn : normQ q C.refs z=normQ q B z+t* U.Ex (fun y => B z y*Y z y) := by + change U.Ex _ = U.Ex _ + _ + rw [← Ex_lmul,← Ex_add] + congr 1; funext i + change B z i*(1+t*Y z i) = _ + ring + have hG : side D.size ≤ normQ q B z := + le_trans (Ex_const U _).ge (Ex_mono U fun i => (hD.total _ _).1) + have h₃ : 0 < normQ q B z := normQ_pos _ _ (Chart.Sound.pos D hD) _ + have h₄ : 0 < normQ q C.refs z := normQ_pos _ _ (Chart.Sound.pos C HL) _ + rw [← log_div h₄.ne' h₃.ne'] + apply le_trans (Real.log_le_sub_one_of_pos (div_pos h₄ h₃)) + have hl : 2^D.size * side r ≤ side D.size*(a/128) := (leak_gate _ _ main).2 + refine sub_le_iff_le_add.mpr ((div_le_iff₀ h₃).mpr ?_) + nlinarith [hh.trans (hl.trans (mul_le_mul_of_nonneg_right hG (by positivity : 0 ≤ a/128)))] + have h₃ : t*(a/8) - 2*t^2 ≤ P.Ex (fun i => log (L i.2 i.1)) := by + let W (i : Grid M × Grid M) := Y i.2 i.1 + have hi (i : Grid M × Grid M) : + t*W i - 2*t^2 ≤ log (L i.2 i.1) := by + have hW := HT i.2 i.1 + have hX : |t* W i| ≤ t := by + rw [abs_mul,abs_of_nonneg hT.le] + exact mul_le_of_le_one_right hT.le hW + have hF : log (1+t*W i) ≥ t*W i -2*(t*W i)^2 := + log_shift_bound _ (hX.trans (by unfold t; linarith)) + change _ ≤ log (1+t*W i) + have hE := pow_le_pow_left₀ (abs_nonneg _) hX 2; rw [sq_abs] at hE + linarith + have HT := P.Ex_mono hi + rw [Ex_sub,Ex_lmul,Ex_const] at HT + change a/8 ≤ P.Ex W at hW + nlinarith + unfold flex + let delta := fun (F : ProductRef M) (i : Grid M × Grid M) => log (normQ q F i.2)-log (F i.2 i.1) + have HX (i : Grid M × Grid M) : + delta C.refs i+log (L i.2 i.1) ≤ delta B i+t*(a/128) := by + unfold delta + have heq : C.refs i.2 i.1=B i.2 i.1*L i.2 i.1 := rfl + rw [heq,Real.log_mul ((Chart.Sound.pos D hD) _ _).ne' + (lt_of_lt_of_le (by norm_num : (0:ℝ)<1/2) (HF _ _).1).ne'] + linarith [HE i.2] + have He := Ex_mono P HX + rw [Ex_add,Ex_add,Ex_const] at He + unfold price + change _+ P.Ex (delta C.refs)+ a^2/2048 ≤ _ + P.Ex (delta D.refs) + unfold t B at * + nlinarith + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Book.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Book.lean index 981ea4b8ee83b841c65c40279c8a3f7affb2d2d6..c3a4cbe2db35097bf51f0db9fb2502e1c9c4e702 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Book.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Book.lean @@ -1,114 +1,114 @@ -import OAI.Analysis.FourierLLogL.Capture.Flex -import OAI.Analysis.FourierLLogL.Dyadic.Span - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread - -variable {M : ℕ} -open scoped Classical - -def atReach (d : Finset (Grid M)) (r : ℝ) (x : Grid M) := - ∃ i, enclosed d 1 i ∧ rad (x-i) ≤ r - -def tinted (d : Finset (Grid M)) (r : ℝ) (f : Grid M → ℝ) := - ∀ i, ¬ atReach d r i → fc (fun x => (↑(f x):ℂ)) i = 0 - -lemma atReach_neg {d : Finset (Grid M)} {r : ℝ} {x : Grid M} (h : atReach d r x) : - atReach d r (-x) := by - obtain ⟨i,h1,h2⟩ := h - exact ⟨-i,enclosed_neg h1,by rw [show -x- -i= -(x-i) by ring,rad_even]; exact h2⟩ - -lemma tinted_mul {d : Finset (Grid M)} {r s : ℝ} {f g : Grid M → ℝ} (hf : tinted d r f) - (hg : limitR g s) : tinted d (r+s) (fun i => f i*g i) := by - intro i hi - simp only [Complex.ofReal_mul] - rw [fc_prod] - apply Finset.sum_eq_zero; intro j _ - by_cases h : atReach d r j - · obtain ⟨v,hv,H⟩ := h - have hX : s < rad (i-j) := by - by_contra! he; apply hi; refine ⟨v,hv,?_⟩ - have hZ := rad_add (i-j) (j-v); rw [sub_add_sub_cancel] at hZ; linarith - rw [hg _ hX,mul_zero] - rw [hf j h,zero_mul] - -lemma tinted_cancel {d : Finset (Grid M)} {r : ℝ} {f : Grid M → ℝ} (hf : tinted d r f) - {i : Grid M} (hh : ¬ atReach d r i) (c : ℂ) : - expU (fun x => f x*(c*chi i x).re)=0 := by - have H : c* fc (fun x => (↑(f x):ℂ)) (-i) = 0 := by - rw [hf (-i) (fun h => hh (by simpa using atReach_neg h)),mul_zero] - unfold fc at H - rw [expU_cx,← Cx_lmul] at H - have HE := congrArg Complex.re H - simp_rw [re_Cx,neg_neg,mul_left_comm c, Complex.re_ofReal_mul,← expU_ex, - Complex.zero_re] at HE - exact HE - -lemma tinted_one (d : Finset (Grid M)) {r : ℝ} (h : 0 ≤ r) : tinted d r (fun _ => 1) := by - intro i hi - have hn : ¬ enclosed d 1 i := by - intro H; apply hi; exact ⟨i,H,by simp [rad,h]⟩ - simp only [Complex.ofReal_one]; exact spanned_one _ _ hn - -lemma tinted_extend (f g : Grid M → ℝ) (d : Finset (Grid M)) (r s : ℝ) (hf : tinted d r f) - (hg : limitR g s) (hr : 0 ≤ r) (hs : 0 ≤ s) - (i : Grid M) (hi : ¬ atReach d (r+s) i) (c : ℂ) : - i∉d ∧ tinted (insert i d) (r+s) - (fun x => f x*(1+g x*(c*chi i x).re)) := by - have hx : i∉d := fun H => hi ⟨i,enclosed_mem _ H,by simp [rad]; linarith⟩ - let F : Grid M → ℂ := fun x => (f x * g x:ℝ) - have Hf : ∀ x, ¬ atReach d (r+s) x → fc F x=0 := tinted_mul hf hg - refine ⟨hx,?_⟩ - intro x hh - have H (p : Grid M) (h : ¬ atReach (insert i d) (r+s) x) - (j : ℤ) (hj : |j| ≤ 1) (heq : x=p+j•i) : - ¬ atReach d (r+s) p := by - intro hL - obtain ⟨u,hu,hb⟩ := hL - apply h - refine ⟨u+j•i,enclosed_insert hx hu j (by simpa using hj),?_⟩ - rw [heq]; simpa only [add_sub_add_right_eq_sub] using hb - have hX : ¬ atReach d (r+s) x := H x hh 0 (by norm_num) (by simp) - have hA : ¬ atReach d r x := by - rintro ⟨p,hp,hb⟩ - exact hX ⟨p,hp,by linarith⟩ - have hB : ¬ atReach d (r+s) (x-i) := H _ hh 1 (by norm_num) (by simp) - have hC : ¬ atReach d (r+s) (x+i) := H _ hh (-1) (by norm_num) (by simp) - have he (x) : - (↑(f x * (1 + g x *(c*chi i x).re)):ℂ) = (↑(f x):ℂ)+F x*((c*chi i x).re) := by - unfold F; push_cast; ring - simp_rw [he,fc_add,hf x hA] - have ht := expand_one_step F c i x - rw [Hf x hX,Hf (x-i) hB,Hf (x+i) hC] at ht - have h₁ (x) : F x*(1+(↑((c*chi i x).re):ℂ))=F x+F x*((c*chi i x).re) := by ring - simp_rw [h₁,fc_add,Hf x hX] at ht - exact ht.trans (by ring) - -structure Chart (M : ℕ) where - refs : ProductRef M - items : Finset (Grid M) - wide : ℝ - -namespace Chart -def classic (M : ℕ) : Chart M := ⟨fun _ _ => 1,∅,0⟩ -def size (c : Chart M) := c.items.card -structure Sound (c : Chart M) : Prop where - bound : 0 ≤ c.wide - total z y : side c.size ≤ c.refs z y ∧ c.refs z y ≤ 2^c.size - radius z : tinted c.items c.wide (c.refs z) - -lemma classic_sound (M : ℕ) : (classic M).Sound := - ⟨le_rfl, by simp [classic,size,side], fun z => tinted_one _ le_rfl⟩ -lemma Sound.pos (c : Chart M) (h : c.Sound) (z y) : 0 < c.refs z y := - lt_of_lt_of_le (side_pos _) (h.total z y).1 - -end Chart - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Flex +import OAI.Analysis.FourierLLogL.Dyadic.Span + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread + +variable {M : ℕ} +open scoped Classical + +def atReach (d : Finset (Grid M)) (r : ℝ) (x : Grid M) := + ∃ i, enclosed d 1 i ∧ rad (x-i) ≤ r + +def tinted (d : Finset (Grid M)) (r : ℝ) (f : Grid M → ℝ) := + ∀ i, ¬ atReach d r i → fc (fun x => (↑(f x):ℂ)) i = 0 + +lemma atReach_neg {d : Finset (Grid M)} {r : ℝ} {x : Grid M} (h : atReach d r x) : + atReach d r (-x) := by + obtain ⟨i,h1,h2⟩ := h + exact ⟨-i,enclosed_neg h1,by rw [show -x- -i= -(x-i) by ring,rad_even]; exact h2⟩ + +lemma tinted_mul {d : Finset (Grid M)} {r s : ℝ} {f g : Grid M → ℝ} (hf : tinted d r f) + (hg : limitR g s) : tinted d (r+s) (fun i => f i*g i) := by + intro i hi + simp only [Complex.ofReal_mul] + rw [fc_prod] + apply Finset.sum_eq_zero; intro j _ + by_cases h : atReach d r j + · obtain ⟨v,hv,H⟩ := h + have hX : s < rad (i-j) := by + by_contra! he; apply hi; refine ⟨v,hv,?_⟩ + have hZ := rad_add (i-j) (j-v); rw [sub_add_sub_cancel] at hZ; linarith + rw [hg _ hX,mul_zero] + rw [hf j h,zero_mul] + +lemma tinted_cancel {d : Finset (Grid M)} {r : ℝ} {f : Grid M → ℝ} (hf : tinted d r f) + {i : Grid M} (hh : ¬ atReach d r i) (c : ℂ) : + expU (fun x => f x*(c*chi i x).re)=0 := by + have H : c* fc (fun x => (↑(f x):ℂ)) (-i) = 0 := by + rw [hf (-i) (fun h => hh (by simpa using atReach_neg h)),mul_zero] + unfold fc at H + rw [expU_cx,← Cx_lmul] at H + have HE := congrArg Complex.re H + simp_rw [re_Cx,neg_neg,mul_left_comm c, Complex.re_ofReal_mul,← expU_ex, + Complex.zero_re] at HE + exact HE + +lemma tinted_one (d : Finset (Grid M)) {r : ℝ} (h : 0 ≤ r) : tinted d r (fun _ => 1) := by + intro i hi + have hn : ¬ enclosed d 1 i := by + intro H; apply hi; exact ⟨i,H,by simp [rad,h]⟩ + simp only [Complex.ofReal_one]; exact spanned_one _ _ hn + +lemma tinted_extend (f g : Grid M → ℝ) (d : Finset (Grid M)) (r s : ℝ) (hf : tinted d r f) + (hg : limitR g s) (hr : 0 ≤ r) (hs : 0 ≤ s) + (i : Grid M) (hi : ¬ atReach d (r+s) i) (c : ℂ) : + i∉d ∧ tinted (insert i d) (r+s) + (fun x => f x*(1+g x*(c*chi i x).re)) := by + have hx : i∉d := fun H => hi ⟨i,enclosed_mem _ H,by simp [rad]; linarith⟩ + let F : Grid M → ℂ := fun x => (f x * g x:ℝ) + have Hf : ∀ x, ¬ atReach d (r+s) x → fc F x=0 := tinted_mul hf hg + refine ⟨hx,?_⟩ + intro x hh + have H (p : Grid M) (h : ¬ atReach (insert i d) (r+s) x) + (j : ℤ) (hj : |j| ≤ 1) (heq : x=p+j•i) : + ¬ atReach d (r+s) p := by + intro hL + obtain ⟨u,hu,hb⟩ := hL + apply h + refine ⟨u+j•i,enclosed_insert hx hu j (by simpa using hj),?_⟩ + rw [heq]; simpa only [add_sub_add_right_eq_sub] using hb + have hX : ¬ atReach d (r+s) x := H x hh 0 (by norm_num) (by simp) + have hA : ¬ atReach d r x := by + rintro ⟨p,hp,hb⟩ + exact hX ⟨p,hp,by linarith⟩ + have hB : ¬ atReach d (r+s) (x-i) := H _ hh 1 (by norm_num) (by simp) + have hC : ¬ atReach d (r+s) (x+i) := H _ hh (-1) (by norm_num) (by simp) + have he (x) : + (↑(f x * (1 + g x *(c*chi i x).re)):ℂ) = (↑(f x):ℂ)+F x*((c*chi i x).re) := by + unfold F; push_cast; ring + simp_rw [he,fc_add,hf x hA] + have ht := expand_one_step F c i x + rw [Hf x hX,Hf (x-i) hB,Hf (x+i) hC] at ht + have h₁ (x) : F x*(1+(↑((c*chi i x).re):ℂ))=F x+F x*((c*chi i x).re) := by ring + simp_rw [h₁,fc_add,Hf x hX] at ht + exact ht.trans (by ring) + +structure Chart (M : ℕ) where + refs : ProductRef M + items : Finset (Grid M) + wide : ℝ + +namespace Chart +def classic (M : ℕ) : Chart M := ⟨fun _ _ => 1,∅,0⟩ +def size (c : Chart M) := c.items.card +structure Sound (c : Chart M) : Prop where + bound : 0 ≤ c.wide + total z y : side c.size ≤ c.refs z y ∧ c.refs z y ≤ 2^c.size + radius z : tinted c.items c.wide (c.refs z) + +lemma classic_sound (M : ℕ) : (classic M).Sound := + ⟨le_rfl, by simp [classic,size,side], fun z => tinted_one _ le_rfl⟩ +lemma Sound.pos (c : Chart M) (h : c.Sound) (z y) : 0 < c.refs z y := + lt_of_lt_of_le (side_pos _) (h.total z y).1 + +end Chart + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Criterion.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Criterion.lean index 24c426b6515acbdaa415b7177c4eb54cb9b1a7eb..fdba5a3127d7083841f3b8750a6dd550d3a08b07 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Criterion.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Criterion.lean @@ -1,206 +1,206 @@ -import OAI.Analysis.FourierLLogL.Capture.Mask -import OAI.Analysis.FourierLLogL.Plateau.Local - -/-! # Criteria and their polarized tests -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread - -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -def optic (s : Scene M) (b : Bool) := if b then s.fund.mood else 0 -def energy (s : Scene M) (u : Grid M) := energyZ s.here s.wage s.norm s.fund.mood u -def scalar (s : Scene M) (u : Grid M) := - ∑ i : Grid M, (↑(wt s.here s.wage s.norm i):ℂ)*chi u i -def magnitude (main : Bool) (s : Scene M) (u : Grid M) := - if main then energy s u else ‖scalar s u‖ - -def strength (main : Bool) (b : ℝ) := if main then b^2 else b - -def intense (main : Bool) (s : Scene M) (r : ℝ) (i : Grid M) := - ∃ b, strength main r ≤ magnitude main (s.smaller b) i - -def amp (i : ℕ) (b : Bool) := strength b (side i) - -lemma strength_bounds (b : Bool) {r : ℝ} (hr : 0 < r) (h : r ≤ 1) : - 0 < strength b r ∧ strength b r ≤ r ∧ r^2 ≤ strength b r := by - have he : r^2 ≤ r := by nlinarith - cases b <;> simp only [strength, Bool.false_eq_true,↓reduceIte] <;> exact ⟨by positivity,by linarith,by linarith⟩ - -lemma amp_pos (i : ℕ) (b : Bool) : 0 < amp i b := - (strength_bounds b (side_pos i) (side_le i)).1 - -lemma amp_hi (i : ℕ) (b : Bool) : amp i b ≤ 1 := - (strength_bounds b (side_pos i) (side_le i)).2.1.trans (side_le i) - -lemma gate_amp (i : ℕ) (b : Bool) : - gateWidth b i = amp i b /128 := rfl - -lemma intent_main (s : Scene M) (r : ℝ) (i : Grid M) : - intense true s r i ↔ high s.here s.wage s.norm s.fund.mood r i := by - rfl - -lemma scalar_series (s : Scene M) (A : Form M) (u : Grid M) : - scalar s u = ∑ z, fz s.here s.wage s.norm A u z := by - unfold fz scalar - rw [Finset.sum_comm]; refine Finset.sum_congr rfl fun i hi => ?_ - unfold omegaObs; simp_rw [Complex.ofReal_mul,mul_right_comm - ((wt s.here s.wage s.norm i):ℂ) _ (chi u i)] - rw [← Finset.mul_sum,← Complex.ofReal_sum,(channel A i).total]; simp - -lemma strong_sum (s : Scene M) (b : Bool) (x : Grid M) - (ha : 0 ≤ s.wage) (hb : 0 < s.norm) : - magnitude b s x ≤ ∑ z, ‖fz s.here s.wage s.norm (optic s b) x z‖ := by - cases b - · change ‖scalar s x‖ ≤ _ - rw [scalar_series s (optic s false)]; exact norm_sum_le _ _ - · exact energyZ_le s.here ha hb.le _ _ - -def tracker (s : Scene M) (main : Bool) := s.actor.withNext (channel (optic s main)) - -def aim {p : Node} (b : Bool) (y : Grid M) : ℝ := if y∈(child p b).block M then 1 else 0 -private lemma aim_identity (s : Scene M) (b : Bool) (i : Grid M) (main : Bool) - (v : Grid M → ℂ) (hp : s.here.valid) (hh : s.here.lev < M) - (ha : 0 ≤ s.wage) (hb : 0 < s.norm) (hm : 0 < s.mass) : - let q := s.smaller b - ∑ z, (v z*fz q.here q.wage q.norm (optic q main) i z).re = - (2*s.mass/s.norm)*(tracker s main).Ex (fun w => - aim (p:=s.here) b w.1*(v w.2*chi i w.1).re) := by - classical - intro q - let A := optic s main - let L := channel A; let P := s.actor - let H := fun y : Grid M => aim (p:=s.here) b y - let c := 2*s.mass/s.norm - have HE (z : Grid M) : - (v z*fz q.here q.wage q.norm (optic q main) i z).re = - ∑ y, c * (P y * (L y z * (H y*(v z*chi i y).re))) := by - unfold fz; rw [Finset.mul_sum,Complex.re_sum] - refine Finset.sum_congr rfl fun y hy => ?_ - have Hi : omegaObs q.here q.wage q.norm (optic q main) y z = - c*H y*P y*L y z := by - unfold omegaObs - change wt (s.here.child b) s.wage s.norm y*L y z = _ - unfold H Procedure.Capture.aim c P - rw [actor_def ⟨hp,hh.le⟩ ha hm y,wt_half hp hh] - split_ifs - · unfold mass wt; unfold mass at hm; field_simp - · simp - rw [Hi,mul_left_comm,Complex.re_ofReal_mul]; ring - unfold tracker; rw [Ex_withNext]; simp only [HE,← Finset.mul_sum,Ex] - congr 1 - rw [Finset.sum_comm] - dsimp only [c,P,H,A,L] - simp_rw [Finset.mul_sum] - -lemma criterion_bias (s : Scene M) (main : Bool) (k : ℕ) (x : Grid M) - (hp : s.here.valid) (hh : s.here.lev < M) (ha : 0 ≤ s.wage) - (hx : 0 < s.norm) (hm : 0 < s.mass) (he : intense main s (side k) x) : - ∃ b : Bool, ∃ f : Grid M → ℂ, (∀ z, ‖f z‖ ≤ 1) ∧ - amp k main /2 ≤ (s.mass/s.norm)*(tracker s main).Ex (fun w => - aim (p:=s.here) b w.1 * (f w.2*chi x w.1).re) := by - obtain ⟨b,hL⟩ := he - let q := s.smaller b - let v := fun z => fz q.here q.wage q.norm (optic q main) x z - let f := fun z => polar (v z) - have HH := aim_identity s b x main f hp hh ha hx hm - have He (z : Grid M) : (f z*v z).re=‖v z‖ := polar_identity _ - have H : amp k main ≤ ∑ z, ‖v z‖ := hL.trans (strong_sum q main x ha hx) - refine ⟨b,f,fun z => polar_unit _,?_⟩ - change ∑ z,(f z*v z).re = _ at HH - simp_rw [He,mul_div_assoc] at HH - nlinarith - -private lemma test_one (g z : ℂ) (hg : ‖g‖ ≤ 1) (hz : ‖z‖ = 1) : - |(g*z).re| ≤ 1 := (Complex.abs_re_le_norm _).trans - (by rw [norm_mul,hz,mul_one]; exact hg) - -lemma expensive_intent {s : Scene M} (main : Bool) {k : ℕ} {x : Grid M} - (hp : s.here.valid) (hh : s.here.lev < M) (ha : 0 ≤ s.wage) - (hx : 0 < s.norm) (hm : 0 < s.mass) (he : intense main s (side k) x) : - amp k main /2 ≤ s.mass/s.norm := by - obtain ⟨b,f,hf,hb⟩ := criterion_bias _ _ _ _ hp hh ha hx hm he - apply hb.trans - apply mul_le_of_le_one_right (by positivity) - refine le_trans (Ex_mono _ (g:=fun _ => 1) fun i => ?_) (Ex_const _ _).le - unfold aim; split_ifs - · rw [one_mul]; exact (le_abs_self _).trans (test_one _ _ (hf _) (norm_chi _ _)) - norm_num - -lemma edge_prob {s : Scene M} {i : ℕ} (hm : 0 < s.mass) (hn : 0 < s.norm) - (hp : s.here.ok M) (h : 0 ≤ s.wage) : - s.mass/s.norm * s.actor.prob (fun x => lanes s.here i M ∧ flanked s.here i x) - = edgeW s i := by - classical - rw [prob_eq,edgeW] - by_cases hh : lanes s.here i M - · simp only [hh,ite_true,true_and]; rw [Finset.mul_sum] - refine Finset.sum_congr rfl fun x hx => ?_ - split_ifs with H - · rw [actor_def hp h hm] - unfold wt mass; unfold mass at hm - field_simp - exact mul_zero _ - simp [hh] - -/-- Mask of sufficient precision is a biased test under the post law -/ -theorem masked_bias {s : Scene M} {i : ℕ} (main : Bool) (x : Grid M) - (hp : s.here.valid) (he : s.here.lev < M) - (ha : 0 ≤ s.wage) (hn : 0 < s.norm) (hm : 0 < s.mass) (hM : s.mass ≤ 2*s.norm) - (h : intense main s (side i) x) - (hB : ¬ hazard s main i) (r : ℕ) (hr : side r ≤ amp i main /128) : - ∃ g : Grid M → ℂ, (∀ z, ‖g z‖ ≤ 1) ∧ ∃ f : Grid M → ℝ, - limitR f (band s.here i r) ∧ (∀ y, 0 ≤ f y ∧ f y ≤ 1) ∧ - expU (fun y => if y∈s.here.block M then 0 else f y) ≤ s.here.len*side r ∧ - amp i main /8 ≤ (tracker s main).Ex (fun y => f y.1*(g y.2*chi x y.1).re) := by - obtain ⟨b,g,hg,hG⟩ := criterion_bias s main i x hp he ha hn hm h - obtain ⟨f,hf,hN,hE,hP⟩ := make_mask (M:=M) s.here hp he b i r - let p := s.here - let a := amp i main; let v := s.mass/s.norm - let P := tracker s main - let w (y : Grid M × Grid M) := (g y.2*chi x y.1).re - let G (y : Grid M) := lanes p i M ∧ flanked p i y - let F (y : Grid M × Grid M) := f y.1*w y - have Hq : v*P.prob (fun y => G y.1) ≤ a/128 := by - rw [← prob_send P Prod.fst G,show P.send Prod.fst=s.actor from send_fst_withNext _ _] - rw [edge_prob hm hn ⟨hp,he.le⟩ ha] - exact le_of_not_ge hB - have Hw (i) : |w i| ≤ 1 := test_one _ _ (hg _) (norm_chi _ _) - have H : v * P.Ex (fun i => aim (p:=p) b i.1*w i) ≤ - v* (P.Ex F + P.prob (fun i => G i.1) + side r) := by - apply mul_le_mul_of_nonneg_left _ (by positivity) - rw [← Ex_const P (side r)]; unfold prob - rw [← Ex_add,← Ex_add] - apply Ex_mono_mem; rintro ⟨y,z⟩ h - have h0 : y∈p.block M := by - have hh : 0 < s.actor y := pos_of_mul_pos_left h ((channel (optic s main) y).nonneg z) - exact piece_support ⟨hp,he.le⟩ (actor_def ⟨hp,he.le⟩ ha hm) _ hh - change aim (p:=p) b y*w (y,z) ≤ f y*w (y,z)+(@ite ℝ (G y) (Classical.propDecidable _) 1 0)+side r - have hi := abs_le.mp (Hw (y,z)) - have hi' := hN y - split_ifs with H - · unfold aim; split_ifs <;> nlinarith [side_pos r] - have He : |aim (p:=p) b y-f y| ≤ side r := hE y h0 H - have hY := (mul_le_mul He (Hw (y,z)) (abs_nonneg _) (side_pos r).le) - rw [← abs_mul,mul_one] at hY - have hx := (abs_le.mp hY).2 - linarith - refine ⟨g,hg,f,hf,hN,hP,?_⟩ - have hv : v ≤ 2 := (div_le_iff₀ hn).mpr hM - have hp' : 0 < v := by unfold v; positivity - change a/2 ≤ _ at hG - change _ ≤ v * P.Ex (fun i => aim (p:=p) b i.1*w i) at hG - have HH : 0 < a := amp_pos _ _ - change a/8 ≤ P.Ex F - change side r ≤ a/128 at hr - nlinarith - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Mask +import OAI.Analysis.FourierLLogL.Plateau.Local + +/-! # Criteria and their polarized tests -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread + +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +def optic (s : Scene M) (b : Bool) := if b then s.fund.mood else 0 +def energy (s : Scene M) (u : Grid M) := energyZ s.here s.wage s.norm s.fund.mood u +def scalar (s : Scene M) (u : Grid M) := + ∑ i : Grid M, (↑(wt s.here s.wage s.norm i):ℂ)*chi u i +def magnitude (main : Bool) (s : Scene M) (u : Grid M) := + if main then energy s u else ‖scalar s u‖ + +def strength (main : Bool) (b : ℝ) := if main then b^2 else b + +def intense (main : Bool) (s : Scene M) (r : ℝ) (i : Grid M) := + ∃ b, strength main r ≤ magnitude main (s.smaller b) i + +def amp (i : ℕ) (b : Bool) := strength b (side i) + +lemma strength_bounds (b : Bool) {r : ℝ} (hr : 0 < r) (h : r ≤ 1) : + 0 < strength b r ∧ strength b r ≤ r ∧ r^2 ≤ strength b r := by + have he : r^2 ≤ r := by nlinarith + cases b <;> simp only [strength, Bool.false_eq_true,↓reduceIte] <;> exact ⟨by positivity,by linarith,by linarith⟩ + +lemma amp_pos (i : ℕ) (b : Bool) : 0 < amp i b := + (strength_bounds b (side_pos i) (side_le i)).1 + +lemma amp_hi (i : ℕ) (b : Bool) : amp i b ≤ 1 := + (strength_bounds b (side_pos i) (side_le i)).2.1.trans (side_le i) + +lemma gate_amp (i : ℕ) (b : Bool) : + gateWidth b i = amp i b /128 := rfl + +lemma intent_main (s : Scene M) (r : ℝ) (i : Grid M) : + intense true s r i ↔ high s.here s.wage s.norm s.fund.mood r i := by + rfl + +lemma scalar_series (s : Scene M) (A : Form M) (u : Grid M) : + scalar s u = ∑ z, fz s.here s.wage s.norm A u z := by + unfold fz scalar + rw [Finset.sum_comm]; refine Finset.sum_congr rfl fun i hi => ?_ + unfold omegaObs; simp_rw [Complex.ofReal_mul,mul_right_comm + ((wt s.here s.wage s.norm i):ℂ) _ (chi u i)] + rw [← Finset.mul_sum,← Complex.ofReal_sum,(channel A i).total]; simp + +lemma strong_sum (s : Scene M) (b : Bool) (x : Grid M) + (ha : 0 ≤ s.wage) (hb : 0 < s.norm) : + magnitude b s x ≤ ∑ z, ‖fz s.here s.wage s.norm (optic s b) x z‖ := by + cases b + · change ‖scalar s x‖ ≤ _ + rw [scalar_series s (optic s false)]; exact norm_sum_le _ _ + · exact energyZ_le s.here ha hb.le _ _ + +def tracker (s : Scene M) (main : Bool) := s.actor.withNext (channel (optic s main)) + +def aim {p : Node} (b : Bool) (y : Grid M) : ℝ := if y∈(child p b).block M then 1 else 0 +private lemma aim_identity (s : Scene M) (b : Bool) (i : Grid M) (main : Bool) + (v : Grid M → ℂ) (hp : s.here.valid) (hh : s.here.lev < M) + (ha : 0 ≤ s.wage) (hb : 0 < s.norm) (hm : 0 < s.mass) : + let q := s.smaller b + ∑ z, (v z*fz q.here q.wage q.norm (optic q main) i z).re = + (2*s.mass/s.norm)*(tracker s main).Ex (fun w => + aim (p:=s.here) b w.1*(v w.2*chi i w.1).re) := by + classical + intro q + let A := optic s main + let L := channel A; let P := s.actor + let H := fun y : Grid M => aim (p:=s.here) b y + let c := 2*s.mass/s.norm + have HE (z : Grid M) : + (v z*fz q.here q.wage q.norm (optic q main) i z).re = + ∑ y, c * (P y * (L y z * (H y*(v z*chi i y).re))) := by + unfold fz; rw [Finset.mul_sum,Complex.re_sum] + refine Finset.sum_congr rfl fun y hy => ?_ + have Hi : omegaObs q.here q.wage q.norm (optic q main) y z = + c*H y*P y*L y z := by + unfold omegaObs + change wt (s.here.child b) s.wage s.norm y*L y z = _ + unfold H Procedure.Capture.aim c P + rw [actor_def ⟨hp,hh.le⟩ ha hm y,wt_half hp hh] + split_ifs + · unfold mass wt; unfold mass at hm; field_simp + · simp + rw [Hi,mul_left_comm,Complex.re_ofReal_mul]; ring + unfold tracker; rw [Ex_withNext]; simp only [HE,← Finset.mul_sum,Ex] + congr 1 + rw [Finset.sum_comm] + dsimp only [c,P,H,A,L] + simp_rw [Finset.mul_sum] + +lemma criterion_bias (s : Scene M) (main : Bool) (k : ℕ) (x : Grid M) + (hp : s.here.valid) (hh : s.here.lev < M) (ha : 0 ≤ s.wage) + (hx : 0 < s.norm) (hm : 0 < s.mass) (he : intense main s (side k) x) : + ∃ b : Bool, ∃ f : Grid M → ℂ, (∀ z, ‖f z‖ ≤ 1) ∧ + amp k main /2 ≤ (s.mass/s.norm)*(tracker s main).Ex (fun w => + aim (p:=s.here) b w.1 * (f w.2*chi x w.1).re) := by + obtain ⟨b,hL⟩ := he + let q := s.smaller b + let v := fun z => fz q.here q.wage q.norm (optic q main) x z + let f := fun z => polar (v z) + have HH := aim_identity s b x main f hp hh ha hx hm + have He (z : Grid M) : (f z*v z).re=‖v z‖ := polar_identity _ + have H : amp k main ≤ ∑ z, ‖v z‖ := hL.trans (strong_sum q main x ha hx) + refine ⟨b,f,fun z => polar_unit _,?_⟩ + change ∑ z,(f z*v z).re = _ at HH + simp_rw [He,mul_div_assoc] at HH + nlinarith + +private lemma test_one (g z : ℂ) (hg : ‖g‖ ≤ 1) (hz : ‖z‖ = 1) : + |(g*z).re| ≤ 1 := (Complex.abs_re_le_norm _).trans + (by rw [norm_mul,hz,mul_one]; exact hg) + +lemma expensive_intent {s : Scene M} (main : Bool) {k : ℕ} {x : Grid M} + (hp : s.here.valid) (hh : s.here.lev < M) (ha : 0 ≤ s.wage) + (hx : 0 < s.norm) (hm : 0 < s.mass) (he : intense main s (side k) x) : + amp k main /2 ≤ s.mass/s.norm := by + obtain ⟨b,f,hf,hb⟩ := criterion_bias _ _ _ _ hp hh ha hx hm he + apply hb.trans + apply mul_le_of_le_one_right (by positivity) + refine le_trans (Ex_mono _ (g:=fun _ => 1) fun i => ?_) (Ex_const _ _).le + unfold aim; split_ifs + · rw [one_mul]; exact (le_abs_self _).trans (test_one _ _ (hf _) (norm_chi _ _)) + norm_num + +lemma edge_prob {s : Scene M} {i : ℕ} (hm : 0 < s.mass) (hn : 0 < s.norm) + (hp : s.here.ok M) (h : 0 ≤ s.wage) : + s.mass/s.norm * s.actor.prob (fun x => lanes s.here i M ∧ flanked s.here i x) + = edgeW s i := by + classical + rw [prob_eq,edgeW] + by_cases hh : lanes s.here i M + · simp only [hh,ite_true,true_and]; rw [Finset.mul_sum] + refine Finset.sum_congr rfl fun x hx => ?_ + split_ifs with H + · rw [actor_def hp h hm] + unfold wt mass; unfold mass at hm + field_simp + exact mul_zero _ + simp [hh] + +/-- Mask of sufficient precision is a biased test under the post law -/ +theorem masked_bias {s : Scene M} {i : ℕ} (main : Bool) (x : Grid M) + (hp : s.here.valid) (he : s.here.lev < M) + (ha : 0 ≤ s.wage) (hn : 0 < s.norm) (hm : 0 < s.mass) (hM : s.mass ≤ 2*s.norm) + (h : intense main s (side i) x) + (hB : ¬ hazard s main i) (r : ℕ) (hr : side r ≤ amp i main /128) : + ∃ g : Grid M → ℂ, (∀ z, ‖g z‖ ≤ 1) ∧ ∃ f : Grid M → ℝ, + limitR f (band s.here i r) ∧ (∀ y, 0 ≤ f y ∧ f y ≤ 1) ∧ + expU (fun y => if y∈s.here.block M then 0 else f y) ≤ s.here.len*side r ∧ + amp i main /8 ≤ (tracker s main).Ex (fun y => f y.1*(g y.2*chi x y.1).re) := by + obtain ⟨b,g,hg,hG⟩ := criterion_bias s main i x hp he ha hn hm h + obtain ⟨f,hf,hN,hE,hP⟩ := make_mask (M:=M) s.here hp he b i r + let p := s.here + let a := amp i main; let v := s.mass/s.norm + let P := tracker s main + let w (y : Grid M × Grid M) := (g y.2*chi x y.1).re + let G (y : Grid M) := lanes p i M ∧ flanked p i y + let F (y : Grid M × Grid M) := f y.1*w y + have Hq : v*P.prob (fun y => G y.1) ≤ a/128 := by + rw [← prob_send P Prod.fst G,show P.send Prod.fst=s.actor from send_fst_withNext _ _] + rw [edge_prob hm hn ⟨hp,he.le⟩ ha] + exact le_of_not_ge hB + have Hw (i) : |w i| ≤ 1 := test_one _ _ (hg _) (norm_chi _ _) + have H : v * P.Ex (fun i => aim (p:=p) b i.1*w i) ≤ + v* (P.Ex F + P.prob (fun i => G i.1) + side r) := by + apply mul_le_mul_of_nonneg_left _ (by positivity) + rw [← Ex_const P (side r)]; unfold prob + rw [← Ex_add,← Ex_add] + apply Ex_mono_mem; rintro ⟨y,z⟩ h + have h0 : y∈p.block M := by + have hh : 0 < s.actor y := pos_of_mul_pos_left h ((channel (optic s main) y).nonneg z) + exact piece_support ⟨hp,he.le⟩ (actor_def ⟨hp,he.le⟩ ha hm) _ hh + change aim (p:=p) b y*w (y,z) ≤ f y*w (y,z)+(@ite ℝ (G y) (Classical.propDecidable _) 1 0)+side r + have hi := abs_le.mp (Hw (y,z)) + have hi' := hN y + split_ifs with H + · unfold aim; split_ifs <;> nlinarith [side_pos r] + have He : |aim (p:=p) b y-f y| ≤ side r := hE y h0 H + have hY := (mul_le_mul He (Hw (y,z)) (abs_nonneg _) (side_pos r).le) + rw [← abs_mul,mul_one] at hY + have hx := (abs_le.mp hY).2 + linarith + refine ⟨g,hg,f,hf,hN,hP,?_⟩ + have hv : v ≤ 2 := (div_le_iff₀ hn).mpr hM + have hp' : 0 < v := by unfold v; positivity + change a/2 ≤ _ at hG + change _ ≤ v * P.Ex (fun i => aim (p:=p) b i.1*w i) at hG + have HH : 0 < a := amp_pos _ _ + change a/8 ≤ P.Ex F + change side r ≤ a/128 at hr + nlinarith + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Curve.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Curve.lean index a0961b9666fe1698faedc639fc8a5dbdc29a7858..69358a25eae9ca40657f6bf5f16f585bfbc8639b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Curve.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Curve.lean @@ -1,190 +1,190 @@ -import OAI.Analysis.FourierLLogL.Capture.Append - -/-! # Adaptive frequency list along a plateau -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Chart - -variable {M J j : ℕ} -open scoped Classical - -def speed (p : Node) (i n : ℕ) := band p i (offset i n) -def horizon (p : Node) (i n : ℕ) := (1+n:ℝ)*speed p i n -def localSize (p : Node) (i : ℕ) (c : Chart M) := - c.wide ≤ c.size*speed p i c.size - -lemma speed_pos (p : Node) (i n : ℕ) : 0 ≤ speed p i n := by unfold speed band; positivity -lemma speed_le {p q : Node} (h : p.lev ≤ q.lev) {i n m : ℕ} (he : n ≤ m) : - speed p i n ≤ speed q i m := by - unfold speed band offset - gcongr; norm_num - -/-- One or several appends before a jump. -/ -structure Widen (p : Scene M) (i : ℕ) (main : Bool) (c d : Chart M) : Prop where - sonic : d.Sound - extend : c.items ⊆ d.items - paid : flex p main d.refs+price main i*(d.size-c.size:ℝ) ≤ flex p main c.refs - expands : localSize p.here i c → localSize p.here i d - mass : d.size=c.size ∨ amp i main/2 ≤ p.mass/p.norm - awake : ¬ hazard p main i → ∀ x, intense main p (side i) x → - atReach d.items (d.wide+speed p.here i d.size) x - -lemma widen_done (main : Bool) (i : ℕ) {q : Scene M} - (hp : q.here.valid) (he : q.here.lev < M) (ha : 0 ≤ q.wage) (hn : 0 < q.norm) - (hm : 0 < q.mass) (hM : q.mass ≤ 2*q.norm) - (c : Chart M) (h : c.Sound) : ∃ d, Widen q i main c d := by - classical - let b := Fintype.card (Grid M) - suffices H : ∀ n : ℕ, ∀ c : Chart M, b-c.size=n → c.Sound → - ∃ d, Widen q i main c d from H _ _ rfl h - intro n; induction n using Nat.strong_induction_on with - | h n ih => - intro c heq h - by_cases hv : ¬ hazard q main i → ∀ x, intense main q (side i) x → - atReach c.items (c.wide+speed q.here i c.size) x - · exact ⟨c,h,Finset.Subset.refl _,by simp,id,Or.inl rfl,hv⟩ - push Not at hv - obtain ⟨hH,x,hx,hB⟩ := hv - obtain ⟨d,h1,h2,h3,h4,h5⟩ := append_choose _ _ hp he ha hn hm hM hH c h x hx hB - have hd : b-d.size < n := by - have H : d.size ≤ b := Finset.card_le_univ _; omega - obtain ⟨t,h₁,h₂,h₃,h₄,h₅,h₆⟩ := ih _ hd d rfl h4 - refine ⟨t,h₁,Finset.Subset.trans ?_ h₂,?_,fun hk => h₄ ?_,Or.inr - (expensive_intent main hp he ha hn hm hx), h₆⟩ - · rw [h1]; exact Finset.subset_insert _ _ - · rw [← h2] at h₃; push_cast at * - linarith - unfold localSize at *; rw [h3,← h2]; push_cast - have hi' : speed q.here i c.size ≤ speed q.here i (c.size+1) := speed_le le_rfl (by omega) - change c.wide+speed q.here i c.size ≤ _ - nlinarith [Nat.cast_nonneg (α:=ℝ) c.size] - -def zoom (s : Scene M) (i : ℕ) (main : Bool) (q : Chart M) : Chart M := - if h : ∃ c, Widen s i main q c then Classical.choose h else q - -lemma zoom_spec {s q : Scene M} {l} - (t : Chart M) (hp : t.Sound) (i : ℕ) (main : Bool) {f : Grid M → ℝ} - (h : Following j J f (Thread.ran s q l)) : - Widen q i main t (zoom q i main t) := by - unfold zoom; split - · exact Classical.choose_spec _ - · rename_i hF; apply False.elim; apply hF - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - have hS := hd.2.2; unfold enter at hS - have ht := hd.1.2.2 - exact widen_done main _ hc.within.1 (rooted _ (hk false)).within.2 hc.positive (by linarith) ht - (by linarith) _ hp - - -def goal (i : ℕ) (main : Bool) (q : Chart M) : List (Beat M) → Chart M - | [] => q - | b::l => goal i main (zoom b.post i main q) l - -def buildup (i : ℕ) (main : Bool) (q : Chart M) (l : List (Beat M)) := - ((goal i main q l).size:ℝ) - q.size - -lemma buildup_ran (i : ℕ) (main : Bool) (q : Chart M) (b : Beat M) (l : List (Beat M)) : - buildup i main q (b::l) = (zoom b.post i main q).size - (q.size:ℝ) + - buildup i main (zoom b.post i main q) l := by simp only [buildup,goal]; ring - -def chartDebt (s : Scene M) (j : ℕ) (q : Chart M) (b : Bool) := - flex s b q.refs+(minimumT j)⁻¹ - -lemma buildup_total (s : Scene M) (t : Thread M s) (f : Grid M → ℝ) - (h : Following j J f t) (main : Bool) (i : ℕ) (C : Chart M) (hC : C.Sound) - (hn : 0 < s.norm) : - (amp i main/2*price main i)*(s.here.dice M).Ex (fun x => buildup i main C (t.stream x)) ≤ - s.mass/s.norm*chartDebt s j C main := by - induction t generalizing C with - | blank s => - have hh := rooted _ h - rcases eq_or_lt_of_le hh.mass_lower with H|H - · simp [stream,buildup,goal,← H,Ex_const] - have hF := flex_nonneg _ ⟨hh.within,hh.positive⟩ H main _ (Chart.Sound.pos C hC) - have hp : 0 ≤ s.mass/s.norm*chartDebt s j C main := mul_nonneg (by positivity) - (add_nonneg hF (inv_nonneg.mpr (minimum_pos j).le)) - simpa only [stream,buildup,goal,sub_self,Ex_const,mul_zero] using hp - | ran s q k ih => - let Z := zoom q i main C - let A := amp i main; let b := price main i - let G (u : Chart M) (t : Scene M) := flex t main u.refs - let B := (minimumT j)⁻¹ - let v := step q; let m := (keep s q).snip - have hm' := pre_pos h - have hD : Widen q i main C Z := zoom_spec _ hC i main h - have hd := debt_at h - rw [stream_branch k h (fun l _ => buildup i main C l)] - have he b' : - (q.smaller b').norm = q.norm := rfl - obtain ⟨ha,hb,hc,hf,ht,hk⟩ := h - have hs' : ReadyAt s := ⟨ha.within,ha.positive⟩ - have hq' : ReadyAt q := ⟨hc.within,hc.positive⟩ - have hQ : 0 < q.norm := by rw [hb.1.flat]; exact hn - have hq := hf.1.2.2 - have hv : q.here.lev < M := (rooted _ (hk false)).within.2 - have hF := flex_down main q hc.within.1 hv hc.positive hq Z.refs (Chart.Sound.pos _ hD.sonic) - have hS := flex_keep main hb.1 hs' hq' hm' _ (Chart.Sound.pos _ hC) - have hK : m.probs true * s.mass=q.mass := (kept_mass hb.1 hs' hq' hm').symm - have hz : 0 ≤ (Z.size:ℝ)-C.size := by - apply sub_nonneg.mpr - exact_mod_cast Finset.card_le_card hD.extend - have HP : 0 ≤ b := by unfold b price; positivity - let d := (Z.size:ℝ)-C.size - have hP : A/2*b*d ≤ q.mass/q.norm*(b*d) := by - rcases hD.mass with H|H - · unfold d; simp [H] - · rw [mul_assoc] - exact mul_le_mul_of_nonneg_right H (mul_nonneg HP hz) - let D (u) := G Z (q.smaller u)+B - have hh (u : Bool) : - (A/2*b)* ((q.smaller u).here.dice M).Ex (fun x => buildup i main Z ((k u).stream x)) ≤ - (q.smaller u).mass/q.norm * D u := ih u (hk u) Z hD.sonic hQ - let U := uniform Bool - let x (u : Bool) := ((q.smaller u).here.dice M).Ex (fun x => buildup i main Z ((k u).stream x)) - - have HI : U.Ex (fun z => ((q.smaller z).mass/q.norm)*D z) ≤ - q.mass/q.norm*(G Z q +B) := by - have He := output_split q hc.within.1 hv hc.positive hq (fun z => q.norm⁻¹*D z) - have HM : (step q).probs.Ex D ≤ G Z q+B := by - unfold D; rw [Ex_add,Ex_const] - exact add_le_add_left hF B - rw [Ex_lmul] at He - calc - _ = q.mass*(q.norm⁻¹ * v.probs.Ex D) := by - unfold U v; rw [He]; congr 1; funext z; ring - _ ≤ _ := by - rw [← mul_assoc,← div_eq_mul_inv]; exact mul_le_mul_of_nonneg_left HM (by positivity) - have HS : - (A/2*b)*(U.Ex fun z => d+x z) ≤ q.mass/q.norm*(b*d+G Z q+B) := by - rw [Ex_add (f:=fun _ => d),Ex_const,mul_add,← Ex_lmul] - have HH := U.Ex_mono hh - change U.Ex (fun z => A/2*b*x z) ≤ _ at HH - apply le_trans (add_le_add hP (HH.trans HI)); rw [mul_add,mul_add] - exact le_of_eq (by ring) - simp_rw [buildup_ran,Ex_add,Ex_const] - have hhT : (A/2*b)*(d+U.Ex x) ≤ q.mass/q.norm*(b*d+G Z q+B) := by - rw [Ex_add (f:=fun _=>d),Ex_const] at HS; exact HS - - have hg3 : - q.mass/q.norm*(b*d+G Z q+B) ≤ s.mass/s.norm*(G C s+B) := by - rw [hb.1.flat,← hK] - have hx : b*d + G Z q ≤ G C q := by - have hi := hD.paid; change G Z q + b*d ≤ G C q at hi; linarith - have ht : m.probs true*(b*d+G Z q+B) ≤ G C s+B := by - change m.probs.entropy ≤ B*m.probs false at hd - change m.probs true*G C q ≤ G C s+m.probs.entropy at hS - have HQ : m.probs true + m.probs false = 1 := by simpa only [Fintype.sum_bool] using m.probs.total - nlinarith [m.probs.nonneg true,show B * (m.probs true + m.probs false) = B by rw [HQ]; ring] - rw [mul_div_assoc,mul_comm (m.probs true),mul_assoc (a:=s.mass/s.norm)] - exact mul_le_mul_of_nonneg_left ht (div_nonneg hm'.le hn.le) - change A/2*b*(d+U.Ex x) ≤ s.mass/s.norm*(G C s+B) - exact hhT.trans hg3 - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Append + +/-! # Adaptive frequency list along a plateau -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Chart + +variable {M J j : ℕ} +open scoped Classical + +def speed (p : Node) (i n : ℕ) := band p i (offset i n) +def horizon (p : Node) (i n : ℕ) := (1+n:ℝ)*speed p i n +def localSize (p : Node) (i : ℕ) (c : Chart M) := + c.wide ≤ c.size*speed p i c.size + +lemma speed_pos (p : Node) (i n : ℕ) : 0 ≤ speed p i n := by unfold speed band; positivity +lemma speed_le {p q : Node} (h : p.lev ≤ q.lev) {i n m : ℕ} (he : n ≤ m) : + speed p i n ≤ speed q i m := by + unfold speed band offset + gcongr; norm_num + +/-- One or several appends before a jump. -/ +structure Widen (p : Scene M) (i : ℕ) (main : Bool) (c d : Chart M) : Prop where + sonic : d.Sound + extend : c.items ⊆ d.items + paid : flex p main d.refs+price main i*(d.size-c.size:ℝ) ≤ flex p main c.refs + expands : localSize p.here i c → localSize p.here i d + mass : d.size=c.size ∨ amp i main/2 ≤ p.mass/p.norm + awake : ¬ hazard p main i → ∀ x, intense main p (side i) x → + atReach d.items (d.wide+speed p.here i d.size) x + +lemma widen_done (main : Bool) (i : ℕ) {q : Scene M} + (hp : q.here.valid) (he : q.here.lev < M) (ha : 0 ≤ q.wage) (hn : 0 < q.norm) + (hm : 0 < q.mass) (hM : q.mass ≤ 2*q.norm) + (c : Chart M) (h : c.Sound) : ∃ d, Widen q i main c d := by + classical + let b := Fintype.card (Grid M) + suffices H : ∀ n : ℕ, ∀ c : Chart M, b-c.size=n → c.Sound → + ∃ d, Widen q i main c d from H _ _ rfl h + intro n; induction n using Nat.strong_induction_on with + | h n ih => + intro c heq h + by_cases hv : ¬ hazard q main i → ∀ x, intense main q (side i) x → + atReach c.items (c.wide+speed q.here i c.size) x + · exact ⟨c,h,Finset.Subset.refl _,by simp,id,Or.inl rfl,hv⟩ + push Not at hv + obtain ⟨hH,x,hx,hB⟩ := hv + obtain ⟨d,h1,h2,h3,h4,h5⟩ := append_choose _ _ hp he ha hn hm hM hH c h x hx hB + have hd : b-d.size < n := by + have H : d.size ≤ b := Finset.card_le_univ _; omega + obtain ⟨t,h₁,h₂,h₃,h₄,h₅,h₆⟩ := ih _ hd d rfl h4 + refine ⟨t,h₁,Finset.Subset.trans ?_ h₂,?_,fun hk => h₄ ?_,Or.inr + (expensive_intent main hp he ha hn hm hx), h₆⟩ + · rw [h1]; exact Finset.subset_insert _ _ + · rw [← h2] at h₃; push_cast at * + linarith + unfold localSize at *; rw [h3,← h2]; push_cast + have hi' : speed q.here i c.size ≤ speed q.here i (c.size+1) := speed_le le_rfl (by omega) + change c.wide+speed q.here i c.size ≤ _ + nlinarith [Nat.cast_nonneg (α:=ℝ) c.size] + +def zoom (s : Scene M) (i : ℕ) (main : Bool) (q : Chart M) : Chart M := + if h : ∃ c, Widen s i main q c then Classical.choose h else q + +lemma zoom_spec {s q : Scene M} {l} + (t : Chart M) (hp : t.Sound) (i : ℕ) (main : Bool) {f : Grid M → ℝ} + (h : Following j J f (Thread.ran s q l)) : + Widen q i main t (zoom q i main t) := by + unfold zoom; split + · exact Classical.choose_spec _ + · rename_i hF; apply False.elim; apply hF + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + have hS := hd.2.2; unfold enter at hS + have ht := hd.1.2.2 + exact widen_done main _ hc.within.1 (rooted _ (hk false)).within.2 hc.positive (by linarith) ht + (by linarith) _ hp + + +def goal (i : ℕ) (main : Bool) (q : Chart M) : List (Beat M) → Chart M + | [] => q + | b::l => goal i main (zoom b.post i main q) l + +def buildup (i : ℕ) (main : Bool) (q : Chart M) (l : List (Beat M)) := + ((goal i main q l).size:ℝ) - q.size + +lemma buildup_ran (i : ℕ) (main : Bool) (q : Chart M) (b : Beat M) (l : List (Beat M)) : + buildup i main q (b::l) = (zoom b.post i main q).size - (q.size:ℝ) + + buildup i main (zoom b.post i main q) l := by simp only [buildup,goal]; ring + +def chartDebt (s : Scene M) (j : ℕ) (q : Chart M) (b : Bool) := + flex s b q.refs+(minimumT j)⁻¹ + +lemma buildup_total (s : Scene M) (t : Thread M s) (f : Grid M → ℝ) + (h : Following j J f t) (main : Bool) (i : ℕ) (C : Chart M) (hC : C.Sound) + (hn : 0 < s.norm) : + (amp i main/2*price main i)*(s.here.dice M).Ex (fun x => buildup i main C (t.stream x)) ≤ + s.mass/s.norm*chartDebt s j C main := by + induction t generalizing C with + | blank s => + have hh := rooted _ h + rcases eq_or_lt_of_le hh.mass_lower with H|H + · simp [stream,buildup,goal,← H,Ex_const] + have hF := flex_nonneg _ ⟨hh.within,hh.positive⟩ H main _ (Chart.Sound.pos C hC) + have hp : 0 ≤ s.mass/s.norm*chartDebt s j C main := mul_nonneg (by positivity) + (add_nonneg hF (inv_nonneg.mpr (minimum_pos j).le)) + simpa only [stream,buildup,goal,sub_self,Ex_const,mul_zero] using hp + | ran s q k ih => + let Z := zoom q i main C + let A := amp i main; let b := price main i + let G (u : Chart M) (t : Scene M) := flex t main u.refs + let B := (minimumT j)⁻¹ + let v := step q; let m := (keep s q).snip + have hm' := pre_pos h + have hD : Widen q i main C Z := zoom_spec _ hC i main h + have hd := debt_at h + rw [stream_branch k h (fun l _ => buildup i main C l)] + have he b' : + (q.smaller b').norm = q.norm := rfl + obtain ⟨ha,hb,hc,hf,ht,hk⟩ := h + have hs' : ReadyAt s := ⟨ha.within,ha.positive⟩ + have hq' : ReadyAt q := ⟨hc.within,hc.positive⟩ + have hQ : 0 < q.norm := by rw [hb.1.flat]; exact hn + have hq := hf.1.2.2 + have hv : q.here.lev < M := (rooted _ (hk false)).within.2 + have hF := flex_down main q hc.within.1 hv hc.positive hq Z.refs (Chart.Sound.pos _ hD.sonic) + have hS := flex_keep main hb.1 hs' hq' hm' _ (Chart.Sound.pos _ hC) + have hK : m.probs true * s.mass=q.mass := (kept_mass hb.1 hs' hq' hm').symm + have hz : 0 ≤ (Z.size:ℝ)-C.size := by + apply sub_nonneg.mpr + exact_mod_cast Finset.card_le_card hD.extend + have HP : 0 ≤ b := by unfold b price; positivity + let d := (Z.size:ℝ)-C.size + have hP : A/2*b*d ≤ q.mass/q.norm*(b*d) := by + rcases hD.mass with H|H + · unfold d; simp [H] + · rw [mul_assoc] + exact mul_le_mul_of_nonneg_right H (mul_nonneg HP hz) + let D (u) := G Z (q.smaller u)+B + have hh (u : Bool) : + (A/2*b)* ((q.smaller u).here.dice M).Ex (fun x => buildup i main Z ((k u).stream x)) ≤ + (q.smaller u).mass/q.norm * D u := ih u (hk u) Z hD.sonic hQ + let U := uniform Bool + let x (u : Bool) := ((q.smaller u).here.dice M).Ex (fun x => buildup i main Z ((k u).stream x)) + + have HI : U.Ex (fun z => ((q.smaller z).mass/q.norm)*D z) ≤ + q.mass/q.norm*(G Z q +B) := by + have He := output_split q hc.within.1 hv hc.positive hq (fun z => q.norm⁻¹*D z) + have HM : (step q).probs.Ex D ≤ G Z q+B := by + unfold D; rw [Ex_add,Ex_const] + exact add_le_add_left hF B + rw [Ex_lmul] at He + calc + _ = q.mass*(q.norm⁻¹ * v.probs.Ex D) := by + unfold U v; rw [He]; congr 1; funext z; ring + _ ≤ _ := by + rw [← mul_assoc,← div_eq_mul_inv]; exact mul_le_mul_of_nonneg_left HM (by positivity) + have HS : + (A/2*b)*(U.Ex fun z => d+x z) ≤ q.mass/q.norm*(b*d+G Z q+B) := by + rw [Ex_add (f:=fun _ => d),Ex_const,mul_add,← Ex_lmul] + have HH := U.Ex_mono hh + change U.Ex (fun z => A/2*b*x z) ≤ _ at HH + apply le_trans (add_le_add hP (HH.trans HI)); rw [mul_add,mul_add] + exact le_of_eq (by ring) + simp_rw [buildup_ran,Ex_add,Ex_const] + have hhT : (A/2*b)*(d+U.Ex x) ≤ q.mass/q.norm*(b*d+G Z q+B) := by + rw [Ex_add (f:=fun _=>d),Ex_const] at HS; exact HS + + have hg3 : + q.mass/q.norm*(b*d+G Z q+B) ≤ s.mass/s.norm*(G C s+B) := by + rw [hb.1.flat,← hK] + have hx : b*d + G Z q ≤ G C q := by + have hi := hD.paid; change G Z q + b*d ≤ G C q at hi; linarith + have ht : m.probs true*(b*d+G Z q+B) ≤ G C s+B := by + change m.probs.entropy ≤ B*m.probs false at hd + change m.probs true*G C q ≤ G C s+m.probs.entropy at hS + have HQ : m.probs true + m.probs false = 1 := by simpa only [Fintype.sum_bool] using m.probs.total + nlinarith [m.probs.nonneg true,show B * (m.probs true + m.probs false) = B by rw [HQ]; ring] + rw [mul_div_assoc,mul_comm (m.probs true),mul_assoc (a:=s.mass/s.norm)] + exact mul_le_mul_of_nonneg_left ht (div_nonneg hm'.le hn.le) + change A/2*b*(d+U.Ex x) ≤ s.mass/s.norm*(G C s+B) + exact hhT.trans hg3 + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Dual.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Dual.lean index dceef54b09583d40a33899b2773c293a60e79a10..cb213f44e9a2fc9cdb4a114ebc224244e088b3ff 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Dual.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Dual.lean @@ -1,157 +1,157 @@ -import OAI.Analysis.FourierLLogL.Capture.Criterion -import OAI.Analysis.FourierLLogL.Plateau.Features - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture - -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread -variable {M : ℕ} {s : Scene M} -open scoped Classical - -def utility (main : Bool) (k x : ℂ) := - if main then 2*(k*x).re-‖k‖^2 else (k*x).re - -def sales (main : Bool) (s : Scene M) (c : Grid M → ℂ) (i : Grid M) := - rung s (channel (optic s main)) (fun x z => utility main (c z) (chi i x)) - -lemma utility_bounds (main : Bool) (k x : ℂ) (hk : ‖k‖ ≤ 1) (hx : ‖x‖=1) : - |utility main k x| ≤ 3 := by - have hi : |(k*x).re| ≤ 1 := (Complex.abs_re_le_norm _).trans (by rw [norm_mul,hx,mul_one]; exact hk) - obtain ⟨h1,h2⟩ := abs_le.mp hi - rw [abs_le] - unfold utility; split <;> - · constructor <;> nlinarith [norm_nonneg k] - -lemma utility_diff (main : Bool) (k x y : ℂ) (hk : ‖k‖ ≤ 1) : - |utility main k x- utility main k y| ≤ 2* ‖x-y‖ := by - have he : |(k*x).re-(k*y).re| ≤ ‖x-y‖ := by - rw [← Complex.sub_re,←mul_sub] - exact (Complex.abs_re_le_norm _).trans (by - rw [norm_mul]; exact mul_le_of_le_one_left (norm_nonneg _) hk) - cases main - · change |(k*x).re-(k*y).re| ≤ _; linarith [norm_nonneg (x-y)] - change |2*_ -_-(2*_ -_)| ≤ _ - rw [sub_sub_sub_cancel_right,←mul_sub,abs_mul,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] - linarith - -private lemma income (main : Bool) (s : Scene M) (c : Grid M → ℂ) (i : Grid M) : - let A := optic s main - (sales main s c i) = ∑ z : Grid M, let f := fz s.here s.wage s.norm A i z - let H := hist s (channel A) z - if main then 2 * (c z*f).re - H*‖c z‖^2 - else (c z*f).re := by - intro A - unfold sales rung - rw [Finset.sum_comm] - refine Finset.sum_congr rfl fun z _ => ?_ - let l := fun x : Grid M => addMass s (channel A) x z - have hi : (c z * fz s.here s.wage s.norm A i z).re = - ∑ x : Grid M, l x * (c z * chi i x).re := by - unfold fz - rw [Finset.mul_sum,Complex.re_sum] - refine Finset.sum_congr rfl fun x hx => ?_ - rw [mul_left_comm, Complex.re_ofReal_mul] - rfl - change ∑ x, l x * utility main _ _ = (if _ then _ else _) - rw [hi] - split - case isTrue he => - simp_rw [show ∀ k v : ℂ, utility main k v = 2*(k*v).re-‖k‖^2 from fun _ _ => by simp [he,utility]] - rw [Finset.mul_sum,show hist s (channel A) z=∑ i, l i from rfl, Finset.sum_mul] - rw [← Finset.sum_sub_distrib]; exact Finset.sum_congr rfl fun _ _ => by ring - case isFalse he => - simp_rw [show ∀ k v : ℂ, utility main k v = (k*v).re from fun _ _ => by simp [he,utility]] - -private lemma fz_dry (s : Scene M) (i z : Grid M) : - fz s.here s.wage s.norm 0 i z = (side M:ℂ)*scalar s i := by - unfold fz omegaObs scalar; simp_rw [no_label,uniform_side,Complex.ofReal_mul,Finset.mul_sum] - exact Finset.sum_congr rfl fun _ _ => by ring - -private def seller (M : ℝ) (f : ℂ) : ℂ := conj f / M - -private lemma seller_limit (M : ℝ) (f : ℂ) (hf : ‖f‖ ≤ M) : ‖seller M f‖ ≤ 1 := by - have hx : 0 ≤ M := le_trans (norm_nonneg _) hf - unfold seller; rw [norm_div,Complex.norm_real,Real.norm_eq_abs, - abs_of_nonneg hx,Complex.norm_conj] - rcases eq_or_lt_of_le hx with h|h - · rw [← h,div_zero]; norm_num - exact (div_le_one h).mpr hf - -private lemma price_upper (m : ℝ) (f k : ℂ) (hf : ‖f‖ ≤ m) : - 2*(k*f).re - m*‖k‖^2 ≤ ‖f‖^2/m := by - have hx : 0 ≤ m := le_trans (norm_nonneg _) hf - rcases eq_or_lt_of_le hx with h|h - · have hh : f=0 := norm_le_zero_iff.mp (h ▸ hf) - simp [←h, hh] - · have hi : (k*f).re ≤ ‖k‖ * ‖f‖ := - (le_abs_self _).trans ((Complex.abs_re_le_norm _).trans (by rw [norm_mul])) - apply (le_div_iff₀ h).mpr - nlinarith [sq_nonneg (m *‖k‖ - ‖f‖),mul_le_mul_of_nonneg_left hi h.le] - -private lemma seller_eq (m : ℝ) (f : ℂ) (hf : ‖f‖ ≤ m) : - 2*(seller m f*f).re - m*‖seller m f‖^2= ‖f‖^2 / m := by - have hx : 0 ≤ m := le_trans (norm_nonneg _) hf - unfold seller - by_cases hm : m = 0 - · subst m; simp - · rw [div_mul_eq_mul_div,mul_comm (conj f) f,Complex.mul_conj, - ←Complex.sq_norm,← Complex.ofReal_div,norm_div,Complex.norm_real, - Real.norm_eq_abs,abs_of_nonneg hx,Complex.norm_conj,Complex.ofReal_re] - field_simp; ring - -/-- sales of every packet at most the magnitude, attained by choice of a bounded tester. -/ -theorem test_complete (main : Bool) (s : Scene M) (hp : 0 ≤ s.wage) (hv : 0 ≤ s.norm) - (i : Grid M) : - (∀ c : Grid M → ℂ, (∀ z, ‖c z‖ ≤ 1) → sales main s c i ≤ magnitude main s i) ∧ - ∃ c : Grid M → ℂ, (∀ z, ‖c z‖ ≤ 1) ∧ sales main s c i = magnitude main s i := by - cases main - case true => - let L (z) := Mz s.here s.wage s.norm s.fund.mood z - let f (z) := fz s.here s.wage s.norm s.fund.mood i z - have hf z : ‖f z‖ ≤ L z := fz_norm _ hp hv _ _ _ - constructor - · intro c _; rw [income] - apply Finset.sum_le_sum; intro j hj; exact price_upper (L j) (f j) _ (hf j) - refine ⟨fun i => seller (L i) (f i),fun i => seller_limit _ _ (hf i),?_⟩ - rw [income] - refine Finset.sum_congr rfl fun j _ => seller_eq (L j) (f j) (hf j) - case false => - let f := scalar s i - have h1 (c : Grid M → ℂ) : - sales false s c i = (uniform (Grid M)).Ex (fun z => (c z*f).re) := by - rw [income] - change ∑ z : Grid M,(c z*fz s.here s.wage s.norm 0 i z).re= _ - unfold Ex - refine Finset.sum_congr rfl fun z _ => ?_ - rw [fz_dry,mul_left_comm,Complex.re_ofReal_mul,uniform_side] - constructor - · intro c hc - rw [h1] - apply le_trans (b:=(uniform (Grid M)).Ex fun _ => ‖f‖) - · refine Ex_mono _ fun i => ?_ - apply (le_abs_self _).trans ((Complex.abs_re_le_norm _).trans _) - rw [norm_mul]; exact mul_le_of_le_one_left (norm_nonneg _) (hc i) - rw [Ex_const] - exact le_rfl - refine ⟨fun _ => polar f,fun _ => polar_unit f,?_⟩ - rw [h1,Ex_const] - exact polar_identity f - -lemma sup_children (main : Bool) (s : Scene M) (hp : s.here.valid) (h : s.here.lev < M) - (ha : 0 ≤ s.wage) (hb : 0 ≤ s.norm) (i : Grid M) : - ∃ b, magnitude main s i ≤ magnitude main (s.smaller b) i := by - obtain ⟨c,hc,H⟩ := (test_complete main s ha hb i).2 - let T (b : Bool) := sales main (s.smaller b) c i - have he : T false+T true= 2*sales main s c i := rung_split s hp h _ _ - by_contra! HH - have h1 (b : Bool) : T b ≤ magnitude main (s.smaller b) i := - (test_complete main (s.smaller b) ha hb i).1 _ hc - linarith [HH true,HH false,h1 true,h1 false] - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Criterion +import OAI.Analysis.FourierLLogL.Plateau.Features + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture + +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread +variable {M : ℕ} {s : Scene M} +open scoped Classical + +def utility (main : Bool) (k x : ℂ) := + if main then 2*(k*x).re-‖k‖^2 else (k*x).re + +def sales (main : Bool) (s : Scene M) (c : Grid M → ℂ) (i : Grid M) := + rung s (channel (optic s main)) (fun x z => utility main (c z) (chi i x)) + +lemma utility_bounds (main : Bool) (k x : ℂ) (hk : ‖k‖ ≤ 1) (hx : ‖x‖=1) : + |utility main k x| ≤ 3 := by + have hi : |(k*x).re| ≤ 1 := (Complex.abs_re_le_norm _).trans (by rw [norm_mul,hx,mul_one]; exact hk) + obtain ⟨h1,h2⟩ := abs_le.mp hi + rw [abs_le] + unfold utility; split <;> + · constructor <;> nlinarith [norm_nonneg k] + +lemma utility_diff (main : Bool) (k x y : ℂ) (hk : ‖k‖ ≤ 1) : + |utility main k x- utility main k y| ≤ 2* ‖x-y‖ := by + have he : |(k*x).re-(k*y).re| ≤ ‖x-y‖ := by + rw [← Complex.sub_re,←mul_sub] + exact (Complex.abs_re_le_norm _).trans (by + rw [norm_mul]; exact mul_le_of_le_one_left (norm_nonneg _) hk) + cases main + · change |(k*x).re-(k*y).re| ≤ _; linarith [norm_nonneg (x-y)] + change |2*_ -_-(2*_ -_)| ≤ _ + rw [sub_sub_sub_cancel_right,←mul_sub,abs_mul,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] + linarith + +private lemma income (main : Bool) (s : Scene M) (c : Grid M → ℂ) (i : Grid M) : + let A := optic s main + (sales main s c i) = ∑ z : Grid M, let f := fz s.here s.wage s.norm A i z + let H := hist s (channel A) z + if main then 2 * (c z*f).re - H*‖c z‖^2 + else (c z*f).re := by + intro A + unfold sales rung + rw [Finset.sum_comm] + refine Finset.sum_congr rfl fun z _ => ?_ + let l := fun x : Grid M => addMass s (channel A) x z + have hi : (c z * fz s.here s.wage s.norm A i z).re = + ∑ x : Grid M, l x * (c z * chi i x).re := by + unfold fz + rw [Finset.mul_sum,Complex.re_sum] + refine Finset.sum_congr rfl fun x hx => ?_ + rw [mul_left_comm, Complex.re_ofReal_mul] + rfl + change ∑ x, l x * utility main _ _ = (if _ then _ else _) + rw [hi] + split + case isTrue he => + simp_rw [show ∀ k v : ℂ, utility main k v = 2*(k*v).re-‖k‖^2 from fun _ _ => by simp [he,utility]] + rw [Finset.mul_sum,show hist s (channel A) z=∑ i, l i from rfl, Finset.sum_mul] + rw [← Finset.sum_sub_distrib]; exact Finset.sum_congr rfl fun _ _ => by ring + case isFalse he => + simp_rw [show ∀ k v : ℂ, utility main k v = (k*v).re from fun _ _ => by simp [he,utility]] + +private lemma fz_dry (s : Scene M) (i z : Grid M) : + fz s.here s.wage s.norm 0 i z = (side M:ℂ)*scalar s i := by + unfold fz omegaObs scalar; simp_rw [no_label,uniform_side,Complex.ofReal_mul,Finset.mul_sum] + exact Finset.sum_congr rfl fun _ _ => by ring + +private def seller (M : ℝ) (f : ℂ) : ℂ := conj f / M + +private lemma seller_limit (M : ℝ) (f : ℂ) (hf : ‖f‖ ≤ M) : ‖seller M f‖ ≤ 1 := by + have hx : 0 ≤ M := le_trans (norm_nonneg _) hf + unfold seller; rw [norm_div,Complex.norm_real,Real.norm_eq_abs, + abs_of_nonneg hx,Complex.norm_conj] + rcases eq_or_lt_of_le hx with h|h + · rw [← h,div_zero]; norm_num + exact (div_le_one h).mpr hf + +private lemma price_upper (m : ℝ) (f k : ℂ) (hf : ‖f‖ ≤ m) : + 2*(k*f).re - m*‖k‖^2 ≤ ‖f‖^2/m := by + have hx : 0 ≤ m := le_trans (norm_nonneg _) hf + rcases eq_or_lt_of_le hx with h|h + · have hh : f=0 := norm_le_zero_iff.mp (h ▸ hf) + simp [←h, hh] + · have hi : (k*f).re ≤ ‖k‖ * ‖f‖ := + (le_abs_self _).trans ((Complex.abs_re_le_norm _).trans (by rw [norm_mul])) + apply (le_div_iff₀ h).mpr + nlinarith [sq_nonneg (m *‖k‖ - ‖f‖),mul_le_mul_of_nonneg_left hi h.le] + +private lemma seller_eq (m : ℝ) (f : ℂ) (hf : ‖f‖ ≤ m) : + 2*(seller m f*f).re - m*‖seller m f‖^2= ‖f‖^2 / m := by + have hx : 0 ≤ m := le_trans (norm_nonneg _) hf + unfold seller + by_cases hm : m = 0 + · subst m; simp + · rw [div_mul_eq_mul_div,mul_comm (conj f) f,Complex.mul_conj, + ←Complex.sq_norm,← Complex.ofReal_div,norm_div,Complex.norm_real, + Real.norm_eq_abs,abs_of_nonneg hx,Complex.norm_conj,Complex.ofReal_re] + field_simp; ring + +/-- sales of every packet at most the magnitude, attained by choice of a bounded tester. -/ +theorem test_complete (main : Bool) (s : Scene M) (hp : 0 ≤ s.wage) (hv : 0 ≤ s.norm) + (i : Grid M) : + (∀ c : Grid M → ℂ, (∀ z, ‖c z‖ ≤ 1) → sales main s c i ≤ magnitude main s i) ∧ + ∃ c : Grid M → ℂ, (∀ z, ‖c z‖ ≤ 1) ∧ sales main s c i = magnitude main s i := by + cases main + case true => + let L (z) := Mz s.here s.wage s.norm s.fund.mood z + let f (z) := fz s.here s.wage s.norm s.fund.mood i z + have hf z : ‖f z‖ ≤ L z := fz_norm _ hp hv _ _ _ + constructor + · intro c _; rw [income] + apply Finset.sum_le_sum; intro j hj; exact price_upper (L j) (f j) _ (hf j) + refine ⟨fun i => seller (L i) (f i),fun i => seller_limit _ _ (hf i),?_⟩ + rw [income] + refine Finset.sum_congr rfl fun j _ => seller_eq (L j) (f j) (hf j) + case false => + let f := scalar s i + have h1 (c : Grid M → ℂ) : + sales false s c i = (uniform (Grid M)).Ex (fun z => (c z*f).re) := by + rw [income] + change ∑ z : Grid M,(c z*fz s.here s.wage s.norm 0 i z).re= _ + unfold Ex + refine Finset.sum_congr rfl fun z _ => ?_ + rw [fz_dry,mul_left_comm,Complex.re_ofReal_mul,uniform_side] + constructor + · intro c hc + rw [h1] + apply le_trans (b:=(uniform (Grid M)).Ex fun _ => ‖f‖) + · refine Ex_mono _ fun i => ?_ + apply (le_abs_self _).trans ((Complex.abs_re_le_norm _).trans _) + rw [norm_mul]; exact mul_le_of_le_one_left (norm_nonneg _) (hc i) + rw [Ex_const] + exact le_rfl + refine ⟨fun _ => polar f,fun _ => polar_unit f,?_⟩ + rw [h1,Ex_const] + exact polar_identity f + +lemma sup_children (main : Bool) (s : Scene M) (hp : s.here.valid) (h : s.here.lev < M) + (ha : 0 ≤ s.wage) (hb : 0 ≤ s.norm) (i : Grid M) : + ∃ b, magnitude main s i ≤ magnitude main (s.smaller b) i := by + obtain ⟨c,hc,H⟩ := (test_complete main s ha hb i).2 + let T (b : Bool) := sales main (s.smaller b) c i + have he : T false+T true= 2*sales main s c i := rung_split s hp h _ _ + by_contra! HH + have h1 (b : Bool) : T b ≤ magnitude main (s.smaller b) i := + (test_complete main (s.smaller b) ha hb i).1 _ hc + linarith [HH true,HH false,h1 true,h1 false] + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Flex.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Flex.lean index cca1b74652bdc6b696326574379569797a92e610..2af9eac3efaf2eed3418116fc35a22de9c0a0384 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Flex.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Flex.lean @@ -1,167 +1,167 @@ -import OAI.Analysis.FourierLLogL.Capture.Criterion -/-! # Entropy relative to the adaptive product -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread - -variable {M : ℕ} {s q : Scene M} -open scoped Classical - -abbrev ProductRef (M : ℕ) := Grid M → Grid M → ℝ -def normQ (s : Scene M) (f : ProductRef M) (z : Grid M) := - (s.here.dice M).Ex (f z) - -lemma normQ_pos (s : Scene M) (f : ProductRef M) (h : ∀ i j, 0 < f i j) (z) : - 0 < normQ s f z := by - let Q := s.here.dice M - obtain ⟨i,hi,he⟩ := Q.exists_le_Ex (f z) - exact lt_of_lt_of_le (h z i) he - -def flex (s : Scene M) (main : Bool) (Q : ProductRef M) := - deficit s (channel (optic s main)) + - (tracker s main).Ex (fun i => log (normQ s Q i.2)-log (Q i.2 i.1)) - -lemma flex_nonneg (s : Scene M) (h : ReadyAt s) (hm : 0 < s.mass) - (main : Bool) (f : ProductRef M) (hP : ∀ z y, 0 < f z y) : - 0 ≤ flex s main f := by - let A := optic s main - let k := channel A - let Q := s.actor.withNext k - let P := s.here.dice M - have hh (z : Grid M) : 0 < ∑ i, P i * f z i := normQ_pos s _ hP z - let Hq (z : Grid M) : Law (Grid M) := proportional (fun i => P i*f z i) - (fun i => mul_nonneg (P.nonneg _) (hP _ _).le) (hh z) - have hz (i z : Grid M) : Hq z i = P i*f z i / normQ s f z := rfl - have HV (x : Grid M × Grid M) (hx : 0 < Q x) : 0 < P x.1 := by - apply (dice_positive h.1).mpr - apply piece_support h.1 (actor_def h.1 h.2 hm) - exact pos_of_mul_pos_left hx ((k _).nonneg _) - have hi := Hcond_compare Q Prod.fst Prod.snd Hq (fun i hi => by - rw [hz]; exact div_pos (mul_pos (HV i hi) (hP _ _)) (normQ_pos s _ hP _)) - have he (i) (hx : 0 < Q i) : - -log (Hq i.2 i.1) = levels s+(log (normQ s f i.2)-log (f i.2 i.1)) := by - rw [hz,Real.log_div (mul_pos (HV i hx) (hP _ _)).ne' (normQ_pos s _ hP _).ne', - Real.log_mul (HV i hx).ne' (hP _ _).ne'] - have HE := (dice_positive h.1).mp (HV i hx) - unfold levels P - rw [dice_apply h.1,ite_eq_left HE,Real.log_mul (side_pos _).ne' - (inv_pos.mpr s.here.len_pos).ne'] - unfold len; simp_rw [side_pow_two,log_inv,log_pow] - rw [Nat.cast_sub h.1.2] - ring - rw [Ex_congr_mem _ he, Ex_add (f:=fun _ => levels s), Ex_const] at hi - change 0 ≤ levels s -Q.Hcond Prod.fst Prod.snd + Q.Ex _ - linarith - -lemma flex_one (s : Scene M) (main : Bool) : - flex s main (fun _ _ => 1) = deficit s (channel (optic s main)) := by - simp_rw [flex,normQ,Ex_const,log_one,sub_zero,add_zero] - - -lemma track_shift {s : Scene M} (main : Bool) {v : Fork M} (he : v.src = s) - (h : v.TrueLaw) (k : ∀ i, optic (v.dst i) main = optic s main) - (g : Bool → Grid M × Grid M → ℝ) : - v.probs.Ex (fun i => (tracker (v.dst i) main).Ex (g i)) = - (tracker s main).Ex (fun x => g (v.bit x.1) x) := by - subst s - unfold tracker - simp_rw [k,Ex_withNext] - exact expect_bit h (fun i x => (channel _ x).Ex (fun y => g i (x,y))) - -lemma flex_keep (main : Bool) {s q : Scene M} (h : Less s q) (hh : ReadyAt s) - (hp : ReadyAt q) (hm : 0 < s.mass) - (f : ProductRef M) (hF : ∀ i j, 0 < f i j) : - let P := (keep s q).snip.probs - P true * flex q main f ≤ flex s main f + P.entropy := by - intro P - let v := (keep s q).snip - have he := keep_true h hh hp hm - let A := optic s main; let k := channel A - have hX (i : Bool) : optic (v.dst i) main = A := by - cases i - · rfl - change optic q main=optic s main; simp [optic,h.fund] - have hi (i) : (v.dst i).here=s.here := keep_here h _ - let Z (i : Grid M × Grid M) := log (normQ s f i.2)-log (f i.2 i.1) - have hv : P.Ex (fun i => flex (v.dst i) main f) ≤ flex s main f + P.entropy := by - have hi' := track_shift main (v:=v) rfl he hX (fun _ => Z) - have heq (i z) : normQ (v.dst i) f z = normQ s f z := by unfold normQ; rw [hi] - have hE' : P.Ex (fun i => deficit (v.dst i) k) ≤ deficit s k + P.entropy := by - have he := conditioned_balance k v he - unfold deficit; rw [Ex_sub]; have hz (b) : levels (v.dst b)=levels s := by unfold levels; rw [hi] - simp_rw [hz,Ex_const] - have HE := old_entropy_cost v k - change _+_= s.actor.privateH k at he - unfold P v at * - linarith - have hF (i) : flex (v.dst i) main f = - deficit (v.dst i) k + (tracker (v.dst i) main).Ex Z := by - unfold flex; simp_rw [heq,hX]; rfl - simp_rw [hF,Ex_add] - change P.Ex _ + v.probs.Ex _ ≤ deficit s k + (tracker s main).Ex Z + _ - rw [hi'] - change _ + (tracker s main).Ex Z ≤ _ - linarith - rw [Ex,Fintype.sum_bool] at hv - have hE : 0 ≤ P false * flex (v.dst false) main f := by - rcases eq_or_lt_of_le (P.nonneg false) with H|H; rw [← H,zero_mul] - exact mul_nonneg H.le <| flex_nonneg _ (other_ok h hh) - ((true_mass he).mp H) _ _ hF - exact (le_add_of_nonneg_right hE).trans hv - -lemma flex_down (main : Bool) (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (ha : 0 ≤ s.wage) (hm : 0 < s.mass) - (f : ProductRef M) (hF : ∀ i j, 0 < f i j) : - (step s).probs.Ex (fun i => flex (s.smaller i) main f) ≤ flex s main f := by - let P := tracker s main; let v := step s - let k := channel (optic s main) - have hx (i : Bool) : channel (optic (s.smaller i) main) = k := rfl - let g (x : Grid M × Grid M) := v.bit x.1 - have H := step_true hp hh ha hm - let q (z : Grid M) (b : Bool) : ℝ := normQ (s.smaller b) f z/(2*normQ s f z) - have HQ (z i) : 0 < q z i := div_pos (normQ_pos _ _ hF _) (by have := normQ_pos s f hF z; positivity) - let Q (z : Grid M) : Law Bool := - ⟨q z,fun i => (HQ z i).le,by - rw [Fintype.sum_bool] - have HE : - normQ s f z=(normQ (s.smaller false) f z+normQ (s.smaller true) f z)/2 := - s.here.dice_child hp hh _ - have hh := normQ_pos s f hF z - simp only [q]; field_simp; linarith⟩ - have he := Hcond_compare P g Prod.snd Q (fun i _ => HQ _ _) - have hl (i : Grid M × Grid M) : -log (Q i.2 (g i)) = - log 2+log (normQ s f i.2)-log (normQ (s.smaller (g i)) f i.2) := by - change -log (q _ _) = _ - unfold q; have hx := normQ_pos s f hF i.2 - rw [log_div (normQ_pos _ _ hF _).ne' (by positivity),log_mul (by norm_num) hx.ne']; ring - rw [funext hl] at he - let D (b : Bool) (x : Grid M × Grid M) := - log (normQ (s.smaller b) f x.2)-log (f x.2 x.1) - have hi : P.Hcond g Prod.snd = sideH v k := rfl - rw [hi] at he - have hT : - v.probs.Ex (fun i => (tracker (s.smaller i) main).Ex (D i)) = P.Ex fun x => D (g x) x := - track_shift main (v:=v) rfl H (fun _ => rfl) D - have hhE : v.probs.Ex (fun b => (s.smaller b).actor.privateH k) = - s.actor.privateH k-sideH v k := by - have Hh := conditioned_balance k v H - change v.probs.Ex (fun b => (s.smaller b).actor.privateH k)+_=s.actor.privateH k at Hh - linarith - change v.probs.Ex (fun b => deficit _ k + (tracker _ _).Ex (D b)) ≤ _ - unfold flex deficit - simp_rw [levels_child s hh, Ex_add v.probs] - rw [show v.probs.Ex (fun x => _-_) = levels s-log 2-v.probs.Ex - (fun b => (s.smaller b).actor.privateH k) by rw [Ex_sub (f:=fun _ => levels s-log 2),Ex_const],hT,hhE] - unfold D at * - simp_rw [Ex_sub] at * - change _ ≤ levels s-s.actor.privateH k+(P.Ex _-P.Ex _) - rw [Ex_add (f:=fun _=>log 2),Ex_const] at he - linarith - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Criterion +/-! # Entropy relative to the adaptive product -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread + +variable {M : ℕ} {s q : Scene M} +open scoped Classical + +abbrev ProductRef (M : ℕ) := Grid M → Grid M → ℝ +def normQ (s : Scene M) (f : ProductRef M) (z : Grid M) := + (s.here.dice M).Ex (f z) + +lemma normQ_pos (s : Scene M) (f : ProductRef M) (h : ∀ i j, 0 < f i j) (z) : + 0 < normQ s f z := by + let Q := s.here.dice M + obtain ⟨i,hi,he⟩ := Q.exists_le_Ex (f z) + exact lt_of_lt_of_le (h z i) he + +def flex (s : Scene M) (main : Bool) (Q : ProductRef M) := + deficit s (channel (optic s main)) + + (tracker s main).Ex (fun i => log (normQ s Q i.2)-log (Q i.2 i.1)) + +lemma flex_nonneg (s : Scene M) (h : ReadyAt s) (hm : 0 < s.mass) + (main : Bool) (f : ProductRef M) (hP : ∀ z y, 0 < f z y) : + 0 ≤ flex s main f := by + let A := optic s main + let k := channel A + let Q := s.actor.withNext k + let P := s.here.dice M + have hh (z : Grid M) : 0 < ∑ i, P i * f z i := normQ_pos s _ hP z + let Hq (z : Grid M) : Law (Grid M) := proportional (fun i => P i*f z i) + (fun i => mul_nonneg (P.nonneg _) (hP _ _).le) (hh z) + have hz (i z : Grid M) : Hq z i = P i*f z i / normQ s f z := rfl + have HV (x : Grid M × Grid M) (hx : 0 < Q x) : 0 < P x.1 := by + apply (dice_positive h.1).mpr + apply piece_support h.1 (actor_def h.1 h.2 hm) + exact pos_of_mul_pos_left hx ((k _).nonneg _) + have hi := Hcond_compare Q Prod.fst Prod.snd Hq (fun i hi => by + rw [hz]; exact div_pos (mul_pos (HV i hi) (hP _ _)) (normQ_pos s _ hP _)) + have he (i) (hx : 0 < Q i) : + -log (Hq i.2 i.1) = levels s+(log (normQ s f i.2)-log (f i.2 i.1)) := by + rw [hz,Real.log_div (mul_pos (HV i hx) (hP _ _)).ne' (normQ_pos s _ hP _).ne', + Real.log_mul (HV i hx).ne' (hP _ _).ne'] + have HE := (dice_positive h.1).mp (HV i hx) + unfold levels P + rw [dice_apply h.1,ite_eq_left HE,Real.log_mul (side_pos _).ne' + (inv_pos.mpr s.here.len_pos).ne'] + unfold len; simp_rw [side_pow_two,log_inv,log_pow] + rw [Nat.cast_sub h.1.2] + ring + rw [Ex_congr_mem _ he, Ex_add (f:=fun _ => levels s), Ex_const] at hi + change 0 ≤ levels s -Q.Hcond Prod.fst Prod.snd + Q.Ex _ + linarith + +lemma flex_one (s : Scene M) (main : Bool) : + flex s main (fun _ _ => 1) = deficit s (channel (optic s main)) := by + simp_rw [flex,normQ,Ex_const,log_one,sub_zero,add_zero] + + +lemma track_shift {s : Scene M} (main : Bool) {v : Fork M} (he : v.src = s) + (h : v.TrueLaw) (k : ∀ i, optic (v.dst i) main = optic s main) + (g : Bool → Grid M × Grid M → ℝ) : + v.probs.Ex (fun i => (tracker (v.dst i) main).Ex (g i)) = + (tracker s main).Ex (fun x => g (v.bit x.1) x) := by + subst s + unfold tracker + simp_rw [k,Ex_withNext] + exact expect_bit h (fun i x => (channel _ x).Ex (fun y => g i (x,y))) + +lemma flex_keep (main : Bool) {s q : Scene M} (h : Less s q) (hh : ReadyAt s) + (hp : ReadyAt q) (hm : 0 < s.mass) + (f : ProductRef M) (hF : ∀ i j, 0 < f i j) : + let P := (keep s q).snip.probs + P true * flex q main f ≤ flex s main f + P.entropy := by + intro P + let v := (keep s q).snip + have he := keep_true h hh hp hm + let A := optic s main; let k := channel A + have hX (i : Bool) : optic (v.dst i) main = A := by + cases i + · rfl + change optic q main=optic s main; simp [optic,h.fund] + have hi (i) : (v.dst i).here=s.here := keep_here h _ + let Z (i : Grid M × Grid M) := log (normQ s f i.2)-log (f i.2 i.1) + have hv : P.Ex (fun i => flex (v.dst i) main f) ≤ flex s main f + P.entropy := by + have hi' := track_shift main (v:=v) rfl he hX (fun _ => Z) + have heq (i z) : normQ (v.dst i) f z = normQ s f z := by unfold normQ; rw [hi] + have hE' : P.Ex (fun i => deficit (v.dst i) k) ≤ deficit s k + P.entropy := by + have he := conditioned_balance k v he + unfold deficit; rw [Ex_sub]; have hz (b) : levels (v.dst b)=levels s := by unfold levels; rw [hi] + simp_rw [hz,Ex_const] + have HE := old_entropy_cost v k + change _+_= s.actor.privateH k at he + unfold P v at * + linarith + have hF (i) : flex (v.dst i) main f = + deficit (v.dst i) k + (tracker (v.dst i) main).Ex Z := by + unfold flex; simp_rw [heq,hX]; rfl + simp_rw [hF,Ex_add] + change P.Ex _ + v.probs.Ex _ ≤ deficit s k + (tracker s main).Ex Z + _ + rw [hi'] + change _ + (tracker s main).Ex Z ≤ _ + linarith + rw [Ex,Fintype.sum_bool] at hv + have hE : 0 ≤ P false * flex (v.dst false) main f := by + rcases eq_or_lt_of_le (P.nonneg false) with H|H; rw [← H,zero_mul] + exact mul_nonneg H.le <| flex_nonneg _ (other_ok h hh) + ((true_mass he).mp H) _ _ hF + exact (le_add_of_nonneg_right hE).trans hv + +lemma flex_down (main : Bool) (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (ha : 0 ≤ s.wage) (hm : 0 < s.mass) + (f : ProductRef M) (hF : ∀ i j, 0 < f i j) : + (step s).probs.Ex (fun i => flex (s.smaller i) main f) ≤ flex s main f := by + let P := tracker s main; let v := step s + let k := channel (optic s main) + have hx (i : Bool) : channel (optic (s.smaller i) main) = k := rfl + let g (x : Grid M × Grid M) := v.bit x.1 + have H := step_true hp hh ha hm + let q (z : Grid M) (b : Bool) : ℝ := normQ (s.smaller b) f z/(2*normQ s f z) + have HQ (z i) : 0 < q z i := div_pos (normQ_pos _ _ hF _) (by have := normQ_pos s f hF z; positivity) + let Q (z : Grid M) : Law Bool := + ⟨q z,fun i => (HQ z i).le,by + rw [Fintype.sum_bool] + have HE : + normQ s f z=(normQ (s.smaller false) f z+normQ (s.smaller true) f z)/2 := + s.here.dice_child hp hh _ + have hh := normQ_pos s f hF z + simp only [q]; field_simp; linarith⟩ + have he := Hcond_compare P g Prod.snd Q (fun i _ => HQ _ _) + have hl (i : Grid M × Grid M) : -log (Q i.2 (g i)) = + log 2+log (normQ s f i.2)-log (normQ (s.smaller (g i)) f i.2) := by + change -log (q _ _) = _ + unfold q; have hx := normQ_pos s f hF i.2 + rw [log_div (normQ_pos _ _ hF _).ne' (by positivity),log_mul (by norm_num) hx.ne']; ring + rw [funext hl] at he + let D (b : Bool) (x : Grid M × Grid M) := + log (normQ (s.smaller b) f x.2)-log (f x.2 x.1) + have hi : P.Hcond g Prod.snd = sideH v k := rfl + rw [hi] at he + have hT : + v.probs.Ex (fun i => (tracker (s.smaller i) main).Ex (D i)) = P.Ex fun x => D (g x) x := + track_shift main (v:=v) rfl H (fun _ => rfl) D + have hhE : v.probs.Ex (fun b => (s.smaller b).actor.privateH k) = + s.actor.privateH k-sideH v k := by + have Hh := conditioned_balance k v H + change v.probs.Ex (fun b => (s.smaller b).actor.privateH k)+_=s.actor.privateH k at Hh + linarith + change v.probs.Ex (fun b => deficit _ k + (tracker _ _).Ex (D b)) ≤ _ + unfold flex deficit + simp_rw [levels_child s hh, Ex_add v.probs] + rw [show v.probs.Ex (fun x => _-_) = levels s-log 2-v.probs.Ex + (fun b => (s.smaller b).actor.privateH k) by rw [Ex_sub (f:=fun _ => levels s-log 2),Ex_const],hT,hhE] + unfold D at * + simp_rw [Ex_sub] at * + change _ ≤ levels s-s.actor.privateH k+(P.Ex _-P.Ex _) + rw [Ex_add (f:=fun _=>log 2),Ex_const] at he + linarith + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/Mask.lean b/lean/OAI/Analysis/FourierLLogL/Capture/Mask.lean index f05b4de54ad39522ad81a7d5c7864bd2d435d6cb..5ea9f9a1138269bdca0c35582d02a4ecf927479f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/Mask.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/Mask.lean @@ -1,196 +1,196 @@ -import OAI.Analysis.FourierLLogL.Capture.MaskKernel -import OAI.Analysis.FourierLLogL.Finite.Tail -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {p : Node} {M : ℕ} -open scoped Classical - -lemma margins (p : Node) (hp : p.valid) : 0 ≤ p.a ∧ p.a+p.len ≤ 1 := by - unfold a - have he : 0 < p.len := p.len_pos - refine ⟨by positivity,?_⟩ - calc - _ = ↑(p.ix+1)*p.len := by push_cast; ring - _ ≤ ↑(2^p.lev:ℕ)*p.len := - mul_le_mul_of_nonneg_right (Nat.cast_le.mpr hp) he.le - _ = _ := by unfold len; push_cast; rw [mul_comm,side_card] - -lemma inner_strip (p : Node) (b : Bool) (i : ℕ) (x : Grid M) - (hx : x∈(child p b).block M) (hh : ¬ flanked p i x) : - let q := child p b; let r := p.len*side (reach i) - q.a+r ≤ pos x ∧ pos x < q.a+q.len-r := by - intro q r - have hi (c : Bool) : ¬ (x∈(strip p i b c).block M) := - fun h => hh ⟨_,_,h⟩ - have h1 : (strip p i b false).a=q.a := extreme_left q _ - have h2 (c : Bool) : (strip p i b c).len=r := strip_length _ _ _ _ - have h3 : (strip p i b true).a + r=q.a+q.len := by - rw [← h2 true]; exact extreme_right _ _ - have Hp : x∈q.block M:=hx - constructor - · by_contra! h - apply hi false - change pos x∈Ico _ _ - rw [h1,h2] - exact ⟨Hp.1,h⟩ - · by_contra! h - apply hi true - change pos x∈Ico _ _ - rw [h2,h3]; exact ⟨by linarith,Hp.2⟩ - -lemma circle_near (p : Node) (hp : p.valid) (x y : Grid M) (r : ℝ) - (hr : 0 < r) (hx : p.a+r ≤ pos x) (he : pos x ≤ p.a+p.len-r) - (hy : Dyadic.diam (x-y) linarith - rw [diam_min,min_lt_iff,pos_neg H] at hy - rcases le_total x.val y.val with h|h - · have h1 := pos_sub_val h - rw [show y-x= -(x-y) by ring,pos_neg H] at h1 - rcases hy with hy|hy <;> constructor <;> linarith [pos_lt y,pos_lt (x-y),pos_nonneg y] - have h1 := pos_sub_val h - rcases hy with hy|hy <;> constructor <;> linarith [pos_nonneg y,pos_lt y,pos_nonneg (x-y)] - -private def inland (p : Node) (r : ℝ) (x : Grid M) := ∀ y, Dyadic.diam (x-y) P.prob fun y => q (x-y)) r := by - have He (x : Grid M) : - P.prob (fun y => q (x-y)) = ∑ y, (if q y then P (x-y) else 0) := by - rw [prob_eq] - apply Fintype.sum_equiv ((Equiv.neg _).trans (Equiv.addLeft x)) - intro i - have HH : x-i=x+ -i := sub_eq_add_neg x i - change (if q (x-i) then P i else 0) = if q (x+ -i) then P (x-(x+ -i)) else 0; rw [← HH,sub_sub_self] - unfold limitR limit; intro i hi - simp_rw [He,Complex.ofReal_sum] - rw [fc_sum _ (fun y x => (↑(if q y then P (x-y) else 0):ℂ))] - apply Finset.sum_eq_zero; intro x hx - by_cases H : q x - · simp only [H,ite_true] - rw [fc_right_translate (fun x => (P x:ℂ)) x i, h _ hi,mul_zero] - · simp only [H,ite_false,fc, Complex.ofReal_zero,zero_mul,expU,Finset.sum_const_zero,smul_zero] - -def band (p : Node) (i j : ℕ) : ℝ := (2:ℝ)^(p.lev+reach i+10)*(2*(j+10)) - -lemma make_mask (p : Node) (h : p.valid) (hp : p.lev < M) (b : Bool) (i j : ℕ) : - let q := p.child b - ∃ f : Grid M → ℝ, limitR f (band p i j) ∧ (∀ x, 0 ≤ f x ∧ f x ≤ 1) ∧ - (∀ x, x∈p.block M → ¬ (lanes p i M ∧ flanked p i x) → - |(if x∈q.block M then (1:ℝ) else 0)-f x| ≤ side j) ∧ - expU (fun x => if x∈p.block M then 0 else f x) ≤ p.len*side j := by - classical - let s : ℝ := p.len*side (reach i) - intro q - have Hq := child_ok p h hp b - have hpq := block_of_below (child_below p b) M - let D := 2^(p.lev+reach i+10) - have hD : 0 - let f (x : Grid M) := if x∈q.block M then (1:ℝ) else 0 - have hj := side_pos j - refine ⟨f,limit_full _ ?_, fun x => by unfold f; split_ifs <;> norm_num,fun x _ _ => by unfold f; simp [hj.le],?_⟩ - · rw [He]; have hx : (2:ℝ)^M ≤ D := by exact_mod_cast le_of_not_ge H - nlinarith [Nat.cast_nonneg (α:=ℝ) j] - have HE (x : Grid M) : (if x∈p.block M then 0 else f x) = 0 := by - unfold f - split_ifs with h hq - · rfl - · exact (h (hpq hq)).elim - rfl - simp_rw [HE,expU,Finset.sum_const_zero,smul_zero]; exact (mul_pos p.len_pos hj).le - - obtain ⟨P,hP,hT⟩ := boosted M D j hD H - let r : ℝ := 16/D - have hr : 0 g (x-i)) - have hHi (x : Grid M) (hx : g x) : x∈q.block M := by - apply hx x - simpa only [sub_self,diam,rad, neg_zero,ZMod.val_zero,min_self,Nat.cast_zero,mul_zero] using hr - refine ⟨f,smoothing_limit _ _ _ ?_,fun x => ⟨prob_nonneg _ _,prob_le_one _ _⟩,?_,?_⟩ - · rw [He,show (D:ℝ)*(2*(j+10))=2*(D:ℝ)*(j+10) by ring] - exact hP - · intro x hx hE - have hv : ¬ flanked p i x := fun hh => hE ⟨hb,hh⟩ - by_cases HH : x∈q.block M - · have hi := inner_strip p b i x HH hv - have hi (v : Grid M) (hv : Dyadic.diam v ?_).trans hT - exact le_of_not_gt (fun h => hv (hi i h)) - rw [ite_eq_right HH,zero_sub,abs_neg,abs_of_nonneg (prob_nonneg _ _)] - apply le_trans _ hT - refine prob_mono _ _ _ fun i hi => ?_ - apply le_of_not_gt; intro hi' - apply HH - apply hi x; rw [sub_sub_cancel_left,diam_even]; exact hi' - rw [expU_ex] - let U := uniform (Grid M) - let G (x i : Grid M) := if x∈p.block M then (0:ℝ) else if g (x-i) then 1 else 0 - have heq (x : Grid M) : (if x∈p.block M then 0 else f x) = P.Ex (G x) := by - unfold f prob G; split_ifs with hx - rw [Ex_const] - rfl - have hg1 : U.prob g ≤ p.len := by - rw [← prob_block (M:=M) (p:=p) ⟨h,hp.le⟩] - apply prob_mono _ _ _ (fun i hi => hpq (hHi _ hi)) - have heq' (i : Grid M) : U.Ex (fun x => G x i) ≤ p.len*(if r ≤ Dyadic.diam i then 1 else 0) := by - split_ifs with H - · rw [mul_one] - apply le_trans (b:=U.prob fun x => g (x-i)) - · unfold prob G; apply Ex_mono; intro x; split_ifs <;> norm_num - rw [prob_translate g]; exact hg1 - have he (x : Grid M) : G x i=0 := by - unfold G; split_ifs with h h' - · rfl - · refine (h (hpq (h' x ?_))).elim - rw [sub_sub_cancel_left,diam_even] - exact not_le.mp H - rfl - rw [funext he,Ex_const,mul_zero] - simp_rw [heq,Ex_swap (uniform (Grid M)) P] - apply le_trans (Ex_mono P heq') - rw [Ex_lmul] - apply mul_le_mul_of_nonneg_left _ p.len_pos.le - apply le_trans _ hT - - rw [prob_eq] - unfold Ex r - simp - -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Capture.MaskKernel +import OAI.Analysis.FourierLLogL.Finite.Tail +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {p : Node} {M : ℕ} +open scoped Classical + +lemma margins (p : Node) (hp : p.valid) : 0 ≤ p.a ∧ p.a+p.len ≤ 1 := by + unfold a + have he : 0 < p.len := p.len_pos + refine ⟨by positivity,?_⟩ + calc + _ = ↑(p.ix+1)*p.len := by push_cast; ring + _ ≤ ↑(2^p.lev:ℕ)*p.len := + mul_le_mul_of_nonneg_right (Nat.cast_le.mpr hp) he.le + _ = _ := by unfold len; push_cast; rw [mul_comm,side_card] + +lemma inner_strip (p : Node) (b : Bool) (i : ℕ) (x : Grid M) + (hx : x∈(child p b).block M) (hh : ¬ flanked p i x) : + let q := child p b; let r := p.len*side (reach i) + q.a+r ≤ pos x ∧ pos x < q.a+q.len-r := by + intro q r + have hi (c : Bool) : ¬ (x∈(strip p i b c).block M) := + fun h => hh ⟨_,_,h⟩ + have h1 : (strip p i b false).a=q.a := extreme_left q _ + have h2 (c : Bool) : (strip p i b c).len=r := strip_length _ _ _ _ + have h3 : (strip p i b true).a + r=q.a+q.len := by + rw [← h2 true]; exact extreme_right _ _ + have Hp : x∈q.block M:=hx + constructor + · by_contra! h + apply hi false + change pos x∈Ico _ _ + rw [h1,h2] + exact ⟨Hp.1,h⟩ + · by_contra! h + apply hi true + change pos x∈Ico _ _ + rw [h2,h3]; exact ⟨by linarith,Hp.2⟩ + +lemma circle_near (p : Node) (hp : p.valid) (x y : Grid M) (r : ℝ) + (hr : 0 < r) (hx : p.a+r ≤ pos x) (he : pos x ≤ p.a+p.len-r) + (hy : Dyadic.diam (x-y) linarith + rw [diam_min,min_lt_iff,pos_neg H] at hy + rcases le_total x.val y.val with h|h + · have h1 := pos_sub_val h + rw [show y-x= -(x-y) by ring,pos_neg H] at h1 + rcases hy with hy|hy <;> constructor <;> linarith [pos_lt y,pos_lt (x-y),pos_nonneg y] + have h1 := pos_sub_val h + rcases hy with hy|hy <;> constructor <;> linarith [pos_nonneg y,pos_lt y,pos_nonneg (x-y)] + +private def inland (p : Node) (r : ℝ) (x : Grid M) := ∀ y, Dyadic.diam (x-y) P.prob fun y => q (x-y)) r := by + have He (x : Grid M) : + P.prob (fun y => q (x-y)) = ∑ y, (if q y then P (x-y) else 0) := by + rw [prob_eq] + apply Fintype.sum_equiv ((Equiv.neg _).trans (Equiv.addLeft x)) + intro i + have HH : x-i=x+ -i := sub_eq_add_neg x i + change (if q (x-i) then P i else 0) = if q (x+ -i) then P (x-(x+ -i)) else 0; rw [← HH,sub_sub_self] + unfold limitR limit; intro i hi + simp_rw [He,Complex.ofReal_sum] + rw [fc_sum _ (fun y x => (↑(if q y then P (x-y) else 0):ℂ))] + apply Finset.sum_eq_zero; intro x hx + by_cases H : q x + · simp only [H,ite_true] + rw [fc_right_translate (fun x => (P x:ℂ)) x i, h _ hi,mul_zero] + · simp only [H,ite_false,fc, Complex.ofReal_zero,zero_mul,expU,Finset.sum_const_zero,smul_zero] + +def band (p : Node) (i j : ℕ) : ℝ := (2:ℝ)^(p.lev+reach i+10)*(2*(j+10)) + +lemma make_mask (p : Node) (h : p.valid) (hp : p.lev < M) (b : Bool) (i j : ℕ) : + let q := p.child b + ∃ f : Grid M → ℝ, limitR f (band p i j) ∧ (∀ x, 0 ≤ f x ∧ f x ≤ 1) ∧ + (∀ x, x∈p.block M → ¬ (lanes p i M ∧ flanked p i x) → + |(if x∈q.block M then (1:ℝ) else 0)-f x| ≤ side j) ∧ + expU (fun x => if x∈p.block M then 0 else f x) ≤ p.len*side j := by + classical + let s : ℝ := p.len*side (reach i) + intro q + have Hq := child_ok p h hp b + have hpq := block_of_below (child_below p b) M + let D := 2^(p.lev+reach i+10) + have hD : 0 + let f (x : Grid M) := if x∈q.block M then (1:ℝ) else 0 + have hj := side_pos j + refine ⟨f,limit_full _ ?_, fun x => by unfold f; split_ifs <;> norm_num,fun x _ _ => by unfold f; simp [hj.le],?_⟩ + · rw [He]; have hx : (2:ℝ)^M ≤ D := by exact_mod_cast le_of_not_ge H + nlinarith [Nat.cast_nonneg (α:=ℝ) j] + have HE (x : Grid M) : (if x∈p.block M then 0 else f x) = 0 := by + unfold f + split_ifs with h hq + · rfl + · exact (h (hpq hq)).elim + rfl + simp_rw [HE,expU,Finset.sum_const_zero,smul_zero]; exact (mul_pos p.len_pos hj).le + + obtain ⟨P,hP,hT⟩ := boosted M D j hD H + let r : ℝ := 16/D + have hr : 0 g (x-i)) + have hHi (x : Grid M) (hx : g x) : x∈q.block M := by + apply hx x + simpa only [sub_self,diam,rad, neg_zero,ZMod.val_zero,min_self,Nat.cast_zero,mul_zero] using hr + refine ⟨f,smoothing_limit _ _ _ ?_,fun x => ⟨prob_nonneg _ _,prob_le_one _ _⟩,?_,?_⟩ + · rw [He,show (D:ℝ)*(2*(j+10))=2*(D:ℝ)*(j+10) by ring] + exact hP + · intro x hx hE + have hv : ¬ flanked p i x := fun hh => hE ⟨hb,hh⟩ + by_cases HH : x∈q.block M + · have hi := inner_strip p b i x HH hv + have hi (v : Grid M) (hv : Dyadic.diam v ?_).trans hT + exact le_of_not_gt (fun h => hv (hi i h)) + rw [ite_eq_right HH,zero_sub,abs_neg,abs_of_nonneg (prob_nonneg _ _)] + apply le_trans _ hT + refine prob_mono _ _ _ fun i hi => ?_ + apply le_of_not_gt; intro hi' + apply HH + apply hi x; rw [sub_sub_cancel_left,diam_even]; exact hi' + rw [expU_ex] + let U := uniform (Grid M) + let G (x i : Grid M) := if x∈p.block M then (0:ℝ) else if g (x-i) then 1 else 0 + have heq (x : Grid M) : (if x∈p.block M then 0 else f x) = P.Ex (G x) := by + unfold f prob G; split_ifs with hx + rw [Ex_const] + rfl + have hg1 : U.prob g ≤ p.len := by + rw [← prob_block (M:=M) (p:=p) ⟨h,hp.le⟩] + apply prob_mono _ _ _ (fun i hi => hpq (hHi _ hi)) + have heq' (i : Grid M) : U.Ex (fun x => G x i) ≤ p.len*(if r ≤ Dyadic.diam i then 1 else 0) := by + split_ifs with H + · rw [mul_one] + apply le_trans (b:=U.prob fun x => g (x-i)) + · unfold prob G; apply Ex_mono; intro x; split_ifs <;> norm_num + rw [prob_translate g]; exact hg1 + have he (x : Grid M) : G x i=0 := by + unfold G; split_ifs with h h' + · rfl + · refine (h (hpq (h' x ?_))).elim + rw [sub_sub_cancel_left,diam_even] + exact not_le.mp H + rfl + rw [funext he,Ex_const,mul_zero] + simp_rw [heq,Ex_swap (uniform (Grid M)) P] + apply le_trans (Ex_mono P heq') + rw [Ex_lmul] + apply mul_le_mul_of_nonneg_left _ p.len_pos.le + apply le_trans _ hT + + rw [prob_eq] + unfold Ex r + simp + +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Capture/MaskKernel.lean b/lean/OAI/Analysis/FourierLLogL/Capture/MaskKernel.lean index cd92d61042a3b41b10412a27ebed6a6fb080b9c6..2ef46b0888e377ec4c695ed642f071b8c88802c5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Capture/MaskKernel.lean +++ b/lean/OAI/Analysis/FourierLLogL/Capture/MaskKernel.lean @@ -1,243 +1,243 @@ -import OAI.Analysis.FourierLLogL.Plateau.Thread - -/-! # Band-limited cutoffs -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Capture -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {M : ℕ} -open scoped Classical - -def limit (f : Grid M → ℂ) (r : ℝ) := ∀ x, r < rad x → fc f x=0 -def limitR (f : Grid M → ℝ) := limit (fun x => (f x:ℂ)) - -lemma limit_mul (f g : Grid M → ℂ) (p q : ℝ) (hf : limit f p) (hg : limit g q) : - limit (fun x => f x*g x) (p+q) := by - intro x hx; rw [fc_prod]; apply Finset.sum_eq_zero; intro i _ - by_cases hh : p < rad i - · rw [hf i hh,zero_mul] - have H : q < rad (x-i) := by - have Hh := rad_add (x-i) i; rw [sub_add_cancel] at Hh - linarith - rw [hg _ H,mul_zero] -lemma limit_scale (f : Grid M → ℂ) (c : ℂ) (p : ℝ) (hf : limit f p) : - limit (fun x => c*f x) p := by intro x hx; rw [fc_lmul,hf x hx,mul_zero] -lemma limit_const (p : ℝ) (hp : 0 ≤ p) : limit (M:=M) (fun _ => 1) p := by - intro x hx; rw [← funext chi_zero_left,fc_chi,ite_eq_right] - intro h; subst x; simp [rad] at hx; linarith - -lemma limit_pow (f : Grid M → ℂ) (p : ℝ) (hf : limit f p) (k : ℕ) : - limit (fun x => f x^k) (k*p) := by - induction k with - | zero => simpa only [Nat.cast_zero,zero_mul,pow_zero] using limit_const (M:=M) 0 le_rfl - | succ k ih => - simp_rw [pow_succ,Nat.cast_add_one,add_mul,one_mul] - exact limit_mul _ _ _ p ih hf -lemma limit_star (f : Grid M → ℂ) (p : ℝ) (hf : limit f p) : - limit (fun x => conj (f x)) p := by - intro x hx - have hh : fc (fun x => conj (f x)) x = conj (fc f (-x)) := by - unfold fc - simp_rw [expU_cx,← Cx_conj,chi_neg', map_mul] - simp - rw [hh,hf _ (by rw [rad_even]; exact hx),map_zero] - -/-- Dirichlet factor normalized to one. -/ -private def tile (M d : ℕ) (x : Grid M) := (d:ℂ)⁻¹ * ∑ i∈Finset.range d, chi (i:Grid M) x -def slab (M d : ℕ) (x : Grid M) := ‖tile M d x‖^2 - -private lemma tile_limit (M d : ℕ) : limit (tile M d) d := by - intro x hx - unfold tile; rw [fc_lmul,fc_sum _ (fun i : ℕ => chi (i:Grid M))] - suffices H : ∀ i ∈ Finset.range d, fc (chi (i:Grid M)) x=0 by simp [Finset.sum_eq_zero H] - intro i hi; rw [fc_chi,ite_eq_right] - intro h; subst x - have H := rad_le_nat (M:=M) i - have hi : (i:ℝ) < d := Nat.cast_lt.mpr (Finset.mem_range.mp hi) - linarith - -lemma slab_limit (M d : ℕ) : limitR (slab M d) (2*d) := by - have hi (x : Grid M) : (↑(slab M d x):ℂ) = - tile M d x * conj (tile M d x) := by rw [slab,Complex.mul_conj,Complex.sq_norm] - have h := tile_limit M d - unfold limitR; simp_rw [hi, two_mul] - exact limit_mul _ _ _ _ h (limit_star _ _ h) - -lemma slab_nonneg (M d : ℕ) (x : Grid M) : 0 ≤ slab M d x := by unfold slab; positivity - -lemma slab_total (M : ℕ) (d : ℕ) (h : 0 < d) (he : d ≤ 2^M) : - expU (slab M d) = (d:ℝ)⁻¹ := by - change expU (fun x => ‖tile M d x‖^2) = _ - rw [← fc_parseval] - have H (x : Grid M) : fc (tile M d) x = if x∈Dyadic.first M d then (d:ℂ)⁻¹ else 0 := by - unfold tile; rw [fc_lmul,fc_sum _ (fun i : ℕ => chi (i:Grid M))] - simp_rw [fc_chi] - by_cases hx : x∈Dyadic.first M d - · rw [ite_eq_left hx] - rw [Finset.sum_eq_single x.val, ite_eq_left (ZMod.natCast_zmod_val _),mul_one] - · intro j hj he'; rw [ite_eq_right] - intro H - have hi := ZMod.val_natCast_of_lt ((Finset.mem_range.mp hj).trans_le he) - rw [H] at hi; exact he' hi.symm - · exact fun HH => (HH (Finset.mem_range.mpr ((mem_first _ _).mp hx))).elim - rw [first_eq_range d he] at hx - rw [first_eq_range d he,ite_eq_right hx,Finset.sum_eq_zero,mul_zero] - intro i hi; rw [ite_eq_right]; intro H; exact hx (Finset.mem_image.mpr ⟨_,hi,H⟩) - simp_rw [H] - calc - _ = ∑ _x ∈ Dyadic.first M d, ((d:ℝ)⁻¹)^2 := by - classical - have hi (x : Grid M) : - ‖(if x∈Dyadic.first M d then (d:ℂ)⁻¹ else 0)‖^2 = - if x∈Dyadic.first M d then ((d:ℝ)⁻¹)^2 else 0 := by split_ifs <;> simp - simp_rw [hi]; rw [← Finset.sum_filter] - simp - _ = _ := by simp [first_card d he]; field_simp - -private lemma tile_lower (M d : ℕ) (h : 0 < d) (x : Grid M) - (hx : Dyadic.diam x ≤ (1/16:ℝ)/d) : - 1/2 ≤ ‖tile M d x‖ := by - have hp : 0 < (d:ℝ) := by positivity - have hh := norm_phase_sq 1 x - have ha : ‖1-chi 1 x‖ ≤ 8*Dyadic.diam x := by nlinarith [(cost_diam x).2,diam_nonneg x] - have HF (i : ℕ) : ‖1-chi (i:Grid M) x‖ ≤ i*‖1-chi 1 x‖ := by - induction i with - | zero => simp [chi_zero_left] - | succ i ih => - push_cast - have Hx := norm_phase_add x (i:Grid M) 1 - rw [chi_comm x _,chi_comm x _,chi_comm x _] at Hx - linarith - have hE (i : ℕ) (hi : i < d) : ‖1-chi (i:Grid M) x‖ ≤ 1/2 := calc - _ ≤ _ := HF i - _ ≤ d*(8*Dyadic.diam x) := mul_le_mul (Nat.cast_le.mpr hi.le) ha (norm_nonneg _) hp.le - _ ≤ _ := by have := (le_div_iff₀ hp).mp hx; linarith - have hr (i : ℕ) (hi : i < d) : 1/2 ≤ (chi (i:Grid M) x).re := by - have H := Complex.abs_re_le_norm (1-chi (i:Grid M) x); simp only [Complex.sub_re,Complex.one_re] at H - linarith [(abs_le.mp (H.trans (hE i hi))).2] - apply le_trans (b:=(tile M d x).re) - · unfold tile; rw [show (d:ℂ)⁻¹=(↑((d:ℝ)⁻¹):ℂ) by push_cast; rfl, - Complex.re_ofReal_mul,Complex.re_sum,← div_eq_inv_mul] - apply (le_div_iff₀ hp).mpr - have HH := Finset.sum_le_sum (s:=Finset.range d) fun i hi => hr i (Finset.mem_range.mp hi) - simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at HH; linarith - exact (le_abs_self _).trans (Complex.abs_re_le_norm _) - - -private lemma tile_upper (M d : ℕ) (h : 0 < d) (x : Grid M) : - (4*d:ℝ)*Dyadic.diam x * ‖tile M d x‖ ≤ 2 := by - let z := chi (1:Grid M) x - have he (i : ℕ) : z ^ i = chi (i:Grid M) x := by unfold z; rw [← chi_nsmul,nsmul_one] - have H : ‖1-z‖ * (d:ℝ)* ‖tile M d x‖ ≤ 2 := by - let W := ∑ i∈Finset.range d,z^i - have HH := geom_sum_mul_neg z d - have HB : ‖W*(1-z)‖ ≤ 2 := by - rw [show W*(1-z)=_ from HH] - apply (norm_sub_le _ _).trans; rw [norm_pow,norm_one,show ‖z‖=1 from norm_chi _ _]; simp - norm_num - rw [norm_mul] at HB - have Hp : 0 < (d:ℝ) := by positivity - have hz : ‖tile M d x‖ = (d:ℝ)⁻¹*‖W‖ := by - unfold tile W; simp_rw [he, norm_mul,norm_inv]; simp - rw [hz] - apply le_trans _ HB; exact le_of_eq (by field_simp) - have he₁ := (cost_diam x).1 - have He : 4*Dyadic.diam x ≤ ‖1-z‖ := by unfold z; nlinarith [norm_phase_sq 1 x,norm_nonneg (1-chi 1 x)] - apply le_trans _ H - have hp : 0 ≤ (d:ℝ) := by positivity - rw [mul_right_comm (4:ℝ)]; gcongr - -open Node - -lemma boosted (M d k : ℕ) (h : 0 < d) (he : d ≤ 2^M) : - ∃ P : Law (Grid M), limitR P (2*(d:ℝ)*(k+10)) ∧ - P.prob (fun x => 16/d ≤ Dyadic.diam x) ≤ side k := by - let n := k+10 - let f (x : Grid M) := slab M d x^n - let a := ∑ x, f x - have hh : (0:ℝ) hf i) - have ha : 0 < a := lt_of_lt_of_le (by unfold r; positivity) HG - let P := proportional f hf ha - refine ⟨P,?_,?_⟩ - · have He : limitR f (n*(2*d):ℝ) := by - unfold limitR f; simp_rw [Complex.ofReal_pow]; exact limit_pow _ _ (slab_limit M d) _ - unfold limitR - have HE (x : Grid M) : P x = a⁻¹ * f x := by unfold P; rw [proportional_apply,div_eq_inv_mul] - simp_rw [HE,Complex.ofReal_mul] - rw [show 2*(d:ℝ)*(k+10) = n*(2*d) by unfold n; push_cast; ring] - exact limit_scale _ _ _ He - · let t (x : Grid M) := (16/d:ℝ) ≤ Dyadic.diam x - have Ht (x : Grid M) (hx : t x) : slab M d x ≤ 1/16 := by - unfold slab - have Hx := tile_upper M d h x - have HH := (div_le_iff₀ hh).mp hx - generalize HE : ‖tile M d x‖ = w at * - have hw : 0 ≤ w := HE ▸ norm_nonneg _ - nlinarith [mul_le_mul_of_nonneg_right HH hw] - let A := (1/16:ℝ)^(k+9) - have HI : - ∑ x, (if t x then f x else 0) ≤ A*(∑ x : Grid M, slab M d x) := by - rw [Finset.mul_sum] - apply Finset.sum_le_sum; intro i hi - have hip := slab_nonneg M d i - split_ifs with H - · apply le_trans (b:=slab M d i^(k+9)*slab M d i) - · unfold f n; rw [show k+10=k+9+1 from rfl,pow_succ] - unfold A; gcongr; exact Ht i H - unfold A; positivity - have HT (x : Grid M) : P x=f x/a := proportional_apply f hf ha _ - rw [prob_eq]; simp_rw [HT] - have HH (i : Grid M) : (if t i then f i/a else 0) = (if t i then f i else 0)/a := by - split_ifs <;> simp - change (∑ i, if t i then f i / a else 0) ≤ side k - simp_rw [HH, ← Finset.sum_div] - apply (div_le_iff₀ ha).mpr - have H := slab_total M d h he - change b* (∑ x : Grid M, slab M d x) = _ at H - calc - _ ≤ _ := HI - _ ≤ side k * (r*(1/4:ℝ)^n) := by - apply le_of_mul_le_mul_left ?_ hb - rw [mul_left_comm b, H] - unfold r n - have he : b*(side k*(1/16/d/b*(1/4:ℝ)^(k+10))) = - (d:ℝ)⁻¹*((1/16)*(side k*(1/4:ℝ)^(k+10))) := by field_simp - rw [he] - unfold A - apply le_trans (le_of_eq (mul_comm _ _)) (mul_le_mul_of_nonneg_left ?_ (by positivity)) - unfold side - rw [pow_add, pow_add] - have hh : (1/16:ℝ)^k ≤ (1/2:ℝ)^k*(1/4:ℝ)^k := by rw [← mul_pow]; norm_num; gcongr; norm_num - norm_num at *; nlinarith [show 0 ≤ (1/16:ℝ)^k by positivity] - _ ≤ _ := mul_le_mul_of_nonneg_left HG (side_pos _).le -end FourierLLogL.Dyadic.Procedure.Capture - -end OAI +import OAI.Analysis.FourierLLogL.Plateau.Thread + +/-! # Band-limited cutoffs -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Capture +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {M : ℕ} +open scoped Classical + +def limit (f : Grid M → ℂ) (r : ℝ) := ∀ x, r < rad x → fc f x=0 +def limitR (f : Grid M → ℝ) := limit (fun x => (f x:ℂ)) + +lemma limit_mul (f g : Grid M → ℂ) (p q : ℝ) (hf : limit f p) (hg : limit g q) : + limit (fun x => f x*g x) (p+q) := by + intro x hx; rw [fc_prod]; apply Finset.sum_eq_zero; intro i _ + by_cases hh : p < rad i + · rw [hf i hh,zero_mul] + have H : q < rad (x-i) := by + have Hh := rad_add (x-i) i; rw [sub_add_cancel] at Hh + linarith + rw [hg _ H,mul_zero] +lemma limit_scale (f : Grid M → ℂ) (c : ℂ) (p : ℝ) (hf : limit f p) : + limit (fun x => c*f x) p := by intro x hx; rw [fc_lmul,hf x hx,mul_zero] +lemma limit_const (p : ℝ) (hp : 0 ≤ p) : limit (M:=M) (fun _ => 1) p := by + intro x hx; rw [← funext chi_zero_left,fc_chi,ite_eq_right] + intro h; subst x; simp [rad] at hx; linarith + +lemma limit_pow (f : Grid M → ℂ) (p : ℝ) (hf : limit f p) (k : ℕ) : + limit (fun x => f x^k) (k*p) := by + induction k with + | zero => simpa only [Nat.cast_zero,zero_mul,pow_zero] using limit_const (M:=M) 0 le_rfl + | succ k ih => + simp_rw [pow_succ,Nat.cast_add_one,add_mul,one_mul] + exact limit_mul _ _ _ p ih hf +lemma limit_star (f : Grid M → ℂ) (p : ℝ) (hf : limit f p) : + limit (fun x => conj (f x)) p := by + intro x hx + have hh : fc (fun x => conj (f x)) x = conj (fc f (-x)) := by + unfold fc + simp_rw [expU_cx,← Cx_conj,chi_neg', map_mul] + simp + rw [hh,hf _ (by rw [rad_even]; exact hx),map_zero] + +/-- Dirichlet factor normalized to one. -/ +private def tile (M d : ℕ) (x : Grid M) := (d:ℂ)⁻¹ * ∑ i∈Finset.range d, chi (i:Grid M) x +def slab (M d : ℕ) (x : Grid M) := ‖tile M d x‖^2 + +private lemma tile_limit (M d : ℕ) : limit (tile M d) d := by + intro x hx + unfold tile; rw [fc_lmul,fc_sum _ (fun i : ℕ => chi (i:Grid M))] + suffices H : ∀ i ∈ Finset.range d, fc (chi (i:Grid M)) x=0 by simp [Finset.sum_eq_zero H] + intro i hi; rw [fc_chi,ite_eq_right] + intro h; subst x + have H := rad_le_nat (M:=M) i + have hi : (i:ℝ) < d := Nat.cast_lt.mpr (Finset.mem_range.mp hi) + linarith + +lemma slab_limit (M d : ℕ) : limitR (slab M d) (2*d) := by + have hi (x : Grid M) : (↑(slab M d x):ℂ) = + tile M d x * conj (tile M d x) := by rw [slab,Complex.mul_conj,Complex.sq_norm] + have h := tile_limit M d + unfold limitR; simp_rw [hi, two_mul] + exact limit_mul _ _ _ _ h (limit_star _ _ h) + +lemma slab_nonneg (M d : ℕ) (x : Grid M) : 0 ≤ slab M d x := by unfold slab; positivity + +lemma slab_total (M : ℕ) (d : ℕ) (h : 0 < d) (he : d ≤ 2^M) : + expU (slab M d) = (d:ℝ)⁻¹ := by + change expU (fun x => ‖tile M d x‖^2) = _ + rw [← fc_parseval] + have H (x : Grid M) : fc (tile M d) x = if x∈Dyadic.first M d then (d:ℂ)⁻¹ else 0 := by + unfold tile; rw [fc_lmul,fc_sum _ (fun i : ℕ => chi (i:Grid M))] + simp_rw [fc_chi] + by_cases hx : x∈Dyadic.first M d + · rw [ite_eq_left hx] + rw [Finset.sum_eq_single x.val, ite_eq_left (ZMod.natCast_zmod_val _),mul_one] + · intro j hj he'; rw [ite_eq_right] + intro H + have hi := ZMod.val_natCast_of_lt ((Finset.mem_range.mp hj).trans_le he) + rw [H] at hi; exact he' hi.symm + · exact fun HH => (HH (Finset.mem_range.mpr ((mem_first _ _).mp hx))).elim + rw [first_eq_range d he] at hx + rw [first_eq_range d he,ite_eq_right hx,Finset.sum_eq_zero,mul_zero] + intro i hi; rw [ite_eq_right]; intro H; exact hx (Finset.mem_image.mpr ⟨_,hi,H⟩) + simp_rw [H] + calc + _ = ∑ _x ∈ Dyadic.first M d, ((d:ℝ)⁻¹)^2 := by + classical + have hi (x : Grid M) : + ‖(if x∈Dyadic.first M d then (d:ℂ)⁻¹ else 0)‖^2 = + if x∈Dyadic.first M d then ((d:ℝ)⁻¹)^2 else 0 := by split_ifs <;> simp + simp_rw [hi]; rw [← Finset.sum_filter] + simp + _ = _ := by simp [first_card d he]; field_simp + +private lemma tile_lower (M d : ℕ) (h : 0 < d) (x : Grid M) + (hx : Dyadic.diam x ≤ (1/16:ℝ)/d) : + 1/2 ≤ ‖tile M d x‖ := by + have hp : 0 < (d:ℝ) := by positivity + have hh := norm_phase_sq 1 x + have ha : ‖1-chi 1 x‖ ≤ 8*Dyadic.diam x := by nlinarith [(cost_diam x).2,diam_nonneg x] + have HF (i : ℕ) : ‖1-chi (i:Grid M) x‖ ≤ i*‖1-chi 1 x‖ := by + induction i with + | zero => simp [chi_zero_left] + | succ i ih => + push_cast + have Hx := norm_phase_add x (i:Grid M) 1 + rw [chi_comm x _,chi_comm x _,chi_comm x _] at Hx + linarith + have hE (i : ℕ) (hi : i < d) : ‖1-chi (i:Grid M) x‖ ≤ 1/2 := calc + _ ≤ _ := HF i + _ ≤ d*(8*Dyadic.diam x) := mul_le_mul (Nat.cast_le.mpr hi.le) ha (norm_nonneg _) hp.le + _ ≤ _ := by have := (le_div_iff₀ hp).mp hx; linarith + have hr (i : ℕ) (hi : i < d) : 1/2 ≤ (chi (i:Grid M) x).re := by + have H := Complex.abs_re_le_norm (1-chi (i:Grid M) x); simp only [Complex.sub_re,Complex.one_re] at H + linarith [(abs_le.mp (H.trans (hE i hi))).2] + apply le_trans (b:=(tile M d x).re) + · unfold tile; rw [show (d:ℂ)⁻¹=(↑((d:ℝ)⁻¹):ℂ) by push_cast; rfl, + Complex.re_ofReal_mul,Complex.re_sum,← div_eq_inv_mul] + apply (le_div_iff₀ hp).mpr + have HH := Finset.sum_le_sum (s:=Finset.range d) fun i hi => hr i (Finset.mem_range.mp hi) + simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at HH; linarith + exact (le_abs_self _).trans (Complex.abs_re_le_norm _) + + +private lemma tile_upper (M d : ℕ) (h : 0 < d) (x : Grid M) : + (4*d:ℝ)*Dyadic.diam x * ‖tile M d x‖ ≤ 2 := by + let z := chi (1:Grid M) x + have he (i : ℕ) : z ^ i = chi (i:Grid M) x := by unfold z; rw [← chi_nsmul,nsmul_one] + have H : ‖1-z‖ * (d:ℝ)* ‖tile M d x‖ ≤ 2 := by + let W := ∑ i∈Finset.range d,z^i + have HH := geom_sum_mul_neg z d + have HB : ‖W*(1-z)‖ ≤ 2 := by + rw [show W*(1-z)=_ from HH] + apply (norm_sub_le _ _).trans; rw [norm_pow,norm_one,show ‖z‖=1 from norm_chi _ _]; simp + norm_num + rw [norm_mul] at HB + have Hp : 0 < (d:ℝ) := by positivity + have hz : ‖tile M d x‖ = (d:ℝ)⁻¹*‖W‖ := by + unfold tile W; simp_rw [he, norm_mul,norm_inv]; simp + rw [hz] + apply le_trans _ HB; exact le_of_eq (by field_simp) + have he₁ := (cost_diam x).1 + have He : 4*Dyadic.diam x ≤ ‖1-z‖ := by unfold z; nlinarith [norm_phase_sq 1 x,norm_nonneg (1-chi 1 x)] + apply le_trans _ H + have hp : 0 ≤ (d:ℝ) := by positivity + rw [mul_right_comm (4:ℝ)]; gcongr + +open Node + +lemma boosted (M d k : ℕ) (h : 0 < d) (he : d ≤ 2^M) : + ∃ P : Law (Grid M), limitR P (2*(d:ℝ)*(k+10)) ∧ + P.prob (fun x => 16/d ≤ Dyadic.diam x) ≤ side k := by + let n := k+10 + let f (x : Grid M) := slab M d x^n + let a := ∑ x, f x + have hh : (0:ℝ) hf i) + have ha : 0 < a := lt_of_lt_of_le (by unfold r; positivity) HG + let P := proportional f hf ha + refine ⟨P,?_,?_⟩ + · have He : limitR f (n*(2*d):ℝ) := by + unfold limitR f; simp_rw [Complex.ofReal_pow]; exact limit_pow _ _ (slab_limit M d) _ + unfold limitR + have HE (x : Grid M) : P x = a⁻¹ * f x := by unfold P; rw [proportional_apply,div_eq_inv_mul] + simp_rw [HE,Complex.ofReal_mul] + rw [show 2*(d:ℝ)*(k+10) = n*(2*d) by unfold n; push_cast; ring] + exact limit_scale _ _ _ He + · let t (x : Grid M) := (16/d:ℝ) ≤ Dyadic.diam x + have Ht (x : Grid M) (hx : t x) : slab M d x ≤ 1/16 := by + unfold slab + have Hx := tile_upper M d h x + have HH := (div_le_iff₀ hh).mp hx + generalize HE : ‖tile M d x‖ = w at * + have hw : 0 ≤ w := HE ▸ norm_nonneg _ + nlinarith [mul_le_mul_of_nonneg_right HH hw] + let A := (1/16:ℝ)^(k+9) + have HI : + ∑ x, (if t x then f x else 0) ≤ A*(∑ x : Grid M, slab M d x) := by + rw [Finset.mul_sum] + apply Finset.sum_le_sum; intro i hi + have hip := slab_nonneg M d i + split_ifs with H + · apply le_trans (b:=slab M d i^(k+9)*slab M d i) + · unfold f n; rw [show k+10=k+9+1 from rfl,pow_succ] + unfold A; gcongr; exact Ht i H + unfold A; positivity + have HT (x : Grid M) : P x=f x/a := proportional_apply f hf ha _ + rw [prob_eq]; simp_rw [HT] + have HH (i : Grid M) : (if t i then f i/a else 0) = (if t i then f i else 0)/a := by + split_ifs <;> simp + change (∑ i, if t i then f i / a else 0) ≤ side k + simp_rw [HH, ← Finset.sum_div] + apply (div_le_iff₀ ha).mpr + have H := slab_total M d h he + change b* (∑ x : Grid M, slab M d x) = _ at H + calc + _ ≤ _ := HI + _ ≤ side k * (r*(1/4:ℝ)^n) := by + apply le_of_mul_le_mul_left ?_ hb + rw [mul_left_comm b, H] + unfold r n + have he : b*(side k*(1/16/d/b*(1/4:ℝ)^(k+10))) = + (d:ℝ)⁻¹*((1/16)*(side k*(1/4:ℝ)^(k+10))) := by field_simp + rw [he] + unfold A + apply le_trans (le_of_eq (mul_comm _ _)) (mul_le_mul_of_nonneg_left ?_ (by positivity)) + unfold side + rw [pow_add, pow_add] + have hh : (1/16:ℝ)^k ≤ (1/2:ℝ)^k*(1/4:ℝ)^k := by rw [← mul_pow]; norm_num; gcongr; norm_num + norm_num at *; nlinarith [show 0 ≤ (1/16:ℝ)^k by positivity] + _ ≤ _ := mul_le_mul_of_nonneg_left HG (side_pos _).le +end FourierLLogL.Dyadic.Procedure.Capture + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Blocks.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Blocks.lean index 155920364b42e8c34729103d321a093f8062bd4e..d3620700415d09c3937370c1ba315605a83214d6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Blocks.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Blocks.lean @@ -1,165 +1,165 @@ -import OAI.Analysis.FourierLLogL.Compress.Variational -/-! # Indexed blocks of letters -/ - -open Function Filter Set -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real - -abbrev Letters (n : ℕ) (α : Type) := Fin n → α -abbrev WordSample (n : ℕ) (α G : Type) := Sample (Fin n) α G - -variable {ι τ κ n m k : ℕ} {α Ω G : Type} - -def offset (j l : ℕ) (h : j+l ≤ n) (i : Fin l) : Fin n := ⟨j+i, by have := i.isLt; omega⟩ - -def readBlock (X : Ω → Letters n α) (j l : ℕ) (h : j+l ≤ n) := - keep X (offset j l h) -def prefixB (X : Ω → Letters n α) (l : ℕ) (h : l ≤ n) := readBlock X 0 l (by omega) -def readLetter (X : Ω → Letters n α) (j : ℕ) (h : j X x ⟨j,h⟩ - -lemma offset_injective (j l : ℕ) (h : j+l ≤ n) : Injective (offset j l h) := by - intro a b hab - have H := congrArg Fin.val hab - simp only [offset,Nat.add_left_cancel_iff] at H - exact Fin.ext H - -lemma prefix_partition (X : Ω → Letters n α) (l k : ℕ) (h : l+k ≤ n) (x y : Ω) : - prefixB X (l+k) h x = prefixB X (l+k) h y ↔ - prefixB X l (by omega) x = prefixB X l (by omega) y ∧ readBlock X l k h x = readBlock X l k h y := by - simp only [funext_iff] - constructor - · intro H; constructor - · intro i - exact H (i.castAdd k) - · intro ⟨i,hi⟩ - have h1 := H (⟨l+i,by omega⟩:Fin (l+k)) - simpa only [prefixB,readBlock,keep,offset,zero_add] using h1 - · rintro ⟨H,G⟩ ⟨i,hi⟩ - by_cases hp : i < l - · exact H ⟨i,hp⟩ - · have h1 := G (⟨i-l,by omega⟩:Fin k) - simpa only [prefixB,readBlock,keep,offset,zero_add, Nat.add_sub_of_le (le_of_not_gt hp)] - using h1 - -lemma prefix_last_partition (X : Ω → Letters n α) (l : ℕ) (h : l congrFun e (0:Fin 1),fun e => by funext i; rw [Subsingleton.elim i 0]; exact e⟩ - rw [HH,and_comm] - -lemma prefix_zero (X : Ω → Letters n α) : prefixB X 0 (by omega) = fun _ => Fin.elim0 := - funext fun _ => Subsingleton.elim _ _ - -lemma prefix_all (X : Ω → Letters n α) : prefixB X n le_rfl = X := by - funext a ⟨i,hi⟩ - simp only [prefixB,readBlock,keep,offset,zero_add] - -lemma block_cut (X : Ω → Letters n α) (j m l : ℕ) (h : j+m ≤ n) (hl : l ≤ m) : - readBlock X j l (by omega) = prefixB (readBlock X j m h) l hl := by - funext a ⟨i,hi⟩; simp only [prefixB,readBlock,keep,offset,zero_add] - -lemma block_in_prefix (X : Ω → Letters n α) (j k l : ℕ) (hk : j+k ≤ l) (hl : l ≤ n) : - readBlock X j k (hk.trans hl) = readBlock (prefixB X l hl) j k hk := by - funext a ⟨i,hi⟩; simp only [prefixB,readBlock,keep,offset,zero_add] - - -def splitIx {n : ℕ} (l m : ℕ) (h : l+m=n) : (Fin m ⊕ Fin l) ≃ Fin n := - ((Equiv.sumComm _ _).trans finSumFinEquiv).trans (finCongr h) - -lemma splitIx_left (l m : ℕ) (h : l+m=n) (i : Fin m) : - splitIx l m h (.inl i) = offset l m h.le i := by - apply Fin.ext - simp only [splitIx,Equiv.trans_apply,finCongr_apply_coe] - rfl - -lemma splitIx_right (l m : ℕ) (h : l+m=n) (i : Fin l) : - splitIx l m h (.inr i) = offset 0 l (by omega : 0+l≤n) i := by - apply Fin.ext - simp only [splitIx,Equiv.trans_apply,finCongr_apply_coe,offset,zero_add] - rfl - - -section reindex -variable {I J K : Type} [Fintype I] [DecidableEq I] [Fintype J] [DecidableEq J] - [Fintype K] [DecidableEq K] - -omit [Fintype I] [Fintype J] [DecidableEq J] [DecidableEq I] in -lemma lift_perm (g : J → I) (hg : Injective g) (e : Equiv.Perm J) : - ∃ f : Equiv.Perm I, (∀ i, f (g i) = g (e i)) ∧ ∀ x, x ∉ range g → f x = x := by - classical - let T := (Equiv.sumCongr (Equiv.ofInjective g hg) (Equiv.refl {x : I // x ∉ range g})) - |>.trans (Equiv.sumCompl (fun x => x ∈ range g)) - let E := Equiv.sumCongr e (Equiv.refl {x : I // x ∉ range g}) - have H (x) : ((T.symm.trans E).trans T) (T x) = T (E x) := by simp - refine ⟨(T.symm.trans E).trans T,fun i => H (.inl i),fun x hx => ?_⟩ - exact H (.inr ⟨x,hx⟩) - -variable [Fintype α] [Fintype G] - -def subSample (g : J → I) (p : Sample I α G) : Sample J α G := - ((fun j => p.1 (g j)), p.2) - -lemma exch_subSample {P : Law (Sample I α G)} (hx : exch P) - (g : J → I) (hg : Injective g) : exch (P.send (subSample g)) := by - intro e - obtain ⟨f,hf,hf'⟩ := lift_perm g hg e - rw [send_send] - have HH := congrArg (fun R : Law (Sample I α G) => R.send (subSample g)) (hx f) - rw [send_send] at HH - simpa [subSample,appPerm,hf] using HH - -lemma exch_subSample_cond {P : Law (Sample I α G)} (hx : exch P) - (g : J → I) (hg : Injective g) (u : K → I) (hu : ∀ i, u i ∉ range g) - (w : K → α) : - let W := keep (fun p : Sample I α G => p.1) u - exch ((P.after W w).send (subSample g)) := by - intro W e - obtain ⟨f,hf,hf'⟩ := lift_perm g hg e - have HH := congrArg (fun R : Law (Sample I α G) => R.send fun x => (subSample g x,W x)) (hx f) - rw [send_send] at HH - have h1 (x : Sample I α G) : subSample g (appPerm f x) = appPerm e (subSample g x) := by - simp only [appPerm,subSample,hf] - have h2 (x : Sample I α G) : W (appPerm f x) = W x := by - funext i - simp only [W,keep,appPerm,hf' _ (hu i)] - simp_rw [h1,h2] at HH - rw [send_send] - exact after_send_eq_of_send_pair_eq (X:=fun x => appPerm e (subSample g x)) (W:=W) - (Y:=subSample g) (V:=W) HH w - -end reindex - -section billsplit -variable [Fintype α] [Fintype Ω] [Fintype G] [AddCommGroup G] -lemma prefix_bill (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω → Letters n α) (U : Ω → G) (h : covered P X q) - (j m : ℕ) (hh : j+m ≤ n) : - let W := prefixB X j (by omega) - (P.send W).Ex (fun w => bill tag c q (P.after W w) (readBlock X j m hh) U) ≤ - bill tag c q P X U := by - intro W - let l := n-j - have he : j+l=n := by dsimp [l]; omega - have hhh := bill_split tag c q P X U h (splitIx j l he) - let V := readBlock X j l he.le - have H1 : - (P.send W).Ex (fun w => bill tag c q (P.after W w) V U) ≤ bill tag c q P X U := by - simpa only [W,V,prefixB,readBlock,splitIx_left,splitIx_right] using hhh - apply le_trans ?_ H1 - refine Ex_mono_mem _ (fun w hw => ?_) - have hB : readBlock X j m hh = prefixB V m (by omega) := block_cut X j l m he.le (by omega) - rw [hB] - exact bill_subfamily tag c q _ V U - (covered_subfamily (covered_after h _ _ hw) _) _ (offset_injective 0 m _) -end billsplit - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Variational +/-! # Indexed blocks of letters -/ + +open Function Filter Set +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real + +abbrev Letters (n : ℕ) (α : Type) := Fin n → α +abbrev WordSample (n : ℕ) (α G : Type) := Sample (Fin n) α G + +variable {ι τ κ n m k : ℕ} {α Ω G : Type} + +def offset (j l : ℕ) (h : j+l ≤ n) (i : Fin l) : Fin n := ⟨j+i, by have := i.isLt; omega⟩ + +def readBlock (X : Ω → Letters n α) (j l : ℕ) (h : j+l ≤ n) := + keep X (offset j l h) +def prefixB (X : Ω → Letters n α) (l : ℕ) (h : l ≤ n) := readBlock X 0 l (by omega) +def readLetter (X : Ω → Letters n α) (j : ℕ) (h : j X x ⟨j,h⟩ + +lemma offset_injective (j l : ℕ) (h : j+l ≤ n) : Injective (offset j l h) := by + intro a b hab + have H := congrArg Fin.val hab + simp only [offset,Nat.add_left_cancel_iff] at H + exact Fin.ext H + +lemma prefix_partition (X : Ω → Letters n α) (l k : ℕ) (h : l+k ≤ n) (x y : Ω) : + prefixB X (l+k) h x = prefixB X (l+k) h y ↔ + prefixB X l (by omega) x = prefixB X l (by omega) y ∧ readBlock X l k h x = readBlock X l k h y := by + simp only [funext_iff] + constructor + · intro H; constructor + · intro i + exact H (i.castAdd k) + · intro ⟨i,hi⟩ + have h1 := H (⟨l+i,by omega⟩:Fin (l+k)) + simpa only [prefixB,readBlock,keep,offset,zero_add] using h1 + · rintro ⟨H,G⟩ ⟨i,hi⟩ + by_cases hp : i < l + · exact H ⟨i,hp⟩ + · have h1 := G (⟨i-l,by omega⟩:Fin k) + simpa only [prefixB,readBlock,keep,offset,zero_add, Nat.add_sub_of_le (le_of_not_gt hp)] + using h1 + +lemma prefix_last_partition (X : Ω → Letters n α) (l : ℕ) (h : l congrFun e (0:Fin 1),fun e => by funext i; rw [Subsingleton.elim i 0]; exact e⟩ + rw [HH,and_comm] + +lemma prefix_zero (X : Ω → Letters n α) : prefixB X 0 (by omega) = fun _ => Fin.elim0 := + funext fun _ => Subsingleton.elim _ _ + +lemma prefix_all (X : Ω → Letters n α) : prefixB X n le_rfl = X := by + funext a ⟨i,hi⟩ + simp only [prefixB,readBlock,keep,offset,zero_add] + +lemma block_cut (X : Ω → Letters n α) (j m l : ℕ) (h : j+m ≤ n) (hl : l ≤ m) : + readBlock X j l (by omega) = prefixB (readBlock X j m h) l hl := by + funext a ⟨i,hi⟩; simp only [prefixB,readBlock,keep,offset,zero_add] + +lemma block_in_prefix (X : Ω → Letters n α) (j k l : ℕ) (hk : j+k ≤ l) (hl : l ≤ n) : + readBlock X j k (hk.trans hl) = readBlock (prefixB X l hl) j k hk := by + funext a ⟨i,hi⟩; simp only [prefixB,readBlock,keep,offset,zero_add] + + +def splitIx {n : ℕ} (l m : ℕ) (h : l+m=n) : (Fin m ⊕ Fin l) ≃ Fin n := + ((Equiv.sumComm _ _).trans finSumFinEquiv).trans (finCongr h) + +lemma splitIx_left (l m : ℕ) (h : l+m=n) (i : Fin m) : + splitIx l m h (.inl i) = offset l m h.le i := by + apply Fin.ext + simp only [splitIx,Equiv.trans_apply,finCongr_apply_coe] + rfl + +lemma splitIx_right (l m : ℕ) (h : l+m=n) (i : Fin l) : + splitIx l m h (.inr i) = offset 0 l (by omega : 0+l≤n) i := by + apply Fin.ext + simp only [splitIx,Equiv.trans_apply,finCongr_apply_coe,offset,zero_add] + rfl + + +section reindex +variable {I J K : Type} [Fintype I] [DecidableEq I] [Fintype J] [DecidableEq J] + [Fintype K] [DecidableEq K] + +omit [Fintype I] [Fintype J] [DecidableEq J] [DecidableEq I] in +lemma lift_perm (g : J → I) (hg : Injective g) (e : Equiv.Perm J) : + ∃ f : Equiv.Perm I, (∀ i, f (g i) = g (e i)) ∧ ∀ x, x ∉ range g → f x = x := by + classical + let T := (Equiv.sumCongr (Equiv.ofInjective g hg) (Equiv.refl {x : I // x ∉ range g})) + |>.trans (Equiv.sumCompl (fun x => x ∈ range g)) + let E := Equiv.sumCongr e (Equiv.refl {x : I // x ∉ range g}) + have H (x) : ((T.symm.trans E).trans T) (T x) = T (E x) := by simp + refine ⟨(T.symm.trans E).trans T,fun i => H (.inl i),fun x hx => ?_⟩ + exact H (.inr ⟨x,hx⟩) + +variable [Fintype α] [Fintype G] + +def subSample (g : J → I) (p : Sample I α G) : Sample J α G := + ((fun j => p.1 (g j)), p.2) + +lemma exch_subSample {P : Law (Sample I α G)} (hx : exch P) + (g : J → I) (hg : Injective g) : exch (P.send (subSample g)) := by + intro e + obtain ⟨f,hf,hf'⟩ := lift_perm g hg e + rw [send_send] + have HH := congrArg (fun R : Law (Sample I α G) => R.send (subSample g)) (hx f) + rw [send_send] at HH + simpa [subSample,appPerm,hf] using HH + +lemma exch_subSample_cond {P : Law (Sample I α G)} (hx : exch P) + (g : J → I) (hg : Injective g) (u : K → I) (hu : ∀ i, u i ∉ range g) + (w : K → α) : + let W := keep (fun p : Sample I α G => p.1) u + exch ((P.after W w).send (subSample g)) := by + intro W e + obtain ⟨f,hf,hf'⟩ := lift_perm g hg e + have HH := congrArg (fun R : Law (Sample I α G) => R.send fun x => (subSample g x,W x)) (hx f) + rw [send_send] at HH + have h1 (x : Sample I α G) : subSample g (appPerm f x) = appPerm e (subSample g x) := by + simp only [appPerm,subSample,hf] + have h2 (x : Sample I α G) : W (appPerm f x) = W x := by + funext i + simp only [W,keep,appPerm,hf' _ (hu i)] + simp_rw [h1,h2] at HH + rw [send_send] + exact after_send_eq_of_send_pair_eq (X:=fun x => appPerm e (subSample g x)) (W:=W) + (Y:=subSample g) (V:=W) HH w + +end reindex + +section billsplit +variable [Fintype α] [Fintype Ω] [Fintype G] [AddCommGroup G] +lemma prefix_bill (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω → Letters n α) (U : Ω → G) (h : covered P X q) + (j m : ℕ) (hh : j+m ≤ n) : + let W := prefixB X j (by omega) + (P.send W).Ex (fun w => bill tag c q (P.after W w) (readBlock X j m hh) U) ≤ + bill tag c q P X U := by + intro W + let l := n-j + have he : j+l=n := by dsimp [l]; omega + have hhh := bill_split tag c q P X U h (splitIx j l he) + let V := readBlock X j l he.le + have H1 : + (P.send W).Ex (fun w => bill tag c q (P.after W w) V U) ≤ bill tag c q P X U := by + simpa only [W,V,prefixB,readBlock,splitIx_left,splitIx_right] using hhh + apply le_trans ?_ H1 + refine Ex_mono_mem _ (fun w hw => ?_) + have hB : readBlock X j m hh = prefixB V m (by omega) := block_cut X j l m he.le (by omega) + rw [hB] + exact bill_subfamily tag c q _ V U + (covered_subfamily (covered_after h _ _ hw) _) _ (offset_injective 0 m _) +end billsplit + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Context.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Context.lean index 7da902defb34a08ccf8cd8c8ea7624e6e18f7a41..35ad01f82ddf60c7b61d55c908fd0ed21de71a01 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Context.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Context.lean @@ -1,260 +1,260 @@ -import OAI.Analysis.FourierLLogL.Compress.Projection -import OAI.Analysis.FourierLLogL.Compress.Select - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A V W G E α β Ω Ω' : Type} [Fintype A] [Fintype G] [AddCommGroup G] - [Fintype α] [Fintype β] [Fintype Ω] [Fintype Ω'] [Fintype V] [Fintype W] - [Fintype E] [Nonempty E] {n : ℕ} -set_option linter.unusedSectionVars false - -/-- Numerical Context State. -/ -structure Labelled (A : Type) [Fintype A] where - obs : Type - [fObs : Fintype obs] - law : Law (A × obs) - -attribute [instance] Labelled.fObs - -def initial (p : Law A) : Labelled A := {obs := Unit, law:=p.send (fun x => (x,()))} - -def refines (P Q : Labelled A) : Prop := - ∃ u : P.obs → Q.obs, P.law.send (fun a => (a.1,u a.2)) = Q.law - -lemma refines_refl (p : Labelled A) : refines p p := by - refine ⟨id,?_⟩; exact send_id p.law - -lemma refines_trans {p q r : Labelled A} : - refines p q → refines q r → refines p r := by - rintro ⟨u,hu⟩ ⟨v,hv⟩ - rw [← hu,send_send] at hv - exact ⟨v ∘ u,hv⟩ - -def lblSum (tag : A → G) (W : Labelled A) (m : ℕ) (x : Letters m (A × W.obs)) : G := - tot (tag ∘ Prod.fst) x -def lblCtx (W : Labelled A) (m : ℕ) (x : Letters m (A × W.obs)) : Letters m W.obs := - fun i => (x i).2 - -/-- m-letter block of joint letter-and-context pairs, iid. -/ -def lblP (W : Labelled A) (m : ℕ) := iid W.law m - -/-- Conditional entropy of total sum. -/ -def fundG (tag : A → G) (p : Labelled A) (m : ℕ) := - (lblP p m).Hcond (lblSum tag p m) (lblCtx p m) - -/-- Conditional exact cost. -/ -def fundI (tag : A → G) (P : Labelled A) (r : ℕ) := - opt r P.law (tag ∘ Prod.fst) Prod.snd (fun _ : Unit => (0:G)) (fun _ => 0) - -/-- Conditional m-block noisy cost. -/ -def fundK (tag : A → G) (c : G → ℝ) (P : Labelled A) (m r : ℕ) := - opt r (lblP P m) (lblSum tag P m) (lblCtx P m) id c - -lemma I_anti (tag : A → G) {P Q : Labelled A} (h : refines P Q) (r : ℕ) : - fundI tag P r ≤ fundI tag Q r := by - obtain ⟨u,hu⟩ := h - unfold fundI - rw [← hu] - let f (x : A×P.obs) := (x.1,u x.2) - exact opt_refined (tag ∘ Prod.fst) Prod.snd Prod.snd (fun _ : Unit => 0) (fun _ => 0) P.law f r u rfl - -lemma K_anti (tag : A → G) (c : G → ℝ) {P Q : Labelled A} - (h : refines P Q) (m r : ℕ) : fundK tag c P m r ≤ fundK tag c Q m r := by - obtain ⟨u,hu⟩ := h - let X (x : A × P.obs) := (x.1,u x.2) - let f (x : Letters m _) i := X (x i) - have H : lblP Q m = (lblP P m).send f := by - unfold lblP iid; rw [show Q.law = _ from hu.symm] - exact (sendPack (fun _ : Fin m => P.law) (fun _ => X)).symm - unfold fundK - rw [H] - exact opt_refined (lblSum tag Q m) (lblCtx Q m) (lblCtx P m) id c (lblP P m) f r (fun x i => u (x i)) rfl - -lemma G_anti (tag : A → G) {P Q : Labelled A} (h : refines P Q) - (m : ℕ) : fundG tag P m ≤ fundG tag Q m := by - obtain ⟨u,hu⟩ := h - let X (x : A × P.obs) := (x.1,u x.2) - let f (x : Letters m _) i := X (x i) - have H : lblP Q m = (lblP P m).send f := by - unfold lblP iid; rw [show Q.law = _ from hu.symm] - exact (sendPack (fun _ : Fin m => P.law) (fun _ => X)).symm - unfold fundG - rw [H,Hcond_send] - apply Hcond_antitone - intro a b hh - funext i - exact congrArg u (congrFun hh i) - -lemma fundG_one (tag : A → G) (hi : Injective tag) (P : Labelled A) : - fundG tag P 1 = P.law.Hcond Prod.fst Prod.snd := by - let Q := lblP P 1 - have h : Q.send (fun x => x 0) = P.law := marginalPack _ _ - rw [← h,Hcond_send] - unfold fundG Hcond - congr 1 - · refine H_partition _ fun a b => ?_ - unfold lblSum lblCtx tot - simp only [Fin.sum_univ_one,comp_apply,Prod.mk.injEq,hi.eq_iff,funext_iff] - simp [Fin.forall_fin_one] - · refine H_partition _ fun a b => ?_ - simp [funext_iff,lblCtx, Fin.forall_fin_one] - -def appendOne {m : ℕ} (p : A × Letters m A) : Letters (m+1) A := Fin.cons p.1 p.2 - -lemma iid_succ (P : Law A) (m : ℕ) : - iid P (m+1) = (P.tensorP (iid P m)).send appendOne := by - apply ext_by_products - intro f - dsimp only [iid] - rw [Ex_send,Ex_tensorP] - rw [pack_Ex_prod _ f] - simp_rw [Fin.prod_univ_succ] - unfold appendOne - simp only [Fin.cons_zero,Fin.cons_succ] - simp_rw [Ex_lmul,pack_Ex_prod _ (fun i : Fin m => f i.succ),Ex_mul] - -lemma cons_part {m : ℕ} {a b : A} {x y : Letters m A} : - (Fin.cons a x : Letters (m+1) A) = Fin.cons b y ↔ a = b ∧ x = y := by - constructor - · intro h - exact ⟨by simpa using congrFun h 0,by funext i; simpa using congrFun h i.succ⟩ - · rintro ⟨rfl,rfl⟩; rfl - - -lemma Hcond_tensor (P : Law Ω) (Q : Law Ω') (X : Ω → α) (Z : Ω' → β) - (Y : Ω → V) (T : Ω' → W) : - (P.tensorP Q).Hcond (fun x => (X x.1,Z x.2)) (fun x => (Y x.1,T x.2)) = - P.Hcond X Y + Q.Hcond Z T := by - unfold Hcond - rw [H_partition (X:=fun (x:Ω×Ω') => ((X x.1,Z x.2),(Y x.1,T x.2))) - (Y:=fun x => ((X x.1,Y x.1),(Z x.2,T x.2))) _ (fun _ _ => by simp; tauto)] - rw [H_tensorP P Q (fun x => (X x,Y x)) (fun x => (Z x,T x)),H_tensorP]; ring - -lemma Hcond_tensor_right (P : Law Ω) (Q : Law Ω') (Z : Ω' → β) - (Y : Ω → V) (T : Ω' → W) : - (P.tensorP Q).Hcond (fun x => Z x.2) (fun x => (Y x.1,T x.2)) = Q.Hcond Z T := by - let X (_x : Ω) := () - rw [show Q.Hcond Z T = P.Hcond X Y + Q.Hcond Z T by - rw [Hcond_determined (P:=P) (X:=X) (W:=Y) (fun _ _ _ => rfl)]; ring, - ← Hcond_tensor] - unfold Hcond - congr 1 - apply H_partition - simp only [Prod.mk.injEq, X]; tauto - -lemma lbl_succ_sum (tag : A → G) (P : Labelled A) (m : ℕ) - (x : (A×P.obs) × Letters m (A × P.obs)) : - lblSum tag P (m+1) (appendOne x) = tag x.1.1 + lblSum tag P m x.2 := by - unfold lblSum tot; rw [Fin.sum_univ_succ]; rfl - -lemma lbl_succ_ctx (P : Labelled A) (m : ℕ) - (x : (A×P.obs) × Letters m (A × P.obs)) : - lblCtx P (m+1) (appendOne x) = appendOne (x.1.2,lblCtx P m x.2) := by - funext i - cases i using Fin.cases <;> simp [lblCtx,appendOne] - -lemma fundG_succ (tag : A → G) (P : Labelled A) (m : ℕ) : - let Q := P.law.tensorP (lblP P m) - let X (x : (A×P.obs) × Letters m (A×P.obs)) := tag x.1.1 + lblSum tag P m x.2 - let Z (x : (A×P.obs) × Letters m (A×P.obs)) := (x.1.2,lblCtx P m x.2) - fundG tag P (m+1) = Q.Hcond X Z := by - intro Q X Z - unfold fundG - rw [show lblP P (m+1)=Q.send appendOne from iid_succ P.law m,Hcond_send] - simp_rw [lbl_succ_sum,lbl_succ_ctx] - refine Hcond_right_partition _ X (fun x y => ?_) - unfold appendOne Z - simp only [cons_part,Prod.mk.injEq] - -lemma fundG_mono (tag : A → G) (P : Labelled A) : Monotone (fundG tag P) := by - refine monotone_nat_of_le_succ fun i => ?_ - rw [fundG_succ] - let Q := lblP P i - let R := P.law.tensorP Q - let W (x : (A×P.obs) × Letters i (A×P.obs)) := (x.1, lblCtx P i x.2) - let X (x : (A×P.obs) × Letters i (A×P.obs)) := tag x.1.1 + lblSum tag P i x.2 - have hr : R.Hcond X W = R.Hcond (fun x => lblSum tag P i x.2) W := by - unfold Hcond - congr 1 - apply H_partition - intro x y - simp only [W,X,Prod.mk.injEq] - constructor - · rintro ⟨h,h1,h2⟩; rw [h1] at h; exact ⟨add_left_cancel h,h1,h2⟩ - · rintro ⟨h,h1,h2⟩; exact ⟨by rw [h,h1],h1,h2⟩ - have he : R.Hcond (fun x => lblSum tag P i x.2) W = fundG tag P i := - Hcond_tensor_right P.law Q (lblSum tag P i) (fun x => x) (lblCtx P i) - change fundG tag P i ≤ R.Hcond X _ - rw [← hr.trans he] - apply Hcond_antitone - intro x y h - have H1 := congrArg Prod.fst h - have H2 := congrArg Prod.snd h - exact Prod.ext (show x.1.2=y.1.2 from congrArg Prod.snd H1) (show lblCtx P i x.2=lblCtx P i y.2 from H2) - -lemma opt_succ (p : Law A) (tag : A → G) (w : A → V) - (u : E → G) (c : E → ℝ) (n : ℕ) : - opt n p tag w u c ≤ opt (n+1) p tag w u c := by - obtain ⟨P,hP,he⟩ := (opt_spec (n+1) p tag w u c).1 - let X : WordSample (n+1) A E → Letters (n+1) A := Prod.fst - let U : WordSample (n+1) A E → E := Prod.snd - let Y (x : _) (i : Fin n) := X x i.succ - let A' := readP X 0 - let W (x) := ans tag X (fun x => u (U x)) x - let Z (x) := ans tag Y (fun x => u (U x)) x - let WX (x : WordSample (n+1) A E) := (W x,ctxt w X x) - let WY (x : WordSample (n+1) A E) := (Z x,ctxt w Y x) - have hr : rent tag w u c P Y U ≤ rent tag w u c P X U := by - have h : - P.Hcond X WX = P.Hcond Y (fun x => (A' x,WX x))+P.Hcond A' WX := by - rw [← Hcond_chain] - unfold Hcond - congr 1 - refine H_partition _ fun x y => ?_ - have hX : X x = X y ↔ Y x = Y y ∧ A' x = A' y := by - constructor - · intro h; dsimp only [Y,A',readP]; rw [h]; exact ⟨rfl,rfl⟩ - · rintro ⟨h1,h2⟩; funext i - cases i using Fin.cases with - | zero => exact h2 - | succ j => exact congrFun h1 j - simp only [Prod.mk.injEq,hX] - have hc : P.Hcond Y (fun x => (A' x,WX x)) ≤ P.Hcond Y WY := by - refine Hcond_antitone _ _ fun x y hh => ?_ - change (Z x,ctxt w Y x) = _ - have heW (x) : W x = tag (A' x)+Z x := by - unfold W Z ans tot - rw [Fin.sum_univ_succ,add_assoc] - rfl - have h1 := congrArg Prod.fst hh - have h2 := congrArg Prod.snd hh - have h3 : W x=W y := congrArg (fun a : G×_ => a.1) h2 - have h4 : ctxt w X x=ctxt w X y := congrArg (fun a : G×_ => a.2) h2 - rw [heW,heW, show A' x = A' y from h1] at h3 - exact Prod.ext (add_left_cancel h3) (funext fun i => congrFun h4 i.succ) - have hd : P.Hcond A' WX ≤ P.Hcond A' (w ∘ A') := by - refine Hcond_antitone P _ (fun x y hh => ?_) - have he : ctxt w X x = ctxt w X y := congrArg (fun a : G×_ => a.2) hh - exact congrFun he 0 - unfold rent - refine add_le_add_left ?_ _ - change _ - P.Hcond Y WY ≤ _ - P.Hcond X WX - rw [Fin.sum_univ_succ,h] - have hEq (i : Fin n) : readP X i.succ = readP Y i := rfl - simp_rw [hEq] - change _ ≤ P.Hcond A' (w ∘ A')+_ - _ - linarith - exact (opt_le_rent tag w u c p P Y U fun i => hP i.succ).trans (hr.trans he.le) - -lemma fundI_mono (tag : A → G) (P : Labelled A) : Monotone (fundI tag P) := - monotone_nat_of_le_succ (fun n => opt_succ P.law (tag ∘ Prod.fst) Prod.snd - (fun _ : Unit => 0) (fun _ => 0) n) - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Projection +import OAI.Analysis.FourierLLogL.Compress.Select + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A V W G E α β Ω Ω' : Type} [Fintype A] [Fintype G] [AddCommGroup G] + [Fintype α] [Fintype β] [Fintype Ω] [Fintype Ω'] [Fintype V] [Fintype W] + [Fintype E] [Nonempty E] {n : ℕ} +set_option linter.unusedSectionVars false + +/-- Numerical Context State. -/ +structure Labelled (A : Type) [Fintype A] where + obs : Type + [fObs : Fintype obs] + law : Law (A × obs) + +attribute [instance] Labelled.fObs + +def initial (p : Law A) : Labelled A := {obs := Unit, law:=p.send (fun x => (x,()))} + +def refines (P Q : Labelled A) : Prop := + ∃ u : P.obs → Q.obs, P.law.send (fun a => (a.1,u a.2)) = Q.law + +lemma refines_refl (p : Labelled A) : refines p p := by + refine ⟨id,?_⟩; exact send_id p.law + +lemma refines_trans {p q r : Labelled A} : + refines p q → refines q r → refines p r := by + rintro ⟨u,hu⟩ ⟨v,hv⟩ + rw [← hu,send_send] at hv + exact ⟨v ∘ u,hv⟩ + +def lblSum (tag : A → G) (W : Labelled A) (m : ℕ) (x : Letters m (A × W.obs)) : G := + tot (tag ∘ Prod.fst) x +def lblCtx (W : Labelled A) (m : ℕ) (x : Letters m (A × W.obs)) : Letters m W.obs := + fun i => (x i).2 + +/-- m-letter block of joint letter-and-context pairs, iid. -/ +def lblP (W : Labelled A) (m : ℕ) := iid W.law m + +/-- Conditional entropy of total sum. -/ +def fundG (tag : A → G) (p : Labelled A) (m : ℕ) := + (lblP p m).Hcond (lblSum tag p m) (lblCtx p m) + +/-- Conditional exact cost. -/ +def fundI (tag : A → G) (P : Labelled A) (r : ℕ) := + opt r P.law (tag ∘ Prod.fst) Prod.snd (fun _ : Unit => (0:G)) (fun _ => 0) + +/-- Conditional m-block noisy cost. -/ +def fundK (tag : A → G) (c : G → ℝ) (P : Labelled A) (m r : ℕ) := + opt r (lblP P m) (lblSum tag P m) (lblCtx P m) id c + +lemma I_anti (tag : A → G) {P Q : Labelled A} (h : refines P Q) (r : ℕ) : + fundI tag P r ≤ fundI tag Q r := by + obtain ⟨u,hu⟩ := h + unfold fundI + rw [← hu] + let f (x : A×P.obs) := (x.1,u x.2) + exact opt_refined (tag ∘ Prod.fst) Prod.snd Prod.snd (fun _ : Unit => 0) (fun _ => 0) P.law f r u rfl + +lemma K_anti (tag : A → G) (c : G → ℝ) {P Q : Labelled A} + (h : refines P Q) (m r : ℕ) : fundK tag c P m r ≤ fundK tag c Q m r := by + obtain ⟨u,hu⟩ := h + let X (x : A × P.obs) := (x.1,u x.2) + let f (x : Letters m _) i := X (x i) + have H : lblP Q m = (lblP P m).send f := by + unfold lblP iid; rw [show Q.law = _ from hu.symm] + exact (sendPack (fun _ : Fin m => P.law) (fun _ => X)).symm + unfold fundK + rw [H] + exact opt_refined (lblSum tag Q m) (lblCtx Q m) (lblCtx P m) id c (lblP P m) f r (fun x i => u (x i)) rfl + +lemma G_anti (tag : A → G) {P Q : Labelled A} (h : refines P Q) + (m : ℕ) : fundG tag P m ≤ fundG tag Q m := by + obtain ⟨u,hu⟩ := h + let X (x : A × P.obs) := (x.1,u x.2) + let f (x : Letters m _) i := X (x i) + have H : lblP Q m = (lblP P m).send f := by + unfold lblP iid; rw [show Q.law = _ from hu.symm] + exact (sendPack (fun _ : Fin m => P.law) (fun _ => X)).symm + unfold fundG + rw [H,Hcond_send] + apply Hcond_antitone + intro a b hh + funext i + exact congrArg u (congrFun hh i) + +lemma fundG_one (tag : A → G) (hi : Injective tag) (P : Labelled A) : + fundG tag P 1 = P.law.Hcond Prod.fst Prod.snd := by + let Q := lblP P 1 + have h : Q.send (fun x => x 0) = P.law := marginalPack _ _ + rw [← h,Hcond_send] + unfold fundG Hcond + congr 1 + · refine H_partition _ fun a b => ?_ + unfold lblSum lblCtx tot + simp only [Fin.sum_univ_one,comp_apply,Prod.mk.injEq,hi.eq_iff,funext_iff] + simp [Fin.forall_fin_one] + · refine H_partition _ fun a b => ?_ + simp [funext_iff,lblCtx, Fin.forall_fin_one] + +def appendOne {m : ℕ} (p : A × Letters m A) : Letters (m+1) A := Fin.cons p.1 p.2 + +lemma iid_succ (P : Law A) (m : ℕ) : + iid P (m+1) = (P.tensorP (iid P m)).send appendOne := by + apply ext_by_products + intro f + dsimp only [iid] + rw [Ex_send,Ex_tensorP] + rw [pack_Ex_prod _ f] + simp_rw [Fin.prod_univ_succ] + unfold appendOne + simp only [Fin.cons_zero,Fin.cons_succ] + simp_rw [Ex_lmul,pack_Ex_prod _ (fun i : Fin m => f i.succ),Ex_mul] + +lemma cons_part {m : ℕ} {a b : A} {x y : Letters m A} : + (Fin.cons a x : Letters (m+1) A) = Fin.cons b y ↔ a = b ∧ x = y := by + constructor + · intro h + exact ⟨by simpa using congrFun h 0,by funext i; simpa using congrFun h i.succ⟩ + · rintro ⟨rfl,rfl⟩; rfl + + +lemma Hcond_tensor (P : Law Ω) (Q : Law Ω') (X : Ω → α) (Z : Ω' → β) + (Y : Ω → V) (T : Ω' → W) : + (P.tensorP Q).Hcond (fun x => (X x.1,Z x.2)) (fun x => (Y x.1,T x.2)) = + P.Hcond X Y + Q.Hcond Z T := by + unfold Hcond + rw [H_partition (X:=fun (x:Ω×Ω') => ((X x.1,Z x.2),(Y x.1,T x.2))) + (Y:=fun x => ((X x.1,Y x.1),(Z x.2,T x.2))) _ (fun _ _ => by simp; tauto)] + rw [H_tensorP P Q (fun x => (X x,Y x)) (fun x => (Z x,T x)),H_tensorP]; ring + +lemma Hcond_tensor_right (P : Law Ω) (Q : Law Ω') (Z : Ω' → β) + (Y : Ω → V) (T : Ω' → W) : + (P.tensorP Q).Hcond (fun x => Z x.2) (fun x => (Y x.1,T x.2)) = Q.Hcond Z T := by + let X (_x : Ω) := () + rw [show Q.Hcond Z T = P.Hcond X Y + Q.Hcond Z T by + rw [Hcond_determined (P:=P) (X:=X) (W:=Y) (fun _ _ _ => rfl)]; ring, + ← Hcond_tensor] + unfold Hcond + congr 1 + apply H_partition + simp only [Prod.mk.injEq, X]; tauto + +lemma lbl_succ_sum (tag : A → G) (P : Labelled A) (m : ℕ) + (x : (A×P.obs) × Letters m (A × P.obs)) : + lblSum tag P (m+1) (appendOne x) = tag x.1.1 + lblSum tag P m x.2 := by + unfold lblSum tot; rw [Fin.sum_univ_succ]; rfl + +lemma lbl_succ_ctx (P : Labelled A) (m : ℕ) + (x : (A×P.obs) × Letters m (A × P.obs)) : + lblCtx P (m+1) (appendOne x) = appendOne (x.1.2,lblCtx P m x.2) := by + funext i + cases i using Fin.cases <;> simp [lblCtx,appendOne] + +lemma fundG_succ (tag : A → G) (P : Labelled A) (m : ℕ) : + let Q := P.law.tensorP (lblP P m) + let X (x : (A×P.obs) × Letters m (A×P.obs)) := tag x.1.1 + lblSum tag P m x.2 + let Z (x : (A×P.obs) × Letters m (A×P.obs)) := (x.1.2,lblCtx P m x.2) + fundG tag P (m+1) = Q.Hcond X Z := by + intro Q X Z + unfold fundG + rw [show lblP P (m+1)=Q.send appendOne from iid_succ P.law m,Hcond_send] + simp_rw [lbl_succ_sum,lbl_succ_ctx] + refine Hcond_right_partition _ X (fun x y => ?_) + unfold appendOne Z + simp only [cons_part,Prod.mk.injEq] + +lemma fundG_mono (tag : A → G) (P : Labelled A) : Monotone (fundG tag P) := by + refine monotone_nat_of_le_succ fun i => ?_ + rw [fundG_succ] + let Q := lblP P i + let R := P.law.tensorP Q + let W (x : (A×P.obs) × Letters i (A×P.obs)) := (x.1, lblCtx P i x.2) + let X (x : (A×P.obs) × Letters i (A×P.obs)) := tag x.1.1 + lblSum tag P i x.2 + have hr : R.Hcond X W = R.Hcond (fun x => lblSum tag P i x.2) W := by + unfold Hcond + congr 1 + apply H_partition + intro x y + simp only [W,X,Prod.mk.injEq] + constructor + · rintro ⟨h,h1,h2⟩; rw [h1] at h; exact ⟨add_left_cancel h,h1,h2⟩ + · rintro ⟨h,h1,h2⟩; exact ⟨by rw [h,h1],h1,h2⟩ + have he : R.Hcond (fun x => lblSum tag P i x.2) W = fundG tag P i := + Hcond_tensor_right P.law Q (lblSum tag P i) (fun x => x) (lblCtx P i) + change fundG tag P i ≤ R.Hcond X _ + rw [← hr.trans he] + apply Hcond_antitone + intro x y h + have H1 := congrArg Prod.fst h + have H2 := congrArg Prod.snd h + exact Prod.ext (show x.1.2=y.1.2 from congrArg Prod.snd H1) (show lblCtx P i x.2=lblCtx P i y.2 from H2) + +lemma opt_succ (p : Law A) (tag : A → G) (w : A → V) + (u : E → G) (c : E → ℝ) (n : ℕ) : + opt n p tag w u c ≤ opt (n+1) p tag w u c := by + obtain ⟨P,hP,he⟩ := (opt_spec (n+1) p tag w u c).1 + let X : WordSample (n+1) A E → Letters (n+1) A := Prod.fst + let U : WordSample (n+1) A E → E := Prod.snd + let Y (x : _) (i : Fin n) := X x i.succ + let A' := readP X 0 + let W (x) := ans tag X (fun x => u (U x)) x + let Z (x) := ans tag Y (fun x => u (U x)) x + let WX (x : WordSample (n+1) A E) := (W x,ctxt w X x) + let WY (x : WordSample (n+1) A E) := (Z x,ctxt w Y x) + have hr : rent tag w u c P Y U ≤ rent tag w u c P X U := by + have h : + P.Hcond X WX = P.Hcond Y (fun x => (A' x,WX x))+P.Hcond A' WX := by + rw [← Hcond_chain] + unfold Hcond + congr 1 + refine H_partition _ fun x y => ?_ + have hX : X x = X y ↔ Y x = Y y ∧ A' x = A' y := by + constructor + · intro h; dsimp only [Y,A',readP]; rw [h]; exact ⟨rfl,rfl⟩ + · rintro ⟨h1,h2⟩; funext i + cases i using Fin.cases with + | zero => exact h2 + | succ j => exact congrFun h1 j + simp only [Prod.mk.injEq,hX] + have hc : P.Hcond Y (fun x => (A' x,WX x)) ≤ P.Hcond Y WY := by + refine Hcond_antitone _ _ fun x y hh => ?_ + change (Z x,ctxt w Y x) = _ + have heW (x) : W x = tag (A' x)+Z x := by + unfold W Z ans tot + rw [Fin.sum_univ_succ,add_assoc] + rfl + have h1 := congrArg Prod.fst hh + have h2 := congrArg Prod.snd hh + have h3 : W x=W y := congrArg (fun a : G×_ => a.1) h2 + have h4 : ctxt w X x=ctxt w X y := congrArg (fun a : G×_ => a.2) h2 + rw [heW,heW, show A' x = A' y from h1] at h3 + exact Prod.ext (add_left_cancel h3) (funext fun i => congrFun h4 i.succ) + have hd : P.Hcond A' WX ≤ P.Hcond A' (w ∘ A') := by + refine Hcond_antitone P _ (fun x y hh => ?_) + have he : ctxt w X x = ctxt w X y := congrArg (fun a : G×_ => a.2) hh + exact congrFun he 0 + unfold rent + refine add_le_add_left ?_ _ + change _ - P.Hcond Y WY ≤ _ - P.Hcond X WX + rw [Fin.sum_univ_succ,h] + have hEq (i : Fin n) : readP X i.succ = readP Y i := rfl + simp_rw [hEq] + change _ ≤ P.Hcond A' (w ∘ A')+_ - _ + linarith + exact (opt_le_rent tag w u c p P Y U fun i => hP i.succ).trans (hr.trans he.le) + +lemma fundI_mono (tag : A → G) (P : Labelled A) : Monotone (fundI tag P) := + monotone_nat_of_le_succ (fun n => opt_succ P.law (tag ∘ Prod.fst) Prod.snd + (fun _ : Unit => 0) (fun _ => 0) n) + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Cost.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Cost.lean index bd35c64a0047de6536c41ddef856a1bace67a41c..70b289e2d7033db53e3269d6f1fde5709cf7106e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Cost.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Cost.lean @@ -1,232 +1,232 @@ -import OAI.Analysis.FourierLLogL.Finite.Alternative -import OAI.Analysis.FourierLLogL.Finite.Independent -/-! # Noisy sum entropy cost -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL.Compress -open Real Law - -variable {ι τ κ α Ω Ω' A G : Type} -variable [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] [Fintype κ] [DecidableEq κ] -variable [Fintype α] [Fintype Ω] [Fintype Ω'] [Fintype A] [Fintype G] [AddCommGroup G] - -def tot (tag : α → G) (x : ι → α) : G := ∑ i, tag (x i) - -def ans (tag : α → G) (X : Ω → ι → α) (U : Ω → G) (a : Ω) := tot tag (X a)+U a - -/-- Noisy bill: cross entropy with fixed comparator law per position, entropy of -letter vector conditional on noisy sum, plus error cost. -/ -def bill (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) := - (∑ i, P.Ex fun a => -log (q (X a i))) - P.Hcond X (ans tag X U) + P.Ex (fun a => c (U a)) - -lemma bill_send (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω' → ι → α) (U : Ω' → G) (W : Ω → Ω') : - bill tag c q (P.send W) X U = bill tag c q P (fun x => X (W x)) (fun x => U (W x)) := by - unfold bill; simp_rw [Hcond_send,Ex_send]; rfl - -lemma bill_mixture_le (tag : α → G) (c : G → ℝ) (q : Law α) - (P Q : Law Ω) (X : Ω → ι → α) (U : Ω → G) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - bill tag c q (mixture P Q t h0 h1) X U ≤ - t*bill tag c q P X U + (1-t)*bill tag c q Q X U := by - unfold bill - simp_rw [mixture_Ex,Finset.sum_add_distrib, ← Finset.mul_sum] - have H := Hcond_mixture_ge P Q t h0 h1 X (ans tag X U) - linarith - -lemma bill_cont (tag : α → G) (c : G → ℝ) (q : Law α) - (X : Ω → ι → α) (U : Ω → G) : - Continuous (fun P : Law Ω => bill tag c q P X U) := - ((continuous_finsetSum _ fun _ _ => continuous_Ex _).sub (continuous_Hcond _ _)).add - (continuous_Ex _) - -lemma bill_through_le (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω' → ι → α) (U : Ω' → G) (Q : Ω → Law Ω') : - bill tag c q (P.through Q) X U ≤ P.Ex (fun a => bill tag c q (Q a) X U) := by - unfold bill - simp_rw [Ex_add,Ex_sub,Ex_through, ← Ex_finset_sum] - linarith [Hcond_through P Q X (ans tag X U)] - -/-- All letters have support contained in comparison law's. -/ -def covered (P : Law Ω) (X : Ω → ι → α) (q : Law α) : Prop := - ∀ x, 0 < P x → ∀ i, 0 < q (X x i) - -lemma covered_pack {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) : - (P.send X).dominated (pack (fun _ => q)) := by - intro x hx - obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx - exact (pack_pos _ _).mpr (h a ha) - -lemma sum_crosspack {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) : - (P.send X).crossE (pack (fun _ => q)) = ∑ i, P.Ex fun a => -log (q (X a i)) := by - rw [crossE_pack (covered_pack h)] - simp_rw [crossE,Ex_send] - -lemma kl_le_bill {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (tag : α → G) (c : G → ℝ) (_hc : ∀ x, 0 ≤ c x) (U : Ω → G) : - (P.send X).kl (pack (fun _ => q)) + P.Ex (fun a => c (U a)) ≤ bill tag c q P X U := by - unfold bill kl - rw [sum_crosspack h] - exact add_le_add_left (sub_le_sub_left (Hcond_le_H P X _) _) _ - -lemma bill_nonneg {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (tag : α → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (U : Ω → G) : - 0 ≤ bill tag c q P X U := le_trans - (add_nonneg (kl_nonneg (covered_pack h)) (P.Ex_nonneg fun a => hc (U a))) - (kl_le_bill h tag c hc U) - -lemma bill_with_entropy {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (tag : α → G) (c : G → ℝ) (U : Ω → G) : - bill tag c q P X U = - (P.send X).kl (pack (fun _ => q)) + (P.H (ans tag X U) - P.Hcond U X) + - P.Ex fun a => c (U a) := by - let Z := ans tag X U - have h1 : P.H (fun a => (U a,X a)) = P.H (fun a => (X a,Z a)) := - P.H_partition fun x y => by - simp only [Z,ans,Prod.mk.injEq] - constructor - · rintro ⟨hu,hx⟩; rw [hu,hx]; exact ⟨rfl,rfl⟩ - · rintro ⟨hx,hz⟩; rw [hx] at hz; exact ⟨add_left_cancel hz,hx⟩ - unfold bill kl - rw [← sum_crosspack h] - change _ - P.Hcond X Z + _ = (_ - P.H X) + (P.H Z- P.Hcond U X)+_ - unfold Hcond; rw [h1]; ring - -lemma bill_change {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (tag : α → G) (c : G → ℝ) (U : Ω → G) (p : Law α) - (hp : ∀ i, P.send (fun x => X x i) = p) : - bill tag c p P X U ≤ bill tag c q P X U := by - apply add_le_add_left - apply sub_le_sub_right - refine Finset.sum_le_sum fun i _ => ?_ - let x (a : Ω) := X a i - have h1 : p.dominated q := by - intro b hb - have HH : 0 < P.send x b := by rw [hp i]; exact hb - obtain ⟨a,rfl,ha⟩ := (P.send_pos_iff _ b).mp HH - exact h a ha i - suffices hi : (P.send x).crossE p ≤ (P.send x).crossE q by - simpa only [crossE, Ex_send] using hi - rw [show x = _ from rfl, hp i] - exact sub_nonneg.mp (kl_nonneg h1) - -lemma bill_condition (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (W : Ω → A) : - (P.send W).Ex (fun w => bill tag c q (P.after W w) X U) ≤ - bill tag c q P X U + P.H W := by - let Z := ans tag X U - have hE (f : Ω → ℝ) : - (P.send W).Ex (fun w => (P.after W w).Ex f) = P.Ex f := - P.after_Ex_sum W (fun _ => f) - have hY := Hcond_after_cond P X Z W - have hP := P.Hcond_nonneg W (fun x => (X x,Z x)) - have hl := Hcond_le_H P W Z - have heq := P.H_partition (X:=fun x => ((X x), Z x,W x)) - (Y:=fun x => (W x,X x,Z x)) (fun _ _ => by simp; tauto) - have hp := P.H_pair_rev W Z - unfold bill - simp_rw [Ex_add,Ex_sub,Ex_finset_sum,hE] - change _ - ((P.send W).Ex (fun w => (P.after W w).Hcond X Z)) + _ ≤ - (_-P.Hcond X Z + _)+_ - rw [← hY] - unfold Hcond at * - linarith - - -omit [DecidableEq ι] [DecidableEq τ] [DecidableEq κ] in -lemma sum_split' (e : τ ⊕ κ ≃ ι) {M : Type*} [AddCommMonoid M] (f : ι → M) : - ∑ i, f i = ∑ i, f (e (.inl i)) + ∑ i, f (e (.inr i)) := - calc - _ = ∑ i, f (e i) := - (Fintype.sum_equiv e _ f (fun _ => rfl)).symm - _ = _ := Fintype.sum_sum_type _ - -def keep (X : Ω → ι → α) (s : τ → ι) := fun a i => X a (s i) - -lemma bill_split (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (h : covered P X q) (e : τ ⊕ κ ≃ ι) : - let V := keep X (fun i => e (.inl i)) - let W := keep X (fun i => e (.inr i)) - (P.send W).Ex (fun w => bill tag c q (P.after W w) V U) ≤ bill tag c q P X U := by - intro V W - let Y := ans tag V U - let Z := ans tag X U - have hs (x : Ω) : Z x = Y x + tot tag (W x) := by - unfold Z Y ans tot - rw [sum_split' e (fun i => tag _)] - dsimp only [V,W,keep] - abel - have hh (x y : Ω) : X x = X y ↔ (V x,W x) = (V y,W y) := by - constructor - · intro h; unfold V W keep; rw [h] - · intro H - funext i - obtain ⟨t,rfl⟩ := e.surjective i - rcases t with t|t - · exact congrFun (congrArg Prod.fst H) t - · exact congrFun (congrArg Prod.snd H) t - have hpart : - P.Hcond X Z = P.Hcond (fun x => (V x,W x)) Z := by - unfold Hcond - congr 1 - exact P.H_partition fun x y => by simp [hh x y] - have hpart' : - P.Hcond V (fun x => (W x,Z x)) = P.Hcond V (fun x => (Y x,W x)) := by - apply Hcond_right_partition - intro a b - simp only [Prod.mk.injEq, hs] - constructor - · rintro ⟨h,he⟩; rw [h] at he; exact ⟨add_right_cancel he,h⟩ - · rintro ⟨h,h'⟩; rw [h,h']; exact ⟨rfl,rfl⟩ - have hCE : P.Hcond W Z ≤ ∑ i, P.Ex (fun x => -log (q (W x i))) := by - have hcW : covered P W q := fun x hx _ => h x hx _ - apply le_trans (P.Hcond_le_H W Z) - rw [← sum_crosspack hcW] - exact sub_nonneg.mp (kl_nonneg (covered_pack hcW)) - have hE (f : Ω → ℝ) : - (P.send W).Ex (fun w => (P.after W w).Ex f) = P.Ex f := - P.after_Ex_sum W (fun _ => f) - unfold bill - simp_rw [Ex_add,Ex_sub,Ex_finset_sum,hE, ← Hcond_after_cond] - change _ - P.Hcond V _ + _ ≤ (_ - P.Hcond X Z) + _ - rw [sum_split' e (fun i : ι => P.Ex (fun x => -log (q (X x i)))), - hpart,Hcond_chain,hpart'] - change _ - P.Hcond V (fun x => (Y x,W x)) + _ ≤ - ((∑ i, P.Ex fun x => -log (q (V x i))) + (∑ i, P.Ex fun x => -log (q (W x i))) - _) + _ - linarith - -lemma bill_subfamily (tag : α → G) (c : G → ℝ) (q : Law α) - (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (h : covered P X q) - (g : τ → ι) (hg : Injective g) : - bill tag c q P (keep X g) U ≤ bill tag c q P X U := by - classical - let e := (Equiv.sumCongr (Equiv.ofInjective g hg) - (Equiv.refl {x // ¬ (fun y => y ∈ range g) x})).trans (Equiv.sumCompl (fun y => y ∈ range g)) - let V := keep X (fun i => e (.inl i)) - let W := keep X (fun i => e (.inr i)) - have H : - bill tag c q P V U ≤ bill tag c q P X U := by - refine le_trans ?_ (bill_split tag c q P X U h e) - have hh := bill_through_le tag c q (P.send W) V U (P.after W) - rw [through_after] at hh - exact hh - have heq : V = keep X g := rfl - rwa [heq] at H - -omit [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] in -lemma covered_subfamily {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (g : τ → ι) : covered P (keep X g) q := fun x hx _ => h x hx _ - -omit [Fintype ι] [DecidableEq ι] in -lemma covered_after {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) - (W : Ω → A) (w : A) (hw : 0 < P.send W w) : covered (P.after W w) X q := - fun x hx i => h x ((after_mem hw).mp hx).2 i - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Alternative +import OAI.Analysis.FourierLLogL.Finite.Independent +/-! # Noisy sum entropy cost -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL.Compress +open Real Law + +variable {ι τ κ α Ω Ω' A G : Type} +variable [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] [Fintype κ] [DecidableEq κ] +variable [Fintype α] [Fintype Ω] [Fintype Ω'] [Fintype A] [Fintype G] [AddCommGroup G] + +def tot (tag : α → G) (x : ι → α) : G := ∑ i, tag (x i) + +def ans (tag : α → G) (X : Ω → ι → α) (U : Ω → G) (a : Ω) := tot tag (X a)+U a + +/-- Noisy bill: cross entropy with fixed comparator law per position, entropy of +letter vector conditional on noisy sum, plus error cost. -/ +def bill (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) := + (∑ i, P.Ex fun a => -log (q (X a i))) - P.Hcond X (ans tag X U) + P.Ex (fun a => c (U a)) + +lemma bill_send (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω' → ι → α) (U : Ω' → G) (W : Ω → Ω') : + bill tag c q (P.send W) X U = bill tag c q P (fun x => X (W x)) (fun x => U (W x)) := by + unfold bill; simp_rw [Hcond_send,Ex_send]; rfl + +lemma bill_mixture_le (tag : α → G) (c : G → ℝ) (q : Law α) + (P Q : Law Ω) (X : Ω → ι → α) (U : Ω → G) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + bill tag c q (mixture P Q t h0 h1) X U ≤ + t*bill tag c q P X U + (1-t)*bill tag c q Q X U := by + unfold bill + simp_rw [mixture_Ex,Finset.sum_add_distrib, ← Finset.mul_sum] + have H := Hcond_mixture_ge P Q t h0 h1 X (ans tag X U) + linarith + +lemma bill_cont (tag : α → G) (c : G → ℝ) (q : Law α) + (X : Ω → ι → α) (U : Ω → G) : + Continuous (fun P : Law Ω => bill tag c q P X U) := + ((continuous_finsetSum _ fun _ _ => continuous_Ex _).sub (continuous_Hcond _ _)).add + (continuous_Ex _) + +lemma bill_through_le (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω' → ι → α) (U : Ω' → G) (Q : Ω → Law Ω') : + bill tag c q (P.through Q) X U ≤ P.Ex (fun a => bill tag c q (Q a) X U) := by + unfold bill + simp_rw [Ex_add,Ex_sub,Ex_through, ← Ex_finset_sum] + linarith [Hcond_through P Q X (ans tag X U)] + +/-- All letters have support contained in comparison law's. -/ +def covered (P : Law Ω) (X : Ω → ι → α) (q : Law α) : Prop := + ∀ x, 0 < P x → ∀ i, 0 < q (X x i) + +lemma covered_pack {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) : + (P.send X).dominated (pack (fun _ => q)) := by + intro x hx + obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx + exact (pack_pos _ _).mpr (h a ha) + +lemma sum_crosspack {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) : + (P.send X).crossE (pack (fun _ => q)) = ∑ i, P.Ex fun a => -log (q (X a i)) := by + rw [crossE_pack (covered_pack h)] + simp_rw [crossE,Ex_send] + +lemma kl_le_bill {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (tag : α → G) (c : G → ℝ) (_hc : ∀ x, 0 ≤ c x) (U : Ω → G) : + (P.send X).kl (pack (fun _ => q)) + P.Ex (fun a => c (U a)) ≤ bill tag c q P X U := by + unfold bill kl + rw [sum_crosspack h] + exact add_le_add_left (sub_le_sub_left (Hcond_le_H P X _) _) _ + +lemma bill_nonneg {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (tag : α → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (U : Ω → G) : + 0 ≤ bill tag c q P X U := le_trans + (add_nonneg (kl_nonneg (covered_pack h)) (P.Ex_nonneg fun a => hc (U a))) + (kl_le_bill h tag c hc U) + +lemma bill_with_entropy {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (tag : α → G) (c : G → ℝ) (U : Ω → G) : + bill tag c q P X U = + (P.send X).kl (pack (fun _ => q)) + (P.H (ans tag X U) - P.Hcond U X) + + P.Ex fun a => c (U a) := by + let Z := ans tag X U + have h1 : P.H (fun a => (U a,X a)) = P.H (fun a => (X a,Z a)) := + P.H_partition fun x y => by + simp only [Z,ans,Prod.mk.injEq] + constructor + · rintro ⟨hu,hx⟩; rw [hu,hx]; exact ⟨rfl,rfl⟩ + · rintro ⟨hx,hz⟩; rw [hx] at hz; exact ⟨add_left_cancel hz,hx⟩ + unfold bill kl + rw [← sum_crosspack h] + change _ - P.Hcond X Z + _ = (_ - P.H X) + (P.H Z- P.Hcond U X)+_ + unfold Hcond; rw [h1]; ring + +lemma bill_change {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (tag : α → G) (c : G → ℝ) (U : Ω → G) (p : Law α) + (hp : ∀ i, P.send (fun x => X x i) = p) : + bill tag c p P X U ≤ bill tag c q P X U := by + apply add_le_add_left + apply sub_le_sub_right + refine Finset.sum_le_sum fun i _ => ?_ + let x (a : Ω) := X a i + have h1 : p.dominated q := by + intro b hb + have HH : 0 < P.send x b := by rw [hp i]; exact hb + obtain ⟨a,rfl,ha⟩ := (P.send_pos_iff _ b).mp HH + exact h a ha i + suffices hi : (P.send x).crossE p ≤ (P.send x).crossE q by + simpa only [crossE, Ex_send] using hi + rw [show x = _ from rfl, hp i] + exact sub_nonneg.mp (kl_nonneg h1) + +lemma bill_condition (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (W : Ω → A) : + (P.send W).Ex (fun w => bill tag c q (P.after W w) X U) ≤ + bill tag c q P X U + P.H W := by + let Z := ans tag X U + have hE (f : Ω → ℝ) : + (P.send W).Ex (fun w => (P.after W w).Ex f) = P.Ex f := + P.after_Ex_sum W (fun _ => f) + have hY := Hcond_after_cond P X Z W + have hP := P.Hcond_nonneg W (fun x => (X x,Z x)) + have hl := Hcond_le_H P W Z + have heq := P.H_partition (X:=fun x => ((X x), Z x,W x)) + (Y:=fun x => (W x,X x,Z x)) (fun _ _ => by simp; tauto) + have hp := P.H_pair_rev W Z + unfold bill + simp_rw [Ex_add,Ex_sub,Ex_finset_sum,hE] + change _ - ((P.send W).Ex (fun w => (P.after W w).Hcond X Z)) + _ ≤ + (_-P.Hcond X Z + _)+_ + rw [← hY] + unfold Hcond at * + linarith + + +omit [DecidableEq ι] [DecidableEq τ] [DecidableEq κ] in +lemma sum_split' (e : τ ⊕ κ ≃ ι) {M : Type*} [AddCommMonoid M] (f : ι → M) : + ∑ i, f i = ∑ i, f (e (.inl i)) + ∑ i, f (e (.inr i)) := + calc + _ = ∑ i, f (e i) := + (Fintype.sum_equiv e _ f (fun _ => rfl)).symm + _ = _ := Fintype.sum_sum_type _ + +def keep (X : Ω → ι → α) (s : τ → ι) := fun a i => X a (s i) + +lemma bill_split (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (h : covered P X q) (e : τ ⊕ κ ≃ ι) : + let V := keep X (fun i => e (.inl i)) + let W := keep X (fun i => e (.inr i)) + (P.send W).Ex (fun w => bill tag c q (P.after W w) V U) ≤ bill tag c q P X U := by + intro V W + let Y := ans tag V U + let Z := ans tag X U + have hs (x : Ω) : Z x = Y x + tot tag (W x) := by + unfold Z Y ans tot + rw [sum_split' e (fun i => tag _)] + dsimp only [V,W,keep] + abel + have hh (x y : Ω) : X x = X y ↔ (V x,W x) = (V y,W y) := by + constructor + · intro h; unfold V W keep; rw [h] + · intro H + funext i + obtain ⟨t,rfl⟩ := e.surjective i + rcases t with t|t + · exact congrFun (congrArg Prod.fst H) t + · exact congrFun (congrArg Prod.snd H) t + have hpart : + P.Hcond X Z = P.Hcond (fun x => (V x,W x)) Z := by + unfold Hcond + congr 1 + exact P.H_partition fun x y => by simp [hh x y] + have hpart' : + P.Hcond V (fun x => (W x,Z x)) = P.Hcond V (fun x => (Y x,W x)) := by + apply Hcond_right_partition + intro a b + simp only [Prod.mk.injEq, hs] + constructor + · rintro ⟨h,he⟩; rw [h] at he; exact ⟨add_right_cancel he,h⟩ + · rintro ⟨h,h'⟩; rw [h,h']; exact ⟨rfl,rfl⟩ + have hCE : P.Hcond W Z ≤ ∑ i, P.Ex (fun x => -log (q (W x i))) := by + have hcW : covered P W q := fun x hx _ => h x hx _ + apply le_trans (P.Hcond_le_H W Z) + rw [← sum_crosspack hcW] + exact sub_nonneg.mp (kl_nonneg (covered_pack hcW)) + have hE (f : Ω → ℝ) : + (P.send W).Ex (fun w => (P.after W w).Ex f) = P.Ex f := + P.after_Ex_sum W (fun _ => f) + unfold bill + simp_rw [Ex_add,Ex_sub,Ex_finset_sum,hE, ← Hcond_after_cond] + change _ - P.Hcond V _ + _ ≤ (_ - P.Hcond X Z) + _ + rw [sum_split' e (fun i : ι => P.Ex (fun x => -log (q (X x i)))), + hpart,Hcond_chain,hpart'] + change _ - P.Hcond V (fun x => (Y x,W x)) + _ ≤ + ((∑ i, P.Ex fun x => -log (q (V x i))) + (∑ i, P.Ex fun x => -log (q (W x i))) - _) + _ + linarith + +lemma bill_subfamily (tag : α → G) (c : G → ℝ) (q : Law α) + (P : Law Ω) (X : Ω → ι → α) (U : Ω → G) (h : covered P X q) + (g : τ → ι) (hg : Injective g) : + bill tag c q P (keep X g) U ≤ bill tag c q P X U := by + classical + let e := (Equiv.sumCongr (Equiv.ofInjective g hg) + (Equiv.refl {x // ¬ (fun y => y ∈ range g) x})).trans (Equiv.sumCompl (fun y => y ∈ range g)) + let V := keep X (fun i => e (.inl i)) + let W := keep X (fun i => e (.inr i)) + have H : + bill tag c q P V U ≤ bill tag c q P X U := by + refine le_trans ?_ (bill_split tag c q P X U h e) + have hh := bill_through_le tag c q (P.send W) V U (P.after W) + rw [through_after] at hh + exact hh + have heq : V = keep X g := rfl + rwa [heq] at H + +omit [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] in +lemma covered_subfamily {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (g : τ → ι) : covered P (keep X g) q := fun x hx _ => h x hx _ + +omit [Fintype ι] [DecidableEq ι] in +lemma covered_after {P : Law Ω} {X : Ω → ι → α} {q : Law α} (h : covered P X q) + (W : Ω → A) (w : A) (hw : 0 < P.send W w) : covered (P.after W w) X q := + fun x hx i => h x ((after_mem hw).mp hx).2 i + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Endpoint.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Endpoint.lean index 05c4cdea46e493cc1b0a8cc4da7f5d7e73924421..b278645317b61bb36397bbb1819ff3c3f1d1d398 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Endpoint.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Endpoint.lean @@ -1,222 +1,222 @@ -import OAI.Analysis.FourierLLogL.Compress.Theorem -import OAI.Analysis.FourierLLogL.Compress.Params -/-! # The compression lemma, asymptotic scales -/ -open Set Function Filter -open scoped Topology BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Real Law Params - -lemma log_ticket (r j : ℕ) (hr : 1 ≤ r) (h : r ≤ 2^j) (hj : 1 ≤ j) : - log (Fintype.card (Ticket r (2*r)) : ℝ) ≤ 1 + r*(2*(j:ℝ)) := by - have hp : 0 < (r:ℝ) := by exact_mod_cast hr - have hcard : Fintype.card (Fin r ↪ Fin (2*r)) ≤ (2*r)^r := by - have hi := Fintype.card_le_of_injective - (fun (e : Fin r ↪ Fin (2*r)) => (e : Fin r → Fin (2*r))) (by intro a b hab; ext i : 1; exact congrFun hab i) - simpa using hi - have hl2 := log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num) - have hx : log (2*(r:ℝ)) ≤ 2*(j:ℝ) := by - rw [log_mul (by norm_num) hp.ne'] - have h1 := Real.log_le_log hp (show (r:ℝ) ≤ 2^j by exact_mod_cast h) - rw [log_pow] at h1 - have hJ : (1:ℝ) ≤ j := by exact_mod_cast hj - nlinarith - unfold Ticket - rw [Fintype.card_option] - have ht : 0 < ((Fintype.card (Fin r ↪ Fin (2*r)) + 1:ℕ):ℝ) := by positivity - apply le_trans (Real.log_le_log ht (y:=2*(2*(r:ℝ))^r) ?_) ?_ - · have h1 : Fintype.card (Fin r ↪ Fin (2*r)) + 1 ≤ 2*(2*r)^r := by - have hp : 0 < (2*r)^r := pow_pos (by omega) _ - omega - exact_mod_cast h1 - rw [Real.log_mul (by norm_num) (by positivity),Real.log_pow] - nlinarith - -private lemma approx_error {d h F D T m j : ℝ} (hh : 1 ≤ h) (hj : h ≤ j) - (hd : 0 < d) (hm : 0 < m) (hD : 1 ≤ D) - (hF : d/h^2 ≤ F) (hFd : F ≤ D*d) - (Ht : d=4*T*h^6) - (H : 1024*D*j^16 ≤ m) (H' : m^4 ≤ d) : - (2*m*√F/T)*(d*j^2) ≤ F := by - have hh' : 0 < h := by linarith - have hF' : 0 < F := lt_of_lt_of_le (by positivity) hF - have hp : 0 < T := pos_of_mul_pos_left (show 0 < T*(4*h^6) by - calc 0 < d := hd - _ = T*(4*h^6) := by rw [Ht]; ring) (by positivity) - have hw : 8*D*(j^2*h^8) ≤ m := by - apply le_trans _ H - have hj1 : 1 ≤ j := hh.trans hj - calc - _ ≤ 8*D*(j^2*j^8) := by gcongr - _ ≤ 1024*D*j^16 := by - have HH : j^10 ≤ j^16 := pow_le_pow_right₀ hj1 (by omega) - rw [← pow_add]; gcongr; norm_num - have hsr : √F ≤ D*√d := by - have Hx := sq_sqrt hF'.le - have Hy := sq_sqrt hd.le - have Hh : D*d ≤ (D*√d)^2 := by - rw [mul_pow,Hy]; gcongr; nlinarith - nlinarith [hFd.trans Hh,sqrt_nonneg F, show 0 ≤ D*√d by positivity] - have hQ : m^2 ≤ √d := by - have Ht := sq_sqrt hd.le - nlinarith [show m^4=(m^2)^2 by ring,sqrt_nonneg d,sq_nonneg (m^2-√d)] - have hz : 8*m*√F*j^2*h^8 ≤ d := by - calc - _ ≤ m * √d * (8*D*(j^2*h^8)) := by - calc - _ ≤ 8*m*(D*√d)*j^2*h^8 := by gcongr - _ = _ := by ring - _ ≤ m*√d*m := mul_le_mul_of_nonneg_left hw (by positivity) - _ = m^2*√d := by ring - _ ≤ √d*√d := mul_le_mul_of_nonneg_right hQ (sqrt_nonneg _) - _ = d := by rw [← sq,sq_sqrt hd.le] - apply le_trans (b:=d/h^2) _ hF - apply (le_div_iff₀ (pow_pos hh' 2)).mpr - - have hle : 2*m*√F*j^2*h^2 ≤ T := by - rw [Ht] at hz - refine le_of_mul_le_mul_right ?_ (show 0 < 4*h^6 by positivity) - calc - _ = 8*m*√F*j^2*h^8 := by ring - _ ≤ 4*T*h^6 := hz - _ = T*(4*h^6) := by ring - calc - _ = (2*m*√F*j^2*h^2)/T*d := by ring - _ ≤ 1*d := mul_le_mul_of_nonneg_right ((div_le_one hp).mpr hle) hd.le - _ = d := one_mul d - -private lemma size_bound {d h F D T a : ℝ} (hh : 1 ≤ h) (hd : 0 < d) (hT : 0 < T) - (hD : 1 ≤ D) (hp : 0 < F) (hF : F ≤ D*d) - (Ht : d=4*T*h^6) - (H : D*h^8 ≤ d) (ha : a ≤ 66/h^2) : - 8*(3*(F/T)+a*(20*D*d)*(20*D*d)/F)+2 ≤ 300000*D^2*d^2/(F*h^2) := by - have hh' : 0 < h := by linarith - have hc : 0 < 20*D*d := by positivity - apply (le_div_iff₀ (by positivity : 0 < F*h^2)).mpr - let x := D^2*d^2 - have hx : 0 ≤ x := by unfold x; positivity - have hF1 : F^2 ≤ x := by unfold x; rw [← mul_pow]; gcongr - have hT1 : h^2 ≤ 4*T := by - have H1 : h^8 ≤ d := le_trans (le_mul_of_one_le_left (by positivity) hD) H - rw [Ht,show h^8=h^2*h^6 by ring] at H1 - exact le_of_mul_le_mul_right H1 (by positivity) - have he1 : F/T*(F*h^2) ≤ 4*x := calc - _ ≤ F/T*(F*(4*T)) := by gcongr - _ = 4*F^2 := by field_simp - _ ≤ 4*x := by gcongr - have he2 : a*(20*D*d)*(20*D*d)/F*(F*h^2) ≤ (66*400)*x := by - calc - _ ≤ (66/h^2)*(20*D*d)*(20*D*d)/F*(F*h^2) := by gcongr - _ = _ := by unfold x; field_simp; ring - have HH : h^2 ≤ h^8 := pow_le_pow_right₀ hh (by norm_num) - have he3 : F*h^2 ≤ x := by - have h1 : h^8 ≤ d := le_trans (le_mul_of_one_le_left (by positivity) hD) H - calc - _ ≤ (D*d)*(D*d) := by gcongr; nlinarith - _ = _ := by unfold x; ring - calc - _ = 24*(F/T*(F*h^2))+8*(a*(20*D*d)*(20*D*d)/F*(F*h^2))+2*(F*h^2) := by ring - _ ≤ 300000*x := by linarith - _ = _ := by unfold x; ring - -/-- Compression at the scales in terms of d=2^j. -/ -theorem entropy_compression (D : ℝ) (hD : 1 ≤ D) : - ∀ᶠ j : ℕ in atTop, ∀ (A G Θ : Type) - (_ : Fintype A) (_ : Fintype G) (_ : Fintype Θ) (_ : Nonempty A) - (_ : DecidableEq G) (_ : AddCommGroup G) - (tag : A → G), Injective tag → ∀ (q : Law G), (∀ x, 0 < q x) → - ∀ (c : G → ℝ), (∀ x, 0 ≤ c x) → c 0=0 → - ∀ (Q : Law Θ) (χ : Θ → AddChar G ℂ), - (∀ t x, ‖χ t x‖=1) → - (∀ g, (↑(exp (-c g)):ℂ)=Q.Cx (fun t => χ t g)) → - (∀ g, -log (q g) = -log (q 0) + c g) → - -log (q 0) ≤ D*height j → - (∀ n : ℕ, n ≤ 2^(2*j) → lnS (Fin n) tag ≤ height j * j^2) → - ∀ F : ℝ, height j/(slow j:ℝ)^2 ≤ F → F ≤ D*height j → - (∀ p : Law A, exp (-F) ≤ heatMoment (Fin (sLen j)) p tag c) → - ∃ (a : A) (s : Finset A), s.Nonempty ∧ - log (Fintype.card A)-log (s.card:ℝ) ≤ - 300000*D^2*(height j)^2/(F*(slow j:ℝ)^2) ∧ - small_defects tag χ Q (long j) s a (350*F) := by - filter_upwards [array_spec,big_t_real,below_base 16 (1024*D), - slow_le_j,slow_tendsto.eventually (eventually_ge_atTop 4),t_expr] with j hs ht hb hj h4 hjt - intro A G Θ _ _ _ _ _ _ tag hi q hq c hc hc0 Q χ hn hrq hlog hq0 hv F hF hFd h - let d := height j; let t : ℝ := slow j - have h4' : 4 ≤ t := by unfold t; exact_mod_cast h4 - have htj : t ≤ j := by unfold t; exact_mod_cast hj - have hd : 0 < d := by unfold d height; positivity - let N := sSeg j - have hpN : (0:ℝ) < N := by - unfold N; exact_mod_cast pow_pos_nat (j-6*ell j-2) - obtain ⟨hT,hsm,hmd,hil,horder⟩ := hs - change d=4*N*t^6 ∧ _ at ht - have hb' : (base j:ℝ) ≤ cap j := by exact_mod_cast base_le_cap j - have hj1 : (1:ℝ) ≤ (j:ℝ) := by linarith - have hh : D*t^8 ≤ d := by - have h1 : (cap j:ℝ) ≤ d := by unfold d height; exact_mod_cast hmd - calc - _ ≤ 1024*D*(j:ℝ)^16 := by - have hx : t^8 ≤ (j:ℝ)^16 := le_trans (pow_le_pow_left₀ (by linarith) htj _) (pow_le_pow_right₀ hj1 (by norm_num)) - gcongr; nlinarith - _ ≤ _ := hb.trans (hb'.trans h1) - have h44 : t^2 ≤ d := by - calc - t^2 ≤ t^8 := pow_le_pow_right₀ (by linarith) (by norm_num) - _ ≤ _ := le_trans (le_mul_of_one_le_left (by positivity) hD) hh - have htp : 0 < t := by linarith - have he : 1 ≤ F := le_trans (b := d / t^2) ((le_div_iff₀ (sq_pos_of_pos htp)).mpr (by simpa)) hF - let S := searchSched j h4 - obtain ⟨a,s,h1,h2,h3⟩ := compress_arrays tag hi q hq c hc hc0 Q χ hn hrq hlog - F (long j) (20*D*d) he (by unfold long; positivity) - (sLen j) N (cap j) (pow_pos_nat _) hsm hT S (stepLow j) (stepHigh j) (stepRows j) - (by - intro i _ - have hh := hil i - exact ⟨hh.1,hh.2.1,pow_pos_nat _,pow_pos_nat _,hh.2.2.1.le,hh.2.2.2.1⟩) - (by intro i k hik hk; exact horder i k hik hk) h (by dsimp only [d] at *; linarith) - (fun i _ => pow_pos_nat _) (by - have hn' : N ≤ 2^j := by unfold N sSeg; apply Nat.pow_le_pow_right <;> omega - have hErr := approx_error (by linarith : (1:ℝ) ≤ t) htj hd - (show (0:ℝ) < cap j by exact_mod_cast pow_pos_nat (capE j)) - hD hF hFd ht.1 (hb.trans hb') ht.2 - intro i _ - let l := stepLow j i; let r := stepRows j i - have hlo := hil i - have hjt : 2^j ≤ 2^(2*j) := Nat.pow_le_pow_right (by omega) (by omega) - let ε := 2*(cap j:ℝ)*√F/N - have hε : 0 ≤ ε := by unfold ε; positivity - have hz (k : ℕ) (hh : k ≤ 2^j) : ε * lnS (Fin k) tag ≤ F := - (mul_le_mul_of_nonneg_left (hv k (hh.trans hjt)) hε).trans hErr - have hp : 0 < r := pow_pos_nat _ - have hl : 0 < l := pow_pos_nat _ - have hqr : 2*r*l=N := hlo.2.2.1 - have hiN : r*l ≤ N := by nlinarith - have hiR : r ≤ N := le_trans (Nat.le_mul_of_pos_right r hl) hiN - have HR := hz (r*l) (hiN.trans hn') - refine ⟨hz _ (hlo.2.1.trans hmd),?_⟩ - have H1 := log_ticket r j hp (hiR.trans hn') (by omega) - have hb'' : (base j:ℝ) ≤ l := by exact_mod_cast hlo.2.2.2.2 - have hlB : 4*(j:ℝ)*t^2 ≤ l := by - apply le_trans _ (hb.trans hb'') - calc - _ ≤ 4*((j:ℝ)*(j:ℝ)^2) := by rw [mul_assoc]; gcongr - _ ≤ _ := by - rw [show (j:ℝ)*(j:ℝ)^2=(j:ℝ)^3 by ring] - have HH : (j:ℝ)^3 ≤ (j:ℝ)^16 := pow_le_pow_right₀ hj1 (by norm_num) - gcongr; linarith - have hND : (N:ℝ) ≤ d := by unfold d height; exact_mod_cast hn' - have hRN : (r:ℝ)*l ≤ N := by exact_mod_cast hiN - have hrD : (r:ℝ)*(4*(j:ℝ)) ≤ F := by - apply le_trans _ hF - apply (le_div_iff₀ (by positivity : 0 < t^2)).mpr - rw [mul_assoc] - exact (mul_le_mul_of_nonneg_left hlB (by positivity)).trans (hRN.trans hND) - change log _+(r:ℝ)+ 2 * ε*lnS (Fin (r*l)) tag ≤ _ - nlinarith [show (0:ℝ) ≤ r by positivity]) - exact ⟨a,s,h1,h2.trans - (size_bound (by linarith : (1:ℝ) ≤ t) hd hpN hD (by linarith) hFd ht.1 hh (searchSched_loss _ h4)),h3⟩ -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Theorem +import OAI.Analysis.FourierLLogL.Compress.Params +/-! # The compression lemma, asymptotic scales -/ +open Set Function Filter +open scoped Topology BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Real Law Params + +lemma log_ticket (r j : ℕ) (hr : 1 ≤ r) (h : r ≤ 2^j) (hj : 1 ≤ j) : + log (Fintype.card (Ticket r (2*r)) : ℝ) ≤ 1 + r*(2*(j:ℝ)) := by + have hp : 0 < (r:ℝ) := by exact_mod_cast hr + have hcard : Fintype.card (Fin r ↪ Fin (2*r)) ≤ (2*r)^r := by + have hi := Fintype.card_le_of_injective + (fun (e : Fin r ↪ Fin (2*r)) => (e : Fin r → Fin (2*r))) (by intro a b hab; ext i : 1; exact congrFun hab i) + simpa using hi + have hl2 := log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num) + have hx : log (2*(r:ℝ)) ≤ 2*(j:ℝ) := by + rw [log_mul (by norm_num) hp.ne'] + have h1 := Real.log_le_log hp (show (r:ℝ) ≤ 2^j by exact_mod_cast h) + rw [log_pow] at h1 + have hJ : (1:ℝ) ≤ j := by exact_mod_cast hj + nlinarith + unfold Ticket + rw [Fintype.card_option] + have ht : 0 < ((Fintype.card (Fin r ↪ Fin (2*r)) + 1:ℕ):ℝ) := by positivity + apply le_trans (Real.log_le_log ht (y:=2*(2*(r:ℝ))^r) ?_) ?_ + · have h1 : Fintype.card (Fin r ↪ Fin (2*r)) + 1 ≤ 2*(2*r)^r := by + have hp : 0 < (2*r)^r := pow_pos (by omega) _ + omega + exact_mod_cast h1 + rw [Real.log_mul (by norm_num) (by positivity),Real.log_pow] + nlinarith + +private lemma approx_error {d h F D T m j : ℝ} (hh : 1 ≤ h) (hj : h ≤ j) + (hd : 0 < d) (hm : 0 < m) (hD : 1 ≤ D) + (hF : d/h^2 ≤ F) (hFd : F ≤ D*d) + (Ht : d=4*T*h^6) + (H : 1024*D*j^16 ≤ m) (H' : m^4 ≤ d) : + (2*m*√F/T)*(d*j^2) ≤ F := by + have hh' : 0 < h := by linarith + have hF' : 0 < F := lt_of_lt_of_le (by positivity) hF + have hp : 0 < T := pos_of_mul_pos_left (show 0 < T*(4*h^6) by + calc 0 < d := hd + _ = T*(4*h^6) := by rw [Ht]; ring) (by positivity) + have hw : 8*D*(j^2*h^8) ≤ m := by + apply le_trans _ H + have hj1 : 1 ≤ j := hh.trans hj + calc + _ ≤ 8*D*(j^2*j^8) := by gcongr + _ ≤ 1024*D*j^16 := by + have HH : j^10 ≤ j^16 := pow_le_pow_right₀ hj1 (by omega) + rw [← pow_add]; gcongr; norm_num + have hsr : √F ≤ D*√d := by + have Hx := sq_sqrt hF'.le + have Hy := sq_sqrt hd.le + have Hh : D*d ≤ (D*√d)^2 := by + rw [mul_pow,Hy]; gcongr; nlinarith + nlinarith [hFd.trans Hh,sqrt_nonneg F, show 0 ≤ D*√d by positivity] + have hQ : m^2 ≤ √d := by + have Ht := sq_sqrt hd.le + nlinarith [show m^4=(m^2)^2 by ring,sqrt_nonneg d,sq_nonneg (m^2-√d)] + have hz : 8*m*√F*j^2*h^8 ≤ d := by + calc + _ ≤ m * √d * (8*D*(j^2*h^8)) := by + calc + _ ≤ 8*m*(D*√d)*j^2*h^8 := by gcongr + _ = _ := by ring + _ ≤ m*√d*m := mul_le_mul_of_nonneg_left hw (by positivity) + _ = m^2*√d := by ring + _ ≤ √d*√d := mul_le_mul_of_nonneg_right hQ (sqrt_nonneg _) + _ = d := by rw [← sq,sq_sqrt hd.le] + apply le_trans (b:=d/h^2) _ hF + apply (le_div_iff₀ (pow_pos hh' 2)).mpr + + have hle : 2*m*√F*j^2*h^2 ≤ T := by + rw [Ht] at hz + refine le_of_mul_le_mul_right ?_ (show 0 < 4*h^6 by positivity) + calc + _ = 8*m*√F*j^2*h^8 := by ring + _ ≤ 4*T*h^6 := hz + _ = T*(4*h^6) := by ring + calc + _ = (2*m*√F*j^2*h^2)/T*d := by ring + _ ≤ 1*d := mul_le_mul_of_nonneg_right ((div_le_one hp).mpr hle) hd.le + _ = d := one_mul d + +private lemma size_bound {d h F D T a : ℝ} (hh : 1 ≤ h) (hd : 0 < d) (hT : 0 < T) + (hD : 1 ≤ D) (hp : 0 < F) (hF : F ≤ D*d) + (Ht : d=4*T*h^6) + (H : D*h^8 ≤ d) (ha : a ≤ 66/h^2) : + 8*(3*(F/T)+a*(20*D*d)*(20*D*d)/F)+2 ≤ 300000*D^2*d^2/(F*h^2) := by + have hh' : 0 < h := by linarith + have hc : 0 < 20*D*d := by positivity + apply (le_div_iff₀ (by positivity : 0 < F*h^2)).mpr + let x := D^2*d^2 + have hx : 0 ≤ x := by unfold x; positivity + have hF1 : F^2 ≤ x := by unfold x; rw [← mul_pow]; gcongr + have hT1 : h^2 ≤ 4*T := by + have H1 : h^8 ≤ d := le_trans (le_mul_of_one_le_left (by positivity) hD) H + rw [Ht,show h^8=h^2*h^6 by ring] at H1 + exact le_of_mul_le_mul_right H1 (by positivity) + have he1 : F/T*(F*h^2) ≤ 4*x := calc + _ ≤ F/T*(F*(4*T)) := by gcongr + _ = 4*F^2 := by field_simp + _ ≤ 4*x := by gcongr + have he2 : a*(20*D*d)*(20*D*d)/F*(F*h^2) ≤ (66*400)*x := by + calc + _ ≤ (66/h^2)*(20*D*d)*(20*D*d)/F*(F*h^2) := by gcongr + _ = _ := by unfold x; field_simp; ring + have HH : h^2 ≤ h^8 := pow_le_pow_right₀ hh (by norm_num) + have he3 : F*h^2 ≤ x := by + have h1 : h^8 ≤ d := le_trans (le_mul_of_one_le_left (by positivity) hD) H + calc + _ ≤ (D*d)*(D*d) := by gcongr; nlinarith + _ = _ := by unfold x; ring + calc + _ = 24*(F/T*(F*h^2))+8*(a*(20*D*d)*(20*D*d)/F*(F*h^2))+2*(F*h^2) := by ring + _ ≤ 300000*x := by linarith + _ = _ := by unfold x; ring + +/-- Compression at the scales in terms of d=2^j. -/ +theorem entropy_compression (D : ℝ) (hD : 1 ≤ D) : + ∀ᶠ j : ℕ in atTop, ∀ (A G Θ : Type) + (_ : Fintype A) (_ : Fintype G) (_ : Fintype Θ) (_ : Nonempty A) + (_ : DecidableEq G) (_ : AddCommGroup G) + (tag : A → G), Injective tag → ∀ (q : Law G), (∀ x, 0 < q x) → + ∀ (c : G → ℝ), (∀ x, 0 ≤ c x) → c 0=0 → + ∀ (Q : Law Θ) (χ : Θ → AddChar G ℂ), + (∀ t x, ‖χ t x‖=1) → + (∀ g, (↑(exp (-c g)):ℂ)=Q.Cx (fun t => χ t g)) → + (∀ g, -log (q g) = -log (q 0) + c g) → + -log (q 0) ≤ D*height j → + (∀ n : ℕ, n ≤ 2^(2*j) → lnS (Fin n) tag ≤ height j * j^2) → + ∀ F : ℝ, height j/(slow j:ℝ)^2 ≤ F → F ≤ D*height j → + (∀ p : Law A, exp (-F) ≤ heatMoment (Fin (sLen j)) p tag c) → + ∃ (a : A) (s : Finset A), s.Nonempty ∧ + log (Fintype.card A)-log (s.card:ℝ) ≤ + 300000*D^2*(height j)^2/(F*(slow j:ℝ)^2) ∧ + small_defects tag χ Q (long j) s a (350*F) := by + filter_upwards [array_spec,big_t_real,below_base 16 (1024*D), + slow_le_j,slow_tendsto.eventually (eventually_ge_atTop 4),t_expr] with j hs ht hb hj h4 hjt + intro A G Θ _ _ _ _ _ _ tag hi q hq c hc hc0 Q χ hn hrq hlog hq0 hv F hF hFd h + let d := height j; let t : ℝ := slow j + have h4' : 4 ≤ t := by unfold t; exact_mod_cast h4 + have htj : t ≤ j := by unfold t; exact_mod_cast hj + have hd : 0 < d := by unfold d height; positivity + let N := sSeg j + have hpN : (0:ℝ) < N := by + unfold N; exact_mod_cast pow_pos_nat (j-6*ell j-2) + obtain ⟨hT,hsm,hmd,hil,horder⟩ := hs + change d=4*N*t^6 ∧ _ at ht + have hb' : (base j:ℝ) ≤ cap j := by exact_mod_cast base_le_cap j + have hj1 : (1:ℝ) ≤ (j:ℝ) := by linarith + have hh : D*t^8 ≤ d := by + have h1 : (cap j:ℝ) ≤ d := by unfold d height; exact_mod_cast hmd + calc + _ ≤ 1024*D*(j:ℝ)^16 := by + have hx : t^8 ≤ (j:ℝ)^16 := le_trans (pow_le_pow_left₀ (by linarith) htj _) (pow_le_pow_right₀ hj1 (by norm_num)) + gcongr; nlinarith + _ ≤ _ := hb.trans (hb'.trans h1) + have h44 : t^2 ≤ d := by + calc + t^2 ≤ t^8 := pow_le_pow_right₀ (by linarith) (by norm_num) + _ ≤ _ := le_trans (le_mul_of_one_le_left (by positivity) hD) hh + have htp : 0 < t := by linarith + have he : 1 ≤ F := le_trans (b := d / t^2) ((le_div_iff₀ (sq_pos_of_pos htp)).mpr (by simpa)) hF + let S := searchSched j h4 + obtain ⟨a,s,h1,h2,h3⟩ := compress_arrays tag hi q hq c hc hc0 Q χ hn hrq hlog + F (long j) (20*D*d) he (by unfold long; positivity) + (sLen j) N (cap j) (pow_pos_nat _) hsm hT S (stepLow j) (stepHigh j) (stepRows j) + (by + intro i _ + have hh := hil i + exact ⟨hh.1,hh.2.1,pow_pos_nat _,pow_pos_nat _,hh.2.2.1.le,hh.2.2.2.1⟩) + (by intro i k hik hk; exact horder i k hik hk) h (by dsimp only [d] at *; linarith) + (fun i _ => pow_pos_nat _) (by + have hn' : N ≤ 2^j := by unfold N sSeg; apply Nat.pow_le_pow_right <;> omega + have hErr := approx_error (by linarith : (1:ℝ) ≤ t) htj hd + (show (0:ℝ) < cap j by exact_mod_cast pow_pos_nat (capE j)) + hD hF hFd ht.1 (hb.trans hb') ht.2 + intro i _ + let l := stepLow j i; let r := stepRows j i + have hlo := hil i + have hjt : 2^j ≤ 2^(2*j) := Nat.pow_le_pow_right (by omega) (by omega) + let ε := 2*(cap j:ℝ)*√F/N + have hε : 0 ≤ ε := by unfold ε; positivity + have hz (k : ℕ) (hh : k ≤ 2^j) : ε * lnS (Fin k) tag ≤ F := + (mul_le_mul_of_nonneg_left (hv k (hh.trans hjt)) hε).trans hErr + have hp : 0 < r := pow_pos_nat _ + have hl : 0 < l := pow_pos_nat _ + have hqr : 2*r*l=N := hlo.2.2.1 + have hiN : r*l ≤ N := by nlinarith + have hiR : r ≤ N := le_trans (Nat.le_mul_of_pos_right r hl) hiN + have HR := hz (r*l) (hiN.trans hn') + refine ⟨hz _ (hlo.2.1.trans hmd),?_⟩ + have H1 := log_ticket r j hp (hiR.trans hn') (by omega) + have hb'' : (base j:ℝ) ≤ l := by exact_mod_cast hlo.2.2.2.2 + have hlB : 4*(j:ℝ)*t^2 ≤ l := by + apply le_trans _ (hb.trans hb'') + calc + _ ≤ 4*((j:ℝ)*(j:ℝ)^2) := by rw [mul_assoc]; gcongr + _ ≤ _ := by + rw [show (j:ℝ)*(j:ℝ)^2=(j:ℝ)^3 by ring] + have HH : (j:ℝ)^3 ≤ (j:ℝ)^16 := pow_le_pow_right₀ hj1 (by norm_num) + gcongr; linarith + have hND : (N:ℝ) ≤ d := by unfold d height; exact_mod_cast hn' + have hRN : (r:ℝ)*l ≤ N := by exact_mod_cast hiN + have hrD : (r:ℝ)*(4*(j:ℝ)) ≤ F := by + apply le_trans _ hF + apply (le_div_iff₀ (by positivity : 0 < t^2)).mpr + rw [mul_assoc] + exact (mul_le_mul_of_nonneg_left hlB (by positivity)).trans (hRN.trans hND) + change log _+(r:ℝ)+ 2 * ε*lnS (Fin (r*l)) tag ≤ _ + nlinarith [show (0:ℝ) ≤ r by positivity]) + exact ⟨a,s,h1,h2.trans + (size_bound (by linarith : (1:ℝ) ≤ t) hd hpN hD (by linarith) hFd ht.1 hh (searchSched_loss _ h4)),h3⟩ +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Fraction.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Fraction.lean index 1a8f234c4edd63c6bdc9e52f52edf9525ba43127..2bb3e36b219b0ff1a223159b18f709406dd41cbb 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Fraction.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Fraction.lean @@ -1,170 +1,170 @@ -import OAI.Analysis.FourierLLogL.Compress.Observe -/-! # Partial refinements -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {V W Y Ω G A : Type} [Fintype A] [Fintype V] [Fintype W] [Fintype G] - [Fintype Y] [Fintype Ω] [AddCommGroup G] - -lemma mix_send (P Q : Law Ω) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) - (X : Ω → A) : - (mixture P Q t h0 h1).send X = mixture (P.send X) (Q.send X) t h0 h1 := by - apply ext_of_Ex; intro f - simp_rw [mixture_Ex,Ex_send,mixture_Ex] - -lemma marked_cond (P Q : Law Ω) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) - (X : Ω → A) (W : Ω → V) (f : V → Bool) - (hP : ∀ x, 0 < P x → f (W x) = true) - (hQ : ∀ x, 0 < Q x → f (W x) = false) : - (mixture P Q t h0 h1).Hcond X W = - t*P.Hcond X W+(1-t)*Q.Hcond X W := by - rw [mixture_eq_through] - let B := coinP t h0 h1 - let K (b : Bool) := if b then P else Q - let R := withNext B K - change (through B K).Hcond X W = _ - have hy (z : Bool × Ω) (hz : 0 < R z) : f (W z.2) = z.1 := by - obtain ⟨b,x⟩ := z - change 0 < B b * K b x at hz - have hx : 0 < K b x := pos_of_mul_pos_right hz (B.nonneg b) - cases b - · exact hQ x hx - · exact hP x hx - let X' (z : Bool×Ω) := X z.2 - let W' (z : Bool×Ω) := W z.2 - rw [← send_snd_withNext B K,Hcond_send] - have HH : R.Hcond X' W' = R.Hcond X' (fun z => (z.1, W' z)) := by - calc - _ = R.Hcond X' (fun z => (f (W' z),W' z)) := - Hcond_right_partition _ _ (fun _ _ => by simp; grind) - _ = _ := Hcond_congr (X:=X') (fun _ _ => rfl) - (W:=fun z => (f (W' z),W' z)) fun z hz => by rw [show _=z.1 from hy z hz] - have H := - Hcond_withNext B K (fun _ => X) (fun _ => W) - change R.Hcond X' W' = _ - rw [HH,show R.Hcond X' _ = _ from H,coinP_Ex] - rfl - -def retag (f : V → W) (x : A×V) : A×W := (x.1,f x.2) - -omit [Fintype A] [Fintype V] [Fintype W] in -lemma retag_inj (f : V → W) (hf : Injective f) : - Injective (retag (A:=A) f) := by - rintro ⟨x,a⟩ ⟨y,b⟩ h - simpa only [retag,Prod.mk.injEq,hf.eq_iff] using h - -abbrev blend (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Labelled A := - { obs := P.obs ⊕ Q.obs - law := mixture (P.law.send (retag Sum.inl)) (Q.law.send (retag Sum.inr)) t h0 h1 } - -lemma blend_refines (P Q R : Labelled A) (hp : refines P R) (hq : refines Q R) - (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : refines (blend P Q t h0 h1) R := by - obtain ⟨u,hu⟩ := hp; obtain ⟨v,hv⟩ := hq - refine ⟨Sum.elim u v,?_⟩ - dsimp only [blend] - rw [mix_send,send_send,send_send] - apply Law.ext; intro a - rw [mixture_apply] - dsimp only [retag,Sum.elim_inl,Sum.elim_inr] - rw [hu,hv]; ring - -lemma retag_Hcond (P : Law (A×V)) (f : V→W) (h : Injective f) : - (P.send (retag f)).Hcond (fun x => x) Prod.snd = P.Hcond (fun x => x) Prod.snd := by - rw [full_pair,full_pair,Hcond_send] - apply Hcond_right_partition - exact fun _ _ => h.eq_iff - -lemma blend_H (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - (blend P Q t h0 h1).law.Hcond (fun a => a) Prod.snd = - t*P.law.Hcond (fun a => a) Prod.snd+(1-t)*Q.law.Hcond (fun a => a) Prod.snd := by - dsimp only [blend] - let K := Sum.elim (fun _ : P.obs => true) (fun _ : Q.obs => false) - rw [marked_cond _ _ t h0 h1 _ _ K, retag_Hcond _ (Sum.inl : P.obs → P.obs ⊕ Q.obs) - Sum.inl_injective,retag_Hcond _ (Sum.inr : Q.obs → P.obs ⊕ Q.obs) Sum.inr_injective] - all_goals - intro x hx - obtain ⟨y,rfl,_⟩ := (send_pos_iff _ _ _).mp hx - rfl - - -def upRows (n : ℕ) (f : V→W) (x : WordSample n (A×V) Unit) : - WordSample n (A×W) Unit := (ctxt (retag f) Prod.fst x, x.2) - -def packedH (n : ℕ) (tag : A→G) (W : Type) [Fintype W] (x : WordSample n (A×W) Unit) := - (ans (tag ∘ Prod.fst) Prod.fst (fun _ => 0) x,ctxt Prod.snd Prod.fst x) - -lemma retag_trial (n : ℕ) (tag : A → G) (P : Law (WordSample n (A×V) Unit)) - (f : V → W) (h : Injective f) : - (P.send (upRows n f)).Hcond Prod.fst (packedH n tag W) = - P.Hcond Prod.fst (packedH n tag V) := by - rw [Hcond_send] - have hc (x y : WordSample n (A×V) Unit) : - packedH n tag W (upRows n f x) = packedH n tag W (upRows n f y) ↔ - packedH n tag V x = packedH n tag V y := by - simp only [packedH,upRows,ctxt,ans,tot,comp_apply,retag, Prod.mk.injEq,funext_iff,h.eq_iff] - unfold Hcond - congr 1 - · refine H_partition _ fun x y => ?_ - have heq : (upRows n f x).1=(upRows n f y).1 ↔ x.1=y.1 := by - simp only [funext_iff,upRows,ctxt,(retag_inj f h).eq_iff] - rw [show (Prod.fst x) = x.1 from rfl] - constructor - · intro hh - replace hh := Prod.mk.inj hh - exact Prod.ext (heq.mp hh.1) ((hc x y).mp hh.2) - · intro hh - replace hh := Prod.mk.inj hh - exact Prod.ext (heq.mpr hh.1) ((hc x y).mpr hh.2) - · exact H_partition _ hc - -lemma retag_trial_rows (n : ℕ) (p : Law (A × V)) (P : Law (WordSample n (A × V) Unit)) - (h : hasRows p P Prod.fst) (f : V → W) : - hasRows (p.send (retag f)) (P.send (upRows n f)) Prod.fst := by - intro i; rw [← h i,send_send,send_send]; rfl - -lemma exact_rent (n : ℕ) (S : Labelled A) (tag : A → G) - (P : Law (WordSample n (A×S.obs) Unit)) (h : hasRows S.law P Prod.fst) : - rent (tag ∘ Prod.fst) Prod.snd (fun _ : Unit => (0:G)) (fun _ => 0) P Prod.fst Prod.snd = - (n:ℝ) * S.law.Hcond (fun a => a) Prod.snd - P.Hcond Prod.fst (packedH n tag _) := by - rw [rent,rent_first h, Fintype.card_fin] - rw [Ex_const,add_zero]; rfl - -lemma blend_I (tag : A → G) (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) - (n : ℕ) : fundI tag (blend P Q t h0 h1) n ≤ t*fundI tag P n+(1-t)*fundI tag Q n := by - let u0 (_ : Unit) := (0:G) - let c0 (_ : Unit) := (0:ℝ) - obtain ⟨R,hR,eR⟩ := (opt_spec n P.law (tag ∘ Prod.fst) Prod.snd u0 c0).1 - obtain ⟨S,hS,eS⟩ := (opt_spec n Q.law (tag ∘ Prod.fst) Prod.snd u0 c0).1 - let B := blend P Q t h0 h1 - let p := (Sum.inl : P.obs → P.obs ⊕ Q.obs) - let q := (Sum.inr : Q.obs → P.obs ⊕ Q.obs) - let T := mixture (R.send (upRows n p)) (S.send (upRows n q)) t h0 h1 - have hT : hasRows (blend P Q t h0 h1).law T Prod.fst := by - intro i - unfold T - rw [mix_send, retag_trial_rows n _ R hR p i, retag_trial_rows n _ S hS q i] - apply le_trans (opt_le_rent (tag ∘ Prod.fst) Prod.snd u0 c0 _ T _ Prod.snd hT) - change _ ≤ t*opt n P.law _ _ u0 c0+(1-t)*opt n Q.law _ _ u0 c0 - rw [← eR,← eS] - dsimp only [u0,c0] - rw [exact_rent n (blend P Q t h0 h1) tag T hT, exact_rent n P tag R hR,exact_rent n Q tag S hS] - have hp := Hcond_mixture_ge (R.send (upRows n p)) (S.send (upRows n q)) t h0 h1 - Prod.fst (packedH n tag _) - rw [retag_trial n tag R p Sum.inl_injective, retag_trial n tag S q Sum.inr_injective] at hp - rw [blend_H] - unfold T - linarith - -lemma blend_G1 (tag : A→G) (hi : Injective tag) (P Q : Labelled A) - (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - fundG tag (blend P Q t h0 h1) 1 = t*fundG tag P 1+(1-t)*fundG tag Q 1 := by - rw [I_single tag hi, I_single tag hi, I_single tag hi]; exact blend_H P Q t h0 h1 - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Observe +/-! # Partial refinements -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {V W Y Ω G A : Type} [Fintype A] [Fintype V] [Fintype W] [Fintype G] + [Fintype Y] [Fintype Ω] [AddCommGroup G] + +lemma mix_send (P Q : Law Ω) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) + (X : Ω → A) : + (mixture P Q t h0 h1).send X = mixture (P.send X) (Q.send X) t h0 h1 := by + apply ext_of_Ex; intro f + simp_rw [mixture_Ex,Ex_send,mixture_Ex] + +lemma marked_cond (P Q : Law Ω) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) + (X : Ω → A) (W : Ω → V) (f : V → Bool) + (hP : ∀ x, 0 < P x → f (W x) = true) + (hQ : ∀ x, 0 < Q x → f (W x) = false) : + (mixture P Q t h0 h1).Hcond X W = + t*P.Hcond X W+(1-t)*Q.Hcond X W := by + rw [mixture_eq_through] + let B := coinP t h0 h1 + let K (b : Bool) := if b then P else Q + let R := withNext B K + change (through B K).Hcond X W = _ + have hy (z : Bool × Ω) (hz : 0 < R z) : f (W z.2) = z.1 := by + obtain ⟨b,x⟩ := z + change 0 < B b * K b x at hz + have hx : 0 < K b x := pos_of_mul_pos_right hz (B.nonneg b) + cases b + · exact hQ x hx + · exact hP x hx + let X' (z : Bool×Ω) := X z.2 + let W' (z : Bool×Ω) := W z.2 + rw [← send_snd_withNext B K,Hcond_send] + have HH : R.Hcond X' W' = R.Hcond X' (fun z => (z.1, W' z)) := by + calc + _ = R.Hcond X' (fun z => (f (W' z),W' z)) := + Hcond_right_partition _ _ (fun _ _ => by simp; grind) + _ = _ := Hcond_congr (X:=X') (fun _ _ => rfl) + (W:=fun z => (f (W' z),W' z)) fun z hz => by rw [show _=z.1 from hy z hz] + have H := + Hcond_withNext B K (fun _ => X) (fun _ => W) + change R.Hcond X' W' = _ + rw [HH,show R.Hcond X' _ = _ from H,coinP_Ex] + rfl + +def retag (f : V → W) (x : A×V) : A×W := (x.1,f x.2) + +omit [Fintype A] [Fintype V] [Fintype W] in +lemma retag_inj (f : V → W) (hf : Injective f) : + Injective (retag (A:=A) f) := by + rintro ⟨x,a⟩ ⟨y,b⟩ h + simpa only [retag,Prod.mk.injEq,hf.eq_iff] using h + +abbrev blend (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Labelled A := + { obs := P.obs ⊕ Q.obs + law := mixture (P.law.send (retag Sum.inl)) (Q.law.send (retag Sum.inr)) t h0 h1 } + +lemma blend_refines (P Q R : Labelled A) (hp : refines P R) (hq : refines Q R) + (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : refines (blend P Q t h0 h1) R := by + obtain ⟨u,hu⟩ := hp; obtain ⟨v,hv⟩ := hq + refine ⟨Sum.elim u v,?_⟩ + dsimp only [blend] + rw [mix_send,send_send,send_send] + apply Law.ext; intro a + rw [mixture_apply] + dsimp only [retag,Sum.elim_inl,Sum.elim_inr] + rw [hu,hv]; ring + +lemma retag_Hcond (P : Law (A×V)) (f : V→W) (h : Injective f) : + (P.send (retag f)).Hcond (fun x => x) Prod.snd = P.Hcond (fun x => x) Prod.snd := by + rw [full_pair,full_pair,Hcond_send] + apply Hcond_right_partition + exact fun _ _ => h.eq_iff + +lemma blend_H (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + (blend P Q t h0 h1).law.Hcond (fun a => a) Prod.snd = + t*P.law.Hcond (fun a => a) Prod.snd+(1-t)*Q.law.Hcond (fun a => a) Prod.snd := by + dsimp only [blend] + let K := Sum.elim (fun _ : P.obs => true) (fun _ : Q.obs => false) + rw [marked_cond _ _ t h0 h1 _ _ K, retag_Hcond _ (Sum.inl : P.obs → P.obs ⊕ Q.obs) + Sum.inl_injective,retag_Hcond _ (Sum.inr : Q.obs → P.obs ⊕ Q.obs) Sum.inr_injective] + all_goals + intro x hx + obtain ⟨y,rfl,_⟩ := (send_pos_iff _ _ _).mp hx + rfl + + +def upRows (n : ℕ) (f : V→W) (x : WordSample n (A×V) Unit) : + WordSample n (A×W) Unit := (ctxt (retag f) Prod.fst x, x.2) + +def packedH (n : ℕ) (tag : A→G) (W : Type) [Fintype W] (x : WordSample n (A×W) Unit) := + (ans (tag ∘ Prod.fst) Prod.fst (fun _ => 0) x,ctxt Prod.snd Prod.fst x) + +lemma retag_trial (n : ℕ) (tag : A → G) (P : Law (WordSample n (A×V) Unit)) + (f : V → W) (h : Injective f) : + (P.send (upRows n f)).Hcond Prod.fst (packedH n tag W) = + P.Hcond Prod.fst (packedH n tag V) := by + rw [Hcond_send] + have hc (x y : WordSample n (A×V) Unit) : + packedH n tag W (upRows n f x) = packedH n tag W (upRows n f y) ↔ + packedH n tag V x = packedH n tag V y := by + simp only [packedH,upRows,ctxt,ans,tot,comp_apply,retag, Prod.mk.injEq,funext_iff,h.eq_iff] + unfold Hcond + congr 1 + · refine H_partition _ fun x y => ?_ + have heq : (upRows n f x).1=(upRows n f y).1 ↔ x.1=y.1 := by + simp only [funext_iff,upRows,ctxt,(retag_inj f h).eq_iff] + rw [show (Prod.fst x) = x.1 from rfl] + constructor + · intro hh + replace hh := Prod.mk.inj hh + exact Prod.ext (heq.mp hh.1) ((hc x y).mp hh.2) + · intro hh + replace hh := Prod.mk.inj hh + exact Prod.ext (heq.mpr hh.1) ((hc x y).mpr hh.2) + · exact H_partition _ hc + +lemma retag_trial_rows (n : ℕ) (p : Law (A × V)) (P : Law (WordSample n (A × V) Unit)) + (h : hasRows p P Prod.fst) (f : V → W) : + hasRows (p.send (retag f)) (P.send (upRows n f)) Prod.fst := by + intro i; rw [← h i,send_send,send_send]; rfl + +lemma exact_rent (n : ℕ) (S : Labelled A) (tag : A → G) + (P : Law (WordSample n (A×S.obs) Unit)) (h : hasRows S.law P Prod.fst) : + rent (tag ∘ Prod.fst) Prod.snd (fun _ : Unit => (0:G)) (fun _ => 0) P Prod.fst Prod.snd = + (n:ℝ) * S.law.Hcond (fun a => a) Prod.snd - P.Hcond Prod.fst (packedH n tag _) := by + rw [rent,rent_first h, Fintype.card_fin] + rw [Ex_const,add_zero]; rfl + +lemma blend_I (tag : A → G) (P Q : Labelled A) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) + (n : ℕ) : fundI tag (blend P Q t h0 h1) n ≤ t*fundI tag P n+(1-t)*fundI tag Q n := by + let u0 (_ : Unit) := (0:G) + let c0 (_ : Unit) := (0:ℝ) + obtain ⟨R,hR,eR⟩ := (opt_spec n P.law (tag ∘ Prod.fst) Prod.snd u0 c0).1 + obtain ⟨S,hS,eS⟩ := (opt_spec n Q.law (tag ∘ Prod.fst) Prod.snd u0 c0).1 + let B := blend P Q t h0 h1 + let p := (Sum.inl : P.obs → P.obs ⊕ Q.obs) + let q := (Sum.inr : Q.obs → P.obs ⊕ Q.obs) + let T := mixture (R.send (upRows n p)) (S.send (upRows n q)) t h0 h1 + have hT : hasRows (blend P Q t h0 h1).law T Prod.fst := by + intro i + unfold T + rw [mix_send, retag_trial_rows n _ R hR p i, retag_trial_rows n _ S hS q i] + apply le_trans (opt_le_rent (tag ∘ Prod.fst) Prod.snd u0 c0 _ T _ Prod.snd hT) + change _ ≤ t*opt n P.law _ _ u0 c0+(1-t)*opt n Q.law _ _ u0 c0 + rw [← eR,← eS] + dsimp only [u0,c0] + rw [exact_rent n (blend P Q t h0 h1) tag T hT, exact_rent n P tag R hR,exact_rent n Q tag S hS] + have hp := Hcond_mixture_ge (R.send (upRows n p)) (S.send (upRows n q)) t h0 h1 + Prod.fst (packedH n tag _) + rw [retag_trial n tag R p Sum.inl_injective, retag_trial n tag S q Sum.inr_injective] at hp + rw [blend_H] + unfold T + linarith + +lemma blend_G1 (tag : A→G) (hi : Injective tag) (P Q : Labelled A) + (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + fundG tag (blend P Q t h0 h1) 1 = t*fundG tag P 1+(1-t)*fundG tag Q 1 := by + rw [I_single tag hi, I_single tag hi, I_single tag hi]; exact blend_H P Q t h0 h1 + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Freeze.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Freeze.lean index 40cff26fdfdf36a18a9762d78a138d420f4ab125..9c1ceb0736d3919435174ab77d97f4740429edb1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Freeze.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Freeze.lean @@ -1,216 +1,216 @@ -import OAI.Analysis.FourierLLogL.Compress.Context - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A G V B Ω I : Type} [Fintype A] [Fintype G] [AddCommGroup G] - [Fintype V] [Fintype Ω] [Fintype B] [Fintype I] [DecidableEq I] -set_option linter.unusedSectionVars false - -lemma average_two_below (P : Law Ω) (f g : Ω → ℝ) - (h0 : ∀ x, 0 < P x → 0 ≤ f x) (h1 : ∀ x, 0 < P x → 0 ≤ g x) - (F G : ℝ) (hp : 0 < F) (hq : 0 < G) (hf : P.Ex f ≤ F) (hg : P.Ex g ≤ G) : - ∃ x, 0 < P x ∧ f x ≤ 2*F ∧ g x ≤ 2*G := by - obtain ⟨x,hx,hh⟩ := P.exists_le_Ex (fun x => F*g x + G*f x) - rw [Ex_add,Ex_lmul,Ex_lmul] at hh - have H : F*g x + G*f x ≤ F*G+G*F := le_trans hh (by gcongr) - exact ⟨x,hx,by nlinarith [h1 _ hx],by nlinarith [h0 _ hx]⟩ - -/-- A small noisy cost implies a large heat sum (Gibbs comparison). -/ -lemma large_heat (P : Law Ω) (q : Law A) (X : Ω → A) (tag : A → G) (U : Ω → G) (c : G → ℝ) - (hcov : (P.send X).dominated q) (F : ℝ) - (hF : P.Ex (fun x => -log (q (X x))) - P.Hcond X (fun x => tag (X x)+U x)+ - P.Ex (fun x => c (U x)) ≤ F) : - ∃ z : G, exp (-F) ≤ q.Ex fun a => exp (-c (z-tag a)) := by - let Z (a : Ω) := tag (X a)+U a - let V := P.send Z - let p z := P.after Z z - let g (z : G) := - (p z).Ex (fun x => -log (q (X x))) - (p z).H X + (p z).Ex (fun x => c (U x)) - have hh : V.Ex g ≤ F := by - have H (f : Ω → ℝ) : - V.Ex (fun z => (p z).Ex f) = P.Ex f := after_Ex_sum P Z (fun _ => f) - unfold g; rw [Ex_add,Ex_sub,H,H] - change _ - (P.send Z).Ex (fun z => (P.after Z z).H X)+_≤ _ - rw [← Hcond_after]; exact hF - obtain ⟨z,hz,hze⟩ := V.exists_le_Ex g - let f a := q a* exp (-c (z-tag a)) - let D := ∑ a, f a - have hf (a) : 0 ≤ f a := mul_nonneg (q.nonneg _) (exp_nonneg _) - have hc (a) (ha : 0 < p z a) : 0 < q (X a) := by - have HH := (after_mem (P:=P) (W:=Z) hz).mp ha - exact hcov _ (P.send_pos X HH.2) - have ht : 0 < D := by - obtain ⟨x,hx⟩ := q.mem_nonempty - have h : 0 < f x := mul_pos hx (exp_pos _) - exact lt_of_lt_of_le h (Finset.single_le_sum (s:=Finset.univ) (fun a _ => hf a) (Finset.mem_univ _)) - let Q := proportional f hf ht - have he (x) (hx : 0 < p z x) : - -log (Q (X x)) = -log (q (X x))+c (U x)+log D := by - have hx' := (after_mem (P:=P) (W:=Z) hz).mp hx - have h : z-tag (X x)=U x := by rw [← hx'.1]; unfold Z; abel - change -log (f _/D) = _ - rw [log_div (mul_pos (hc _ hx) (exp_pos _)).ne' ht.ne'] - rw [log_mul (hc _ hx).ne' (exp_pos _).ne',h,log_exp] - ring - have hd : ((p z).send X).dominated Q := by - intro a ha - obtain ⟨y,rfl,hy⟩ := (send_pos_iff _ _ _).mp ha - exact div_pos (mul_pos (hc y hy) (exp_pos _)) ht - have HC : (p z).H X ≤ (p z).Ex (fun x => -log (Q (X x))) := by - rw [← Ex_send (p z) X (fun x => -log (Q x))] - exact sub_nonneg.mp (kl_nonneg hd) - have hlin := ((p z).Ex_congr_mem he) - rw [hlin,Ex_add,Ex_add,Ex_const] at HC - have h1 : -F ≤ log D := by - have hi := hze.trans hh - dsimp only [g] at hi; linarith - refine ⟨z,le_trans (Real.exp_le_exp.mpr h1) (Real.exp_log ht).le⟩ - -/-- Stack selection with one coordinate per actual letter. -/ -lemma stack_select (tag : A → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) - (p : Law (A×V)) (P : Law Ω) (X : Ω → I → A) (W : Ω → I → V) (U : Ω → G) - (hp : ∀ i : I, P.send (fun x => (X x i,W x i)) = p) - (F D : ℝ) (hF : 0 < F) (hD : 0 < D) - [Nonempty I] - (hs : Fintype.card I * p.Hcond Prod.fst Prod.snd - - P.Hcond X (fun x => (ans tag X U x, W x)) + P.Ex (fun x => c (U x)) ≤ F) - (hd : log (Fintype.card A) - p.Hcond Prod.fst Prod.snd ≤ D) : - ∃ (q : I → Law A) (z : G), - exp (-(2*F)) ≤ (pack q).Ex (fun x => exp (-c (z-tot tag x))) ∧ - ∑ i, (log (Fintype.card A)- (q i).entropy) ≤ 2*(Fintype.card I*D) := by - classical - let R w := P.after W w - let k v := (p.after Prod.snd v).send Prod.fst - let q (w : I → V) (i : I) : Law A := k (w i) - let Z := ans tag X U - let Y := P.send W - have hval (a : Ω) (ha : 0 < P a) (i : I) : 0 < q (W a) i (X a i) := by - have H0 := P.send_pos (fun a => (X a i,W a i)) ha - rw [hp i] at H0 - exact (p.after Prod.snd (W a i)).send_pos Prod.fst (p.after_self_pos _ H0) - have hdom (w : I → V) (hw : 0 < Y w) : - ((R w).send X).dominated (pack (q w)) := by - intro x hx - obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx - have H := (after_mem hw).mp ha - rw [pack_pos,← H.1] - exact hval a H.2 - let N := Fintype.card I - let a (w : I → V) := - (R w).Ex (fun x => -log (pack (q w) (X x))) - - (R w).Hcond X Z + (R w).Ex fun x => c (U x) - let b (w : I → V) := ∑ i, (log (Fintype.card A) - entropy (q w i)) - have ha (w) (hw : 0 < Y w) : 0 ≤ a w := by - have hh := kl_nonneg (hdom w hw) - unfold kl crossE at hh - rw [Ex_send] at hh - have ht := Hcond_le_H (R w) X Z - unfold H at ht - exact add_nonneg (by linarith) (Ex_nonneg (R w) (fun x => hc _)) - have hb (w : I → V) : 0 ≤ b w := - Finset.sum_nonneg fun i _ => sub_nonneg.mpr (entropy_le_log_card _) - have hrow (i : I) (f : (A×V) → ℝ) : - P.Ex (fun x => f (X x i,W x i)) = p.Ex f := by - rw [← Ex_send P (fun x => (X x i,W x i)), hp i] - have hE : Y.Ex a ≤ F := by - have hX : Y.Ex (fun w => (R w).Ex fun x => -log (pack (q w) (X x))) = - ∑ i : I, p.Hcond Prod.fst Prod.snd := by - let f (w : I → V) (x : Ω) := -log (pack (q w) (X x)) - rw [show Y.Ex _ = P.Ex _ from P.after_Ex_sum W f] - have H1 (x : Ω) (hx : 0 < P x) : - f (W x) x = ∑ i, -log (q (W x) i (X x i)) := by - unfold f; rw [pack_apply,log_prod,Finset.sum_neg_distrib] - exact fun i _ => (hval x hx i).ne' - rw [P.Ex_congr_mem H1,Ex_finset_sum,Hcond_log] - refine Finset.sum_congr rfl fun i _ => ?_ - exact hrow i (fun x => -log (k x.2 x.1)) - have HU : Y.Ex (fun w => (R w).Ex (fun x => c (U x))) = _ := - P.after_Ex_sum W (fun _ x => c (U x)) - have hz : Y.Ex (fun w => (R w).Hcond X Z) = _ := - (Hcond_after_cond P X Z W).symm - unfold a - rw [Ex_add,Ex_sub,hX,HU,hz,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - exact hs - have hB : Y.Ex b ≤ N * D := by - have HH : ∀ i : I, Y.Ex (fun w => entropy (q w i)) = p.Hcond Prod.fst Prod.snd := by - intro i - dsimp only [Y]; rw [Ex_send] - rw [Hcond_after,Ex_send] - exact hrow i (fun x => entropy (k x.2)) - unfold b - simp_rw [Ex_finset_sum,Ex_sub,HH,Ex_const,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - exact mul_le_mul_of_nonneg_left hd (by positivity) - obtain ⟨w,hw,hw',hw''⟩ := - average_two_below Y a b ha (fun x _ => hb x) F (N*D) hF - (mul_pos (by unfold N; positivity) hD) hE hB - obtain ⟨z,hz⟩ := large_heat (R w) (pack (q w)) X (tot tag) U c (hdom w hw) (2*F) hw' - exact ⟨q w,z,hz,hw''⟩ - -lemma freeze (tag : A → G) (hi : Injective tag) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) - (P : Labelled A) (m r : ℕ) (hm : 0 < m) (hr : 0 < r) - (F D : ℝ) (hF : 0 < F) (hD : 0 < D) (hs : fundK tag c P m r ≤ F) - (hd : log (Fintype.card A) - fundG tag P 1 ≤ D) : - ∃ (q : (Fin r×Fin m) → Law A) (z : G), - exp (-(2*F)) ≤ (pack q).Ex (fun x => exp (-c (z-tot tag x))) ∧ - ∑ i, (log (Fintype.card A)- (q i).entropy) ≤ 2*((r:ℝ)*m*D) := by - have : NeZero r := ⟨hr.ne'⟩ - have : NeZero m := ⟨hm.ne'⟩ - let t := lblSum tag P m - let C := lblCtx P m - let Q := lblP P m - obtain ⟨S,hS,he⟩ := (opt_spec r Q t C (fun x => x) c).1 - let Y := WordSample r (Letters m (A×P.obs)) G - let X (p : Y) (i : Fin r × Fin m) := (p.1 i.1 i.2).1 - let W (p : Y) (i : Fin r × Fin m) := (p.1 i.1 i.2).2 - have Hrow (i : Fin r×Fin m) : S.send (fun p => (X p i,W p i)) = P.law := by - have h1 : Q.send (fun x => x i.2) = P.law := marginalPack _ _ - rw [← h1,← hS i.1,send_send]; rfl - have HQ : Q.Hcond (fun x => x) C = m*P.law.Hcond Prod.fst Prod.snd := by - have HH := Hcond_vec_pack (fun _ : Fin m => P.law) (fun _ a => a) - (fun _ => Prod.snd) - have hi : P.law.Hcond (fun x => x) Prod.snd = P.law.Hcond Prod.fst Prod.snd := by - unfold Hcond; congr 1; apply H_partition; simp - simp_rw [hi,Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] at HH - exact HH - let u0 : Y → G := Prod.snd - let s0 (x : Y) := ans t Prod.fst u0 x - have ht0 (x : Y) : s0 x = ans tag X u0 x := by - unfold s0 t lblSum ans tot - simp only [Fintype.sum_prod_type, Function.comp_apply, X] - let w0 (x : Y) := ctxt C Prod.fst x - have h1 : S.Hcond Prod.fst (fun x => (s0 x,w0 x)) = - S.Hcond X (fun x => (ans tag X u0 x,W x)) := by - unfold Hcond - have h' (x y : Y) : w0 x = w0 y ↔ W x=W y := by - simp only [w0,W,C,ctxt,lblCtx,funext_iff,Prod.forall] - congr 1 - · apply H_partition - intro x y - simp only [Prod.mk.injEq,h',ht0] - have hy : x.1=y.1 ↔ X x=X y ∧ W x=W y := by - simp only [X,W,funext_iff,Prod.forall] - constructor - · intro h; simp [h] - · grind - tauto - · apply H_partition; simp only [Prod.mk.injEq,h',ht0]; tauto - have hcost : Fintype.card (Fin r×Fin m)*P.law.Hcond Prod.fst Prod.snd - - S.Hcond X (fun x => (ans tag X u0 x,W x))+ S.Ex (fun a => c (u0 a)) ≤ F := by - have HH : rent t C (fun x => x) c S Prod.fst Prod.snd ≤ F := he.le.trans hs - unfold rent at HH - rw [rent_first hS,HQ] at HH - change _ - S.Hcond Prod.fst (fun x => (s0 x,w0 x))+S.Ex (fun x => c (u0 x)) ≤ F at HH - rw [h1] at HH - simp only [Fintype.card_fin,Fintype.card_prod,Nat.cast_mul,mul_assoc] at * - exact HH - have Hsel := stack_select tag c hc P.law S X W u0 Hrow F D hF hD hcost - (by rwa [fundG_one tag hi] at hd) - simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul] using Hsel -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Context + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A G V B Ω I : Type} [Fintype A] [Fintype G] [AddCommGroup G] + [Fintype V] [Fintype Ω] [Fintype B] [Fintype I] [DecidableEq I] +set_option linter.unusedSectionVars false + +lemma average_two_below (P : Law Ω) (f g : Ω → ℝ) + (h0 : ∀ x, 0 < P x → 0 ≤ f x) (h1 : ∀ x, 0 < P x → 0 ≤ g x) + (F G : ℝ) (hp : 0 < F) (hq : 0 < G) (hf : P.Ex f ≤ F) (hg : P.Ex g ≤ G) : + ∃ x, 0 < P x ∧ f x ≤ 2*F ∧ g x ≤ 2*G := by + obtain ⟨x,hx,hh⟩ := P.exists_le_Ex (fun x => F*g x + G*f x) + rw [Ex_add,Ex_lmul,Ex_lmul] at hh + have H : F*g x + G*f x ≤ F*G+G*F := le_trans hh (by gcongr) + exact ⟨x,hx,by nlinarith [h1 _ hx],by nlinarith [h0 _ hx]⟩ + +/-- A small noisy cost implies a large heat sum (Gibbs comparison). -/ +lemma large_heat (P : Law Ω) (q : Law A) (X : Ω → A) (tag : A → G) (U : Ω → G) (c : G → ℝ) + (hcov : (P.send X).dominated q) (F : ℝ) + (hF : P.Ex (fun x => -log (q (X x))) - P.Hcond X (fun x => tag (X x)+U x)+ + P.Ex (fun x => c (U x)) ≤ F) : + ∃ z : G, exp (-F) ≤ q.Ex fun a => exp (-c (z-tag a)) := by + let Z (a : Ω) := tag (X a)+U a + let V := P.send Z + let p z := P.after Z z + let g (z : G) := + (p z).Ex (fun x => -log (q (X x))) - (p z).H X + (p z).Ex (fun x => c (U x)) + have hh : V.Ex g ≤ F := by + have H (f : Ω → ℝ) : + V.Ex (fun z => (p z).Ex f) = P.Ex f := after_Ex_sum P Z (fun _ => f) + unfold g; rw [Ex_add,Ex_sub,H,H] + change _ - (P.send Z).Ex (fun z => (P.after Z z).H X)+_≤ _ + rw [← Hcond_after]; exact hF + obtain ⟨z,hz,hze⟩ := V.exists_le_Ex g + let f a := q a* exp (-c (z-tag a)) + let D := ∑ a, f a + have hf (a) : 0 ≤ f a := mul_nonneg (q.nonneg _) (exp_nonneg _) + have hc (a) (ha : 0 < p z a) : 0 < q (X a) := by + have HH := (after_mem (P:=P) (W:=Z) hz).mp ha + exact hcov _ (P.send_pos X HH.2) + have ht : 0 < D := by + obtain ⟨x,hx⟩ := q.mem_nonempty + have h : 0 < f x := mul_pos hx (exp_pos _) + exact lt_of_lt_of_le h (Finset.single_le_sum (s:=Finset.univ) (fun a _ => hf a) (Finset.mem_univ _)) + let Q := proportional f hf ht + have he (x) (hx : 0 < p z x) : + -log (Q (X x)) = -log (q (X x))+c (U x)+log D := by + have hx' := (after_mem (P:=P) (W:=Z) hz).mp hx + have h : z-tag (X x)=U x := by rw [← hx'.1]; unfold Z; abel + change -log (f _/D) = _ + rw [log_div (mul_pos (hc _ hx) (exp_pos _)).ne' ht.ne'] + rw [log_mul (hc _ hx).ne' (exp_pos _).ne',h,log_exp] + ring + have hd : ((p z).send X).dominated Q := by + intro a ha + obtain ⟨y,rfl,hy⟩ := (send_pos_iff _ _ _).mp ha + exact div_pos (mul_pos (hc y hy) (exp_pos _)) ht + have HC : (p z).H X ≤ (p z).Ex (fun x => -log (Q (X x))) := by + rw [← Ex_send (p z) X (fun x => -log (Q x))] + exact sub_nonneg.mp (kl_nonneg hd) + have hlin := ((p z).Ex_congr_mem he) + rw [hlin,Ex_add,Ex_add,Ex_const] at HC + have h1 : -F ≤ log D := by + have hi := hze.trans hh + dsimp only [g] at hi; linarith + refine ⟨z,le_trans (Real.exp_le_exp.mpr h1) (Real.exp_log ht).le⟩ + +/-- Stack selection with one coordinate per actual letter. -/ +lemma stack_select (tag : A → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) + (p : Law (A×V)) (P : Law Ω) (X : Ω → I → A) (W : Ω → I → V) (U : Ω → G) + (hp : ∀ i : I, P.send (fun x => (X x i,W x i)) = p) + (F D : ℝ) (hF : 0 < F) (hD : 0 < D) + [Nonempty I] + (hs : Fintype.card I * p.Hcond Prod.fst Prod.snd - + P.Hcond X (fun x => (ans tag X U x, W x)) + P.Ex (fun x => c (U x)) ≤ F) + (hd : log (Fintype.card A) - p.Hcond Prod.fst Prod.snd ≤ D) : + ∃ (q : I → Law A) (z : G), + exp (-(2*F)) ≤ (pack q).Ex (fun x => exp (-c (z-tot tag x))) ∧ + ∑ i, (log (Fintype.card A)- (q i).entropy) ≤ 2*(Fintype.card I*D) := by + classical + let R w := P.after W w + let k v := (p.after Prod.snd v).send Prod.fst + let q (w : I → V) (i : I) : Law A := k (w i) + let Z := ans tag X U + let Y := P.send W + have hval (a : Ω) (ha : 0 < P a) (i : I) : 0 < q (W a) i (X a i) := by + have H0 := P.send_pos (fun a => (X a i,W a i)) ha + rw [hp i] at H0 + exact (p.after Prod.snd (W a i)).send_pos Prod.fst (p.after_self_pos _ H0) + have hdom (w : I → V) (hw : 0 < Y w) : + ((R w).send X).dominated (pack (q w)) := by + intro x hx + obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx + have H := (after_mem hw).mp ha + rw [pack_pos,← H.1] + exact hval a H.2 + let N := Fintype.card I + let a (w : I → V) := + (R w).Ex (fun x => -log (pack (q w) (X x))) - + (R w).Hcond X Z + (R w).Ex fun x => c (U x) + let b (w : I → V) := ∑ i, (log (Fintype.card A) - entropy (q w i)) + have ha (w) (hw : 0 < Y w) : 0 ≤ a w := by + have hh := kl_nonneg (hdom w hw) + unfold kl crossE at hh + rw [Ex_send] at hh + have ht := Hcond_le_H (R w) X Z + unfold H at ht + exact add_nonneg (by linarith) (Ex_nonneg (R w) (fun x => hc _)) + have hb (w : I → V) : 0 ≤ b w := + Finset.sum_nonneg fun i _ => sub_nonneg.mpr (entropy_le_log_card _) + have hrow (i : I) (f : (A×V) → ℝ) : + P.Ex (fun x => f (X x i,W x i)) = p.Ex f := by + rw [← Ex_send P (fun x => (X x i,W x i)), hp i] + have hE : Y.Ex a ≤ F := by + have hX : Y.Ex (fun w => (R w).Ex fun x => -log (pack (q w) (X x))) = + ∑ i : I, p.Hcond Prod.fst Prod.snd := by + let f (w : I → V) (x : Ω) := -log (pack (q w) (X x)) + rw [show Y.Ex _ = P.Ex _ from P.after_Ex_sum W f] + have H1 (x : Ω) (hx : 0 < P x) : + f (W x) x = ∑ i, -log (q (W x) i (X x i)) := by + unfold f; rw [pack_apply,log_prod,Finset.sum_neg_distrib] + exact fun i _ => (hval x hx i).ne' + rw [P.Ex_congr_mem H1,Ex_finset_sum,Hcond_log] + refine Finset.sum_congr rfl fun i _ => ?_ + exact hrow i (fun x => -log (k x.2 x.1)) + have HU : Y.Ex (fun w => (R w).Ex (fun x => c (U x))) = _ := + P.after_Ex_sum W (fun _ x => c (U x)) + have hz : Y.Ex (fun w => (R w).Hcond X Z) = _ := + (Hcond_after_cond P X Z W).symm + unfold a + rw [Ex_add,Ex_sub,hX,HU,hz,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + exact hs + have hB : Y.Ex b ≤ N * D := by + have HH : ∀ i : I, Y.Ex (fun w => entropy (q w i)) = p.Hcond Prod.fst Prod.snd := by + intro i + dsimp only [Y]; rw [Ex_send] + rw [Hcond_after,Ex_send] + exact hrow i (fun x => entropy (k x.2)) + unfold b + simp_rw [Ex_finset_sum,Ex_sub,HH,Ex_const,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + exact mul_le_mul_of_nonneg_left hd (by positivity) + obtain ⟨w,hw,hw',hw''⟩ := + average_two_below Y a b ha (fun x _ => hb x) F (N*D) hF + (mul_pos (by unfold N; positivity) hD) hE hB + obtain ⟨z,hz⟩ := large_heat (R w) (pack (q w)) X (tot tag) U c (hdom w hw) (2*F) hw' + exact ⟨q w,z,hz,hw''⟩ + +lemma freeze (tag : A → G) (hi : Injective tag) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) + (P : Labelled A) (m r : ℕ) (hm : 0 < m) (hr : 0 < r) + (F D : ℝ) (hF : 0 < F) (hD : 0 < D) (hs : fundK tag c P m r ≤ F) + (hd : log (Fintype.card A) - fundG tag P 1 ≤ D) : + ∃ (q : (Fin r×Fin m) → Law A) (z : G), + exp (-(2*F)) ≤ (pack q).Ex (fun x => exp (-c (z-tot tag x))) ∧ + ∑ i, (log (Fintype.card A)- (q i).entropy) ≤ 2*((r:ℝ)*m*D) := by + have : NeZero r := ⟨hr.ne'⟩ + have : NeZero m := ⟨hm.ne'⟩ + let t := lblSum tag P m + let C := lblCtx P m + let Q := lblP P m + obtain ⟨S,hS,he⟩ := (opt_spec r Q t C (fun x => x) c).1 + let Y := WordSample r (Letters m (A×P.obs)) G + let X (p : Y) (i : Fin r × Fin m) := (p.1 i.1 i.2).1 + let W (p : Y) (i : Fin r × Fin m) := (p.1 i.1 i.2).2 + have Hrow (i : Fin r×Fin m) : S.send (fun p => (X p i,W p i)) = P.law := by + have h1 : Q.send (fun x => x i.2) = P.law := marginalPack _ _ + rw [← h1,← hS i.1,send_send]; rfl + have HQ : Q.Hcond (fun x => x) C = m*P.law.Hcond Prod.fst Prod.snd := by + have HH := Hcond_vec_pack (fun _ : Fin m => P.law) (fun _ a => a) + (fun _ => Prod.snd) + have hi : P.law.Hcond (fun x => x) Prod.snd = P.law.Hcond Prod.fst Prod.snd := by + unfold Hcond; congr 1; apply H_partition; simp + simp_rw [hi,Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] at HH + exact HH + let u0 : Y → G := Prod.snd + let s0 (x : Y) := ans t Prod.fst u0 x + have ht0 (x : Y) : s0 x = ans tag X u0 x := by + unfold s0 t lblSum ans tot + simp only [Fintype.sum_prod_type, Function.comp_apply, X] + let w0 (x : Y) := ctxt C Prod.fst x + have h1 : S.Hcond Prod.fst (fun x => (s0 x,w0 x)) = + S.Hcond X (fun x => (ans tag X u0 x,W x)) := by + unfold Hcond + have h' (x y : Y) : w0 x = w0 y ↔ W x=W y := by + simp only [w0,W,C,ctxt,lblCtx,funext_iff,Prod.forall] + congr 1 + · apply H_partition + intro x y + simp only [Prod.mk.injEq,h',ht0] + have hy : x.1=y.1 ↔ X x=X y ∧ W x=W y := by + simp only [X,W,funext_iff,Prod.forall] + constructor + · intro h; simp [h] + · grind + tauto + · apply H_partition; simp only [Prod.mk.injEq,h',ht0]; tauto + have hcost : Fintype.card (Fin r×Fin m)*P.law.Hcond Prod.fst Prod.snd - + S.Hcond X (fun x => (ans tag X u0 x,W x))+ S.Ex (fun a => c (u0 a)) ≤ F := by + have HH : rent t C (fun x => x) c S Prod.fst Prod.snd ≤ F := he.le.trans hs + unfold rent at HH + rw [rent_first hS,HQ] at HH + change _ - S.Hcond Prod.fst (fun x => (s0 x,w0 x))+S.Ex (fun x => c (u0 x)) ≤ F at HH + rw [h1] at HH + simp only [Fintype.card_fin,Fintype.card_prod,Nat.cast_mul,mul_assoc] at * + exact HH + have Hsel := stack_select tag c hc P.law S X W u0 Hrow F D hF hD hcost + (by rwa [fundG_one tag hi] at hd) + simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul] using Hsel +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Low.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Low.lean index aaaf6a60aee07a8bb547a32c6ca0ec311ec2259e..b893b2a31ebaf5caff79b2b80f90fbb490ae6d35 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Low.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Low.lean @@ -1,274 +1,274 @@ -import OAI.Analysis.FourierLLogL.Compress.Thinning -import OAI.Analysis.FourierLLogL.Compress.Context -/-! # Initial estimates for contextual costs -/ -open Function Filter Set -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A Ω G : Type} [Fintype A] [Fintype Ω] [Fintype G] [AddCommGroup G] - [DecidableEq G] {n : ℕ} -set_option linter.unusedSectionVars false - - -lemma initial_pack (p : Law A) (m : ℕ) : - lblP (initial p) m = (iid p m).send - (fun x => fun i => (x i,())) := by - unfold lblP iid initial - exact (sendPack (fun _ => p) (fun _ (x : A) => (x,()))).symm - -lemma G_initial (tag : A → G) (p : Law A) (m : ℕ) : - fundG tag (initial p) m = (iid p m).H (tot tag) := by - unfold fundG - rw [initial_pack] - exact (Hcond_send (iid p m) (fun x i => (x i,())) - (lblSum tag (initial p) m) (lblCtx (initial p) m)).trans (Hcond_constant _ _ (fun _ => ())) - -lemma K_initial (tag : A → G) (c : G → ℝ) (p : Law A) (m r : ℕ) : - fundK tag c (initial p) m r = opt r (iid p m) (tot tag) - (fun _ => fun _ : Fin m => ()) id c := by - unfold fundK - rw [initial_pack] - exact opt_proj (lblSum tag (initial p) m) (lblCtx (initial p) m) id c - (iid p m) (fun x i => (x i,())) r - -lemma I_initial (tag : A → G) (p : Law A) (r : ℕ) : - fundI tag (initial p) r = opt r p tag (fun _ => ()) (fun _ : Unit => (0:G)) - (fun _ => 0) := by - unfold fundI initial - exact opt_proj (fun x : A×Unit => tag x.1) Prod.snd _ _ p (fun x => (x,())) r - -lemma trial_initial {E Z : Type} [Fintype E] [Fintype Z] [Nonempty E] - (tag : A → G) (p : Law A) (e : E → G) (c : E→ℝ) (z : Z) - (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) - (h : hasRows p P X) : - rent tag (fun _ => z) e c P X U = - (∑ i, P.Ex (fun a => -log (p (X a i)))) - - P.Hcond X (ans tag X (fun a => e (U a))) + P.Ex (fun a => c (U a)) := by - have hz (i : Fin n) : - P.Hcond (readP X i) ((fun _ => z) ∘ readP X i) = - P.Ex (fun a => -log (p (X a i))) := by - have h1 : P.Hcond (readP X i) (fun _ => z) = P.H (readP X i) := - Hcond_constant _ _ _ - rw [show (fun (_:A) => z) ∘ readP X i = fun _ => z from rfl,h1,H_log,h i]; rfl - unfold rent - simp_rw [hz] - congr 2 - apply Hcond_right_partition - simp only [Prod.mk.injEq]; tauto - -lemma K_initial_le (tag : A → G) (c : G → ℝ) (p : Law A) (m r : ℕ) - (P : Law Ω) (X : Ω → Letters r (Letters m A)) (U : Ω → G) - (h : hasRows (iid p m) P X) : - fundK tag c (initial p) m r ≤ bill (tot tag) c (iid p m) P X U := by - rw [K_initial] - apply le_trans (opt_le_rent _ _ _ _ _ P X U h) (le_of_eq ?_) - rw [trial_initial _ _ _ _ _ _ X U h]; rfl - -lemma exact_zero_g {I : Type} [Fintype I] [DecidableEq I] - (P : Law Ω) (X : Ω → I → A) (tag : A → G) (q : Law A) (h : covered P X q) : - bill tag (fun _=>0) q P X (fun _=>0) = - (P.send X).kl (pack (fun _ => q)) + P.H (fun a => tot tag (X a)) := by - rw [bill_with_entropy h,Hcond_determined (P:=P) (X:=fun _=>0) (W:=X) (fun _ _ _ => rfl)] - unfold ans - simp_rw [add_zero,Ex_const] - ring - -lemma I_initial_le (tag : A → G) (p : Law A) (r : ℕ) - (P : Law Ω) (X : Ω → Letters r A) (hh : hasRows p P X) : - fundI tag (initial p) r ≤ (P.send X).kl (iid p r)+P.H (fun a => tot tag (X a)) := by - rw [I_initial] - apply le_trans (opt_le_rent _ _ _ _ p P X (fun _ => ()) hh) - rw [trial_initial _ _ _ _ _ _ X _ hh] - change bill tag (fun _=>0) p P X (fun _=>0) ≤ _ - exact (exact_zero_g P X tag p (kl_covered P X p hh)).le - -/-- Compare the entropy of the exact sum with the entropy of the perturbed sum and the error. -/ -lemma sum_entropy {I : Type} [Fintype I] [DecidableEq I] - (P : Law Ω) (X : Ω → I → A) (U : Ω → G) (tag : A → G) - (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (q : Law A) (hcov : covered P X q) - (Q : Law G) (hQ : ∀ y, 0 < Q y) (L : ℝ) (hL : ∀ y, -log (Q y) = L+c y) : - let F := bill tag c q P X U - P.H (fun a => tot tag (X a)) + (P.send X).kl (pack fun _ => q) ≤ 3*F+2*L := by - intro F - let S (a) := tot tag (X a) - let Z := ans tag X U - have hEnt : P.H U ≤ L + P.Ex (fun a => c (U a)) := by - have H := entropy_le_cross_aux (P.send U) Q Q.nonneg Q.total.le fun x _ => hQ x - refine H.trans (le_of_eq ?_) - unfold crossE - simp_rw [hL,Ex_send,Ex_add,Ex_const] - have hBill : P.Ex (fun a => c (U a)) ≤ F := - ((le_add_of_nonneg_left (kl_nonneg (covered_pack hcov))).trans - (kl_le_bill hcov tag c hc _)) - have hH := bill_with_entropy hcov tag c U - change F = _+(P.H Z-_)+_ at hH - have HI : P.H S ≤ P.H Z + P.H U := by - have H1 : P.H S ≤ P.H (fun a => (U a,Z a)) := - H_le_of_determined _ fun a b hh => by - have hi := Prod.mk.inj hh - change U a=U b ∧ S a+U a=S b+U b at hi - exact add_right_cancel (b:=U b) (by rw [← hi.2,hi.1]) - have H2 := Hcond_le_H P U Z - rw [H_pair_chain] at H1; linarith - have hC := Hcond_le_H P U X - change P.H S + _ ≤ _ - linarith [P.Ex_nonneg (fun a => hc (U a))] - - -structure Starting (tag : A → G) (c : G → ℝ) (N m : ℕ) (E ε : ℝ) where - q : Law A - P : Law (WordSample N A G) - sym : exch P - marg (i : Fin N) : P.send (fun a => a.1 i) = q - bound : bill tag c q P Prod.fst Prod.snd ≤ E - within : m ≤ N - near : tv (P.send (prefixB Prod.fst m within)) (iid q m) ≤ ε - -namespace Starting -variable {tag : A → G} {c : G → ℝ} {N m : ℕ} {E ε : ℝ} (s : Starting tag c N m E ε) - -lemma cover : covered s.P Prod.fst s.q := kl_covered _ _ _ s.marg - -lemma exact_bound (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) - (L : ℝ) (hL : ∀ y, -log (Q y)=L+c y) - (n : ℕ) (hn : n ≤ N) : - let X := prefixB (α:=A) (Ω:=WordSample N A G) Prod.fst n hn - s.P.H (fun a => tot tag (X a)) + (s.P.send X).kl (iid s.q n) ≤ 3*E+2*L := by - intro X - have h1 := bill_subfamily tag c s.q s.P Prod.fst Prod.snd s.cover - (offset 0 n (show 0+n ≤ N by omega)) (offset_injective _ _ _) - change bill tag c s.q s.P X Prod.snd ≤ _ at h1 - refine (sum_entropy s.P X Prod.snd tag c hc s.q (covered_subfamily s.cover _) Q hQ L hL).trans ?_ - linarith [s.bound] - -lemma I_upper (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) - (L : ℝ) (hL : ∀ y,-log (Q y)=L+c y) (n : ℕ) (hn : n ≤ N) : - fundI tag (initial s.q) n ≤ 3*E+2*L := by - have h := s.exact_bound hc Q hQ L hL n hn - let X := prefixB (α:=A) (Ω:=WordSample N A G) Prod.fst n hn - have heq : hasRows s.q s.P X := fun i => s.marg (offset 0 n _ i) - refine (I_initial_le tag s.q n s.P X heq).trans ?_ - rw [add_comm] - exact h - -lemma near_small (n : ℕ) (hn : n ≤ m) : - tv (s.P.send (prefixB Prod.fst n (hn.trans s.within))) (iid s.q n) ≤ ε := by - let X : Letters m A → Letters n A := fun x i => x (offset 0 n (by omega : 0+n ≤ m) i) - have h := tv_data_processing (s.P.send (prefixB Prod.fst m s.within)) (iid s.q m) X - rw [send_send] at h - have hh : (iid s.q m).send X = iid s.q n := - sendPack_embedding _ _ (offset_injective _ _ _) - rw [hh] at h - apply le_trans _ s.near - have he : (fun a : WordSample N A G => X (prefixB Prod.fst m s.within a)) = prefixB Prod.fst n (hn.trans s.within) := by - funext x ⟨i,hi⟩; simp only [X,prefixB,readBlock,keep,offset,zero_add] - rwa [he] at h -lemma G_upper (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) - (L : ℝ) (hL : ∀ y,-log (Q y)=L+c y) (n : ℕ) (hn : n ≤ m) : - fundG tag (initial s.q) n ≤ 3*E+2*L+log 2 + ε*lnS (Fin n) tag := by - rw [G_initial] - let X := prefixB (α:=A) (Ω:= WordSample N A G) Prod.fst n (hn.trans s.within) - let Y := tot tag (ι:=Fin n) - have H1 : tv ((iid s.q n).send Y) ((s.P.send X).send Y) ≤ ε := by - rw [tv_comm] - exact (tv_data_processing _ _ Y).trans (s.near_small n hn) - have HT := entropy_le_of_tv ((iid s.q n).send Y) ((s.P.send X).send Y) - (sums (Fin n) tag) fun x hx => by - obtain ⟨a,rfl,_⟩ := (send_pos_iff _ _ _).mp hx - exact Finset.mem_image_of_mem _ (Finset.mem_univ _) - change _ ≤ (s.P.send X).H Y + (_ + _ * lnS (Fin n) tag) at HT - have hk : dominated (s.P.send X) (iid s.q n) := covered_pack - (covered_subfamily s.cover _) - have he := s.exact_bound hc Q hQ L hL n (hn.trans s.within) - change s.P.H _+kl (s.P.send X) _ ≤ _ at he - apply HT.trans - rw [H_send] - nlinarith [kl_nonneg hk,lnS_nonneg (Fin n) tag] - -lemma block_near (n : ℕ) (hn : n ≤ m) (g : Fin n → Fin N) (hg : Injective g) : - tv (s.P.send (keep Prod.fst g)) (iid s.q n) ≤ ε := by - let f := offset 0 n (by have := s.within; omega : 0+n ≤ N) - have Hf : Injective f := offset_injective _ _ _ - obtain ⟨e,he⟩ := Equiv.Perm.exists_extending_pair _ _ Hf hg - have hh := - congrArg (fun R : Law (WordSample N A G) => R.send (keep Prod.fst f)) (s.sym e) - rw [send_send] at hh - have heq : (fun x : WordSample N A G => keep Prod.fst f (appPerm e x)) = - keep Prod.fst g := by - funext x i; dsimp only [keep,appPerm]; rw [he i] - rw [heq] at hh - rw [hh] - exact s.near_small n hn - -lemma block_card (n r : ℕ) : - lnS (Fin r) (tot tag (ι:=Fin n)) = lnS (Fin (r*n)) tag := by - let e := finProdFinEquiv (m:=r) (n:=n) - have hh (x : Fin (r*n) → A) : - tot tag x = tot (tot tag (ι:=Fin n)) (fun i : Fin r => fun j => x (e (i,j))) := by - unfold tot - rw [← Fintype.sum_equiv e (fun i => tag (x (e i))) (fun i => tag (x i)) (fun _ => rfl), - Fintype.sum_prod_type] - have H : sums (Fin (r*n)) tag = sums (Fin r) (tot tag (ι:=Fin n)) := by - ext a - simp only [sums,Finset.mem_image,Finset.mem_univ,true_and] - constructor - · rintro ⟨x,rfl⟩ - exact ⟨_,(hh x).symm⟩ - · rintro ⟨x,rfl⟩ - refine ⟨(fun i => x (e.symm i).1 (e.symm i).2),?_⟩ - rw [hh]; simp - simp only [lnS,H] - -lemma K_upper (hc : ∀ x, 0 ≤ c x) (hc0 : c 0 = 0) - (n r : ℕ) (hr : 0 < r) (hn : n ≤ m) (hn' : 2*r*n ≤ N) : - fundK tag c (initial s.q) n r ≤ E + log (Fintype.card (Ticket r (2*r))) + - (r + 2*ε*lnS (Fin (r*n)) tag) := by - let g : Fin (2*r) × Fin n → Fin N := offset 0 ((2*r)*n) (by omega) ∘ finProdFinEquiv - have hg : Injective g := (offset_injective _ _ _).comp finProdFinEquiv.injective - let X (x : WordSample N A G) (i : Fin (2*r)) (j : Fin n) := x.1 (g (i,j)) - let q := iid s.q n - have Hc : covered s.P X q := by - intro x hx i; exact (pack_pos _ _).mpr (fun j => s.cover x hx (g (i,j))) - have Hm (i : Fin (2*r)) : tv (s.P.send (fun x => X x i)) q ≤ ε := - s.block_near _ hn (fun j => g (i,j)) (fun _ _ h => congrArg Prod.snd (hg h)) - obtain ⟨Q,hQ,hb⟩ := thinned_bill (tot tag (ι:=Fin n)) c hc hc0 r (2*r) hr rfl - s.P X Prod.snd q Hc ε Hm - let X' := keep (Prod.fst : WordSample N A G → _) g - have heq : bill (tot tag (ι:=Fin n)) c q s.P X Prod.snd = - bill tag c s.q s.P X' Prod.snd := by - let Y := ans tag X' Prod.snd - have hy : ans (tot tag (ι:=Fin n)) X Prod.snd = Y := by - ext x; unfold Y ans tot - rw [Fintype.sum_prod_type]; rfl - unfold bill - rw [hy, Fintype.sum_prod_type] - have h2 : s.P.Hcond X Y = s.P.Hcond X' Y := by - unfold Hcond - congr 1 - refine H_partition _ fun a b => ?_ - simp only [Prod.mk.injEq,funext_iff,Prod.forall,X,X',keep] - have h1 (i : Fin (2*r)) : s.P.Ex (fun a => -log (q (X a i))) = - ∑ j, s.P.Ex (fun a => -log (s.q (X' a (i,j)))) := by - rw [← Ex_finset_sum] - apply Ex_congr_mem - intro x hx - change -log (∏ j : Fin n, s.q (X x i j)) = _ - rw [log_prod,Finset.sum_neg_distrib] - · rfl - · exact fun j _ => (s.cover x hx _).ne' - rw [h2] - simp_rw [h1] - rfl - have hh := bill_subfamily tag c s.q s.P Prod.fst Prod.snd s.cover g hg - change bill tag c s.q s.P X' Prod.snd ≤ _ at hh - rw [heq,block_card] at hb - refine le_trans (K_initial_le tag c s.q n r Q Prod.fst Prod.snd hQ) ?_ - exact le_trans hb (add_le_add_left (add_le_add_left (hh.trans s.bound) _) _) - -end Starting -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Thinning +import OAI.Analysis.FourierLLogL.Compress.Context +/-! # Initial estimates for contextual costs -/ +open Function Filter Set +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A Ω G : Type} [Fintype A] [Fintype Ω] [Fintype G] [AddCommGroup G] + [DecidableEq G] {n : ℕ} +set_option linter.unusedSectionVars false + + +lemma initial_pack (p : Law A) (m : ℕ) : + lblP (initial p) m = (iid p m).send + (fun x => fun i => (x i,())) := by + unfold lblP iid initial + exact (sendPack (fun _ => p) (fun _ (x : A) => (x,()))).symm + +lemma G_initial (tag : A → G) (p : Law A) (m : ℕ) : + fundG tag (initial p) m = (iid p m).H (tot tag) := by + unfold fundG + rw [initial_pack] + exact (Hcond_send (iid p m) (fun x i => (x i,())) + (lblSum tag (initial p) m) (lblCtx (initial p) m)).trans (Hcond_constant _ _ (fun _ => ())) + +lemma K_initial (tag : A → G) (c : G → ℝ) (p : Law A) (m r : ℕ) : + fundK tag c (initial p) m r = opt r (iid p m) (tot tag) + (fun _ => fun _ : Fin m => ()) id c := by + unfold fundK + rw [initial_pack] + exact opt_proj (lblSum tag (initial p) m) (lblCtx (initial p) m) id c + (iid p m) (fun x i => (x i,())) r + +lemma I_initial (tag : A → G) (p : Law A) (r : ℕ) : + fundI tag (initial p) r = opt r p tag (fun _ => ()) (fun _ : Unit => (0:G)) + (fun _ => 0) := by + unfold fundI initial + exact opt_proj (fun x : A×Unit => tag x.1) Prod.snd _ _ p (fun x => (x,())) r + +lemma trial_initial {E Z : Type} [Fintype E] [Fintype Z] [Nonempty E] + (tag : A → G) (p : Law A) (e : E → G) (c : E→ℝ) (z : Z) + (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) + (h : hasRows p P X) : + rent tag (fun _ => z) e c P X U = + (∑ i, P.Ex (fun a => -log (p (X a i)))) - + P.Hcond X (ans tag X (fun a => e (U a))) + P.Ex (fun a => c (U a)) := by + have hz (i : Fin n) : + P.Hcond (readP X i) ((fun _ => z) ∘ readP X i) = + P.Ex (fun a => -log (p (X a i))) := by + have h1 : P.Hcond (readP X i) (fun _ => z) = P.H (readP X i) := + Hcond_constant _ _ _ + rw [show (fun (_:A) => z) ∘ readP X i = fun _ => z from rfl,h1,H_log,h i]; rfl + unfold rent + simp_rw [hz] + congr 2 + apply Hcond_right_partition + simp only [Prod.mk.injEq]; tauto + +lemma K_initial_le (tag : A → G) (c : G → ℝ) (p : Law A) (m r : ℕ) + (P : Law Ω) (X : Ω → Letters r (Letters m A)) (U : Ω → G) + (h : hasRows (iid p m) P X) : + fundK tag c (initial p) m r ≤ bill (tot tag) c (iid p m) P X U := by + rw [K_initial] + apply le_trans (opt_le_rent _ _ _ _ _ P X U h) (le_of_eq ?_) + rw [trial_initial _ _ _ _ _ _ X U h]; rfl + +lemma exact_zero_g {I : Type} [Fintype I] [DecidableEq I] + (P : Law Ω) (X : Ω → I → A) (tag : A → G) (q : Law A) (h : covered P X q) : + bill tag (fun _=>0) q P X (fun _=>0) = + (P.send X).kl (pack (fun _ => q)) + P.H (fun a => tot tag (X a)) := by + rw [bill_with_entropy h,Hcond_determined (P:=P) (X:=fun _=>0) (W:=X) (fun _ _ _ => rfl)] + unfold ans + simp_rw [add_zero,Ex_const] + ring + +lemma I_initial_le (tag : A → G) (p : Law A) (r : ℕ) + (P : Law Ω) (X : Ω → Letters r A) (hh : hasRows p P X) : + fundI tag (initial p) r ≤ (P.send X).kl (iid p r)+P.H (fun a => tot tag (X a)) := by + rw [I_initial] + apply le_trans (opt_le_rent _ _ _ _ p P X (fun _ => ()) hh) + rw [trial_initial _ _ _ _ _ _ X _ hh] + change bill tag (fun _=>0) p P X (fun _=>0) ≤ _ + exact (exact_zero_g P X tag p (kl_covered P X p hh)).le + +/-- Compare the entropy of the exact sum with the entropy of the perturbed sum and the error. -/ +lemma sum_entropy {I : Type} [Fintype I] [DecidableEq I] + (P : Law Ω) (X : Ω → I → A) (U : Ω → G) (tag : A → G) + (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (q : Law A) (hcov : covered P X q) + (Q : Law G) (hQ : ∀ y, 0 < Q y) (L : ℝ) (hL : ∀ y, -log (Q y) = L+c y) : + let F := bill tag c q P X U + P.H (fun a => tot tag (X a)) + (P.send X).kl (pack fun _ => q) ≤ 3*F+2*L := by + intro F + let S (a) := tot tag (X a) + let Z := ans tag X U + have hEnt : P.H U ≤ L + P.Ex (fun a => c (U a)) := by + have H := entropy_le_cross_aux (P.send U) Q Q.nonneg Q.total.le fun x _ => hQ x + refine H.trans (le_of_eq ?_) + unfold crossE + simp_rw [hL,Ex_send,Ex_add,Ex_const] + have hBill : P.Ex (fun a => c (U a)) ≤ F := + ((le_add_of_nonneg_left (kl_nonneg (covered_pack hcov))).trans + (kl_le_bill hcov tag c hc _)) + have hH := bill_with_entropy hcov tag c U + change F = _+(P.H Z-_)+_ at hH + have HI : P.H S ≤ P.H Z + P.H U := by + have H1 : P.H S ≤ P.H (fun a => (U a,Z a)) := + H_le_of_determined _ fun a b hh => by + have hi := Prod.mk.inj hh + change U a=U b ∧ S a+U a=S b+U b at hi + exact add_right_cancel (b:=U b) (by rw [← hi.2,hi.1]) + have H2 := Hcond_le_H P U Z + rw [H_pair_chain] at H1; linarith + have hC := Hcond_le_H P U X + change P.H S + _ ≤ _ + linarith [P.Ex_nonneg (fun a => hc (U a))] + + +structure Starting (tag : A → G) (c : G → ℝ) (N m : ℕ) (E ε : ℝ) where + q : Law A + P : Law (WordSample N A G) + sym : exch P + marg (i : Fin N) : P.send (fun a => a.1 i) = q + bound : bill tag c q P Prod.fst Prod.snd ≤ E + within : m ≤ N + near : tv (P.send (prefixB Prod.fst m within)) (iid q m) ≤ ε + +namespace Starting +variable {tag : A → G} {c : G → ℝ} {N m : ℕ} {E ε : ℝ} (s : Starting tag c N m E ε) + +lemma cover : covered s.P Prod.fst s.q := kl_covered _ _ _ s.marg + +lemma exact_bound (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) + (L : ℝ) (hL : ∀ y, -log (Q y)=L+c y) + (n : ℕ) (hn : n ≤ N) : + let X := prefixB (α:=A) (Ω:=WordSample N A G) Prod.fst n hn + s.P.H (fun a => tot tag (X a)) + (s.P.send X).kl (iid s.q n) ≤ 3*E+2*L := by + intro X + have h1 := bill_subfamily tag c s.q s.P Prod.fst Prod.snd s.cover + (offset 0 n (show 0+n ≤ N by omega)) (offset_injective _ _ _) + change bill tag c s.q s.P X Prod.snd ≤ _ at h1 + refine (sum_entropy s.P X Prod.snd tag c hc s.q (covered_subfamily s.cover _) Q hQ L hL).trans ?_ + linarith [s.bound] + +lemma I_upper (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) + (L : ℝ) (hL : ∀ y,-log (Q y)=L+c y) (n : ℕ) (hn : n ≤ N) : + fundI tag (initial s.q) n ≤ 3*E+2*L := by + have h := s.exact_bound hc Q hQ L hL n hn + let X := prefixB (α:=A) (Ω:=WordSample N A G) Prod.fst n hn + have heq : hasRows s.q s.P X := fun i => s.marg (offset 0 n _ i) + refine (I_initial_le tag s.q n s.P X heq).trans ?_ + rw [add_comm] + exact h + +lemma near_small (n : ℕ) (hn : n ≤ m) : + tv (s.P.send (prefixB Prod.fst n (hn.trans s.within))) (iid s.q n) ≤ ε := by + let X : Letters m A → Letters n A := fun x i => x (offset 0 n (by omega : 0+n ≤ m) i) + have h := tv_data_processing (s.P.send (prefixB Prod.fst m s.within)) (iid s.q m) X + rw [send_send] at h + have hh : (iid s.q m).send X = iid s.q n := + sendPack_embedding _ _ (offset_injective _ _ _) + rw [hh] at h + apply le_trans _ s.near + have he : (fun a : WordSample N A G => X (prefixB Prod.fst m s.within a)) = prefixB Prod.fst n (hn.trans s.within) := by + funext x ⟨i,hi⟩; simp only [X,prefixB,readBlock,keep,offset,zero_add] + rwa [he] at h +lemma G_upper (hc : ∀ x, 0 ≤ c x) (Q : Law G) (hQ : ∀ y, 0 < Q y) + (L : ℝ) (hL : ∀ y,-log (Q y)=L+c y) (n : ℕ) (hn : n ≤ m) : + fundG tag (initial s.q) n ≤ 3*E+2*L+log 2 + ε*lnS (Fin n) tag := by + rw [G_initial] + let X := prefixB (α:=A) (Ω:= WordSample N A G) Prod.fst n (hn.trans s.within) + let Y := tot tag (ι:=Fin n) + have H1 : tv ((iid s.q n).send Y) ((s.P.send X).send Y) ≤ ε := by + rw [tv_comm] + exact (tv_data_processing _ _ Y).trans (s.near_small n hn) + have HT := entropy_le_of_tv ((iid s.q n).send Y) ((s.P.send X).send Y) + (sums (Fin n) tag) fun x hx => by + obtain ⟨a,rfl,_⟩ := (send_pos_iff _ _ _).mp hx + exact Finset.mem_image_of_mem _ (Finset.mem_univ _) + change _ ≤ (s.P.send X).H Y + (_ + _ * lnS (Fin n) tag) at HT + have hk : dominated (s.P.send X) (iid s.q n) := covered_pack + (covered_subfamily s.cover _) + have he := s.exact_bound hc Q hQ L hL n (hn.trans s.within) + change s.P.H _+kl (s.P.send X) _ ≤ _ at he + apply HT.trans + rw [H_send] + nlinarith [kl_nonneg hk,lnS_nonneg (Fin n) tag] + +lemma block_near (n : ℕ) (hn : n ≤ m) (g : Fin n → Fin N) (hg : Injective g) : + tv (s.P.send (keep Prod.fst g)) (iid s.q n) ≤ ε := by + let f := offset 0 n (by have := s.within; omega : 0+n ≤ N) + have Hf : Injective f := offset_injective _ _ _ + obtain ⟨e,he⟩ := Equiv.Perm.exists_extending_pair _ _ Hf hg + have hh := + congrArg (fun R : Law (WordSample N A G) => R.send (keep Prod.fst f)) (s.sym e) + rw [send_send] at hh + have heq : (fun x : WordSample N A G => keep Prod.fst f (appPerm e x)) = + keep Prod.fst g := by + funext x i; dsimp only [keep,appPerm]; rw [he i] + rw [heq] at hh + rw [hh] + exact s.near_small n hn + +lemma block_card (n r : ℕ) : + lnS (Fin r) (tot tag (ι:=Fin n)) = lnS (Fin (r*n)) tag := by + let e := finProdFinEquiv (m:=r) (n:=n) + have hh (x : Fin (r*n) → A) : + tot tag x = tot (tot tag (ι:=Fin n)) (fun i : Fin r => fun j => x (e (i,j))) := by + unfold tot + rw [← Fintype.sum_equiv e (fun i => tag (x (e i))) (fun i => tag (x i)) (fun _ => rfl), + Fintype.sum_prod_type] + have H : sums (Fin (r*n)) tag = sums (Fin r) (tot tag (ι:=Fin n)) := by + ext a + simp only [sums,Finset.mem_image,Finset.mem_univ,true_and] + constructor + · rintro ⟨x,rfl⟩ + exact ⟨_,(hh x).symm⟩ + · rintro ⟨x,rfl⟩ + refine ⟨(fun i => x (e.symm i).1 (e.symm i).2),?_⟩ + rw [hh]; simp + simp only [lnS,H] + +lemma K_upper (hc : ∀ x, 0 ≤ c x) (hc0 : c 0 = 0) + (n r : ℕ) (hr : 0 < r) (hn : n ≤ m) (hn' : 2*r*n ≤ N) : + fundK tag c (initial s.q) n r ≤ E + log (Fintype.card (Ticket r (2*r))) + + (r + 2*ε*lnS (Fin (r*n)) tag) := by + let g : Fin (2*r) × Fin n → Fin N := offset 0 ((2*r)*n) (by omega) ∘ finProdFinEquiv + have hg : Injective g := (offset_injective _ _ _).comp finProdFinEquiv.injective + let X (x : WordSample N A G) (i : Fin (2*r)) (j : Fin n) := x.1 (g (i,j)) + let q := iid s.q n + have Hc : covered s.P X q := by + intro x hx i; exact (pack_pos _ _).mpr (fun j => s.cover x hx (g (i,j))) + have Hm (i : Fin (2*r)) : tv (s.P.send (fun x => X x i)) q ≤ ε := + s.block_near _ hn (fun j => g (i,j)) (fun _ _ h => congrArg Prod.snd (hg h)) + obtain ⟨Q,hQ,hb⟩ := thinned_bill (tot tag (ι:=Fin n)) c hc hc0 r (2*r) hr rfl + s.P X Prod.snd q Hc ε Hm + let X' := keep (Prod.fst : WordSample N A G → _) g + have heq : bill (tot tag (ι:=Fin n)) c q s.P X Prod.snd = + bill tag c s.q s.P X' Prod.snd := by + let Y := ans tag X' Prod.snd + have hy : ans (tot tag (ι:=Fin n)) X Prod.snd = Y := by + ext x; unfold Y ans tot + rw [Fintype.sum_prod_type]; rfl + unfold bill + rw [hy, Fintype.sum_prod_type] + have h2 : s.P.Hcond X Y = s.P.Hcond X' Y := by + unfold Hcond + congr 1 + refine H_partition _ fun a b => ?_ + simp only [Prod.mk.injEq,funext_iff,Prod.forall,X,X',keep] + have h1 (i : Fin (2*r)) : s.P.Ex (fun a => -log (q (X a i))) = + ∑ j, s.P.Ex (fun a => -log (s.q (X' a (i,j)))) := by + rw [← Ex_finset_sum] + apply Ex_congr_mem + intro x hx + change -log (∏ j : Fin n, s.q (X x i j)) = _ + rw [log_prod,Finset.sum_neg_distrib] + · rfl + · exact fun j _ => (s.cover x hx _).ne' + rw [h2] + simp_rw [h1] + rfl + have hh := bill_subfamily tag c s.q s.P Prod.fst Prod.snd s.cover g hg + change bill tag c s.q s.P X' Prod.snd ≤ _ at hh + rw [heq,block_card] at hb + refine le_trans (K_initial_le tag c s.q n r Q Prod.fst Prod.snd hQ) ?_ + exact le_trans hb (add_le_add_left (add_le_add_left (hh.trans s.bound) _) _) + +end Starting +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Observe.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Observe.lean index 82cad15febe512d789da986a6e124957a8b24e46..1a59bc9fccf4565a5a95663da7657b6ca7c02816 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Observe.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Observe.lean @@ -1,222 +1,222 @@ -import OAI.Analysis.FourierLLogL.Compress.Context -/-! # The block observation -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A V G E Ω Ω' : Type} [Fintype A] [Fintype G] [AddCommGroup G] - [Fintype Ω] [Fintype Ω'] -set_option linter.unusedSectionVars false - - -def sumLaw (tag : A → G) (P : Labelled A) (m : ℕ) := - (lblP P m).send (fun a => (lblSum tag P m a,lblCtx P m a)) - -lemma full_pair (p : Law (Ω×Ω')) : - p.Hcond (fun x => x) Prod.snd = p.Hcond Prod.fst Prod.snd := by - unfold Hcond - congr 1 - exact H_partition _ (by simp) - -lemma G_as_sum (tag : A → G) (P : Labelled A) (m) : - fundG tag P m = (sumLaw tag P m).Hcond (fun x => x) Prod.snd := by - rw [full_pair] - unfold sumLaw fundG; rw [Hcond_send] - -lemma I_single (tag : A → G) (hi : Injective tag) (P : Labelled A) : - fundG tag P 1 = P.law.Hcond (fun x => x) Prod.snd := - (fundG_one _ hi P).trans (full_pair P.law).symm - -lemma K_as_sum (tag : A → G) (c : G → ℝ) (P : Labelled A) (m n) : - fundK tag c P m n = opt n (sumLaw tag P m) Prod.fst Prod.snd (fun x => x) c := - (opt_proj Prod.fst Prod.snd (fun x => x) c (lblP P m) (fun a => (lblSum tag P m a,lblCtx P m a)) n).symm - -def sumPlus (tag : A → G) (P : Labelled A) (m : ℕ) - (x : (A × P.obs) × (G × Letters m P.obs)) : G × Letters (m+1) P.obs := - (tag x.1.1+x.2.1, appendOne (x.1.2,x.2.2)) - -def rowPlus (tag : A → G) (P : Labelled A) (m : ℕ) - (x : (A × P.obs) × (G × Letters m P.obs)) := (x.1.1,sumPlus tag P m x) - -lemma sumLaw_succ (tag : A → G) (P : Labelled A) (m) : - sumLaw tag P (m+1) = (P.law.tensorP (sumLaw tag P m)).send (sumPlus tag P m) := by - unfold sumLaw - rw [show lblP P (m+1) = _ from iid_succ P.law m,send_send] - have hQ := send_tensorP P.law (lblP P m) (fun x => x) - (fun a => (lblSum tag P m a,lblCtx P m a)) - rw [send_id] at hQ - rw [← hQ,send_send] - simp_rw [lbl_succ_sum,lbl_succ_ctx] - rfl - -abbrev observe (tag : A → G) (P : Labelled A) (m : ℕ) : Labelled A := - { obs := G × Letters (m+1) P.obs - law := (P.law.tensorP (sumLaw tag P m)).send (rowPlus tag P m) } - -lemma observe_refines (tag : A → G) (P : Labelled A) (m : ℕ) : - refines (observe tag P m) P := by - refine ⟨fun x => x.2 0,?_⟩ - dsimp only [observe] - rw [send_send] - have he : (fun x : (A × P.obs) × (G × Letters m P.obs) => - ((rowPlus tag P m x).1,(rowPlus tag P m x).2.2 0)) = Prod.fst := by - funext x; rfl - rw [he, send_fst_tensorP] - -lemma rowPlus_inj (tag : A → G) (P : Labelled A) (m : ℕ) : - Injective (rowPlus tag P m) := by - rintro ⟨⟨a,b⟩,⟨s,w⟩⟩ ⟨⟨a',b'⟩,⟨s',w'⟩⟩ he - simp only [rowPlus,sumPlus,Prod.mk.injEq,appendOne,cons_part] at * - grind - -lemma observe_row_ent (tag : A → G) (P : Labelled A) (m : ℕ) : - (sumLaw tag P (m+1)).Hcond (fun x => x) Prod.snd + - (observe tag P m).law.Hcond (fun x => x) Prod.snd = - P.law.Hcond (fun x => x) Prod.snd + (sumLaw tag P m).Hcond (fun x => x) Prod.snd := by - rw [sumLaw_succ] - dsimp only [observe] - let R := P.law - let Q := sumLaw tag P m - let Z := R.tensorP Q - let Y := sumPlus tag P m - let X := rowPlus tag P m - have hf {α β : Type} [Fintype α] [Fintype β] (P : Law (α × β)) : - P.Hcond (fun x => x) Prod.snd = P.entropy - P.H Prod.snd := by - have h := Hcond_function P (fun x => x) Prod.snd - rw [H_self] at h; exact h - simp_rw [hf] - have hx : Z.H X = Z.entropy := - (H_partition Z (X:=X) (Y:=fun x => x) (fun a b => (rowPlus_inj tag P m).eq_iff)).trans (H_self Z) - have hz : (Z.send Y).H Prod.snd = P.law.H Prod.snd + Q.H Prod.snd := by - rw [H_send]; refine Eq.trans ?_ (H_tensorP R Q Prod.snd Prod.snd) - apply H_partition - intro x y - simp only [Y,sumPlus,appendOne,cons_part,Prod.mk.injEq] - change ((Z.send Y).entropy - (Z.send Y).H Prod.snd)+((Z.send X).entropy - (Z.send X).H Prod.snd) = _ - rw [hz,H_send] - change (Z.H Y- (R.H Prod.snd+Q.H Prod.snd))+(Z.H X-Z.H Y) = _ - rw [hx,show Z.entropy = _ from entropy_tensorP R Q] - change _ = (R.entropy-_) + (Q.entropy-_) - ring - -lemma observe_cost (tag : A → G) (hi : Injective tag) (P : Labelled A) (m : ℕ) : - fundG tag P 1 - fundG tag (observe tag P m) 1 = - fundG tag P (m+1) - fundG tag P m := by - rw [I_single tag hi,I_single tag hi,G_as_sum tag P (m+1),G_as_sum tag P m] - linarith [observe_row_ent tag P m] - - -private lemma observe_cond (tag : A → G) (P : Labelled A) (m r : ℕ) - (R : Law Ω) (S : Law Ω') (X : Ω → Letters r (A×P.obs)) - (Y : Ω' → Letters r (G × Letters m P.obs)) (g : Ω' → G) : - let V := R.tensorP S - let tag' := tag ∘ Prod.fst (β := P.obs) - let Y' (x : Ω × Ω') (i : Fin r) := sumPlus tag P m (X x.1 i, Y x.2 i) - let X' (x : Ω × Ω') (i : Fin r) := rowPlus tag P m (X x.1 i, Y x.2 i) - R.Hcond X (fun x => (ans tag' X (fun _ => 0) x, ctxt Prod.snd X x)) + - S.Hcond Y (fun x => (ans Prod.fst Y g x, ctxt Prod.snd Y x)) ≤ - V.Hcond Y' (fun x => (ans Prod.fst Y' (fun x => g x.2) x, ctxt Prod.snd Y' x)) + - V.Hcond X' (fun x => (ans (tag ∘ Prod.fst) X' (fun _ => 0) x, ctxt Prod.snd X' x)) := by - intro V tag' Y' X' - let B (x) := (ans tag' X (fun _ => 0) x, ctxt Prod.snd X x) - let C (x) := (ans Prod.fst Y g x, ctxt Prod.snd Y x) - let B' (x) := (ans Prod.fst Y' (fun x => g x.2) x, ctxt Prod.snd Y' x) - let C' (x) := (ans (tag ∘ Prod.fst) X' (fun _ => 0) x, ctxt Prod.snd X' x) - let W (x : Ω × Ω') := (B x.1,C x.2) - have hb1 (x : Ω × Ω') : (B' x).1 = (B x.1).1 + (C x.2).1 := by - unfold B' B C Y' - unfold ans tot tag' sumPlus - simp only [Finset.sum_add_distrib,comp_apply,add_zero,add_assoc] - have hc1 (x : Ω × Ω') : (C' x).1 = (B x.1).1 := rfl - have H1 : V.Hcond Y' W ≤ V.Hcond Y' B' := by - apply Hcond_antitone - intro x y hh - have h1 : B x.1 = B y.1 := congrArg (fun v => v.1) hh - have h2 : C x.2 = C y.2 := congrArg Prod.snd hh - apply Prod.ext - · rw [hb1,hb1,h1,h2] - · funext i - have h3 := congrFun (congrArg Prod.snd h1) i - have h4 := congrFun (congrArg Prod.snd h2) i - change appendOne (_,_) = appendOne (_,_) - exact congrArg appendOne (Prod.ext h3 h4) - have H2 : V.Hcond X' (fun x => (Y' x,W x)) ≤ V.Hcond X' C' := by - refine Hcond_antitone _ _ fun x y hh => ?_ - have h1 : W x=W y := congrArg Prod.snd hh - have h2 : Y' x=Y' y := congrArg Prod.fst hh - apply Prod.ext - · rw [hc1,hc1,show B x.1 = B y.1 from congrArg Prod.fst h1] - · exact h2 - have Hi : V.Hcond (fun x => (X x.1,Y x.2)) W = V.Hcond (fun x => (X' x,Y' x)) W := by - unfold Hcond - congr 1 - apply H_partition - intro a b - have HH : (X a.1,Y a.2)=(X b.1,Y b.2) ↔ (X' a,Y' a)=(X' b,Y' b) := by - simp only [X',Y',funext_iff,Prod.mk.injEq,(rowPlus_inj tag P m).eq_iff,forall_and] - constructor - · intro ⟨hh,h⟩; simp [hh,h] - · tauto - simp only [Prod.mk.injEq,HH] - rw [Hcond_chain V X' Y' W] at Hi - have h := Hcond_tensor R S X Y B C - change V.Hcond (fun x => (X x.1,Y x.2)) W = _ at h - change R.Hcond X B+S.Hcond Y C ≤ V.Hcond Y' B'+V.Hcond X' C' - linarith - -lemma observe_save (tag : A → G) (c : G → ℝ) (P : Labelled A) (m r : ℕ) : - fundK tag c P (m+1) r + fundI tag (observe tag P m) r ≤ - fundI tag P r + fundK tag c P m r := by - classical - rw [K_as_sum,K_as_sum] - let tag' : A × P.obs → G := tag ∘ Prod.fst - let u0 (_ : Unit) := (0:G) - let c0 (_ : Unit) := (0:ℝ) - obtain ⟨R,hR,eR⟩ := (opt_spec r P.law tag' Prod.snd u0 c0).1 - let Q := sumLaw tag P m - obtain ⟨S,hS,eS⟩ := (opt_spec r Q Prod.fst Prod.snd (fun x => x) c).1 - let D := WordSample r (A × P.obs) Unit - let E := WordSample r (G × Letters m P.obs) G - let V := R.tensorP S - let Y (x : D×E) (i : Fin r) := sumPlus tag P m (x.1.1 i, x.2.1 i) - let X (x : D×E) (i : Fin r) := rowPlus tag P m (x.1.1 i, x.2.1 i) - let Ue (x : D×E) := x.2.2 - have Hlaw (i : Fin r) : - V.send (fun x : D×E => (x.1.1 i,x.2.1 i)) = P.law.tensorP Q := by - rw [show (fun x : D×E => (x.1.1 i,x.2.1 i)) = _ from rfl] - have HH := send_tensorP R S (readP Prod.fst i) (readP Prod.fst i) - rw [hR i,hS i] at HH - exact HH - have hY : hasRows (sumLaw tag P (m+1)) V Y := by - intro i - rw [sumLaw_succ,show P.law.tensorP (sumLaw tag P m) = _ from (Hlaw i).symm, - send_send]; rfl - have hX : hasRows (observe tag P m).law V X := by - intro i; dsimp only [observe] - rw [show P.law.tensorP (sumLaw tag P m) = _ from (Hlaw i).symm,send_send]; rfl - let tag'' : (A × (observe tag P m).obs) → G := tag ∘ Prod.fst - refine (add_le_add (opt_le_rent _ _ _ c _ V Y Ue hY) - (opt_le_rent tag'' Prod.snd u0 c0 _ V X (fun _ => ()) hX)).trans ?_ - change _ ≤ opt _ _ tag' _ u0 c0 + opt _ Q _ _ _ _ - rw [← eR,← eS] - dsimp only [] - unfold rent - rw [rent_first hX,rent_first hY,rent_first hS,rent_first hR] - have H := observe_cond tag P m r R S Prod.fst Prod.fst Prod.snd - have H0 := observe_row_ent tag P m - - have heq : V.Ex (fun a => c (Ue a)) = S.Ex (fun a => c a.2) := by - dsimp only [V,Ue] - rw [Ex_tensorP]; exact Ex_const R (S.Ex (fun a => c a.2)) - dsimp only [u0,c0,X,Y,Ue,tag'',tag',V,Q,id_eq,comp_apply] at * - simp only [Ex_const] - have hmul := congrArg (fun t : ℝ => (Fintype.card (Fin r):ℝ)*t) H0 - linarith - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Context +/-! # The block observation -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A V G E Ω Ω' : Type} [Fintype A] [Fintype G] [AddCommGroup G] + [Fintype Ω] [Fintype Ω'] +set_option linter.unusedSectionVars false + + +def sumLaw (tag : A → G) (P : Labelled A) (m : ℕ) := + (lblP P m).send (fun a => (lblSum tag P m a,lblCtx P m a)) + +lemma full_pair (p : Law (Ω×Ω')) : + p.Hcond (fun x => x) Prod.snd = p.Hcond Prod.fst Prod.snd := by + unfold Hcond + congr 1 + exact H_partition _ (by simp) + +lemma G_as_sum (tag : A → G) (P : Labelled A) (m) : + fundG tag P m = (sumLaw tag P m).Hcond (fun x => x) Prod.snd := by + rw [full_pair] + unfold sumLaw fundG; rw [Hcond_send] + +lemma I_single (tag : A → G) (hi : Injective tag) (P : Labelled A) : + fundG tag P 1 = P.law.Hcond (fun x => x) Prod.snd := + (fundG_one _ hi P).trans (full_pair P.law).symm + +lemma K_as_sum (tag : A → G) (c : G → ℝ) (P : Labelled A) (m n) : + fundK tag c P m n = opt n (sumLaw tag P m) Prod.fst Prod.snd (fun x => x) c := + (opt_proj Prod.fst Prod.snd (fun x => x) c (lblP P m) (fun a => (lblSum tag P m a,lblCtx P m a)) n).symm + +def sumPlus (tag : A → G) (P : Labelled A) (m : ℕ) + (x : (A × P.obs) × (G × Letters m P.obs)) : G × Letters (m+1) P.obs := + (tag x.1.1+x.2.1, appendOne (x.1.2,x.2.2)) + +def rowPlus (tag : A → G) (P : Labelled A) (m : ℕ) + (x : (A × P.obs) × (G × Letters m P.obs)) := (x.1.1,sumPlus tag P m x) + +lemma sumLaw_succ (tag : A → G) (P : Labelled A) (m) : + sumLaw tag P (m+1) = (P.law.tensorP (sumLaw tag P m)).send (sumPlus tag P m) := by + unfold sumLaw + rw [show lblP P (m+1) = _ from iid_succ P.law m,send_send] + have hQ := send_tensorP P.law (lblP P m) (fun x => x) + (fun a => (lblSum tag P m a,lblCtx P m a)) + rw [send_id] at hQ + rw [← hQ,send_send] + simp_rw [lbl_succ_sum,lbl_succ_ctx] + rfl + +abbrev observe (tag : A → G) (P : Labelled A) (m : ℕ) : Labelled A := + { obs := G × Letters (m+1) P.obs + law := (P.law.tensorP (sumLaw tag P m)).send (rowPlus tag P m) } + +lemma observe_refines (tag : A → G) (P : Labelled A) (m : ℕ) : + refines (observe tag P m) P := by + refine ⟨fun x => x.2 0,?_⟩ + dsimp only [observe] + rw [send_send] + have he : (fun x : (A × P.obs) × (G × Letters m P.obs) => + ((rowPlus tag P m x).1,(rowPlus tag P m x).2.2 0)) = Prod.fst := by + funext x; rfl + rw [he, send_fst_tensorP] + +lemma rowPlus_inj (tag : A → G) (P : Labelled A) (m : ℕ) : + Injective (rowPlus tag P m) := by + rintro ⟨⟨a,b⟩,⟨s,w⟩⟩ ⟨⟨a',b'⟩,⟨s',w'⟩⟩ he + simp only [rowPlus,sumPlus,Prod.mk.injEq,appendOne,cons_part] at * + grind + +lemma observe_row_ent (tag : A → G) (P : Labelled A) (m : ℕ) : + (sumLaw tag P (m+1)).Hcond (fun x => x) Prod.snd + + (observe tag P m).law.Hcond (fun x => x) Prod.snd = + P.law.Hcond (fun x => x) Prod.snd + (sumLaw tag P m).Hcond (fun x => x) Prod.snd := by + rw [sumLaw_succ] + dsimp only [observe] + let R := P.law + let Q := sumLaw tag P m + let Z := R.tensorP Q + let Y := sumPlus tag P m + let X := rowPlus tag P m + have hf {α β : Type} [Fintype α] [Fintype β] (P : Law (α × β)) : + P.Hcond (fun x => x) Prod.snd = P.entropy - P.H Prod.snd := by + have h := Hcond_function P (fun x => x) Prod.snd + rw [H_self] at h; exact h + simp_rw [hf] + have hx : Z.H X = Z.entropy := + (H_partition Z (X:=X) (Y:=fun x => x) (fun a b => (rowPlus_inj tag P m).eq_iff)).trans (H_self Z) + have hz : (Z.send Y).H Prod.snd = P.law.H Prod.snd + Q.H Prod.snd := by + rw [H_send]; refine Eq.trans ?_ (H_tensorP R Q Prod.snd Prod.snd) + apply H_partition + intro x y + simp only [Y,sumPlus,appendOne,cons_part,Prod.mk.injEq] + change ((Z.send Y).entropy - (Z.send Y).H Prod.snd)+((Z.send X).entropy - (Z.send X).H Prod.snd) = _ + rw [hz,H_send] + change (Z.H Y- (R.H Prod.snd+Q.H Prod.snd))+(Z.H X-Z.H Y) = _ + rw [hx,show Z.entropy = _ from entropy_tensorP R Q] + change _ = (R.entropy-_) + (Q.entropy-_) + ring + +lemma observe_cost (tag : A → G) (hi : Injective tag) (P : Labelled A) (m : ℕ) : + fundG tag P 1 - fundG tag (observe tag P m) 1 = + fundG tag P (m+1) - fundG tag P m := by + rw [I_single tag hi,I_single tag hi,G_as_sum tag P (m+1),G_as_sum tag P m] + linarith [observe_row_ent tag P m] + + +private lemma observe_cond (tag : A → G) (P : Labelled A) (m r : ℕ) + (R : Law Ω) (S : Law Ω') (X : Ω → Letters r (A×P.obs)) + (Y : Ω' → Letters r (G × Letters m P.obs)) (g : Ω' → G) : + let V := R.tensorP S + let tag' := tag ∘ Prod.fst (β := P.obs) + let Y' (x : Ω × Ω') (i : Fin r) := sumPlus tag P m (X x.1 i, Y x.2 i) + let X' (x : Ω × Ω') (i : Fin r) := rowPlus tag P m (X x.1 i, Y x.2 i) + R.Hcond X (fun x => (ans tag' X (fun _ => 0) x, ctxt Prod.snd X x)) + + S.Hcond Y (fun x => (ans Prod.fst Y g x, ctxt Prod.snd Y x)) ≤ + V.Hcond Y' (fun x => (ans Prod.fst Y' (fun x => g x.2) x, ctxt Prod.snd Y' x)) + + V.Hcond X' (fun x => (ans (tag ∘ Prod.fst) X' (fun _ => 0) x, ctxt Prod.snd X' x)) := by + intro V tag' Y' X' + let B (x) := (ans tag' X (fun _ => 0) x, ctxt Prod.snd X x) + let C (x) := (ans Prod.fst Y g x, ctxt Prod.snd Y x) + let B' (x) := (ans Prod.fst Y' (fun x => g x.2) x, ctxt Prod.snd Y' x) + let C' (x) := (ans (tag ∘ Prod.fst) X' (fun _ => 0) x, ctxt Prod.snd X' x) + let W (x : Ω × Ω') := (B x.1,C x.2) + have hb1 (x : Ω × Ω') : (B' x).1 = (B x.1).1 + (C x.2).1 := by + unfold B' B C Y' + unfold ans tot tag' sumPlus + simp only [Finset.sum_add_distrib,comp_apply,add_zero,add_assoc] + have hc1 (x : Ω × Ω') : (C' x).1 = (B x.1).1 := rfl + have H1 : V.Hcond Y' W ≤ V.Hcond Y' B' := by + apply Hcond_antitone + intro x y hh + have h1 : B x.1 = B y.1 := congrArg (fun v => v.1) hh + have h2 : C x.2 = C y.2 := congrArg Prod.snd hh + apply Prod.ext + · rw [hb1,hb1,h1,h2] + · funext i + have h3 := congrFun (congrArg Prod.snd h1) i + have h4 := congrFun (congrArg Prod.snd h2) i + change appendOne (_,_) = appendOne (_,_) + exact congrArg appendOne (Prod.ext h3 h4) + have H2 : V.Hcond X' (fun x => (Y' x,W x)) ≤ V.Hcond X' C' := by + refine Hcond_antitone _ _ fun x y hh => ?_ + have h1 : W x=W y := congrArg Prod.snd hh + have h2 : Y' x=Y' y := congrArg Prod.fst hh + apply Prod.ext + · rw [hc1,hc1,show B x.1 = B y.1 from congrArg Prod.fst h1] + · exact h2 + have Hi : V.Hcond (fun x => (X x.1,Y x.2)) W = V.Hcond (fun x => (X' x,Y' x)) W := by + unfold Hcond + congr 1 + apply H_partition + intro a b + have HH : (X a.1,Y a.2)=(X b.1,Y b.2) ↔ (X' a,Y' a)=(X' b,Y' b) := by + simp only [X',Y',funext_iff,Prod.mk.injEq,(rowPlus_inj tag P m).eq_iff,forall_and] + constructor + · intro ⟨hh,h⟩; simp [hh,h] + · tauto + simp only [Prod.mk.injEq,HH] + rw [Hcond_chain V X' Y' W] at Hi + have h := Hcond_tensor R S X Y B C + change V.Hcond (fun x => (X x.1,Y x.2)) W = _ at h + change R.Hcond X B+S.Hcond Y C ≤ V.Hcond Y' B'+V.Hcond X' C' + linarith + +lemma observe_save (tag : A → G) (c : G → ℝ) (P : Labelled A) (m r : ℕ) : + fundK tag c P (m+1) r + fundI tag (observe tag P m) r ≤ + fundI tag P r + fundK tag c P m r := by + classical + rw [K_as_sum,K_as_sum] + let tag' : A × P.obs → G := tag ∘ Prod.fst + let u0 (_ : Unit) := (0:G) + let c0 (_ : Unit) := (0:ℝ) + obtain ⟨R,hR,eR⟩ := (opt_spec r P.law tag' Prod.snd u0 c0).1 + let Q := sumLaw tag P m + obtain ⟨S,hS,eS⟩ := (opt_spec r Q Prod.fst Prod.snd (fun x => x) c).1 + let D := WordSample r (A × P.obs) Unit + let E := WordSample r (G × Letters m P.obs) G + let V := R.tensorP S + let Y (x : D×E) (i : Fin r) := sumPlus tag P m (x.1.1 i, x.2.1 i) + let X (x : D×E) (i : Fin r) := rowPlus tag P m (x.1.1 i, x.2.1 i) + let Ue (x : D×E) := x.2.2 + have Hlaw (i : Fin r) : + V.send (fun x : D×E => (x.1.1 i,x.2.1 i)) = P.law.tensorP Q := by + rw [show (fun x : D×E => (x.1.1 i,x.2.1 i)) = _ from rfl] + have HH := send_tensorP R S (readP Prod.fst i) (readP Prod.fst i) + rw [hR i,hS i] at HH + exact HH + have hY : hasRows (sumLaw tag P (m+1)) V Y := by + intro i + rw [sumLaw_succ,show P.law.tensorP (sumLaw tag P m) = _ from (Hlaw i).symm, + send_send]; rfl + have hX : hasRows (observe tag P m).law V X := by + intro i; dsimp only [observe] + rw [show P.law.tensorP (sumLaw tag P m) = _ from (Hlaw i).symm,send_send]; rfl + let tag'' : (A × (observe tag P m).obs) → G := tag ∘ Prod.fst + refine (add_le_add (opt_le_rent _ _ _ c _ V Y Ue hY) + (opt_le_rent tag'' Prod.snd u0 c0 _ V X (fun _ => ()) hX)).trans ?_ + change _ ≤ opt _ _ tag' _ u0 c0 + opt _ Q _ _ _ _ + rw [← eR,← eS] + dsimp only [] + unfold rent + rw [rent_first hX,rent_first hY,rent_first hS,rent_first hR] + have H := observe_cond tag P m r R S Prod.fst Prod.fst Prod.snd + have H0 := observe_row_ent tag P m + + have heq : V.Ex (fun a => c (Ue a)) = S.Ex (fun a => c a.2) := by + dsimp only [V,Ue] + rw [Ex_tensorP]; exact Ex_const R (S.Ex (fun a => c a.2)) + dsimp only [u0,c0,X,Y,Ue,tag'',tag',V,Q,id_eq,comp_apply] at * + simp only [Ex_const] + have hmul := congrArg (fun t : ℝ => (Fintype.card (Fin r):ℝ)*t) H0 + linarith + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Params.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Params.lean index ac0124015d0570fc1349c163e1f4155041a05e55..44ee0722f3115ffd02380910cfca8352898b86ee 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Params.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Params.lean @@ -1,330 +1,330 @@ -import OAI.Analysis.FourierLLogL.Compress.Schedule -/-! # Choice of entropy parameters - -Here j tends to infinity, d=2^j. We take h a power of two, comparable with log j. -/ -open Set Function Filter -open scoped Topology BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress.Params -open Real -def ell (j : ℕ) := Nat.log 2 (Nat.log 2 j / 1000) -def slow (j : ℕ) : ℕ := 2^ell j -def wide (j : ℕ) : ℕ := 2^slow j -def height (j : ℕ) : ℝ := 2^j -def sLen (j : ℕ) := 2^(j-6*ell j) -def sSeg (j : ℕ) := 2^(j-6*ell j-2) -def long (j : ℕ) := height j * (2:ℝ)^wide j - - -lemma pow_pos_nat (n : ℕ) : 0 < 2^n := Nat.pow_pos (by omega) - -lemma slow_pos (j : ℕ) : 0 < slow j := pow_pos_nat _ -lemma ell_tendsto : Tendsto ell atTop atTop := by - refine tendsto_atTop.2 fun l => ?_ - filter_upwards [eventually_ge_atTop (2^(2^l*1000))] with j hj - have h := Nat.le_log_of_pow_le (by omega : 1 < 2) hj - apply Nat.le_log_of_pow_le (by omega : 1 < 2) - change 2^l ≤ Nat.log 2 j / 1000 - omega - -lemma slow_tendsto : Tendsto slow atTop atTop := by - refine tendsto_atTop_mono (f:=ell) ?_ ell_tendsto - intro j - exact (Nat.lt_two_pow_self).le - -lemma wide_tendsto : Tendsto wide atTop atTop := by - refine tendsto_atTop_mono (f:=slow) ?_ slow_tendsto - intro j - exact Nat.le_of_lt (Nat.lt_two_pow_self) - -lemma evt_slow : ∀ᶠ j : ℕ in atTop, - 2^(1000*slow j) ≤ j ∧ j < 2^(3000*slow j) := by - filter_upwards [eventually_ge_atTop (2^1000)] with j hj - have h := Nat.le_log_of_pow_le (by omega : 1 < 2) hj - have hp := (show 0 < Nat.log 2 j/1000 by omega) - have h1 : 2^ell j ≤ Nat.log 2 j/1000 := Nat.pow_log_le_self 2 hp.ne' - have hx := slow_pos j - have h2 := Nat.lt_pow_succ_log_self (by omega : 1 < 2) (Nat.log 2 j/1000) - change _ < 2^(ell j+1) at h2 - rw [pow_succ] at h2 - change _ < slow j *2 at h2 - change slow j ≤ _ at h1 - constructor - · apply le_trans (b:=2^(Nat.log 2 j)) ?_ (Nat.pow_log_le_self 2 (by have := pow_pos_nat 1000; omega)) - apply Nat.pow_le_pow_right (by omega) - omega - · apply lt_of_lt_of_le (Nat.lt_pow_succ_log_self (by omega : 1 < 2) j) - apply Nat.pow_le_pow_right (by omega) - omega - -lemma eventually_pow_small (n : ℕ) (C : ℝ) : - ∀ᶠ j : ℕ in atTop, C*(j:ℝ)^n ≤ 2^j := by - have H := (tendsto_pow_const_div_const_pow_of_one_lt n (show (1:ℝ)<2 by norm_num)).const_mul C - rw [mul_zero] at H - filter_upwards [H.eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num))] with j hj - apply le_of_lt - apply (div_lt_one (by positivity : (0:ℝ)<2^j)).mp - rw [mul_div_assoc] - exact hj - -lemma eventually_pow_small_slow (n : ℕ) (C : ℝ) : - ∀ᶠ j : ℕ in atTop, C*(slow j:ℝ)^n ≤ 2^slow j := - slow_tendsto.eventually (eventually_pow_small n C) - -lemma nat_pow_small_slow (n C : ℕ) : - ∀ᶠ j in atTop, C * (slow j)^n ≤ wide j := by - filter_upwards [eventually_pow_small_slow n C] with j hj - unfold wide; exact_mod_cast hj - - -def base (j : ℕ) : ℕ := 2^(wide j ^ 2) -def capE (j : ℕ) : ℕ := 2^(64*slow j) -def cap (j : ℕ) : ℕ := 2^capE j - - -lemma wide_le_j : ∀ᶠ j : ℕ in atTop, wide j ≤ j := by - filter_upwards [evt_slow] with j ⟨hj,h'⟩ - exact le_trans (Nat.pow_le_pow_right (by omega) (by omega)) hj -lemma ell_le_h (j : ℕ) : ell j ≤ slow j := Nat.le_of_lt Nat.lt_two_pow_self -lemma h_le_wide (j : ℕ) : slow j ≤ wide j := Nat.le_of_lt Nat.lt_two_pow_self -lemma slow_le_j : ∀ᶠ j in atTop, slow j ≤ j := wide_le_j.mono fun j h => (h_le_wide j).trans h - -lemma base_le_cap (j : ℕ) : base j ≤ cap j := by - unfold base cap capE wide - apply Nat.pow_le_pow_right (by omega) - rw [← pow_mul] - apply Nat.pow_le_pow_right (by omega) - omega - -lemma cap_power : ∀ᶠ j : ℕ in atTop, 64*capE j ≤ j := by - filter_upwards [evt_slow] with j hj - apply le_trans (b:=2^(1000*slow j)) _ hj.1 - unfold capE - rw [show 64 = 2^6 by norm_num] - rw [← pow_add] - apply Nat.pow_le_pow_right (by omega) - have := slow_pos j; omega - -lemma below_base (n : ℕ) (C : ℝ) : - ∀ᶠ j : ℕ in atTop, C * (j:ℝ)^n ≤ base j := by - obtain ⟨k,hk⟩ := exists_nat_gt C - filter_upwards [evt_slow, wide_tendsto.eventually (eventually_ge_atTop (max k (4000*n+2)))] with j ⟨hj,h1⟩ h2 - apply le_trans (b:=(k:ℝ)*(j:ℝ)^n) (mul_le_mul_of_nonneg_right hk.le (by positivity)) - suffices H : k*j^n ≤ base j by exact_mod_cast H - have hk1 : k ≤ wide j := le_trans (le_max_left _ _) h2 - apply le_trans (b:= (2^k)*(2^(3000*slow j))^n) (Nat.mul_le_mul - (Nat.le_of_lt Nat.lt_two_pow_self) (Nat.pow_le_pow_left h1.le n)) - unfold base - rw [← pow_mul, ← pow_add] - apply Nat.pow_le_pow_right (by omega) - have hx : 4000*n+2 ≤ wide j := le_trans (le_max_right _ _) h2 - nlinarith [h_le_wide j] - -lemma t_expr : ∀ᶠ j : ℕ in atTop, - sLen j * (slow j)^6 = 2^j ∧ 4*sSeg j = sLen j := by - filter_upwards [evt_slow] with j ⟨h,_⟩ - have h1 := Nat.lt_two_pow_self (n:=1000*slow j) - have h2 := ell_le_h j - have h3 := slow_pos j - constructor - · unfold sLen slow - rw [← pow_mul,← pow_add] - congr 1; omega - · unfold sSeg sLen - rw [show 4=2^2 by norm_num,← pow_add] - congr 1; omega - - -def nSched (j : ℕ) : ℕ := 2^(16*slow j) -def qSched (j : ℕ) : ℕ := 2*slow j^2 -def mSched (j : ℕ) : ℕ := nSched j * (qSched j*nSched j) - -def searchSched (j : ℕ) (hj : 4 ≤ slow j) : Schedule := by - let h : ℝ := slow j - have hh : 4 ≤ h := by dsimp only [h]; exact_mod_cast hj - have h' : 0 < h := by linarith - let N := nSched j; let q := qSched j - have hn : exp (6*h) ≤ (N:ℝ) := by - have hi : (1:ℝ)/2 ≤ log 2 := by linarith [Real.log_two_gt_d9] - have hN : 0 < (N:ℝ) := by exact_mod_cast pow_pos_nat (16*slow j) - rw [← Real.exp_log hN] - apply Real.exp_monotone - dsimp only [N,nSched]; push_cast; rw [Real.log_pow] - push_cast - unfold h at * - nlinarith - have Hn (k : ℝ) (hk : k ≤ 4) : 1 < N * exp (-k*h) := by - have H : 0 < 6*h+-k*h := by nlinarith - calc - 1 < exp (6*h+-k*h) := Real.one_lt_exp_iff.mpr H - _ = _ := Real.exp_add _ _ - _ ≤ _ := mul_le_mul_of_nonneg_right hn (exp_nonneg _) - have hq : (q:ℝ)=2*h^2 := by dsimp only [q,h,qSched]; push_cast; rfl - refine { N := N, n := N, q := q - N0 := pow_pos_nat _, n0 := pow_pos_nat _, q0 := ?_ - u := 1/h^2, a:= 1/h^3, δ:=exp (-4*h), η:=exp (-2*h) - α:=32/h^2, ε:=exp (-2*h) - u0:=by positivity - a0:=by positivity - d0:=exp_pos _ - heta:=?_ - hn:= Hn 4 le_rfl - hα:=by positivity - hε:=exp_pos _ - hβ:=?_ - hc:=?_ - hq:=?_ - ht:=Hn 2 (by norm_num) } - · have : (0:ℝ) < q := by rw [hq]; positivity - exact_mod_cast this - · rw [← exp_add] - apply le_of_eq; congr 1; field_simp; ring - · field_simp; nlinarith - · rw [show (1/h^3)/(32/h^2) = 1/(32*h) by field_simp] - rw [hq,add_mul,← exp_add] - have he : -2*h+ 2*(1/(32*h))*(2*h^2) ≤ -h := by field_simp; nlinarith - have hx := Real.exp_monotone he - rw [Real.exp_neg] at hx - have HH : (exp h)⁻¹ ≤ 1/2 := by - have := Real.add_one_le_exp h - rw [inv_le_comm₀ (exp_pos h) (by norm_num)] - norm_num; linarith - linarith - · rw [hq]; field_simp; nlinarith - -lemma nSched_scale (j : ℕ) (hj : 4 ≤ slow j) : - (searchSched j hj).size = mSched j := rfl - -lemma searchSched_loss (j : ℕ) (hj : 4 ≤ slow j) : - (searchSched j hj).loss ≤ 66/(slow j:ℝ)^2 := by - let h : ℝ := slow j - have hp : 4 ≤ h := by dsimp only [h]; exact_mod_cast hj - change 2*(32/h^2)+1/h^3*(1+exp (-2*h)) ≤ 66/h^2 - have HH : exp (-2*h) ≤ 1 := Real.exp_le_one_iff.mpr (by linarith) - have : (0:ℝ) < h := by linarith - field_simp; ring_nf at HH ⊢; nlinarith - - -def levelLow (j i : ℕ) : ℕ := wide j ^ 2 + 4*wide j*(mSched j-i) -def levelHigh (j i : ℕ) : ℕ := levelLow j i + 3*wide j -def stepLow (j i : ℕ) : ℕ := 2^levelLow j i -def stepHigh (j i : ℕ) : ℕ := 2^levelHigh j i - -def stepRows (j i : ℕ) : ℕ := 2^(j-(6*ell j+3+levelLow j i)) - -lemma index_budgets : - ∀ᶠ j : ℕ in atTop, ∀ i, levelHigh j i ≤ capE j ∧ 6*ell j+3+levelHigh j i ≤ j := by - filter_upwards [wide_tendsto.eventually (eventually_ge_atTop 8),cap_power] with j h wj - let w := wide j - have hp : 8 ≤ w := h - have hm : nSched j = w^16 := by - unfold nSched w wide - rw [← pow_mul]; congr 1; omega - have hc : capE j = w^64 := by - unfold capE w wide - rw [← pow_mul]; congr 1; omega - have hb : mSched j ≤ 2*w^34 := by - rw [mSched,hm,qSched] - calc - _ ≤ w^16*(2*w^2*w^16) := by - have H : slow j ≤ w := h_le_wide j - gcongr - _ = _ := by ring - have hhE : 6*ell j+3 ≤ capE j := by - have hg : ell j ≤ w := (ell_le_h j).trans (h_le_wide j) - rw [hc] - have hh : w^2 ≤ w^64 := Nat.pow_le_pow_right (by omega) (by omega) - nlinarith - intro i - have hx : levelHigh j i ≤ capE j := by - have H1 : mSched j-i ≤ 2*w^34 := le_trans (by omega) hb - change w^2+4*w*(mSched j-i)+3*w ≤ _ - rw [hc] - have h₁ : w ≤ w^35 := by - exact le_trans (by rw [pow_one] : w ≤ w^1) (Nat.pow_le_pow_right (by omega) (by omega)) - have h₂ : w^2 ≤ w^35 := Nat.pow_le_pow_right (by omega) (by omega) - have h₃ : 16*w^35 ≤ w^64 := by - have H : 16 ≤ w^29 := le_trans (b:=w^2) (by nlinarith) - (Nat.pow_le_pow_right (by omega) (by omega)) - apply le_trans (Nat.mul_le_mul_right (w^35) H) - rw [← pow_add] - calc - _ ≤ w^2+4*w*(2*w^34)+3*w := by gcongr - _ ≤ 16*w^35 := by - rw [show 4*w*(2*w^34)=8*w^35 by ring]; omega - _ ≤ _ := h₃ - exact ⟨hx,by omega⟩ - -lemma array_spec : - ∀ᶠ j : ℕ in atTop, - 3*sSeg j ≤ sLen j ∧ cap j ≤ sSeg j ∧ - cap j ≤ 2^j ∧ - (∀ i, stepLow j i ≤ stepHigh j i ∧ stepHigh j i ≤ cap j ∧ - 2*stepRows j i * stepLow j i = sSeg j ∧ - long j ≤ (stepRows j i:ℝ) * stepHigh j i ∧ - base j ≤ stepLow j i) ∧ - (∀ i k, i < k → k < mSched j → - stepHigh j k ≤ stepLow j i ∧ stepRows j i ≤ stepRows j k) := by - filter_upwards [index_budgets,t_expr,nat_pow_small_slow 1 10, - slow_tendsto.eventually (eventually_ge_atTop 4),cap_power] with j heq ht hw hs hj - have h1 : 6*ell j+2+capE j ≤ j := by - have H0 : 6*ell j ≤ levelHigh j 0 := by - unfold levelHigh levelLow; rw [pow_one] at hw - have H := ell_le_h j; omega - have h2 := heq 0 - have hh := pow_pos_nat (64*slow j); change 0 < capE j at hh - omega - refine ⟨by omega,?_,?_ ,?_,?_⟩ - · apply Nat.pow_le_pow_right (by omega) - omega - · apply Nat.pow_le_pow_right (by omega) - omega - · intro i - have hp := heq i - have hlow : levelLow j i ≤ levelHigh j i := by unfold levelHigh; omega - refine ⟨Nat.pow_le_pow_right (by omega) hlow,Nat.pow_le_pow_right (by omega) hp.1,?_,?_,?_⟩ - · unfold stepLow stepRows sSeg - nth_rw 1 [show 2 = 2^1 by rfl] - rw [← pow_add,← pow_add]; congr 1; omega - · unfold long height stepRows stepHigh - push_cast - rw [← pow_add, ← pow_add] - apply pow_le_pow_right₀ (by norm_num) - unfold levelHigh at * - rw [pow_one] at hw - have hh := ell_le_h j - omega - · unfold base stepLow - apply Nat.pow_le_pow_right (by omega) - unfold levelLow; omega - intro i k hi hk - have h1 : levelHigh j k ≤ levelLow j i := by - unfold levelHigh levelLow - have h2 : mSched j-k+1 ≤ mSched j-i := by omega - nlinarith - constructor - · exact Nat.pow_le_pow_right (by omega) h1 - · unfold stepRows - apply Nat.pow_le_pow_right (by omega) - unfold levelHigh at h1; omega - -lemma big_t_real : - ∀ᶠ j : ℕ in atTop, - height j = 4*(sSeg j:ℝ)*(slow j:ℝ)^6 ∧ - (cap j:ℝ)^4 ≤ height j := by - filter_upwards [t_expr,cap_power] with j h h' - constructor - · have H : 2^j = 4*sSeg j*slow j^6 := by rw [h.2,h.1] - unfold height - exact_mod_cast H - · have H1 : cap j^4 ≤ (2:ℕ)^j := by - rw [cap, ← pow_mul] - exact Nat.pow_le_pow_right (by omega) (by omega) - unfold height - exact_mod_cast H1 - -end FourierLLogL.Compress.Params - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Schedule +/-! # Choice of entropy parameters + +Here j tends to infinity, d=2^j. We take h a power of two, comparable with log j. -/ +open Set Function Filter +open scoped Topology BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress.Params +open Real +def ell (j : ℕ) := Nat.log 2 (Nat.log 2 j / 1000) +def slow (j : ℕ) : ℕ := 2^ell j +def wide (j : ℕ) : ℕ := 2^slow j +def height (j : ℕ) : ℝ := 2^j +def sLen (j : ℕ) := 2^(j-6*ell j) +def sSeg (j : ℕ) := 2^(j-6*ell j-2) +def long (j : ℕ) := height j * (2:ℝ)^wide j + + +lemma pow_pos_nat (n : ℕ) : 0 < 2^n := Nat.pow_pos (by omega) + +lemma slow_pos (j : ℕ) : 0 < slow j := pow_pos_nat _ +lemma ell_tendsto : Tendsto ell atTop atTop := by + refine tendsto_atTop.2 fun l => ?_ + filter_upwards [eventually_ge_atTop (2^(2^l*1000))] with j hj + have h := Nat.le_log_of_pow_le (by omega : 1 < 2) hj + apply Nat.le_log_of_pow_le (by omega : 1 < 2) + change 2^l ≤ Nat.log 2 j / 1000 + omega + +lemma slow_tendsto : Tendsto slow atTop atTop := by + refine tendsto_atTop_mono (f:=ell) ?_ ell_tendsto + intro j + exact (Nat.lt_two_pow_self).le + +lemma wide_tendsto : Tendsto wide atTop atTop := by + refine tendsto_atTop_mono (f:=slow) ?_ slow_tendsto + intro j + exact Nat.le_of_lt (Nat.lt_two_pow_self) + +lemma evt_slow : ∀ᶠ j : ℕ in atTop, + 2^(1000*slow j) ≤ j ∧ j < 2^(3000*slow j) := by + filter_upwards [eventually_ge_atTop (2^1000)] with j hj + have h := Nat.le_log_of_pow_le (by omega : 1 < 2) hj + have hp := (show 0 < Nat.log 2 j/1000 by omega) + have h1 : 2^ell j ≤ Nat.log 2 j/1000 := Nat.pow_log_le_self 2 hp.ne' + have hx := slow_pos j + have h2 := Nat.lt_pow_succ_log_self (by omega : 1 < 2) (Nat.log 2 j/1000) + change _ < 2^(ell j+1) at h2 + rw [pow_succ] at h2 + change _ < slow j *2 at h2 + change slow j ≤ _ at h1 + constructor + · apply le_trans (b:=2^(Nat.log 2 j)) ?_ (Nat.pow_log_le_self 2 (by have := pow_pos_nat 1000; omega)) + apply Nat.pow_le_pow_right (by omega) + omega + · apply lt_of_lt_of_le (Nat.lt_pow_succ_log_self (by omega : 1 < 2) j) + apply Nat.pow_le_pow_right (by omega) + omega + +lemma eventually_pow_small (n : ℕ) (C : ℝ) : + ∀ᶠ j : ℕ in atTop, C*(j:ℝ)^n ≤ 2^j := by + have H := (tendsto_pow_const_div_const_pow_of_one_lt n (show (1:ℝ)<2 by norm_num)).const_mul C + rw [mul_zero] at H + filter_upwards [H.eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num))] with j hj + apply le_of_lt + apply (div_lt_one (by positivity : (0:ℝ)<2^j)).mp + rw [mul_div_assoc] + exact hj + +lemma eventually_pow_small_slow (n : ℕ) (C : ℝ) : + ∀ᶠ j : ℕ in atTop, C*(slow j:ℝ)^n ≤ 2^slow j := + slow_tendsto.eventually (eventually_pow_small n C) + +lemma nat_pow_small_slow (n C : ℕ) : + ∀ᶠ j in atTop, C * (slow j)^n ≤ wide j := by + filter_upwards [eventually_pow_small_slow n C] with j hj + unfold wide; exact_mod_cast hj + + +def base (j : ℕ) : ℕ := 2^(wide j ^ 2) +def capE (j : ℕ) : ℕ := 2^(64*slow j) +def cap (j : ℕ) : ℕ := 2^capE j + + +lemma wide_le_j : ∀ᶠ j : ℕ in atTop, wide j ≤ j := by + filter_upwards [evt_slow] with j ⟨hj,h'⟩ + exact le_trans (Nat.pow_le_pow_right (by omega) (by omega)) hj +lemma ell_le_h (j : ℕ) : ell j ≤ slow j := Nat.le_of_lt Nat.lt_two_pow_self +lemma h_le_wide (j : ℕ) : slow j ≤ wide j := Nat.le_of_lt Nat.lt_two_pow_self +lemma slow_le_j : ∀ᶠ j in atTop, slow j ≤ j := wide_le_j.mono fun j h => (h_le_wide j).trans h + +lemma base_le_cap (j : ℕ) : base j ≤ cap j := by + unfold base cap capE wide + apply Nat.pow_le_pow_right (by omega) + rw [← pow_mul] + apply Nat.pow_le_pow_right (by omega) + omega + +lemma cap_power : ∀ᶠ j : ℕ in atTop, 64*capE j ≤ j := by + filter_upwards [evt_slow] with j hj + apply le_trans (b:=2^(1000*slow j)) _ hj.1 + unfold capE + rw [show 64 = 2^6 by norm_num] + rw [← pow_add] + apply Nat.pow_le_pow_right (by omega) + have := slow_pos j; omega + +lemma below_base (n : ℕ) (C : ℝ) : + ∀ᶠ j : ℕ in atTop, C * (j:ℝ)^n ≤ base j := by + obtain ⟨k,hk⟩ := exists_nat_gt C + filter_upwards [evt_slow, wide_tendsto.eventually (eventually_ge_atTop (max k (4000*n+2)))] with j ⟨hj,h1⟩ h2 + apply le_trans (b:=(k:ℝ)*(j:ℝ)^n) (mul_le_mul_of_nonneg_right hk.le (by positivity)) + suffices H : k*j^n ≤ base j by exact_mod_cast H + have hk1 : k ≤ wide j := le_trans (le_max_left _ _) h2 + apply le_trans (b:= (2^k)*(2^(3000*slow j))^n) (Nat.mul_le_mul + (Nat.le_of_lt Nat.lt_two_pow_self) (Nat.pow_le_pow_left h1.le n)) + unfold base + rw [← pow_mul, ← pow_add] + apply Nat.pow_le_pow_right (by omega) + have hx : 4000*n+2 ≤ wide j := le_trans (le_max_right _ _) h2 + nlinarith [h_le_wide j] + +lemma t_expr : ∀ᶠ j : ℕ in atTop, + sLen j * (slow j)^6 = 2^j ∧ 4*sSeg j = sLen j := by + filter_upwards [evt_slow] with j ⟨h,_⟩ + have h1 := Nat.lt_two_pow_self (n:=1000*slow j) + have h2 := ell_le_h j + have h3 := slow_pos j + constructor + · unfold sLen slow + rw [← pow_mul,← pow_add] + congr 1; omega + · unfold sSeg sLen + rw [show 4=2^2 by norm_num,← pow_add] + congr 1; omega + + +def nSched (j : ℕ) : ℕ := 2^(16*slow j) +def qSched (j : ℕ) : ℕ := 2*slow j^2 +def mSched (j : ℕ) : ℕ := nSched j * (qSched j*nSched j) + +def searchSched (j : ℕ) (hj : 4 ≤ slow j) : Schedule := by + let h : ℝ := slow j + have hh : 4 ≤ h := by dsimp only [h]; exact_mod_cast hj + have h' : 0 < h := by linarith + let N := nSched j; let q := qSched j + have hn : exp (6*h) ≤ (N:ℝ) := by + have hi : (1:ℝ)/2 ≤ log 2 := by linarith [Real.log_two_gt_d9] + have hN : 0 < (N:ℝ) := by exact_mod_cast pow_pos_nat (16*slow j) + rw [← Real.exp_log hN] + apply Real.exp_monotone + dsimp only [N,nSched]; push_cast; rw [Real.log_pow] + push_cast + unfold h at * + nlinarith + have Hn (k : ℝ) (hk : k ≤ 4) : 1 < N * exp (-k*h) := by + have H : 0 < 6*h+-k*h := by nlinarith + calc + 1 < exp (6*h+-k*h) := Real.one_lt_exp_iff.mpr H + _ = _ := Real.exp_add _ _ + _ ≤ _ := mul_le_mul_of_nonneg_right hn (exp_nonneg _) + have hq : (q:ℝ)=2*h^2 := by dsimp only [q,h,qSched]; push_cast; rfl + refine { N := N, n := N, q := q + N0 := pow_pos_nat _, n0 := pow_pos_nat _, q0 := ?_ + u := 1/h^2, a:= 1/h^3, δ:=exp (-4*h), η:=exp (-2*h) + α:=32/h^2, ε:=exp (-2*h) + u0:=by positivity + a0:=by positivity + d0:=exp_pos _ + heta:=?_ + hn:= Hn 4 le_rfl + hα:=by positivity + hε:=exp_pos _ + hβ:=?_ + hc:=?_ + hq:=?_ + ht:=Hn 2 (by norm_num) } + · have : (0:ℝ) < q := by rw [hq]; positivity + exact_mod_cast this + · rw [← exp_add] + apply le_of_eq; congr 1; field_simp; ring + · field_simp; nlinarith + · rw [show (1/h^3)/(32/h^2) = 1/(32*h) by field_simp] + rw [hq,add_mul,← exp_add] + have he : -2*h+ 2*(1/(32*h))*(2*h^2) ≤ -h := by field_simp; nlinarith + have hx := Real.exp_monotone he + rw [Real.exp_neg] at hx + have HH : (exp h)⁻¹ ≤ 1/2 := by + have := Real.add_one_le_exp h + rw [inv_le_comm₀ (exp_pos h) (by norm_num)] + norm_num; linarith + linarith + · rw [hq]; field_simp; nlinarith + +lemma nSched_scale (j : ℕ) (hj : 4 ≤ slow j) : + (searchSched j hj).size = mSched j := rfl + +lemma searchSched_loss (j : ℕ) (hj : 4 ≤ slow j) : + (searchSched j hj).loss ≤ 66/(slow j:ℝ)^2 := by + let h : ℝ := slow j + have hp : 4 ≤ h := by dsimp only [h]; exact_mod_cast hj + change 2*(32/h^2)+1/h^3*(1+exp (-2*h)) ≤ 66/h^2 + have HH : exp (-2*h) ≤ 1 := Real.exp_le_one_iff.mpr (by linarith) + have : (0:ℝ) < h := by linarith + field_simp; ring_nf at HH ⊢; nlinarith + + +def levelLow (j i : ℕ) : ℕ := wide j ^ 2 + 4*wide j*(mSched j-i) +def levelHigh (j i : ℕ) : ℕ := levelLow j i + 3*wide j +def stepLow (j i : ℕ) : ℕ := 2^levelLow j i +def stepHigh (j i : ℕ) : ℕ := 2^levelHigh j i + +def stepRows (j i : ℕ) : ℕ := 2^(j-(6*ell j+3+levelLow j i)) + +lemma index_budgets : + ∀ᶠ j : ℕ in atTop, ∀ i, levelHigh j i ≤ capE j ∧ 6*ell j+3+levelHigh j i ≤ j := by + filter_upwards [wide_tendsto.eventually (eventually_ge_atTop 8),cap_power] with j h wj + let w := wide j + have hp : 8 ≤ w := h + have hm : nSched j = w^16 := by + unfold nSched w wide + rw [← pow_mul]; congr 1; omega + have hc : capE j = w^64 := by + unfold capE w wide + rw [← pow_mul]; congr 1; omega + have hb : mSched j ≤ 2*w^34 := by + rw [mSched,hm,qSched] + calc + _ ≤ w^16*(2*w^2*w^16) := by + have H : slow j ≤ w := h_le_wide j + gcongr + _ = _ := by ring + have hhE : 6*ell j+3 ≤ capE j := by + have hg : ell j ≤ w := (ell_le_h j).trans (h_le_wide j) + rw [hc] + have hh : w^2 ≤ w^64 := Nat.pow_le_pow_right (by omega) (by omega) + nlinarith + intro i + have hx : levelHigh j i ≤ capE j := by + have H1 : mSched j-i ≤ 2*w^34 := le_trans (by omega) hb + change w^2+4*w*(mSched j-i)+3*w ≤ _ + rw [hc] + have h₁ : w ≤ w^35 := by + exact le_trans (by rw [pow_one] : w ≤ w^1) (Nat.pow_le_pow_right (by omega) (by omega)) + have h₂ : w^2 ≤ w^35 := Nat.pow_le_pow_right (by omega) (by omega) + have h₃ : 16*w^35 ≤ w^64 := by + have H : 16 ≤ w^29 := le_trans (b:=w^2) (by nlinarith) + (Nat.pow_le_pow_right (by omega) (by omega)) + apply le_trans (Nat.mul_le_mul_right (w^35) H) + rw [← pow_add] + calc + _ ≤ w^2+4*w*(2*w^34)+3*w := by gcongr + _ ≤ 16*w^35 := by + rw [show 4*w*(2*w^34)=8*w^35 by ring]; omega + _ ≤ _ := h₃ + exact ⟨hx,by omega⟩ + +lemma array_spec : + ∀ᶠ j : ℕ in atTop, + 3*sSeg j ≤ sLen j ∧ cap j ≤ sSeg j ∧ + cap j ≤ 2^j ∧ + (∀ i, stepLow j i ≤ stepHigh j i ∧ stepHigh j i ≤ cap j ∧ + 2*stepRows j i * stepLow j i = sSeg j ∧ + long j ≤ (stepRows j i:ℝ) * stepHigh j i ∧ + base j ≤ stepLow j i) ∧ + (∀ i k, i < k → k < mSched j → + stepHigh j k ≤ stepLow j i ∧ stepRows j i ≤ stepRows j k) := by + filter_upwards [index_budgets,t_expr,nat_pow_small_slow 1 10, + slow_tendsto.eventually (eventually_ge_atTop 4),cap_power] with j heq ht hw hs hj + have h1 : 6*ell j+2+capE j ≤ j := by + have H0 : 6*ell j ≤ levelHigh j 0 := by + unfold levelHigh levelLow; rw [pow_one] at hw + have H := ell_le_h j; omega + have h2 := heq 0 + have hh := pow_pos_nat (64*slow j); change 0 < capE j at hh + omega + refine ⟨by omega,?_,?_ ,?_,?_⟩ + · apply Nat.pow_le_pow_right (by omega) + omega + · apply Nat.pow_le_pow_right (by omega) + omega + · intro i + have hp := heq i + have hlow : levelLow j i ≤ levelHigh j i := by unfold levelHigh; omega + refine ⟨Nat.pow_le_pow_right (by omega) hlow,Nat.pow_le_pow_right (by omega) hp.1,?_,?_,?_⟩ + · unfold stepLow stepRows sSeg + nth_rw 1 [show 2 = 2^1 by rfl] + rw [← pow_add,← pow_add]; congr 1; omega + · unfold long height stepRows stepHigh + push_cast + rw [← pow_add, ← pow_add] + apply pow_le_pow_right₀ (by norm_num) + unfold levelHigh at * + rw [pow_one] at hw + have hh := ell_le_h j + omega + · unfold base stepLow + apply Nat.pow_le_pow_right (by omega) + unfold levelLow; omega + intro i k hi hk + have h1 : levelHigh j k ≤ levelLow j i := by + unfold levelHigh levelLow + have h2 : mSched j-k+1 ≤ mSched j-i := by omega + nlinarith + constructor + · exact Nat.pow_le_pow_right (by omega) h1 + · unfold stepRows + apply Nat.pow_le_pow_right (by omega) + unfold levelHigh at h1; omega + +lemma big_t_real : + ∀ᶠ j : ℕ in atTop, + height j = 4*(sSeg j:ℝ)*(slow j:ℝ)^6 ∧ + (cap j:ℝ)^4 ≤ height j := by + filter_upwards [t_expr,cap_power] with j h h' + constructor + · have H : 2^j = 4*sSeg j*slow j^6 := by rw [h.2,h.1] + unfold height + exact_mod_cast H + · have H1 : cap j^4 ≤ (2:ℕ)^j := by + rw [cap, ← pow_mul] + exact Nat.pow_le_pow_right (by omega) (by omega) + unfold height + exact_mod_cast H1 + +end FourierLLogL.Compress.Params + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Phase.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Phase.lean index dc39ed602f402e01bd8cdd2ae04c668b20c49756..fd2278a235bb32ecb3eb3e1f00fe3b927b1e8871 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Phase.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Phase.lean @@ -1,284 +1,284 @@ -import OAI.Analysis.FourierLLogL.Compress.Freeze -import OAI.Analysis.FourierLLogL.Finite.Complex - -open Set Function Filter -open scoped BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Real Law -variable {A G Θ I Ω : Type} [Fintype A] [Fintype G] [AddCommGroup G] - [Fintype Θ] [Fintype I] [DecidableEq I] [Fintype Ω] -set_option linter.unusedSectionVars false - -lemma char_neg (χ : AddChar G ℂ) (hn : ∀ x, ‖χ x‖ = 1) (x : G) : - χ (-x) = conj (χ x) := by - apply mul_left_cancel₀ (a := χ x) (by intro h; have H := hn x; rw [h,norm_zero] at H; norm_num at H) - rw [← χ.map_add_eq_mul,Complex.mul_conj,← Complex.sq_norm, hn] - simp - -lemma char_sum (χ : AddChar G ℂ) (f : I → G) : - χ (∑ i, f i) = ∏ i, χ (f i) := by - classical - suffices H : ∀ s : Finset I, χ (∑ i ∈ s, f i) = ∏ i ∈ s, χ (f i) from H _ - intro s - induction s using Finset.induction_on with - | empty => simp - | @insert i s hi ih => rw [Finset.prod_insert hi,Finset.sum_insert hi,χ.map_add_eq_mul,ih] - -lemma heat_prod (tag : A→G) (c : G→ℝ) (Q : Law Θ) (χ : Θ→AddChar G ℂ) - (hn : ∀ t x, ‖χ t x‖=1) - (hc : ∀ g, ((exp (-c g) : ℝ):ℂ)=Q.Cx (fun t => χ t g)) - (p : I→Law A) (z : G) : - (pack p).Ex (fun x => exp (-c (z-tot tag x))) ≤ - Q.Ex (fun t => ∏ i, ‖(p i).Cx (fun a => χ t (tag a))‖) := by - let f (x : I→A) := exp (-c (z-tot tag x)) - have H (x : I→A) (t : Θ) : - χ t (z-tot tag x) = χ t z * ∏ i, conj (χ t (tag (x i))) := by - rw [sub_eq_add_neg,(χ t).map_add_eq_mul,char_neg (χ t) (hn t)] - unfold tot - rw [char_sum,map_prod] - let C (i : I) (t : Θ) := (p i).Cx fun a => χ t (tag a) - have heq : - (↑((pack p).Ex f) : ℂ) = - Q.Cx fun t => χ t z * ∏ i, conj (C i t) := by - rw [← Cx_ofReal]; unfold f - simp_rw [hc,H] - rw [Cx_swap] - apply congrArg - - funext t - rw [Cx_lmul,Cx_prod p (fun _ x => conj (χ t (tag x)))] - simp_rw [Cx_conj]; rfl - have ht := Cx_norm Q (fun t => χ t z * ∏ i, conj (C i t)) - rw [← heq, Complex.norm_real, Real.norm_eq_abs] at ht - simp_rw [norm_mul,Complex.norm_prod,hn,one_mul,Complex.norm_conj] at ht - exact le_trans (le_abs_self _) ht - -/-- Defect between two letter phases. -/ -def defect (tag : A → G) (χ : Θ → AddChar G ℂ) (a b : A) (t : Θ) := - 1 - (χ t (tag a-tag b)).re - -lemma defect_nonneg (tag : A → G) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) (a b t) : - 0 ≤ defect tag χ a b t := by - unfold defect - have H := Complex.abs_re_le_norm (χ t (tag a-tag b)) - rw [hn] at H; linarith [(abs_le.mp H).2] - -lemma defect_mean (tag : A → G) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) - (p : Law A) (t) : - p.Ex (fun a => p.Ex (fun b => defect tag χ a b t)) = - 1- ‖p.Cx (fun a => χ t (tag a))‖^2 := by - unfold defect - have h (a b : A) : - (χ t) (tag a-tag b) = χ t (tag a)*conj (χ t (tag b)) := by - rw [sub_eq_add_neg,(χ t).map_add_eq_mul,char_neg _ (hn t)] - simp_rw [h,Ex_sub,Ex_const,sq_norm_Cx] - -/-- If the characteristic product has large integral, then it has large volume at the -corresponding defect level. -/ -lemma phase_region (Q : Law Θ) (f : I → Θ → ℝ) (hf : ∀ i t, 0 ≤ f i t ∧ f i t ≤ 1) - {E : ℝ} (he : 1 ≤ E) (h : exp (-(2*E)) ≤ Q.Ex fun t => ∏ i, f i t) : - let A (x : Θ) := ∑ i, (1-f i x ^ 2) - exp (-(3*E)) ≤ Q.prob fun x => A x ≤ 8*E := by - classical - intro D - let S (x : Θ) := D x ≤ 8*E - have h1 (x : ℝ) (hp : 0 ≤ x) : x ≤ exp (-(1-x^2)/2) := by - have hi := Real.add_one_le_exp (-(1-x^2)) - have hh := Real.exp_pos (-(1-x^2)/2) - rw [show -(1-x^2)=-(1-x^2)/2+ -(1-x^2)/2 by ring, - Real.exp_add] at hi - nlinarith - have hx (x : Θ) : - (∏ i, f i x) ≤ (if S x then (1:ℝ) else 0)+ exp (-(4*E)) := by - split_ifs with H - · apply le_trans (b:=∏ i : I,(1:ℝ)) (Finset.prod_le_prod₀ (fun i _ => (hf i x).1) - (fun i _ => (hf i x).2)) - simp [exp_nonneg] - · apply le_trans (b:=∏ i, exp (-(1-f i x^2)/2)) (Finset.prod_le_prod₀ - (fun i _ => (hf i x).1) (fun i _ => h1 _ (hf _ _).1)) - have heq : ∑ i, (-(1-f i x^2)/2) = -(D x)/2 := by - unfold D; rw [← Finset.sum_div,Finset.sum_neg_distrib] - rw [← Real.exp_sum,heq,zero_add] - apply exp_monotone; unfold S at H; linarith - have heq : Q.Ex (fun x => if S x then (1:ℝ) else 0) = Q.prob S := by - unfold prob - congr 1 - have H := Q.Ex_mono hx - rw [Ex_add,heq,Ex_const] at H - have hb : 2 ≤ exp E := le_trans (by linarith) (Real.add_one_le_exp E) - have H1 := exp_nonneg (-(4*E)) - have H2 := exp_nonneg (-(3*E)) - have H3 : exp (-(2*E)) = exp (-(3*E))*exp E := by rw [← exp_add]; congr 1; ring - have H4 : exp (-(3*E)) = exp (-(4*E))*exp E := by rw [← exp_add]; congr 1; ring - change _ ≤ Q.prob S - nlinarith - - -lemma log_gap (P : Law A) [Nonempty A] - (s : Finset A) - (hp : (1/2:ℝ) ≤ P.prob fun x => x ∈ s) : - log (Fintype.card A) - log (s.card:ℝ) ≤ - 2*(log (Fintype.card A)-P.entropy+log 2) := by - classical - let f (x : A) : Bool := decide (x ∈ s) - let Q := P.send f - have h1 : P.entropy = Q.entropy + - Q.Ex fun b => (P.after f b).entropy := by - have hI : P.H (fun x => (x,f x)) = P.entropy := by - rw [← H_self]; apply H_partition; simp - rw [← hI,H_pair_chain,Hcond_after] - simp_rw [H_self] - rfl - have hh : Q true = P.prob (fun x => x ∈ s) := by - unfold Q; rw [ send_apply] - congr; simp [f] - have hPos : 0 < Q true := by rw [hh]; linarith - have hcard : 0 < (s.card:ℝ) := by - obtain ⟨x,hx,_⟩ := (send_pos_iff P f true).mp hPos - have ht : x ∈ s := by simpa only [f,decide_eq_true_eq] using hx - exact_mod_cast Finset.card_pos.mpr ⟨x,ht⟩ - let L := log (Fintype.card A) - let T := log (s.card:ℝ) - have hT : T ≤ L := Real.log_le_log hcard (by exact_mod_cast Finset.card_le_univ _) - have H : (P.after f true).entropy ≤ T := - entropy_le_log_size _ s fun x hx => by - simpa only [f,decide_eq_true_eq] using ((after_mem hPos).mp hx).1 - have H' : (P.after f false).entropy ≤ L := entropy_le_log_card _ - have Hb : Q.entropy ≤ log 2 := by simpa using entropy_le_log_card Q - have HI : Q false + Q true = 1 := by simpa [Fintype.sum_bool,add_comm] using Q.total - rw [h1,Ex,Fintype.sum_bool] - change L-T ≤ _ - have : L-T ≤ 2*(L-(Q.entropy+ - (Q true*(P.after f true).entropy + Q false*(P.after f false).entropy))+log 2) := by - have hPos' := Q.nonneg false - rw [← hh] at hp - nlinarith [mul_le_mul_of_nonneg_left H hPos.le, - mul_le_mul_of_nonneg_left H' hPos', show Q false * L+ Q true * L=L by rw [← add_mul,HI,one_mul]] - exact this - - -lemma prob_half (P : Law Ω) (f : Ω → ℝ) (hn : ∀ x, 0 ≤ f x) (E : ℝ) (he : 0 < E) - (hp : P.Ex f ≤ E) : - (1/2:ℝ) ≤ P.prob fun x => f x ≤ 2*E := by - classical - let p (x : Ω) := f x ≤ 2*E - let F (x : Ω) := if ¬p x then 1 else (0:ℝ) - have hh : P.Ex F = P.prob (fun x => ¬p x) := by - apply congrArg (P.Ex) - funext x; unfold F - split_ifs <;> simp_all - rw [prob_compl] at hh - have hi (x : Ω) : 2*E * F x ≤ f x := by - unfold F p; split_ifs with H - · simp [hn x] - · simp; linarith - have H := (P.Ex_mono hi).trans hp - rw [Ex_lmul, hh] at H - change _ ≤ P.prob p - nlinarith - -def small_defects (tag : A → G) (χ : Θ → AddChar G ℂ) (Q : Law Θ) - (L : ℝ) (s : Finset A) (a : A) (F : ℝ) : Prop := - ∀ p : Law A, (∀ x, 0 < p x → x ∈ s) → - exp (-F) ≤ Q.Ex fun t => exp ((-L)*p.Ex (fun x => defect tag χ x a t)) - -lemma alignment (tag : A → G) (c : G → ℝ) (Q : Law Θ) (χ : Θ → AddChar G ℂ) - (hn : ∀ t x, ‖χ t x‖=1) - (hc : ∀ g, ((exp (-c g) : ℝ):ℂ)=Q.Cx (fun t => χ t g)) - (p : I → Law A) [Nonempty I] [Nonempty A] (z : G) - (F D L : ℝ) (he : 1 ≤ F) (hd : 0 < D) (hL : 0 ≤ L) - (hL' : L ≤ Fintype.card I) - (hz : exp (-(2*F)) ≤ (pack p).Ex (fun x => exp (-c (z-tot tag x)))) - (hs : ∑ i, (log (Fintype.card A)-entropy (p i)) ≤ 2*(Fintype.card I*D)) : - ∃ (a : A) (s : Finset A), s.Nonempty ∧ - log (Fintype.card A) - log (s.card:ℝ) ≤ 8*D+2 ∧ - small_defects tag χ Q L s a (35*F) := by - classical - let f i (t : Θ) := ‖(p i).Cx (fun a => χ t (tag a))‖ - have hf1 (i : I) (t : Θ) : f i t ≤ 1 := norm_Cx_unit _ _ fun x => (hn _ _).le - have hf0 (i : I) (t : Θ) : 0 ≤ f i t := norm_nonneg _ - let W (x : Θ) : Prop := ∑ i, (1 - f i x ^ 2) ≤ 8*F - have hh : exp (-(3*F)) ≤ Q.prob W := phase_region Q f (fun i t => ⟨hf0 _ _,hf1 _ _⟩) - he (hz.trans (heat_prod _ _ _ _ hn hc p z)) - let P := Q.condP W - have hW : 0 < Q.prob W := lt_of_lt_of_le (exp_pos _) hh - let g (i : I) := P.Ex fun t => 1-f i t^2 - let A' (i : I) := log (Fintype.card A)-entropy (p i) - have hgp (i : I) : 0 ≤ g i := Ex_nonneg P fun t => by - nlinarith [hf1 i t,hf0 i t] - let T := (Fintype.card I:ℝ) - have ht : 0 < T := by unfold T; positivity - have hg : (uniform I).Ex g ≤ 8*F/T := by - have hi : ∑ i, g i ≤ 8*F := by - unfold g - rw [← Ex_finset_sum] - refine le_trans (b:=P.Ex fun _ => 8*F) (P.Ex_mono_mem fun x hx => ?_) (P.Ex_const _).le - exact ((Law.condP_mem hW).mp hx).1 - simp_rw [Ex,uniform_apply,← Finset.mul_sum] - exact le_trans (mul_le_mul_of_nonneg_left hi (inv_pos.mpr ht).le) - (by dsimp [T]; rw [← div_eq_inv_mul]) - have ha : (uniform I).Ex A' ≤ 2*D := by - simp_rw [Ex,uniform_apply,← Finset.mul_sum,← div_eq_inv_mul] - exact (div_le_iff₀ ht).mpr (by dsimp only [T,A']; linarith) - obtain ⟨i, _,hi,hi'⟩ := average_two_below (uniform I) g A' (fun i _ => hgp i) - (fun i _ => sub_nonneg.mpr (entropy_le_log_card _)) (8*F/T) (2*D) - (by have := lt_of_lt_of_le zero_lt_one he; positivity) (by positivity) hg ha - let R := p i - let G (x y : A) := P.Ex (defect tag χ x y) - have HG : R.Ex (fun y => R.Ex fun x => G x y) ≤ 16*F/T := by - have Heq : R.Ex (fun y => R.Ex fun x => G x y) = g i := by - rw [Ex_swap] - unfold G - simp_rw [Ex_swap R P] - unfold g f - simp_rw [defect_mean _ _ hn] - rfl - rw [Heq] - exact hi.trans (le_of_eq (by ring)) - obtain ⟨a,ha',ha''⟩ := R.exists_le_Ex (fun y => R.Ex fun x => G x y) - let s := Finset.univ.filter fun x => G x a ≤ 2*(16*F/T) - have ha0 : R.Ex (fun x => G x a) ≤ 16*F/T := ha''.trans HG - have hp : (1/2:ℝ) ≤ R.prob fun x => x ∈ s := by - simp only [s,Finset.mem_filter,Finset.mem_univ,true_and] - exact prob_half _ _ (fun x => Ex_nonneg _ (fun y => defect_nonneg _ _ hn x a y)) - _ (by have := lt_of_lt_of_le zero_lt_one he; positivity) ha0 - obtain ⟨x,hx,_⟩ := (prob_pos_iff R (fun x => x∈s)).mp - (lt_of_lt_of_le (by norm_num : (0:ℝ)<1/2) hp) - refine ⟨a,s,⟨x,hx⟩,(log_gap R s hp).trans ?_,?_⟩ - · change log (Fintype.card A) - entropy R ≤ _ at hi' - have hi := Real.log_le_sub_one_of_pos (x:=2) (by norm_num) - linarith - intro V hv - let v (t : Θ) := V.Ex fun x => defect tag χ x a t - have hB : P.Ex v ≤ 32*F/T := by - unfold v; rw [Ex_swap P V] - refine le_trans (b:=V.Ex fun _ => 32*F/T) (V.Ex_mono_mem fun x hx => ?_) - (V.Ex_const _).le - have H := (Finset.mem_filter.mp (hv x hx)).2 - change G x a ≤ _ - exact H.trans (le_of_eq (by ring)) - have H1 : P.Ex (fun t => exp ((-L)* v t)) ≥ exp (-(32*F)) := by - have H := Ex_exp P (fun t => (-L)*v t) - rw [Ex_lmul] at H - apply le_trans _ H - apply exp_monotone - have hbp := (le_div_iff₀ ht).mp hB - change L ≤ T at hL' - have hg1 : 0 ≤ P.Ex v := - P.Ex_nonneg fun t => V.Ex_nonneg fun x => defect_nonneg tag χ hn x a t - nlinarith - calc - exp (-(35*F)) = exp (-(3*F))*exp (-(32*F)) := by rw [← exp_add]; congr 1; ring - _ ≤ Q.prob W * P.Ex (fun t => exp ((-L)*v t)) := - mul_le_mul hh H1 (exp_nonneg _) hW.le - _ ≤ _ := by - dsimp only [P] - rw [prob_mul_Ex_condP] - apply Q.Ex_mono; intro t; split_ifs <;> first | exact le_rfl | exact exp_nonneg _ - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Freeze +import OAI.Analysis.FourierLLogL.Finite.Complex + +open Set Function Filter +open scoped BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Real Law +variable {A G Θ I Ω : Type} [Fintype A] [Fintype G] [AddCommGroup G] + [Fintype Θ] [Fintype I] [DecidableEq I] [Fintype Ω] +set_option linter.unusedSectionVars false + +lemma char_neg (χ : AddChar G ℂ) (hn : ∀ x, ‖χ x‖ = 1) (x : G) : + χ (-x) = conj (χ x) := by + apply mul_left_cancel₀ (a := χ x) (by intro h; have H := hn x; rw [h,norm_zero] at H; norm_num at H) + rw [← χ.map_add_eq_mul,Complex.mul_conj,← Complex.sq_norm, hn] + simp + +lemma char_sum (χ : AddChar G ℂ) (f : I → G) : + χ (∑ i, f i) = ∏ i, χ (f i) := by + classical + suffices H : ∀ s : Finset I, χ (∑ i ∈ s, f i) = ∏ i ∈ s, χ (f i) from H _ + intro s + induction s using Finset.induction_on with + | empty => simp + | @insert i s hi ih => rw [Finset.prod_insert hi,Finset.sum_insert hi,χ.map_add_eq_mul,ih] + +lemma heat_prod (tag : A→G) (c : G→ℝ) (Q : Law Θ) (χ : Θ→AddChar G ℂ) + (hn : ∀ t x, ‖χ t x‖=1) + (hc : ∀ g, ((exp (-c g) : ℝ):ℂ)=Q.Cx (fun t => χ t g)) + (p : I→Law A) (z : G) : + (pack p).Ex (fun x => exp (-c (z-tot tag x))) ≤ + Q.Ex (fun t => ∏ i, ‖(p i).Cx (fun a => χ t (tag a))‖) := by + let f (x : I→A) := exp (-c (z-tot tag x)) + have H (x : I→A) (t : Θ) : + χ t (z-tot tag x) = χ t z * ∏ i, conj (χ t (tag (x i))) := by + rw [sub_eq_add_neg,(χ t).map_add_eq_mul,char_neg (χ t) (hn t)] + unfold tot + rw [char_sum,map_prod] + let C (i : I) (t : Θ) := (p i).Cx fun a => χ t (tag a) + have heq : + (↑((pack p).Ex f) : ℂ) = + Q.Cx fun t => χ t z * ∏ i, conj (C i t) := by + rw [← Cx_ofReal]; unfold f + simp_rw [hc,H] + rw [Cx_swap] + apply congrArg + + funext t + rw [Cx_lmul,Cx_prod p (fun _ x => conj (χ t (tag x)))] + simp_rw [Cx_conj]; rfl + have ht := Cx_norm Q (fun t => χ t z * ∏ i, conj (C i t)) + rw [← heq, Complex.norm_real, Real.norm_eq_abs] at ht + simp_rw [norm_mul,Complex.norm_prod,hn,one_mul,Complex.norm_conj] at ht + exact le_trans (le_abs_self _) ht + +/-- Defect between two letter phases. -/ +def defect (tag : A → G) (χ : Θ → AddChar G ℂ) (a b : A) (t : Θ) := + 1 - (χ t (tag a-tag b)).re + +lemma defect_nonneg (tag : A → G) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) (a b t) : + 0 ≤ defect tag χ a b t := by + unfold defect + have H := Complex.abs_re_le_norm (χ t (tag a-tag b)) + rw [hn] at H; linarith [(abs_le.mp H).2] + +lemma defect_mean (tag : A → G) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) + (p : Law A) (t) : + p.Ex (fun a => p.Ex (fun b => defect tag χ a b t)) = + 1- ‖p.Cx (fun a => χ t (tag a))‖^2 := by + unfold defect + have h (a b : A) : + (χ t) (tag a-tag b) = χ t (tag a)*conj (χ t (tag b)) := by + rw [sub_eq_add_neg,(χ t).map_add_eq_mul,char_neg _ (hn t)] + simp_rw [h,Ex_sub,Ex_const,sq_norm_Cx] + +/-- If the characteristic product has large integral, then it has large volume at the +corresponding defect level. -/ +lemma phase_region (Q : Law Θ) (f : I → Θ → ℝ) (hf : ∀ i t, 0 ≤ f i t ∧ f i t ≤ 1) + {E : ℝ} (he : 1 ≤ E) (h : exp (-(2*E)) ≤ Q.Ex fun t => ∏ i, f i t) : + let A (x : Θ) := ∑ i, (1-f i x ^ 2) + exp (-(3*E)) ≤ Q.prob fun x => A x ≤ 8*E := by + classical + intro D + let S (x : Θ) := D x ≤ 8*E + have h1 (x : ℝ) (hp : 0 ≤ x) : x ≤ exp (-(1-x^2)/2) := by + have hi := Real.add_one_le_exp (-(1-x^2)) + have hh := Real.exp_pos (-(1-x^2)/2) + rw [show -(1-x^2)=-(1-x^2)/2+ -(1-x^2)/2 by ring, + Real.exp_add] at hi + nlinarith + have hx (x : Θ) : + (∏ i, f i x) ≤ (if S x then (1:ℝ) else 0)+ exp (-(4*E)) := by + split_ifs with H + · apply le_trans (b:=∏ i : I,(1:ℝ)) (Finset.prod_le_prod₀ (fun i _ => (hf i x).1) + (fun i _ => (hf i x).2)) + simp [exp_nonneg] + · apply le_trans (b:=∏ i, exp (-(1-f i x^2)/2)) (Finset.prod_le_prod₀ + (fun i _ => (hf i x).1) (fun i _ => h1 _ (hf _ _).1)) + have heq : ∑ i, (-(1-f i x^2)/2) = -(D x)/2 := by + unfold D; rw [← Finset.sum_div,Finset.sum_neg_distrib] + rw [← Real.exp_sum,heq,zero_add] + apply exp_monotone; unfold S at H; linarith + have heq : Q.Ex (fun x => if S x then (1:ℝ) else 0) = Q.prob S := by + unfold prob + congr 1 + have H := Q.Ex_mono hx + rw [Ex_add,heq,Ex_const] at H + have hb : 2 ≤ exp E := le_trans (by linarith) (Real.add_one_le_exp E) + have H1 := exp_nonneg (-(4*E)) + have H2 := exp_nonneg (-(3*E)) + have H3 : exp (-(2*E)) = exp (-(3*E))*exp E := by rw [← exp_add]; congr 1; ring + have H4 : exp (-(3*E)) = exp (-(4*E))*exp E := by rw [← exp_add]; congr 1; ring + change _ ≤ Q.prob S + nlinarith + + +lemma log_gap (P : Law A) [Nonempty A] + (s : Finset A) + (hp : (1/2:ℝ) ≤ P.prob fun x => x ∈ s) : + log (Fintype.card A) - log (s.card:ℝ) ≤ + 2*(log (Fintype.card A)-P.entropy+log 2) := by + classical + let f (x : A) : Bool := decide (x ∈ s) + let Q := P.send f + have h1 : P.entropy = Q.entropy + + Q.Ex fun b => (P.after f b).entropy := by + have hI : P.H (fun x => (x,f x)) = P.entropy := by + rw [← H_self]; apply H_partition; simp + rw [← hI,H_pair_chain,Hcond_after] + simp_rw [H_self] + rfl + have hh : Q true = P.prob (fun x => x ∈ s) := by + unfold Q; rw [ send_apply] + congr; simp [f] + have hPos : 0 < Q true := by rw [hh]; linarith + have hcard : 0 < (s.card:ℝ) := by + obtain ⟨x,hx,_⟩ := (send_pos_iff P f true).mp hPos + have ht : x ∈ s := by simpa only [f,decide_eq_true_eq] using hx + exact_mod_cast Finset.card_pos.mpr ⟨x,ht⟩ + let L := log (Fintype.card A) + let T := log (s.card:ℝ) + have hT : T ≤ L := Real.log_le_log hcard (by exact_mod_cast Finset.card_le_univ _) + have H : (P.after f true).entropy ≤ T := + entropy_le_log_size _ s fun x hx => by + simpa only [f,decide_eq_true_eq] using ((after_mem hPos).mp hx).1 + have H' : (P.after f false).entropy ≤ L := entropy_le_log_card _ + have Hb : Q.entropy ≤ log 2 := by simpa using entropy_le_log_card Q + have HI : Q false + Q true = 1 := by simpa [Fintype.sum_bool,add_comm] using Q.total + rw [h1,Ex,Fintype.sum_bool] + change L-T ≤ _ + have : L-T ≤ 2*(L-(Q.entropy+ + (Q true*(P.after f true).entropy + Q false*(P.after f false).entropy))+log 2) := by + have hPos' := Q.nonneg false + rw [← hh] at hp + nlinarith [mul_le_mul_of_nonneg_left H hPos.le, + mul_le_mul_of_nonneg_left H' hPos', show Q false * L+ Q true * L=L by rw [← add_mul,HI,one_mul]] + exact this + + +lemma prob_half (P : Law Ω) (f : Ω → ℝ) (hn : ∀ x, 0 ≤ f x) (E : ℝ) (he : 0 < E) + (hp : P.Ex f ≤ E) : + (1/2:ℝ) ≤ P.prob fun x => f x ≤ 2*E := by + classical + let p (x : Ω) := f x ≤ 2*E + let F (x : Ω) := if ¬p x then 1 else (0:ℝ) + have hh : P.Ex F = P.prob (fun x => ¬p x) := by + apply congrArg (P.Ex) + funext x; unfold F + split_ifs <;> simp_all + rw [prob_compl] at hh + have hi (x : Ω) : 2*E * F x ≤ f x := by + unfold F p; split_ifs with H + · simp [hn x] + · simp; linarith + have H := (P.Ex_mono hi).trans hp + rw [Ex_lmul, hh] at H + change _ ≤ P.prob p + nlinarith + +def small_defects (tag : A → G) (χ : Θ → AddChar G ℂ) (Q : Law Θ) + (L : ℝ) (s : Finset A) (a : A) (F : ℝ) : Prop := + ∀ p : Law A, (∀ x, 0 < p x → x ∈ s) → + exp (-F) ≤ Q.Ex fun t => exp ((-L)*p.Ex (fun x => defect tag χ x a t)) + +lemma alignment (tag : A → G) (c : G → ℝ) (Q : Law Θ) (χ : Θ → AddChar G ℂ) + (hn : ∀ t x, ‖χ t x‖=1) + (hc : ∀ g, ((exp (-c g) : ℝ):ℂ)=Q.Cx (fun t => χ t g)) + (p : I → Law A) [Nonempty I] [Nonempty A] (z : G) + (F D L : ℝ) (he : 1 ≤ F) (hd : 0 < D) (hL : 0 ≤ L) + (hL' : L ≤ Fintype.card I) + (hz : exp (-(2*F)) ≤ (pack p).Ex (fun x => exp (-c (z-tot tag x)))) + (hs : ∑ i, (log (Fintype.card A)-entropy (p i)) ≤ 2*(Fintype.card I*D)) : + ∃ (a : A) (s : Finset A), s.Nonempty ∧ + log (Fintype.card A) - log (s.card:ℝ) ≤ 8*D+2 ∧ + small_defects tag χ Q L s a (35*F) := by + classical + let f i (t : Θ) := ‖(p i).Cx (fun a => χ t (tag a))‖ + have hf1 (i : I) (t : Θ) : f i t ≤ 1 := norm_Cx_unit _ _ fun x => (hn _ _).le + have hf0 (i : I) (t : Θ) : 0 ≤ f i t := norm_nonneg _ + let W (x : Θ) : Prop := ∑ i, (1 - f i x ^ 2) ≤ 8*F + have hh : exp (-(3*F)) ≤ Q.prob W := phase_region Q f (fun i t => ⟨hf0 _ _,hf1 _ _⟩) + he (hz.trans (heat_prod _ _ _ _ hn hc p z)) + let P := Q.condP W + have hW : 0 < Q.prob W := lt_of_lt_of_le (exp_pos _) hh + let g (i : I) := P.Ex fun t => 1-f i t^2 + let A' (i : I) := log (Fintype.card A)-entropy (p i) + have hgp (i : I) : 0 ≤ g i := Ex_nonneg P fun t => by + nlinarith [hf1 i t,hf0 i t] + let T := (Fintype.card I:ℝ) + have ht : 0 < T := by unfold T; positivity + have hg : (uniform I).Ex g ≤ 8*F/T := by + have hi : ∑ i, g i ≤ 8*F := by + unfold g + rw [← Ex_finset_sum] + refine le_trans (b:=P.Ex fun _ => 8*F) (P.Ex_mono_mem fun x hx => ?_) (P.Ex_const _).le + exact ((Law.condP_mem hW).mp hx).1 + simp_rw [Ex,uniform_apply,← Finset.mul_sum] + exact le_trans (mul_le_mul_of_nonneg_left hi (inv_pos.mpr ht).le) + (by dsimp [T]; rw [← div_eq_inv_mul]) + have ha : (uniform I).Ex A' ≤ 2*D := by + simp_rw [Ex,uniform_apply,← Finset.mul_sum,← div_eq_inv_mul] + exact (div_le_iff₀ ht).mpr (by dsimp only [T,A']; linarith) + obtain ⟨i, _,hi,hi'⟩ := average_two_below (uniform I) g A' (fun i _ => hgp i) + (fun i _ => sub_nonneg.mpr (entropy_le_log_card _)) (8*F/T) (2*D) + (by have := lt_of_lt_of_le zero_lt_one he; positivity) (by positivity) hg ha + let R := p i + let G (x y : A) := P.Ex (defect tag χ x y) + have HG : R.Ex (fun y => R.Ex fun x => G x y) ≤ 16*F/T := by + have Heq : R.Ex (fun y => R.Ex fun x => G x y) = g i := by + rw [Ex_swap] + unfold G + simp_rw [Ex_swap R P] + unfold g f + simp_rw [defect_mean _ _ hn] + rfl + rw [Heq] + exact hi.trans (le_of_eq (by ring)) + obtain ⟨a,ha',ha''⟩ := R.exists_le_Ex (fun y => R.Ex fun x => G x y) + let s := Finset.univ.filter fun x => G x a ≤ 2*(16*F/T) + have ha0 : R.Ex (fun x => G x a) ≤ 16*F/T := ha''.trans HG + have hp : (1/2:ℝ) ≤ R.prob fun x => x ∈ s := by + simp only [s,Finset.mem_filter,Finset.mem_univ,true_and] + exact prob_half _ _ (fun x => Ex_nonneg _ (fun y => defect_nonneg _ _ hn x a y)) + _ (by have := lt_of_lt_of_le zero_lt_one he; positivity) ha0 + obtain ⟨x,hx,_⟩ := (prob_pos_iff R (fun x => x∈s)).mp + (lt_of_lt_of_le (by norm_num : (0:ℝ)<1/2) hp) + refine ⟨a,s,⟨x,hx⟩,(log_gap R s hp).trans ?_,?_⟩ + · change log (Fintype.card A) - entropy R ≤ _ at hi' + have hi := Real.log_le_sub_one_of_pos (x:=2) (by norm_num) + linarith + intro V hv + let v (t : Θ) := V.Ex fun x => defect tag χ x a t + have hB : P.Ex v ≤ 32*F/T := by + unfold v; rw [Ex_swap P V] + refine le_trans (b:=V.Ex fun _ => 32*F/T) (V.Ex_mono_mem fun x hx => ?_) + (V.Ex_const _).le + have H := (Finset.mem_filter.mp (hv x hx)).2 + change G x a ≤ _ + exact H.trans (le_of_eq (by ring)) + have H1 : P.Ex (fun t => exp ((-L)* v t)) ≥ exp (-(32*F)) := by + have H := Ex_exp P (fun t => (-L)*v t) + rw [Ex_lmul] at H + apply le_trans _ H + apply exp_monotone + have hbp := (le_div_iff₀ ht).mp hB + change L ≤ T at hL' + have hg1 : 0 ≤ P.Ex v := + P.Ex_nonneg fun t => V.Ex_nonneg fun x => defect_nonneg tag χ hn x a t + nlinarith + calc + exp (-(35*F)) = exp (-(3*F))*exp (-(32*F)) := by rw [← exp_add]; congr 1; ring + _ ≤ Q.prob W * P.Ex (fun t => exp ((-L)*v t)) := + mul_le_mul hh H1 (exp_nonneg _) hW.le + _ ≤ _ := by + dsimp only [P] + rw [prob_mul_Ex_condP] + apply Q.Ex_mono; intro t; split_ifs <;> first | exact le_rfl | exact exp_nonneg _ + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Prefix.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Prefix.lean index c70decf7098080e321ed6b4cbea0ef55e1a96196..04953d0e7d324544818c55b4a39ee3b898b9679d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Prefix.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Prefix.lean @@ -1,169 +1,169 @@ -import OAI.Analysis.FourierLLogL.Compress.Blocks -/-! # Entropy increments along a prefix -/ - -open Function Set Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {n m : ℕ} {α Ω G : Type} [Fintype α] [Fintype Ω] [Fintype G] - -def rateH (P : Law Ω) (X : Ω → Letters n α) (j : ℕ) := - if h : j < n then P.Hcond (readLetter X j h) (prefixB X j h.le) else 0 - -lemma rate_lt (P : Law Ω) (X : Ω → Letters n α) {j : ℕ} (h : j < n) : - rateH P X j = P.Hcond (readLetter X j h) (prefixB X j h.le) := - dite_eq_left h - -lemma H_prefix (P : Law Ω) (X : Ω → Letters n α) (l : ℕ) (h : l ≤ n) : - P.H (prefixB X l h) = ∑ j ∈ Finset.range l, rateH P X j := by - induction l with - | zero => - rw [prefix_zero, H_constant]; simp - | succ l ih => - have hh := P.H_partition (X:=prefixB X (l+1) h) - (Y:=fun x => (readLetter X l h x, prefixB X l (by omega) x)) - (by intro x y; simp only [Prod.mk.injEq]; exact prefix_last_partition X l h x y) - rw [hh,H_pair_chain, ih, Finset.sum_range_succ, rate_lt _ _ h] - -lemma H_post_prefix (P : Law Ω) (X : Ω → Letters n α) (j l : ℕ) (h : j+l ≤ n) : - let W := prefixB X j (by omega) - (P.send W).Ex (fun w => (P.after W w).H (readBlock X j l h)) = - ∑ i ∈ Finset.range l, rateH P X (j+i) := by - intro W - rw [← Hcond_after,Hcond] - have H : P.H (prefixB X (j+l) h) = P.H (fun x => (readBlock X j l h x, W x)) := by - refine P.H_partition fun x y => ?_ - rw [prefix_partition, Prod.mk.injEq] - exact and_comm - rw [← H,H_prefix] - dsimp only [W] - rw [H_prefix, Finset.sum_range_add] - ring - - -private abbrev S := WordSample - -lemma conditional_block_exch {P : Law (S n α G)} (hp : exch P) - {j m} (hh : j+m ≤ n) (w : Letters j α) : - exch ((P.after (prefixB Prod.fst j (by omega)) w).send (fun p => - (readBlock Prod.fst j m hh p,p.2))) := by - apply exch_subSample_cond hp (offset j m hh) (offset_injective _ _ _) - (offset 0 j (by omega)) - rintro i ⟨b,hb⟩ - have H := congrArg Fin.val hb - simp only [offset] at H - omega - -lemma conditional_block_marg {P : Law (S n α G)} (hp : exch P) - {j m} (hh : j+m ≤ n) (w : Letters j α) (i : Fin m) : - let R := P.after (prefixB Prod.fst j (by omega)) w - R.send (fun x => readBlock Prod.fst j m hh x i) = - R.send (readLetter Prod.fst j (by have := i.isLt; omega)) := by - intro R - let i₀ : Fin m := ⟨0,by have := i.isLt; omega⟩ - have he := same_marginal (conditional_block_exch hp hh w) i i₀ - simp only [send_send] at he - exact he - -lemma rate_antitone_succ {P : Law (S n α G)} (hp : exch P) - {j} (h : j+1 < n) : rateH P Prod.fst (j+1) ≤ rateH P Prod.fst j := by - let X : S n α G → Letters n α := Prod.fst - let W := prefixB X j (by omega) - let V := prefixB X (j+1) h.le - have H : P.Hcond (readLetter X (j+1) h) W = P.Hcond (readLetter X j (by omega)) W := by - rw [Hcond_after,Hcond_after] - have HH (w) := conditional_block_marg hp (j:=j) (m:=2) (by omega) w (1:Fin 2) - have hEq (w : Letters j α) : - (P.after W w).send (readLetter X (j+1) h) = - (P.after W w).send (readLetter X j (by omega)) := HH w - simp_rw [Law.H,hEq] - rw [rate_lt _ _ h, rate_lt _ _ (by omega : j < n)] - change P.Hcond _ V ≤ P.Hcond (readLetter X j _) W - rw [← H] - apply Hcond_antitone - exact fun a b hh => ((prefix_last_partition X j (by omega) a b).mp hh).2 - -lemma rate_antitone {P : Law (S n α G)} (hp : exch P) : - Antitone (rateH P Prod.fst) := by - refine antitone_nat_of_succ_le fun k => ?_ - by_cases h : k+1 simp - | succ k => apply log_nonneg; simp - -/-- The joint divergence of the conditional block from the product of its row marginals. -/ -lemma avg_block_kl {P : Law (S n α G)} (hp : exch P) - {j m} (hh : j+m ≤ n) (hj : j - let R := P.after W w - kl (R.send V) (pack (fun _ : Fin m => R.send (readLetter X j hj)))) = - ∑ i ∈ Finset.range m, (rateH P X j - rateH P X (j+i)) := by - intro X W V - let f := readLetter X j hj - let A (w : Letters j α) := P.after W w - have hk (w : Letters j α) : - kl ((A w).send V) (pack (fun _ : Fin m => (A w).send f)) = - (m:ℝ) * (A w).H f - (A w).H V := by - have hm (i : Fin m) := conditional_block_marg hp hh w i - have hm' (i : Fin m) : - ((A w).send V).send (fun x => x i) = (A w).send f := by rw [send_send]; exact hm i - have he : dominated ((A w).send V) (pack (fun _ : Fin m => (A w).send f)) := by - intro x hx - rw [pack_pos] - intro i - rw [← hm' i]; exact send_pos _ _ hx - rw [kl,crossE_pack he] - simp_rw [hm'] - simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] - rfl - change (P.send W).Ex (fun w => kl ((A w).send V) (pack (fun _ : Fin m => (A w).send f))) = _ - have h1 : (P.send W).Ex (fun w => (A w).H f) = rateH P X j := - (rate_lt P X hj ▸ Hcond_after P f W).symm - have h2 : (P.send W).Ex (fun w => (A w).H V) = _ := H_post_prefix P X j m hh - simp_rw [hk,Ex_sub,Ex_lmul,h1,h2,Finset.sum_sub_distrib,Finset.sum_const,Finset.card_range, - nsmul_eq_mul] - -lemma kl_uniform {α : Type} [Fintype α] [Nonempty α] - (P : Law (Letters n α)) : - P.kl (pack (fun _ : Fin n => uniform α)) = - n*log (Fintype.card α) - P.entropy := by - have h : P.dominated (pack (fun _ => uniform α)) := by - intro x _ - rw [pack_pos] - intro i - rw [uniform_apply] - positivity - unfold kl - rw [crossE_pack h] - simp_rw [crossE, uniform_apply, log_inv,neg_neg,Ex_const,Finset.sum_const,Finset.card_univ, - Fintype.card_fin,nsmul_eq_mul] - -lemma sum_deficit (P : Law Ω) [Nonempty α] (X : Ω → Letters n α) : - ∑ j ∈ Finset.range n, (log (Fintype.card α)-rateH P X j) = - (P.send X).kl (pack (fun _ => uniform α)) := by - have h := H_prefix P X n le_rfl - rw [prefix_all] at h - rw [Finset.sum_sub_distrib,kl_uniform,← h] - simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul]; rfl - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Blocks +/-! # Entropy increments along a prefix -/ + +open Function Set Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {n m : ℕ} {α Ω G : Type} [Fintype α] [Fintype Ω] [Fintype G] + +def rateH (P : Law Ω) (X : Ω → Letters n α) (j : ℕ) := + if h : j < n then P.Hcond (readLetter X j h) (prefixB X j h.le) else 0 + +lemma rate_lt (P : Law Ω) (X : Ω → Letters n α) {j : ℕ} (h : j < n) : + rateH P X j = P.Hcond (readLetter X j h) (prefixB X j h.le) := + dite_eq_left h + +lemma H_prefix (P : Law Ω) (X : Ω → Letters n α) (l : ℕ) (h : l ≤ n) : + P.H (prefixB X l h) = ∑ j ∈ Finset.range l, rateH P X j := by + induction l with + | zero => + rw [prefix_zero, H_constant]; simp + | succ l ih => + have hh := P.H_partition (X:=prefixB X (l+1) h) + (Y:=fun x => (readLetter X l h x, prefixB X l (by omega) x)) + (by intro x y; simp only [Prod.mk.injEq]; exact prefix_last_partition X l h x y) + rw [hh,H_pair_chain, ih, Finset.sum_range_succ, rate_lt _ _ h] + +lemma H_post_prefix (P : Law Ω) (X : Ω → Letters n α) (j l : ℕ) (h : j+l ≤ n) : + let W := prefixB X j (by omega) + (P.send W).Ex (fun w => (P.after W w).H (readBlock X j l h)) = + ∑ i ∈ Finset.range l, rateH P X (j+i) := by + intro W + rw [← Hcond_after,Hcond] + have H : P.H (prefixB X (j+l) h) = P.H (fun x => (readBlock X j l h x, W x)) := by + refine P.H_partition fun x y => ?_ + rw [prefix_partition, Prod.mk.injEq] + exact and_comm + rw [← H,H_prefix] + dsimp only [W] + rw [H_prefix, Finset.sum_range_add] + ring + + +private abbrev S := WordSample + +lemma conditional_block_exch {P : Law (S n α G)} (hp : exch P) + {j m} (hh : j+m ≤ n) (w : Letters j α) : + exch ((P.after (prefixB Prod.fst j (by omega)) w).send (fun p => + (readBlock Prod.fst j m hh p,p.2))) := by + apply exch_subSample_cond hp (offset j m hh) (offset_injective _ _ _) + (offset 0 j (by omega)) + rintro i ⟨b,hb⟩ + have H := congrArg Fin.val hb + simp only [offset] at H + omega + +lemma conditional_block_marg {P : Law (S n α G)} (hp : exch P) + {j m} (hh : j+m ≤ n) (w : Letters j α) (i : Fin m) : + let R := P.after (prefixB Prod.fst j (by omega)) w + R.send (fun x => readBlock Prod.fst j m hh x i) = + R.send (readLetter Prod.fst j (by have := i.isLt; omega)) := by + intro R + let i₀ : Fin m := ⟨0,by have := i.isLt; omega⟩ + have he := same_marginal (conditional_block_exch hp hh w) i i₀ + simp only [send_send] at he + exact he + +lemma rate_antitone_succ {P : Law (S n α G)} (hp : exch P) + {j} (h : j+1 < n) : rateH P Prod.fst (j+1) ≤ rateH P Prod.fst j := by + let X : S n α G → Letters n α := Prod.fst + let W := prefixB X j (by omega) + let V := prefixB X (j+1) h.le + have H : P.Hcond (readLetter X (j+1) h) W = P.Hcond (readLetter X j (by omega)) W := by + rw [Hcond_after,Hcond_after] + have HH (w) := conditional_block_marg hp (j:=j) (m:=2) (by omega) w (1:Fin 2) + have hEq (w : Letters j α) : + (P.after W w).send (readLetter X (j+1) h) = + (P.after W w).send (readLetter X j (by omega)) := HH w + simp_rw [Law.H,hEq] + rw [rate_lt _ _ h, rate_lt _ _ (by omega : j < n)] + change P.Hcond _ V ≤ P.Hcond (readLetter X j _) W + rw [← H] + apply Hcond_antitone + exact fun a b hh => ((prefix_last_partition X j (by omega) a b).mp hh).2 + +lemma rate_antitone {P : Law (S n α G)} (hp : exch P) : + Antitone (rateH P Prod.fst) := by + refine antitone_nat_of_succ_le fun k => ?_ + by_cases h : k+1 simp + | succ k => apply log_nonneg; simp + +/-- The joint divergence of the conditional block from the product of its row marginals. -/ +lemma avg_block_kl {P : Law (S n α G)} (hp : exch P) + {j m} (hh : j+m ≤ n) (hj : j + let R := P.after W w + kl (R.send V) (pack (fun _ : Fin m => R.send (readLetter X j hj)))) = + ∑ i ∈ Finset.range m, (rateH P X j - rateH P X (j+i)) := by + intro X W V + let f := readLetter X j hj + let A (w : Letters j α) := P.after W w + have hk (w : Letters j α) : + kl ((A w).send V) (pack (fun _ : Fin m => (A w).send f)) = + (m:ℝ) * (A w).H f - (A w).H V := by + have hm (i : Fin m) := conditional_block_marg hp hh w i + have hm' (i : Fin m) : + ((A w).send V).send (fun x => x i) = (A w).send f := by rw [send_send]; exact hm i + have he : dominated ((A w).send V) (pack (fun _ : Fin m => (A w).send f)) := by + intro x hx + rw [pack_pos] + intro i + rw [← hm' i]; exact send_pos _ _ hx + rw [kl,crossE_pack he] + simp_rw [hm'] + simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] + rfl + change (P.send W).Ex (fun w => kl ((A w).send V) (pack (fun _ : Fin m => (A w).send f))) = _ + have h1 : (P.send W).Ex (fun w => (A w).H f) = rateH P X j := + (rate_lt P X hj ▸ Hcond_after P f W).symm + have h2 : (P.send W).Ex (fun w => (A w).H V) = _ := H_post_prefix P X j m hh + simp_rw [hk,Ex_sub,Ex_lmul,h1,h2,Finset.sum_sub_distrib,Finset.sum_const,Finset.card_range, + nsmul_eq_mul] + +lemma kl_uniform {α : Type} [Fintype α] [Nonempty α] + (P : Law (Letters n α)) : + P.kl (pack (fun _ : Fin n => uniform α)) = + n*log (Fintype.card α) - P.entropy := by + have h : P.dominated (pack (fun _ => uniform α)) := by + intro x _ + rw [pack_pos] + intro i + rw [uniform_apply] + positivity + unfold kl + rw [crossE_pack h] + simp_rw [crossE, uniform_apply, log_inv,neg_neg,Ex_const,Finset.sum_const,Finset.card_univ, + Fintype.card_fin,nsmul_eq_mul] + +lemma sum_deficit (P : Law Ω) [Nonempty α] (X : Ω → Letters n α) : + ∑ j ∈ Finset.range n, (log (Fintype.card α)-rateH P X j) = + (P.send X).kl (pack (fun _ => uniform α)) := by + have h := H_prefix P X n le_rfl + rw [prefix_all] at h + rw [Finset.sum_sub_distrib,kl_uniform,← h] + simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul]; rfl + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Projection.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Projection.lean index b9dc808d697aad87ec02617a2d50ecac96516ba4..a2b13849aaf323f410d3fb20fa20254652a7d9d3 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Projection.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Projection.lean @@ -1,179 +1,179 @@ -import OAI.Analysis.FourierLLogL.Compress.Rent -/-! # Data processing for contextual cost -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A B V W G E Ω Ω' : Type} [Fintype A] [Fintype B] - [Fintype V] [Fintype W] [Fintype G] [Fintype E] [Nonempty E] - [Fintype Ω] [Fintype Ω'] [AddCommGroup G] {n : ℕ} -set_option linter.unusedSectionVars false - -lemma Hcond_congr {P : Law Ω} {X Y : Ω → A} {W W' : Ω → B} - (h : ∀ a, 0 < P a → X a = Y a) (h' : ∀ a, 0 < P a → W a = W' a) : - P.Hcond X W = P.Hcond Y W' := by - unfold Hcond - rw [H_congr_mem P h'] - congr 1 - refine H_congr_mem P fun a ha => ?_ - rw [h a ha,h' a ha] - -lemma rent_proj_eq (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) - (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) (σ : A → B) : - let Y := ctxt σ X - rent (tag ∘ σ) (w ∘ σ) u c P X U = - rent tag w u c P Y U + - ((∑ i, P.Hcond (readP X i) (readP Y i)) - - P.Hcond X (fun x => (Y x, ans tag Y (fun a => u (U a)) x))) := by - intro Y - let Z := ans tag Y (fun a => u (U a)) - have h1 (i : Fin n) : - P.Hcond (readP X i) ((w ∘ σ) ∘ readP X i) = - P.Hcond (readP X i) (readP Y i) + P.Hcond (readP Y i) (w ∘ readP Y i) := by - let R := (w ∘ readP Y i) - have he := Hcond_reduce P (readP X i) R σ - change P.Hcond _ R = _ + P.Hcond (readP Y i) R - rw [he]; congr 1 - apply Hcond_right_partition - simp only [R,Y,ctxt,readP,comp_apply,Prod.mk.injEq] - grind - have h2 : - P.Hcond X (fun x => (Z x,ctxt w Y x)) = - P.Hcond X (fun x => (Y x,Z x)) + - P.Hcond Y (fun x => (Z x,ctxt w Y x)) := by - nth_rw 1 [Hcond_reduce P X _ (fun v i => σ (v i))] - congr 1 - apply Hcond_right_partition - intro a b - simp only [Prod.mk.injEq,Y,ctxt,funext_iff] - grind - unfold rent - change _ - P.Hcond X (fun x => (Z x,ctxt w Y x))+_ = _ - rw [h2] - simp_rw [h1,Finset.sum_add_distrib] - dsimp only [Z] - ring - -lemma rent_projection (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) - (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) (σ : A → B) : - rent tag w u c P (ctxt σ X) U ≤ rent (tag ∘ σ) (w ∘ σ) u c P X U := by - rw [rent_proj_eq] - refine le_add_of_nonneg_right (sub_nonneg.mpr ?_) - let Y := ctxt σ X - let Z (x : Ω) := (Y x,ans tag Y (fun x => u (U x)) x) - refine (Hcond_vec_le P X Z).trans (Finset.sum_le_sum fun i _ => ?_) - apply Hcond_antitone - intro a b h - exact congrFun (congrArg Prod.fst h) i - -lemma rent_congr_mem (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (P : Law Ω) {X Y : Ω → Letters n A} (U : Ω → E) - (h : ∀ x, 0 < P x → X x=Y x) : - rent tag w u c P X U = rent tag w u c P Y U := by - unfold rent - suffices H : ∑ i, P.Hcond (readP X i) (w ∘ readP X i) = - ∑ i, P.Hcond (readP Y i) (w ∘ readP Y i) by - have hh (x : Ω) (hx : 0 < P x) : - (ans tag X (fun x => u (U x)) x, ctxt w X x) = - (ans tag Y (fun x => u (U x)) x, ctxt w Y x) := by - unfold ans ctxt; rw [h x hx] - rw [H] - have ht := Hcond_congr h (W:=fun x => (ans tag X (fun x => u (U x)) x, ctxt w X x)) - (W':=fun x => (ans tag Y (fun x => u (U x)) x, ctxt w Y x)) hh - rw [ht] - refine Finset.sum_congr rfl fun i _ => ?_ - apply Hcond_congr - · exact fun x hx => congrFun (h x hx) _ - · exact fun x hx => congrArg w (congrFun (h x hx) _) - -lemma opt_proj (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) - (p : Law A) (σ : A → B) (n : ℕ) : - opt n (p.send σ) tag w u c = opt n p (tag ∘ σ) (w ∘ σ) u c := by - classical - apply le_antisymm - · obtain ⟨P,hP,he⟩ := (opt_spec n p (tag ∘ σ) (w ∘ σ) u c).1 - rw [← he] - let X : WordSample n A E → Letters n A := Prod.fst - exact (opt_le_rent tag w u c (p.send σ) P (ctxt σ X) Prod.snd fun i => by - rw [← hP i,send_send]; rfl).trans (rent_projection tag w u c P _ _ _) - obtain ⟨P,hP,he⟩ := (opt_spec n (p.send σ) tag w u c).1 - let X : WordSample n B E → Letters n B := Prod.fst - let Q x := pack (fun i : Fin n => p.after σ (X x i)) - let R := P.withNext Q - let Y : WordSample n B E × (Letters n A) → Letters n A := Prod.snd - let B0 (z : WordSample n B E × Letters n A) := X z.1 - let error (z : WordSample n B E × Letters n A) := z.1.2 - have hr : hasRows p R Y := by - intro i - apply ext_of_Ex; intro f - rw [Ex_send] - change R.Ex (fun z => f (z.2 i)) = p.Ex f - unfold R - rw [Ex_withNext] - simp_rw [Q, Ex_pack_proj _ i f] - change P.Ex (fun x => (p.after σ (readP X i x)).Ex f) = _ - rw [← Ex_send P (readP X i) (fun b => (p.after σ b).Ex f),show P.send (readP X i) = _ from hP i] - exact p.after_Ex_sum σ (fun _ => f) - have heq (x : WordSample n B E × Letters n A) (hx : 0 < R x) : ctxt σ Y x = B0 x := by - rcases x with ⟨a,b⟩ - change 0 < P a * Q a b at hx - have hpos : 0 < P a ∧ 0 < Q a b := - (mul_pos_iff.mp hx).resolve_right (by intro hh; linarith [hh.1,P.nonneg a]) - funext i - have hi := (pack_pos (fun i => p.after σ (X a i)) b).mp hpos.2 i - have hmarg := P.send_pos (readP X i) hpos.1 - rw [show P.send (readP X i) = _ from hP i] at hmarg - exact ((after_mem hmarg).mp hi).1 - have hsrc : rent tag w u c R B0 error = opt n (p.send σ) tag w u c := by - have h1 := send_fst_withNext P Q - have hi := rent_send tag w u c R X Prod.snd Prod.fst - change rent tag w u c (R.send Prod.fst) X Prod.snd = _ at hi - rw [h1] at hi - exact hi.symm.trans he - refine (opt_le_rent (tag ∘ σ) (w ∘ σ) u c p R Y error hr).trans ?_ - rw [rent_proj_eq] - let B1 := ctxt σ Y - let Z (b : WordSample n B E × Letters n A) := - (B1 b, ans tag B1 (fun x => u (error x)) b) - let Z0 (b : WordSample n B E × Letters n A) := - (B0 b, ans tag B0 (fun x => u (error x)) b) - change rent tag w u c R B1 error + (_ - R.Hcond Y Z) ≤ _ - rw [rent_congr_mem tag w u c R error heq,hsrc] - suffices H : ∑ i, R.Hcond (readP Y i) (readP B1 i) ≤ R.Hcond Y Z by linarith - have hZ : R.Hcond Y Z = R.Hcond Y Z0 := - Hcond_congr (fun _ _ => rfl) fun x hx => by unfold Z Z0 ans; rw [show B1 x = B0 x from heq x hx] - rw [hZ] - apply le_trans (b:=R.Hcond Y Prod.fst) - · have H1 : R.Hcond Y Prod.fst = P.Ex (fun x => (Q x).entropy) := by - have H := Hcond_withNext' P Q (fun _ y => y) - simpa only [H_self] using H - have H2 (i : Fin n) : R.Hcond (readP Y i) (readP B1 i) = p.Hcond (fun a => a) σ := by - rw [← hr i] - rw [Hcond_send]; rfl - have H3 (i : Fin n) : - P.Ex (fun x => (p.after σ (X x i)).entropy) = p.Hcond (fun a => a) σ := by - rw [Hcond_after] - simp_rw [H_self] - have hi := hP i - rw [← hi,Ex_send]; rfl - rw [H1]; simp_rw [Q,entropy_pack,H2,Ex_finset_sum,H3]; exact le_rfl - · apply Hcond_antitone - rintro ⟨a,b⟩ ⟨a',b'⟩ h - cases (show a=a' from h) - rfl - -lemma opt_refined (tag : B → G) (w : B → V) (w' : A → W) (u : E → G) (c : E → ℝ) - (p : Law A) (σ : A → B) (n : ℕ) (g : W → V) (h : w ∘ σ = g ∘ w') : - opt n p (tag ∘ σ) w' u c ≤ opt n (p.send σ) tag w u c := by - rw [opt_proj tag w] - obtain ⟨P,hP,he⟩ := (opt_spec n p (tag ∘ σ) (w ∘ σ) u c).1 - exact (opt_le_rent _ _ _ _ p P _ _ hP).trans - ((rent_antitone _ w' (w ∘ σ) u c g h P Prod.fst Prod.snd).trans he.le) - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Rent +/-! # Data processing for contextual cost -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A B V W G E Ω Ω' : Type} [Fintype A] [Fintype B] + [Fintype V] [Fintype W] [Fintype G] [Fintype E] [Nonempty E] + [Fintype Ω] [Fintype Ω'] [AddCommGroup G] {n : ℕ} +set_option linter.unusedSectionVars false + +lemma Hcond_congr {P : Law Ω} {X Y : Ω → A} {W W' : Ω → B} + (h : ∀ a, 0 < P a → X a = Y a) (h' : ∀ a, 0 < P a → W a = W' a) : + P.Hcond X W = P.Hcond Y W' := by + unfold Hcond + rw [H_congr_mem P h'] + congr 1 + refine H_congr_mem P fun a ha => ?_ + rw [h a ha,h' a ha] + +lemma rent_proj_eq (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) + (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) (σ : A → B) : + let Y := ctxt σ X + rent (tag ∘ σ) (w ∘ σ) u c P X U = + rent tag w u c P Y U + + ((∑ i, P.Hcond (readP X i) (readP Y i)) - + P.Hcond X (fun x => (Y x, ans tag Y (fun a => u (U a)) x))) := by + intro Y + let Z := ans tag Y (fun a => u (U a)) + have h1 (i : Fin n) : + P.Hcond (readP X i) ((w ∘ σ) ∘ readP X i) = + P.Hcond (readP X i) (readP Y i) + P.Hcond (readP Y i) (w ∘ readP Y i) := by + let R := (w ∘ readP Y i) + have he := Hcond_reduce P (readP X i) R σ + change P.Hcond _ R = _ + P.Hcond (readP Y i) R + rw [he]; congr 1 + apply Hcond_right_partition + simp only [R,Y,ctxt,readP,comp_apply,Prod.mk.injEq] + grind + have h2 : + P.Hcond X (fun x => (Z x,ctxt w Y x)) = + P.Hcond X (fun x => (Y x,Z x)) + + P.Hcond Y (fun x => (Z x,ctxt w Y x)) := by + nth_rw 1 [Hcond_reduce P X _ (fun v i => σ (v i))] + congr 1 + apply Hcond_right_partition + intro a b + simp only [Prod.mk.injEq,Y,ctxt,funext_iff] + grind + unfold rent + change _ - P.Hcond X (fun x => (Z x,ctxt w Y x))+_ = _ + rw [h2] + simp_rw [h1,Finset.sum_add_distrib] + dsimp only [Z] + ring + +lemma rent_projection (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) + (P : Law Ω) (X : Ω → Letters n A) (U : Ω → E) (σ : A → B) : + rent tag w u c P (ctxt σ X) U ≤ rent (tag ∘ σ) (w ∘ σ) u c P X U := by + rw [rent_proj_eq] + refine le_add_of_nonneg_right (sub_nonneg.mpr ?_) + let Y := ctxt σ X + let Z (x : Ω) := (Y x,ans tag Y (fun x => u (U x)) x) + refine (Hcond_vec_le P X Z).trans (Finset.sum_le_sum fun i _ => ?_) + apply Hcond_antitone + intro a b h + exact congrFun (congrArg Prod.fst h) i + +lemma rent_congr_mem (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (P : Law Ω) {X Y : Ω → Letters n A} (U : Ω → E) + (h : ∀ x, 0 < P x → X x=Y x) : + rent tag w u c P X U = rent tag w u c P Y U := by + unfold rent + suffices H : ∑ i, P.Hcond (readP X i) (w ∘ readP X i) = + ∑ i, P.Hcond (readP Y i) (w ∘ readP Y i) by + have hh (x : Ω) (hx : 0 < P x) : + (ans tag X (fun x => u (U x)) x, ctxt w X x) = + (ans tag Y (fun x => u (U x)) x, ctxt w Y x) := by + unfold ans ctxt; rw [h x hx] + rw [H] + have ht := Hcond_congr h (W:=fun x => (ans tag X (fun x => u (U x)) x, ctxt w X x)) + (W':=fun x => (ans tag Y (fun x => u (U x)) x, ctxt w Y x)) hh + rw [ht] + refine Finset.sum_congr rfl fun i _ => ?_ + apply Hcond_congr + · exact fun x hx => congrFun (h x hx) _ + · exact fun x hx => congrArg w (congrFun (h x hx) _) + +lemma opt_proj (tag : B → G) (w : B → V) (u : E → G) (c : E → ℝ) + (p : Law A) (σ : A → B) (n : ℕ) : + opt n (p.send σ) tag w u c = opt n p (tag ∘ σ) (w ∘ σ) u c := by + classical + apply le_antisymm + · obtain ⟨P,hP,he⟩ := (opt_spec n p (tag ∘ σ) (w ∘ σ) u c).1 + rw [← he] + let X : WordSample n A E → Letters n A := Prod.fst + exact (opt_le_rent tag w u c (p.send σ) P (ctxt σ X) Prod.snd fun i => by + rw [← hP i,send_send]; rfl).trans (rent_projection tag w u c P _ _ _) + obtain ⟨P,hP,he⟩ := (opt_spec n (p.send σ) tag w u c).1 + let X : WordSample n B E → Letters n B := Prod.fst + let Q x := pack (fun i : Fin n => p.after σ (X x i)) + let R := P.withNext Q + let Y : WordSample n B E × (Letters n A) → Letters n A := Prod.snd + let B0 (z : WordSample n B E × Letters n A) := X z.1 + let error (z : WordSample n B E × Letters n A) := z.1.2 + have hr : hasRows p R Y := by + intro i + apply ext_of_Ex; intro f + rw [Ex_send] + change R.Ex (fun z => f (z.2 i)) = p.Ex f + unfold R + rw [Ex_withNext] + simp_rw [Q, Ex_pack_proj _ i f] + change P.Ex (fun x => (p.after σ (readP X i x)).Ex f) = _ + rw [← Ex_send P (readP X i) (fun b => (p.after σ b).Ex f),show P.send (readP X i) = _ from hP i] + exact p.after_Ex_sum σ (fun _ => f) + have heq (x : WordSample n B E × Letters n A) (hx : 0 < R x) : ctxt σ Y x = B0 x := by + rcases x with ⟨a,b⟩ + change 0 < P a * Q a b at hx + have hpos : 0 < P a ∧ 0 < Q a b := + (mul_pos_iff.mp hx).resolve_right (by intro hh; linarith [hh.1,P.nonneg a]) + funext i + have hi := (pack_pos (fun i => p.after σ (X a i)) b).mp hpos.2 i + have hmarg := P.send_pos (readP X i) hpos.1 + rw [show P.send (readP X i) = _ from hP i] at hmarg + exact ((after_mem hmarg).mp hi).1 + have hsrc : rent tag w u c R B0 error = opt n (p.send σ) tag w u c := by + have h1 := send_fst_withNext P Q + have hi := rent_send tag w u c R X Prod.snd Prod.fst + change rent tag w u c (R.send Prod.fst) X Prod.snd = _ at hi + rw [h1] at hi + exact hi.symm.trans he + refine (opt_le_rent (tag ∘ σ) (w ∘ σ) u c p R Y error hr).trans ?_ + rw [rent_proj_eq] + let B1 := ctxt σ Y + let Z (b : WordSample n B E × Letters n A) := + (B1 b, ans tag B1 (fun x => u (error x)) b) + let Z0 (b : WordSample n B E × Letters n A) := + (B0 b, ans tag B0 (fun x => u (error x)) b) + change rent tag w u c R B1 error + (_ - R.Hcond Y Z) ≤ _ + rw [rent_congr_mem tag w u c R error heq,hsrc] + suffices H : ∑ i, R.Hcond (readP Y i) (readP B1 i) ≤ R.Hcond Y Z by linarith + have hZ : R.Hcond Y Z = R.Hcond Y Z0 := + Hcond_congr (fun _ _ => rfl) fun x hx => by unfold Z Z0 ans; rw [show B1 x = B0 x from heq x hx] + rw [hZ] + apply le_trans (b:=R.Hcond Y Prod.fst) + · have H1 : R.Hcond Y Prod.fst = P.Ex (fun x => (Q x).entropy) := by + have H := Hcond_withNext' P Q (fun _ y => y) + simpa only [H_self] using H + have H2 (i : Fin n) : R.Hcond (readP Y i) (readP B1 i) = p.Hcond (fun a => a) σ := by + rw [← hr i] + rw [Hcond_send]; rfl + have H3 (i : Fin n) : + P.Ex (fun x => (p.after σ (X x i)).entropy) = p.Hcond (fun a => a) σ := by + rw [Hcond_after] + simp_rw [H_self] + have hi := hP i + rw [← hi,Ex_send]; rfl + rw [H1]; simp_rw [Q,entropy_pack,H2,Ex_finset_sum,H3]; exact le_rfl + · apply Hcond_antitone + rintro ⟨a,b⟩ ⟨a',b'⟩ h + cases (show a=a' from h) + rfl + +lemma opt_refined (tag : B → G) (w : B → V) (w' : A → W) (u : E → G) (c : E → ℝ) + (p : Law A) (σ : A → B) (n : ℕ) (g : W → V) (h : w ∘ σ = g ∘ w') : + opt n p (tag ∘ σ) w' u c ≤ opt n (p.send σ) tag w u c := by + rw [opt_proj tag w] + obtain ⟨P,hP,he⟩ := (opt_spec n p (tag ∘ σ) (w ∘ σ) u c).1 + exact (opt_le_rent _ _ _ _ p P _ _ hP).trans + ((rent_antitone _ w' (w ∘ σ) u c g h P Prod.fst Prod.snd).trans he.le) + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Rent.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Rent.lean index 1e623537a95489ec385bcc65dd350a23299dba36..de016240c272e86a6ad4d751e3490998964c603b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Rent.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Rent.lean @@ -1,171 +1,171 @@ -import OAI.Analysis.FourierLLogL.Compress.Blocks -/-! # Cost of a coupling of contextual rows -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A B U V W G Ω Ω' E : Type} [Fintype A] [Fintype B] - [Fintype U] [Fintype V] [Fintype W] [Fintype G] [Fintype E] [Nonempty E] - [AddCommGroup G] [Fintype Ω] [Fintype Ω'] - {I : Type} [Fintype I] [DecidableEq I] {n : ℕ} - -/-- Context part of a vector of full rows. -/ -def ctxt (w : A → V) (X : Ω → I → A) (a : Ω) (i : I) := w (X a i) -def readP (X : Ω → I → A) (i : I) (a : Ω) : A := X a i - -/-- Contextual cost of a coupling with tag-sum output and random error ø of alphabet E. -/ -def rent (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (P : Law Ω) (X : Ω → I → A) (U : Ω → E) := - (∑ i, P.Hcond (readP X i) (w ∘ readP X i)) - - P.Hcond X (fun a => (ans tag X (fun x => u (U x)) a, ctxt w X a)) + - P.Ex (fun a => c (U a)) - -def hasRows (p : Law A) (P : Law Ω) (X : Ω → I → A) : Prop := - ∀ i, P.send (readP X i) = p - -omit [DecidableEq I] in -lemma rent_first {P : Law Ω} {X : Ω → I → A} {p : Law A} (h : hasRows p P X) - (w : A → V) : - ∑ i, P.Hcond (readP X i) (w ∘ readP X i) = Fintype.card I * p.Hcond (fun x => x) w := by - have heq (i : I) : - P.Hcond (readP X i) (w ∘ readP X i) = p.Hcond (fun x => x) w := by - rw [← h i,Hcond_send]; rfl - simp_rw [heq,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - -omit [Fintype E] [Nonempty E] in -lemma rent_nonneg (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (hc : ∀ x, 0 ≤ c x) (P : Law Ω) (X : Ω → I → A) (U : Ω → E) : - 0 ≤ rent tag w u c P X U := by - refine add_nonneg (sub_nonneg.mpr ?_) (P.Ex_nonneg fun a => hc _) - refine (P.Hcond_vec_le X _).trans (Finset.sum_le_sum fun i _ => ?_) - apply Hcond_antitone - intro a b hh - exact congrFun (congrArg Prod.snd hh) i - -omit [Fintype E] [Nonempty E] in -lemma rent_send (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (P : Law Ω) (X : Ω' → I → A) (U : Ω' → E) (b : Ω → Ω') : - rent tag w u c (P.send b) X U = rent tag w u c P (fun a => X (b a)) (fun a => U (b a)) := by - unfold rent; simp_rw [Hcond_send,Ex_send] - rfl - -omit [Fintype E] [Nonempty E] in -lemma rent_cont (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (X : Ω → I → A) (U : Ω → E) : - Continuous (fun P : Law Ω => rent tag w u c P X U) := - ((continuous_finsetSum _ fun _ _ => continuous_Hcond _ _).sub (continuous_Hcond _ _)).add - (continuous_Ex _) - -/-- Min rent uses r rows. -/ -def opt (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) := - sInf ((fun P : Law (WordSample n A E) => rent tag w u c P Prod.fst Prod.snd) '' - {P | hasRows p P Prod.fst}) - -lemma opt_spec (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) : - let C (P : Law (WordSample n A E)) := rent tag w u c P Prod.fst Prod.snd - (∃ P : Law (WordSample n A E), hasRows p P Prod.fst ∧ C P = opt n p tag w u c) ∧ - ∀ P : Law (WordSample n A E), hasRows p P Prod.fst → opt n p tag w u c ≤ C P := by - classical - intro C - let s := {P : Law (WordSample n A E) | hasRows p P Prod.fst} - have hs : IsCompact s := by - apply IsClosed.isCompact - have heq : s = ⋂ i, {P | P.send (readP Prod.fst i) = p} := by ext; simp [s,hasRows] - rw [heq] - exact isClosed_iInter fun i => isClosed_eq (continuous_send _) continuous_const - let Q := pack fun _ : Fin n => p - obtain ⟨e⟩ := (inferInstance : Nonempty E) - have h1 : s.Nonempty := by - refine ⟨Q.send (fun x => (x,e)),fun i => ?_⟩ - rw [send_send]; exact marginalPack _ i - have hh := hs.image (rent_cont tag w u c Prod.fst Prod.snd) - have H1 : (C '' s).Nonempty := h1.image _ - exact ⟨hh.sInf_mem H1,fun P h => csInf_le hh.bddBelow ⟨_, h,rfl⟩⟩ - -lemma opt_le_rent (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (p : Law A) (P : Law Ω) (X : Ω → Fin n → A) (U : Ω → E) (h : hasRows p P X) : - opt n p tag w u c ≤ rent tag w u c P X U := by - let Q := P.send (fun a => (X a,U a)) - have hh := (opt_spec n p tag w u c).2 Q (by - intro i - dsimp only [Q]; rw [send_send]; exact h i) - dsimp only [Q] at hh - rw [rent_send] at hh - exact hh - -lemma opt_nonneg (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) - (hc : ∀ x, 0 ≤ c x) : 0 ≤ opt n p tag w u c := by - obtain ⟨P,_,he⟩ := (opt_spec n p tag w u c).1 - rw [← he] - exact rent_nonneg tag w u c hc P _ _ - - -lemma Hcond_function (P : Law Ω) (X : Ω → A) (W : A → V) : - P.Hcond X (fun x => W (X x)) = P.H X - P.H (fun x => W (X x)) := by - unfold Hcond - congr 1 - apply H_partition - simp [Prod.mk.injEq]; grind - -/-- Chain rule for deterministic reduction f(X). -/ -lemma Hcond_reduce (P : Law Ω) (X : Ω → A) (W : Ω → V) (f : A → B) : - P.Hcond X W = P.Hcond X (fun x => (f (X x),W x)) + P.Hcond (fun x => f (X x)) W := by - rw [← Hcond_chain P X (fun x => f (X x)) W] - unfold Hcond - congr 1 - refine H_partition _ fun a b => ?_ - simp only [Prod.mk.injEq] - constructor <;> grind - -omit [Fintype E] [Nonempty E] in -/-- Improving contexts, for the same full rows and tag, reduces the rent. -/ -lemma rent_antitone (tag : A → G) (w : A → V) (w' : A → W) (u : E → G) (c : E → ℝ) - (p : V → W) (h : w' = p ∘ w) - (P : Law Ω) (X : Ω → I → A) (U : Ω → E) : - rent tag w u c P X U ≤ rent tag w' u c P X U := by - subst w' - let Y := ans tag X (fun x => u (U x)) - let W₁ := ctxt w X - let W₂ := ctxt (p ∘ w) X - have h (i : I) : P.Hcond (readP X i) ((p ∘ w) ∘ readP X i) = - P.Hcond (readP X i) (w ∘ readP X i) + P.Hcond (readP W₁ i) (readP W₂ i) := by - have h1 := Hcond_function P (readP X i) (p ∘ w) - have h2 := Hcond_function P (readP X i) w - have h3 := Hcond_function P (readP W₁ i) p - change P.Hcond (readP X i) (fun x => p (w ((readP X i) x))) = _ - dsimp only [comp_apply] at h1 - rw [h1] - change _ = P.Hcond (readP X i) (fun x => w ((readP X i) x)) + - P.Hcond (readP W₁ i) (fun x => p (readP W₁ i x)) - rw [h2,h3]; simp only [W₁]; unfold readP ctxt; ring - let vmap (x : I → A) (i : I) := w (x i) - let Z₁ (x : Ω) := (Y x,W₁ x) - let Z₂ (x : Ω) := (Y x,W₂ x) - have hz : P.Hcond X Z₂ = P.Hcond X Z₁ + P.Hcond W₁ Z₂ := by - nth_rw 1 [Hcond_reduce P X Z₂ vmap] - have H : P.Hcond X (fun x => (vmap (X x), Z₂ x)) = P.Hcond X Z₁ := by - apply Hcond_right_partition - intro a b - simp only [Z₁,Z₂,W₁,W₂,ctxt,vmap,Prod.mk.injEq,funext_iff,comp_apply] - constructor - · tauto - · rintro ⟨hh,hx⟩ - simp [hx,hh] - rw [H]; rfl - have H : P.Hcond W₁ Z₂ ≤ ∑ i, P.Hcond (readP W₁ i) (readP W₂ i) := by - refine (P.Hcond_vec_le W₁ Z₂).trans (Finset.sum_le_sum fun i _ => ?_) - exact Hcond_antitone P _ fun _ _ hh => congrFun (congrArg Prod.snd hh) i - unfold rent - refine add_le_add_left ?_ _ - change _ - P.Hcond X Z₁ ≤ _ - P.Hcond X Z₂ - rw [hz] - simp_rw [h,Finset.sum_add_distrib] - linarith - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Blocks +/-! # Cost of a coupling of contextual rows -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A B U V W G Ω Ω' E : Type} [Fintype A] [Fintype B] + [Fintype U] [Fintype V] [Fintype W] [Fintype G] [Fintype E] [Nonempty E] + [AddCommGroup G] [Fintype Ω] [Fintype Ω'] + {I : Type} [Fintype I] [DecidableEq I] {n : ℕ} + +/-- Context part of a vector of full rows. -/ +def ctxt (w : A → V) (X : Ω → I → A) (a : Ω) (i : I) := w (X a i) +def readP (X : Ω → I → A) (i : I) (a : Ω) : A := X a i + +/-- Contextual cost of a coupling with tag-sum output and random error ø of alphabet E. -/ +def rent (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (P : Law Ω) (X : Ω → I → A) (U : Ω → E) := + (∑ i, P.Hcond (readP X i) (w ∘ readP X i)) - + P.Hcond X (fun a => (ans tag X (fun x => u (U x)) a, ctxt w X a)) + + P.Ex (fun a => c (U a)) + +def hasRows (p : Law A) (P : Law Ω) (X : Ω → I → A) : Prop := + ∀ i, P.send (readP X i) = p + +omit [DecidableEq I] in +lemma rent_first {P : Law Ω} {X : Ω → I → A} {p : Law A} (h : hasRows p P X) + (w : A → V) : + ∑ i, P.Hcond (readP X i) (w ∘ readP X i) = Fintype.card I * p.Hcond (fun x => x) w := by + have heq (i : I) : + P.Hcond (readP X i) (w ∘ readP X i) = p.Hcond (fun x => x) w := by + rw [← h i,Hcond_send]; rfl + simp_rw [heq,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + +omit [Fintype E] [Nonempty E] in +lemma rent_nonneg (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (hc : ∀ x, 0 ≤ c x) (P : Law Ω) (X : Ω → I → A) (U : Ω → E) : + 0 ≤ rent tag w u c P X U := by + refine add_nonneg (sub_nonneg.mpr ?_) (P.Ex_nonneg fun a => hc _) + refine (P.Hcond_vec_le X _).trans (Finset.sum_le_sum fun i _ => ?_) + apply Hcond_antitone + intro a b hh + exact congrFun (congrArg Prod.snd hh) i + +omit [Fintype E] [Nonempty E] in +lemma rent_send (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (P : Law Ω) (X : Ω' → I → A) (U : Ω' → E) (b : Ω → Ω') : + rent tag w u c (P.send b) X U = rent tag w u c P (fun a => X (b a)) (fun a => U (b a)) := by + unfold rent; simp_rw [Hcond_send,Ex_send] + rfl + +omit [Fintype E] [Nonempty E] in +lemma rent_cont (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (X : Ω → I → A) (U : Ω → E) : + Continuous (fun P : Law Ω => rent tag w u c P X U) := + ((continuous_finsetSum _ fun _ _ => continuous_Hcond _ _).sub (continuous_Hcond _ _)).add + (continuous_Ex _) + +/-- Min rent uses r rows. -/ +def opt (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) := + sInf ((fun P : Law (WordSample n A E) => rent tag w u c P Prod.fst Prod.snd) '' + {P | hasRows p P Prod.fst}) + +lemma opt_spec (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) : + let C (P : Law (WordSample n A E)) := rent tag w u c P Prod.fst Prod.snd + (∃ P : Law (WordSample n A E), hasRows p P Prod.fst ∧ C P = opt n p tag w u c) ∧ + ∀ P : Law (WordSample n A E), hasRows p P Prod.fst → opt n p tag w u c ≤ C P := by + classical + intro C + let s := {P : Law (WordSample n A E) | hasRows p P Prod.fst} + have hs : IsCompact s := by + apply IsClosed.isCompact + have heq : s = ⋂ i, {P | P.send (readP Prod.fst i) = p} := by ext; simp [s,hasRows] + rw [heq] + exact isClosed_iInter fun i => isClosed_eq (continuous_send _) continuous_const + let Q := pack fun _ : Fin n => p + obtain ⟨e⟩ := (inferInstance : Nonempty E) + have h1 : s.Nonempty := by + refine ⟨Q.send (fun x => (x,e)),fun i => ?_⟩ + rw [send_send]; exact marginalPack _ i + have hh := hs.image (rent_cont tag w u c Prod.fst Prod.snd) + have H1 : (C '' s).Nonempty := h1.image _ + exact ⟨hh.sInf_mem H1,fun P h => csInf_le hh.bddBelow ⟨_, h,rfl⟩⟩ + +lemma opt_le_rent (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (p : Law A) (P : Law Ω) (X : Ω → Fin n → A) (U : Ω → E) (h : hasRows p P X) : + opt n p tag w u c ≤ rent tag w u c P X U := by + let Q := P.send (fun a => (X a,U a)) + have hh := (opt_spec n p tag w u c).2 Q (by + intro i + dsimp only [Q]; rw [send_send]; exact h i) + dsimp only [Q] at hh + rw [rent_send] at hh + exact hh + +lemma opt_nonneg (n : ℕ) (p : Law A) (tag : A → G) (w : A → V) (u : E → G) (c : E → ℝ) + (hc : ∀ x, 0 ≤ c x) : 0 ≤ opt n p tag w u c := by + obtain ⟨P,_,he⟩ := (opt_spec n p tag w u c).1 + rw [← he] + exact rent_nonneg tag w u c hc P _ _ + + +lemma Hcond_function (P : Law Ω) (X : Ω → A) (W : A → V) : + P.Hcond X (fun x => W (X x)) = P.H X - P.H (fun x => W (X x)) := by + unfold Hcond + congr 1 + apply H_partition + simp [Prod.mk.injEq]; grind + +/-- Chain rule for deterministic reduction f(X). -/ +lemma Hcond_reduce (P : Law Ω) (X : Ω → A) (W : Ω → V) (f : A → B) : + P.Hcond X W = P.Hcond X (fun x => (f (X x),W x)) + P.Hcond (fun x => f (X x)) W := by + rw [← Hcond_chain P X (fun x => f (X x)) W] + unfold Hcond + congr 1 + refine H_partition _ fun a b => ?_ + simp only [Prod.mk.injEq] + constructor <;> grind + +omit [Fintype E] [Nonempty E] in +/-- Improving contexts, for the same full rows and tag, reduces the rent. -/ +lemma rent_antitone (tag : A → G) (w : A → V) (w' : A → W) (u : E → G) (c : E → ℝ) + (p : V → W) (h : w' = p ∘ w) + (P : Law Ω) (X : Ω → I → A) (U : Ω → E) : + rent tag w u c P X U ≤ rent tag w' u c P X U := by + subst w' + let Y := ans tag X (fun x => u (U x)) + let W₁ := ctxt w X + let W₂ := ctxt (p ∘ w) X + have h (i : I) : P.Hcond (readP X i) ((p ∘ w) ∘ readP X i) = + P.Hcond (readP X i) (w ∘ readP X i) + P.Hcond (readP W₁ i) (readP W₂ i) := by + have h1 := Hcond_function P (readP X i) (p ∘ w) + have h2 := Hcond_function P (readP X i) w + have h3 := Hcond_function P (readP W₁ i) p + change P.Hcond (readP X i) (fun x => p (w ((readP X i) x))) = _ + dsimp only [comp_apply] at h1 + rw [h1] + change _ = P.Hcond (readP X i) (fun x => w ((readP X i) x)) + + P.Hcond (readP W₁ i) (fun x => p (readP W₁ i x)) + rw [h2,h3]; simp only [W₁]; unfold readP ctxt; ring + let vmap (x : I → A) (i : I) := w (x i) + let Z₁ (x : Ω) := (Y x,W₁ x) + let Z₂ (x : Ω) := (Y x,W₂ x) + have hz : P.Hcond X Z₂ = P.Hcond X Z₁ + P.Hcond W₁ Z₂ := by + nth_rw 1 [Hcond_reduce P X Z₂ vmap] + have H : P.Hcond X (fun x => (vmap (X x), Z₂ x)) = P.Hcond X Z₁ := by + apply Hcond_right_partition + intro a b + simp only [Z₁,Z₂,W₁,W₂,ctxt,vmap,Prod.mk.injEq,funext_iff,comp_apply] + constructor + · tauto + · rintro ⟨hh,hx⟩ + simp [hx,hh] + rw [H]; rfl + have H : P.Hcond W₁ Z₂ ≤ ∑ i, P.Hcond (readP W₁ i) (readP W₂ i) := by + refine (P.Hcond_vec_le W₁ Z₂).trans (Finset.sum_le_sum fun i _ => ?_) + exact Hcond_antitone P _ fun _ _ hh => congrFun (congrArg Prod.snd hh) i + unfold rent + refine add_le_add_left ?_ _ + change _ - P.Hcond X Z₁ ≤ _ - P.Hcond X Z₂ + rw [hz] + simp_rw [h,Finset.sum_add_distrib] + linarith + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Schedule.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Schedule.lean index d5b7b5609af365181873bea0b0ff83d69c7a057c..e4ee3784000c987ce18ea5413cac2a51240e37e0 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Schedule.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Schedule.lean @@ -1,205 +1,205 @@ -import OAI.Analysis.FourierLLogL.Compress.Search -import OAI.Analysis.FourierLLogL.Compress.Fraction -/-! # Using the refinement search -/ - -open Function Filter Set -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real - -private lemma any_above {f : ℕ → ℝ} {k : ℕ} (a : ℝ) - (h : k*a < ∑ i ∈ Finset.range k, f i) : - ∃ i < k, a < f i := by - by_contra! hh - have he : ∑ i ∈ Finset.range k, f i ≤ ∑ i ∈ Finset.range k, a := - Finset.sum_le_sum fun i hi => hh i (Finset.mem_range.mp hi) - simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at he - linarith - -private lemma exists_offer (k m : ℕ) (hkm : k ≤ m) (hmp : 0 < m) - (d c : ℕ → ℝ) (τ p : ℝ) (hT : 0 < τ) (hp : 0 ≤ p) - (h : τ < ∑ i ∈ Finset.range k, d i) (h1 : ∑ i ∈ Finset.range k, c i = τ*p) - (hg : ∀ i, 0 ≤ c i) : - ∃ i < k, τ/(2*m) ≤ d i ∧ c i ≤ 2*p*d i := by - have hpos : (0:ℝ) < m := by exact_mod_cast hmp - let v := τ/(2*m) - have hv : 0 ≤ v := by unfold v; positivity - have HH : k*v ≤ τ/2 := calc - _ ≤ (m:ℝ)*v := by gcongr - _ = τ/2 := by unfold v; field_simp - obtain hs|hs := eq_or_lt_of_le hp - · obtain ⟨i,hi,hb⟩ := any_above v (f:=d) (k:=k) (by linarith) - refine ⟨i,hi,hb.le,?_⟩ - have he := Finset.single_le_sum (fun i (_ : i ∈ Finset.range k) => hg i) - (Finset.mem_range.mpr hi) - rw [← hs] at h1 ⊢ - linarith - obtain ⟨i,hi,hb⟩ := any_above v (f:=fun i => d i - c i/(2*p)) (k:=k) (by - rw [Finset.sum_sub_distrib, ← Finset.sum_div,h1] - rw [show τ*p/(2*p)=τ/2 by field_simp] - linarith) - have hn : 0 ≤ c i/(2*p) := by have := hg i; positivity - refine ⟨i,hi,le_of_lt (by linarith),?_⟩ - have hI : c i/(2*p) ≤ d i := by linarith - have he := (div_le_iff₀ (show 0 < 2*p by positivity)).mp hI - linarith - -/-- Parameters of the two-order search. -/ -structure Schedule where - N : ℕ - q : ℕ - n : ℕ - N0 : 0 < N - q0 : 0 < q - n0 : 0 < n - u : ℝ - a : ℝ - δ : ℝ - η : ℝ - α : ℝ - ε : ℝ - u0 : 0 < u - a0 : 0 < a - d0 : 0 < δ - heta : δ*exp (2*u/a) ≤ η - hn : 1 < (n:ℝ)*δ - hα : 0 < α - hε : 0 < ε - hβ : 2*(a/α) ≤ 1 - hc : (ε+η)*exp (2*(a/α)*q) ≤ 1 - hq : 1 < (q:ℝ)*u - ht : 1 < (N:ℝ)*ε - -namespace Schedule -def size (w : Schedule) := w.N*(w.q*w.n) -def loss (w : Schedule) := 2*w.α+w.a*(1+w.η) -lemma loss_pos (w : Schedule) : 0 < w.loss := by - have H := lt_of_lt_of_le (mul_pos w.d0 (exp_pos _)) w.heta - unfold loss; nlinarith [w.hα,w.a0] -end Schedule - -variable {A G : Type} [Fintype A] [Fintype G] [AddCommGroup G] - -lemma offer_at (tag : A→G) (hi : Injective tag) (c : G→ℝ) (s : Labelled A) - (D F : ℝ) (hd : 0 < D) (hf : 0 < F) (r l u M : ℕ) (hl : l ≤ u) (hu : u ≤ M) - (hm : 0 < M) (hno : F+fundK tag c s l r < fundK tag c s u r) - (x : ℝ) (hx : 0 < x) (hb : x ≤ F/D/(2*M)) : - ∃ t, refines t s ∧ - x ≤ fundI tag s r / D - fundI tag t r / D ∧ - F/D * (fundG tag s 1 / D - fundG tag t 1 / D) ≤ - 2*(fundG tag s u/D-fundG tag s l/D)*x := by - let g n := fundG tag s n/D - let a t := fundI tag t r/D - let τ := F/D - let T m := observe tag s (l+m) - let k := u-l - let f m := a s - a (T m) - let b m := τ*(fundG tag s 1/D - fundG tag (T m) 1 / D) - let K n := fundK tag c s n r - have hg : Monotone g := fun i j hh => div_le_div_of_nonneg_right - (fundG_mono tag s hh) hd.le - have hτ : 0 < τ := div_pos hf hd - have h1 : τ < ∑ i ∈ Finset.range k, f i := by - have hc (i : ℕ) : K (l+(i+1))/D-K (l+i)/D ≤ f i := by - have hh := observe_save tag c s (l+i) r - unfold K f a T; rw [← sub_div, ← sub_div,← Nat.add_assoc] - exact div_le_div_of_nonneg_right (by linarith) hd.le - refine lt_of_lt_of_le ?_ (Finset.sum_le_sum (fun i _ => hc i)) - rw [Finset.sum_range_sub (fun i => K (l+i)/D)] - unfold k τ K - rw [Nat.add_zero,Nat.add_sub_of_le hl, ← sub_div] - gcongr; linarith - have heq (i : ℕ) : b i = τ*(g (l+(i+1))-g (l+i)) := by - unfold b g T - rw [← sub_div,observe_cost tag hi s (l+i),← sub_div,Nat.add_assoc] - have h2 : ∑ i ∈ Finset.range k, b i = τ*(g u-g l) := by - simp_rw [heq,← Finset.mul_sum] - rw [Finset.sum_range_sub (fun i => g (l+i))] - simp only [k,Nat.add_sub_of_le hl,Nat.add_zero] - obtain ⟨i,hh,hI,hJ⟩ := exists_offer k M (by omega) hm f b τ (g u-g l) - hτ (sub_nonneg.mpr (hg hl)) h1 h2 (by - intro i; rw [heq]; exact mul_nonneg hτ.le (sub_nonneg.mpr (hg (by omega)))) - have hX : x ≤ f i := le_trans hb hI - have Hf : 0 < f i := lt_of_lt_of_le hx hX - let y := x/f i - have hy0 : 0 ≤ y := (div_pos hx Hf).le - have hy1 : y ≤ 1 := (div_le_one Hf).mpr hX - refine ⟨blend (T i) s y hy0 hy1, - blend_refines _ _ _ (observe_refines _ _ _) (refines_refl s) _ _ _, ?_,?_⟩ - · have hh := blend_I tag (T i) s y hy0 hy1 r - have hi := div_le_div_of_nonneg_right hh hd.le - have He : y*f i=x := div_mul_cancel₀ _ Hf.ne' - unfold f a at He; rw [add_div,mul_div_assoc,mul_div_assoc] at hi - linarith - · rw [blend_G1 tag hi _ _ _] - change τ*_ ≤ 2*(g u-g l)*x - calc - _ = y*b i := by unfold b; ring - _ ≤ y*(2*(g u-g l)*f i) := mul_le_mul_of_nonneg_left hJ hy0 - _ = _ := by rw [mul_left_comm,show y*f i=x from div_mul_cancel₀ _ Hf.ne'] - -/-- Reach a long, low-cost block length. -/ -theorem reach (tag : A → G) (hi : Injective tag) (c : G → ℝ) (_hc0 : ∀ x, 0 ≤ c x) - (S : Labelled A) (D F : ℝ) (hd : 0 < D) (hf : 0 < F) (B : Schedule) - (m : ℕ) (l u r : ℕ → ℕ) (hm : 0 < m) - (hl : ∀ i, i < B.size → l i ≤ u i ∧ u i ≤ m) - (hp : ∀ i j, i < j → j < B.size → u j ≤ l i ∧ r i ≤ r j) - (hG : ∀ i, i refines t.val s.val - refl := fun s => refines_refl _ - trans := fun h1 h2 => refines_trans h2 h1 - topc := fun x => τ*g 1 x - pay := fun {x y} hh => mul_le_mul_of_nonneg_left (div_le_div_of_nonneg_right - (G_anti tag hh 1) hd.le) hτ.le - A := a - Alo := fun s i _ => div_nonneg (opt_nonneg _ s.val.law (tag ∘ Prod.fst) Prod.snd - (fun _ : Unit => 0) (fun _ => 0) (fun _ => le_refl _)) hd.le - Ahi := fun s i hi => (div_le_one hd).mpr ((I_anti tag s.property _).trans (hI i hi)) - Am := fun s {i j} hij hj => by - obtain h|h := eq_or_lt_of_le hij - · subst i; exact le_rfl - · exact div_le_div_of_nonneg_right (fundI_mono tag _ (hp i j h hj).2) hd.le - As := fun {x y} h _ _ => div_le_div_of_nonneg_right (I_anti _ h _) hd.le - L := fun i => g (l i) - H := fun i => g (u i) - Llo := fun s i h => div_nonneg (show 0 ≤ fundG tag s.val (l i) from Hcond_nonneg _ _ _) hd.le - Hhi := fun s i hi => (div_le_one hd).mpr ((G_anti tag s.property _).trans (hG i hi)) - LH := fun s i h => div_le_div_of_nonneg_right (fundG_mono _ _ (hl i h).1) hd.le - BC := fun s {i j} h hb => div_le_div_of_nonneg_right (fundG_mono _ _ (hp i j h hb).1) hd.le - Ls := fun {s t} h i _ => div_le_div_of_nonneg_right (G_anti _ h _) hd.le - Hs := fun {s t} h i _ => div_le_div_of_nonneg_right (G_anti _ h _) hd.le - found := fun s => ∃ i, i < B.size ∧ fundK tag c s.val (u i) (r i) ≤ F + - fundK tag c s.val (l i) (r i) - rate := τ/(2*m) - r0 := by have : (0:ℝ) < m := by exact_mod_cast hm - positivity - offer := by - intro s i hb h x hx hx' - obtain ⟨Q,hQ,h1,h2⟩ := offer_at tag hi c s.val D F hd hf (r i) (l i) (u i) - m (hl i hb).1 (hl i hb).2 hm (lt_of_not_ge (fun hh => h ⟨i,hb,hh⟩)) x hx hx' - exact ⟨⟨Q,refines_trans hQ s.property⟩,hQ,h1,by rwa [← mul_sub]⟩ } - obtain ⟨t,ht,hc,hP⟩ := - v.complete_search ⟨S,refines_refl S⟩ B.N B.q B.n B.n0 B.q0 B.N0 le_rfl - B.u B.a B.δ B.η B.α B.ε B.u0 B.a0 B.d0 B.heta B.hn B.hα B.hε B.hβ B.hc B.hq B.ht - refine ⟨t.val,t.property,?_,hc⟩ - change F/D * (fundG tag S 1 / D) - F/D*(fundG tag t.val 1 / D) ≤ B.loss at hP - field_simp at * - - linarith - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Search +import OAI.Analysis.FourierLLogL.Compress.Fraction +/-! # Using the refinement search -/ + +open Function Filter Set +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real + +private lemma any_above {f : ℕ → ℝ} {k : ℕ} (a : ℝ) + (h : k*a < ∑ i ∈ Finset.range k, f i) : + ∃ i < k, a < f i := by + by_contra! hh + have he : ∑ i ∈ Finset.range k, f i ≤ ∑ i ∈ Finset.range k, a := + Finset.sum_le_sum fun i hi => hh i (Finset.mem_range.mp hi) + simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at he + linarith + +private lemma exists_offer (k m : ℕ) (hkm : k ≤ m) (hmp : 0 < m) + (d c : ℕ → ℝ) (τ p : ℝ) (hT : 0 < τ) (hp : 0 ≤ p) + (h : τ < ∑ i ∈ Finset.range k, d i) (h1 : ∑ i ∈ Finset.range k, c i = τ*p) + (hg : ∀ i, 0 ≤ c i) : + ∃ i < k, τ/(2*m) ≤ d i ∧ c i ≤ 2*p*d i := by + have hpos : (0:ℝ) < m := by exact_mod_cast hmp + let v := τ/(2*m) + have hv : 0 ≤ v := by unfold v; positivity + have HH : k*v ≤ τ/2 := calc + _ ≤ (m:ℝ)*v := by gcongr + _ = τ/2 := by unfold v; field_simp + obtain hs|hs := eq_or_lt_of_le hp + · obtain ⟨i,hi,hb⟩ := any_above v (f:=d) (k:=k) (by linarith) + refine ⟨i,hi,hb.le,?_⟩ + have he := Finset.single_le_sum (fun i (_ : i ∈ Finset.range k) => hg i) + (Finset.mem_range.mpr hi) + rw [← hs] at h1 ⊢ + linarith + obtain ⟨i,hi,hb⟩ := any_above v (f:=fun i => d i - c i/(2*p)) (k:=k) (by + rw [Finset.sum_sub_distrib, ← Finset.sum_div,h1] + rw [show τ*p/(2*p)=τ/2 by field_simp] + linarith) + have hn : 0 ≤ c i/(2*p) := by have := hg i; positivity + refine ⟨i,hi,le_of_lt (by linarith),?_⟩ + have hI : c i/(2*p) ≤ d i := by linarith + have he := (div_le_iff₀ (show 0 < 2*p by positivity)).mp hI + linarith + +/-- Parameters of the two-order search. -/ +structure Schedule where + N : ℕ + q : ℕ + n : ℕ + N0 : 0 < N + q0 : 0 < q + n0 : 0 < n + u : ℝ + a : ℝ + δ : ℝ + η : ℝ + α : ℝ + ε : ℝ + u0 : 0 < u + a0 : 0 < a + d0 : 0 < δ + heta : δ*exp (2*u/a) ≤ η + hn : 1 < (n:ℝ)*δ + hα : 0 < α + hε : 0 < ε + hβ : 2*(a/α) ≤ 1 + hc : (ε+η)*exp (2*(a/α)*q) ≤ 1 + hq : 1 < (q:ℝ)*u + ht : 1 < (N:ℝ)*ε + +namespace Schedule +def size (w : Schedule) := w.N*(w.q*w.n) +def loss (w : Schedule) := 2*w.α+w.a*(1+w.η) +lemma loss_pos (w : Schedule) : 0 < w.loss := by + have H := lt_of_lt_of_le (mul_pos w.d0 (exp_pos _)) w.heta + unfold loss; nlinarith [w.hα,w.a0] +end Schedule + +variable {A G : Type} [Fintype A] [Fintype G] [AddCommGroup G] + +lemma offer_at (tag : A→G) (hi : Injective tag) (c : G→ℝ) (s : Labelled A) + (D F : ℝ) (hd : 0 < D) (hf : 0 < F) (r l u M : ℕ) (hl : l ≤ u) (hu : u ≤ M) + (hm : 0 < M) (hno : F+fundK tag c s l r < fundK tag c s u r) + (x : ℝ) (hx : 0 < x) (hb : x ≤ F/D/(2*M)) : + ∃ t, refines t s ∧ + x ≤ fundI tag s r / D - fundI tag t r / D ∧ + F/D * (fundG tag s 1 / D - fundG tag t 1 / D) ≤ + 2*(fundG tag s u/D-fundG tag s l/D)*x := by + let g n := fundG tag s n/D + let a t := fundI tag t r/D + let τ := F/D + let T m := observe tag s (l+m) + let k := u-l + let f m := a s - a (T m) + let b m := τ*(fundG tag s 1/D - fundG tag (T m) 1 / D) + let K n := fundK tag c s n r + have hg : Monotone g := fun i j hh => div_le_div_of_nonneg_right + (fundG_mono tag s hh) hd.le + have hτ : 0 < τ := div_pos hf hd + have h1 : τ < ∑ i ∈ Finset.range k, f i := by + have hc (i : ℕ) : K (l+(i+1))/D-K (l+i)/D ≤ f i := by + have hh := observe_save tag c s (l+i) r + unfold K f a T; rw [← sub_div, ← sub_div,← Nat.add_assoc] + exact div_le_div_of_nonneg_right (by linarith) hd.le + refine lt_of_lt_of_le ?_ (Finset.sum_le_sum (fun i _ => hc i)) + rw [Finset.sum_range_sub (fun i => K (l+i)/D)] + unfold k τ K + rw [Nat.add_zero,Nat.add_sub_of_le hl, ← sub_div] + gcongr; linarith + have heq (i : ℕ) : b i = τ*(g (l+(i+1))-g (l+i)) := by + unfold b g T + rw [← sub_div,observe_cost tag hi s (l+i),← sub_div,Nat.add_assoc] + have h2 : ∑ i ∈ Finset.range k, b i = τ*(g u-g l) := by + simp_rw [heq,← Finset.mul_sum] + rw [Finset.sum_range_sub (fun i => g (l+i))] + simp only [k,Nat.add_sub_of_le hl,Nat.add_zero] + obtain ⟨i,hh,hI,hJ⟩ := exists_offer k M (by omega) hm f b τ (g u-g l) + hτ (sub_nonneg.mpr (hg hl)) h1 h2 (by + intro i; rw [heq]; exact mul_nonneg hτ.le (sub_nonneg.mpr (hg (by omega)))) + have hX : x ≤ f i := le_trans hb hI + have Hf : 0 < f i := lt_of_lt_of_le hx hX + let y := x/f i + have hy0 : 0 ≤ y := (div_pos hx Hf).le + have hy1 : y ≤ 1 := (div_le_one Hf).mpr hX + refine ⟨blend (T i) s y hy0 hy1, + blend_refines _ _ _ (observe_refines _ _ _) (refines_refl s) _ _ _, ?_,?_⟩ + · have hh := blend_I tag (T i) s y hy0 hy1 r + have hi := div_le_div_of_nonneg_right hh hd.le + have He : y*f i=x := div_mul_cancel₀ _ Hf.ne' + unfold f a at He; rw [add_div,mul_div_assoc,mul_div_assoc] at hi + linarith + · rw [blend_G1 tag hi _ _ _] + change τ*_ ≤ 2*(g u-g l)*x + calc + _ = y*b i := by unfold b; ring + _ ≤ y*(2*(g u-g l)*f i) := mul_le_mul_of_nonneg_left hJ hy0 + _ = _ := by rw [mul_left_comm,show y*f i=x from div_mul_cancel₀ _ Hf.ne'] + +/-- Reach a long, low-cost block length. -/ +theorem reach (tag : A → G) (hi : Injective tag) (c : G → ℝ) (_hc0 : ∀ x, 0 ≤ c x) + (S : Labelled A) (D F : ℝ) (hd : 0 < D) (hf : 0 < F) (B : Schedule) + (m : ℕ) (l u r : ℕ → ℕ) (hm : 0 < m) + (hl : ∀ i, i < B.size → l i ≤ u i ∧ u i ≤ m) + (hp : ∀ i j, i < j → j < B.size → u j ≤ l i ∧ r i ≤ r j) + (hG : ∀ i, i refines t.val s.val + refl := fun s => refines_refl _ + trans := fun h1 h2 => refines_trans h2 h1 + topc := fun x => τ*g 1 x + pay := fun {x y} hh => mul_le_mul_of_nonneg_left (div_le_div_of_nonneg_right + (G_anti tag hh 1) hd.le) hτ.le + A := a + Alo := fun s i _ => div_nonneg (opt_nonneg _ s.val.law (tag ∘ Prod.fst) Prod.snd + (fun _ : Unit => 0) (fun _ => 0) (fun _ => le_refl _)) hd.le + Ahi := fun s i hi => (div_le_one hd).mpr ((I_anti tag s.property _).trans (hI i hi)) + Am := fun s {i j} hij hj => by + obtain h|h := eq_or_lt_of_le hij + · subst i; exact le_rfl + · exact div_le_div_of_nonneg_right (fundI_mono tag _ (hp i j h hj).2) hd.le + As := fun {x y} h _ _ => div_le_div_of_nonneg_right (I_anti _ h _) hd.le + L := fun i => g (l i) + H := fun i => g (u i) + Llo := fun s i h => div_nonneg (show 0 ≤ fundG tag s.val (l i) from Hcond_nonneg _ _ _) hd.le + Hhi := fun s i hi => (div_le_one hd).mpr ((G_anti tag s.property _).trans (hG i hi)) + LH := fun s i h => div_le_div_of_nonneg_right (fundG_mono _ _ (hl i h).1) hd.le + BC := fun s {i j} h hb => div_le_div_of_nonneg_right (fundG_mono _ _ (hp i j h hb).1) hd.le + Ls := fun {s t} h i _ => div_le_div_of_nonneg_right (G_anti _ h _) hd.le + Hs := fun {s t} h i _ => div_le_div_of_nonneg_right (G_anti _ h _) hd.le + found := fun s => ∃ i, i < B.size ∧ fundK tag c s.val (u i) (r i) ≤ F + + fundK tag c s.val (l i) (r i) + rate := τ/(2*m) + r0 := by have : (0:ℝ) < m := by exact_mod_cast hm + positivity + offer := by + intro s i hb h x hx hx' + obtain ⟨Q,hQ,h1,h2⟩ := offer_at tag hi c s.val D F hd hf (r i) (l i) (u i) + m (hl i hb).1 (hl i hb).2 hm (lt_of_not_ge (fun hh => h ⟨i,hb,hh⟩)) x hx hx' + exact ⟨⟨Q,refines_trans hQ s.property⟩,hQ,h1,by rwa [← mul_sub]⟩ } + obtain ⟨t,ht,hc,hP⟩ := + v.complete_search ⟨S,refines_refl S⟩ B.N B.q B.n B.n0 B.q0 B.N0 le_rfl + B.u B.a B.δ B.η B.α B.ε B.u0 B.a0 B.d0 B.heta B.hn B.hα B.hε B.hβ B.hc B.hq B.ht + refine ⟨t.val,t.property,?_,hc⟩ + change F/D * (fundG tag S 1 / D) - F/D*(fundG tag t.val 1 / D) ≤ B.loss at hP + field_simp at * + + linarith + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Search.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Search.lean index ff3aebd3ef0b9e40d604a3b4fe4e7f957e593457..c2d4f03ec1305d4e0290c7781fd2ee064e5a2f94 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Search.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Search.lean @@ -1,363 +1,363 @@ -import Mathlib -/-! # Two-order finite refinement search -/ -open Set Function Filter -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -set_option maxHeartbeats 600000 -open Real -universe u -variable {S : Type u} - -/-- Normalized potentials. A can only move down in time. Across indices A -moves up while the low and high potentials are margins of disjoint intervals -that move down. -/ -structure Arena (S : Type u) where - N : ℕ - mov : S → S → Prop - refl (s) : mov s s - trans {s t u} : mov s t → mov t u → mov s u - topc : S → ℝ - pay {s t} : mov s t → topc t ≤ topc s - A : ℕ → S → ℝ - Alo (s i) (hi : i - refine ⟨s,d.refl _,?_ ,Or.inr ?_⟩ <;> simp [cost,Δ,b,le_of_lt hu] - | succ k ih => - obtain ⟨t,hst,hc,hk⟩ := ih - by_cases h : goal t - · exact ⟨t,hst,hc,Or.inl h⟩ - unfold goal at h - push Not at h - have he : 0 < u-Δ t := by linarith [h.2.1] - let x := min d.rate (u-Δ t) - have hx : 0 < x := lt_min d.r0 he - obtain ⟨q,hq,hA,hV⟩ := d.offer t hi h.1 x hx (min_le_left _ _) - have hT : b t = Δ t := min_eq_right h.2.1.le - have hQ : Δ t+x ≤ b q := by - refine le_min ?_ ?_ - · linarith [show x ≤ u-Δ t from min_le_right _ _] - · dsimp [Δ]; linarith - have hcp : 0 ≤ cost t := sub_nonneg.mpr (d.pay hst) - have H1 : cost q+a*δ ≤ (cost t+a*δ)*(1+2*x/a) := by - have HH : a*(d.H i t-d.L i t) ≤ cost t+a*δ := - (mul_le_mul_of_nonneg_left (sub_le_sub_right (d.Hs hst hi) _) ha.le).trans h.2.2.le - have h1 : a*(cost q-cost t) ≤ (cost t+a*δ)* (2*x) := by - calc - _ ≤ a*(2*(d.H i t-d.L i t)*x) := - mul_le_mul_of_nonneg_left (by unfold cost; linarith) ha.le - _ = a*(d.H i t-d.L i t)*(2*x) := by ring - _ ≤ _ := mul_le_mul_of_nonneg_right HH (by positivity) - field_simp; linarith - have H2 : exp (2*b t/a) * exp (2*x/a) ≤ exp (2*b q/a) := by - rw [← exp_add,exp_le_exp,hT] - field_simp; linarith - have H3 : cost q+a*δ ≤ a*δ*exp (2*b q/a) := by - apply le_trans H1 - have hxp : 1+2*x/a ≤ exp (2*x/a) := by linarith [add_one_le_exp (2*x/a)] - apply le_trans (mul_le_mul hc hxp (by positivity) (by positivity)) - rw [mul_assoc] - exact mul_le_mul_of_nonneg_left H2 (mul_pos ha hδ).le - refine ⟨q,d.trans hst hq,H3,?_⟩ - - rcases le_total d.rate (u-Δ t) with hn|hn - · have hh : k*d.rate ≤ Δ t := hk.resolve_left (by unfold goal; grind) - right - have hh' : x = d.rate := min_eq_left hn - push_cast - have H := min_le_right u (Δ q) - linarith [show b q ≤ Δ q from H] - · left - refine Or.inr (Or.inl ?_) - have hh : x = u - Δ t := min_eq_right hn - have he : b q ≤ Δ q := min_le_right _ _ - linarith - obtain ⟨k,hk⟩ := exists_nat_gt (1 / d.rate) - obtain ⟨t,hst,h1,h2⟩ := H k - have h3 : goal t := - h2.resolve_right (fun hh => by - have Hr := (div_lt_iff₀ d.r0).mp hk - unfold Δ at hh; linarith [d.Ahi s i hi,d.Alo t i hi]) - refine ⟨t,hst,?_ ,h3⟩ - have H0 : b t ≤ u := min_le_left _ _ - have H1 : exp (2*b t/a) ≤ exp (2*u/a) := by gcongr - change cost t ≤ _ - nlinarith [mul_le_mul_of_nonneg_left H1 (mul_pos ha hδ).le, - mul_le_mul_of_nonneg_left hη ha.le] - -/-- Forward search through adjacent blocks [o+i*m,o+(i+1)m). Fully failed blocks consume width. -/ -lemma budget_ascending (s : S) (G : S → Prop) - (o n m : ℕ) (hm : 0 < m) (hn : 0 < n) (hb : o+n*m ≤ d.N) - (a b δ : ℝ) (ha : 0 < a) (hδ : 1 < n*δ) - (hstep : ∀ (t) (_h : d.mov s t) i (_hi : i simpa [last] using hstep s (d.refl s) 0 hk - | succ k ih => - have hk' : k - refine ⟨s,d.refl _,Or.inr ⟨by simp [x],by simp,?_⟩⟩ - intro i hi; have HH : i - obtain ⟨t,hst,ht|⟨ht,hv,hA⟩⟩ := ih (by omega) - · exact ⟨t,hst,Or.inl ht⟩ - let b := o+(q-(k+1))*n - have h1 : b+n=o+(q-k)*n := by - have H1 : q-k=q-(k+1)+1 := by omega - unfold b; rw [H1,add_mul]; omega - have h2 : b+n ≤ o+q*n := by rw [h1]; gcongr; omega - obtain ⟨t',htt',hg,hw⟩ := d.budget_sub t b n hn (h2.trans hb) u a δ η hu ha hδ hη hnδ - have hx := d.trans hst htt' - refine ⟨t',hx,?_⟩ - have hy1 : b+n-1 ≤ l := by dsimp only [l]; omega - have hb0 : o ≤ b := Nat.le_add_right _ _ - have hy2 : o ≤ b+n-1 := by omega - have hl2 := lt_of_le_of_lt hy1 hl - have hl3 : b ≤ b+n-1 := by omega - have hl4 := lt_of_le_of_lt hl3 hl2 - have hw' : x t' - x t ≤ a*(d.H o s-d.L l t')+a*η := by - have hA : d.H b t ≤ d.H o s := le_trans (d.Hs hst hl4) (d.H_index s hb0 hl4) - have hL := d.L_index t' hy1 hl - unfold x - nlinarith [hw] - rcases hg with hh|hh - · have hE : exp (2*β*k) ≤ exp (2*β*q) := by - gcongr; omega - have hv : z*exp (2*β*q) ≤ α := by - unfold z; nlinarith [mul_le_mul_of_nonneg_left hc ha'.le] - have hT : x t ≤ α := by nlinarith - exact Or.inl (Or.inl ⟨hh,by nlinarith [d.Hhi s o hl',d.Llo t' l hl]⟩) - by_cases H : x t'+α*ε ≤ y t' - · exact Or.inl (Or.inr H) - have hy : y t' ≤ x t'+α*ε := le_of_not_ge H - - have hrec : (x t'+z)*(1-β) ≤ (x t+z) := by - unfold y at hy - unfold z - have Hh := mul_le_mul_of_nonneg_left hy bpos.le - - simp only [mul_add, ← mul_assoc,hB] at Hh - have h3 (c : ℝ) : β*(α*c) = a*c := by rw [← mul_assoc,hB] - nlinarith [h3 (η+ε)] - have hxp : x t'+z ≥ 0 := by have H := d.pay hx; unfold x; linarith - - have hiter : (x t'+z) ≤ z * exp (2*β*(k+1)) := by - have hg : 1 ≤ (1-β)*exp (2*β) := by - have h := Real.add_one_le_exp (2*β) - nlinarith - apply le_trans (b:=(z*exp (2*β*k))*exp (2*β)) - · have HH := mul_le_mul_of_nonneg_right (hrec.trans ht) (exp_nonneg (2*β)) - exact le_trans (by - rw [mul_assoc]; exact le_mul_of_one_le_right hxp hg) HH - · rw [mul_assoc,← exp_add] - apply le_of_eq; ring_nf - refine Or.inr ⟨by push_cast; exact hiter,?_,?_⟩ - · have H := hA (b+n-1) (by omega) - have H1 := d.Alo t' b hl4 - push_cast; linarith - · intro i hi - have H := hA (b+n-1) (by omega) - change i < b at hi - have H1 := d.Am t' hi.le hl4 - push_cast; linarith - obtain ⟨t,hst,h|h⟩ := HI q le_rfl - · exact ⟨t,hst,h⟩ - · exfalso - linarith [h.2.1] - -lemma complete_search (s : S) (N q n : ℕ) (hn : 0 + refine ⟨s,d.refl _,?_ ,Or.inr ?_⟩ <;> simp [cost,Δ,b,le_of_lt hu] + | succ k ih => + obtain ⟨t,hst,hc,hk⟩ := ih + by_cases h : goal t + · exact ⟨t,hst,hc,Or.inl h⟩ + unfold goal at h + push Not at h + have he : 0 < u-Δ t := by linarith [h.2.1] + let x := min d.rate (u-Δ t) + have hx : 0 < x := lt_min d.r0 he + obtain ⟨q,hq,hA,hV⟩ := d.offer t hi h.1 x hx (min_le_left _ _) + have hT : b t = Δ t := min_eq_right h.2.1.le + have hQ : Δ t+x ≤ b q := by + refine le_min ?_ ?_ + · linarith [show x ≤ u-Δ t from min_le_right _ _] + · dsimp [Δ]; linarith + have hcp : 0 ≤ cost t := sub_nonneg.mpr (d.pay hst) + have H1 : cost q+a*δ ≤ (cost t+a*δ)*(1+2*x/a) := by + have HH : a*(d.H i t-d.L i t) ≤ cost t+a*δ := + (mul_le_mul_of_nonneg_left (sub_le_sub_right (d.Hs hst hi) _) ha.le).trans h.2.2.le + have h1 : a*(cost q-cost t) ≤ (cost t+a*δ)* (2*x) := by + calc + _ ≤ a*(2*(d.H i t-d.L i t)*x) := + mul_le_mul_of_nonneg_left (by unfold cost; linarith) ha.le + _ = a*(d.H i t-d.L i t)*(2*x) := by ring + _ ≤ _ := mul_le_mul_of_nonneg_right HH (by positivity) + field_simp; linarith + have H2 : exp (2*b t/a) * exp (2*x/a) ≤ exp (2*b q/a) := by + rw [← exp_add,exp_le_exp,hT] + field_simp; linarith + have H3 : cost q+a*δ ≤ a*δ*exp (2*b q/a) := by + apply le_trans H1 + have hxp : 1+2*x/a ≤ exp (2*x/a) := by linarith [add_one_le_exp (2*x/a)] + apply le_trans (mul_le_mul hc hxp (by positivity) (by positivity)) + rw [mul_assoc] + exact mul_le_mul_of_nonneg_left H2 (mul_pos ha hδ).le + refine ⟨q,d.trans hst hq,H3,?_⟩ + + rcases le_total d.rate (u-Δ t) with hn|hn + · have hh : k*d.rate ≤ Δ t := hk.resolve_left (by unfold goal; grind) + right + have hh' : x = d.rate := min_eq_left hn + push_cast + have H := min_le_right u (Δ q) + linarith [show b q ≤ Δ q from H] + · left + refine Or.inr (Or.inl ?_) + have hh : x = u - Δ t := min_eq_right hn + have he : b q ≤ Δ q := min_le_right _ _ + linarith + obtain ⟨k,hk⟩ := exists_nat_gt (1 / d.rate) + obtain ⟨t,hst,h1,h2⟩ := H k + have h3 : goal t := + h2.resolve_right (fun hh => by + have Hr := (div_lt_iff₀ d.r0).mp hk + unfold Δ at hh; linarith [d.Ahi s i hi,d.Alo t i hi]) + refine ⟨t,hst,?_ ,h3⟩ + have H0 : b t ≤ u := min_le_left _ _ + have H1 : exp (2*b t/a) ≤ exp (2*u/a) := by gcongr + change cost t ≤ _ + nlinarith [mul_le_mul_of_nonneg_left H1 (mul_pos ha hδ).le, + mul_le_mul_of_nonneg_left hη ha.le] + +/-- Forward search through adjacent blocks [o+i*m,o+(i+1)m). Fully failed blocks consume width. -/ +lemma budget_ascending (s : S) (G : S → Prop) + (o n m : ℕ) (hm : 0 < m) (hn : 0 < n) (hb : o+n*m ≤ d.N) + (a b δ : ℝ) (ha : 0 < a) (hδ : 1 < n*δ) + (hstep : ∀ (t) (_h : d.mov s t) i (_hi : i simpa [last] using hstep s (d.refl s) 0 hk + | succ k ih => + have hk' : k + refine ⟨s,d.refl _,Or.inr ⟨by simp [x],by simp,?_⟩⟩ + intro i hi; have HH : i + obtain ⟨t,hst,ht|⟨ht,hv,hA⟩⟩ := ih (by omega) + · exact ⟨t,hst,Or.inl ht⟩ + let b := o+(q-(k+1))*n + have h1 : b+n=o+(q-k)*n := by + have H1 : q-k=q-(k+1)+1 := by omega + unfold b; rw [H1,add_mul]; omega + have h2 : b+n ≤ o+q*n := by rw [h1]; gcongr; omega + obtain ⟨t',htt',hg,hw⟩ := d.budget_sub t b n hn (h2.trans hb) u a δ η hu ha hδ hη hnδ + have hx := d.trans hst htt' + refine ⟨t',hx,?_⟩ + have hy1 : b+n-1 ≤ l := by dsimp only [l]; omega + have hb0 : o ≤ b := Nat.le_add_right _ _ + have hy2 : o ≤ b+n-1 := by omega + have hl2 := lt_of_le_of_lt hy1 hl + have hl3 : b ≤ b+n-1 := by omega + have hl4 := lt_of_le_of_lt hl3 hl2 + have hw' : x t' - x t ≤ a*(d.H o s-d.L l t')+a*η := by + have hA : d.H b t ≤ d.H o s := le_trans (d.Hs hst hl4) (d.H_index s hb0 hl4) + have hL := d.L_index t' hy1 hl + unfold x + nlinarith [hw] + rcases hg with hh|hh + · have hE : exp (2*β*k) ≤ exp (2*β*q) := by + gcongr; omega + have hv : z*exp (2*β*q) ≤ α := by + unfold z; nlinarith [mul_le_mul_of_nonneg_left hc ha'.le] + have hT : x t ≤ α := by nlinarith + exact Or.inl (Or.inl ⟨hh,by nlinarith [d.Hhi s o hl',d.Llo t' l hl]⟩) + by_cases H : x t'+α*ε ≤ y t' + · exact Or.inl (Or.inr H) + have hy : y t' ≤ x t'+α*ε := le_of_not_ge H + + have hrec : (x t'+z)*(1-β) ≤ (x t+z) := by + unfold y at hy + unfold z + have Hh := mul_le_mul_of_nonneg_left hy bpos.le + + simp only [mul_add, ← mul_assoc,hB] at Hh + have h3 (c : ℝ) : β*(α*c) = a*c := by rw [← mul_assoc,hB] + nlinarith [h3 (η+ε)] + have hxp : x t'+z ≥ 0 := by have H := d.pay hx; unfold x; linarith + + have hiter : (x t'+z) ≤ z * exp (2*β*(k+1)) := by + have hg : 1 ≤ (1-β)*exp (2*β) := by + have h := Real.add_one_le_exp (2*β) + nlinarith + apply le_trans (b:=(z*exp (2*β*k))*exp (2*β)) + · have HH := mul_le_mul_of_nonneg_right (hrec.trans ht) (exp_nonneg (2*β)) + exact le_trans (by + rw [mul_assoc]; exact le_mul_of_one_le_right hxp hg) HH + · rw [mul_assoc,← exp_add] + apply le_of_eq; ring_nf + refine Or.inr ⟨by push_cast; exact hiter,?_,?_⟩ + · have H := hA (b+n-1) (by omega) + have H1 := d.Alo t' b hl4 + push_cast; linarith + · intro i hi + have H := hA (b+n-1) (by omega) + change i < b at hi + have H1 := d.Am t' hi.le hl4 + push_cast; linarith + obtain ⟨t,hst,h|h⟩ := HI q le_rfl + · exact ⟨t,hst,h⟩ + · exfalso + linarith [h.2.1] + +lemma complete_search (s : S) (N q n : ℕ) (hn : 0 h0 i)) hh - rw [Finset.sum_range_add] at Hlt - have H : ∑ _ ∈ Finset.range N, a l ≤ ∑ i ∈ Finset.range N, a (l+i) := - Finset.sum_le_sum fun i _ => ha (by omega : l ≤ l+i) - simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at H - apply (le_div_iff₀ hp).mpr - have : 0 ≤ ∑ j ∈ Finset.range l, a j := Finset.sum_nonneg fun j _ => h0 j - linarith - have HD : 0 ≤ D := div_nonneg hE hp.le - have hr (i : ℕ) (hi : i ≤ m) : - ∑ j ∈ Finset.range N, (a (j+i)-a j) ≤ m*D := by - have h1 := Finset.sum_range_add a N i - have h2 := Finset.sum_range_add a i N - have hcmp : ∑ j ∈ Finset.range i, a (N+j) ≤ ∑ j ∈ Finset.range i, D := by - apply Finset.sum_le_sum - intro j hj - apply hs; rw [Finset.mem_range] at hj; omega - simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at hcmp - have hl : 0 ≤ ∑ j ∈ Finset.range i, a j := Finset.sum_nonneg fun j _ => h0 j - have hle : (i:ℝ)*D ≤ (m:ℝ)*D := by gcongr - rw [show i+N=N+i by omega] at h2 - simp_rw [show ∀ j, j+i=i+j from fun j => add_comm _ _, Finset.sum_sub_distrib] - linarith - let f j := ∑ i ∈ Finset.range m, (a (j+i)-a j) - have hAvg : ∑ j ∈ Finset.range N, f j ≤ ∑ i ∈ Finset.range N, ((m:ℝ)^2*E/(N:ℝ)^2) := by - unfold f - rw [Finset.sum_comm] - calc - _ ≤ ∑ i ∈ Finset.range m, ((m:ℝ)*D) := - Finset.sum_le_sum fun i hi => hr i (Finset.mem_range.mp hi).le - _ = _ := by simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul,D]; field_simp - by_contra! hy - apply (not_lt_of_ge hAvg) - refine Finset.sum_lt_sum_of_nonempty ⟨0,Finset.mem_range.mpr hN⟩ (fun j hj => ?_) - have hj := Finset.mem_range.mp hj - - exact hy _ hj (hs _ (by omega)) - -variable {α G Ω : Type} [Fintype α] [Fintype G] [Fintype Ω] [AddCommGroup G] - -omit [AddCommGroup G] in -private lemma choose3 (P : Law Ω) (a b c : Ω → ℝ) - (ha : ∀ x, 0 ≤ a x) (hb : ∀ x, 0 ≤ b x) (hc : ∀ x, 0 ≤ c x) - (A B C : ℝ) (hA : 0 < A) (hB : 0 < B) (hC : 0 < C) - (h1 : P.Ex a ≤ A) (h2 : P.Ex b ≤ B) (h3 : P.Ex c ≤ C) : - ∃ x, 0 < P x ∧ a x ≤ 3*A ∧ b x ≤ 3*B ∧ c x ≤ 3*C := by - obtain ⟨x,hx,hh⟩ := P.exists_le_Ex fun x => a x*A⁻¹+b x*B⁻¹+c x*C⁻¹ - have H : a x/A+b x/B+c x/C ≤ 3 := by - simp_rw [Ex_add,Ex_mul, ← div_eq_mul_inv] at hh - linarith [(div_le_one hA).mpr h1,(div_le_one hB).mpr h2,(div_le_one hC).mpr h3] - have hI := div_nonneg (ha x) hA.le - have hJ := div_nonneg (hb x) hB.le - have hK := div_nonneg (hc x) hC.le - exact ⟨x,hx,(div_le_iff₀ hA).mp (by linarith),(div_le_iff₀ hB).mp - (by linarith),(div_le_iff₀ hC).mp (by linarith)⟩ - -lemma kl_covered {ι : Type} [Fintype ι] [DecidableEq ι] - (P : Law Ω) (X : Ω → ι → α) (q : Law α) (h : ∀ i, P.send (fun a => X a i) = q) : - covered P X q := by - intro a ha i - rw [← h i] - exact P.send_pos (fun a => X a i) ha - - -def iid (p : Law α) (n : ℕ) := pack (fun _ : Fin n => p) - -theorem select_context (tag : α → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) - (T N m : ℕ) (hm0 : 0 < m) (hm : m ≤ N) (hT : 3*N ≤ T) - (E : ℝ) (hE : 0 < E) [Nonempty α] - (h : ∀ p : Law α, exp (-E) ≤ heatMoment (Fin T) p tag c) : - ∃ (p : Law α) (Ψ : Law (WordSample N α G)), exch Ψ ∧ - (∀ i : Fin N, Ψ.send (fun a => a.1 i) = p) ∧ - log (Fintype.card α) - p.entropy ≤ 3*(E/N) ∧ - bill tag c p Ψ Prod.fst Prod.snd ≤ 3*E ∧ - tv (Ψ.send (prefixB Prod.fst m hm)) (iid p m) ≤ 2*m*√E/N := by - obtain ⟨P,hp,hx,hu⟩ := noisy_symm tag c E h - let X : WordSample T α G → Letters T α := Prod.fst - let L := log (Fintype.card α) - let a j := L-rateH P X j - have h0 : 0 < N := by omega - have h1 (j : ℕ) : 0 ≤ a j := sub_nonneg.mpr (rate_bdd P X j) - let u := uniform α - have hcov : covered P X u := kl_covered P X u hu - have heq : ∑ j ∈ Finset.range T, a j ≤ E := by - have H := kl_le_bill hcov tag c hc Prod.snd - dsimp only [a,L] - rw [sum_deficit] - exact le_trans (le_trans - (le_add_of_nonneg_right (P.Ex_nonneg fun x => hc (x.2))) H) hp - obtain ⟨j,hj,hb,hj'⟩ := choose_slow (a:=a) (fun x y hh => - sub_le_sub_left (rate_antitone hx hh) _) h1 hE.le T N m h0 hm hT heq - let W := prefixB X j (by omega) - let V := readBlock X j N (by omega) - let Y := readBlock X j m (by omega) - let R (w) := P.after W w - let H := readLetter X j (by omega) - let ν (w) := (R w).send H - let Pw := P.send W - let b (w) := L-(ν w).entropy - let k (w) := bill tag c u (R w) V Prod.snd - let d (w) := kl ((R w).send Y) (pack (fun _ : Fin m => ν w)) - have Hb (w : Letters j α) : 0 ≤ b w := - sub_nonneg.mpr (entropy_le_log_card _) - have Hk (w : Letters j α) : 0 ≤ k w := by - apply bill_nonneg _ tag c hc - intro a _ _ - rw [uniform_apply] - positivity - have hv (w) (i : Fin m) : - (R w).send (fun x => Y x i) = ν w := conditional_block_marg hx (j:=j) (m:=m) (by omega) w i - have hv' (w) (i : Fin N) : - (R w).send (fun x => V x i) = ν w := conditional_block_marg hx (j:=j) (m:=N) (by omega) w i - have Hd (w : Letters j α) : 0 ≤ d w := kl_nonneg - (covered_pack (kl_covered _ _ _ (hv w))) - have Hm' : (0:ℝ) < m := by exact_mod_cast hm0 - have HN : (0:ℝ) < N := by exact_mod_cast h0 - have H1 : Pw.Ex b ≤ E/N := by - unfold b ν Pw - rw [Ex_sub,Ex_const] - exact le_trans (le_of_eq (by - dsimp only [a] - rw [rate_lt P X (show j (V x,x.2)) - have hmx := conditional_block_exch hx (j:=j) (m:=N) (by omega) w - refine ⟨ν w,Ψ,hmx,fun i => ?_,h1,?_,?_⟩ - · dsimp only [Ψ] - rw [send_send]; exact hv' w i - · dsimp only [Ψ] - rw [bill_send] - exact (bill_change (covered_subfamily (covered_after hcov W w hw) (offset j N (by omega))) - tag c Prod.snd (ν w) (hv' w)).trans h2 - · have hh : Ψ.send (prefixB Prod.fst m hm) = (R w).send Y := by - unfold Ψ; rw [send_send, show Y = _ from block_cut X j N m (by omega) hm]; rfl - rw [hh] - let v := tv ((R w).send Y) (iid (ν w) m) - have ht : v^2 ≤ 3*((m:ℝ)^2*E/N^2) := - (tv_sq_le_kl (covered_pack (kl_covered _ _ _ (hv w)))).trans h3 - have h4 : 3*((m:ℝ)^2*E/N^2) ≤ (2*m*√E/N)^2 := by - rw [div_pow,mul_pow,mul_pow, sq_sqrt hE.le] - have H := pow_pos HN 2 - field_simp; nlinarith - change v ≤ _ - nlinarith [ht.trans h4,sqrt_nonneg E,show 0 ≤ 2*m*√E/N by positivity] - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Prefix +/-! # Selecting conditional iid comparator -/ +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Real Law + +private lemma choose_slow {a : ℕ → ℝ} (ha : Monotone a) + (h0 : ∀ j, 0 ≤ a j) {E : ℝ} (hE : 0 ≤ E) (T N m : ℕ) + (hN : 0 < N) (hm : m ≤ N) (hT : 3*N ≤ T) + (hh : ∑ j ∈ Finset.range T, a j ≤ E) : + ∃ j, j < N ∧ a j ≤ E/N ∧ + ∑ i ∈ Finset.range m, (a (j+i)-a j) ≤ m^2*E/(N:ℝ)^2 := by + have hp : 0 < (N:ℝ) := by exact_mod_cast hN + let D := E/N + have hs (l : ℕ) (hl : l ≤ 2*N) : a l ≤ D := by + have Hlt : ∑ j ∈ Finset.range (l+N), a j ≤ E := + le_trans (Finset.sum_le_sum_of_subset_of_nonneg + (Finset.range_mono (by omega)) (fun i _ _ => h0 i)) hh + rw [Finset.sum_range_add] at Hlt + have H : ∑ _ ∈ Finset.range N, a l ≤ ∑ i ∈ Finset.range N, a (l+i) := + Finset.sum_le_sum fun i _ => ha (by omega : l ≤ l+i) + simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at H + apply (le_div_iff₀ hp).mpr + have : 0 ≤ ∑ j ∈ Finset.range l, a j := Finset.sum_nonneg fun j _ => h0 j + linarith + have HD : 0 ≤ D := div_nonneg hE hp.le + have hr (i : ℕ) (hi : i ≤ m) : + ∑ j ∈ Finset.range N, (a (j+i)-a j) ≤ m*D := by + have h1 := Finset.sum_range_add a N i + have h2 := Finset.sum_range_add a i N + have hcmp : ∑ j ∈ Finset.range i, a (N+j) ≤ ∑ j ∈ Finset.range i, D := by + apply Finset.sum_le_sum + intro j hj + apply hs; rw [Finset.mem_range] at hj; omega + simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul] at hcmp + have hl : 0 ≤ ∑ j ∈ Finset.range i, a j := Finset.sum_nonneg fun j _ => h0 j + have hle : (i:ℝ)*D ≤ (m:ℝ)*D := by gcongr + rw [show i+N=N+i by omega] at h2 + simp_rw [show ∀ j, j+i=i+j from fun j => add_comm _ _, Finset.sum_sub_distrib] + linarith + let f j := ∑ i ∈ Finset.range m, (a (j+i)-a j) + have hAvg : ∑ j ∈ Finset.range N, f j ≤ ∑ i ∈ Finset.range N, ((m:ℝ)^2*E/(N:ℝ)^2) := by + unfold f + rw [Finset.sum_comm] + calc + _ ≤ ∑ i ∈ Finset.range m, ((m:ℝ)*D) := + Finset.sum_le_sum fun i hi => hr i (Finset.mem_range.mp hi).le + _ = _ := by simp only [Finset.sum_const,Finset.card_range,nsmul_eq_mul,D]; field_simp + by_contra! hy + apply (not_lt_of_ge hAvg) + refine Finset.sum_lt_sum_of_nonempty ⟨0,Finset.mem_range.mpr hN⟩ (fun j hj => ?_) + have hj := Finset.mem_range.mp hj + + exact hy _ hj (hs _ (by omega)) + +variable {α G Ω : Type} [Fintype α] [Fintype G] [Fintype Ω] [AddCommGroup G] + +omit [AddCommGroup G] in +private lemma choose3 (P : Law Ω) (a b c : Ω → ℝ) + (ha : ∀ x, 0 ≤ a x) (hb : ∀ x, 0 ≤ b x) (hc : ∀ x, 0 ≤ c x) + (A B C : ℝ) (hA : 0 < A) (hB : 0 < B) (hC : 0 < C) + (h1 : P.Ex a ≤ A) (h2 : P.Ex b ≤ B) (h3 : P.Ex c ≤ C) : + ∃ x, 0 < P x ∧ a x ≤ 3*A ∧ b x ≤ 3*B ∧ c x ≤ 3*C := by + obtain ⟨x,hx,hh⟩ := P.exists_le_Ex fun x => a x*A⁻¹+b x*B⁻¹+c x*C⁻¹ + have H : a x/A+b x/B+c x/C ≤ 3 := by + simp_rw [Ex_add,Ex_mul, ← div_eq_mul_inv] at hh + linarith [(div_le_one hA).mpr h1,(div_le_one hB).mpr h2,(div_le_one hC).mpr h3] + have hI := div_nonneg (ha x) hA.le + have hJ := div_nonneg (hb x) hB.le + have hK := div_nonneg (hc x) hC.le + exact ⟨x,hx,(div_le_iff₀ hA).mp (by linarith),(div_le_iff₀ hB).mp + (by linarith),(div_le_iff₀ hC).mp (by linarith)⟩ + +lemma kl_covered {ι : Type} [Fintype ι] [DecidableEq ι] + (P : Law Ω) (X : Ω → ι → α) (q : Law α) (h : ∀ i, P.send (fun a => X a i) = q) : + covered P X q := by + intro a ha i + rw [← h i] + exact P.send_pos (fun a => X a i) ha + + +def iid (p : Law α) (n : ℕ) := pack (fun _ : Fin n => p) + +theorem select_context (tag : α → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) + (T N m : ℕ) (hm0 : 0 < m) (hm : m ≤ N) (hT : 3*N ≤ T) + (E : ℝ) (hE : 0 < E) [Nonempty α] + (h : ∀ p : Law α, exp (-E) ≤ heatMoment (Fin T) p tag c) : + ∃ (p : Law α) (Ψ : Law (WordSample N α G)), exch Ψ ∧ + (∀ i : Fin N, Ψ.send (fun a => a.1 i) = p) ∧ + log (Fintype.card α) - p.entropy ≤ 3*(E/N) ∧ + bill tag c p Ψ Prod.fst Prod.snd ≤ 3*E ∧ + tv (Ψ.send (prefixB Prod.fst m hm)) (iid p m) ≤ 2*m*√E/N := by + obtain ⟨P,hp,hx,hu⟩ := noisy_symm tag c E h + let X : WordSample T α G → Letters T α := Prod.fst + let L := log (Fintype.card α) + let a j := L-rateH P X j + have h0 : 0 < N := by omega + have h1 (j : ℕ) : 0 ≤ a j := sub_nonneg.mpr (rate_bdd P X j) + let u := uniform α + have hcov : covered P X u := kl_covered P X u hu + have heq : ∑ j ∈ Finset.range T, a j ≤ E := by + have H := kl_le_bill hcov tag c hc Prod.snd + dsimp only [a,L] + rw [sum_deficit] + exact le_trans (le_trans + (le_add_of_nonneg_right (P.Ex_nonneg fun x => hc (x.2))) H) hp + obtain ⟨j,hj,hb,hj'⟩ := choose_slow (a:=a) (fun x y hh => + sub_le_sub_left (rate_antitone hx hh) _) h1 hE.le T N m h0 hm hT heq + let W := prefixB X j (by omega) + let V := readBlock X j N (by omega) + let Y := readBlock X j m (by omega) + let R (w) := P.after W w + let H := readLetter X j (by omega) + let ν (w) := (R w).send H + let Pw := P.send W + let b (w) := L-(ν w).entropy + let k (w) := bill tag c u (R w) V Prod.snd + let d (w) := kl ((R w).send Y) (pack (fun _ : Fin m => ν w)) + have Hb (w : Letters j α) : 0 ≤ b w := + sub_nonneg.mpr (entropy_le_log_card _) + have Hk (w : Letters j α) : 0 ≤ k w := by + apply bill_nonneg _ tag c hc + intro a _ _ + rw [uniform_apply] + positivity + have hv (w) (i : Fin m) : + (R w).send (fun x => Y x i) = ν w := conditional_block_marg hx (j:=j) (m:=m) (by omega) w i + have hv' (w) (i : Fin N) : + (R w).send (fun x => V x i) = ν w := conditional_block_marg hx (j:=j) (m:=N) (by omega) w i + have Hd (w : Letters j α) : 0 ≤ d w := kl_nonneg + (covered_pack (kl_covered _ _ _ (hv w))) + have Hm' : (0:ℝ) < m := by exact_mod_cast hm0 + have HN : (0:ℝ) < N := by exact_mod_cast h0 + have H1 : Pw.Ex b ≤ E/N := by + unfold b ν Pw + rw [Ex_sub,Ex_const] + exact le_trans (le_of_eq (by + dsimp only [a] + rw [rate_lt P X (show j (V x,x.2)) + have hmx := conditional_block_exch hx (j:=j) (m:=N) (by omega) w + refine ⟨ν w,Ψ,hmx,fun i => ?_,h1,?_,?_⟩ + · dsimp only [Ψ] + rw [send_send]; exact hv' w i + · dsimp only [Ψ] + rw [bill_send] + exact (bill_change (covered_subfamily (covered_after hcov W w hw) (offset j N (by omega))) + tag c Prod.snd (ν w) (hv' w)).trans h2 + · have hh : Ψ.send (prefixB Prod.fst m hm) = (R w).send Y := by + unfold Ψ; rw [send_send, show Y = _ from block_cut X j N m (by omega) hm]; rfl + rw [hh] + let v := tv ((R w).send Y) (iid (ν w) m) + have ht : v^2 ≤ 3*((m:ℝ)^2*E/N^2) := + (tv_sq_le_kl (covered_pack (kl_covered _ _ _ (hv w)))).trans h3 + have h4 : 3*((m:ℝ)^2*E/N^2) ≤ (2*m*√E/N)^2 := by + rw [div_pow,mul_pow,mul_pow, sq_sqrt hE.le] + have H := pow_pos HN 2 + field_simp; nlinarith + change v ≤ _ + nlinarith [ht.trans h4,sqrt_nonneg E,show 0 ≤ 2*m*√E/N by positivity] + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Theorem.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Theorem.lean index 6518bcdd4816071b18cfcb3d8ec32fbde42b58d5..31073dcb75c534ae44f8823238e4db26b89d7f2e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Theorem.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Theorem.lean @@ -1,95 +1,95 @@ -import OAI.Analysis.FourierLLogL.Compress.Schedule -import OAI.Analysis.FourierLLogL.Compress.Phase -import OAI.Analysis.FourierLLogL.Compress.Low - -open Function Filter Set -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A G Θ : Type} [Fintype A] [Fintype G] [DecidableEq G] - [AddCommGroup G] [Nonempty A] [Fintype Θ] - -omit [Nonempty A] in -lemma init_gap (tag : A → G) (hi : Injective tag) (P : Law A) : - fundG tag (initial P) 1 = P.entropy := by - rw [G_initial] - let Q := iid P 1 - have h : Q.send (fun x => x 0) = P := marginalPack (fun _ => P) _ - change Q.H _ = _; rw [← h] - refine H_partition Q (X:=tot tag) (Y:=fun x => x 0) (fun _ _ => ?_) - simp only [tot,Fin.sum_univ_one,hi.eq_iff] - -/-- Finite compression from schedule and numerical constraints, before a -choice of scales in terms of height. -/ -lemma compress_arrays (tag : A → G) (hi : Injective tag) (q : Law G) - (hq : ∀ x, 0 < q x) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (hc0 : c 0=0) - (Q : Law Θ) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) - (hrq : ∀ g, (↑(exp (-c g)):ℂ)=Q.Cx fun t => χ t g) - (hlog : ∀ g, -log (q g) = -log (q 0) + c g) - (F L C : ℝ) (hF : 1 ≤ F) (hL : 0 ≤ L) - (T N m : ℕ) (hm : 0 < m) (hN : m ≤ N) (hT : 3*N ≤ T) (S : Schedule) - (l u r : ℕ → ℕ) - (hl : ∀ i, i < S.size → l i ≤ u i ∧ u i ≤ m ∧ 0 < u i ∧ 0 < r i ∧ - 2*r i*l i ≤ N ∧ L ≤ (r i:ℝ)*(u i:ℝ)) - (hp : ∀ i j, i < j → j < S.size → u j ≤ l i ∧ r i ≤ r j) - (hg : ∀ P : Law A, exp (-F) ≤ heatMoment (Fin T) P tag c) - (hC : 10*F+2*(-log (q 0))+1 ≤ C) - (hh : ∀ i, i < S.size → 0 < l i) - (hs : ∀ i, i < S.size → - 2*m*√F/N*lnS (Fin (u i)) tag ≤ F ∧ - log (Fintype.card (Ticket (r i) (2*r i))) + r i + - 2*(2*m*√F/N)*lnS (Fin (r i*l i)) tag ≤ 6*F) : - ∃ (a : A) (s : Finset A), s.Nonempty ∧ - log (Fintype.card A)-log (s.card:ℝ) ≤ - 8*(3*(F/N)+S.loss*C*C/F)+2 ∧ small_defects tag χ Q L s a (350*F) := by - have hFp : 0 < F := by linarith - have hCp : 0 < C := by - have hh := Real.log_nonpos (q.nonneg 0) (q.le_one 0) - linarith - have hNp : (0:ℝ)< N := by - exact_mod_cast (Nat.lt_of_lt_of_le hm hN) - obtain ⟨p,R,h1,h2,h3,h4,h5⟩ := select_context tag c hc T N m hm hN hT F hFp hg - let ε := 2*m*√F/N - let x : Starting tag c N m (3*F) ε := ⟨p,R,h1,h2,h4,hN,h5⟩ - let W := initial p - have ha (i) (hh : i < S.size) : fundG tag W (u i) ≤ C := by - have H := x.G_upper hc q hq (-log (q 0)) hlog _ (hl i hh).2.1 - apply H.trans - unfold ε - have hj := Real.log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num) - linarith [(hs i hh).1] - have hb (i) (hh : i < S.size) (hle : 1 ≤ l i) : fundI tag W (r i) ≤ C := by - refine (x.I_upper hc q hq (-log (q 0)) hlog _ ?_).trans ?_ - · have := (hl i hh).2.2.2.2.1 - nlinarith - linarith - - obtain ⟨P,hv,hW,i,hi',hK⟩ := - reach tag hi c hc W C F hCp hFp S m l u r hm (fun i h => ⟨(hl i h).1,(hl i h).2.1⟩) - hp ha (fun i h => hb i h (hh i h)) - obtain ⟨hi1,hi2,hu',hr',hn',hL'⟩ := hl i hi' - have HK : fundK tag c P (u i) (r i) ≤ 10*F := by - have H := x.K_upper hc hc0 (l i) (r i) hr' (le_trans hi1 hi2) hn' - have HH := K_anti tag c hv (l i) (r i) - change fundK tag c W _ _ ≤ _ at H - dsimp only [ε] at H - linarith [(hs i hi').2] - let D := 3*(F/N)+S.loss*C*C/F - have hD : 0 < D := by unfold D; have := S.loss_pos; positivity - have Hd : log (Fintype.card A) - fundG tag P 1 ≤ D := by - rw [show fundG tag W 1 = _ from init_gap tag hi p] at hW - linarith - obtain ⟨p',z,hz,hz'⟩ := freeze tag hi c hc P (u i) (r i) hu' hr' (10*F) D - (by positivity) hD HK Hd - have : NeZero (r i) := ⟨hr'.ne'⟩ - have : NeZero (u i) := ⟨hu'.ne'⟩ - have H0 := alignment tag c Q χ hn hrq p' z (10*F) D L (by linarith) hD hL - (by simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul]) hz (by - simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul]) - rwa [show 350*F = 35*(10*F) by ring] -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Schedule +import OAI.Analysis.FourierLLogL.Compress.Phase +import OAI.Analysis.FourierLLogL.Compress.Low + +open Function Filter Set +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A G Θ : Type} [Fintype A] [Fintype G] [DecidableEq G] + [AddCommGroup G] [Nonempty A] [Fintype Θ] + +omit [Nonempty A] in +lemma init_gap (tag : A → G) (hi : Injective tag) (P : Law A) : + fundG tag (initial P) 1 = P.entropy := by + rw [G_initial] + let Q := iid P 1 + have h : Q.send (fun x => x 0) = P := marginalPack (fun _ => P) _ + change Q.H _ = _; rw [← h] + refine H_partition Q (X:=tot tag) (Y:=fun x => x 0) (fun _ _ => ?_) + simp only [tot,Fin.sum_univ_one,hi.eq_iff] + +/-- Finite compression from schedule and numerical constraints, before a +choice of scales in terms of height. -/ +lemma compress_arrays (tag : A → G) (hi : Injective tag) (q : Law G) + (hq : ∀ x, 0 < q x) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (hc0 : c 0=0) + (Q : Law Θ) (χ : Θ → AddChar G ℂ) (hn : ∀ t x, ‖χ t x‖=1) + (hrq : ∀ g, (↑(exp (-c g)):ℂ)=Q.Cx fun t => χ t g) + (hlog : ∀ g, -log (q g) = -log (q 0) + c g) + (F L C : ℝ) (hF : 1 ≤ F) (hL : 0 ≤ L) + (T N m : ℕ) (hm : 0 < m) (hN : m ≤ N) (hT : 3*N ≤ T) (S : Schedule) + (l u r : ℕ → ℕ) + (hl : ∀ i, i < S.size → l i ≤ u i ∧ u i ≤ m ∧ 0 < u i ∧ 0 < r i ∧ + 2*r i*l i ≤ N ∧ L ≤ (r i:ℝ)*(u i:ℝ)) + (hp : ∀ i j, i < j → j < S.size → u j ≤ l i ∧ r i ≤ r j) + (hg : ∀ P : Law A, exp (-F) ≤ heatMoment (Fin T) P tag c) + (hC : 10*F+2*(-log (q 0))+1 ≤ C) + (hh : ∀ i, i < S.size → 0 < l i) + (hs : ∀ i, i < S.size → + 2*m*√F/N*lnS (Fin (u i)) tag ≤ F ∧ + log (Fintype.card (Ticket (r i) (2*r i))) + r i + + 2*(2*m*√F/N)*lnS (Fin (r i*l i)) tag ≤ 6*F) : + ∃ (a : A) (s : Finset A), s.Nonempty ∧ + log (Fintype.card A)-log (s.card:ℝ) ≤ + 8*(3*(F/N)+S.loss*C*C/F)+2 ∧ small_defects tag χ Q L s a (350*F) := by + have hFp : 0 < F := by linarith + have hCp : 0 < C := by + have hh := Real.log_nonpos (q.nonneg 0) (q.le_one 0) + linarith + have hNp : (0:ℝ)< N := by + exact_mod_cast (Nat.lt_of_lt_of_le hm hN) + obtain ⟨p,R,h1,h2,h3,h4,h5⟩ := select_context tag c hc T N m hm hN hT F hFp hg + let ε := 2*m*√F/N + let x : Starting tag c N m (3*F) ε := ⟨p,R,h1,h2,h4,hN,h5⟩ + let W := initial p + have ha (i) (hh : i < S.size) : fundG tag W (u i) ≤ C := by + have H := x.G_upper hc q hq (-log (q 0)) hlog _ (hl i hh).2.1 + apply H.trans + unfold ε + have hj := Real.log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num) + linarith [(hs i hh).1] + have hb (i) (hh : i < S.size) (hle : 1 ≤ l i) : fundI tag W (r i) ≤ C := by + refine (x.I_upper hc q hq (-log (q 0)) hlog _ ?_).trans ?_ + · have := (hl i hh).2.2.2.2.1 + nlinarith + linarith + + obtain ⟨P,hv,hW,i,hi',hK⟩ := + reach tag hi c hc W C F hCp hFp S m l u r hm (fun i h => ⟨(hl i h).1,(hl i h).2.1⟩) + hp ha (fun i h => hb i h (hh i h)) + obtain ⟨hi1,hi2,hu',hr',hn',hL'⟩ := hl i hi' + have HK : fundK tag c P (u i) (r i) ≤ 10*F := by + have H := x.K_upper hc hc0 (l i) (r i) hr' (le_trans hi1 hi2) hn' + have HH := K_anti tag c hv (l i) (r i) + change fundK tag c W _ _ ≤ _ at H + dsimp only [ε] at H + linarith [(hs i hi').2] + let D := 3*(F/N)+S.loss*C*C/F + have hD : 0 < D := by unfold D; have := S.loss_pos; positivity + have Hd : log (Fintype.card A) - fundG tag P 1 ≤ D := by + rw [show fundG tag W 1 = _ from init_gap tag hi p] at hW + linarith + obtain ⟨p',z,hz,hz'⟩ := freeze tag hi c hc P (u i) (r i) hu' hr' (10*F) D + (by positivity) hD HK Hd + have : NeZero (r i) := ⟨hr'.ne'⟩ + have : NeZero (u i) := ⟨hu'.ne'⟩ + have H0 := alignment tag c Q χ hn hrq p' z (10*F) D L (by linarith) hD hL + (by simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul]) hz (by + simpa only [Fintype.card_prod,Fintype.card_fin,Nat.cast_mul]) + rwa [show 350*F = 35*(10*F) by ring] +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/ThinChoice.lean b/lean/OAI/Analysis/FourierLLogL/Compress/ThinChoice.lean index c150635ed806deed41df41a49c2162a25972fcbe..76c7fab793b1b045768452feadbdec9fe19d48b7 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/ThinChoice.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/ThinChoice.lean @@ -1,213 +1,213 @@ -import OAI.Analysis.FourierLLogL.Compress.Select -/-! # Thinning to exact row marginals -/ -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A B Ω ι κ : Type} [Fintype A] [Fintype B] [DecidableEq B] [Fintype Ω] {n r : ℕ} - -abbrev Ticket (r n : ℕ) := Option (Fin r ↪ Fin n) - - -def gain (j : Fin r) (f : Fin n → ℝ) : Ticket r n → ℝ - | none => 0 - | some e => f (e j) - -private lemma perm_uniform (x : B) : - (uniform (Equiv.Perm B)).send (fun e => e x) = letI : Nonempty B := ⟨x⟩; uniform B := by - have : Nonempty B := ⟨x⟩ - let R := uniform (Equiv.Perm B) - let W := R.send (fun e => e x) - have he (a b : B) : W a = W b := by - classical - let f := Equiv.swap a b - have hh (e : Equiv.Perm B) : (e x=a) ↔ (f*e) x=b := by - change _ ↔ f (e x) = _ - rw [← show f a = b from Equiv.swap_apply_left _ _, f.injective.eq_iff] - unfold W; rw [send_apply,send_apply,prob_eq,prob_eq] - exact Fintype.sum_equiv (Equiv.mulLeft f) _ _ fun i => by - change _ = if (f*i) x=b then _ else _ - split_ifs <;> simp_all only - all_goals first | rfl | exact False.elim (by grind) - apply Law.ext - intro a - have hh (w : B) := he w a - have H := W.total - simp_rw [hh, Finset.sum_const,Finset.card_univ,nsmul_eq_mul] at H - change W a = _ - rw [uniform_apply] - field_simp - linarith - -private lemma count_frac (s : Finset (Fin n)) (hn : 0 < n) : - 0 ≤ (s.card:ℝ)/n ∧ (s.card:ℝ)/n ≤ 1 := by - have hp : (0:ℝ)< n := by exact_mod_cast hn - constructor - · positivity - · apply (div_le_one hp).mpr - exact_mod_cast (by simpa using Finset.card_le_univ s : s.card ≤ n) - -/-- Sampling rule for the realized set of tagged input blocks. -/ -lemma choose_tickets (r n : ℕ) (hr : 0 < r) (hn : 0 < n) (s : Finset (Fin n)) : - ∃ k : Law (Ticket r n), ∀ (j : Fin r) (f : Fin n → ℝ), - k.Ex (gain j f) = if r ≤ s.card then (∑ i ∈ s, f i)/n else 0 := by - classical - by_cases hs : r ≤ s.card - · have he : r ≤ Fintype.card s := by simpa using hs - have : Nonempty s := Fintype.card_pos_iff.mp (lt_of_lt_of_le hr he) - let t := (s.card:ℝ)/n - let g : Fin r ↪ s := (Fin.castLEEmb he).trans (Fintype.equivFin _).symm.toEmbedding - let T (f : Equiv.Perm s) : Ticket r n := - some ((g.trans f.toEmbedding).trans (Embedding.subtype _)) - refine ⟨mixture ((uniform (Equiv.Perm s)).send T) (point none) t - (count_frac _ hn).1 (count_frac _ hn).2,?_⟩ - intro j f - rw [ite_eq_left hs, mixture_Ex,Ex_point,Ex_send] - dsimp only [gain,T] - let F (x : s) := f x - - change t*(uniform (Equiv.Perm s)).Ex (fun i => F (i (g j))) + _*0 = _ - have h : (uniform (Equiv.Perm s)).Ex (fun i => F (i (g j))) = (uniform s).Ex F := by - have H := Ex_send (uniform (Equiv.Perm s)) (fun i => i (g j)) F - rw [perm_uniform] at H - exact H.symm - rw [h] - unfold t Ex F - have hh : 0 < (s.card:ℝ) := by exact_mod_cast lt_of_lt_of_le hr hs - simp only [uniform_apply,← Finset.mul_sum,mul_zero,add_zero, Fintype.card_coe, - Finset.univ_eq_attach,Finset.sum_attach] - field_simp - · refine ⟨point none,fun j f => ?_⟩ - rw [ite_eq_right hs,Ex_point]; rfl - -/-- A numerical tag probability. -/ -private def ratio (a b : ℝ) := min a b/a - -private lemma ratio_bounds (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : - 0 ≤ ratio a b ∧ ratio a b ≤ 1 ∧ a * ratio a b = min a b := by - unfold ratio - obtain hx|hx := eq_or_lt_of_le ha - · simp [← hx,hb] - exact ⟨div_nonneg (le_min ha hb) ha, (div_le_one hx).mpr (min_le_left _ _), by field_simp⟩ - -def tagChance (p q : Law A) (x : A) : Law Bool := - coinP (ratio (p x) (q x)) (ratio_bounds _ _ (p.nonneg _) (q.nonneg _)).1 - (ratio_bounds _ _ (p.nonneg _) (q.nonneg _)).2.1 - -lemma tag_test (p q : Law A) (f : A → ℝ) : - p.Ex (fun x => (tagChance p q x).Ex (fun b : Bool => if b then f x else 0)) = - ∑ x, min (p x) (q x) * f x := by - unfold tagChance - simp_rw [coinP_Ex] - simp only [Bool.false_eq_true, ↓reduceIte,mul_zero,add_zero,Ex,← mul_assoc, - (ratio_bounds (p _) (q _) (p.nonneg _) (q.nonneg _)).2.2] - -/-- Build a channel from the underlying input to the rejection/selected rows. -/ -lemma thin_sampler (r n : ℕ) (hr : 0 < r) (hn : n=2*r) (P : Law Ω) (q : Law A) - (X : Ω → Fin n → A) (ε : ℝ) - (hε : ∀ i, ((P.send fun x => X x i).tv q) ≤ ε) : - ∃ (k : Ω → Law (Ticket r n)), - let R := P.withNext k - R.prob (fun x => x.2 = none) ≤ 2 * ε ∧ - (∀ (j : Fin r) (f : A → ℝ) (_h : ∀ x, 0 ≤ f x), - R.Ex (fun x => gain j (fun i => f (X x.1 i)) x.2) ≤ q.Ex f) := by - classical - have hnp : 0 < n := by omega - have hNr : (0:ℝ) < n := by exact_mod_cast hnp - choose c hc using choose_tickets r n hr hnp - let p (i : Fin n) := P.send fun x => X x i - let Q (x) := pack (fun i : Fin n => tagChance (p i) q (X x i)) - let S (x : Fin n → Bool) := Finset.univ.filter (fun i => x i = true) - let k (x : Ω) := (Q x).through fun y => c (S y) - let R := P.withNext k - have hTag (x) (f : Fin n → ℝ) : - (Q x).Ex (fun b => ∑ i ∈ S b, f i) = - ∑ i : Fin n, (tagChance (p i) q (X x i)).Ex (fun b : Bool => if b then f i else 0) := by - unfold S Q - simp_rw [Finset.sum_filter,Ex_finset_sum] - apply Finset.sum_congr rfl - intro i _ - exact Ex_pack_proj _ i (fun b : Bool => if b then f i else 0) - have hComp (f : A → ℝ) : - P.Ex (fun x => (Q x).Ex fun b => ∑ i ∈ S b, f (X x i)) = - ∑ i : Fin n, ∑ a, min (p i a) (q a) * f a := by - have HH (x : Ω) := hTag x (fun i => f (X x i)) - simp_rw [HH,Ex_finset_sum] - apply Finset.sum_congr rfl; intro i _ - rw [← tag_test] - exact (Ex_send P (fun x => X x i) (fun y => - (tagChance (p i) q y).Ex (fun b => if b then f y else 0))).symm - have hTick (x) (j : Fin r) (f : Fin n → ℝ) : - (k x).Ex (gain j f) = - (Q x).Ex (fun b => if r ≤ (S b).card then (∑ i ∈ S b, f i)/n else 0) := by - unfold k; rw [Ex_through]; simp_rw [hc] - refine ⟨k,?_,?_⟩ - · let j : Fin r := ⟨0,hr⟩ - let F := fun (_ : Fin n) => (1:ℝ) - have hg : R.prob (fun x => x.2 = none) = - 1 - P.Ex (fun x => (k x).Ex (gain j F)) := by - unfold prob - rw [← Ex_withNext P k (fun x => gain j F x.2)] - calc - _ = R.Ex fun x => 1-gain j F x.2 := by - congr 1; funext x; cases x with - | mk a b => cases b <;> simp [gain,F] - _ = _ := by rw [Ex_sub (f:=fun _ => 1),Ex_const] - have h1 (x : Ω) : - 2/n * (Q x).Ex (fun b => ∑ i ∈ S b, F i)-1 ≤ (k x).Ex (gain j F) := by - rw [hTick] - have HH (b : Fin n → Bool) : - 2/(n:ℝ) * (∑ i ∈ S b, F i) - 1 ≤ if r ≤ (S b).card then - (∑ i ∈ S b, F i)/n else 0 := by - have ha : (↑n:ℝ) = 2*(r:ℝ) := by exact_mod_cast hn - have hb : ((S b).card:ℝ) ≤ n := by exact_mod_cast - (by simpa using Finset.card_le_univ (S b) : (S b).card ≤ n) - simp only [F,Finset.sum_const,nsmul_eq_mul,mul_one] - split_ifs with H - · field_simp; linarith - · have : ((S b).card:ℝ) ≤ r := by exact_mod_cast (le_of_not_ge H) - field_simp; linarith - refine le_trans (b:=(Q x).Ex (fun b => 2/(n:ℝ)*(∑ i ∈ S b, F i)-1)) - ?_ (Ex_mono _ HH) - rw [Ex_sub,Ex_lmul,Ex_const] - have HE := hComp (fun _ => 1) - have HF : (n:ℝ)*(1-ε) ≤ P.Ex fun x => (Q x).Ex (fun b => ∑ i ∈ S b, F i) := by - unfold F; rw [HE] - simp_rw [mul_one,tv_common] - apply le_trans (b:=∑ i : Fin n,(1-ε)) _ (Finset.sum_le_sum _) - · simp; apply le_of_eq; ring - intro i _; exact sub_le_sub_left (hε i) _ - have HL := P.Ex_mono h1 - rw [Ex_sub,Ex_lmul,Ex_const] at HL - change R.prob _ ≤ _ - rw [hg] - field_simp at HL - - nlinarith - · intro j f hf - change R.Ex _ ≤ _ - unfold R; rw [Ex_withNext] - have h1 (x : Ω) : - (k x).Ex (gain j (fun i => f (X x i))) ≤ - (Q x).Ex (fun b => ∑ i ∈ S b, f (X x i)) * (n:ℝ)⁻¹ := by - rw [hTick, ← Ex_mul] - apply Ex_mono - intro b - split_ifs - · simp [div_eq_mul_inv] - · exact mul_nonneg (Finset.sum_nonneg (fun i _ => hf _)) (by positivity) - refine le_trans (Ex_mono P h1) ?_ - rw [Ex_mul,hComp] - apply le_trans (b := (∑ i : Fin n, q.Ex f) * (n:ℝ)⁻¹) - · apply mul_le_mul_of_nonneg_right _ (by positivity) - refine Finset.sum_le_sum fun i _ => Finset.sum_le_sum fun a _ => - mul_le_mul_of_nonneg_right (min_le_right _ _) (hf _) - simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] - apply le_of_eq; field_simp - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Select +/-! # Thinning to exact row marginals -/ +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A B Ω ι κ : Type} [Fintype A] [Fintype B] [DecidableEq B] [Fintype Ω] {n r : ℕ} + +abbrev Ticket (r n : ℕ) := Option (Fin r ↪ Fin n) + + +def gain (j : Fin r) (f : Fin n → ℝ) : Ticket r n → ℝ + | none => 0 + | some e => f (e j) + +private lemma perm_uniform (x : B) : + (uniform (Equiv.Perm B)).send (fun e => e x) = letI : Nonempty B := ⟨x⟩; uniform B := by + have : Nonempty B := ⟨x⟩ + let R := uniform (Equiv.Perm B) + let W := R.send (fun e => e x) + have he (a b : B) : W a = W b := by + classical + let f := Equiv.swap a b + have hh (e : Equiv.Perm B) : (e x=a) ↔ (f*e) x=b := by + change _ ↔ f (e x) = _ + rw [← show f a = b from Equiv.swap_apply_left _ _, f.injective.eq_iff] + unfold W; rw [send_apply,send_apply,prob_eq,prob_eq] + exact Fintype.sum_equiv (Equiv.mulLeft f) _ _ fun i => by + change _ = if (f*i) x=b then _ else _ + split_ifs <;> simp_all only + all_goals first | rfl | exact False.elim (by grind) + apply Law.ext + intro a + have hh (w : B) := he w a + have H := W.total + simp_rw [hh, Finset.sum_const,Finset.card_univ,nsmul_eq_mul] at H + change W a = _ + rw [uniform_apply] + field_simp + linarith + +private lemma count_frac (s : Finset (Fin n)) (hn : 0 < n) : + 0 ≤ (s.card:ℝ)/n ∧ (s.card:ℝ)/n ≤ 1 := by + have hp : (0:ℝ)< n := by exact_mod_cast hn + constructor + · positivity + · apply (div_le_one hp).mpr + exact_mod_cast (by simpa using Finset.card_le_univ s : s.card ≤ n) + +/-- Sampling rule for the realized set of tagged input blocks. -/ +lemma choose_tickets (r n : ℕ) (hr : 0 < r) (hn : 0 < n) (s : Finset (Fin n)) : + ∃ k : Law (Ticket r n), ∀ (j : Fin r) (f : Fin n → ℝ), + k.Ex (gain j f) = if r ≤ s.card then (∑ i ∈ s, f i)/n else 0 := by + classical + by_cases hs : r ≤ s.card + · have he : r ≤ Fintype.card s := by simpa using hs + have : Nonempty s := Fintype.card_pos_iff.mp (lt_of_lt_of_le hr he) + let t := (s.card:ℝ)/n + let g : Fin r ↪ s := (Fin.castLEEmb he).trans (Fintype.equivFin _).symm.toEmbedding + let T (f : Equiv.Perm s) : Ticket r n := + some ((g.trans f.toEmbedding).trans (Embedding.subtype _)) + refine ⟨mixture ((uniform (Equiv.Perm s)).send T) (point none) t + (count_frac _ hn).1 (count_frac _ hn).2,?_⟩ + intro j f + rw [ite_eq_left hs, mixture_Ex,Ex_point,Ex_send] + dsimp only [gain,T] + let F (x : s) := f x + + change t*(uniform (Equiv.Perm s)).Ex (fun i => F (i (g j))) + _*0 = _ + have h : (uniform (Equiv.Perm s)).Ex (fun i => F (i (g j))) = (uniform s).Ex F := by + have H := Ex_send (uniform (Equiv.Perm s)) (fun i => i (g j)) F + rw [perm_uniform] at H + exact H.symm + rw [h] + unfold t Ex F + have hh : 0 < (s.card:ℝ) := by exact_mod_cast lt_of_lt_of_le hr hs + simp only [uniform_apply,← Finset.mul_sum,mul_zero,add_zero, Fintype.card_coe, + Finset.univ_eq_attach,Finset.sum_attach] + field_simp + · refine ⟨point none,fun j f => ?_⟩ + rw [ite_eq_right hs,Ex_point]; rfl + +/-- A numerical tag probability. -/ +private def ratio (a b : ℝ) := min a b/a + +private lemma ratio_bounds (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : + 0 ≤ ratio a b ∧ ratio a b ≤ 1 ∧ a * ratio a b = min a b := by + unfold ratio + obtain hx|hx := eq_or_lt_of_le ha + · simp [← hx,hb] + exact ⟨div_nonneg (le_min ha hb) ha, (div_le_one hx).mpr (min_le_left _ _), by field_simp⟩ + +def tagChance (p q : Law A) (x : A) : Law Bool := + coinP (ratio (p x) (q x)) (ratio_bounds _ _ (p.nonneg _) (q.nonneg _)).1 + (ratio_bounds _ _ (p.nonneg _) (q.nonneg _)).2.1 + +lemma tag_test (p q : Law A) (f : A → ℝ) : + p.Ex (fun x => (tagChance p q x).Ex (fun b : Bool => if b then f x else 0)) = + ∑ x, min (p x) (q x) * f x := by + unfold tagChance + simp_rw [coinP_Ex] + simp only [Bool.false_eq_true, ↓reduceIte,mul_zero,add_zero,Ex,← mul_assoc, + (ratio_bounds (p _) (q _) (p.nonneg _) (q.nonneg _)).2.2] + +/-- Build a channel from the underlying input to the rejection/selected rows. -/ +lemma thin_sampler (r n : ℕ) (hr : 0 < r) (hn : n=2*r) (P : Law Ω) (q : Law A) + (X : Ω → Fin n → A) (ε : ℝ) + (hε : ∀ i, ((P.send fun x => X x i).tv q) ≤ ε) : + ∃ (k : Ω → Law (Ticket r n)), + let R := P.withNext k + R.prob (fun x => x.2 = none) ≤ 2 * ε ∧ + (∀ (j : Fin r) (f : A → ℝ) (_h : ∀ x, 0 ≤ f x), + R.Ex (fun x => gain j (fun i => f (X x.1 i)) x.2) ≤ q.Ex f) := by + classical + have hnp : 0 < n := by omega + have hNr : (0:ℝ) < n := by exact_mod_cast hnp + choose c hc using choose_tickets r n hr hnp + let p (i : Fin n) := P.send fun x => X x i + let Q (x) := pack (fun i : Fin n => tagChance (p i) q (X x i)) + let S (x : Fin n → Bool) := Finset.univ.filter (fun i => x i = true) + let k (x : Ω) := (Q x).through fun y => c (S y) + let R := P.withNext k + have hTag (x) (f : Fin n → ℝ) : + (Q x).Ex (fun b => ∑ i ∈ S b, f i) = + ∑ i : Fin n, (tagChance (p i) q (X x i)).Ex (fun b : Bool => if b then f i else 0) := by + unfold S Q + simp_rw [Finset.sum_filter,Ex_finset_sum] + apply Finset.sum_congr rfl + intro i _ + exact Ex_pack_proj _ i (fun b : Bool => if b then f i else 0) + have hComp (f : A → ℝ) : + P.Ex (fun x => (Q x).Ex fun b => ∑ i ∈ S b, f (X x i)) = + ∑ i : Fin n, ∑ a, min (p i a) (q a) * f a := by + have HH (x : Ω) := hTag x (fun i => f (X x i)) + simp_rw [HH,Ex_finset_sum] + apply Finset.sum_congr rfl; intro i _ + rw [← tag_test] + exact (Ex_send P (fun x => X x i) (fun y => + (tagChance (p i) q y).Ex (fun b => if b then f y else 0))).symm + have hTick (x) (j : Fin r) (f : Fin n → ℝ) : + (k x).Ex (gain j f) = + (Q x).Ex (fun b => if r ≤ (S b).card then (∑ i ∈ S b, f i)/n else 0) := by + unfold k; rw [Ex_through]; simp_rw [hc] + refine ⟨k,?_,?_⟩ + · let j : Fin r := ⟨0,hr⟩ + let F := fun (_ : Fin n) => (1:ℝ) + have hg : R.prob (fun x => x.2 = none) = + 1 - P.Ex (fun x => (k x).Ex (gain j F)) := by + unfold prob + rw [← Ex_withNext P k (fun x => gain j F x.2)] + calc + _ = R.Ex fun x => 1-gain j F x.2 := by + congr 1; funext x; cases x with + | mk a b => cases b <;> simp [gain,F] + _ = _ := by rw [Ex_sub (f:=fun _ => 1),Ex_const] + have h1 (x : Ω) : + 2/n * (Q x).Ex (fun b => ∑ i ∈ S b, F i)-1 ≤ (k x).Ex (gain j F) := by + rw [hTick] + have HH (b : Fin n → Bool) : + 2/(n:ℝ) * (∑ i ∈ S b, F i) - 1 ≤ if r ≤ (S b).card then + (∑ i ∈ S b, F i)/n else 0 := by + have ha : (↑n:ℝ) = 2*(r:ℝ) := by exact_mod_cast hn + have hb : ((S b).card:ℝ) ≤ n := by exact_mod_cast + (by simpa using Finset.card_le_univ (S b) : (S b).card ≤ n) + simp only [F,Finset.sum_const,nsmul_eq_mul,mul_one] + split_ifs with H + · field_simp; linarith + · have : ((S b).card:ℝ) ≤ r := by exact_mod_cast (le_of_not_ge H) + field_simp; linarith + refine le_trans (b:=(Q x).Ex (fun b => 2/(n:ℝ)*(∑ i ∈ S b, F i)-1)) + ?_ (Ex_mono _ HH) + rw [Ex_sub,Ex_lmul,Ex_const] + have HE := hComp (fun _ => 1) + have HF : (n:ℝ)*(1-ε) ≤ P.Ex fun x => (Q x).Ex (fun b => ∑ i ∈ S b, F i) := by + unfold F; rw [HE] + simp_rw [mul_one,tv_common] + apply le_trans (b:=∑ i : Fin n,(1-ε)) _ (Finset.sum_le_sum _) + · simp; apply le_of_eq; ring + intro i _; exact sub_le_sub_left (hε i) _ + have HL := P.Ex_mono h1 + rw [Ex_sub,Ex_lmul,Ex_const] at HL + change R.prob _ ≤ _ + rw [hg] + field_simp at HL + + nlinarith + · intro j f hf + change R.Ex _ ≤ _ + unfold R; rw [Ex_withNext] + have h1 (x : Ω) : + (k x).Ex (gain j (fun i => f (X x i))) ≤ + (Q x).Ex (fun b => ∑ i ∈ S b, f (X x i)) * (n:ℝ)⁻¹ := by + rw [hTick, ← Ex_mul] + apply Ex_mono + intro b + split_ifs + · simp [div_eq_mul_inv] + · exact mul_nonneg (Finset.sum_nonneg (fun i _ => hf _)) (by positivity) + refine le_trans (Ex_mono P h1) ?_ + rw [Ex_mul,hComp] + apply le_trans (b := (∑ i : Fin n, q.Ex f) * (n:ℝ)⁻¹) + · apply mul_le_mul_of_nonneg_right _ (by positivity) + refine Finset.sum_le_sum fun i _ => Finset.sum_le_sum fun a _ => + mul_le_mul_of_nonneg_right (min_le_right _ _) (hf _) + simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] + apply le_of_eq; field_simp + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Thinning.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Thinning.lean index 2b09cee74147b5d9eb96434d10b3a7e54dedc6be..fbe36ce13ea49528cd1542fc9b1922928f0dfdd7 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Thinning.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Thinning.lean @@ -1,207 +1,207 @@ -import OAI.Analysis.FourierLLogL.Compress.ThinChoice -/-! # Thinned coupling -/ - -open Function Set Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {A G Ω I : Type} [Fintype A] [Fintype G] [Fintype Ω] [Fintype I] [DecidableEq I] - [AddCommGroup G] [DecidableEq G] -set_option linter.unusedSectionVars false - -def sums (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) : Finset G := - Finset.univ.image (tot tag : (I→A) → G) - -def lnS (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) := - log ((sums I tag).card) - -lemma lnS_bound (P : Law Ω) (X : Ω → I → A) (tag : A → G) : - P.H (fun x => tot tag (X x)) ≤ lnS I tag := by - classical - refine entropy_le_log_size _ _ fun x hx => ?_ - obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx - exact Finset.mem_image_of_mem _ (Finset.mem_univ _) - -lemma lnS_nonneg (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) : - 0 ≤ lnS I tag := by - unfold lnS - cases (sums I tag).card with - | zero => simp - | succ n => exact log_nonneg (by simp) - - -lemma kl_le_by_height (d : ℝ) (hd : 0 < d) (P Q : Law A) (h : ∀ x, d*P x ≤ Q x) : - P.kl Q ≤ -log d := by - rw [kl_ex,show -log d = P.Ex _ from (Ex_const P (-log d)).symm] - refine Ex_mono_mem P ?_ - intro x hx - have H := Real.log_le_log (mul_pos hd hx) (h x) - rw [log_mul hd.ne' hx.ne'] at H - linarith - -lemma fill_cost (tag : A → G) (c : G → ℝ) (hc : c 0=0) (q : Law A) - (d : ℝ) (hd : 0 < d) (Q : I → Law A) (hq : ∀ i x, d*Q i x ≤ q x) : - d * bill tag c q (pack Q) (fun x => x) (fun _ => 0) ≤ - Fintype.card I + d * lnS I tag := by - have hh (i : I) : (Q i).dominated q := - fun x hx => lt_of_lt_of_le (mul_pos hd hx) (hq i x) - have heq : covered (pack Q) (fun x => x) q := - fun x hx i => hh i _ ((pack_pos Q x).mp hx i) - rw [bill_with_entropy heq,send_id,pack_kl hh] - have Hk : ∑ i, kl (Q i) q ≤ ∑ _i : I, -log d := Finset.sum_le_sum - (fun i _ => kl_le_by_height d hd (Q i) q (hq i)) - have Hs := lnS_bound (pack Q) (fun x => x) tag - unfold ans - simp only [add_zero] - have Hn := Hcond_nonneg (pack Q) (fun _ => (0:G)) (fun x => x) - simp_rw [hc,Ex_const] - have HC := Real.negMulLog_le_one_sub_self hd.le - unfold negMulLog at HC - have hsub : d*(-log d) ≤ 1 := by linarith - calc - _ ≤ d*((∑ _i : I, -log d)+lnS I tag) := by - apply mul_le_mul_of_nonneg_left _ hd.le - linarith - _ ≤ _ := by - rw [Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - have H : (0:ℝ)≤ Fintype.card I := by positivity - nlinarith [mul_le_mul_of_nonneg_left hsub H] - -private lemma complete_row (δ : ℝ) (hd : 0 ≤ δ) (P : Law A) (a : A → ℝ) - (_h0 : ∀ x, 0 ≤ a x) (hp : ∀ x, a x ≤ P x) (ht : ∑ x, a x = 1-δ) : - ∃ R : Law A, ∀ x, a x + δ*R x = P x := by - have h : ∑ x, (P x - a x) = δ := by rw [Finset.sum_sub_distrib,P.total,ht]; ring - obtain hz|hz := eq_or_lt_of_le hd - · refine ⟨P,fun x => ?_⟩ - have HH := Finset.single_le_sum (s:=Finset.univ) (f:=fun x => P x-a x) - (fun x _ => sub_nonneg.mpr (hp x)) (Finset.mem_univ x) - rw [← hz,zero_mul]; linarith [hp x] - · refine ⟨proportional (fun x => P x-a x) (fun x => sub_nonneg.mpr (hp x)) (by rw [h]; exact hz),fun x => ?_⟩ - rw [proportional_apply,h]; field_simp; ring - -theorem thinned_bill (tag : A → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (hc0 : c 0 = 0) - (r n : ℕ) (hr : 0 < r) (hn : n=2*r) - (P : Law Ω) (X : Ω → Letters n A) (U : Ω → G) (q : Law A) - (hcov : covered P X q) (ε : ℝ) (hε : ∀ i, tv (P.send (fun x => X x i)) q ≤ ε) : - ∃ Q : Law (WordSample r A G), (∀ i, Q.send (fun a => a.1 i) = q) ∧ - bill tag c q Q Prod.fst Prod.snd ≤ bill tag c q P X U + - log (Fintype.card (Ticket r n)) + (r + 2*ε*lnS (Fin r) tag) := by - classical - obtain ⟨k, hδ, h⟩ := thin_sampler r n hr hn P q X ε hε - let R := P.withNext k - let W := Ticket r n - let Z : Ω × W → W := Prod.snd - let D := R.send Prod.snd - let δ := D none - have hd : 0 ≤ δ := D.nonneg _ - have hds : δ ≤ 2*ε := le_trans (le_of_eq (send_apply R Prod.snd none)) hδ - let X' (p : Ω × W) := X p.1 - let U' (p : Ω × W) := U p.1 - let QA (e : Fin r ↪ Fin n) : Law (WordSample r A G) := - (R.after Z (some e)).send (fun x => (keep X' e x,U' x)) - let m (i : Fin r) (a : A) := - ∑ e : Fin r ↪ Fin n, D (some e) * ((QA e).send (fun x => x.1 i)) a - have hmm (i : Fin r) (f : A → ℝ) : - ∑ e : Fin r ↪ Fin n, D (some e)* (QA e).Ex (fun x => f (x.1 i)) = - R.Ex (fun x => gain i (fun j => f (X' x j)) x.2) := by - let V (j : W) (x : Ω × W) := gain i (fun a => f (X' x a)) j - have H : D.Ex (fun w => (R.after Z w).Ex (V w)) = _ := - R.after_Ex_sum Z V - refine Eq.trans ?_ H - change _ = ∑ w, D w * (R.after Z w).Ex (V w) - rw [Fintype.sum_option] - unfold V gain - change _ = D none*((R.after Z none).Ex fun _ => 0)+(∑ e : Fin r ↪ Fin n,D (some e)*((R.after Z (some e)).Ex fun a => f (X' a (e i)))) - rw [Ex_const,mul_zero,zero_add] - refine Finset.sum_congr rfl fun e _ => ?_ - dsimp only [QA] - rw [Ex_send]; rfl - have Hm0 (i a) : 0 ≤ m i a := Finset.sum_nonneg fun e _ => - mul_nonneg (D.nonneg _) ((send _ _).nonneg _) - have Hmp (i a) : m i a ≤ q a := by - let f (x : A) : ℝ := if x=a then 1 else 0 - have HH := h i f (by intro x; unfold f; split_ifs <;> norm_num) - change R.Ex (fun x => gain i (fun j => f (X' x j)) x.2) ≤ _ at HH - rw [← hmm] at HH - have H1 (P : Law A) : P.Ex f = P a := by unfold f Ex; simp - have H2 (e : Fin r ↪ Fin n) : - (QA e).Ex (fun x => f (x.1 i)) = (QA e).send (fun x => x.1 i) a := - (Ex_send (QA e) (fun x => x.1 i) f).symm.trans (H1 _) - simp_rw [H1,H2] at HH; exact HH - have ht (i) : ∑ a, m i a = 1-δ := by - unfold m - rw [Finset.sum_comm] - simp only [← Finset.mul_sum,Law.total,mul_one] - have HH := D.total - rw [Fintype.sum_option] at HH - unfold δ; linarith - choose R' hR using fun i => complete_row δ hd q (m i) (Hm0 i) (Hmp i) (ht i) - let Ξ : Law (WordSample r A G) := (pack R').send (fun x => (x,0)) - let T (x : Ticket r n) := match x with - | none => Ξ - | some e => QA e - refine ⟨D.through T,fun i => ?_,?_⟩ - · apply Law.ext; intro a - let f (x : WordSample r A G) := x.1 i - have heq : (D.through T).send f = D.through (fun w => (T w).send f) := by - apply ext_of_Ex; intro g; rw [Ex_send] - simp_rw [Ex_through,Ex_send] - change (D.through T).send f a = q a - rw [heq,through_apply,Ex,Fintype.sum_option] - have h1 : (T none).send f a = R' i a := by - change (((pack R').send _).send _) _ = _ - unfold f; rw [send_send] - exact congrArg (fun P : Law A => P a) (marginalPack _ i) - rw [h1] - exact (add_comm _ _).trans (hR i a) - let C (Q : Law (WordSample r A G)) := bill tag c q Q Prod.fst Prod.snd - let V (w) := bill tag c q (R.after Z w) X' U' - have HV : D.Ex V ≤ bill tag c q P X U + log (Fintype.card (Ticket r n)) := by - have heq : bill tag c q R X' U' = bill tag c q P X U := - calc - _ = bill tag c q (R.send Prod.fst) X U := (bill_send tag c q R X U Prod.fst).symm - _ = _ := by rw [show R.send Prod.fst = P from send_fst_withNext P k] - refine (bill_condition tag c q R X' U' Z).trans ?_ - rw [heq] - exact add_le_add_right (H_card _ _) _ - have hr' : covered R X' q := fun x hx i => hcov _ (by - have H := R.send_pos Prod.fst hx; rwa [show R.send Prod.fst=P from send_fst_withNext P k] at H) i - have hb (e : Fin r ↪ Fin n) : - D (some e)*C (QA e) ≤ D (some e)*V (some e) := by - obtain hh|hh := eq_or_lt_of_le (D.nonneg (some e)) - · rw [← hh,zero_mul,zero_mul] - · apply mul_le_mul_of_nonneg_left _ hh.le - unfold C QA - rw [bill_send] - exact bill_subfamily tag c q (R.after Z _) X' U' (covered_after hr' Z _ hh) _ e.injective - have hxy : δ*C Ξ ≤ r + 2*ε*lnS (Fin r) tag + δ*V none := by - obtain he|he := eq_or_lt_of_le hd - · rw [← he,zero_mul,zero_mul] - have := lnS_nonneg (Fin r) tag - have : 0 ≤ ε := by linarith - positivity - unfold C Ξ - rw [bill_send] - have hF := fill_cost tag c hc0 q δ he R' (fun i x => by linarith [hR i x,Hm0 i x]) - rw [Fintype.card_fin] at hF - have hV : 0 ≤ V none := bill_nonneg (covered_after hr' Z _ he) _ _ hc _ - refine hF.trans ?_ - nlinarith [lnS_nonneg (Fin r) tag] - refine (bill_through_le tag c q D Prod.fst Prod.snd T).trans ?_ - change D.Ex (fun w => C (T w)) ≤ _ + _ - calc - _ ≤ D.Ex V + (r+2*ε*lnS (Fin r) tag) := by - unfold Ex - rw [Fintype.sum_option,Fintype.sum_option] - dsimp only [T] - have HH := Finset.sum_le_sum (s:=Finset.univ) (fun e _ => hb e) - unfold δ at hxy - linarith - _ ≤ _ := add_le_add_left HV _ - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.ThinChoice +/-! # Thinned coupling -/ + +open Function Set Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {A G Ω I : Type} [Fintype A] [Fintype G] [Fintype Ω] [Fintype I] [DecidableEq I] + [AddCommGroup G] [DecidableEq G] +set_option linter.unusedSectionVars false + +def sums (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) : Finset G := + Finset.univ.image (tot tag : (I→A) → G) + +def lnS (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) := + log ((sums I tag).card) + +lemma lnS_bound (P : Law Ω) (X : Ω → I → A) (tag : A → G) : + P.H (fun x => tot tag (X x)) ≤ lnS I tag := by + classical + refine entropy_le_log_size _ _ fun x hx => ?_ + obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx + exact Finset.mem_image_of_mem _ (Finset.mem_univ _) + +lemma lnS_nonneg (I : Type) [Fintype I] [DecidableEq I] (tag : A → G) : + 0 ≤ lnS I tag := by + unfold lnS + cases (sums I tag).card with + | zero => simp + | succ n => exact log_nonneg (by simp) + + +lemma kl_le_by_height (d : ℝ) (hd : 0 < d) (P Q : Law A) (h : ∀ x, d*P x ≤ Q x) : + P.kl Q ≤ -log d := by + rw [kl_ex,show -log d = P.Ex _ from (Ex_const P (-log d)).symm] + refine Ex_mono_mem P ?_ + intro x hx + have H := Real.log_le_log (mul_pos hd hx) (h x) + rw [log_mul hd.ne' hx.ne'] at H + linarith + +lemma fill_cost (tag : A → G) (c : G → ℝ) (hc : c 0=0) (q : Law A) + (d : ℝ) (hd : 0 < d) (Q : I → Law A) (hq : ∀ i x, d*Q i x ≤ q x) : + d * bill tag c q (pack Q) (fun x => x) (fun _ => 0) ≤ + Fintype.card I + d * lnS I tag := by + have hh (i : I) : (Q i).dominated q := + fun x hx => lt_of_lt_of_le (mul_pos hd hx) (hq i x) + have heq : covered (pack Q) (fun x => x) q := + fun x hx i => hh i _ ((pack_pos Q x).mp hx i) + rw [bill_with_entropy heq,send_id,pack_kl hh] + have Hk : ∑ i, kl (Q i) q ≤ ∑ _i : I, -log d := Finset.sum_le_sum + (fun i _ => kl_le_by_height d hd (Q i) q (hq i)) + have Hs := lnS_bound (pack Q) (fun x => x) tag + unfold ans + simp only [add_zero] + have Hn := Hcond_nonneg (pack Q) (fun _ => (0:G)) (fun x => x) + simp_rw [hc,Ex_const] + have HC := Real.negMulLog_le_one_sub_self hd.le + unfold negMulLog at HC + have hsub : d*(-log d) ≤ 1 := by linarith + calc + _ ≤ d*((∑ _i : I, -log d)+lnS I tag) := by + apply mul_le_mul_of_nonneg_left _ hd.le + linarith + _ ≤ _ := by + rw [Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + have H : (0:ℝ)≤ Fintype.card I := by positivity + nlinarith [mul_le_mul_of_nonneg_left hsub H] + +private lemma complete_row (δ : ℝ) (hd : 0 ≤ δ) (P : Law A) (a : A → ℝ) + (_h0 : ∀ x, 0 ≤ a x) (hp : ∀ x, a x ≤ P x) (ht : ∑ x, a x = 1-δ) : + ∃ R : Law A, ∀ x, a x + δ*R x = P x := by + have h : ∑ x, (P x - a x) = δ := by rw [Finset.sum_sub_distrib,P.total,ht]; ring + obtain hz|hz := eq_or_lt_of_le hd + · refine ⟨P,fun x => ?_⟩ + have HH := Finset.single_le_sum (s:=Finset.univ) (f:=fun x => P x-a x) + (fun x _ => sub_nonneg.mpr (hp x)) (Finset.mem_univ x) + rw [← hz,zero_mul]; linarith [hp x] + · refine ⟨proportional (fun x => P x-a x) (fun x => sub_nonneg.mpr (hp x)) (by rw [h]; exact hz),fun x => ?_⟩ + rw [proportional_apply,h]; field_simp; ring + +theorem thinned_bill (tag : A → G) (c : G → ℝ) (hc : ∀ x, 0 ≤ c x) (hc0 : c 0 = 0) + (r n : ℕ) (hr : 0 < r) (hn : n=2*r) + (P : Law Ω) (X : Ω → Letters n A) (U : Ω → G) (q : Law A) + (hcov : covered P X q) (ε : ℝ) (hε : ∀ i, tv (P.send (fun x => X x i)) q ≤ ε) : + ∃ Q : Law (WordSample r A G), (∀ i, Q.send (fun a => a.1 i) = q) ∧ + bill tag c q Q Prod.fst Prod.snd ≤ bill tag c q P X U + + log (Fintype.card (Ticket r n)) + (r + 2*ε*lnS (Fin r) tag) := by + classical + obtain ⟨k, hδ, h⟩ := thin_sampler r n hr hn P q X ε hε + let R := P.withNext k + let W := Ticket r n + let Z : Ω × W → W := Prod.snd + let D := R.send Prod.snd + let δ := D none + have hd : 0 ≤ δ := D.nonneg _ + have hds : δ ≤ 2*ε := le_trans (le_of_eq (send_apply R Prod.snd none)) hδ + let X' (p : Ω × W) := X p.1 + let U' (p : Ω × W) := U p.1 + let QA (e : Fin r ↪ Fin n) : Law (WordSample r A G) := + (R.after Z (some e)).send (fun x => (keep X' e x,U' x)) + let m (i : Fin r) (a : A) := + ∑ e : Fin r ↪ Fin n, D (some e) * ((QA e).send (fun x => x.1 i)) a + have hmm (i : Fin r) (f : A → ℝ) : + ∑ e : Fin r ↪ Fin n, D (some e)* (QA e).Ex (fun x => f (x.1 i)) = + R.Ex (fun x => gain i (fun j => f (X' x j)) x.2) := by + let V (j : W) (x : Ω × W) := gain i (fun a => f (X' x a)) j + have H : D.Ex (fun w => (R.after Z w).Ex (V w)) = _ := + R.after_Ex_sum Z V + refine Eq.trans ?_ H + change _ = ∑ w, D w * (R.after Z w).Ex (V w) + rw [Fintype.sum_option] + unfold V gain + change _ = D none*((R.after Z none).Ex fun _ => 0)+(∑ e : Fin r ↪ Fin n,D (some e)*((R.after Z (some e)).Ex fun a => f (X' a (e i)))) + rw [Ex_const,mul_zero,zero_add] + refine Finset.sum_congr rfl fun e _ => ?_ + dsimp only [QA] + rw [Ex_send]; rfl + have Hm0 (i a) : 0 ≤ m i a := Finset.sum_nonneg fun e _ => + mul_nonneg (D.nonneg _) ((send _ _).nonneg _) + have Hmp (i a) : m i a ≤ q a := by + let f (x : A) : ℝ := if x=a then 1 else 0 + have HH := h i f (by intro x; unfold f; split_ifs <;> norm_num) + change R.Ex (fun x => gain i (fun j => f (X' x j)) x.2) ≤ _ at HH + rw [← hmm] at HH + have H1 (P : Law A) : P.Ex f = P a := by unfold f Ex; simp + have H2 (e : Fin r ↪ Fin n) : + (QA e).Ex (fun x => f (x.1 i)) = (QA e).send (fun x => x.1 i) a := + (Ex_send (QA e) (fun x => x.1 i) f).symm.trans (H1 _) + simp_rw [H1,H2] at HH; exact HH + have ht (i) : ∑ a, m i a = 1-δ := by + unfold m + rw [Finset.sum_comm] + simp only [← Finset.mul_sum,Law.total,mul_one] + have HH := D.total + rw [Fintype.sum_option] at HH + unfold δ; linarith + choose R' hR using fun i => complete_row δ hd q (m i) (Hm0 i) (Hmp i) (ht i) + let Ξ : Law (WordSample r A G) := (pack R').send (fun x => (x,0)) + let T (x : Ticket r n) := match x with + | none => Ξ + | some e => QA e + refine ⟨D.through T,fun i => ?_,?_⟩ + · apply Law.ext; intro a + let f (x : WordSample r A G) := x.1 i + have heq : (D.through T).send f = D.through (fun w => (T w).send f) := by + apply ext_of_Ex; intro g; rw [Ex_send] + simp_rw [Ex_through,Ex_send] + change (D.through T).send f a = q a + rw [heq,through_apply,Ex,Fintype.sum_option] + have h1 : (T none).send f a = R' i a := by + change (((pack R').send _).send _) _ = _ + unfold f; rw [send_send] + exact congrArg (fun P : Law A => P a) (marginalPack _ i) + rw [h1] + exact (add_comm _ _).trans (hR i a) + let C (Q : Law (WordSample r A G)) := bill tag c q Q Prod.fst Prod.snd + let V (w) := bill tag c q (R.after Z w) X' U' + have HV : D.Ex V ≤ bill tag c q P X U + log (Fintype.card (Ticket r n)) := by + have heq : bill tag c q R X' U' = bill tag c q P X U := + calc + _ = bill tag c q (R.send Prod.fst) X U := (bill_send tag c q R X U Prod.fst).symm + _ = _ := by rw [show R.send Prod.fst = P from send_fst_withNext P k] + refine (bill_condition tag c q R X' U' Z).trans ?_ + rw [heq] + exact add_le_add_right (H_card _ _) _ + have hr' : covered R X' q := fun x hx i => hcov _ (by + have H := R.send_pos Prod.fst hx; rwa [show R.send Prod.fst=P from send_fst_withNext P k] at H) i + have hb (e : Fin r ↪ Fin n) : + D (some e)*C (QA e) ≤ D (some e)*V (some e) := by + obtain hh|hh := eq_or_lt_of_le (D.nonneg (some e)) + · rw [← hh,zero_mul,zero_mul] + · apply mul_le_mul_of_nonneg_left _ hh.le + unfold C QA + rw [bill_send] + exact bill_subfamily tag c q (R.after Z _) X' U' (covered_after hr' Z _ hh) _ e.injective + have hxy : δ*C Ξ ≤ r + 2*ε*lnS (Fin r) tag + δ*V none := by + obtain he|he := eq_or_lt_of_le hd + · rw [← he,zero_mul,zero_mul] + have := lnS_nonneg (Fin r) tag + have : 0 ≤ ε := by linarith + positivity + unfold C Ξ + rw [bill_send] + have hF := fill_cost tag c hc0 q δ he R' (fun i x => by linarith [hR i x,Hm0 i x]) + rw [Fintype.card_fin] at hF + have hV : 0 ≤ V none := bill_nonneg (covered_after hr' Z _ he) _ _ hc _ + refine hF.trans ?_ + nlinarith [lnS_nonneg (Fin r) tag] + refine (bill_through_le tag c q D Prod.fst Prod.snd T).trans ?_ + change D.Ex (fun w => C (T w)) ≤ _ + _ + calc + _ ≤ D.Ex V + (r+2*ε*lnS (Fin r) tag) := by + unfold Ex + rw [Fintype.sum_option,Fintype.sum_option] + dsimp only [T] + have HH := Finset.sum_le_sum (s:=Finset.univ) (fun e _ => hb e) + unfold δ at hxy + linarith + _ ≤ _ := add_le_add_left HV _ + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Compress/Variational.lean b/lean/OAI/Analysis/FourierLLogL/Compress/Variational.lean index d6d7acec12055e9c1240d7fd2ff879c2e7f36b5a..865da00956781bb5bacaa448d87b3f427a98f7a8 100644 --- a/lean/OAI/Analysis/FourierLLogL/Compress/Variational.lean +++ b/lean/OAI/Analysis/FourierLLogL/Compress/Variational.lean @@ -1,230 +1,230 @@ -import OAI.Analysis.FourierLLogL.Compress.Cost -/-! # Noisy variational construction -/ - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Compress -open Law Real -variable {ι α G : Type} [Fintype ι] [DecidableEq ι] [Fintype α] [Fintype G] [AddCommGroup G] - -abbrev Sample (ι α G : Type) := (ι → α) × G - -def Emp (P : Law (Sample ι α G)) (a : α) : ℝ := - ∑ i, P.send (fun x => x.1 i) a - -omit [AddCommGroup G] in -lemma Emp_cont : Continuous (fun P : Law (Sample ι α G) => Emp P) := - continuous_pi fun y => continuous_finsetSum _ fun _ _ => continuous_send_pdf _ y - -omit [AddCommGroup G] in -lemma Emp_mixture (P Q : Law (Sample ι α G)) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - Emp (mixture P Q t h0 h1) = t • Emp P + (1-t) • Emp Q := by - classical - funext x - simp only [Pi.add_apply, Pi.smul_apply,smul_eq_mul,Emp,send_apply,prob,mixture_Ex,Finset.sum_add_distrib,← Finset.mul_sum] - -/-- Average of exp(-c) on the sum-difference of 2T independent letters. -/ -def heatMoment (ι : Type) [Fintype ι] [DecidableEq ι] (p : Law α) (tag : α → G) (c : G → ℝ) : ℝ := - let Q := pack (fun _ : ι => p) - Q.Ex fun y => Q.Ex fun x => exp (-c (tot tag y - tot tag x)) - - -variable [Nonempty α] - -lemma trial_bill_uniform (tag : α → G) (c : G → ℝ) - (p : Law (ι → α)) (z : G) : - bill tag c (uniform α) (p.send (fun x => (x,z-tot tag x))) Prod.fst Prod.snd = - Fintype.card ι * log (Fintype.card α) - p.entropy + - p.Ex fun x => c (z-tot tag x) := by - rw [bill_send] - unfold bill - have HH : ans tag (fun x : ι → α => x) (fun x => z - tot tag x) = - (fun _ => z) := funext (fun x => by - change tot tag x + (z-tot tag x) = z - rw [add_comm,sub_add_cancel]) - change _ - Hcond p (fun x => x) (ans tag (fun x => x) (fun x => z - tot tag x)) + _ = _ - rw [HH,Hcond_constant,H_self] - simp only [uniform_apply, log_inv, neg_neg,Ex_const,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - -lemma exp_norm (a : α → ℝ) : 0 < ∑ x, exp (a x) := - Finset.sum_pos (fun _ _ => exp_pos _) Finset.univ_nonempty - -/-- The scalar potential dual test used below. -/ -lemma trial_cost_with_potential (tag : α → G) (c : G → ℝ) - (E : ℝ) (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) - (a : α → ℝ) : - ∃ P : Law (Sample ι α G), - bill tag c (uniform α) P Prod.fst Prod.snd + - ∑ i : ι, (P.Ex (fun x => a (x.1 i))-(uniform α).Ex a) ≤ E := by - classical - let s : ℝ := ∑ x, exp (-a x) - have hs : 0 < s := exp_norm _ - let p : Law α := proportional (fun x => exp (-a x)) (fun _ => exp_nonneg _) hs - have hpos (x : α) : 0 < p x := div_pos (exp_pos _) hs - let Q := pack fun _ : ι => p - obtain ⟨y,_,hy⟩ := Q.exists_Ex_le fun y => Q.Ex fun x => exp (-c (tot tag y - tot tag x)) - let z := tot tag y - let W (x) := Q x * exp (-c (z - tot tag x)) - have hpW (x) : 0 < W x := mul_pos ((pack_pos _ _).mpr fun _ => hpos _) (exp_pos _) - let D := ∑ x, W x - have hD : exp (-E) ≤ D := le_trans (h p) hy - have hD' : 0 < D := (exp_pos _).trans_le hD - let R := proportional W (fun x => (hpW x).le) hD' - let n := (Fintype.card ι : ℝ) - have hlogp (x : α) : log (p x) = -a x - log s := by - dsimp only [p,s] - rw [proportional_apply,log_div (exp_pos _).ne' hs.ne', log_exp] - have hlogQ (x) : log (Q x) = ∑ i : ι, (-a (x i) - log s) := by - dsimp only [Q]; rw [pack_apply,Real.log_prod (fun j _ => (hpos _).ne')] - simp_rw [hlogp] - have hlogR (x) : - -log (R x) = (∑ i, a (x i)) + n*log s + c (z - tot tag x) + log D := by - change -log (W x/D) = _ - rw [log_div (hpW x).ne' hD'.ne'] - unfold W; rw [log_mul ((pack_pos _ _).mpr fun _ => hpos _).ne' - (exp_pos _).ne',hlogQ,log_exp, Finset.sum_sub_distrib,Finset.sum_const,Finset.sum_neg_distrib] - unfold n; rw [Finset.card_univ,nsmul_eq_mul]; ring - have hR : R.entropy = - (∑ i, R.Ex (fun x => a (x i))) + n*log s + R.Ex (fun x => c (z-tot tag x)) + log D := by - unfold entropy crossE; simp_rw [hlogR,Ex_add,Ex_finset_sum,Ex_const] - refine ⟨R.send (fun x => (x,z-tot tag x)),?_⟩ - rw [trial_bill_uniform] - simp_rw [Ex_send,Finset.sum_sub_distrib,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - have hce := Law.entropy_le_cross_aux (uniform α) p p.nonneg p.total.le (fun x _ => hpos _) - rw [Law.entropy_uniform, crossE] at hce - have Hpl (x : α) : -log (p x) = a x + log s := by rw [hlogp]; ring - simp_rw [Hpl,Ex_add,Ex_const] at hce - have HH : -E ≤ log D := by - rw [← Real.exp_le_exp]; simpa only [Real.exp_log hD'] using hD - change n*_ - _ + _ + (_ - n*_) ≤ E - rw [hR] - have Hn : 0 ≤ n := by dsimp [n]; positivity - nlinarith - -omit [AddCommGroup G] in -private lemma Emp_linear (P : Law (Sample ι α G)) - (L : (α → ℝ) →L[ℝ] ℝ) : - L (Emp P - fun x => Fintype.card ι * uniform α x) = - ∑ i : ι, (P.Ex (fun x => L (point (x.1 i) : α → ℝ)) - - (uniform α).Ex (fun a => L (point a : α → ℝ))) := by - classical - have hh (Q : Law α) : L (fun a => Q a) = Q.Ex (fun a => L (point a : α → ℝ)) := by - have hEq : (fun a => Q a) = ∑ i : α, Q i • (fun a => point i a) := by - funext a - simp only [Finset.sum_apply,Pi.smul_apply,smul_eq_mul,point_apply] - simp - nth_rw 1 [hEq] - simp only [map_sum, map_smul, Ex] - rfl - have he : Emp P = ∑ i : ι, (fun y => P.send (fun x => x.1 i) y) := by - funext; simp only [Finset.sum_apply, Emp] - rw [map_sub,he,map_sum,show (fun x => Fintype.card ι * uniform α x) = - (Fintype.card ι : ℝ) • (fun x => uniform α x) from rfl,map_smul] - rw [hh (uniform α),Finset.sum_sub_distrib,Finset.sum_const,Finset.card_univ,nsmul_eq_mul,smul_eq_mul] - congr 1 - apply Finset.sum_congr rfl - intro i _ - rw [hh, Ex_send] - -/-- Version with uniform averaged one-letter marginal. -/ -theorem noisy_avg (tag : α → G) (c : G → ℝ) (E : ℝ) - (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) : - ∃ P : Law (Sample ι α G), - bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ - Emp P = fun x => Fintype.card ι * uniform α x := by - classical - let g (P : Law (Sample ι α G)) := Emp P - fun x => Fintype.card ι * uniform α x - have hh : ∃ P, bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ g P = 0 := by - refine Law.primal_feasible _ (bill_cont _ _ _ _ _) g (Emp_cont.sub continuous_const) - (fun _ _ _ _ _ => bill_mixture_le _ _ _ _ _ _ _ _ _ _) ?_ ?_ E ?_ - · intro P Q t h0 h1 - unfold g; rw [Emp_mixture] - module - · let P : Law (ι → α) := pack (fun _ => uniform α) - refine ⟨P.send (fun x => (x,(0:G))), ?_⟩ - unfold g - rw [sub_eq_zero] - ext1 x - simp_rw [Emp,send_send] - - simp only [P,marginalPack, Finset.sum_const,Finset.card_univ,nsmul_eq_mul] - intro L - obtain ⟨P,hP⟩ := trial_cost_with_potential tag c E h - (fun x => L (point x : α → ℝ)) - exact ⟨P,by unfold g; rw [Emp_linear]; exact hP⟩ - obtain ⟨P,h1,h2⟩ := hh - exact ⟨P,h1,sub_eq_zero.mp h2⟩ - - -def appPerm (e : Equiv.Perm ι) (x : Sample ι α G) : Sample ι α G := - ((fun i => x.1 (e i)), x.2) - -def exch (P : Law (Sample ι α G)) := ∀ e : Equiv.Perm ι, P.send (appPerm e) = P - -omit [AddCommGroup G] [Nonempty α] in -lemma same_marginal {P : Law (Sample ι α G)} (hx : exch P) (i j : ι) : - P.send (fun a => a.1 i) = P.send (fun a => a.1 j) := by - let e := Equiv.swap i j - have H := congrArg (fun r : Law (Sample ι α G) => send r (fun a => a.1 i)) (hx e) - rw [send_send] at H - simpa [appPerm,e] using H.symm - -omit [AddCommGroup G] [Nonempty α] in -lemma Emp_perm (P : Law (Sample ι α G)) (e : Equiv.Perm ι) : - Emp (P.send (appPerm e)) = Emp P := by - ext x - unfold Emp - simp_rw [send_send] - exact Fintype.sum_equiv e _ (fun i : ι => P.send (fun a => a.1 i) x) (fun i => rfl) - -/-- Symmetrize to obtain exact prescribed marginal in each coordinate. -/ -theorem noisy_symm (tag : α → G) (c : G → ℝ) (E : ℝ) - (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) : - ∃ P : Law (Sample ι α G), - bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ exch P ∧ - ∀ i : ι, P.send (fun x => x.1 i) = uniform α := by - classical - obtain ⟨P,hp,hp'⟩ := noisy_avg tag c E h - let V := uniform (Equiv.Perm ι) - let q (e : Equiv.Perm ι) := P.send (appPerm e) - let R := V.through q - have hx : exch R := by - intro e - apply ext_of_Ex; intro f - rw [Ex_send] - dsimp only [R,q] - simp_rw [Ex_through,Ex_send] - let g (d : Equiv.Perm ι) := P.Ex fun x => f (appPerm d x) - change V.Ex (fun d => P.Ex (fun x => f (appPerm e (appPerm d x)))) = V.Ex g - have heq (d : Equiv.Perm ι) : P.Ex (fun x => f (appPerm e (appPerm d x))) = g (d*e) := rfl - simp_rw [heq,Ex] - exact Fintype.sum_equiv (Equiv.mulRight e) _ _ (fun d => rfl) - refine ⟨R,?_ ,hx,?_⟩ - · have hq (e : Equiv.Perm ι) : - bill tag c (uniform α) (q e) Prod.fst Prod.snd ≤ E := by - dsimp only [q] - rw [bill_send] - have HH := bill_subfamily tag c (uniform α) P Prod.fst Prod.snd (fun x _ i => by - rw [uniform_apply]; positivity) e e.injective - exact HH.trans hp - exact le_trans (bill_through_le tag c _ _ _ _ _) ((V.Ex_mono hq).trans (V.Ex_const _).le) - intro i; have : Nonempty ι := ⟨i⟩ - apply Law.ext; intro x - have H (e) : Emp (q e) x = Fintype.card ι * (uniform α x) := by dsimp only [q]; rw [Emp_perm, hp'] - have H2 : Emp R x = V.Ex fun e => Emp (q e) x := by - unfold Emp R - simp_rw [send_apply,prob,Ex_through,Ex_finset_sum] - have H3 : Emp R x = (Fintype.card ι : ℝ) * R.send (fun a => a.1 i) x := by - calc - _ = ∑ j : ι, R.send (fun a => a.1 i) x := - Finset.sum_congr rfl fun j _ => by rw [same_marginal hx j i] - _ = _ := by simp - simp_rw [H, V.Ex_const] at H2 - exact mul_left_cancel₀ (by positivity : (Fintype.card ι : ℝ) ≠ 0) (H3.symm.trans H2) - -end FourierLLogL.Compress - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Cost +/-! # Noisy variational construction -/ + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Compress +open Law Real +variable {ι α G : Type} [Fintype ι] [DecidableEq ι] [Fintype α] [Fintype G] [AddCommGroup G] + +abbrev Sample (ι α G : Type) := (ι → α) × G + +def Emp (P : Law (Sample ι α G)) (a : α) : ℝ := + ∑ i, P.send (fun x => x.1 i) a + +omit [AddCommGroup G] in +lemma Emp_cont : Continuous (fun P : Law (Sample ι α G) => Emp P) := + continuous_pi fun y => continuous_finsetSum _ fun _ _ => continuous_send_pdf _ y + +omit [AddCommGroup G] in +lemma Emp_mixture (P Q : Law (Sample ι α G)) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + Emp (mixture P Q t h0 h1) = t • Emp P + (1-t) • Emp Q := by + classical + funext x + simp only [Pi.add_apply, Pi.smul_apply,smul_eq_mul,Emp,send_apply,prob,mixture_Ex,Finset.sum_add_distrib,← Finset.mul_sum] + +/-- Average of exp(-c) on the sum-difference of 2T independent letters. -/ +def heatMoment (ι : Type) [Fintype ι] [DecidableEq ι] (p : Law α) (tag : α → G) (c : G → ℝ) : ℝ := + let Q := pack (fun _ : ι => p) + Q.Ex fun y => Q.Ex fun x => exp (-c (tot tag y - tot tag x)) + + +variable [Nonempty α] + +lemma trial_bill_uniform (tag : α → G) (c : G → ℝ) + (p : Law (ι → α)) (z : G) : + bill tag c (uniform α) (p.send (fun x => (x,z-tot tag x))) Prod.fst Prod.snd = + Fintype.card ι * log (Fintype.card α) - p.entropy + + p.Ex fun x => c (z-tot tag x) := by + rw [bill_send] + unfold bill + have HH : ans tag (fun x : ι → α => x) (fun x => z - tot tag x) = + (fun _ => z) := funext (fun x => by + change tot tag x + (z-tot tag x) = z + rw [add_comm,sub_add_cancel]) + change _ - Hcond p (fun x => x) (ans tag (fun x => x) (fun x => z - tot tag x)) + _ = _ + rw [HH,Hcond_constant,H_self] + simp only [uniform_apply, log_inv, neg_neg,Ex_const,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + +lemma exp_norm (a : α → ℝ) : 0 < ∑ x, exp (a x) := + Finset.sum_pos (fun _ _ => exp_pos _) Finset.univ_nonempty + +/-- The scalar potential dual test used below. -/ +lemma trial_cost_with_potential (tag : α → G) (c : G → ℝ) + (E : ℝ) (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) + (a : α → ℝ) : + ∃ P : Law (Sample ι α G), + bill tag c (uniform α) P Prod.fst Prod.snd + + ∑ i : ι, (P.Ex (fun x => a (x.1 i))-(uniform α).Ex a) ≤ E := by + classical + let s : ℝ := ∑ x, exp (-a x) + have hs : 0 < s := exp_norm _ + let p : Law α := proportional (fun x => exp (-a x)) (fun _ => exp_nonneg _) hs + have hpos (x : α) : 0 < p x := div_pos (exp_pos _) hs + let Q := pack fun _ : ι => p + obtain ⟨y,_,hy⟩ := Q.exists_Ex_le fun y => Q.Ex fun x => exp (-c (tot tag y - tot tag x)) + let z := tot tag y + let W (x) := Q x * exp (-c (z - tot tag x)) + have hpW (x) : 0 < W x := mul_pos ((pack_pos _ _).mpr fun _ => hpos _) (exp_pos _) + let D := ∑ x, W x + have hD : exp (-E) ≤ D := le_trans (h p) hy + have hD' : 0 < D := (exp_pos _).trans_le hD + let R := proportional W (fun x => (hpW x).le) hD' + let n := (Fintype.card ι : ℝ) + have hlogp (x : α) : log (p x) = -a x - log s := by + dsimp only [p,s] + rw [proportional_apply,log_div (exp_pos _).ne' hs.ne', log_exp] + have hlogQ (x) : log (Q x) = ∑ i : ι, (-a (x i) - log s) := by + dsimp only [Q]; rw [pack_apply,Real.log_prod (fun j _ => (hpos _).ne')] + simp_rw [hlogp] + have hlogR (x) : + -log (R x) = (∑ i, a (x i)) + n*log s + c (z - tot tag x) + log D := by + change -log (W x/D) = _ + rw [log_div (hpW x).ne' hD'.ne'] + unfold W; rw [log_mul ((pack_pos _ _).mpr fun _ => hpos _).ne' + (exp_pos _).ne',hlogQ,log_exp, Finset.sum_sub_distrib,Finset.sum_const,Finset.sum_neg_distrib] + unfold n; rw [Finset.card_univ,nsmul_eq_mul]; ring + have hR : R.entropy = + (∑ i, R.Ex (fun x => a (x i))) + n*log s + R.Ex (fun x => c (z-tot tag x)) + log D := by + unfold entropy crossE; simp_rw [hlogR,Ex_add,Ex_finset_sum,Ex_const] + refine ⟨R.send (fun x => (x,z-tot tag x)),?_⟩ + rw [trial_bill_uniform] + simp_rw [Ex_send,Finset.sum_sub_distrib,Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + have hce := Law.entropy_le_cross_aux (uniform α) p p.nonneg p.total.le (fun x _ => hpos _) + rw [Law.entropy_uniform, crossE] at hce + have Hpl (x : α) : -log (p x) = a x + log s := by rw [hlogp]; ring + simp_rw [Hpl,Ex_add,Ex_const] at hce + have HH : -E ≤ log D := by + rw [← Real.exp_le_exp]; simpa only [Real.exp_log hD'] using hD + change n*_ - _ + _ + (_ - n*_) ≤ E + rw [hR] + have Hn : 0 ≤ n := by dsimp [n]; positivity + nlinarith + +omit [AddCommGroup G] in +private lemma Emp_linear (P : Law (Sample ι α G)) + (L : (α → ℝ) →L[ℝ] ℝ) : + L (Emp P - fun x => Fintype.card ι * uniform α x) = + ∑ i : ι, (P.Ex (fun x => L (point (x.1 i) : α → ℝ)) - + (uniform α).Ex (fun a => L (point a : α → ℝ))) := by + classical + have hh (Q : Law α) : L (fun a => Q a) = Q.Ex (fun a => L (point a : α → ℝ)) := by + have hEq : (fun a => Q a) = ∑ i : α, Q i • (fun a => point i a) := by + funext a + simp only [Finset.sum_apply,Pi.smul_apply,smul_eq_mul,point_apply] + simp + nth_rw 1 [hEq] + simp only [map_sum, map_smul, Ex] + rfl + have he : Emp P = ∑ i : ι, (fun y => P.send (fun x => x.1 i) y) := by + funext; simp only [Finset.sum_apply, Emp] + rw [map_sub,he,map_sum,show (fun x => Fintype.card ι * uniform α x) = + (Fintype.card ι : ℝ) • (fun x => uniform α x) from rfl,map_smul] + rw [hh (uniform α),Finset.sum_sub_distrib,Finset.sum_const,Finset.card_univ,nsmul_eq_mul,smul_eq_mul] + congr 1 + apply Finset.sum_congr rfl + intro i _ + rw [hh, Ex_send] + +/-- Version with uniform averaged one-letter marginal. -/ +theorem noisy_avg (tag : α → G) (c : G → ℝ) (E : ℝ) + (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) : + ∃ P : Law (Sample ι α G), + bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ + Emp P = fun x => Fintype.card ι * uniform α x := by + classical + let g (P : Law (Sample ι α G)) := Emp P - fun x => Fintype.card ι * uniform α x + have hh : ∃ P, bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ g P = 0 := by + refine Law.primal_feasible _ (bill_cont _ _ _ _ _) g (Emp_cont.sub continuous_const) + (fun _ _ _ _ _ => bill_mixture_le _ _ _ _ _ _ _ _ _ _) ?_ ?_ E ?_ + · intro P Q t h0 h1 + unfold g; rw [Emp_mixture] + module + · let P : Law (ι → α) := pack (fun _ => uniform α) + refine ⟨P.send (fun x => (x,(0:G))), ?_⟩ + unfold g + rw [sub_eq_zero] + ext1 x + simp_rw [Emp,send_send] + + simp only [P,marginalPack, Finset.sum_const,Finset.card_univ,nsmul_eq_mul] + intro L + obtain ⟨P,hP⟩ := trial_cost_with_potential tag c E h + (fun x => L (point x : α → ℝ)) + exact ⟨P,by unfold g; rw [Emp_linear]; exact hP⟩ + obtain ⟨P,h1,h2⟩ := hh + exact ⟨P,h1,sub_eq_zero.mp h2⟩ + + +def appPerm (e : Equiv.Perm ι) (x : Sample ι α G) : Sample ι α G := + ((fun i => x.1 (e i)), x.2) + +def exch (P : Law (Sample ι α G)) := ∀ e : Equiv.Perm ι, P.send (appPerm e) = P + +omit [AddCommGroup G] [Nonempty α] in +lemma same_marginal {P : Law (Sample ι α G)} (hx : exch P) (i j : ι) : + P.send (fun a => a.1 i) = P.send (fun a => a.1 j) := by + let e := Equiv.swap i j + have H := congrArg (fun r : Law (Sample ι α G) => send r (fun a => a.1 i)) (hx e) + rw [send_send] at H + simpa [appPerm,e] using H.symm + +omit [AddCommGroup G] [Nonempty α] in +lemma Emp_perm (P : Law (Sample ι α G)) (e : Equiv.Perm ι) : + Emp (P.send (appPerm e)) = Emp P := by + ext x + unfold Emp + simp_rw [send_send] + exact Fintype.sum_equiv e _ (fun i : ι => P.send (fun a => a.1 i) x) (fun i => rfl) + +/-- Symmetrize to obtain exact prescribed marginal in each coordinate. -/ +theorem noisy_symm (tag : α → G) (c : G → ℝ) (E : ℝ) + (h : ∀ p : Law α, exp (-E) ≤ heatMoment ι p tag c) : + ∃ P : Law (Sample ι α G), + bill tag c (uniform α) P Prod.fst Prod.snd ≤ E ∧ exch P ∧ + ∀ i : ι, P.send (fun x => x.1 i) = uniform α := by + classical + obtain ⟨P,hp,hp'⟩ := noisy_avg tag c E h + let V := uniform (Equiv.Perm ι) + let q (e : Equiv.Perm ι) := P.send (appPerm e) + let R := V.through q + have hx : exch R := by + intro e + apply ext_of_Ex; intro f + rw [Ex_send] + dsimp only [R,q] + simp_rw [Ex_through,Ex_send] + let g (d : Equiv.Perm ι) := P.Ex fun x => f (appPerm d x) + change V.Ex (fun d => P.Ex (fun x => f (appPerm e (appPerm d x)))) = V.Ex g + have heq (d : Equiv.Perm ι) : P.Ex (fun x => f (appPerm e (appPerm d x))) = g (d*e) := rfl + simp_rw [heq,Ex] + exact Fintype.sum_equiv (Equiv.mulRight e) _ _ (fun d => rfl) + refine ⟨R,?_ ,hx,?_⟩ + · have hq (e : Equiv.Perm ι) : + bill tag c (uniform α) (q e) Prod.fst Prod.snd ≤ E := by + dsimp only [q] + rw [bill_send] + have HH := bill_subfamily tag c (uniform α) P Prod.fst Prod.snd (fun x _ i => by + rw [uniform_apply]; positivity) e e.injective + exact HH.trans hp + exact le_trans (bill_through_le tag c _ _ _ _ _) ((V.Ex_mono hq).trans (V.Ex_const _).le) + intro i; have : Nonempty ι := ⟨i⟩ + apply Law.ext; intro x + have H (e) : Emp (q e) x = Fintype.card ι * (uniform α x) := by dsimp only [q]; rw [Emp_perm, hp'] + have H2 : Emp R x = V.Ex fun e => Emp (q e) x := by + unfold Emp R + simp_rw [send_apply,prob,Ex_through,Ex_finset_sum] + have H3 : Emp R x = (Fintype.card ι : ℝ) * R.send (fun a => a.1 i) x := by + calc + _ = ∑ j : ι, R.send (fun a => a.1 i) x := + Finset.sum_congr rfl fun j _ => by rw [same_marginal hx j i] + _ = _ := by simp + simp_rw [H, V.Ex_const] at H2 + exact mul_left_cancel₀ (by positivity : (Fintype.card ι : ℝ) ≠ 0) (H3.symm.trans H2) + +end FourierLLogL.Compress + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Circle.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Circle.lean index 887c4ecd290d817abc1e2dfc6bb80ecdb30036c5..c893b804fef4b133241963d3d350875366899cf6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Circle.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Circle.lean @@ -1,192 +1,192 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Discrete -/-! # Grid distance and elementary counting -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Law Period Real - -variable {M : ℕ} - -def rad (x : Grid M) : ℝ := ↑(min x.val (-x).val) -def diam (x : Grid M) : ℝ := side M * rad x - -def liftInt (x : Grid M) : ℤ := if x.val ≤ (-x).val then x.val else -↑(-x).val - -lemma rad_abs (x : Grid M) : rad x = |(liftInt x:ℝ)| := by - unfold liftInt rad - split_ifs with h - · rw [min_eq_left h, Int.cast_natCast,abs_of_nonneg (by positivity)] - · rw [min_eq_right (by omega),Int.cast_neg, abs_neg,Int.cast_natCast,abs_of_nonneg (by positivity)] - -lemma liftInt_cast (x : Grid M) : (liftInt x:Grid M) = x := by unfold liftInt; split_ifs <;> simp -lemma rad_even (x : Grid M) : rad (-x) = rad x := by simp [rad,min_comm] -lemma diam_even (x : Grid M) : diam (-x) = diam x := by rw [diam,diam,rad_even] - -lemma rad_nonneg (x : Grid M) : 0 ≤ rad x := by rw [rad_abs]; positivity -lemma diam_nonneg (x : Grid M) : 0 ≤ diam x := mul_nonneg (side_pos _).le (rad_nonneg _) - -lemma pos_nonneg (x : Grid M) : 0 ≤ pos x := mul_nonneg (by positivity) (side_pos _).le -lemma pos_lt (x : Grid M) : pos x < 1 := (atom_sub x (pos_mem x)).2 -lemma pos_neg {x : Grid M} (hx : x≠0) : pos (-x) = 1-pos x := by - have : NeZero x := ⟨hx⟩ - simp_rw [pos, ZMod.val_neg_of_ne_zero, Nat.cast_sub (ZMod.val_lt _).le, sub_mul,side_pow_two] - push_cast - rw [mul_inv_cancel₀ (by positivity)] - -lemma diam_min (x : Grid M) : diam x = min (pos x) (pos (-x)) := by - unfold diam rad pos - rw [Nat.cast_min, mul_comm, min_mul_of_nonneg _ _ (side_pos _).le] - -lemma diam_le_half (x : Grid M) : diam x ≤ 1/2 := by - rw [diam_min] - by_cases hx : x=0 - · subst x; simp [pos] - · rw [pos_neg hx, min_le_iff] - by_cases H : pos x ≤ 1/2 - · exact Or.inl H - · exact Or.inr (by linarith) - -lemma rad_le_val (x : Grid M) : rad x ≤ ↑x.val := by simp [rad] -lemma rad_le_nat (k : ℕ) : rad (k:Grid M) ≤ ↑k := by - refine (rad_le_val _).trans ?_ - rw [ZMod.val_natCast]; exact_mod_cast (Nat.mod_le k (2^M)) - -lemma rad_le_int (k : ℤ) : rad (k:Grid M) ≤ |(k:ℝ)| := by - cases k with - | ofNat k => simpa using rad_le_nat (M:=M) k - | negSucc k => - rw [show ((Int.negSucc k:ℤ):Grid M) = -((k+1:ℕ):Grid M) by simp, rad_even] - convert rad_le_nat (M:=M) (k+1) using 1 - push_cast - rw [abs_of_nonpos (by have H := Nat.cast_nonneg (α:=ℝ) k; linarith)]; ring - -lemma rad_add (x y : Grid M) : rad (x+y) ≤ rad x+rad y := by - have H := rad_le_int (M:=M) (liftInt x + liftInt y) - rw [Int.cast_add,liftInt_cast,liftInt_cast,Int.cast_add] at H - rw [rad_abs x,rad_abs y] - exact H.trans (abs_add_le _ _) -lemma diam_add (x y : Grid M) : diam (x+y) ≤ diam x+diam y := by - simp_rw [diam]; rw [← mul_add] - exact mul_le_mul_of_nonneg_left (rad_add _ _) (side_pos _).le - -lemma pos_sub_val {x y : Grid M} (h : y.val ≤ x.val) : pos (x-y) = pos x-pos y := by - simp [pos,ZMod.val_sub h,Nat.cast_sub h,sub_mul] - -lemma diam_le_diff (x y : Grid M) : diam (x-y) ≤ |pos x-pos y| := by - rcases le_total y.val x.val with h|h - · exact le_trans (show diam (x-y) ≤ pos (x-y) by rw [diam_min]; exact min_le_left _ _) - (by rw [pos_sub_val h]; exact le_abs_self _) - rw [diam_min,neg_sub] - exact (min_le_right _ _).trans (by rw [pos_sub_val h,abs_sub_comm]; exact le_abs_self _) - -lemma phase_distance (s x y : Grid M) : - ‖chi s x-chi s y‖ ≤ rad s * (8 * |pos x-pos y|) := by - calc - _ = ‖phase (liftInt s :ℝ) (pos x) - phase (liftInt s:ℝ) (pos y)‖ := by - rw [← chi_cast,← chi_cast,liftInt_cast] - _ ≤ _ := (norm_phase_sub _ _ _).trans (by rw [rad_abs s]; gcongr; linarith [pi_le_four]) - - -def first (M m : ℕ) : Finset (Grid M) := - Finset.univ.filter fun x => x.val < m -lemma mem_first (m : ℕ) (x : Grid M) : x ∈ first M m ↔ x.val < m := by simp [first] - -lemma first_eq_range (m : ℕ) (h : m ≤ 2^M) : - first M m = (Finset.range m).image (fun k : ℕ => (k:Grid M)) := by - ext x - rw [mem_first,Finset.mem_image] - constructor - · intro hi; exact ⟨x.val,Finset.mem_range.mpr hi, ZMod.natCast_zmod_val _⟩ - · rintro ⟨k,hk,rfl⟩; rw [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hk).trans_le h)] - exact Finset.mem_range.mp hk - -lemma range_inj {m : ℕ} (h : m ≤ 2^M) : - Set.InjOn (fun k : ℕ => (k:Grid M)) (Finset.range m) := by - intro i hi j hj HH - have he := congrArg ZMod.val HH - rwa [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hi).trans_le h), - ZMod.val_natCast_of_lt ((Finset.mem_range.mp hj).trans_le h)] at he - -lemma first_card (m : ℕ) (h : m ≤ 2^M) : (first M m).card = m := by - rw [first_eq_range m h,Finset.card_image_of_injOn (range_inj h), Finset.card_range] - -lemma first_card_le (m : ℕ) : (first M m).card ≤ m := by - rcases le_total m (2^M) with h | h - · exact (first_card _ h).le - · refine le_trans (Finset.card_le_univ _) ?_ - simpa [Grid] using h - -lemma sum_first {E : Type*} [AddCommMonoid E] (m : ℕ) (h : m ≤ 2^M) (f : ℕ → E) : - ∑ x ∈ first M m, f x.val = ∑ k ∈ Finset.range m, f k := by - rw [first_eq_range _ h, Finset.sum_image] - · exact Finset.sum_congr rfl fun i hi => by - rw [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hi).trans_le h)] - intro i hi j hj h' - exact range_inj h hi hj h' - -lemma first_max : first M (2^M) = Finset.univ := by - ext x; simp [first,ZMod.val_lt] - -lemma sum_grid {E : Type*} [AddCommMonoid E] (f : ℕ → E) : - ∑ x : Grid M, f x.val = ∑ k ∈ Finset.range (2^M), f k := by - rw [← sum_first _ le_rfl, first_max] - -def arc (M : ℕ) (r : ℝ) : Finset (Grid M) := Finset.univ.filter fun x => rad x ≤ r -lemma mem_arc (r : ℝ) (x : Grid M) : x ∈ arc M r ↔ rad x ≤ r := by simp [arc] - -lemma arc_card_hi (M : ℕ) (r : ℝ) (hr : 0 ≤ r) : - ((arc M r).card :ℝ) ≤ 2*(r+1) := by - let m := Nat.floor r +1 - have hin : - arc M r ⊆ first M m ∪ (first M m).image Neg.neg := by - intro x hx - rw [mem_arc, rad, Nat.cast_min,min_le_iff] at hx - rw [Finset.mem_union,Finset.mem_image,mem_first] - rcases hx with H | H - · exact Or.inl (Nat.lt_succ_of_le ((Nat.le_floor_iff hr).mpr H)) - exact Or.inr ⟨-x, (mem_first _ _).mpr (Nat.lt_succ_of_le ((Nat.le_floor_iff hr).mpr H)),neg_neg _⟩ - have h1 := (Finset.card_le_card hin).trans (Finset.card_union_le _ _) - have h2 := Finset.card_image_le (s:=first M m) (f:=Neg.neg) - have he : (arc M r).card ≤ 2*m := by have := first_card_le (M:=M) m; omega - calc - _ ≤ ((2*m:ℕ):ℝ) := Nat.cast_le.mpr he - _ ≤ _ := by unfold m; push_cast; have H := Nat.floor_le hr; linarith - -lemma arc_card_lo (M : ℕ) (r : ℝ) (_hr : 0 ≤ r) (h : r ≤ 2^M) : - r ≤ ((arc M r).card:ℝ) := by - let m := Nat.ceil r - have hm : m ≤ 2^M := Nat.ceil_le.mpr (by exact_mod_cast h) - have hh : first M m ⊆ arc M r := by - intro x hx - rw [mem_first] at hx; rw [mem_arc] - exact (rad_le_val x).trans ((Nat.lt_ceil.mp hx).le) - calc - _ ≤ (m:ℝ) := Nat.le_ceil _ - _ ≤ _ := Nat.cast_le.mpr (by rw [← first_card m hm]; exact Finset.card_le_card hh) - -lemma side_card (M : ℕ) : side M * (2:ℝ)^M = 1 := by rw [side_pow_two]; field_simp - -lemma arc_length (M : ℕ) (r : ℝ) (hr : 0 ≤ r) : - (uniform (Grid M)).prob (fun x => diam x ≤ r) ≤ 2*(r+side M) := by - rw [prob_eq] - have Heq : - (∑ x : Grid M, if diam x ≤ r then uniform (Grid M) x else 0) = - (arc M (r / side M)).card* side M := by - have he : arc M (r / side M) = Finset.univ.filter fun x : Grid M => diam x ≤ r := by - ext x - simp [diam,arc,le_div_iff₀ (side_pos M),mul_comm] - rw [← Finset.sum_filter,← he] - simp [uniform_side] - rw [Heq] - calc - _ ≤ (2*(r / side M +1))*side M := by - apply mul_le_mul_of_nonneg_right (arc_card_hi M _ (div_nonneg hr (side_pos _).le)) (side_pos _).le - _ = _ := by field_simp [(side_pos _).ne'] - -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Discrete +/-! # Grid distance and elementary counting -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Law Period Real + +variable {M : ℕ} + +def rad (x : Grid M) : ℝ := ↑(min x.val (-x).val) +def diam (x : Grid M) : ℝ := side M * rad x + +def liftInt (x : Grid M) : ℤ := if x.val ≤ (-x).val then x.val else -↑(-x).val + +lemma rad_abs (x : Grid M) : rad x = |(liftInt x:ℝ)| := by + unfold liftInt rad + split_ifs with h + · rw [min_eq_left h, Int.cast_natCast,abs_of_nonneg (by positivity)] + · rw [min_eq_right (by omega),Int.cast_neg, abs_neg,Int.cast_natCast,abs_of_nonneg (by positivity)] + +lemma liftInt_cast (x : Grid M) : (liftInt x:Grid M) = x := by unfold liftInt; split_ifs <;> simp +lemma rad_even (x : Grid M) : rad (-x) = rad x := by simp [rad,min_comm] +lemma diam_even (x : Grid M) : diam (-x) = diam x := by rw [diam,diam,rad_even] + +lemma rad_nonneg (x : Grid M) : 0 ≤ rad x := by rw [rad_abs]; positivity +lemma diam_nonneg (x : Grid M) : 0 ≤ diam x := mul_nonneg (side_pos _).le (rad_nonneg _) + +lemma pos_nonneg (x : Grid M) : 0 ≤ pos x := mul_nonneg (by positivity) (side_pos _).le +lemma pos_lt (x : Grid M) : pos x < 1 := (atom_sub x (pos_mem x)).2 +lemma pos_neg {x : Grid M} (hx : x≠0) : pos (-x) = 1-pos x := by + have : NeZero x := ⟨hx⟩ + simp_rw [pos, ZMod.val_neg_of_ne_zero, Nat.cast_sub (ZMod.val_lt _).le, sub_mul,side_pow_two] + push_cast + rw [mul_inv_cancel₀ (by positivity)] + +lemma diam_min (x : Grid M) : diam x = min (pos x) (pos (-x)) := by + unfold diam rad pos + rw [Nat.cast_min, mul_comm, min_mul_of_nonneg _ _ (side_pos _).le] + +lemma diam_le_half (x : Grid M) : diam x ≤ 1/2 := by + rw [diam_min] + by_cases hx : x=0 + · subst x; simp [pos] + · rw [pos_neg hx, min_le_iff] + by_cases H : pos x ≤ 1/2 + · exact Or.inl H + · exact Or.inr (by linarith) + +lemma rad_le_val (x : Grid M) : rad x ≤ ↑x.val := by simp [rad] +lemma rad_le_nat (k : ℕ) : rad (k:Grid M) ≤ ↑k := by + refine (rad_le_val _).trans ?_ + rw [ZMod.val_natCast]; exact_mod_cast (Nat.mod_le k (2^M)) + +lemma rad_le_int (k : ℤ) : rad (k:Grid M) ≤ |(k:ℝ)| := by + cases k with + | ofNat k => simpa using rad_le_nat (M:=M) k + | negSucc k => + rw [show ((Int.negSucc k:ℤ):Grid M) = -((k+1:ℕ):Grid M) by simp, rad_even] + convert rad_le_nat (M:=M) (k+1) using 1 + push_cast + rw [abs_of_nonpos (by have H := Nat.cast_nonneg (α:=ℝ) k; linarith)]; ring + +lemma rad_add (x y : Grid M) : rad (x+y) ≤ rad x+rad y := by + have H := rad_le_int (M:=M) (liftInt x + liftInt y) + rw [Int.cast_add,liftInt_cast,liftInt_cast,Int.cast_add] at H + rw [rad_abs x,rad_abs y] + exact H.trans (abs_add_le _ _) +lemma diam_add (x y : Grid M) : diam (x+y) ≤ diam x+diam y := by + simp_rw [diam]; rw [← mul_add] + exact mul_le_mul_of_nonneg_left (rad_add _ _) (side_pos _).le + +lemma pos_sub_val {x y : Grid M} (h : y.val ≤ x.val) : pos (x-y) = pos x-pos y := by + simp [pos,ZMod.val_sub h,Nat.cast_sub h,sub_mul] + +lemma diam_le_diff (x y : Grid M) : diam (x-y) ≤ |pos x-pos y| := by + rcases le_total y.val x.val with h|h + · exact le_trans (show diam (x-y) ≤ pos (x-y) by rw [diam_min]; exact min_le_left _ _) + (by rw [pos_sub_val h]; exact le_abs_self _) + rw [diam_min,neg_sub] + exact (min_le_right _ _).trans (by rw [pos_sub_val h,abs_sub_comm]; exact le_abs_self _) + +lemma phase_distance (s x y : Grid M) : + ‖chi s x-chi s y‖ ≤ rad s * (8 * |pos x-pos y|) := by + calc + _ = ‖phase (liftInt s :ℝ) (pos x) - phase (liftInt s:ℝ) (pos y)‖ := by + rw [← chi_cast,← chi_cast,liftInt_cast] + _ ≤ _ := (norm_phase_sub _ _ _).trans (by rw [rad_abs s]; gcongr; linarith [pi_le_four]) + + +def first (M m : ℕ) : Finset (Grid M) := + Finset.univ.filter fun x => x.val < m +lemma mem_first (m : ℕ) (x : Grid M) : x ∈ first M m ↔ x.val < m := by simp [first] + +lemma first_eq_range (m : ℕ) (h : m ≤ 2^M) : + first M m = (Finset.range m).image (fun k : ℕ => (k:Grid M)) := by + ext x + rw [mem_first,Finset.mem_image] + constructor + · intro hi; exact ⟨x.val,Finset.mem_range.mpr hi, ZMod.natCast_zmod_val _⟩ + · rintro ⟨k,hk,rfl⟩; rw [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hk).trans_le h)] + exact Finset.mem_range.mp hk + +lemma range_inj {m : ℕ} (h : m ≤ 2^M) : + Set.InjOn (fun k : ℕ => (k:Grid M)) (Finset.range m) := by + intro i hi j hj HH + have he := congrArg ZMod.val HH + rwa [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hi).trans_le h), + ZMod.val_natCast_of_lt ((Finset.mem_range.mp hj).trans_le h)] at he + +lemma first_card (m : ℕ) (h : m ≤ 2^M) : (first M m).card = m := by + rw [first_eq_range m h,Finset.card_image_of_injOn (range_inj h), Finset.card_range] + +lemma first_card_le (m : ℕ) : (first M m).card ≤ m := by + rcases le_total m (2^M) with h | h + · exact (first_card _ h).le + · refine le_trans (Finset.card_le_univ _) ?_ + simpa [Grid] using h + +lemma sum_first {E : Type*} [AddCommMonoid E] (m : ℕ) (h : m ≤ 2^M) (f : ℕ → E) : + ∑ x ∈ first M m, f x.val = ∑ k ∈ Finset.range m, f k := by + rw [first_eq_range _ h, Finset.sum_image] + · exact Finset.sum_congr rfl fun i hi => by + rw [ZMod.val_natCast_of_lt ((Finset.mem_range.mp hi).trans_le h)] + intro i hi j hj h' + exact range_inj h hi hj h' + +lemma first_max : first M (2^M) = Finset.univ := by + ext x; simp [first,ZMod.val_lt] + +lemma sum_grid {E : Type*} [AddCommMonoid E] (f : ℕ → E) : + ∑ x : Grid M, f x.val = ∑ k ∈ Finset.range (2^M), f k := by + rw [← sum_first _ le_rfl, first_max] + +def arc (M : ℕ) (r : ℝ) : Finset (Grid M) := Finset.univ.filter fun x => rad x ≤ r +lemma mem_arc (r : ℝ) (x : Grid M) : x ∈ arc M r ↔ rad x ≤ r := by simp [arc] + +lemma arc_card_hi (M : ℕ) (r : ℝ) (hr : 0 ≤ r) : + ((arc M r).card :ℝ) ≤ 2*(r+1) := by + let m := Nat.floor r +1 + have hin : + arc M r ⊆ first M m ∪ (first M m).image Neg.neg := by + intro x hx + rw [mem_arc, rad, Nat.cast_min,min_le_iff] at hx + rw [Finset.mem_union,Finset.mem_image,mem_first] + rcases hx with H | H + · exact Or.inl (Nat.lt_succ_of_le ((Nat.le_floor_iff hr).mpr H)) + exact Or.inr ⟨-x, (mem_first _ _).mpr (Nat.lt_succ_of_le ((Nat.le_floor_iff hr).mpr H)),neg_neg _⟩ + have h1 := (Finset.card_le_card hin).trans (Finset.card_union_le _ _) + have h2 := Finset.card_image_le (s:=first M m) (f:=Neg.neg) + have he : (arc M r).card ≤ 2*m := by have := first_card_le (M:=M) m; omega + calc + _ ≤ ((2*m:ℕ):ℝ) := Nat.cast_le.mpr he + _ ≤ _ := by unfold m; push_cast; have H := Nat.floor_le hr; linarith + +lemma arc_card_lo (M : ℕ) (r : ℝ) (_hr : 0 ≤ r) (h : r ≤ 2^M) : + r ≤ ((arc M r).card:ℝ) := by + let m := Nat.ceil r + have hm : m ≤ 2^M := Nat.ceil_le.mpr (by exact_mod_cast h) + have hh : first M m ⊆ arc M r := by + intro x hx + rw [mem_first] at hx; rw [mem_arc] + exact (rad_le_val x).trans ((Nat.lt_ceil.mp hx).le) + calc + _ ≤ (m:ℝ) := Nat.le_ceil _ + _ ≤ _ := Nat.cast_le.mpr (by rw [← first_card m hm]; exact Finset.card_le_card hh) + +lemma side_card (M : ℕ) : side M * (2:ℝ)^M = 1 := by rw [side_pow_two]; field_simp + +lemma arc_length (M : ℕ) (r : ℝ) (hr : 0 ≤ r) : + (uniform (Grid M)).prob (fun x => diam x ≤ r) ≤ 2*(r+side M) := by + rw [prob_eq] + have Heq : + (∑ x : Grid M, if diam x ≤ r then uniform (Grid M) x else 0) = + (arc M (r / side M)).card* side M := by + have he : arc M (r / side M) = Finset.univ.filter fun x : Grid M => diam x ≤ r := by + ext x + simp [diam,arc,le_div_iff₀ (side_pos M),mul_comm] + rw [← Finset.sum_filter,← he] + simp [uniform_side] + rw [Heq] + calc + _ ≤ (2*(r / side M +1))*side M := by + apply mul_le_mul_of_nonneg_right (arc_card_hi M _ (div_nonneg hr (side_pos _).le)) (side_pos _).le + _ = _ := by field_simp [(side_pos _).ne'] + +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Discrete.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Discrete.lean index 5c98dc2871896def6a0ded5f54cc32d372576a73..28f944723d0e47190fbd0e0a0ef5f36f3a95e076 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Discrete.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Discrete.lean @@ -1,214 +1,214 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Grid -import OAI.Analysis.FourierLLogL.Finite.Complex - -/-! # Cyclic grid, characters and averaged Fourier coefficients -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate - -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic -open Period Law - -variable {M : ℕ} - -lemma card_side (M : ℕ) : side M = (Fintype.card (Grid M):ℝ)⁻¹ := by - unfold Grid - rw [ZMod.card] - simp [side] - -lemma uniform_side (x : Grid M) : uniform (Grid M) x = side M := - (uniform_apply x).trans (card_side M).symm - -lemma side_pow_two (M : ℕ) : side M = ((2:ℝ)^M)⁻¹ := by simp [side] - -lemma expU_ex (f : Grid M → ℝ) : expU f = (uniform _).Ex f := by - unfold expU Ex - simp_rw [uniform_side,← Finset.mul_sum]; rfl - -lemma expU_cx (f : Grid M → ℂ) : expU f = (uniform _).Cx f := by - unfold expU Cx - simp_rw [uniform_side,← Finset.mul_sum] - rw [Complex.real_smul] - -lemma expU_prod {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] - (f : Grid M → Grid M → E) : - expU (fun y => expU (f y)) = expU fun x => expU fun y => f y x := by - unfold expU - simp_rw [← Finset.smul_sum] - rw [Finset.sum_comm] - -lemma expU_sum {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] - {ι : Type*} (s : Finset ι) (f : ι → Grid M → E) : - expU (fun y => ∑ i ∈ s, f i y) = ∑ i ∈ s, expU (f i) := by - unfold expU; rw [Finset.sum_comm, Finset.smul_sum] - -lemma expU_add {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] - (f g : Grid M → E) : - expU (fun x => f x+g x) = expU f+expU g := by - unfold expU; rw [Finset.sum_add_distrib, smul_add] - -lemma expU_translate {E : Type*} [AddCommMonoid E] [SMul ℝ E] - (f : Grid M → E) (a : Grid M) : - expU (fun x => f (a+x)) = expU f := by - unfold expU - congr 1 - exact Fintype.sum_equiv (Equiv.addLeft a) _ _ (fun _ => rfl) - -lemma expU_reflect {E : Type*} [AddCommMonoid E] [SMul ℝ E] - (f : Grid M → E) : - expU (fun x => f (-x)) = expU f := by - unfold expU; congr 1 - exact Fintype.sum_equiv (Equiv.neg _) _ _ (fun _ => rfl) - -lemma expU_lmul (a : ℂ) (f : Grid M → ℂ) : - expU (fun x => a*f x) = a*expU f := by rw [expU_cx, expU_cx, Cx_lmul] - -lemma expU_real (f : Grid M → ℝ) : - expU (fun x => (f x:ℂ)) = (expU f:ℝ) := by rw [expU_cx, expU_ex, Cx_ofReal] - -/-- Wrap grid characters as an AddChar. -/ -def phaseChar (u : Grid M) : AddChar (Grid M) ℂ := ZMod.stdAddChar.mulShift u - -@[simp] lemma phaseChar_apply (u x : Grid M) : phaseChar u x = chi u x := rfl - -lemma chi_comm (x y : Grid M) : chi x y = chi y x := by - unfold chi; rw [mul_comm] - -lemma chi_zero_right (u : Grid M) : chi u 0 = 1 := (phaseChar u).map_zero_eq_one -lemma chi_zero_left (u : Grid M) : chi 0 u = 1 := by rw [chi_comm,chi_zero_right] - -lemma chi_add (u x y : Grid M) : chi u (x+y) = chi u x * chi u y := - (phaseChar u).map_add_eq_mul _ _ - -lemma chi_add' (x y u : Grid M) : chi (x+y) u = chi x u * chi y u := by - rw [chi_comm _ u,chi_add,chi_comm u x,chi_comm u y] - -lemma chi_neg (u x : Grid M) : chi u (-x) = conj (chi u x) := by - have H1 : chi u x * chi u (-x) = 1 := by rw [← chi_add, add_neg_cancel, chi_zero_right] - have H2 := Complex.mul_conj (chi u x) - rw [← Complex.sq_norm,norm_chi] at H2 - simp only [one_pow,Complex.ofReal_one] at H2 - exact mul_left_cancel₀ (by rintro H; rw [H] at H1; simp at H1) (H1.trans H2.symm) - -lemma chi_neg' (x u : Grid M) : chi (-x) u = conj (chi x u) := by rw [chi_comm,chi_neg,chi_comm u x] -lemma chi_sub (u x y : Grid M) : chi u (x-y) = chi u x * conj (chi u y) := by - rw [sub_eq_add_neg,chi_add,chi_neg] - -lemma chi_nsmul (u x : Grid M) (n : ℕ) : chi (n • u) x = (chi u x)^n := by - induction n with - | zero => simp [chi_zero_left] - | succ n ih => - rw [add_smul,one_smul,chi_add',ih,pow_succ] - -lemma expU_chi (u : Grid M) : - expU (chi u) = if u=0 then 1 else 0 := by - by_cases H : u=0 - · subst u - rw [expU_cx, funext chi_zero_left] - norm_num [Cx,← Complex.ofReal_sum, uniform_apply, ZMod.card,Grid] - · rw [ite_eq_right H] - unfold expU chi - rw [show ∑ x : Grid M, ZMod.stdAddChar (u*x) = 0 from - AddChar.sum_eq_zero_of_ne_one (ZMod.isPrimitive_stdAddChar _ H), smul_zero] - -/-- Averages against conj character. Evaluation form instead of linear notation. -/ -def fc (f : Grid M → ℂ) (u : Grid M) : ℂ := expU fun x => f x * chi (-u) x - -lemma fc_lmul (f : Grid M → ℂ) (a : ℂ) (u : Grid M) : - fc (fun x => a*f x) u = a*fc f u := by - simp_rw [fc,mul_assoc] - exact expU_lmul _ _ - -lemma fc_add (f g : Grid M → ℂ) (u : Grid M) : - fc (fun x => f x+g x) u = fc f u+fc g u := by - simp_rw [fc,add_mul] - exact expU_add _ _ - -lemma fc_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → ℂ) (u : Grid M) : - fc (fun x => ∑ i ∈ s, f i x) u = ∑ i ∈ s, fc (f i) u := by - simp_rw [fc,Finset.sum_mul] - exact expU_sum _ _ - -lemma fc_chi (u v : Grid M) : fc (chi v) u = if v=u then 1 else 0 := by - simp_rw [fc,← chi_add', ← sub_eq_add_neg, expU_chi, sub_eq_zero] - -lemma expU_coeff (f : Grid M → ℂ) (u : Grid M) : - expU (fun x => f x*chi u x) = fc f (-u) := by rw [fc]; simp - -lemma fc_neg_even (f : Grid M → ℂ) (h : f.Even) (u : Grid M) : - fc f (-u) = fc f u := by - unfold fc - rw [← expU_reflect (fun x => f x*chi (-u) x)] - simp_rw [neg_neg, h _, chi_neg',chi_neg, RingHomCompTriple.comp_apply] - rfl - -lemma fc_neg_real (f : Grid M → ℝ) (u : Grid M) : - fc (fun x => (f x:ℂ)) (-u) = conj (fc (fun x => (f x:ℂ)) u) := by - unfold fc - simp_rw [expU_cx, ← Cx_conj, map_mul, chi_neg', Complex.conj_ofReal] - -lemma real_fc_even (f : Grid M → ℝ) (h : f.Even) (u : Grid M) : - (↑(fc (fun x => (f x:ℂ)) u).re:ℂ) = fc (fun x => (f x:ℂ)) u := by - rw [← fc_neg_even (fun x => (f x:ℂ)) (fun x => by simp [h x]) u] - have H := fc_neg_even (fun x => (f x:ℂ)) (fun x => by simp [h x]) u - have he := fc_neg_real f u - rw [H] - have hi := congrArg Complex.im (H.symm.trans he) - simp only [Complex.conj_im] at hi - apply Complex.ext - · simp - · simp; linarith - -lemma invert_fc (f : Grid M → ℂ) (x : Grid M) : - ∑ u : Grid M, fc f u * chi u x = f x := by - unfold fc expU - simp only [Complex.real_smul, Finset.mul_sum,Finset.sum_mul] - rw [Finset.sum_comm] - have heq (y : Grid M) : - ∑ u : Grid M, ↑(side M)*(f y*chi (-u) y)*chi u x = - f y * expU fun u : Grid M => chi (x-y) u := by - unfold expU; simp only [Complex.real_smul, Finset.mul_sum] - apply Finset.sum_congr rfl; intro u hu - rw [chi_comm (x-y) u,chi_sub,chi_neg'] - ring - simp_rw [heq,expU_chi,sub_eq_zero,mul_ite,mul_one,mul_zero] - simp - -lemma fc_mul_chi (f : Grid M → ℂ) (u v : Grid M) : - fc (fun y => f y * chi v y) u = fc f (u-v) := by - simp_rw [fc, mul_assoc, ← chi_add', neg_sub, ← sub_eq_add_neg] - -lemma fc_prod (f g : Grid M → ℂ) (u : Grid M) : - fc (fun y => f y * g y) u = ∑ v : Grid M, fc f v * fc g (u-v) := by - calc - _ = fc (fun y => ∑ v : Grid M, fc f v * (g y*chi v y)) u := by - congr 1; funext y; rw [← invert_fc f y,Finset.sum_mul] - exact Finset.sum_congr rfl fun _ _ => by ring - _ = _ := by simp_rw [fc_sum,fc_lmul,fc_mul_chi] - -lemma fc_right_translate (f : Grid M → ℂ) (a u : Grid M) : - fc (fun y => f (y-a)) u = chi (-u) a * fc f u := by - unfold fc; rw [← expU_translate (fun y => f (y-a)*chi (-u) y) a] - simp_rw [add_sub_cancel_left,chi_add] - rw [← expU_lmul] - congr 1; funext y; ring - -lemma fc_parseval (f : Grid M → ℂ) : - ∑ u, ‖fc f u‖^2 = expU fun y => ‖f y‖^2 := by - have hc (u : Grid M) : - fc (fun y => conj (f y)) (-u) = conj (fc f u) := by - unfold fc - simp_rw [expU_cx,← Cx_conj,map_mul, chi_neg'] - have H := fc_prod f (fun y => conj (f y)) 0 - simp_rw [zero_sub,hc,Complex.mul_conj,← Complex.sq_norm] at H - unfold fc at H - rw [neg_zero] at H - simp_rw [chi_zero_left,mul_one,expU_real,← Complex.ofReal_sum] at H - exact Complex.ofReal_inj.mp H.symm - -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Grid +import OAI.Analysis.FourierLLogL.Finite.Complex + +/-! # Cyclic grid, characters and averaged Fourier coefficients -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate + +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic +open Period Law + +variable {M : ℕ} + +lemma card_side (M : ℕ) : side M = (Fintype.card (Grid M):ℝ)⁻¹ := by + unfold Grid + rw [ZMod.card] + simp [side] + +lemma uniform_side (x : Grid M) : uniform (Grid M) x = side M := + (uniform_apply x).trans (card_side M).symm + +lemma side_pow_two (M : ℕ) : side M = ((2:ℝ)^M)⁻¹ := by simp [side] + +lemma expU_ex (f : Grid M → ℝ) : expU f = (uniform _).Ex f := by + unfold expU Ex + simp_rw [uniform_side,← Finset.mul_sum]; rfl + +lemma expU_cx (f : Grid M → ℂ) : expU f = (uniform _).Cx f := by + unfold expU Cx + simp_rw [uniform_side,← Finset.mul_sum] + rw [Complex.real_smul] + +lemma expU_prod {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] + (f : Grid M → Grid M → E) : + expU (fun y => expU (f y)) = expU fun x => expU fun y => f y x := by + unfold expU + simp_rw [← Finset.smul_sum] + rw [Finset.sum_comm] + +lemma expU_sum {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] + {ι : Type*} (s : Finset ι) (f : ι → Grid M → E) : + expU (fun y => ∑ i ∈ s, f i y) = ∑ i ∈ s, expU (f i) := by + unfold expU; rw [Finset.sum_comm, Finset.smul_sum] + +lemma expU_add {E : Type*} [AddCommMonoid E] [DistribMulAction ℝ E] + (f g : Grid M → E) : + expU (fun x => f x+g x) = expU f+expU g := by + unfold expU; rw [Finset.sum_add_distrib, smul_add] + +lemma expU_translate {E : Type*} [AddCommMonoid E] [SMul ℝ E] + (f : Grid M → E) (a : Grid M) : + expU (fun x => f (a+x)) = expU f := by + unfold expU + congr 1 + exact Fintype.sum_equiv (Equiv.addLeft a) _ _ (fun _ => rfl) + +lemma expU_reflect {E : Type*} [AddCommMonoid E] [SMul ℝ E] + (f : Grid M → E) : + expU (fun x => f (-x)) = expU f := by + unfold expU; congr 1 + exact Fintype.sum_equiv (Equiv.neg _) _ _ (fun _ => rfl) + +lemma expU_lmul (a : ℂ) (f : Grid M → ℂ) : + expU (fun x => a*f x) = a*expU f := by rw [expU_cx, expU_cx, Cx_lmul] + +lemma expU_real (f : Grid M → ℝ) : + expU (fun x => (f x:ℂ)) = (expU f:ℝ) := by rw [expU_cx, expU_ex, Cx_ofReal] + +/-- Wrap grid characters as an AddChar. -/ +def phaseChar (u : Grid M) : AddChar (Grid M) ℂ := ZMod.stdAddChar.mulShift u + +@[simp] lemma phaseChar_apply (u x : Grid M) : phaseChar u x = chi u x := rfl + +lemma chi_comm (x y : Grid M) : chi x y = chi y x := by + unfold chi; rw [mul_comm] + +lemma chi_zero_right (u : Grid M) : chi u 0 = 1 := (phaseChar u).map_zero_eq_one +lemma chi_zero_left (u : Grid M) : chi 0 u = 1 := by rw [chi_comm,chi_zero_right] + +lemma chi_add (u x y : Grid M) : chi u (x+y) = chi u x * chi u y := + (phaseChar u).map_add_eq_mul _ _ + +lemma chi_add' (x y u : Grid M) : chi (x+y) u = chi x u * chi y u := by + rw [chi_comm _ u,chi_add,chi_comm u x,chi_comm u y] + +lemma chi_neg (u x : Grid M) : chi u (-x) = conj (chi u x) := by + have H1 : chi u x * chi u (-x) = 1 := by rw [← chi_add, add_neg_cancel, chi_zero_right] + have H2 := Complex.mul_conj (chi u x) + rw [← Complex.sq_norm,norm_chi] at H2 + simp only [one_pow,Complex.ofReal_one] at H2 + exact mul_left_cancel₀ (by rintro H; rw [H] at H1; simp at H1) (H1.trans H2.symm) + +lemma chi_neg' (x u : Grid M) : chi (-x) u = conj (chi x u) := by rw [chi_comm,chi_neg,chi_comm u x] +lemma chi_sub (u x y : Grid M) : chi u (x-y) = chi u x * conj (chi u y) := by + rw [sub_eq_add_neg,chi_add,chi_neg] + +lemma chi_nsmul (u x : Grid M) (n : ℕ) : chi (n • u) x = (chi u x)^n := by + induction n with + | zero => simp [chi_zero_left] + | succ n ih => + rw [add_smul,one_smul,chi_add',ih,pow_succ] + +lemma expU_chi (u : Grid M) : + expU (chi u) = if u=0 then 1 else 0 := by + by_cases H : u=0 + · subst u + rw [expU_cx, funext chi_zero_left] + norm_num [Cx,← Complex.ofReal_sum, uniform_apply, ZMod.card,Grid] + · rw [ite_eq_right H] + unfold expU chi + rw [show ∑ x : Grid M, ZMod.stdAddChar (u*x) = 0 from + AddChar.sum_eq_zero_of_ne_one (ZMod.isPrimitive_stdAddChar _ H), smul_zero] + +/-- Averages against conj character. Evaluation form instead of linear notation. -/ +def fc (f : Grid M → ℂ) (u : Grid M) : ℂ := expU fun x => f x * chi (-u) x + +lemma fc_lmul (f : Grid M → ℂ) (a : ℂ) (u : Grid M) : + fc (fun x => a*f x) u = a*fc f u := by + simp_rw [fc,mul_assoc] + exact expU_lmul _ _ + +lemma fc_add (f g : Grid M → ℂ) (u : Grid M) : + fc (fun x => f x+g x) u = fc f u+fc g u := by + simp_rw [fc,add_mul] + exact expU_add _ _ + +lemma fc_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → ℂ) (u : Grid M) : + fc (fun x => ∑ i ∈ s, f i x) u = ∑ i ∈ s, fc (f i) u := by + simp_rw [fc,Finset.sum_mul] + exact expU_sum _ _ + +lemma fc_chi (u v : Grid M) : fc (chi v) u = if v=u then 1 else 0 := by + simp_rw [fc,← chi_add', ← sub_eq_add_neg, expU_chi, sub_eq_zero] + +lemma expU_coeff (f : Grid M → ℂ) (u : Grid M) : + expU (fun x => f x*chi u x) = fc f (-u) := by rw [fc]; simp + +lemma fc_neg_even (f : Grid M → ℂ) (h : f.Even) (u : Grid M) : + fc f (-u) = fc f u := by + unfold fc + rw [← expU_reflect (fun x => f x*chi (-u) x)] + simp_rw [neg_neg, h _, chi_neg',chi_neg, RingHomCompTriple.comp_apply] + rfl + +lemma fc_neg_real (f : Grid M → ℝ) (u : Grid M) : + fc (fun x => (f x:ℂ)) (-u) = conj (fc (fun x => (f x:ℂ)) u) := by + unfold fc + simp_rw [expU_cx, ← Cx_conj, map_mul, chi_neg', Complex.conj_ofReal] + +lemma real_fc_even (f : Grid M → ℝ) (h : f.Even) (u : Grid M) : + (↑(fc (fun x => (f x:ℂ)) u).re:ℂ) = fc (fun x => (f x:ℂ)) u := by + rw [← fc_neg_even (fun x => (f x:ℂ)) (fun x => by simp [h x]) u] + have H := fc_neg_even (fun x => (f x:ℂ)) (fun x => by simp [h x]) u + have he := fc_neg_real f u + rw [H] + have hi := congrArg Complex.im (H.symm.trans he) + simp only [Complex.conj_im] at hi + apply Complex.ext + · simp + · simp; linarith + +lemma invert_fc (f : Grid M → ℂ) (x : Grid M) : + ∑ u : Grid M, fc f u * chi u x = f x := by + unfold fc expU + simp only [Complex.real_smul, Finset.mul_sum,Finset.sum_mul] + rw [Finset.sum_comm] + have heq (y : Grid M) : + ∑ u : Grid M, ↑(side M)*(f y*chi (-u) y)*chi u x = + f y * expU fun u : Grid M => chi (x-y) u := by + unfold expU; simp only [Complex.real_smul, Finset.mul_sum] + apply Finset.sum_congr rfl; intro u hu + rw [chi_comm (x-y) u,chi_sub,chi_neg'] + ring + simp_rw [heq,expU_chi,sub_eq_zero,mul_ite,mul_one,mul_zero] + simp + +lemma fc_mul_chi (f : Grid M → ℂ) (u v : Grid M) : + fc (fun y => f y * chi v y) u = fc f (u-v) := by + simp_rw [fc, mul_assoc, ← chi_add', neg_sub, ← sub_eq_add_neg] + +lemma fc_prod (f g : Grid M → ℂ) (u : Grid M) : + fc (fun y => f y * g y) u = ∑ v : Grid M, fc f v * fc g (u-v) := by + calc + _ = fc (fun y => ∑ v : Grid M, fc f v * (g y*chi v y)) u := by + congr 1; funext y; rw [← invert_fc f y,Finset.sum_mul] + exact Finset.sum_congr rfl fun _ _ => by ring + _ = _ := by simp_rw [fc_sum,fc_lmul,fc_mul_chi] + +lemma fc_right_translate (f : Grid M → ℂ) (a u : Grid M) : + fc (fun y => f (y-a)) u = chi (-u) a * fc f u := by + unfold fc; rw [← expU_translate (fun y => f (y-a)*chi (-u) y) a] + simp_rw [add_sub_cancel_left,chi_add] + rw [← expU_lmul] + congr 1; funext y; ring + +lemma fc_parseval (f : Grid M → ℂ) : + ∑ u, ‖fc f u‖^2 = expU fun y => ‖f y‖^2 := by + have hc (u : Grid M) : + fc (fun y => conj (f y)) (-u) = conj (fc f u) := by + unfold fc + simp_rw [expU_cx,← Cx_conj,map_mul, chi_neg'] + have H := fc_prod f (fun y => conj (f y)) 0 + simp_rw [zero_sub,hc,Complex.mul_conj,← Complex.sq_norm] at H + unfold fc at H + rw [neg_zero] at H + simp_rw [chi_zero_left,mul_one,expU_real,← Complex.ofReal_sum] at H + exact Complex.ofReal_inj.mp H.symm + +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Grid.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Grid.lean index 4a27ef12a462becdca879b449febc3716bb9bb21..f86648ab570240acedbd896e5d24a2d28388fdef 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Grid.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Grid.lean @@ -1,259 +1,259 @@ -import OAI.Analysis.FourierLLogL.Fourier.Affine -/-! # The finite spatial model -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Dyadic -open Period -noncomputable section - -abbrev Grid (M : ℕ) := ZMod (2^M) - -def pos {M : ℕ} (x : Grid M) : ℝ := x.val * side M - -def chi {M : ℕ} (n x : Grid M) : ℂ := ZMod.stdAddChar (n*x) - -lemma chi_cast {M : ℕ} (n : ℤ) (x : Grid M) : chi (n:Grid M) x = phase n (pos x) := by - unfold chi - have heq : (n:Grid M)*x = ((n*(x.val:ℤ):ℤ):Grid M) := by - rw [Int.cast_mul, Int.cast_natCast, ZMod.natCast_zmod_val] - rw [heq, ZMod.stdAddChar_coe, phase, pos, side] - congr 1 - push_cast - simp only [div_eq_mul_inv, ← inv_pow]; ring - -lemma norm_chi {M : ℕ} (n x : Grid M) : ‖chi n x‖ = 1 := by - unfold chi - simp [ZMod.stdAddChar_apply] - -/-- Uniform probability average. -/ -def expU {E : Type*} [SMul ℝ E] [AddCommMonoid E] {M : ℕ} (f : Grid M → E) := - side M • ∑ y, f y - -@[ext] structure Node where - lev : ℕ - ix : ℕ - deriving DecidableEq - -namespace Node - -def root : Node := ⟨0,0⟩ - -def valid (p : Node) : Prop := p.ix < 2^p.lev - -def len (p : Node) := side p.lev - -def a (p : Node) : ℝ := p.ix * p.len - -def cells (p : Node) := box p.a p.len - -theorem len_pos (p : Node) : 0 < p.len := side_pos _ - -lemma cells_sub {p : Node} (hp : p.valid) : p.cells ⊆ unitInt := by - simpa [lft, unitInt, cells, a, len, box] using cell_in_top 0 zero_lt_one hp - -lemma cells_dis {n k l : ℕ} (h : k ≠ l) : - Disjoint (cells ⟨n,k⟩) (cells ⟨n,l⟩) := by - rw [Set.disjoint_left] - intro x hx hy - apply h - apply cell_unique 0 1 zero_lt_one n (x:=x) - all_goals simpa only [lft,one_mul,zero_add] - - -def L (p : Node) : Node := ⟨p.lev+1,p.ix*2⟩ -def R (p : Node) : Node := ⟨p.lev+1,p.ix*2+1⟩ - -lemma len_L (p : Node) : p.L.len = p.len/2 := side_succ _ -lemma len_R (p : Node) : p.R.len = p.len/2 := side_succ _ - -lemma a_L (p : Node) : p.L.a = p.a := by - unfold a - rw [len_L] - dsimp only [L] - push_cast; ring - -lemma a_R (p : Node) : p.R.a = p.a+p.len/2 := by - unfold a - rw [len_R] - dsimp only [R] - push_cast; ring - -lemma cells_L (p : Node) : p.L.cells = Ico p.a (p.a+p.len/2) := by rw [cells, len_L, a_L, box] -lemma cells_R (p : Node) : p.R.cells = Ico (p.a+p.len/2) (p.a+p.len) := by - rw [cells,len_R,a_R,box] - ring_nf - -lemma cells_split (p : Node) : p.cells = p.L.cells ∪ p.R.cells := by - rw [cells_L,cells_R,Ico_union_Ico_eq_Ico (by linarith [p.len_pos]) (by linarith [p.len_pos])] - rfl - -lemma valid_L (p : Node) : p.L.valid ↔ p.valid := by - unfold L valid; simp; omega -lemma valid_R (p : Node) : p.R.valid ↔ p.valid := by - unfold R valid; simp; omega - -def block (p : Node) (M : ℕ) := (pos : Grid M → ℝ) ⁻¹' p.cells - -def avg (p : Node) {M : ℕ} {E : Type*} [AddCommMonoid E] [SMul ℝ E] (f : Grid M → E) := - p.len⁻¹ • expU ((p.block M).indicator f) - -def normarg (p : Node) (x : ℝ) : ℝ := (x-p.a) / p.len - -def Hi (p : Node) (x : ℝ) : ℝ := haarH (p.normarg x) -def Gi (p : Node) (x : ℝ) : ℝ := haarG (p.normarg x) - -lemma arg_mem (p : Node) (x : ℝ) : p.normarg x ∈ unitInt ↔ x ∈ p.cells := - norm_mem_box p.len_pos - -lemma argL_eq (p : Node) (x : ℝ) : p.L.normarg x = 2*p.normarg x := by - rw [normarg, a_L,len_L,normarg] - ring -lemma argR_eq (p : Node) (x : ℝ) : p.R.normarg x = 2*p.normarg x - 1 := by - rw [normarg, a_R,len_R,normarg] - have H := p.len_pos - field_simp; ring - -lemma Gi_eq (p : Node) (x : ℝ) : p.Gi x = p.L.Hi x - p.R.Hi x := by - rw [Hi,Hi, argL_eq, argR_eq]; rfl - -lemma Hi_L (p : Node) {x : ℝ} (hx : x ∈ p.L.cells) : p.Hi x = 1 := by - rw [← arg_mem, argL_eq] at hx - apply H_of_left - constructor <;> linarith [hx.1,hx.2] - -lemma Hi_R (p : Node) {x : ℝ} (hx : x ∈ p.R.cells) : p.Hi x = -1 := by - rw [← arg_mem, argR_eq] at hx - apply H_of_right - constructor <;> linarith [hx.1,hx.2] - -lemma Hi_out (p : Node) {x : ℝ} (hx : x ∉ p.cells) : p.Hi x = 0 := by - rw [← arg_mem] at hx - exact H_outside hx - -lemma Gi_out (p : Node) {x : ℝ} (hx : x ∉ p.cells) : p.Gi x = 0 := by - rw [← arg_mem] at hx - exact G_outside hx - -end Node - -open Node - -def vertex {M : ℕ} (x : Grid M) : Node := ⟨M,x.val⟩ - -def atom {M : ℕ} (x : Grid M) := (vertex x).cells - -lemma pos_mem {M : ℕ} (x : Grid M) : pos x ∈ atom x := - ⟨le_refl (pos x), lt_add_of_pos_right (pos x) (side_pos M)⟩ - -lemma atom_sub {M : ℕ} (x : Grid M) : atom x ⊆ unitInt := - cells_sub (ZMod.val_lt _) - -lemma atom_disjoint {M : ℕ} : Pairwise (fun x y : Grid M => Disjoint (atom x) (atom y)) := by - intro x y h - apply cells_dis - intro he - have H := congrArg (fun n : ℕ => (n:Grid M)) he - exact h (by simpa using H) - -lemma atom_measurable {M : ℕ} (x : Grid M) : MeasurableSet (atom x) := measurableSet_Ico - -lemma atom_volume {M : ℕ} (x : Grid M) : volume (atom x) = ENNReal.ofReal (side M) := by - change volume (Ico (pos x) (pos x+side M)) = _ - simp - -lemma atom_partition (M : ℕ) : unitInt = ⋃ x : Grid M, atom x := by - apply Subset.antisymm _ (iUnion_subset atom_sub) - intro x hx - obtain ⟨p,hp,hi,hxp⟩ : - ∃ p : Node, p.valid ∧ p.lev = M ∧ x ∈ p.cells := by - induction M with - | zero => - refine ⟨root,by simp [valid,root], rfl, ?_⟩ - simpa [root,cells,box, len,side,a,unitInt] using hx - | succ M ih => - obtain ⟨p,hv,hM,hx⟩ := ih - rw [cells_split] at hx - exact hx.elim (fun h => ⟨p.L,p.valid_L.mpr hv,congrArg Nat.succ hM,h⟩) - (fun h => ⟨p.R,p.valid_R.mpr hv,congrArg Nat.succ hM,h⟩) - refine mem_iUnion.mpr ⟨(p.ix:Grid M),?_⟩ - obtain ⟨n,k⟩ := p - cases hi - change x ∈ cells ⟨_, ((k:Grid _).val)⟩ - rwa [ZMod.val_natCast_of_lt hp] - -/-- Every atom fully inside/out of a coarser dyadic cell. -/ -lemma coarse_const {p q : Node} (h : p.lev ≤ q.lev) {x y : ℝ} - (hx : x ∈ q.cells) (hy : y ∈ q.cells) : x ∈ p.cells ↔ y ∈ p.cells := by - suffices HH : ∀ n : ℕ, ∀ q : Node, q.lev = p.lev+n → - x ∈ q.cells → y ∈ q.cells → (x ∈ p.cells ↔ y ∈ p.cells) by - exact HH (q.lev - p.lev) q (by omega) hx hy - intro n - induction n with - | zero => - rintro ⟨l,k⟩ hl hx hy - obtain ⟨m,r⟩ := p - simp only [Nat.add_zero] at hl - subst l - by_cases H : r = k - · subst r; exact iff_of_true hx hy - · have Hd := Set.disjoint_left.mp (cells_dis (n:=m) H) - exact iff_of_false (fun hh => Hd hh hx) (fun hh => Hd hh hy) - | succ n ih => - rintro ⟨l,k⟩ hl hx hy - let r : Node := ⟨p.lev+n,k/2⟩ - have hs : cells ⟨l,k⟩ ⊆ r.cells := by - have heq : (⟨l,k⟩:Node) = r.L ∨ (⟨l,k⟩:Node) = r.R := by - simp only [r,L,R,Node.mk.injEq] - change l = p.lev+(n+1) at hl - omega - rw [cells_split r] - exact heq.elim (fun e => e ▸ subset_union_left) (fun e => e ▸ subset_union_right) - exact ih r rfl (hs hx) (hs hy) - -lemma atom_iff_coarse {M : ℕ} (p : Node) (h : p.lev ≤ M) {y : Grid M} {x : ℝ} - (hx : x ∈ atom y) : x ∈ p.cells ↔ y ∈ p.block M := - coarse_const (q:=vertex y) h hx (pos_mem y) - -lemma Hi_const {p q : Node} (h : p.lev+1 ≤ q.lev) {x y : ℝ} (hx : x ∈ q.cells) - (hy : y ∈ q.cells) : p.Hi x = p.Hi y := by - by_cases hp : x ∈ p.cells - · rw [cells_split] at hp - exact hp.elim (fun hh => (p.Hi_L hh).trans - (p.Hi_L ((coarse_const (p:=p.L) h hx hy).mp hh)).symm) - (fun hh => (p.Hi_R hh).trans (p.Hi_R ((coarse_const (p:=p.R) h hx hy).mp hh)).symm) - · rw [p.Hi_out hp, p.Hi_out (mt (coarse_const (p:=p) (by omega) hy hx).mp hp)] - -lemma Gi_const {p q : Node} (h : p.lev+2 ≤ q.lev) {x y : ℝ} (hx : x ∈ q.cells) - (hy : y ∈ q.cells) : p.Gi x = p.Gi y := by - rw [Gi_eq,Gi_eq, Hi_const (p:=p.L) h hx hy, Hi_const (p:=p.R) h hx hy] - -def FinJump {M : ℕ} (p : Node) (F : Grid M → ℂ) (x : Grid M) := - p.Gi (pos x) • p.avg (fun y => p.Hi (pos y) • F y) - -def FinShift {M : ℕ} (J : ℕ) (F : Grid M → ℂ) (x : Grid M) := - ∑ n ∈ Finset.range J, ∑ k ∈ Finset.range (2^n), FinJump ⟨n,k⟩ F x - -def finMod {M : ℕ} (ρ : Grid M → ℝ) (u : Grid M) (x : Grid M) := - (ρ x : ℂ)*chi u x - -def finGood {M : ℕ} (D : ℝ) (J : ℕ) (ρ : Grid M → ℝ) (x : Grid M) := - ∀ p : Node, p.valid → p.lev < J → x ∈ p.block M → p.avg ρ ≤ D - -/-- Finite cut statement; asks for an L¹ majorant restricted to points where density steps stay -below threshold. There is no dependence on a frequency or depth cutoff in the constant. -/ -def GridBound : Prop := - ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ J : ℕ, ∀ᶠ M : ℕ in atTop, - ∀ (ρ : Grid M → ℝ), (∀ x, 0 ≤ ρ x) → expU ρ ≤ 2 → - (∀ x, ρ x ≤ Real.exp ((2:ℝ)^j)) → - ∃ W : Grid M → ℝ, (∀ x, 0 ≤ W x) ∧ expU W ≤ C*(2:ℝ)^j ∧ - ∀ x : Grid M, finGood (((j:ℝ)+1)^3) J ρ x → - ∀ u : Grid M, ‖FinShift J (finMod ρ u) x‖ ≤ W x - -end -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Affine +/-! # The finite spatial model -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Dyadic +open Period +noncomputable section + +abbrev Grid (M : ℕ) := ZMod (2^M) + +def pos {M : ℕ} (x : Grid M) : ℝ := x.val * side M + +def chi {M : ℕ} (n x : Grid M) : ℂ := ZMod.stdAddChar (n*x) + +lemma chi_cast {M : ℕ} (n : ℤ) (x : Grid M) : chi (n:Grid M) x = phase n (pos x) := by + unfold chi + have heq : (n:Grid M)*x = ((n*(x.val:ℤ):ℤ):Grid M) := by + rw [Int.cast_mul, Int.cast_natCast, ZMod.natCast_zmod_val] + rw [heq, ZMod.stdAddChar_coe, phase, pos, side] + congr 1 + push_cast + simp only [div_eq_mul_inv, ← inv_pow]; ring + +lemma norm_chi {M : ℕ} (n x : Grid M) : ‖chi n x‖ = 1 := by + unfold chi + simp [ZMod.stdAddChar_apply] + +/-- Uniform probability average. -/ +def expU {E : Type*} [SMul ℝ E] [AddCommMonoid E] {M : ℕ} (f : Grid M → E) := + side M • ∑ y, f y + +@[ext] structure Node where + lev : ℕ + ix : ℕ + deriving DecidableEq + +namespace Node + +def root : Node := ⟨0,0⟩ + +def valid (p : Node) : Prop := p.ix < 2^p.lev + +def len (p : Node) := side p.lev + +def a (p : Node) : ℝ := p.ix * p.len + +def cells (p : Node) := box p.a p.len + +theorem len_pos (p : Node) : 0 < p.len := side_pos _ + +lemma cells_sub {p : Node} (hp : p.valid) : p.cells ⊆ unitInt := by + simpa [lft, unitInt, cells, a, len, box] using cell_in_top 0 zero_lt_one hp + +lemma cells_dis {n k l : ℕ} (h : k ≠ l) : + Disjoint (cells ⟨n,k⟩) (cells ⟨n,l⟩) := by + rw [Set.disjoint_left] + intro x hx hy + apply h + apply cell_unique 0 1 zero_lt_one n (x:=x) + all_goals simpa only [lft,one_mul,zero_add] + + +def L (p : Node) : Node := ⟨p.lev+1,p.ix*2⟩ +def R (p : Node) : Node := ⟨p.lev+1,p.ix*2+1⟩ + +lemma len_L (p : Node) : p.L.len = p.len/2 := side_succ _ +lemma len_R (p : Node) : p.R.len = p.len/2 := side_succ _ + +lemma a_L (p : Node) : p.L.a = p.a := by + unfold a + rw [len_L] + dsimp only [L] + push_cast; ring + +lemma a_R (p : Node) : p.R.a = p.a+p.len/2 := by + unfold a + rw [len_R] + dsimp only [R] + push_cast; ring + +lemma cells_L (p : Node) : p.L.cells = Ico p.a (p.a+p.len/2) := by rw [cells, len_L, a_L, box] +lemma cells_R (p : Node) : p.R.cells = Ico (p.a+p.len/2) (p.a+p.len) := by + rw [cells,len_R,a_R,box] + ring_nf + +lemma cells_split (p : Node) : p.cells = p.L.cells ∪ p.R.cells := by + rw [cells_L,cells_R,Ico_union_Ico_eq_Ico (by linarith [p.len_pos]) (by linarith [p.len_pos])] + rfl + +lemma valid_L (p : Node) : p.L.valid ↔ p.valid := by + unfold L valid; simp; omega +lemma valid_R (p : Node) : p.R.valid ↔ p.valid := by + unfold R valid; simp; omega + +def block (p : Node) (M : ℕ) := (pos : Grid M → ℝ) ⁻¹' p.cells + +def avg (p : Node) {M : ℕ} {E : Type*} [AddCommMonoid E] [SMul ℝ E] (f : Grid M → E) := + p.len⁻¹ • expU ((p.block M).indicator f) + +def normarg (p : Node) (x : ℝ) : ℝ := (x-p.a) / p.len + +def Hi (p : Node) (x : ℝ) : ℝ := haarH (p.normarg x) +def Gi (p : Node) (x : ℝ) : ℝ := haarG (p.normarg x) + +lemma arg_mem (p : Node) (x : ℝ) : p.normarg x ∈ unitInt ↔ x ∈ p.cells := + norm_mem_box p.len_pos + +lemma argL_eq (p : Node) (x : ℝ) : p.L.normarg x = 2*p.normarg x := by + rw [normarg, a_L,len_L,normarg] + ring +lemma argR_eq (p : Node) (x : ℝ) : p.R.normarg x = 2*p.normarg x - 1 := by + rw [normarg, a_R,len_R,normarg] + have H := p.len_pos + field_simp; ring + +lemma Gi_eq (p : Node) (x : ℝ) : p.Gi x = p.L.Hi x - p.R.Hi x := by + rw [Hi,Hi, argL_eq, argR_eq]; rfl + +lemma Hi_L (p : Node) {x : ℝ} (hx : x ∈ p.L.cells) : p.Hi x = 1 := by + rw [← arg_mem, argL_eq] at hx + apply H_of_left + constructor <;> linarith [hx.1,hx.2] + +lemma Hi_R (p : Node) {x : ℝ} (hx : x ∈ p.R.cells) : p.Hi x = -1 := by + rw [← arg_mem, argR_eq] at hx + apply H_of_right + constructor <;> linarith [hx.1,hx.2] + +lemma Hi_out (p : Node) {x : ℝ} (hx : x ∉ p.cells) : p.Hi x = 0 := by + rw [← arg_mem] at hx + exact H_outside hx + +lemma Gi_out (p : Node) {x : ℝ} (hx : x ∉ p.cells) : p.Gi x = 0 := by + rw [← arg_mem] at hx + exact G_outside hx + +end Node + +open Node + +def vertex {M : ℕ} (x : Grid M) : Node := ⟨M,x.val⟩ + +def atom {M : ℕ} (x : Grid M) := (vertex x).cells + +lemma pos_mem {M : ℕ} (x : Grid M) : pos x ∈ atom x := + ⟨le_refl (pos x), lt_add_of_pos_right (pos x) (side_pos M)⟩ + +lemma atom_sub {M : ℕ} (x : Grid M) : atom x ⊆ unitInt := + cells_sub (ZMod.val_lt _) + +lemma atom_disjoint {M : ℕ} : Pairwise (fun x y : Grid M => Disjoint (atom x) (atom y)) := by + intro x y h + apply cells_dis + intro he + have H := congrArg (fun n : ℕ => (n:Grid M)) he + exact h (by simpa using H) + +lemma atom_measurable {M : ℕ} (x : Grid M) : MeasurableSet (atom x) := measurableSet_Ico + +lemma atom_volume {M : ℕ} (x : Grid M) : volume (atom x) = ENNReal.ofReal (side M) := by + change volume (Ico (pos x) (pos x+side M)) = _ + simp + +lemma atom_partition (M : ℕ) : unitInt = ⋃ x : Grid M, atom x := by + apply Subset.antisymm _ (iUnion_subset atom_sub) + intro x hx + obtain ⟨p,hp,hi,hxp⟩ : + ∃ p : Node, p.valid ∧ p.lev = M ∧ x ∈ p.cells := by + induction M with + | zero => + refine ⟨root,by simp [valid,root], rfl, ?_⟩ + simpa [root,cells,box, len,side,a,unitInt] using hx + | succ M ih => + obtain ⟨p,hv,hM,hx⟩ := ih + rw [cells_split] at hx + exact hx.elim (fun h => ⟨p.L,p.valid_L.mpr hv,congrArg Nat.succ hM,h⟩) + (fun h => ⟨p.R,p.valid_R.mpr hv,congrArg Nat.succ hM,h⟩) + refine mem_iUnion.mpr ⟨(p.ix:Grid M),?_⟩ + obtain ⟨n,k⟩ := p + cases hi + change x ∈ cells ⟨_, ((k:Grid _).val)⟩ + rwa [ZMod.val_natCast_of_lt hp] + +/-- Every atom fully inside/out of a coarser dyadic cell. -/ +lemma coarse_const {p q : Node} (h : p.lev ≤ q.lev) {x y : ℝ} + (hx : x ∈ q.cells) (hy : y ∈ q.cells) : x ∈ p.cells ↔ y ∈ p.cells := by + suffices HH : ∀ n : ℕ, ∀ q : Node, q.lev = p.lev+n → + x ∈ q.cells → y ∈ q.cells → (x ∈ p.cells ↔ y ∈ p.cells) by + exact HH (q.lev - p.lev) q (by omega) hx hy + intro n + induction n with + | zero => + rintro ⟨l,k⟩ hl hx hy + obtain ⟨m,r⟩ := p + simp only [Nat.add_zero] at hl + subst l + by_cases H : r = k + · subst r; exact iff_of_true hx hy + · have Hd := Set.disjoint_left.mp (cells_dis (n:=m) H) + exact iff_of_false (fun hh => Hd hh hx) (fun hh => Hd hh hy) + | succ n ih => + rintro ⟨l,k⟩ hl hx hy + let r : Node := ⟨p.lev+n,k/2⟩ + have hs : cells ⟨l,k⟩ ⊆ r.cells := by + have heq : (⟨l,k⟩:Node) = r.L ∨ (⟨l,k⟩:Node) = r.R := by + simp only [r,L,R,Node.mk.injEq] + change l = p.lev+(n+1) at hl + omega + rw [cells_split r] + exact heq.elim (fun e => e ▸ subset_union_left) (fun e => e ▸ subset_union_right) + exact ih r rfl (hs hx) (hs hy) + +lemma atom_iff_coarse {M : ℕ} (p : Node) (h : p.lev ≤ M) {y : Grid M} {x : ℝ} + (hx : x ∈ atom y) : x ∈ p.cells ↔ y ∈ p.block M := + coarse_const (q:=vertex y) h hx (pos_mem y) + +lemma Hi_const {p q : Node} (h : p.lev+1 ≤ q.lev) {x y : ℝ} (hx : x ∈ q.cells) + (hy : y ∈ q.cells) : p.Hi x = p.Hi y := by + by_cases hp : x ∈ p.cells + · rw [cells_split] at hp + exact hp.elim (fun hh => (p.Hi_L hh).trans + (p.Hi_L ((coarse_const (p:=p.L) h hx hy).mp hh)).symm) + (fun hh => (p.Hi_R hh).trans (p.Hi_R ((coarse_const (p:=p.R) h hx hy).mp hh)).symm) + · rw [p.Hi_out hp, p.Hi_out (mt (coarse_const (p:=p) (by omega) hy hx).mp hp)] + +lemma Gi_const {p q : Node} (h : p.lev+2 ≤ q.lev) {x y : ℝ} (hx : x ∈ q.cells) + (hy : y ∈ q.cells) : p.Gi x = p.Gi y := by + rw [Gi_eq,Gi_eq, Hi_const (p:=p.L) h hx hy, Hi_const (p:=p.R) h hx hy] + +def FinJump {M : ℕ} (p : Node) (F : Grid M → ℂ) (x : Grid M) := + p.Gi (pos x) • p.avg (fun y => p.Hi (pos y) • F y) + +def FinShift {M : ℕ} (J : ℕ) (F : Grid M → ℂ) (x : Grid M) := + ∑ n ∈ Finset.range J, ∑ k ∈ Finset.range (2^n), FinJump ⟨n,k⟩ F x + +def finMod {M : ℕ} (ρ : Grid M → ℝ) (u : Grid M) (x : Grid M) := + (ρ x : ℂ)*chi u x + +def finGood {M : ℕ} (D : ℝ) (J : ℕ) (ρ : Grid M → ℝ) (x : Grid M) := + ∀ p : Node, p.valid → p.lev < J → x ∈ p.block M → p.avg ρ ≤ D + +/-- Finite cut statement; asks for an L¹ majorant restricted to points where density steps stay +below threshold. There is no dependence on a frequency or depth cutoff in the constant. -/ +def GridBound : Prop := + ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ J : ℕ, ∀ᶠ M : ℕ in atTop, + ∀ (ρ : Grid M → ℝ), (∀ x, 0 ≤ ρ x) → expU ρ ≤ 2 → + (∀ x, ρ x ≤ Real.exp ((2:ℝ)^j)) → + ∃ W : Grid M → ℝ, (∀ x, 0 ≤ W x) ∧ expU W ≤ C*(2:ℝ)^j ∧ + ∀ x : Grid M, finGood (((j:ℝ)+1)^3) J ρ x → + ∀ u : Grid M, ‖FinShift J (finMod ρ u) x‖ ≤ W x + +end +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Lift.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Lift.lean index 749ac23e78c213b84eec761c490832b2fec5e8b3..519e64d5e3dd46ab000d79123e67b8ce5b26e11e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Lift.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Lift.lean @@ -1,331 +1,331 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Grid -/-! # Reduction to the finite grid -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Dyadic -open Period Node -noncomputable section - -lemma expU_mono {M : ℕ} {f g : Grid M → ℝ} (h : ∀ y, f y ≤ g y) : - expU f ≤ expU g := - mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun y _ => h y)) (side_pos _).le - -lemma avg_mono {M : ℕ} (p : Node) {f g : Grid M → ℝ} (h : ∀ y ∈ p.block M, f y ≤ g y) : - p.avg f ≤ p.avg g := by - apply mul_le_mul_of_nonneg_left (expU_mono ?_) (inv_pos.mpr p.len_pos).le - intro y - by_cases hy : y ∈ p.block M - · simpa [hy] using h y hy - · simp [hy] - -lemma atom_volume_real {M : ℕ} (x : Grid M) : volume.real (atom x) = side M := by - rw [measureReal_def, atom_volume, ENNReal.toReal_ofReal (side_pos M).le] - -section E -variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] - -/-- Cell averages from the real model. -/ -def liftGrid (M : ℕ) (f : ℝ → E) (y : Grid M) : E := - (side M)⁻¹ • ∫ x in atom y, f x - -lemma expU_liftGrid (M : ℕ) (f : ℝ → E) (hf : Integrable f μ₁) : - expU (liftGrid M f) = ∫ x, f x ∂μ₁ := by - have hi : IntegrableOn f (⋃ y : Grid M, atom y) := by rw [← atom_partition]; exact hf - unfold expU liftGrid μ₁ - rw [atom_partition M, integral_iUnion atom_measurable atom_disjoint hi, tsum_fintype] - rw [Finset.smul_sum] - refine Finset.sum_congr rfl fun x _ => ?_ - rw [smul_smul, mul_inv_cancel₀ (side_pos M).ne', one_smul] - -lemma liftGrid_ind {M : ℕ} (p : Node) (h : p.lev ≤ M) (f : ℝ → E) : - liftGrid M (p.cells.indicator f) = (p.block M).indicator (liftGrid M f) := by - ext y - by_cases hy : y ∈ p.block M - · rw [indicator_of_mem hy] - unfold liftGrid - congr 1 - exact setIntegral_congr_fun (atom_measurable y) - (fun x hx => indicator_of_mem ((atom_iff_coarse p h hx).mpr hy) f) - · rw [indicator_of_notMem hy, liftGrid, setIntegral_eq_zero_of_forall_eq_zero] - · exact smul_zero _ - · exact fun x hx => indicator_of_notMem (mt (atom_iff_coarse p h hx).mp hy) f - -lemma avg_liftGrid {M : ℕ} (f : ℝ → E) (hf : Integrable f μ₁) - {p : Node} (hp : p.valid) (h : p.lev ≤ M) : - p.avg (liftGrid M f) = p.len⁻¹ • ∫ x in p.cells, f x := by - rw [avg, ← liftGrid_ind p h, expU_liftGrid, μ₁, setIntegral_indicator - (show MeasurableSet p.cells from measurableSet_Ico), inter_eq_right.mpr (cells_sub hp)] - exact hf.indicator measurableSet_Ico - -lemma avg_smulHi_lift {M : ℕ} (f : ℝ → E) {p : Node} (hp : p.valid) (h : p.lev+1 ≤ M) - (hF : Integrable (fun x => p.Hi x • f x) μ₁) : - p.avg (fun y => p.Hi (pos y) • liftGrid M f y) = - ∫ x, haarH x • f (p.a+p.len*x) ∂μ₁ := by - have heq : (fun y => p.Hi (pos y) • liftGrid M f y) = - liftGrid M (fun x => p.Hi x • f x) := by - ext y - unfold liftGrid - rw [smul_comm, ← integral_smul] - congr 1 - exact setIntegral_congr_fun (atom_measurable _) fun x hx => - congrArg (fun c : ℝ => c • f x) (Hi_const (p:=p) (q:=vertex y) h (pos_mem y) hx) - rw [heq, avg_liftGrid _ hF hp (by omega), - cells, box, ← mean_translate_scale (fun x => p.Hi x • f x) p.a p.len_pos] - congr 1 - funext x - rw [show p.Hi (p.a+p.len*x) = haarH x from congrArg haarH (by - unfold normarg; field_simp [p.len_pos.ne']; ring)] - -lemma norm_expU_le {M : ℕ} (f : Grid M → E) : ‖expU f‖ ≤ expU (fun x => ‖f x‖) := by - unfold expU - rw [norm_smul, Real.norm_eq_abs, abs_of_pos (side_pos _)] - exact mul_le_mul_of_nonneg_left (norm_sum_le _ _) (side_pos _).le - -lemma norm_avg_le {M : ℕ} (p : Node) (f : Grid M → E) : - ‖p.avg f‖ ≤ p.avg (fun x => ‖f x‖) := by - unfold avg - rw [norm_smul, Real.norm_eq_abs, abs_of_pos (inv_pos.mpr p.len_pos)] - refine mul_le_mul_of_nonneg_left ((norm_expU_le _).trans ?_) (inv_pos.mpr p.len_pos).le - apply expU_mono - intro y; by_cases hy : y ∈ block p M <;> simp [hy] - -lemma avg_sub {M : ℕ} (p : Node) (f g : Grid M → E) : - p.avg (fun y => f y - g y) = p.avg f - p.avg g := by - unfold avg expU - simp_rw [indicator_sub, Finset.sum_sub_distrib, smul_sub] - -variable [CompleteSpace E] - -lemma liftGrid_const {M : ℕ} (c : E) (y : Grid M) : - liftGrid M (fun _ => c) y = c := by - rw [liftGrid, setIntegral_const, atom_volume_real, smul_smul, - inv_mul_cancel₀ (side_pos _).ne', one_smul] - - -lemma avg_const {M : ℕ} {p : Node} (hp : p.valid) (h : p.lev ≤ M) (c : E) : - p.avg (fun _ : Grid M => c) = c := by - have heq := funext (liftGrid_const (M:=M) c) - rw [← heq, avg_liftGrid (fun _ : ℝ => c) (integrable_const _) hp h, setIntegral_const] - have hh : volume.real p.cells = p.len := by - rw [cells,box, Real.volume_real_Ico_of_le (by linarith [p.len_pos])] - ring - rw [hh, smul_smul, inv_mul_cancel₀ p.len_pos.ne', one_smul] - -end E - -lemma lift_bounds {M : ℕ} {ρ : ℝ → ℝ} (hρ : Integrable ρ μ₁) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) (y : Grid M) : - 0 ≤ liftGrid M ρ y ∧ liftGrid M ρ y ≤ B := by - have hi : IntegrableOn ρ (atom y) := (show IntegrableOn ρ unitInt volume from hρ).mono_set (atom_sub y) - have : IsFiniteMeasure (volume.restrict (atom y)) := - show IsFiniteMeasure (volume.restrict (Ico (pos y) (pos y+side M))) from inferInstance - constructor - · exact mul_nonneg (inv_pos.mpr (side_pos _)).le (integral_nonneg h0) - · have H : (∫ x in atom y, ρ x) ≤ ∫ x in atom y, B := - integral_mono hi (integrable_const _) hb - exact le_trans (mul_le_mul_of_nonneg_left H (inv_pos.mpr (side_pos _)).le) - (liftGrid_const B y).le - -lemma lift_shift {M : ℕ} (J : ℕ) (hJ : J+1 ≤ M) (f : ℝ → ℂ) - (hf : Measurable f) (B : ℝ) (hb : ∀ x, ‖f x‖ ≤ B) {y : Grid M} {x : ℝ} (hx : x ∈ atom y) : - FinShift J (liftGrid M f) y = shift f 0 1 J x := by - unfold FinShift shift haarLayer - refine Finset.sum_congr rfl fun n hn => Finset.sum_congr rfl fun k hk => ?_ - let p : Node := ⟨n,k⟩ - have hl : p.lev+2 ≤ M := by dsimp [p]; rw [Finset.mem_range] at hn; omega - have hB' (x : ℝ) := H_bound (p.normarg x) - have Hm : Measurable (p.Hi) := measurableH.comp (by unfold normarg; fun_prop) - have Hi : Integrable (fun x => p.Hi x • f x) μ₁ := - Integrable.of_bound (Hm.smul hf).aestronglyMeasurable B (ae_of_all _ (fun x => by - change ‖p.Hi x • f x‖ ≤ _ - rw [norm_smul] - exact (mul_le_of_le_one_left (norm_nonneg _) - (show ‖p.Hi x‖ ≤ 1 from hB' x)).trans (hb x))) - rw [show lft 0 1 n k = p.a by simp [lft,p,a,len], one_mul] - change FinJump p _ y = p.Gi x • _ - rw [FinJump, Gi_const (q:=vertex y) hl (pos_mem y) hx, - avg_smulHi_lift f (Finset.mem_range.mp hk) (by omega) Hi] - rfl - - -lemma lift_mod_error (M : ℕ) {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) (n : ℤ) (y : Grid M) : - ‖liftGrid M (modρ ρ (n:ℝ)) y - finMod (liftGrid M ρ) (n:Grid M) y‖ ≤ - (B * |(n:ℝ)| * (2*π)) * side M := by - rw [finMod, chi_cast] - have : IsFiniteMeasure (volume.restrict (atom y)) := - show IsFiniteMeasure (volume.restrict (Ico (pos y) (pos y+side M))) from inferInstance - let u : ℝ := n - let q (x : ℝ) := (ρ x:ℂ) * phase u (pos y) - let F := modρ ρ u - let C := (B * |u| *(2*π))*side M - have heq : (↑(liftGrid M ρ y) : ℂ) * phase u (pos y) = liftGrid M q y := by - unfold liftGrid q - rw [integral_mul_const, integral_complex_ofReal] - simp only [Complex.real_smul, smul_eq_mul, Complex.ofReal_mul, mul_assoc] - change ‖liftGrid M F y - _ * phase u (pos y)‖ ≤ C - rw [heq] - have HiF : IntegrableOn F (atom y) := Integrable.of_bound (measurable_mod hρ _).aestronglyMeasurable B - (ae_of_all _ fun x => by rw [norm_mod h0]; exact hb x) - have Hiq : IntegrableOn q (atom y) := - Integrable.of_bound (((Complex.continuous_ofReal.measurable.comp hρ).mul measurable_const).aestronglyMeasurable) B (ae_of_all _ fun x => by - change ‖q x‖ ≤ _ - unfold q - simpa only [norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 x), - phase_unimod, mul_one] using hb x) - have hh (x : ℝ) (hx : x ∈ atom y) : ‖F x - q x‖ ≤ C := by - unfold F modρ q C - rw [← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 _)] - have hk : |x-pos y| ≤ side M := by - change pos y ≤ x ∧ x < pos y+side M at hx - rw [abs_le]; constructor <;> linarith [hx.1,hx.2,side_pos M] - calc - _ ≤ B*(|u| *(2*π*side M)) := mul_le_mul (hb x) - ((norm_phase_sub u x _).trans (by gcongr)) (norm_nonneg _) ((h0 x).trans (hb x)) - _ = _ := by ring - rw [liftGrid, liftGrid, ← smul_sub, ← integral_sub HiF Hiq, norm_smul, Real.norm_eq_abs, - abs_of_pos (inv_pos.mpr (side_pos M))] - have H : ‖∫ x in atom y, F x - q x‖ ≤ ∫ _ in atom y, C := - (norm_integral_le_integral_norm _).trans (integral_mono_ae (HiF.sub Hiq).norm - (integrable_const _) ((ae_restrict_mem (atom_measurable y)).mono hh)) - refine (mul_le_mul_of_nonneg_left H (inv_pos.mpr (side_pos M)).le).trans ?_ - rw [setIntegral_const, atom_volume_real, smul_eq_mul, ← mul_assoc, - inv_mul_cancel₀ (side_pos M).ne', one_mul] - -lemma FinJump_error {M : ℕ} {f g : Grid M → ℂ} (B : ℝ) - (h : ∀ x, ‖f x - g x‖ ≤ B) {p : Node} (hp : p.valid) (hl : p.lev ≤ M) (x : Grid M) : - ‖FinJump p f x - FinJump p g x‖ ≤ B := by - rw [FinJump, FinJump, ← smul_sub, ← avg_sub, norm_smul, Real.norm_eq_abs] - refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans - ((norm_avg_le _ _).trans ?_) - refine (avg_mono p (g:=fun _ => B) fun y _ => ?_).trans (avg_const hp hl B).le - rw [← smul_sub, norm_smul, Real.norm_eq_abs] - exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (h _) - -lemma FinShift_error {M : ℕ} {f g : Grid M → ℂ} (B : ℝ) - (h : ∀ x, ‖f x - g x‖ ≤ B) {J : ℕ} (hJ : J ≤ M) (x : Grid M) : - ‖FinShift J f x - FinShift J g x‖ ≤ (∑ n ∈ Finset.range J, (2:ℝ)^n)*B := by - unfold FinShift - rw [← Finset.sum_sub_distrib,Finset.sum_mul] - refine (norm_sum_le _ _).trans (Finset.sum_le_sum (fun n hn => ?_)) - rw [← Finset.sum_sub_distrib] - refine (norm_sum_le _ _).trans ?_ - apply le_trans (b:=∑ k ∈ Finset.range (2^n), B) - · exact Finset.sum_le_sum (fun k hk => FinJump_error B h (Finset.mem_range.mp hk) - ((Finset.mem_range.mp hn).le.trans hJ) _) - simp - - -lemma usable_transfer {ρ : ℝ → ℝ} (hf : Integrable ρ μ₁) (D : ℝ) - {M J : ℕ} (h : J ≤ M) {x : ℝ} (hx : usable ρ 0 1 J D x) - {y : Grid M} (hy : x ∈ atom y) : - finGood D J (liftGrid M ρ) y := by - intro p hp hl hpy - rw [avg_liftGrid ρ hf hp (hl.le.trans h)] - have hx' : x ∈ p.cells := (atom_iff_coarse p (hl.le.trans h) hy).mpr hpy - have H1 : lft 0 1 p.lev p.ix = p.a := by simp [lft,a,len] - have H2 : 1*side p.lev = p.len := one_mul _ - have H := hx p.lev hl p.ix hp (by rwa [H1,H2]) - rw [H1,H2,density,mean_translate_scale _ _ p.len_pos] at H - exact H - -lemma expU_add_one (M : ℕ) (W : Grid M → ℝ) : - expU (fun y => W y + 1) = expU W + 1 := by - simp only [expU, Finset.sum_add_distrib, smul_eq_mul, Finset.sum_const, Finset.card_univ, - nsmul_eq_mul, mul_one, mul_add, Grid, ZMod.card, Nat.cast_pow, Nat.cast_ofNat] - rw [mul_comm (side M) ((2:ℝ)^M), pow_mul_side] - -theorem GridBound.unitTreeBound (hgrid : GridBound) : UnitTreeBound := by - obtain ⟨C,hC,h⟩ := hgrid - refine ⟨C+1, by positivity,?_⟩ - filter_upwards [h] with j hj - intro ρ hρ h0 hm hb J G hG hGu hD - let B := Real.exp ((2:ℝ)^j) - have hB : 0 ≤ B := Real.exp_nonneg _ - have hbi : Integrable ρ μ₁ := Integrable.of_bound hρ.aestronglyMeasurable B - (ae_of_all _ (fun x => by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 x)] using hb x)) - let Y (n : ℤ) (x : ℝ) := ENNReal.ofReal ‖shift (modρ ρ n) 0 1 J x‖ - let Z (n : ℕ) (x : ℝ) := ⨆ i : ℤ, ⨆ (_h : i.natAbs ≤ n), Y i x - have hYm (n : ℤ) : Measurable (Y n) := - (show Measurable (fun x => shift (modρ ρ n) 0 1 J x) - from measurable_shift_comp (measurable_mod hρ _) measurable_const measurable_const measurable_id J) - |>.norm.ennreal_ofReal - have hZm (n : ℕ) : Measurable (Z n) := - Measurable.iSup fun i => Measurable.iSup fun (_ : i.natAbs ≤ n) => hYm i - have hZo : Monotone Z := fun n m h x => iSup_le fun i => iSup_le fun hi => - le_iSup₂ (f := fun i (_h : i.natAbs ≤ m) => Y i x) i (le_trans hi h) - have hZ (x : ℝ) : maxShift ρ 0 1 J x ≤ ⨆ n, Z n x := by - refine iSup_le fun i => - (le_iSup₂ (f := fun l : ℤ => fun (_ : l.natAbs ≤ i.natAbs) => Y l x) i le_rfl).trans - (le_iSup (fun n => Z n x) i.natAbs) - refine (lintegral_mono hZ).trans ?_ - rw [lintegral_iSup hZm hZo] - refine iSup_le fun n => ?_ - let S := ∑ m ∈ Finset.range J, (2:ℝ)^m - have ht : Tendsto (fun M => S*(B*(n:ℝ)*(2*π))*side M) atTop (𝓝 0) := by - have hs : Tendsto side atTop (𝓝 0) := tendsto_pow_atTop_nhds_zero_of_lt_one - (by norm_num) (by norm_num) - simpa only [mul_zero] using hs.const_mul (S*(B*(n:ℝ)*(2*π))) - obtain ⟨M,hM,hMJ,hMt⟩ := ((hj J).and - ((eventually_ge_atTop (J+1)).and (ht.eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num))))).exists - change _ < (1:ℝ) at hMt - obtain ⟨W,hW,hWm,hWe⟩ := - hM (liftGrid M ρ) (fun y => (lift_bounds hbi h0 B hb y).1) (by rwa [expU_liftGrid M ρ hbi]) - (fun y => (lift_bounds hbi h0 B hb y).2) - have Hx (y : Grid M) (x : ℝ) (hx : x ∈ atom y) (hxg : x ∈ G) : - Z n x ≤ ENNReal.ofReal (W y + 1) := by - refine iSup_le fun (i : ℤ) => iSup_le fun hi => ?_ - have hw := hWe y (usable_transfer hbi _ (by omega) (hD x hxg) hx) (i:Grid M) - let f := liftGrid M (modρ ρ (i:ℝ)) - have hh := FinShift_error (B*|(i:ℝ)| * (2*π)*side M) (lift_mod_error M hρ h0 B hb i) - (show J ≤ M by omega) y - change ‖FinShift J f y - _‖ ≤ S*_ at hh - have Hlem : |(i:ℝ)| ≤ (n:ℝ) := by rw [abs_le]; constructor <;> exact_mod_cast (by omega) - have hlim : S*(B*|(i:ℝ)| * (2*π)*side M) ≤ 1 := by - calc - _ ≤ S*(B*(n:ℝ)*(2*π)*side M) := by - have h := side_pos M - have : 0 ≤ S := Finset.sum_nonneg (fun _ _ => by positivity) - gcongr - _ ≤ 1 := by linarith - change ENNReal.ofReal _ ≤ _ - apply ENNReal.ofReal_le_ofReal - have hfi (x : ℝ) : ‖modρ ρ i x‖ ≤ B := by rw [norm_mod h0]; exact hb _ - rw [← lift_shift J hMJ _ (measurable_mod hρ _) B hfi hx] - have HE := norm_sub_norm_le (FinShift J f y) (FinShift J (finMod (liftGrid M ρ) (i:Grid M)) y) - - change ‖FinShift J f y‖ ≤ W y + 1 - linarith - let V := G.indicator (Z n) - have HV : (∫⁻ x in G, Z n x) = ∫⁻ x in unitInt, V x := by - unfold V - rw [lintegral_indicator hG, Measure.restrict_restrict hG, inter_eq_left.mpr hGu] - rw [HV, atom_partition M, lintegral_iUnion atom_measurable atom_disjoint, tsum_fintype] - have Hv (y : Grid M) : - (∫⁻ x in atom y, V x) ≤ ENNReal.ofReal ((W y+1)*side M) := by - calc - _ ≤ ∫⁻ _ in atom y, ENNReal.ofReal (W y+1) := by - apply lintegral_mono_ae - filter_upwards [ae_restrict_mem (atom_measurable y)] with x hx - by_cases H : x ∈ G - · simpa [V,H] using Hx y x hx H - · simp [V,H] - _ = _ := by rw [setLIntegral_const, atom_volume, - ENNReal.ofReal_mul (by linarith [hW y] : 0 ≤ W y+1)] - refine (Finset.sum_le_sum (fun y _ => Hv y)).trans ?_ - rw [← ENNReal.ofReal_sum_of_nonneg] - · apply ENNReal.ofReal_le_ofReal - calc - ∑ y, _ = expU (fun y => W y + 1) := by simp only [expU,smul_eq_mul,← Finset.sum_mul]; ring - _ ≤ C*(2:ℝ)^j+1 := by rw [expU_add_one M W]; linarith - _ ≤ _ := by linarith [one_le_pow₀ (by norm_num : (1:ℝ) ≤ 2) (n:=j)] - · intro y _; have := side_pos M; have := hW y; positivity - -end -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Grid +/-! # Reduction to the finite grid -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Dyadic +open Period Node +noncomputable section + +lemma expU_mono {M : ℕ} {f g : Grid M → ℝ} (h : ∀ y, f y ≤ g y) : + expU f ≤ expU g := + mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun y _ => h y)) (side_pos _).le + +lemma avg_mono {M : ℕ} (p : Node) {f g : Grid M → ℝ} (h : ∀ y ∈ p.block M, f y ≤ g y) : + p.avg f ≤ p.avg g := by + apply mul_le_mul_of_nonneg_left (expU_mono ?_) (inv_pos.mpr p.len_pos).le + intro y + by_cases hy : y ∈ p.block M + · simpa [hy] using h y hy + · simp [hy] + +lemma atom_volume_real {M : ℕ} (x : Grid M) : volume.real (atom x) = side M := by + rw [measureReal_def, atom_volume, ENNReal.toReal_ofReal (side_pos M).le] + +section E +variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] + +/-- Cell averages from the real model. -/ +def liftGrid (M : ℕ) (f : ℝ → E) (y : Grid M) : E := + (side M)⁻¹ • ∫ x in atom y, f x + +lemma expU_liftGrid (M : ℕ) (f : ℝ → E) (hf : Integrable f μ₁) : + expU (liftGrid M f) = ∫ x, f x ∂μ₁ := by + have hi : IntegrableOn f (⋃ y : Grid M, atom y) := by rw [← atom_partition]; exact hf + unfold expU liftGrid μ₁ + rw [atom_partition M, integral_iUnion atom_measurable atom_disjoint hi, tsum_fintype] + rw [Finset.smul_sum] + refine Finset.sum_congr rfl fun x _ => ?_ + rw [smul_smul, mul_inv_cancel₀ (side_pos M).ne', one_smul] + +lemma liftGrid_ind {M : ℕ} (p : Node) (h : p.lev ≤ M) (f : ℝ → E) : + liftGrid M (p.cells.indicator f) = (p.block M).indicator (liftGrid M f) := by + ext y + by_cases hy : y ∈ p.block M + · rw [indicator_of_mem hy] + unfold liftGrid + congr 1 + exact setIntegral_congr_fun (atom_measurable y) + (fun x hx => indicator_of_mem ((atom_iff_coarse p h hx).mpr hy) f) + · rw [indicator_of_notMem hy, liftGrid, setIntegral_eq_zero_of_forall_eq_zero] + · exact smul_zero _ + · exact fun x hx => indicator_of_notMem (mt (atom_iff_coarse p h hx).mp hy) f + +lemma avg_liftGrid {M : ℕ} (f : ℝ → E) (hf : Integrable f μ₁) + {p : Node} (hp : p.valid) (h : p.lev ≤ M) : + p.avg (liftGrid M f) = p.len⁻¹ • ∫ x in p.cells, f x := by + rw [avg, ← liftGrid_ind p h, expU_liftGrid, μ₁, setIntegral_indicator + (show MeasurableSet p.cells from measurableSet_Ico), inter_eq_right.mpr (cells_sub hp)] + exact hf.indicator measurableSet_Ico + +lemma avg_smulHi_lift {M : ℕ} (f : ℝ → E) {p : Node} (hp : p.valid) (h : p.lev+1 ≤ M) + (hF : Integrable (fun x => p.Hi x • f x) μ₁) : + p.avg (fun y => p.Hi (pos y) • liftGrid M f y) = + ∫ x, haarH x • f (p.a+p.len*x) ∂μ₁ := by + have heq : (fun y => p.Hi (pos y) • liftGrid M f y) = + liftGrid M (fun x => p.Hi x • f x) := by + ext y + unfold liftGrid + rw [smul_comm, ← integral_smul] + congr 1 + exact setIntegral_congr_fun (atom_measurable _) fun x hx => + congrArg (fun c : ℝ => c • f x) (Hi_const (p:=p) (q:=vertex y) h (pos_mem y) hx) + rw [heq, avg_liftGrid _ hF hp (by omega), + cells, box, ← mean_translate_scale (fun x => p.Hi x • f x) p.a p.len_pos] + congr 1 + funext x + rw [show p.Hi (p.a+p.len*x) = haarH x from congrArg haarH (by + unfold normarg; field_simp [p.len_pos.ne']; ring)] + +lemma norm_expU_le {M : ℕ} (f : Grid M → E) : ‖expU f‖ ≤ expU (fun x => ‖f x‖) := by + unfold expU + rw [norm_smul, Real.norm_eq_abs, abs_of_pos (side_pos _)] + exact mul_le_mul_of_nonneg_left (norm_sum_le _ _) (side_pos _).le + +lemma norm_avg_le {M : ℕ} (p : Node) (f : Grid M → E) : + ‖p.avg f‖ ≤ p.avg (fun x => ‖f x‖) := by + unfold avg + rw [norm_smul, Real.norm_eq_abs, abs_of_pos (inv_pos.mpr p.len_pos)] + refine mul_le_mul_of_nonneg_left ((norm_expU_le _).trans ?_) (inv_pos.mpr p.len_pos).le + apply expU_mono + intro y; by_cases hy : y ∈ block p M <;> simp [hy] + +lemma avg_sub {M : ℕ} (p : Node) (f g : Grid M → E) : + p.avg (fun y => f y - g y) = p.avg f - p.avg g := by + unfold avg expU + simp_rw [indicator_sub, Finset.sum_sub_distrib, smul_sub] + +variable [CompleteSpace E] + +lemma liftGrid_const {M : ℕ} (c : E) (y : Grid M) : + liftGrid M (fun _ => c) y = c := by + rw [liftGrid, setIntegral_const, atom_volume_real, smul_smul, + inv_mul_cancel₀ (side_pos _).ne', one_smul] + + +lemma avg_const {M : ℕ} {p : Node} (hp : p.valid) (h : p.lev ≤ M) (c : E) : + p.avg (fun _ : Grid M => c) = c := by + have heq := funext (liftGrid_const (M:=M) c) + rw [← heq, avg_liftGrid (fun _ : ℝ => c) (integrable_const _) hp h, setIntegral_const] + have hh : volume.real p.cells = p.len := by + rw [cells,box, Real.volume_real_Ico_of_le (by linarith [p.len_pos])] + ring + rw [hh, smul_smul, inv_mul_cancel₀ p.len_pos.ne', one_smul] + +end E + +lemma lift_bounds {M : ℕ} {ρ : ℝ → ℝ} (hρ : Integrable ρ μ₁) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) (y : Grid M) : + 0 ≤ liftGrid M ρ y ∧ liftGrid M ρ y ≤ B := by + have hi : IntegrableOn ρ (atom y) := (show IntegrableOn ρ unitInt volume from hρ).mono_set (atom_sub y) + have : IsFiniteMeasure (volume.restrict (atom y)) := + show IsFiniteMeasure (volume.restrict (Ico (pos y) (pos y+side M))) from inferInstance + constructor + · exact mul_nonneg (inv_pos.mpr (side_pos _)).le (integral_nonneg h0) + · have H : (∫ x in atom y, ρ x) ≤ ∫ x in atom y, B := + integral_mono hi (integrable_const _) hb + exact le_trans (mul_le_mul_of_nonneg_left H (inv_pos.mpr (side_pos _)).le) + (liftGrid_const B y).le + +lemma lift_shift {M : ℕ} (J : ℕ) (hJ : J+1 ≤ M) (f : ℝ → ℂ) + (hf : Measurable f) (B : ℝ) (hb : ∀ x, ‖f x‖ ≤ B) {y : Grid M} {x : ℝ} (hx : x ∈ atom y) : + FinShift J (liftGrid M f) y = shift f 0 1 J x := by + unfold FinShift shift haarLayer + refine Finset.sum_congr rfl fun n hn => Finset.sum_congr rfl fun k hk => ?_ + let p : Node := ⟨n,k⟩ + have hl : p.lev+2 ≤ M := by dsimp [p]; rw [Finset.mem_range] at hn; omega + have hB' (x : ℝ) := H_bound (p.normarg x) + have Hm : Measurable (p.Hi) := measurableH.comp (by unfold normarg; fun_prop) + have Hi : Integrable (fun x => p.Hi x • f x) μ₁ := + Integrable.of_bound (Hm.smul hf).aestronglyMeasurable B (ae_of_all _ (fun x => by + change ‖p.Hi x • f x‖ ≤ _ + rw [norm_smul] + exact (mul_le_of_le_one_left (norm_nonneg _) + (show ‖p.Hi x‖ ≤ 1 from hB' x)).trans (hb x))) + rw [show lft 0 1 n k = p.a by simp [lft,p,a,len], one_mul] + change FinJump p _ y = p.Gi x • _ + rw [FinJump, Gi_const (q:=vertex y) hl (pos_mem y) hx, + avg_smulHi_lift f (Finset.mem_range.mp hk) (by omega) Hi] + rfl + + +lemma lift_mod_error (M : ℕ) {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) (n : ℤ) (y : Grid M) : + ‖liftGrid M (modρ ρ (n:ℝ)) y - finMod (liftGrid M ρ) (n:Grid M) y‖ ≤ + (B * |(n:ℝ)| * (2*π)) * side M := by + rw [finMod, chi_cast] + have : IsFiniteMeasure (volume.restrict (atom y)) := + show IsFiniteMeasure (volume.restrict (Ico (pos y) (pos y+side M))) from inferInstance + let u : ℝ := n + let q (x : ℝ) := (ρ x:ℂ) * phase u (pos y) + let F := modρ ρ u + let C := (B * |u| *(2*π))*side M + have heq : (↑(liftGrid M ρ y) : ℂ) * phase u (pos y) = liftGrid M q y := by + unfold liftGrid q + rw [integral_mul_const, integral_complex_ofReal] + simp only [Complex.real_smul, smul_eq_mul, Complex.ofReal_mul, mul_assoc] + change ‖liftGrid M F y - _ * phase u (pos y)‖ ≤ C + rw [heq] + have HiF : IntegrableOn F (atom y) := Integrable.of_bound (measurable_mod hρ _).aestronglyMeasurable B + (ae_of_all _ fun x => by rw [norm_mod h0]; exact hb x) + have Hiq : IntegrableOn q (atom y) := + Integrable.of_bound (((Complex.continuous_ofReal.measurable.comp hρ).mul measurable_const).aestronglyMeasurable) B (ae_of_all _ fun x => by + change ‖q x‖ ≤ _ + unfold q + simpa only [norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 x), + phase_unimod, mul_one] using hb x) + have hh (x : ℝ) (hx : x ∈ atom y) : ‖F x - q x‖ ≤ C := by + unfold F modρ q C + rw [← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 _)] + have hk : |x-pos y| ≤ side M := by + change pos y ≤ x ∧ x < pos y+side M at hx + rw [abs_le]; constructor <;> linarith [hx.1,hx.2,side_pos M] + calc + _ ≤ B*(|u| *(2*π*side M)) := mul_le_mul (hb x) + ((norm_phase_sub u x _).trans (by gcongr)) (norm_nonneg _) ((h0 x).trans (hb x)) + _ = _ := by ring + rw [liftGrid, liftGrid, ← smul_sub, ← integral_sub HiF Hiq, norm_smul, Real.norm_eq_abs, + abs_of_pos (inv_pos.mpr (side_pos M))] + have H : ‖∫ x in atom y, F x - q x‖ ≤ ∫ _ in atom y, C := + (norm_integral_le_integral_norm _).trans (integral_mono_ae (HiF.sub Hiq).norm + (integrable_const _) ((ae_restrict_mem (atom_measurable y)).mono hh)) + refine (mul_le_mul_of_nonneg_left H (inv_pos.mpr (side_pos M)).le).trans ?_ + rw [setIntegral_const, atom_volume_real, smul_eq_mul, ← mul_assoc, + inv_mul_cancel₀ (side_pos M).ne', one_mul] + +lemma FinJump_error {M : ℕ} {f g : Grid M → ℂ} (B : ℝ) + (h : ∀ x, ‖f x - g x‖ ≤ B) {p : Node} (hp : p.valid) (hl : p.lev ≤ M) (x : Grid M) : + ‖FinJump p f x - FinJump p g x‖ ≤ B := by + rw [FinJump, FinJump, ← smul_sub, ← avg_sub, norm_smul, Real.norm_eq_abs] + refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans + ((norm_avg_le _ _).trans ?_) + refine (avg_mono p (g:=fun _ => B) fun y _ => ?_).trans (avg_const hp hl B).le + rw [← smul_sub, norm_smul, Real.norm_eq_abs] + exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (h _) + +lemma FinShift_error {M : ℕ} {f g : Grid M → ℂ} (B : ℝ) + (h : ∀ x, ‖f x - g x‖ ≤ B) {J : ℕ} (hJ : J ≤ M) (x : Grid M) : + ‖FinShift J f x - FinShift J g x‖ ≤ (∑ n ∈ Finset.range J, (2:ℝ)^n)*B := by + unfold FinShift + rw [← Finset.sum_sub_distrib,Finset.sum_mul] + refine (norm_sum_le _ _).trans (Finset.sum_le_sum (fun n hn => ?_)) + rw [← Finset.sum_sub_distrib] + refine (norm_sum_le _ _).trans ?_ + apply le_trans (b:=∑ k ∈ Finset.range (2^n), B) + · exact Finset.sum_le_sum (fun k hk => FinJump_error B h (Finset.mem_range.mp hk) + ((Finset.mem_range.mp hn).le.trans hJ) _) + simp + + +lemma usable_transfer {ρ : ℝ → ℝ} (hf : Integrable ρ μ₁) (D : ℝ) + {M J : ℕ} (h : J ≤ M) {x : ℝ} (hx : usable ρ 0 1 J D x) + {y : Grid M} (hy : x ∈ atom y) : + finGood D J (liftGrid M ρ) y := by + intro p hp hl hpy + rw [avg_liftGrid ρ hf hp (hl.le.trans h)] + have hx' : x ∈ p.cells := (atom_iff_coarse p (hl.le.trans h) hy).mpr hpy + have H1 : lft 0 1 p.lev p.ix = p.a := by simp [lft,a,len] + have H2 : 1*side p.lev = p.len := one_mul _ + have H := hx p.lev hl p.ix hp (by rwa [H1,H2]) + rw [H1,H2,density,mean_translate_scale _ _ p.len_pos] at H + exact H + +lemma expU_add_one (M : ℕ) (W : Grid M → ℝ) : + expU (fun y => W y + 1) = expU W + 1 := by + simp only [expU, Finset.sum_add_distrib, smul_eq_mul, Finset.sum_const, Finset.card_univ, + nsmul_eq_mul, mul_one, mul_add, Grid, ZMod.card, Nat.cast_pow, Nat.cast_ofNat] + rw [mul_comm (side M) ((2:ℝ)^M), pow_mul_side] + +theorem GridBound.unitTreeBound (hgrid : GridBound) : UnitTreeBound := by + obtain ⟨C,hC,h⟩ := hgrid + refine ⟨C+1, by positivity,?_⟩ + filter_upwards [h] with j hj + intro ρ hρ h0 hm hb J G hG hGu hD + let B := Real.exp ((2:ℝ)^j) + have hB : 0 ≤ B := Real.exp_nonneg _ + have hbi : Integrable ρ μ₁ := Integrable.of_bound hρ.aestronglyMeasurable B + (ae_of_all _ (fun x => by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 x)] using hb x)) + let Y (n : ℤ) (x : ℝ) := ENNReal.ofReal ‖shift (modρ ρ n) 0 1 J x‖ + let Z (n : ℕ) (x : ℝ) := ⨆ i : ℤ, ⨆ (_h : i.natAbs ≤ n), Y i x + have hYm (n : ℤ) : Measurable (Y n) := + (show Measurable (fun x => shift (modρ ρ n) 0 1 J x) + from measurable_shift_comp (measurable_mod hρ _) measurable_const measurable_const measurable_id J) + |>.norm.ennreal_ofReal + have hZm (n : ℕ) : Measurable (Z n) := + Measurable.iSup fun i => Measurable.iSup fun (_ : i.natAbs ≤ n) => hYm i + have hZo : Monotone Z := fun n m h x => iSup_le fun i => iSup_le fun hi => + le_iSup₂ (f := fun i (_h : i.natAbs ≤ m) => Y i x) i (le_trans hi h) + have hZ (x : ℝ) : maxShift ρ 0 1 J x ≤ ⨆ n, Z n x := by + refine iSup_le fun i => + (le_iSup₂ (f := fun l : ℤ => fun (_ : l.natAbs ≤ i.natAbs) => Y l x) i le_rfl).trans + (le_iSup (fun n => Z n x) i.natAbs) + refine (lintegral_mono hZ).trans ?_ + rw [lintegral_iSup hZm hZo] + refine iSup_le fun n => ?_ + let S := ∑ m ∈ Finset.range J, (2:ℝ)^m + have ht : Tendsto (fun M => S*(B*(n:ℝ)*(2*π))*side M) atTop (𝓝 0) := by + have hs : Tendsto side atTop (𝓝 0) := tendsto_pow_atTop_nhds_zero_of_lt_one + (by norm_num) (by norm_num) + simpa only [mul_zero] using hs.const_mul (S*(B*(n:ℝ)*(2*π))) + obtain ⟨M,hM,hMJ,hMt⟩ := ((hj J).and + ((eventually_ge_atTop (J+1)).and (ht.eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num))))).exists + change _ < (1:ℝ) at hMt + obtain ⟨W,hW,hWm,hWe⟩ := + hM (liftGrid M ρ) (fun y => (lift_bounds hbi h0 B hb y).1) (by rwa [expU_liftGrid M ρ hbi]) + (fun y => (lift_bounds hbi h0 B hb y).2) + have Hx (y : Grid M) (x : ℝ) (hx : x ∈ atom y) (hxg : x ∈ G) : + Z n x ≤ ENNReal.ofReal (W y + 1) := by + refine iSup_le fun (i : ℤ) => iSup_le fun hi => ?_ + have hw := hWe y (usable_transfer hbi _ (by omega) (hD x hxg) hx) (i:Grid M) + let f := liftGrid M (modρ ρ (i:ℝ)) + have hh := FinShift_error (B*|(i:ℝ)| * (2*π)*side M) (lift_mod_error M hρ h0 B hb i) + (show J ≤ M by omega) y + change ‖FinShift J f y - _‖ ≤ S*_ at hh + have Hlem : |(i:ℝ)| ≤ (n:ℝ) := by rw [abs_le]; constructor <;> exact_mod_cast (by omega) + have hlim : S*(B*|(i:ℝ)| * (2*π)*side M) ≤ 1 := by + calc + _ ≤ S*(B*(n:ℝ)*(2*π)*side M) := by + have h := side_pos M + have : 0 ≤ S := Finset.sum_nonneg (fun _ _ => by positivity) + gcongr + _ ≤ 1 := by linarith + change ENNReal.ofReal _ ≤ _ + apply ENNReal.ofReal_le_ofReal + have hfi (x : ℝ) : ‖modρ ρ i x‖ ≤ B := by rw [norm_mod h0]; exact hb _ + rw [← lift_shift J hMJ _ (measurable_mod hρ _) B hfi hx] + have HE := norm_sub_norm_le (FinShift J f y) (FinShift J (finMod (liftGrid M ρ) (i:Grid M)) y) + + change ‖FinShift J f y‖ ≤ W y + 1 + linarith + let V := G.indicator (Z n) + have HV : (∫⁻ x in G, Z n x) = ∫⁻ x in unitInt, V x := by + unfold V + rw [lintegral_indicator hG, Measure.restrict_restrict hG, inter_eq_left.mpr hGu] + rw [HV, atom_partition M, lintegral_iUnion atom_measurable atom_disjoint, tsum_fintype] + have Hv (y : Grid M) : + (∫⁻ x in atom y, V x) ≤ ENNReal.ofReal ((W y+1)*side M) := by + calc + _ ≤ ∫⁻ _ in atom y, ENNReal.ofReal (W y+1) := by + apply lintegral_mono_ae + filter_upwards [ae_restrict_mem (atom_measurable y)] with x hx + by_cases H : x ∈ G + · simpa [V,H] using Hx y x hx H + · simp [V,H] + _ = _ := by rw [setLIntegral_const, atom_volume, + ENNReal.ofReal_mul (by linarith [hW y] : 0 ≤ W y+1)] + refine (Finset.sum_le_sum (fun y _ => Hv y)).trans ?_ + rw [← ENNReal.ofReal_sum_of_nonneg] + · apply ENNReal.ofReal_le_ofReal + calc + ∑ y, _ = expU (fun y => W y + 1) := by simp only [expU,smul_eq_mul,← Finset.sum_mul]; ring + _ ≤ C*(2:ℝ)^j+1 := by rw [expU_add_one M W]; linarith + _ ≤ _ := by linarith [one_le_pow₀ (by norm_num : (1:ℝ) ≤ 2) (n:=j)] + · intro y _; have := side_pos M; have := hW y; positivity + +end +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Node.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Node.lean index d878a2e14bcbd21d685bd2d312ac7544ae18e4f0..930b852de78b251deaeb7c4ee621ae4cbdbf93a7 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Node.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Node.lean @@ -1,232 +1,232 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Circle - -/-! # Finite dyadic blocks and probabilities -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real - -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Period Law -namespace Node - -variable {M : ℕ} - -def invlen (p : Node) : ℝ := 2^p.lev - -lemma rinv (p : Node) : invlen p = p.len⁻¹ := by rw [len,side_pow_two,inv_inv]; rfl - -lemma invlen_ge (p : Node) : 1 ≤ invlen p := one_le_pow₀ (by norm_num) - -lemma invlen_pos (p : Node) : 0 < invlen p := lt_of_lt_of_le zero_lt_one p.invlen_ge - -lemma descale {p : Node} (h : p.lev ≤ M) : - p.len = side M * (2:ℝ)^(M-p.lev) := by - rw [len,side_pow_two,side_pow_two] - conv_rhs => lhs; rw [show M=p.lev+(M-p.lev) by omega] - rw [pow_add]; field_simp - -lemma mem_block_iff {p : Node} (h : p.lev ≤ M) (x : Grid M) : - x ∈ p.block M ↔ p.ix*2^(M-p.lev) ≤ x.val ∧ x.val < (p.ix+1)*2^(M-p.lev) := by - change p.a ≤ pos x ∧ _ < p.a+p.len ↔ _ - have he (i : ℕ) : (i:ℝ)*p.len = (i*2^(M-p.lev) : ℕ) * side M := by - rw [descale h]; push_cast; ring - unfold a pos - rw [show p.ix*p.len+p.len=↑(p.ix+1)*p.len by push_cast; ring, he,he, - mul_le_mul_iff_left₀ (side_pos _),mul_lt_mul_iff_left₀ (side_pos _)] - norm_cast - -/-- First grid index in a fitting node. -/ -def idx (p : Node) (M : ℕ) := p.ix * 2^(M-p.lev) -def wid (p : Node) (M : ℕ) := 2^(M-p.lev) - -lemma fitting (p : Node) (hv : p.valid) (h : p.lev ≤ M) : - p.idx M+p.wid M ≤ 2^M := by - unfold idx wid - calc - _ = (p.ix+1)*2^(M-p.lev) := by ring - _ ≤ 2^p.lev*2^(M-p.lev) := Nat.mul_le_mul_right _ hv - _ = _ := by rw [← pow_add]; congr 1; omega - -lemma wid_pos (p : Node) (M : ℕ) : 0 < p.wid M := pow_pos (by omega) _ - -def start (p : Node) (M : ℕ) : Grid M := (p.idx M : Grid M) - -lemma start_val {p : Node} (hv : p.valid) (h : p.lev ≤ M) : - (p.start M).val = p.idx M := by - apply ZMod.val_natCast_of_lt - have HH := p.fitting hv h - have HH' := p.wid_pos M - omega - -lemma start_pos {p : Node} (hv : p.valid) (h : p.lev ≤ M) : - pos (p.start M) = p.a := by - rw [pos, start_val hv h,a,descale h] - unfold idx; push_cast; ring - -lemma start_mem {p : Node} (hv : p.valid) (h : p.lev ≤ M) : - p.start M ∈ p.block M := by - rw [mem_block_iff h,start_val hv h] - unfold idx - constructor - · exact le_rfl - · gcongr; omega - -open scoped Classical -/-- Finset of vertices. -/ -def group (p : Node) (M : ℕ) := (p.block M).toFinset - -lemma mem_group (p : Node) (x : Grid M) : x ∈ p.group M ↔ x ∈ p.block M := by simp [group] - -lemma group_range {p : Node} (h : p.lev ≤ M) : - p.group M = first M (p.idx M+p.wid M) \ first M (p.idx M) := by - ext i; simp only [mem_group, Finset.mem_sdiff, mem_first, mem_block_iff h] - unfold idx wid; rw [add_mul,one_mul]; omega - -lemma group_card {p : Node} (hv : p.valid) (h : p.lev ≤ M) : - (p.group M).card = p.wid M := by - have hh := p.fitting hv h - rw [group_range h,Finset.card_sdiff_of_subset ?sub,first_card _ hh,first_card _ (by omega)] - · omega - case sub => - intro x hx - simp_rw [mem_first] at * - omega - -lemma group_size {p : Node} (hv : p.valid) (h : p.lev ≤ M) : - (p.group M).card * side M = p.len := by - rw [group_card hv h,descale h] - unfold wid; push_cast; ring - -lemma block_separate (p : Node) (M : ℕ) : - Disjoint (p.L.block M) (p.R.block M) := by - refine Set.disjoint_left.mpr fun x hx hy => ?_ - exact Set.disjoint_left.mp (cells_dis (k:=p.ix*2) (l:=p.ix*2+1) (by omega) - (n:=p.lev+1)) hx hy - -lemma block_union (p : Node) (M : ℕ) : - p.block M = p.L.block M ∪ p.R.block M := by rw [block,p.cells_split]; rfl - -lemma group_dis (p : Node) (M : ℕ) : - Disjoint (p.L.group M) (p.R.group M) := - Finset.disjoint_left.mpr fun _ hx hy => Set.disjoint_left.mp (p.block_separate _) - ((mem_group _ _).mp hx) ((mem_group _ _).mp hy) - -lemma group_union (p : Node) (M : ℕ) : - p.group M = p.L.group M ∪ p.R.group M := by ext x; simp only [Finset.mem_union,mem_group]; rw [p.block_union]; rfl - -def ok (p : Node) (M : ℕ) : Prop := p.valid ∧ p.lev ≤ M - -lemma root_ok (M : ℕ) : ok root M := by simp [ok,valid,root] -lemma len_top : root.len = 1 := by simp [len,root,side] -lemma invlen_top : root.invlen = 1 := by simp [invlen,root] -lemma group_top (M : ℕ) : root.group M = Finset.univ := by - ext x; rw [mem_group,mem_block_iff (root_ok M).2] - simp only [root, Nat.zero_mul, Nat.zero_le, true_and, zero_add,one_mul,Nat.sub_zero] - simp [ZMod.val_lt] - -open scoped Classical - -/-- Local averages as sums. -/ -lemma avg_sum {E : Type*} [AddCommMonoid E] [SMul ℝ E] (p : Node) - (f : Grid M → E) : - p.avg f = p.len⁻¹ • (side M • ∑ x ∈ p.group M, f x) := by - unfold avg expU - congr 2 - calc - _ = ∑ x ∈ p.group M, (p.block M).indicator f x := by - refine (Finset.sum_subset (Finset.subset_univ _) ?_).symm - intro i _ hi - apply Set.indicator_of_notMem ((mem_group _ _).not.mp hi) f - _ = _ := Finset.sum_congr rfl fun i hi => Set.indicator_of_mem ((mem_group _ _).mp hi) f - -lemma avg_nonneg (p : Node) {f : Grid M → ℝ} (h : ∀ x, x∈p.block M → 0 ≤ f x) : - 0 ≤ p.avg f := by - rw [avg_sum, smul_eq_mul, smul_eq_mul] - exact mul_nonneg (inv_nonneg.mpr p.len_pos.le) <| mul_nonneg (side_pos _).le - (Finset.sum_nonneg fun i hi => h _ ((mem_group _ _).mp hi)) - -lemma avg_split_real (p : Node) (f : Grid M → ℝ) : - p.avg f = (p.L.avg f+p.R.avg f)/2 := by - rw [avg_sum,avg_sum,avg_sum,p.group_union,Finset.sum_union (p.group_dis M),p.len_L,p.len_R]; simp; ring - -lemma avg_split_complex (p : Node) (f : Grid M → ℂ) : - p.avg f = (p.L.avg f+p.R.avg f)/2 := by - rw [avg_sum,avg_sum,avg_sum,p.group_union,Finset.sum_union (p.group_dis M),p.len_L,p.len_R] - simp_rw [Complex.real_smul]; push_cast; ring_nf - -/-- Uniform conditional law on a block (total default law if not fitting). -/ -def dice (p : Node) (M : ℕ) : Law (Grid M) := (uniform _).condP fun x => x ∈ p.block M - -lemma prob_block {p : Node} (h : p.ok M) : - (uniform (Grid M)).prob (fun x => x∈p.block M) = p.len := by - rw [prob_eq] - have He : Finset.univ.filter (fun x : Grid M => x∈p.block M)=p.group M := by - ext x; simp [mem_group] - rw [← Finset.sum_filter, He] - simp_rw [uniform_side,Finset.sum_const,nsmul_eq_mul,group_size h.1 h.2] - -lemma dice_apply {p : Node} (h : p.ok M) (x : Grid M) : - p.dice M x = if x∈p.block M then side M * p.len⁻¹ else 0 := by - unfold dice - rw [condP_pos_apply _ _ (by rw [prob_block h]; exact p.len_pos),prob_block h,uniform_side] - split_ifs <;> simp [div_eq_mul_inv] -lemma dice_positive {p : Node} (h : p.ok M) {x : Grid M} : - 0 < p.dice M x ↔ x ∈ p.block M := by - rw [dice_apply h]; split_ifs with hh - · exact iff_of_true (mul_pos (side_pos _) (inv_pos.mpr p.len_pos)) hh - · simp [hh] - -lemma avg_ex {p : Node} (h : p.ok M) (f : Grid M → ℝ) : - p.avg f = (p.dice M).Ex f := by - simp_rw [avg,smul_eq_mul,expU_ex,Ex,dice_apply h,uniform_side,Finset.mul_sum] - refine Finset.sum_congr rfl fun i _ => ?_ - split_ifs with hi - · rw [Set.indicator_of_mem hi]; ring - rw [Set.indicator_of_notMem hi]; ring - -lemma avg_cx {p : Node} (h : p.ok M) (f : Grid M → ℂ) : - p.avg f = (p.dice M).Cx f := by - simp_rw [avg,Complex.real_smul,expU_cx,Cx,dice_apply h,uniform_side,Finset.mul_sum] - refine Finset.sum_congr rfl fun i _ => ?_ - split_ifs with hi - · rw [Set.indicator_of_mem hi]; push_cast; ring - rw [Set.indicator_of_notMem hi]; push_cast; ring - -lemma dice_child (p : Node) (hv : p.valid) (h : p.lev < M) (f : Grid M → ℝ) : - (p.dice M).Ex f = ((p.L.dice M).Ex f + (p.R.dice M).Ex f)/2 := by - have H : ok p M := ⟨hv,h.le⟩ - have H1 : ok p.L M := ⟨p.valid_L.mpr hv,h⟩ - have H2 : ok p.R M := ⟨p.valid_R.mpr hv,h⟩ - rw [← avg_ex H,← avg_ex H1,← avg_ex H2] - exact p.avg_split_real _ - -/-- Called branching. -/ -def below (p q : Node) : Prop := p.lev ≥ q.lev ∧ p.cells ⊆ q.cells - -lemma below_left (p : Node) : p.L.below p := by - refine ⟨Nat.le_add_right _ 1,?_⟩ - rw [cells_split p]; exact subset_union_left -lemma below_right (p : Node) : p.R.below p := by - refine ⟨Nat.le_add_right _ 1,?_⟩ - rw [cells_split p]; exact subset_union_right - -lemma below_refl (p : Node) : p.below p := ⟨le_rfl,Subset.rfl⟩ - -lemma below_trans {p q r : Node} (h : p.below q) (h' : q.below r) : p.below r := - ⟨h'.1.trans h.1,h.2.trans h'.2⟩ - -lemma block_of_below {p q : Node} (h : p.below q) (M : ℕ) : p.block M ⊆ q.block M := - Set.preimage_mono h.2 - -lemma scale_below {p q : Node} (h : p.below q) : q.invlen ≤ p.invlen := - pow_le_pow_right₀ (by norm_num) h.1 - -lemma diam_block {p : Node} {x y : Grid M} (hx : x ∈ p.block M) (hy : y ∈ p.block M) : - |pos x-pos y| ≤ p.len := by rw [abs_le]; constructor <;> linarith [hx.1,hx.2,hy.1,hy.2] - -end Node -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Circle + +/-! # Finite dyadic blocks and probabilities -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real + +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Period Law +namespace Node + +variable {M : ℕ} + +def invlen (p : Node) : ℝ := 2^p.lev + +lemma rinv (p : Node) : invlen p = p.len⁻¹ := by rw [len,side_pow_two,inv_inv]; rfl + +lemma invlen_ge (p : Node) : 1 ≤ invlen p := one_le_pow₀ (by norm_num) + +lemma invlen_pos (p : Node) : 0 < invlen p := lt_of_lt_of_le zero_lt_one p.invlen_ge + +lemma descale {p : Node} (h : p.lev ≤ M) : + p.len = side M * (2:ℝ)^(M-p.lev) := by + rw [len,side_pow_two,side_pow_two] + conv_rhs => lhs; rw [show M=p.lev+(M-p.lev) by omega] + rw [pow_add]; field_simp + +lemma mem_block_iff {p : Node} (h : p.lev ≤ M) (x : Grid M) : + x ∈ p.block M ↔ p.ix*2^(M-p.lev) ≤ x.val ∧ x.val < (p.ix+1)*2^(M-p.lev) := by + change p.a ≤ pos x ∧ _ < p.a+p.len ↔ _ + have he (i : ℕ) : (i:ℝ)*p.len = (i*2^(M-p.lev) : ℕ) * side M := by + rw [descale h]; push_cast; ring + unfold a pos + rw [show p.ix*p.len+p.len=↑(p.ix+1)*p.len by push_cast; ring, he,he, + mul_le_mul_iff_left₀ (side_pos _),mul_lt_mul_iff_left₀ (side_pos _)] + norm_cast + +/-- First grid index in a fitting node. -/ +def idx (p : Node) (M : ℕ) := p.ix * 2^(M-p.lev) +def wid (p : Node) (M : ℕ) := 2^(M-p.lev) + +lemma fitting (p : Node) (hv : p.valid) (h : p.lev ≤ M) : + p.idx M+p.wid M ≤ 2^M := by + unfold idx wid + calc + _ = (p.ix+1)*2^(M-p.lev) := by ring + _ ≤ 2^p.lev*2^(M-p.lev) := Nat.mul_le_mul_right _ hv + _ = _ := by rw [← pow_add]; congr 1; omega + +lemma wid_pos (p : Node) (M : ℕ) : 0 < p.wid M := pow_pos (by omega) _ + +def start (p : Node) (M : ℕ) : Grid M := (p.idx M : Grid M) + +lemma start_val {p : Node} (hv : p.valid) (h : p.lev ≤ M) : + (p.start M).val = p.idx M := by + apply ZMod.val_natCast_of_lt + have HH := p.fitting hv h + have HH' := p.wid_pos M + omega + +lemma start_pos {p : Node} (hv : p.valid) (h : p.lev ≤ M) : + pos (p.start M) = p.a := by + rw [pos, start_val hv h,a,descale h] + unfold idx; push_cast; ring + +lemma start_mem {p : Node} (hv : p.valid) (h : p.lev ≤ M) : + p.start M ∈ p.block M := by + rw [mem_block_iff h,start_val hv h] + unfold idx + constructor + · exact le_rfl + · gcongr; omega + +open scoped Classical +/-- Finset of vertices. -/ +def group (p : Node) (M : ℕ) := (p.block M).toFinset + +lemma mem_group (p : Node) (x : Grid M) : x ∈ p.group M ↔ x ∈ p.block M := by simp [group] + +lemma group_range {p : Node} (h : p.lev ≤ M) : + p.group M = first M (p.idx M+p.wid M) \ first M (p.idx M) := by + ext i; simp only [mem_group, Finset.mem_sdiff, mem_first, mem_block_iff h] + unfold idx wid; rw [add_mul,one_mul]; omega + +lemma group_card {p : Node} (hv : p.valid) (h : p.lev ≤ M) : + (p.group M).card = p.wid M := by + have hh := p.fitting hv h + rw [group_range h,Finset.card_sdiff_of_subset ?sub,first_card _ hh,first_card _ (by omega)] + · omega + case sub => + intro x hx + simp_rw [mem_first] at * + omega + +lemma group_size {p : Node} (hv : p.valid) (h : p.lev ≤ M) : + (p.group M).card * side M = p.len := by + rw [group_card hv h,descale h] + unfold wid; push_cast; ring + +lemma block_separate (p : Node) (M : ℕ) : + Disjoint (p.L.block M) (p.R.block M) := by + refine Set.disjoint_left.mpr fun x hx hy => ?_ + exact Set.disjoint_left.mp (cells_dis (k:=p.ix*2) (l:=p.ix*2+1) (by omega) + (n:=p.lev+1)) hx hy + +lemma block_union (p : Node) (M : ℕ) : + p.block M = p.L.block M ∪ p.R.block M := by rw [block,p.cells_split]; rfl + +lemma group_dis (p : Node) (M : ℕ) : + Disjoint (p.L.group M) (p.R.group M) := + Finset.disjoint_left.mpr fun _ hx hy => Set.disjoint_left.mp (p.block_separate _) + ((mem_group _ _).mp hx) ((mem_group _ _).mp hy) + +lemma group_union (p : Node) (M : ℕ) : + p.group M = p.L.group M ∪ p.R.group M := by ext x; simp only [Finset.mem_union,mem_group]; rw [p.block_union]; rfl + +def ok (p : Node) (M : ℕ) : Prop := p.valid ∧ p.lev ≤ M + +lemma root_ok (M : ℕ) : ok root M := by simp [ok,valid,root] +lemma len_top : root.len = 1 := by simp [len,root,side] +lemma invlen_top : root.invlen = 1 := by simp [invlen,root] +lemma group_top (M : ℕ) : root.group M = Finset.univ := by + ext x; rw [mem_group,mem_block_iff (root_ok M).2] + simp only [root, Nat.zero_mul, Nat.zero_le, true_and, zero_add,one_mul,Nat.sub_zero] + simp [ZMod.val_lt] + +open scoped Classical + +/-- Local averages as sums. -/ +lemma avg_sum {E : Type*} [AddCommMonoid E] [SMul ℝ E] (p : Node) + (f : Grid M → E) : + p.avg f = p.len⁻¹ • (side M • ∑ x ∈ p.group M, f x) := by + unfold avg expU + congr 2 + calc + _ = ∑ x ∈ p.group M, (p.block M).indicator f x := by + refine (Finset.sum_subset (Finset.subset_univ _) ?_).symm + intro i _ hi + apply Set.indicator_of_notMem ((mem_group _ _).not.mp hi) f + _ = _ := Finset.sum_congr rfl fun i hi => Set.indicator_of_mem ((mem_group _ _).mp hi) f + +lemma avg_nonneg (p : Node) {f : Grid M → ℝ} (h : ∀ x, x∈p.block M → 0 ≤ f x) : + 0 ≤ p.avg f := by + rw [avg_sum, smul_eq_mul, smul_eq_mul] + exact mul_nonneg (inv_nonneg.mpr p.len_pos.le) <| mul_nonneg (side_pos _).le + (Finset.sum_nonneg fun i hi => h _ ((mem_group _ _).mp hi)) + +lemma avg_split_real (p : Node) (f : Grid M → ℝ) : + p.avg f = (p.L.avg f+p.R.avg f)/2 := by + rw [avg_sum,avg_sum,avg_sum,p.group_union,Finset.sum_union (p.group_dis M),p.len_L,p.len_R]; simp; ring + +lemma avg_split_complex (p : Node) (f : Grid M → ℂ) : + p.avg f = (p.L.avg f+p.R.avg f)/2 := by + rw [avg_sum,avg_sum,avg_sum,p.group_union,Finset.sum_union (p.group_dis M),p.len_L,p.len_R] + simp_rw [Complex.real_smul]; push_cast; ring_nf + +/-- Uniform conditional law on a block (total default law if not fitting). -/ +def dice (p : Node) (M : ℕ) : Law (Grid M) := (uniform _).condP fun x => x ∈ p.block M + +lemma prob_block {p : Node} (h : p.ok M) : + (uniform (Grid M)).prob (fun x => x∈p.block M) = p.len := by + rw [prob_eq] + have He : Finset.univ.filter (fun x : Grid M => x∈p.block M)=p.group M := by + ext x; simp [mem_group] + rw [← Finset.sum_filter, He] + simp_rw [uniform_side,Finset.sum_const,nsmul_eq_mul,group_size h.1 h.2] + +lemma dice_apply {p : Node} (h : p.ok M) (x : Grid M) : + p.dice M x = if x∈p.block M then side M * p.len⁻¹ else 0 := by + unfold dice + rw [condP_pos_apply _ _ (by rw [prob_block h]; exact p.len_pos),prob_block h,uniform_side] + split_ifs <;> simp [div_eq_mul_inv] +lemma dice_positive {p : Node} (h : p.ok M) {x : Grid M} : + 0 < p.dice M x ↔ x ∈ p.block M := by + rw [dice_apply h]; split_ifs with hh + · exact iff_of_true (mul_pos (side_pos _) (inv_pos.mpr p.len_pos)) hh + · simp [hh] + +lemma avg_ex {p : Node} (h : p.ok M) (f : Grid M → ℝ) : + p.avg f = (p.dice M).Ex f := by + simp_rw [avg,smul_eq_mul,expU_ex,Ex,dice_apply h,uniform_side,Finset.mul_sum] + refine Finset.sum_congr rfl fun i _ => ?_ + split_ifs with hi + · rw [Set.indicator_of_mem hi]; ring + rw [Set.indicator_of_notMem hi]; ring + +lemma avg_cx {p : Node} (h : p.ok M) (f : Grid M → ℂ) : + p.avg f = (p.dice M).Cx f := by + simp_rw [avg,Complex.real_smul,expU_cx,Cx,dice_apply h,uniform_side,Finset.mul_sum] + refine Finset.sum_congr rfl fun i _ => ?_ + split_ifs with hi + · rw [Set.indicator_of_mem hi]; push_cast; ring + rw [Set.indicator_of_notMem hi]; push_cast; ring + +lemma dice_child (p : Node) (hv : p.valid) (h : p.lev < M) (f : Grid M → ℝ) : + (p.dice M).Ex f = ((p.L.dice M).Ex f + (p.R.dice M).Ex f)/2 := by + have H : ok p M := ⟨hv,h.le⟩ + have H1 : ok p.L M := ⟨p.valid_L.mpr hv,h⟩ + have H2 : ok p.R M := ⟨p.valid_R.mpr hv,h⟩ + rw [← avg_ex H,← avg_ex H1,← avg_ex H2] + exact p.avg_split_real _ + +/-- Called branching. -/ +def below (p q : Node) : Prop := p.lev ≥ q.lev ∧ p.cells ⊆ q.cells + +lemma below_left (p : Node) : p.L.below p := by + refine ⟨Nat.le_add_right _ 1,?_⟩ + rw [cells_split p]; exact subset_union_left +lemma below_right (p : Node) : p.R.below p := by + refine ⟨Nat.le_add_right _ 1,?_⟩ + rw [cells_split p]; exact subset_union_right + +lemma below_refl (p : Node) : p.below p := ⟨le_rfl,Subset.rfl⟩ + +lemma below_trans {p q r : Node} (h : p.below q) (h' : q.below r) : p.below r := + ⟨h'.1.trans h.1,h.2.trans h'.2⟩ + +lemma block_of_below {p q : Node} (h : p.below q) (M : ℕ) : p.block M ⊆ q.block M := + Set.preimage_mono h.2 + +lemma scale_below {p q : Node} (h : p.below q) : q.invlen ≤ p.invlen := + pow_le_pow_right₀ (by norm_num) h.1 + +lemma diam_block {p : Node} {x y : Grid M} (hx : x ∈ p.block M) (hy : y ∈ p.block M) : + |pos x-pos y| ≤ p.len := by rw [abs_le]; constructor <;> linarith [hx.1,hx.2,hy.1,hy.2] + +end Node +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Snap.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Snap.lean index 0c6ebc9a838da5a0018b7cc95c1632f21ceeb917..0c42e297d331872d1fb1921918dffbb52fdf4697 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Snap.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Snap.lean @@ -1,163 +1,163 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Node - -/-! # Subgrids, snapping and projecting -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Law Node Period Real - -variable {M l : ℕ} - -private def times_step (l M : ℕ) : ℤ →+ Grid M where - toFun x := ↑((2^(M-l):ℕ)*(x:ℤ)) - map_zero' := by simp - map_add' x y := by push_cast; ring - -/-- Mesh 2^(M-l) in multiples. -/ -def raise (h : l ≤ M) : Grid l →+ Grid M := - ZMod.lift (2^l) ⟨times_step l M,by - change (((↑(2^(M-l):ℕ):ℤ) * ↑(2^l:ℕ) : ℤ):Grid M)=0 - rw [← Nat.cast_mul,← pow_add,show M-l+l=M by omega] - rw [Int.cast_natCast,ZMod.natCast_self] - ⟩ - -def cut (h : l ≤ M) : Grid M →+* Grid l := - ZMod.castHom (pow_dvd_pow _ h) _ - -lemma cut_natCast (h : l ≤ M) (k : ℕ) : - cut h (k:Grid M) = (k:Grid l) := map_natCast _ _ - -lemma cut_val (h : l ≤ M) (x : Grid M) : - cut h x = (x.val:Grid l) := by rw [← cut_natCast h,ZMod.natCast_zmod_val] - -lemma cut_mod (h : l ≤ M) (x : Grid M) : - (cut h x).val = x.val % 2^l := by rw [cut_val,ZMod.val_natCast] - -lemma raise_cast (h : l ≤ M) (x : ℕ) : - raise h (x : Grid l) = (↑(2^(M-l)*x:ℕ):Grid M) := by - have H : ∀ k : ℤ, raise h (k:Grid l) = times_step l M k := fun k => ZMod.lift_coe _ _ _ - convert H x using 1 <;> simp [times_step] - -lemma raise_eq (h : l ≤ M) (x : Grid l) : - raise h x = ↑(2^(M-l)*x.val:ℕ) := by rw [← raise_cast h,ZMod.natCast_zmod_val] - -lemma raise_val (h : l ≤ M) (x : Grid l) : - (raise h x).val = 2^(M-l)*x.val := by - rw [raise_eq,ZMod.val_natCast_of_lt] - calc - _ < 2^(M-l)*2^l := Nat.mul_lt_mul_of_pos_left (ZMod.val_lt _) (by positivity) - _ = _ := by rw [← pow_add]; congr 1; omega - -lemma raise_inj (h : l ≤ M) : Injective (raise h) := by - intro x y he - apply ZMod.val_injective _ - have H := congrArg ZMod.val he - simp only [raise_val] at H - exact mul_left_cancel₀ (by positivity) H - -lemma raised_pos (h : l ≤ M) (i : Grid l) : pos (raise h i) = pos i := by - simp only [pos,raise_val] - have heq : side l = side M*(2:ℝ)^(M-l) := descale (p:=⟨l,0⟩) h - rw [heq]; push_cast; ring - -lemma cut_chi (h : l ≤ M) (i : Grid l) (x : Grid M) : - chi (raise h i) x = chi i (cut h x) := by - rw [cut_val,chi_comm i _,chi_comm (raise h i) x] - have heq (x : ℕ) : chi (x:Grid M) (raise h i) = chi (x:Grid l) i := by - have H := chi_cast (x:ℤ) (raise h i) - have hh := chi_cast (x:ℤ) i - rw [raised_pos] at H - simpa only [Int.cast_natCast] using H.trans hh.symm - rw [← heq, ZMod.natCast_zmod_val x] - -lemma annihilator (h : l ≤ M) (u : Grid M) : - cut (show M-l ≤ M by omega) u = 0 ↔ ∃ i, raise h i = u := by - let p := cut (show M-l ≤ M by omega) - have he (u : Grid M) : p u = 0 ↔ 2^(M-l) ∣ u.val := by - rw [← (ZMod.val_injective (2^(M-l))).eq_iff] - simp only [p,cut_mod,ZMod.val_zero,Nat.dvd_iff_mod_eq_zero] - constructor - · intro hu - obtain ⟨i,hi⟩ := (he _).mp hu - refine ⟨(i:Grid l),?_⟩ - rw [raise_cast,← hi,ZMod.natCast_zmod_val] - rintro ⟨i,rfl⟩ - exact (he _).mpr (by rw [raise_val]; simp) - -/-- A snap (rounding down). -/ -def down (_h : l ≤ M) (u : Grid M) : Grid l := - ↑(u.val / 2^(M-l)) - -lemma raise_down (h : l ≤ M) (u : Grid M) : - u - raise h (down h u) = (↑(u.val % 2^(M-l)):Grid M) := by - have hh := Nat.mod_add_div u.val (2^(M-l)) - unfold down; rw [raise_cast] - apply sub_eq_iff_eq_add.mpr; rw [← Nat.cast_add,hh,ZMod.natCast_zmod_val] - -lemma snap_dist (h : l ≤ M) (u : Grid M) : - rad (u-raise h (down h u)) ≤ 2^(M-l)-1 := by - rw [raise_down] - apply le_trans (rad_le_nat _) - have H : u.val % 2^(M-l) +1 ≤ 2^(M-l) := Nat.mod_lt _ (by positivity) - apply le_sub_iff_add_le.mpr - exact_mod_cast H - -lemma cut_injOn {p : Node} (h : l ≤ M) (hp : p.lev ≤ M) (hh : M-l ≤ p.lev) : - InjOn (cut h) (p.block M) := by - intro x hx y hy hxy - apply ZMod.val_injective _ - have HE := congrArg ZMod.val hxy - simp only [cut_mod] at HE - rw [mem_block_iff hp x] at hx; rw [mem_block_iff hp y] at hy - let a := 2^(M-p.lev) - let b := 2^l - let i := p.ix - change i*a ≤ _ ∧ _ < (i+1)*a at hx hy - rw [add_mul,one_mul] at hx hy - have h1 := Nat.mod_add_div x.val b - have h2 := Nat.mod_add_div y.val b - have hi : a ≤ b := Nat.pow_le_pow_right (by omega) (by omega) - change _ % b = _ % b at HE - have h3 : x.val/b=y.val/b := by - by_contra! he - rcases lt_or_gt_of_ne he with H|H - · have Hx : b*(x.val/b +1) ≤ b*(y.val/b) := Nat.mul_le_mul_left b H - rw [mul_add,mul_one] at Hx; omega - · have Hx : b*(y.val/b +1) ≤ b*(x.val/b) := Nat.mul_le_mul_left b H - rw [mul_add,mul_one] at Hx; omega - rw [HE,h3,h2] at h1; exact h1.symm - -/-- Projection density bound for any input sublaw on p. -/ -lemma cut_send {p : Node} (h : l ≤ M) (hp : p.ok M) (hh : M-l ≤ p.lev) - (P : Law (Grid M)) (a : ℝ) (ha : 0 ≤ a) - (H : ∀ x, P x ≤ a*p.dice M x) (i : Grid l) : - P.send (cut h) i ≤ a*p.invlen*side M := by - classical - rcases eq_or_lt_of_le ((P.send (cut h)).nonneg i) with H0|H0 - · rw [← H0] - have := p.invlen_pos; have := side_pos M; positivity - obtain ⟨x,hx,hx'⟩ := (P.send_pos_iff _ _).mp H0 - have he {x : Grid M} (hh : 0 < P x) : x∈p.block M := by - have H := lt_of_lt_of_le hh (H x) - rw [dice_apply hp] at H - by_contra h' - rw [ite_eq_right h',mul_zero] at H - exact lt_irrefl _ H - rw [send_apply, prob_eq] - have HX := H x - rw [dice_apply hp,ite_eq_left (he hx'), p.rinv] at * - rw [Finset.sum_eq_single x,ite_eq_left hx,mul_right_comm] - · nlinarith - · intro j _ hj; split_ifs with hi - · by_contra hh' - have h2 : j∈p.block M := he ((P.nonneg j).lt_of_ne (Ne.symm hh')) - exact hj (cut_injOn h hp.2 hh h2 (he hx') (hi.trans hx.symm)) - rfl - simp - -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Node + +/-! # Subgrids, snapping and projecting -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Law Node Period Real + +variable {M l : ℕ} + +private def times_step (l M : ℕ) : ℤ →+ Grid M where + toFun x := ↑((2^(M-l):ℕ)*(x:ℤ)) + map_zero' := by simp + map_add' x y := by push_cast; ring + +/-- Mesh 2^(M-l) in multiples. -/ +def raise (h : l ≤ M) : Grid l →+ Grid M := + ZMod.lift (2^l) ⟨times_step l M,by + change (((↑(2^(M-l):ℕ):ℤ) * ↑(2^l:ℕ) : ℤ):Grid M)=0 + rw [← Nat.cast_mul,← pow_add,show M-l+l=M by omega] + rw [Int.cast_natCast,ZMod.natCast_self] + ⟩ + +def cut (h : l ≤ M) : Grid M →+* Grid l := + ZMod.castHom (pow_dvd_pow _ h) _ + +lemma cut_natCast (h : l ≤ M) (k : ℕ) : + cut h (k:Grid M) = (k:Grid l) := map_natCast _ _ + +lemma cut_val (h : l ≤ M) (x : Grid M) : + cut h x = (x.val:Grid l) := by rw [← cut_natCast h,ZMod.natCast_zmod_val] + +lemma cut_mod (h : l ≤ M) (x : Grid M) : + (cut h x).val = x.val % 2^l := by rw [cut_val,ZMod.val_natCast] + +lemma raise_cast (h : l ≤ M) (x : ℕ) : + raise h (x : Grid l) = (↑(2^(M-l)*x:ℕ):Grid M) := by + have H : ∀ k : ℤ, raise h (k:Grid l) = times_step l M k := fun k => ZMod.lift_coe _ _ _ + convert H x using 1 <;> simp [times_step] + +lemma raise_eq (h : l ≤ M) (x : Grid l) : + raise h x = ↑(2^(M-l)*x.val:ℕ) := by rw [← raise_cast h,ZMod.natCast_zmod_val] + +lemma raise_val (h : l ≤ M) (x : Grid l) : + (raise h x).val = 2^(M-l)*x.val := by + rw [raise_eq,ZMod.val_natCast_of_lt] + calc + _ < 2^(M-l)*2^l := Nat.mul_lt_mul_of_pos_left (ZMod.val_lt _) (by positivity) + _ = _ := by rw [← pow_add]; congr 1; omega + +lemma raise_inj (h : l ≤ M) : Injective (raise h) := by + intro x y he + apply ZMod.val_injective _ + have H := congrArg ZMod.val he + simp only [raise_val] at H + exact mul_left_cancel₀ (by positivity) H + +lemma raised_pos (h : l ≤ M) (i : Grid l) : pos (raise h i) = pos i := by + simp only [pos,raise_val] + have heq : side l = side M*(2:ℝ)^(M-l) := descale (p:=⟨l,0⟩) h + rw [heq]; push_cast; ring + +lemma cut_chi (h : l ≤ M) (i : Grid l) (x : Grid M) : + chi (raise h i) x = chi i (cut h x) := by + rw [cut_val,chi_comm i _,chi_comm (raise h i) x] + have heq (x : ℕ) : chi (x:Grid M) (raise h i) = chi (x:Grid l) i := by + have H := chi_cast (x:ℤ) (raise h i) + have hh := chi_cast (x:ℤ) i + rw [raised_pos] at H + simpa only [Int.cast_natCast] using H.trans hh.symm + rw [← heq, ZMod.natCast_zmod_val x] + +lemma annihilator (h : l ≤ M) (u : Grid M) : + cut (show M-l ≤ M by omega) u = 0 ↔ ∃ i, raise h i = u := by + let p := cut (show M-l ≤ M by omega) + have he (u : Grid M) : p u = 0 ↔ 2^(M-l) ∣ u.val := by + rw [← (ZMod.val_injective (2^(M-l))).eq_iff] + simp only [p,cut_mod,ZMod.val_zero,Nat.dvd_iff_mod_eq_zero] + constructor + · intro hu + obtain ⟨i,hi⟩ := (he _).mp hu + refine ⟨(i:Grid l),?_⟩ + rw [raise_cast,← hi,ZMod.natCast_zmod_val] + rintro ⟨i,rfl⟩ + exact (he _).mpr (by rw [raise_val]; simp) + +/-- A snap (rounding down). -/ +def down (_h : l ≤ M) (u : Grid M) : Grid l := + ↑(u.val / 2^(M-l)) + +lemma raise_down (h : l ≤ M) (u : Grid M) : + u - raise h (down h u) = (↑(u.val % 2^(M-l)):Grid M) := by + have hh := Nat.mod_add_div u.val (2^(M-l)) + unfold down; rw [raise_cast] + apply sub_eq_iff_eq_add.mpr; rw [← Nat.cast_add,hh,ZMod.natCast_zmod_val] + +lemma snap_dist (h : l ≤ M) (u : Grid M) : + rad (u-raise h (down h u)) ≤ 2^(M-l)-1 := by + rw [raise_down] + apply le_trans (rad_le_nat _) + have H : u.val % 2^(M-l) +1 ≤ 2^(M-l) := Nat.mod_lt _ (by positivity) + apply le_sub_iff_add_le.mpr + exact_mod_cast H + +lemma cut_injOn {p : Node} (h : l ≤ M) (hp : p.lev ≤ M) (hh : M-l ≤ p.lev) : + InjOn (cut h) (p.block M) := by + intro x hx y hy hxy + apply ZMod.val_injective _ + have HE := congrArg ZMod.val hxy + simp only [cut_mod] at HE + rw [mem_block_iff hp x] at hx; rw [mem_block_iff hp y] at hy + let a := 2^(M-p.lev) + let b := 2^l + let i := p.ix + change i*a ≤ _ ∧ _ < (i+1)*a at hx hy + rw [add_mul,one_mul] at hx hy + have h1 := Nat.mod_add_div x.val b + have h2 := Nat.mod_add_div y.val b + have hi : a ≤ b := Nat.pow_le_pow_right (by omega) (by omega) + change _ % b = _ % b at HE + have h3 : x.val/b=y.val/b := by + by_contra! he + rcases lt_or_gt_of_ne he with H|H + · have Hx : b*(x.val/b +1) ≤ b*(y.val/b) := Nat.mul_le_mul_left b H + rw [mul_add,mul_one] at Hx; omega + · have Hx : b*(y.val/b +1) ≤ b*(x.val/b) := Nat.mul_le_mul_left b H + rw [mul_add,mul_one] at Hx; omega + rw [HE,h3,h2] at h1; exact h1.symm + +/-- Projection density bound for any input sublaw on p. -/ +lemma cut_send {p : Node} (h : l ≤ M) (hp : p.ok M) (hh : M-l ≤ p.lev) + (P : Law (Grid M)) (a : ℝ) (ha : 0 ≤ a) + (H : ∀ x, P x ≤ a*p.dice M x) (i : Grid l) : + P.send (cut h) i ≤ a*p.invlen*side M := by + classical + rcases eq_or_lt_of_le ((P.send (cut h)).nonneg i) with H0|H0 + · rw [← H0] + have := p.invlen_pos; have := side_pos M; positivity + obtain ⟨x,hx,hx'⟩ := (P.send_pos_iff _ _).mp H0 + have he {x : Grid M} (hh : 0 < P x) : x∈p.block M := by + have H := lt_of_lt_of_le hh (H x) + rw [dice_apply hp] at H + by_contra h' + rw [ite_eq_right h',mul_zero] at H + exact lt_irrefl _ H + rw [send_apply, prob_eq] + have HX := H x + rw [dice_apply hp,ite_eq_left (he hx'), p.rinv] at * + rw [Finset.sum_eq_single x,ite_eq_left hx,mul_right_comm] + · nlinarith + · intro j _ hj; split_ifs with hi + · by_contra hh' + have h2 : j∈p.block M := he ((P.nonneg j).lt_of_ne (Ne.symm hh')) + exact hj (cut_injOn h hp.2 hh h2 (he hx') (hi.trans hx.symm)) + rfl + simp + +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Dyadic/Span.lean b/lean/OAI/Analysis/FourierLLogL/Dyadic/Span.lean index de455f90228a34a67ca8486bd5422665e54e104a..d24fe6a3e565a69cca742c1d7e6125aa09047cb2 100644 --- a/lean/OAI/Analysis/FourierLLogL/Dyadic/Span.lean +++ b/lean/OAI/Analysis/FourierLLogL/Dyadic/Span.lean @@ -1,137 +1,137 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Discrete - -/-! # Signed combination spans and Fourier support -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Law Period Real -variable {M : ℕ} - -def enclosed (s : Finset (Grid M)) (n : ℕ) (x : Grid M) : Prop := - ∃ f : Grid M → ℤ, (∀ i∈s, |f i| ≤ n) ∧ ∑ i∈s, f i • i = x - -lemma enclosed_zero (s : Finset (Grid M)) (n : ℕ) : enclosed s n 0 := - ⟨fun _ => 0,by simp,by simp⟩ - -lemma enclosed_mem (s : Finset (Grid M)) {x : Grid M} (hx : x∈s) : enclosed s 1 x := by - refine ⟨fun i => if i=x then 1 else 0,fun i hi => ?_,?_⟩ - · dsimp only; split_ifs <;> norm_num - simp [ite_smul,hx] - -lemma enclosed_neg {s : Finset (Grid M)} {n : ℕ} {x : Grid M} (h : enclosed s n x) : - enclosed s n (-x) := by - obtain ⟨f,hf,rfl⟩ := h - refine ⟨fun i => -f i,fun i hi => ?_,?_⟩ - · simpa only [abs_neg] using hf i hi - simp - -lemma enclosed_add {s : Finset (Grid M)} {n m : ℕ} {x y : Grid M} (h : enclosed s n x) - (h' : enclosed s m y) : enclosed s (n+m) (x+y) := by - obtain ⟨f,hf,rfl⟩ := h - obtain ⟨g,hg,rfl⟩ := h' - refine ⟨fun i => f i + g i,fun i hi => ?_,?_⟩ - · rw [Nat.cast_add] - exact (abs_add_le _ _).trans (add_le_add (hf _ hi) (hg _ hi)) - simp [add_smul,Finset.sum_add_distrib] - -lemma enclosed_insert {s : Finset (Grid M)} {i x : Grid M} (hi : i ∉ s) - {n : ℕ} (h : enclosed s n x) (j : ℤ) (hj : |j| ≤ n) : - enclosed (insert i s) n (x+j•i) := by - obtain ⟨f,hf,rfl⟩ := h - refine ⟨fun k => if k=i then j else f k,?_,?_⟩ - · intro k hk - dsimp only - split_ifs with h - · exact hj - · exact hf _ ((Finset.mem_insert.mp hk).resolve_left h) - · rw [Finset.sum_insert hi]; dsimp only; rw [ite_eq_left rfl,add_comm _ (j • i)] - congr 1 - exact Finset.sum_congr rfl fun k hk => by rw [ite_eq_right (by intro he; exact hi (he ▸ hk))] - -lemma enclosed_sum {α : Type*} [Fintype α] (s : Finset (Grid M)) (X : α → Grid M) - (h : ∀ x, enclosed s 1 (X x)) : - enclosed s (Fintype.card α) (∑ i, X i) := by - classical - suffices HH : ∀ t : Finset α, enclosed s t.card (∑ i∈t, X i) from HH _ - intro t; induction t using Finset.induction_on with - | empty => simp [enclosed_zero] - | @insert i t hi ih => - rw [Finset.sum_insert hi,Finset.card_insert_of_notMem hi,add_comm t.card] - exact enclosed_add (h _) ih - -open scoped Classical - -def palace (s : Finset (Grid M)) (n : ℕ) := Finset.univ.filter (enclosed s n) - -lemma palace_size (s : Finset (Grid M)) (n : ℕ) : (palace s n).card ≤ (2*n+1)^s.card := by - classical - let t : Finset ℤ := Finset.Icc (-(n:ℤ)) n - let f (w : s → (t:Type)) := ∑ i : s, (w i).val • (i:Grid M) - have hs : palace s n ⊆ Finset.univ.image f := by - intro i hi - obtain ⟨g,hg,he⟩ := (Finset.mem_filter.mp hi).2 - let w (i : s) : t := ⟨g i, Finset.mem_Icc.mpr (abs_le.mp (hg _ i.property))⟩ - refine Finset.mem_image.mpr ⟨w,Finset.mem_univ _,?_⟩ - rw [← he,← Finset.sum_attach s] - simp only [f,w]; rfl - apply le_trans ((Finset.card_le_card hs).trans Finset.card_image_le) - simp only [Finset.card_univ, Fintype.card_fun,Fintype.card_coe] - have He : t.card = 2*n+1 := by simp [t]; omega - rw [He] - -/-- The Fourier coefficients are supported within a bandwidth of the signed span. This -form (arbitrary thickening of our unthickened case) will also be used for broad capture. -/ -def spanned (s : Finset (Grid M)) (f : Grid M → ℂ) : Prop := - ∀ i, ¬enclosed s 1 i → fc f i=0 - -lemma spanned_one (s : Finset (Grid M)) : spanned s 1 := by - intro i hi - have hn : i≠0 := fun he => hi (he ▸ enclosed_zero s 1) - change fc (fun x => 1) i = 0 - rw [← funext chi_zero_left,fc_chi,ite_eq_right hn.symm] - -private lemma fc_real_step (f : Grid M → ℂ) (c : ℂ) (s i : Grid M) : - fc (fun x => f x * (↑((c*chi s x).re):ℂ)) i = - 2⁻¹*(c*fc f (i-s)+conj c*fc f (i+s)) := by - have he (x : Grid M) : - f x * (↑((c*chi s x).re):ℂ) = 2⁻¹ * - (c*(f x*chi s x) + (conj c)*(f x * chi (-s) x)) := by - rw [Complex.re_eq_add_conj,map_mul,chi_neg'] - ring - simp_rw [he]; rw [fc_lmul,fc_add, fc_lmul,fc_lmul, fc_mul_chi,fc_mul_chi] - rw [sub_neg_eq_add] - -lemma expand_one_step (f : Grid M → ℂ) (c : ℂ) (s i : Grid M) : - fc (fun x => f x * (1 + (↑((c*chi s x).re):ℂ))) i = - fc f i + 2⁻¹*(c*fc f (i-s)+conj c*fc f (i+s)) := by - simp_rw [mul_add,mul_one,fc_add,fc_real_step] - ring - -lemma span_augment {f : Grid M → ℂ} {u : Finset (Grid M)} (h : spanned u f) - (c : ℂ) (s : Grid M) (hs : ¬ enclosed u 1 s) : - let g := fun x : Grid M => f x * (1+(↑((c*chi s x).re):ℂ)) - spanned (insert s u) g ∧ fc g 0 = fc f 0 := by - have hx : s ∉ u := fun h => hs (enclosed_mem _ h) - intro g - have ha (i : Grid M) := expand_one_step f c s i - constructor - · intro i hi - have hA : ¬ enclosed u 1 i := by - intro he - apply hi; simpa using enclosed_insert hx he 0 (by norm_num) - have hB : ¬ enclosed u 1 (i-s) := by - intro he - apply hi; simpa using enclosed_insert hx he 1 (by norm_num) - have hC : ¬ enclosed u 1 (i+s) := by - intro he - apply hi; simpa using enclosed_insert hx he (-1) (by norm_num) - rw [ha i,h _ hA,h _ hB,h _ hC]; ring - · have hs' : ¬ enclosed u 1 (-s) := fun he => hs (by simpa using enclosed_neg he) - rw [ha 0, zero_add, zero_sub,h _ hs,h _ hs']; ring - -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Discrete + +/-! # Signed combination spans and Fourier support -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Law Period Real +variable {M : ℕ} + +def enclosed (s : Finset (Grid M)) (n : ℕ) (x : Grid M) : Prop := + ∃ f : Grid M → ℤ, (∀ i∈s, |f i| ≤ n) ∧ ∑ i∈s, f i • i = x + +lemma enclosed_zero (s : Finset (Grid M)) (n : ℕ) : enclosed s n 0 := + ⟨fun _ => 0,by simp,by simp⟩ + +lemma enclosed_mem (s : Finset (Grid M)) {x : Grid M} (hx : x∈s) : enclosed s 1 x := by + refine ⟨fun i => if i=x then 1 else 0,fun i hi => ?_,?_⟩ + · dsimp only; split_ifs <;> norm_num + simp [ite_smul,hx] + +lemma enclosed_neg {s : Finset (Grid M)} {n : ℕ} {x : Grid M} (h : enclosed s n x) : + enclosed s n (-x) := by + obtain ⟨f,hf,rfl⟩ := h + refine ⟨fun i => -f i,fun i hi => ?_,?_⟩ + · simpa only [abs_neg] using hf i hi + simp + +lemma enclosed_add {s : Finset (Grid M)} {n m : ℕ} {x y : Grid M} (h : enclosed s n x) + (h' : enclosed s m y) : enclosed s (n+m) (x+y) := by + obtain ⟨f,hf,rfl⟩ := h + obtain ⟨g,hg,rfl⟩ := h' + refine ⟨fun i => f i + g i,fun i hi => ?_,?_⟩ + · rw [Nat.cast_add] + exact (abs_add_le _ _).trans (add_le_add (hf _ hi) (hg _ hi)) + simp [add_smul,Finset.sum_add_distrib] + +lemma enclosed_insert {s : Finset (Grid M)} {i x : Grid M} (hi : i ∉ s) + {n : ℕ} (h : enclosed s n x) (j : ℤ) (hj : |j| ≤ n) : + enclosed (insert i s) n (x+j•i) := by + obtain ⟨f,hf,rfl⟩ := h + refine ⟨fun k => if k=i then j else f k,?_,?_⟩ + · intro k hk + dsimp only + split_ifs with h + · exact hj + · exact hf _ ((Finset.mem_insert.mp hk).resolve_left h) + · rw [Finset.sum_insert hi]; dsimp only; rw [ite_eq_left rfl,add_comm _ (j • i)] + congr 1 + exact Finset.sum_congr rfl fun k hk => by rw [ite_eq_right (by intro he; exact hi (he ▸ hk))] + +lemma enclosed_sum {α : Type*} [Fintype α] (s : Finset (Grid M)) (X : α → Grid M) + (h : ∀ x, enclosed s 1 (X x)) : + enclosed s (Fintype.card α) (∑ i, X i) := by + classical + suffices HH : ∀ t : Finset α, enclosed s t.card (∑ i∈t, X i) from HH _ + intro t; induction t using Finset.induction_on with + | empty => simp [enclosed_zero] + | @insert i t hi ih => + rw [Finset.sum_insert hi,Finset.card_insert_of_notMem hi,add_comm t.card] + exact enclosed_add (h _) ih + +open scoped Classical + +def palace (s : Finset (Grid M)) (n : ℕ) := Finset.univ.filter (enclosed s n) + +lemma palace_size (s : Finset (Grid M)) (n : ℕ) : (palace s n).card ≤ (2*n+1)^s.card := by + classical + let t : Finset ℤ := Finset.Icc (-(n:ℤ)) n + let f (w : s → (t:Type)) := ∑ i : s, (w i).val • (i:Grid M) + have hs : palace s n ⊆ Finset.univ.image f := by + intro i hi + obtain ⟨g,hg,he⟩ := (Finset.mem_filter.mp hi).2 + let w (i : s) : t := ⟨g i, Finset.mem_Icc.mpr (abs_le.mp (hg _ i.property))⟩ + refine Finset.mem_image.mpr ⟨w,Finset.mem_univ _,?_⟩ + rw [← he,← Finset.sum_attach s] + simp only [f,w]; rfl + apply le_trans ((Finset.card_le_card hs).trans Finset.card_image_le) + simp only [Finset.card_univ, Fintype.card_fun,Fintype.card_coe] + have He : t.card = 2*n+1 := by simp [t]; omega + rw [He] + +/-- The Fourier coefficients are supported within a bandwidth of the signed span. This +form (arbitrary thickening of our unthickened case) will also be used for broad capture. -/ +def spanned (s : Finset (Grid M)) (f : Grid M → ℂ) : Prop := + ∀ i, ¬enclosed s 1 i → fc f i=0 + +lemma spanned_one (s : Finset (Grid M)) : spanned s 1 := by + intro i hi + have hn : i≠0 := fun he => hi (he ▸ enclosed_zero s 1) + change fc (fun x => 1) i = 0 + rw [← funext chi_zero_left,fc_chi,ite_eq_right hn.symm] + +private lemma fc_real_step (f : Grid M → ℂ) (c : ℂ) (s i : Grid M) : + fc (fun x => f x * (↑((c*chi s x).re):ℂ)) i = + 2⁻¹*(c*fc f (i-s)+conj c*fc f (i+s)) := by + have he (x : Grid M) : + f x * (↑((c*chi s x).re):ℂ) = 2⁻¹ * + (c*(f x*chi s x) + (conj c)*(f x * chi (-s) x)) := by + rw [Complex.re_eq_add_conj,map_mul,chi_neg'] + ring + simp_rw [he]; rw [fc_lmul,fc_add, fc_lmul,fc_lmul, fc_mul_chi,fc_mul_chi] + rw [sub_neg_eq_add] + +lemma expand_one_step (f : Grid M → ℂ) (c : ℂ) (s i : Grid M) : + fc (fun x => f x * (1 + (↑((c*chi s x).re):ℂ))) i = + fc f i + 2⁻¹*(c*fc f (i-s)+conj c*fc f (i+s)) := by + simp_rw [mul_add,mul_one,fc_add,fc_real_step] + ring + +lemma span_augment {f : Grid M → ℂ} {u : Finset (Grid M)} (h : spanned u f) + (c : ℂ) (s : Grid M) (hs : ¬ enclosed u 1 s) : + let g := fun x : Grid M => f x * (1+(↑((c*chi s x).re):ℂ)) + spanned (insert s u) g ∧ fc g 0 = fc f 0 := by + have hx : s ∉ u := fun h => hs (enclosed_mem _ h) + intro g + have ha (i : Grid M) := expand_one_step f c s i + constructor + · intro i hi + have hA : ¬ enclosed u 1 i := by + intro he + apply hi; simpa using enclosed_insert hx he 0 (by norm_num) + have hB : ¬ enclosed u 1 (i-s) := by + intro he + apply hi; simpa using enclosed_insert hx he 1 (by norm_num) + have hC : ¬ enclosed u 1 (i+s) := by + intro he + apply hi; simpa using enclosed_insert hx he (-1) (by norm_num) + rw [ha i,h _ hA,h _ hB,h _ hC]; ring + · have hs' : ¬ enclosed u 1 (-s) := fun he => hs (by simpa using enclosed_neg he) + rw [ha 0, zero_add, zero_sub,h _ hs,h _ hs']; ring + +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Charge.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Charge.lean index 033e5af843236b84936d59a9032a880587eef874..116a033710eae7f93450c9c9aafc16bda0ecd998 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Charge.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Charge.lean @@ -1,95 +1,95 @@ -import OAI.Analysis.FourierLLogL.Edges.Intervals - -/-! # The unlabelled entropy charge inequality -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Wire -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse -variable {M : ℕ} {s : Scene M} -open scoped Classical - -def toll (p : Policy M) (main : Bool) (e : Pulse M) := - if e.active = true ∧ ¬ enter e.scene then - e.scene.norm/e.scene.mass * surcharge e.scene main (p e.scene) else 0 - -private lemma mark_total {p : Policy M} (main : Bool) (w : Wire M s) {a : Bool} - (h : w.Valid a) (hm : 0 < s.mass) : - w.expenditure (toll p main) ≤ 2*s.actor.Ex (fun x => labels p (w.read x)) := by - induction w generalizing a with - | term => simp [expenditure,read,Ex_const,labels] - | stay s v w ih => - rw [fee_stay] - cases v <;> exact ih h.2 hm - | spot s u k ih => - have hh := h.2.2.1 hm - have h1 := spotEx u k (fun z _ => (labels p z:ℝ)) hh - rw [fee_spot _ _ _ hh,h1,← Ex_lmul] - apply Ex_mono_mem _ - exact fun b hb => ih b (h.2.2.2 _) ((true_mass hh).mp hb) - | link s v k ih => - obtain ⟨he,hh,hx,hk⟩ := h - have hf := step_true he.1.1 hh he.2 hm - let P := (step s).probs; let T := s.actor - let F (b : Bool) := (k b).expenditure (toll p main) - let L := Wire.link s v k - let Q (b : Bool) := (s.smaller b).actor - have ht (b : Bool) (hp : 0 < P b) : - F b ≤ 2*(Q b).Ex (fun x => (labels p ((k b).read x):ℝ)) := - ih b (hk _) ((true_mass hf).mp hp) - let Y (b : Bool) : Pulse M := ⟨s,v,b⟩ - let B := if v = true ∧ ¬ s.enter then s.norm/s.mass * surcharge s main (p s) else 0 - have hT (b) : toll p main (Y b) = B := rfl - let x1 (b : Bool) (i : Grid M) : ℝ := ↑(tags p (Y b) ((k b).read i)) - let x2 (b : Bool) (i : Grid M) : ℝ := ↑(labels p ((k b).read i)) - have Hx : T.Ex (fun i => (labels p (L.read i):ℝ)) = - P.Ex (fun b => (Q b).Ex (x1 b)+(Q b).Ex (x2 b)) := by - unfold T L; rw [linkEx v k (fun z _ => (labels p z:ℝ)) hf] - unfold P Q; simp only [labels,Nat.cast_add] - simp_rw [Ex_add]; rfl - have H₁ : B ≤ 2*P.Ex (fun b => (Q b).Ex (x1 b)) := by - have hp1 : 0 ≤ 2*P.Ex (fun b => (Q b).Ex (x1 b)) := by - apply mul_nonneg (by norm_num) - exact Ex_nonneg _ fun i => Ex_nonneg _ fun j => by unfold x1; positivity - dsimp only [B] - split_ifs with hv - · obtain ⟨hv,hE⟩ := hv - cases ha : decide (hazard s main (p s)) - · rw [surcharge,ite_eq_right (of_decide_eq_false ha),mul_zero]; exact hp1 - have hi := of_decide_eq_true ha - rw [surcharge,ite_eq_left hi,(hazard_rule he.1 he.2 hm hE hi).2.1] - have Hh : L.Valid a := ⟨he,hh,hx,hk⟩ - apply (mark_chance L Hh hm hE main (p s) hi).trans - refine mul_le_mul_of_nonneg_left (le_of_eq ?_) (by norm_num) - unfold prob - rw [linkEx v k (fun z i => if startMark (p s) z then (1:ℝ) else 0) hf] - unfold P Q - congr 1; funext b; refine congrArg (Q b).Ex ?_ - funext i - unfold x1 tags Y - simp [hv] - exact hp1 - rw [fee_link v k _ hf,Ex_add] - change P.Ex (fun b => toll p main (Y b))+P.Ex F ≤ _ - simp_rw [hT,Ex_const] - change _ ≤ 2*T.Ex (fun i => (labels p (L.read i):ℝ)) - rw [Hx,Ex_add,mul_add] - apply add_le_add H₁ - rw [← Ex_lmul]; exact Ex_mono_mem _ ht - -/-- All length charge or the version using trailing K. -/ -theorem edge_total (p : Policy M) (main : Bool) (t : Wire M s) {a : Bool} - (h : Valid t a) (hm : 0 < s.mass) (K : ℕ) (hk : ∀ x : Scene M, K=0 ∨ reach (p x) ≤ K) : - t.expenditure (toll p main) ≤ 128*t.expenditure (due K)+2*K := by - apply le_trans (mark_total main t h hm) - have H := s.actor.Ex_mono (mark_intervals p t h K hk) - rw [Ex_add,Ex_lmul,Ex_const] at H - unfold expenditure - linarith - -end FourierLLogL.Dyadic.Procedure.Wire - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Intervals + +/-! # The unlabelled entropy charge inequality -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Wire +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse +variable {M : ℕ} {s : Scene M} +open scoped Classical + +def toll (p : Policy M) (main : Bool) (e : Pulse M) := + if e.active = true ∧ ¬ enter e.scene then + e.scene.norm/e.scene.mass * surcharge e.scene main (p e.scene) else 0 + +private lemma mark_total {p : Policy M} (main : Bool) (w : Wire M s) {a : Bool} + (h : w.Valid a) (hm : 0 < s.mass) : + w.expenditure (toll p main) ≤ 2*s.actor.Ex (fun x => labels p (w.read x)) := by + induction w generalizing a with + | term => simp [expenditure,read,Ex_const,labels] + | stay s v w ih => + rw [fee_stay] + cases v <;> exact ih h.2 hm + | spot s u k ih => + have hh := h.2.2.1 hm + have h1 := spotEx u k (fun z _ => (labels p z:ℝ)) hh + rw [fee_spot _ _ _ hh,h1,← Ex_lmul] + apply Ex_mono_mem _ + exact fun b hb => ih b (h.2.2.2 _) ((true_mass hh).mp hb) + | link s v k ih => + obtain ⟨he,hh,hx,hk⟩ := h + have hf := step_true he.1.1 hh he.2 hm + let P := (step s).probs; let T := s.actor + let F (b : Bool) := (k b).expenditure (toll p main) + let L := Wire.link s v k + let Q (b : Bool) := (s.smaller b).actor + have ht (b : Bool) (hp : 0 < P b) : + F b ≤ 2*(Q b).Ex (fun x => (labels p ((k b).read x):ℝ)) := + ih b (hk _) ((true_mass hf).mp hp) + let Y (b : Bool) : Pulse M := ⟨s,v,b⟩ + let B := if v = true ∧ ¬ s.enter then s.norm/s.mass * surcharge s main (p s) else 0 + have hT (b) : toll p main (Y b) = B := rfl + let x1 (b : Bool) (i : Grid M) : ℝ := ↑(tags p (Y b) ((k b).read i)) + let x2 (b : Bool) (i : Grid M) : ℝ := ↑(labels p ((k b).read i)) + have Hx : T.Ex (fun i => (labels p (L.read i):ℝ)) = + P.Ex (fun b => (Q b).Ex (x1 b)+(Q b).Ex (x2 b)) := by + unfold T L; rw [linkEx v k (fun z _ => (labels p z:ℝ)) hf] + unfold P Q; simp only [labels,Nat.cast_add] + simp_rw [Ex_add]; rfl + have H₁ : B ≤ 2*P.Ex (fun b => (Q b).Ex (x1 b)) := by + have hp1 : 0 ≤ 2*P.Ex (fun b => (Q b).Ex (x1 b)) := by + apply mul_nonneg (by norm_num) + exact Ex_nonneg _ fun i => Ex_nonneg _ fun j => by unfold x1; positivity + dsimp only [B] + split_ifs with hv + · obtain ⟨hv,hE⟩ := hv + cases ha : decide (hazard s main (p s)) + · rw [surcharge,ite_eq_right (of_decide_eq_false ha),mul_zero]; exact hp1 + have hi := of_decide_eq_true ha + rw [surcharge,ite_eq_left hi,(hazard_rule he.1 he.2 hm hE hi).2.1] + have Hh : L.Valid a := ⟨he,hh,hx,hk⟩ + apply (mark_chance L Hh hm hE main (p s) hi).trans + refine mul_le_mul_of_nonneg_left (le_of_eq ?_) (by norm_num) + unfold prob + rw [linkEx v k (fun z i => if startMark (p s) z then (1:ℝ) else 0) hf] + unfold P Q + congr 1; funext b; refine congrArg (Q b).Ex ?_ + funext i + unfold x1 tags Y + simp [hv] + exact hp1 + rw [fee_link v k _ hf,Ex_add] + change P.Ex (fun b => toll p main (Y b))+P.Ex F ≤ _ + simp_rw [hT,Ex_const] + change _ ≤ 2*T.Ex (fun i => (labels p (L.read i):ℝ)) + rw [Hx,Ex_add,mul_add] + apply add_le_add H₁ + rw [← Ex_lmul]; exact Ex_mono_mem _ ht + +/-- All length charge or the version using trailing K. -/ +theorem edge_total (p : Policy M) (main : Bool) (t : Wire M s) {a : Bool} + (h : Valid t a) (hm : 0 < s.mass) (K : ℕ) (hk : ∀ x : Scene M, K=0 ∨ reach (p x) ≤ K) : + t.expenditure (toll p main) ≤ 128*t.expenditure (due K)+2*K := by + apply le_trans (mark_total main t h hm) + have H := s.actor.Ex_mono (mark_intervals p t h K hk) + rw [Ex_add,Ex_lmul,Ex_const] at H + unfold expenditure + linarith + +end FourierLLogL.Dyadic.Procedure.Wire + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Extended.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Extended.lean index e913dcd1591da156fbb51e8fde3b467f9445e9e5..c427468373d70bd7a8aa890c45a6f96da3c6628c 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Extended.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Extended.lean @@ -1,200 +1,200 @@ -import OAI.Analysis.FourierLLogL.Edges.Charge - -/-! # Full extensions and their bill -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Wire -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -/-- Additional unlabelled steps after exit, continuing with a piece frozen. -/ -def extra (s : Scene M) : ℕ → Wire M s - | 0 => term s - | n+1 => link s false fun b => extra (s.smaller b) n - -lemma extra_ok (s : Scene M) (n : ℕ) (a : Bool) (h : ReadyAt s) (hn : s.here.lev+n=M) : - Valid (extra s n) a := by - induction n generalizing s a with - | zero => exact ⟨h,hn⟩ - | succ n ih => - have hp : s.here.lev < M := by omega - refine ⟨h,hp,fun he => False.elim (by simp at he),fun b => ih _ _ ?_ ?_⟩ - · exact ⟨child_ok _ h.1.1 hp b,h.2⟩ - have hh := child_depth s.here b - change (s.here.child b).lev+n=M; omega - -/-- Total entropy cost of non-spatial branching. -/ -def spotDebt {s : Scene M} (t : Wire M s) : ℝ := match t with -| .stay _ _ k => k.spotDebt -| .spot _ u k => - u.snip.probs.entropy + u.snip.probs.Ex (fun b => (k b).spotDebt) -| .link s _ k => (step s).probs.Ex (fun b => (k b).spotDebt) -| .term _ => 0 - -lemma extraDebt (s : Scene M) (n : ℕ) : spotDebt (extra s n) = 0 := by - induction n generalizing s with - | zero => simp [extra,spotDebt] - | succ n ih => simp [extra,spotDebt,ih,Ex_const] - -lemma due_all (t : Wire M s) (a : Bool) (h : Valid t a) (hm : 0 < s.mass) : - t.expenditure (due 0) ≤ levels s - s.actor.entropy + t.spotDebt := by - let unit := fun _ : Grid M => uniform Unit - have h1 (v : Fork M) (ht : v.TrueLaw) : - v.probs.Ex (fun b => (v.dst b).actor.entropy) = - v.src.actor.entropy - v.probs.entropy := by - have he := conditioned_balance unit v ht - have hx : sideH v unit = v.probs.entropy := by - let P := v.src.actor.tensorP (uniform Unit) - let q := fun i : Grid M × Unit => v.bit i.1 - have HS : P.send q = v.probs := by - have HH := P.send_send Prod.fst v.bit - rw [send_fst_tensorP] at HH; exact HH.symm - have hh : (Prod.snd : Grid M × Unit → Unit)=fun _ => () := rfl - change P.Hcond q Prod.snd=_ - rw [Hcond_right_partition (Z:=fun _ => ()) _ _ (fun i j => by simp)] - rw [Hcond_constant, Law.H, HS] - simp only [unit, privateH_const] at he - unfold unit at hx; linarith - induction t generalizing a with - | term => - simp only [spotDebt,expenditure,read,List.map_nil,List.sum_nil,Ex_const] - have hx := cap_entropy _ h.1.1 h.1.2 hm - linarith - | stay s v k ih => - rw [fee_stay] - cases v <;> exact ih _ h.2 hm - | spot s u k ih => - obtain ⟨hs,hS,hv,h⟩ := h - let v := u.snip - have hb := hv hm - rw [fee_spot _ _ _ hb,spotDebt] - have Hx (b : Bool) (hx : 0 < v.probs b) := ih b _ (h _) ((true_mass hb).mp hx) - have HH := v.probs.Ex_mono_mem Hx - have hl (b) : levels (u.dst b)=levels s := by unfold levels; rw [hS] - rw [Ex_add,Ex_sub] at HH - simp_rw [hl,Ex_const] at HH - have h1 := h1 v hb - change v.probs.Ex (fun b => (u.dst b).actor.entropy) =s.actor.entropy-v.probs.entropy at h1 - unfold v at *; linarith - | link s v k ih => - obtain ⟨hs,hS,hv,h⟩ := h - let U := step s - have hb := step_true hs.1.1 hS hs.2 hm - rw [fee_link v k _ hb,spotDebt] - have Hx (b : Bool) (hx : 0 < U.probs b) := ih b _ (h _) ((true_mass hb).mp hx) - have Hh := U.probs.Ex_mono_mem Hx - simp_rw [due,true_or, ite_true] - rw [Ex_add] - simp_rw [signal,Ex_const] - rw [Ex_add,Ex_sub] at Hh - simp_rw [levels_child _ hS,Ex_const] at Hh - have hh := h1 U hb - change U.probs.Ex (fun b => (s.smaller b).actor.entropy)=s.actor.entropy-U.probs.entropy at hh - unfold pitch; unfold U at * - linarith - -def Turn.up {s : Scene M} (u : Update j M s.here) : TossUp s := ⟨(toss s u).bit,(toss s u).dst⟩ -lemma fork_up (u : Update j M s.here) : (Turn.up u).snip = toss s u := rfl - -/-- Every input experiment extended to full spatial resolution -/ -def tape {s : Scene M} (t : Road j M s) : Wire M s := match t with -| .finish s _ => extra s (M-s.here.lev) -| .push s v k => stay s v (tape k) -| .fork s v k => - match v with - | .ask u => spot s (Turn.up u) fun b => tape (k b) - | .move => link s true fun b => tape (k b) - -lemma tape_ok (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) : - (tape t).Valid true := by - have he (s : Scene M) (h : s.Safe j J f) : ReadyAt s := ⟨h.within,h.positive⟩ - induction t with - | finish => - exact extra_ok _ _ _ (he _ h.1) (by rw [Nat.add_sub_of_le h.1.within.2]) - | push s v k ih => exact ⟨he _ h.1,ih h.2.2.2⟩ - | fork s v k ih => - have H := branchTrusted h - obtain ⟨hs,hb,hv,h⟩ := h - cases v with - | ask u => exact ⟨he _ hs,fun b => by cases b <;> rfl,fun _ => H,fun b => ih b (h _)⟩ - | move => exact ⟨he _ hs,(rootSafe (h false)).within.2,id,fun b => ih b (h _)⟩ - -lemma debt_like (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) (hm : 0 < s.mass) : - (tape t).spotDebt = t.flatFee memFee := by - induction t with - | finish s b => rw [flat_finish]; exact extraDebt _ _ - | push s v k ih => - rw [flat_push]; simp only [memFee,winFee,failFee,zero_add]; apply ih h.2.2.2 - rw [pace_mass]; exact hm - | fork s v k ih => - have hh := flat_fork v k h memFee - have H := branchTrusted h - have ht : v.fork.probs.Ex (fun b => (tape (k b)).spotDebt) = - v.fork.probs.Ex (fun b => (k b).flatFee memFee) := - Ex_congr_mem _ fun b hb => ih b (h.2.2.2 b) ((true_mass H).mp hb) - rw [hh,Ex_add,← ht] - cases v with - | move => simp [tape,spotDebt,memFee,winFee,failFee,Turn.fork,Ex_const]; rfl - | ask u => - let P := (toss s u).probs; let v : Procedure.Turn j s := .ask u - have HE : P.Ex (fun b => memFee (.march s v b))=P.entropy := by - unfold entropy crossE; congr 1; funext b; cases b <;> simp [memFee,winFee,failFee,surprise,v] <;> rfl - change P.entropy + P.Ex _ = P.Ex (fun b => memFee (.march s v b))+P.Ex _ - rw [HE]; rfl - - -lemma extra_bill (s : Scene M) (n : ℕ) (f : Pulse M → ℝ) (h : ∀ e : Pulse M, e.active=false → f e=0) - (hs : ReadyAt s) (_hm : 0 < s.mass) (hn : s.here.lev+n = M) : - expenditure (extra s n) f = 0 := by - let t := extra s n - unfold expenditure - have he (x : Grid M) : - ((t.read x).map f).sum=0 := by - have h1 := faded _ (fades _ (extra_ok s n false hs hn) x) - apply List.sum_eq_zero - intro i hi; obtain ⟨j,hj,rfl⟩ := List.mem_map.mp hi; exact h _ (h1 _ hj) - change _ = _ - change s.actor.Ex (fun x => _) = _ - simp_rw [show (fun x : Grid M => ((read (extra s n) x).map f).sum)=(fun _ => 0) from funext he] - rw [Ex_const] - -lemma active_charge (F : Pulse M → ℝ) (hF : ∀ i : Pulse M, i.active=false → F i=0) - (g : Event j M → ℝ) (ha : ∀ s v, g (Event.pace s v)=0) - (hb : ∀ (s : Scene M) (u : Update j M s.here) b, g (.march s (.ask u) b)=0) - (hg : ∀ s, 0 < s.mass → - (step s).probs.Ex (fun b => g (.march s .move b)) = - (step s).probs.Ex (fun b => F ⟨s,true,b⟩)) - (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) (hm : 0 < s.mass) : - (tape t).expenditure F=t.flatFee g := by - induction t with - | finish => - rw [flat_finish] - exact extra_bill _ _ F hF (Valid.atSrc (tape_ok _ h)) hm (by rw [Nat.add_sub_of_le h.1.within.2]) - | push s v k ih => - rw [flat_push,ha,zero_add] - rw [tape,fee_stay]; exact ih h.2.2.2 (by rw [pace_mass]; exact hm) - | fork s v k ih => - have ht := branchTrusted h - rw [flat_fork _ _ h,Ex_add] - have HH : v.fork.probs.Ex (fun b => expenditure (tape (k b)) F) = - v.fork.probs.Ex fun b => (k b).flatFee g := - Ex_congr_mem _ fun i hi => ih i (h.2.2.2 i) ((true_mass ht).mp hi) - cases v with - | ask u => - change expenditure (.spot s (Turn.up u) _) F = _ - rw [fee_spot (Turn.up u) _ _ ht] - simp_rw [hb,Ex_const,zero_add] - exact HH - | move => - change expenditure (.link s true _) F = - (step s).probs.Ex (fun b => g (.march s .move b))+ _ - rw [fee_link _ _ _ ht,Ex_add,hg s hm] - congr 1 - -end FourierLLogL.Dyadic.Procedure.Wire - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Charge + +/-! # Full extensions and their bill -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Wire +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +/-- Additional unlabelled steps after exit, continuing with a piece frozen. -/ +def extra (s : Scene M) : ℕ → Wire M s + | 0 => term s + | n+1 => link s false fun b => extra (s.smaller b) n + +lemma extra_ok (s : Scene M) (n : ℕ) (a : Bool) (h : ReadyAt s) (hn : s.here.lev+n=M) : + Valid (extra s n) a := by + induction n generalizing s a with + | zero => exact ⟨h,hn⟩ + | succ n ih => + have hp : s.here.lev < M := by omega + refine ⟨h,hp,fun he => False.elim (by simp at he),fun b => ih _ _ ?_ ?_⟩ + · exact ⟨child_ok _ h.1.1 hp b,h.2⟩ + have hh := child_depth s.here b + change (s.here.child b).lev+n=M; omega + +/-- Total entropy cost of non-spatial branching. -/ +def spotDebt {s : Scene M} (t : Wire M s) : ℝ := match t with +| .stay _ _ k => k.spotDebt +| .spot _ u k => + u.snip.probs.entropy + u.snip.probs.Ex (fun b => (k b).spotDebt) +| .link s _ k => (step s).probs.Ex (fun b => (k b).spotDebt) +| .term _ => 0 + +lemma extraDebt (s : Scene M) (n : ℕ) : spotDebt (extra s n) = 0 := by + induction n generalizing s with + | zero => simp [extra,spotDebt] + | succ n ih => simp [extra,spotDebt,ih,Ex_const] + +lemma due_all (t : Wire M s) (a : Bool) (h : Valid t a) (hm : 0 < s.mass) : + t.expenditure (due 0) ≤ levels s - s.actor.entropy + t.spotDebt := by + let unit := fun _ : Grid M => uniform Unit + have h1 (v : Fork M) (ht : v.TrueLaw) : + v.probs.Ex (fun b => (v.dst b).actor.entropy) = + v.src.actor.entropy - v.probs.entropy := by + have he := conditioned_balance unit v ht + have hx : sideH v unit = v.probs.entropy := by + let P := v.src.actor.tensorP (uniform Unit) + let q := fun i : Grid M × Unit => v.bit i.1 + have HS : P.send q = v.probs := by + have HH := P.send_send Prod.fst v.bit + rw [send_fst_tensorP] at HH; exact HH.symm + have hh : (Prod.snd : Grid M × Unit → Unit)=fun _ => () := rfl + change P.Hcond q Prod.snd=_ + rw [Hcond_right_partition (Z:=fun _ => ()) _ _ (fun i j => by simp)] + rw [Hcond_constant, Law.H, HS] + simp only [unit, privateH_const] at he + unfold unit at hx; linarith + induction t generalizing a with + | term => + simp only [spotDebt,expenditure,read,List.map_nil,List.sum_nil,Ex_const] + have hx := cap_entropy _ h.1.1 h.1.2 hm + linarith + | stay s v k ih => + rw [fee_stay] + cases v <;> exact ih _ h.2 hm + | spot s u k ih => + obtain ⟨hs,hS,hv,h⟩ := h + let v := u.snip + have hb := hv hm + rw [fee_spot _ _ _ hb,spotDebt] + have Hx (b : Bool) (hx : 0 < v.probs b) := ih b _ (h _) ((true_mass hb).mp hx) + have HH := v.probs.Ex_mono_mem Hx + have hl (b) : levels (u.dst b)=levels s := by unfold levels; rw [hS] + rw [Ex_add,Ex_sub] at HH + simp_rw [hl,Ex_const] at HH + have h1 := h1 v hb + change v.probs.Ex (fun b => (u.dst b).actor.entropy) =s.actor.entropy-v.probs.entropy at h1 + unfold v at *; linarith + | link s v k ih => + obtain ⟨hs,hS,hv,h⟩ := h + let U := step s + have hb := step_true hs.1.1 hS hs.2 hm + rw [fee_link v k _ hb,spotDebt] + have Hx (b : Bool) (hx : 0 < U.probs b) := ih b _ (h _) ((true_mass hb).mp hx) + have Hh := U.probs.Ex_mono_mem Hx + simp_rw [due,true_or, ite_true] + rw [Ex_add] + simp_rw [signal,Ex_const] + rw [Ex_add,Ex_sub] at Hh + simp_rw [levels_child _ hS,Ex_const] at Hh + have hh := h1 U hb + change U.probs.Ex (fun b => (s.smaller b).actor.entropy)=s.actor.entropy-U.probs.entropy at hh + unfold pitch; unfold U at * + linarith + +def Turn.up {s : Scene M} (u : Update j M s.here) : TossUp s := ⟨(toss s u).bit,(toss s u).dst⟩ +lemma fork_up (u : Update j M s.here) : (Turn.up u).snip = toss s u := rfl + +/-- Every input experiment extended to full spatial resolution -/ +def tape {s : Scene M} (t : Road j M s) : Wire M s := match t with +| .finish s _ => extra s (M-s.here.lev) +| .push s v k => stay s v (tape k) +| .fork s v k => + match v with + | .ask u => spot s (Turn.up u) fun b => tape (k b) + | .move => link s true fun b => tape (k b) + +lemma tape_ok (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) : + (tape t).Valid true := by + have he (s : Scene M) (h : s.Safe j J f) : ReadyAt s := ⟨h.within,h.positive⟩ + induction t with + | finish => + exact extra_ok _ _ _ (he _ h.1) (by rw [Nat.add_sub_of_le h.1.within.2]) + | push s v k ih => exact ⟨he _ h.1,ih h.2.2.2⟩ + | fork s v k ih => + have H := branchTrusted h + obtain ⟨hs,hb,hv,h⟩ := h + cases v with + | ask u => exact ⟨he _ hs,fun b => by cases b <;> rfl,fun _ => H,fun b => ih b (h _)⟩ + | move => exact ⟨he _ hs,(rootSafe (h false)).within.2,id,fun b => ih b (h _)⟩ + +lemma debt_like (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) (hm : 0 < s.mass) : + (tape t).spotDebt = t.flatFee memFee := by + induction t with + | finish s b => rw [flat_finish]; exact extraDebt _ _ + | push s v k ih => + rw [flat_push]; simp only [memFee,winFee,failFee,zero_add]; apply ih h.2.2.2 + rw [pace_mass]; exact hm + | fork s v k ih => + have hh := flat_fork v k h memFee + have H := branchTrusted h + have ht : v.fork.probs.Ex (fun b => (tape (k b)).spotDebt) = + v.fork.probs.Ex (fun b => (k b).flatFee memFee) := + Ex_congr_mem _ fun b hb => ih b (h.2.2.2 b) ((true_mass H).mp hb) + rw [hh,Ex_add,← ht] + cases v with + | move => simp [tape,spotDebt,memFee,winFee,failFee,Turn.fork,Ex_const]; rfl + | ask u => + let P := (toss s u).probs; let v : Procedure.Turn j s := .ask u + have HE : P.Ex (fun b => memFee (.march s v b))=P.entropy := by + unfold entropy crossE; congr 1; funext b; cases b <;> simp [memFee,winFee,failFee,surprise,v] <;> rfl + change P.entropy + P.Ex _ = P.Ex (fun b => memFee (.march s v b))+P.Ex _ + rw [HE]; rfl + + +lemma extra_bill (s : Scene M) (n : ℕ) (f : Pulse M → ℝ) (h : ∀ e : Pulse M, e.active=false → f e=0) + (hs : ReadyAt s) (_hm : 0 < s.mass) (hn : s.here.lev+n = M) : + expenditure (extra s n) f = 0 := by + let t := extra s n + unfold expenditure + have he (x : Grid M) : + ((t.read x).map f).sum=0 := by + have h1 := faded _ (fades _ (extra_ok s n false hs hn) x) + apply List.sum_eq_zero + intro i hi; obtain ⟨j,hj,rfl⟩ := List.mem_map.mp hi; exact h _ (h1 _ hj) + change _ = _ + change s.actor.Ex (fun x => _) = _ + simp_rw [show (fun x : Grid M => ((read (extra s n) x).map f).sum)=(fun _ => 0) from funext he] + rw [Ex_const] + +lemma active_charge (F : Pulse M → ℝ) (hF : ∀ i : Pulse M, i.active=false → F i=0) + (g : Event j M → ℝ) (ha : ∀ s v, g (Event.pace s v)=0) + (hb : ∀ (s : Scene M) (u : Update j M s.here) b, g (.march s (.ask u) b)=0) + (hg : ∀ s, 0 < s.mass → + (step s).probs.Ex (fun b => g (.march s .move b)) = + (step s).probs.Ex (fun b => F ⟨s,true,b⟩)) + (t : Road j M s) {f : Grid M → ℝ} (h : Trusted J f t) (hm : 0 < s.mass) : + (tape t).expenditure F=t.flatFee g := by + induction t with + | finish => + rw [flat_finish] + exact extra_bill _ _ F hF (Valid.atSrc (tape_ok _ h)) hm (by rw [Nat.add_sub_of_le h.1.within.2]) + | push s v k ih => + rw [flat_push,ha,zero_add] + rw [tape,fee_stay]; exact ih h.2.2.2 (by rw [pace_mass]; exact hm) + | fork s v k ih => + have ht := branchTrusted h + rw [flat_fork _ _ h,Ex_add] + have HH : v.fork.probs.Ex (fun b => expenditure (tape (k b)) F) = + v.fork.probs.Ex fun b => (k b).flatFee g := + Ex_congr_mem _ fun i hi => ih i (h.2.2.2 i) ((true_mass ht).mp hi) + cases v with + | ask u => + change expenditure (.spot s (Turn.up u) _) F = _ + rw [fee_spot (Turn.up u) _ _ ht] + simp_rw [hb,Ex_const,zero_add] + exact HH + | move => + change expenditure (.link s true _) F = + (step s).probs.Ex (fun b => g (.march s .move b))+ _ + rw [fee_link _ _ _ ht,Ex_add,hg s hm] + congr 1 + +end FourierLLogL.Dyadic.Procedure.Wire + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Fringe.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Fringe.lean index 4a8270adaabb59b0fd397b9514a20ad60dd7c7b1..7cf9e7c8beeccf8d07540f01780f679a8dc94d22 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Fringe.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Fringe.lean @@ -1,179 +1,179 @@ -import OAI.Analysis.FourierLLogL.Procedure.Fork - -/-! # Thin parts of a grid interval -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -def reach (i : ℕ) := 80+8*i -def gateWidth (main : Bool) (i : ℕ) := (if main then side i^2 else side i) / 128 - -lemma reach_pos (i : ℕ) : 1 ≤ reach i := by unfold reach; omega -lemma gate_pos (main : Bool) (i : ℕ) : 0 < gateWidth main i := by unfold gateWidth; have := side_pos i; split <;> positivity - -/-- all b steps child -/ -def extreme (p : Node) (b : Bool) : ℕ → Node -| 0 => p -| m+1 => (extreme p b m).child b - -lemma child_depth (p : Node) (b : Bool) : (p.child b).lev=p.lev+1 := by cases b <;> rfl - -lemma extreme_depth (p : Node) (b : Bool) (n : ℕ) : - (extreme p b n).lev = p.lev+n := by - induction n with - | zero => simp [extreme] - | succ n ih => - rw [extreme,child_depth,ih, Nat.add_assoc] - -lemma extreme_length (p : Node) (b : Bool) (n : ℕ) : - (extreme p b n).len = p.len*side n := by rw [len,extreme_depth,len,side,side,side,pow_add] - -lemma extreme_below (p : Node) (b : Bool) (n : ℕ) : (extreme p b n).below p := by - induction n with - | zero => exact below_refl p - | succ n ih => exact below_trans (child_below _ _) ih - -lemma extreme_valid (p : Node) (hp : p.valid) (b : Bool) (n : ℕ) : - (extreme p b n).valid := by - induction n with - | zero => exact hp - | succ n ih => cases b; exact (valid_L _).mpr ih; exact (valid_R _).mpr ih - -lemma extreme_left (p : Node) (n : ℕ) : (extreme p false n).a=p.a := by - induction n with - | zero => rfl - | succ n ih => - change (extreme p false n).L.a=_ - rw [a_L,ih] - -lemma extreme_right (p : Node) (n : ℕ) : - (extreme p true n).a+(extreme p true n).len=p.a+p.len := by - induction n with - | zero => rfl - | succ n ih => - change (extreme p true n).R.a+(extreme p true n).R.len=_ - rw [a_R,len_R]; linarith - -/-- strips of width 2^-l p about the two edges of both children of p; empty if the strip width -needs more bits than available. -/ -def strip (p : Node) (i : ℕ) (b c : Bool) := - extreme (child p b) c (reach i-1) - -lemma strip_depth (p : Node) (i : ℕ) (b c : Bool) : - (strip p i b c).lev = p.lev+reach i := by rw [strip,extreme_depth,child_depth]; have := reach_pos i; omega -lemma strip_valid {p : Node} (hp : p.valid) (i : ℕ) (b c : Bool) : - (strip p i b c).valid := by - apply extreme_valid; cases b; exact p.valid_L.mpr hp; exact p.valid_R.mpr hp -lemma strip_below_child (p : Node) (i : ℕ) (b c : Bool) : - (strip p i b c).below (p.child b) := extreme_below _ _ _ -lemma strip_below (p : Node) (i : ℕ) (b c : Bool) : - (strip p i b c).below p := below_trans (strip_below_child _ _ _ _) (child_below _ _) - -lemma strip_length (p : Node) (i : ℕ) (b c : Bool) : - (strip p i b c).len = p.len * side (reach i) := by rw [len,strip_depth,len,side,side,side,pow_add] - -def flanked (p : Node) (i : ℕ) (x : Grid M) := - ∃ b c, x ∈ (strip p i b c).block M - -def lanes (p : Node) (i M : ℕ) := p.lev+reach i ≤ M - -def edgeW (s : Scene M) (i : ℕ) := - if lanes s.here i M then (∑ x, if flanked s.here i x then wt s.here s.wage s.norm x else 0) - else 0 - -def hazard (s : Scene M) (main : Bool) (i : ℕ) := - gateWidth main i ≤ edgeW s i -def surcharge (s : Scene M) (main : Bool) (i : ℕ) := - if hazard s main i then edgeW s i else 0 - -lemma surcharge_nonneg (s : Scene M) (main : Bool) (i : ℕ) : - 0 ≤ surcharge s main i := by - unfold surcharge; split_ifs with H - · exact (gate_pos _ _).le.trans H - exact le_rfl - -lemma flank_in {p : Node} {i : ℕ} {x : Grid M} (hi : flanked p i x) : x∈p.block M := by - obtain ⟨u,v,h⟩ := hi - exact block_of_below (strip_below _ _ _ _) _ h - -/-- A property measurable in depth l. Does not require l≤M. -/ -def spotted (p : Grid M → Prop) (l : ℕ) := - ∀ v : Node, v.lev=l → ∀ x∈v.block M, ∀ y∈v.block M, p x ↔ p y - -lemma flanked_at (p : Node) (i : ℕ) : - spotted (M:=M) (flanked p i) (p.lev+reach i) := by - intro q he x hx y hy - unfold flanked - have Heq (b c) : x∈(strip p i b c).block M ↔ y∈(strip p i b c).block M := by - apply coarse_const (q:=q) _ hx hy - rw [he,strip_depth] - simp_rw [Heq] - -lemma prob_under {p q : Node} (hp : p.ok M) (hh : q.ok M) (h : q.below p) : - (p.dice M).prob (fun x => x∈q.block M) = q.len/p.len := by - classical - rw [prob_eq] - calc - _ = p.len⁻¹*(uniform _).prob (fun x => x∈q.block M) := by - rw [prob_eq,Finset.mul_sum] - refine Finset.sum_congr rfl fun x _ => ?_ - by_cases hx : x∈q.block M - · simp only [hx,ite_true,dice_apply hp,ite_eq_left (block_of_below h _ hx),uniform_side,mul_comm] - simp [hx] - _ = q.len/p.len := by rw [prob_block hh]; ring - -lemma flank_prob (p : Node) (hp : p.valid) (i : ℕ) (he : lanes p i M) : - (p.dice M).prob (flanked p i) ≤ 4 * side (reach i) := by - let f (b c : Bool) (x : Grid M) := if x∈(strip p i b c).block M then (1:ℝ) else 0 - let Q := p.dice M - have HF (b c) : Q.Ex (f b c) = side (reach i) := by - have hh : (strip p i b c).ok M := ⟨strip_valid hp _ _ _,strip_depth p _ _ _ |>.symm ▸ he⟩ - have hx := prob_under (p:=p) (q:=strip p i b c) - (show p.ok M from ⟨hp,Nat.le_trans (Nat.le_add_right _ _) he⟩) hh (strip_below _ _ _ _) - have hy : Q.prob (fun x => x∈(strip p i b c).block M) = Q.Ex (f b c) := by rw [prob_eq]; simp [Ex,f] - change Q.prob _ = _ at hx - rw [hy,strip_length] at hx; rw [hx]; field_simp [p.len_pos.ne'] - unfold prob; apply le_trans (b:=Q.Ex fun x => - f false false x + f false true x + f true false x + f true true x) - · apply Ex_mono; intro x - split_ifs with H - · obtain ⟨a,b,H⟩ := H - cases a <;> cases b <;> unfold f <;> split_ifs <;> norm_num - unfold f; split_ifs <;> norm_num - · simp_rw [Ex_add,HF]; linarith - -lemma hazard_rule {s : Scene M} {main : Bool} {i : ℕ} (ha : s.here.ok M) - (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (ht : ¬ s.enter) (hx : hazard s main i) : - lanes s.here i M ∧ - s.norm/s.mass * edgeW s i = s.actor.prob (flanked s.here i) ∧ - gateWidth main i / 2 ≤ s.actor.prob (flanked s.here i) := by - classical - change gateWidth main i ≤ _ at hx - have hl : lanes s.here i M := by - by_contra hv; rw [edgeW,ite_eq_right hv] at hx - exact (gate_pos _ _).not_ge hx - unfold enter at ht - have hp : 0 < s.norm := by linarith - have he : s.norm/s.mass * edgeW s i = s.actor.prob (flanked s.here i) := by - rw [prob_eq,edgeW,ite_eq_left hl, Finset.mul_sum] - refine Finset.sum_congr rfl fun x h => ?_ - split_ifs with H - · rw [actor_def ha hh hm x] - unfold wt; unfold mass at * - field_simp - exact mul_zero _ - refine ⟨hl,he,?_⟩ - rw [← he] - apply le_trans (b:=1/2*gateWidth main i) (le_of_eq (by ring)) - apply mul_le_mul _ hx (gate_pos _ _).le (by positivity) - apply (le_div_iff₀ hm).mpr; linarith - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Fork + +/-! # Thin parts of a grid interval -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +def reach (i : ℕ) := 80+8*i +def gateWidth (main : Bool) (i : ℕ) := (if main then side i^2 else side i) / 128 + +lemma reach_pos (i : ℕ) : 1 ≤ reach i := by unfold reach; omega +lemma gate_pos (main : Bool) (i : ℕ) : 0 < gateWidth main i := by unfold gateWidth; have := side_pos i; split <;> positivity + +/-- all b steps child -/ +def extreme (p : Node) (b : Bool) : ℕ → Node +| 0 => p +| m+1 => (extreme p b m).child b + +lemma child_depth (p : Node) (b : Bool) : (p.child b).lev=p.lev+1 := by cases b <;> rfl + +lemma extreme_depth (p : Node) (b : Bool) (n : ℕ) : + (extreme p b n).lev = p.lev+n := by + induction n with + | zero => simp [extreme] + | succ n ih => + rw [extreme,child_depth,ih, Nat.add_assoc] + +lemma extreme_length (p : Node) (b : Bool) (n : ℕ) : + (extreme p b n).len = p.len*side n := by rw [len,extreme_depth,len,side,side,side,pow_add] + +lemma extreme_below (p : Node) (b : Bool) (n : ℕ) : (extreme p b n).below p := by + induction n with + | zero => exact below_refl p + | succ n ih => exact below_trans (child_below _ _) ih + +lemma extreme_valid (p : Node) (hp : p.valid) (b : Bool) (n : ℕ) : + (extreme p b n).valid := by + induction n with + | zero => exact hp + | succ n ih => cases b; exact (valid_L _).mpr ih; exact (valid_R _).mpr ih + +lemma extreme_left (p : Node) (n : ℕ) : (extreme p false n).a=p.a := by + induction n with + | zero => rfl + | succ n ih => + change (extreme p false n).L.a=_ + rw [a_L,ih] + +lemma extreme_right (p : Node) (n : ℕ) : + (extreme p true n).a+(extreme p true n).len=p.a+p.len := by + induction n with + | zero => rfl + | succ n ih => + change (extreme p true n).R.a+(extreme p true n).R.len=_ + rw [a_R,len_R]; linarith + +/-- strips of width 2^-l p about the two edges of both children of p; empty if the strip width +needs more bits than available. -/ +def strip (p : Node) (i : ℕ) (b c : Bool) := + extreme (child p b) c (reach i-1) + +lemma strip_depth (p : Node) (i : ℕ) (b c : Bool) : + (strip p i b c).lev = p.lev+reach i := by rw [strip,extreme_depth,child_depth]; have := reach_pos i; omega +lemma strip_valid {p : Node} (hp : p.valid) (i : ℕ) (b c : Bool) : + (strip p i b c).valid := by + apply extreme_valid; cases b; exact p.valid_L.mpr hp; exact p.valid_R.mpr hp +lemma strip_below_child (p : Node) (i : ℕ) (b c : Bool) : + (strip p i b c).below (p.child b) := extreme_below _ _ _ +lemma strip_below (p : Node) (i : ℕ) (b c : Bool) : + (strip p i b c).below p := below_trans (strip_below_child _ _ _ _) (child_below _ _) + +lemma strip_length (p : Node) (i : ℕ) (b c : Bool) : + (strip p i b c).len = p.len * side (reach i) := by rw [len,strip_depth,len,side,side,side,pow_add] + +def flanked (p : Node) (i : ℕ) (x : Grid M) := + ∃ b c, x ∈ (strip p i b c).block M + +def lanes (p : Node) (i M : ℕ) := p.lev+reach i ≤ M + +def edgeW (s : Scene M) (i : ℕ) := + if lanes s.here i M then (∑ x, if flanked s.here i x then wt s.here s.wage s.norm x else 0) + else 0 + +def hazard (s : Scene M) (main : Bool) (i : ℕ) := + gateWidth main i ≤ edgeW s i +def surcharge (s : Scene M) (main : Bool) (i : ℕ) := + if hazard s main i then edgeW s i else 0 + +lemma surcharge_nonneg (s : Scene M) (main : Bool) (i : ℕ) : + 0 ≤ surcharge s main i := by + unfold surcharge; split_ifs with H + · exact (gate_pos _ _).le.trans H + exact le_rfl + +lemma flank_in {p : Node} {i : ℕ} {x : Grid M} (hi : flanked p i x) : x∈p.block M := by + obtain ⟨u,v,h⟩ := hi + exact block_of_below (strip_below _ _ _ _) _ h + +/-- A property measurable in depth l. Does not require l≤M. -/ +def spotted (p : Grid M → Prop) (l : ℕ) := + ∀ v : Node, v.lev=l → ∀ x∈v.block M, ∀ y∈v.block M, p x ↔ p y + +lemma flanked_at (p : Node) (i : ℕ) : + spotted (M:=M) (flanked p i) (p.lev+reach i) := by + intro q he x hx y hy + unfold flanked + have Heq (b c) : x∈(strip p i b c).block M ↔ y∈(strip p i b c).block M := by + apply coarse_const (q:=q) _ hx hy + rw [he,strip_depth] + simp_rw [Heq] + +lemma prob_under {p q : Node} (hp : p.ok M) (hh : q.ok M) (h : q.below p) : + (p.dice M).prob (fun x => x∈q.block M) = q.len/p.len := by + classical + rw [prob_eq] + calc + _ = p.len⁻¹*(uniform _).prob (fun x => x∈q.block M) := by + rw [prob_eq,Finset.mul_sum] + refine Finset.sum_congr rfl fun x _ => ?_ + by_cases hx : x∈q.block M + · simp only [hx,ite_true,dice_apply hp,ite_eq_left (block_of_below h _ hx),uniform_side,mul_comm] + simp [hx] + _ = q.len/p.len := by rw [prob_block hh]; ring + +lemma flank_prob (p : Node) (hp : p.valid) (i : ℕ) (he : lanes p i M) : + (p.dice M).prob (flanked p i) ≤ 4 * side (reach i) := by + let f (b c : Bool) (x : Grid M) := if x∈(strip p i b c).block M then (1:ℝ) else 0 + let Q := p.dice M + have HF (b c) : Q.Ex (f b c) = side (reach i) := by + have hh : (strip p i b c).ok M := ⟨strip_valid hp _ _ _,strip_depth p _ _ _ |>.symm ▸ he⟩ + have hx := prob_under (p:=p) (q:=strip p i b c) + (show p.ok M from ⟨hp,Nat.le_trans (Nat.le_add_right _ _) he⟩) hh (strip_below _ _ _ _) + have hy : Q.prob (fun x => x∈(strip p i b c).block M) = Q.Ex (f b c) := by rw [prob_eq]; simp [Ex,f] + change Q.prob _ = _ at hx + rw [hy,strip_length] at hx; rw [hx]; field_simp [p.len_pos.ne'] + unfold prob; apply le_trans (b:=Q.Ex fun x => + f false false x + f false true x + f true false x + f true true x) + · apply Ex_mono; intro x + split_ifs with H + · obtain ⟨a,b,H⟩ := H + cases a <;> cases b <;> unfold f <;> split_ifs <;> norm_num + unfold f; split_ifs <;> norm_num + · simp_rw [Ex_add,HF]; linarith + +lemma hazard_rule {s : Scene M} {main : Bool} {i : ℕ} (ha : s.here.ok M) + (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (ht : ¬ s.enter) (hx : hazard s main i) : + lanes s.here i M ∧ + s.norm/s.mass * edgeW s i = s.actor.prob (flanked s.here i) ∧ + gateWidth main i / 2 ≤ s.actor.prob (flanked s.here i) := by + classical + change gateWidth main i ≤ _ at hx + have hl : lanes s.here i M := by + by_contra hv; rw [edgeW,ite_eq_right hv] at hx + exact (gate_pos _ _).not_ge hx + unfold enter at ht + have hp : 0 < s.norm := by linarith + have he : s.norm/s.mass * edgeW s i = s.actor.prob (flanked s.here i) := by + rw [prob_eq,edgeW,ite_eq_left hl, Finset.mul_sum] + refine Finset.sum_congr rfl fun x h => ?_ + split_ifs with H + · rw [actor_def ha hh hm x] + unfold wt; unfold mass at * + field_simp + exact mul_zero _ + refine ⟨hl,he,?_⟩ + rw [← he] + apply le_trans (b:=1/2*gateWidth main i) (le_of_eq (by ring)) + apply mul_le_mul _ hx (gate_pos _ _).le (by positivity) + apply (le_div_iff₀ hm).mpr; linarith + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Global.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Global.lean index b3c7c4f4aa36ef088093c95824457f22e23c6e36..b0908d01421bf39ca8ea37c937b4c8dc1559cd2d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Global.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Global.lean @@ -1,89 +1,89 @@ -import OAI.Analysis.FourierLLogL.Edges.Extended - -/-! # The edge bounds in the global road -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -def Event.edgeFee (p : Policy M) (b : Bool) (e : Event j M) := - match e with - | .march s .move t => toll p b ⟨s,true,t⟩ - | _ => 0 - -lemma cut_wire (t : Road j M s) (h : Trusted J f t) (hm : 0 < s.mass) (K : ℕ) (hK : K ≠ 0) : - expenditure (tape t) (due K) = flatFee t likelihood := by - apply active_charge _ _ _ _ _ _ t h hm - · intro i hi; unfold due; rw [ite_eq_right]; simp [hi,hK] - · intro s p; simp [likelihood] - · intro s v p; simp [likelihood] - intro s _ - simp only [due,or_true, ↓reduceIte,Pulse.signal] - change (step s).probs.Ex (step s).whistle = _ - rw [Ex_const,pitch_eq] - -lemma toll_wire (t : Road j M s) (h : Trusted J f t) (hm : 0 < s.mass) (p : Policy M) (b : Bool) : - expenditure (tape t) (toll p b) = flatFee t (edgeFee p b) := by - apply active_charge _ _ _ _ _ _ t h hm - · intro i hi; simp [toll,hi] - · intro s v; simp [edgeFee] - · intro s u v; simp [edgeFee] - intro s _; rfl - -lemma global_edges : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, - (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Road j M s, Trusted J f t → 1/Thermal.H j ≤ s.mass → - (∀ p : Policy M, t.flatFee (edgeFee p false) ≤ 1000*height j) ∧ - ∀ i, i ≤ testIx j → t.flatFee (edgeFee (fun _ => i) true) ≤ 10000*limitC*Thermal.H j := by - filter_upwards [Road.final_coarse, Road.mem_budget,hpoly limitC 1] with j hi hL h - M J f hF s t hT hM - let a := Thermal.H j; let d := height j - have hp : 0 < a := H_pos _ - have he : 1 ≤ a := H_ge _ - have hc : 1 ≤ limitC := by unfold limitC; norm_num - have hm : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hM - let T := tape t - have ht : T.Valid true := tape_ok t hT - constructor - · intro p - rw [← toll_wire t hT hm] - apply le_trans (edge_total p false T ht hm 0 (fun i => Or.inl rfl)) - have hr := rootSafe hT - let k := fun _ : Grid M => uniform Unit - have hD : levels s-s.actor.entropy ≤ d+a := by - have h := local_deficit_dry s (uniform Unit) hr.within hr.positive hm d hF - unfold deficit at h - rw [privateH_const] at h - have HH := log_le_log (by positivity : 0 < 1/a) hM - rw [one_div,log_inv] at HH - linarith [log_le_sub_one_of_pos hp] - have H₁ := due_all T _ ht hm - rw [show T.spotDebt=t.flatFee memFee from debt_like t hT hm] at H₁ - have HH := (hL M J s f t hT hm).2 - change t.flatFee memFee ≤ limitC*a at HH - rw [pow_one] at h; change limitC*a≤d at h - change 128 * expenditure T (due 0)+_≤1000*d - norm_num; nlinarith - intro i he' - rw [←toll_wire t hT hm] - apply le_trans (edge_total _ true T ht hm (reach i) (fun i => Or.inr le_rfl)) - have H : T.expenditure (due (reach i)) = t.flatFee likelihood := cut_wire t hT hm _ (Nat.ne_of_gt (reach_pos _)) - rw [H] - have hi := (hi M J s f t hT hM).1 - have hz := (main_bounds _ he').2.1 - unfold reach; push_cast - change t.flatFee likelihood ≤ 2*limitC *a at hi - change _ ≤ 19*a at hz - change _ ≤ 10000*limitC*a - nlinarith - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Extended + +/-! # The edge bounds in the global road -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +def Event.edgeFee (p : Policy M) (b : Bool) (e : Event j M) := + match e with + | .march s .move t => toll p b ⟨s,true,t⟩ + | _ => 0 + +lemma cut_wire (t : Road j M s) (h : Trusted J f t) (hm : 0 < s.mass) (K : ℕ) (hK : K ≠ 0) : + expenditure (tape t) (due K) = flatFee t likelihood := by + apply active_charge _ _ _ _ _ _ t h hm + · intro i hi; unfold due; rw [ite_eq_right]; simp [hi,hK] + · intro s p; simp [likelihood] + · intro s v p; simp [likelihood] + intro s _ + simp only [due,or_true, ↓reduceIte,Pulse.signal] + change (step s).probs.Ex (step s).whistle = _ + rw [Ex_const,pitch_eq] + +lemma toll_wire (t : Road j M s) (h : Trusted J f t) (hm : 0 < s.mass) (p : Policy M) (b : Bool) : + expenditure (tape t) (toll p b) = flatFee t (edgeFee p b) := by + apply active_charge _ _ _ _ _ _ t h hm + · intro i hi; simp [toll,hi] + · intro s v; simp [edgeFee] + · intro s u v; simp [edgeFee] + intro s _; rfl + +lemma global_edges : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, + (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, Trusted J f t → 1/Thermal.H j ≤ s.mass → + (∀ p : Policy M, t.flatFee (edgeFee p false) ≤ 1000*height j) ∧ + ∀ i, i ≤ testIx j → t.flatFee (edgeFee (fun _ => i) true) ≤ 10000*limitC*Thermal.H j := by + filter_upwards [Road.final_coarse, Road.mem_budget,hpoly limitC 1] with j hi hL h + M J f hF s t hT hM + let a := Thermal.H j; let d := height j + have hp : 0 < a := H_pos _ + have he : 1 ≤ a := H_ge _ + have hc : 1 ≤ limitC := by unfold limitC; norm_num + have hm : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hM + let T := tape t + have ht : T.Valid true := tape_ok t hT + constructor + · intro p + rw [← toll_wire t hT hm] + apply le_trans (edge_total p false T ht hm 0 (fun i => Or.inl rfl)) + have hr := rootSafe hT + let k := fun _ : Grid M => uniform Unit + have hD : levels s-s.actor.entropy ≤ d+a := by + have h := local_deficit_dry s (uniform Unit) hr.within hr.positive hm d hF + unfold deficit at h + rw [privateH_const] at h + have HH := log_le_log (by positivity : 0 < 1/a) hM + rw [one_div,log_inv] at HH + linarith [log_le_sub_one_of_pos hp] + have H₁ := due_all T _ ht hm + rw [show T.spotDebt=t.flatFee memFee from debt_like t hT hm] at H₁ + have HH := (hL M J s f t hT hm).2 + change t.flatFee memFee ≤ limitC*a at HH + rw [pow_one] at h; change limitC*a≤d at h + change 128 * expenditure T (due 0)+_≤1000*d + norm_num; nlinarith + intro i he' + rw [←toll_wire t hT hm] + apply le_trans (edge_total _ true T ht hm (reach i) (fun i => Or.inr le_rfl)) + have H : T.expenditure (due (reach i)) = t.flatFee likelihood := cut_wire t hT hm _ (Nat.ne_of_gt (reach_pos _)) + rw [H] + have hi := (hi M J s f t hT hM).1 + have hz := (main_bounds _ he').2.1 + unfold reach; push_cast + change t.flatFee likelihood ≤ 2*limitC *a at hi + change _ ≤ 19*a at hz + change _ ≤ 10000*limitC*a + nlinarith + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Intervals.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Intervals.lean index 5c2a1da352a867624136a8c48341f85d59b240b5..de9e1d82369c1721e78153689132507077c46b17 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Intervals.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Intervals.lean @@ -1,203 +1,203 @@ -import OAI.Analysis.FourierLLogL.Edges.Window - -/-! # Charging marked intervals on the path -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Wire -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse -open scoped Classical - -variable {M J j : ℕ} {s : Scene M} - -abbrev Policy (M : ℕ) := Scene M → ℕ - -def tags (p : Policy M) (e : Pulse M) (l : List (Pulse M)) : ℕ := - if e.active = true ∧ startMark (p e.scene) (e::l) then 1 else 0 -def labels (p : Policy M) : List (Pulse M) → ℕ -| [] => 0 -| e::l => tags p e l + labels p l - -/-- index budget K=0 requests the all-bit bound -/ -def due (K : ℕ) (e : Pulse M) : ℝ := - if K=0 ∨ e.active=true then e.signal else 0 - -lemma due_nonneg (K : ℕ) (e : Pulse M) : 0 ≤ due K e := by - unfold due; split_ifs; exact signal_nonneg _; exact le_rfl - -def fading : List (Pulse M) → Bool → Prop -| [], _ => True -| e::l, a => (e.active=true → a=true) ∧ fading l e.active - -lemma faded (l : List (Pulse M)) (h : fading l false) : - ∀ e∈l, e.active=false := by - induction l with - | nil => simp - | cons i l ih => - obtain ⟨h,hl⟩ := h - have he : i.active=false := by cases H:i.active <;> simp_all - rw [he] at hl - intro j hj; rcases List.mem_cons.mp hj with H|H; exact H.symm ▸ he; exact ih hl j H - -lemma fades (t : Wire M s) {a : Bool} (h : t.Valid a) (x : Grid M) : - fading (t.read x) a := by - induction t generalizing a with - | term => trivial - | stay s v t ih => exact ih h.2 - | spot s u k ih => exact ih _ (h.2.2.2 _) - | link s v k ih => exact ⟨h.2.2.1, ih _ (h.2.2.2 _)⟩ - -private lemma mark_rent (l : List (Pulse M)) (i : ℕ) (ht : startMark i l) : - (reach i:ℝ) ≤ 64 * (((l.take (reach i)).map Pulse.signal).sum) := by - let n := reach i; let L := l.take n - let f := fun e : Pulse M => max e.realized 0 - have hf (e) : f e^2 ≤ 4*e.signal := whistle_high _ _ - have hQ (l : List (Pulse M)) : - ((l.map f).sum)^2 ≤ l.length*(4:ℝ)*((l.map Pulse.signal).sum) := by - have HH (z : List (Pulse M)) : - 0 ≤ (z.map Pulse.signal).sum ∧ - ((z.map f).sum)^2 ≤ z.length*(4:ℝ)*((z.map Pulse.signal).sum) := by - induction z with - | nil => simp - | cons i z ih => - obtain ⟨ha,hb⟩ := ih - simp only [List.sum_cons,List.map_cons,List.length_cons,Nat.cast_add,Nat.cast_one] - refine ⟨add_nonneg (signal_nonneg _) ha,?_⟩ - let m := (z.length:ℝ) - let a := (z.map Pulse.signal).sum - let b := (z.map f).sum - change b^2 ≤ m*4*a at hb - change (f i+b)^2 ≤ (m+1)*4*(i.signal+a) - have hv : 0 ≤ m := by unfold m; positivity - have HH : 2*f i*b ≤ 4*(a + m*i.signal) := by - rcases eq_or_lt_of_le hv with hi|hi - · have hx : b=0 := by nlinarith - rw [← hi,hx]; linarith - have he := hf i - by_contra! h - have hX := mul_le_mul_of_nonneg_left he (by positivity : 0 ≤ m^2) - have hZ := mul_lt_mul_of_pos_left h hi - nlinarith [sq_nonneg (m*f i-b)] - nlinarith [hf i] - exact (HH l).2 - have H (a : List (Pulse M)) : (a.map Pulse.realized).sum ≤ (a.map f).sum := by - induction a with - | nil => exact le_rfl - | cons i a hi => - simp only [List.sum_cons,List.map_cons] - exact add_le_add (le_max_left _ _) hi - have hx := H L; have he := hQ L - change ↑n*log 2/2 ≤ (L.map Pulse.realized).sum at ht - have hn : (0:ℝ)< n := by unfold n; exact_mod_cast reach_pos i - have hb : (0.5:ℝ) ≤ log 2 := by linarith [log_two_gt_d9] - let a := (L.map Pulse.signal).sum - have hh : (L.length:ℝ) ≤ n := by unfold L; simp - have ha : 0 ≤ a := List.sum_nonneg (by intro i hi; rcases List.mem_map.mp hi with ⟨x,_,rfl⟩; exact signal_nonneg x) - change (n:ℝ) ≤ 64*a - change _ ≤ _*4*a at he - have hE : (n:ℝ)/4 ≤ (L.map f).sum := by nlinarith - have hY : ((n:ℝ)/4)^2 ≤ (n:ℝ)*4*a := (pow_le_pow_left₀ (by positivity) hE 2).trans (he.trans (by gcongr)) - nlinarith - -private lemma fades_suffix (l l' : List (Pulse M)) {a : Bool} (h : fading (l++l') a) : - (∃ b, fading l' b) ∧ ((∃ i∈l, i.active=false) → fading l' false) := by - induction l generalizing a with - | nil => - refine ⟨⟨a,h⟩,?_⟩; simp - | cons i l ih => - change (_→_)∧fading (l++l') i.active at h - obtain ⟨hh,hx⟩ := ih h.2 - refine ⟨hh,?_⟩ - rintro ⟨x,hx',hf⟩ - rcases List.mem_cons.mp hx' with H|H; subst x - · have he (l l' : List (Pulse M)) : fading (l++l') false → fading l' false := by - induction l with - | nil => exact fun h => h - | cons i l ih => - rintro ⟨h,h'⟩ - apply ih - have hh : i.active=false := by cases H : i.active <;> simp_all - exact hh ▸ h' - exact he l l' (hf ▸ h.2) - exact hx ⟨x,H,hf⟩ - -theorem mark_intervals (p : Policy M) (t : Wire M s) {a : Bool} (h : t.Valid a) - (K : ℕ) (hk : ∀ x : Scene M, K=0 ∨ reach (p x) ≤ K) (x : Grid M) : - (labels p (t.read x):ℝ) ≤ 64*(((t.read x).map (due K)).sum) + K := by - have hv0 (z : List (Pulse M)) : 0 ≤ (z.map (due K)).sum := - List.sum_nonneg (by intro i hi; rcases List.mem_map.mp hi with ⟨x,_,rfl⟩; exact due_nonneg K x) - have hc : ∀ k : ℕ, ∀ (l : List (Pulse M)), l.length=k → ∀ a, fading l a → - (labels p l:ℝ) ≤ 64*((l.map (due K)).sum) + K := by - intro k; induction k using Nat.strong_induction_on with - | h k ih => - intro l hl a ha - cases l with - | nil => simp [labels] - | cons e l => - let Z := e::l - rcases ha with ⟨H,hh⟩ - by_cases ht : e.active=true ∧ startMark (p e.scene) (e::l) - · let n := reach (p e.scene) - let U := Z.take n; let V := Z.drop n - have hE : U ++ V= Z := List.take_append_drop n Z - have he : V.length < k := by - have h1 : 0 < n := reach_pos _ - change (e::l).length=k at hl; unfold V Z; rw [List.length_drop] - simp only [List.length_cons] at *; omega - have hT : (labels p Z:ℝ) ≤ (U.length:ℝ)+labels p V := by - have hh (l : List (Pulse M)) : - labels p (l++V) ≤ l.length+labels p V := by - induction l with - | nil => simp - | cons i l ih => - change tags p i (l++V) + labels p (l++V) ≤ _ - have ht : tags p i (l++V) ≤ 1 := by unfold tags; split_ifs <;> omega - simp only [List.length_cons] - omega - have H := hh U; rw [hE] at H - exact_mod_cast H - have hx1 := mark_rent (e::l) _ ht.2 - have HX : fading Z a := ⟨H,hh⟩ - rw [← hE] at HX - obtain ⟨⟨b,hx⟩,hx'⟩ := fades_suffix U V HX - have hz : (Z.map (due K)).sum = (U.map (due K)).sum + (V.map (due K)).sum := - calc - _ = ((U++V).map (due K)).sum := by rw [hE] - _ = _ := by simp - have hn : (U.length:ℝ) ≤ n := by simp [U] - change (labels p Z:ℝ) ≤ 64* (Z.map (due K)).sum + K - rw [hz] - by_cases HH : ∀ y∈U, K=0 ∨ y.active=true - · have hhi := ih _ he V rfl b hx - change (n:ℝ) ≤ 64*(U.map Pulse.signal).sum at hx1 - have hQ : U.map (due K) = U.map Pulse.signal := - List.map_congr_left (fun i hi => ite_eq_left (HH i hi)) - rw [hQ] - linarith - push Not at HH - obtain ⟨i,hi,hK,hi'⟩ := HH - have hk : (n:ℝ) ≤ K := by - exact_mod_cast ((hk _).resolve_left hK : n ≤ K) - have hi'' : fading V false := - hx' ⟨i,hi,by cases H:i.active <;> simp_all⟩ - have hj (l : List (Pulse M)) (hh : ∀ i∈l,i.active=false) : labels p l=0 := by - induction l with - | nil => rfl - | cons i l ih => - have he : i.active=false := hh i List.mem_cons_self - have hV := ih fun i hi => hh i (List.mem_cons_of_mem _ hi) - simp [labels, tags,hV, he] - rw [hj _ (faded V hi''),Nat.cast_zero] at hT - linarith [hv0 V,hv0 U] - have HE : tags p e l=0 := ite_eq_right ht - have h1 := ih _ (by rw [List.length_cons] at hl; omega : l.length < k) l rfl _ hh - simp only [labels,HE,zero_add,List.map_cons,List.sum_cons] - linarith [due_nonneg K e] - exact hc _ (t.read x) rfl a (t.fades h x) - -end FourierLLogL.Dyadic.Procedure.Wire - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Window + +/-! # Charging marked intervals on the path -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Wire +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Pulse +open scoped Classical + +variable {M J j : ℕ} {s : Scene M} + +abbrev Policy (M : ℕ) := Scene M → ℕ + +def tags (p : Policy M) (e : Pulse M) (l : List (Pulse M)) : ℕ := + if e.active = true ∧ startMark (p e.scene) (e::l) then 1 else 0 +def labels (p : Policy M) : List (Pulse M) → ℕ +| [] => 0 +| e::l => tags p e l + labels p l + +/-- index budget K=0 requests the all-bit bound -/ +def due (K : ℕ) (e : Pulse M) : ℝ := + if K=0 ∨ e.active=true then e.signal else 0 + +lemma due_nonneg (K : ℕ) (e : Pulse M) : 0 ≤ due K e := by + unfold due; split_ifs; exact signal_nonneg _; exact le_rfl + +def fading : List (Pulse M) → Bool → Prop +| [], _ => True +| e::l, a => (e.active=true → a=true) ∧ fading l e.active + +lemma faded (l : List (Pulse M)) (h : fading l false) : + ∀ e∈l, e.active=false := by + induction l with + | nil => simp + | cons i l ih => + obtain ⟨h,hl⟩ := h + have he : i.active=false := by cases H:i.active <;> simp_all + rw [he] at hl + intro j hj; rcases List.mem_cons.mp hj with H|H; exact H.symm ▸ he; exact ih hl j H + +lemma fades (t : Wire M s) {a : Bool} (h : t.Valid a) (x : Grid M) : + fading (t.read x) a := by + induction t generalizing a with + | term => trivial + | stay s v t ih => exact ih h.2 + | spot s u k ih => exact ih _ (h.2.2.2 _) + | link s v k ih => exact ⟨h.2.2.1, ih _ (h.2.2.2 _)⟩ + +private lemma mark_rent (l : List (Pulse M)) (i : ℕ) (ht : startMark i l) : + (reach i:ℝ) ≤ 64 * (((l.take (reach i)).map Pulse.signal).sum) := by + let n := reach i; let L := l.take n + let f := fun e : Pulse M => max e.realized 0 + have hf (e) : f e^2 ≤ 4*e.signal := whistle_high _ _ + have hQ (l : List (Pulse M)) : + ((l.map f).sum)^2 ≤ l.length*(4:ℝ)*((l.map Pulse.signal).sum) := by + have HH (z : List (Pulse M)) : + 0 ≤ (z.map Pulse.signal).sum ∧ + ((z.map f).sum)^2 ≤ z.length*(4:ℝ)*((z.map Pulse.signal).sum) := by + induction z with + | nil => simp + | cons i z ih => + obtain ⟨ha,hb⟩ := ih + simp only [List.sum_cons,List.map_cons,List.length_cons,Nat.cast_add,Nat.cast_one] + refine ⟨add_nonneg (signal_nonneg _) ha,?_⟩ + let m := (z.length:ℝ) + let a := (z.map Pulse.signal).sum + let b := (z.map f).sum + change b^2 ≤ m*4*a at hb + change (f i+b)^2 ≤ (m+1)*4*(i.signal+a) + have hv : 0 ≤ m := by unfold m; positivity + have HH : 2*f i*b ≤ 4*(a + m*i.signal) := by + rcases eq_or_lt_of_le hv with hi|hi + · have hx : b=0 := by nlinarith + rw [← hi,hx]; linarith + have he := hf i + by_contra! h + have hX := mul_le_mul_of_nonneg_left he (by positivity : 0 ≤ m^2) + have hZ := mul_lt_mul_of_pos_left h hi + nlinarith [sq_nonneg (m*f i-b)] + nlinarith [hf i] + exact (HH l).2 + have H (a : List (Pulse M)) : (a.map Pulse.realized).sum ≤ (a.map f).sum := by + induction a with + | nil => exact le_rfl + | cons i a hi => + simp only [List.sum_cons,List.map_cons] + exact add_le_add (le_max_left _ _) hi + have hx := H L; have he := hQ L + change ↑n*log 2/2 ≤ (L.map Pulse.realized).sum at ht + have hn : (0:ℝ)< n := by unfold n; exact_mod_cast reach_pos i + have hb : (0.5:ℝ) ≤ log 2 := by linarith [log_two_gt_d9] + let a := (L.map Pulse.signal).sum + have hh : (L.length:ℝ) ≤ n := by unfold L; simp + have ha : 0 ≤ a := List.sum_nonneg (by intro i hi; rcases List.mem_map.mp hi with ⟨x,_,rfl⟩; exact signal_nonneg x) + change (n:ℝ) ≤ 64*a + change _ ≤ _*4*a at he + have hE : (n:ℝ)/4 ≤ (L.map f).sum := by nlinarith + have hY : ((n:ℝ)/4)^2 ≤ (n:ℝ)*4*a := (pow_le_pow_left₀ (by positivity) hE 2).trans (he.trans (by gcongr)) + nlinarith + +private lemma fades_suffix (l l' : List (Pulse M)) {a : Bool} (h : fading (l++l') a) : + (∃ b, fading l' b) ∧ ((∃ i∈l, i.active=false) → fading l' false) := by + induction l generalizing a with + | nil => + refine ⟨⟨a,h⟩,?_⟩; simp + | cons i l ih => + change (_→_)∧fading (l++l') i.active at h + obtain ⟨hh,hx⟩ := ih h.2 + refine ⟨hh,?_⟩ + rintro ⟨x,hx',hf⟩ + rcases List.mem_cons.mp hx' with H|H; subst x + · have he (l l' : List (Pulse M)) : fading (l++l') false → fading l' false := by + induction l with + | nil => exact fun h => h + | cons i l ih => + rintro ⟨h,h'⟩ + apply ih + have hh : i.active=false := by cases H : i.active <;> simp_all + exact hh ▸ h' + exact he l l' (hf ▸ h.2) + exact hx ⟨x,H,hf⟩ + +theorem mark_intervals (p : Policy M) (t : Wire M s) {a : Bool} (h : t.Valid a) + (K : ℕ) (hk : ∀ x : Scene M, K=0 ∨ reach (p x) ≤ K) (x : Grid M) : + (labels p (t.read x):ℝ) ≤ 64*(((t.read x).map (due K)).sum) + K := by + have hv0 (z : List (Pulse M)) : 0 ≤ (z.map (due K)).sum := + List.sum_nonneg (by intro i hi; rcases List.mem_map.mp hi with ⟨x,_,rfl⟩; exact due_nonneg K x) + have hc : ∀ k : ℕ, ∀ (l : List (Pulse M)), l.length=k → ∀ a, fading l a → + (labels p l:ℝ) ≤ 64*((l.map (due K)).sum) + K := by + intro k; induction k using Nat.strong_induction_on with + | h k ih => + intro l hl a ha + cases l with + | nil => simp [labels] + | cons e l => + let Z := e::l + rcases ha with ⟨H,hh⟩ + by_cases ht : e.active=true ∧ startMark (p e.scene) (e::l) + · let n := reach (p e.scene) + let U := Z.take n; let V := Z.drop n + have hE : U ++ V= Z := List.take_append_drop n Z + have he : V.length < k := by + have h1 : 0 < n := reach_pos _ + change (e::l).length=k at hl; unfold V Z; rw [List.length_drop] + simp only [List.length_cons] at *; omega + have hT : (labels p Z:ℝ) ≤ (U.length:ℝ)+labels p V := by + have hh (l : List (Pulse M)) : + labels p (l++V) ≤ l.length+labels p V := by + induction l with + | nil => simp + | cons i l ih => + change tags p i (l++V) + labels p (l++V) ≤ _ + have ht : tags p i (l++V) ≤ 1 := by unfold tags; split_ifs <;> omega + simp only [List.length_cons] + omega + have H := hh U; rw [hE] at H + exact_mod_cast H + have hx1 := mark_rent (e::l) _ ht.2 + have HX : fading Z a := ⟨H,hh⟩ + rw [← hE] at HX + obtain ⟨⟨b,hx⟩,hx'⟩ := fades_suffix U V HX + have hz : (Z.map (due K)).sum = (U.map (due K)).sum + (V.map (due K)).sum := + calc + _ = ((U++V).map (due K)).sum := by rw [hE] + _ = _ := by simp + have hn : (U.length:ℝ) ≤ n := by simp [U] + change (labels p Z:ℝ) ≤ 64* (Z.map (due K)).sum + K + rw [hz] + by_cases HH : ∀ y∈U, K=0 ∨ y.active=true + · have hhi := ih _ he V rfl b hx + change (n:ℝ) ≤ 64*(U.map Pulse.signal).sum at hx1 + have hQ : U.map (due K) = U.map Pulse.signal := + List.map_congr_left (fun i hi => ite_eq_left (HH i hi)) + rw [hQ] + linarith + push Not at HH + obtain ⟨i,hi,hK,hi'⟩ := HH + have hk : (n:ℝ) ≤ K := by + exact_mod_cast ((hk _).resolve_left hK : n ≤ K) + have hi'' : fading V false := + hx' ⟨i,hi,by cases H:i.active <;> simp_all⟩ + have hj (l : List (Pulse M)) (hh : ∀ i∈l,i.active=false) : labels p l=0 := by + induction l with + | nil => rfl + | cons i l ih => + have he : i.active=false := hh i List.mem_cons_self + have hV := ih fun i hi => hh i (List.mem_cons_of_mem _ hi) + simp [labels, tags,hV, he] + rw [hj _ (faded V hi''),Nat.cast_zero] at hT + linarith [hv0 V,hv0 U] + have HE : tags p e l=0 := ite_eq_right ht + have h1 := ih _ (by rw [List.length_cons] at hl; omega : l.length < k) l rfl _ hh + simp only [labels,HE,zero_add,List.map_cons,List.sum_cons] + linarith [due_nonneg K e] + exact hc _ (t.read x) rfl a (t.fades h x) + +end FourierLLogL.Dyadic.Procedure.Wire + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Window.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Window.lean index 9f7ddc774db8cf18f4c8058e8c3cf7e2441c7db3..9b4cf3d9da555737c43200abca3cedac30284361 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Window.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Window.lean @@ -1,166 +1,166 @@ -import OAI.Analysis.FourierLLogL.Edges.Wire -/-! # Cost of an observed boundary window -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal - -namespace Wire -variable {M : ℕ} {s : Scene M} {t : Wire M s} -open scoped Classical - -private lemma spotted_equal {s : Scene M} (h : ReadyAt s) (hm : 0 < s.mass) - (q : Grid M → Prop) (hq : spotted q s.here.lev) : - s.actor.prob q = (s.here.dice M).prob q := by - let c := s.here.start M - have hc : c∈s.here.block M := start_mem h.1.1 h.1.2 - have hh (P : Law (Grid M)) (hp : ∀ x, 0 < P x → x∈s.here.block M) : - P.prob q = if q c then 1 else 0 := by - unfold prob - conv_rhs => rw [← Ex_const P (if q c then 1 else 0)] - refine Ex_congr_mem _ fun x hx => ?_ - have H := hq _ rfl _ (hp x hx) _ hc - by_cases hv : q c <;> simp [hv,H] - exact (hh _ (piece_support h.1 (actor_def h.1 h.2 hm))).trans (hh _ - (fun x hx => (dice_positive h.1).mp hx)).symm - -def coverL (n : ℕ) (l : List (Pulse M)) : ℝ := ((l.take n).map Pulse.realized).sum - -def fairValue (q : Grid M → Prop) (n : ℕ) (l : List (Pulse M)) (x : Grid M) := - if q x then exp (-coverL n l) else 0 - -private lemma present {s : Scene M} (t : Wire M s) {a : Bool} (h : t.Valid a) - (hm : 0 < s.mass) (n : ℕ) (hn : s.here.lev+n ≤ M) (q : Grid M → Prop) - (hq : spotted q (s.here.lev+n)) : - s.actor.Ex (fun x => fairValue q n (t.read x) x) ≤ (s.here.dice M).prob q := by - classical - induction t generalizing n a with - | term s => - obtain ⟨hh,he⟩ := h - have H : n = 0 := by omega - subst n - simp only [Nat.add_zero] at hq - rw [← spotted_equal hh hm _ hq] - unfold prob; simp only [fairValue,coverL,List.take_zero,List.map_nil,List.sum_nil, - neg_zero,exp_zero]; exact le_rfl - | stay s v k ih => - cases v <;> exact ih h.2 hm n hn hq - | spot s u k ih => - obtain ⟨ha,h2,hH,hk⟩ := h - have he := hH hm - rw [spotEx u k (fairValue q n) he] - rw [← Ex_const u.snip.probs ((s.here.dice M).prob q)] - apply Ex_mono_mem - intro b hb - have hx := ih b (hk b) ((true_mass he).mp hb) n - (by rw [h2 b]; exact hn) (by rw [h2 b]; exact hq) - rw [h2 b] at hx - exact hx - | link s g k ih => - obtain ⟨ha,hG,hV,hk⟩ := h - have he : (step s).TrueLaw := step_true ha.1.1 hG ha.2 hm - cases n with - | zero => - simp only [Nat.add_zero] at hq - rw [← spotted_equal ha hm _ hq] - unfold prob; simp only [fairValue,coverL,List.take_zero,List.map_nil,List.sum_nil, - neg_zero,exp_zero]; exact le_rfl - | succ n => - rw [linkEx g k (fairValue q (n+1)) he] - let P := (step s).probs - let V (b) := (s.smaller b).actor - let Z (b) := fun x => fairValue q (n+1) (⟨s,g,b⟩::(k b).read x) x - have HE (b : Bool) : - P b * (V b).Ex (Z b) ≤ (1/2)*(s.smaller b |>.here.dice M).prob q := by - rcases eq_or_lt_of_le (P.nonneg b) with hi|hi - · rw [← hi,zero_mul] - apply mul_nonneg (by norm_num) (prob_nonneg _ _) - have hP : (s.smaller b).here.lev+n=s.here.lev+(n+1) := by cases b <;> - simp [smaller,child,L,R,add_comm n,add_assoc] - have H := ih b (hk b) ((true_mass he).mp hi) n (by rw [hP]; exact hn) - (by rw [hP]; exact hq) - have hZ (x : Grid M) : Z b x = - (2*P b)⁻¹ * fairValue q n ((k b).read x) x := by - unfold Z fairValue; split_ifs - · unfold coverL; simp only [List.take_succ_cons,List.map_cons,List.sum_cons, Pulse.realized] - rw [show -(whistle _ _+_)= -whistle (step s) b + -coverL n ((k b).read x) by - unfold coverL; ring,exp_add,Real.exp_neg,whistle,exp_log (by change 0< 2* P b; positivity)] - rfl - · ring - - rw [funext hZ,Ex_lmul, ← mul_assoc] - rw [show P b*(2*P b)⁻¹=1/2 by field_simp] - exact mul_le_mul_of_nonneg_left H (by norm_num) - change P.Ex (fun b => (V b).Ex (Z b)) ≤ _ - rw [Ex,Fintype.sum_bool] - apply (add_le_add (HE true) (HE false)).trans - unfold prob - rw [dice_child s.here ha.1.1 hG] - simp only [smaller,child] - linarith - -def startMark (i : ℕ) (l : List (Pulse M)) : Prop := - reach i * log 2/2 ≤ coverL (reach i) l - -private lemma distance_scale (i : ℕ) (main : Bool) : - exp ((reach i)*log 2/2)*(4*side (reach i)) ≤ gateWidth main i/4 := by - let n := 40+4*i - have hi : reach i = 2*n := by unfold reach n; omega - have hE : - exp (((reach i):ℝ)*log 2/2)*(4*side (reach i)) = 4 * side n := by - rw [hi,Nat.cast_mul,Nat.cast_ofNat,show 2*(n:ℝ)*log 2/2=log (2^n) by rw [log_pow]; ring, - exp_log (by positivity : 0 < (2:ℝ)^n)] - unfold side - rw [mul_comm 2 n,pow_mul,one_div,inv_pow] - field_simp - rw [hE] - have he : side n = side 40 * side i ^ 4 := by - unfold n side; rw [pow_add,mul_comm 4 i,pow_mul] - unfold gateWidth; rw [he] - have hQ : side i^4 ≤ side i^2 := - pow_le_pow_of_le_one (side_pos _).le (side_le _) (by omega) - have h₂ : side i^2 ≤ side i := by - simpa using pow_le_pow_of_le_one (side_pos i).le (side_le i) (by omega : 1 ≤ 2) - split <;> norm_num [side] at * <;> nlinarith - -theorem mark_chance {s : Scene M} (t : Wire M s) {a : Bool} (h : t.Valid a) - (hm : 0 < s.mass) (ht : ¬ enter s) (main : Bool) (i : ℕ) (hh : hazard s main i) : - let P := s.actor - P.prob (flanked s.here i) ≤ 2*P.prob (fun x => startMark i (t.read x)) := by - obtain ⟨hx1,hx2⟩ := h.atSrc - obtain ⟨hi,hj,hc⟩ := hazard_rule hx1 hx2 hm ht hh - let q := flanked (M:=M) s.here i - let n := reach i; let r := n * log 2/2 - intro P - have HH := present t h hm n hi q (flanked_at _ _) - have H₁ := HH.trans (flank_prob _ hx1.1 i hi) - let f := fun x => fairValue q n (t.read x) x - let b := fun x => startMark i (t.read x) - let F (x) := if b x then (1:ℝ) else 0 - have H : - P.prob q ≤ P.prob b + exp r*P.Ex f := by - unfold prob - rw [← Ex_lmul, ← Ex_add] - refine Ex_mono _ fun x => ?_ - unfold f fairValue - by_cases h1 : q x - · simp only [h1,ite_true] - split_ifs with H - · have H : 0 ≤ exp r * exp (-coverL n (t.read x)) := by positivity - linarith - rw [zero_add,← exp_add]; apply Real.one_le_exp - change ¬ r ≤ coverL n (t.read x) at H; linarith - simp only [h1,ite_false]; split_ifs <;> norm_num - have hT : exp r * P.Ex f ≤ gateWidth main i /4 := - (mul_le_mul_of_nonneg_left H₁ (exp_nonneg _)).trans (distance_scale i main) - change gateWidth main i/2 ≤ P.prob q at hc - change P.prob q ≤ 2*P.prob b - linarith - -end Wire -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Wire +/-! # Cost of an observed boundary window -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal + +namespace Wire +variable {M : ℕ} {s : Scene M} {t : Wire M s} +open scoped Classical + +private lemma spotted_equal {s : Scene M} (h : ReadyAt s) (hm : 0 < s.mass) + (q : Grid M → Prop) (hq : spotted q s.here.lev) : + s.actor.prob q = (s.here.dice M).prob q := by + let c := s.here.start M + have hc : c∈s.here.block M := start_mem h.1.1 h.1.2 + have hh (P : Law (Grid M)) (hp : ∀ x, 0 < P x → x∈s.here.block M) : + P.prob q = if q c then 1 else 0 := by + unfold prob + conv_rhs => rw [← Ex_const P (if q c then 1 else 0)] + refine Ex_congr_mem _ fun x hx => ?_ + have H := hq _ rfl _ (hp x hx) _ hc + by_cases hv : q c <;> simp [hv,H] + exact (hh _ (piece_support h.1 (actor_def h.1 h.2 hm))).trans (hh _ + (fun x hx => (dice_positive h.1).mp hx)).symm + +def coverL (n : ℕ) (l : List (Pulse M)) : ℝ := ((l.take n).map Pulse.realized).sum + +def fairValue (q : Grid M → Prop) (n : ℕ) (l : List (Pulse M)) (x : Grid M) := + if q x then exp (-coverL n l) else 0 + +private lemma present {s : Scene M} (t : Wire M s) {a : Bool} (h : t.Valid a) + (hm : 0 < s.mass) (n : ℕ) (hn : s.here.lev+n ≤ M) (q : Grid M → Prop) + (hq : spotted q (s.here.lev+n)) : + s.actor.Ex (fun x => fairValue q n (t.read x) x) ≤ (s.here.dice M).prob q := by + classical + induction t generalizing n a with + | term s => + obtain ⟨hh,he⟩ := h + have H : n = 0 := by omega + subst n + simp only [Nat.add_zero] at hq + rw [← spotted_equal hh hm _ hq] + unfold prob; simp only [fairValue,coverL,List.take_zero,List.map_nil,List.sum_nil, + neg_zero,exp_zero]; exact le_rfl + | stay s v k ih => + cases v <;> exact ih h.2 hm n hn hq + | spot s u k ih => + obtain ⟨ha,h2,hH,hk⟩ := h + have he := hH hm + rw [spotEx u k (fairValue q n) he] + rw [← Ex_const u.snip.probs ((s.here.dice M).prob q)] + apply Ex_mono_mem + intro b hb + have hx := ih b (hk b) ((true_mass he).mp hb) n + (by rw [h2 b]; exact hn) (by rw [h2 b]; exact hq) + rw [h2 b] at hx + exact hx + | link s g k ih => + obtain ⟨ha,hG,hV,hk⟩ := h + have he : (step s).TrueLaw := step_true ha.1.1 hG ha.2 hm + cases n with + | zero => + simp only [Nat.add_zero] at hq + rw [← spotted_equal ha hm _ hq] + unfold prob; simp only [fairValue,coverL,List.take_zero,List.map_nil,List.sum_nil, + neg_zero,exp_zero]; exact le_rfl + | succ n => + rw [linkEx g k (fairValue q (n+1)) he] + let P := (step s).probs + let V (b) := (s.smaller b).actor + let Z (b) := fun x => fairValue q (n+1) (⟨s,g,b⟩::(k b).read x) x + have HE (b : Bool) : + P b * (V b).Ex (Z b) ≤ (1/2)*(s.smaller b |>.here.dice M).prob q := by + rcases eq_or_lt_of_le (P.nonneg b) with hi|hi + · rw [← hi,zero_mul] + apply mul_nonneg (by norm_num) (prob_nonneg _ _) + have hP : (s.smaller b).here.lev+n=s.here.lev+(n+1) := by cases b <;> + simp [smaller,child,L,R,add_comm n,add_assoc] + have H := ih b (hk b) ((true_mass he).mp hi) n (by rw [hP]; exact hn) + (by rw [hP]; exact hq) + have hZ (x : Grid M) : Z b x = + (2*P b)⁻¹ * fairValue q n ((k b).read x) x := by + unfold Z fairValue; split_ifs + · unfold coverL; simp only [List.take_succ_cons,List.map_cons,List.sum_cons, Pulse.realized] + rw [show -(whistle _ _+_)= -whistle (step s) b + -coverL n ((k b).read x) by + unfold coverL; ring,exp_add,Real.exp_neg,whistle,exp_log (by change 0< 2* P b; positivity)] + rfl + · ring + + rw [funext hZ,Ex_lmul, ← mul_assoc] + rw [show P b*(2*P b)⁻¹=1/2 by field_simp] + exact mul_le_mul_of_nonneg_left H (by norm_num) + change P.Ex (fun b => (V b).Ex (Z b)) ≤ _ + rw [Ex,Fintype.sum_bool] + apply (add_le_add (HE true) (HE false)).trans + unfold prob + rw [dice_child s.here ha.1.1 hG] + simp only [smaller,child] + linarith + +def startMark (i : ℕ) (l : List (Pulse M)) : Prop := + reach i * log 2/2 ≤ coverL (reach i) l + +private lemma distance_scale (i : ℕ) (main : Bool) : + exp ((reach i)*log 2/2)*(4*side (reach i)) ≤ gateWidth main i/4 := by + let n := 40+4*i + have hi : reach i = 2*n := by unfold reach n; omega + have hE : + exp (((reach i):ℝ)*log 2/2)*(4*side (reach i)) = 4 * side n := by + rw [hi,Nat.cast_mul,Nat.cast_ofNat,show 2*(n:ℝ)*log 2/2=log (2^n) by rw [log_pow]; ring, + exp_log (by positivity : 0 < (2:ℝ)^n)] + unfold side + rw [mul_comm 2 n,pow_mul,one_div,inv_pow] + field_simp + rw [hE] + have he : side n = side 40 * side i ^ 4 := by + unfold n side; rw [pow_add,mul_comm 4 i,pow_mul] + unfold gateWidth; rw [he] + have hQ : side i^4 ≤ side i^2 := + pow_le_pow_of_le_one (side_pos _).le (side_le _) (by omega) + have h₂ : side i^2 ≤ side i := by + simpa using pow_le_pow_of_le_one (side_pos i).le (side_le i) (by omega : 1 ≤ 2) + split <;> norm_num [side] at * <;> nlinarith + +theorem mark_chance {s : Scene M} (t : Wire M s) {a : Bool} (h : t.Valid a) + (hm : 0 < s.mass) (ht : ¬ enter s) (main : Bool) (i : ℕ) (hh : hazard s main i) : + let P := s.actor + P.prob (flanked s.here i) ≤ 2*P.prob (fun x => startMark i (t.read x)) := by + obtain ⟨hx1,hx2⟩ := h.atSrc + obtain ⟨hi,hj,hc⟩ := hazard_rule hx1 hx2 hm ht hh + let q := flanked (M:=M) s.here i + let n := reach i; let r := n * log 2/2 + intro P + have HH := present t h hm n hi q (flanked_at _ _) + have H₁ := HH.trans (flank_prob _ hx1.1 i hi) + let f := fun x => fairValue q n (t.read x) x + let b := fun x => startMark i (t.read x) + let F (x) := if b x then (1:ℝ) else 0 + have H : + P.prob q ≤ P.prob b + exp r*P.Ex f := by + unfold prob + rw [← Ex_lmul, ← Ex_add] + refine Ex_mono _ fun x => ?_ + unfold f fairValue + by_cases h1 : q x + · simp only [h1,ite_true] + split_ifs with H + · have H : 0 ≤ exp r * exp (-coverL n (t.read x)) := by positivity + linarith + rw [zero_add,← exp_add]; apply Real.one_le_exp + change ¬ r ≤ coverL n (t.read x) at H; linarith + simp only [h1,ite_false]; split_ifs <;> norm_num + have hT : exp r * P.Ex f ≤ gateWidth main i /4 := + (mul_le_mul_of_nonneg_left H₁ (exp_nonneg _)).trans (distance_scale i main) + change gateWidth main i/2 ≤ P.prob q at hc + change P.prob q ≤ 2*P.prob b + linarith + +end Wire +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Edges/Wire.lean b/lean/OAI/Analysis/FourierLLogL/Edges/Wire.lean index 92744bad7376146ea08f87e1835bf55588424c37..d0dbce2fe2b1383f0d94cb17b4646ef72d5866c0 100644 --- a/lean/OAI/Analysis/FourierLLogL/Edges/Wire.lean +++ b/lean/OAI/Analysis/FourierLLogL/Edges/Wire.lean @@ -1,143 +1,143 @@ -import OAI.Analysis.FourierLLogL.Procedure.Fisher -import OAI.Analysis.FourierLLogL.Edges.Fringe - -/-! # Complete spatial wires reading every bit to leaf -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal - -variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -namespace Fork -/-- dry, single-bit price -/ -def pitch (q : Fork M) := log 2 - q.probs.entropy -def whistle (q : Fork M) (b : Bool) := log (2*q.probs b) - -lemma pitch_nonneg (q : Fork M) : 0 ≤ q.pitch := by - unfold pitch; have h := entropy_le_log_card q.probs; simpa [sub_nonneg] using h - -lemma pitch_eq (q : Fork M) : q.pitch=q.probs.Ex q.whistle := by - unfold pitch entropy crossE whistle; rw [Ex_neg,sub_neg_eq_add] - conv_lhs => lhs; rw [← Ex_const q.probs (log 2)] - rw [← Ex_add] - exact Ex_congr_mem _ fun i hi => (Real.log_mul (by norm_num) hi.ne').symm - -lemma whistle_high (q : Fork M) (b : Bool) : - max (q.whistle b) 0 ^2 ≤ 4*q.pitch := by - let x := q.probs - have H := bit_cost x - unfold pitch whistle - change max (log (2*x b)) 0 ^2 ≤ 4*(log 2-x.entropy) - apply le_trans (b:=(x false-x true)^2) _ H - by_cases hi : x b=0 - · simp [hi,sq_nonneg] - have he := log_le_sub_one_of_pos (mul_pos (by norm_num : (0:ℝ)<2) - ((x.nonneg b).lt_of_ne (Ne.symm hi))) - have hj : x true+x false=1 := by simpa [Fintype.sum_bool] using x.total - rw [max_def] - split_ifs with h - · simp [sq_nonneg] - cases b - · have hE : 2*x false-1=x false-x true := by linarith - rw [hE] at he; nlinarith - · have hE : 2*x true-1= -(x false-x true) := by linarith - rw [hE] at he; nlinarith - -end Fork - -structure Pulse (M : ℕ) where - scene : Scene M - active : Bool - chosen : Bool - -namespace Pulse -def signal (e : Pulse M) := (step e.scene).pitch -def realized (e : Pulse M) := (step e.scene).whistle e.chosen -lemma signal_nonneg (e : Pulse M) : 0 ≤ e.signal := pitch_nonneg _ -end Pulse - -/-- Generic membership fork, same spatial node both children. -/ -structure TossUp (s : Scene M) where - coord : Grid M → Bool - dst : Bool → Scene M - -def TossUp.snip {s : Scene M} (t : TossUp s) : Fork M := ⟨s,t.dst,t.coord⟩ - -inductive Wire (M : ℕ) : Scene M → Type -| term (s : Scene M) : Wire M s -| stay (s : Scene M) (v : Pace) (t : Wire M (s.pace v)) : Wire M s -| spot (s : Scene M) (t : TossUp s) (k : ∀ b : Bool, Wire M (t.dst b)) : Wire M s -| link (s : Scene M) (activeStep : Bool) (k : ∀ b : Bool, Wire M (s.smaller b)) : Wire M s - -namespace Wire -inductive Part where -| jump | no - -def ReadyAt (s : Scene M) : Prop := s.here.ok M ∧ 0 ≤ s.wage - -/-- valid with active prefix -/ -def Valid {s : Scene M} (t : Wire M s) (a : Bool) : Prop := - match t with - | .term s => ReadyAt s ∧ s.here.lev=M - | .stay s _ k => ReadyAt s ∧ k.Valid a - | .spot s u k => - ReadyAt s ∧ (∀ b, (u.dst b).here=s.here) ∧ (0 < s.mass → u.snip.TrueLaw) ∧ - ∀ b, (k b).Valid a - | .link s v k => - ReadyAt s ∧ s.here.lev < M ∧ (v=true → a=true) ∧ ∀ b, (k b).Valid v - -lemma Valid.atSrc {s : Scene M} {t : Wire M s} {a : Bool} (h : t.Valid a) : - ReadyAt s := by cases t <;> exact h.1 - -def read {s : Scene M} (t : Wire M s) (x : Grid M) : List (Pulse M) := - match t with - | .term _ => [] - | .stay _ _ k => k.read x - | .spot _ u k => (k (u.coord x)).read x - | .link s v k => - let b := (step s).bit x - ⟨s,v,b⟩ :: (k b).read x - -def expenditure {s : Scene M} (t : Wire M s) (f : Pulse M → ℝ) := - s.actor.Ex fun x => ((t.read x).map f).sum - -lemma linkEx {s : Scene M} (v : Bool) (k : ∀ b, Wire M (s.smaller b)) - (t : List (Pulse M) → Grid M → ℝ) (h : (step s).TrueLaw) : - s.actor.Ex (fun x => t ((Wire.link s v k).read x) x) = (step s).probs.Ex (fun b => - (s.smaller b).actor.Ex fun x => t (⟨s,v,b⟩::(k b).read x) x) := by - let f := fun (b : Bool) x => t (⟨s,v,b⟩::(k b).read x) x - exact (expect_bit h f).symm - -lemma spotEx {s : Scene M} (u : TossUp s) (k : ∀ b, Wire M (u.dst b)) (t : List (Pulse M) → Grid M → ℝ) - (h : u.snip.TrueLaw) : - s.actor.Ex (fun x => t ((Wire.spot s u k).read x) x) = u.snip.probs.Ex (fun b => - (u.dst b).actor.Ex fun x => t ((k b).read x) x) := by - let f := fun (b : Bool) x => t ((k b).read x) x - exact (expect_bit h f).symm - -lemma fee_stay {s : Scene M} (v : Pace) (k : Wire M (s.pace v)) (f : Pulse M → ℝ) : - (Wire.stay s v k).expenditure f = k.expenditure f := by cases v <;> rfl - -lemma fee_spot {s : Scene M} (u : TossUp s) (k : ∀ b, Wire M (u.dst b)) (f : Pulse M → ℝ) - (h : u.snip.TrueLaw) : - (Wire.spot s u k).expenditure f = u.snip.probs.Ex (fun b => (k b).expenditure f) := - spotEx u k (fun l _ => (l.map f).sum) h - -lemma fee_link {s : Scene M} (v : Bool) (k : ∀ b, Wire M (s.smaller b)) (f : Pulse M → ℝ) - (h : (step s).TrueLaw) : - (Wire.link s v k).expenditure f = (step s).probs.Ex (fun b => f ⟨s,v,b⟩+(k b).expenditure f) := by - have hh := linkEx v k (fun l _ => (l.map f).sum) h - simp_rw [List.map_cons,List.sum_cons,Ex_add,Ex_const] at hh - unfold expenditure - rw [hh, Ex_add] - - -end Wire -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Fisher +import OAI.Analysis.FourierLLogL.Edges.Fringe + +/-! # Complete spatial wires reading every bit to leaf -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal + +variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +namespace Fork +/-- dry, single-bit price -/ +def pitch (q : Fork M) := log 2 - q.probs.entropy +def whistle (q : Fork M) (b : Bool) := log (2*q.probs b) + +lemma pitch_nonneg (q : Fork M) : 0 ≤ q.pitch := by + unfold pitch; have h := entropy_le_log_card q.probs; simpa [sub_nonneg] using h + +lemma pitch_eq (q : Fork M) : q.pitch=q.probs.Ex q.whistle := by + unfold pitch entropy crossE whistle; rw [Ex_neg,sub_neg_eq_add] + conv_lhs => lhs; rw [← Ex_const q.probs (log 2)] + rw [← Ex_add] + exact Ex_congr_mem _ fun i hi => (Real.log_mul (by norm_num) hi.ne').symm + +lemma whistle_high (q : Fork M) (b : Bool) : + max (q.whistle b) 0 ^2 ≤ 4*q.pitch := by + let x := q.probs + have H := bit_cost x + unfold pitch whistle + change max (log (2*x b)) 0 ^2 ≤ 4*(log 2-x.entropy) + apply le_trans (b:=(x false-x true)^2) _ H + by_cases hi : x b=0 + · simp [hi,sq_nonneg] + have he := log_le_sub_one_of_pos (mul_pos (by norm_num : (0:ℝ)<2) + ((x.nonneg b).lt_of_ne (Ne.symm hi))) + have hj : x true+x false=1 := by simpa [Fintype.sum_bool] using x.total + rw [max_def] + split_ifs with h + · simp [sq_nonneg] + cases b + · have hE : 2*x false-1=x false-x true := by linarith + rw [hE] at he; nlinarith + · have hE : 2*x true-1= -(x false-x true) := by linarith + rw [hE] at he; nlinarith + +end Fork + +structure Pulse (M : ℕ) where + scene : Scene M + active : Bool + chosen : Bool + +namespace Pulse +def signal (e : Pulse M) := (step e.scene).pitch +def realized (e : Pulse M) := (step e.scene).whistle e.chosen +lemma signal_nonneg (e : Pulse M) : 0 ≤ e.signal := pitch_nonneg _ +end Pulse + +/-- Generic membership fork, same spatial node both children. -/ +structure TossUp (s : Scene M) where + coord : Grid M → Bool + dst : Bool → Scene M + +def TossUp.snip {s : Scene M} (t : TossUp s) : Fork M := ⟨s,t.dst,t.coord⟩ + +inductive Wire (M : ℕ) : Scene M → Type +| term (s : Scene M) : Wire M s +| stay (s : Scene M) (v : Pace) (t : Wire M (s.pace v)) : Wire M s +| spot (s : Scene M) (t : TossUp s) (k : ∀ b : Bool, Wire M (t.dst b)) : Wire M s +| link (s : Scene M) (activeStep : Bool) (k : ∀ b : Bool, Wire M (s.smaller b)) : Wire M s + +namespace Wire +inductive Part where +| jump | no + +def ReadyAt (s : Scene M) : Prop := s.here.ok M ∧ 0 ≤ s.wage + +/-- valid with active prefix -/ +def Valid {s : Scene M} (t : Wire M s) (a : Bool) : Prop := + match t with + | .term s => ReadyAt s ∧ s.here.lev=M + | .stay s _ k => ReadyAt s ∧ k.Valid a + | .spot s u k => + ReadyAt s ∧ (∀ b, (u.dst b).here=s.here) ∧ (0 < s.mass → u.snip.TrueLaw) ∧ + ∀ b, (k b).Valid a + | .link s v k => + ReadyAt s ∧ s.here.lev < M ∧ (v=true → a=true) ∧ ∀ b, (k b).Valid v + +lemma Valid.atSrc {s : Scene M} {t : Wire M s} {a : Bool} (h : t.Valid a) : + ReadyAt s := by cases t <;> exact h.1 + +def read {s : Scene M} (t : Wire M s) (x : Grid M) : List (Pulse M) := + match t with + | .term _ => [] + | .stay _ _ k => k.read x + | .spot _ u k => (k (u.coord x)).read x + | .link s v k => + let b := (step s).bit x + ⟨s,v,b⟩ :: (k b).read x + +def expenditure {s : Scene M} (t : Wire M s) (f : Pulse M → ℝ) := + s.actor.Ex fun x => ((t.read x).map f).sum + +lemma linkEx {s : Scene M} (v : Bool) (k : ∀ b, Wire M (s.smaller b)) + (t : List (Pulse M) → Grid M → ℝ) (h : (step s).TrueLaw) : + s.actor.Ex (fun x => t ((Wire.link s v k).read x) x) = (step s).probs.Ex (fun b => + (s.smaller b).actor.Ex fun x => t (⟨s,v,b⟩::(k b).read x) x) := by + let f := fun (b : Bool) x => t (⟨s,v,b⟩::(k b).read x) x + exact (expect_bit h f).symm + +lemma spotEx {s : Scene M} (u : TossUp s) (k : ∀ b, Wire M (u.dst b)) (t : List (Pulse M) → Grid M → ℝ) + (h : u.snip.TrueLaw) : + s.actor.Ex (fun x => t ((Wire.spot s u k).read x) x) = u.snip.probs.Ex (fun b => + (u.dst b).actor.Ex fun x => t ((k b).read x) x) := by + let f := fun (b : Bool) x => t ((k b).read x) x + exact (expect_bit h f).symm + +lemma fee_stay {s : Scene M} (v : Pace) (k : Wire M (s.pace v)) (f : Pulse M → ℝ) : + (Wire.stay s v k).expenditure f = k.expenditure f := by cases v <;> rfl + +lemma fee_spot {s : Scene M} (u : TossUp s) (k : ∀ b, Wire M (u.dst b)) (f : Pulse M → ℝ) + (h : u.snip.TrueLaw) : + (Wire.spot s u k).expenditure f = u.snip.probs.Ex (fun b => (k b).expenditure f) := + spotEx u k (fun l _ => (l.map f).sum) h + +lemma fee_link {s : Scene M} (v : Bool) (k : ∀ b, Wire M (s.smaller b)) (f : Pulse M → ℝ) + (h : (step s).TrueLaw) : + (Wire.link s v k).expenditure f = (step s).probs.Ex (fun b => f ⟨s,v,b⟩+(k b).expenditure f) := by + have hh := linkEx v k (fun l _ => (l.map f).sum) h + simp_rw [List.map_cons,List.sum_cons,Ex_add,Ex_const] at hh + unfold expenditure + rw [hh, Ex_add] + + +end Wire +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Alternative.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Alternative.lean index 5c694aa18fda5d14217437d85e1d72f84a96b8c7..47e2adbb0dd3749a7e0c54f1918c9c9de9437fd6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Alternative.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Alternative.lean @@ -1,93 +1,93 @@ -import OAI.Analysis.FourierLLogL.Finite.Mixture -/-! # Finite-dimensional separation for laws -/ -open Set Function Filter -open scoped BigOperators - -noncomputable section -namespace OAI - -namespace FourierLLogL.Law -universe u v w -variable {α : Type u} {β : Type v} {γ : Type w} [Fintype α] [Fintype β] [Fintype γ] - -lemma continuous_pdf (a : α) : Continuous (fun P : Law α => P a) := - (continuous_apply a).comp continuous_subtype_val - -lemma continuous_entropy : Continuous (fun P : Law α => P.entropy) := by - simp only [entropy_formula] - exact continuous_finsetSum _ fun x _ => Real.continuous_negMulLog.comp (continuous_pdf _) - -lemma continuous_Ex (f : α → ℝ) : Continuous (fun P : Law α => P.Ex f) := - continuous_finsetSum _ fun a _ => (continuous_pdf a).mul continuous_const - -lemma continuous_prob (p : α → Prop) : Continuous (fun P : Law α => P.prob p) := - continuous_Ex _ - -lemma continuous_send_pdf (X : α → β) (b : β) : Continuous (fun P : Law α => P.send X b) := by - simp_rw [send_apply]; exact continuous_prob _ - -lemma continuous_send (X : α → β) : Continuous (fun P : Law α => P.send X) := by - refine Continuous.subtype_mk (p:=(fun f : β → ℝ => (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1)) ?_ _ - exact continuous_pi (fun x => continuous_send_pdf X x) - -lemma continuous_H (X : α → β) : Continuous (fun P : Law α => P.H X) := - continuous_entropy.comp (continuous_send X) - -lemma continuous_Hcond (X : α → β) (Z : α → γ) : Continuous (fun P : Law α => P.Hcond X Z) := - (continuous_H _).sub (continuous_H _) - -/-- Convex minimization over finite distributions with a linear constraint g=0: - if any dual objective (arbitrary multipliers) is at most C somewhere, - there is a constrained law with value at most C. -/ -lemma primal_feasible {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] - (f : Law α → ℝ) (hf : Continuous f) - (g : Law α → E) (hg : Continuous g) - (hfc : ∀ P Q t h0 h1, f (mixture P Q t h0 h1) ≤ t*f P+(1-t)*f Q) - (hgc : ∀ P Q t h0 h1, g (mixture P Q t h0 h1) = t • g P+(1-t) • g Q) - (hP : ∃ P, g P = 0) - (C : ℝ) (hC : ∀ L : E →L[ℝ] ℝ, ∃ P, f P + L (g P) ≤ C) : - ∃ P, f P ≤ C ∧ g P = 0 := by - classical - let K : Set (E×ℝ) := {x | ∃ P, g P = x.1 ∧ f P ≤ x.2} - let S : Set ((Law α)×(E×ℝ)) := {z | g z.1 = z.2.1} ∩ {z | f z.1 ≤ z.2.2} - have hK : K = Prod.snd '' S := by - ext - simp [K,S] - have hs : IsClosed S := - (isClosed_eq (hg.comp continuous_fst) continuous_snd.fst).inter - (isClosed_le (hf.comp continuous_fst) continuous_snd.snd) - have hkc : IsClosed K := hK ▸ isClosedMap_snd_of_compactSpace _ hs - have hkv : Convex ℝ K := by - rintro x ⟨P,h1,h2⟩ y ⟨Q,h3,h4⟩ a b ha hb hab - have ha' : a ≤ 1 := by linarith - refine ⟨mixture P Q a ha ha',?_ ,?_⟩ - · rw [hgc,h1,h3,show 1-a=b by linarith] - rfl - · change _ ≤ a*x.2+b*y.2 - exact (hfc P Q a ha ha').trans (by rw [show 1-a=b by linarith]; gcongr) - suffices H : (0,C) ∈ K by - obtain ⟨P,hp,hq⟩ := H; exact ⟨P,hq,hp⟩ - by_contra hk - obtain ⟨L,r,ha,hb⟩ := geometric_hahn_banach_point_closed hkv hkc hk - let l := L.comp (ContinuousLinearMap.inl ℝ E ℝ) - let m := L (0,1) - have hl (x : E) (t : ℝ) : L (x,t) = l x + t*m := by - have heq : (x,t) = (x,(0:ℝ))+t • ((0:E),(1:ℝ)) := by simp - rw [heq,map_add,map_smul]; rfl - have hA : C*m < r := by simpa [hl] using ha - have hB (P : Law α) : r < l (g P) + f P*m := hl _ _ ▸ hb (g P,f P) ⟨P,rfl,le_refl _⟩ - obtain ⟨P,hgp⟩ := hP - have hc : C < f P := lt_of_not_ge (fun hh => hk ⟨P,hgp,hh⟩) - have hm : 0 < m := by - have hh := hB P - rw [hgp,map_zero] at hh - nlinarith - obtain ⟨Q,hQ⟩ := hC (m⁻¹ • l) - change f Q + m⁻¹ * l (g Q) ≤ C at hQ - have HH := mul_le_mul_of_nonneg_right hQ hm.le - rw [add_mul, mul_comm (m⁻¹ * _), ← mul_assoc, mul_inv_cancel₀ hm.ne', one_mul] at HH - linarith [hB Q] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Mixture +/-! # Finite-dimensional separation for laws -/ +open Set Function Filter +open scoped BigOperators + +noncomputable section +namespace OAI + +namespace FourierLLogL.Law +universe u v w +variable {α : Type u} {β : Type v} {γ : Type w} [Fintype α] [Fintype β] [Fintype γ] + +lemma continuous_pdf (a : α) : Continuous (fun P : Law α => P a) := + (continuous_apply a).comp continuous_subtype_val + +lemma continuous_entropy : Continuous (fun P : Law α => P.entropy) := by + simp only [entropy_formula] + exact continuous_finsetSum _ fun x _ => Real.continuous_negMulLog.comp (continuous_pdf _) + +lemma continuous_Ex (f : α → ℝ) : Continuous (fun P : Law α => P.Ex f) := + continuous_finsetSum _ fun a _ => (continuous_pdf a).mul continuous_const + +lemma continuous_prob (p : α → Prop) : Continuous (fun P : Law α => P.prob p) := + continuous_Ex _ + +lemma continuous_send_pdf (X : α → β) (b : β) : Continuous (fun P : Law α => P.send X b) := by + simp_rw [send_apply]; exact continuous_prob _ + +lemma continuous_send (X : α → β) : Continuous (fun P : Law α => P.send X) := by + refine Continuous.subtype_mk (p:=(fun f : β → ℝ => (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1)) ?_ _ + exact continuous_pi (fun x => continuous_send_pdf X x) + +lemma continuous_H (X : α → β) : Continuous (fun P : Law α => P.H X) := + continuous_entropy.comp (continuous_send X) + +lemma continuous_Hcond (X : α → β) (Z : α → γ) : Continuous (fun P : Law α => P.Hcond X Z) := + (continuous_H _).sub (continuous_H _) + +/-- Convex minimization over finite distributions with a linear constraint g=0: + if any dual objective (arbitrary multipliers) is at most C somewhere, + there is a constrained law with value at most C. -/ +lemma primal_feasible {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] + (f : Law α → ℝ) (hf : Continuous f) + (g : Law α → E) (hg : Continuous g) + (hfc : ∀ P Q t h0 h1, f (mixture P Q t h0 h1) ≤ t*f P+(1-t)*f Q) + (hgc : ∀ P Q t h0 h1, g (mixture P Q t h0 h1) = t • g P+(1-t) • g Q) + (hP : ∃ P, g P = 0) + (C : ℝ) (hC : ∀ L : E →L[ℝ] ℝ, ∃ P, f P + L (g P) ≤ C) : + ∃ P, f P ≤ C ∧ g P = 0 := by + classical + let K : Set (E×ℝ) := {x | ∃ P, g P = x.1 ∧ f P ≤ x.2} + let S : Set ((Law α)×(E×ℝ)) := {z | g z.1 = z.2.1} ∩ {z | f z.1 ≤ z.2.2} + have hK : K = Prod.snd '' S := by + ext + simp [K,S] + have hs : IsClosed S := + (isClosed_eq (hg.comp continuous_fst) continuous_snd.fst).inter + (isClosed_le (hf.comp continuous_fst) continuous_snd.snd) + have hkc : IsClosed K := hK ▸ isClosedMap_snd_of_compactSpace _ hs + have hkv : Convex ℝ K := by + rintro x ⟨P,h1,h2⟩ y ⟨Q,h3,h4⟩ a b ha hb hab + have ha' : a ≤ 1 := by linarith + refine ⟨mixture P Q a ha ha',?_ ,?_⟩ + · rw [hgc,h1,h3,show 1-a=b by linarith] + rfl + · change _ ≤ a*x.2+b*y.2 + exact (hfc P Q a ha ha').trans (by rw [show 1-a=b by linarith]; gcongr) + suffices H : (0,C) ∈ K by + obtain ⟨P,hp,hq⟩ := H; exact ⟨P,hq,hp⟩ + by_contra hk + obtain ⟨L,r,ha,hb⟩ := geometric_hahn_banach_point_closed hkv hkc hk + let l := L.comp (ContinuousLinearMap.inl ℝ E ℝ) + let m := L (0,1) + have hl (x : E) (t : ℝ) : L (x,t) = l x + t*m := by + have heq : (x,t) = (x,(0:ℝ))+t • ((0:E),(1:ℝ)) := by simp + rw [heq,map_add,map_smul]; rfl + have hA : C*m < r := by simpa [hl] using ha + have hB (P : Law α) : r < l (g P) + f P*m := hl _ _ ▸ hb (g P,f P) ⟨P,rfl,le_refl _⟩ + obtain ⟨P,hgp⟩ := hP + have hc : C < f P := lt_of_not_ge (fun hh => hk ⟨P,hgp,hh⟩) + have hm : 0 < m := by + have hh := hB P + rw [hgp,map_zero] at hh + nlinarith + obtain ⟨Q,hQ⟩ := hC (m⁻¹ • l) + change f Q + m⁻¹ * l (g Q) ≤ C at hQ + have HH := mul_le_mul_of_nonneg_right hQ hm.le + rw [add_mul, mul_comm (m⁻¹ * _), ← mul_assoc, mul_inv_cancel₀ hm.ne', one_mul] at HH + linarith [hB Q] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Comparison.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Comparison.lean index 9a609eb9bbce98e143f737b2d8a0b2d5be550ebd..83cac5e5c1472faffd8043b2808334cdf2d97c59 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Comparison.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Comparison.lean @@ -1,132 +1,132 @@ -import OAI.Analysis.FourierLLogL.Finite.Entropy -/-! # Comparison, data processing and Pinsker -/ - -open Function Filter Set -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL.Law -open Real -universe u v w w' -variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} - [Fintype α] [Fintype β] [Fintype γ] [Fintype η] - -lemma kl_ex (P Q : Law α) : - P.kl Q = P.Ex (fun x => log (P x) - log (Q x)) := by - unfold kl entropy crossE - simp only [Ex_sub, Ex_neg] - ring - -lemma after_dominated {P Q : Law α} (h : P.dominated Q) (X : α → β) - {x : β} (hx : 0 < P.send X x) : (P.after X x).dominated (Q.after X x) := by - intro a ha - obtain ⟨rfl, hp⟩ := (after_mem hx).mp ha - exact Q.after_self_pos X (h a hp) - -lemma kl_chain {P Q : Law α} (h : P.dominated Q) (X : α → β) : - P.kl Q = (P.send X).kl (Q.send X) + - (P.send X).Ex (fun x => (P.after X x).kl (Q.after X x)) := by - classical - simp_rw [kl_ex] - have hA := P.after_Ex_sum X (fun x a => log ((P.after X x) a)-log ((Q.after X x) a)) - rw [hA,Ex_send, ← Ex_add] - apply Ex_congr_mem - intro a ha - have hh (P : Law α) (ha : 0 < P a) : - log (P a) = log (P.send X (X a)) + log (P.after X (X a) a) := by - rw [← log_mul (P.send_pos X ha).ne' (P.after_self_pos X ha).ne', - send_mul_after, ite_eq_left rfl] - - linarith [hh P ha,hh Q (h a ha)] - -lemma kl_data_processing {P Q : Law α} (h : P.dominated Q) (X : α → β) : - (P.send X).kl (Q.send X) ≤ P.kl Q := by - rw [kl_chain h X] - have hh : 0 ≤ (P.send X).Ex (fun x => (P.after X x).kl (Q.after X x)) := by - rw [← Ex_const (P.send X) 0] - apply Ex_mono_mem - exact fun x hx => kl_nonneg (after_dominated h _ hx) - linarith - -def tv (P Q : Law α) := (∑ a, |P a - Q a|) / 2 - -lemma tv_nonneg (P Q : Law α) : 0 ≤ P.tv Q := by unfold tv; positivity - -lemma tv_comm (P Q : Law α) : P.tv Q = Q.tv P := by unfold tv; simp_rw [abs_sub_comm (P _)] - -lemma tv_common (P Q : Law α) : - (∑ a, min (P a) (Q a)) = 1 - P.tv Q := by - have H (a : α) : min (P a) (Q a) = (P a + Q a - |P a - Q a|)/2 := by grind - simp_rw [H, ← Finset.sum_div, Finset.sum_sub_distrib, Finset.sum_add_distrib, Law.total] - unfold tv; ring - -lemma tv_le_one (P Q : Law α) : P.tv Q ≤ 1 := by - have hh : 0 ≤ ∑ a, min (P a) (Q a) := - Finset.sum_nonneg fun a _ => le_min (P.nonneg _) (Q.nonneg _) - rw [tv_common] at hh; linarith - -lemma tv_data_processing (P Q : Law α) (X : α → β) : - (P.send X).tv (Q.send X) ≤ P.tv Q := by - classical - suffices H : (∑ y : β, |P.send X y - Q.send X y|) ≤ ∑ x : α, |P x-Q x| by - unfold tv - exact div_le_div_of_nonneg_right H (by norm_num) - simp_rw [send_apply,prob_eq, ← Finset.sum_sub_distrib] - refine (Finset.sum_le_sum (fun y _ => Finset.abs_sum_le_sum_abs _ _)).trans ?_ - rw [Finset.sum_comm] - refine Finset.sum_le_sum fun x _ => ?_ - rw [Finset.sum_eq_single (X x)] <;> intros <;> simp_all [eq_comm] - -/-- Pinsker (only need numerical constant). Use log tangent inequality on square roots. -/ -lemma tv_sq_le_kl {P Q : Law α} (h : P.dominated Q) : - (P.tv Q)^2 ≤ P.kl Q := by - let a (x) := √(P x) - let b (x) := √(Q x) - have ha (x : α) : (a x)^2 = P x := sq_sqrt (P.nonneg x) - have hb (x : α) : (b x)^2 = Q x := sq_sqrt (Q.nonneg x) - have hkl : ∑ x, (a x - b x)^2 ≤ P.kl Q := by - have H (x : α) : - (a x - b x)^2 ≤ P x * (log (P x)-log (Q x)) - P x + Q x := by - obtain hx | hx := eq_or_lt_of_le (P.nonneg x) - · have hh : a x = 0 := by simp only [a, ← hx,sqrt_zero] - rw [hh, ← hx]; nlinarith [hb x] - have hu : 0 < a x := sqrt_pos.mpr hx - have hv : 0 < b x := sqrt_pos.mpr (h x hx) - have hh := log_le_sub_one_of_pos (div_pos hv hu) - rw [log_div hv.ne' hu.ne'] at hh - have heq (c : ℝ) : log (c^2) = 2*log c := by rw [Real.log_pow]; norm_num - have hg := mul_le_mul_of_nonneg_right hh (sq_nonneg (a x)) - have he : b x/a x * (a x)^2 = a x * b x := by field_simp - rw [sub_mul, sub_mul, he] at hg - rw [← ha x, ← hb x,heq,heq] - nlinarith - have hh := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) - simp only [Finset.sum_add_distrib,Finset.sum_sub_distrib,Law.total,sub_add_cancel] at hh - rw [kl_ex]; exact hh - have hab1 : (∑ x, (a x + b x)^2) ≤ 4 := by - have H (x : α) : (a x+b x)^2 ≤ 2*P x+2*Q x := by - rw [← ha x,← hb x] - nlinarith [sq_nonneg (a x - b x)] - have hh := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) - simp only [Finset.sum_add_distrib,← Finset.mul_sum,Law.total] at hh - linarith - have hdiff (x : α) : |P x-Q x| = |a x - b x| * (a x+b x) := by - rw [← ha x, ← hb x, sq_sub_sq, abs_mul, - abs_of_nonneg (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _))] - exact mul_comm _ _ - have hh := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ - (fun x => |a x-b x|) (fun x => a x+b x) - simp_rw [← hdiff,sq_abs] at hh - have hi := mul_le_mul hkl hab1 (by positivity) (kl_nonneg h) - unfold tv - nlinarith [hh.trans hi] - -lemma tv_le_sqrt_kl {P Q : Law α} (h : P.dominated Q) : - P.tv Q ≤ √(P.kl Q) := by - nlinarith [tv_sq_le_kl h, Real.sq_sqrt (kl_nonneg h), Real.sqrt_nonneg (P.kl Q)] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Entropy +/-! # Comparison, data processing and Pinsker -/ + +open Function Filter Set +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL.Law +open Real +universe u v w w' +variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} + [Fintype α] [Fintype β] [Fintype γ] [Fintype η] + +lemma kl_ex (P Q : Law α) : + P.kl Q = P.Ex (fun x => log (P x) - log (Q x)) := by + unfold kl entropy crossE + simp only [Ex_sub, Ex_neg] + ring + +lemma after_dominated {P Q : Law α} (h : P.dominated Q) (X : α → β) + {x : β} (hx : 0 < P.send X x) : (P.after X x).dominated (Q.after X x) := by + intro a ha + obtain ⟨rfl, hp⟩ := (after_mem hx).mp ha + exact Q.after_self_pos X (h a hp) + +lemma kl_chain {P Q : Law α} (h : P.dominated Q) (X : α → β) : + P.kl Q = (P.send X).kl (Q.send X) + + (P.send X).Ex (fun x => (P.after X x).kl (Q.after X x)) := by + classical + simp_rw [kl_ex] + have hA := P.after_Ex_sum X (fun x a => log ((P.after X x) a)-log ((Q.after X x) a)) + rw [hA,Ex_send, ← Ex_add] + apply Ex_congr_mem + intro a ha + have hh (P : Law α) (ha : 0 < P a) : + log (P a) = log (P.send X (X a)) + log (P.after X (X a) a) := by + rw [← log_mul (P.send_pos X ha).ne' (P.after_self_pos X ha).ne', + send_mul_after, ite_eq_left rfl] + + linarith [hh P ha,hh Q (h a ha)] + +lemma kl_data_processing {P Q : Law α} (h : P.dominated Q) (X : α → β) : + (P.send X).kl (Q.send X) ≤ P.kl Q := by + rw [kl_chain h X] + have hh : 0 ≤ (P.send X).Ex (fun x => (P.after X x).kl (Q.after X x)) := by + rw [← Ex_const (P.send X) 0] + apply Ex_mono_mem + exact fun x hx => kl_nonneg (after_dominated h _ hx) + linarith + +def tv (P Q : Law α) := (∑ a, |P a - Q a|) / 2 + +lemma tv_nonneg (P Q : Law α) : 0 ≤ P.tv Q := by unfold tv; positivity + +lemma tv_comm (P Q : Law α) : P.tv Q = Q.tv P := by unfold tv; simp_rw [abs_sub_comm (P _)] + +lemma tv_common (P Q : Law α) : + (∑ a, min (P a) (Q a)) = 1 - P.tv Q := by + have H (a : α) : min (P a) (Q a) = (P a + Q a - |P a - Q a|)/2 := by grind + simp_rw [H, ← Finset.sum_div, Finset.sum_sub_distrib, Finset.sum_add_distrib, Law.total] + unfold tv; ring + +lemma tv_le_one (P Q : Law α) : P.tv Q ≤ 1 := by + have hh : 0 ≤ ∑ a, min (P a) (Q a) := + Finset.sum_nonneg fun a _ => le_min (P.nonneg _) (Q.nonneg _) + rw [tv_common] at hh; linarith + +lemma tv_data_processing (P Q : Law α) (X : α → β) : + (P.send X).tv (Q.send X) ≤ P.tv Q := by + classical + suffices H : (∑ y : β, |P.send X y - Q.send X y|) ≤ ∑ x : α, |P x-Q x| by + unfold tv + exact div_le_div_of_nonneg_right H (by norm_num) + simp_rw [send_apply,prob_eq, ← Finset.sum_sub_distrib] + refine (Finset.sum_le_sum (fun y _ => Finset.abs_sum_le_sum_abs _ _)).trans ?_ + rw [Finset.sum_comm] + refine Finset.sum_le_sum fun x _ => ?_ + rw [Finset.sum_eq_single (X x)] <;> intros <;> simp_all [eq_comm] + +/-- Pinsker (only need numerical constant). Use log tangent inequality on square roots. -/ +lemma tv_sq_le_kl {P Q : Law α} (h : P.dominated Q) : + (P.tv Q)^2 ≤ P.kl Q := by + let a (x) := √(P x) + let b (x) := √(Q x) + have ha (x : α) : (a x)^2 = P x := sq_sqrt (P.nonneg x) + have hb (x : α) : (b x)^2 = Q x := sq_sqrt (Q.nonneg x) + have hkl : ∑ x, (a x - b x)^2 ≤ P.kl Q := by + have H (x : α) : + (a x - b x)^2 ≤ P x * (log (P x)-log (Q x)) - P x + Q x := by + obtain hx | hx := eq_or_lt_of_le (P.nonneg x) + · have hh : a x = 0 := by simp only [a, ← hx,sqrt_zero] + rw [hh, ← hx]; nlinarith [hb x] + have hu : 0 < a x := sqrt_pos.mpr hx + have hv : 0 < b x := sqrt_pos.mpr (h x hx) + have hh := log_le_sub_one_of_pos (div_pos hv hu) + rw [log_div hv.ne' hu.ne'] at hh + have heq (c : ℝ) : log (c^2) = 2*log c := by rw [Real.log_pow]; norm_num + have hg := mul_le_mul_of_nonneg_right hh (sq_nonneg (a x)) + have he : b x/a x * (a x)^2 = a x * b x := by field_simp + rw [sub_mul, sub_mul, he] at hg + rw [← ha x, ← hb x,heq,heq] + nlinarith + have hh := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) + simp only [Finset.sum_add_distrib,Finset.sum_sub_distrib,Law.total,sub_add_cancel] at hh + rw [kl_ex]; exact hh + have hab1 : (∑ x, (a x + b x)^2) ≤ 4 := by + have H (x : α) : (a x+b x)^2 ≤ 2*P x+2*Q x := by + rw [← ha x,← hb x] + nlinarith [sq_nonneg (a x - b x)] + have hh := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) + simp only [Finset.sum_add_distrib,← Finset.mul_sum,Law.total] at hh + linarith + have hdiff (x : α) : |P x-Q x| = |a x - b x| * (a x+b x) := by + rw [← ha x, ← hb x, sq_sub_sq, abs_mul, + abs_of_nonneg (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _))] + exact mul_comm _ _ + have hh := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ + (fun x => |a x-b x|) (fun x => a x+b x) + simp_rw [← hdiff,sq_abs] at hh + have hi := mul_le_mul hkl hab1 (by positivity) (kl_nonneg h) + unfold tv + nlinarith [hh.trans hi] + +lemma tv_le_sqrt_kl {P Q : Law α} (h : P.dominated Q) : + P.tv Q ≤ √(P.kl Q) := by + nlinarith [tv_sq_le_kl h, Real.sq_sqrt (kl_nonneg h), Real.sqrt_nonneg (P.kl Q)] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Complex.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Complex.lean index 33a1b6a518ab579c24b4e3490c6a3694461d378d..d4abf84496e0ecc6a704c57c2fcc55c05d21b6d5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Complex.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Complex.lean @@ -1,79 +1,79 @@ -import OAI.Analysis.FourierLLogL.Finite.Independent -/-! # Complex moments -/ -open Function Set Filter -open scoped BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Law -open Real -variable {α β γ I : Type} [Fintype α] [Fintype β] [Fintype γ] [Fintype I] [DecidableEq I] - -def Cx (P : Law α) (f : α → ℂ) : ℂ := ∑ x, (P x:ℂ)*f x - -lemma Cx_ofReal (P : Law α) (f : α → ℝ) : P.Cx (fun x => (f x:ℂ)) = ↑(P.Ex f) := by - simp only [Cx,Ex, Complex.ofReal_sum, Complex.ofReal_mul] - -lemma re_Cx (P : Law α) (f : α → ℂ) : - (P.Cx f).re = P.Ex (fun x => (f x).re) := by - simp [Cx,Ex,Complex.re_sum,Complex.mul_re] - -lemma Cx_mul (P : Law α) (f : α → ℂ) (c : ℂ) : P.Cx (fun x => f x*c) = P.Cx f * c := by - simp_rw [Cx, ← mul_assoc, Finset.sum_mul] - -lemma Cx_lmul (P : Law α) (f : α → ℂ) (c : ℂ) : P.Cx (fun x => c*f x) = c*P.Cx f := by - simpa only [mul_comm c] using P.Cx_mul f c - -lemma Cx_conj (P : Law α) (f : α → ℂ) : P.Cx (fun x => conj (f x)) = conj (P.Cx f) := by - simp only [Cx,map_sum,map_mul,Complex.conj_ofReal] - -lemma Cx_norm (P : Law α) (f : α → ℂ) : ‖P.Cx f‖ ≤ P.Ex (fun x => ‖f x‖) := by - unfold Cx - refine (norm_sum_le _ _).trans ?_ - simp_rw [Ex,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (P.nonneg _)] - exact le_rfl - -lemma norm_Cx_unit (P : Law α) (f : α → ℂ) (h : ∀ x, ‖f x‖ ≤ 1) : - ‖P.Cx f‖ ≤ 1 := (P.Cx_norm f).trans ((P.Ex_mono h).trans (P.Ex_const 1).le) - -lemma Cx_swap (P : Law α) (Q : Law β) (f : α → β → ℂ) : - P.Cx (fun x => Q.Cx (f x)) = Q.Cx (fun y => P.Cx (fun x => f x y)) := by - unfold Cx - simp_rw [Finset.mul_sum] - rw [Finset.sum_comm] - apply Finset.sum_congr rfl; intro i _; apply Finset.sum_congr rfl; intro j _; ring - -lemma Ex_swap (P : Law α) (Q : Law β) (f : α → β → ℝ) : - P.Ex (fun x => Q.Ex (f x)) = Q.Ex (fun y => P.Ex (fun x => f x y)) := by - unfold Ex - simp_rw [Finset.mul_sum] - rw [Finset.sum_comm] - simp_rw [mul_left_comm (Q _)] - -lemma Cx_prod (P : I → Law α) (f : I → α → ℂ) : - (pack P).Cx (fun x => ∏ i, f i (x i)) = ∏ i, (P i).Cx (f i) := by - simp_rw [Cx,pack_apply,Complex.ofReal_prod,← Finset.prod_mul_distrib] - exact (Fintype.prod_sum (fun i x => (P i x:ℂ)*f i x)).symm - -lemma sq_norm_Cx (P : Law α) (f : α → ℂ) : - ‖P.Cx f‖^2 = P.Ex (fun x => P.Ex (fun y => (f x*conj (f y)).re)) := by - have heq : P.Cx f * conj (P.Cx f) = - P.Cx (fun x => P.Cx (fun y => f x*conj (f y))) := by - simp_rw [Cx_lmul,Cx_conj,Cx_mul] - rw [Complex.sq_norm, ← Complex.ofReal_re (Complex.normSq _), - ← Complex.mul_conj, heq] - simp_rw [re_Cx] - -lemma Ex_exp (P : Law α) (f : α → ℝ) : exp (P.Ex f) ≤ P.Ex (fun x => exp (f x)) := by - let C := P.Ex f - have H (x : ℝ) : exp C * ((x-C)+1) ≤ exp x := by - apply le_trans (mul_le_mul_of_nonneg_left (add_one_le_exp (x-C)) (exp_nonneg _)) - rw [← exp_add]; simp - have H' := P.Ex_mono (f:=fun x => exp C*((f x-C)+1)) (g:=fun x => exp (f x)) - (fun x => H (f x)) - rw [Ex_lmul,Ex_add,Ex_sub,Ex_const,Ex_const,show P.Ex f=C from rfl] at H' - simpa only [sub_self,zero_add,mul_one] using H' - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Independent +/-! # Complex moments -/ +open Function Set Filter +open scoped BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Law +open Real +variable {α β γ I : Type} [Fintype α] [Fintype β] [Fintype γ] [Fintype I] [DecidableEq I] + +def Cx (P : Law α) (f : α → ℂ) : ℂ := ∑ x, (P x:ℂ)*f x + +lemma Cx_ofReal (P : Law α) (f : α → ℝ) : P.Cx (fun x => (f x:ℂ)) = ↑(P.Ex f) := by + simp only [Cx,Ex, Complex.ofReal_sum, Complex.ofReal_mul] + +lemma re_Cx (P : Law α) (f : α → ℂ) : + (P.Cx f).re = P.Ex (fun x => (f x).re) := by + simp [Cx,Ex,Complex.re_sum,Complex.mul_re] + +lemma Cx_mul (P : Law α) (f : α → ℂ) (c : ℂ) : P.Cx (fun x => f x*c) = P.Cx f * c := by + simp_rw [Cx, ← mul_assoc, Finset.sum_mul] + +lemma Cx_lmul (P : Law α) (f : α → ℂ) (c : ℂ) : P.Cx (fun x => c*f x) = c*P.Cx f := by + simpa only [mul_comm c] using P.Cx_mul f c + +lemma Cx_conj (P : Law α) (f : α → ℂ) : P.Cx (fun x => conj (f x)) = conj (P.Cx f) := by + simp only [Cx,map_sum,map_mul,Complex.conj_ofReal] + +lemma Cx_norm (P : Law α) (f : α → ℂ) : ‖P.Cx f‖ ≤ P.Ex (fun x => ‖f x‖) := by + unfold Cx + refine (norm_sum_le _ _).trans ?_ + simp_rw [Ex,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (P.nonneg _)] + exact le_rfl + +lemma norm_Cx_unit (P : Law α) (f : α → ℂ) (h : ∀ x, ‖f x‖ ≤ 1) : + ‖P.Cx f‖ ≤ 1 := (P.Cx_norm f).trans ((P.Ex_mono h).trans (P.Ex_const 1).le) + +lemma Cx_swap (P : Law α) (Q : Law β) (f : α → β → ℂ) : + P.Cx (fun x => Q.Cx (f x)) = Q.Cx (fun y => P.Cx (fun x => f x y)) := by + unfold Cx + simp_rw [Finset.mul_sum] + rw [Finset.sum_comm] + apply Finset.sum_congr rfl; intro i _; apply Finset.sum_congr rfl; intro j _; ring + +lemma Ex_swap (P : Law α) (Q : Law β) (f : α → β → ℝ) : + P.Ex (fun x => Q.Ex (f x)) = Q.Ex (fun y => P.Ex (fun x => f x y)) := by + unfold Ex + simp_rw [Finset.mul_sum] + rw [Finset.sum_comm] + simp_rw [mul_left_comm (Q _)] + +lemma Cx_prod (P : I → Law α) (f : I → α → ℂ) : + (pack P).Cx (fun x => ∏ i, f i (x i)) = ∏ i, (P i).Cx (f i) := by + simp_rw [Cx,pack_apply,Complex.ofReal_prod,← Finset.prod_mul_distrib] + exact (Fintype.prod_sum (fun i x => (P i x:ℂ)*f i x)).symm + +lemma sq_norm_Cx (P : Law α) (f : α → ℂ) : + ‖P.Cx f‖^2 = P.Ex (fun x => P.Ex (fun y => (f x*conj (f y)).re)) := by + have heq : P.Cx f * conj (P.Cx f) = + P.Cx (fun x => P.Cx (fun y => f x*conj (f y))) := by + simp_rw [Cx_lmul,Cx_conj,Cx_mul] + rw [Complex.sq_norm, ← Complex.ofReal_re (Complex.normSq _), + ← Complex.mul_conj, heq] + simp_rw [re_Cx] + +lemma Ex_exp (P : Law α) (f : α → ℝ) : exp (P.Ex f) ≤ P.Ex (fun x => exp (f x)) := by + let C := P.Ex f + have H (x : ℝ) : exp C * ((x-C)+1) ≤ exp x := by + apply le_trans (mul_le_mul_of_nonneg_left (add_one_le_exp (x-C)) (exp_nonneg _)) + rw [← exp_add]; simp + have H' := P.Ex_mono (f:=fun x => exp C*((f x-C)+1)) (g:=fun x => exp (f x)) + (fun x => H (f x)) + rw [Ex_lmul,Ex_add,Ex_sub,Ex_const,Ex_const,show P.Ex f=C from rfl] at H' + simpa only [sub_self,zero_add,mul_one] using H' + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Entropy.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Entropy.lean index 1bd70d1b2849e693eb9ccada3b6d3482373f0b36..13c6ed77971e733ea5ce2c73903d1036c242fa3f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Entropy.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Entropy.lean @@ -1,245 +1,245 @@ -import OAI.Analysis.FourierLLogL.Finite.Law -/-! # Finite conditional entropy -/ -open Function Set Filter -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL.Law -universe u v w w' -variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} - [Fintype α] [Fintype β] [Fintype γ] [Fintype η] - -open Real - -/-- cross entropy (only meaningful for comparison positive on support). -/ -def crossE (P : Law α) (q : α → ℝ) : ℝ := P.Ex fun x => -log (q x) - -def entropy (P : Law α) : ℝ := crossE P P - -lemma entropy_formula (P : Law α) : P.entropy = ∑ x, negMulLog (P x) := by - simp only [entropy,crossE,Ex,negMulLog,mul_neg,neg_mul] - -lemma entropy_nonneg (P : Law α) : 0 ≤ P.entropy := by - rw [entropy_formula] - exact Finset.sum_nonneg fun x _ => negMulLog_nonneg (P.nonneg _) (P.le_one _) - -/-- Positivity on support condition. -/ -def dominated (P Q : Law α) : Prop := ∀ x, 0 < P x → 0 < Q x - -lemma dominated_refl (P : Law α) : P.dominated P := fun _ h => h - -lemma dominated_send {P Q : Law α} (h : P.dominated Q) (X : α → β) : - (P.send X).dominated (Q.send X) := by - intro x hx - obtain ⟨y,rfl,hy⟩ := (send_pos_iff P X x).mp hx - exact Q.send_pos X (h y hy) - -def kl (P Q : Law α) : ℝ := P.crossE Q - P.entropy - -lemma entropy_le_cross_aux (P : Law α) (q : α → ℝ) - (h0 : ∀ x, 0 ≤ q x) (ht : ∑ x, q x ≤ 1) (h : ∀ x, 0 < P x → 0 < q x) : - P.entropy ≤ P.crossE q := by - have H (x : α) : - P x * (-log (P x)) - P x * (-log (q x)) ≤ q x - P x := by - obtain heq|hp := eq_or_lt_of_le (P.nonneg x) - · simpa [← heq] using h0 x - have he := Real.log_le_sub_one_of_pos (div_pos (h x hp) hp) - rw [log_div (h x hp).ne' hp.ne'] at he - have HH := mul_le_mul_of_nonneg_right he hp.le - rw [sub_mul, sub_mul, div_mul_cancel₀ _ hp.ne'] at HH - linarith - have HH := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) - simp_rw [Finset.sum_sub_distrib, P.total] at HH - unfold entropy crossE Ex; linarith - -lemma kl_nonneg {P Q : Law α} (h : P.dominated Q) : 0 ≤ P.kl Q := - sub_nonneg.mpr (entropy_le_cross_aux P Q Q.nonneg Q.total.le h) - -lemma entropy_uniform [Nonempty α] : - (uniform α).entropy = log (Fintype.card α : ℝ) := by - unfold entropy crossE - simp_rw [uniform_apply, log_inv, neg_neg, Ex_const] - -lemma entropy_le_log_card (P : Law α) : - P.entropy ≤ log (Fintype.card α : ℝ) := by - obtain ⟨x,_⟩ := P.mem_nonempty - have : Nonempty α := ⟨x⟩ - have h : P.dominated (uniform α) := by intro x _; rw [uniform_apply]; positivity - have hi := kl_nonneg h - unfold kl crossE at hi - simp_rw [uniform_apply, log_inv, neg_neg, Ex_const] at hi - exact sub_nonneg.mp hi - -lemma entropy_point (x : α) : (point x).entropy = 0 := by - classical - simp only [entropy, crossE, Ex_point, point_apply, ite_true, log_one, neg_zero] - -/-- Shannon entropy of an observable on the finite law P. -/ -def H (P : Law α) (X : α → β) : ℝ := (P.send X).entropy - -/-- Conditional entropy H(X|W). -/ -def Hcond (P : Law α) (X : α → β) (W : α → γ) : ℝ := - P.H (fun x => (X x, W x)) - P.H W - -lemma H_self (P : Law α) : P.H (fun x => x) = P.entropy := by rw [H,send_id] - -lemma H_nonneg (P : Law α) (X : α → β) : 0 ≤ P.H X := entropy_nonneg _ - -lemma H_card (P : Law α) (X : α → β) : P.H X ≤ log (Fintype.card β : ℝ) := - entropy_le_log_card _ - -lemma H_log (P : Law α) (X : α → β) : - P.H X = P.Ex (fun x => -log (P.send X (X x))) := by rw [H,entropy,crossE,Ex_send] - -lemma H_congr_mem (P : Law α) {X Y : α → β} (h : ∀ x, 0 < P x → X x = Y x) : - P.H X = P.H Y := congrArg entropy (P.send_congr_mem h) - -lemma H_send (P : Law α) (X : α → β) (Y : β → γ) : - (P.send X).H Y = P.H (fun x => Y (X x)) := by rw [H, H, send_send] - -/-- If two observables induce the same partition, their entropies coincide. -/ -lemma H_partition (P : Law α) {X : α → β} {Y : α → γ} - (h : ∀ x y, X x = X y ↔ Y x = Y y) : P.H X = P.H Y := by - have hh (x : α) : P.send X (X x) = P.send Y (Y x) := by - rw [send_apply, send_apply] - congr 1 - ext y; exact h _ _ - simp_rw [H_log, hh] - -lemma H_pair_rev (P : Law α) (X : α → β) (W : α → γ) : - P.H (fun a => (X a, W a)) = P.H (fun a => (W a, X a)) := - P.H_partition (fun x y => by simp [and_comm]) - -lemma H_constant (P : Law α) (b : β) : P.H (fun _ => b) = 0 := by - have h : P.send (fun _ => b) = point b := by - apply ext_of_Ex; intro f - rw [Ex_send, Ex_point, Ex_const] - rw [H,h,entropy_point] - -lemma Hcond_constant (P : Law α) (X : α → β) (b : γ) : - P.Hcond X (fun _ => b) = P.H X := by - rw [Hcond,H_constant,sub_zero] - exact P.H_partition (fun x y => by simp) - -lemma Hcond_send (P : Law α) (X : α → β) (Y : β → γ) (W : β → η) : - (P.send X).Hcond Y W = P.Hcond (fun x => Y (X x)) (fun x => W (X x)) := by - unfold Hcond; rw [H_send, H_send] - -lemma H_pair_chain (P : Law α) (X : α → β) (W : α → γ) : - P.H (fun x => (X x,W x)) = P.H W + P.Hcond X W := by unfold Hcond; ring - -lemma Hcond_log (P : Law α) (X : α → β) (W : α → γ) : - P.Hcond X W = P.Ex (fun x => -log ((P.after W (W x)).send X (X x))) := by - unfold Hcond - simp_rw [H_log, ← Ex_sub] - apply Ex_congr_mem - intro x hx - have h1 := send_pos P W hx - have h2 := send_pos _ X (P.after_self_pos W hx) - rw [joint_after, log_mul h1.ne' h2.ne'] - ring - -lemma Hcond_after (P : Law α) (X : α → β) (W : α → γ) : - P.Hcond X W = (P.send W).Ex fun w => (P.after W w).H X := by - rw [Hcond_log] - simp_rw [H_log] - exact (P.after_Ex_sum W (fun w x => -log ((P.after W w).send X (X x)))).symm - -lemma Hcond_nonneg (P : Law α) (X : α → β) (W : α → γ) : - 0 ≤ P.Hcond X W := by - rw [Hcond_after] - exact Ex_nonneg _ fun w => H_nonneg _ _ - -lemma Hcond_determined (P : Law α) {X : α → β} {W : α → γ} - (h : ∀ x y, W x = W y → X x = X y) : P.Hcond X W = 0 := - sub_eq_zero.mpr (P.H_partition fun x y => by simp; exact fun hh => h x y hh) - -lemma H_le_of_determined (P : Law α) {X : α → β} {W : α → γ} - (h : ∀ x y, W x = W y → X x = X y) : P.H X ≤ P.H W := by - have h1 := P.Hcond_nonneg W X - have h2 := P.Hcond_determined h - rw [Hcond,P.H_pair_rev] at h1; rw [Hcond] at h2; linarith - -lemma Hcond_le_H (P : Law α) (X : α → β) (W : α → γ) : - P.Hcond X W ≤ P.H X := by - have H1 : P.H X = (P.send W).Ex fun w => ((P.after W w).send X).crossE (P.send X) := by - rw [H_log] - simp_rw [crossE,Ex_send] - have hh := (P.after_Ex_sum W (fun _ x => -log ((P.send X) (X x)))).symm - rwa [Ex_send] at hh - - rw [H1,Hcond_after] - refine Ex_mono_mem _ ?_ - intro w hw - apply entropy_le_cross_aux - · exact (send P X).nonneg - · exact (send P X).total.le - intro x hx - obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx - exact send_pos _ _ ((after_mem hw).mp ha).2 - -lemma H_associate (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.H (fun x => ((X x,Y x),Z x)) = P.H (fun x => (X x,(Y x,Z x))) := - P.H_partition (fun x y => by simp [and_assoc]) - -lemma Hcond_chain (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.Hcond (fun x => (X x,Y x)) Z = - P.Hcond X (fun x => (Y x,Z x)) + P.Hcond Y Z := by - unfold Hcond - rw [H_associate]; ring - -lemma Hcond_after_cond (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.Hcond X (fun x => (Y x,Z x)) = (P.send Z).Ex fun z => (P.after Z z).Hcond X Y := by - have h := P.Hcond_chain X Y Z - have h2 : - (P.send Z).Ex (fun z => (P.after Z z).Hcond X Y) = - P.Hcond (fun x => (X x,Y x)) Z - P.Hcond Y Z := by - rw [Hcond_after, Hcond_after, ← Ex_sub] - rfl - linarith - -/-- Conditioning reduces entropy. -/ -lemma Hcond_refine (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.Hcond X (fun x => (Y x,Z x)) ≤ P.Hcond X Z := by - rw [Hcond_after_cond, Hcond_after] - exact Ex_mono _ fun _ => Hcond_le_H _ _ _ - -lemma Hcond_right_partition (P : Law α) (X : α → β) {Y : α → γ} {Z : α → η} - (h : ∀ a b, Y a = Y b ↔ Z a = Z b) : P.Hcond X Y = P.Hcond X Z := by - unfold Hcond - rw [H_partition _ h] - congr 1 - apply P.H_partition - intro a b; simp [h] - -lemma Hcond_antitone (P : Law α) (X : α → β) {Y : α → γ} {Z : α → η} - (h : ∀ a b, Y a = Y b → Z a = Z b) : P.Hcond X Y ≤ P.Hcond X Z := by - rw [Hcond_right_partition (Z:=fun x => (Y x,Z x)) P X (fun a b => by simp; exact h a b)] - exact Hcond_refine P X _ _ - -lemma Hcond_subadd (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.Hcond (fun x => (X x,Y x)) Z ≤ P.Hcond X Z + P.Hcond Y Z := by - rw [Hcond_chain] - gcongr - exact Hcond_refine P _ _ _ - - -lemma entropy_withNext (P : Law α) (k : α → Law β) : - (P.withNext k).entropy = P.entropy + P.Ex (fun x => (k x).entropy) := by - simp only [entropy_formula, Ex, Fintype.sum_prod_type, withNext_apply] - simp_rw [negMulLog_mul, Finset.sum_add_distrib, ← Finset.sum_mul, ← Finset.mul_sum] - simp only [Law.total, one_mul] - -lemma entropy_tensorP (P : Law α) (Q : Law β) : - (P.tensorP Q).entropy = P.entropy + Q.entropy := by - rw [tensorP, entropy_withNext, Ex_const] - -lemma H_tensorP (P : Law α) (Q : Law β) (X : α → γ) (Y : β → η) : - (P.tensorP Q).H (fun x => (X x.1,Y x.2)) = P.H X + Q.H Y := by - unfold H; rw [send_tensorP, entropy_tensorP] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Law +/-! # Finite conditional entropy -/ +open Function Set Filter +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL.Law +universe u v w w' +variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} + [Fintype α] [Fintype β] [Fintype γ] [Fintype η] + +open Real + +/-- cross entropy (only meaningful for comparison positive on support). -/ +def crossE (P : Law α) (q : α → ℝ) : ℝ := P.Ex fun x => -log (q x) + +def entropy (P : Law α) : ℝ := crossE P P + +lemma entropy_formula (P : Law α) : P.entropy = ∑ x, negMulLog (P x) := by + simp only [entropy,crossE,Ex,negMulLog,mul_neg,neg_mul] + +lemma entropy_nonneg (P : Law α) : 0 ≤ P.entropy := by + rw [entropy_formula] + exact Finset.sum_nonneg fun x _ => negMulLog_nonneg (P.nonneg _) (P.le_one _) + +/-- Positivity on support condition. -/ +def dominated (P Q : Law α) : Prop := ∀ x, 0 < P x → 0 < Q x + +lemma dominated_refl (P : Law α) : P.dominated P := fun _ h => h + +lemma dominated_send {P Q : Law α} (h : P.dominated Q) (X : α → β) : + (P.send X).dominated (Q.send X) := by + intro x hx + obtain ⟨y,rfl,hy⟩ := (send_pos_iff P X x).mp hx + exact Q.send_pos X (h y hy) + +def kl (P Q : Law α) : ℝ := P.crossE Q - P.entropy + +lemma entropy_le_cross_aux (P : Law α) (q : α → ℝ) + (h0 : ∀ x, 0 ≤ q x) (ht : ∑ x, q x ≤ 1) (h : ∀ x, 0 < P x → 0 < q x) : + P.entropy ≤ P.crossE q := by + have H (x : α) : + P x * (-log (P x)) - P x * (-log (q x)) ≤ q x - P x := by + obtain heq|hp := eq_or_lt_of_le (P.nonneg x) + · simpa [← heq] using h0 x + have he := Real.log_le_sub_one_of_pos (div_pos (h x hp) hp) + rw [log_div (h x hp).ne' hp.ne'] at he + have HH := mul_le_mul_of_nonneg_right he hp.le + rw [sub_mul, sub_mul, div_mul_cancel₀ _ hp.ne'] at HH + linarith + have HH := Finset.sum_le_sum (s:=Finset.univ) (fun x _ => H x) + simp_rw [Finset.sum_sub_distrib, P.total] at HH + unfold entropy crossE Ex; linarith + +lemma kl_nonneg {P Q : Law α} (h : P.dominated Q) : 0 ≤ P.kl Q := + sub_nonneg.mpr (entropy_le_cross_aux P Q Q.nonneg Q.total.le h) + +lemma entropy_uniform [Nonempty α] : + (uniform α).entropy = log (Fintype.card α : ℝ) := by + unfold entropy crossE + simp_rw [uniform_apply, log_inv, neg_neg, Ex_const] + +lemma entropy_le_log_card (P : Law α) : + P.entropy ≤ log (Fintype.card α : ℝ) := by + obtain ⟨x,_⟩ := P.mem_nonempty + have : Nonempty α := ⟨x⟩ + have h : P.dominated (uniform α) := by intro x _; rw [uniform_apply]; positivity + have hi := kl_nonneg h + unfold kl crossE at hi + simp_rw [uniform_apply, log_inv, neg_neg, Ex_const] at hi + exact sub_nonneg.mp hi + +lemma entropy_point (x : α) : (point x).entropy = 0 := by + classical + simp only [entropy, crossE, Ex_point, point_apply, ite_true, log_one, neg_zero] + +/-- Shannon entropy of an observable on the finite law P. -/ +def H (P : Law α) (X : α → β) : ℝ := (P.send X).entropy + +/-- Conditional entropy H(X|W). -/ +def Hcond (P : Law α) (X : α → β) (W : α → γ) : ℝ := + P.H (fun x => (X x, W x)) - P.H W + +lemma H_self (P : Law α) : P.H (fun x => x) = P.entropy := by rw [H,send_id] + +lemma H_nonneg (P : Law α) (X : α → β) : 0 ≤ P.H X := entropy_nonneg _ + +lemma H_card (P : Law α) (X : α → β) : P.H X ≤ log (Fintype.card β : ℝ) := + entropy_le_log_card _ + +lemma H_log (P : Law α) (X : α → β) : + P.H X = P.Ex (fun x => -log (P.send X (X x))) := by rw [H,entropy,crossE,Ex_send] + +lemma H_congr_mem (P : Law α) {X Y : α → β} (h : ∀ x, 0 < P x → X x = Y x) : + P.H X = P.H Y := congrArg entropy (P.send_congr_mem h) + +lemma H_send (P : Law α) (X : α → β) (Y : β → γ) : + (P.send X).H Y = P.H (fun x => Y (X x)) := by rw [H, H, send_send] + +/-- If two observables induce the same partition, their entropies coincide. -/ +lemma H_partition (P : Law α) {X : α → β} {Y : α → γ} + (h : ∀ x y, X x = X y ↔ Y x = Y y) : P.H X = P.H Y := by + have hh (x : α) : P.send X (X x) = P.send Y (Y x) := by + rw [send_apply, send_apply] + congr 1 + ext y; exact h _ _ + simp_rw [H_log, hh] + +lemma H_pair_rev (P : Law α) (X : α → β) (W : α → γ) : + P.H (fun a => (X a, W a)) = P.H (fun a => (W a, X a)) := + P.H_partition (fun x y => by simp [and_comm]) + +lemma H_constant (P : Law α) (b : β) : P.H (fun _ => b) = 0 := by + have h : P.send (fun _ => b) = point b := by + apply ext_of_Ex; intro f + rw [Ex_send, Ex_point, Ex_const] + rw [H,h,entropy_point] + +lemma Hcond_constant (P : Law α) (X : α → β) (b : γ) : + P.Hcond X (fun _ => b) = P.H X := by + rw [Hcond,H_constant,sub_zero] + exact P.H_partition (fun x y => by simp) + +lemma Hcond_send (P : Law α) (X : α → β) (Y : β → γ) (W : β → η) : + (P.send X).Hcond Y W = P.Hcond (fun x => Y (X x)) (fun x => W (X x)) := by + unfold Hcond; rw [H_send, H_send] + +lemma H_pair_chain (P : Law α) (X : α → β) (W : α → γ) : + P.H (fun x => (X x,W x)) = P.H W + P.Hcond X W := by unfold Hcond; ring + +lemma Hcond_log (P : Law α) (X : α → β) (W : α → γ) : + P.Hcond X W = P.Ex (fun x => -log ((P.after W (W x)).send X (X x))) := by + unfold Hcond + simp_rw [H_log, ← Ex_sub] + apply Ex_congr_mem + intro x hx + have h1 := send_pos P W hx + have h2 := send_pos _ X (P.after_self_pos W hx) + rw [joint_after, log_mul h1.ne' h2.ne'] + ring + +lemma Hcond_after (P : Law α) (X : α → β) (W : α → γ) : + P.Hcond X W = (P.send W).Ex fun w => (P.after W w).H X := by + rw [Hcond_log] + simp_rw [H_log] + exact (P.after_Ex_sum W (fun w x => -log ((P.after W w).send X (X x)))).symm + +lemma Hcond_nonneg (P : Law α) (X : α → β) (W : α → γ) : + 0 ≤ P.Hcond X W := by + rw [Hcond_after] + exact Ex_nonneg _ fun w => H_nonneg _ _ + +lemma Hcond_determined (P : Law α) {X : α → β} {W : α → γ} + (h : ∀ x y, W x = W y → X x = X y) : P.Hcond X W = 0 := + sub_eq_zero.mpr (P.H_partition fun x y => by simp; exact fun hh => h x y hh) + +lemma H_le_of_determined (P : Law α) {X : α → β} {W : α → γ} + (h : ∀ x y, W x = W y → X x = X y) : P.H X ≤ P.H W := by + have h1 := P.Hcond_nonneg W X + have h2 := P.Hcond_determined h + rw [Hcond,P.H_pair_rev] at h1; rw [Hcond] at h2; linarith + +lemma Hcond_le_H (P : Law α) (X : α → β) (W : α → γ) : + P.Hcond X W ≤ P.H X := by + have H1 : P.H X = (P.send W).Ex fun w => ((P.after W w).send X).crossE (P.send X) := by + rw [H_log] + simp_rw [crossE,Ex_send] + have hh := (P.after_Ex_sum W (fun _ x => -log ((P.send X) (X x)))).symm + rwa [Ex_send] at hh + + rw [H1,Hcond_after] + refine Ex_mono_mem _ ?_ + intro w hw + apply entropy_le_cross_aux + · exact (send P X).nonneg + · exact (send P X).total.le + intro x hx + obtain ⟨a,rfl,ha⟩ := (send_pos_iff _ _ _).mp hx + exact send_pos _ _ ((after_mem hw).mp ha).2 + +lemma H_associate (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.H (fun x => ((X x,Y x),Z x)) = P.H (fun x => (X x,(Y x,Z x))) := + P.H_partition (fun x y => by simp [and_assoc]) + +lemma Hcond_chain (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.Hcond (fun x => (X x,Y x)) Z = + P.Hcond X (fun x => (Y x,Z x)) + P.Hcond Y Z := by + unfold Hcond + rw [H_associate]; ring + +lemma Hcond_after_cond (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.Hcond X (fun x => (Y x,Z x)) = (P.send Z).Ex fun z => (P.after Z z).Hcond X Y := by + have h := P.Hcond_chain X Y Z + have h2 : + (P.send Z).Ex (fun z => (P.after Z z).Hcond X Y) = + P.Hcond (fun x => (X x,Y x)) Z - P.Hcond Y Z := by + rw [Hcond_after, Hcond_after, ← Ex_sub] + rfl + linarith + +/-- Conditioning reduces entropy. -/ +lemma Hcond_refine (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.Hcond X (fun x => (Y x,Z x)) ≤ P.Hcond X Z := by + rw [Hcond_after_cond, Hcond_after] + exact Ex_mono _ fun _ => Hcond_le_H _ _ _ + +lemma Hcond_right_partition (P : Law α) (X : α → β) {Y : α → γ} {Z : α → η} + (h : ∀ a b, Y a = Y b ↔ Z a = Z b) : P.Hcond X Y = P.Hcond X Z := by + unfold Hcond + rw [H_partition _ h] + congr 1 + apply P.H_partition + intro a b; simp [h] + +lemma Hcond_antitone (P : Law α) (X : α → β) {Y : α → γ} {Z : α → η} + (h : ∀ a b, Y a = Y b → Z a = Z b) : P.Hcond X Y ≤ P.Hcond X Z := by + rw [Hcond_right_partition (Z:=fun x => (Y x,Z x)) P X (fun a b => by simp; exact h a b)] + exact Hcond_refine P X _ _ + +lemma Hcond_subadd (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.Hcond (fun x => (X x,Y x)) Z ≤ P.Hcond X Z + P.Hcond Y Z := by + rw [Hcond_chain] + gcongr + exact Hcond_refine P _ _ _ + + +lemma entropy_withNext (P : Law α) (k : α → Law β) : + (P.withNext k).entropy = P.entropy + P.Ex (fun x => (k x).entropy) := by + simp only [entropy_formula, Ex, Fintype.sum_prod_type, withNext_apply] + simp_rw [negMulLog_mul, Finset.sum_add_distrib, ← Finset.sum_mul, ← Finset.mul_sum] + simp only [Law.total, one_mul] + +lemma entropy_tensorP (P : Law α) (Q : Law β) : + (P.tensorP Q).entropy = P.entropy + Q.entropy := by + rw [tensorP, entropy_withNext, Ex_const] + +lemma H_tensorP (P : Law α) (Q : Law β) (X : α → γ) (Y : β → η) : + (P.tensorP Q).H (fun x => (X x.1,Y x.2)) = P.H X + Q.H Y := by + unfold H; rw [send_tensorP, entropy_tensorP] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Independent.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Independent.lean index 495cc1210c6e35bad8372b1c58302ba5551332a5..254c1e390a5cf009aa610902b96b94abe90b79ad 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Independent.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Independent.lean @@ -1,176 +1,176 @@ -import OAI.Analysis.FourierLLogL.Finite.Mixture -/-! # Product laws -/ -open Set Function Filter -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL.Law -open Real -universe u u' v v' w w' -variable {ι : Type u} {τ : Type u'} {α : Type v} {β : Type v'} {γ : Type w} {η : Type w'} - [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] - [Fintype α] [Fintype β] [Fintype γ] [Fintype η] - -def pack (P : ι → Law α) : Law (ι → α) := - ⟨fun x => ∏ i, P i (x i), fun x => Finset.prod_nonneg (fun i _ => (P i).nonneg _), by - rw [← Fintype.prod_sum (fun i x => P i x)] - simp [Law.total]⟩ - -lemma pack_apply (P : ι → Law α) (x : ι → α) : - pack P x = ∏ i, P i (x i) := rfl - -lemma pack_pos (P : ι → Law α) (x : ι → α) : - 0 < pack P x ↔ ∀ i, 0 < P i (x i) := by - rw [pack_apply] - constructor - · intro h i - obtain hx|hx := eq_or_lt_of_le ((P i).nonneg (x i)) - · rw [Finset.prod_eq_zero (Finset.mem_univ i) hx.symm] at h - exact (lt_irrefl _ h).elim - · exact hx - · exact fun h => Finset.prod_pos fun i _ => h i - -lemma pack_Ex_prod (P : ι → Law α) (f : ι → α → ℝ) : - (pack P).Ex (fun x => ∏ i, f i (x i)) = ∏ i, (P i).Ex (f i) := by - simp_rw [Ex, pack_apply, ← Finset.prod_mul_distrib] - exact (Fintype.prod_sum (fun i x => P i x * f i x)).symm - -lemma ext_by_products {P Q : Law (ι → α)} - (h : ∀ f : ι → α → ℝ, P.Ex (fun x => ∏ i, f i (x i)) = Q.Ex fun x => ∏ i, f i (x i)) : - P = Q := by - classical - apply Law.ext - intro x - let f i (a : α) : ℝ := if a = x i then 1 else 0 - have heq (y : ι → α) : (∏ i, f i (y i)) = if y = x then 1 else 0 := by - split_ifs with H - · subst y; simp [f] - · have : ∃ i, y i ≠ x i := by contrapose! H; exact funext H - obtain ⟨i,hi⟩ := this - exact Finset.prod_eq_zero (Finset.mem_univ i) (ite_eq_right hi) - have HH := h f - simp_rw [heq] at HH - simpa [Ex, mul_ite] using HH - -lemma sendPack (P : ι → Law α) (X : ι → α → β) : - (pack P).send (fun x i => X i (x i)) = pack (fun i => (P i).send (X i)) := by - apply ext_by_products; intro f - rw [Ex_send,pack_Ex_prod] - have h := pack_Ex_prod P (fun i a => f i (X i a)) - simp_rw [Ex_send] - exact h - -lemma marginalPack (P : ι → Law α) (i : ι) : - (pack P).send (fun x => x i) = P i := by - apply ext_of_Ex; intro f - let F (j : ι) := if j=i then f else fun _ : α => (1:ℝ) - have h := pack_Ex_prod P F - have h1 (x : ι → α) : (∏ j, F j (x j)) = f (x i) := by - rw [Finset.prod_eq_single i] <;> intros <;> simp_all [F] - have h2 : (∏ j, (P j).Ex (F j)) = (P i).Ex f := by - rw [Finset.prod_eq_single i] <;> intros <;> simp_all [F, Ex_const] - rw [Ex_send]; simp_rw [h1,h2] at h; exact h - -lemma Ex_pack_proj (P : ι → Law α) (i : ι) (f : α → ℝ) : - (pack P).Ex (fun x => f (x i)) = (P i).Ex f := by - rw [← Ex_send (pack P) (fun x => x i), marginalPack] - -lemma sendPack_embedding (P : ι → Law α) (s : τ → ι) (hs : Injective s) : - (pack P).send (fun x i => x (s i)) = pack (fun i => P (s i)) := by - classical - apply ext_by_products; intro f - let F := extend s f (fun _ _ => 1) - have h1 (i : τ) : F (s i) = f i := hs.extend_apply _ _ _ - have h2 (i : ι) (hi : i ∉ range s) : F i = fun _ => 1 := - extend_apply' _ _ _ hi - have H (a : ι → ℝ) (h : ∀ i, i ∉ range s → a i = 1) : - ∏ i, a i = ∏ i, a (s i) := by - rw [← Finset.prod_image hs.injOn] - apply (Finset.prod_subset (Finset.subset_univ _) ?_).symm - intro i _ hi; apply h; simpa using hi - have h3 (x : ι → α) : - (∏ i, F i (x i)) = ∏ i, f i (x (s i)) := by - rw [H]; simp_rw [h1]; intro i hi; rw [h2 i hi] - have h4 : - (∏ i, (P i).Ex (F i)) = ∏ i, (P (s i)).Ex (f i) := by - rw [H]; simp_rw [h1]; intro i hi; rw [h2 i hi, Ex_const] - have h := pack_Ex_prod P F - rw [Ex_send,pack_Ex_prod]; simp_rw [h3,h4] at h; exact h - -lemma crossE_pack {P : Law (ι → α)} {Q : ι → Law α} - (h : P.dominated (pack Q)) : - P.crossE (pack Q) = ∑ i, (P.send (fun x => x i)).crossE (Q i) := by - have hh (x) (hx : 0 < P x) : -log (pack Q x) = ∑ i, -log (Q i (x i)) := by - rw [pack_apply, log_prod, Finset.sum_neg_distrib] - intro i _ - exact ((pack_pos Q x).mp (h x hx) i).ne' - unfold crossE - have hi : P.Ex (fun x => -log (pack Q x)) = - P.Ex fun x => ∑ i, -log (Q i (x i)) := P.Ex_congr_mem hh - rw [hi, Ex_finset_sum] - simp_rw [Ex_send] - -lemma pack_kl {P Q : ι → Law α} (h : ∀ i, (P i).dominated (Q i)) : - (pack P).kl (pack Q) = ∑ i, (P i).kl (Q i) := by - have h1 : (pack P).dominated (pack Q) := fun x hx => - (pack_pos Q _).mpr fun i => h i _ ((pack_pos _ _).mp hx i) - simp only [kl,entropy,crossE_pack h1,crossE_pack (dominated_refl (pack P)),marginalPack] - rw [Finset.sum_sub_distrib] - -lemma entropy_pack (P : ι → Law α) : (pack P).entropy = ∑ i, (P i).entropy := by - rw [entropy,crossE_pack (dominated_refl (pack P))] - simp only [marginalPack, entropy] - -lemma entropy_coord_bound (P : Law (ι → α)) : - P.entropy ≤ ∑ i, P.H (fun x => x i) := by - let Q i := P.send (fun x => x i) - have hh : P.dominated (pack Q) := fun x hx => (pack_pos Q _).mpr fun i => P.send_pos _ hx - refine le_trans (b:=P.crossE (pack Q)) (sub_nonneg.mp (kl_nonneg hh)) ?_ - rw [crossE_pack hh] - exact le_refl _ - -lemma H_vec_le (P : Law α) (X : α → ι → β) : - P.H X ≤ ∑ i, P.H (fun x => X x i) := by - have h := entropy_coord_bound (P.send X) - simp only [H_send] at h; exact h - -lemma Hcond_vec_le (P : Law α) (X : α → ι → β) (W : α → γ) : - P.Hcond X W ≤ ∑ i, P.Hcond (fun x => X x i) W := by - simp_rw [Hcond_after, ← Ex_finset_sum] - exact Ex_mono _ fun _ => H_vec_le _ _ - -lemma H_vec_pack (P : ι → Law α) (X : ι → α → β) : - (pack P).H (fun x i => X i (x i)) = ∑ i, (P i).H (X i) := by - unfold H - rw [sendPack P X, entropy_pack] - -lemma Hcond_vec_pack (P : ι → Law α) (X : ι → α → β) (W : ι → α → γ) : - (pack P).Hcond (fun x i => X i (x i)) (fun x i => W i (x i)) = - ∑ i, (P i).Hcond (X i) (W i) := by - unfold Hcond - let Y i x := (X i x,W i x) - have Heq : - (pack P).H (fun x => ((fun i => X i (x i)),(fun i => W i (x i)))) = - (pack P).H (fun x i => Y i (x i)) := by - refine H_partition _ fun x y => ?_ - simp only [Y, Prod.mk.injEq, funext_iff, forall_and] - rw [Heq,H_vec_pack P Y,H_vec_pack P W,Finset.sum_sub_distrib] - -/-- Attach an independent new observation per row. When the rows of -the initial law are independent, the refined rows remain independent. -/ -lemma pack_withNext (P : ι → Law α) (Q : ι → α → Law β) : - ((pack P).withNext (fun v => pack (fun i => Q i (v i)))).send - (fun (z : (ι→α)×(ι→β)) i => (z.1 i,z.2 i)) = - pack (fun i => (P i).withNext (Q i)) := by - apply ext_by_products; intro f - rw [Ex_send, Ex_withNext] - have H (x : ι → α) := pack_Ex_prod (fun i => Q i (x i)) (fun i y => f i (x i,y)) - simp_rw [H] - rw [pack_Ex_prod P (fun i a => (Q i a).Ex (fun y => f i (a,y))), pack_Ex_prod] - simp_rw [Ex_withNext] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Mixture +/-! # Product laws -/ +open Set Function Filter +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL.Law +open Real +universe u u' v v' w w' +variable {ι : Type u} {τ : Type u'} {α : Type v} {β : Type v'} {γ : Type w} {η : Type w'} + [Fintype ι] [DecidableEq ι] [Fintype τ] [DecidableEq τ] + [Fintype α] [Fintype β] [Fintype γ] [Fintype η] + +def pack (P : ι → Law α) : Law (ι → α) := + ⟨fun x => ∏ i, P i (x i), fun x => Finset.prod_nonneg (fun i _ => (P i).nonneg _), by + rw [← Fintype.prod_sum (fun i x => P i x)] + simp [Law.total]⟩ + +lemma pack_apply (P : ι → Law α) (x : ι → α) : + pack P x = ∏ i, P i (x i) := rfl + +lemma pack_pos (P : ι → Law α) (x : ι → α) : + 0 < pack P x ↔ ∀ i, 0 < P i (x i) := by + rw [pack_apply] + constructor + · intro h i + obtain hx|hx := eq_or_lt_of_le ((P i).nonneg (x i)) + · rw [Finset.prod_eq_zero (Finset.mem_univ i) hx.symm] at h + exact (lt_irrefl _ h).elim + · exact hx + · exact fun h => Finset.prod_pos fun i _ => h i + +lemma pack_Ex_prod (P : ι → Law α) (f : ι → α → ℝ) : + (pack P).Ex (fun x => ∏ i, f i (x i)) = ∏ i, (P i).Ex (f i) := by + simp_rw [Ex, pack_apply, ← Finset.prod_mul_distrib] + exact (Fintype.prod_sum (fun i x => P i x * f i x)).symm + +lemma ext_by_products {P Q : Law (ι → α)} + (h : ∀ f : ι → α → ℝ, P.Ex (fun x => ∏ i, f i (x i)) = Q.Ex fun x => ∏ i, f i (x i)) : + P = Q := by + classical + apply Law.ext + intro x + let f i (a : α) : ℝ := if a = x i then 1 else 0 + have heq (y : ι → α) : (∏ i, f i (y i)) = if y = x then 1 else 0 := by + split_ifs with H + · subst y; simp [f] + · have : ∃ i, y i ≠ x i := by contrapose! H; exact funext H + obtain ⟨i,hi⟩ := this + exact Finset.prod_eq_zero (Finset.mem_univ i) (ite_eq_right hi) + have HH := h f + simp_rw [heq] at HH + simpa [Ex, mul_ite] using HH + +lemma sendPack (P : ι → Law α) (X : ι → α → β) : + (pack P).send (fun x i => X i (x i)) = pack (fun i => (P i).send (X i)) := by + apply ext_by_products; intro f + rw [Ex_send,pack_Ex_prod] + have h := pack_Ex_prod P (fun i a => f i (X i a)) + simp_rw [Ex_send] + exact h + +lemma marginalPack (P : ι → Law α) (i : ι) : + (pack P).send (fun x => x i) = P i := by + apply ext_of_Ex; intro f + let F (j : ι) := if j=i then f else fun _ : α => (1:ℝ) + have h := pack_Ex_prod P F + have h1 (x : ι → α) : (∏ j, F j (x j)) = f (x i) := by + rw [Finset.prod_eq_single i] <;> intros <;> simp_all [F] + have h2 : (∏ j, (P j).Ex (F j)) = (P i).Ex f := by + rw [Finset.prod_eq_single i] <;> intros <;> simp_all [F, Ex_const] + rw [Ex_send]; simp_rw [h1,h2] at h; exact h + +lemma Ex_pack_proj (P : ι → Law α) (i : ι) (f : α → ℝ) : + (pack P).Ex (fun x => f (x i)) = (P i).Ex f := by + rw [← Ex_send (pack P) (fun x => x i), marginalPack] + +lemma sendPack_embedding (P : ι → Law α) (s : τ → ι) (hs : Injective s) : + (pack P).send (fun x i => x (s i)) = pack (fun i => P (s i)) := by + classical + apply ext_by_products; intro f + let F := extend s f (fun _ _ => 1) + have h1 (i : τ) : F (s i) = f i := hs.extend_apply _ _ _ + have h2 (i : ι) (hi : i ∉ range s) : F i = fun _ => 1 := + extend_apply' _ _ _ hi + have H (a : ι → ℝ) (h : ∀ i, i ∉ range s → a i = 1) : + ∏ i, a i = ∏ i, a (s i) := by + rw [← Finset.prod_image hs.injOn] + apply (Finset.prod_subset (Finset.subset_univ _) ?_).symm + intro i _ hi; apply h; simpa using hi + have h3 (x : ι → α) : + (∏ i, F i (x i)) = ∏ i, f i (x (s i)) := by + rw [H]; simp_rw [h1]; intro i hi; rw [h2 i hi] + have h4 : + (∏ i, (P i).Ex (F i)) = ∏ i, (P (s i)).Ex (f i) := by + rw [H]; simp_rw [h1]; intro i hi; rw [h2 i hi, Ex_const] + have h := pack_Ex_prod P F + rw [Ex_send,pack_Ex_prod]; simp_rw [h3,h4] at h; exact h + +lemma crossE_pack {P : Law (ι → α)} {Q : ι → Law α} + (h : P.dominated (pack Q)) : + P.crossE (pack Q) = ∑ i, (P.send (fun x => x i)).crossE (Q i) := by + have hh (x) (hx : 0 < P x) : -log (pack Q x) = ∑ i, -log (Q i (x i)) := by + rw [pack_apply, log_prod, Finset.sum_neg_distrib] + intro i _ + exact ((pack_pos Q x).mp (h x hx) i).ne' + unfold crossE + have hi : P.Ex (fun x => -log (pack Q x)) = + P.Ex fun x => ∑ i, -log (Q i (x i)) := P.Ex_congr_mem hh + rw [hi, Ex_finset_sum] + simp_rw [Ex_send] + +lemma pack_kl {P Q : ι → Law α} (h : ∀ i, (P i).dominated (Q i)) : + (pack P).kl (pack Q) = ∑ i, (P i).kl (Q i) := by + have h1 : (pack P).dominated (pack Q) := fun x hx => + (pack_pos Q _).mpr fun i => h i _ ((pack_pos _ _).mp hx i) + simp only [kl,entropy,crossE_pack h1,crossE_pack (dominated_refl (pack P)),marginalPack] + rw [Finset.sum_sub_distrib] + +lemma entropy_pack (P : ι → Law α) : (pack P).entropy = ∑ i, (P i).entropy := by + rw [entropy,crossE_pack (dominated_refl (pack P))] + simp only [marginalPack, entropy] + +lemma entropy_coord_bound (P : Law (ι → α)) : + P.entropy ≤ ∑ i, P.H (fun x => x i) := by + let Q i := P.send (fun x => x i) + have hh : P.dominated (pack Q) := fun x hx => (pack_pos Q _).mpr fun i => P.send_pos _ hx + refine le_trans (b:=P.crossE (pack Q)) (sub_nonneg.mp (kl_nonneg hh)) ?_ + rw [crossE_pack hh] + exact le_refl _ + +lemma H_vec_le (P : Law α) (X : α → ι → β) : + P.H X ≤ ∑ i, P.H (fun x => X x i) := by + have h := entropy_coord_bound (P.send X) + simp only [H_send] at h; exact h + +lemma Hcond_vec_le (P : Law α) (X : α → ι → β) (W : α → γ) : + P.Hcond X W ≤ ∑ i, P.Hcond (fun x => X x i) W := by + simp_rw [Hcond_after, ← Ex_finset_sum] + exact Ex_mono _ fun _ => H_vec_le _ _ + +lemma H_vec_pack (P : ι → Law α) (X : ι → α → β) : + (pack P).H (fun x i => X i (x i)) = ∑ i, (P i).H (X i) := by + unfold H + rw [sendPack P X, entropy_pack] + +lemma Hcond_vec_pack (P : ι → Law α) (X : ι → α → β) (W : ι → α → γ) : + (pack P).Hcond (fun x i => X i (x i)) (fun x i => W i (x i)) = + ∑ i, (P i).Hcond (X i) (W i) := by + unfold Hcond + let Y i x := (X i x,W i x) + have Heq : + (pack P).H (fun x => ((fun i => X i (x i)),(fun i => W i (x i)))) = + (pack P).H (fun x i => Y i (x i)) := by + refine H_partition _ fun x y => ?_ + simp only [Y, Prod.mk.injEq, funext_iff, forall_and] + rw [Heq,H_vec_pack P Y,H_vec_pack P W,Finset.sum_sub_distrib] + +/-- Attach an independent new observation per row. When the rows of +the initial law are independent, the refined rows remain independent. -/ +lemma pack_withNext (P : ι → Law α) (Q : ι → α → Law β) : + ((pack P).withNext (fun v => pack (fun i => Q i (v i)))).send + (fun (z : (ι→α)×(ι→β)) i => (z.1 i,z.2 i)) = + pack (fun i => (P i).withNext (Q i)) := by + apply ext_by_products; intro f + rw [Ex_send, Ex_withNext] + have H (x : ι → α) := pack_Ex_prod (fun i => Q i (x i)) (fun i y => f i (x i,y)) + simp_rw [H] + rw [pack_Ex_prod P (fun i a => (Q i a).Ex (fun y => f i (a,y))), pack_Ex_prod] + simp_rw [Ex_withNext] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Law.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Law.lean index 8177edc08872b67fae1d6528abed6302fe039123..77db91addaeb67532d7c13b2f02d116a26276358 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Law.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Law.lean @@ -1,405 +1,405 @@ -import Mathlib -/-! # Finite probability tools used in the compression and the dyadic model -/ - -open Function Set Filter -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL -universe u v w w' - -private def lawSet (α : Type u) [Fintype α] : Set (α → ℝ) := - {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} - -abbrev Law (α : Type u) [Fintype α] : Type u := lawSet α - -instance {α : Type u} [Fintype α] : FunLike (Law α) α ℝ where - coe P := P.val - coe_injective := by aesop - -instance {α : Type u} [Fintype α] : CompactSpace (Law α) := by - apply isCompact_iff_compactSpace.mp - have hclosed : IsClosed (lawSet α) := by - have heq : lawSet α = - (⋂ x, {f : α → ℝ | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by - ext f - simp [lawSet] - rw [heq] - exact (isClosed_iInter (fun x => - isClosed_le continuous_const (continuous_apply x))).inter - (isClosed_eq (by fun_prop) continuous_const) - apply IsCompact.of_isClosed_subset - (isCompact_Icc : IsCompact (Set.Icc (0 : α → ℝ) 1)) hclosed - intro f hf - rcases hf with ⟨h0, hsum⟩ - constructor - · exact fun x => h0 x - · intro x - change f x ≤ (1 : ℝ) - rw [← hsum] - exact Finset.single_le_sum (fun y _ => h0 y) (Finset.mem_univ x) - -namespace Law -variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} - [Fintype α] [Fintype β] [Fintype γ] [Fintype η] - -protected lemma ext {P Q : Law α} (h : ∀ x, P x = Q x) : P = Q := - DFunLike.ext _ _ h - -protected lemma nonneg (P : Law α) (x) : 0 ≤ P x := P.property.1 x - -protected lemma total (P : Law α) : ∑ x, P x = 1 := P.property.2 - -lemma mem_nonempty (P : Law α) : ∃ x, 0 < P x := by - by_contra! hh - have hi : ∑ x, P x ≤ 0 := Finset.sum_nonpos fun x _ => hh x - rw [P.total] at hi; norm_num at hi - -def Ex (P : Law α) (f : α → ℝ) : ℝ := ∑ x, P x * f x - -lemma Ex_const (P : Law α) (c : ℝ) : P.Ex (fun _ => c) = c := by - rw [Ex, ← Finset.sum_mul, P.total, one_mul] - -lemma Ex_add (P : Law α) (f g : α → ℝ) : - P.Ex (fun x => f x + g x) = P.Ex f + P.Ex g := by - simp_rw [Ex, mul_add, Finset.sum_add_distrib] - -lemma Ex_sub (P : Law α) (f g : α → ℝ) : - P.Ex (fun x => f x - g x) = P.Ex f - P.Ex g := by - simp_rw [Ex, mul_sub, Finset.sum_sub_distrib] - -lemma Ex_mul (P : Law α) (f : α → ℝ) (c : ℝ) : - P.Ex (fun x => f x * c) = P.Ex f * c := by - simp_rw [Ex, ← mul_assoc, Finset.sum_mul] - -lemma Ex_lmul (P : Law α) (f : α → ℝ) (c : ℝ) : - P.Ex (fun x => c * f x) = c * P.Ex f := by - simpa only [mul_comm c] using Ex_mul P f c - -lemma Ex_neg (P : Law α) (f : α → ℝ) : - P.Ex (fun x => -f x) = -P.Ex f := by - simpa only [neg_one_mul] using Ex_lmul P f (-1) - -lemma Ex_finset_sum {ι : Type*} (s : Finset ι) (P : Law α) (f : ι → α → ℝ) : - P.Ex (fun x => ∑ i ∈ s, f i x) = ∑ i ∈ s, P.Ex (f i) := by - unfold Ex - simp_rw [Finset.mul_sum] - exact Finset.sum_comm - -lemma Ex_mono_mem (P : Law α) {f g : α → ℝ} - (h : ∀ x, 0 < P x → f x ≤ g x) : P.Ex f ≤ P.Ex g := by - refine Finset.sum_le_sum fun x _ => ?_ - obtain hP | hP := eq_or_lt_of_le (P.nonneg x) - · rw [← hP, zero_mul, zero_mul] - · exact mul_le_mul_of_nonneg_left (h x hP) hP.le - -lemma Ex_mono (P : Law α) {f g : α → ℝ} - (h : ∀ x, f x ≤ g x) : P.Ex f ≤ P.Ex g := P.Ex_mono_mem fun x _ => h x - -lemma Ex_nonneg (P : Law α) {f : α → ℝ} (h : ∀ x, 0 ≤ f x) : 0 ≤ P.Ex f := by - simpa only [Ex_const] using P.Ex_mono h - -lemma Ex_congr_mem (P : Law α) {f g : α → ℝ} - (h : ∀ x, 0 < P x → f x = g x) : P.Ex f = P.Ex g := - le_antisymm (P.Ex_mono_mem fun x hx => (h x hx).le) - (P.Ex_mono_mem fun x hx => (h x hx).ge) - -lemma exists_le_Ex (P : Law α) (f : α → ℝ) : ∃ x, 0 < P x ∧ f x ≤ P.Ex f := by - by_contra! hh - obtain ⟨a,ha⟩ := P.mem_nonempty - have H : P.Ex (fun _ => P.Ex f) < P.Ex f := by - refine Finset.sum_lt_sum (fun x _ => ?_) ⟨a,Finset.mem_univ _,?_⟩ - · obtain hP | hP := eq_or_lt_of_le (P.nonneg x) - · rw [← hP,zero_mul,zero_mul] - · exact mul_le_mul_of_nonneg_left (hh x hP).le hP.le - · exact mul_lt_mul_of_pos_left (hh a ha) ha - rw [Ex_const] at H - exact (lt_irrefl _ H) - -lemma exists_Ex_le (P : Law α) (f : α → ℝ) : ∃ x, 0 < P x ∧ P.Ex f ≤ f x := by - obtain ⟨x,hx,h⟩ := P.exists_le_Ex fun x => -f x - exact ⟨x,hx, by simpa only [Ex_neg, neg_le_neg_iff] using h⟩ - -open scoped Classical -def prob (P : Law α) (p : α → Prop) : ℝ := P.Ex (fun x => if p x then 1 else 0) - -lemma prob_eq (P : Law α) (p : α → Prop) [DecidablePred p] : - P.prob p = ∑ x, if p x then P x else 0 := by simp only [prob, Ex, mul_ite, mul_one, mul_zero] - -lemma prob_nonneg (P : Law α) (p : α → Prop) : 0 ≤ P.prob p := - Ex_nonneg P (fun x => by split_ifs <;> norm_num) - -lemma prob_le_one (P : Law α) (p : α → Prop) : P.prob p ≤ 1 := by - refine le_trans (b:=P.Ex fun _ => 1) (Ex_mono P fun x => ?_) (P.Ex_const 1).le - split_ifs <;> norm_num - -lemma mem_le_prob (P : Law α) {p : α → Prop} {x} (h : p x) : P x ≤ P.prob p := by - rw [prob_eq] - let f (x : α) := if p x then P x else 0 - have h' (x) : 0 ≤ f x := by unfold f; split_ifs with H; exact P.nonneg _; exact le_rfl - change P x ≤ ∑ y, f y - rw [show P x = f x from (ite_eq_left h).symm] - exact Finset.single_le_sum (fun x _ => h' x) (Finset.mem_univ _) - -lemma prob_pos_iff (P : Law α) (p : α → Prop) : - 0 < P.prob p ↔ ∃ x, p x ∧ 0 < P x := by - constructor - · intro ha - by_contra! h - rw [prob_eq] at ha - refine (not_lt_of_ge (Finset.sum_nonpos fun x _ => ?_) ha) - split_ifs with hh; exact h _ hh; exact le_rfl - · rintro ⟨x,hx,h⟩; exact lt_of_lt_of_le h (mem_le_prob P hx) - -lemma prob_single (P : Law α) (x : α) : P.prob (· = x) = P x := by simp [prob_eq] - -lemma prob_compl (P : Law α) (p : α → Prop) : P.prob (fun x => ¬ p x) = 1 - P.prob p := by - unfold prob - calc - _ = P.Ex (fun x => 1-(if p x then 1 else 0)) := by congr 1; funext x; split_ifs <;> simp_all - _ = _ := by rw [Ex_sub (f:=fun _ => 1), Ex_const] - -lemma ext_of_Ex {P Q : Law α} (h : ∀ f : α → ℝ, P.Ex f = Q.Ex f) : P = Q := by - apply Law.ext; intro x - have H := h (fun a => if a=x then 1 else 0) - change P.prob (· = x) = Q.prob (· = x) at H - simpa only [prob_single] using H - -def point (x : α) : Law α := by - classical - exact ⟨Pi.single x 1, (fun y => by by_cases h : y = x <;> simp [h]), by simp⟩ - -lemma point_apply (x y : α) [DecidableEq α] : - point x y = (if y=x then 1 else 0) := by - classical - change (point x).val y = _ - by_cases h : y = x <;> simp [point, h] - -@[simp] lemma Ex_point (x : α) (f : α → ℝ) : (point x).Ex f = f x := by - simp [Ex, point_apply] - -def uniform (α : Type u) [Fintype α] [Nonempty α] : Law α := - ⟨fun _ => (Fintype.card α : ℝ)⁻¹, fun _ => by positivity, by simp [Fintype.card_ne_zero]⟩ - -lemma uniform_apply [Nonempty α] (x : α) : uniform α x = (Fintype.card α : ℝ)⁻¹ := rfl - -/-- First draw from P, then apply a channel. -/ -def through (P : Law α) (k : α → Law β) : Law β := - ⟨fun y => P.Ex (fun x => k x y), fun _ => Ex_nonneg _ (fun x => (k x).nonneg _), by - simp_rw [← Ex_finset_sum _ P (fun y x => k x y), Law.total, Ex_const]⟩ - - -lemma through_apply (P : Law α) (k : α → Law β) (y) : P.through k y = P.Ex (fun x => k x y) := rfl - -lemma Ex_through (P : Law α) (k : α → Law β) (f : β → ℝ) : - (P.through k).Ex f = P.Ex (fun x => (k x).Ex f) := by - rw [Ex] - simp_rw [through_apply, ← Ex_mul, ← Ex_finset_sum] - rfl - -/-- First draw from P, then apply a channel, retaining the original. -/ -def withNext (P : Law α) (k : α → Law β) : Law (α × β) := - ⟨fun z => P z.1 * k z.1 z.2, fun z => mul_nonneg (P.nonneg _) ((k z.1).nonneg _), by - rw [Fintype.sum_prod_type] - simp only [← Finset.mul_sum, Law.total, mul_one]⟩ - -lemma withNext_apply (P : Law α) (k : α → Law β) (a : α) (b : β) : - P.withNext k (a,b) = P a * k a b := rfl - -lemma Ex_withNext (P : Law α) (k : α → Law β) (f : α × β → ℝ) : - (P.withNext k).Ex f = P.Ex (fun x => (k x).Ex (fun y => f (x,y))) := by - rw [Ex,Fintype.sum_prod_type] - simp only [Ex, withNext_apply, mul_assoc, Finset.mul_sum] - -def tensorP (P : Law α) (Q : Law β) := withNext P (fun _ => Q) - -lemma tensorP_apply (P : Law α) (Q : Law β) (a : α) (b : β) : - P.tensorP Q (a,b) = P a * Q b := rfl - -lemma Ex_tensorP (P : Law α) (Q : Law β) (f : α × β → ℝ) : - (P.tensorP Q).Ex f = P.Ex fun x => Q.Ex fun y => f (x,y) := - Ex_withNext _ _ _ - -/-- Law of a function (random variable). -/ -def send (P : Law α) (X : α → β) : Law β := P.through (fun x => point (X x)) - -lemma send_apply (P : Law α) (X : α → β) (y) : P.send X y = P.prob (fun x => X x = y) := by - simp_rw [send, through_apply, point_apply, prob, eq_comm] - -lemma Ex_send (P : Law α) (X : α → β) (f : β → ℝ) : - (P.send X).Ex f = P.Ex fun x => f (X x) := by simp_rw [send, Ex_through, Ex_point] - -lemma send_send (P : Law α) (X : α → β) (Y : β → γ) : - (P.send X).send Y = P.send (fun x => Y (X x)) := by - apply ext_of_Ex; intro f; simp_rw [Ex_send] - -@[simp] lemma send_id (P : Law α) : P.send (fun x => x) = P := by - apply ext_of_Ex; intro f; exact Ex_send _ _ _ - -lemma le_send (P : Law α) (X : α → β) (x) : P x ≤ P.send X (X x) := by - rw [send_apply]; exact mem_le_prob P (p:=fun y => X y = X x) rfl - -lemma send_pos (P : Law α) (X : α → β) {x} (h : 0 < P x) : - 0 < P.send X (X x) := lt_of_lt_of_le h (le_send P X x) - -lemma send_pos_iff (P : Law α) (X : α → β) (y) : - 0 < P.send X y ↔ ∃ x, X x = y ∧ 0 < P x := by rw [send_apply]; apply prob_pos_iff - -lemma send_congr_mem (P : Law α) {X Y : α → β} (h : ∀ x, 0 < P x → X x = Y x) : - P.send X = P.send Y := by - apply ext_of_Ex; intro f - rw [Ex_send,Ex_send] - exact Ex_congr_mem _ fun x hx => congrArg f (h x hx) - -lemma send_fst_withNext (P : Law α) (k : α → Law β) : - (P.withNext k).send Prod.fst = P := by - apply ext_of_Ex; intro f - simp_rw [Ex_send,Ex_withNext,Ex_const] - -lemma send_snd_withNext (P : Law α) (k : α → Law β) : - (P.withNext k).send Prod.snd = P.through k := by - apply ext_of_Ex; intro f - simp_rw [Ex_send,Ex_withNext,Ex_through] - -lemma send_tensorP (P : Law α) (Q : Law β) (X : α → γ) (Y : β → η) : - (P.tensorP Q).send (fun p => (X p.1, Y p.2)) = (P.send X).tensorP (Q.send Y) := by - apply ext_of_Ex; intro f - simp_rw [Ex_tensorP, Ex_send, Ex_tensorP] - -lemma send_fst_tensorP (P : Law α) (Q : Law β) : - (P.tensorP Q).send Prod.fst = P := send_fst_withNext _ _ - -lemma send_snd_tensorP (P : Law α) (Q : Law β) : - (P.tensorP Q).send Prod.snd = Q := by - apply ext_of_Ex; intro f - simp_rw [Ex_send,Ex_tensorP,Ex_const] - - -/-- Conditional distribution on an event; returns the old law when probability is zero. -/ -def condP (P : Law α) (p : α → Prop) : Law α := - if hp : 0 < P.prob p then - ⟨fun x => (if p x then P x else 0) / P.prob p, - fun x => div_nonneg (by split_ifs; exact P.nonneg _; exact le_rfl) hp.le, by - rw [← Finset.sum_div, ← prob_eq P p, div_self hp.ne']⟩ - else P - -lemma condP_pos_apply (P : Law α) (p : α → Prop) [DecidablePred p] - (hp : 0 < P.prob p) (x : α) : - P.condP p x = (if p x then P x else 0) / P.prob p := by - unfold condP - rw [dite_eq_left hp] - change @ite ℝ (p x) (Classical.propDecidable (p x)) (P x) 0 / P.prob p = _ - by_cases H : p x <;> simp [H] - -lemma prob_mul_condP (P : Law α) (p : α → Prop) [DecidablePred p] (x : α) : - P.prob p * P.condP p x = if p x then P x else 0 := by - obtain H|H := eq_or_lt_of_le (P.prob_nonneg p) - · rw [← H, zero_mul] - split_ifs with h - · exact (P.nonneg _).antisymm ((mem_le_prob P h).trans H.ge) - · rfl - · rw [condP_pos_apply P p H] - field_simp - -lemma condP_mem {P : Law α} {p : α → Prop} (hp : 0 < P.prob p) {x : α} : - 0 < P.condP p x ↔ p x ∧ 0 < P x := by - rw [condP_pos_apply P _ hp, lt_div_iff₀ hp, zero_mul] - split_ifs <;> simp_all - -lemma prob_mul_Ex_condP (P : Law α) (p : α → Prop) [DecidablePred p] (f : α → ℝ) : - P.prob p * (P.condP p).Ex f = P.Ex fun x => if p x then f x else 0 := by - unfold Ex - rw [Finset.mul_sum] - simp only [← mul_assoc, prob_mul_condP, ite_mul, mul_ite, mul_zero, zero_mul] - -/-- Post law after observing W=b. -/ -def after (P : Law α) (W : α → β) (b : β) := P.condP (fun x => W x = b) - -lemma send_mul_after (P : Law α) (W : α → β) [DecidableEq β] (b : β) (x : α) : - P.send W b * P.after W b x = if W x = b then P x else 0 := by - rw [send_apply] - exact prob_mul_condP _ _ _ - -lemma after_mem {P : Law α} {W : α → β} {b : β} (h : 0 < P.send W b) {x : α} : - 0 < P.after W b x ↔ W x = b ∧ 0 < P x := - condP_mem (by rwa [send_apply] at h) - -lemma after_self_pos (P : Law α) (W : α → β) {x : α} (hx : 0 < P x) : - 0 < P.after W (W x) x := (after_mem (P.send_pos W hx)).mpr ⟨rfl,hx⟩ - -lemma after_Ex_sum (P : Law α) (W : α → β) (f : β → α → ℝ) : - (P.send W).Ex (fun w => (P.after W w).Ex (f w)) = - P.Ex fun x => f (W x) x := by - unfold Ex - simp_rw [Finset.mul_sum, ← mul_assoc, send_mul_after] - rw [Finset.sum_comm] - apply Finset.sum_congr rfl - intro x _ - simp [ite_mul] - -lemma through_after (P : Law α) (W : α → β) : - (P.send W).through (P.after W) = P := by - apply ext_of_Ex - intro f - rw [Ex_through] - exact P.after_Ex_sum W (fun _ => f) - -lemma joint_after (P : Law α) (X : α → β) (W : α → γ) (x w) : - P.send (fun a => (X a, W a)) (x,w) = P.send W w * ((P.after W w).send X x) := by - rw [send_apply P, send_apply (after _ _ _)] - simp_rw [prob_eq, Finset.mul_sum, mul_ite, mul_zero, send_mul_after] - refine Finset.sum_congr rfl fun a _ => ?_ - simp only [Prod.mk.injEq] - split_ifs <;> simp_all - -/-- All expectations agree iff the law agrees, so matching after a get operation can -be checked by its marginals. -/ -lemma after_send_eq_of_send_pair_eq {P : Law α} {Q : Law η} - {X : α → β} {W : α → γ} {Y : η → β} {V : η → γ} - (h : P.send (fun a => (X a, W a)) = Q.send (fun a => (Y a, V a))) - (w : γ) : - (P.after W w).send X = (Q.after V w).send Y := by - have H1 := congrArg (fun p : Law (β × γ) => p.send Prod.fst) h - have H2 := congrArg (fun p : Law (β × γ) => p.send Prod.snd) h - simp only [send_send] at H1 H2 - by_cases hh : 0 < P.send W w - · apply Law.ext; intro x - have he : P.send W w * (P.after W w).send X x = - Q.send V w * (Q.after V w).send Y x := by rw [← joint_after, ← joint_after, h] - rw [← H2] at he - exact mul_left_cancel₀ hh.ne' he - · have hh' : ¬ 0 < Q.send V w := by rwa [← H2] - rw [send_apply] at hh hh' - simp only [after, condP, dite_eq_right hh, dite_eq_right hh', H1] - -/-- Binary mixture; real probability t for the first option. -/ -def mixture (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Law α := - ⟨fun a => t * P a + (1-t) * Q a, fun a => add_nonneg - (mul_nonneg h0 (P.nonneg a)) (mul_nonneg (by linarith) (Q.nonneg a)), by - simp only [Finset.sum_add_distrib, ← Finset.mul_sum, Law.total]; ring⟩ - -lemma mixture_apply (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (x : α) : - mixture P Q t h0 h1 x = t * P x + (1-t)*Q x := rfl - -lemma mixture_Ex (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (f : α → ℝ) : - (mixture P Q t h0 h1).Ex f = t * P.Ex f + (1-t) * Q.Ex f := by - simp_rw [Ex,mixture_apply, add_mul,mul_assoc,Finset.sum_add_distrib,Finset.mul_sum] - -lemma le_one (P : Law α) (x : α) : P x ≤ 1 := by - calc - P x ≤ ∑ y, P y := Finset.single_le_sum (fun y _ => P.nonneg y) (Finset.mem_univ x) - _ = 1 := P.total - -/-- Change density proportionally to a nonnegative function. -/ -def proportional (w : α → ℝ) (hw : ∀ x, 0 ≤ w x) (ht : 0 < ∑ x, w x) : Law α := - ⟨fun x => w x / ∑ y, w y, fun x => div_nonneg (hw x) ht.le, by rw [← Finset.sum_div, div_self ht.ne']⟩ - -lemma proportional_apply (w : α → ℝ) (hw : ∀ x, 0 ≤ w x) (ht : 0 < ∑ x, w x) (x) : - proportional w hw ht x = w x / ∑ y, w y := rfl - -end Law -end FourierLLogL - -end OAI +import Mathlib +/-! # Finite probability tools used in the compression and the dyadic model -/ + +open Function Set Filter +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL +universe u v w w' + +private def lawSet (α : Type u) [Fintype α] : Set (α → ℝ) := + {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} + +abbrev Law (α : Type u) [Fintype α] : Type u := lawSet α + +instance {α : Type u} [Fintype α] : FunLike (Law α) α ℝ where + coe P := P.val + coe_injective := by aesop + +instance {α : Type u} [Fintype α] : CompactSpace (Law α) := by + apply isCompact_iff_compactSpace.mp + have hclosed : IsClosed (lawSet α) := by + have heq : lawSet α = + (⋂ x, {f : α → ℝ | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by + ext f + simp [lawSet] + rw [heq] + exact (isClosed_iInter (fun x => + isClosed_le continuous_const (continuous_apply x))).inter + (isClosed_eq (by fun_prop) continuous_const) + apply IsCompact.of_isClosed_subset + (isCompact_Icc : IsCompact (Set.Icc (0 : α → ℝ) 1)) hclosed + intro f hf + rcases hf with ⟨h0, hsum⟩ + constructor + · exact fun x => h0 x + · intro x + change f x ≤ (1 : ℝ) + rw [← hsum] + exact Finset.single_le_sum (fun y _ => h0 y) (Finset.mem_univ x) + +namespace Law +variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} + [Fintype α] [Fintype β] [Fintype γ] [Fintype η] + +protected lemma ext {P Q : Law α} (h : ∀ x, P x = Q x) : P = Q := + DFunLike.ext _ _ h + +protected lemma nonneg (P : Law α) (x) : 0 ≤ P x := P.property.1 x + +protected lemma total (P : Law α) : ∑ x, P x = 1 := P.property.2 + +lemma mem_nonempty (P : Law α) : ∃ x, 0 < P x := by + by_contra! hh + have hi : ∑ x, P x ≤ 0 := Finset.sum_nonpos fun x _ => hh x + rw [P.total] at hi; norm_num at hi + +def Ex (P : Law α) (f : α → ℝ) : ℝ := ∑ x, P x * f x + +lemma Ex_const (P : Law α) (c : ℝ) : P.Ex (fun _ => c) = c := by + rw [Ex, ← Finset.sum_mul, P.total, one_mul] + +lemma Ex_add (P : Law α) (f g : α → ℝ) : + P.Ex (fun x => f x + g x) = P.Ex f + P.Ex g := by + simp_rw [Ex, mul_add, Finset.sum_add_distrib] + +lemma Ex_sub (P : Law α) (f g : α → ℝ) : + P.Ex (fun x => f x - g x) = P.Ex f - P.Ex g := by + simp_rw [Ex, mul_sub, Finset.sum_sub_distrib] + +lemma Ex_mul (P : Law α) (f : α → ℝ) (c : ℝ) : + P.Ex (fun x => f x * c) = P.Ex f * c := by + simp_rw [Ex, ← mul_assoc, Finset.sum_mul] + +lemma Ex_lmul (P : Law α) (f : α → ℝ) (c : ℝ) : + P.Ex (fun x => c * f x) = c * P.Ex f := by + simpa only [mul_comm c] using Ex_mul P f c + +lemma Ex_neg (P : Law α) (f : α → ℝ) : + P.Ex (fun x => -f x) = -P.Ex f := by + simpa only [neg_one_mul] using Ex_lmul P f (-1) + +lemma Ex_finset_sum {ι : Type*} (s : Finset ι) (P : Law α) (f : ι → α → ℝ) : + P.Ex (fun x => ∑ i ∈ s, f i x) = ∑ i ∈ s, P.Ex (f i) := by + unfold Ex + simp_rw [Finset.mul_sum] + exact Finset.sum_comm + +lemma Ex_mono_mem (P : Law α) {f g : α → ℝ} + (h : ∀ x, 0 < P x → f x ≤ g x) : P.Ex f ≤ P.Ex g := by + refine Finset.sum_le_sum fun x _ => ?_ + obtain hP | hP := eq_or_lt_of_le (P.nonneg x) + · rw [← hP, zero_mul, zero_mul] + · exact mul_le_mul_of_nonneg_left (h x hP) hP.le + +lemma Ex_mono (P : Law α) {f g : α → ℝ} + (h : ∀ x, f x ≤ g x) : P.Ex f ≤ P.Ex g := P.Ex_mono_mem fun x _ => h x + +lemma Ex_nonneg (P : Law α) {f : α → ℝ} (h : ∀ x, 0 ≤ f x) : 0 ≤ P.Ex f := by + simpa only [Ex_const] using P.Ex_mono h + +lemma Ex_congr_mem (P : Law α) {f g : α → ℝ} + (h : ∀ x, 0 < P x → f x = g x) : P.Ex f = P.Ex g := + le_antisymm (P.Ex_mono_mem fun x hx => (h x hx).le) + (P.Ex_mono_mem fun x hx => (h x hx).ge) + +lemma exists_le_Ex (P : Law α) (f : α → ℝ) : ∃ x, 0 < P x ∧ f x ≤ P.Ex f := by + by_contra! hh + obtain ⟨a,ha⟩ := P.mem_nonempty + have H : P.Ex (fun _ => P.Ex f) < P.Ex f := by + refine Finset.sum_lt_sum (fun x _ => ?_) ⟨a,Finset.mem_univ _,?_⟩ + · obtain hP | hP := eq_or_lt_of_le (P.nonneg x) + · rw [← hP,zero_mul,zero_mul] + · exact mul_le_mul_of_nonneg_left (hh x hP).le hP.le + · exact mul_lt_mul_of_pos_left (hh a ha) ha + rw [Ex_const] at H + exact (lt_irrefl _ H) + +lemma exists_Ex_le (P : Law α) (f : α → ℝ) : ∃ x, 0 < P x ∧ P.Ex f ≤ f x := by + obtain ⟨x,hx,h⟩ := P.exists_le_Ex fun x => -f x + exact ⟨x,hx, by simpa only [Ex_neg, neg_le_neg_iff] using h⟩ + +open scoped Classical +def prob (P : Law α) (p : α → Prop) : ℝ := P.Ex (fun x => if p x then 1 else 0) + +lemma prob_eq (P : Law α) (p : α → Prop) [DecidablePred p] : + P.prob p = ∑ x, if p x then P x else 0 := by simp only [prob, Ex, mul_ite, mul_one, mul_zero] + +lemma prob_nonneg (P : Law α) (p : α → Prop) : 0 ≤ P.prob p := + Ex_nonneg P (fun x => by split_ifs <;> norm_num) + +lemma prob_le_one (P : Law α) (p : α → Prop) : P.prob p ≤ 1 := by + refine le_trans (b:=P.Ex fun _ => 1) (Ex_mono P fun x => ?_) (P.Ex_const 1).le + split_ifs <;> norm_num + +lemma mem_le_prob (P : Law α) {p : α → Prop} {x} (h : p x) : P x ≤ P.prob p := by + rw [prob_eq] + let f (x : α) := if p x then P x else 0 + have h' (x) : 0 ≤ f x := by unfold f; split_ifs with H; exact P.nonneg _; exact le_rfl + change P x ≤ ∑ y, f y + rw [show P x = f x from (ite_eq_left h).symm] + exact Finset.single_le_sum (fun x _ => h' x) (Finset.mem_univ _) + +lemma prob_pos_iff (P : Law α) (p : α → Prop) : + 0 < P.prob p ↔ ∃ x, p x ∧ 0 < P x := by + constructor + · intro ha + by_contra! h + rw [prob_eq] at ha + refine (not_lt_of_ge (Finset.sum_nonpos fun x _ => ?_) ha) + split_ifs with hh; exact h _ hh; exact le_rfl + · rintro ⟨x,hx,h⟩; exact lt_of_lt_of_le h (mem_le_prob P hx) + +lemma prob_single (P : Law α) (x : α) : P.prob (· = x) = P x := by simp [prob_eq] + +lemma prob_compl (P : Law α) (p : α → Prop) : P.prob (fun x => ¬ p x) = 1 - P.prob p := by + unfold prob + calc + _ = P.Ex (fun x => 1-(if p x then 1 else 0)) := by congr 1; funext x; split_ifs <;> simp_all + _ = _ := by rw [Ex_sub (f:=fun _ => 1), Ex_const] + +lemma ext_of_Ex {P Q : Law α} (h : ∀ f : α → ℝ, P.Ex f = Q.Ex f) : P = Q := by + apply Law.ext; intro x + have H := h (fun a => if a=x then 1 else 0) + change P.prob (· = x) = Q.prob (· = x) at H + simpa only [prob_single] using H + +def point (x : α) : Law α := by + classical + exact ⟨Pi.single x 1, (fun y => by by_cases h : y = x <;> simp [h]), by simp⟩ + +lemma point_apply (x y : α) [DecidableEq α] : + point x y = (if y=x then 1 else 0) := by + classical + change (point x).val y = _ + by_cases h : y = x <;> simp [point, h] + +@[simp] lemma Ex_point (x : α) (f : α → ℝ) : (point x).Ex f = f x := by + simp [Ex, point_apply] + +def uniform (α : Type u) [Fintype α] [Nonempty α] : Law α := + ⟨fun _ => (Fintype.card α : ℝ)⁻¹, fun _ => by positivity, by simp [Fintype.card_ne_zero]⟩ + +lemma uniform_apply [Nonempty α] (x : α) : uniform α x = (Fintype.card α : ℝ)⁻¹ := rfl + +/-- First draw from P, then apply a channel. -/ +def through (P : Law α) (k : α → Law β) : Law β := + ⟨fun y => P.Ex (fun x => k x y), fun _ => Ex_nonneg _ (fun x => (k x).nonneg _), by + simp_rw [← Ex_finset_sum _ P (fun y x => k x y), Law.total, Ex_const]⟩ + + +lemma through_apply (P : Law α) (k : α → Law β) (y) : P.through k y = P.Ex (fun x => k x y) := rfl + +lemma Ex_through (P : Law α) (k : α → Law β) (f : β → ℝ) : + (P.through k).Ex f = P.Ex (fun x => (k x).Ex f) := by + rw [Ex] + simp_rw [through_apply, ← Ex_mul, ← Ex_finset_sum] + rfl + +/-- First draw from P, then apply a channel, retaining the original. -/ +def withNext (P : Law α) (k : α → Law β) : Law (α × β) := + ⟨fun z => P z.1 * k z.1 z.2, fun z => mul_nonneg (P.nonneg _) ((k z.1).nonneg _), by + rw [Fintype.sum_prod_type] + simp only [← Finset.mul_sum, Law.total, mul_one]⟩ + +lemma withNext_apply (P : Law α) (k : α → Law β) (a : α) (b : β) : + P.withNext k (a,b) = P a * k a b := rfl + +lemma Ex_withNext (P : Law α) (k : α → Law β) (f : α × β → ℝ) : + (P.withNext k).Ex f = P.Ex (fun x => (k x).Ex (fun y => f (x,y))) := by + rw [Ex,Fintype.sum_prod_type] + simp only [Ex, withNext_apply, mul_assoc, Finset.mul_sum] + +def tensorP (P : Law α) (Q : Law β) := withNext P (fun _ => Q) + +lemma tensorP_apply (P : Law α) (Q : Law β) (a : α) (b : β) : + P.tensorP Q (a,b) = P a * Q b := rfl + +lemma Ex_tensorP (P : Law α) (Q : Law β) (f : α × β → ℝ) : + (P.tensorP Q).Ex f = P.Ex fun x => Q.Ex fun y => f (x,y) := + Ex_withNext _ _ _ + +/-- Law of a function (random variable). -/ +def send (P : Law α) (X : α → β) : Law β := P.through (fun x => point (X x)) + +lemma send_apply (P : Law α) (X : α → β) (y) : P.send X y = P.prob (fun x => X x = y) := by + simp_rw [send, through_apply, point_apply, prob, eq_comm] + +lemma Ex_send (P : Law α) (X : α → β) (f : β → ℝ) : + (P.send X).Ex f = P.Ex fun x => f (X x) := by simp_rw [send, Ex_through, Ex_point] + +lemma send_send (P : Law α) (X : α → β) (Y : β → γ) : + (P.send X).send Y = P.send (fun x => Y (X x)) := by + apply ext_of_Ex; intro f; simp_rw [Ex_send] + +@[simp] lemma send_id (P : Law α) : P.send (fun x => x) = P := by + apply ext_of_Ex; intro f; exact Ex_send _ _ _ + +lemma le_send (P : Law α) (X : α → β) (x) : P x ≤ P.send X (X x) := by + rw [send_apply]; exact mem_le_prob P (p:=fun y => X y = X x) rfl + +lemma send_pos (P : Law α) (X : α → β) {x} (h : 0 < P x) : + 0 < P.send X (X x) := lt_of_lt_of_le h (le_send P X x) + +lemma send_pos_iff (P : Law α) (X : α → β) (y) : + 0 < P.send X y ↔ ∃ x, X x = y ∧ 0 < P x := by rw [send_apply]; apply prob_pos_iff + +lemma send_congr_mem (P : Law α) {X Y : α → β} (h : ∀ x, 0 < P x → X x = Y x) : + P.send X = P.send Y := by + apply ext_of_Ex; intro f + rw [Ex_send,Ex_send] + exact Ex_congr_mem _ fun x hx => congrArg f (h x hx) + +lemma send_fst_withNext (P : Law α) (k : α → Law β) : + (P.withNext k).send Prod.fst = P := by + apply ext_of_Ex; intro f + simp_rw [Ex_send,Ex_withNext,Ex_const] + +lemma send_snd_withNext (P : Law α) (k : α → Law β) : + (P.withNext k).send Prod.snd = P.through k := by + apply ext_of_Ex; intro f + simp_rw [Ex_send,Ex_withNext,Ex_through] + +lemma send_tensorP (P : Law α) (Q : Law β) (X : α → γ) (Y : β → η) : + (P.tensorP Q).send (fun p => (X p.1, Y p.2)) = (P.send X).tensorP (Q.send Y) := by + apply ext_of_Ex; intro f + simp_rw [Ex_tensorP, Ex_send, Ex_tensorP] + +lemma send_fst_tensorP (P : Law α) (Q : Law β) : + (P.tensorP Q).send Prod.fst = P := send_fst_withNext _ _ + +lemma send_snd_tensorP (P : Law α) (Q : Law β) : + (P.tensorP Q).send Prod.snd = Q := by + apply ext_of_Ex; intro f + simp_rw [Ex_send,Ex_tensorP,Ex_const] + + +/-- Conditional distribution on an event; returns the old law when probability is zero. -/ +def condP (P : Law α) (p : α → Prop) : Law α := + if hp : 0 < P.prob p then + ⟨fun x => (if p x then P x else 0) / P.prob p, + fun x => div_nonneg (by split_ifs; exact P.nonneg _; exact le_rfl) hp.le, by + rw [← Finset.sum_div, ← prob_eq P p, div_self hp.ne']⟩ + else P + +lemma condP_pos_apply (P : Law α) (p : α → Prop) [DecidablePred p] + (hp : 0 < P.prob p) (x : α) : + P.condP p x = (if p x then P x else 0) / P.prob p := by + unfold condP + rw [dite_eq_left hp] + change @ite ℝ (p x) (Classical.propDecidable (p x)) (P x) 0 / P.prob p = _ + by_cases H : p x <;> simp [H] + +lemma prob_mul_condP (P : Law α) (p : α → Prop) [DecidablePred p] (x : α) : + P.prob p * P.condP p x = if p x then P x else 0 := by + obtain H|H := eq_or_lt_of_le (P.prob_nonneg p) + · rw [← H, zero_mul] + split_ifs with h + · exact (P.nonneg _).antisymm ((mem_le_prob P h).trans H.ge) + · rfl + · rw [condP_pos_apply P p H] + field_simp + +lemma condP_mem {P : Law α} {p : α → Prop} (hp : 0 < P.prob p) {x : α} : + 0 < P.condP p x ↔ p x ∧ 0 < P x := by + rw [condP_pos_apply P _ hp, lt_div_iff₀ hp, zero_mul] + split_ifs <;> simp_all + +lemma prob_mul_Ex_condP (P : Law α) (p : α → Prop) [DecidablePred p] (f : α → ℝ) : + P.prob p * (P.condP p).Ex f = P.Ex fun x => if p x then f x else 0 := by + unfold Ex + rw [Finset.mul_sum] + simp only [← mul_assoc, prob_mul_condP, ite_mul, mul_ite, mul_zero, zero_mul] + +/-- Post law after observing W=b. -/ +def after (P : Law α) (W : α → β) (b : β) := P.condP (fun x => W x = b) + +lemma send_mul_after (P : Law α) (W : α → β) [DecidableEq β] (b : β) (x : α) : + P.send W b * P.after W b x = if W x = b then P x else 0 := by + rw [send_apply] + exact prob_mul_condP _ _ _ + +lemma after_mem {P : Law α} {W : α → β} {b : β} (h : 0 < P.send W b) {x : α} : + 0 < P.after W b x ↔ W x = b ∧ 0 < P x := + condP_mem (by rwa [send_apply] at h) + +lemma after_self_pos (P : Law α) (W : α → β) {x : α} (hx : 0 < P x) : + 0 < P.after W (W x) x := (after_mem (P.send_pos W hx)).mpr ⟨rfl,hx⟩ + +lemma after_Ex_sum (P : Law α) (W : α → β) (f : β → α → ℝ) : + (P.send W).Ex (fun w => (P.after W w).Ex (f w)) = + P.Ex fun x => f (W x) x := by + unfold Ex + simp_rw [Finset.mul_sum, ← mul_assoc, send_mul_after] + rw [Finset.sum_comm] + apply Finset.sum_congr rfl + intro x _ + simp [ite_mul] + +lemma through_after (P : Law α) (W : α → β) : + (P.send W).through (P.after W) = P := by + apply ext_of_Ex + intro f + rw [Ex_through] + exact P.after_Ex_sum W (fun _ => f) + +lemma joint_after (P : Law α) (X : α → β) (W : α → γ) (x w) : + P.send (fun a => (X a, W a)) (x,w) = P.send W w * ((P.after W w).send X x) := by + rw [send_apply P, send_apply (after _ _ _)] + simp_rw [prob_eq, Finset.mul_sum, mul_ite, mul_zero, send_mul_after] + refine Finset.sum_congr rfl fun a _ => ?_ + simp only [Prod.mk.injEq] + split_ifs <;> simp_all + +/-- All expectations agree iff the law agrees, so matching after a get operation can +be checked by its marginals. -/ +lemma after_send_eq_of_send_pair_eq {P : Law α} {Q : Law η} + {X : α → β} {W : α → γ} {Y : η → β} {V : η → γ} + (h : P.send (fun a => (X a, W a)) = Q.send (fun a => (Y a, V a))) + (w : γ) : + (P.after W w).send X = (Q.after V w).send Y := by + have H1 := congrArg (fun p : Law (β × γ) => p.send Prod.fst) h + have H2 := congrArg (fun p : Law (β × γ) => p.send Prod.snd) h + simp only [send_send] at H1 H2 + by_cases hh : 0 < P.send W w + · apply Law.ext; intro x + have he : P.send W w * (P.after W w).send X x = + Q.send V w * (Q.after V w).send Y x := by rw [← joint_after, ← joint_after, h] + rw [← H2] at he + exact mul_left_cancel₀ hh.ne' he + · have hh' : ¬ 0 < Q.send V w := by rwa [← H2] + rw [send_apply] at hh hh' + simp only [after, condP, dite_eq_right hh, dite_eq_right hh', H1] + +/-- Binary mixture; real probability t for the first option. -/ +def mixture (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Law α := + ⟨fun a => t * P a + (1-t) * Q a, fun a => add_nonneg + (mul_nonneg h0 (P.nonneg a)) (mul_nonneg (by linarith) (Q.nonneg a)), by + simp only [Finset.sum_add_distrib, ← Finset.mul_sum, Law.total]; ring⟩ + +lemma mixture_apply (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (x : α) : + mixture P Q t h0 h1 x = t * P x + (1-t)*Q x := rfl + +lemma mixture_Ex (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (f : α → ℝ) : + (mixture P Q t h0 h1).Ex f = t * P.Ex f + (1-t) * Q.Ex f := by + simp_rw [Ex,mixture_apply, add_mul,mul_assoc,Finset.sum_add_distrib,Finset.mul_sum] + +lemma le_one (P : Law α) (x : α) : P x ≤ 1 := by + calc + P x ≤ ∑ y, P y := Finset.single_le_sum (fun y _ => P.nonneg y) (Finset.mem_univ x) + _ = 1 := P.total + +/-- Change density proportionally to a nonnegative function. -/ +def proportional (w : α → ℝ) (hw : ∀ x, 0 ≤ w x) (ht : 0 < ∑ x, w x) : Law α := + ⟨fun x => w x / ∑ y, w y, fun x => div_nonneg (hw x) ht.le, by rw [← Finset.sum_div, div_self ht.ne']⟩ + +lemma proportional_apply (w : α → ℝ) (hw : ∀ x, 0 ≤ w x) (ht : 0 < ∑ x, w x) (x) : + proportional w hw ht x = w x / ∑ y, w y := rfl + +end Law +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Mixture.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Mixture.lean index 4ef7bcca85d8a1b625a8d9db26b0af230c33c788..c40d2232e25b9976d00eadddd7c81c236266f0bd 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Mixture.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Mixture.lean @@ -1,188 +1,188 @@ -import OAI.Analysis.FourierLLogL.Finite.Comparison -/-! # Entropy of mixtures -/ -open Function Set Filter -open scoped BigOperators -noncomputable section - -namespace OAI - -namespace FourierLLogL.Law -open Real -universe u v w w' - -variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} - [Fintype α] [Fintype β] [Fintype γ] [Fintype η] - -lemma send_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) : - (P.withNext Q).send (fun (z : α × β) => (z.1, X z.1 z.2)) = - P.withNext (fun i => (Q i).send (X i)) := by - apply ext_of_Ex; intro f - simp_rw [Ex_withNext,Ex_send,Ex_withNext] - -lemma H_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) : - (P.withNext Q).H (fun (z : α × β) => (z.1, X z.1 z.2)) = - P.entropy + P.Ex (fun i => (Q i).H (X i)) := by - unfold H - rw [send_withNext, entropy_withNext] - -lemma Hcond_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) (W : α → β → η) : - (P.withNext Q).Hcond (fun z => X z.1 z.2) (fun z => (z.1,W z.1 z.2)) = - P.Ex fun i => (Q i).Hcond (X i) (W i) := by - unfold Hcond - let R := P.withNext Q - have H : R.H (fun z => ((X z.1 z.2),z.1,W z.1 z.2)) = - R.H (fun z => (z.1,X z.1 z.2,W z.1 z.2)) := - R.H_partition fun x y => by simp [and_left_comm] - change R.H _ - R.H _ = _ - rw [H] - unfold R - rw [H_withNext P Q (fun i x => (X i x,W i x)), H_withNext P Q W, - Ex_sub]; ring - -lemma Hcond_withNext' (P : Law α) (Q : α → Law β) (X : α → β → γ) : - (P.withNext Q).Hcond (fun z => X z.1 z.2) Prod.fst = - P.Ex fun i => (Q i).H (X i) := by - have h := Hcond_withNext P Q X (fun _ _ => ()) - rw [Hcond_right_partition _ _ (Z:=Prod.fst) (by simp)] at h - simpa only [Hcond_constant] using h - -lemma Hcond_through (P : Law α) (Q : α → Law β) (X : β → γ) (W : β → η) : - P.Ex (fun a => (Q a).Hcond X W) ≤ (P.through Q).Hcond X W := by - rw [← send_snd_withNext, Hcond_send, - ← Hcond_withNext P Q (fun _ => X) (fun _ => W)] - exact Hcond_refine _ _ Prod.fst _ - -lemma H_through_ge (P : Law α) (Q : α → Law β) (X : β → γ) : - P.Ex (fun a => (Q a).H X) ≤ (P.through Q).H X := by - rw [← send_snd_withNext, H_send, - ← Hcond_withNext' P Q (fun _ => X)] - exact Hcond_le_H _ _ _ - -lemma H_through_le (P : Law α) (Q : α → Law β) (X : β → γ) : - (P.through Q).H X ≤ P.entropy + P.Ex (fun a => (Q a).H X) := by - rw [← H_withNext P Q (fun _ => X), ← send_snd_withNext, H_send] - exact H_le_of_determined _ (by simp) - - -def coinP (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Law Bool := - mixture (point true) (point false) t h0 h1 - -lemma coinP_Ex (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (f : Bool → ℝ) : - (coinP t h0 h1).Ex f = t*f true+(1-t)*f false := by - rw [coinP, mixture_Ex, Ex_point, Ex_point] - -lemma coinP_entropy (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - (coinP t h0 h1).entropy ≤ log 2 := by simpa using entropy_le_log_card (coinP t h0 h1) - -lemma mixture_eq_through (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - mixture P Q t h0 h1 = (coinP t h0 h1).through (fun b => if b then P else Q) := by - apply ext_of_Ex; intro f - rw [Ex_through,mixture_Ex,coinP_Ex] - rfl - -lemma H_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (X : α → β) : - t*(P.H X)+(1-t)*(Q.H X) ≤ (mixture P Q t h0 h1).H X := by - rw [mixture_eq_through] - have h := H_through_ge (coinP t h0 h1) (fun b => if b then P else Q) X - rwa [coinP_Ex] at h - -lemma Hcond_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) - (X : α → β) (W : α → γ) : - t*(P.Hcond X W)+(1-t)*(Q.Hcond X W) ≤ (mixture P Q t h0 h1).Hcond X W := by - rw [mixture_eq_through] - have h := Hcond_through (coinP t h0 h1) (fun b => if b then P else Q) X W - rwa [coinP_Ex] at h - -lemma entropy_mixture_le (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - (mixture P Q t h0 h1).entropy ≤ log 2 + (t*P.entropy+(1-t)*Q.entropy) := by - rw [mixture_eq_through, ← H_self] - let C := coinP t h0 h1 - let f (b : Bool) := if b then P else Q - refine (H_through_le C f (fun x => x)).trans ?_ - dsimp only [C] - rw [coinP_Ex] - simp only [f, H_self]; norm_num - exact coinP_entropy _ _ _ - -lemma entropy_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : - t*P.entropy+(1-t)*Q.entropy ≤ (mixture P Q t h0 h1).entropy := by - simpa only [H_self] using H_mixture_ge P Q t h0 h1 (fun x => x) - -/-- Support version of the entropy bound. -/ -lemma entropy_le_log_size (P : Law α) (s : Finset α) - (h : ∀ x, 0 < P x → x ∈ s) : P.entropy ≤ log (s.card : ℝ) := by - classical - let q (x : α) := if x ∈ s then (s.card:ℝ)⁻¹ else 0 - have hs : 0 < (s.card:ℝ) := by - obtain ⟨x,hx⟩ := P.mem_nonempty - exact_mod_cast (Finset.card_pos.mpr ⟨x,h x hx⟩) - have H : P.entropy ≤ P.crossE q := by - apply entropy_le_cross_aux - · intro x; dsimp only [q]; split_ifs <;> positivity - · rw [← Finset.sum_subset (Finset.subset_univ s) (by intros i _ hi; simp [q,hi])] - rw [Finset.sum_congr rfl (fun x (hx : x ∈ s) => ite_eq_left hx : ∀ x ∈ s, q x = (s.card:ℝ)⁻¹)] - simp [ne_of_gt hs] - · intro x hx - dsimp only [q]; rw [ite_eq_left (h x hx)]; positivity - refine H.trans (le_of_eq ?_) - unfold crossE - calc - _ = P.Ex (fun _ => log (s.card:ℝ)) := P.Ex_congr_mem fun x hx => by - simp only [q, ite_eq_left (h x hx), log_inv, neg_neg] - _ = _ := P.Ex_const _ - -/-- Entropy modulus sufficient for the replacement by independent letters. -/ -theorem entropy_le_of_tv (P Q : Law α) (s : Finset α) - (h : ∀ x, 0 < P x → x ∈ s) : - P.entropy ≤ Q.entropy + (log 2 + P.tv Q * log (s.card:ℝ)) := by - let t := P.tv Q - have h0 : 0 ≤ t := tv_nonneg _ _ - have h1 : t ≤ 1 := tv_le_one _ _ - let a (x : α) := min (P x) (Q x) - have ht : ∑ x, a x = 1-t := P.tv_common Q - have ha (x : α) : 0 ≤ a x := le_min (P.nonneg x) (Q.nonneg x) - have hpl (x : α) : 0 ≤ P x - a x := sub_nonneg.mpr (min_le_left _ _) - have hql (x : α) : 0 ≤ Q x - a x := sub_nonneg.mpr (min_le_right _ _) - have hps : ∑ x, (P x - a x) = t := by rw [Finset.sum_sub_distrib, P.total, ht]; ring - have hqs : ∑ x, (Q x - a x) = t := by rw [Finset.sum_sub_distrib, Q.total, ht]; ring - have hg : 0 ≤ log (2:ℝ) := log_nonneg (by norm_num) - change _ ≤ _+(log 2+t*_) - obtain ht0|ht0 := eq_or_lt_of_le h0 - · have H : P = Q := by - apply Law.ext; intro x - have h1 := Finset.single_le_sum (f:=fun x => P x-a x) (fun y _ => hpl y) (Finset.mem_univ x) - have h2 := Finset.single_le_sum (f:=fun x => Q x-a x) (fun y _ => hql y) (Finset.mem_univ x) - linarith [hpl x,hql x] - rw [← ht0, H,zero_mul]; linarith - obtain ht1|ht1 := eq_or_lt_of_le h1 - · rw [ht1, one_mul]; linarith [entropy_le_log_size P s h, Q.entropy_nonneg] - have hb := proportional_apply a ha (ht.symm ▸ sub_pos.mpr ht1) - let R := proportional a ha (by linarith) - let U := proportional _ hpl (by linarith) - let V := proportional _ hql (by linarith) - have hP : P = mixture U R t h0 h1 := by - apply Law.ext; intro x - rw [mixture_apply] - dsimp only [R,U]; rw [proportional_apply, proportional_apply, ht,hps] - field_simp - ring - - have hQ : Q = mixture V R t h0 h1 := by - apply Law.ext; intro x - rw [mixture_apply] - dsimp only [R,V]; rw [proportional_apply, proportional_apply, ht,hqs] - field_simp - ring - have hU : U.entropy ≤ log (s.card:ℝ) := entropy_le_log_size U s fun x hx => by - apply h - rw [hP,mixture_apply] - exact add_pos_of_pos_of_nonneg (mul_pos ht0 hx) - (mul_nonneg (sub_nonneg.mpr h1) (R.nonneg x)) - have Hu := entropy_mixture_le U R t h0 h1 - have Hv := entropy_mixture_ge V R t h0 h1 - rw [hP, hQ] - nlinarith [V.entropy_nonneg] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Comparison +/-! # Entropy of mixtures -/ +open Function Set Filter +open scoped BigOperators +noncomputable section + +namespace OAI + +namespace FourierLLogL.Law +open Real +universe u v w w' + +variable {α : Type u} {β : Type v} {γ : Type w} {η : Type w'} + [Fintype α] [Fintype β] [Fintype γ] [Fintype η] + +lemma send_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) : + (P.withNext Q).send (fun (z : α × β) => (z.1, X z.1 z.2)) = + P.withNext (fun i => (Q i).send (X i)) := by + apply ext_of_Ex; intro f + simp_rw [Ex_withNext,Ex_send,Ex_withNext] + +lemma H_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) : + (P.withNext Q).H (fun (z : α × β) => (z.1, X z.1 z.2)) = + P.entropy + P.Ex (fun i => (Q i).H (X i)) := by + unfold H + rw [send_withNext, entropy_withNext] + +lemma Hcond_withNext (P : Law α) (Q : α → Law β) (X : α → β → γ) (W : α → β → η) : + (P.withNext Q).Hcond (fun z => X z.1 z.2) (fun z => (z.1,W z.1 z.2)) = + P.Ex fun i => (Q i).Hcond (X i) (W i) := by + unfold Hcond + let R := P.withNext Q + have H : R.H (fun z => ((X z.1 z.2),z.1,W z.1 z.2)) = + R.H (fun z => (z.1,X z.1 z.2,W z.1 z.2)) := + R.H_partition fun x y => by simp [and_left_comm] + change R.H _ - R.H _ = _ + rw [H] + unfold R + rw [H_withNext P Q (fun i x => (X i x,W i x)), H_withNext P Q W, + Ex_sub]; ring + +lemma Hcond_withNext' (P : Law α) (Q : α → Law β) (X : α → β → γ) : + (P.withNext Q).Hcond (fun z => X z.1 z.2) Prod.fst = + P.Ex fun i => (Q i).H (X i) := by + have h := Hcond_withNext P Q X (fun _ _ => ()) + rw [Hcond_right_partition _ _ (Z:=Prod.fst) (by simp)] at h + simpa only [Hcond_constant] using h + +lemma Hcond_through (P : Law α) (Q : α → Law β) (X : β → γ) (W : β → η) : + P.Ex (fun a => (Q a).Hcond X W) ≤ (P.through Q).Hcond X W := by + rw [← send_snd_withNext, Hcond_send, + ← Hcond_withNext P Q (fun _ => X) (fun _ => W)] + exact Hcond_refine _ _ Prod.fst _ + +lemma H_through_ge (P : Law α) (Q : α → Law β) (X : β → γ) : + P.Ex (fun a => (Q a).H X) ≤ (P.through Q).H X := by + rw [← send_snd_withNext, H_send, + ← Hcond_withNext' P Q (fun _ => X)] + exact Hcond_le_H _ _ _ + +lemma H_through_le (P : Law α) (Q : α → Law β) (X : β → γ) : + (P.through Q).H X ≤ P.entropy + P.Ex (fun a => (Q a).H X) := by + rw [← H_withNext P Q (fun _ => X), ← send_snd_withNext, H_send] + exact H_le_of_determined _ (by simp) + + +def coinP (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : Law Bool := + mixture (point true) (point false) t h0 h1 + +lemma coinP_Ex (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (f : Bool → ℝ) : + (coinP t h0 h1).Ex f = t*f true+(1-t)*f false := by + rw [coinP, mixture_Ex, Ex_point, Ex_point] + +lemma coinP_entropy (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + (coinP t h0 h1).entropy ≤ log 2 := by simpa using entropy_le_log_card (coinP t h0 h1) + +lemma mixture_eq_through (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + mixture P Q t h0 h1 = (coinP t h0 h1).through (fun b => if b then P else Q) := by + apply ext_of_Ex; intro f + rw [Ex_through,mixture_Ex,coinP_Ex] + rfl + +lemma H_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) (X : α → β) : + t*(P.H X)+(1-t)*(Q.H X) ≤ (mixture P Q t h0 h1).H X := by + rw [mixture_eq_through] + have h := H_through_ge (coinP t h0 h1) (fun b => if b then P else Q) X + rwa [coinP_Ex] at h + +lemma Hcond_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) + (X : α → β) (W : α → γ) : + t*(P.Hcond X W)+(1-t)*(Q.Hcond X W) ≤ (mixture P Q t h0 h1).Hcond X W := by + rw [mixture_eq_through] + have h := Hcond_through (coinP t h0 h1) (fun b => if b then P else Q) X W + rwa [coinP_Ex] at h + +lemma entropy_mixture_le (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + (mixture P Q t h0 h1).entropy ≤ log 2 + (t*P.entropy+(1-t)*Q.entropy) := by + rw [mixture_eq_through, ← H_self] + let C := coinP t h0 h1 + let f (b : Bool) := if b then P else Q + refine (H_through_le C f (fun x => x)).trans ?_ + dsimp only [C] + rw [coinP_Ex] + simp only [f, H_self]; norm_num + exact coinP_entropy _ _ _ + +lemma entropy_mixture_ge (P Q : Law α) (t : ℝ) (h0 : 0 ≤ t) (h1 : t ≤ 1) : + t*P.entropy+(1-t)*Q.entropy ≤ (mixture P Q t h0 h1).entropy := by + simpa only [H_self] using H_mixture_ge P Q t h0 h1 (fun x => x) + +/-- Support version of the entropy bound. -/ +lemma entropy_le_log_size (P : Law α) (s : Finset α) + (h : ∀ x, 0 < P x → x ∈ s) : P.entropy ≤ log (s.card : ℝ) := by + classical + let q (x : α) := if x ∈ s then (s.card:ℝ)⁻¹ else 0 + have hs : 0 < (s.card:ℝ) := by + obtain ⟨x,hx⟩ := P.mem_nonempty + exact_mod_cast (Finset.card_pos.mpr ⟨x,h x hx⟩) + have H : P.entropy ≤ P.crossE q := by + apply entropy_le_cross_aux + · intro x; dsimp only [q]; split_ifs <;> positivity + · rw [← Finset.sum_subset (Finset.subset_univ s) (by intros i _ hi; simp [q,hi])] + rw [Finset.sum_congr rfl (fun x (hx : x ∈ s) => ite_eq_left hx : ∀ x ∈ s, q x = (s.card:ℝ)⁻¹)] + simp [ne_of_gt hs] + · intro x hx + dsimp only [q]; rw [ite_eq_left (h x hx)]; positivity + refine H.trans (le_of_eq ?_) + unfold crossE + calc + _ = P.Ex (fun _ => log (s.card:ℝ)) := P.Ex_congr_mem fun x hx => by + simp only [q, ite_eq_left (h x hx), log_inv, neg_neg] + _ = _ := P.Ex_const _ + +/-- Entropy modulus sufficient for the replacement by independent letters. -/ +theorem entropy_le_of_tv (P Q : Law α) (s : Finset α) + (h : ∀ x, 0 < P x → x ∈ s) : + P.entropy ≤ Q.entropy + (log 2 + P.tv Q * log (s.card:ℝ)) := by + let t := P.tv Q + have h0 : 0 ≤ t := tv_nonneg _ _ + have h1 : t ≤ 1 := tv_le_one _ _ + let a (x : α) := min (P x) (Q x) + have ht : ∑ x, a x = 1-t := P.tv_common Q + have ha (x : α) : 0 ≤ a x := le_min (P.nonneg x) (Q.nonneg x) + have hpl (x : α) : 0 ≤ P x - a x := sub_nonneg.mpr (min_le_left _ _) + have hql (x : α) : 0 ≤ Q x - a x := sub_nonneg.mpr (min_le_right _ _) + have hps : ∑ x, (P x - a x) = t := by rw [Finset.sum_sub_distrib, P.total, ht]; ring + have hqs : ∑ x, (Q x - a x) = t := by rw [Finset.sum_sub_distrib, Q.total, ht]; ring + have hg : 0 ≤ log (2:ℝ) := log_nonneg (by norm_num) + change _ ≤ _+(log 2+t*_) + obtain ht0|ht0 := eq_or_lt_of_le h0 + · have H : P = Q := by + apply Law.ext; intro x + have h1 := Finset.single_le_sum (f:=fun x => P x-a x) (fun y _ => hpl y) (Finset.mem_univ x) + have h2 := Finset.single_le_sum (f:=fun x => Q x-a x) (fun y _ => hql y) (Finset.mem_univ x) + linarith [hpl x,hql x] + rw [← ht0, H,zero_mul]; linarith + obtain ht1|ht1 := eq_or_lt_of_le h1 + · rw [ht1, one_mul]; linarith [entropy_le_log_size P s h, Q.entropy_nonneg] + have hb := proportional_apply a ha (ht.symm ▸ sub_pos.mpr ht1) + let R := proportional a ha (by linarith) + let U := proportional _ hpl (by linarith) + let V := proportional _ hql (by linarith) + have hP : P = mixture U R t h0 h1 := by + apply Law.ext; intro x + rw [mixture_apply] + dsimp only [R,U]; rw [proportional_apply, proportional_apply, ht,hps] + field_simp + ring + + have hQ : Q = mixture V R t h0 h1 := by + apply Law.ext; intro x + rw [mixture_apply] + dsimp only [R,V]; rw [proportional_apply, proportional_apply, ht,hqs] + field_simp + ring + have hU : U.entropy ≤ log (s.card:ℝ) := entropy_le_log_size U s fun x hx => by + apply h + rw [hP,mixture_apply] + exact add_pos_of_pos_of_nonneg (mul_pos ht0 hx) + (mul_nonneg (sub_nonneg.mpr h1) (R.nonneg x)) + have Hu := entropy_mixture_le U R t h0 h1 + have Hv := entropy_mixture_ge V R t h0 h1 + rw [hP, hQ] + nlinarith [V.entropy_nonneg] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Quadratic.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Quadratic.lean index a27b3bf9ffbe99816be05b6a93511692b8f6c62d..1b4267afb6963925a9174f0122826f21fa0568dc 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Quadratic.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Quadratic.lean @@ -1,54 +1,54 @@ -import OAI.Analysis.FourierLLogL.Finite.Complex - -open Set Function Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Law -open Real -variable {α : Type} [Fintype α] - -lemma sq_Ex_le (P : Law α) (f g h : α → ℝ) (hg : ∀ x, 0 ≤ g x) (hh : ∀ x, 0 ≤ h x) - (H : ∀ x, f x^2 ≤ g x*h x) : P.Ex f^2 ≤ P.Ex g * P.Ex h := by - refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ - (f:=fun x => P x * g x) (g:=fun x => P x * h x) - (fun x _ => mul_nonneg (P.nonneg x) (hg x)) - (fun x _ => mul_nonneg (P.nonneg x) (hh x)) ?_ - intro x _ - have hi := mul_le_mul_of_nonneg_left (H x) (sq_nonneg (P x)) - calc - (P x * f x)^2 = P x^2 * f x^2 := mul_pow _ _ _ - _ ≤ _ := hi - _ = P x * g x * (P x * h x) := by ring - -lemma sq_Ex_mul (P : Law α) (f g : α → ℝ) : - (P.Ex (fun x => f x * g x))^2 ≤ P.Ex (fun x => f x^2) * P.Ex (fun x => g x^2) := - sq_Ex_le _ _ _ _ (fun _ => sq_nonneg _) (fun _ => sq_nonneg _) (fun _ => (mul_pow _ _ _).le) - -lemma sq_Ex (P : Law α) (f : α → ℝ) : - (P.Ex f)^2 ≤ P.Ex (fun x => f x^2) := by - simpa only [mul_one,one_pow,Ex_const] using sq_Ex_mul P f (fun _ => 1) - -lemma quadratic_minkowski (P : Law α) (f g : α → ℝ) : - √(P.Ex fun x => (f x+g x)^2) ≤ √(P.Ex fun x => (f x)^2) + √(P.Ex fun x => (g x)^2) := by - let A := P.Ex fun x => (f x)^2; let B := P.Ex fun x => (g x)^2 - let E := P.Ex fun x => (f x+g x)^2 - have hA := sq_sqrt (show 0 ≤ A from P.Ex_nonneg fun x => sq_nonneg _) - have hB := sq_sqrt (show 0 ≤ B from P.Ex_nonneg fun x => sq_nonneg _) - have hE := sq_sqrt (show 0 ≤ E from P.Ex_nonneg fun x => sq_nonneg _) - let L := P.Ex fun x => f x * g x - have hL : E = A+B+2*L := by - unfold E A B L - simp_rw [← Ex_lmul,← Ex_add]; congr 1; funext i; ring - have hU : L^2 ≤ (√A*√B)^2 := by rw [mul_pow,hA,hB]; exact sq_Ex_mul _ _ _ - have hL' : L ≤ √A*√B := by nlinarith [show 0 ≤ √A*√B by positivity] - change √E ≤ √A+√B - nlinarith [sqrt_nonneg A,sqrt_nonneg B] - -lemma sq_Cx_norm (P : Law α) (f : α → ℂ) : - ‖P.Cx f‖^2 ≤ P.Ex (fun x => ‖f x‖^2) := - (pow_le_pow_left₀ (norm_nonneg _) (P.Cx_norm _) _).trans (sq_Ex _ _) -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Complex + +open Set Function Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Law +open Real +variable {α : Type} [Fintype α] + +lemma sq_Ex_le (P : Law α) (f g h : α → ℝ) (hg : ∀ x, 0 ≤ g x) (hh : ∀ x, 0 ≤ h x) + (H : ∀ x, f x^2 ≤ g x*h x) : P.Ex f^2 ≤ P.Ex g * P.Ex h := by + refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ + (f:=fun x => P x * g x) (g:=fun x => P x * h x) + (fun x _ => mul_nonneg (P.nonneg x) (hg x)) + (fun x _ => mul_nonneg (P.nonneg x) (hh x)) ?_ + intro x _ + have hi := mul_le_mul_of_nonneg_left (H x) (sq_nonneg (P x)) + calc + (P x * f x)^2 = P x^2 * f x^2 := mul_pow _ _ _ + _ ≤ _ := hi + _ = P x * g x * (P x * h x) := by ring + +lemma sq_Ex_mul (P : Law α) (f g : α → ℝ) : + (P.Ex (fun x => f x * g x))^2 ≤ P.Ex (fun x => f x^2) * P.Ex (fun x => g x^2) := + sq_Ex_le _ _ _ _ (fun _ => sq_nonneg _) (fun _ => sq_nonneg _) (fun _ => (mul_pow _ _ _).le) + +lemma sq_Ex (P : Law α) (f : α → ℝ) : + (P.Ex f)^2 ≤ P.Ex (fun x => f x^2) := by + simpa only [mul_one,one_pow,Ex_const] using sq_Ex_mul P f (fun _ => 1) + +lemma quadratic_minkowski (P : Law α) (f g : α → ℝ) : + √(P.Ex fun x => (f x+g x)^2) ≤ √(P.Ex fun x => (f x)^2) + √(P.Ex fun x => (g x)^2) := by + let A := P.Ex fun x => (f x)^2; let B := P.Ex fun x => (g x)^2 + let E := P.Ex fun x => (f x+g x)^2 + have hA := sq_sqrt (show 0 ≤ A from P.Ex_nonneg fun x => sq_nonneg _) + have hB := sq_sqrt (show 0 ≤ B from P.Ex_nonneg fun x => sq_nonneg _) + have hE := sq_sqrt (show 0 ≤ E from P.Ex_nonneg fun x => sq_nonneg _) + let L := P.Ex fun x => f x * g x + have hL : E = A+B+2*L := by + unfold E A B L + simp_rw [← Ex_lmul,← Ex_add]; congr 1; funext i; ring + have hU : L^2 ≤ (√A*√B)^2 := by rw [mul_pow,hA,hB]; exact sq_Ex_mul _ _ _ + have hL' : L ≤ √A*√B := by nlinarith [show 0 ≤ √A*√B by positivity] + change √E ≤ √A+√B + nlinarith [sqrt_nonneg A,sqrt_nonneg B] + +lemma sq_Cx_norm (P : Law α) (f : α → ℂ) : + ‖P.Cx f‖^2 ≤ P.Ex (fun x => ‖f x‖^2) := + (pow_le_pow_left₀ (norm_nonneg _) (P.Cx_norm _) _).trans (sq_Ex _ _) +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Signal.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Signal.lean index d863b51b0154256d0a9f796e1e23a7fa060b3c4c..7ed6ed003e294f54881e856be61baf93cb56463a 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Signal.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Signal.lean @@ -1,146 +1,146 @@ -import OAI.Analysis.FourierLLogL.Finite.Comparison - -/-! # Entropy cost of changes of signal -/ -open Function Set Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Law -open Real -variable {α β γ η : Type} [Fintype α] [Fintype β] [Fintype γ] [Fintype η] -open scoped Classical - -/-- expected entropy remaining conditional on a signal channel -/ -def privateH (P : Law α) (k : α → Law β) := - (P.withNext k).Hcond Prod.fst Prod.snd - -lemma privateH_le (P : Law α) (k : α → Law β) : P.privateH k ≤ P.entropy := by - have H := (P.withNext k).Hcond_le_H Prod.fst Prod.snd - exact (show _ ≤ _ from H).trans (le_of_eq (by - unfold Law.H; rw [send_fst_withNext])) - -lemma signal_cost (P : Law α) (k : α → Law β) : - (P.withNext k).Hcond Prod.snd Prod.fst = P.Ex fun x => (k x).entropy := by - unfold Hcond; rw [H_pair_rev]; simp_rw [Prod.mk.eta,H_self,entropy_withNext,H,send_fst_withNext]; ring - -lemma privateH_const (P : Law α) (Q : Law β) : - P.privateH (fun _ => Q) = P.entropy := by - unfold privateH Hcond - simp only [Prod.mk.eta,H_self] - rw [entropy_withNext,Ex_const] - unfold H - rw [show P.withNext (fun _ => Q) = P.tensorP Q from rfl,send_snd_tensorP]; ring - -lemma Hcond_compare (P : Law α) (Y : α → β) (X : α → γ) (q : γ → Law β) - (hq : ∀ i, 0 < P i → 0 < q (X i) (Y i)) : - P.Hcond Y X ≤ P.Ex fun i => -log (q (X i) (Y i)) := by - rw [Hcond_after] - rw [← after_Ex_sum P X (fun x i => -log (q x (Y i)))] - refine Ex_mono_mem _ fun x hx => ?_ - rw [← Ex_send (P.after X x) Y (fun i => -log (q x i))] - apply entropy_le_cross_aux _ (q x) (q x).nonneg (q x).total.le - intro i hi - obtain ⟨i,rfl,hii⟩ := (send_pos_iff _ _ _).mp hi - obtain ⟨rfl,H⟩ := (after_mem hx).mp hii - exact hq i H - -lemma Hcond_pair_comm (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : - P.Hcond (fun x => (X x,Y x)) Z = P.Hcond (fun x => (Y x,X x)) Z := by - unfold Hcond; congr 1; apply P.H_partition; simp [and_comm] - -def triple (P : Law α) (k : α → Law β) (v : α × β → Law γ) := - (P.withNext k).withNext v - -/-- Increment cost when jointly revealing two signals; the second may depend on x and first. -/ -theorem refine_signal (P : Law α) (k : α → Law β) (v : α × β → Law γ) - (q : β → Law γ) (hq : ∀ x i, 0 < q x i) : - let R := triple P k v - P.privateH k - R.Hcond (fun i => i.1.1) (fun i => (i.2,i.1.2)) ≤ - (P.withNext k).Ex (fun x => (v x).kl (q x.2)) := by - intro R - let X := fun i : (α × β) × γ => i.1.1 - let B := fun i : (α × β) × γ => i.1.2 - let C : (α × β) × γ → γ := Prod.snd - have he : P.privateH k = R.Hcond X B := by - have HH := R.Hcond_send Prod.fst Prod.fst Prod.snd - rw [show R.send Prod.fst=P.withNext k from send_fst_withNext _ _] at HH - exact HH - have hi := Hcond_compare R C B q (fun x _ => hq _ _) - have hx : (P.withNext k).Ex (fun x => (v x).crossE (q x.2)) = R.Ex (fun i => -log (q (B i) (C i))) := by - unfold crossE R triple; rw [Ex_withNext _ v] - have hv1 : R.Hcond C (fun x => (X x,B x)) = R.Hcond Prod.snd Prod.fst := by - refine Hcond_right_partition _ _ ?_ - intro a b; unfold X B; simp - have hv2 : R.Hcond C (fun x => (X x,B x)) = (P.withNext k).Ex fun x => (v x).entropy := - hv1.trans (signal_cost _ _) - change P.privateH k - R.Hcond X (fun x => (C x,B x)) ≤ _ - simp_rw [he,kl,Ex_sub,hx,← hv2] - have h1 := Hcond_chain R X C B - have h2 := Hcond_chain R C X B - rw [Hcond_pair_comm] at h2 - linarith - - -/-- When retaining the new signal only, the cost increment is smaller. -/ -theorem replace_signal (P : Law α) (k : α → Law β) (v : α → Law γ) - (q : β → Law γ) (hq : ∀ x i, 0 < q x i) : - P.privateH k - P.privateH v ≤ - (P.withNext k).Ex (fun x => (v x.1).kl (q x.2)) := by - have H := refine_signal P k (fun x => v x.1) q hq - let R := triple P k (fun x => v x.1) - have hi : R.send (fun x => (x.1.1,x.2))=P.withNext v := by - apply ext_of_Ex; intro f - unfold R triple; rw [Ex_send]; simp only [Ex_withNext]; simp only [Ex_const] - have HH : P.privateH v=R.Hcond (fun x => x.1.1) (fun x => x.2) := by - unfold privateH - rw [← hi,Hcond_send] - have hu : R.Hcond (fun x => x.1.1) (fun x => (x.2,x.1.2)) ≤ P.privateH v := by - rw [HH] - apply Hcond_antitone - intro a b h; exact (Prod.mk.inj h).1 - calc - _ ≤ P.privateH k - R.Hcond (fun x => x.1.1) (fun x => (x.2,x.1.2)) := - sub_le_sub_left hu _ - _ ≤ _ := H - -/-- Restriction on a deterministic bit or observation w/o revealing labels -/ -lemma after_channel (P : Law α) (f : α → β) (k : α → Law γ) - {b : β} (h : 0 < P.send f b) : - (P.withNext k).after (fun x => f x.1) b = (P.after f b).withNext k := by - have H : (P.withNext k).send (fun x => f x.1) = P.send f := by - exact Eq.symm (by - conv_lhs => rw [← send_fst_withNext P k] - rw [send_send]) - apply Law.ext; rintro ⟨x,i⟩ - apply mul_left_cancel₀ h.ne' - nth_rw 1 [← H] - rw [send_mul_after,withNext_apply,withNext_apply,← mul_assoc,send_mul_after] - simp only [ite_mul,zero_mul] - -lemma det_channel (P : Law α) (f : α → β) (k : α → Law γ) : - let R := P.withNext k - let W : α × γ → β := fun i => f i.1 - (P.send f).Ex (fun i => (P.after f i).privateH k) + R.Hcond W Prod.snd = - P.privateH k := by - intro R W - have HH : (P.withNext k).send (fun x => f x.1) = P.send f := by - exact Eq.symm (by conv_lhs => rw [← send_fst_withNext P k,send_send]) - have he : (P.send f).Ex (fun i => (P.after f i).privateH k) = - R.Hcond Prod.fst (fun i => (W i,i.2)) := by - rw [Hcond_right_partition (Z:=fun i => (i.2,W i)) R Prod.fst (by simp [and_comm]),Hcond_after_cond] - rw [show R.send W=_ from HH] - apply Ex_congr_mem - intro i hi - change _ = ((P.withNext k).after _ _).Hcond _ _ - rw [after_channel P f k hi] - rfl - rw [he,← Hcond_chain] - unfold privateH Hcond - congr 1 - apply H_partition - simp [W] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Comparison + +/-! # Entropy cost of changes of signal -/ +open Function Set Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Law +open Real +variable {α β γ η : Type} [Fintype α] [Fintype β] [Fintype γ] [Fintype η] +open scoped Classical + +/-- expected entropy remaining conditional on a signal channel -/ +def privateH (P : Law α) (k : α → Law β) := + (P.withNext k).Hcond Prod.fst Prod.snd + +lemma privateH_le (P : Law α) (k : α → Law β) : P.privateH k ≤ P.entropy := by + have H := (P.withNext k).Hcond_le_H Prod.fst Prod.snd + exact (show _ ≤ _ from H).trans (le_of_eq (by + unfold Law.H; rw [send_fst_withNext])) + +lemma signal_cost (P : Law α) (k : α → Law β) : + (P.withNext k).Hcond Prod.snd Prod.fst = P.Ex fun x => (k x).entropy := by + unfold Hcond; rw [H_pair_rev]; simp_rw [Prod.mk.eta,H_self,entropy_withNext,H,send_fst_withNext]; ring + +lemma privateH_const (P : Law α) (Q : Law β) : + P.privateH (fun _ => Q) = P.entropy := by + unfold privateH Hcond + simp only [Prod.mk.eta,H_self] + rw [entropy_withNext,Ex_const] + unfold H + rw [show P.withNext (fun _ => Q) = P.tensorP Q from rfl,send_snd_tensorP]; ring + +lemma Hcond_compare (P : Law α) (Y : α → β) (X : α → γ) (q : γ → Law β) + (hq : ∀ i, 0 < P i → 0 < q (X i) (Y i)) : + P.Hcond Y X ≤ P.Ex fun i => -log (q (X i) (Y i)) := by + rw [Hcond_after] + rw [← after_Ex_sum P X (fun x i => -log (q x (Y i)))] + refine Ex_mono_mem _ fun x hx => ?_ + rw [← Ex_send (P.after X x) Y (fun i => -log (q x i))] + apply entropy_le_cross_aux _ (q x) (q x).nonneg (q x).total.le + intro i hi + obtain ⟨i,rfl,hii⟩ := (send_pos_iff _ _ _).mp hi + obtain ⟨rfl,H⟩ := (after_mem hx).mp hii + exact hq i H + +lemma Hcond_pair_comm (P : Law α) (X : α → β) (Y : α → γ) (Z : α → η) : + P.Hcond (fun x => (X x,Y x)) Z = P.Hcond (fun x => (Y x,X x)) Z := by + unfold Hcond; congr 1; apply P.H_partition; simp [and_comm] + +def triple (P : Law α) (k : α → Law β) (v : α × β → Law γ) := + (P.withNext k).withNext v + +/-- Increment cost when jointly revealing two signals; the second may depend on x and first. -/ +theorem refine_signal (P : Law α) (k : α → Law β) (v : α × β → Law γ) + (q : β → Law γ) (hq : ∀ x i, 0 < q x i) : + let R := triple P k v + P.privateH k - R.Hcond (fun i => i.1.1) (fun i => (i.2,i.1.2)) ≤ + (P.withNext k).Ex (fun x => (v x).kl (q x.2)) := by + intro R + let X := fun i : (α × β) × γ => i.1.1 + let B := fun i : (α × β) × γ => i.1.2 + let C : (α × β) × γ → γ := Prod.snd + have he : P.privateH k = R.Hcond X B := by + have HH := R.Hcond_send Prod.fst Prod.fst Prod.snd + rw [show R.send Prod.fst=P.withNext k from send_fst_withNext _ _] at HH + exact HH + have hi := Hcond_compare R C B q (fun x _ => hq _ _) + have hx : (P.withNext k).Ex (fun x => (v x).crossE (q x.2)) = R.Ex (fun i => -log (q (B i) (C i))) := by + unfold crossE R triple; rw [Ex_withNext _ v] + have hv1 : R.Hcond C (fun x => (X x,B x)) = R.Hcond Prod.snd Prod.fst := by + refine Hcond_right_partition _ _ ?_ + intro a b; unfold X B; simp + have hv2 : R.Hcond C (fun x => (X x,B x)) = (P.withNext k).Ex fun x => (v x).entropy := + hv1.trans (signal_cost _ _) + change P.privateH k - R.Hcond X (fun x => (C x,B x)) ≤ _ + simp_rw [he,kl,Ex_sub,hx,← hv2] + have h1 := Hcond_chain R X C B + have h2 := Hcond_chain R C X B + rw [Hcond_pair_comm] at h2 + linarith + + +/-- When retaining the new signal only, the cost increment is smaller. -/ +theorem replace_signal (P : Law α) (k : α → Law β) (v : α → Law γ) + (q : β → Law γ) (hq : ∀ x i, 0 < q x i) : + P.privateH k - P.privateH v ≤ + (P.withNext k).Ex (fun x => (v x.1).kl (q x.2)) := by + have H := refine_signal P k (fun x => v x.1) q hq + let R := triple P k (fun x => v x.1) + have hi : R.send (fun x => (x.1.1,x.2))=P.withNext v := by + apply ext_of_Ex; intro f + unfold R triple; rw [Ex_send]; simp only [Ex_withNext]; simp only [Ex_const] + have HH : P.privateH v=R.Hcond (fun x => x.1.1) (fun x => x.2) := by + unfold privateH + rw [← hi,Hcond_send] + have hu : R.Hcond (fun x => x.1.1) (fun x => (x.2,x.1.2)) ≤ P.privateH v := by + rw [HH] + apply Hcond_antitone + intro a b h; exact (Prod.mk.inj h).1 + calc + _ ≤ P.privateH k - R.Hcond (fun x => x.1.1) (fun x => (x.2,x.1.2)) := + sub_le_sub_left hu _ + _ ≤ _ := H + +/-- Restriction on a deterministic bit or observation w/o revealing labels -/ +lemma after_channel (P : Law α) (f : α → β) (k : α → Law γ) + {b : β} (h : 0 < P.send f b) : + (P.withNext k).after (fun x => f x.1) b = (P.after f b).withNext k := by + have H : (P.withNext k).send (fun x => f x.1) = P.send f := by + exact Eq.symm (by + conv_lhs => rw [← send_fst_withNext P k] + rw [send_send]) + apply Law.ext; rintro ⟨x,i⟩ + apply mul_left_cancel₀ h.ne' + nth_rw 1 [← H] + rw [send_mul_after,withNext_apply,withNext_apply,← mul_assoc,send_mul_after] + simp only [ite_mul,zero_mul] + +lemma det_channel (P : Law α) (f : α → β) (k : α → Law γ) : + let R := P.withNext k + let W : α × γ → β := fun i => f i.1 + (P.send f).Ex (fun i => (P.after f i).privateH k) + R.Hcond W Prod.snd = + P.privateH k := by + intro R W + have HH : (P.withNext k).send (fun x => f x.1) = P.send f := by + exact Eq.symm (by conv_lhs => rw [← send_fst_withNext P k,send_send]) + have he : (P.send f).Ex (fun i => (P.after f i).privateH k) = + R.Hcond Prod.fst (fun i => (W i,i.2)) := by + rw [Hcond_right_partition (Z:=fun i => (i.2,W i)) R Prod.fst (by simp [and_comm]),Hcond_after_cond] + rw [show R.send W=_ from HH] + apply Ex_congr_mem + intro i hi + change _ = ((P.withNext k).after _ _).Hcond _ _ + rw [after_channel P f k hi] + rfl + rw [he,← Hcond_chain] + unfold privateH Hcond + congr 1 + apply H_partition + simp [W] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Finite/Tail.lean b/lean/OAI/Analysis/FourierLLogL/Finite/Tail.lean index 52fc4759b1901f09b9d25ff83f2001a5fbad60bb..fc897cd718964d445ab68f988d5450e74f71076b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Finite/Tail.lean +++ b/lean/OAI/Analysis/FourierLLogL/Finite/Tail.lean @@ -1,81 +1,81 @@ -import OAI.Analysis.FourierLLogL.Finite.Quadratic - -/-! # More estimates on event probabilities -/ -open Function Set Filter -open scoped BigOperators -noncomputable section -namespace OAI - -namespace FourierLLogL.Law -open Real -variable {α β γ : Type} [Fintype α] [Fintype β] [Fintype γ] -open scoped Classical - -lemma prob_mono (P : Law α) (p q : α → Prop) (h : ∀ i, p i → q i) : P.prob p ≤ P.prob q := by - unfold prob; apply Ex_mono; intro i - split_ifs with H H' H' - · norm_num - · exact (H' (h _ H)).elim - · norm_num - · norm_num - -lemma prob_union (P : Law α) (p q : α → Prop) : P.prob (fun i => p i ∨ q i) ≤ P.prob p + P.prob q := by - unfold prob; rw [← Ex_add]; apply Ex_mono; intro i - by_cases H : p i <;> by_cases H' : q i <;> norm_num [H,H'] - -lemma prob_send (P : Law α) (f : α → β) (p : β → Prop) : - (P.send f).prob p = P.prob fun x => p (f x) := by - unfold prob; rw [Ex_send] - -lemma upperTail (P : Law α) (p : α → Prop) (f : α → ℝ) (h : ∀ i, 0 ≤ f i) - (r : ℝ) (he : ∀ i, p i → r ≤ f i) : - r*P.prob p ≤ P.Ex f := by - unfold prob; rw [← Ex_lmul]; apply Ex_mono; intro i - split_ifs with H - · rw [mul_one]; exact he i H - · rw [mul_zero]; exact h i - -lemma independent (P : Law α) (p : α → Prop) (T : ℕ) : - (pack (fun _ : Fin T => P)).prob (fun i => ∀ k, p (i k)) = - (P.prob p)^T := by - classical - have H (i : Fin T → α) : - (if ∀ k, p (i k) then 1 else (0:ℝ)) = - ∏ k, (if p (i k) then (1:ℝ) else 0) := by - split_ifs with H - · simp [H] - push Not at H - obtain ⟨x,hx⟩ := H - exact Eq.symm (Finset.prod_eq_zero (i:=x) (Finset.mem_univ x) (by simp [hx])) - - have he : (pack (fun _ : Fin T => P)).prob (fun i => ∀ k, p (i k)) = - (pack (fun _ : Fin T => P)).Ex (fun i => ∏ k, (if p (i k) then (1:ℝ) else 0)) := by - rw [prob_eq]; simp_rw [Ex, ← H] - simp - rw [he]; unfold prob - rw [pack_Ex_prod (fun _ : Fin T => P) (fun _ i => if p i then 1 else 0)] - simp - -lemma bernoulli_trial {x : ℝ} (h : 0 ≤ x) (hh : x ≤ 1) (m : ℕ) : - ↑m * x^m * (1-x) ≤ 1 := by - have H := geom_sum_mul_neg x m - have H1 : m*x^m ≤ ∑ i∈Finset.range m, x^i := by - calc - _ = ∑ i∈Finset.range m, x^m := by simp - _ ≤ _ := by - exact Finset.sum_le_sum fun i hi => pow_le_pow_of_le_one h hh (Finset.mem_range.mp hi).le - nlinarith [pow_nonneg h m, mul_le_mul_of_nonneg_right H1 (sub_nonneg.mpr hh)] - -lemma lowerTail (P : Law α) (p : α → Prop) (h Hp : ℝ) (f : α → ℝ) - (hh : ∀ i, p i → f i ≤ Hp+h) (hf : ∀ i, ¬p i → f i ≤ h) : - P.Ex f ≤ Hp * P.prob p + h := by - unfold prob - refine le_trans (P.Ex_mono (g:=fun i => Hp*(if p i then 1 else 0)+h) (fun i => ?_)) ?_ - · split_ifs with H - · rw [mul_one]; exact hh _ H - rw [mul_zero,zero_add]; exact hf _ H - rw [Ex_add,Ex_lmul,Ex_const] - -end FourierLLogL.Law - -end OAI +import OAI.Analysis.FourierLLogL.Finite.Quadratic + +/-! # More estimates on event probabilities -/ +open Function Set Filter +open scoped BigOperators +noncomputable section +namespace OAI + +namespace FourierLLogL.Law +open Real +variable {α β γ : Type} [Fintype α] [Fintype β] [Fintype γ] +open scoped Classical + +lemma prob_mono (P : Law α) (p q : α → Prop) (h : ∀ i, p i → q i) : P.prob p ≤ P.prob q := by + unfold prob; apply Ex_mono; intro i + split_ifs with H H' H' + · norm_num + · exact (H' (h _ H)).elim + · norm_num + · norm_num + +lemma prob_union (P : Law α) (p q : α → Prop) : P.prob (fun i => p i ∨ q i) ≤ P.prob p + P.prob q := by + unfold prob; rw [← Ex_add]; apply Ex_mono; intro i + by_cases H : p i <;> by_cases H' : q i <;> norm_num [H,H'] + +lemma prob_send (P : Law α) (f : α → β) (p : β → Prop) : + (P.send f).prob p = P.prob fun x => p (f x) := by + unfold prob; rw [Ex_send] + +lemma upperTail (P : Law α) (p : α → Prop) (f : α → ℝ) (h : ∀ i, 0 ≤ f i) + (r : ℝ) (he : ∀ i, p i → r ≤ f i) : + r*P.prob p ≤ P.Ex f := by + unfold prob; rw [← Ex_lmul]; apply Ex_mono; intro i + split_ifs with H + · rw [mul_one]; exact he i H + · rw [mul_zero]; exact h i + +lemma independent (P : Law α) (p : α → Prop) (T : ℕ) : + (pack (fun _ : Fin T => P)).prob (fun i => ∀ k, p (i k)) = + (P.prob p)^T := by + classical + have H (i : Fin T → α) : + (if ∀ k, p (i k) then 1 else (0:ℝ)) = + ∏ k, (if p (i k) then (1:ℝ) else 0) := by + split_ifs with H + · simp [H] + push Not at H + obtain ⟨x,hx⟩ := H + exact Eq.symm (Finset.prod_eq_zero (i:=x) (Finset.mem_univ x) (by simp [hx])) + + have he : (pack (fun _ : Fin T => P)).prob (fun i => ∀ k, p (i k)) = + (pack (fun _ : Fin T => P)).Ex (fun i => ∏ k, (if p (i k) then (1:ℝ) else 0)) := by + rw [prob_eq]; simp_rw [Ex, ← H] + simp + rw [he]; unfold prob + rw [pack_Ex_prod (fun _ : Fin T => P) (fun _ i => if p i then 1 else 0)] + simp + +lemma bernoulli_trial {x : ℝ} (h : 0 ≤ x) (hh : x ≤ 1) (m : ℕ) : + ↑m * x^m * (1-x) ≤ 1 := by + have H := geom_sum_mul_neg x m + have H1 : m*x^m ≤ ∑ i∈Finset.range m, x^i := by + calc + _ = ∑ i∈Finset.range m, x^m := by simp + _ ≤ _ := by + exact Finset.sum_le_sum fun i hi => pow_le_pow_of_le_one h hh (Finset.mem_range.mp hi).le + nlinarith [pow_nonneg h m, mul_le_mul_of_nonneg_right H1 (sub_nonneg.mpr hh)] + +lemma lowerTail (P : Law α) (p : α → Prop) (h Hp : ℝ) (f : α → ℝ) + (hh : ∀ i, p i → f i ≤ Hp+h) (hf : ∀ i, ¬p i → f i ≤ h) : + P.Ex f ≤ Hp * P.prob p + h := by + unfold prob + refine le_trans (P.Ex_mono (g:=fun i => Hp*(if p i then 1 else 0)+h) (fun i => ?_)) ?_ + · split_ifs with H + · rw [mul_one]; exact hh _ H + rw [mul_zero,zero_add]; exact hf _ H + rw [Ex_add,Ex_lmul,Ex_const] + +end FourierLLogL.Law + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Affine.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Affine.lean index 08e56c53c72f7930a741f32b91cb18b34d4e8c41..7989b4834dca546eb6390a7f52d082712b3b1838 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Affine.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Affine.lean @@ -1,342 +1,342 @@ -import OAI.Analysis.FourierLLogL.Fourier.Haar -/-! # Changing the size and origin of the top interval -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -lemma phase_add (u x y : ℝ) : - phase u (x + y) = phase u x * phase u y := by - unfold phase - rw [← Complex.exp_add] - congr 1 - push_cast - ring - -lemma phase_freq_add (u v x : ℝ) : - phase (u+v) x = phase u x * phase v x := by - unfold phase - rw [← Complex.exp_add] - congr 1 - push_cast - ring - -lemma phase_mul_arg (u x y : ℝ) : phase u (x*y) = phase (u*x) y := by - unfold phase; ring_nf - -lemma norm_phase_sub (u x y : ℝ) : - ‖phase u x - phase u y‖ ≤ |u| * (2*π* |x-y|) := by - have heq : phase u x - phase u y = (phase u (x-y)-1) * phase u y := by - rw [sub_mul, ← phase_add, sub_add_cancel, one_mul] - rw [heq, norm_mul, phase_unimod, mul_one] - unfold phase - have HH : ‖Complex.exp (Complex.I * ↑(u * (2*π*(x-y)))) - 1‖ ≤ ‖u*(2*π*(x-y))‖ := - Real.norm_exp_I_mul_ofReal_sub_one_le - refine HH.trans ?_ - simp [Real.norm_eq_abs, abs_of_nonneg Real.pi_pos.le] - -lemma norm_mod {ρ : ℝ → ℝ} (h0 : ∀ x, 0 ≤ ρ x) (u x : ℝ) : - ‖modρ ρ u x‖ = ρ x := by - simp only [modρ, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 _), - phase_unimod, mul_one] - -lemma coeff_near {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) - {u v a r x : ℝ} (hx : x ∈ box a r) (hu : |u-v| ≤ 1) : - ‖coeffH (modρ ρ u) a r - phase (u-v) x * coeffH (modρ ρ v) a r‖ ≤ - density ρ a r * (2 * π * r) := by - have h1 := hx.1 - have h2 := hx.2 - have hr : 0 < r := by linarith - let w (t : ℝ) := a+r*t - have hw : Measurable w := measurable_const.add (measurable_const.mul measurable_id) - let F (u t : ℝ) := haarH t • modρ ρ u (w t) - have hF (s : ℝ) : Integrable (F s) μ₁ := by - apply Integrable.of_bound (Measurable.aestronglyMeasurable - (measurableH.smul ((measurable_mod hρ s).comp hw))) B - refine ae_of_all _ fun z => ?_ - change ‖haarH z • modρ ρ s (w z)‖ ≤ _ - rw [norm_smul, Real.norm_eq_abs, norm_mod h0] - exact (mul_le_of_le_one_left (h0 _) (H_bound z)).trans (hb _) - let c := phase (u-v) x - have ht (t) (ht : t ∈ unitInt) : - ‖F u t - c * F v t‖ ≤ ρ (w t) * (2 * π * r) := by - have heq : F u t = phase (u-v) (w t) * F v t := by - dsimp only [F, modρ] - nth_rw 1 [← sub_add_cancel u v] - - rw [phase_freq_add] - simp only [Complex.real_smul] - ring - rw [heq, ← sub_mul, norm_mul] - have hk : |w t - x| ≤ r := by - rcases ht with ⟨t1,t2⟩ - dsimp [w] - rw [abs_le] - constructor <;> nlinarith - calc - ‖phase (u-v) (w t) - c‖ * ‖F v t‖ ≤ (2*π*r) * ρ (w t) := - mul_le_mul ((norm_phase_sub _ _ _).trans (by - calc - _ ≤ 1 * (2*π*r) := by gcongr - _ = _ := one_mul _)) (by - rw [show F v t = haarH t • modρ ρ v (w t) from rfl, norm_smul, - Real.norm_eq_abs, norm_mod h0] - exact mul_le_of_le_one_left (h0 _) (H_bound _)) - (norm_nonneg _) (by positivity) - _ = _ := mul_comm _ _ - have hwi : Integrable (fun t => ρ (w t)) μ₁ := - Integrable.of_bound ((hρ.comp hw).aestronglyMeasurable) B - (ae_of_all _ fun t => by - change ‖ρ (w t)‖ ≤ B - simpa only [Real.norm_eq_abs, abs_of_nonneg (h0 _)] using hb _) - change ‖(∫ t, F u t ∂μ₁) - c*(∫ t, F v t ∂μ₁)‖ ≤ (∫ t, ρ (w t) ∂μ₁)*_ - have hhcv : Integrable (fun t => c * F v t) μ₁ := (hF v).const_mul _ - rw [← integral_const_mul, ← integral_mul_const, ← integral_sub (hF u) hhcv] - exact (norm_integral_le_integral_norm _).trans - (integral_mono_ae ((hF u).sub hhcv).norm (hwi.mul_const _) - ((ae_restrict_mem measurable_unitInt).mono ht)) - -lemma cell_unique (a s : ℝ) (hs : 0 < s) (n : ℕ) {k l : ℕ} {x : ℝ} - (hx : x ∈ box (lft a s n k) (s*side n)) - (hy : x ∈ box (lft a s n l) (s*side n)) : k = l := by - have hr : 0 < s*side n := mul_pos hs (side_pos _) - have key : ∀ k l : ℕ, k < l → - lft a s n k + s*side n ≤ lft a s n l := by - intro k l h - have H : ((k:ℝ)+1) ≤ (l:ℝ) := by exact_mod_cast h - unfold lft - exact le_trans (by rw [add_mul]; simp [add_assoc] : - a + (k:ℝ)*(s*side n) + s * side n ≤ a + ((k:ℝ)+1) * (s*side n)) - (add_le_add_right (mul_le_mul_of_nonneg_right H hr.le) _) - by_contra h - rcases lt_or_gt_of_ne h with h|h - · exact (not_lt_of_ge ((key k l h).trans hy.1)) hx.2 - · exact (not_lt_of_ge ((key l k h).trans hx.1)) hy.2 - -lemma shift_near_unit {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) - {u v x D : ℝ} (J : ℕ) (hx : usable ρ 0 1 J D x) (hD : 0 ≤ D) - (hu : |u-v| ≤ 1) : - ‖shift (modρ ρ u) 0 1 J x‖ ≤ ‖shift (modρ ρ v) 0 1 J x‖ + - D * (2 * π) * 2 := by - let c := phase (u-v) x - have hl (n : ℕ) (hn : n < J) : ‖haarLayer (modρ ρ u) 0 1 n x - - c * haarLayer (modρ ρ v) 0 1 n x‖ ≤ D * (2 * π) * side n := by - unfold haarLayer - rw [Finset.mul_sum, ← Finset.sum_sub_distrib] - let inP (k : ℕ) := x ∈ box (lft 0 1 n k) (1*side n) - have hp : 0 < (1:ℝ)*side n := mul_pos zero_lt_one (side_pos n) - by_cases h : ∃ k ∈ Finset.range (2^n), inP k - · obtain ⟨k,hk,hxk⟩ := h - rw [Finset.sum_eq_single k] - · let a := lft 0 1 n k - let r := 1 * side n - change ‖haarG _ • coeffH _ a r - c * (haarG _ • coeffH _ a r)‖ ≤ _ - simp only [Complex.real_smul] - rw [mul_left_comm c, ← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs] - refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans - ((coeff_near hρ h0 B hb hxk hu).trans ?_) - have Hk := hx n hn k (Finset.mem_range.mp hk) hxk - refine (mul_le_mul_of_nonneg_right Hk (show 0 ≤ 2*π*(1*side n) by positivity)).trans ?_ - simp [mul_assoc] - · intro l hl hlk - have H : ¬ inP l := fun hh => hlk (cell_unique 0 1 zero_lt_one n hh hxk) - rw [jump_outside hp H _, jump_outside hp H _] - simp - · intro h; exact (h hk).elim - · rw [Finset.sum_eq_zero] - · rw [norm_zero]; exact mul_nonneg (by positivity) (side_pos _).le - · intro k hk - have H : ¬ inP k := fun hh => h ⟨k,hk,hh⟩ - rw [jump_outside hp H _, jump_outside hp H _] - simp - have ht : ‖shift (modρ ρ u) 0 1 J x - c * shift (modρ ρ v) 0 1 J x‖ ≤ D*(2*π)*2 := by - unfold shift - rw [Finset.mul_sum, ← Finset.sum_sub_distrib] - refine (norm_sum_le _ _).trans ((Finset.sum_le_sum (fun n hn => hl n (Finset.mem_range.mp hn))).trans ?_) - rw [← Finset.mul_sum] - exact mul_le_mul_of_nonneg_left (sum_geometric_two_le _) (by positivity) - have hh := norm_sub_norm_le (shift (modρ ρ u) 0 1 J x) (c * shift (modρ ρ v) 0 1 J x) - rw [norm_mul, phase_unimod, one_mul] at hh - linarith - -lemma shift_pull (F : ℝ → ℂ) (a s : ℝ) (hs : 0 < s) (J : ℕ) (x : ℝ) : - shift F a s J (a+s*x) = shift (fun y => F (a+s*y)) 0 1 J x := by - unfold shift - refine Finset.sum_congr rfl fun n _ => Finset.sum_congr rfl fun k _ => ?_ - let l := lft 0 1 n k - have h1 : lft a s n k = a+s*l := by unfold l lft; ring - rw [h1] - unfold jump coeffH - congr 2 - · dsimp only [l] - field_simp - all_goals ring - all_goals - simp_rw [show ∀ v : ℝ, a+s*l+(s*side n)*v = a+s*(l+(1*side n)*v) by intro v; ring] - rfl - -lemma shift_scale (F : ℝ → ℂ) (c : ℂ) (a s : ℝ) (J : ℕ) (x : ℝ) : - shift (fun y => c * F y) a s J x = c * shift F a s J x := by - unfold shift haarLayer - simp_rw [Finset.mul_sum] - refine Finset.sum_congr rfl fun n _ => Finset.sum_congr rfl fun k _ => ?_ - dsimp only [jump, coeffH] - simp only [Complex.real_smul] - simp_rw [mul_left_comm (↑(haarH _)) c, integral_const_mul] - ring - -lemma map_affine (a : ℝ) {s : ℝ} (hs : 0 < s) : - Measure.map (fun x : ℝ => a+s*x) volume = ENNReal.ofReal (s⁻¹) • volume := by - rw [show (fun x : ℝ => a+s*x) = ((a+·) ∘ fun x : ℝ => s*x) from rfl] - rw [← Measure.map_map (by fun_prop) (by fun_prop), Real.map_volume_mul_left hs.ne', - Measure.map_smul] - all_goals first - | simp only [abs_of_pos (inv_pos.mpr hs), map_add_left_eq_self] - | exact (measurable_const.add measurable_id).aemeasurable - -lemma restrict_affine (a : ℝ) {s : ℝ} (hs : 0 < s) (G : Set ℝ) (hG : MeasurableSet G) - (f : ℝ → ℝ≥0∞) (hf : Measurable f) : - (∫⁻ x in G, f x) = - ENNReal.ofReal s * ∫⁻ x in (fun y : ℝ => a+s*y) ⁻¹' G, f (a+s*x) := by - let φ (x : ℝ) := a+s*x - have hm : Measurable φ := by unfold φ; fun_prop - have H : Measure.map φ (volume.restrict (φ ⁻¹' G)) = - ENNReal.ofReal (s⁻¹) • volume.restrict G := by - rw [← Measure.restrict_map hm hG, map_affine a hs, Measure.restrict_smul] - change _ = ENNReal.ofReal s * ∫⁻ x in φ ⁻¹' G, f (φ x) - rw [← lintegral_map hf hm, H, lintegral_smul_measure, smul_eq_mul, ← mul_assoc, - ← ENNReal.ofReal_mul hs.le, mul_inv_cancel₀ hs.ne', ENNReal.ofReal_one, one_mul] - -def shiftD (ρ : ℝ → ℝ) (D : ℝ) (G : Set ℝ) : Prop := - ∀ x ∈ G, ∀ (a s : ℝ), 0 < s → s ≤ 1 → x ∈ box a s → - (∫ y in box a s, ρ y) ≤ D*s - -def AffineTreeBound : Prop := - ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (ρ : ℝ → ℝ), - Measurable ρ → Periodic ρ 1 → (∀ x, 0 ≤ ρ x) → (∫ x, ρ x ∂μ₁) ≤ 1 → - (∀ x, ρ x ≤ Real.exp ((2 : ℝ)^j)) → - ∀ (J : ℕ) (G : Set ℝ), MeasurableSet G → shiftD ρ (((j:ℝ)+1)^3) G → - ∀ (a s : ℝ), 1/2 ≤ s → s ≤ 1 → - (∫⁻ x in G, maxShift ρ a s J x) ≤ ENNReal.ofReal (C * (2:ℝ)^j) - -lemma cube_eventually : - ∀ᶠ j : ℕ in atTop, ((j:ℝ)+1)^3 ≤ (8:ℝ) * 2^j := by - filter_upwards [(tendsto_pow_const_div_const_pow_of_one_lt 3 - (show (1:ℝ)<2 by norm_num)).eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num)), - eventually_ge_atTop 1] with j hj h - change (j:ℝ)^3/(2:ℝ)^j < 1 at hj - have hh := (div_lt_one (by positivity)).mp hj - have H : (j:ℝ) + 1 ≤ 2 * (j:ℝ) := by exact_mod_cast (show j+1 ≤ 2*j by omega) - calc - _ ≤ (2*(j:ℝ))^3 := by gcongr - _ ≤ 8*2^j := by rw [mul_pow]; linarith - -theorem UnitTreeBound.affine (hunit : UnitTreeBound) : AffineTreeBound := by - obtain ⟨C, hC, h⟩ := hunit - refine ⟨C+8*(2*π)*2, by positivity, ?_⟩ - filter_upwards [h, cube_eventually] with j hj hcj - intro ρ hρ hp h0 hm hB J G hG hGood a s hs hs' - let φ (x : ℝ) := a+s*x - let F (x : ℝ) := ρ (φ x) - have hs0 : 0 < s := by linarith - have hmφ : Measurable φ := by unfold φ; fun_prop - have hF : Measurable F := hρ.comp hmφ - let B := Real.exp ((2:ℝ)^j) - let D := ((j:ℝ)+1)^3 - have hD : 0 ≤ D := by unfold D; positivity - have hF0 (x) : 0 ≤ F x := h0 _ - have hFB (x) : F x ≤ B := hB _ - let Gt := G ∩ box a s - have hbm : MeasurableSet (box a s) := measurableSet_Ico - let H := φ ⁻¹' Gt - have hH : MeasurableSet H := hmφ (hG.inter hbm) - have hsub : H ⊆ unitInt := by - intro x hx - have ⟨h1,h2⟩ := (show φ x ∈ Gt from hx).2 - dsimp only [φ] at * - constructor <;> nlinarith - have hmF : (∫ x, F x ∂μ₁) ≤ 2 := by - change (∫ x, ρ (a+s*x) ∂μ₁) ≤ 2 - rw [mean_translate_scale ρ a hs0, smul_eq_mul, ← div_eq_inv_mul] - exact (div_le_iff₀ hs0).mpr ((density_short ρ hρ hp h0 B hB hm a s hs').trans (by linarith)) - have hu : ∀ x ∈ H, usable F 0 1 J D x := by - intro x hx n _ k _ hk - let l := lft 0 1 n k - let r := (1:ℝ)*side n - have hr : 0 < r := mul_pos zero_lt_one (side_pos _) - have hrs := mul_pos hs0 hr - change density F l r ≤ _ - have hgd := hGood _ ((show φ x ∈ Gt from hx).1) (φ l) (s*r) hrs - (by simpa [r] using (show s * side n ≤ 1 from - (mul_le_mul_of_nonneg_right hs' (side_pos n).le).trans - (by simpa using side_le n))) - (show φ x ∈ box (φ l) (s*r) by - change l ≤ x ∧ x < l+r at hk - change a+s*l ≤ a+s*x ∧ a+s*x < a+s*l+s*r - constructor <;> nlinarith [hk.1,hk.2]) - change D*(s*r) ≥ _ at hgd - change (∫ y, ρ (φ (l+r*y)) ∂μ₁) ≤ _ - simp_rw [show ∀ y, φ (l+r*y) = φ l + (s*r)*y by intro y; unfold φ; ring, - mean_translate_scale ρ (φ l) hrs] - rw [smul_eq_mul, ← div_eq_inv_mul, div_le_iff₀ hrs] - exact hgd - have htrans (x : ℝ) (hx : x ∈ H) : - maxShift ρ a s J (φ x) ≤ maxShift F 0 1 J x + ENNReal.ofReal (D*(2*π)*2) := by - refine iSup_le fun n => ?_ - let u := (n:ℝ)*s - let k := ⌊u⌋ - have heq (y : ℝ) : modρ ρ (n:ℝ) (a+s*y) = phase n a * modρ F u y := by - unfold modρ - rw [phase_add, phase_mul_arg] - dsimp only [F,φ,u] - ring - change ENNReal.ofReal ‖shift _ _ _ _ (a+s*x)‖ ≤ _ - simp_rw [shift_pull _ a s hs0, heq, shift_scale, norm_mul, phase_unimod, one_mul] - have hnear : |u-(k:ℝ)| ≤ 1 := - abs_le.mpr (by have := Int.floor_le u; have := Int.lt_floor_add_one u; dsimp only [k]; constructor <;> linarith) - refine (ENNReal.ofReal_le_ofReal (shift_near_unit hF hF0 B hFB J (hu _ hx) hD hnear)).trans ?_ - rw [ENNReal.ofReal_add (by positivity) (by positivity)] - exact add_le_add_left (le_iSup (fun n : ℤ => - ENNReal.ofReal ‖shift (modρ F n) 0 1 J x‖) k) _ - let Z := maxShift ρ a s J - have hZ : Measurable Z := - measurable_maxShift_comp hρ measurable_const measurable_const measurable_id _ - have hz (x) (hx : x ∉ box a s) : Z x = 0 := by - unfold Z maxShift; simp only [shift_outside hs0 hx, norm_zero, ENNReal.ofReal_zero, iSup_const] - have hiG : (∫⁻ x in G, Z x) = ∫⁻ x in Gt, Z x := by - rw [← lintegral_indicator hG, ← lintegral_indicator (hG.inter hbm)] - exact lintegral_congr (fun x => by - by_cases hx : x ∈ box a s <;> by_cases hy : x ∈ G <;> simp_all only [ - mem_inter_iff, not_false_eq_true, and_self, and_false, false_and, - indicator_of_mem, indicator_of_notMem]) - change (∫⁻ x in G, Z x) ≤ _ - rw [hiG, restrict_affine a hs0 Gt (hG.inter hbm) Z hZ] - change ENNReal.ofReal s * ∫⁻ x in H, Z (φ x) ≤ _ - have hmH := hj F hF hF0 hmF hFB J H hH hsub hu - refine le_trans (b := ∫⁻ x in H, Z (φ x)) (mul_le_of_le_one_left zero_le - (by simpa using ENNReal.ofReal_le_ofReal hs')) ?_ - refine (lintegral_mono_ae ((ae_restrict_mem hH).mono htrans)).trans ?_ - rw [lintegral_add_right _ measurable_const] - have Hvol : volume H ≤ 1 := (measure_mono hsub).trans (by simp [unitInt]) - have Hbound : (∫⁻ _ : ℝ in H, ENNReal.ofReal (D*(2*π)*2)) ≤ ENNReal.ofReal (D*(2*π)*2) := by - simp only [setLIntegral_const] - exact mul_le_of_le_one_right zero_le Hvol - refine (add_le_add hmH Hbound).trans ?_ - rw [← ENNReal.ofReal_add (by positivity) (by positivity)] - apply ENNReal.ofReal_le_ofReal - rw [add_mul] - apply add_le_add_right - calc - _ ≤ ((8:ℝ)*2^j)*(2*π)*2 := by gcongr - _ = _ := by ring - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Haar +/-! # Changing the size and origin of the top interval -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +lemma phase_add (u x y : ℝ) : + phase u (x + y) = phase u x * phase u y := by + unfold phase + rw [← Complex.exp_add] + congr 1 + push_cast + ring + +lemma phase_freq_add (u v x : ℝ) : + phase (u+v) x = phase u x * phase v x := by + unfold phase + rw [← Complex.exp_add] + congr 1 + push_cast + ring + +lemma phase_mul_arg (u x y : ℝ) : phase u (x*y) = phase (u*x) y := by + unfold phase; ring_nf + +lemma norm_phase_sub (u x y : ℝ) : + ‖phase u x - phase u y‖ ≤ |u| * (2*π* |x-y|) := by + have heq : phase u x - phase u y = (phase u (x-y)-1) * phase u y := by + rw [sub_mul, ← phase_add, sub_add_cancel, one_mul] + rw [heq, norm_mul, phase_unimod, mul_one] + unfold phase + have HH : ‖Complex.exp (Complex.I * ↑(u * (2*π*(x-y)))) - 1‖ ≤ ‖u*(2*π*(x-y))‖ := + Real.norm_exp_I_mul_ofReal_sub_one_le + refine HH.trans ?_ + simp [Real.norm_eq_abs, abs_of_nonneg Real.pi_pos.le] + +lemma norm_mod {ρ : ℝ → ℝ} (h0 : ∀ x, 0 ≤ ρ x) (u x : ℝ) : + ‖modρ ρ u x‖ = ρ x := by + simp only [modρ, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (h0 _), + phase_unimod, mul_one] + +lemma coeff_near {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) + {u v a r x : ℝ} (hx : x ∈ box a r) (hu : |u-v| ≤ 1) : + ‖coeffH (modρ ρ u) a r - phase (u-v) x * coeffH (modρ ρ v) a r‖ ≤ + density ρ a r * (2 * π * r) := by + have h1 := hx.1 + have h2 := hx.2 + have hr : 0 < r := by linarith + let w (t : ℝ) := a+r*t + have hw : Measurable w := measurable_const.add (measurable_const.mul measurable_id) + let F (u t : ℝ) := haarH t • modρ ρ u (w t) + have hF (s : ℝ) : Integrable (F s) μ₁ := by + apply Integrable.of_bound (Measurable.aestronglyMeasurable + (measurableH.smul ((measurable_mod hρ s).comp hw))) B + refine ae_of_all _ fun z => ?_ + change ‖haarH z • modρ ρ s (w z)‖ ≤ _ + rw [norm_smul, Real.norm_eq_abs, norm_mod h0] + exact (mul_le_of_le_one_left (h0 _) (H_bound z)).trans (hb _) + let c := phase (u-v) x + have ht (t) (ht : t ∈ unitInt) : + ‖F u t - c * F v t‖ ≤ ρ (w t) * (2 * π * r) := by + have heq : F u t = phase (u-v) (w t) * F v t := by + dsimp only [F, modρ] + nth_rw 1 [← sub_add_cancel u v] + + rw [phase_freq_add] + simp only [Complex.real_smul] + ring + rw [heq, ← sub_mul, norm_mul] + have hk : |w t - x| ≤ r := by + rcases ht with ⟨t1,t2⟩ + dsimp [w] + rw [abs_le] + constructor <;> nlinarith + calc + ‖phase (u-v) (w t) - c‖ * ‖F v t‖ ≤ (2*π*r) * ρ (w t) := + mul_le_mul ((norm_phase_sub _ _ _).trans (by + calc + _ ≤ 1 * (2*π*r) := by gcongr + _ = _ := one_mul _)) (by + rw [show F v t = haarH t • modρ ρ v (w t) from rfl, norm_smul, + Real.norm_eq_abs, norm_mod h0] + exact mul_le_of_le_one_left (h0 _) (H_bound _)) + (norm_nonneg _) (by positivity) + _ = _ := mul_comm _ _ + have hwi : Integrable (fun t => ρ (w t)) μ₁ := + Integrable.of_bound ((hρ.comp hw).aestronglyMeasurable) B + (ae_of_all _ fun t => by + change ‖ρ (w t)‖ ≤ B + simpa only [Real.norm_eq_abs, abs_of_nonneg (h0 _)] using hb _) + change ‖(∫ t, F u t ∂μ₁) - c*(∫ t, F v t ∂μ₁)‖ ≤ (∫ t, ρ (w t) ∂μ₁)*_ + have hhcv : Integrable (fun t => c * F v t) μ₁ := (hF v).const_mul _ + rw [← integral_const_mul, ← integral_mul_const, ← integral_sub (hF u) hhcv] + exact (norm_integral_le_integral_norm _).trans + (integral_mono_ae ((hF u).sub hhcv).norm (hwi.mul_const _) + ((ae_restrict_mem measurable_unitInt).mono ht)) + +lemma cell_unique (a s : ℝ) (hs : 0 < s) (n : ℕ) {k l : ℕ} {x : ℝ} + (hx : x ∈ box (lft a s n k) (s*side n)) + (hy : x ∈ box (lft a s n l) (s*side n)) : k = l := by + have hr : 0 < s*side n := mul_pos hs (side_pos _) + have key : ∀ k l : ℕ, k < l → + lft a s n k + s*side n ≤ lft a s n l := by + intro k l h + have H : ((k:ℝ)+1) ≤ (l:ℝ) := by exact_mod_cast h + unfold lft + exact le_trans (by rw [add_mul]; simp [add_assoc] : + a + (k:ℝ)*(s*side n) + s * side n ≤ a + ((k:ℝ)+1) * (s*side n)) + (add_le_add_right (mul_le_mul_of_nonneg_right H hr.le) _) + by_contra h + rcases lt_or_gt_of_ne h with h|h + · exact (not_lt_of_ge ((key k l h).trans hy.1)) hx.2 + · exact (not_lt_of_ge ((key l k h).trans hx.1)) hy.2 + +lemma shift_near_unit {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) + {u v x D : ℝ} (J : ℕ) (hx : usable ρ 0 1 J D x) (hD : 0 ≤ D) + (hu : |u-v| ≤ 1) : + ‖shift (modρ ρ u) 0 1 J x‖ ≤ ‖shift (modρ ρ v) 0 1 J x‖ + + D * (2 * π) * 2 := by + let c := phase (u-v) x + have hl (n : ℕ) (hn : n < J) : ‖haarLayer (modρ ρ u) 0 1 n x - + c * haarLayer (modρ ρ v) 0 1 n x‖ ≤ D * (2 * π) * side n := by + unfold haarLayer + rw [Finset.mul_sum, ← Finset.sum_sub_distrib] + let inP (k : ℕ) := x ∈ box (lft 0 1 n k) (1*side n) + have hp : 0 < (1:ℝ)*side n := mul_pos zero_lt_one (side_pos n) + by_cases h : ∃ k ∈ Finset.range (2^n), inP k + · obtain ⟨k,hk,hxk⟩ := h + rw [Finset.sum_eq_single k] + · let a := lft 0 1 n k + let r := 1 * side n + change ‖haarG _ • coeffH _ a r - c * (haarG _ • coeffH _ a r)‖ ≤ _ + simp only [Complex.real_smul] + rw [mul_left_comm c, ← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs] + refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans + ((coeff_near hρ h0 B hb hxk hu).trans ?_) + have Hk := hx n hn k (Finset.mem_range.mp hk) hxk + refine (mul_le_mul_of_nonneg_right Hk (show 0 ≤ 2*π*(1*side n) by positivity)).trans ?_ + simp [mul_assoc] + · intro l hl hlk + have H : ¬ inP l := fun hh => hlk (cell_unique 0 1 zero_lt_one n hh hxk) + rw [jump_outside hp H _, jump_outside hp H _] + simp + · intro h; exact (h hk).elim + · rw [Finset.sum_eq_zero] + · rw [norm_zero]; exact mul_nonneg (by positivity) (side_pos _).le + · intro k hk + have H : ¬ inP k := fun hh => h ⟨k,hk,hh⟩ + rw [jump_outside hp H _, jump_outside hp H _] + simp + have ht : ‖shift (modρ ρ u) 0 1 J x - c * shift (modρ ρ v) 0 1 J x‖ ≤ D*(2*π)*2 := by + unfold shift + rw [Finset.mul_sum, ← Finset.sum_sub_distrib] + refine (norm_sum_le _ _).trans ((Finset.sum_le_sum (fun n hn => hl n (Finset.mem_range.mp hn))).trans ?_) + rw [← Finset.mul_sum] + exact mul_le_mul_of_nonneg_left (sum_geometric_two_le _) (by positivity) + have hh := norm_sub_norm_le (shift (modρ ρ u) 0 1 J x) (c * shift (modρ ρ v) 0 1 J x) + rw [norm_mul, phase_unimod, one_mul] at hh + linarith + +lemma shift_pull (F : ℝ → ℂ) (a s : ℝ) (hs : 0 < s) (J : ℕ) (x : ℝ) : + shift F a s J (a+s*x) = shift (fun y => F (a+s*y)) 0 1 J x := by + unfold shift + refine Finset.sum_congr rfl fun n _ => Finset.sum_congr rfl fun k _ => ?_ + let l := lft 0 1 n k + have h1 : lft a s n k = a+s*l := by unfold l lft; ring + rw [h1] + unfold jump coeffH + congr 2 + · dsimp only [l] + field_simp + all_goals ring + all_goals + simp_rw [show ∀ v : ℝ, a+s*l+(s*side n)*v = a+s*(l+(1*side n)*v) by intro v; ring] + rfl + +lemma shift_scale (F : ℝ → ℂ) (c : ℂ) (a s : ℝ) (J : ℕ) (x : ℝ) : + shift (fun y => c * F y) a s J x = c * shift F a s J x := by + unfold shift haarLayer + simp_rw [Finset.mul_sum] + refine Finset.sum_congr rfl fun n _ => Finset.sum_congr rfl fun k _ => ?_ + dsimp only [jump, coeffH] + simp only [Complex.real_smul] + simp_rw [mul_left_comm (↑(haarH _)) c, integral_const_mul] + ring + +lemma map_affine (a : ℝ) {s : ℝ} (hs : 0 < s) : + Measure.map (fun x : ℝ => a+s*x) volume = ENNReal.ofReal (s⁻¹) • volume := by + rw [show (fun x : ℝ => a+s*x) = ((a+·) ∘ fun x : ℝ => s*x) from rfl] + rw [← Measure.map_map (by fun_prop) (by fun_prop), Real.map_volume_mul_left hs.ne', + Measure.map_smul] + all_goals first + | simp only [abs_of_pos (inv_pos.mpr hs), map_add_left_eq_self] + | exact (measurable_const.add measurable_id).aemeasurable + +lemma restrict_affine (a : ℝ) {s : ℝ} (hs : 0 < s) (G : Set ℝ) (hG : MeasurableSet G) + (f : ℝ → ℝ≥0∞) (hf : Measurable f) : + (∫⁻ x in G, f x) = + ENNReal.ofReal s * ∫⁻ x in (fun y : ℝ => a+s*y) ⁻¹' G, f (a+s*x) := by + let φ (x : ℝ) := a+s*x + have hm : Measurable φ := by unfold φ; fun_prop + have H : Measure.map φ (volume.restrict (φ ⁻¹' G)) = + ENNReal.ofReal (s⁻¹) • volume.restrict G := by + rw [← Measure.restrict_map hm hG, map_affine a hs, Measure.restrict_smul] + change _ = ENNReal.ofReal s * ∫⁻ x in φ ⁻¹' G, f (φ x) + rw [← lintegral_map hf hm, H, lintegral_smul_measure, smul_eq_mul, ← mul_assoc, + ← ENNReal.ofReal_mul hs.le, mul_inv_cancel₀ hs.ne', ENNReal.ofReal_one, one_mul] + +def shiftD (ρ : ℝ → ℝ) (D : ℝ) (G : Set ℝ) : Prop := + ∀ x ∈ G, ∀ (a s : ℝ), 0 < s → s ≤ 1 → x ∈ box a s → + (∫ y in box a s, ρ y) ≤ D*s + +def AffineTreeBound : Prop := + ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (ρ : ℝ → ℝ), + Measurable ρ → Periodic ρ 1 → (∀ x, 0 ≤ ρ x) → (∫ x, ρ x ∂μ₁) ≤ 1 → + (∀ x, ρ x ≤ Real.exp ((2 : ℝ)^j)) → + ∀ (J : ℕ) (G : Set ℝ), MeasurableSet G → shiftD ρ (((j:ℝ)+1)^3) G → + ∀ (a s : ℝ), 1/2 ≤ s → s ≤ 1 → + (∫⁻ x in G, maxShift ρ a s J x) ≤ ENNReal.ofReal (C * (2:ℝ)^j) + +lemma cube_eventually : + ∀ᶠ j : ℕ in atTop, ((j:ℝ)+1)^3 ≤ (8:ℝ) * 2^j := by + filter_upwards [(tendsto_pow_const_div_const_pow_of_one_lt 3 + (show (1:ℝ)<2 by norm_num)).eventually (gt_mem_nhds (show (0:ℝ)<1 by norm_num)), + eventually_ge_atTop 1] with j hj h + change (j:ℝ)^3/(2:ℝ)^j < 1 at hj + have hh := (div_lt_one (by positivity)).mp hj + have H : (j:ℝ) + 1 ≤ 2 * (j:ℝ) := by exact_mod_cast (show j+1 ≤ 2*j by omega) + calc + _ ≤ (2*(j:ℝ))^3 := by gcongr + _ ≤ 8*2^j := by rw [mul_pow]; linarith + +theorem UnitTreeBound.affine (hunit : UnitTreeBound) : AffineTreeBound := by + obtain ⟨C, hC, h⟩ := hunit + refine ⟨C+8*(2*π)*2, by positivity, ?_⟩ + filter_upwards [h, cube_eventually] with j hj hcj + intro ρ hρ hp h0 hm hB J G hG hGood a s hs hs' + let φ (x : ℝ) := a+s*x + let F (x : ℝ) := ρ (φ x) + have hs0 : 0 < s := by linarith + have hmφ : Measurable φ := by unfold φ; fun_prop + have hF : Measurable F := hρ.comp hmφ + let B := Real.exp ((2:ℝ)^j) + let D := ((j:ℝ)+1)^3 + have hD : 0 ≤ D := by unfold D; positivity + have hF0 (x) : 0 ≤ F x := h0 _ + have hFB (x) : F x ≤ B := hB _ + let Gt := G ∩ box a s + have hbm : MeasurableSet (box a s) := measurableSet_Ico + let H := φ ⁻¹' Gt + have hH : MeasurableSet H := hmφ (hG.inter hbm) + have hsub : H ⊆ unitInt := by + intro x hx + have ⟨h1,h2⟩ := (show φ x ∈ Gt from hx).2 + dsimp only [φ] at * + constructor <;> nlinarith + have hmF : (∫ x, F x ∂μ₁) ≤ 2 := by + change (∫ x, ρ (a+s*x) ∂μ₁) ≤ 2 + rw [mean_translate_scale ρ a hs0, smul_eq_mul, ← div_eq_inv_mul] + exact (div_le_iff₀ hs0).mpr ((density_short ρ hρ hp h0 B hB hm a s hs').trans (by linarith)) + have hu : ∀ x ∈ H, usable F 0 1 J D x := by + intro x hx n _ k _ hk + let l := lft 0 1 n k + let r := (1:ℝ)*side n + have hr : 0 < r := mul_pos zero_lt_one (side_pos _) + have hrs := mul_pos hs0 hr + change density F l r ≤ _ + have hgd := hGood _ ((show φ x ∈ Gt from hx).1) (φ l) (s*r) hrs + (by simpa [r] using (show s * side n ≤ 1 from + (mul_le_mul_of_nonneg_right hs' (side_pos n).le).trans + (by simpa using side_le n))) + (show φ x ∈ box (φ l) (s*r) by + change l ≤ x ∧ x < l+r at hk + change a+s*l ≤ a+s*x ∧ a+s*x < a+s*l+s*r + constructor <;> nlinarith [hk.1,hk.2]) + change D*(s*r) ≥ _ at hgd + change (∫ y, ρ (φ (l+r*y)) ∂μ₁) ≤ _ + simp_rw [show ∀ y, φ (l+r*y) = φ l + (s*r)*y by intro y; unfold φ; ring, + mean_translate_scale ρ (φ l) hrs] + rw [smul_eq_mul, ← div_eq_inv_mul, div_le_iff₀ hrs] + exact hgd + have htrans (x : ℝ) (hx : x ∈ H) : + maxShift ρ a s J (φ x) ≤ maxShift F 0 1 J x + ENNReal.ofReal (D*(2*π)*2) := by + refine iSup_le fun n => ?_ + let u := (n:ℝ)*s + let k := ⌊u⌋ + have heq (y : ℝ) : modρ ρ (n:ℝ) (a+s*y) = phase n a * modρ F u y := by + unfold modρ + rw [phase_add, phase_mul_arg] + dsimp only [F,φ,u] + ring + change ENNReal.ofReal ‖shift _ _ _ _ (a+s*x)‖ ≤ _ + simp_rw [shift_pull _ a s hs0, heq, shift_scale, norm_mul, phase_unimod, one_mul] + have hnear : |u-(k:ℝ)| ≤ 1 := + abs_le.mpr (by have := Int.floor_le u; have := Int.lt_floor_add_one u; dsimp only [k]; constructor <;> linarith) + refine (ENNReal.ofReal_le_ofReal (shift_near_unit hF hF0 B hFB J (hu _ hx) hD hnear)).trans ?_ + rw [ENNReal.ofReal_add (by positivity) (by positivity)] + exact add_le_add_left (le_iSup (fun n : ℤ => + ENNReal.ofReal ‖shift (modρ F n) 0 1 J x‖) k) _ + let Z := maxShift ρ a s J + have hZ : Measurable Z := + measurable_maxShift_comp hρ measurable_const measurable_const measurable_id _ + have hz (x) (hx : x ∉ box a s) : Z x = 0 := by + unfold Z maxShift; simp only [shift_outside hs0 hx, norm_zero, ENNReal.ofReal_zero, iSup_const] + have hiG : (∫⁻ x in G, Z x) = ∫⁻ x in Gt, Z x := by + rw [← lintegral_indicator hG, ← lintegral_indicator (hG.inter hbm)] + exact lintegral_congr (fun x => by + by_cases hx : x ∈ box a s <;> by_cases hy : x ∈ G <;> simp_all only [ + mem_inter_iff, not_false_eq_true, and_self, and_false, false_and, + indicator_of_mem, indicator_of_notMem]) + change (∫⁻ x in G, Z x) ≤ _ + rw [hiG, restrict_affine a hs0 Gt (hG.inter hbm) Z hZ] + change ENNReal.ofReal s * ∫⁻ x in H, Z (φ x) ≤ _ + have hmH := hj F hF hF0 hmF hFB J H hH hsub hu + refine le_trans (b := ∫⁻ x in H, Z (φ x)) (mul_le_of_le_one_left zero_le + (by simpa using ENNReal.ofReal_le_ofReal hs')) ?_ + refine (lintegral_mono_ae ((ae_restrict_mem hH).mono htrans)).trans ?_ + rw [lintegral_add_right _ measurable_const] + have Hvol : volume H ≤ 1 := (measure_mono hsub).trans (by simp [unitInt]) + have Hbound : (∫⁻ _ : ℝ in H, ENNReal.ofReal (D*(2*π)*2)) ≤ ENNReal.ofReal (D*(2*π)*2) := by + simp only [setLIntegral_const] + exact mul_le_of_le_one_right zero_le Hvol + refine (add_le_add hmH Hbound).trans ?_ + rw [← ENNReal.ofReal_add (by positivity) (by positivity)] + apply ENNReal.ofReal_le_ofReal + rw [add_mul] + apply add_le_add_right + calc + _ ≤ ((8:ℝ)*2^j)*(2*π)*2 := by gcongr + _ = _ := by ring + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Coordinates.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Coordinates.lean index 72925380f3532bbc08932e305739e02352f9ddc0..13483de6873aed2274d3fad8aeaefab2a7dcf1fe 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Coordinates.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Coordinates.lean @@ -1,252 +1,252 @@ -import OAI.Analysis.FourierLLogL.Fourier.Endpoint -/-! # Using period one in real coordinates -/ - -noncomputable section - -open MeasureTheory Filter Set -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL - -namespace Period - -def unitInt : Set ℝ := Ico 0 1 - -noncomputable def μ₁ : Measure ℝ := volume.restrict unitInt - -def circle (x : ℝ) : Torus := ((2 * π) * x : ℝ) - -lemma measurable_unitInt : MeasurableSet unitInt := measurableSet_Ico - -instance : IsProbabilityMeasure μ₁ := by - constructor - simp [μ₁, unitInt] - -theorem measurable_circle : Measurable circle := - AddCircle.measurable_mk'.comp (measurable_const.mul measurable_id) - -lemma map_circle_volume : Measure.map circle μ₁ = circleMeasure := by - let t : ℝ := 2*π - have ht : 0 < t := Real.two_pi_pos - let φ (x : ℝ) := t*x - have h : Measurable φ := measurable_const.mul measurable_id - change Measure.map (((↑) : ℝ → AddCircle t) ∘ φ) μ₁ = _ - rw [← Measure.map_map AddCircle.measurable_mk' h] - have hf : φ ⁻¹' Ico 0 (0+t) = unitInt := by - ext x - simp only [unitInt, mem_Ico, mem_preimage, zero_add, φ] - constructor - · intro ⟨hx, hy⟩; constructor <;> nlinarith - · intro ⟨hx, hy⟩; constructor <;> nlinarith - rw [μ₁, ← hf, ← Measure.restrict_map h measurableSet_Ico, - Real.map_volume_mul_left ht.ne', Measure.restrict_smul, - Measure.restrict_congr_set Ico_ae_eq_Ioc, Measure.map_smul, - (AddCircle.measurePreserving_mk t 0).map_eq, AddCircle.volume_eq_smul_haarAddCircle, - smul_smul, abs_of_pos (inv_pos.mpr ht), ← ENNReal.ofReal_mul (inv_pos.mpr ht).le, - inv_mul_cancel₀ ht.ne', ENNReal.ofReal_one, one_smul] - all_goals first - | rfl - | exact AddCircle.measurable_mk'.aemeasurable - -lemma periodic_circle : Function.Periodic circle 1 := by - intro x - simp only [circle, mul_add, mul_one, AddCircle.coe_add_period] - -def coord (x : Torus) : ℝ := - ((AddCircle.measurableEquivIco (2*π) 0 x).val) / (2*π) - -theorem measurable_coord : Measurable coord := - (measurable_subtype_coe.comp (AddCircle.measurableEquivIco (2*π) 0).measurable).div_const _ - -lemma coord_circle_eq {x : ℝ} (hx : x ∈ unitInt) : coord (circle x) = x := by - let t := (2 * π) - have ht : (0:ℝ) < t := Real.two_pi_pos - change (((AddCircle.equivIco t 0 ((t*x:ℝ):AddCircle t)).val) / t) = x - have h : t * x ∈ Ico 0 (0+t) := by - rcases hx with ⟨h1,h2⟩ - simp only [mem_Ico] - constructor <;> nlinarith - rw [AddCircle.equivIco_coe_eq h] - exact mul_div_cancel_left₀ _ ht.ne' - -/-- Character with cycles-per-unit real frequency. -/ -def phase (u x : ℝ) : ℂ := - Complex.exp (Complex.I * ↑(u*((2*π) * x))) - -lemma phase_cont (u) : Continuous (phase u) := by unfold phase; fun_prop -lemma phase_unimod (u x) : ‖phase u x‖ = 1 := by - simp [phase, Complex.norm_exp] - -lemma fourier_circle (n : ℤ) (x : ℝ) : fourier n (circle x) = phase n x := by - simp only [circle, fourier_of_real, phase, Complex.ofReal_mul, Complex.ofReal_intCast, - mul_assoc] - -lemma phase_periodic (n : ℤ) : Function.Periodic (phase n) 1 := - fun x => by rw [← fourier_circle, ← fourier_circle, periodic_circle x] - -def sumReal (f : ℝ → ℂ) (N : ℕ) (x : ℝ) : ℂ := - ∑ k ∈ Finset.Icc (-(N:ℤ)) (N:ℤ), (∫ y, f y * phase (-(k:ℤ):ℤ) y ∂μ₁) * phase k x - -def maxReal (f : ℝ → ℂ) (x : ℝ) : ℝ≥0∞ := - ⨆ N : ℕ, ENNReal.ofReal ‖sumReal f N x‖ - -lemma sum_eq (f : Torus → ℂ) (hf : Measurable f) (N x) : - symmetricPartialSum f N (circle x) = sumReal (f ∘ circle) N x := by - simp only [symmetricPartialSum, sumReal, fourierCoeff_eq_integral, ← map_circle_volume] - refine Finset.sum_congr rfl ?_ - intro n _ - rw [integral_map (f:=fun x => f x * fourier (-n) x) measurable_circle.aemeasurable - ((hf.mul (fourier _).continuous.measurable).aestronglyMeasurable)] - simp only [fourier_circle, Function.comp_def] - -def RealGoodBound : Prop := - ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (f : ℝ → ℝ), - Measurable f → Function.Periodic f 1 → (∀ x, 0 ≤ f x) → (∫ x, f x ∂μ₁) ≤ 1 → - (∀ x, f x ≤ Real.exp ((2 : ℝ)^j)) → - ∃ G : Set ℝ, MeasurableSet G ∧ - μ₁ Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ - (∫⁻ x in G, maxReal (fun y => (f y : ℂ)) x ∂μ₁) ≤ ENNReal.ofReal (C * (2:ℝ)^j) - -end Period - -def HasMaxCost (f : Torus → ℂ) (j : ℕ) (C : ℝ) : Prop := - ∃ G : Set Torus, MeasurableSet G ∧ - circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ - (∫⁻ x in G, symmetricMaximal f x ∂circleMeasure) ≤ ENNReal.ofReal (C * (2:ℝ)^j) - -namespace Period - -lemma cost_transfer (f : Torus → ℂ) (hf : Measurable f) - (j : ℕ) (C : ℝ) (G : Set ℝ) (hG : MeasurableSet G) - (hsize : μ₁ Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2)) - (hint : (∫⁻ x in G, maxReal (f ∘ circle) x ∂μ₁) ≤ ENNReal.ofReal (C * (2:ℝ)^j)) : - HasMaxCost f j C := by - let S := coord ⁻¹' G - have hS : MeasurableSet S := measurable_coord hG - refine ⟨S, hS, ?_, ?_⟩ - · rw [← map_circle_volume, Measure.map_apply measurable_circle hS.compl] - have heq : circle ⁻¹' Sᶜ =ᵐ[μ₁] Gᶜ := by - filter_upwards [ae_restrict_mem (μ:=volume) measurable_unitInt] with x hx - change (coord (circle x) ∉ G) = (x ∉ G) - rw [coord_circle_eq hx] - rwa [measure_congr heq] - · rw [← lintegral_indicator hS, ← map_circle_volume, - lintegral_map ((measurable_symmetricMaximal _).indicator hS) measurable_circle] - rw [← lintegral_indicator hG] at hint - convert hint using 1 - apply lintegral_congr_ae - filter_upwards [ae_restrict_mem (μ:=volume) measurable_unitInt] with x hx - have heq : symmetricMaximal f (circle x) = maxReal (f ∘ circle) x := by - simp only [symmetricMaximal, maxReal, sum_eq f hf] - by_cases h : x ∈ G <;> simp [S, coord_circle_eq hx, heq, h] - -end Period - -lemma HasMaxCost.add {f g : Torus → ℂ} {C D : ℝ} {j : ℕ} - (hF : HasMaxCost f j C) (hG : HasMaxCost g j D) - (hf : Integrable f circleMeasure) (hg : Integrable g circleMeasure) - (hC : 0 ≤ C) (hD : 0 ≤ D) : HasMaxCost (f+g) j (C+D) := by - obtain ⟨F, hFm, hFs, hFi⟩ := hF - obtain ⟨G, hGm, hGs, hGi⟩ := hG - refine ⟨F ∩ G, hFm.inter hGm, ?_, ?_⟩ - · rw [add_div, ENNReal.ofReal_add (by positivity) (by positivity), Set.compl_inter] - exact (measure_union_le _ _).trans (add_le_add hFs hGs) - · have h (x : Torus) : - symmetricMaximal (f + g) x ≤ symmetricMaximal f x + symmetricMaximal g x := by - unfold symmetricMaximal - refine iSup_le fun n => ?_ - rw [symmetricPartialSum_add hf hg] - refine (ENNReal.ofReal_le_ofReal - (norm_add_le (symmetricPartialSum f n x) (symmetricPartialSum g n x))).trans ?_ - rw [ENNReal.ofReal_add (by positivity) (by positivity)] - exact add_le_add (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum f N x‖) n) - (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum g N x‖) n) - refine (lintegral_mono h).trans ?_ - rw [lintegral_add_left (measurable_symmetricMaximal _), add_mul, - ENNReal.ofReal_add (by positivity) (by positivity)] - exact add_le_add ((lintegral_mono_set inter_subset_left).trans hFi) - ((lintegral_mono_set inter_subset_right).trans hGi) - -lemma HasMaxCost.rot {f j C} (h : HasMaxCost f j C) - (c : ℂ) (hc : ‖c‖ = 1) : HasMaxCost (fun x => c * f x) j C := by - simpa [HasMaxCost, symmetricMaximal_mul, hc] using h - - -theorem Period.RealGoodBound.goodBound (hb : Period.RealGoodBound) : GoodBound := by - obtain ⟨C, hC, h⟩ := hb - refine ⟨C+C+(C+C), by positivity, ?_⟩ - filter_upwards [h] with j hj - intro f hf ha he - let f₀ := hf.aestronglyMeasurable.mk f - let g (x : Torus) := if ‖f₀ x‖ ≤ Real.exp ((2:ℝ)^j) then f₀ x else 0 - have hf₀ : Measurable f₀ := hf.aestronglyMeasurable.measurable_mk - have hg : Measurable g := - Measurable.ite (measurableSet_le hf₀.norm measurable_const) hf₀ measurable_const - have hfg : f =ᵐ[circleMeasure] g := by - filter_upwards [hf.aestronglyMeasurable.ae_eq_mk, he] with x hx hx' - change f x = f₀ x at hx - simp only [g, ← hx, hx', ite_true] - have hgb (x : Torus) : ‖g x‖ ≤ Real.exp ((2:ℝ)^j) := by - dsimp only [g] - split_ifs with HH - · exact HH - · rw [norm_zero]; positivity - have hgi : Integrable g circleMeasure := hf.congr hfg - have hgm : (∫ x, ‖g x‖ ∂circleMeasure) ≤ 1 := - (integral_congr_ae (hfg.symm.fun_comp (‖·‖))).le.trans ha - have hpart (w : Torus → ℝ) (hw : Measurable w) - (hw0 : ∀ x, 0 ≤ w x) (hwn : ∀ x, w x ≤ ‖g x‖) : - HasMaxCost (fun x => (w x : ℂ)) j C ∧ - Integrable (fun x => (w x : ℂ)) circleMeasure := by - have hc : Measurable (fun x => (w x : ℂ)) := Complex.continuous_ofReal.measurable.comp hw - have hwi : Integrable w circleMeasure := Integrable.of_bound hw.aestronglyMeasurable _ - (ae_of_all _ (fun x => by - simpa only [Real.norm_eq_abs, abs_of_nonneg (hw0 x)] using - (hwn x).trans (hgb x))) - have hwci : Integrable (fun x => (w x : ℂ)) circleMeasure := - Integrable.of_bound hc.aestronglyMeasurable _ (ae_of_all _ (fun x => by - simpa only [Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw0 x)] using - (hwn x).trans (hgb x))) - have hm : (∫ x, (w ∘ circle) x ∂μ₁) ≤ 1 := by - simp only [Function.comp_apply, ← integral_map (f:=w) - measurable_circle.aemeasurable hw.aestronglyMeasurable, map_circle_volume] - exact (integral_mono hwi hgi.norm hwn).trans hgm - obtain ⟨G,hG,hsize,hInt⟩ := hj (w ∘ circle) (hw.comp measurable_circle) - (fun x => congrArg w (periodic_circle x)) - (fun x => hw0 _) hm (fun x => (hwn _).trans (hgb _)) - exact ⟨cost_transfer _ hc j C G hG hsize hInt, hwci⟩ - have hdiff (w : Torus → ℝ) (hw : Measurable w) (hwn : ∀ x, |w x| ≤ ‖g x‖) : - HasMaxCost (fun x => (w x : ℂ)) j (C+C) ∧ - Integrable (fun x => (w x : ℂ)) circleMeasure := by - obtain ⟨hp, hpi⟩ := hpart (fun x => max 0 (w x)) (measurable_const.max hw) (fun x => le_max_left _ _) - (fun x => max_le (norm_nonneg _) ((abs_le.mp (hwn x)).2)) - obtain ⟨hn, hni⟩ := hpart (fun x => max 0 (-w x)) (measurable_const.max hw.neg) - (fun x => le_max_left _ _) (fun x => max_le (norm_nonneg _) (by have := (abs_le.mp (hwn x)).1; linarith)) - rw [show (fun x => (w x : ℂ)) = (fun x => ((max 0 (w x) : ℝ) : ℂ)) - + (fun x => (-1:ℂ) * ((max 0 (-w x) : ℝ) : ℂ)) by - ext1 x - rcases le_total 0 (w x) with hwx| hwx - · simp [hwx] - · simp [hwx]] - exact ⟨hp.add (hn.rot _ (by simp)) hpi (hni.const_mul _) hC hC, hpi.add (hni.const_mul _)⟩ - obtain ⟨hr, hri⟩ := hdiff (fun x => (g x).re) - (Complex.continuous_re.measurable.comp hg) (fun x => Complex.abs_re_le_norm _) - obtain ⟨hi, hii⟩ := hdiff (fun x => (g x).im) - (Complex.continuous_im.measurable.comp hg) (fun x => Complex.abs_im_le_norm _) - have H : HasMaxCost g j (C+C+(C+C)) := by - have H := hr.add (hi.rot Complex.I (by simp)) hri (hii.const_mul Complex.I) - (by linarith) (by linarith) - convert H using 1 - ext1 x - rw [Pi.add_apply, mul_comm, Complex.re_add_im] - have eq : symmetricMaximal f = symmetricMaximal g := by - ext1 x - simp only [symmetricMaximal, symmetricPartialSum_congr_ae hfg] - rw [eq] - exact H - -end FourierLLogL - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Endpoint +/-! # Using period one in real coordinates -/ + +noncomputable section + +open MeasureTheory Filter Set +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL + +namespace Period + +def unitInt : Set ℝ := Ico 0 1 + +noncomputable def μ₁ : Measure ℝ := volume.restrict unitInt + +def circle (x : ℝ) : Torus := ((2 * π) * x : ℝ) + +lemma measurable_unitInt : MeasurableSet unitInt := measurableSet_Ico + +instance : IsProbabilityMeasure μ₁ := by + constructor + simp [μ₁, unitInt] + +theorem measurable_circle : Measurable circle := + AddCircle.measurable_mk'.comp (measurable_const.mul measurable_id) + +lemma map_circle_volume : Measure.map circle μ₁ = circleMeasure := by + let t : ℝ := 2*π + have ht : 0 < t := Real.two_pi_pos + let φ (x : ℝ) := t*x + have h : Measurable φ := measurable_const.mul measurable_id + change Measure.map (((↑) : ℝ → AddCircle t) ∘ φ) μ₁ = _ + rw [← Measure.map_map AddCircle.measurable_mk' h] + have hf : φ ⁻¹' Ico 0 (0+t) = unitInt := by + ext x + simp only [unitInt, mem_Ico, mem_preimage, zero_add, φ] + constructor + · intro ⟨hx, hy⟩; constructor <;> nlinarith + · intro ⟨hx, hy⟩; constructor <;> nlinarith + rw [μ₁, ← hf, ← Measure.restrict_map h measurableSet_Ico, + Real.map_volume_mul_left ht.ne', Measure.restrict_smul, + Measure.restrict_congr_set Ico_ae_eq_Ioc, Measure.map_smul, + (AddCircle.measurePreserving_mk t 0).map_eq, AddCircle.volume_eq_smul_haarAddCircle, + smul_smul, abs_of_pos (inv_pos.mpr ht), ← ENNReal.ofReal_mul (inv_pos.mpr ht).le, + inv_mul_cancel₀ ht.ne', ENNReal.ofReal_one, one_smul] + all_goals first + | rfl + | exact AddCircle.measurable_mk'.aemeasurable + +lemma periodic_circle : Function.Periodic circle 1 := by + intro x + simp only [circle, mul_add, mul_one, AddCircle.coe_add_period] + +def coord (x : Torus) : ℝ := + ((AddCircle.measurableEquivIco (2*π) 0 x).val) / (2*π) + +theorem measurable_coord : Measurable coord := + (measurable_subtype_coe.comp (AddCircle.measurableEquivIco (2*π) 0).measurable).div_const _ + +lemma coord_circle_eq {x : ℝ} (hx : x ∈ unitInt) : coord (circle x) = x := by + let t := (2 * π) + have ht : (0:ℝ) < t := Real.two_pi_pos + change (((AddCircle.equivIco t 0 ((t*x:ℝ):AddCircle t)).val) / t) = x + have h : t * x ∈ Ico 0 (0+t) := by + rcases hx with ⟨h1,h2⟩ + simp only [mem_Ico] + constructor <;> nlinarith + rw [AddCircle.equivIco_coe_eq h] + exact mul_div_cancel_left₀ _ ht.ne' + +/-- Character with cycles-per-unit real frequency. -/ +def phase (u x : ℝ) : ℂ := + Complex.exp (Complex.I * ↑(u*((2*π) * x))) + +lemma phase_cont (u) : Continuous (phase u) := by unfold phase; fun_prop +lemma phase_unimod (u x) : ‖phase u x‖ = 1 := by + simp [phase, Complex.norm_exp] + +lemma fourier_circle (n : ℤ) (x : ℝ) : fourier n (circle x) = phase n x := by + simp only [circle, fourier_of_real, phase, Complex.ofReal_mul, Complex.ofReal_intCast, + mul_assoc] + +lemma phase_periodic (n : ℤ) : Function.Periodic (phase n) 1 := + fun x => by rw [← fourier_circle, ← fourier_circle, periodic_circle x] + +def sumReal (f : ℝ → ℂ) (N : ℕ) (x : ℝ) : ℂ := + ∑ k ∈ Finset.Icc (-(N:ℤ)) (N:ℤ), (∫ y, f y * phase (-(k:ℤ):ℤ) y ∂μ₁) * phase k x + +def maxReal (f : ℝ → ℂ) (x : ℝ) : ℝ≥0∞ := + ⨆ N : ℕ, ENNReal.ofReal ‖sumReal f N x‖ + +lemma sum_eq (f : Torus → ℂ) (hf : Measurable f) (N x) : + symmetricPartialSum f N (circle x) = sumReal (f ∘ circle) N x := by + simp only [symmetricPartialSum, sumReal, fourierCoeff_eq_integral, ← map_circle_volume] + refine Finset.sum_congr rfl ?_ + intro n _ + rw [integral_map (f:=fun x => f x * fourier (-n) x) measurable_circle.aemeasurable + ((hf.mul (fourier _).continuous.measurable).aestronglyMeasurable)] + simp only [fourier_circle, Function.comp_def] + +def RealGoodBound : Prop := + ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (f : ℝ → ℝ), + Measurable f → Function.Periodic f 1 → (∀ x, 0 ≤ f x) → (∫ x, f x ∂μ₁) ≤ 1 → + (∀ x, f x ≤ Real.exp ((2 : ℝ)^j)) → + ∃ G : Set ℝ, MeasurableSet G ∧ + μ₁ Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ + (∫⁻ x in G, maxReal (fun y => (f y : ℂ)) x ∂μ₁) ≤ ENNReal.ofReal (C * (2:ℝ)^j) + +end Period + +def HasMaxCost (f : Torus → ℂ) (j : ℕ) (C : ℝ) : Prop := + ∃ G : Set Torus, MeasurableSet G ∧ + circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ + (∫⁻ x in G, symmetricMaximal f x ∂circleMeasure) ≤ ENNReal.ofReal (C * (2:ℝ)^j) + +namespace Period + +lemma cost_transfer (f : Torus → ℂ) (hf : Measurable f) + (j : ℕ) (C : ℝ) (G : Set ℝ) (hG : MeasurableSet G) + (hsize : μ₁ Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2)) + (hint : (∫⁻ x in G, maxReal (f ∘ circle) x ∂μ₁) ≤ ENNReal.ofReal (C * (2:ℝ)^j)) : + HasMaxCost f j C := by + let S := coord ⁻¹' G + have hS : MeasurableSet S := measurable_coord hG + refine ⟨S, hS, ?_, ?_⟩ + · rw [← map_circle_volume, Measure.map_apply measurable_circle hS.compl] + have heq : circle ⁻¹' Sᶜ =ᵐ[μ₁] Gᶜ := by + filter_upwards [ae_restrict_mem (μ:=volume) measurable_unitInt] with x hx + change (coord (circle x) ∉ G) = (x ∉ G) + rw [coord_circle_eq hx] + rwa [measure_congr heq] + · rw [← lintegral_indicator hS, ← map_circle_volume, + lintegral_map ((measurable_symmetricMaximal _).indicator hS) measurable_circle] + rw [← lintegral_indicator hG] at hint + convert hint using 1 + apply lintegral_congr_ae + filter_upwards [ae_restrict_mem (μ:=volume) measurable_unitInt] with x hx + have heq : symmetricMaximal f (circle x) = maxReal (f ∘ circle) x := by + simp only [symmetricMaximal, maxReal, sum_eq f hf] + by_cases h : x ∈ G <;> simp [S, coord_circle_eq hx, heq, h] + +end Period + +lemma HasMaxCost.add {f g : Torus → ℂ} {C D : ℝ} {j : ℕ} + (hF : HasMaxCost f j C) (hG : HasMaxCost g j D) + (hf : Integrable f circleMeasure) (hg : Integrable g circleMeasure) + (hC : 0 ≤ C) (hD : 0 ≤ D) : HasMaxCost (f+g) j (C+D) := by + obtain ⟨F, hFm, hFs, hFi⟩ := hF + obtain ⟨G, hGm, hGs, hGi⟩ := hG + refine ⟨F ∩ G, hFm.inter hGm, ?_, ?_⟩ + · rw [add_div, ENNReal.ofReal_add (by positivity) (by positivity), Set.compl_inter] + exact (measure_union_le _ _).trans (add_le_add hFs hGs) + · have h (x : Torus) : + symmetricMaximal (f + g) x ≤ symmetricMaximal f x + symmetricMaximal g x := by + unfold symmetricMaximal + refine iSup_le fun n => ?_ + rw [symmetricPartialSum_add hf hg] + refine (ENNReal.ofReal_le_ofReal + (norm_add_le (symmetricPartialSum f n x) (symmetricPartialSum g n x))).trans ?_ + rw [ENNReal.ofReal_add (by positivity) (by positivity)] + exact add_le_add (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum f N x‖) n) + (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum g N x‖) n) + refine (lintegral_mono h).trans ?_ + rw [lintegral_add_left (measurable_symmetricMaximal _), add_mul, + ENNReal.ofReal_add (by positivity) (by positivity)] + exact add_le_add ((lintegral_mono_set inter_subset_left).trans hFi) + ((lintegral_mono_set inter_subset_right).trans hGi) + +lemma HasMaxCost.rot {f j C} (h : HasMaxCost f j C) + (c : ℂ) (hc : ‖c‖ = 1) : HasMaxCost (fun x => c * f x) j C := by + simpa [HasMaxCost, symmetricMaximal_mul, hc] using h + + +theorem Period.RealGoodBound.goodBound (hb : Period.RealGoodBound) : GoodBound := by + obtain ⟨C, hC, h⟩ := hb + refine ⟨C+C+(C+C), by positivity, ?_⟩ + filter_upwards [h] with j hj + intro f hf ha he + let f₀ := hf.aestronglyMeasurable.mk f + let g (x : Torus) := if ‖f₀ x‖ ≤ Real.exp ((2:ℝ)^j) then f₀ x else 0 + have hf₀ : Measurable f₀ := hf.aestronglyMeasurable.measurable_mk + have hg : Measurable g := + Measurable.ite (measurableSet_le hf₀.norm measurable_const) hf₀ measurable_const + have hfg : f =ᵐ[circleMeasure] g := by + filter_upwards [hf.aestronglyMeasurable.ae_eq_mk, he] with x hx hx' + change f x = f₀ x at hx + simp only [g, ← hx, hx', ite_true] + have hgb (x : Torus) : ‖g x‖ ≤ Real.exp ((2:ℝ)^j) := by + dsimp only [g] + split_ifs with HH + · exact HH + · rw [norm_zero]; positivity + have hgi : Integrable g circleMeasure := hf.congr hfg + have hgm : (∫ x, ‖g x‖ ∂circleMeasure) ≤ 1 := + (integral_congr_ae (hfg.symm.fun_comp (‖·‖))).le.trans ha + have hpart (w : Torus → ℝ) (hw : Measurable w) + (hw0 : ∀ x, 0 ≤ w x) (hwn : ∀ x, w x ≤ ‖g x‖) : + HasMaxCost (fun x => (w x : ℂ)) j C ∧ + Integrable (fun x => (w x : ℂ)) circleMeasure := by + have hc : Measurable (fun x => (w x : ℂ)) := Complex.continuous_ofReal.measurable.comp hw + have hwi : Integrable w circleMeasure := Integrable.of_bound hw.aestronglyMeasurable _ + (ae_of_all _ (fun x => by + simpa only [Real.norm_eq_abs, abs_of_nonneg (hw0 x)] using + (hwn x).trans (hgb x))) + have hwci : Integrable (fun x => (w x : ℂ)) circleMeasure := + Integrable.of_bound hc.aestronglyMeasurable _ (ae_of_all _ (fun x => by + simpa only [Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw0 x)] using + (hwn x).trans (hgb x))) + have hm : (∫ x, (w ∘ circle) x ∂μ₁) ≤ 1 := by + simp only [Function.comp_apply, ← integral_map (f:=w) + measurable_circle.aemeasurable hw.aestronglyMeasurable, map_circle_volume] + exact (integral_mono hwi hgi.norm hwn).trans hgm + obtain ⟨G,hG,hsize,hInt⟩ := hj (w ∘ circle) (hw.comp measurable_circle) + (fun x => congrArg w (periodic_circle x)) + (fun x => hw0 _) hm (fun x => (hwn _).trans (hgb _)) + exact ⟨cost_transfer _ hc j C G hG hsize hInt, hwci⟩ + have hdiff (w : Torus → ℝ) (hw : Measurable w) (hwn : ∀ x, |w x| ≤ ‖g x‖) : + HasMaxCost (fun x => (w x : ℂ)) j (C+C) ∧ + Integrable (fun x => (w x : ℂ)) circleMeasure := by + obtain ⟨hp, hpi⟩ := hpart (fun x => max 0 (w x)) (measurable_const.max hw) (fun x => le_max_left _ _) + (fun x => max_le (norm_nonneg _) ((abs_le.mp (hwn x)).2)) + obtain ⟨hn, hni⟩ := hpart (fun x => max 0 (-w x)) (measurable_const.max hw.neg) + (fun x => le_max_left _ _) (fun x => max_le (norm_nonneg _) (by have := (abs_le.mp (hwn x)).1; linarith)) + rw [show (fun x => (w x : ℂ)) = (fun x => ((max 0 (w x) : ℝ) : ℂ)) + + (fun x => (-1:ℂ) * ((max 0 (-w x) : ℝ) : ℂ)) by + ext1 x + rcases le_total 0 (w x) with hwx| hwx + · simp [hwx] + · simp [hwx]] + exact ⟨hp.add (hn.rot _ (by simp)) hpi (hni.const_mul _) hC hC, hpi.add (hni.const_mul _)⟩ + obtain ⟨hr, hri⟩ := hdiff (fun x => (g x).re) + (Complex.continuous_re.measurable.comp hg) (fun x => Complex.abs_re_le_norm _) + obtain ⟨hi, hii⟩ := hdiff (fun x => (g x).im) + (Complex.continuous_im.measurable.comp hg) (fun x => Complex.abs_im_le_norm _) + have H : HasMaxCost g j (C+C+(C+C)) := by + have H := hr.add (hi.rot Complex.I (by simp)) hri (hii.const_mul Complex.I) + (by linarith) (by linarith) + convert H using 1 + ext1 x + rw [Pi.add_apply, mul_comm, Complex.re_add_im] + have eq : symmetricMaximal f = symmetricMaximal g := by + ext1 x + simp only [symmetricMaximal, symmetricPartialSum_congr_ae hfg] + rw [eq] + exact H + +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Dirichlet.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Dirichlet.lean index 7897e4a24a8d92becfad54c187eec5bf2c3efbe3..75cc898c77959bb7fb3be681e2b06879dbaa049a 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Dirichlet.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Dirichlet.lean @@ -1,203 +1,203 @@ -import OAI.Analysis.FourierLLogL.Fourier.Scale -/-! # Comparing the Fourier projections to the averaged Haar kernel -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def centerWin : Set ℝ := Ico (-1/2) (1/2) - -def ang (u t : ℝ) : ℝ := u * (2*π*t) - -lemma phase_trig (u t : ℝ) : - phase u t = ↑(Real.cos (ang u t)) + ↑(Real.sin (ang u t)) * Complex.I := by - unfold phase ang - rw [mul_comm Complex.I, Complex.exp_mul_I, Complex.ofReal_cos, Complex.ofReal_sin] - -lemma phase_sub (u x y : ℝ) : - phase u (x-y) = phase u x * phase (-u) y := by - rw [sub_eq_add_neg, phase_add] - unfold phase - simp - -def dirichlet (N : ℕ) (t : ℝ) : ℂ := - ∑ k ∈ Finset.Icc (-(N:ℤ)) (N:ℤ), phase k t - -lemma dirichlet_cont (N : ℕ) : Continuous (dirichlet N) := by - apply continuous_finsetSum - exact fun _ _ => phase_cont _ - -lemma dirichlet_bound (N : ℕ) (t : ℝ) : - ‖dirichlet N t‖ ≤ 2*(N:ℝ)+1 := by - refine (norm_sum_le _ _).trans ?_ - simp only [phase_unimod, Finset.sum_const, nsmul_eq_mul, mul_one, Int.card_Icc] - exact_mod_cast (by omega : ((N:ℤ)+1-(-(N:ℤ))).toNat ≤ 2*N+1) - -lemma dirichlet_succ (N : ℕ) (t : ℝ) : - dirichlet (N+1) t = dirichlet N t + ↑(2 * Real.cos (ang (N+1:ℕ) t)) := by - unfold dirichlet - let M : ℤ := N - have heq : Finset.Icc (-((N+1:ℕ):ℤ)) ((N+1:ℕ):ℤ) = - insert (M+1) (insert (-M-1) (Finset.Icc (-M) M)) := by - ext i - simp only [Finset.mem_insert, Finset.mem_Icc] - dsimp [M]; omega - have h1 : M+1 ∉ insert (-M-1) (Finset.Icc (-M) M) := by - simp only [Finset.mem_insert, Finset.mem_Icc] - dsimp [M]; omega - have h2 : -M-1 ∉ Finset.Icc (-M) M := by - simp only [Finset.mem_Icc]; omega - rw [heq, Finset.sum_insert h1, Finset.sum_insert h2] - have h3 : ang ((↑(-M-1) : ℝ)) t = -ang (N+1:ℕ) t := by unfold M ang; push_cast; ring - have h4 : ((↑(M+1):ℝ)) = ((N+1:ℕ):ℝ) := by simp [M] - rw [phase_trig, phase_trig, h3, h4, Real.sin_neg, Real.cos_neg, Complex.ofReal_neg, - Complex.ofReal_mul, Complex.ofReal_ofNat] - dsimp only [M] - ring - -lemma dirichlet_sin (N : ℕ) (t : ℝ) : - dirichlet N t * ↑(Real.sin (π*t)) = ↑(Real.sin (ang N t + π*t)) := by - induction N with - | zero => simp [dirichlet, phase, ang] - | succ n ih => - rw [dirichlet_succ, add_mul, ih, ← Complex.ofReal_mul, ← Complex.ofReal_add] - congr 1 - have H : ang n t + π*t = ang (n+1:ℕ) t - π*t := by - unfold ang; push_cast; ring - rw [H, Real.sin_sub, Real.sin_add] - ring - -lemma sin_posabs {q : ℝ} (hq : |q| ≤ π/2) : - |q|/2 ≤ |Real.sin q| := by - have H := Real.mul_abs_le_abs_sin hq - apply le_trans _ H - have hc : 1/2 ≤ 2 / (π:ℝ) := (le_div_iff₀ Real.pi_pos).mpr (by linarith [Real.pi_lt_four]) - nlinarith [abs_nonneg q] - -lemma dirichlet_id (N : ℕ) {t : ℝ} (ht : t ∈ centerWin) (hn : t ≠ 0) : - dirichlet N t = ↑(Real.cos (ang N t) + - Real.sin (ang N t) * (Real.cos (π*t) / Real.sin (π*t))) := by - have hh : |π*t| ≤ π/2 := by - rcases ht with ⟨h1,h2⟩ - rw [abs_le]; constructor <;> nlinarith [Real.pi_pos] - have hsin : Real.sin (π*t) ≠ 0 := by - have H1 := abs_pos.mpr (mul_ne_zero Real.pi_pos.ne' hn) - have H2 := sin_posabs hh - intro h - rw [h,abs_zero] at H2 - linarith - have H := dirichlet_sin N t - apply mul_right_cancel₀ (by exact_mod_cast hsin : (↑(Real.sin (π*t)) : ℂ) ≠ 0) - rw [H, ← Complex.ofReal_mul, Real.sin_add]; congr 1; field_simp; ring - -lemma cot_error {q : ℝ} (hq : |q| ≤ π/2) (hn : q ≠ 0) : - |Real.cos q / Real.sin q - 1/q| ≤ 4 := by - have hk := sin_posabs hq - have hp : 0 < |q| := abs_pos.mpr hn - have hs' : 0 < |Real.sin q| := by linarith - have hs : Real.sin q ≠ 0 := abs_pos.mp hs' - have h2 : |q| ≤ 2 := by linarith [Real.pi_lt_four] - have hcos : |Real.cos q - 1| ≤ |q|^2 / 2 := by - rw [abs_le, sq_abs] - constructor - · linarith [@Real.one_sub_sq_div_two_le_cos q] - · nlinarith [Real.cos_le_one q] - have H : |q * Real.cos q - Real.sin q| ≤ |q|^3 := by - rw [show q * Real.cos q - Real.sin q = q * (Real.cos q - 1) + (q-Real.sin q) by ring] - apply (abs_add_le _ _).trans - rw [abs_mul] - calc - _ ≤ |q| * (|q|^2/2) + |q|^3/6 := add_le_add - (mul_le_mul_of_nonneg_left hcos hp.le) (Real.abs_sub_sin_le q) - _ ≤ _ := by nlinarith [pow_pos hp 3] - rw [show Real.cos q / Real.sin q - 1/q = (q*Real.cos q - Real.sin q)/(q*Real.sin q) by field_simp, - abs_div, abs_mul] - apply (div_le_iff₀ (mul_pos hp hs')).mpr - exact H.trans (by nlinarith [mul_le_mul_of_nonneg_right h2 (sq_nonneg |q|)]) - -def κfactor := 1 / (π * constK) - -lemma κfactor_bound : |κfactor| ≤ 4 := by - unfold κfactor constK - rw [abs_div, abs_mul, abs_of_pos Real.pi_pos] - rw [div_le_iff₀ (by positivity)] - norm_num - linarith [Real.two_le_pi] - -/-- Dirichlet profile minus rescaled approximate Hilbert profile modulated by sine. -/ -def diffK (δ : ℝ) (N : ℕ) (t : ℝ) : ℂ := - centerWin.indicator (dirichlet N) t - - ↑(κfactor * kernel δ t * Real.sin (ang N t)) - -theorem diffK_major {t δ : ℝ} (hd : 0 < δ) (hd' : δ ≤ 1) - (ht : t ∈ window) (N : ℕ) : - ‖diffK δ N t‖ ≤ if |t| < δ then 70 * ((N:ℝ) + 1) else 20 := by - have hs : |Real.sin (ang N t)| ≤ 1 := Real.abs_sin_le_one _ - have hcc := κfactor_bound - have htw : |t| ≤ 1 := abs_le.mpr ⟨ht.1, ht.2.le⟩ - split_ifs with H - · apply (norm_sub_le _ _).trans - have h1 : ‖centerWin.indicator (dirichlet N) t‖ ≤ 2*(N:ℝ)+1 := by - by_cases h : t ∈ centerWin - · simpa [h] using dirichlet_bound N t - · simp only [indicator_of_notMem h, norm_zero]; positivity - rw [Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_mul] - have h2 : |Real.sin (ang N t)| ≤ (N:ℝ)*(8*δ) := by - apply Real.abs_sin_le_abs.trans - simp only [ang, abs_mul, abs_of_pos Real.pi_pos] - norm_num - gcongr - linarith [Real.pi_lt_four] - calc - _ ≤ 2*(N:ℝ)+1+(4*(2/δ))*((N:ℝ)*(8*δ)) := - add_le_add h1 (mul_le_mul (mul_le_mul hcc (kernel_small hd hd' t) - (abs_nonneg _) (by norm_num)) h2 (abs_nonneg _) (by positivity)) - _ ≤ _ := by field_simp; nlinarith - · have htpos : δ ≤ |t| := le_of_not_gt H - have hn : t ≠ 0 := abs_pos.mp (hd.trans_le htpos) - have hk := kernel_middle hd htpos htw - by_cases hc : t ∈ centerWin - · have hθ : |π*t| ≤ π/2 := by - rcases hc with ⟨h1,h2⟩ - rw [abs_le]; constructor <;> nlinarith [Real.pi_pos] - have hA : |Real.cos (π*t) / Real.sin (π*t) - κfactor * kernel δ t| ≤ 4 + 4 * 2 := by - rw [show Real.cos (π*t) / Real.sin (π*t) - κfactor * kernel δ t = - (Real.cos (π*t)/Real.sin (π*t)-1/(π*t)) + (-κfactor)*(kernel δ t-constK/t) by - unfold κfactor constK - have := Real.pi_pos - field_simp - all_goals ring] - apply (abs_add_le _ _).trans - rw [abs_mul, abs_neg] - exact add_le_add (cot_error hθ (mul_ne_zero Real.pi_pos.ne' hn)) - (mul_le_mul hcc hk (abs_nonneg _) (by norm_num)) - unfold diffK - rw [indicator_of_mem hc, dirichlet_id _ hc hn, ← Complex.ofReal_sub, Complex.norm_real, - Real.norm_eq_abs] - rw [show Real.cos (ang N t) + Real.sin (ang N t) * (Real.cos (π*t) / Real.sin (π*t)) - - κfactor*kernel δ t*Real.sin (ang N t) = Real.cos (ang N t) + - Real.sin (ang N t)*(Real.cos (π*t)/Real.sin (π*t)-κfactor*kernel δ t) by ring] - apply (abs_add_le _ _).trans - rw [abs_mul] - refine (add_le_add (Real.abs_cos_le_one _) (mul_le_mul hs hA (abs_nonneg _) zero_le_one)).trans ?_ - norm_num - · have htl : 1/2 ≤ |t| := by unfold centerWin at hc; grind - unfold diffK - rw [indicator_of_notMem hc, zero_sub, norm_neg, Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_mul] - have he : |constK/t| ≤ 1 := by - rw [abs_div, div_le_one (abs_pos.mpr hn)] - unfold constK - norm_num - linarith - have hQ : |kernel δ t| ≤ 3 := by rw [abs_le] at *; grind - exact (mul_le_mul (mul_le_mul hcc hQ (abs_nonneg _) (by norm_num)) hs - (abs_nonneg _) (by norm_num)).trans (by norm_num) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Scale +/-! # Comparing the Fourier projections to the averaged Haar kernel -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def centerWin : Set ℝ := Ico (-1/2) (1/2) + +def ang (u t : ℝ) : ℝ := u * (2*π*t) + +lemma phase_trig (u t : ℝ) : + phase u t = ↑(Real.cos (ang u t)) + ↑(Real.sin (ang u t)) * Complex.I := by + unfold phase ang + rw [mul_comm Complex.I, Complex.exp_mul_I, Complex.ofReal_cos, Complex.ofReal_sin] + +lemma phase_sub (u x y : ℝ) : + phase u (x-y) = phase u x * phase (-u) y := by + rw [sub_eq_add_neg, phase_add] + unfold phase + simp + +def dirichlet (N : ℕ) (t : ℝ) : ℂ := + ∑ k ∈ Finset.Icc (-(N:ℤ)) (N:ℤ), phase k t + +lemma dirichlet_cont (N : ℕ) : Continuous (dirichlet N) := by + apply continuous_finsetSum + exact fun _ _ => phase_cont _ + +lemma dirichlet_bound (N : ℕ) (t : ℝ) : + ‖dirichlet N t‖ ≤ 2*(N:ℝ)+1 := by + refine (norm_sum_le _ _).trans ?_ + simp only [phase_unimod, Finset.sum_const, nsmul_eq_mul, mul_one, Int.card_Icc] + exact_mod_cast (by omega : ((N:ℤ)+1-(-(N:ℤ))).toNat ≤ 2*N+1) + +lemma dirichlet_succ (N : ℕ) (t : ℝ) : + dirichlet (N+1) t = dirichlet N t + ↑(2 * Real.cos (ang (N+1:ℕ) t)) := by + unfold dirichlet + let M : ℤ := N + have heq : Finset.Icc (-((N+1:ℕ):ℤ)) ((N+1:ℕ):ℤ) = + insert (M+1) (insert (-M-1) (Finset.Icc (-M) M)) := by + ext i + simp only [Finset.mem_insert, Finset.mem_Icc] + dsimp [M]; omega + have h1 : M+1 ∉ insert (-M-1) (Finset.Icc (-M) M) := by + simp only [Finset.mem_insert, Finset.mem_Icc] + dsimp [M]; omega + have h2 : -M-1 ∉ Finset.Icc (-M) M := by + simp only [Finset.mem_Icc]; omega + rw [heq, Finset.sum_insert h1, Finset.sum_insert h2] + have h3 : ang ((↑(-M-1) : ℝ)) t = -ang (N+1:ℕ) t := by unfold M ang; push_cast; ring + have h4 : ((↑(M+1):ℝ)) = ((N+1:ℕ):ℝ) := by simp [M] + rw [phase_trig, phase_trig, h3, h4, Real.sin_neg, Real.cos_neg, Complex.ofReal_neg, + Complex.ofReal_mul, Complex.ofReal_ofNat] + dsimp only [M] + ring + +lemma dirichlet_sin (N : ℕ) (t : ℝ) : + dirichlet N t * ↑(Real.sin (π*t)) = ↑(Real.sin (ang N t + π*t)) := by + induction N with + | zero => simp [dirichlet, phase, ang] + | succ n ih => + rw [dirichlet_succ, add_mul, ih, ← Complex.ofReal_mul, ← Complex.ofReal_add] + congr 1 + have H : ang n t + π*t = ang (n+1:ℕ) t - π*t := by + unfold ang; push_cast; ring + rw [H, Real.sin_sub, Real.sin_add] + ring + +lemma sin_posabs {q : ℝ} (hq : |q| ≤ π/2) : + |q|/2 ≤ |Real.sin q| := by + have H := Real.mul_abs_le_abs_sin hq + apply le_trans _ H + have hc : 1/2 ≤ 2 / (π:ℝ) := (le_div_iff₀ Real.pi_pos).mpr (by linarith [Real.pi_lt_four]) + nlinarith [abs_nonneg q] + +lemma dirichlet_id (N : ℕ) {t : ℝ} (ht : t ∈ centerWin) (hn : t ≠ 0) : + dirichlet N t = ↑(Real.cos (ang N t) + + Real.sin (ang N t) * (Real.cos (π*t) / Real.sin (π*t))) := by + have hh : |π*t| ≤ π/2 := by + rcases ht with ⟨h1,h2⟩ + rw [abs_le]; constructor <;> nlinarith [Real.pi_pos] + have hsin : Real.sin (π*t) ≠ 0 := by + have H1 := abs_pos.mpr (mul_ne_zero Real.pi_pos.ne' hn) + have H2 := sin_posabs hh + intro h + rw [h,abs_zero] at H2 + linarith + have H := dirichlet_sin N t + apply mul_right_cancel₀ (by exact_mod_cast hsin : (↑(Real.sin (π*t)) : ℂ) ≠ 0) + rw [H, ← Complex.ofReal_mul, Real.sin_add]; congr 1; field_simp; ring + +lemma cot_error {q : ℝ} (hq : |q| ≤ π/2) (hn : q ≠ 0) : + |Real.cos q / Real.sin q - 1/q| ≤ 4 := by + have hk := sin_posabs hq + have hp : 0 < |q| := abs_pos.mpr hn + have hs' : 0 < |Real.sin q| := by linarith + have hs : Real.sin q ≠ 0 := abs_pos.mp hs' + have h2 : |q| ≤ 2 := by linarith [Real.pi_lt_four] + have hcos : |Real.cos q - 1| ≤ |q|^2 / 2 := by + rw [abs_le, sq_abs] + constructor + · linarith [@Real.one_sub_sq_div_two_le_cos q] + · nlinarith [Real.cos_le_one q] + have H : |q * Real.cos q - Real.sin q| ≤ |q|^3 := by + rw [show q * Real.cos q - Real.sin q = q * (Real.cos q - 1) + (q-Real.sin q) by ring] + apply (abs_add_le _ _).trans + rw [abs_mul] + calc + _ ≤ |q| * (|q|^2/2) + |q|^3/6 := add_le_add + (mul_le_mul_of_nonneg_left hcos hp.le) (Real.abs_sub_sin_le q) + _ ≤ _ := by nlinarith [pow_pos hp 3] + rw [show Real.cos q / Real.sin q - 1/q = (q*Real.cos q - Real.sin q)/(q*Real.sin q) by field_simp, + abs_div, abs_mul] + apply (div_le_iff₀ (mul_pos hp hs')).mpr + exact H.trans (by nlinarith [mul_le_mul_of_nonneg_right h2 (sq_nonneg |q|)]) + +def κfactor := 1 / (π * constK) + +lemma κfactor_bound : |κfactor| ≤ 4 := by + unfold κfactor constK + rw [abs_div, abs_mul, abs_of_pos Real.pi_pos] + rw [div_le_iff₀ (by positivity)] + norm_num + linarith [Real.two_le_pi] + +/-- Dirichlet profile minus rescaled approximate Hilbert profile modulated by sine. -/ +def diffK (δ : ℝ) (N : ℕ) (t : ℝ) : ℂ := + centerWin.indicator (dirichlet N) t - + ↑(κfactor * kernel δ t * Real.sin (ang N t)) + +theorem diffK_major {t δ : ℝ} (hd : 0 < δ) (hd' : δ ≤ 1) + (ht : t ∈ window) (N : ℕ) : + ‖diffK δ N t‖ ≤ if |t| < δ then 70 * ((N:ℝ) + 1) else 20 := by + have hs : |Real.sin (ang N t)| ≤ 1 := Real.abs_sin_le_one _ + have hcc := κfactor_bound + have htw : |t| ≤ 1 := abs_le.mpr ⟨ht.1, ht.2.le⟩ + split_ifs with H + · apply (norm_sub_le _ _).trans + have h1 : ‖centerWin.indicator (dirichlet N) t‖ ≤ 2*(N:ℝ)+1 := by + by_cases h : t ∈ centerWin + · simpa [h] using dirichlet_bound N t + · simp only [indicator_of_notMem h, norm_zero]; positivity + rw [Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_mul] + have h2 : |Real.sin (ang N t)| ≤ (N:ℝ)*(8*δ) := by + apply Real.abs_sin_le_abs.trans + simp only [ang, abs_mul, abs_of_pos Real.pi_pos] + norm_num + gcongr + linarith [Real.pi_lt_four] + calc + _ ≤ 2*(N:ℝ)+1+(4*(2/δ))*((N:ℝ)*(8*δ)) := + add_le_add h1 (mul_le_mul (mul_le_mul hcc (kernel_small hd hd' t) + (abs_nonneg _) (by norm_num)) h2 (abs_nonneg _) (by positivity)) + _ ≤ _ := by field_simp; nlinarith + · have htpos : δ ≤ |t| := le_of_not_gt H + have hn : t ≠ 0 := abs_pos.mp (hd.trans_le htpos) + have hk := kernel_middle hd htpos htw + by_cases hc : t ∈ centerWin + · have hθ : |π*t| ≤ π/2 := by + rcases hc with ⟨h1,h2⟩ + rw [abs_le]; constructor <;> nlinarith [Real.pi_pos] + have hA : |Real.cos (π*t) / Real.sin (π*t) - κfactor * kernel δ t| ≤ 4 + 4 * 2 := by + rw [show Real.cos (π*t) / Real.sin (π*t) - κfactor * kernel δ t = + (Real.cos (π*t)/Real.sin (π*t)-1/(π*t)) + (-κfactor)*(kernel δ t-constK/t) by + unfold κfactor constK + have := Real.pi_pos + field_simp + all_goals ring] + apply (abs_add_le _ _).trans + rw [abs_mul, abs_neg] + exact add_le_add (cot_error hθ (mul_ne_zero Real.pi_pos.ne' hn)) + (mul_le_mul hcc hk (abs_nonneg _) (by norm_num)) + unfold diffK + rw [indicator_of_mem hc, dirichlet_id _ hc hn, ← Complex.ofReal_sub, Complex.norm_real, + Real.norm_eq_abs] + rw [show Real.cos (ang N t) + Real.sin (ang N t) * (Real.cos (π*t) / Real.sin (π*t)) - + κfactor*kernel δ t*Real.sin (ang N t) = Real.cos (ang N t) + + Real.sin (ang N t)*(Real.cos (π*t)/Real.sin (π*t)-κfactor*kernel δ t) by ring] + apply (abs_add_le _ _).trans + rw [abs_mul] + refine (add_le_add (Real.abs_cos_le_one _) (mul_le_mul hs hA (abs_nonneg _) zero_le_one)).trans ?_ + norm_num + · have htl : 1/2 ≤ |t| := by unfold centerWin at hc; grind + unfold diffK + rw [indicator_of_notMem hc, zero_sub, norm_neg, Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_mul] + have he : |constK/t| ≤ 1 := by + rw [abs_div, div_le_one (abs_pos.mpr hn)] + unfold constK + norm_num + linarith + have hQ : |kernel δ t| ≤ 3 := by rw [abs_le] at *; grind + exact (mul_le_mul (mul_le_mul hcc hQ (abs_nonneg _) (by norm_num)) hs + (abs_nonneg _) (by norm_num)).trans (by norm_num) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Endpoint.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Endpoint.lean index c2033128850ac3a5aab3b8835c0045f06de018e4..f99c10bb937cacb0c9b8fc376a73ecc22b6ffc3b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Endpoint.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Endpoint.lean @@ -1,365 +1,365 @@ -import OAI.Analysis.Fourier.HeightLayers - -/-! # Deduction of the L log L theorem from a good set estimate -/ - -noncomputable section - -open MeasureTheory Filter -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL - -def symmetricMaximal (f : Torus → ℂ) (x : Torus) : ℝ≥0∞ := - ⨆ N : ℕ, ENNReal.ofReal ‖symmetricPartialSum f N x‖ - -theorem measurable_symmetricMaximal (f : Torus → ℂ) : Measurable (symmetricMaximal f) := - Measurable.iSup (fun N => (measurable_symmetricPartialSum f N).norm.ennreal_ofReal) - -theorem symmetricMaximal_mul (f : Torus → ℂ) (c : ℂ) (x : Torus) : - symmetricMaximal (fun y => c * f y) x = ENNReal.ofReal ‖c‖ * symmetricMaximal f x := by - simp only [symmetricMaximal, symmetricPartialSum_const_mul, norm_mul, - ENNReal.ofReal_mul (norm_nonneg c), ENNReal.mul_iSup] - -/-- Good-set estimate. We state it at exponential heights exp d with d = 2^j. -/ -def GoodBound : Prop := - ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (f : Torus → ℂ), - Integrable f circleMeasure → (∫ x, ‖f x‖ ∂circleMeasure) ≤ 1 → - (∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ Real.exp ((2 : ℝ)^j)) → - ∃ G : Set Torus, MeasurableSet G ∧ - circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ - (∫⁻ x in G, symmetricMaximal f x ∂circleMeasure) ≤ ENNReal.ofReal (C * (2:ℝ)^j) - -/-- Transfer the good-set estimates to summable pointwise majorants for a sequence. -Finitely many terms per point may not have been bounded. We include the untransformed term -for use with approximations. -/ -theorem GoodBound.ae_tail_majorant - (hgb : GoodBound) (F : ℕ → Torus → ℂ) (w : ℕ → ℝ) (L : ℕ) - (hF : ∀ j, Integrable (F j) circleMeasure) - (hw : ∀ j, 0 < w j) - (hsum : Summable (fun j => (2:ℝ)^j * w j)) - (hm : ∀ j, (∫ x, ‖F j x‖ ∂circleMeasure) ≤ w j) - (hh : ∀ j, ∀ᵐ x ∂circleMeasure, ‖F j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+L))) : - ∀ᵐ x ∂circleMeasure, ∃ b : ℕ → ℝ, Summable b ∧ ∀ᶠ j : ℕ in atTop, - ∀ N, ‖symmetricPartialSum (F j) N x‖ + ‖F j x‖ ≤ b j := by - classical - obtain ⟨C, hC, hG⟩ := hgb - obtain ⟨j₀, hj₀⟩ := eventually_atTop.1 hG - let D := L + j₀ - have hg (j : ℕ) : ∃ G : Set Torus, MeasurableSet G ∧ - circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ - (∫⁻ x in G, symmetricMaximal (F j) x ∂circleMeasure) - ≤ ENNReal.ofReal (w j * (C * (2:ℝ)^(j+D))) := by - let f (x : Torus) : ℂ := (((w j)⁻¹ : ℝ) : ℂ) * F j x - have winv : 0 < (w j)⁻¹ := inv_pos.mpr (hw j) - have hf (x : Torus) : ‖f x‖ = (w j)⁻¹ * ‖F j x‖ := by - simp only [f, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_pos winv] - have hnorm : (∫ x, ‖f x‖ ∂circleMeasure) ≤ 1 := by - simp_rw [hf] - rw [integral_const_mul] - exact (mul_le_mul_of_nonneg_left (hm j) winv.le).trans (le_of_eq (inv_mul_cancel₀ (hw j).ne')) - have hheight : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ Real.exp ((2:ℝ)^(j+D)) := by - filter_upwards [hh j] with x hx - calc - ‖f x‖ = (w j)⁻¹ * ‖F j x‖ := hf x - _ ≤ (w j)⁻¹ * (w j * Real.exp ((2:ℝ)^(j+L))) := - mul_le_mul_of_nonneg_left hx winv.le - _ = Real.exp ((2:ℝ)^(j+L)) := by field_simp [(hw j).ne'] - _ ≤ Real.exp ((2:ℝ)^(j+D)) := Real.exp_monotone - (pow_le_pow_right₀ (by norm_num) (by dsimp [D]; omega)) - obtain ⟨G, hs, hsize, hi⟩ := hj₀ (j+D) (by dsimp [D]; omega) f ((hF j).const_mul _) hnorm - hheight - refine ⟨G, hs, hsize.trans (ENNReal.ofReal_le_ofReal ?_), ?_⟩ - · apply div_le_div_of_nonneg_left hC (by positivity) - gcongr - simp - have heq : F j = (fun x => ((w j : ℝ) : ℂ) * f x) := by - ext1 x - dsimp [f] - rw [← mul_assoc, ← Complex.ofReal_mul, mul_inv_cancel₀ (hw j).ne', - Complex.ofReal_one, one_mul] - rw [heq] - simp_rw [symmetricMaximal_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_pos (hw j)] - rw [lintegral_const_mul _ (measurable_symmetricMaximal f), ENNReal.ofReal_mul (hw j).le] - exact mul_le_mul_right hi _ - choose G hGm hGsize hGi using hg - have hCs : Summable (fun j : ℕ => C / ((j:ℝ)+1)^2) := by - have hh : Summable (fun n : ℕ => 1 / ((n+1:ℕ):ℝ)^2) := - (summable_nat_add_iff 1).mpr (Real.summable_one_div_nat_pow.mpr (by norm_num)) - simpa [div_eq_mul_inv] using hh.mul_left C - have hcompl : (∑' j, circleMeasure (G j)ᶜ) ≠ ∞ := by - apply ne_of_lt - refine (ENNReal.tsum_le_tsum hGsize).trans_lt ?_ - rw [← ENNReal.ofReal_tsum_of_nonneg _ hCs] - · exact ENNReal.ofReal_lt_top - · intro n; positivity - let M (j : ℕ) (x : Torus) := - (G j).indicator (symmetricMaximal (F j)) x + ENNReal.ofReal ‖F j x‖ - have hM (j : ℕ) : AEMeasurable (M j) circleMeasure := - (((measurable_symmetricMaximal _).indicator (hGm _)).aemeasurable).add - ((hF j).norm.aestronglyMeasurable.aemeasurable.ennreal_ofReal) - have ht : ∫⁻ x, (∑' j, M j x) ∂circleMeasure ≠ ∞ := by - have hhM (j : ℕ) : (∫⁻ x, M j x ∂circleMeasure) - ≤ ENNReal.ofReal (w j * (C * (2:ℝ)^(j+D)) + w j) := by - dsimp only [M] - rw [lintegral_add_left ((measurable_symmetricMaximal _).indicator (hGm _)), - lintegral_indicator (hGm j), - ← ofReal_integral_eq_lintegral_ofReal (hF j).norm - (ae_of_all _ fun x => norm_nonneg _), - ENNReal.ofReal_add (by have := hw j; positivity) (hw j).le] - exact add_le_add (hGi j) (ENNReal.ofReal_le_ofReal (hm j)) - rw [lintegral_tsum hM] - apply ne_of_lt - refine (ENNReal.tsum_le_tsum hhM).trans_lt ?_ - have hwS : Summable w := hsum.of_nonneg_of_le (fun n => (hw n).le) (fun n => - le_mul_of_one_le_left (hw n).le (one_le_pow₀ (by norm_num : (1:ℝ) ≤ 2))) - have hfull : Summable (fun j => w j * (C * (2:ℝ)^(j+D)) + w j) := - ((hsum.mul_left (C*(2:ℝ)^D)).add hwS).congr fun n => by - simp only [pow_add] - ring - rw [← ENNReal.ofReal_tsum_of_nonneg _ hfull] - · exact ENNReal.ofReal_lt_top - · intro n; have := hw n; positivity - filter_upwards [ae_eventually_notMem hcompl, ae_lt_top' (AEMeasurable.tsum hM) ht] with x hx hx' - refine ⟨fun n => (M n x).toReal, ENNReal.summable_toReal hx'.ne, ?_⟩ - filter_upwards [hx] with j hj - intro N - have hmn := ENNReal.ne_top_of_tsum_ne_top hx'.ne j - rw [← ENNReal.toReal_ofReal - (add_nonneg (norm_nonneg (symmetricPartialSum _ _ _)) (norm_nonneg _))] - apply ENNReal.toReal_mono hmn - simp only [M, ENNReal.ofReal_add (norm_nonneg _) (norm_nonneg _)] - have : x ∈ G j := by simpa using hj - rw [Set.indicator_of_mem this] - exact add_le_add_left (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum (F j) N x‖) N) _ - -/-- Trigonometric polynomials, as a subspace of continuous functions. -/ -def trigPolys : Submodule ℂ C(Torus, ℂ) := Submodule.span ℂ (Set.range fourier) - -lemma integrable_continuous {f : Torus → ℂ} (hf : Continuous f) : - Integrable f circleMeasure := by - apply hf.integrable_of_hasCompactSupport - exact IsClosed.isCompact (isClosed_tsupport f) - -theorem eventually_partialSum_trigPoly {p : C(Torus, ℂ)} (h : p ∈ trigPolys) : - ∀ᶠ N : ℕ in atTop, ∀ x, symmetricPartialSum p N x = p x := by - induction h using Submodule.span_induction with - | mem p hp => - obtain ⟨k, rfl⟩ := hp - filter_upwards [eventually_ge_atTop k.natAbs] with N hN - intro x - simp only [symmetricPartialSum, fourierCoeff_fourier] - rw [Finset.sum_eq_single k] - · simp - · intro b _ hb - simp [hb] - · intro hk - exfalso - apply hk - simp only [Finset.mem_Icc] - omega - | zero => filter_upwards with N x using symmetricPartialSum_zero N x - | add p q _ _ hp hq => - filter_upwards [hp, hq] with N hpN hqN - intro x - change symmetricPartialSum ((p : Torus → ℂ) + (q : Torus → ℂ)) N x = p x + q x - rw [symmetricPartialSum_add (integrable_continuous p.continuous) - (integrable_continuous q.continuous), hpN x, hqN x] - | smul c p _ hp => - filter_upwards [hp] with N hpN - intro x - change symmetricPartialSum (fun y => c * p y) N x = c * p x - rw [symmetricPartialSum_const_mul, hpN x] - -lemma exists_bounded_trigPoly_approx (f : Torus → ℂ) - (hf : Integrable f circleMeasure) - (B : ℝ) (hB : 0 < B) (hb : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ B) - (ε : ℝ) (hε : 0 < ε) : - ∃ p : C(Torus, ℂ), p ∈ trigPolys ∧ - (∫ x, ‖f x - p x‖ ∂circleMeasure) ≤ ε ∧ ∀ x, ‖p x‖ ≤ B + 1 := by - obtain ⟨g, hg, hgi⟩ := - hf.exists_boundedContinuous_integral_sub_le (show 0 < ε/4 by linarith) - let k (x : Torus) : ℂ := (B / max B ‖g x‖) • g x - have hk : Continuous k := by - have h1 : Continuous fun x => max B ‖g x‖ := continuous_const.max g.continuous.norm - exact (continuous_const.div h1 (fun x => ne_of_gt (lt_max_of_lt_left hB))).smul g.continuous - let k' : C(Torus, ℂ) := ⟨k, hk⟩ - have hkB (x : Torus) : ‖k x‖ ≤ B := by - have hm : 0 < max B ‖g x‖ := lt_max_of_lt_left hB - simp only [k, norm_smul, Real.norm_eq_abs, abs_of_pos (div_pos hB hm)] - exact (mul_le_mul_of_nonneg_left (le_max_right B ‖g x‖) (div_pos hB hm).le).trans - (div_mul_cancel₀ B hm.ne').le - have hdk (x : Torus) (hx : ‖f x‖ ≤ B) : ‖f x - k x‖ ≤ 2 * ‖f x - g x‖ := by - suffices H : ‖g x - k x‖ ≤ ‖f x - g x‖ by - have hi := dist_triangle (f x) (g x) (k x) - simp only [dist_eq_norm] at hi - linarith - rcases le_total ‖g x‖ B with hxg | hxg - · simp [k, max_eq_left hxg, hB.ne'] - have hp : 0 < ‖g x‖ := hB.trans_le hxg - have hxone : (0:ℝ) ≤ 1 - B/‖g x‖ := sub_nonneg.mpr ((div_le_one hp).mpr hxg) - calc - ‖g x - k x‖ = ‖((1:ℝ)-B/‖g x‖) • g x‖ := by - rw [sub_smul, one_smul]; simp only [k, max_eq_right hxg] - _ = ‖g x‖ - B := by - rw [norm_smul, Real.norm_eq_abs, abs_of_nonneg hxone, sub_mul, one_mul, - div_mul_cancel₀ B hp.ne'] - _ ≤ ‖g x‖ - ‖f x‖ := sub_le_sub_left hx _ - _ ≤ ‖f x - g x‖ := by rw [norm_sub_rev]; exact norm_sub_norm_le _ _ - have hkc : k' ∈ closure (trigPolys : Set _) := by - change k' ∈ trigPolys.topologicalClosure - rw [trigPolys, span_fourier_closure_eq_top]; trivial - obtain ⟨p, hps, hp⟩ := Metric.mem_closure_iff.mp hkc (min 1 (ε / 2)) (by positivity) - have hp' (x : Torus) : ‖k x - p x‖ < min 1 (ε / 2) := by - change ‖k' x - p x‖ < _ - rw [← dist_eq_norm] - exact lt_of_le_of_lt (ContinuousMap.dist_apply_le_dist _) hp - refine ⟨p, hps, ?_, fun x => ?_⟩ - · calc - _ ≤ ∫ x, (2 * ‖f x - g x‖ + (ε / 2)) ∂circleMeasure := by - apply integral_mono_ae (hf.sub (integrable_continuous p.continuous)).norm - (((hf.sub hgi).norm.const_mul _).add (integrable_const _)) - filter_upwards [hb] with x hx - have h1 := dist_triangle (f x) (k x) (p x) - simp only [dist_eq_norm] at h1 - exact h1.trans (add_le_add (hdk x hx) - ((hp' x).le.trans (min_le_right _ _))) - _ = 2 * (∫ x, ‖f x - g x‖ ∂circleMeasure) + (ε/2) := by - rw [integral_add (show Integrable (fun x => 2 * ‖f x - g x‖) _ from ((hf.sub hgi).norm.const_mul _)) (integrable_const _), - integral_const_mul]; simp - _ ≤ ε := by linarith - · have h1 := norm_sub_norm_le (p x) (k x) - rw [norm_sub_rev] at h1 - have h2 := ((hp' x).le.trans (min_le_left _ _)) - linarith [hkB x] - - -private def pad (j : ℕ) : ℝ := ((1/4:ℝ))^j - -private lemma pad_pos (j) : 0 < pad j := by unfold pad; positivity - -private lemma summable_pad : Summable (fun j => (2:ℝ)^j * pad j) := by - simp only [pad, ← mul_pow] - exact summable_geometric_of_norm_lt_one (by norm_num) - - -private lemma pad_height (j : ℕ) : - Real.exp ((2:ℝ)^(j+2)) ≤ pad j * Real.exp ((2:ℝ)^(j+4)) := by - have hj : (j:ℝ) ≤ (2:ℝ)^j := by exact_mod_cast (Nat.lt_two_pow_self (n:=j)).le - have hex : (4:ℝ)^j ≤ Real.exp ((3:ℝ) * j) := by - have h3 := Real.add_one_le_exp 3 - rw [mul_comm 3, Real.exp_nat_mul] - exact pow_le_pow_left₀ (by norm_num) (by norm_num at h3; linarith) _ - have Hmul : (4:ℝ)^j * Real.exp ((2:ℝ)^(j+2)) ≤ Real.exp ((2:ℝ)^(j+4)) := by - refine (mul_le_mul_of_nonneg_right hex (Real.exp_nonneg _)).trans ?_ - rw [← Real.exp_add] - apply Real.exp_monotone - simp only [pow_add] - linarith - simp only [pad, one_div, inv_pow, ← div_eq_inv_mul] - apply (le_div_iff₀ (by positivity : (0:ℝ)<4^j)).mpr - rw [mul_comm] - exact Hmul - -theorem GoodBound.ae_convergence_bounded (hgb : GoodBound) (f : Torus → ℂ) - (hf : Integrable f circleMeasure) (B : ℝ) (hB : 0 < B) - (hb : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ B) : - ∀ᵐ x ∂circleMeasure, Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := by - choose p hp hm hpB using - (fun j => exists_bounded_trigPoly_approx f hf B hB hb (pad j) (pad_pos j)) - let F (j : ℕ) (x : Torus) : ℂ := f x - p j x - have hFi (j) : Integrable (F j) circleMeasure := - hf.sub (integrable_continuous (p j).continuous) - let w (j : ℕ) : ℝ := (2*B+1) * pad j - have hw (j) : 0 < w j := mul_pos (by linarith) (pad_pos _) - have hs : Summable fun j => (2:ℝ)^j * w j := - (summable_pad.mul_left (2*B+1)).congr fun j => by dsimp only [w]; ring - have hmw (j) : (∫ x, ‖F j x‖ ∂circleMeasure) ≤ w j := - (hm j).trans (le_mul_of_one_le_left (pad_pos j).le (by linarith)) - have hheight (j) : ∀ᵐ x ∂circleMeasure, ‖F j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by - filter_upwards [hb] with x hx - calc - ‖F j x‖ ≤ ‖f x‖ + ‖p j x‖ := norm_sub_le _ _ - _ ≤ (2*B+1)*Real.exp ((2:ℝ)^(j+2)) := by - have := hpB j x - have : 1 ≤ Real.exp ((2:ℝ)^(j+2)) := Real.one_le_exp (by positivity) - nlinarith - _ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by - dsimp only [w] - rw [mul_assoc] - exact mul_le_mul_of_nonneg_left (pad_height j) (by linarith) - filter_upwards [hgb.ae_tail_majorant F w 4 hFi hw hs hmw hheight] with x hx - obtain ⟨b, hsum, hmaj⟩ := hx - apply Metric.tendsto_nhds.mpr - intro ε hε - obtain ⟨j, hj, hj'⟩ := - (hmaj.and (hsum.tendsto_atTop_zero.eventually (gt_mem_nhds hε))).exists - filter_upwards [eventually_partialSum_trigPoly (hp j)] with N hN - have hfdecomp : f = F j + (p j : Torus → ℂ) := by ext; simp [F] - calc - dist (symmetricPartialSum f N x) (f x) - = ‖symmetricPartialSum (F j) N x - F j x‖ := by - nth_rw 1 2 [hfdecomp] - rw [symmetricPartialSum_add (hFi j) (integrable_continuous (p j).continuous), - hN x, dist_eq_norm] - simp only [Pi.add_apply, add_sub_add_right_eq_sub] - _ ≤ ‖symmetricPartialSum (F j) N x‖ + ‖F j x‖ := norm_sub_le _ _ - _ ≤ b j := hj N - _ < ε := hj' - -theorem GoodBound.main_bound_consequence (hgb : GoodBound) - {f : Torus → ℂ} (hf : MemLLogL f) : - ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, - Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := by - classical - let w (j : ℕ) := heightLayerMass f j + pad j - have hw (j : ℕ) : 0 < w j := - add_pos_of_nonneg_of_pos (heightLayerMass_nonneg f _) (pad_pos _) - have hs : Summable (fun j => (2:ℝ)^j * w j) := by - have hs' := hf.summable_weighted_heightLayerMass.mul_left ((1:ℝ)/2) - refine (hs'.add summable_pad).congr ?_ - intro j - dsimp only [w] - rw [pow_succ]; ring - have hm (j) : (∫ x, ‖heightLayer f j x‖ ∂circleMeasure) ≤ w j := by - rw [← heightLayerMass_eq_integral_norm hf] - apply le_add_of_nonneg_right - exact (pad_pos j).le - have hh (j) : ∀ᵐ x ∂circleMeasure, - ‖heightLayer f j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by - refine ae_of_all _ fun x => ((norm_heightLayer_le f j x).trans (pad_height j)).trans ?_ - exact mul_le_mul_of_nonneg_right - (le_add_of_nonneg_left (heightLayerMass_nonneg _ _)) (Real.exp_nonneg _) - have hbConv := hgb.ae_convergence_bounded _ hf.boundedPart_integrable _ - (Real.exp_pos _) (ae_of_all _ (norm_boundedPart_le f)) - have hConv (j) := hgb.ae_convergence_bounded _ (hf.heightLayer_integrable j) _ - (Real.exp_pos _) (ae_of_all _ (norm_heightLayer_le f j)) - apply exists_measurable_null_of_ae - filter_upwards [hbConv, ae_all_iff.mpr hConv, - hgb.ae_tail_majorant _ w 4 hf.heightLayer_integrable hw hs hm hh] with x hb hc hx - obtain ⟨b, hsum, hmaj⟩ := hx - obtain ⟨k, hk⟩ := eventually_atTop.1 hmaj - have hbd (j : ℕ) : BddAbove (Set.range - (fun N : ℕ => ‖symmetricPartialSum (heightLayer f j) N x‖)) := - ((hc j).norm).bddAbove_range - choose r hr using hbd - let b' (j : ℕ) := if j < k then r j else b j - have hs' : Summable b' := by - apply (summable_nat_add_iff k).mp - have hh := (summable_nat_add_iff k).mpr hsum - simpa [b'] using hh - have hmaj' (j N) : ‖symmetricPartialSum (heightLayer f j) N x‖ ≤ b' j := by - dsimp [b'] - split_ifs with H - · exact hr j (Set.mem_range_self N) - · exact (le_add_of_nonneg_right (norm_nonneg _)).trans (hk j (by omega) N) - have ht := tendsto_tsum_of_dominated_convergence hs' hc - (Eventually.of_forall fun N j => hmaj' j N) - have h := hb.add ht - - simpa only [hf.symmetricPartialSum_heightDecomposition, boundedPart_add_tsum_heightLayer] using h - -end FourierLLogL - -end OAI +import OAI.Analysis.Fourier.HeightLayers + +/-! # Deduction of the L log L theorem from a good set estimate -/ + +noncomputable section + +open MeasureTheory Filter +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL + +def symmetricMaximal (f : Torus → ℂ) (x : Torus) : ℝ≥0∞ := + ⨆ N : ℕ, ENNReal.ofReal ‖symmetricPartialSum f N x‖ + +theorem measurable_symmetricMaximal (f : Torus → ℂ) : Measurable (symmetricMaximal f) := + Measurable.iSup (fun N => (measurable_symmetricPartialSum f N).norm.ennreal_ofReal) + +theorem symmetricMaximal_mul (f : Torus → ℂ) (c : ℂ) (x : Torus) : + symmetricMaximal (fun y => c * f y) x = ENNReal.ofReal ‖c‖ * symmetricMaximal f x := by + simp only [symmetricMaximal, symmetricPartialSum_const_mul, norm_mul, + ENNReal.ofReal_mul (norm_nonneg c), ENNReal.mul_iSup] + +/-- Good-set estimate. We state it at exponential heights exp d with d = 2^j. -/ +def GoodBound : Prop := + ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (f : Torus → ℂ), + Integrable f circleMeasure → (∫ x, ‖f x‖ ∂circleMeasure) ≤ 1 → + (∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ Real.exp ((2 : ℝ)^j)) → + ∃ G : Set Torus, MeasurableSet G ∧ + circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ + (∫⁻ x in G, symmetricMaximal f x ∂circleMeasure) ≤ ENNReal.ofReal (C * (2:ℝ)^j) + +/-- Transfer the good-set estimates to summable pointwise majorants for a sequence. +Finitely many terms per point may not have been bounded. We include the untransformed term +for use with approximations. -/ +theorem GoodBound.ae_tail_majorant + (hgb : GoodBound) (F : ℕ → Torus → ℂ) (w : ℕ → ℝ) (L : ℕ) + (hF : ∀ j, Integrable (F j) circleMeasure) + (hw : ∀ j, 0 < w j) + (hsum : Summable (fun j => (2:ℝ)^j * w j)) + (hm : ∀ j, (∫ x, ‖F j x‖ ∂circleMeasure) ≤ w j) + (hh : ∀ j, ∀ᵐ x ∂circleMeasure, ‖F j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+L))) : + ∀ᵐ x ∂circleMeasure, ∃ b : ℕ → ℝ, Summable b ∧ ∀ᶠ j : ℕ in atTop, + ∀ N, ‖symmetricPartialSum (F j) N x‖ + ‖F j x‖ ≤ b j := by + classical + obtain ⟨C, hC, hG⟩ := hgb + obtain ⟨j₀, hj₀⟩ := eventually_atTop.1 hG + let D := L + j₀ + have hg (j : ℕ) : ∃ G : Set Torus, MeasurableSet G ∧ + circleMeasure Gᶜ ≤ ENNReal.ofReal (C / ((j:ℝ) + 1)^2) ∧ + (∫⁻ x in G, symmetricMaximal (F j) x ∂circleMeasure) + ≤ ENNReal.ofReal (w j * (C * (2:ℝ)^(j+D))) := by + let f (x : Torus) : ℂ := (((w j)⁻¹ : ℝ) : ℂ) * F j x + have winv : 0 < (w j)⁻¹ := inv_pos.mpr (hw j) + have hf (x : Torus) : ‖f x‖ = (w j)⁻¹ * ‖F j x‖ := by + simp only [f, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_pos winv] + have hnorm : (∫ x, ‖f x‖ ∂circleMeasure) ≤ 1 := by + simp_rw [hf] + rw [integral_const_mul] + exact (mul_le_mul_of_nonneg_left (hm j) winv.le).trans (le_of_eq (inv_mul_cancel₀ (hw j).ne')) + have hheight : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ Real.exp ((2:ℝ)^(j+D)) := by + filter_upwards [hh j] with x hx + calc + ‖f x‖ = (w j)⁻¹ * ‖F j x‖ := hf x + _ ≤ (w j)⁻¹ * (w j * Real.exp ((2:ℝ)^(j+L))) := + mul_le_mul_of_nonneg_left hx winv.le + _ = Real.exp ((2:ℝ)^(j+L)) := by field_simp [(hw j).ne'] + _ ≤ Real.exp ((2:ℝ)^(j+D)) := Real.exp_monotone + (pow_le_pow_right₀ (by norm_num) (by dsimp [D]; omega)) + obtain ⟨G, hs, hsize, hi⟩ := hj₀ (j+D) (by dsimp [D]; omega) f ((hF j).const_mul _) hnorm + hheight + refine ⟨G, hs, hsize.trans (ENNReal.ofReal_le_ofReal ?_), ?_⟩ + · apply div_le_div_of_nonneg_left hC (by positivity) + gcongr + simp + have heq : F j = (fun x => ((w j : ℝ) : ℂ) * f x) := by + ext1 x + dsimp [f] + rw [← mul_assoc, ← Complex.ofReal_mul, mul_inv_cancel₀ (hw j).ne', + Complex.ofReal_one, one_mul] + rw [heq] + simp_rw [symmetricMaximal_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_pos (hw j)] + rw [lintegral_const_mul _ (measurable_symmetricMaximal f), ENNReal.ofReal_mul (hw j).le] + exact mul_le_mul_right hi _ + choose G hGm hGsize hGi using hg + have hCs : Summable (fun j : ℕ => C / ((j:ℝ)+1)^2) := by + have hh : Summable (fun n : ℕ => 1 / ((n+1:ℕ):ℝ)^2) := + (summable_nat_add_iff 1).mpr (Real.summable_one_div_nat_pow.mpr (by norm_num)) + simpa [div_eq_mul_inv] using hh.mul_left C + have hcompl : (∑' j, circleMeasure (G j)ᶜ) ≠ ∞ := by + apply ne_of_lt + refine (ENNReal.tsum_le_tsum hGsize).trans_lt ?_ + rw [← ENNReal.ofReal_tsum_of_nonneg _ hCs] + · exact ENNReal.ofReal_lt_top + · intro n; positivity + let M (j : ℕ) (x : Torus) := + (G j).indicator (symmetricMaximal (F j)) x + ENNReal.ofReal ‖F j x‖ + have hM (j : ℕ) : AEMeasurable (M j) circleMeasure := + (((measurable_symmetricMaximal _).indicator (hGm _)).aemeasurable).add + ((hF j).norm.aestronglyMeasurable.aemeasurable.ennreal_ofReal) + have ht : ∫⁻ x, (∑' j, M j x) ∂circleMeasure ≠ ∞ := by + have hhM (j : ℕ) : (∫⁻ x, M j x ∂circleMeasure) + ≤ ENNReal.ofReal (w j * (C * (2:ℝ)^(j+D)) + w j) := by + dsimp only [M] + rw [lintegral_add_left ((measurable_symmetricMaximal _).indicator (hGm _)), + lintegral_indicator (hGm j), + ← ofReal_integral_eq_lintegral_ofReal (hF j).norm + (ae_of_all _ fun x => norm_nonneg _), + ENNReal.ofReal_add (by have := hw j; positivity) (hw j).le] + exact add_le_add (hGi j) (ENNReal.ofReal_le_ofReal (hm j)) + rw [lintegral_tsum hM] + apply ne_of_lt + refine (ENNReal.tsum_le_tsum hhM).trans_lt ?_ + have hwS : Summable w := hsum.of_nonneg_of_le (fun n => (hw n).le) (fun n => + le_mul_of_one_le_left (hw n).le (one_le_pow₀ (by norm_num : (1:ℝ) ≤ 2))) + have hfull : Summable (fun j => w j * (C * (2:ℝ)^(j+D)) + w j) := + ((hsum.mul_left (C*(2:ℝ)^D)).add hwS).congr fun n => by + simp only [pow_add] + ring + rw [← ENNReal.ofReal_tsum_of_nonneg _ hfull] + · exact ENNReal.ofReal_lt_top + · intro n; have := hw n; positivity + filter_upwards [ae_eventually_notMem hcompl, ae_lt_top' (AEMeasurable.tsum hM) ht] with x hx hx' + refine ⟨fun n => (M n x).toReal, ENNReal.summable_toReal hx'.ne, ?_⟩ + filter_upwards [hx] with j hj + intro N + have hmn := ENNReal.ne_top_of_tsum_ne_top hx'.ne j + rw [← ENNReal.toReal_ofReal + (add_nonneg (norm_nonneg (symmetricPartialSum _ _ _)) (norm_nonneg _))] + apply ENNReal.toReal_mono hmn + simp only [M, ENNReal.ofReal_add (norm_nonneg _) (norm_nonneg _)] + have : x ∈ G j := by simpa using hj + rw [Set.indicator_of_mem this] + exact add_le_add_left (le_iSup (fun N => ENNReal.ofReal ‖symmetricPartialSum (F j) N x‖) N) _ + +/-- Trigonometric polynomials, as a subspace of continuous functions. -/ +def trigPolys : Submodule ℂ C(Torus, ℂ) := Submodule.span ℂ (Set.range fourier) + +lemma integrable_continuous {f : Torus → ℂ} (hf : Continuous f) : + Integrable f circleMeasure := by + apply hf.integrable_of_hasCompactSupport + exact IsClosed.isCompact (isClosed_tsupport f) + +theorem eventually_partialSum_trigPoly {p : C(Torus, ℂ)} (h : p ∈ trigPolys) : + ∀ᶠ N : ℕ in atTop, ∀ x, symmetricPartialSum p N x = p x := by + induction h using Submodule.span_induction with + | mem p hp => + obtain ⟨k, rfl⟩ := hp + filter_upwards [eventually_ge_atTop k.natAbs] with N hN + intro x + simp only [symmetricPartialSum, fourierCoeff_fourier] + rw [Finset.sum_eq_single k] + · simp + · intro b _ hb + simp [hb] + · intro hk + exfalso + apply hk + simp only [Finset.mem_Icc] + omega + | zero => filter_upwards with N x using symmetricPartialSum_zero N x + | add p q _ _ hp hq => + filter_upwards [hp, hq] with N hpN hqN + intro x + change symmetricPartialSum ((p : Torus → ℂ) + (q : Torus → ℂ)) N x = p x + q x + rw [symmetricPartialSum_add (integrable_continuous p.continuous) + (integrable_continuous q.continuous), hpN x, hqN x] + | smul c p _ hp => + filter_upwards [hp] with N hpN + intro x + change symmetricPartialSum (fun y => c * p y) N x = c * p x + rw [symmetricPartialSum_const_mul, hpN x] + +lemma exists_bounded_trigPoly_approx (f : Torus → ℂ) + (hf : Integrable f circleMeasure) + (B : ℝ) (hB : 0 < B) (hb : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ B) + (ε : ℝ) (hε : 0 < ε) : + ∃ p : C(Torus, ℂ), p ∈ trigPolys ∧ + (∫ x, ‖f x - p x‖ ∂circleMeasure) ≤ ε ∧ ∀ x, ‖p x‖ ≤ B + 1 := by + obtain ⟨g, hg, hgi⟩ := + hf.exists_boundedContinuous_integral_sub_le (show 0 < ε/4 by linarith) + let k (x : Torus) : ℂ := (B / max B ‖g x‖) • g x + have hk : Continuous k := by + have h1 : Continuous fun x => max B ‖g x‖ := continuous_const.max g.continuous.norm + exact (continuous_const.div h1 (fun x => ne_of_gt (lt_max_of_lt_left hB))).smul g.continuous + let k' : C(Torus, ℂ) := ⟨k, hk⟩ + have hkB (x : Torus) : ‖k x‖ ≤ B := by + have hm : 0 < max B ‖g x‖ := lt_max_of_lt_left hB + simp only [k, norm_smul, Real.norm_eq_abs, abs_of_pos (div_pos hB hm)] + exact (mul_le_mul_of_nonneg_left (le_max_right B ‖g x‖) (div_pos hB hm).le).trans + (div_mul_cancel₀ B hm.ne').le + have hdk (x : Torus) (hx : ‖f x‖ ≤ B) : ‖f x - k x‖ ≤ 2 * ‖f x - g x‖ := by + suffices H : ‖g x - k x‖ ≤ ‖f x - g x‖ by + have hi := dist_triangle (f x) (g x) (k x) + simp only [dist_eq_norm] at hi + linarith + rcases le_total ‖g x‖ B with hxg | hxg + · simp [k, max_eq_left hxg, hB.ne'] + have hp : 0 < ‖g x‖ := hB.trans_le hxg + have hxone : (0:ℝ) ≤ 1 - B/‖g x‖ := sub_nonneg.mpr ((div_le_one hp).mpr hxg) + calc + ‖g x - k x‖ = ‖((1:ℝ)-B/‖g x‖) • g x‖ := by + rw [sub_smul, one_smul]; simp only [k, max_eq_right hxg] + _ = ‖g x‖ - B := by + rw [norm_smul, Real.norm_eq_abs, abs_of_nonneg hxone, sub_mul, one_mul, + div_mul_cancel₀ B hp.ne'] + _ ≤ ‖g x‖ - ‖f x‖ := sub_le_sub_left hx _ + _ ≤ ‖f x - g x‖ := by rw [norm_sub_rev]; exact norm_sub_norm_le _ _ + have hkc : k' ∈ closure (trigPolys : Set _) := by + change k' ∈ trigPolys.topologicalClosure + rw [trigPolys, span_fourier_closure_eq_top]; trivial + obtain ⟨p, hps, hp⟩ := Metric.mem_closure_iff.mp hkc (min 1 (ε / 2)) (by positivity) + have hp' (x : Torus) : ‖k x - p x‖ < min 1 (ε / 2) := by + change ‖k' x - p x‖ < _ + rw [← dist_eq_norm] + exact lt_of_le_of_lt (ContinuousMap.dist_apply_le_dist _) hp + refine ⟨p, hps, ?_, fun x => ?_⟩ + · calc + _ ≤ ∫ x, (2 * ‖f x - g x‖ + (ε / 2)) ∂circleMeasure := by + apply integral_mono_ae (hf.sub (integrable_continuous p.continuous)).norm + (((hf.sub hgi).norm.const_mul _).add (integrable_const _)) + filter_upwards [hb] with x hx + have h1 := dist_triangle (f x) (k x) (p x) + simp only [dist_eq_norm] at h1 + exact h1.trans (add_le_add (hdk x hx) + ((hp' x).le.trans (min_le_right _ _))) + _ = 2 * (∫ x, ‖f x - g x‖ ∂circleMeasure) + (ε/2) := by + rw [integral_add (show Integrable (fun x => 2 * ‖f x - g x‖) _ from ((hf.sub hgi).norm.const_mul _)) (integrable_const _), + integral_const_mul]; simp + _ ≤ ε := by linarith + · have h1 := norm_sub_norm_le (p x) (k x) + rw [norm_sub_rev] at h1 + have h2 := ((hp' x).le.trans (min_le_left _ _)) + linarith [hkB x] + + +private def pad (j : ℕ) : ℝ := ((1/4:ℝ))^j + +private lemma pad_pos (j) : 0 < pad j := by unfold pad; positivity + +private lemma summable_pad : Summable (fun j => (2:ℝ)^j * pad j) := by + simp only [pad, ← mul_pow] + exact summable_geometric_of_norm_lt_one (by norm_num) + + +private lemma pad_height (j : ℕ) : + Real.exp ((2:ℝ)^(j+2)) ≤ pad j * Real.exp ((2:ℝ)^(j+4)) := by + have hj : (j:ℝ) ≤ (2:ℝ)^j := by exact_mod_cast (Nat.lt_two_pow_self (n:=j)).le + have hex : (4:ℝ)^j ≤ Real.exp ((3:ℝ) * j) := by + have h3 := Real.add_one_le_exp 3 + rw [mul_comm 3, Real.exp_nat_mul] + exact pow_le_pow_left₀ (by norm_num) (by norm_num at h3; linarith) _ + have Hmul : (4:ℝ)^j * Real.exp ((2:ℝ)^(j+2)) ≤ Real.exp ((2:ℝ)^(j+4)) := by + refine (mul_le_mul_of_nonneg_right hex (Real.exp_nonneg _)).trans ?_ + rw [← Real.exp_add] + apply Real.exp_monotone + simp only [pow_add] + linarith + simp only [pad, one_div, inv_pow, ← div_eq_inv_mul] + apply (le_div_iff₀ (by positivity : (0:ℝ)<4^j)).mpr + rw [mul_comm] + exact Hmul + +theorem GoodBound.ae_convergence_bounded (hgb : GoodBound) (f : Torus → ℂ) + (hf : Integrable f circleMeasure) (B : ℝ) (hB : 0 < B) + (hb : ∀ᵐ x ∂circleMeasure, ‖f x‖ ≤ B) : + ∀ᵐ x ∂circleMeasure, Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := by + choose p hp hm hpB using + (fun j => exists_bounded_trigPoly_approx f hf B hB hb (pad j) (pad_pos j)) + let F (j : ℕ) (x : Torus) : ℂ := f x - p j x + have hFi (j) : Integrable (F j) circleMeasure := + hf.sub (integrable_continuous (p j).continuous) + let w (j : ℕ) : ℝ := (2*B+1) * pad j + have hw (j) : 0 < w j := mul_pos (by linarith) (pad_pos _) + have hs : Summable fun j => (2:ℝ)^j * w j := + (summable_pad.mul_left (2*B+1)).congr fun j => by dsimp only [w]; ring + have hmw (j) : (∫ x, ‖F j x‖ ∂circleMeasure) ≤ w j := + (hm j).trans (le_mul_of_one_le_left (pad_pos j).le (by linarith)) + have hheight (j) : ∀ᵐ x ∂circleMeasure, ‖F j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by + filter_upwards [hb] with x hx + calc + ‖F j x‖ ≤ ‖f x‖ + ‖p j x‖ := norm_sub_le _ _ + _ ≤ (2*B+1)*Real.exp ((2:ℝ)^(j+2)) := by + have := hpB j x + have : 1 ≤ Real.exp ((2:ℝ)^(j+2)) := Real.one_le_exp (by positivity) + nlinarith + _ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by + dsimp only [w] + rw [mul_assoc] + exact mul_le_mul_of_nonneg_left (pad_height j) (by linarith) + filter_upwards [hgb.ae_tail_majorant F w 4 hFi hw hs hmw hheight] with x hx + obtain ⟨b, hsum, hmaj⟩ := hx + apply Metric.tendsto_nhds.mpr + intro ε hε + obtain ⟨j, hj, hj'⟩ := + (hmaj.and (hsum.tendsto_atTop_zero.eventually (gt_mem_nhds hε))).exists + filter_upwards [eventually_partialSum_trigPoly (hp j)] with N hN + have hfdecomp : f = F j + (p j : Torus → ℂ) := by ext; simp [F] + calc + dist (symmetricPartialSum f N x) (f x) + = ‖symmetricPartialSum (F j) N x - F j x‖ := by + nth_rw 1 2 [hfdecomp] + rw [symmetricPartialSum_add (hFi j) (integrable_continuous (p j).continuous), + hN x, dist_eq_norm] + simp only [Pi.add_apply, add_sub_add_right_eq_sub] + _ ≤ ‖symmetricPartialSum (F j) N x‖ + ‖F j x‖ := norm_sub_le _ _ + _ ≤ b j := hj N + _ < ε := hj' + +theorem GoodBound.main_bound_consequence (hgb : GoodBound) + {f : Torus → ℂ} (hf : MemLLogL f) : + ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, + Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := by + classical + let w (j : ℕ) := heightLayerMass f j + pad j + have hw (j : ℕ) : 0 < w j := + add_pos_of_nonneg_of_pos (heightLayerMass_nonneg f _) (pad_pos _) + have hs : Summable (fun j => (2:ℝ)^j * w j) := by + have hs' := hf.summable_weighted_heightLayerMass.mul_left ((1:ℝ)/2) + refine (hs'.add summable_pad).congr ?_ + intro j + dsimp only [w] + rw [pow_succ]; ring + have hm (j) : (∫ x, ‖heightLayer f j x‖ ∂circleMeasure) ≤ w j := by + rw [← heightLayerMass_eq_integral_norm hf] + apply le_add_of_nonneg_right + exact (pad_pos j).le + have hh (j) : ∀ᵐ x ∂circleMeasure, + ‖heightLayer f j x‖ ≤ w j * Real.exp ((2:ℝ)^(j+4)) := by + refine ae_of_all _ fun x => ((norm_heightLayer_le f j x).trans (pad_height j)).trans ?_ + exact mul_le_mul_of_nonneg_right + (le_add_of_nonneg_left (heightLayerMass_nonneg _ _)) (Real.exp_nonneg _) + have hbConv := hgb.ae_convergence_bounded _ hf.boundedPart_integrable _ + (Real.exp_pos _) (ae_of_all _ (norm_boundedPart_le f)) + have hConv (j) := hgb.ae_convergence_bounded _ (hf.heightLayer_integrable j) _ + (Real.exp_pos _) (ae_of_all _ (norm_heightLayer_le f j)) + apply exists_measurable_null_of_ae + filter_upwards [hbConv, ae_all_iff.mpr hConv, + hgb.ae_tail_majorant _ w 4 hf.heightLayer_integrable hw hs hm hh] with x hb hc hx + obtain ⟨b, hsum, hmaj⟩ := hx + obtain ⟨k, hk⟩ := eventually_atTop.1 hmaj + have hbd (j : ℕ) : BddAbove (Set.range + (fun N : ℕ => ‖symmetricPartialSum (heightLayer f j) N x‖)) := + ((hc j).norm).bddAbove_range + choose r hr using hbd + let b' (j : ℕ) := if j < k then r j else b j + have hs' : Summable b' := by + apply (summable_nat_add_iff k).mp + have hh := (summable_nat_add_iff k).mpr hsum + simpa [b'] using hh + have hmaj' (j N) : ‖symmetricPartialSum (heightLayer f j) N x‖ ≤ b' j := by + dsimp [b'] + split_ifs with H + · exact hr j (Set.mem_range_self N) + · exact (le_add_of_nonneg_right (norm_nonneg _)).trans (hk j (by omega) N) + have ht := tendsto_tsum_of_dominated_convergence hs' hc + (Eventually.of_forall fun N j => hmaj' j N) + have h := hb.add ht + + simpa only [hf.symmetricPartialSum_heightDecomposition, boundedPart_add_tsum_heightLayer] using h + +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Good.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Good.lean index 6d6e0537b3d570f2396836572d4523cc6131e613..8a58567f50ad8f6f0ca4a551288127aab6755ab1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Good.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Good.lean @@ -1,263 +1,263 @@ -import OAI.Analysis.FourierLLogL.Fourier.Dirichlet -/-! # GoodBound from the dyadic lemma -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -private lemma center_sub : centerWin ⊆ window := by - unfold centerWin window; grind - -lemma sumReal_integral (ρ : ℝ → ℝ) (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) (hp : Periodic ρ 1) (N : ℕ) (x : ℝ) : - sumReal (fun y => (ρ y : ℂ)) N x = - ∫ t in window, (ρ (x-t):ℂ) * centerWin.indicator (dirichlet N) t := by - have hterm (n : ℤ) : - (∫ y, (ρ y:ℂ) * phase (-n:ℤ) y ∂μ₁) * phase n x = - ∫ t in centerWin, (ρ (x-t):ℂ) * phase n t := by - let p (y : ℝ) := modρ ρ (-n:ℤ) y * phase n x - have hpp : Periodic p 1 := by - intro w - simp only [p,modρ,hp w,phase_periodic (-n) w] - have H (t : ℝ) : p (x-t) = (ρ (x-t) : ℂ) * phase n t := by - dsimp only [p, modρ]; push_cast - rw [mul_assoc, mul_comm (phase _ _), ← phase_sub, - show x-(x-t)=t by ring] - rw [← integral_mul_const] - change (∫ y, p y ∂μ₁) = _ - simp_rw [← H] - have HH := seg_translate_scale_rev p x (by norm_num : (-(1/2):ℝ)≤1/2) - (by norm_num : (0:ℝ)<1) - simp only [one_mul, inv_one, one_smul] at HH - rw [show x-(-(1/2):ℝ) = (x-1/2)+1 by ring, seg_period_unit p hpp] at HH - simpa only [centerWin, neg_div] using HH.symm - have hxρ : Measurable fun t => (ρ (x-t):ℂ) := - Complex.continuous_ofReal.measurable.comp (hρ.comp (by fun_prop)) - have heq (t : ℝ) : (ρ (x-t):ℂ)*centerWin.indicator (dirichlet N) t = - centerWin.indicator (fun t => (ρ (x-t):ℂ)*dirichlet N t) t := by - by_cases ht : t ∈ centerWin <;> simp [ht] - unfold sumReal - simp_rw [hterm,heq] - rw [setIntegral_indicator (show MeasurableSet centerWin from measurableSet_Ico), inter_eq_right.mpr center_sub] - unfold dirichlet - simp_rw [Finset.mul_sum] - rw [integral_finsetSum] - intro n _ - refine seg_integrable _ (show Measurable (fun t => (ρ (x-t):ℂ)*phase n t) - from hxρ.mul (phase_cont n).measurable) B _ _ fun t _ => ?_ - simp only [norm_mul, phase_unimod, Complex.norm_real, Real.norm_eq_abs, - abs_of_nonneg (h0 _), mul_one] - exact hb _ - -private instance : IsFiniteMeasure (volume.restrict window) := - show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance - -lemma kernel_compare_result (ρ : ℝ → ℝ) (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ x, ρ x ≤ B) (hp : Periodic ρ 1) - (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (N J : ℕ) - (hJ : 140 * ((N:ℝ)+1) * B * side J ≤ 1) (x : ℝ) : - ENNReal.ofReal ‖sumReal (fun y => (ρ y:ℂ)) N x‖ ≤ 41 + 4 * maxConvK ρ J x := by - let d := side J - have hd := side_pos J - have hd' := side_le J - have hB : 0 ≤ B := le_trans (h0 0) (hb 0) - let u : ℝ := N - let w (t : ℝ) := ρ (x-t) - let V (t : ℝ) : ℂ := kernel d t • modρ ρ u (x-t) - have hwm : Measurable w := hρ.comp (by fun_prop) - have hw (t : ℝ) : 0 ≤ w t := h0 _ - have hwB (t : ℝ) : w t ≤ B := hb _ - have hwi (a b) : IntervalIntegrable w volume a b := - interval_integrable w hwm B (fun t => by - simpa [Real.norm_eq_abs, abs_of_nonneg (hw t)] using hwB t) a b - have hw' : IntegrableOn w window := - Integrable.of_bound hwm.aestronglyMeasurable B (ae_of_all _ fun t => by - simpa [Real.norm_eq_abs, abs_of_nonneg (hw t)] using hwB t) - have hmass : (∫ t in window, w t) ≤ 2 := by - have H := seg_translate_scale_rev ρ x (by norm_num : (-1:ℝ) ≤ 1) - (by norm_num : (0:ℝ)<1) - simp only [one_mul, inv_one, one_smul, sub_neg_eq_add] at H - change (∫ t in Ico (-1:ℝ) 1, ρ (x-t)) ≤ 2 - have h := seg_period_int ρ hp (interval_integrable ρ hρ B (fun t => by - simpa [Real.norm_eq_abs, abs_of_nonneg (h0 t)] using hb t)) (x-1) 2 - rw [H, show x+1=x-1+(↑(2:ℕ) : ℝ) by norm_num; ring, h] - simp; linarith - have hVi : IntegrableOn V window := by - have hVm : Measurable V := (kernel_measurable _).smul - ((measurable_mod hρ u).comp (by fun_prop)) - apply Integrable.of_bound hVm.aestronglyMeasurable ((2/d)*B) - apply ae_of_all - intro t - dsimp only [V] - rw [norm_smul, norm_mod h0] - exact mul_le_mul (kernel_small hd hd' _) (hb _) (h0 _) (div_nonneg (by norm_num) hd.le) - let c := phase (-u) x - let A : ℝ → ℂ := fun t => (w t:ℂ)* ↑(κfactor * kernel d t * Real.sin (ang u t)) - have hA (t) : A t = ↑((-κfactor)*(c * V t).im) := by - have heq : c * V t = ↑(kernel d t)* (w t:ℂ) * phase u (-t) := by - simp only [V,c,modρ,w,sub_eq_add_neg, phase_add, Complex.real_smul] - have H : phase (-u) x * phase u x = 1 := by rw [← phase_freq_add]; simp [phase] - linear_combination ↑(kernel d t) * (↑(ρ (x + -t)):ℂ) * phase u (-t) * H - rw [heq, phase_trig] - simp [A, ang, -Complex.ofReal_sin, -Complex.ofReal_cos]; ring - have hiA : IntegrableOn A window := by - - have h : Integrable (fun t => c * V t) (volume.restrict window) := hVi.const_mul _ - rw [show A = _ from funext hA] - exact Integrable.ofReal (h.im.const_mul _) - have heA : (∫ t in window, A t) = ↑((-κfactor)*(c * convK (modρ ρ u) x J).im) := by - simp_rw [hA] - rw [integral_complex_ofReal, integral_const_mul] - have h : Integrable (fun t => c * V t) (volume.restrict window) := hVi.const_mul _ - have hh := integral_im h - change (∫ t in window, (c * V t).im) = (∫ t in window, c * V t).im at hh - rw [hh, integral_const_mul]; rfl - let T (t : ℝ) := (w t:ℂ) * centerWin.indicator (dirichlet N) t - have hiT : IntegrableOn T window := by - have h : Measurable T := (Complex.continuous_ofReal.measurable.comp hwm).mul - ((dirichlet_cont _).measurable.indicator measurableSet_Ico) - apply Integrable.of_bound h.aestronglyMeasurable (B*(2*N+1)) - apply ae_of_all - intro t - simp only [T, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw t)] - apply mul_le_mul (hwB _) ?_ (norm_nonneg _) hB - by_cases ht : t ∈ centerWin - · simpa [ht] using dirichlet_bound N t - · simp only [indicator_of_notMem ht, norm_zero]; positivity - let M := (70:ℝ) * ((N:ℝ)+1) - let S : Set ℝ := Ico (-d) d - have hSm : MeasurableSet S := measurableSet_Ico - have hSw : S ⊆ window := Ico_subset_Ico (neg_le_neg hd') hd' - let W (t : ℝ) := S.indicator (fun _ => M*B) t - have hiW : IntegrableOn W window := (integrable_const _).indicator hSm - have heW : (∫ t in window, W t) ≤ 1 := by - dsimp only [W] - rw [setIntegral_indicator hSm, inter_eq_right.mpr hSw, setIntegral_const, - Real.volume_real_Ico_of_le (by dsimp only [d]; linarith), smul_eq_mul] - dsimp only [M,d] - linarith - have hei : - ‖(∫ t in window, T t) - ∫ t in window, A t‖ ≤ 41 := by - rw [← integral_sub hiT hiA] - apply (norm_integral_le_integral_norm _).trans - calc - _ ≤ ∫ t in window, (20*w t + W t) := by - apply integral_mono_ae (hiT.sub hiA).norm ((hw'.const_mul (20:ℝ)).add hiW) - filter_upwards [ae_restrict_mem (show MeasurableSet window from measurableSet_Ico)] - with t ht - have hk : ‖T t - A t‖ = w t * ‖diffK d N t‖ := by - dsimp only [T,A, diffK,u] - rw [← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw t)] - change ‖T t - A t‖ ≤ 20*w t+W t - have HH := diffK_major hd hd' ht N - rw [hk] - by_cases htd : |t| < d - · rw [ite_eq_left (show |t| < side J from htd)] at HH - have hs : t ∈ S := ⟨(abs_lt.mp htd).1.le,(abs_lt.mp htd).2⟩ - rw [show W t = M*B from indicator_of_mem hs _] - change _ ≤ M at HH - have : 0 ≤ M := by dsimp [M]; positivity - have := mul_le_mul (hwB t) HH (norm_nonneg _) hB - nlinarith [hw t] - · rw [ite_eq_right (show ¬ |t| < side J from htd)] at HH - have : 0 ≤ W t := by - by_cases h : t ∈ S - · rw [show W t = M*B from indicator_of_mem h _] - dsimp [M]; positivity - · rw [show W t = 0 from indicator_of_notMem h _] - nlinarith [hw t] - _ ≤ 41 := by rw [integral_add (hw'.const_mul (20:ℝ)) hiW, integral_const_mul]; linarith - have hbound : ‖sumReal (fun y => (ρ y:ℂ)) N x‖ ≤ 41 + 4 * ‖convK (modρ ρ u) x J‖ := by - rw [sumReal_integral ρ hρ h0 B hb hp] - change ‖∫ t in window, T t‖ ≤ _ - have hh := le_trans (norm_sub_norm_le (∫ t in window, T t) (∫ t in window, A t)) hei - rw [heA, Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_neg] at hh - have H1 := Complex.abs_im_le_norm (c * convK (modρ ρ u) x J) - rw [norm_mul, show ‖c‖=1 from phase_unimod _ _, one_mul] at H1 - have := mul_le_mul κfactor_bound H1 (abs_nonneg _) (by norm_num) - linarith - apply (ENNReal.ofReal_le_ofReal hbound).trans - rw [ENNReal.ofReal_add (by norm_num) (by positivity), ENNReal.ofReal_mul (by norm_num)] - have Hle := le_iSup (fun n : ℤ => ENNReal.ofReal ‖convK (modρ ρ n) x J‖) (N:ℤ) - simp only [Int.cast_natCast] at Hle - norm_num - - gcongr - rw [← ofReal_norm]; exact Hle - - -theorem AffineTreeBound.realGoodBound (h : AffineTreeBound) : RealGoodBound := by - obtain ⟨C, hC, h⟩ := h - let D := 100 + 64*C - refine ⟨D, (show 0 ≤ 100+64*C by positivity), ?_⟩ - filter_upwards [h] with j hj - intro ρ hρ hp h0 hm hb - let B := Real.exp ((2:ℝ)^j) - have hB : 0 < B := Real.exp_pos _ - obtain ⟨G,hG,hGs,hGc,hGd⟩ := - density_good_set ρ hρ hp h0 B hb hm (((j:ℝ)+1)^3) (by positivity) - refine ⟨G,hG,hGc.trans (ENNReal.ofReal_le_ofReal ?_),?_⟩ - · have hn : (1:ℝ) ≤ (j:ℝ)+1 := by simp - apply div_le_div₀ - all_goals first - | dsimp [D]; positivity - | positivity - | dsimp [D]; linarith - | gcongr; norm_num - rw [μ₁, Measure.restrict_restrict hG, inter_eq_left.mpr hGs] - let Y (N : ℕ) (x : ℝ) := ENNReal.ofReal ‖sumReal (fun y => (ρ y:ℂ)) N x‖ - let Ym (n : ℕ) (x : ℝ) := ⨆ N : ℕ, ⨆ (_ : N ≤ n), Y N x - have hYm (n : ℕ) : Measurable (Ym n) := by - refine Measurable.iSup fun N => Measurable.iSup fun _ => ?_ - refine Measurable.ennreal_ofReal (Measurable.norm (Continuous.measurable ?_)) - apply continuous_finsetSum - intro k _ - exact continuous_const.mul (phase_cont _) - have hYm_m : Monotone Ym := fun n m h x => iSup_le fun k => iSup_le fun hk => - le_iSup₂ (f:=fun N (_h : N ≤ m) => Y N x) k (le_trans hk h) - have hmax (x : ℝ) : maxReal (fun y => (ρ y:ℂ)) x ≤ ⨆ n, Ym n x := by - refine iSup_le fun n => - (le_iSup₂ (f := fun N (_h : N ≤ n) => Y N x) n le_rfl).trans (le_iSup (fun n => Ym n x) n) - refine (lintegral_mono hmax).trans ?_ - rw [lintegral_iSup hYm hYm_m] - refine iSup_le fun n => ?_ - have Hs : Tendsto side atTop (𝓝 0) := - tendsto_pow_atTop_nhds_zero_of_lt_one (by norm_num) (by norm_num) - obtain ⟨J,hJ⟩ := ((show Tendsto (fun j => 140*((n:ℝ)+1)*B*side j) atTop (𝓝 0) by - simpa only [mul_zero] using Hs.const_mul (140*((n:ℝ)+1)*B)).eventually - (gt_mem_nhds (show (0:ℝ)<1 by norm_num))).exists - change 140*((n:ℝ)+1)*B*side J < 1 at hJ - have HL : (∫⁻ x in G, maxConvK ρ J x) ≤ 16 * ENNReal.ofReal (C*(2:ℝ)^j) := - convolution_cost hρ h0 B hb G hG hGs _ J - (fun s hs a _ => hj ρ hρ hp h0 hm hb J G hG hGd a s hs.1 hs.2.le) - have HE (x : ℝ) : Ym n x ≤ 41 + 4 * maxConvK ρ J x := by - refine iSup_le fun k => iSup_le fun hk => - kernel_compare_result ρ hρ h0 B hb hp hm k J (le_trans ?_ hJ.le) x - have := side_pos J - gcongr - refine (lintegral_mono HE).trans ?_ - rw [lintegral_add_left measurable_const, - lintegral_const_mul _ (show Measurable (maxConvK ρ J) from measurable_maxConvK hρ J), - setLIntegral_const] - have hv : volume G ≤ 1 := (measure_mono hGs).trans (by simp [unitInt]) - calc - _ ≤ 41 + 4*(16*ENNReal.ofReal (C*(2:ℝ)^j)) := - add_le_add (mul_le_of_le_one_right zero_le hv) (mul_le_mul_right HL _) - _ = ENNReal.ofReal (41 + 4*(16*(C*(2:ℝ)^j))) := by - rw [ENNReal.ofReal_add (by positivity) (by positivity), - ENNReal.ofReal_mul (show (0:ℝ)≤4 by norm_num), - ENNReal.ofReal_mul (show (0:ℝ)≤16 by norm_num)] - norm_num - _ ≤ _ := ENNReal.ofReal_le_ofReal (by - dsimp [D] - have H : (1:ℝ) ≤ 2^j := one_le_pow₀ (by norm_num) - linarith) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Dirichlet +/-! # GoodBound from the dyadic lemma -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +private lemma center_sub : centerWin ⊆ window := by + unfold centerWin window; grind + +lemma sumReal_integral (ρ : ℝ → ℝ) (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) (hp : Periodic ρ 1) (N : ℕ) (x : ℝ) : + sumReal (fun y => (ρ y : ℂ)) N x = + ∫ t in window, (ρ (x-t):ℂ) * centerWin.indicator (dirichlet N) t := by + have hterm (n : ℤ) : + (∫ y, (ρ y:ℂ) * phase (-n:ℤ) y ∂μ₁) * phase n x = + ∫ t in centerWin, (ρ (x-t):ℂ) * phase n t := by + let p (y : ℝ) := modρ ρ (-n:ℤ) y * phase n x + have hpp : Periodic p 1 := by + intro w + simp only [p,modρ,hp w,phase_periodic (-n) w] + have H (t : ℝ) : p (x-t) = (ρ (x-t) : ℂ) * phase n t := by + dsimp only [p, modρ]; push_cast + rw [mul_assoc, mul_comm (phase _ _), ← phase_sub, + show x-(x-t)=t by ring] + rw [← integral_mul_const] + change (∫ y, p y ∂μ₁) = _ + simp_rw [← H] + have HH := seg_translate_scale_rev p x (by norm_num : (-(1/2):ℝ)≤1/2) + (by norm_num : (0:ℝ)<1) + simp only [one_mul, inv_one, one_smul] at HH + rw [show x-(-(1/2):ℝ) = (x-1/2)+1 by ring, seg_period_unit p hpp] at HH + simpa only [centerWin, neg_div] using HH.symm + have hxρ : Measurable fun t => (ρ (x-t):ℂ) := + Complex.continuous_ofReal.measurable.comp (hρ.comp (by fun_prop)) + have heq (t : ℝ) : (ρ (x-t):ℂ)*centerWin.indicator (dirichlet N) t = + centerWin.indicator (fun t => (ρ (x-t):ℂ)*dirichlet N t) t := by + by_cases ht : t ∈ centerWin <;> simp [ht] + unfold sumReal + simp_rw [hterm,heq] + rw [setIntegral_indicator (show MeasurableSet centerWin from measurableSet_Ico), inter_eq_right.mpr center_sub] + unfold dirichlet + simp_rw [Finset.mul_sum] + rw [integral_finsetSum] + intro n _ + refine seg_integrable _ (show Measurable (fun t => (ρ (x-t):ℂ)*phase n t) + from hxρ.mul (phase_cont n).measurable) B _ _ fun t _ => ?_ + simp only [norm_mul, phase_unimod, Complex.norm_real, Real.norm_eq_abs, + abs_of_nonneg (h0 _), mul_one] + exact hb _ + +private instance : IsFiniteMeasure (volume.restrict window) := + show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance + +lemma kernel_compare_result (ρ : ℝ → ℝ) (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ x, ρ x ≤ B) (hp : Periodic ρ 1) + (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (N J : ℕ) + (hJ : 140 * ((N:ℝ)+1) * B * side J ≤ 1) (x : ℝ) : + ENNReal.ofReal ‖sumReal (fun y => (ρ y:ℂ)) N x‖ ≤ 41 + 4 * maxConvK ρ J x := by + let d := side J + have hd := side_pos J + have hd' := side_le J + have hB : 0 ≤ B := le_trans (h0 0) (hb 0) + let u : ℝ := N + let w (t : ℝ) := ρ (x-t) + let V (t : ℝ) : ℂ := kernel d t • modρ ρ u (x-t) + have hwm : Measurable w := hρ.comp (by fun_prop) + have hw (t : ℝ) : 0 ≤ w t := h0 _ + have hwB (t : ℝ) : w t ≤ B := hb _ + have hwi (a b) : IntervalIntegrable w volume a b := + interval_integrable w hwm B (fun t => by + simpa [Real.norm_eq_abs, abs_of_nonneg (hw t)] using hwB t) a b + have hw' : IntegrableOn w window := + Integrable.of_bound hwm.aestronglyMeasurable B (ae_of_all _ fun t => by + simpa [Real.norm_eq_abs, abs_of_nonneg (hw t)] using hwB t) + have hmass : (∫ t in window, w t) ≤ 2 := by + have H := seg_translate_scale_rev ρ x (by norm_num : (-1:ℝ) ≤ 1) + (by norm_num : (0:ℝ)<1) + simp only [one_mul, inv_one, one_smul, sub_neg_eq_add] at H + change (∫ t in Ico (-1:ℝ) 1, ρ (x-t)) ≤ 2 + have h := seg_period_int ρ hp (interval_integrable ρ hρ B (fun t => by + simpa [Real.norm_eq_abs, abs_of_nonneg (h0 t)] using hb t)) (x-1) 2 + rw [H, show x+1=x-1+(↑(2:ℕ) : ℝ) by norm_num; ring, h] + simp; linarith + have hVi : IntegrableOn V window := by + have hVm : Measurable V := (kernel_measurable _).smul + ((measurable_mod hρ u).comp (by fun_prop)) + apply Integrable.of_bound hVm.aestronglyMeasurable ((2/d)*B) + apply ae_of_all + intro t + dsimp only [V] + rw [norm_smul, norm_mod h0] + exact mul_le_mul (kernel_small hd hd' _) (hb _) (h0 _) (div_nonneg (by norm_num) hd.le) + let c := phase (-u) x + let A : ℝ → ℂ := fun t => (w t:ℂ)* ↑(κfactor * kernel d t * Real.sin (ang u t)) + have hA (t) : A t = ↑((-κfactor)*(c * V t).im) := by + have heq : c * V t = ↑(kernel d t)* (w t:ℂ) * phase u (-t) := by + simp only [V,c,modρ,w,sub_eq_add_neg, phase_add, Complex.real_smul] + have H : phase (-u) x * phase u x = 1 := by rw [← phase_freq_add]; simp [phase] + linear_combination ↑(kernel d t) * (↑(ρ (x + -t)):ℂ) * phase u (-t) * H + rw [heq, phase_trig] + simp [A, ang, -Complex.ofReal_sin, -Complex.ofReal_cos]; ring + have hiA : IntegrableOn A window := by + + have h : Integrable (fun t => c * V t) (volume.restrict window) := hVi.const_mul _ + rw [show A = _ from funext hA] + exact Integrable.ofReal (h.im.const_mul _) + have heA : (∫ t in window, A t) = ↑((-κfactor)*(c * convK (modρ ρ u) x J).im) := by + simp_rw [hA] + rw [integral_complex_ofReal, integral_const_mul] + have h : Integrable (fun t => c * V t) (volume.restrict window) := hVi.const_mul _ + have hh := integral_im h + change (∫ t in window, (c * V t).im) = (∫ t in window, c * V t).im at hh + rw [hh, integral_const_mul]; rfl + let T (t : ℝ) := (w t:ℂ) * centerWin.indicator (dirichlet N) t + have hiT : IntegrableOn T window := by + have h : Measurable T := (Complex.continuous_ofReal.measurable.comp hwm).mul + ((dirichlet_cont _).measurable.indicator measurableSet_Ico) + apply Integrable.of_bound h.aestronglyMeasurable (B*(2*N+1)) + apply ae_of_all + intro t + simp only [T, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw t)] + apply mul_le_mul (hwB _) ?_ (norm_nonneg _) hB + by_cases ht : t ∈ centerWin + · simpa [ht] using dirichlet_bound N t + · simp only [indicator_of_notMem ht, norm_zero]; positivity + let M := (70:ℝ) * ((N:ℝ)+1) + let S : Set ℝ := Ico (-d) d + have hSm : MeasurableSet S := measurableSet_Ico + have hSw : S ⊆ window := Ico_subset_Ico (neg_le_neg hd') hd' + let W (t : ℝ) := S.indicator (fun _ => M*B) t + have hiW : IntegrableOn W window := (integrable_const _).indicator hSm + have heW : (∫ t in window, W t) ≤ 1 := by + dsimp only [W] + rw [setIntegral_indicator hSm, inter_eq_right.mpr hSw, setIntegral_const, + Real.volume_real_Ico_of_le (by dsimp only [d]; linarith), smul_eq_mul] + dsimp only [M,d] + linarith + have hei : + ‖(∫ t in window, T t) - ∫ t in window, A t‖ ≤ 41 := by + rw [← integral_sub hiT hiA] + apply (norm_integral_le_integral_norm _).trans + calc + _ ≤ ∫ t in window, (20*w t + W t) := by + apply integral_mono_ae (hiT.sub hiA).norm ((hw'.const_mul (20:ℝ)).add hiW) + filter_upwards [ae_restrict_mem (show MeasurableSet window from measurableSet_Ico)] + with t ht + have hk : ‖T t - A t‖ = w t * ‖diffK d N t‖ := by + dsimp only [T,A, diffK,u] + rw [← mul_sub, norm_mul, Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (hw t)] + change ‖T t - A t‖ ≤ 20*w t+W t + have HH := diffK_major hd hd' ht N + rw [hk] + by_cases htd : |t| < d + · rw [ite_eq_left (show |t| < side J from htd)] at HH + have hs : t ∈ S := ⟨(abs_lt.mp htd).1.le,(abs_lt.mp htd).2⟩ + rw [show W t = M*B from indicator_of_mem hs _] + change _ ≤ M at HH + have : 0 ≤ M := by dsimp [M]; positivity + have := mul_le_mul (hwB t) HH (norm_nonneg _) hB + nlinarith [hw t] + · rw [ite_eq_right (show ¬ |t| < side J from htd)] at HH + have : 0 ≤ W t := by + by_cases h : t ∈ S + · rw [show W t = M*B from indicator_of_mem h _] + dsimp [M]; positivity + · rw [show W t = 0 from indicator_of_notMem h _] + nlinarith [hw t] + _ ≤ 41 := by rw [integral_add (hw'.const_mul (20:ℝ)) hiW, integral_const_mul]; linarith + have hbound : ‖sumReal (fun y => (ρ y:ℂ)) N x‖ ≤ 41 + 4 * ‖convK (modρ ρ u) x J‖ := by + rw [sumReal_integral ρ hρ h0 B hb hp] + change ‖∫ t in window, T t‖ ≤ _ + have hh := le_trans (norm_sub_norm_le (∫ t in window, T t) (∫ t in window, A t)) hei + rw [heA, Complex.norm_real, Real.norm_eq_abs, abs_mul, abs_neg] at hh + have H1 := Complex.abs_im_le_norm (c * convK (modρ ρ u) x J) + rw [norm_mul, show ‖c‖=1 from phase_unimod _ _, one_mul] at H1 + have := mul_le_mul κfactor_bound H1 (abs_nonneg _) (by norm_num) + linarith + apply (ENNReal.ofReal_le_ofReal hbound).trans + rw [ENNReal.ofReal_add (by norm_num) (by positivity), ENNReal.ofReal_mul (by norm_num)] + have Hle := le_iSup (fun n : ℤ => ENNReal.ofReal ‖convK (modρ ρ n) x J‖) (N:ℤ) + simp only [Int.cast_natCast] at Hle + norm_num + + gcongr + rw [← ofReal_norm]; exact Hle + + +theorem AffineTreeBound.realGoodBound (h : AffineTreeBound) : RealGoodBound := by + obtain ⟨C, hC, h⟩ := h + let D := 100 + 64*C + refine ⟨D, (show 0 ≤ 100+64*C by positivity), ?_⟩ + filter_upwards [h] with j hj + intro ρ hρ hp h0 hm hb + let B := Real.exp ((2:ℝ)^j) + have hB : 0 < B := Real.exp_pos _ + obtain ⟨G,hG,hGs,hGc,hGd⟩ := + density_good_set ρ hρ hp h0 B hb hm (((j:ℝ)+1)^3) (by positivity) + refine ⟨G,hG,hGc.trans (ENNReal.ofReal_le_ofReal ?_),?_⟩ + · have hn : (1:ℝ) ≤ (j:ℝ)+1 := by simp + apply div_le_div₀ + all_goals first + | dsimp [D]; positivity + | positivity + | dsimp [D]; linarith + | gcongr; norm_num + rw [μ₁, Measure.restrict_restrict hG, inter_eq_left.mpr hGs] + let Y (N : ℕ) (x : ℝ) := ENNReal.ofReal ‖sumReal (fun y => (ρ y:ℂ)) N x‖ + let Ym (n : ℕ) (x : ℝ) := ⨆ N : ℕ, ⨆ (_ : N ≤ n), Y N x + have hYm (n : ℕ) : Measurable (Ym n) := by + refine Measurable.iSup fun N => Measurable.iSup fun _ => ?_ + refine Measurable.ennreal_ofReal (Measurable.norm (Continuous.measurable ?_)) + apply continuous_finsetSum + intro k _ + exact continuous_const.mul (phase_cont _) + have hYm_m : Monotone Ym := fun n m h x => iSup_le fun k => iSup_le fun hk => + le_iSup₂ (f:=fun N (_h : N ≤ m) => Y N x) k (le_trans hk h) + have hmax (x : ℝ) : maxReal (fun y => (ρ y:ℂ)) x ≤ ⨆ n, Ym n x := by + refine iSup_le fun n => + (le_iSup₂ (f := fun N (_h : N ≤ n) => Y N x) n le_rfl).trans (le_iSup (fun n => Ym n x) n) + refine (lintegral_mono hmax).trans ?_ + rw [lintegral_iSup hYm hYm_m] + refine iSup_le fun n => ?_ + have Hs : Tendsto side atTop (𝓝 0) := + tendsto_pow_atTop_nhds_zero_of_lt_one (by norm_num) (by norm_num) + obtain ⟨J,hJ⟩ := ((show Tendsto (fun j => 140*((n:ℝ)+1)*B*side j) atTop (𝓝 0) by + simpa only [mul_zero] using Hs.const_mul (140*((n:ℝ)+1)*B)).eventually + (gt_mem_nhds (show (0:ℝ)<1 by norm_num))).exists + change 140*((n:ℝ)+1)*B*side J < 1 at hJ + have HL : (∫⁻ x in G, maxConvK ρ J x) ≤ 16 * ENNReal.ofReal (C*(2:ℝ)^j) := + convolution_cost hρ h0 B hb G hG hGs _ J + (fun s hs a _ => hj ρ hρ hp h0 hm hb J G hG hGd a s hs.1 hs.2.le) + have HE (x : ℝ) : Ym n x ≤ 41 + 4 * maxConvK ρ J x := by + refine iSup_le fun k => iSup_le fun hk => + kernel_compare_result ρ hρ h0 B hb hp hm k J (le_trans ?_ hJ.le) x + have := side_pos J + gcongr + refine (lintegral_mono HE).trans ?_ + rw [lintegral_add_left measurable_const, + lintegral_const_mul _ (show Measurable (maxConvK ρ J) from measurable_maxConvK hρ J), + setLIntegral_const] + have hv : volume G ≤ 1 := (measure_mono hGs).trans (by simp [unitInt]) + calc + _ ≤ 41 + 4*(16*ENNReal.ofReal (C*(2:ℝ)^j)) := + add_le_add (mul_le_of_le_one_right zero_le hv) (mul_le_mul_right HL _) + _ = ENNReal.ofReal (41 + 4*(16*(C*(2:ℝ)^j))) := by + rw [ENNReal.ofReal_add (by positivity) (by positivity), + ENNReal.ofReal_mul (show (0:ℝ)≤4 by norm_num), + ENNReal.ofReal_mul (show (0:ℝ)≤16 by norm_num)] + norm_num + _ ≤ _ := ENNReal.ofReal_le_ofReal (by + dsimp [D] + have H : (1:ℝ) ≤ 2^j := one_le_pow₀ (by norm_num) + linarith) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Haar.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Haar.lean index 0512f55b034809f5e6652f7e2e82cdf3f6e7c940..6cc5c407ede8c25ea63589c3906eab21359c04ff 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Haar.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Haar.lean @@ -1,198 +1,198 @@ -import OAI.Analysis.FourierLLogL.Fourier.Interval -/-! # Delayed Haar shifts on real intervals -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def box (a s : ℝ) := Ico a (a+s) - -def haarH : ℝ → ℝ := - (Ico (0:ℝ) (1/2)).indicator (fun _ => 1) - - (Ico (1/2:ℝ) 1).indicator (fun _ => 1) - -def haarG (x : ℝ) := haarH (2*x) - haarH (2*x-1) - -lemma measurableH : Measurable haarH := - (measurable_const.indicator measurableSet_Ico).sub (measurable_const.indicator measurableSet_Ico) - -lemma measurableG : Measurable haarG := - (measurableH.comp (measurable_const.mul measurable_id)).sub - (measurableH.comp ((measurable_const.mul measurable_id).sub measurable_const)) - -lemma H_of_left {x : ℝ} (hx : x ∈ Ico (0:ℝ) (1/2)) : haarH x = 1 := by - have : x ∉ Ico (1/2:ℝ) 1 := fun h => (not_lt_of_ge h.1) hx.2 - rw [haarH, Pi.sub_apply, indicator_of_mem hx, indicator_of_notMem this, sub_zero] - -lemma H_of_right {x : ℝ} (hx : x ∈ Ico (1/2:ℝ) 1) : haarH x = -1 := by - have : x ∉ Ico (0:ℝ) (1/2) := fun h => (not_lt_of_ge hx.1) h.2 - rw [haarH, Pi.sub_apply, indicator_of_notMem this, indicator_of_mem hx, zero_sub] - -lemma H_outside {x : ℝ} (hx : x ∉ unitInt) : haarH x = 0 := by - have h1 : x ∉ Ico (0:ℝ) (1/2) := fun h => hx ⟨h.1,h.2.trans (by norm_num)⟩ - have h2 : x ∉ Ico (1/2:ℝ) 1 := fun h => hx ⟨le_trans (by norm_num : (0:ℝ)≤1/2) h.1,h.2⟩ - rw [haarH, Pi.sub_apply, indicator_of_notMem h1, indicator_of_notMem h2, sub_zero] - -lemma H_bound (x : ℝ) : |haarH x| ≤ 1 := by - by_cases hx : x ∈ unitInt - · rcases lt_or_ge x (1/2) with h|h - · rw [H_of_left ⟨hx.1,h⟩]; simp - · rw [H_of_right ⟨h,hx.2⟩]; simp - · simp [H_outside hx] - -lemma G_outside {x : ℝ} (hx : x ∉ unitInt) : haarG x = 0 := by - have h1 : 2*x ∉ unitInt := by - rintro ⟨h1,h2⟩ - exact hx ⟨by linarith, by linarith⟩ - have h2 : 2*x - 1 ∉ unitInt := by - rintro ⟨h1,h2⟩ - exact hx ⟨by linarith, by linarith⟩ - simp [haarG,H_outside h1,H_outside h2] - -lemma G_bound (x : ℝ) : |haarG x| ≤ 1 := by - by_cases hx : 2*x ∈ unitInt - · have hx' : 2*x-1 ∉ unitInt := by - intro h - linarith [hx.2,h.1] - simpa [haarG,H_outside hx'] using H_bound (2*x) - · simpa [haarG,H_outside hx] using H_bound (2*x-1) - -def side (n : ℕ) : ℝ := ((1:ℝ) / 2)^n - -lemma side_pos (n : ℕ) : 0 < side n := by unfold side; positivity - -lemma side_le (n : ℕ) : side n ≤ 1 := by unfold side; exact pow_le_one₀ (by norm_num) (by norm_num) - -lemma pow_mul_side (n : ℕ) : (2:ℝ)^n * side n = 1 := by rw [side, ← mul_pow]; norm_num - -lemma side_succ (n : ℕ) : side (n+1) = side n / 2 := by simp [side, pow_succ, div_eq_mul_inv] - -def lft (a s : ℝ) (n k : ℕ) : ℝ := a + k*(s * side n) - -lemma cell_in_top (a : ℝ) {s : ℝ} {n k : ℕ} (hs : 0 < s) (hk : k < 2^n) : - box (lft a s n k) (s * side n) ⊆ box a s := by - intro x hx - rcases hx with ⟨h1,h2⟩ - constructor - · refine le_trans ?_ h1 - exact le_add_of_nonneg_right (mul_nonneg (Nat.cast_nonneg _) (mul_pos hs (side_pos _)).le) - · refine lt_of_lt_of_le h2 ?_ - calc - lft a s n k + s * side n = a + ((k+1:ℕ) : ℝ) * (s*side n) := by - push_cast; unfold lft; ring - _ ≤ a + ((2:ℝ)^n) * (s*side n) := - add_le_add_right (mul_le_mul_of_nonneg_right - (by exact_mod_cast (Nat.succ_le_of_lt hk)) (mul_pos hs (side_pos _)).le) _ - _ = a + s := by rw [mul_left_comm, pow_mul_side, mul_one] - -lemma norm_mem_box {a s x : ℝ} (hs : 0 < s) : - (x-a)/s ∈ unitInt ↔ x ∈ box a s := by - unfold unitInt box - simp only [mem_Ico, le_div_iff₀ hs, div_lt_iff₀ hs, one_mul, zero_mul] - constructor - · intro ⟨h1,h2⟩; constructor <;> linarith - · intro ⟨h1,h2⟩; constructor <;> linarith - -def coeffH (F : ℝ → ℂ) (a r : ℝ) := - ∫ v, haarH v • F (a+r*v) ∂μ₁ - -def jump (F : ℝ → ℂ) (a r x : ℝ) := - haarG ((x-a)/r) • coeffH F a r - -def haarLayer (F : ℝ → ℂ) (a s : ℝ) (n : ℕ) (x : ℝ) := - ∑ k ∈ Finset.range (2^n), jump F (lft a s n k) (s*side n) x - -def shift (F : ℝ → ℂ) (a s : ℝ) (J : ℕ) (x : ℝ) := - ∑ n ∈ Finset.range J, haarLayer F a s n x - -def modρ (ρ : ℝ → ℝ) (u : ℝ) (x : ℝ) : ℂ := (ρ x:ℂ)* phase u x - -def maxShift (ρ : ℝ → ℝ) (a s : ℝ) (J : ℕ) (x : ℝ) : ℝ≥0∞ := - ⨆ n : ℤ, ENNReal.ofReal ‖shift (modρ ρ n) a s J x‖ - -def density (ρ : ℝ → ℝ) (a s : ℝ) := ∫ y, ρ (a+s*y) ∂μ₁ - -def usable (ρ : ℝ → ℝ) (a s : ℝ) (J : ℕ) (B : ℝ) (x : ℝ) : Prop := - ∀ n, n < J → ∀ k, k < 2^n → x ∈ box (lft a s n k) (s*side n) → - density ρ (lft a s n k) (s*side n) ≤ B - -/-- A cut dyadic statement on one top. Used only at levels B polynomial in log d. -/ -def UnitTreeBound : Prop := - ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (ρ : ℝ → ℝ), - Measurable ρ → (∀ x, 0 ≤ ρ x) → (∫ x, ρ x ∂μ₁) ≤ 2 → - (∀ x, ρ x ≤ Real.exp ((2 : ℝ)^j)) → - ∀ (J : ℕ) (G : Set ℝ), MeasurableSet G → G ⊆ unitInt → - (∀ x ∈ G, usable ρ 0 1 J (((j:ℝ)+1)^3) x) → - (∫⁻ x in G, maxShift ρ 0 1 J x) ≤ ENNReal.ofReal (C * (2:ℝ)^j) - -lemma jump_outside {a r x : ℝ} (hr : 0 < r) (hx : x ∉ box a r) (F) : - jump F a r x = 0 := by simp only [jump, G_outside (mt (norm_mem_box hr).mp hx), zero_smul] - -lemma shift_outside {a s x : ℝ} (hs : 0 < s) (hx : x ∉ box a s) (F J) : - shift F a s J x = 0 := by - refine Finset.sum_eq_zero fun n _ => Finset.sum_eq_zero fun k hk => ?_ - exact jump_outside (mul_pos hs (side_pos _)) (fun H => hx (cell_in_top a hs - (Finset.mem_range.mp hk) H)) _ - -lemma measurable_mod {ρ : ℝ → ℝ} (hρ : Measurable ρ) (u : ℝ) : Measurable (modρ ρ u) := - (Complex.continuous_ofReal.measurable.comp hρ).mul (phase_cont u).measurable - -section meas -variable {X : Type*} [MeasurableSpace X] {A R O : X → ℝ} - {F : ℝ → ℂ} - -lemma measurable_jump_comp (hF : Measurable F) (ha : Measurable A) (hr : Measurable R) - (ho : Measurable O) : Measurable fun x => jump F (A x) (R x) (O x) := by - refine (measurableG.comp ((ho.sub ha).div hr)).smul ?_ - apply StronglyMeasurable.measurable - unfold coeffH - refine StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable ?_) - exact (measurableH.comp measurable_snd).smul (hF.comp - ((ha.comp measurable_fst).add ((hr.comp measurable_fst).mul measurable_snd))) - -lemma measurable_shift_comp (hF : Measurable F) (ha : Measurable A) (hr : Measurable R) - (ho : Measurable O) (J : ℕ) : Measurable fun x => shift F (A x) (R x) J (O x) := - Finset.measurable_sum _ (fun _ _ => Finset.measurable_sum _ (fun _ _ => - measurable_jump_comp hF (ha.add (measurable_const.mul (hr.mul measurable_const))) - (hr.mul measurable_const) ho)) - -lemma measurable_maxShift_comp {ρ : ℝ → ℝ} (hρ : Measurable ρ) (ha : Measurable A) (hr : Measurable R) - (ho : Measurable O) (J) : Measurable fun x => maxShift ρ (A x) (R x) J (O x) := - Measurable.iSup (fun _ => (measurable_shift_comp (measurable_mod hρ _) ha hr ho J).norm.ennreal_ofReal) - -end meas - -lemma bounded_jump {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - (a r x : ℝ) : ‖jump F a r x‖ ≤ B := by - have h (v : ℝ) : ‖haarH v • F (a+r*v)‖ ≤ B := by - rw [norm_smul, Real.norm_eq_abs] - exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (hb _) - have hm : Measurable (fun v => haarH v • F (a+r*v)) := - measurableH.smul (hf.comp (measurable_const.add (measurable_const.mul measurable_id))) - rw [jump, norm_smul, Real.norm_eq_abs] - refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans - ((norm_integral_le_integral_norm _).trans ?_) - apply le_trans (b:= ∫ _, B ∂μ₁) - · exact integral_mono (Integrable.of_bound hm.aestronglyMeasurable B (ae_of_all _ h)).norm - (integrable_const _) h - · simp - -lemma bounded_haarLayer {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - (a s : ℝ) (n : ℕ) (x : ℝ) : ‖haarLayer F a s n x‖ ≤ (2:ℝ)^n * B := by - apply (norm_sum_le _ _).trans - apply le_trans (b := ∑ _ ∈ Finset.range ((2:ℕ)^n), B) - · exact Finset.sum_le_sum fun k _ => bounded_jump hf B hb _ _ _ - simp - -lemma bounded_shift {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - (a s : ℝ) (J : ℕ) (x : ℝ) : ‖shift F a s J x‖ ≤ ∑ n ∈ Finset.range J, (2:ℝ)^n * B := - (norm_sum_le _ _).trans (Finset.sum_le_sum fun _ _ => bounded_haarLayer hf B hb _ _ _ _) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Interval +/-! # Delayed Haar shifts on real intervals -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def box (a s : ℝ) := Ico a (a+s) + +def haarH : ℝ → ℝ := + (Ico (0:ℝ) (1/2)).indicator (fun _ => 1) - + (Ico (1/2:ℝ) 1).indicator (fun _ => 1) + +def haarG (x : ℝ) := haarH (2*x) - haarH (2*x-1) + +lemma measurableH : Measurable haarH := + (measurable_const.indicator measurableSet_Ico).sub (measurable_const.indicator measurableSet_Ico) + +lemma measurableG : Measurable haarG := + (measurableH.comp (measurable_const.mul measurable_id)).sub + (measurableH.comp ((measurable_const.mul measurable_id).sub measurable_const)) + +lemma H_of_left {x : ℝ} (hx : x ∈ Ico (0:ℝ) (1/2)) : haarH x = 1 := by + have : x ∉ Ico (1/2:ℝ) 1 := fun h => (not_lt_of_ge h.1) hx.2 + rw [haarH, Pi.sub_apply, indicator_of_mem hx, indicator_of_notMem this, sub_zero] + +lemma H_of_right {x : ℝ} (hx : x ∈ Ico (1/2:ℝ) 1) : haarH x = -1 := by + have : x ∉ Ico (0:ℝ) (1/2) := fun h => (not_lt_of_ge hx.1) h.2 + rw [haarH, Pi.sub_apply, indicator_of_notMem this, indicator_of_mem hx, zero_sub] + +lemma H_outside {x : ℝ} (hx : x ∉ unitInt) : haarH x = 0 := by + have h1 : x ∉ Ico (0:ℝ) (1/2) := fun h => hx ⟨h.1,h.2.trans (by norm_num)⟩ + have h2 : x ∉ Ico (1/2:ℝ) 1 := fun h => hx ⟨le_trans (by norm_num : (0:ℝ)≤1/2) h.1,h.2⟩ + rw [haarH, Pi.sub_apply, indicator_of_notMem h1, indicator_of_notMem h2, sub_zero] + +lemma H_bound (x : ℝ) : |haarH x| ≤ 1 := by + by_cases hx : x ∈ unitInt + · rcases lt_or_ge x (1/2) with h|h + · rw [H_of_left ⟨hx.1,h⟩]; simp + · rw [H_of_right ⟨h,hx.2⟩]; simp + · simp [H_outside hx] + +lemma G_outside {x : ℝ} (hx : x ∉ unitInt) : haarG x = 0 := by + have h1 : 2*x ∉ unitInt := by + rintro ⟨h1,h2⟩ + exact hx ⟨by linarith, by linarith⟩ + have h2 : 2*x - 1 ∉ unitInt := by + rintro ⟨h1,h2⟩ + exact hx ⟨by linarith, by linarith⟩ + simp [haarG,H_outside h1,H_outside h2] + +lemma G_bound (x : ℝ) : |haarG x| ≤ 1 := by + by_cases hx : 2*x ∈ unitInt + · have hx' : 2*x-1 ∉ unitInt := by + intro h + linarith [hx.2,h.1] + simpa [haarG,H_outside hx'] using H_bound (2*x) + · simpa [haarG,H_outside hx] using H_bound (2*x-1) + +def side (n : ℕ) : ℝ := ((1:ℝ) / 2)^n + +lemma side_pos (n : ℕ) : 0 < side n := by unfold side; positivity + +lemma side_le (n : ℕ) : side n ≤ 1 := by unfold side; exact pow_le_one₀ (by norm_num) (by norm_num) + +lemma pow_mul_side (n : ℕ) : (2:ℝ)^n * side n = 1 := by rw [side, ← mul_pow]; norm_num + +lemma side_succ (n : ℕ) : side (n+1) = side n / 2 := by simp [side, pow_succ, div_eq_mul_inv] + +def lft (a s : ℝ) (n k : ℕ) : ℝ := a + k*(s * side n) + +lemma cell_in_top (a : ℝ) {s : ℝ} {n k : ℕ} (hs : 0 < s) (hk : k < 2^n) : + box (lft a s n k) (s * side n) ⊆ box a s := by + intro x hx + rcases hx with ⟨h1,h2⟩ + constructor + · refine le_trans ?_ h1 + exact le_add_of_nonneg_right (mul_nonneg (Nat.cast_nonneg _) (mul_pos hs (side_pos _)).le) + · refine lt_of_lt_of_le h2 ?_ + calc + lft a s n k + s * side n = a + ((k+1:ℕ) : ℝ) * (s*side n) := by + push_cast; unfold lft; ring + _ ≤ a + ((2:ℝ)^n) * (s*side n) := + add_le_add_right (mul_le_mul_of_nonneg_right + (by exact_mod_cast (Nat.succ_le_of_lt hk)) (mul_pos hs (side_pos _)).le) _ + _ = a + s := by rw [mul_left_comm, pow_mul_side, mul_one] + +lemma norm_mem_box {a s x : ℝ} (hs : 0 < s) : + (x-a)/s ∈ unitInt ↔ x ∈ box a s := by + unfold unitInt box + simp only [mem_Ico, le_div_iff₀ hs, div_lt_iff₀ hs, one_mul, zero_mul] + constructor + · intro ⟨h1,h2⟩; constructor <;> linarith + · intro ⟨h1,h2⟩; constructor <;> linarith + +def coeffH (F : ℝ → ℂ) (a r : ℝ) := + ∫ v, haarH v • F (a+r*v) ∂μ₁ + +def jump (F : ℝ → ℂ) (a r x : ℝ) := + haarG ((x-a)/r) • coeffH F a r + +def haarLayer (F : ℝ → ℂ) (a s : ℝ) (n : ℕ) (x : ℝ) := + ∑ k ∈ Finset.range (2^n), jump F (lft a s n k) (s*side n) x + +def shift (F : ℝ → ℂ) (a s : ℝ) (J : ℕ) (x : ℝ) := + ∑ n ∈ Finset.range J, haarLayer F a s n x + +def modρ (ρ : ℝ → ℝ) (u : ℝ) (x : ℝ) : ℂ := (ρ x:ℂ)* phase u x + +def maxShift (ρ : ℝ → ℝ) (a s : ℝ) (J : ℕ) (x : ℝ) : ℝ≥0∞ := + ⨆ n : ℤ, ENNReal.ofReal ‖shift (modρ ρ n) a s J x‖ + +def density (ρ : ℝ → ℝ) (a s : ℝ) := ∫ y, ρ (a+s*y) ∂μ₁ + +def usable (ρ : ℝ → ℝ) (a s : ℝ) (J : ℕ) (B : ℝ) (x : ℝ) : Prop := + ∀ n, n < J → ∀ k, k < 2^n → x ∈ box (lft a s n k) (s*side n) → + density ρ (lft a s n k) (s*side n) ≤ B + +/-- A cut dyadic statement on one top. Used only at levels B polynomial in log d. -/ +def UnitTreeBound : Prop := + ∃ C : ℝ, 0 ≤ C ∧ ∀ᶠ j : ℕ in atTop, ∀ (ρ : ℝ → ℝ), + Measurable ρ → (∀ x, 0 ≤ ρ x) → (∫ x, ρ x ∂μ₁) ≤ 2 → + (∀ x, ρ x ≤ Real.exp ((2 : ℝ)^j)) → + ∀ (J : ℕ) (G : Set ℝ), MeasurableSet G → G ⊆ unitInt → + (∀ x ∈ G, usable ρ 0 1 J (((j:ℝ)+1)^3) x) → + (∫⁻ x in G, maxShift ρ 0 1 J x) ≤ ENNReal.ofReal (C * (2:ℝ)^j) + +lemma jump_outside {a r x : ℝ} (hr : 0 < r) (hx : x ∉ box a r) (F) : + jump F a r x = 0 := by simp only [jump, G_outside (mt (norm_mem_box hr).mp hx), zero_smul] + +lemma shift_outside {a s x : ℝ} (hs : 0 < s) (hx : x ∉ box a s) (F J) : + shift F a s J x = 0 := by + refine Finset.sum_eq_zero fun n _ => Finset.sum_eq_zero fun k hk => ?_ + exact jump_outside (mul_pos hs (side_pos _)) (fun H => hx (cell_in_top a hs + (Finset.mem_range.mp hk) H)) _ + +lemma measurable_mod {ρ : ℝ → ℝ} (hρ : Measurable ρ) (u : ℝ) : Measurable (modρ ρ u) := + (Complex.continuous_ofReal.measurable.comp hρ).mul (phase_cont u).measurable + +section meas +variable {X : Type*} [MeasurableSpace X] {A R O : X → ℝ} + {F : ℝ → ℂ} + +lemma measurable_jump_comp (hF : Measurable F) (ha : Measurable A) (hr : Measurable R) + (ho : Measurable O) : Measurable fun x => jump F (A x) (R x) (O x) := by + refine (measurableG.comp ((ho.sub ha).div hr)).smul ?_ + apply StronglyMeasurable.measurable + unfold coeffH + refine StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable ?_) + exact (measurableH.comp measurable_snd).smul (hF.comp + ((ha.comp measurable_fst).add ((hr.comp measurable_fst).mul measurable_snd))) + +lemma measurable_shift_comp (hF : Measurable F) (ha : Measurable A) (hr : Measurable R) + (ho : Measurable O) (J : ℕ) : Measurable fun x => shift F (A x) (R x) J (O x) := + Finset.measurable_sum _ (fun _ _ => Finset.measurable_sum _ (fun _ _ => + measurable_jump_comp hF (ha.add (measurable_const.mul (hr.mul measurable_const))) + (hr.mul measurable_const) ho)) + +lemma measurable_maxShift_comp {ρ : ℝ → ℝ} (hρ : Measurable ρ) (ha : Measurable A) (hr : Measurable R) + (ho : Measurable O) (J) : Measurable fun x => maxShift ρ (A x) (R x) J (O x) := + Measurable.iSup (fun _ => (measurable_shift_comp (measurable_mod hρ _) ha hr ho J).norm.ennreal_ofReal) + +end meas + +lemma bounded_jump {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + (a r x : ℝ) : ‖jump F a r x‖ ≤ B := by + have h (v : ℝ) : ‖haarH v • F (a+r*v)‖ ≤ B := by + rw [norm_smul, Real.norm_eq_abs] + exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (hb _) + have hm : Measurable (fun v => haarH v • F (a+r*v)) := + measurableH.smul (hf.comp (measurable_const.add (measurable_const.mul measurable_id))) + rw [jump, norm_smul, Real.norm_eq_abs] + refine (mul_le_of_le_one_left (norm_nonneg _) (G_bound _)).trans + ((norm_integral_le_integral_norm _).trans ?_) + apply le_trans (b:= ∫ _, B ∂μ₁) + · exact integral_mono (Integrable.of_bound hm.aestronglyMeasurable B (ae_of_all _ h)).norm + (integrable_const _) h + · simp + +lemma bounded_haarLayer {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + (a s : ℝ) (n : ℕ) (x : ℝ) : ‖haarLayer F a s n x‖ ≤ (2:ℝ)^n * B := by + apply (norm_sum_le _ _).trans + apply le_trans (b := ∑ _ ∈ Finset.range ((2:ℕ)^n), B) + · exact Finset.sum_le_sum fun k _ => bounded_jump hf B hb _ _ _ + simp + +lemma bounded_shift {F : ℝ → ℂ} (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + (a s : ℝ) (J : ℕ) (x : ℝ) : ‖shift F a s J x‖ ≤ ∑ n ∈ Finset.range J, (2:ℝ)^n * B := + (norm_sum_le _ _).trans (Finset.sum_le_sum fun _ _ => bounded_haarLayer hf B hb _ _ _ _) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Interval.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Interval.lean index 832b664bdbab20e6d36a09cc2d0a2f4f1a7e5b95..21421bb13c5674e72b7f89ab8c9ed808a84b3cbe 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Interval.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Interval.lean @@ -1,164 +1,164 @@ -import OAI.Analysis.FourierLLogL.Fourier.Coordinates - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -section segment -variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] - -lemma seg_eq_interval (f : ℝ → E) {a b : ℝ} (hab : a ≤ b) : - (∫ x in Ico a b, f x) = ∫ x in a..b, f x := by - rw [intervalIntegral.integral_of_le hab] - apply setIntegral_congr_set - exact Ico_ae_eq_Ioc - -lemma seg_translate_scale (f : ℝ → E) (d : ℝ) {a b c : ℝ} (hab : a ≤ b) (hc : 0 < c) : - (∫ x in Ico a b, f (d + c*x)) = c⁻¹ • ∫ x in Ico (d+c*a) (d+c*b), f x := by - rw [seg_eq_interval _ hab, intervalIntegral.integral_comp_add_mul f hc.ne', - seg_eq_interval _ (by gcongr)] - -lemma seg_translate_scale_rev (f : ℝ → E) (d : ℝ) {a b c : ℝ} (hab : a ≤ b) (hc : 0 < c) : - (∫ x in Ico a b, f (d - c*x)) = c⁻¹ • ∫ x in Ico (d-c*b) (d-c*a), f x := by - simp only [sub_eq_add_neg, ← neg_mul] - rw [seg_eq_interval _ hab, intervalIntegral.integral_comp_add_mul f (neg_ne_zero.mpr hc.ne'), - seg_eq_interval _ (by nlinarith : d + (-c)*b ≤ d + (-c)*a), - intervalIntegral.integral_symm, inv_neg] - simp - -lemma mean_translate_scale (f : ℝ → E) (d : ℝ) {c : ℝ} (hc : 0 < c) : - (∫ x, f (d + c*x) ∂μ₁) = c⁻¹ • ∫ x in Ico d (d+c), f x := by - simpa [μ₁, unitInt] using - seg_translate_scale f d (by norm_num : (0:ℝ) ≤ 1) hc - -lemma seg_period_unit (f : ℝ → E) (hf : Periodic f 1) (a : ℝ) : - (∫ x in Ico a (a+1), f x) = ∫ x, f x ∂μ₁ := by - rw [seg_eq_interval _ (by linarith), hf.intervalIntegral_add_eq a 0, - μ₁, unitInt, seg_eq_interval _ (by linarith)] - simp - -lemma seg_period_int (f : ℝ → E) (hf : Periodic f 1) - (hfi : ∀ a b, IntervalIntegrable f volume a b) (a : ℝ) (k : ℕ) : - (∫ x in Ico a (a+k), f x) = k • ∫ x, f x ∂μ₁ := by - rw [seg_eq_interval _ (by simp : a ≤ a+(k:ℝ)), ← seg_period_unit f hf a, - seg_eq_interval _ (by linarith)] - simpa using hf.intervalIntegral_add_zsmul_eq (k:ℤ) a hfi - -variable [MeasurableSpace E] [BorelSpace E] [SecondCountableTopology E] - -omit [NormedSpace ℝ E] in -lemma seg_integrable (f : ℝ → E) (h : Measurable f) (B : ℝ) (a b : ℝ) - (hB : ∀ x ∈ Ico a b, ‖f x‖ ≤ B) : - IntegrableOn f (Ico a b) := by - exact Integrable.of_bound h.aestronglyMeasurable B - ((ae_restrict_mem measurableSet_Ico).mono hB) - -omit [NormedSpace ℝ E] in -lemma interval_integrable (f : ℝ → E) (h : Measurable f) (B : ℝ) (hB : ∀ x, ‖f x‖ ≤ B) - (a b : ℝ) : IntervalIntegrable f volume a b := - ⟨Integrable.of_bound h.aestronglyMeasurable B (ae_of_all _ hB), - Integrable.of_bound h.aestronglyMeasurable B (ae_of_all _ hB)⟩ - -end segment - -lemma density_short (ρ : ℝ → ℝ) (hρ : Measurable ρ) (hp : Periodic ρ 1) - (h0 : ∀ x, 0 ≤ ρ x) (B : ℝ) (hb : ∀ x, ρ x ≤ B) - (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (a s : ℝ) (hs : s ≤ 1) : - (∫ x in Ico a (a+s), ρ x) ≤ 1 := by - have hsub : Ico a (a+s) ⊆ Ico a (a+1) := Ico_subset_Ico_right (by gcongr) - apply le_trans (b := ∫ x in Ico a (a+1), ρ x) - · exact setIntegral_mono_set - (seg_integrable _ hρ B _ _ (fun x _ => by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 x)] using hb x)) - (ae_of_all _ h0) (ae_of_all _ (fun _ hx => hsub hx)) - · rwa [seg_period_unit _ hp] - -/-- One good set works for *all* offsets and dilations of the grids. -/ -theorem density_good_set (ρ : ℝ → ℝ) (hρ : Measurable ρ) (hp : Periodic ρ 1) - (h0 : ∀ x, 0 ≤ ρ x) (B : ℝ) (hb : ∀ x, ρ x ≤ B) - (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (D : ℝ) (hD : 0 < D) : - ∃ G : Set ℝ, MeasurableSet G ∧ G ⊆ unitInt ∧ - μ₁ Gᶜ ≤ ENNReal.ofReal (64 / D) ∧ - ∀ x ∈ G, ∀ (a s : ℝ), 0 < s → s ≤ 1 → x ∈ Ico a (a+s) → - (∫ y in Ico a (a+s), ρ y) ≤ D*s := by - let W (p : ℝ × ℝ) := Metric.ball p.1 p.2 - let T : Set (ℝ × ℝ) := {p | 0 < p.2 ∧ p.2 ≤ 1 ∧ W p ⊆ Ico (-4) 4 ∧ - D*p.2 < ∫ y in W p, ρ y} - let A := ⋃ p ∈ T, W p - have hWo (p) : IsOpen (W p) := Metric.isOpen_ball - have hAo : IsOpen A := isOpen_iUnion fun p => isOpen_iUnion fun _ => hWo p - obtain ⟨V,hVT,hdis,hV⟩ := - Vitali.exists_disjoint_subfamily_covering_enlargement_ball T Prod.fst Prod.snd - 1 (fun p h => h.2.1) 4 (by norm_num) - have cnt : V.Countable := PairwiseDisjoint.countable_of_isOpen (s:=W) hdis - (fun p _ => hWo p) fun p hp => ⟨p.1, Metric.mem_ball_self (hVT hp).1⟩ - have hv (p) : ‖ρ p‖ ≤ B := by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 p)] using hb p - have hi : IntegrableOn ρ (Ico (-4:ℝ) 4) := - seg_integrable _ hρ B _ _ (fun p _ => hv p) - have hneg : (∫ x in Ico (-4:ℝ) 4, ρ x) ≤ 8 := by - have HH := seg_period_int ρ hp (interval_integrable ρ hρ B hv) (-4) 8 - norm_num at HH - rw [HH] - linarith - let ρ' (y : ℝ) := ENNReal.ofReal (ρ y) - have hi' : (∫⁻ x in Ico (-4:ℝ) 4, ρ' x) ≤ ENNReal.ofReal 8 := by - dsimp only [ρ'] - rw [← ofReal_integral_eq_lintegral_ofReal hi (ae_of_all _ h0)] - exact ENNReal.ofReal_le_ofReal hneg - have hcov : A ⊆ ⋃ p ∈ V, Metric.ball p.1 (4 * p.2) := by - refine iUnion₂_subset fun p hp => ?_ - obtain ⟨q,hq,hpq⟩ := hV p hp - exact fun x hx => mem_iUnion₂.mpr ⟨q,hq,hpq hx⟩ - have hsingle (p : ℝ × ℝ) (hp : p ∈ V) : - volume (Metric.ball p.1 (4 * p.2)) ≤ ENNReal.ofReal (8 / D) * ∫⁻ x in W p, ρ' x := by - have hp' := hVT hp - rw [Real.volume_ball, ← ofReal_integral_eq_lintegral_ofReal (hi.mono_set hp'.2.2.1) - (ae_of_all _ h0), ← ENNReal.ofReal_mul (by positivity)] - apply ENNReal.ofReal_le_ofReal - have hh := hp'.2.2.2 - calc - _ = 8 / D * (D * p.2) := by field_simp; ring - _ ≤ _ := by gcongr - have : Countable V := cnt.to_subtype - have htot : volume A ≤ ENNReal.ofReal (64 / D) := by - refine ((measure_mono hcov).trans (measure_biUnion_le volume cnt _)).trans - ((ENNReal.tsum_le_tsum (fun i : V => hsingle i.val i.property)).trans ?_) - rw [ENNReal.tsum_mul_left] - have hlim : (∑' p : V, ∫⁻ x in W p, ρ' x) ≤ ENNReal.ofReal 8 := by - rw [← lintegral_iUnion] - · refine le_trans (lintegral_mono_set (iUnion_subset (fun (p : V) => ?_))) hi' - exact (hVT p.property).2.2.1 - · exact fun p => (hWo p).measurableSet - · exact fun i j h => hdis i.property j.property (fun e => h (Subtype.ext e)) - refine (mul_le_mul_right hlim _).trans (le_of_eq ?_) - rw [← ENNReal.ofReal_mul (by positivity : 0 ≤ 8 / D)] - ring_nf - refine ⟨unitInt \ A, measurable_unitInt.diff hAo.measurableSet, sdiff_subset, ?_, ?_⟩ - · rw [μ₁, Measure.restrict_apply (measurable_unitInt.diff hAo.measurableSet).compl] - exact (measure_mono (by grind : ((unitInt \ A)ᶜ ∩ unitInt) ⊆ A)).trans htot - · intro x hx a s hs hs' hxs - by_contra hn - let p := (a+s/2,s) - have hsub : Ico a (a+s) ⊆ W p := by - intro y hy - rcases hy with ⟨h1,h2⟩ - simp only [W, p, Real.ball_eq_Ioo, mem_Ioo] - constructor <;> linarith - have hsub' : W p ⊆ Ico (-4:ℝ) 4 := by - intro y hy - simp only [W,p,Real.ball_eq_Ioo, mem_Ioo] at hy - rcases hx.1 with ⟨h1,h2⟩ - rcases hxs with ⟨h3,h4⟩ - constructor <;> linarith [hy.1, hy.2] - have hp : p ∈ T := ⟨hs, hs', hsub', lt_of_lt_of_le (lt_of_not_ge hn) - (setIntegral_mono_set (hi.mono_set hsub') (ae_of_all _ h0) - (ae_of_all _ (fun _ h => hsub h)))⟩ - exact hx.2 (mem_iUnion₂.mpr ⟨p,hp,hsub hxs⟩) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Coordinates + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +section segment +variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] + +lemma seg_eq_interval (f : ℝ → E) {a b : ℝ} (hab : a ≤ b) : + (∫ x in Ico a b, f x) = ∫ x in a..b, f x := by + rw [intervalIntegral.integral_of_le hab] + apply setIntegral_congr_set + exact Ico_ae_eq_Ioc + +lemma seg_translate_scale (f : ℝ → E) (d : ℝ) {a b c : ℝ} (hab : a ≤ b) (hc : 0 < c) : + (∫ x in Ico a b, f (d + c*x)) = c⁻¹ • ∫ x in Ico (d+c*a) (d+c*b), f x := by + rw [seg_eq_interval _ hab, intervalIntegral.integral_comp_add_mul f hc.ne', + seg_eq_interval _ (by gcongr)] + +lemma seg_translate_scale_rev (f : ℝ → E) (d : ℝ) {a b c : ℝ} (hab : a ≤ b) (hc : 0 < c) : + (∫ x in Ico a b, f (d - c*x)) = c⁻¹ • ∫ x in Ico (d-c*b) (d-c*a), f x := by + simp only [sub_eq_add_neg, ← neg_mul] + rw [seg_eq_interval _ hab, intervalIntegral.integral_comp_add_mul f (neg_ne_zero.mpr hc.ne'), + seg_eq_interval _ (by nlinarith : d + (-c)*b ≤ d + (-c)*a), + intervalIntegral.integral_symm, inv_neg] + simp + +lemma mean_translate_scale (f : ℝ → E) (d : ℝ) {c : ℝ} (hc : 0 < c) : + (∫ x, f (d + c*x) ∂μ₁) = c⁻¹ • ∫ x in Ico d (d+c), f x := by + simpa [μ₁, unitInt] using + seg_translate_scale f d (by norm_num : (0:ℝ) ≤ 1) hc + +lemma seg_period_unit (f : ℝ → E) (hf : Periodic f 1) (a : ℝ) : + (∫ x in Ico a (a+1), f x) = ∫ x, f x ∂μ₁ := by + rw [seg_eq_interval _ (by linarith), hf.intervalIntegral_add_eq a 0, + μ₁, unitInt, seg_eq_interval _ (by linarith)] + simp + +lemma seg_period_int (f : ℝ → E) (hf : Periodic f 1) + (hfi : ∀ a b, IntervalIntegrable f volume a b) (a : ℝ) (k : ℕ) : + (∫ x in Ico a (a+k), f x) = k • ∫ x, f x ∂μ₁ := by + rw [seg_eq_interval _ (by simp : a ≤ a+(k:ℝ)), ← seg_period_unit f hf a, + seg_eq_interval _ (by linarith)] + simpa using hf.intervalIntegral_add_zsmul_eq (k:ℤ) a hfi + +variable [MeasurableSpace E] [BorelSpace E] [SecondCountableTopology E] + +omit [NormedSpace ℝ E] in +lemma seg_integrable (f : ℝ → E) (h : Measurable f) (B : ℝ) (a b : ℝ) + (hB : ∀ x ∈ Ico a b, ‖f x‖ ≤ B) : + IntegrableOn f (Ico a b) := by + exact Integrable.of_bound h.aestronglyMeasurable B + ((ae_restrict_mem measurableSet_Ico).mono hB) + +omit [NormedSpace ℝ E] in +lemma interval_integrable (f : ℝ → E) (h : Measurable f) (B : ℝ) (hB : ∀ x, ‖f x‖ ≤ B) + (a b : ℝ) : IntervalIntegrable f volume a b := + ⟨Integrable.of_bound h.aestronglyMeasurable B (ae_of_all _ hB), + Integrable.of_bound h.aestronglyMeasurable B (ae_of_all _ hB)⟩ + +end segment + +lemma density_short (ρ : ℝ → ℝ) (hρ : Measurable ρ) (hp : Periodic ρ 1) + (h0 : ∀ x, 0 ≤ ρ x) (B : ℝ) (hb : ∀ x, ρ x ≤ B) + (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (a s : ℝ) (hs : s ≤ 1) : + (∫ x in Ico a (a+s), ρ x) ≤ 1 := by + have hsub : Ico a (a+s) ⊆ Ico a (a+1) := Ico_subset_Ico_right (by gcongr) + apply le_trans (b := ∫ x in Ico a (a+1), ρ x) + · exact setIntegral_mono_set + (seg_integrable _ hρ B _ _ (fun x _ => by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 x)] using hb x)) + (ae_of_all _ h0) (ae_of_all _ (fun _ hx => hsub hx)) + · rwa [seg_period_unit _ hp] + +/-- One good set works for *all* offsets and dilations of the grids. -/ +theorem density_good_set (ρ : ℝ → ℝ) (hρ : Measurable ρ) (hp : Periodic ρ 1) + (h0 : ∀ x, 0 ≤ ρ x) (B : ℝ) (hb : ∀ x, ρ x ≤ B) + (hm : (∫ x, ρ x ∂μ₁) ≤ 1) (D : ℝ) (hD : 0 < D) : + ∃ G : Set ℝ, MeasurableSet G ∧ G ⊆ unitInt ∧ + μ₁ Gᶜ ≤ ENNReal.ofReal (64 / D) ∧ + ∀ x ∈ G, ∀ (a s : ℝ), 0 < s → s ≤ 1 → x ∈ Ico a (a+s) → + (∫ y in Ico a (a+s), ρ y) ≤ D*s := by + let W (p : ℝ × ℝ) := Metric.ball p.1 p.2 + let T : Set (ℝ × ℝ) := {p | 0 < p.2 ∧ p.2 ≤ 1 ∧ W p ⊆ Ico (-4) 4 ∧ + D*p.2 < ∫ y in W p, ρ y} + let A := ⋃ p ∈ T, W p + have hWo (p) : IsOpen (W p) := Metric.isOpen_ball + have hAo : IsOpen A := isOpen_iUnion fun p => isOpen_iUnion fun _ => hWo p + obtain ⟨V,hVT,hdis,hV⟩ := + Vitali.exists_disjoint_subfamily_covering_enlargement_ball T Prod.fst Prod.snd + 1 (fun p h => h.2.1) 4 (by norm_num) + have cnt : V.Countable := PairwiseDisjoint.countable_of_isOpen (s:=W) hdis + (fun p _ => hWo p) fun p hp => ⟨p.1, Metric.mem_ball_self (hVT hp).1⟩ + have hv (p) : ‖ρ p‖ ≤ B := by simpa [Real.norm_eq_abs, abs_of_nonneg (h0 p)] using hb p + have hi : IntegrableOn ρ (Ico (-4:ℝ) 4) := + seg_integrable _ hρ B _ _ (fun p _ => hv p) + have hneg : (∫ x in Ico (-4:ℝ) 4, ρ x) ≤ 8 := by + have HH := seg_period_int ρ hp (interval_integrable ρ hρ B hv) (-4) 8 + norm_num at HH + rw [HH] + linarith + let ρ' (y : ℝ) := ENNReal.ofReal (ρ y) + have hi' : (∫⁻ x in Ico (-4:ℝ) 4, ρ' x) ≤ ENNReal.ofReal 8 := by + dsimp only [ρ'] + rw [← ofReal_integral_eq_lintegral_ofReal hi (ae_of_all _ h0)] + exact ENNReal.ofReal_le_ofReal hneg + have hcov : A ⊆ ⋃ p ∈ V, Metric.ball p.1 (4 * p.2) := by + refine iUnion₂_subset fun p hp => ?_ + obtain ⟨q,hq,hpq⟩ := hV p hp + exact fun x hx => mem_iUnion₂.mpr ⟨q,hq,hpq hx⟩ + have hsingle (p : ℝ × ℝ) (hp : p ∈ V) : + volume (Metric.ball p.1 (4 * p.2)) ≤ ENNReal.ofReal (8 / D) * ∫⁻ x in W p, ρ' x := by + have hp' := hVT hp + rw [Real.volume_ball, ← ofReal_integral_eq_lintegral_ofReal (hi.mono_set hp'.2.2.1) + (ae_of_all _ h0), ← ENNReal.ofReal_mul (by positivity)] + apply ENNReal.ofReal_le_ofReal + have hh := hp'.2.2.2 + calc + _ = 8 / D * (D * p.2) := by field_simp; ring + _ ≤ _ := by gcongr + have : Countable V := cnt.to_subtype + have htot : volume A ≤ ENNReal.ofReal (64 / D) := by + refine ((measure_mono hcov).trans (measure_biUnion_le volume cnt _)).trans + ((ENNReal.tsum_le_tsum (fun i : V => hsingle i.val i.property)).trans ?_) + rw [ENNReal.tsum_mul_left] + have hlim : (∑' p : V, ∫⁻ x in W p, ρ' x) ≤ ENNReal.ofReal 8 := by + rw [← lintegral_iUnion] + · refine le_trans (lintegral_mono_set (iUnion_subset (fun (p : V) => ?_))) hi' + exact (hVT p.property).2.2.1 + · exact fun p => (hWo p).measurableSet + · exact fun i j h => hdis i.property j.property (fun e => h (Subtype.ext e)) + refine (mul_le_mul_right hlim _).trans (le_of_eq ?_) + rw [← ENNReal.ofReal_mul (by positivity : 0 ≤ 8 / D)] + ring_nf + refine ⟨unitInt \ A, measurable_unitInt.diff hAo.measurableSet, sdiff_subset, ?_, ?_⟩ + · rw [μ₁, Measure.restrict_apply (measurable_unitInt.diff hAo.measurableSet).compl] + exact (measure_mono (by grind : ((unitInt \ A)ᶜ ∩ unitInt) ⊆ A)).trans htot + · intro x hx a s hs hs' hxs + by_contra hn + let p := (a+s/2,s) + have hsub : Ico a (a+s) ⊆ W p := by + intro y hy + rcases hy with ⟨h1,h2⟩ + simp only [W, p, Real.ball_eq_Ioo, mem_Ioo] + constructor <;> linarith + have hsub' : W p ⊆ Ico (-4:ℝ) 4 := by + intro y hy + simp only [W,p,Real.ball_eq_Ioo, mem_Ioo] at hy + rcases hx.1 with ⟨h1,h2⟩ + rcases hxs with ⟨h3,h4⟩ + constructor <;> linarith [hy.1, hy.2] + have hp : p ∈ T := ⟨hs, hs', hsub', lt_of_lt_of_le (lt_of_not_ge hn) + (setIntegral_mono_set (hi.mono_set hsub') (ae_of_all _ h0) + (ae_of_all _ (fun _ h => hsub h)))⟩ + exact hx.2 (mem_iUnion₂.mpr ⟨p,hp,hsub hxs⟩) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Kappa.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Kappa.lean index 7a85a3156446047d33a1ed2e5ea80e9ad4fb7693..a3045ff90978234a90be5bc05ea703ee1fb82de6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Kappa.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Kappa.lean @@ -1,208 +1,208 @@ -import OAI.Analysis.FourierLLogL.Fourier.Haar -/-! # The translation-averaged Haar kernel -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def kappa (t : ℝ) : ℝ := - ∫ v, haarG v * haarH (v-t) - -lemma kappa_eq_mean (t : ℝ) : kappa t = - ∫ v, haarG v * haarH (v-t) ∂μ₁ := by - apply Eq.symm - apply setIntegral_eq_integral_of_forall_compl_eq_zero - intro v hv; rw [G_outside hv, zero_mul] - -private def Step (a b x : ℝ) : ℝ := (Ico a b).indicator (fun _ => 1) x -private def P (n : ℕ) (x : ℝ) := Step ((n:ℝ)/4) ((n:ℝ)/4+1/4) x - -def hatK (x : ℝ) : ℝ := max 0 (1/4 - |x|) - -private lemma step_split {a b c : ℝ} (h : a ≤ b) (h' : b ≤ c) (x) : - Step a c x = Step a b x + Step b c x := by - unfold Step - simp only [indicator_apply, mem_Ico] - split_ifs <;> grind - -private lemma haarH_step (x) : - haarH x = (P 0 x + P 1 x) - (P 2 x + P 3 x) := by - change Step 0 (1/2) x - Step (1/2) 1 x = _ - rw [step_split (b:=1/4) (by norm_num) (by norm_num) x, - step_split (c:=1) (b:=3/4) (by norm_num) (by norm_num) x] - norm_num [P] - -private lemma step_twice (a b c x : ℝ) : - Step b c (2*x-a) = Step ((b+a)/2) ((c+a)/2) x := by - simp only [Step, indicator_apply, mem_Ico] - split_ifs <;> grind - -private lemma haarG_step (x) : - haarG x = (P 0 x - P 1 x) - (P 2 x - P 3 x) := by - change Step 0 (1/2) (2*x) - Step (1/2) 1 (2*x) - - (Step 0 (1/2) (2*x-1) - Step (1/2) 1 (2*x-1)) = _ - nth_rw 1 2 [show 2*x = 2*x - 0 from (sub_zero _).symm] - simp only [step_twice] - norm_num [P] - -private lemma step_trans (a b t x : ℝ) : Step a b (x-t) = Step (a+t) (b+t) x := by - simp only [Step, indicator_apply, mem_Ico] - split_ifs <;> grind - -private lemma step_mul (a b c d x : ℝ) : - Step a b x * Step c d x = Step (max a c) (min b d) x := by - simp only [Step, indicator_apply, mem_Ico, max_le_iff, lt_min_iff] - split_ifs <;> grind - -private lemma integrable_step (a b) : Integrable (Step a b) := by - apply IntegrableOn.integrable_indicator _ measurableSet_Ico - exact integrable_const _ - -private lemma integral_step (a b) : (∫ v, Step a b v) = max 0 (b-a) := by - unfold Step - rw [integral_indicator measurableSet_Ico, setIntegral_const, smul_eq_mul, mul_one, - measureReal_def, Real.volume_Ico, ENNReal.toReal_ofReal'] - rw [max_comm] - -private lemma pair_id (t x : ℝ) (n m) : P n x * P m (x-t) = - Step (max ((n:ℝ)/4) ((m:ℝ)/4+t)) (min ((n:ℝ)/4+1/4) ((m:ℝ)/4+1/4+t)) x := by - unfold P; rw [step_trans, step_mul] - -private lemma integrable_pair (t : ℝ) (n m) : Integrable (fun x => P n x * P m (x-t)) := by - simp_rw [pair_id] - exact integrable_step _ _ - -private lemma integral_pair (t : ℝ) (n m) : (∫ x, P n x * P m (x-t)) = - hatK (t-(((n:ℝ)-m)/4)) := by - simp_rw [pair_id] - rw [integral_step, hatK] - congr 1 - rw [abs_eq_max_neg] - grind - -lemma kappa_eq (t : ℝ) : - kappa t = hatK (t-3/4) - hatK (t+3/4) + 3*hatK (t+1/4) - 3*hatK (t-1/4) := by - let F (n : ℕ) (x : ℝ) := P n x * haarH (x-t) - let G (n : ℕ) := - (∫ x, P n x * P 0 (x-t)) + (∫ x, P n x * P 1 (x-t)) - - ((∫ x, P n x * P 2 (x-t)) + (∫ x, P n x * P 3 (x-t))) - have H (n : ℕ) : Integrable (F n) ∧ (∫ x, F n x) = G n := by - let f (m : ℕ) (x : ℝ) := P n x * P m (x-t) - have hm (m) : Integrable (f m) := integrable_pair t n m - have he (x : ℝ) : F n x = (f 0 x + f 1 x)-(f 2 x + f 3 x) := by - unfold F f; rw [haarH_step]; ring - have hfunc : F n = fun x => (f 0 x + f 1 x)-(f 2 x + f 3 x) := funext he - have h1 : Integrable fun x => f 0 x + f 1 x := (hm _).add (hm _) - have h2 : Integrable fun x => f 2 x + f 3 x := (hm _).add (hm _) - rw [hfunc, integral_sub h1 h2, integral_add (hm 0) (hm 1), integral_add (hm 2) (hm 3)] - exact ⟨h1.sub h2, rfl⟩ - have he (x) : haarG x * haarH (x-t) = (F 0 x - F 1 x)-(F 2 x - F 3 x) := by - rw [haarG_step]; unfold F; ring - unfold kappa - simp_rw [he] - have h1 : Integrable (fun x => F 0 x - F 1 x) := (H _).1.sub (H _).1 - have h2 : Integrable (fun x => F 2 x - F 3 x) := (H _).1.sub (H _).1 - rw [integral_sub h1 h2, integral_sub (H 0).1 (H 1).1, integral_sub (H 2).1 (H 3).1] - simp_rw [(H _).2] - dsimp only [G] - simp_rw [integral_pair] - push_cast - ring_nf - -lemma hatK_cont : Continuous hatK := by unfold hatK; fun_prop -lemma hatK_neg (x) : hatK (-x) = hatK x := by simp [hatK] -lemma hatK_bound (x) : |hatK x| ≤ 1/4 := by unfold hatK; grind -lemma hatK_zero {x : ℝ} (hx : (1/4:ℝ) ≤ |x|) : hatK x = 0 := max_eq_left (by linarith) - -lemma kappa_cont : Continuous kappa := by - have heq := funext kappa_eq - rw [heq] - have hh := hatK_cont - fun_prop - -lemma kappa_neg (t) : kappa (-t) = - kappa t := by - rw [kappa_eq, kappa_eq] - simp only [show ∀ a : ℝ, -t+a = -(t-a) by intro a; ring, - show ∀ a : ℝ, -t-a = -(t+a) by intro a; ring, hatK_neg] - ring - -lemma kappa_outside (t : ℝ) (ht : 1 ≤ |t|) : kappa t = 0 := by - rw [kappa_eq] - rw [hatK_zero (by grind), hatK_zero (by grind), hatK_zero (by grind), hatK_zero (by grind)] - norm_num - -lemma kappa_bound (t : ℝ) : |kappa t| ≤ 2 := by - rw [kappa_eq] - have a := hatK_bound (t-3/4) - have b := hatK_bound (t+3/4) - have c := hatK_bound (t+1/4) - have d := hatK_bound (t-1/4) - grind - - -lemma hatK_integral (a : ℝ) : (∫ t in Ico (a-1/4) (a+1/4), hatK (t-a)) = 1/16 := by - have hc : Continuous fun t => hatK (t-a) := hatK_cont.comp (continuous_id.sub continuous_const) - rw [seg_eq_interval _ (by linarith), - ← intervalIntegral.integral_add_adjacent_intervals - (hc.intervalIntegrable (a-1/4) a) - (hc.intervalIntegrable a (a+1/4))] - have hL : (∫ t in a-1/4..a, hatK (t-a)) = ∫ t in a-1/4..a, (t+(1/4-a)) := by - refine intervalIntegral.integral_congr ?_ - intro t ht - rw [uIcc_of_le (by linarith)] at ht - unfold hatK - grind - have hR : (∫ t in a..a+1/4, hatK (t-a)) = ∫ t in a..a+1/4, ((1/4+a)-t) := by - apply intervalIntegral.integral_congr - intro t ht - rw [uIcc_of_le (by linarith)] at ht - unfold hatK - grind - rw [hL,hR, intervalIntegral.integral_add, intervalIntegral.integral_sub (a:=a)] - · norm_num - ring - all_goals apply Continuous.intervalIntegrable; fun_prop - -lemma hatK_inside {a : ℝ} (ha : 1/4 ≤ a) (ha' : a ≤ 3/4) : - (∫ t in Ico (0:ℝ) 1, hatK (t-a)) = 1/16 := by - have hh (t) (ht : t ∉ Ico (a-1/4) (a+1/4)) : hatK (t-a) = 0 := - hatK_zero (by grind) - rw [← hatK_integral a, setIntegral_eq_integral_of_forall_compl_eq_zero, - setIntegral_eq_integral_of_forall_compl_eq_zero hh] - intro t ht; apply hh; grind - -lemma hatK_outside {a : ℝ} (ha : 1/4 ≤ a) : - (∫ t in Ico (0:ℝ) 1, hatK (t+a)) = 0 := by - refine setIntegral_eq_zero_of_forall_eq_zero ?_ - intro t ht - exact hatK_zero (by grind) - -def constK : ℝ := -1/8 - -lemma kappa_integral : (∫ t in Ico (0:ℝ) 1, kappa t) = constK := by - simp_rw [kappa_eq] - have hL (a : ℝ) : IntegrableOn (fun t => hatK (t-a)) (Ico (0:ℝ) 1) := by - refine seg_integrable _ (hatK_cont.measurable.comp (by fun_prop)) (1/4) _ _ ?_ - intro t _; exact hatK_bound _ - have hR (a : ℝ) : IntegrableOn (fun t => hatK (t+a)) (Ico (0:ℝ) 1) := by - simpa using hL (-a) - rw [integral_sub, integral_add, integral_sub] - · rw [integral_const_mul, integral_const_mul, hatK_inside (by norm_num) (by norm_num), - hatK_inside (by norm_num) (by norm_num), hatK_outside (by norm_num), - hatK_outside (by norm_num)]; norm_num [constK] - all_goals first - | exact hR _ - | exact hL _ - | exact (hL _).sub (hR _) - | exact Integrable.const_mul (hR _) _ - | exact Integrable.const_mul (hL _) _ - | exact ((hL _).sub (hR _)).add (Integrable.const_mul (hR _) _) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Haar +/-! # The translation-averaged Haar kernel -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def kappa (t : ℝ) : ℝ := + ∫ v, haarG v * haarH (v-t) + +lemma kappa_eq_mean (t : ℝ) : kappa t = + ∫ v, haarG v * haarH (v-t) ∂μ₁ := by + apply Eq.symm + apply setIntegral_eq_integral_of_forall_compl_eq_zero + intro v hv; rw [G_outside hv, zero_mul] + +private def Step (a b x : ℝ) : ℝ := (Ico a b).indicator (fun _ => 1) x +private def P (n : ℕ) (x : ℝ) := Step ((n:ℝ)/4) ((n:ℝ)/4+1/4) x + +def hatK (x : ℝ) : ℝ := max 0 (1/4 - |x|) + +private lemma step_split {a b c : ℝ} (h : a ≤ b) (h' : b ≤ c) (x) : + Step a c x = Step a b x + Step b c x := by + unfold Step + simp only [indicator_apply, mem_Ico] + split_ifs <;> grind + +private lemma haarH_step (x) : + haarH x = (P 0 x + P 1 x) - (P 2 x + P 3 x) := by + change Step 0 (1/2) x - Step (1/2) 1 x = _ + rw [step_split (b:=1/4) (by norm_num) (by norm_num) x, + step_split (c:=1) (b:=3/4) (by norm_num) (by norm_num) x] + norm_num [P] + +private lemma step_twice (a b c x : ℝ) : + Step b c (2*x-a) = Step ((b+a)/2) ((c+a)/2) x := by + simp only [Step, indicator_apply, mem_Ico] + split_ifs <;> grind + +private lemma haarG_step (x) : + haarG x = (P 0 x - P 1 x) - (P 2 x - P 3 x) := by + change Step 0 (1/2) (2*x) - Step (1/2) 1 (2*x) - + (Step 0 (1/2) (2*x-1) - Step (1/2) 1 (2*x-1)) = _ + nth_rw 1 2 [show 2*x = 2*x - 0 from (sub_zero _).symm] + simp only [step_twice] + norm_num [P] + +private lemma step_trans (a b t x : ℝ) : Step a b (x-t) = Step (a+t) (b+t) x := by + simp only [Step, indicator_apply, mem_Ico] + split_ifs <;> grind + +private lemma step_mul (a b c d x : ℝ) : + Step a b x * Step c d x = Step (max a c) (min b d) x := by + simp only [Step, indicator_apply, mem_Ico, max_le_iff, lt_min_iff] + split_ifs <;> grind + +private lemma integrable_step (a b) : Integrable (Step a b) := by + apply IntegrableOn.integrable_indicator _ measurableSet_Ico + exact integrable_const _ + +private lemma integral_step (a b) : (∫ v, Step a b v) = max 0 (b-a) := by + unfold Step + rw [integral_indicator measurableSet_Ico, setIntegral_const, smul_eq_mul, mul_one, + measureReal_def, Real.volume_Ico, ENNReal.toReal_ofReal'] + rw [max_comm] + +private lemma pair_id (t x : ℝ) (n m) : P n x * P m (x-t) = + Step (max ((n:ℝ)/4) ((m:ℝ)/4+t)) (min ((n:ℝ)/4+1/4) ((m:ℝ)/4+1/4+t)) x := by + unfold P; rw [step_trans, step_mul] + +private lemma integrable_pair (t : ℝ) (n m) : Integrable (fun x => P n x * P m (x-t)) := by + simp_rw [pair_id] + exact integrable_step _ _ + +private lemma integral_pair (t : ℝ) (n m) : (∫ x, P n x * P m (x-t)) = + hatK (t-(((n:ℝ)-m)/4)) := by + simp_rw [pair_id] + rw [integral_step, hatK] + congr 1 + rw [abs_eq_max_neg] + grind + +lemma kappa_eq (t : ℝ) : + kappa t = hatK (t-3/4) - hatK (t+3/4) + 3*hatK (t+1/4) - 3*hatK (t-1/4) := by + let F (n : ℕ) (x : ℝ) := P n x * haarH (x-t) + let G (n : ℕ) := + (∫ x, P n x * P 0 (x-t)) + (∫ x, P n x * P 1 (x-t)) - + ((∫ x, P n x * P 2 (x-t)) + (∫ x, P n x * P 3 (x-t))) + have H (n : ℕ) : Integrable (F n) ∧ (∫ x, F n x) = G n := by + let f (m : ℕ) (x : ℝ) := P n x * P m (x-t) + have hm (m) : Integrable (f m) := integrable_pair t n m + have he (x : ℝ) : F n x = (f 0 x + f 1 x)-(f 2 x + f 3 x) := by + unfold F f; rw [haarH_step]; ring + have hfunc : F n = fun x => (f 0 x + f 1 x)-(f 2 x + f 3 x) := funext he + have h1 : Integrable fun x => f 0 x + f 1 x := (hm _).add (hm _) + have h2 : Integrable fun x => f 2 x + f 3 x := (hm _).add (hm _) + rw [hfunc, integral_sub h1 h2, integral_add (hm 0) (hm 1), integral_add (hm 2) (hm 3)] + exact ⟨h1.sub h2, rfl⟩ + have he (x) : haarG x * haarH (x-t) = (F 0 x - F 1 x)-(F 2 x - F 3 x) := by + rw [haarG_step]; unfold F; ring + unfold kappa + simp_rw [he] + have h1 : Integrable (fun x => F 0 x - F 1 x) := (H _).1.sub (H _).1 + have h2 : Integrable (fun x => F 2 x - F 3 x) := (H _).1.sub (H _).1 + rw [integral_sub h1 h2, integral_sub (H 0).1 (H 1).1, integral_sub (H 2).1 (H 3).1] + simp_rw [(H _).2] + dsimp only [G] + simp_rw [integral_pair] + push_cast + ring_nf + +lemma hatK_cont : Continuous hatK := by unfold hatK; fun_prop +lemma hatK_neg (x) : hatK (-x) = hatK x := by simp [hatK] +lemma hatK_bound (x) : |hatK x| ≤ 1/4 := by unfold hatK; grind +lemma hatK_zero {x : ℝ} (hx : (1/4:ℝ) ≤ |x|) : hatK x = 0 := max_eq_left (by linarith) + +lemma kappa_cont : Continuous kappa := by + have heq := funext kappa_eq + rw [heq] + have hh := hatK_cont + fun_prop + +lemma kappa_neg (t) : kappa (-t) = - kappa t := by + rw [kappa_eq, kappa_eq] + simp only [show ∀ a : ℝ, -t+a = -(t-a) by intro a; ring, + show ∀ a : ℝ, -t-a = -(t+a) by intro a; ring, hatK_neg] + ring + +lemma kappa_outside (t : ℝ) (ht : 1 ≤ |t|) : kappa t = 0 := by + rw [kappa_eq] + rw [hatK_zero (by grind), hatK_zero (by grind), hatK_zero (by grind), hatK_zero (by grind)] + norm_num + +lemma kappa_bound (t : ℝ) : |kappa t| ≤ 2 := by + rw [kappa_eq] + have a := hatK_bound (t-3/4) + have b := hatK_bound (t+3/4) + have c := hatK_bound (t+1/4) + have d := hatK_bound (t-1/4) + grind + + +lemma hatK_integral (a : ℝ) : (∫ t in Ico (a-1/4) (a+1/4), hatK (t-a)) = 1/16 := by + have hc : Continuous fun t => hatK (t-a) := hatK_cont.comp (continuous_id.sub continuous_const) + rw [seg_eq_interval _ (by linarith), + ← intervalIntegral.integral_add_adjacent_intervals + (hc.intervalIntegrable (a-1/4) a) + (hc.intervalIntegrable a (a+1/4))] + have hL : (∫ t in a-1/4..a, hatK (t-a)) = ∫ t in a-1/4..a, (t+(1/4-a)) := by + refine intervalIntegral.integral_congr ?_ + intro t ht + rw [uIcc_of_le (by linarith)] at ht + unfold hatK + grind + have hR : (∫ t in a..a+1/4, hatK (t-a)) = ∫ t in a..a+1/4, ((1/4+a)-t) := by + apply intervalIntegral.integral_congr + intro t ht + rw [uIcc_of_le (by linarith)] at ht + unfold hatK + grind + rw [hL,hR, intervalIntegral.integral_add, intervalIntegral.integral_sub (a:=a)] + · norm_num + ring + all_goals apply Continuous.intervalIntegrable; fun_prop + +lemma hatK_inside {a : ℝ} (ha : 1/4 ≤ a) (ha' : a ≤ 3/4) : + (∫ t in Ico (0:ℝ) 1, hatK (t-a)) = 1/16 := by + have hh (t) (ht : t ∉ Ico (a-1/4) (a+1/4)) : hatK (t-a) = 0 := + hatK_zero (by grind) + rw [← hatK_integral a, setIntegral_eq_integral_of_forall_compl_eq_zero, + setIntegral_eq_integral_of_forall_compl_eq_zero hh] + intro t ht; apply hh; grind + +lemma hatK_outside {a : ℝ} (ha : 1/4 ≤ a) : + (∫ t in Ico (0:ℝ) 1, hatK (t+a)) = 0 := by + refine setIntegral_eq_zero_of_forall_eq_zero ?_ + intro t ht + exact hatK_zero (by grind) + +def constK : ℝ := -1/8 + +lemma kappa_integral : (∫ t in Ico (0:ℝ) 1, kappa t) = constK := by + simp_rw [kappa_eq] + have hL (a : ℝ) : IntegrableOn (fun t => hatK (t-a)) (Ico (0:ℝ) 1) := by + refine seg_integrable _ (hatK_cont.measurable.comp (by fun_prop)) (1/4) _ _ ?_ + intro t _; exact hatK_bound _ + have hR (a : ℝ) : IntegrableOn (fun t => hatK (t+a)) (Ico (0:ℝ) 1) := by + simpa using hL (-a) + rw [integral_sub, integral_add, integral_sub] + · rw [integral_const_mul, integral_const_mul, hatK_inside (by norm_num) (by norm_num), + hatK_inside (by norm_num) (by norm_num), hatK_outside (by norm_num), + hatK_outside (by norm_num)]; norm_num [constK] + all_goals first + | exact hR _ + | exact hL _ + | exact (hL _).sub (hR _) + | exact Integrable.const_mul (hR _) _ + | exact Integrable.const_mul (hL _) _ + | exact ((hL _).sub (hR _)).add (Integrable.const_mul (hR _) _) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Kernel.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Kernel.lean index 92daab51da26bc8cacc910a889a59cfedb12300e..a9a687833f0e7862a4019476c863a105de5ac269 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Kernel.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Kernel.lean @@ -1,121 +1,121 @@ -import OAI.Analysis.FourierLLogL.Fourier.Kappa -/-! # Hilbert kernel comparison -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def kernel (δ t : ℝ) : ℝ := ∫ r in Ico δ 1, kappa (t/r) / r^2 - -lemma kernel_measurable (δ) : Measurable (kernel δ) := by - refine StronglyMeasurable.measurable ?_ - unfold kernel - refine StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable ?_) - exact (kappa_cont.measurable.comp - (measurable_fst.div measurable_snd)).div (measurable_snd.pow_const 2) - -lemma kernel_neg (δ t : ℝ) : kernel δ (-t) = - kernel δ t := by - unfold kernel - simp_rw [neg_div, kappa_neg, neg_div, integral_neg] - -private lemma recip_deriv (t x : ℝ) (hx : x ≠ 0) : - HasDerivAt (fun r : ℝ => t/r) (-t/x^2) x := by - have h := (hasDerivAt_const x t).div (hasDerivAt_id' x) hx - simp only [zero_mul, mul_one, zero_sub] at h - exact h - -private lemma recip_cont {δ : ℝ} (hd : 0 < δ) (hb : δ ≤ 1) (t : ℝ) : - ContinuousOn (fun r : ℝ => -t / r^2) (uIcc δ 1) := by - rw [uIcc_of_le hb] - exact continuousOn_const.div (continuousOn_id.pow 2) - (fun x hx => pow_ne_zero _ (hd.trans_le hx.1).ne') - -lemma kernel_small {δ : ℝ} (hd : 0 < δ) (hb : δ ≤ 1) (t : ℝ) : - |kernel δ t| ≤ 2 / δ := by - have nn (x : ℝ) (hx : x ∈ uIcc δ 1) : x ≠ 0 := - (hd.trans_le ((show x ∈ Icc δ 1 by rwa [uIcc_of_le hb] at hx).1)).ne' - have hc : ContinuousOn (fun r : ℝ => kappa (t/r) / r^2) (uIcc δ 1) := - (kappa_cont.comp_continuousOn (continuousOn_const.div continuousOn_id nn)).div - (continuousOn_id.pow 2) (fun x hx => pow_ne_zero _ (nn x hx)) - have hI : - ∀ c : ℝ, (∫ r in δ..1, -c / r^2) = c/1 - c/δ := fun c => - intervalIntegral.integral_eq_sub_of_hasDerivAt (fun x hx => recip_deriv c x (nn x hx)) - (recip_cont hd hb c).intervalIntegrable - unfold kernel - rw [seg_eq_interval _ hb, abs_le] - constructor - · apply le_trans (b := ∫ r in δ..1, -(2:ℝ) / r^2) ?_ - (intervalIntegral.integral_mono_on hb (recip_cont hd hb _).intervalIntegrable - hc.intervalIntegrable (fun x _ => div_le_div_of_nonneg_right - (abs_le.mp (kappa_bound _)).1 (sq_nonneg x))) - rw [hI]; linarith - · have h := intervalIntegral.integral_mono_on (μ:=volume) hb hc.intervalIntegrable - (recip_cont hd hb (-2)).intervalIntegrable (fun x _ => by - exact div_le_div_of_nonneg_right (by linarith [(abs_le.mp (kappa_bound (t/x))).2]) (sq_nonneg x)) - refine h.trans ?_ - rw [hI] - simp - ring_nf - norm_num - -lemma kernel_middle_id {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ t) (hb : t ≤ 1) : - t * kernel δ t = constK - ∫ x in (0:ℝ)..t, kappa x := by - have hb' := ht.trans hb - have heq := intervalIntegral.integral_comp_mul_deriv (a:=δ) (b:=1) (fun x hx => - recip_deriv t x ((hd.trans_le - ((show x ∈ Icc δ 1 by rwa [uIcc_of_le hb'] at hx).1)).ne')) - (recip_cont hd hb' t) kappa_cont - simp only [Function.comp_apply, div_one] at heq - have H1 : (-t)* kernel δ t = ∫ x in t/δ..t, kappa x := by - rw [← heq, kernel, seg_eq_interval _ hb', ← intervalIntegral.integral_const_mul] - apply intervalIntegral.integral_congr - intro x _ - change -t * (_ / x^2) = _ * (-t / x^2) - ring - have hI : (∫ x in 1..t/δ, kappa x) = 0 := by - have hg : (1:ℝ) ≤ t / δ := (le_div_iff₀ hd).mpr (by simpa) - rw [← seg_eq_interval kappa hg] - exact setIntegral_eq_zero_of_forall_eq_zero fun x hx => - kappa_outside x (hx.1.trans (le_abs_self _)) - have hK := kappa_integral - rw [seg_eq_interval _ (by norm_num)] at hK - rw [intervalIntegral.integral_symm] at H1 - have h2 := intervalIntegral.integral_add_adjacent_intervals - (kappa_cont.intervalIntegrable 0 t (μ:=volume)) (kappa_cont.intervalIntegrable t 1) - have h3 := intervalIntegral.integral_add_adjacent_intervals - (kappa_cont.intervalIntegrable t 1 (μ:=volume)) (kappa_cont.intervalIntegrable 1 (t/δ)) - linarith - -lemma kernel_middle_pos {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ t) (hb : t ≤ 1) : - |kernel δ t - constK / t| ≤ 2 := by - have hp := hd.trans_le ht - have hid := kernel_middle_id hd ht hb - have hh := intervalIntegral.norm_integral_le_of_norm_le_const - (a:=0) (b:=t) (fun x _ => show ‖kappa x‖ ≤ 2 from kappa_bound x) - rw [Real.norm_eq_abs, sub_zero, abs_of_pos hp, abs_le] at hh - rw [abs_le] - constructor - · rw [le_sub_iff_add_le] - field_simp - nlinarith [hh.2] - · rw [sub_le_iff_le_add] - field_simp - nlinarith [hh.1] - -lemma kernel_middle {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ |t|) (hb : |t| ≤ 1) : - |kernel δ t - constK / t| ≤ 2 := by - rcases le_total 0 t with h|h - · rw [abs_of_nonneg h] at ht hb; exact kernel_middle_pos hd ht hb - · rw [abs_of_nonpos h] at ht hb - have H := kernel_middle_pos hd ht hb - rw [kernel_neg, div_neg, show -kernel δ t - -(constK / t) = -(kernel δ t - constK / t) by ring, abs_neg] at H - exact H - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Kappa +/-! # Hilbert kernel comparison -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def kernel (δ t : ℝ) : ℝ := ∫ r in Ico δ 1, kappa (t/r) / r^2 + +lemma kernel_measurable (δ) : Measurable (kernel δ) := by + refine StronglyMeasurable.measurable ?_ + unfold kernel + refine StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable ?_) + exact (kappa_cont.measurable.comp + (measurable_fst.div measurable_snd)).div (measurable_snd.pow_const 2) + +lemma kernel_neg (δ t : ℝ) : kernel δ (-t) = - kernel δ t := by + unfold kernel + simp_rw [neg_div, kappa_neg, neg_div, integral_neg] + +private lemma recip_deriv (t x : ℝ) (hx : x ≠ 0) : + HasDerivAt (fun r : ℝ => t/r) (-t/x^2) x := by + have h := (hasDerivAt_const x t).div (hasDerivAt_id' x) hx + simp only [zero_mul, mul_one, zero_sub] at h + exact h + +private lemma recip_cont {δ : ℝ} (hd : 0 < δ) (hb : δ ≤ 1) (t : ℝ) : + ContinuousOn (fun r : ℝ => -t / r^2) (uIcc δ 1) := by + rw [uIcc_of_le hb] + exact continuousOn_const.div (continuousOn_id.pow 2) + (fun x hx => pow_ne_zero _ (hd.trans_le hx.1).ne') + +lemma kernel_small {δ : ℝ} (hd : 0 < δ) (hb : δ ≤ 1) (t : ℝ) : + |kernel δ t| ≤ 2 / δ := by + have nn (x : ℝ) (hx : x ∈ uIcc δ 1) : x ≠ 0 := + (hd.trans_le ((show x ∈ Icc δ 1 by rwa [uIcc_of_le hb] at hx).1)).ne' + have hc : ContinuousOn (fun r : ℝ => kappa (t/r) / r^2) (uIcc δ 1) := + (kappa_cont.comp_continuousOn (continuousOn_const.div continuousOn_id nn)).div + (continuousOn_id.pow 2) (fun x hx => pow_ne_zero _ (nn x hx)) + have hI : + ∀ c : ℝ, (∫ r in δ..1, -c / r^2) = c/1 - c/δ := fun c => + intervalIntegral.integral_eq_sub_of_hasDerivAt (fun x hx => recip_deriv c x (nn x hx)) + (recip_cont hd hb c).intervalIntegrable + unfold kernel + rw [seg_eq_interval _ hb, abs_le] + constructor + · apply le_trans (b := ∫ r in δ..1, -(2:ℝ) / r^2) ?_ + (intervalIntegral.integral_mono_on hb (recip_cont hd hb _).intervalIntegrable + hc.intervalIntegrable (fun x _ => div_le_div_of_nonneg_right + (abs_le.mp (kappa_bound _)).1 (sq_nonneg x))) + rw [hI]; linarith + · have h := intervalIntegral.integral_mono_on (μ:=volume) hb hc.intervalIntegrable + (recip_cont hd hb (-2)).intervalIntegrable (fun x _ => by + exact div_le_div_of_nonneg_right (by linarith [(abs_le.mp (kappa_bound (t/x))).2]) (sq_nonneg x)) + refine h.trans ?_ + rw [hI] + simp + ring_nf + norm_num + +lemma kernel_middle_id {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ t) (hb : t ≤ 1) : + t * kernel δ t = constK - ∫ x in (0:ℝ)..t, kappa x := by + have hb' := ht.trans hb + have heq := intervalIntegral.integral_comp_mul_deriv (a:=δ) (b:=1) (fun x hx => + recip_deriv t x ((hd.trans_le + ((show x ∈ Icc δ 1 by rwa [uIcc_of_le hb'] at hx).1)).ne')) + (recip_cont hd hb' t) kappa_cont + simp only [Function.comp_apply, div_one] at heq + have H1 : (-t)* kernel δ t = ∫ x in t/δ..t, kappa x := by + rw [← heq, kernel, seg_eq_interval _ hb', ← intervalIntegral.integral_const_mul] + apply intervalIntegral.integral_congr + intro x _ + change -t * (_ / x^2) = _ * (-t / x^2) + ring + have hI : (∫ x in 1..t/δ, kappa x) = 0 := by + have hg : (1:ℝ) ≤ t / δ := (le_div_iff₀ hd).mpr (by simpa) + rw [← seg_eq_interval kappa hg] + exact setIntegral_eq_zero_of_forall_eq_zero fun x hx => + kappa_outside x (hx.1.trans (le_abs_self _)) + have hK := kappa_integral + rw [seg_eq_interval _ (by norm_num)] at hK + rw [intervalIntegral.integral_symm] at H1 + have h2 := intervalIntegral.integral_add_adjacent_intervals + (kappa_cont.intervalIntegrable 0 t (μ:=volume)) (kappa_cont.intervalIntegrable t 1) + have h3 := intervalIntegral.integral_add_adjacent_intervals + (kappa_cont.intervalIntegrable t 1 (μ:=volume)) (kappa_cont.intervalIntegrable 1 (t/δ)) + linarith + +lemma kernel_middle_pos {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ t) (hb : t ≤ 1) : + |kernel δ t - constK / t| ≤ 2 := by + have hp := hd.trans_le ht + have hid := kernel_middle_id hd ht hb + have hh := intervalIntegral.norm_integral_le_of_norm_le_const + (a:=0) (b:=t) (fun x _ => show ‖kappa x‖ ≤ 2 from kappa_bound x) + rw [Real.norm_eq_abs, sub_zero, abs_of_pos hp, abs_le] at hh + rw [abs_le] + constructor + · rw [le_sub_iff_add_le] + field_simp + nlinarith [hh.2] + · rw [sub_le_iff_le_add] + field_simp + nlinarith [hh.1] + +lemma kernel_middle {t δ : ℝ} (hd : 0 < δ) (ht : δ ≤ |t|) (hb : |t| ≤ 1) : + |kernel δ t - constK / t| ≤ 2 := by + rcases le_total 0 t with h|h + · rw [abs_of_nonneg h] at ht hb; exact kernel_middle_pos hd ht hb + · rw [abs_of_nonpos h] at ht hb + have H := kernel_middle_pos hd ht hb + rw [kernel_neg, div_neg, show -kernel δ t - -(constK / t) = -(kernel δ t - constK / t) by ring, abs_neg] at H + exact H + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Scale.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Scale.lean index 8b28261ab485c9e99a3fbe24b0ede95c5054eada..c3222d1911892f0de977308613ad75cb7a52a0bd 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Scale.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Scale.lean @@ -1,264 +1,264 @@ -import OAI.Analysis.FourierLLogL.Fourier.Translate -import OAI.Analysis.FourierLLogL.Fourier.Kernel -import OAI.Analysis.FourierLLogL.Fourier.Affine - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def scales : Set ℝ := Ico (1/2) 1 - -def averageH (F : ℝ → ℂ) (x : ℝ) (J : ℕ) := - ∫ s in scales, (1/s^2) • ∫ a in origins, shift F a s J x - -def convK (F : ℝ → ℂ) (x : ℝ) (J : ℕ) := - ∫ t in window, kernel (side J) t • F (x-t) - -lemma seg_integral_slices (a b : ℝ) (h : ℝ → ℝ → ℂ) (A : Set ℝ) - (hm : Measurable (uncurry h)) (B : ℝ) - (hb : ∀ s ∈ Ico a b, ∀ y, ‖h s y‖ ≤ B) [IsFiniteMeasure (volume.restrict A)] : - IntegrableOn (fun s => ∫ y in A, h s y) (Ico a b) := by - refine seg_integrable _ hm.stronglyMeasurable.integral_prod_right.measurable - (∫ _ in A, B) _ _ (fun s hs => ?_) - apply (norm_integral_le_integral_norm _).trans - have hm' : Measurable (h s) := hm.comp (measurable_const.prodMk measurable_id) - exact integral_mono (Integrable.of_bound hm'.aestronglyMeasurable B - (ae_of_all _ (hb _ hs))).norm (integrable_const _) (hb _ hs) - -lemma measurable_layer {X : Type*} [MeasurableSpace X] {F : ℝ → ℂ} (hf : Measurable F) - {A R O : X → ℝ} (hA : Measurable A) (hR : Measurable R) (hO : Measurable O) (n) : - Measurable fun y => haarLayer F (A y) (R y) n (O y) := - Finset.measurable_sum _ fun _ _ => - measurable_jump_comp hf (hA.add (measurable_const.mul (hR.mul measurable_const))) - (hR.mul measurable_const) hO - -private lemma inv_sq_bound {s : ℝ} (hs : s ∈ scales) : ‖(1/s^2:ℝ)‖ ≤ 4 := by - have hh := hs.1 - have h1 : 0 < s := by linarith - rw [Real.norm_eq_abs, abs_of_nonneg (by positivity)] - rw [div_le_iff₀ (by positivity)] - nlinarith - -private lemma weight_bound {c : ℝ} {v : ℂ} (hc : c ∈ scales) {B : ℝ} - (h : ‖v‖ ≤ B) : ‖(1/c^2) • v‖ ≤ 4 * B := by - rw [norm_smul] - apply mul_le_mul (inv_sq_bound hc) h (norm_nonneg _) - norm_num - -private instance scales_fin : IsFiniteMeasure (volume.restrict scales) := - show IsFiniteMeasure (volume.restrict (Ico (1/2:ℝ) 1)) from inferInstance - -private instance origins_fin : IsFiniteMeasure (volume.restrict origins) := - show IsFiniteMeasure (volume.restrict (Ico (-2:ℝ) 2)) from inferInstance - -private instance window_fin : IsFiniteMeasure (volume.restrict window) := - show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance - -private lemma average_succ (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ y, ‖F y‖ ≤ B) - (x : ℝ) (J : ℕ) : - averageH F x (J+1) = averageH F x J + - ∫ s in scales, (1/s^2) • ∫ a in origins, haarLayer F a s J x := by - let U (s a : ℝ) := shift F a s J x - let V (s a : ℝ) := haarLayer F a s J x - have hu : Measurable (uncurry U) := - measurable_shift_comp hf measurable_snd measurable_fst measurable_const J - have hv : Measurable (uncurry V) := - measurable_layer hf measurable_snd measurable_fst measurable_const J - have hUb (s a) := bounded_shift hf B hb a s J x - let C := ∑ n ∈ Finset.range J, (2:ℝ)^n*B - have HI (w : ℝ → ℝ → ℂ) (hw : Measurable (uncurry w)) (C : ℝ) - (hW : ∀ s a, ‖w s a‖ ≤ C) : - (∀ s, IntegrableOn (w s) origins) ∧ - IntegrableOn (fun s => (1/s^2) • ∫ a in origins, w s a) scales := by - refine ⟨fun s => Integrable.of_bound (hw.of_uncurry_left.aestronglyMeasurable) - C (ae_of_all _ (hW s)), ?_⟩ - simp_rw [← integral_smul] - apply seg_integral_slices _ _ _ _ (_ : Measurable _) (4*C) (fun s hs a => weight_bound hs (hW s a)) - exact (measurable_const.div (measurable_fst.pow_const 2)).smul hw - obtain ⟨hUI,hUI'⟩ := HI U hu C hUb - obtain ⟨hVI,hVI'⟩ := HI V hv ((2:ℝ)^J * B) (fun s a => bounded_haarLayer hf B hb a s J x) - have heq (s : ℝ) : - (1/s^2) • (∫ a in origins, shift F a s (J+1) x) = - ((1/s^2) • ∫ a in origins, U s a) + ((1/s^2) • ∫ a in origins, V s a) := by - simp_rw [show ∀ a, shift F a s (J+1) x = U s a + V s a by - intro a; exact Finset.sum_range_succ (fun n => haarLayer F a s n x) J] - rw [integral_add (hUI s) (hVI s), smul_add] - unfold averageH - simp_rw [heq, integral_add hUI' hVI'] - rfl - -def portion (a b t : ℝ) : ℝ := ∫ r in Ico a b, kappa (t/r) / r^2 - -private lemma scale_subst (n : ℕ) (t : ℝ) : - (∫ s in scales, (1/s^2)*((2:ℝ)^n)*kappa (t/(s*side n))) = - portion (side (n+1)) (side n) t := by - let f (r : ℝ) := kappa (t/r)/r^2 - have hp := side_pos n - have hh := seg_translate_scale f 0 (by norm_num : (1/2:ℝ) ≤ 1) hp - simp only [zero_add, mul_one] at hh - have H (s : ℝ) : (1/s^2)*((2:ℝ)^n)*kappa (t/(s*side n)) = - side n • f (side n * s) := by - rw [show (2:ℝ)^n = (side n)⁻¹ by - field_simp - exact pow_mul_side _] - unfold f - rw [mul_comm (side n) s, smul_eq_mul, mul_pow] - simp only [div_eq_mul_inv, mul_inv_rev] - field_simp - - unfold scales portion - simp_rw [H] - rw [integral_smul, hh, smul_smul, mul_inv_cancel₀ hp.ne', one_smul, side_succ] - simp only [f, div_eq_mul_inv, one_mul] - -private lemma kernel_split (J : ℕ) (t : ℝ) : - kernel (side (J+1)) t = kernel (side J) t + portion (side (J+1)) (side J) t := by - let f (r : ℝ) := kappa (t/r)/r^2 - have hJ := side_pos J - have hlt : side (J+1) ≤ side J := by rw [side_succ]; linarith - have hi {a b : ℝ} (ha : 0 < a) (hab : a ≤ b) : IntervalIntegrable f volume a b := by - apply ContinuousOn.intervalIntegrable - rw [uIcc_of_le hab] - have H (x : ℝ) (hx : x ∈ Icc a b) : x ≠ 0 := (ha.trans_le hx.1).ne' - exact (kappa_cont.comp_continuousOn (continuousOn_const.div continuousOn_id H)).div - (continuousOn_id.pow 2) (fun x hx => pow_ne_zero _ (H x hx)) - rw [kernel, kernel, portion, seg_eq_interval f (side_le (J+1)), seg_eq_interval f (side_le J), - seg_eq_interval f hlt, add_comm (a:=(∫ x in side J..1, f x)), intervalIntegral.integral_add_adjacent_intervals - (hi (side_pos _) hlt) (hi (side_pos _) (side_le J))] - -lemma convolution_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ y, ‖F y‖ ≤ B) - {x : ℝ} (hx : x ∈ unitInt) (J) : - averageH F x J = convK F x J := by - induction J with - | zero => - simp [averageH, convK, shift, kernel, side] - | succ n ih => - let q (t : ℝ) := F (x-t) - have hq : Measurable q := hf.comp (by fun_prop) - let r := side n - let V (s t : ℝ) := (1/s^2*(2:ℝ)^n*kappa (t/(s*r))) • q t - have heq : (∫ s in scales, (1/s^2) • ∫ a in origins, haarLayer F a s n x) = - ∫ t in window, portion (side (n+1)) r t • q t := by - have Hs (s : ℝ) (hs : s ∈ scales) : - ((1/s^2) • ∫ a in origins, haarLayer F a s n x) = ∫ t in window, V s t := by - have hs' : 0 < s := by linarith [hs.1] - rw [haarLayer_average F hf B hb n hx hs' hs.2.le, smul_smul, ← integral_smul] - unfold V q r - simp_rw [smul_smul] - have HVm : Measurable (uncurry V) := - (((measurable_const.div (measurable_fst.pow_const 2)).mul measurable_const).mul - (kappa_cont.measurable.comp (measurable_snd.div (measurable_fst.mul measurable_const)))).smul - (hq.comp measurable_snd) - have Hi : Integrable (uncurry V) ((volume.restrict scales).prod (volume.restrict window)) := by - apply Integrable.of_bound HVm.aestronglyMeasurable ((4*(2:ℝ)^n*2)*B) - rw [Measure.prod_restrict] - filter_upwards [ae_restrict_mem (measurableSet_Ico.prod measurableSet_Ico)] - with ⟨s,t⟩ ⟨hs,_⟩ - dsimp only [uncurry_apply_pair, V, q] - rw [norm_smul, norm_mul, norm_mul, Real.norm_eq_abs (kappa _)] - have h := inv_sq_bound hs - gcongr - all_goals first | simp | exact kappa_bound _ | exact hb _ - calc - _ = ∫ s in scales, ∫ t in window, V s t := setIntegral_congr_fun measurableSet_Ico Hs - _ = ∫ t in window, ∫ s in scales, V s t := integral_integral_swap Hi - _ = _ := by - dsimp only [V, r] - simp_rw [integral_smul_const, scale_subst] - have Hk (n : ℕ) : IntegrableOn (fun t => kernel (side n) t • F (x-t)) window := by - apply seg_integrable _ ((kernel_measurable _).smul hq) - ((2/(side n)) * B) - intro t _ - change ‖kernel (side n) t • F (x-t)‖ ≤ _ - rw [norm_smul] - exact mul_le_mul (kernel_small (side_pos _) (side_le _) t) (hb _) (norm_nonneg _) - (div_nonneg (by norm_num) (side_pos _).le) - let V' (t : ℝ) := portion (side (n+1)) r t • F (x-t) - have H (t : ℝ) : kernel (side (n+1)) t • F (x-t) = - kernel (side n) t • F (x-t) + V' t := by rw [kernel_split, add_smul] - have Hi : IntegrableOn V' window := by - rw [show V' = (fun t => kernel (side (n+1)) t • F (x-t)) - - (fun t => kernel (side n) t • F (x-t)) by ext t; simp [H]] - exact (Hk _).sub (Hk _) - rw [average_succ F hf B hb, ih, heq, convK, convK] - simp_rw [H, integral_add (Hk n) Hi] - rfl - - -def maxConvK (ρ : ℝ → ℝ) (J : ℕ) (x : ℝ) : ℝ≥0∞ := - ⨆ u : ℤ, ENNReal.ofReal ‖convK (modρ ρ u) x J‖ - -lemma measurable_maxConvK {ρ : ℝ → ℝ} (hρ : Measurable ρ) (J : ℕ) : - Measurable (maxConvK ρ J) := by - refine Measurable.iSup fun n => Measurable.ennreal_ofReal - (Measurable.norm (show Measurable (fun x => convK (modρ ρ n) x J) from ?_)) - unfold convK - apply StronglyMeasurable.measurable - exact StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable - (((kernel_measurable _).comp measurable_snd).smul - ((measurable_mod hρ _).comp (measurable_fst.sub measurable_snd)))) - -lemma convolution_cost {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) - (B : ℝ) (hb : ∀ y, ρ y ≤ B) (G : Set ℝ) (hG : MeasurableSet G) - (hGu : G ⊆ unitInt) (Q : ℝ≥0∞) (J : ℕ) - (H : ∀ s ∈ scales, ∀ a ∈ origins, (∫⁻ x in G, maxShift ρ a s J x) ≤ Q) : - (∫⁻ x in G, maxConvK ρ J x) ≤ 16 * Q := by - let Z (x s a : ℝ) := ENNReal.ofReal ‖(1/s^2:ℝ)‖ * maxShift ρ a s J x - have Hi (x : ℝ) (hx : x ∈ G) : - maxConvK ρ J x ≤ ∫⁻ s in scales, (∫⁻ a in origins, Z x s a) := by - refine iSup_le fun n => ?_ - let f := modρ ρ (n:ℝ) - have hm : Measurable f := measurable_mod hρ _ - have hb' (y : ℝ) : ‖f y‖ ≤ B := by rw [show f y = _ from rfl, norm_mod h0]; exact hb _ - rw [← convolution_average (modρ ρ n) hm B hb' (hGu hx) J] - unfold averageH - simp_rw [← integral_smul] - rw [ofReal_norm] - refine (enorm_integral_le_lintegral_enorm _).trans ?_ - refine lintegral_mono fun s => (enorm_integral_le_lintegral_enorm _).trans ?_ - refine lintegral_mono fun a => ?_ - unfold Z maxShift - rw [← ofReal_norm, norm_smul, ENNReal.ofReal_mul (by positivity)] - exact mul_le_mul_right (le_iSup (fun u : ℤ => - ENNReal.ofReal ‖shift (modρ ρ u) a s J x‖) n) _ - have Hm {X : Type} [MeasurableSpace X] (x s a : X → ℝ) - (hx : Measurable x) (hs : Measurable s) (ha : Measurable a) : - Measurable fun y => Z (x y) (s y) (a y) := - ((measurable_const.div (hs.pow_const 2)).norm.ennreal_ofReal).mul - (measurable_maxShift_comp hρ ha hs hx _) - have Hm' : Measurable fun w : ℝ × ℝ => ∫⁻ a in origins, Z w.1 w.2 a := by - refine Measurable.lintegral_prod_right ?_ - exact Hm _ _ _ (measurable_fst.comp measurable_fst) (measurable_snd.comp measurable_fst) - measurable_snd - have HI (s : ℝ) (hs : s ∈ scales) : - (∫⁻ x in G, (∫⁻ a in origins, Z x s a)) ≤ 16 * Q := by - have hZ : Measurable (fun p : ℝ × ℝ => Z p.1 s p.2) := - Hm _ _ _ measurable_fst measurable_const measurable_snd - rw [lintegral_lintegral_swap hZ.aemeasurable] - have hA (a : ℝ) (ha : a ∈ origins) : - (∫⁻ x in G, Z x s a) ≤ 4 * Q := by - unfold Z - rw [lintegral_const_mul _ (show Measurable (maxShift ρ a s J) from - measurable_maxShift_comp hρ measurable_const measurable_const measurable_id J)] - exact mul_le_mul' (by simpa using ENNReal.ofReal_le_ofReal (inv_sq_bound hs)) (H s hs a ha) - refine (lintegral_mono_ae ((ae_restrict_mem measurableSet_Ico).mono hA)).trans ?_ - have HV : volume origins = 4 := by norm_num [origins] - simp only [setLIntegral_const] - change 4*Q*volume origins ≤ _ - rw [HV] - - exact le_of_eq (by ring) - refine (lintegral_mono_ae ((ae_restrict_mem hG).mono Hi)).trans ?_ - rw [lintegral_lintegral_swap Hm'.aemeasurable] - refine (lintegral_mono_ae ((ae_restrict_mem measurableSet_Ico).mono HI)).trans ?_ - simp only [setLIntegral_const] - exact mul_le_of_le_one_right zero_le (by norm_num [scales]) - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Translate +import OAI.Analysis.FourierLLogL.Fourier.Kernel +import OAI.Analysis.FourierLLogL.Fourier.Affine + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def scales : Set ℝ := Ico (1/2) 1 + +def averageH (F : ℝ → ℂ) (x : ℝ) (J : ℕ) := + ∫ s in scales, (1/s^2) • ∫ a in origins, shift F a s J x + +def convK (F : ℝ → ℂ) (x : ℝ) (J : ℕ) := + ∫ t in window, kernel (side J) t • F (x-t) + +lemma seg_integral_slices (a b : ℝ) (h : ℝ → ℝ → ℂ) (A : Set ℝ) + (hm : Measurable (uncurry h)) (B : ℝ) + (hb : ∀ s ∈ Ico a b, ∀ y, ‖h s y‖ ≤ B) [IsFiniteMeasure (volume.restrict A)] : + IntegrableOn (fun s => ∫ y in A, h s y) (Ico a b) := by + refine seg_integrable _ hm.stronglyMeasurable.integral_prod_right.measurable + (∫ _ in A, B) _ _ (fun s hs => ?_) + apply (norm_integral_le_integral_norm _).trans + have hm' : Measurable (h s) := hm.comp (measurable_const.prodMk measurable_id) + exact integral_mono (Integrable.of_bound hm'.aestronglyMeasurable B + (ae_of_all _ (hb _ hs))).norm (integrable_const _) (hb _ hs) + +lemma measurable_layer {X : Type*} [MeasurableSpace X] {F : ℝ → ℂ} (hf : Measurable F) + {A R O : X → ℝ} (hA : Measurable A) (hR : Measurable R) (hO : Measurable O) (n) : + Measurable fun y => haarLayer F (A y) (R y) n (O y) := + Finset.measurable_sum _ fun _ _ => + measurable_jump_comp hf (hA.add (measurable_const.mul (hR.mul measurable_const))) + (hR.mul measurable_const) hO + +private lemma inv_sq_bound {s : ℝ} (hs : s ∈ scales) : ‖(1/s^2:ℝ)‖ ≤ 4 := by + have hh := hs.1 + have h1 : 0 < s := by linarith + rw [Real.norm_eq_abs, abs_of_nonneg (by positivity)] + rw [div_le_iff₀ (by positivity)] + nlinarith + +private lemma weight_bound {c : ℝ} {v : ℂ} (hc : c ∈ scales) {B : ℝ} + (h : ‖v‖ ≤ B) : ‖(1/c^2) • v‖ ≤ 4 * B := by + rw [norm_smul] + apply mul_le_mul (inv_sq_bound hc) h (norm_nonneg _) + norm_num + +private instance scales_fin : IsFiniteMeasure (volume.restrict scales) := + show IsFiniteMeasure (volume.restrict (Ico (1/2:ℝ) 1)) from inferInstance + +private instance origins_fin : IsFiniteMeasure (volume.restrict origins) := + show IsFiniteMeasure (volume.restrict (Ico (-2:ℝ) 2)) from inferInstance + +private instance window_fin : IsFiniteMeasure (volume.restrict window) := + show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance + +private lemma average_succ (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ y, ‖F y‖ ≤ B) + (x : ℝ) (J : ℕ) : + averageH F x (J+1) = averageH F x J + + ∫ s in scales, (1/s^2) • ∫ a in origins, haarLayer F a s J x := by + let U (s a : ℝ) := shift F a s J x + let V (s a : ℝ) := haarLayer F a s J x + have hu : Measurable (uncurry U) := + measurable_shift_comp hf measurable_snd measurable_fst measurable_const J + have hv : Measurable (uncurry V) := + measurable_layer hf measurable_snd measurable_fst measurable_const J + have hUb (s a) := bounded_shift hf B hb a s J x + let C := ∑ n ∈ Finset.range J, (2:ℝ)^n*B + have HI (w : ℝ → ℝ → ℂ) (hw : Measurable (uncurry w)) (C : ℝ) + (hW : ∀ s a, ‖w s a‖ ≤ C) : + (∀ s, IntegrableOn (w s) origins) ∧ + IntegrableOn (fun s => (1/s^2) • ∫ a in origins, w s a) scales := by + refine ⟨fun s => Integrable.of_bound (hw.of_uncurry_left.aestronglyMeasurable) + C (ae_of_all _ (hW s)), ?_⟩ + simp_rw [← integral_smul] + apply seg_integral_slices _ _ _ _ (_ : Measurable _) (4*C) (fun s hs a => weight_bound hs (hW s a)) + exact (measurable_const.div (measurable_fst.pow_const 2)).smul hw + obtain ⟨hUI,hUI'⟩ := HI U hu C hUb + obtain ⟨hVI,hVI'⟩ := HI V hv ((2:ℝ)^J * B) (fun s a => bounded_haarLayer hf B hb a s J x) + have heq (s : ℝ) : + (1/s^2) • (∫ a in origins, shift F a s (J+1) x) = + ((1/s^2) • ∫ a in origins, U s a) + ((1/s^2) • ∫ a in origins, V s a) := by + simp_rw [show ∀ a, shift F a s (J+1) x = U s a + V s a by + intro a; exact Finset.sum_range_succ (fun n => haarLayer F a s n x) J] + rw [integral_add (hUI s) (hVI s), smul_add] + unfold averageH + simp_rw [heq, integral_add hUI' hVI'] + rfl + +def portion (a b t : ℝ) : ℝ := ∫ r in Ico a b, kappa (t/r) / r^2 + +private lemma scale_subst (n : ℕ) (t : ℝ) : + (∫ s in scales, (1/s^2)*((2:ℝ)^n)*kappa (t/(s*side n))) = + portion (side (n+1)) (side n) t := by + let f (r : ℝ) := kappa (t/r)/r^2 + have hp := side_pos n + have hh := seg_translate_scale f 0 (by norm_num : (1/2:ℝ) ≤ 1) hp + simp only [zero_add, mul_one] at hh + have H (s : ℝ) : (1/s^2)*((2:ℝ)^n)*kappa (t/(s*side n)) = + side n • f (side n * s) := by + rw [show (2:ℝ)^n = (side n)⁻¹ by + field_simp + exact pow_mul_side _] + unfold f + rw [mul_comm (side n) s, smul_eq_mul, mul_pow] + simp only [div_eq_mul_inv, mul_inv_rev] + field_simp + + unfold scales portion + simp_rw [H] + rw [integral_smul, hh, smul_smul, mul_inv_cancel₀ hp.ne', one_smul, side_succ] + simp only [f, div_eq_mul_inv, one_mul] + +private lemma kernel_split (J : ℕ) (t : ℝ) : + kernel (side (J+1)) t = kernel (side J) t + portion (side (J+1)) (side J) t := by + let f (r : ℝ) := kappa (t/r)/r^2 + have hJ := side_pos J + have hlt : side (J+1) ≤ side J := by rw [side_succ]; linarith + have hi {a b : ℝ} (ha : 0 < a) (hab : a ≤ b) : IntervalIntegrable f volume a b := by + apply ContinuousOn.intervalIntegrable + rw [uIcc_of_le hab] + have H (x : ℝ) (hx : x ∈ Icc a b) : x ≠ 0 := (ha.trans_le hx.1).ne' + exact (kappa_cont.comp_continuousOn (continuousOn_const.div continuousOn_id H)).div + (continuousOn_id.pow 2) (fun x hx => pow_ne_zero _ (H x hx)) + rw [kernel, kernel, portion, seg_eq_interval f (side_le (J+1)), seg_eq_interval f (side_le J), + seg_eq_interval f hlt, add_comm (a:=(∫ x in side J..1, f x)), intervalIntegral.integral_add_adjacent_intervals + (hi (side_pos _) hlt) (hi (side_pos _) (side_le J))] + +lemma convolution_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ y, ‖F y‖ ≤ B) + {x : ℝ} (hx : x ∈ unitInt) (J) : + averageH F x J = convK F x J := by + induction J with + | zero => + simp [averageH, convK, shift, kernel, side] + | succ n ih => + let q (t : ℝ) := F (x-t) + have hq : Measurable q := hf.comp (by fun_prop) + let r := side n + let V (s t : ℝ) := (1/s^2*(2:ℝ)^n*kappa (t/(s*r))) • q t + have heq : (∫ s in scales, (1/s^2) • ∫ a in origins, haarLayer F a s n x) = + ∫ t in window, portion (side (n+1)) r t • q t := by + have Hs (s : ℝ) (hs : s ∈ scales) : + ((1/s^2) • ∫ a in origins, haarLayer F a s n x) = ∫ t in window, V s t := by + have hs' : 0 < s := by linarith [hs.1] + rw [haarLayer_average F hf B hb n hx hs' hs.2.le, smul_smul, ← integral_smul] + unfold V q r + simp_rw [smul_smul] + have HVm : Measurable (uncurry V) := + (((measurable_const.div (measurable_fst.pow_const 2)).mul measurable_const).mul + (kappa_cont.measurable.comp (measurable_snd.div (measurable_fst.mul measurable_const)))).smul + (hq.comp measurable_snd) + have Hi : Integrable (uncurry V) ((volume.restrict scales).prod (volume.restrict window)) := by + apply Integrable.of_bound HVm.aestronglyMeasurable ((4*(2:ℝ)^n*2)*B) + rw [Measure.prod_restrict] + filter_upwards [ae_restrict_mem (measurableSet_Ico.prod measurableSet_Ico)] + with ⟨s,t⟩ ⟨hs,_⟩ + dsimp only [uncurry_apply_pair, V, q] + rw [norm_smul, norm_mul, norm_mul, Real.norm_eq_abs (kappa _)] + have h := inv_sq_bound hs + gcongr + all_goals first | simp | exact kappa_bound _ | exact hb _ + calc + _ = ∫ s in scales, ∫ t in window, V s t := setIntegral_congr_fun measurableSet_Ico Hs + _ = ∫ t in window, ∫ s in scales, V s t := integral_integral_swap Hi + _ = _ := by + dsimp only [V, r] + simp_rw [integral_smul_const, scale_subst] + have Hk (n : ℕ) : IntegrableOn (fun t => kernel (side n) t • F (x-t)) window := by + apply seg_integrable _ ((kernel_measurable _).smul hq) + ((2/(side n)) * B) + intro t _ + change ‖kernel (side n) t • F (x-t)‖ ≤ _ + rw [norm_smul] + exact mul_le_mul (kernel_small (side_pos _) (side_le _) t) (hb _) (norm_nonneg _) + (div_nonneg (by norm_num) (side_pos _).le) + let V' (t : ℝ) := portion (side (n+1)) r t • F (x-t) + have H (t : ℝ) : kernel (side (n+1)) t • F (x-t) = + kernel (side n) t • F (x-t) + V' t := by rw [kernel_split, add_smul] + have Hi : IntegrableOn V' window := by + rw [show V' = (fun t => kernel (side (n+1)) t • F (x-t)) - + (fun t => kernel (side n) t • F (x-t)) by ext t; simp [H]] + exact (Hk _).sub (Hk _) + rw [average_succ F hf B hb, ih, heq, convK, convK] + simp_rw [H, integral_add (Hk n) Hi] + rfl + + +def maxConvK (ρ : ℝ → ℝ) (J : ℕ) (x : ℝ) : ℝ≥0∞ := + ⨆ u : ℤ, ENNReal.ofReal ‖convK (modρ ρ u) x J‖ + +lemma measurable_maxConvK {ρ : ℝ → ℝ} (hρ : Measurable ρ) (J : ℕ) : + Measurable (maxConvK ρ J) := by + refine Measurable.iSup fun n => Measurable.ennreal_ofReal + (Measurable.norm (show Measurable (fun x => convK (modρ ρ n) x J) from ?_)) + unfold convK + apply StronglyMeasurable.measurable + exact StronglyMeasurable.integral_prod_right (Measurable.stronglyMeasurable + (((kernel_measurable _).comp measurable_snd).smul + ((measurable_mod hρ _).comp (measurable_fst.sub measurable_snd)))) + +lemma convolution_cost {ρ : ℝ → ℝ} (hρ : Measurable ρ) (h0 : ∀ x, 0 ≤ ρ x) + (B : ℝ) (hb : ∀ y, ρ y ≤ B) (G : Set ℝ) (hG : MeasurableSet G) + (hGu : G ⊆ unitInt) (Q : ℝ≥0∞) (J : ℕ) + (H : ∀ s ∈ scales, ∀ a ∈ origins, (∫⁻ x in G, maxShift ρ a s J x) ≤ Q) : + (∫⁻ x in G, maxConvK ρ J x) ≤ 16 * Q := by + let Z (x s a : ℝ) := ENNReal.ofReal ‖(1/s^2:ℝ)‖ * maxShift ρ a s J x + have Hi (x : ℝ) (hx : x ∈ G) : + maxConvK ρ J x ≤ ∫⁻ s in scales, (∫⁻ a in origins, Z x s a) := by + refine iSup_le fun n => ?_ + let f := modρ ρ (n:ℝ) + have hm : Measurable f := measurable_mod hρ _ + have hb' (y : ℝ) : ‖f y‖ ≤ B := by rw [show f y = _ from rfl, norm_mod h0]; exact hb _ + rw [← convolution_average (modρ ρ n) hm B hb' (hGu hx) J] + unfold averageH + simp_rw [← integral_smul] + rw [ofReal_norm] + refine (enorm_integral_le_lintegral_enorm _).trans ?_ + refine lintegral_mono fun s => (enorm_integral_le_lintegral_enorm _).trans ?_ + refine lintegral_mono fun a => ?_ + unfold Z maxShift + rw [← ofReal_norm, norm_smul, ENNReal.ofReal_mul (by positivity)] + exact mul_le_mul_right (le_iSup (fun u : ℤ => + ENNReal.ofReal ‖shift (modρ ρ u) a s J x‖) n) _ + have Hm {X : Type} [MeasurableSpace X] (x s a : X → ℝ) + (hx : Measurable x) (hs : Measurable s) (ha : Measurable a) : + Measurable fun y => Z (x y) (s y) (a y) := + ((measurable_const.div (hs.pow_const 2)).norm.ennreal_ofReal).mul + (measurable_maxShift_comp hρ ha hs hx _) + have Hm' : Measurable fun w : ℝ × ℝ => ∫⁻ a in origins, Z w.1 w.2 a := by + refine Measurable.lintegral_prod_right ?_ + exact Hm _ _ _ (measurable_fst.comp measurable_fst) (measurable_snd.comp measurable_fst) + measurable_snd + have HI (s : ℝ) (hs : s ∈ scales) : + (∫⁻ x in G, (∫⁻ a in origins, Z x s a)) ≤ 16 * Q := by + have hZ : Measurable (fun p : ℝ × ℝ => Z p.1 s p.2) := + Hm _ _ _ measurable_fst measurable_const measurable_snd + rw [lintegral_lintegral_swap hZ.aemeasurable] + have hA (a : ℝ) (ha : a ∈ origins) : + (∫⁻ x in G, Z x s a) ≤ 4 * Q := by + unfold Z + rw [lintegral_const_mul _ (show Measurable (maxShift ρ a s J) from + measurable_maxShift_comp hρ measurable_const measurable_const measurable_id J)] + exact mul_le_mul' (by simpa using ENNReal.ofReal_le_ofReal (inv_sq_bound hs)) (H s hs a ha) + refine (lintegral_mono_ae ((ae_restrict_mem measurableSet_Ico).mono hA)).trans ?_ + have HV : volume origins = 4 := by norm_num [origins] + simp only [setLIntegral_const] + change 4*Q*volume origins ≤ _ + rw [HV] + + exact le_of_eq (by ring) + refine (lintegral_mono_ae ((ae_restrict_mem hG).mono Hi)).trans ?_ + rw [lintegral_lintegral_swap Hm'.aemeasurable] + refine (lintegral_mono_ae ((ae_restrict_mem measurableSet_Ico).mono HI)).trans ?_ + simp only [setLIntegral_const] + exact mul_le_of_le_one_right zero_le (by norm_num [scales]) + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Fourier/Translate.lean b/lean/OAI/Analysis/FourierLLogL/Fourier/Translate.lean index efaa40a45ec24baffce23f8f907c1c0b0fa87763..87289dc6a36246bc27614615b13204683cf226f5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Fourier/Translate.lean +++ b/lean/OAI/Analysis/FourierLLogL/Fourier/Translate.lean @@ -1,134 +1,134 @@ -import OAI.Analysis.FourierLLogL.Fourier.Kappa -/-! # Translation averaging -/ - -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology Real - -namespace OAI - -namespace FourierLLogL.Period -noncomputable section - -def window : Set ℝ := Ico (-1) 1 -def origins : Set ℝ := Ico (-2) 2 - -private lemma flip_dilate (g : ℝ → ℂ) (d c : ℝ) (hc : 0 < c) : - (∫ x : ℝ, g (d-c*x)) = c⁻¹ • ∫ x : ℝ, g x := by - have h := Measure.integral_comp_mul_left (fun x => g (d+x)) (-c) - simp_rw [neg_mul, ← sub_eq_add_neg] at h - rw [integral_add_left_eq_self] at h - simpa [abs_of_pos (inv_pos.mpr hc)] using h - -/-- Translation average over origins for a single length. -/ -lemma jump_average_all (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - (x r b : ℝ) (hr : 0 < r) (hr' : r ≤ 1) : - (∫ a : ℝ, jump F (a+b) r x) = - ∫ t in window, kappa (t/r) • F (x-t) := by - let J (a : ℝ) := jump F (a+b) r x - let f (v : ℝ) := haarG v • ∫ w, haarH w • F (x-r*(v-w)) ∂μ₁ - have heq (v : ℝ) : J (x-b-r*v) = f v := by - have H : (x - (x-b-r*v+b))/r = v := by field_simp; ring - unfold J f jump coeffH - rw [H] - simp_rw [show ∀ w, x-b-r*v+b+r*w = x-r*(v-w) by intro w; ring] - have hA : (∫ a : ℝ, J a) = r • ∫ v, f v ∂μ₁ := by - have HH := flip_dilate J (x-b) r hr - simp_rw [heq] at HH - have Hsupport : (∫ v, f v ∂μ₁) = ∫ v : ℝ, f v := by - exact setIntegral_eq_integral_of_forall_compl_eq_zero - (fun v hv => by unfold f; rw [G_outside hv, zero_smul]) - rw [Hsupport, HH, smul_smul, mul_inv_cancel₀ hr.ne', one_smul] - let W (v t : ℝ) := haarG v • (haarH (v-t) • F (x-r*t)) - have hw (v : ℝ) (hv : v ∈ unitInt) : - f v = ∫ t in window, W v t := by - have HH := flip_dilate (fun w => haarH w • F (x-r*(v-w))) v 1 (by norm_num) - simp only [one_mul, sub_sub_cancel, inv_one, one_smul] at HH - have Hs : (∫ w, haarH w • F (x-r*(v-w)) ∂μ₁) = - ∫ w, haarH w • F (x-r*(v-w)) := by - exact setIntegral_eq_integral_of_forall_compl_eq_zero - (fun w hw => by rw [H_outside hw, zero_smul]) - have Hs' : (∫ t in window, haarH (v-t) • F (x-r*t)) = - ∫ t, haarH (v-t) • F (x-r*t) := by - apply setIntegral_eq_integral_of_forall_compl_eq_zero - intro t ht - rw [H_outside, zero_smul] - rintro ⟨h1,h2⟩ - exact ht ⟨by linarith [hv.1], by linarith [hv.2]⟩ - change haarG v • _ = ∫ t in window, haarG v • (haarH (v-t) • F (x-r*t)) - rw [integral_smul, Hs, Hs', HH] - have hWm : Measurable (uncurry W) := - (measurableG.comp measurable_fst).smul - ((measurableH.comp (measurable_fst.sub measurable_snd)).smul - (hf.comp (measurable_const.sub (measurable_const.mul measurable_snd)))) - have : IsFiniteMeasure (volume.restrict window) := - show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance - have hWi : Integrable (uncurry W) (μ₁.prod (volume.restrict window)) := by - apply Integrable.of_bound hWm.aestronglyMeasurable B - refine ae_of_all _ fun ⟨v,t⟩ => ?_ - simp only [W, uncurry_apply_pair, norm_smul, Real.norm_eq_abs] - refine (mul_le_of_le_one_left (mul_nonneg (abs_nonneg _) (norm_nonneg _)) - (G_bound _)).trans ?_ - exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (hb _) - have hK (t : ℝ) : (∫ v, W v t ∂μ₁) = kappa t • F (x-r*t) := by - dsimp only [W]; simp_rw [smul_smul] - rw [integral_smul_const, kappa_eq_mean] - have hB : (∫ v, f v ∂μ₁) = ∫ t in window, kappa t • F (x-r*t) := by - rw [show (∫ v, f v ∂μ₁) = ∫ v, (∫ t in window, W v t) ∂μ₁ from - setIntegral_congr_fun measurable_unitInt hw, integral_integral_swap hWi] - simp_rw [hK] - let q (t : ℝ) := kappa (t/r) • F (x-t) - have Q : ∀ t, q (r*t) = kappa t • F (x-r*t) := fun t => by - unfold q; rw [mul_div_cancel_left₀ t hr.ne'] - have hg : ∀ t ∉ window, 1 ≤ |t| := by - unfold window; grind - have hq : (∫ t in window, q t) = ∫ t, q t := by - apply setIntegral_eq_integral_of_forall_compl_eq_zero - intro t ht - unfold q - rw [kappa_outside, zero_smul] - rw [abs_div, abs_of_pos hr] - exact (le_div_iff₀ hr).mpr (by linarith [hg t ht]) - have hq' : (∫ t in window, q (r*t)) = ∫ t, q (r*t) := by - exact setIntegral_eq_integral_of_forall_compl_eq_zero - (fun t ht => by rw [Q, kappa_outside t (hg t ht), zero_smul]) - have htrans : (∫ t in window, kappa t • F (x-r*t)) = r⁻¹ • ∫ t in window, q t := by - simp_rw [← Q] - rw [hq, hq', Measure.integral_comp_mul_left, abs_of_pos (inv_pos.mpr hr)] - change (∫ a : ℝ, J a) = ∫ t in window, q t - rw [hA, hB, htrans, smul_smul, mul_inv_cancel₀ hr.ne', one_smul] - -lemma jump_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - {x s : ℝ} (n k : ℕ) (hk : k < 2^n) (hx : x ∈ unitInt) (hs : 0 < s) (hs' : s ≤ 1) : - (∫ a in origins, jump F (lft a s n k) (s*side n) x) = - ∫ t in window, kappa (t/(s*side n)) • F (x-t) := by - have hr : 0 < s*side n := mul_pos hs (side_pos _) - rw [setIntegral_eq_integral_of_forall_compl_eq_zero] - · exact jump_average_all F hf B hb x _ _ hr - ((mul_le_mul_of_nonneg_right hs' (side_pos n).le).trans - (by simpa using side_le n)) - · intro a ha - apply jump_outside hr - intro ht - obtain ⟨h1,h2⟩ := cell_in_top a hs hk ht - exact ha ⟨by linarith [hx.1], by linarith [hx.2]⟩ - -lemma haarLayer_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) - {x s : ℝ} (n : ℕ) (hx : x ∈ unitInt) (hs : 0 < s) (hs' : s ≤ 1) : - (∫ a in origins, haarLayer F a s n x) = - (2:ℝ)^n • ∫ t in window, kappa (t/(s*side n)) • F (x-t) := by - unfold haarLayer - rw [integral_finsetSum] - · calc - _ = ∑ _ ∈ Finset.range (2^n), ∫ t in window, kappa (t/(s*side n)) • F (x-t) := - Finset.sum_congr rfl fun k hk => jump_average F hf B hb n k (Finset.mem_range.mp hk) hx hs hs' - _ = _ := by simp only [Finset.sum_const, Finset.card_range, ← Nat.cast_smul_eq_nsmul ℝ] - norm_cast - · intro k hk - refine seg_integrable _ (measurable_jump_comp hf ?_ measurable_const measurable_const) B _ _ - (fun a _ => bounded_jump hf B hb _ _ _) - exact measurable_id.add_const _ - -end -end FourierLLogL.Period - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Kappa +/-! # Translation averaging -/ + +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology Real + +namespace OAI + +namespace FourierLLogL.Period +noncomputable section + +def window : Set ℝ := Ico (-1) 1 +def origins : Set ℝ := Ico (-2) 2 + +private lemma flip_dilate (g : ℝ → ℂ) (d c : ℝ) (hc : 0 < c) : + (∫ x : ℝ, g (d-c*x)) = c⁻¹ • ∫ x : ℝ, g x := by + have h := Measure.integral_comp_mul_left (fun x => g (d+x)) (-c) + simp_rw [neg_mul, ← sub_eq_add_neg] at h + rw [integral_add_left_eq_self] at h + simpa [abs_of_pos (inv_pos.mpr hc)] using h + +/-- Translation average over origins for a single length. -/ +lemma jump_average_all (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + (x r b : ℝ) (hr : 0 < r) (hr' : r ≤ 1) : + (∫ a : ℝ, jump F (a+b) r x) = + ∫ t in window, kappa (t/r) • F (x-t) := by + let J (a : ℝ) := jump F (a+b) r x + let f (v : ℝ) := haarG v • ∫ w, haarH w • F (x-r*(v-w)) ∂μ₁ + have heq (v : ℝ) : J (x-b-r*v) = f v := by + have H : (x - (x-b-r*v+b))/r = v := by field_simp; ring + unfold J f jump coeffH + rw [H] + simp_rw [show ∀ w, x-b-r*v+b+r*w = x-r*(v-w) by intro w; ring] + have hA : (∫ a : ℝ, J a) = r • ∫ v, f v ∂μ₁ := by + have HH := flip_dilate J (x-b) r hr + simp_rw [heq] at HH + have Hsupport : (∫ v, f v ∂μ₁) = ∫ v : ℝ, f v := by + exact setIntegral_eq_integral_of_forall_compl_eq_zero + (fun v hv => by unfold f; rw [G_outside hv, zero_smul]) + rw [Hsupport, HH, smul_smul, mul_inv_cancel₀ hr.ne', one_smul] + let W (v t : ℝ) := haarG v • (haarH (v-t) • F (x-r*t)) + have hw (v : ℝ) (hv : v ∈ unitInt) : + f v = ∫ t in window, W v t := by + have HH := flip_dilate (fun w => haarH w • F (x-r*(v-w))) v 1 (by norm_num) + simp only [one_mul, sub_sub_cancel, inv_one, one_smul] at HH + have Hs : (∫ w, haarH w • F (x-r*(v-w)) ∂μ₁) = + ∫ w, haarH w • F (x-r*(v-w)) := by + exact setIntegral_eq_integral_of_forall_compl_eq_zero + (fun w hw => by rw [H_outside hw, zero_smul]) + have Hs' : (∫ t in window, haarH (v-t) • F (x-r*t)) = + ∫ t, haarH (v-t) • F (x-r*t) := by + apply setIntegral_eq_integral_of_forall_compl_eq_zero + intro t ht + rw [H_outside, zero_smul] + rintro ⟨h1,h2⟩ + exact ht ⟨by linarith [hv.1], by linarith [hv.2]⟩ + change haarG v • _ = ∫ t in window, haarG v • (haarH (v-t) • F (x-r*t)) + rw [integral_smul, Hs, Hs', HH] + have hWm : Measurable (uncurry W) := + (measurableG.comp measurable_fst).smul + ((measurableH.comp (measurable_fst.sub measurable_snd)).smul + (hf.comp (measurable_const.sub (measurable_const.mul measurable_snd)))) + have : IsFiniteMeasure (volume.restrict window) := + show IsFiniteMeasure (volume.restrict (Ico (-1:ℝ) 1)) from inferInstance + have hWi : Integrable (uncurry W) (μ₁.prod (volume.restrict window)) := by + apply Integrable.of_bound hWm.aestronglyMeasurable B + refine ae_of_all _ fun ⟨v,t⟩ => ?_ + simp only [W, uncurry_apply_pair, norm_smul, Real.norm_eq_abs] + refine (mul_le_of_le_one_left (mul_nonneg (abs_nonneg _) (norm_nonneg _)) + (G_bound _)).trans ?_ + exact (mul_le_of_le_one_left (norm_nonneg _) (H_bound _)).trans (hb _) + have hK (t : ℝ) : (∫ v, W v t ∂μ₁) = kappa t • F (x-r*t) := by + dsimp only [W]; simp_rw [smul_smul] + rw [integral_smul_const, kappa_eq_mean] + have hB : (∫ v, f v ∂μ₁) = ∫ t in window, kappa t • F (x-r*t) := by + rw [show (∫ v, f v ∂μ₁) = ∫ v, (∫ t in window, W v t) ∂μ₁ from + setIntegral_congr_fun measurable_unitInt hw, integral_integral_swap hWi] + simp_rw [hK] + let q (t : ℝ) := kappa (t/r) • F (x-t) + have Q : ∀ t, q (r*t) = kappa t • F (x-r*t) := fun t => by + unfold q; rw [mul_div_cancel_left₀ t hr.ne'] + have hg : ∀ t ∉ window, 1 ≤ |t| := by + unfold window; grind + have hq : (∫ t in window, q t) = ∫ t, q t := by + apply setIntegral_eq_integral_of_forall_compl_eq_zero + intro t ht + unfold q + rw [kappa_outside, zero_smul] + rw [abs_div, abs_of_pos hr] + exact (le_div_iff₀ hr).mpr (by linarith [hg t ht]) + have hq' : (∫ t in window, q (r*t)) = ∫ t, q (r*t) := by + exact setIntegral_eq_integral_of_forall_compl_eq_zero + (fun t ht => by rw [Q, kappa_outside t (hg t ht), zero_smul]) + have htrans : (∫ t in window, kappa t • F (x-r*t)) = r⁻¹ • ∫ t in window, q t := by + simp_rw [← Q] + rw [hq, hq', Measure.integral_comp_mul_left, abs_of_pos (inv_pos.mpr hr)] + change (∫ a : ℝ, J a) = ∫ t in window, q t + rw [hA, hB, htrans, smul_smul, mul_inv_cancel₀ hr.ne', one_smul] + +lemma jump_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + {x s : ℝ} (n k : ℕ) (hk : k < 2^n) (hx : x ∈ unitInt) (hs : 0 < s) (hs' : s ≤ 1) : + (∫ a in origins, jump F (lft a s n k) (s*side n) x) = + ∫ t in window, kappa (t/(s*side n)) • F (x-t) := by + have hr : 0 < s*side n := mul_pos hs (side_pos _) + rw [setIntegral_eq_integral_of_forall_compl_eq_zero] + · exact jump_average_all F hf B hb x _ _ hr + ((mul_le_mul_of_nonneg_right hs' (side_pos n).le).trans + (by simpa using side_le n)) + · intro a ha + apply jump_outside hr + intro ht + obtain ⟨h1,h2⟩ := cell_in_top a hs hk ht + exact ha ⟨by linarith [hx.1], by linarith [hx.2]⟩ + +lemma haarLayer_average (F : ℝ → ℂ) (hf : Measurable F) (B : ℝ) (hb : ∀ x, ‖F x‖ ≤ B) + {x s : ℝ} (n : ℕ) (hx : x ∈ unitInt) (hs : 0 < s) (hs' : s ≤ 1) : + (∫ a in origins, haarLayer F a s n x) = + (2:ℝ)^n • ∫ t in window, kappa (t/(s*side n)) • F (x-t) := by + unfold haarLayer + rw [integral_finsetSum] + · calc + _ = ∑ _ ∈ Finset.range (2^n), ∫ t in window, kappa (t/(s*side n)) • F (x-t) := + Finset.sum_congr rfl fun k hk => jump_average F hf B hb n k (Finset.mem_range.mp hk) hx hs hs' + _ = _ := by simp only [Finset.sum_const, Finset.card_range, ← Nat.cast_smul_eq_nsmul ℝ] + norm_cast + · intro k hk + refine seg_integrable _ (measurable_jump_comp hf ?_ measurable_const measurable_const) B _ _ + (fun a _ => bounded_jump hf B hb _ _ _) + exact measurable_id.add_const _ + +end +end FourierLLogL.Period + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Main.lean b/lean/OAI/Analysis/FourierLLogL/Main.lean index 7bcccc76ead3734f7f5926d9dc8b6eb17b5c9ef9..3c953bd1b82efae882e27d0cf07d759a40ef8f46 100644 --- a/lean/OAI/Analysis/FourierLLogL/Main.lean +++ b/lean/OAI/Analysis/FourierLLogL/Main.lean @@ -1,43 +1,43 @@ -import OAI.Analysis.FourierLLogL.Weak.Endpoint -import OAI.Analysis.FourierLLogL.Assembly.Endpoint -import OAI.Analysis.FourierLLogL.Dyadic.Lift -import OAI.Analysis.FourierLLogL.Fourier.Good - -/-! -# Almost everywhere Fourier convergence in L log L --/ - -open MeasureTheory Filter Set -open scoped Topology - -namespace OAI - -namespace FourierLLogL - -theorem main_of_grid (h : Dyadic.GridBound) {f : Torus → ℂ} (hf : MemLLogL f) : - ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, - Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := - h.unitTreeBound.affine.realGoodBound.goodBound.main_bound_consequence hf -end FourierLLogL - -namespace FourierLLogL -/-- Almost everywhere convergence in L log L, all symmetric cutoffs. -/ -theorem almost_everywhere_fourier_convergence (f : Torus → ℂ) (hf : MemLLogL f) : - ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, - Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := - main_of_grid Dyadic.gridProof hf - -theorem maximal_good_sets : GoodBound := - Dyadic.gridProof.unitTreeBound.affine.realGoodBound.goodBound -end FourierLLogL - -namespace FourierLLogL -/-- Cor. "Weak-L^1 maximal bound", in the equivalent all-levels form. -/ -theorem weak_L1_maximal_bound : - ∃ C : ℝ, 0 < C ∧ ∀ (f : Torus → ℂ), MemLLogL f → - ∀ w : ℝ, 0 < w → - w*circleMeasure.real (cloudSet f w) ≤ C*luxNorm f := - Weak.consequence_maximal maximal_good_sets -end FourierLLogL - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Endpoint +import OAI.Analysis.FourierLLogL.Assembly.Endpoint +import OAI.Analysis.FourierLLogL.Dyadic.Lift +import OAI.Analysis.FourierLLogL.Fourier.Good + +/-! +# Almost everywhere Fourier convergence in L log L +-/ + +open MeasureTheory Filter Set +open scoped Topology + +namespace OAI + +namespace FourierLLogL + +theorem main_of_grid (h : Dyadic.GridBound) {f : Torus → ℂ} (hf : MemLLogL f) : + ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, + Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := + h.unitTreeBound.affine.realGoodBound.goodBound.main_bound_consequence hf +end FourierLLogL + +namespace FourierLLogL +/-- Almost everywhere convergence in L log L, all symmetric cutoffs. -/ +theorem almost_everywhere_fourier_convergence (f : Torus → ℂ) (hf : MemLLogL f) : + ∃ E : Set Torus, MeasurableSet E ∧ circleMeasure E = 0 ∧ ∀ x ∉ E, + Tendsto (fun N : ℕ ↦ symmetricPartialSum f N x) atTop (𝓝 (f x)) := + main_of_grid Dyadic.gridProof hf + +theorem maximal_good_sets : GoodBound := + Dyadic.gridProof.unitTreeBound.affine.realGoodBound.goodBound +end FourierLLogL + +namespace FourierLLogL +/-- Cor. "Weak-L^1 maximal bound", in the equivalent all-levels form. -/ +theorem weak_L1_maximal_bound : + ∃ C : ℝ, 0 < C ∧ ∀ (f : Torus → ℂ), MemLLogL f → + ∀ w : ℝ, 0 < w → + w*circleMeasure.real (cloudSet f w) ≤ C*luxNorm f := + Weak.consequence_maximal maximal_good_sets +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/PaperMain.lean b/lean/OAI/Analysis/FourierLLogL/PaperMain.lean index ef661c70269edbc45422529db90cec265ca16d8f..bedb72c179d611d1278f8357d77346762e4d3352 100644 --- a/lean/OAI/Analysis/FourierLLogL/PaperMain.lean +++ b/lean/OAI/Analysis/FourierLLogL/PaperMain.lean @@ -1,21 +1,21 @@ -import OAI.Analysis.FourierLLogL.Main - -open MeasureTheory Filter -open scoped BigOperators Topology - -namespace OAI.FourierLLogL - -theorem paperMain (f : AddCircle (2 * Real.pi) → ℂ) - (hfm : AEStronglyMeasurable f AddCircle.haarAddCircle) - (hfi : Integrable (fun x ↦ ‖f x‖ * Real.log (2 + ‖f x‖)) - AddCircle.haarAddCircle) : - ∃ E : Set (AddCircle (2 * Real.pi)), - MeasurableSet E ∧ AddCircle.haarAddCircle E = 0 ∧ ∀ x ∉ E, - Tendsto - (fun N : ℕ ↦ - ∑ k ∈ Finset.Icc (-(N : ℤ)) (N : ℤ), fourierCoeff f k * fourier k x) - atTop (𝓝 (f x)) := by - simpa only [circleMeasure, symmetricPartialSum] using - (almost_everywhere_fourier_convergence f ⟨hfm, hfi⟩) - -end OAI.FourierLLogL +import OAI.Analysis.FourierLLogL.Main + +open MeasureTheory Filter +open scoped BigOperators Topology + +namespace OAI.FourierLLogL + +theorem paperMain (f : AddCircle (2 * Real.pi) → ℂ) + (hfm : AEStronglyMeasurable f AddCircle.haarAddCircle) + (hfi : Integrable (fun x ↦ ‖f x‖ * Real.log (2 + ‖f x‖)) + AddCircle.haarAddCircle) : + ∃ E : Set (AddCircle (2 * Real.pi)), + MeasurableSet E ∧ AddCircle.haarAddCircle E = 0 ∧ ∀ x ∉ E, + Tendsto + (fun N : ℕ ↦ + ∑ k ∈ Finset.Icc (-(N : ℤ)) (N : ℤ), fourierCoeff f k * fourier k x) + atTop (𝓝 (f x)) := by + simpa only [circleMeasure, symmetricPartialSum] using + (almost_everywhere_fourier_convergence f ⟨hfm, hfi⟩) + +end OAI.FourierLLogL diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Bridge.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Bridge.lean index 017904bc6a02120f926691277c654ae35a7fdd23..85e4999fd22c36d9e98e24db078232b841233769 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Bridge.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Bridge.lean @@ -1,164 +1,164 @@ -import OAI.Analysis.FourierLLogL.Paths.Ray -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Beam - -lemma prism_limits {s : Scene M} (h : Fit s) (u : Grid M) (W : Paths.Vector M) - (a : ℝ) (z) : - let m := matter s z; let r := prism s u W a z - a*m ≤ r ∧ m* ‖meanV s u z‖ ≤ r := by - intro m r - have H := ray_bounds (W z) (letter s u z) (a*m) - have he : letter s u z=(m:ℂ)* meanV s u z := (mean_hold s h _ _).1.symm - constructor; exact le_trans (le_abs_self _) H.2.2.1 - rw [show m* ‖meanV s u z‖=‖letter s u z‖ by rw [he,norm_scaleC,abs_of_nonneg (matter_nonneg h z)]] - exact H.2.1 - -private lemma square_addthree (x y z : ℝ) : - (x+y+z)^2 ≤ 3*(x^2+y^2+z^2) := by - nlinarith [sq_nonneg (x-y),sq_nonneg (y-z),sq_nonneg (z-x)] - -lemma norm_addthree (x y z : ℂ) : - ‖x+y+z‖^2 ≤ 3*(‖x‖^2+‖y‖^2+‖z‖^2) := by - have H : ‖x+y+z‖ ≤ ‖x‖+‖y‖+‖z‖ := (norm_add_le _ z).trans - (add_le_add_left (norm_add_le x y) _) - exact le_trans (pow_le_pow_left₀ (norm_nonneg _) H _) (square_addthree _ _ _) - -lemma factor_swap {s q : Scene M} (hs : Fit s) (hq : Climb q) - (u : Grid M) (z) (a : ℝ) (ha : 0 < a) (he : a ≤ 1) - (W : Paths.Vector M) (hh : matter q z ≤ 2*matter s z) : - let g := dew u q z; let m := mist u s q z; let r := prism s u W a z; let j := pierce q z - ‖factor q u z‖^2 ≤ r*(8*j+32*((g+m)/a)) ∧ - (a*shell q z)^2 ≤ r*(2*j) := by - intro g m r j - let v := meanV s u z; let w := meanV q u z - let x := matter s z; let y := matter q z - let t := shell q z; let b := nub q u z - have hN : 0 ≤ x := matter_nonneg hs z - have hM : 0 ≤ y := matter_nonneg hq.fit z - change y ≤ 2*x at hh - have ht : t^2=y*j := shell_sq hq z - have hj : 0 ≤ j := pierce_nonneg hq.fit z - have hb : ‖b‖^2 ≤ 4*y*g := nub_innovation q hq u z - have hg : 0 ≤ g := dew_nonneg u hq z - have hm : 0 ≤ m := mist_nonneg u s q hq.fit z - have hv : ‖v‖ ≤ 1 := (mean_hold _ hs _ _).2 - have hd : a*x ≤ r ∧ x* ‖v‖ ≤ r := prism_limits hs u W a z - have hr := le_trans (mul_nonneg ha.le hN) hd.1 - have hi : x ≤ r/a := (le_div_iff₀ ha).2 (by linarith [hd.1]) - have hQ : t^2 ≤ y^2 := by - rw [← sq_abs]; exact pow_le_pow_left₀ (abs_nonneg t) (shell_abs hq z) _ - constructor - · have hx : ‖(t:ℂ)*v‖^2 ≤ 2*r*j := by - rw [norm_scaleC,mul_pow,sq_abs,ht] - have HE : y* ‖v‖^2 ≤ 2*r := by - have H1 : ‖v‖^2 ≤ ‖v‖ := by nlinarith [norm_nonneg v] - nlinarith [norm_nonneg v,hd.2] - nlinarith [mul_le_mul_of_nonneg_right HE hj] - have hy : ‖(t:ℂ)*(w-v)‖^2 ≤ 2*(r/a)*m := by - rw [norm_scaleC,mul_pow,sq_abs] - have HE : m=y*‖w-v‖^2 := rfl - apply le_trans (mul_le_mul_of_nonneg_right hQ (sq_nonneg _)) - calc - _= y*m := by rw [HE]; ring - _ ≤ _ := mul_le_mul_of_nonneg_right (by linarith) hm - have hz : ‖b‖^2 ≤ 8*(r/a)*g := hb.trans (by nlinarith) - rw [show factor q u z=(↑t:ℂ)*v+t*(w-v)+b by unfold t b w nub; ring] - apply (norm_addthree _ _ _).trans - rw [add_div,mul_add] - nlinarith [mul_nonneg hr hj, - mul_nonneg (div_nonneg hr ha.le) hg,mul_nonneg (div_nonneg hr ha.le) hm, - show r*(32*(g/a+m/a))=32*(r/a)*g+32*(r/a)*m by ring] - change (a*t)^2 ≤ _ - rw [mul_pow,ht] - have HE : a^2*y ≤ 2*r := by nlinarith [hd.1] - nlinarith [mul_le_mul_of_nonneg_right HE hj] - -lemma bridge_mist {s : Scene M} (h : Climb s) (u z : Grid M) (i : Bool) - {q : Scene M} (hp : Ideal (s.smaller i) q) : - mist u s q z ≤ 8*(dew u s z + (matter (s.smaller i) z-matter q z)) := by - let t := s.smaller i - let v := meanV s u z; let w := meanV t u z; let l := meanV q u z - let y := matter q z; let x := matter t z - have H : y* ‖l-w‖^2 ≤ 4*(x-y) := (drain_changes hp u z).1 - have hH : x* ‖w-v‖^2 ≤ 2*dew u s z := mist_low u h i z - have he : ‖l-v‖^2 ≤ 2*(‖l-w‖^2+‖w-v‖^2) := by - have H := pow_le_pow_left₀ (norm_nonneg _) (norm_add_le (l-w) (w-v)) 2 - rw [sub_add_sub_cancel] at H - nlinarith [sq_nonneg (‖l-w‖- ‖w-v‖)] - have hy : 0 ≤ y := matter_nonneg hp.climb.fit z - have hv : y ≤ x := by nlinarith - change y* ‖l-v‖^2 ≤ 8*(_+(x-y)) - nlinarith [mul_le_mul_of_nonneg_left he hy,dew_nonneg u h z, - mul_le_mul_of_nonneg_right hv (sq_nonneg ‖w-v‖)] - -lemma extra_joints {s : Scene M} (h : Climb s) (u z : Grid M) (i : Bool) - {q : Scene M} (hp : Ideal (s.smaller i) q) (a : ℝ) (ha : 0 < a) - (he : a ≤ 1) (W : Paths.Vector M) : - let t := s.smaller i - let r := prism s u W a z; let k := matter t z-matter q z - let j₀ := pierce s z; let j₁ := pierce q z - let f₀ := dew u s z; let f₁ := dew u q z - ‖factor s u z‖^2+‖factor q u z‖^2+ (a*shell s z)^2 + (a*shell q z)^2 + - ‖letter t u z-letter q u z‖^2+(a*k)^2 ≤ - r*(20*(j₀+j₁)+400*((k+f₀+f₁)/a)) := by - intro t r k j₀ j₁ f₀ f₁ - let x := matter s z - have H : matter t z ≤ 2*x := matter_max h i z - have He : ‖letter t u z-letter q u z‖ ≤ k := matter_drain hp u z - have hk := (norm_nonneg _).trans He - have hq : matter q z ≤ 2*x := by unfold k at hk; linarith - have hr : 0 ≤ r := ray_pos _ _ _ - have hx : a*x ≤ r := (prism_limits h.fit u W a z).1 - have hi : k ≤ 2*(r/a) := by - have hh : x ≤ r/a := (le_div_iff₀ ha).2 (by linarith) - have hQ := matter_nonneg hp.climb.fit z - change k ≥ _ at hk - unfold k; linarith - have hd : (a*k)^2 ≤ k^2 := pow_le_pow_left₀ (mul_nonneg ha.le hk) (by nlinarith) _ - have hh₁ := factor_swap h.fit h u z a ha he W (by linarith [matter_nonneg h.fit z]) - have HH₁ : ‖factor s u z‖^2 ≤ r*(8*j₀+32*(f₀/a)) := by - have hH : mist u s s z=0 := by simp [mist] - apply hh₁.1.trans - change r*(8*j₀+32*((f₀+mist u s s z)/a)) ≤ _ - rw [hH,add_zero] - have hh₂ := factor_swap h.fit hp.climb u z a ha he W hq - have hh : ‖factor q u z‖^2 ≤ r*(8*j₁+32*((f₁+8*(f₀+k))/a)) := by - have hv : mist u s q z ≤ 8*(f₀+k) := bridge_mist h u z i hp - apply hh₂.1.trans; change r*(8*j₁+32*((f₁+mist u s q z)/a)) ≤ _ - gcongr - have HX : (a*shell s z)^2 ≤ r*(2*j₀) := hh₁.2 - have HY : (a*shell q z)^2 ≤ r*(2*j₁) := hh₂.2 - have h₀ : 0 ≤ j₀ := pierce_nonneg h.fit z - have h₁ : 0 ≤ j₁ := pierce_nonneg hp.climb.fit z - have he₀ : 0 ≤ f₀ := dew_nonneg u h z - have he₁ : 0 ≤ f₁ := dew_nonneg u hp.climb z - have HZ := pow_le_pow_left₀ (norm_nonneg _) He 2 - simp_rw [div_eq_mul_inv] at * - have HH := inv_pos.2 ha - nlinarith [mul_le_mul_of_nonneg_right hi hk, - mul_nonneg hr he₀,mul_nonneg hr he₁,mul_nonneg hr hk] - -lemma contest (x y z : ℂ) (r t p : ℝ) : - ‖x+y+z‖^2 + (r+t+p)^2 ≤ - 3*((‖x‖^2+‖y‖^2+‖z‖^2)+(r^2+t^2+p^2)) := by - linarith [square_addthree r t p,norm_addthree x y z] - -end Beam - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Ray +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Beam + +lemma prism_limits {s : Scene M} (h : Fit s) (u : Grid M) (W : Paths.Vector M) + (a : ℝ) (z) : + let m := matter s z; let r := prism s u W a z + a*m ≤ r ∧ m* ‖meanV s u z‖ ≤ r := by + intro m r + have H := ray_bounds (W z) (letter s u z) (a*m) + have he : letter s u z=(m:ℂ)* meanV s u z := (mean_hold s h _ _).1.symm + constructor; exact le_trans (le_abs_self _) H.2.2.1 + rw [show m* ‖meanV s u z‖=‖letter s u z‖ by rw [he,norm_scaleC,abs_of_nonneg (matter_nonneg h z)]] + exact H.2.1 + +private lemma square_addthree (x y z : ℝ) : + (x+y+z)^2 ≤ 3*(x^2+y^2+z^2) := by + nlinarith [sq_nonneg (x-y),sq_nonneg (y-z),sq_nonneg (z-x)] + +lemma norm_addthree (x y z : ℂ) : + ‖x+y+z‖^2 ≤ 3*(‖x‖^2+‖y‖^2+‖z‖^2) := by + have H : ‖x+y+z‖ ≤ ‖x‖+‖y‖+‖z‖ := (norm_add_le _ z).trans + (add_le_add_left (norm_add_le x y) _) + exact le_trans (pow_le_pow_left₀ (norm_nonneg _) H _) (square_addthree _ _ _) + +lemma factor_swap {s q : Scene M} (hs : Fit s) (hq : Climb q) + (u : Grid M) (z) (a : ℝ) (ha : 0 < a) (he : a ≤ 1) + (W : Paths.Vector M) (hh : matter q z ≤ 2*matter s z) : + let g := dew u q z; let m := mist u s q z; let r := prism s u W a z; let j := pierce q z + ‖factor q u z‖^2 ≤ r*(8*j+32*((g+m)/a)) ∧ + (a*shell q z)^2 ≤ r*(2*j) := by + intro g m r j + let v := meanV s u z; let w := meanV q u z + let x := matter s z; let y := matter q z + let t := shell q z; let b := nub q u z + have hN : 0 ≤ x := matter_nonneg hs z + have hM : 0 ≤ y := matter_nonneg hq.fit z + change y ≤ 2*x at hh + have ht : t^2=y*j := shell_sq hq z + have hj : 0 ≤ j := pierce_nonneg hq.fit z + have hb : ‖b‖^2 ≤ 4*y*g := nub_innovation q hq u z + have hg : 0 ≤ g := dew_nonneg u hq z + have hm : 0 ≤ m := mist_nonneg u s q hq.fit z + have hv : ‖v‖ ≤ 1 := (mean_hold _ hs _ _).2 + have hd : a*x ≤ r ∧ x* ‖v‖ ≤ r := prism_limits hs u W a z + have hr := le_trans (mul_nonneg ha.le hN) hd.1 + have hi : x ≤ r/a := (le_div_iff₀ ha).2 (by linarith [hd.1]) + have hQ : t^2 ≤ y^2 := by + rw [← sq_abs]; exact pow_le_pow_left₀ (abs_nonneg t) (shell_abs hq z) _ + constructor + · have hx : ‖(t:ℂ)*v‖^2 ≤ 2*r*j := by + rw [norm_scaleC,mul_pow,sq_abs,ht] + have HE : y* ‖v‖^2 ≤ 2*r := by + have H1 : ‖v‖^2 ≤ ‖v‖ := by nlinarith [norm_nonneg v] + nlinarith [norm_nonneg v,hd.2] + nlinarith [mul_le_mul_of_nonneg_right HE hj] + have hy : ‖(t:ℂ)*(w-v)‖^2 ≤ 2*(r/a)*m := by + rw [norm_scaleC,mul_pow,sq_abs] + have HE : m=y*‖w-v‖^2 := rfl + apply le_trans (mul_le_mul_of_nonneg_right hQ (sq_nonneg _)) + calc + _= y*m := by rw [HE]; ring + _ ≤ _ := mul_le_mul_of_nonneg_right (by linarith) hm + have hz : ‖b‖^2 ≤ 8*(r/a)*g := hb.trans (by nlinarith) + rw [show factor q u z=(↑t:ℂ)*v+t*(w-v)+b by unfold t b w nub; ring] + apply (norm_addthree _ _ _).trans + rw [add_div,mul_add] + nlinarith [mul_nonneg hr hj, + mul_nonneg (div_nonneg hr ha.le) hg,mul_nonneg (div_nonneg hr ha.le) hm, + show r*(32*(g/a+m/a))=32*(r/a)*g+32*(r/a)*m by ring] + change (a*t)^2 ≤ _ + rw [mul_pow,ht] + have HE : a^2*y ≤ 2*r := by nlinarith [hd.1] + nlinarith [mul_le_mul_of_nonneg_right HE hj] + +lemma bridge_mist {s : Scene M} (h : Climb s) (u z : Grid M) (i : Bool) + {q : Scene M} (hp : Ideal (s.smaller i) q) : + mist u s q z ≤ 8*(dew u s z + (matter (s.smaller i) z-matter q z)) := by + let t := s.smaller i + let v := meanV s u z; let w := meanV t u z; let l := meanV q u z + let y := matter q z; let x := matter t z + have H : y* ‖l-w‖^2 ≤ 4*(x-y) := (drain_changes hp u z).1 + have hH : x* ‖w-v‖^2 ≤ 2*dew u s z := mist_low u h i z + have he : ‖l-v‖^2 ≤ 2*(‖l-w‖^2+‖w-v‖^2) := by + have H := pow_le_pow_left₀ (norm_nonneg _) (norm_add_le (l-w) (w-v)) 2 + rw [sub_add_sub_cancel] at H + nlinarith [sq_nonneg (‖l-w‖- ‖w-v‖)] + have hy : 0 ≤ y := matter_nonneg hp.climb.fit z + have hv : y ≤ x := by nlinarith + change y* ‖l-v‖^2 ≤ 8*(_+(x-y)) + nlinarith [mul_le_mul_of_nonneg_left he hy,dew_nonneg u h z, + mul_le_mul_of_nonneg_right hv (sq_nonneg ‖w-v‖)] + +lemma extra_joints {s : Scene M} (h : Climb s) (u z : Grid M) (i : Bool) + {q : Scene M} (hp : Ideal (s.smaller i) q) (a : ℝ) (ha : 0 < a) + (he : a ≤ 1) (W : Paths.Vector M) : + let t := s.smaller i + let r := prism s u W a z; let k := matter t z-matter q z + let j₀ := pierce s z; let j₁ := pierce q z + let f₀ := dew u s z; let f₁ := dew u q z + ‖factor s u z‖^2+‖factor q u z‖^2+ (a*shell s z)^2 + (a*shell q z)^2 + + ‖letter t u z-letter q u z‖^2+(a*k)^2 ≤ + r*(20*(j₀+j₁)+400*((k+f₀+f₁)/a)) := by + intro t r k j₀ j₁ f₀ f₁ + let x := matter s z + have H : matter t z ≤ 2*x := matter_max h i z + have He : ‖letter t u z-letter q u z‖ ≤ k := matter_drain hp u z + have hk := (norm_nonneg _).trans He + have hq : matter q z ≤ 2*x := by unfold k at hk; linarith + have hr : 0 ≤ r := ray_pos _ _ _ + have hx : a*x ≤ r := (prism_limits h.fit u W a z).1 + have hi : k ≤ 2*(r/a) := by + have hh : x ≤ r/a := (le_div_iff₀ ha).2 (by linarith) + have hQ := matter_nonneg hp.climb.fit z + change k ≥ _ at hk + unfold k; linarith + have hd : (a*k)^2 ≤ k^2 := pow_le_pow_left₀ (mul_nonneg ha.le hk) (by nlinarith) _ + have hh₁ := factor_swap h.fit h u z a ha he W (by linarith [matter_nonneg h.fit z]) + have HH₁ : ‖factor s u z‖^2 ≤ r*(8*j₀+32*(f₀/a)) := by + have hH : mist u s s z=0 := by simp [mist] + apply hh₁.1.trans + change r*(8*j₀+32*((f₀+mist u s s z)/a)) ≤ _ + rw [hH,add_zero] + have hh₂ := factor_swap h.fit hp.climb u z a ha he W hq + have hh : ‖factor q u z‖^2 ≤ r*(8*j₁+32*((f₁+8*(f₀+k))/a)) := by + have hv : mist u s q z ≤ 8*(f₀+k) := bridge_mist h u z i hp + apply hh₂.1.trans; change r*(8*j₁+32*((f₁+mist u s q z)/a)) ≤ _ + gcongr + have HX : (a*shell s z)^2 ≤ r*(2*j₀) := hh₁.2 + have HY : (a*shell q z)^2 ≤ r*(2*j₁) := hh₂.2 + have h₀ : 0 ≤ j₀ := pierce_nonneg h.fit z + have h₁ : 0 ≤ j₁ := pierce_nonneg hp.climb.fit z + have he₀ : 0 ≤ f₀ := dew_nonneg u h z + have he₁ : 0 ≤ f₁ := dew_nonneg u hp.climb z + have HZ := pow_le_pow_left₀ (norm_nonneg _) He 2 + simp_rw [div_eq_mul_inv] at * + have HH := inv_pos.2 ha + nlinarith [mul_le_mul_of_nonneg_right hi hk, + mul_nonneg hr he₀,mul_nonneg hr he₁,mul_nonneg hr hk] + +lemma contest (x y z : ℂ) (r t p : ℝ) : + ‖x+y+z‖^2 + (r+t+p)^2 ≤ + 3*((‖x‖^2+‖y‖^2+‖z‖^2)+(r^2+t^2+p^2)) := by + linarith [square_addthree r t p,norm_addthree x y z] + +end Beam + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Checkpoint.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Checkpoint.lean index d23ac88549c81079a75896fcdb790c179fc63b48..75756b45012b84fd790cd77d70793ab9034ccbba 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Checkpoint.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Checkpoint.lean @@ -1,171 +1,171 @@ -import OAI.Analysis.FourierLLogL.Paths.Chief -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Run.Streak -open Beam - -def wave (e : Streak M) (t : Tide) : Path M → Tide -| [] => t -| b::l => wave e (e.rightNow b.post t).next l -lemma wave_combine (e : Streak M) (t : Tide) (l r : Path M) : - e.wave t (l++r)=e.wave (e.wave t l) r := by - induction l generalizing t with - | nil => rfl - | cons b l ih => exact ih _ - -lemma wave_bling (e : Streak M) (t : Tide) (l r : Path M) : - e.sumbling (e.wave t l) r ≤ e.sumbling t (l++r) := by - induction l generalizing t with - | nil => exact le_rfl - | cons b l ih => - change _ ≤ sumbling e t (b::(l++r)) - rw [sumbling] - apply le_trans (ih _) - apply le_add_of_nonneg_left - split; linarith [(bling_share e (b::(l++r))).1] - exact le_rfl - -lemma holds_tail (e : Streak M) (p r : Path M) (n : ℝ) (hp : e.wave none p=some n) : - ∃ v w : Path M, p=v++w ∧ (∀ b∈w,e.outer b.post) ∧ - fullFax w=n ∧ e.bling (w++r) ≤ e.sumbling none (p++r) := by - induction p using List.reverseRecOn generalizing n r with - | nil => cases hp - | append_singleton p b ih => - rw [wave_combine] at hp - let t := e.wave none p; let k := e.rightNow b.post t - let a := b.post - change k.next=some n at hp - have hx : e.sumbling t ([b]++r) ≤ e.sumbling none ((p++[b])++r) := by - rw [List.append_assoc] - exact wave_bling e none p _ - have HH (ha : k.alpha ∨ k.tick) (h : fax a=n) (hF : outer e a) : - ∃ v w : Path M,p++[b]=v++w ∧ (∀ z∈w,e.outer z.post) ∧ - fullFax w=n ∧ e.bling (w++r) ≤ e.sumbling none ((p++[b])++r) := by - refine ⟨p,[b],rfl,by simpa using hF,?_,?_⟩ - · change fax a+0=_; simpa only [add_zero] using h - · apply le_trans _ hx; change bling _ _ ≤ sumbling e t (b::r); rw [sumbling,ite_eq_left ha] - apply le_add_of_nonneg_right; linarith [sumbling_one e (rightNow e b.post t).next r] - cases ht : t with - | none => - by_cases hV : innerV e b.post - · have hi : k=going true false (fax b.post) := by unfold k; rw [ht]; exact ite_eq_left hV - rw [hi] at hp - exact HH (by rw [hi]; exact Or.inl rfl) (Option.some.inj hp) (inner_outer e _ hV) - · have hi : k=idle := by unfold k; rw [ht]; exact ite_eq_right hV - rw [hi] at hp; cases hp - | some x => - by_cases hV : outer e b.post - · by_cases hz : 1 ≤ x - · have hi : k=going false true (fax b.post) := by simp only [k,ht,rightNow,hV,hz, ↓reduceIte] - rw [hi] at hp - exact HH (by rw [hi]; exact Or.inr rfl) (Option.some.inj hp) hV - have hi : k=going false false (x+fax b.post) := by simp only [k,ht,rightNow,hV,hz, ↓reduceIte] - rw [hi] at hp - obtain ⟨v,w,hw,h1,h2,h3⟩ := ih (b::r) x ht - refine ⟨v,w++[b],by rw [hw,List.append_assoc],?_,?_,?_⟩ - · intro z hz; simp only [List.mem_append,List.mem_singleton] at hz - exact hz.elim (fun hh => h1 z hh) (fun h => h ▸ hV) - · unfold fullFax; rw [subtotal_app] - change fullFax w+(fax b.post+0)=n - rw [h2,add_zero]; exact Option.some.inj hp - simp only [List.append_assoc, List.singleton_append] - exact h3 - · have hi : k=idle := by unfold k; rw [ht]; exact ite_eq_right hV - rw [hi] at hp; cases hp - -lemma holds_inner (e : Streak M) (p l m : Path M) (h : ∀ b∈l,e.innerV b.post) (hh : l ≠ []) : - ∃ v w : Path M, p=v++w ∧ (∀ b∈w++l,e.outer b.post) ∧ fullFax w ≤ 1 ∧ - e.bling (w++(l++m)) ≤ e.sumbling none (p++(l++m)) := by - cases l with - | nil => exact (hh rfl).elim - | cons b l => - let r := b::l; let t := e.wave none p; let k := e.rightNow b.post t - have hv : ∀ b ∈ r,e.outer b.post := fun b hi => inner_outer e b.post (h b hi) - have hh' : innerV e b.post := h b List.mem_cons_self - have ho : outer e b.post := inner_outer e _ hh' - by_cases ha : k.alpha=true ∨ k.tick=true - · refine ⟨p,[],(by simp),hv,(by change (0:ℝ) ≤ 1; norm_num),?_⟩ - apply le_trans _ (wave_bling e none p (r++m)) - change bling e (b::(l++m)) ≤ sumbling e t (b::(l++m)); rw [sumbling,ite_eq_left ha] - apply le_add_of_nonneg_right; linarith [sumbling_one e (rightNow e b.post t).next (l++m)] - · cases hx : t with - | none => - apply (ha ?_).elim - apply Or.inl; unfold k; rw [hx] - rw [rightNow,ite_eq_left hh']; rfl - | some x => - have he : x < 1 := by - by_contra! he - apply ha; apply Or.inr; unfold k; rw [hx] - simp [rightNow,ho,he,going] - obtain ⟨v,w,hq,h₁,h₂,h₃⟩ := holds_tail e p (r++m) x hx - refine ⟨v,w,hq,?_,(h₂.le.trans he.le),h₃⟩ - intro c hc; rw [List.mem_append] at hc - exact hc.elim (h₁ c) (hv c) - -lemma possible (e : Streak M) {x : Grid M} (p l m : Path M) - (h : Trip x s (p++(l++m))) (hp : ∀ b∈l,e.innerV b.post) - (t : ℝ) (ht : 2*log (sumbling e none (p++(l++m))) ≤ t) : - - let L := p++(l++m); yieldW (subtotal (duties x e.u) l) ≤ - 10^15*(fullFax l+(1+fullKills L+e.flagged L)/e.thick+e.thick*t) := by - intro L - let a := e.thick; let F := duties x e.u - have ha : 0 < a := e.thick_rate.1 - have hi : a ≤ 1 := e.thick_rate.2.1 - have hT : 0 ≤ t := le_trans (mul_nonneg (by norm_num) - (log_nonneg (sumbling_one e none L))) ht - have hL : 0 ≤ fullKills L := trip_K h; have hE : 0 ≤ flagged e L := flagged_pos e h - by_cases H : l=[] - · rw [H,subtotal_nil,yield_zero] - change (0:ℝ) ≤ 10^15*(0+(1+fullKills L+flagged e L)/a+a*t) - positivity - obtain ⟨v,w,hv,hq,hf,hB⟩ := holds_inner e p l m hp H - have HE : L=v++((w++l)++m) := by unfold L; simp [hv] - have H0 : Trip x s (v++((w++l)++m)) := HE ▸ h - obtain ⟨h₁,s',hh⟩ := trip_cut H0 - obtain ⟨h₂,q,h₄⟩ := trip_cut hh - obtain ⟨h₃,r,h₅⟩ := trip_cut h₂ - have HX : 2*log (bling e (w++(l++m))) ≤ t := - le_trans (mul_le_mul_of_nonneg_left (log_le_log - (by linarith [(bling_share e (w++(l++m))).1]) hB) (by norm_num)) ht - rw [show subtotal (duties x e.u) l=subtotal F (w++l)-subtotal F w by rw [subtotal_app]; unfold F; abel] - apply (yield_sub _ _).trans - apply le_trans (add_le_add - (ribbon_bound e _ _ h₂ hq t (show 2*log (e.bling ((w++l)++m)) ≤ t from by rwa [List.append_assoc])) - (ribbon_bound e _ _ h₃ (fun b hb => hq b (List.mem_append_left _ hb)) t HX)) - have hiP (g : Beat M → ℝ) : - subtotal g L=subtotal g v+subtotal g (w++l)+subtotal g m := by - rw [HE,subtotal_app,subtotal_app]; linarith - have H₁ (p l : Path M) : fullKills (p++l)=fullKills p+fullKills l := subtotal_app _ _ _ - have H₂ (p l : Path M) : flagged e (p++l)=flagged e p+flagged e l := subtotal_app _ _ _ - have hk : fullKills w ≤ fullKills (w++l) ∧ fullKills (w++l) ≤ fullKills L := by - have H : fullKills L=fullKills v+fullKills (w++l)+fullKills m := hiP killed - constructor <;> linarith [trip_K h₁,trip_K h₄,trip_K h₅,H₁ w l] - have he : flagged e w ≤ flagged e (w++l) ∧ flagged e (w++l) ≤ flagged e L := by - have H : flagged e L=flagged e v+flagged e (w++l)+flagged e m := hiP _ - constructor <;> linarith [flagged_pos e h₁,flagged_pos e h₄,flagged_pos e h₅,H₂ w l] - rw [show fullFax (w++l)=fullFax w+fullFax l from subtotal_app _ w l] - have hY : 1 ≤ (e.thick)⁻¹ := (one_le_inv₀ ha).mpr hi - unfold a at * - simp_rw [div_eq_mul_inv] - nlinarith [trip_fax h₅] - -end Run.Streak - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Chief +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Run.Streak +open Beam + +def wave (e : Streak M) (t : Tide) : Path M → Tide +| [] => t +| b::l => wave e (e.rightNow b.post t).next l +lemma wave_combine (e : Streak M) (t : Tide) (l r : Path M) : + e.wave t (l++r)=e.wave (e.wave t l) r := by + induction l generalizing t with + | nil => rfl + | cons b l ih => exact ih _ + +lemma wave_bling (e : Streak M) (t : Tide) (l r : Path M) : + e.sumbling (e.wave t l) r ≤ e.sumbling t (l++r) := by + induction l generalizing t with + | nil => exact le_rfl + | cons b l ih => + change _ ≤ sumbling e t (b::(l++r)) + rw [sumbling] + apply le_trans (ih _) + apply le_add_of_nonneg_left + split; linarith [(bling_share e (b::(l++r))).1] + exact le_rfl + +lemma holds_tail (e : Streak M) (p r : Path M) (n : ℝ) (hp : e.wave none p=some n) : + ∃ v w : Path M, p=v++w ∧ (∀ b∈w,e.outer b.post) ∧ + fullFax w=n ∧ e.bling (w++r) ≤ e.sumbling none (p++r) := by + induction p using List.reverseRecOn generalizing n r with + | nil => cases hp + | append_singleton p b ih => + rw [wave_combine] at hp + let t := e.wave none p; let k := e.rightNow b.post t + let a := b.post + change k.next=some n at hp + have hx : e.sumbling t ([b]++r) ≤ e.sumbling none ((p++[b])++r) := by + rw [List.append_assoc] + exact wave_bling e none p _ + have HH (ha : k.alpha ∨ k.tick) (h : fax a=n) (hF : outer e a) : + ∃ v w : Path M,p++[b]=v++w ∧ (∀ z∈w,e.outer z.post) ∧ + fullFax w=n ∧ e.bling (w++r) ≤ e.sumbling none ((p++[b])++r) := by + refine ⟨p,[b],rfl,by simpa using hF,?_,?_⟩ + · change fax a+0=_; simpa only [add_zero] using h + · apply le_trans _ hx; change bling _ _ ≤ sumbling e t (b::r); rw [sumbling,ite_eq_left ha] + apply le_add_of_nonneg_right; linarith [sumbling_one e (rightNow e b.post t).next r] + cases ht : t with + | none => + by_cases hV : innerV e b.post + · have hi : k=going true false (fax b.post) := by unfold k; rw [ht]; exact ite_eq_left hV + rw [hi] at hp + exact HH (by rw [hi]; exact Or.inl rfl) (Option.some.inj hp) (inner_outer e _ hV) + · have hi : k=idle := by unfold k; rw [ht]; exact ite_eq_right hV + rw [hi] at hp; cases hp + | some x => + by_cases hV : outer e b.post + · by_cases hz : 1 ≤ x + · have hi : k=going false true (fax b.post) := by simp only [k,ht,rightNow,hV,hz, ↓reduceIte] + rw [hi] at hp + exact HH (by rw [hi]; exact Or.inr rfl) (Option.some.inj hp) hV + have hi : k=going false false (x+fax b.post) := by simp only [k,ht,rightNow,hV,hz, ↓reduceIte] + rw [hi] at hp + obtain ⟨v,w,hw,h1,h2,h3⟩ := ih (b::r) x ht + refine ⟨v,w++[b],by rw [hw,List.append_assoc],?_,?_,?_⟩ + · intro z hz; simp only [List.mem_append,List.mem_singleton] at hz + exact hz.elim (fun hh => h1 z hh) (fun h => h ▸ hV) + · unfold fullFax; rw [subtotal_app] + change fullFax w+(fax b.post+0)=n + rw [h2,add_zero]; exact Option.some.inj hp + simp only [List.append_assoc, List.singleton_append] + exact h3 + · have hi : k=idle := by unfold k; rw [ht]; exact ite_eq_right hV + rw [hi] at hp; cases hp + +lemma holds_inner (e : Streak M) (p l m : Path M) (h : ∀ b∈l,e.innerV b.post) (hh : l ≠ []) : + ∃ v w : Path M, p=v++w ∧ (∀ b∈w++l,e.outer b.post) ∧ fullFax w ≤ 1 ∧ + e.bling (w++(l++m)) ≤ e.sumbling none (p++(l++m)) := by + cases l with + | nil => exact (hh rfl).elim + | cons b l => + let r := b::l; let t := e.wave none p; let k := e.rightNow b.post t + have hv : ∀ b ∈ r,e.outer b.post := fun b hi => inner_outer e b.post (h b hi) + have hh' : innerV e b.post := h b List.mem_cons_self + have ho : outer e b.post := inner_outer e _ hh' + by_cases ha : k.alpha=true ∨ k.tick=true + · refine ⟨p,[],(by simp),hv,(by change (0:ℝ) ≤ 1; norm_num),?_⟩ + apply le_trans _ (wave_bling e none p (r++m)) + change bling e (b::(l++m)) ≤ sumbling e t (b::(l++m)); rw [sumbling,ite_eq_left ha] + apply le_add_of_nonneg_right; linarith [sumbling_one e (rightNow e b.post t).next (l++m)] + · cases hx : t with + | none => + apply (ha ?_).elim + apply Or.inl; unfold k; rw [hx] + rw [rightNow,ite_eq_left hh']; rfl + | some x => + have he : x < 1 := by + by_contra! he + apply ha; apply Or.inr; unfold k; rw [hx] + simp [rightNow,ho,he,going] + obtain ⟨v,w,hq,h₁,h₂,h₃⟩ := holds_tail e p (r++m) x hx + refine ⟨v,w,hq,?_,(h₂.le.trans he.le),h₃⟩ + intro c hc; rw [List.mem_append] at hc + exact hc.elim (h₁ c) (hv c) + +lemma possible (e : Streak M) {x : Grid M} (p l m : Path M) + (h : Trip x s (p++(l++m))) (hp : ∀ b∈l,e.innerV b.post) + (t : ℝ) (ht : 2*log (sumbling e none (p++(l++m))) ≤ t) : + + let L := p++(l++m); yieldW (subtotal (duties x e.u) l) ≤ + 10^15*(fullFax l+(1+fullKills L+e.flagged L)/e.thick+e.thick*t) := by + intro L + let a := e.thick; let F := duties x e.u + have ha : 0 < a := e.thick_rate.1 + have hi : a ≤ 1 := e.thick_rate.2.1 + have hT : 0 ≤ t := le_trans (mul_nonneg (by norm_num) + (log_nonneg (sumbling_one e none L))) ht + have hL : 0 ≤ fullKills L := trip_K h; have hE : 0 ≤ flagged e L := flagged_pos e h + by_cases H : l=[] + · rw [H,subtotal_nil,yield_zero] + change (0:ℝ) ≤ 10^15*(0+(1+fullKills L+flagged e L)/a+a*t) + positivity + obtain ⟨v,w,hv,hq,hf,hB⟩ := holds_inner e p l m hp H + have HE : L=v++((w++l)++m) := by unfold L; simp [hv] + have H0 : Trip x s (v++((w++l)++m)) := HE ▸ h + obtain ⟨h₁,s',hh⟩ := trip_cut H0 + obtain ⟨h₂,q,h₄⟩ := trip_cut hh + obtain ⟨h₃,r,h₅⟩ := trip_cut h₂ + have HX : 2*log (bling e (w++(l++m))) ≤ t := + le_trans (mul_le_mul_of_nonneg_left (log_le_log + (by linarith [(bling_share e (w++(l++m))).1]) hB) (by norm_num)) ht + rw [show subtotal (duties x e.u) l=subtotal F (w++l)-subtotal F w by rw [subtotal_app]; unfold F; abel] + apply (yield_sub _ _).trans + apply le_trans (add_le_add + (ribbon_bound e _ _ h₂ hq t (show 2*log (e.bling ((w++l)++m)) ≤ t from by rwa [List.append_assoc])) + (ribbon_bound e _ _ h₃ (fun b hb => hq b (List.mem_append_left _ hb)) t HX)) + have hiP (g : Beat M → ℝ) : + subtotal g L=subtotal g v+subtotal g (w++l)+subtotal g m := by + rw [HE,subtotal_app,subtotal_app]; linarith + have H₁ (p l : Path M) : fullKills (p++l)=fullKills p+fullKills l := subtotal_app _ _ _ + have H₂ (p l : Path M) : flagged e (p++l)=flagged e p+flagged e l := subtotal_app _ _ _ + have hk : fullKills w ≤ fullKills (w++l) ∧ fullKills (w++l) ≤ fullKills L := by + have H : fullKills L=fullKills v+fullKills (w++l)+fullKills m := hiP killed + constructor <;> linarith [trip_K h₁,trip_K h₄,trip_K h₅,H₁ w l] + have he : flagged e w ≤ flagged e (w++l) ∧ flagged e (w++l) ≤ flagged e L := by + have H : flagged e L=flagged e v+flagged e (w++l)+flagged e m := hiP _ + constructor <;> linarith [flagged_pos e h₁,flagged_pos e h₄,flagged_pos e h₅,H₂ w l] + rw [show fullFax (w++l)=fullFax w+fullFax l from subtotal_app _ w l] + have hY : 1 ≤ (e.thick)⁻¹ := (one_le_inv₀ ha).mpr hi + unfold a at * + simp_rw [div_eq_mul_inv] + nlinarith [trip_fax h₅] + +end Run.Streak + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Chief.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Chief.lean index 74ff7c3729dcc03ee58ae04d64a86fef4b6cbea1..624955e43cf9e683c2c248dcb1004539b6d1efa5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Chief.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Chief.lean @@ -1,277 +1,277 @@ -import OAI.Analysis.FourierLLogL.Paths.Upkeep -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Run.Streak - -abbrev Tide := Option ℝ -structure Tick where - alpha : Bool - tick : Bool - next : Tide - -def idle : Tick := ⟨false,false,none⟩ -def going (first meter : Bool) (x : ℝ) : Tick := ⟨first,meter,some x⟩ -def rightNow (e : Streak M) (s : Scene M) : Tide → Tick - | none => if e.innerV s then going true false (fax s) else idle - | some x => if e.outer s then - if 1 ≤ x then going false true (fax s) else going false false (x+fax s) - else idle - -def events (e : Streak M) (fresh : Bool) (t : Tide) : Path M → ℝ - | [] => 0 - | b::l => - let k := e.rightNow b.post t - (if (if fresh then k.alpha else k.tick) then 1 else 0) + events e fresh k.next l - -def sumbling (e : Streak M) (t : Tide) : Path M → ℝ - | [] => 1 - | b::l => - let k := e.rightNow b.post t - (if k.alpha ∨ k.tick then e.bling (b::l) else 0) + sumbling e k.next l - -lemma sumbling_one (e : Streak M) (t : Tide) (l : Path M) : - 1 ≤ e.sumbling t l := by - induction l generalizing t with - | nil => exact le_rfl - | cons b l ih => - rw [sumbling]; split - · linarith [(bling_share e (b::l)).1,ih (rightNow e b.post t).next] - rw [zero_add]; exact ih _ - -lemma spark_mean (e : Streak M) {s : Scene M} (v : Thread M s) (t : Tide) - (h : Following j J f v) : - stats v (e.sumbling t) ≤ 1+6*(stats v (e.events true t)+stats v (e.events false t)) := by - induction v generalizing t with - | blank => rw [stats_nil,stats_nil,stats_nil]; change (1:ℝ) ≤ 1+6*(0+0); norm_num - | ran s q k ih => - rw [stats_cons h,stats_cons h,stats_cons h] - let m := rightNow e q t - let U := uniform Bool - let A := m.next - have hh (b : Bool) : - stats (k b) (e.sumbling A) ≤ 1+6*(stats (k b) (e.events true A) + stats (k b) (e.events false A)) := - ih b A (h.2.2.2.2.2 b) - have H := U.Ex_mono hh - rw [Ex_add (f:=fun _ => _),Ex_const, Ex_lmul, Ex_add] at H - have HH := bling_mean e (ran s q k) h - change stats (ran s q k) (bling e) ≤ _ at HH - rw [stats_cons h] at HH - simp_rw [sumbling,events] - by_cases h1 : (rightNow e q t).alpha=true <;> - by_cases h2 : (rightNow e q t).tick=true <;> - simp only [h1,h2,or_true,true_or,Bool.false_eq_true,false_or,↓reduceIte] <;> - (try simp_rw [stats_plus _ (f:=fun _ => _),Ex_add _ (f:=fun _ => _)]) - all_goals - - unfold A m U at H - unfold stats at * - try simp_rw [Ex_add _ (f:=fun _ => _)] - simp_rw [Ex_const] - linarith - -def powerOf (t : Tide) := t.getD 0 - -lemma checkpoints (e : Streak M) (t : Tide) {s : Scene M} {l : Path M} - {x : Grid M} (h : Trip x s l) (ht : 0 ≤ powerOf t) : - events e false t l ≤ powerOf t+fullFax l := by - induction l generalizing s t with - | nil => change 0 ≤ powerOf t+0; linarith - | cons b l ih => - have HH := (fax_floor h.2.1.climb).1 - have H (t : Tide) (hh : 0 ≤ powerOf t) := ih t h.2.2 hh - rw [events] - change (if (rightNow e b.post t).tick then 1 else 0)+_ - ≤ powerOf t+(fax b.post+fullFax l) - cases t with - | none => - split_ifs with h₁ - all_goals by_cases hx : e.innerV b.post - all_goals simp only [rightNow,hx,↓reduceIte,idle,going] at h₁ ⊢ - all_goals - first | contradiction | - (have hi := H (some (fax b.post)) HH - simp only [powerOf,Option.getD] at *; linarith) | - (have hi := H none (by change (0:ℝ) ≤ 0; norm_num) - simp only [powerOf,Option.getD] at *; linarith) - | some z => - change 0 ≤ z at ht - by_cases h₁ : e.outer b.post - · by_cases h₂ : 1 ≤ z - · have hi := H (some (fax b.post)) HH - simp only [rightNow,going,h₁,h₂,↓reduceIte,powerOf,Option.getD] at hi ⊢; linarith - have hi := H (some (z+fax b.post)) (add_nonneg ht HH) - simp only [rightNow,going,h₁,h₂,↓reduceIte,Bool.false_eq_true,powerOf,Option.getD] at * - linarith - · have hi := H none (by change (0:ℝ) ≤ 0; norm_num) - simp only [powerOf,rightNow,idle,h₁,Bool.false_eq_true,↓reduceIte,Option.getD] at *; linarith - -def slackOf (e : Streak M) (s : Scene M) (t : Tide) := - if t.isSome then energy s e.u else 128*e.b^2 -def breakFare (e : Streak M) (b : Beat M) := - shower e.u b.post+8*killed b+4*flag e b.post -def quota (e : Streak M) := 64*e.b^2 - -lemma slack_nonneg (e : Streak M) (s : Scene M) (h : Fit s) (t : Tide) : - 0 ≤ slackOf e s t := by - unfold slackOf; split; exact (energy_small h e.u).1; positivity - -lemma fare_step (e : Streak M) (s q : Scene M) (h : Ideal s q) (t : Tide) : - let T := rightNow e q t - (uniform Bool).Ex (fun b => slackOf e (q.smaller b) T.next) - + (if T.alpha then e.quota else 0) ≤ slackOf e s t + breakFare e ⟨s,q,false⟩ := by - intro T - let v := fun b => energy (q.smaller b) e.u - let g := shower e.u q - let K := killed (Thread.Beat.mk s q false) - let Q := e.b^2 - have he : (v false+v true)/2=energy q e.u+g := wet_average q h.climb e.u - have hH (b) : 0 ≤ v b ∧ v b ≤ 4 := - let H := energy_small (h.climb.child b) e.u - ⟨H.1,H.2.trans (h.climb.child b).lift⟩ - have hq : 0 ≤ Q := sq_nonneg _ - have hz : 0 ≤ K := Ideal.kill_nonneg (B:=⟨s,q,false⟩) h - have hG : 0 ≤ g := shower_nonneg h.climb - have hf := flag_nonneg e q - have HH : |energy s e.u-energy q e.u| ≤ 8*K := energy_drain (b:=⟨s,q,false⟩) h e.u - let B := g+8*K+4*flag e q - change _+(if T.alpha=true then 64*Q else 0) ≤ _+B - cases t with - | none => - by_cases hp : innerV e q - · rw [show T=going true false (fax q) from ite_eq_left hp, fair_av] - change _ + _ ≤ 128*Q+B - simp only [slackOf,going,Option.isSome_some,↓reduceIte] - change (v false+v true)/2+64*Q ≤ _ - rcases hp with h1|h1 - · unfold B - rw [show flag e q=1 from ite_eq_left h1]; linarith [(hH false).2,(hH true).2] - · have hx (b) : v b ≤ 64*Q := h1 b - unfold B; linarith [hx false,hx true] - · rw [show T=idle from ite_eq_right hp] - rw [show (uniform Bool).Ex (fun b => slackOf e (q.smaller b) idle.next) =128*Q from Ex_const (uniform Bool) (128*Q)] - simp only [idle,Bool.false_eq_true,↓reduceIte] - change _ ≤ 128*Q+B; unfold B; linarith - | some x => - by_cases hp : outer e q - · have HE : T.next.isSome=true ∧ T.alpha=false := by - simp only [T,rightNow,ite_eq_left hp]; split <;> simp [going] - rw [fair_av] - simp only [slackOf,HE.1,HE.2,Bool.false_eq_true,Option.isSome_some,↓reduceIte] - change (v false+v true)/2+_ ≤ _+B; unfold B - linarith [(abs_le.mp HH).1] - · rw [show T=idle from ite_eq_right hp] - rw [show (uniform Bool).Ex (fun b => slackOf e (q.smaller b) idle.next) =128*Q from Ex_const (uniform Bool) (128*Q)] - simp only [idle,Bool.false_eq_true,↓reduceIte] - have hi : ¬ ∀ b, v b ≤ 256*Q := fun HH => hp (Or.inr HH) - push Not at hi - obtain ⟨b,hb⟩ := hi - change _ ≤ energy s e.u+B; unfold B - cases b <;> linarith [(hH true).1,(hH false).1,(abs_le.mp HH).1] - -lemma fare_total (e : Streak M) {s : Scene M} (v : Thread M s) (t : Tide) - (h : Following j J f v) (hp : Fit s) : - e.quota*stats v (e.events true t) ≤ slackOf e s t+stats v (subtotal (breakFare e)) := by - induction v generalizing t with - | blank s => - rw [stats_nil,stats_nil] - change _*0 ≤ _+0 - linarith [slack_nonneg e s hp t] - | ran s q k ih => - have HH := jump_ideal h hp.lift - rw [stats_cons h,stats_cons h] - let T := rightNow e q t - let A := if T.alpha=true then (1:ℝ) else 0 - let B := breakFare e (Thread.Beat.mk s q false) - change e.quota*_ ≤ _+_ - have HF (b : Bool) : breakFare e ⟨s,q,b⟩=B := rfl - simp_rw [events,subtotal_cons,HF] - have HE (b : Bool) := ih b T.next (h.2.2.2.2.2 b) (HH.climb.child _) - have HI := (uniform Bool).Ex_mono HE - have HC := e.fare_step s q HH t - have H0 : e.quota*A=if T.alpha=true then e.quota else 0 := by unfold A; split <;> ring - unfold stats at * - simp_rw [Ex_add _ (f:=fun _ => _),Ex_const] - rw [Ex_add,Ex_lmul] at HI - change _+(if T.alpha then _ else _) ≤ _+B at HC - rw [← H0] at HC - unfold A T at * - simp only [↓reduceIte] - linarith - -lemma expected_bling : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s q : Scene M, - ∀ f : Grid M → ℝ, ∀ k, Following j J f (ran s q k) → Fit s → q.live=true → - (∀ x, f x ≤ exp (height j)) → - ∀ e : Streak M, e.i ≤ mainIx j → - stats (ran s q k) (e.sumbling none) ≤ 10^30*(height j)^3 := by - filter_upwards [sum_budget_run,hpoly 12800 32] with j hj hs M J s q f k h hh hq hF e he - let v := ran s q k - obtain ⟨h1,h2,h3,h4⟩ := hj M J f s q k h hh hq hF e - let x := stats v (e.events true none); let y := stats v (e.events false none) - let b := e.b^2; let d := height j - have hi : 1 ≤ d := by nlinarith [H_ge j,show (1:ℝ) ≤ Thermal.H j^32 from one_le_pow₀ (H_ge j)] - have HE : 1 ≤ d*b := by - have hh1 := (main_bounds j (show e.i ≤ testIx j by unfold testIx; omega)).1 - have hE := Thermal.H_pos j - rw [div_le_iff₀ (by positivity)] at hh1 - change 1/6400 ≤ b/100*Thermal.H j^32 at hh1 - have HB : 0 ≤ b := sq_nonneg _ - change _ ≤ d at hs - nlinarith - have hb : 0 < b := pow_pos e.rate.1 _; have hh' : b ≤ 1 := pow_le_one₀ e.rate.1.le e.rate.2 - have HR := spark_mean e v none h - apply HR.trans - change 1+6*(x+y) ≤ 10^30*d^3 - have HV : y ≤ stats v fullFax := by - apply Ex_mono_mem - intro i hi - have hL : i∈s.here.block M := (dice_positive hh.ready.1).1 hi - have HE := checkpoints e none (trip_stream v h hh hL) - (by change (0:ℝ) ≤ 0; norm_num) - rw [show powerOf none=0 from rfl, zero_add] at HE; exact HE - let z := stats v e.flagged - change stats v _ ≤ _ at h1 h2 h3 - change b*z ≤ 10^6*d at h4 - have hz : z ≤ 10^6*d^2 := by - by_contra! HH - nlinarith [mul_le_mul_of_nonneg_left HE (show 0 ≤ 10^6*d by positivity)] - have hh0 := fare_total e v none h hh - have hx : stats v (subtotal (breakFare e)) = - stats v (e.showers) + 8*stats v fullKills + 4*z := by - have HI (l : Path M) : subtotal (breakFare e) l = e.showers l+8*fullKills l+4*e.flagged l := by - induction l with - | nil => change (0:ℝ)=0+_*0+_*0; ring - | cons b l ih => - rw [subtotal_cons,ih] - change shower e.u b.post+8*killed b+4*flag e b.post+_= - shower e.u b.post+e.showers l+8*(killed b+fullKills l)+4*(flag e b.post+e.flagged l) - ring - unfold z stats - simp_rw [HI,Ex_add,Ex_lmul] - rw [hx] at hh0 - change 64*b*x ≤ 128*b+_ at hh0 - have HC : 64*b*x ≤ 10^7*d^2 := by nlinarith - have HQ : x ≤ 10^7*d^3 := by - by_contra! hx - nlinarith [mul_le_mul_of_nonneg_left HE (show 0 ≤ 10^7*d^2 by positivity)] - change stats v fullFax ≤ 10^26*d at h3 - nlinarith - -end Run.Streak - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Upkeep +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Run.Streak + +abbrev Tide := Option ℝ +structure Tick where + alpha : Bool + tick : Bool + next : Tide + +def idle : Tick := ⟨false,false,none⟩ +def going (first meter : Bool) (x : ℝ) : Tick := ⟨first,meter,some x⟩ +def rightNow (e : Streak M) (s : Scene M) : Tide → Tick + | none => if e.innerV s then going true false (fax s) else idle + | some x => if e.outer s then + if 1 ≤ x then going false true (fax s) else going false false (x+fax s) + else idle + +def events (e : Streak M) (fresh : Bool) (t : Tide) : Path M → ℝ + | [] => 0 + | b::l => + let k := e.rightNow b.post t + (if (if fresh then k.alpha else k.tick) then 1 else 0) + events e fresh k.next l + +def sumbling (e : Streak M) (t : Tide) : Path M → ℝ + | [] => 1 + | b::l => + let k := e.rightNow b.post t + (if k.alpha ∨ k.tick then e.bling (b::l) else 0) + sumbling e k.next l + +lemma sumbling_one (e : Streak M) (t : Tide) (l : Path M) : + 1 ≤ e.sumbling t l := by + induction l generalizing t with + | nil => exact le_rfl + | cons b l ih => + rw [sumbling]; split + · linarith [(bling_share e (b::l)).1,ih (rightNow e b.post t).next] + rw [zero_add]; exact ih _ + +lemma spark_mean (e : Streak M) {s : Scene M} (v : Thread M s) (t : Tide) + (h : Following j J f v) : + stats v (e.sumbling t) ≤ 1+6*(stats v (e.events true t)+stats v (e.events false t)) := by + induction v generalizing t with + | blank => rw [stats_nil,stats_nil,stats_nil]; change (1:ℝ) ≤ 1+6*(0+0); norm_num + | ran s q k ih => + rw [stats_cons h,stats_cons h,stats_cons h] + let m := rightNow e q t + let U := uniform Bool + let A := m.next + have hh (b : Bool) : + stats (k b) (e.sumbling A) ≤ 1+6*(stats (k b) (e.events true A) + stats (k b) (e.events false A)) := + ih b A (h.2.2.2.2.2 b) + have H := U.Ex_mono hh + rw [Ex_add (f:=fun _ => _),Ex_const, Ex_lmul, Ex_add] at H + have HH := bling_mean e (ran s q k) h + change stats (ran s q k) (bling e) ≤ _ at HH + rw [stats_cons h] at HH + simp_rw [sumbling,events] + by_cases h1 : (rightNow e q t).alpha=true <;> + by_cases h2 : (rightNow e q t).tick=true <;> + simp only [h1,h2,or_true,true_or,Bool.false_eq_true,false_or,↓reduceIte] <;> + (try simp_rw [stats_plus _ (f:=fun _ => _),Ex_add _ (f:=fun _ => _)]) + all_goals + + unfold A m U at H + unfold stats at * + try simp_rw [Ex_add _ (f:=fun _ => _)] + simp_rw [Ex_const] + linarith + +def powerOf (t : Tide) := t.getD 0 + +lemma checkpoints (e : Streak M) (t : Tide) {s : Scene M} {l : Path M} + {x : Grid M} (h : Trip x s l) (ht : 0 ≤ powerOf t) : + events e false t l ≤ powerOf t+fullFax l := by + induction l generalizing s t with + | nil => change 0 ≤ powerOf t+0; linarith + | cons b l ih => + have HH := (fax_floor h.2.1.climb).1 + have H (t : Tide) (hh : 0 ≤ powerOf t) := ih t h.2.2 hh + rw [events] + change (if (rightNow e b.post t).tick then 1 else 0)+_ + ≤ powerOf t+(fax b.post+fullFax l) + cases t with + | none => + split_ifs with h₁ + all_goals by_cases hx : e.innerV b.post + all_goals simp only [rightNow,hx,↓reduceIte,idle,going] at h₁ ⊢ + all_goals + first | contradiction | + (have hi := H (some (fax b.post)) HH + simp only [powerOf,Option.getD] at *; linarith) | + (have hi := H none (by change (0:ℝ) ≤ 0; norm_num) + simp only [powerOf,Option.getD] at *; linarith) + | some z => + change 0 ≤ z at ht + by_cases h₁ : e.outer b.post + · by_cases h₂ : 1 ≤ z + · have hi := H (some (fax b.post)) HH + simp only [rightNow,going,h₁,h₂,↓reduceIte,powerOf,Option.getD] at hi ⊢; linarith + have hi := H (some (z+fax b.post)) (add_nonneg ht HH) + simp only [rightNow,going,h₁,h₂,↓reduceIte,Bool.false_eq_true,powerOf,Option.getD] at * + linarith + · have hi := H none (by change (0:ℝ) ≤ 0; norm_num) + simp only [powerOf,rightNow,idle,h₁,Bool.false_eq_true,↓reduceIte,Option.getD] at *; linarith + +def slackOf (e : Streak M) (s : Scene M) (t : Tide) := + if t.isSome then energy s e.u else 128*e.b^2 +def breakFare (e : Streak M) (b : Beat M) := + shower e.u b.post+8*killed b+4*flag e b.post +def quota (e : Streak M) := 64*e.b^2 + +lemma slack_nonneg (e : Streak M) (s : Scene M) (h : Fit s) (t : Tide) : + 0 ≤ slackOf e s t := by + unfold slackOf; split; exact (energy_small h e.u).1; positivity + +lemma fare_step (e : Streak M) (s q : Scene M) (h : Ideal s q) (t : Tide) : + let T := rightNow e q t + (uniform Bool).Ex (fun b => slackOf e (q.smaller b) T.next) + + (if T.alpha then e.quota else 0) ≤ slackOf e s t + breakFare e ⟨s,q,false⟩ := by + intro T + let v := fun b => energy (q.smaller b) e.u + let g := shower e.u q + let K := killed (Thread.Beat.mk s q false) + let Q := e.b^2 + have he : (v false+v true)/2=energy q e.u+g := wet_average q h.climb e.u + have hH (b) : 0 ≤ v b ∧ v b ≤ 4 := + let H := energy_small (h.climb.child b) e.u + ⟨H.1,H.2.trans (h.climb.child b).lift⟩ + have hq : 0 ≤ Q := sq_nonneg _ + have hz : 0 ≤ K := Ideal.kill_nonneg (B:=⟨s,q,false⟩) h + have hG : 0 ≤ g := shower_nonneg h.climb + have hf := flag_nonneg e q + have HH : |energy s e.u-energy q e.u| ≤ 8*K := energy_drain (b:=⟨s,q,false⟩) h e.u + let B := g+8*K+4*flag e q + change _+(if T.alpha=true then 64*Q else 0) ≤ _+B + cases t with + | none => + by_cases hp : innerV e q + · rw [show T=going true false (fax q) from ite_eq_left hp, fair_av] + change _ + _ ≤ 128*Q+B + simp only [slackOf,going,Option.isSome_some,↓reduceIte] + change (v false+v true)/2+64*Q ≤ _ + rcases hp with h1|h1 + · unfold B + rw [show flag e q=1 from ite_eq_left h1]; linarith [(hH false).2,(hH true).2] + · have hx (b) : v b ≤ 64*Q := h1 b + unfold B; linarith [hx false,hx true] + · rw [show T=idle from ite_eq_right hp] + rw [show (uniform Bool).Ex (fun b => slackOf e (q.smaller b) idle.next) =128*Q from Ex_const (uniform Bool) (128*Q)] + simp only [idle,Bool.false_eq_true,↓reduceIte] + change _ ≤ 128*Q+B; unfold B; linarith + | some x => + by_cases hp : outer e q + · have HE : T.next.isSome=true ∧ T.alpha=false := by + simp only [T,rightNow,ite_eq_left hp]; split <;> simp [going] + rw [fair_av] + simp only [slackOf,HE.1,HE.2,Bool.false_eq_true,Option.isSome_some,↓reduceIte] + change (v false+v true)/2+_ ≤ _+B; unfold B + linarith [(abs_le.mp HH).1] + · rw [show T=idle from ite_eq_right hp] + rw [show (uniform Bool).Ex (fun b => slackOf e (q.smaller b) idle.next) =128*Q from Ex_const (uniform Bool) (128*Q)] + simp only [idle,Bool.false_eq_true,↓reduceIte] + have hi : ¬ ∀ b, v b ≤ 256*Q := fun HH => hp (Or.inr HH) + push Not at hi + obtain ⟨b,hb⟩ := hi + change _ ≤ energy s e.u+B; unfold B + cases b <;> linarith [(hH true).1,(hH false).1,(abs_le.mp HH).1] + +lemma fare_total (e : Streak M) {s : Scene M} (v : Thread M s) (t : Tide) + (h : Following j J f v) (hp : Fit s) : + e.quota*stats v (e.events true t) ≤ slackOf e s t+stats v (subtotal (breakFare e)) := by + induction v generalizing t with + | blank s => + rw [stats_nil,stats_nil] + change _*0 ≤ _+0 + linarith [slack_nonneg e s hp t] + | ran s q k ih => + have HH := jump_ideal h hp.lift + rw [stats_cons h,stats_cons h] + let T := rightNow e q t + let A := if T.alpha=true then (1:ℝ) else 0 + let B := breakFare e (Thread.Beat.mk s q false) + change e.quota*_ ≤ _+_ + have HF (b : Bool) : breakFare e ⟨s,q,b⟩=B := rfl + simp_rw [events,subtotal_cons,HF] + have HE (b : Bool) := ih b T.next (h.2.2.2.2.2 b) (HH.climb.child _) + have HI := (uniform Bool).Ex_mono HE + have HC := e.fare_step s q HH t + have H0 : e.quota*A=if T.alpha=true then e.quota else 0 := by unfold A; split <;> ring + unfold stats at * + simp_rw [Ex_add _ (f:=fun _ => _),Ex_const] + rw [Ex_add,Ex_lmul] at HI + change _+(if T.alpha then _ else _) ≤ _+B at HC + rw [← H0] at HC + unfold A T at * + simp only [↓reduceIte] + linarith + +lemma expected_bling : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s q : Scene M, + ∀ f : Grid M → ℝ, ∀ k, Following j J f (ran s q k) → Fit s → q.live=true → + (∀ x, f x ≤ exp (height j)) → + ∀ e : Streak M, e.i ≤ mainIx j → + stats (ran s q k) (e.sumbling none) ≤ 10^30*(height j)^3 := by + filter_upwards [sum_budget_run,hpoly 12800 32] with j hj hs M J s q f k h hh hq hF e he + let v := ran s q k + obtain ⟨h1,h2,h3,h4⟩ := hj M J f s q k h hh hq hF e + let x := stats v (e.events true none); let y := stats v (e.events false none) + let b := e.b^2; let d := height j + have hi : 1 ≤ d := by nlinarith [H_ge j,show (1:ℝ) ≤ Thermal.H j^32 from one_le_pow₀ (H_ge j)] + have HE : 1 ≤ d*b := by + have hh1 := (main_bounds j (show e.i ≤ testIx j by unfold testIx; omega)).1 + have hE := Thermal.H_pos j + rw [div_le_iff₀ (by positivity)] at hh1 + change 1/6400 ≤ b/100*Thermal.H j^32 at hh1 + have HB : 0 ≤ b := sq_nonneg _ + change _ ≤ d at hs + nlinarith + have hb : 0 < b := pow_pos e.rate.1 _; have hh' : b ≤ 1 := pow_le_one₀ e.rate.1.le e.rate.2 + have HR := spark_mean e v none h + apply HR.trans + change 1+6*(x+y) ≤ 10^30*d^3 + have HV : y ≤ stats v fullFax := by + apply Ex_mono_mem + intro i hi + have hL : i∈s.here.block M := (dice_positive hh.ready.1).1 hi + have HE := checkpoints e none (trip_stream v h hh hL) + (by change (0:ℝ) ≤ 0; norm_num) + rw [show powerOf none=0 from rfl, zero_add] at HE; exact HE + let z := stats v e.flagged + change stats v _ ≤ _ at h1 h2 h3 + change b*z ≤ 10^6*d at h4 + have hz : z ≤ 10^6*d^2 := by + by_contra! HH + nlinarith [mul_le_mul_of_nonneg_left HE (show 0 ≤ 10^6*d by positivity)] + have hh0 := fare_total e v none h hh + have hx : stats v (subtotal (breakFare e)) = + stats v (e.showers) + 8*stats v fullKills + 4*z := by + have HI (l : Path M) : subtotal (breakFare e) l = e.showers l+8*fullKills l+4*e.flagged l := by + induction l with + | nil => change (0:ℝ)=0+_*0+_*0; ring + | cons b l ih => + rw [subtotal_cons,ih] + change shower e.u b.post+8*killed b+4*flag e b.post+_= + shower e.u b.post+e.showers l+8*(killed b+fullKills l)+4*(flag e b.post+e.flagged l) + ring + unfold z stats + simp_rw [HI,Ex_add,Ex_lmul] + rw [hx] at hh0 + change 64*b*x ≤ 128*b+_ at hh0 + have HC : 64*b*x ≤ 10^7*d^2 := by nlinarith + have HQ : x ≤ 10^7*d^3 := by + by_contra! hx + nlinarith [mul_le_mul_of_nonneg_left HE (show 0 ≤ 10^7*d^2 by positivity)] + change stats v fullFax ≤ 10^26*d at h3 + nlinarith + +end Run.Streak + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Encore.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Encore.lean index 5f680e1fb0b96395791a13f0cbf4250d95032414..c3a8785cb45b676f1d0e906755a0a4356552c38e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Encore.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Encore.lean @@ -1,216 +1,216 @@ -import OAI.Analysis.FourierLLogL.Paths.Ticket -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe - -def chooseCap (g : Grade) (l : Path M) := Nat.log 2 (broadMass l g)+1 -lemma choice_bounds (g : Grade) (l : Path M) : - let x := chooseCap g l - x ≤ M+2 ∧ broadMass l g ≤ rangeIx x ∧ scaleC x ≤ 2*((broadMass l g:ℝ)+1) := by - intro x - let n := broadMass l g - have hx : 2^x ≤ 2*(n+1) := (by - change 2^(Nat.log 2 n+1) ≤ _ - have H := Nat.pow_log_le_add_one 2 n - rw [pow_add]; omega) - have H : n ≤ 2^M := by - apply le_trans (Finset.card_le_univ _) (by rw [ZMod.card]) - refine ⟨?_,(Nat.lt_pow_succ_log_self (show 1 < 2 by norm_num) _).le,?_⟩ - · by_contra! hh - have HI : 2^(M+2) < 2^x := Nat.pow_lt_pow_right (by omega) hh - have HF : 0 < 2^M := by positivity - rw [pow_add] at HI - norm_num at HI; omega - · unfold scaleC - change (2:ℝ)^x ≤ 2*((n:ℝ)+1) - exact_mod_cast hx - -def heartP (s : Scene M) (g : Grade) (j : ℕ) := - 10^82*(scaleC g.i)^14 * (amount s j)^4 * exp (cutBook g s j) - -lemma booking : - ∀ᶠ j : ℕ in atTop, - ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - (∀ x,f x ≤ exp (height j)) → - ∀ g : Grade, entitled g s j → ∀ m : ℕ, - stats t (showScore (stage g m) (vacant M) j) ≤ heartP s g j* scaleC m^4 := by - filter_upwards [paint_birth,prelude] with j h₁ h₂ M J s f t h hh he hf g hg m - apply (h₁ M J _ _ f (stage g m) h hh hg hf _).trans - let x := scaleC g.i; let y := scaleC m; let L := amount s j; let z := exp (cutBook g s j) - have hx : 1 ≤ x := scale_one _; have hy : 1 ≤ y := scale_one _ - have hL : 1 ≤ L := (amount_limits _ _).1 - have hz : 1 ≤ z := one_le_exp (cut_nonneg _ _ _) - have HV := (h₂ M J s f t h hh he hf (stage g m)).1 - change stats t _ ≤ 10^50*x^14*L*y^4 at HV - change 1+10^30*L^3*z*_ ≤ 10^82*x^14*L^4*z*y^4 - apply le_trans (add_le_add_right (mul_le_mul_of_nonneg_left HV (by positivity : 0 ≤ 10^30*L^3*z)) 1) - let r : ℝ := x^14*L^4*z*y^4 - have hR : 1 ≤ r := by - unfold r - exact one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_pow₀ hx) (one_le_pow₀ hL)) hz) (one_le_pow₀ hy) - nlinarith [show r=x^14*L^4*z*y^4 from rfl] - -def hush (g : Grade) (j : ℕ) (m : ℕ) (l : Path M) := - showScore (stage g m) (vacant M) j l / scaleC m^5 - -def spillScore (g : Grade) (j : ℕ) (l : Path M) := - 1 + ∑ m∈Finset.range (M+3),hush g j m l - -lemma hush_pos (g : Grade) (j m : ℕ) (l : Path M) : - 0 < hush g j m l := div_pos (lt_of_lt_of_le (by norm_num : (0:ℝ) < 1) - (show_one _ _ _ _)) (pow_pos (scale_pos _) _) -lemma spill_share (g : Grade) (j : ℕ) (l : Path M) : - let k := chooseCap g l - let S := spillScore g j l - 1 ≤ S ∧ showScore (stage g k) (vacant M) j l ≤ S*scaleC k^5 := by - intro k S - have ha : ∀ m, 0 ≤ hush g j m l := fun _ => (hush_pos _ _ _ _).le - constructor - · exact le_add_of_nonneg_right (Finset.sum_nonneg (fun _ _ => ha _)) - · apply (div_le_iff₀ (pow_pos (scale_pos k) _)).mp - change hush g j k l ≤ _ - unfold S spillScore - have h := (choice_bounds g l).1 - have he : k∈Finset.range (M+3) := Finset.mem_range.mpr (by unfold k; omega) - exact le_trans (Finset.single_le_sum (f:=fun i => hush g j i l) (fun _ _ => ha _) he) (le_add_of_nonneg_left (by norm_num : (0:ℝ) ≤ 1)) - -lemma spill_bound : - ∀ᶠ j : ℕ in atTop, - ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - (∀ x,f x ≤ exp (height j)) → - ∀ g : Grade, entitled g s j → stats t (spillScore g j) ≤ 4*heartP s g j := by - filter_upwards [booking] with j hj M J s f t h hh he hf g hg - let x := heartP s g j - have HI : 1 ≤ x := by - unfold x heartP - exact one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (by norm_num) (one_le_pow₀ (scale_one _))) (one_le_pow₀ (amount_limits _ _).1)) (one_le_exp (cut_nonneg _ _ _)) - have HA (m) : stats t (hush g j m) ≤ x * side m := by - let B := scaleC m - have HE := hj M J s f t h hh he hf g hg m - have hp : 0 < B := scale_pos _ - calc - _ = stats t (showScore (stage g m) _ j) / B^5 := by - unfold hush stats B; simp_rw [div_eq_mul_inv,Ex_mul] - _ ≤ x * B^4 / B^5 := div_le_div_of_nonneg_right HE (pow_nonneg hp.le _) - _ = x * side m := by rw [scale_side]; unfold B; have := scale_pos m; field_simp - let R := Finset.range (M+3) - have H (k : ℕ) : ∑ i∈Finset.range k, side i ≤ 2 := by - have h₁ := geom_sum_mul_neg ((1:ℝ)/2) k - unfold side; nlinarith [pow_nonneg (show (0:ℝ) ≤ 1/2 by norm_num) k] - calc - _ = 1+∑ i∈R,stats t (hush g j i) := by - unfold stats spillScore; rw [Ex_add (f:=fun _ => 1),Ex_const,Ex_finset_sum] - _ ≤ 1+∑ i∈R,x*side i := by apply add_le_add_right; exact Finset.sum_le_sum fun i _ => HA i - _ ≤ _ := by rw [← Finset.mul_sum]; unfold R; change _ ≤ 4*x; nlinarith [H (M+3)] - -def timex (g : Grade) (j : ℕ) (l : Path M) := - let v := scaleC (chooseCap g l) - 2*(log (spillScore g j l)+5*log v) - -lemma timex_scores (g : Grade) (j : ℕ) (l : Path M) : - 0 ≤ timex g j l ∧ - 2*log (showScore (stage g (chooseCap g l)) (vacant M) j l) ≤ timex g j l := by - let v := scaleC (chooseCap g l) - have hp : 1 ≤ v := scale_one _ - obtain ⟨h₁,h₂⟩ := spill_share g j l - let a := spillScore g j l - change 1 ≤ a at h₁ - let p := stage g (chooseCap g l) - let s := showScore p (vacant M) j l - change s ≤ a*v^5 at h₂ - have Hs : 0 < s := lt_of_lt_of_le (by norm_num) (show_one _ _ _ _) - have hi := log_le_log Hs h₂ - rw [log_mul (by positivity) (by positivity), log_pow] at hi; push_cast at hi - change 0 ≤ 2*(log a+_) ∧ 2*log s ≤ 2*(log a+5*log v) - constructor - · apply mul_nonneg (by norm_num); exact add_nonneg (log_nonneg h₁) (mul_nonneg (by norm_num) (log_nonneg hp)) - linarith - -lemma stat_jensen (t : Thread M s) (f : Path M→ℝ) (hp : ∀ l, 0 < f l) - (x : ℝ) (hh : 0 < x) (h : stats t f ≤ x) : - stats t (fun l => log (f l)) ≤ log x := by - let P := s.here.dice M - have HE (l) : log (f l) ≤ log x + (x⁻¹ * f l-1) := by - have H := log_le_sub_one_of_pos (div_pos (hp l) hh) - rw [log_div (hp l).ne' hh.ne',div_eq_inv_mul] at H; linarith - have HC := P.Ex_mono (fun x => HE (t.stream x)) - rw [Ex_add _ (f:=fun _ => _),Ex_sub,Ex_const,Ex_lmul,Ex_const] at HC - change _ ≤ log x +(x⁻¹*stats t f-1) at HC - apply HC.trans; nlinarith [inv_pos.2 hh,inv_mul_cancel₀ hh.ne'] - -def dBase (g : Grade) (s : Scene M) (j : ℕ) := - 1+log (amount s j)+g.i - -lemma paid_levels : - ∀ᶠ j : ℕ in atTop, - ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - (∀ x,f x ≤ exp (height j)) → - ∀ g : Grade, entitled g s j → - stats t (timex g j) ≤ 10^7*(dBase g s j+cutBook g s j) ∧ - stats t (fun l => (chooseCap g l:ℝ)) ≤ 10^6*dBase g s j := by - filter_upwards [spill_bound,prelude] with j h₁ h₂ M J s f t h hh he hf g hg - let x := amount s j; let y := scaleC g.i; let z := cutBook g s j - let B := spillScore (M:=M) g j; let Q (l : Path M) := scaleC (chooseCap g l) - have hx : 1 ≤ x := (amount_limits _ _).1; have hy : 1 ≤ y := scale_one _ - have hz : 0 ≤ z := cut_nonneg _ _ _ - have hH := (h₂ M J s f t h hh he hf (stage g 0)).2 - change stats t (fun l => (broadMass l g:ℝ)) ≤ 10^31*y^6*x at hH - have he₁ : stats t Q ≤ 10^33*y^6*x := by - let C (l : Path M) := (broadMass l g:ℝ) - calc - _ ≤ stats t (fun l => 2*(C l+1)) := by - apply Ex_mono; intro i; exact (choice_bounds g (t.stream i)).2.2 - _ = 2*(stats t C+1) := by unfold stats; rw [Ex_lmul,Ex_add,Ex_const] - _ ≤ _ := by unfold C; nlinarith [one_le_pow₀ hy (n:=6)] - let T := dBase g s j - have hi : log y ≤ g.i := by - unfold y scaleC; rw [log_pow] - have Hx : log 2 ≤ 1 := by linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] - exact mul_le_of_le_one_right (by positivity) Hx - have HI : 1 ≤ T := by unfold T dBase; linarith [log_nonneg hx,Nat.cast_nonneg (α:=ℝ) g.i] - have hQ : stats t (fun l => log (Q l)) ≤ 10000*T := by - apply (stat_jensen t Q (fun _ => scale_pos _) _ (by positivity) he₁).trans - rw [log_mul (by positivity) (by positivity), log_mul (by positivity) (by positivity), - log_pow,log_pow]; push_cast - change _ ≤ 10000*(1+log x+_) - linarith [log_nonneg hx,Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10),Nat.cast_nonneg (α:=ℝ) g.i] - have HR : stats t (fun l => log (B l)) ≤ z+10000*T := by - have hh' := h₁ M J s f t h hh he hf g hg - change stats t B ≤ 4*(10^82*y^14*x^4*exp z) at hh' - apply (stat_jensen t B (fun l => lt_of_lt_of_le (by norm_num : (0:ℝ)<1) (spill_share g j l).1) - _ (by positivity) hh').trans - rw [log_mul (by positivity) (by positivity),log_mul (by positivity) (by positivity), - log_mul (by positivity) (by positivity),log_mul (by positivity) (by positivity), - log_pow,log_pow,log_pow,log_exp]; push_cast - change _ ≤ z+10000*(1+log x+_) - linarith [log_nonneg hx,Nat.cast_nonneg (α:=ℝ) g.i, log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10)] - constructor - · change stats t (fun l => 2*(log (B l)+5*log (Q l))) ≤ 10^7*(T+z) - unfold stats at *; rw [Ex_lmul,Ex_add,Ex_lmul]; linarith - have HB (l : Path M) : (chooseCap g l:ℝ) ≤ 2*log (Q l) := by - unfold Q scaleC; rw [log_pow] - nlinarith [log_two_gt_d9,Nat.cast_nonneg (α:=ℝ) (chooseCap g l)] - apply le_trans (b:=stats t (fun l => 2*log (Q l))) (Ex_mono _ (fun i => HB (t.stream i))) - unfold stats at *; rw [Ex_lmul]; change _ ≤ 10^6*T; linarith - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Ticket +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe + +def chooseCap (g : Grade) (l : Path M) := Nat.log 2 (broadMass l g)+1 +lemma choice_bounds (g : Grade) (l : Path M) : + let x := chooseCap g l + x ≤ M+2 ∧ broadMass l g ≤ rangeIx x ∧ scaleC x ≤ 2*((broadMass l g:ℝ)+1) := by + intro x + let n := broadMass l g + have hx : 2^x ≤ 2*(n+1) := (by + change 2^(Nat.log 2 n+1) ≤ _ + have H := Nat.pow_log_le_add_one 2 n + rw [pow_add]; omega) + have H : n ≤ 2^M := by + apply le_trans (Finset.card_le_univ _) (by rw [ZMod.card]) + refine ⟨?_,(Nat.lt_pow_succ_log_self (show 1 < 2 by norm_num) _).le,?_⟩ + · by_contra! hh + have HI : 2^(M+2) < 2^x := Nat.pow_lt_pow_right (by omega) hh + have HF : 0 < 2^M := by positivity + rw [pow_add] at HI + norm_num at HI; omega + · unfold scaleC + change (2:ℝ)^x ≤ 2*((n:ℝ)+1) + exact_mod_cast hx + +def heartP (s : Scene M) (g : Grade) (j : ℕ) := + 10^82*(scaleC g.i)^14 * (amount s j)^4 * exp (cutBook g s j) + +lemma booking : + ∀ᶠ j : ℕ in atTop, + ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + (∀ x,f x ≤ exp (height j)) → + ∀ g : Grade, entitled g s j → ∀ m : ℕ, + stats t (showScore (stage g m) (vacant M) j) ≤ heartP s g j* scaleC m^4 := by + filter_upwards [paint_birth,prelude] with j h₁ h₂ M J s f t h hh he hf g hg m + apply (h₁ M J _ _ f (stage g m) h hh hg hf _).trans + let x := scaleC g.i; let y := scaleC m; let L := amount s j; let z := exp (cutBook g s j) + have hx : 1 ≤ x := scale_one _; have hy : 1 ≤ y := scale_one _ + have hL : 1 ≤ L := (amount_limits _ _).1 + have hz : 1 ≤ z := one_le_exp (cut_nonneg _ _ _) + have HV := (h₂ M J s f t h hh he hf (stage g m)).1 + change stats t _ ≤ 10^50*x^14*L*y^4 at HV + change 1+10^30*L^3*z*_ ≤ 10^82*x^14*L^4*z*y^4 + apply le_trans (add_le_add_right (mul_le_mul_of_nonneg_left HV (by positivity : 0 ≤ 10^30*L^3*z)) 1) + let r : ℝ := x^14*L^4*z*y^4 + have hR : 1 ≤ r := by + unfold r + exact one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_pow₀ hx) (one_le_pow₀ hL)) hz) (one_le_pow₀ hy) + nlinarith [show r=x^14*L^4*z*y^4 from rfl] + +def hush (g : Grade) (j : ℕ) (m : ℕ) (l : Path M) := + showScore (stage g m) (vacant M) j l / scaleC m^5 + +def spillScore (g : Grade) (j : ℕ) (l : Path M) := + 1 + ∑ m∈Finset.range (M+3),hush g j m l + +lemma hush_pos (g : Grade) (j m : ℕ) (l : Path M) : + 0 < hush g j m l := div_pos (lt_of_lt_of_le (by norm_num : (0:ℝ) < 1) + (show_one _ _ _ _)) (pow_pos (scale_pos _) _) +lemma spill_share (g : Grade) (j : ℕ) (l : Path M) : + let k := chooseCap g l + let S := spillScore g j l + 1 ≤ S ∧ showScore (stage g k) (vacant M) j l ≤ S*scaleC k^5 := by + intro k S + have ha : ∀ m, 0 ≤ hush g j m l := fun _ => (hush_pos _ _ _ _).le + constructor + · exact le_add_of_nonneg_right (Finset.sum_nonneg (fun _ _ => ha _)) + · apply (div_le_iff₀ (pow_pos (scale_pos k) _)).mp + change hush g j k l ≤ _ + unfold S spillScore + have h := (choice_bounds g l).1 + have he : k∈Finset.range (M+3) := Finset.mem_range.mpr (by unfold k; omega) + exact le_trans (Finset.single_le_sum (f:=fun i => hush g j i l) (fun _ _ => ha _) he) (le_add_of_nonneg_left (by norm_num : (0:ℝ) ≤ 1)) + +lemma spill_bound : + ∀ᶠ j : ℕ in atTop, + ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + (∀ x,f x ≤ exp (height j)) → + ∀ g : Grade, entitled g s j → stats t (spillScore g j) ≤ 4*heartP s g j := by + filter_upwards [booking] with j hj M J s f t h hh he hf g hg + let x := heartP s g j + have HI : 1 ≤ x := by + unfold x heartP + exact one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (one_le_mul_of_one_le_of_one_le (by norm_num) (one_le_pow₀ (scale_one _))) (one_le_pow₀ (amount_limits _ _).1)) (one_le_exp (cut_nonneg _ _ _)) + have HA (m) : stats t (hush g j m) ≤ x * side m := by + let B := scaleC m + have HE := hj M J s f t h hh he hf g hg m + have hp : 0 < B := scale_pos _ + calc + _ = stats t (showScore (stage g m) _ j) / B^5 := by + unfold hush stats B; simp_rw [div_eq_mul_inv,Ex_mul] + _ ≤ x * B^4 / B^5 := div_le_div_of_nonneg_right HE (pow_nonneg hp.le _) + _ = x * side m := by rw [scale_side]; unfold B; have := scale_pos m; field_simp + let R := Finset.range (M+3) + have H (k : ℕ) : ∑ i∈Finset.range k, side i ≤ 2 := by + have h₁ := geom_sum_mul_neg ((1:ℝ)/2) k + unfold side; nlinarith [pow_nonneg (show (0:ℝ) ≤ 1/2 by norm_num) k] + calc + _ = 1+∑ i∈R,stats t (hush g j i) := by + unfold stats spillScore; rw [Ex_add (f:=fun _ => 1),Ex_const,Ex_finset_sum] + _ ≤ 1+∑ i∈R,x*side i := by apply add_le_add_right; exact Finset.sum_le_sum fun i _ => HA i + _ ≤ _ := by rw [← Finset.mul_sum]; unfold R; change _ ≤ 4*x; nlinarith [H (M+3)] + +def timex (g : Grade) (j : ℕ) (l : Path M) := + let v := scaleC (chooseCap g l) + 2*(log (spillScore g j l)+5*log v) + +lemma timex_scores (g : Grade) (j : ℕ) (l : Path M) : + 0 ≤ timex g j l ∧ + 2*log (showScore (stage g (chooseCap g l)) (vacant M) j l) ≤ timex g j l := by + let v := scaleC (chooseCap g l) + have hp : 1 ≤ v := scale_one _ + obtain ⟨h₁,h₂⟩ := spill_share g j l + let a := spillScore g j l + change 1 ≤ a at h₁ + let p := stage g (chooseCap g l) + let s := showScore p (vacant M) j l + change s ≤ a*v^5 at h₂ + have Hs : 0 < s := lt_of_lt_of_le (by norm_num) (show_one _ _ _ _) + have hi := log_le_log Hs h₂ + rw [log_mul (by positivity) (by positivity), log_pow] at hi; push_cast at hi + change 0 ≤ 2*(log a+_) ∧ 2*log s ≤ 2*(log a+5*log v) + constructor + · apply mul_nonneg (by norm_num); exact add_nonneg (log_nonneg h₁) (mul_nonneg (by norm_num) (log_nonneg hp)) + linarith + +lemma stat_jensen (t : Thread M s) (f : Path M→ℝ) (hp : ∀ l, 0 < f l) + (x : ℝ) (hh : 0 < x) (h : stats t f ≤ x) : + stats t (fun l => log (f l)) ≤ log x := by + let P := s.here.dice M + have HE (l) : log (f l) ≤ log x + (x⁻¹ * f l-1) := by + have H := log_le_sub_one_of_pos (div_pos (hp l) hh) + rw [log_div (hp l).ne' hh.ne',div_eq_inv_mul] at H; linarith + have HC := P.Ex_mono (fun x => HE (t.stream x)) + rw [Ex_add _ (f:=fun _ => _),Ex_sub,Ex_const,Ex_lmul,Ex_const] at HC + change _ ≤ log x +(x⁻¹*stats t f-1) at HC + apply HC.trans; nlinarith [inv_pos.2 hh,inv_mul_cancel₀ hh.ne'] + +def dBase (g : Grade) (s : Scene M) (j : ℕ) := + 1+log (amount s j)+g.i + +lemma paid_levels : + ∀ᶠ j : ℕ in atTop, + ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + (∀ x,f x ≤ exp (height j)) → + ∀ g : Grade, entitled g s j → + stats t (timex g j) ≤ 10^7*(dBase g s j+cutBook g s j) ∧ + stats t (fun l => (chooseCap g l:ℝ)) ≤ 10^6*dBase g s j := by + filter_upwards [spill_bound,prelude] with j h₁ h₂ M J s f t h hh he hf g hg + let x := amount s j; let y := scaleC g.i; let z := cutBook g s j + let B := spillScore (M:=M) g j; let Q (l : Path M) := scaleC (chooseCap g l) + have hx : 1 ≤ x := (amount_limits _ _).1; have hy : 1 ≤ y := scale_one _ + have hz : 0 ≤ z := cut_nonneg _ _ _ + have hH := (h₂ M J s f t h hh he hf (stage g 0)).2 + change stats t (fun l => (broadMass l g:ℝ)) ≤ 10^31*y^6*x at hH + have he₁ : stats t Q ≤ 10^33*y^6*x := by + let C (l : Path M) := (broadMass l g:ℝ) + calc + _ ≤ stats t (fun l => 2*(C l+1)) := by + apply Ex_mono; intro i; exact (choice_bounds g (t.stream i)).2.2 + _ = 2*(stats t C+1) := by unfold stats; rw [Ex_lmul,Ex_add,Ex_const] + _ ≤ _ := by unfold C; nlinarith [one_le_pow₀ hy (n:=6)] + let T := dBase g s j + have hi : log y ≤ g.i := by + unfold y scaleC; rw [log_pow] + have Hx : log 2 ≤ 1 := by linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] + exact mul_le_of_le_one_right (by positivity) Hx + have HI : 1 ≤ T := by unfold T dBase; linarith [log_nonneg hx,Nat.cast_nonneg (α:=ℝ) g.i] + have hQ : stats t (fun l => log (Q l)) ≤ 10000*T := by + apply (stat_jensen t Q (fun _ => scale_pos _) _ (by positivity) he₁).trans + rw [log_mul (by positivity) (by positivity), log_mul (by positivity) (by positivity), + log_pow,log_pow]; push_cast + change _ ≤ 10000*(1+log x+_) + linarith [log_nonneg hx,Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10),Nat.cast_nonneg (α:=ℝ) g.i] + have HR : stats t (fun l => log (B l)) ≤ z+10000*T := by + have hh' := h₁ M J s f t h hh he hf g hg + change stats t B ≤ 4*(10^82*y^14*x^4*exp z) at hh' + apply (stat_jensen t B (fun l => lt_of_lt_of_le (by norm_num : (0:ℝ)<1) (spill_share g j l).1) + _ (by positivity) hh').trans + rw [log_mul (by positivity) (by positivity),log_mul (by positivity) (by positivity), + log_mul (by positivity) (by positivity),log_mul (by positivity) (by positivity), + log_pow,log_pow,log_pow,log_exp]; push_cast + change _ ≤ z+10000*(1+log x+_) + linarith [log_nonneg hx,Nat.cast_nonneg (α:=ℝ) g.i, log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10)] + constructor + · change stats t (fun l => 2*(log (B l)+5*log (Q l))) ≤ 10^7*(T+z) + unfold stats at *; rw [Ex_lmul,Ex_add,Ex_lmul]; linarith + have HB (l : Path M) : (chooseCap g l:ℝ) ≤ 2*log (Q l) := by + unfold Q scaleC; rw [log_pow] + nlinarith [log_two_gt_d9,Nat.cast_nonneg (α:=ℝ) (chooseCap g l)] + apply le_trans (b:=stats t (fun l => 2*log (Q l))) (Ex_mono _ (fun i => HB (t.stream i))) + unfold stats at *; rw [Ex_lmul]; change _ ≤ 10^6*T; linarith + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Energies.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Energies.lean index bd3d847365e6370bc64e44d9ae440228201c759d..f783ee146f8959fb52b0fa11e830f80ea60a8d39 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Energies.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Energies.lean @@ -1,216 +1,216 @@ -import OAI.Analysis.FourierLLogL.Paths.Letters -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -def duet (x y : ℂ) : ℝ := (conj x*y).re -lemma duet_zero (x : ℂ) : duet x 0=0 := by simp [duet] -lemma duet_self (x : ℂ) : duet x x=‖x‖^2 := by - rw [duet,mul_comm,Complex.mul_conj,Complex.sq_norm]; rfl -lemma duet_add (x y z : ℂ) : duet x (y+z)=duet x y+duet x z := by simp [duet,mul_add] -lemma duet_sub (x y z : ℂ) : duet x (y-z)=duet x y-duet x z := by simp [duet,mul_sub] -lemma duet_scale (x y : ℂ) (c : ℝ) : duet x ((c:ℂ)*y)=c* duet x y := by - rw [duet,duet,mul_left_comm,Complex.re_ofReal_mul] -lemma duet_limit (x y : ℂ) : |duet x y| ≤ ‖x‖* ‖y‖ := by - apply (Complex.abs_re_le_norm _).trans - rw [norm_mul,Complex.norm_conj] -lemma duet_inc (x y : ℂ) : ‖x+y‖^2=‖x‖^2+2*duet x y+‖y‖^2 := by - rw [Complex.sq_norm,Complex.sq_norm,Complex.sq_norm,duet] - simp [Complex.normSq_apply]; ring - -def meanV (s : Scene M) (u z : Grid M) := letter s u z / (matter s z:ℂ) -def sun (s : Scene M) (u z : Grid M) := matter s z * ‖meanV s u z‖^2 -def pierce (s : Scene M) (z : Grid M) := shell s z^2 / matter s z -def nub (s : Scene M) (u z : Grid M) := - factor s u z-(shell s z:ℂ)*meanV s u z - -lemma mean_hold (s : Scene M) (h : Fit s) (u z : Grid M) : - (matter s z:ℂ)*meanV s u z=letter s u z ∧ ‖meanV s u z‖ ≤ 1 := by - have H := matter_norm h u z - rcases eq_or_lt_of_le (matter_nonneg h z) with ha|ha - · have hi : letter s u z=0 := norm_le_zero_iff.mp (by linarith) - simp [meanV,← ha,hi] - unfold meanV; constructor - · have hh : (matter s z:ℂ) ≠ 0 := by exact_mod_cast ha.ne' - field_simp - rw [norm_div,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg ha.le] - exact (div_le_one ha).mpr H - -lemma sun_rays {s : Scene M} (h : Fit s) (u z) : - 0 ≤ sun s u z ∧ sun s u z ≤ matter s z := - ⟨mul_nonneg (matter_nonneg h z) (sq_nonneg _),mul_le_of_le_one_right (matter_nonneg h _) - (pow_le_one₀ (norm_nonneg _) (mean_hold _ h _ _).2)⟩ - -lemma sun_energy {s : Scene M} (h : Fit s) (u : Grid M) : - energy s u=∑ z, sun s u z := by - unfold energy energyZ; refine Finset.sum_congr rfl fun z _ => ?_ - have he := mean_hold _ h u z - change ‖letter s u z‖^2 / matter s z=_ - rw [← he.1,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (matter_nonneg h z)] - unfold sun; by_cases h : matter s z=0 - · rw [h]; ring_nf - field_simp - -lemma energy_small {s : Scene M} (h : Fit s) (u) : - 0 ≤ energy s u ∧ energy s u ≤ share s := by - rw [sun_energy h] - constructor - · exact Finset.sum_nonneg (fun i _ => (sun_rays h _ _).1) - rw [←matter_sum h] - exact Finset.sum_le_sum fun i _ => (sun_rays h _ _).2 - -lemma pierce_nonneg {s : Scene M} (h : Fit s) (z) : 0 ≤ pierce s z := - div_nonneg (sq_nonneg _) (matter_nonneg h _) - -lemma shell_sq {s : Scene M} (h : Climb s) (z) : - shell s z^2=matter s z*pierce s z := by - by_cases he : matter s z=0 - · have hx := shell_abs h z; rw [he,abs_nonpos_iff] at hx - rw [he,hx]; ring - unfold pierce; field_simp - -def mist (u : Grid M) (s t : Scene M) (z : Grid M) := - matter t z * ‖meanV t u z-meanV s u z‖^2 -def dew (u : Grid M) (s : Scene M) (z : Grid M) := - (mist u s (s.smaller false) z+mist u s (s.smaller true) z)/2 -def shower (u : Grid M) (s : Scene M) := ∑ z, dew u s z -def gust (s : Scene M) (u : Grid M) := (energy (s.smaller false) u-energy (s.smaller true) u)/2 - -lemma mist_nonneg (u : Grid M) (s t : Scene M) (h : Fit t) (z) : 0 ≤ mist u s t z := - mul_nonneg (matter_nonneg h _) (sq_nonneg _) -lemma mist_low (u : Grid M) {s : Scene M} (h : Climb s) (b z) : - mist u s (s.smaller b) z ≤ 2*dew u s z := by - cases b <;> unfold dew <;> linarith [mist_nonneg u s _ (h.child false) z,mist_nonneg u s _ (h.child true) z] -lemma dew_nonneg (u : Grid M) {s : Scene M} (h : Climb s) (z) : 0 ≤ dew u s z := - div_nonneg (add_nonneg (mist_nonneg _ _ _ (h.child false) _) - (mist_nonneg _ _ _ (h.child true) _)) (by norm_num) -lemma shower_nonneg {u : Grid M} {s : Scene M} (h : Climb s) : 0 ≤ shower u s := - Finset.sum_nonneg fun _ _ => dew_nonneg _ h _ - -lemma mean_shift {s : Scene M} (h : Climb s) (b) (u z) : - (matter (s.smaller b) z:ℂ)*(meanV (s.smaller b) u z-meanV s u z) = - (curl b:ℂ)*nub s u z := by - rw [mul_sub,(mean_hold _ (h.child b) _ _).1,matter_jump h b, - letter_jump h,← (mean_hold _ h.fit _ _).1,nub] - push_cast; ring - - -lemma sun_shift {s : Scene M} (h : Climb s) (u b z) : - sun (s.smaller b) u z = sun s u z+curl b * - (shell s z*‖meanV s u z‖^2 + 2*duet (meanV s u z) (nub s u z))+ - mist u s (s.smaller b) z := by - let v := meanV s u z; let w := meanV (s.smaller b) u z-v - let m := matter (s.smaller b) z - have he : meanV (s.smaller b) u z=v+w := by unfold w; ring - have HH : m*duet v w=curl b*duet v (nub s u z) := by rw [←duet_scale, - show (m:ℂ)*w=_ from mean_shift h _ _ _,duet_scale] - change m*‖meanV (s.smaller b) u z‖^2=matter s z* ‖v‖^2+curl b*(shell s z*‖v‖^2+_) +m*‖w‖^2 - rw [he,duet_inc] - change m*_=matter s z*‖v‖^2+curl b* (shell s z*‖v‖^2+2*duet v (nub s u z))+_ - have he' : m=matter s z + curl b * shell s z := matter_jump h b z - nlinarith [show m*‖v‖^2=(matter s z+curl b*shell s z)*‖v‖^2 by rw [he']] - -lemma wet_average (s : Scene M) (h : Climb s) (u) : - (energy (s.smaller false) u+energy (s.smaller true) u)/2 = - energy s u+shower u s := by - rw [sun_energy (h.child false),sun_energy (h.child true),sun_energy h.fit, - ← Finset.sum_add_distrib, Finset.sum_div,shower,← Finset.sum_add_distrib] - apply Finset.sum_congr rfl - intro z _ - rw [sun_shift h,sun_shift h] - unfold dew; simp [curl] - ring - -lemma wet_walk (s : Scene M) (h : Climb s) (u b) : - energy (s.smaller b) u=energy s u+shower u s+curl b*gust s u := by - rw [←wet_average s h u,gust] - cases b <;> norm_num [curl] <;> ring - -lemma matter_max {s : Scene M} (h : Climb s) (b z) : - matter (s.smaller b) z ≤ 2*matter s z := by - have hi := matter_nonneg (h.child true) z - have hj := matter_nonneg (h.child false) z - cases b <;> linarith [matter_split h z] - -lemma norm_scaleC (x : ℝ) (v : ℂ) : - ‖(x:ℂ)*v‖=|x| * ‖v‖ := by rw [norm_mul,Complex.norm_real,Real.norm_eq_abs] - -lemma nub_innovation (s : Scene M) (h : Climb s) (u z) : - ‖nub s u z‖^2 ≤ 4*matter s z*dew u s z := by - let q := s.smaller false - let x := matter q z - have hp : 0 ≤ x := matter_nonneg (h.child false) z - have hi : ‖nub s u z‖^2=x*mist u s q z := by - have HH := congrArg (fun x : ℂ => ‖x‖) (mean_shift h false u z) - change ‖(x:ℂ)*(meanV q u z-meanV s u z)‖=‖(curl false:ℂ)*nub s u z‖ at HH - rw [norm_scaleC,norm_scaleC,curl_unit,one_mul,abs_of_nonneg hp] at HH - rw [← HH]; unfold mist x; ring - rw [hi] - have H : x ≤ 2*matter s z := matter_max h false _ - have he := mul_le_mul H (mist_low u h false z) (mist_nonneg u s _ (h.child false) z) - (hp.trans H) - change x*mist u s q z ≤ _ at he - linarith - -/-- Relating mean and energy at removals. -/ -lemma drain_changes {s q : Scene M} (h : Ideal s q) (u z) : - let k := matter s z-matter q z - mist u s q z ≤ 4*k ∧ |sun s u z-sun q u z| ≤ 8*k := by - let w := meanV s u z; let v := meanV q u z - let r := matter s z; let t := matter q z - intro k - have hw := mean_hold s h.fit u z; have hv := mean_hold q h.climb.fit u z - have H := matter_drain h u z - rw [← hw.1,← hv.1] at H - have hr : 0 ≤ r := matter_nonneg h.fit z - have ht : 0 ≤ t := matter_nonneg h.climb.fit z - change ‖(r:ℂ)*w-(t:ℂ)*v‖ ≤ k at H - have hk := (norm_nonneg _).trans H - have hh : k=r-t := rfl - have H1 : t* ‖w-v‖ ≤ 2*k := by - rw [show t* ‖w-v‖=‖(t:ℂ)*(w-v)‖ by rw [norm_scaleC,abs_of_nonneg ht]] - rw [show (t:ℂ)*(w-v)=(r*w-t*v)-(k:ℂ)*w by rw [hh]; push_cast; ring] - apply le_trans (norm_sub_le _ _) - rw [norm_scaleC,abs_of_nonneg hk] - have HH : ‖w‖ ≤ 1 := hw.2 - nlinarith - have Hw : ‖w‖ ≤ 1 := hw.2 - have Hv : ‖v‖ ≤ 1 := hv.2 - have HE : ‖w-v‖ ≤ 2 := (norm_sub_le _ _).trans (by linarith) - constructor - · change t* ‖v-w‖^2 ≤ _ - rw [norm_sub_rev] - nlinarith [mul_le_mul_of_nonneg_left HE ht,norm_nonneg (w-v), - mul_le_mul_of_nonneg_left HE (mul_nonneg ht (norm_nonneg (w-v)))] - · change |r* ‖w‖^2-t* ‖v‖^2| ≤ _ - rw [show r* ‖w‖^2-t* ‖v‖^2=k*‖w‖^2+t*(‖w‖-‖v‖)*(‖w‖+‖v‖) by rw [hh]; ring] - apply le_trans (abs_add_le _ _) - have ht' : |‖w‖-‖v‖| ≤ ‖w-v‖ := abs_norm_sub_norm_le w v - rw [abs_of_nonneg (mul_nonneg hk (sq_nonneg _)), abs_mul,abs_mul, - abs_of_nonneg ht,abs_of_nonneg (add_nonneg (norm_nonneg w) (norm_nonneg v))] - have HE' : t*|‖w‖-‖v‖| ≤ 2*k := (mul_le_mul_of_nonneg_left ht' ht).trans H1 - have hW' : k*‖w‖^2 ≤ k := mul_le_of_le_one_right hk - (pow_le_one₀ (norm_nonneg _) Hw) - nlinarith [norm_nonneg v,norm_nonneg w] - -lemma energy_drain {b : Beat M} (h : Ideal b.pre b.post) (u) : - |energy b.pre u-energy b.post u| ≤ 8*killed b := by - rw [sun_energy h.fit,sun_energy h.climb.fit,h.kill_eq,←matter_sum h.fit,←matter_sum h.climb.fit, - ← Finset.sum_sub_distrib,← Finset.sum_sub_distrib,Finset.mul_sum] - apply (Finset.abs_sum_le_sum_abs _ _).trans - exact Finset.sum_le_sum fun i _ => (drain_changes h u i).2 - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Letters +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +def duet (x y : ℂ) : ℝ := (conj x*y).re +lemma duet_zero (x : ℂ) : duet x 0=0 := by simp [duet] +lemma duet_self (x : ℂ) : duet x x=‖x‖^2 := by + rw [duet,mul_comm,Complex.mul_conj,Complex.sq_norm]; rfl +lemma duet_add (x y z : ℂ) : duet x (y+z)=duet x y+duet x z := by simp [duet,mul_add] +lemma duet_sub (x y z : ℂ) : duet x (y-z)=duet x y-duet x z := by simp [duet,mul_sub] +lemma duet_scale (x y : ℂ) (c : ℝ) : duet x ((c:ℂ)*y)=c* duet x y := by + rw [duet,duet,mul_left_comm,Complex.re_ofReal_mul] +lemma duet_limit (x y : ℂ) : |duet x y| ≤ ‖x‖* ‖y‖ := by + apply (Complex.abs_re_le_norm _).trans + rw [norm_mul,Complex.norm_conj] +lemma duet_inc (x y : ℂ) : ‖x+y‖^2=‖x‖^2+2*duet x y+‖y‖^2 := by + rw [Complex.sq_norm,Complex.sq_norm,Complex.sq_norm,duet] + simp [Complex.normSq_apply]; ring + +def meanV (s : Scene M) (u z : Grid M) := letter s u z / (matter s z:ℂ) +def sun (s : Scene M) (u z : Grid M) := matter s z * ‖meanV s u z‖^2 +def pierce (s : Scene M) (z : Grid M) := shell s z^2 / matter s z +def nub (s : Scene M) (u z : Grid M) := + factor s u z-(shell s z:ℂ)*meanV s u z + +lemma mean_hold (s : Scene M) (h : Fit s) (u z : Grid M) : + (matter s z:ℂ)*meanV s u z=letter s u z ∧ ‖meanV s u z‖ ≤ 1 := by + have H := matter_norm h u z + rcases eq_or_lt_of_le (matter_nonneg h z) with ha|ha + · have hi : letter s u z=0 := norm_le_zero_iff.mp (by linarith) + simp [meanV,← ha,hi] + unfold meanV; constructor + · have hh : (matter s z:ℂ) ≠ 0 := by exact_mod_cast ha.ne' + field_simp + rw [norm_div,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg ha.le] + exact (div_le_one ha).mpr H + +lemma sun_rays {s : Scene M} (h : Fit s) (u z) : + 0 ≤ sun s u z ∧ sun s u z ≤ matter s z := + ⟨mul_nonneg (matter_nonneg h z) (sq_nonneg _),mul_le_of_le_one_right (matter_nonneg h _) + (pow_le_one₀ (norm_nonneg _) (mean_hold _ h _ _).2)⟩ + +lemma sun_energy {s : Scene M} (h : Fit s) (u : Grid M) : + energy s u=∑ z, sun s u z := by + unfold energy energyZ; refine Finset.sum_congr rfl fun z _ => ?_ + have he := mean_hold _ h u z + change ‖letter s u z‖^2 / matter s z=_ + rw [← he.1,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (matter_nonneg h z)] + unfold sun; by_cases h : matter s z=0 + · rw [h]; ring_nf + field_simp + +lemma energy_small {s : Scene M} (h : Fit s) (u) : + 0 ≤ energy s u ∧ energy s u ≤ share s := by + rw [sun_energy h] + constructor + · exact Finset.sum_nonneg (fun i _ => (sun_rays h _ _).1) + rw [←matter_sum h] + exact Finset.sum_le_sum fun i _ => (sun_rays h _ _).2 + +lemma pierce_nonneg {s : Scene M} (h : Fit s) (z) : 0 ≤ pierce s z := + div_nonneg (sq_nonneg _) (matter_nonneg h _) + +lemma shell_sq {s : Scene M} (h : Climb s) (z) : + shell s z^2=matter s z*pierce s z := by + by_cases he : matter s z=0 + · have hx := shell_abs h z; rw [he,abs_nonpos_iff] at hx + rw [he,hx]; ring + unfold pierce; field_simp + +def mist (u : Grid M) (s t : Scene M) (z : Grid M) := + matter t z * ‖meanV t u z-meanV s u z‖^2 +def dew (u : Grid M) (s : Scene M) (z : Grid M) := + (mist u s (s.smaller false) z+mist u s (s.smaller true) z)/2 +def shower (u : Grid M) (s : Scene M) := ∑ z, dew u s z +def gust (s : Scene M) (u : Grid M) := (energy (s.smaller false) u-energy (s.smaller true) u)/2 + +lemma mist_nonneg (u : Grid M) (s t : Scene M) (h : Fit t) (z) : 0 ≤ mist u s t z := + mul_nonneg (matter_nonneg h _) (sq_nonneg _) +lemma mist_low (u : Grid M) {s : Scene M} (h : Climb s) (b z) : + mist u s (s.smaller b) z ≤ 2*dew u s z := by + cases b <;> unfold dew <;> linarith [mist_nonneg u s _ (h.child false) z,mist_nonneg u s _ (h.child true) z] +lemma dew_nonneg (u : Grid M) {s : Scene M} (h : Climb s) (z) : 0 ≤ dew u s z := + div_nonneg (add_nonneg (mist_nonneg _ _ _ (h.child false) _) + (mist_nonneg _ _ _ (h.child true) _)) (by norm_num) +lemma shower_nonneg {u : Grid M} {s : Scene M} (h : Climb s) : 0 ≤ shower u s := + Finset.sum_nonneg fun _ _ => dew_nonneg _ h _ + +lemma mean_shift {s : Scene M} (h : Climb s) (b) (u z) : + (matter (s.smaller b) z:ℂ)*(meanV (s.smaller b) u z-meanV s u z) = + (curl b:ℂ)*nub s u z := by + rw [mul_sub,(mean_hold _ (h.child b) _ _).1,matter_jump h b, + letter_jump h,← (mean_hold _ h.fit _ _).1,nub] + push_cast; ring + + +lemma sun_shift {s : Scene M} (h : Climb s) (u b z) : + sun (s.smaller b) u z = sun s u z+curl b * + (shell s z*‖meanV s u z‖^2 + 2*duet (meanV s u z) (nub s u z))+ + mist u s (s.smaller b) z := by + let v := meanV s u z; let w := meanV (s.smaller b) u z-v + let m := matter (s.smaller b) z + have he : meanV (s.smaller b) u z=v+w := by unfold w; ring + have HH : m*duet v w=curl b*duet v (nub s u z) := by rw [←duet_scale, + show (m:ℂ)*w=_ from mean_shift h _ _ _,duet_scale] + change m*‖meanV (s.smaller b) u z‖^2=matter s z* ‖v‖^2+curl b*(shell s z*‖v‖^2+_) +m*‖w‖^2 + rw [he,duet_inc] + change m*_=matter s z*‖v‖^2+curl b* (shell s z*‖v‖^2+2*duet v (nub s u z))+_ + have he' : m=matter s z + curl b * shell s z := matter_jump h b z + nlinarith [show m*‖v‖^2=(matter s z+curl b*shell s z)*‖v‖^2 by rw [he']] + +lemma wet_average (s : Scene M) (h : Climb s) (u) : + (energy (s.smaller false) u+energy (s.smaller true) u)/2 = + energy s u+shower u s := by + rw [sun_energy (h.child false),sun_energy (h.child true),sun_energy h.fit, + ← Finset.sum_add_distrib, Finset.sum_div,shower,← Finset.sum_add_distrib] + apply Finset.sum_congr rfl + intro z _ + rw [sun_shift h,sun_shift h] + unfold dew; simp [curl] + ring + +lemma wet_walk (s : Scene M) (h : Climb s) (u b) : + energy (s.smaller b) u=energy s u+shower u s+curl b*gust s u := by + rw [←wet_average s h u,gust] + cases b <;> norm_num [curl] <;> ring + +lemma matter_max {s : Scene M} (h : Climb s) (b z) : + matter (s.smaller b) z ≤ 2*matter s z := by + have hi := matter_nonneg (h.child true) z + have hj := matter_nonneg (h.child false) z + cases b <;> linarith [matter_split h z] + +lemma norm_scaleC (x : ℝ) (v : ℂ) : + ‖(x:ℂ)*v‖=|x| * ‖v‖ := by rw [norm_mul,Complex.norm_real,Real.norm_eq_abs] + +lemma nub_innovation (s : Scene M) (h : Climb s) (u z) : + ‖nub s u z‖^2 ≤ 4*matter s z*dew u s z := by + let q := s.smaller false + let x := matter q z + have hp : 0 ≤ x := matter_nonneg (h.child false) z + have hi : ‖nub s u z‖^2=x*mist u s q z := by + have HH := congrArg (fun x : ℂ => ‖x‖) (mean_shift h false u z) + change ‖(x:ℂ)*(meanV q u z-meanV s u z)‖=‖(curl false:ℂ)*nub s u z‖ at HH + rw [norm_scaleC,norm_scaleC,curl_unit,one_mul,abs_of_nonneg hp] at HH + rw [← HH]; unfold mist x; ring + rw [hi] + have H : x ≤ 2*matter s z := matter_max h false _ + have he := mul_le_mul H (mist_low u h false z) (mist_nonneg u s _ (h.child false) z) + (hp.trans H) + change x*mist u s q z ≤ _ at he + linarith + +/-- Relating mean and energy at removals. -/ +lemma drain_changes {s q : Scene M} (h : Ideal s q) (u z) : + let k := matter s z-matter q z + mist u s q z ≤ 4*k ∧ |sun s u z-sun q u z| ≤ 8*k := by + let w := meanV s u z; let v := meanV q u z + let r := matter s z; let t := matter q z + intro k + have hw := mean_hold s h.fit u z; have hv := mean_hold q h.climb.fit u z + have H := matter_drain h u z + rw [← hw.1,← hv.1] at H + have hr : 0 ≤ r := matter_nonneg h.fit z + have ht : 0 ≤ t := matter_nonneg h.climb.fit z + change ‖(r:ℂ)*w-(t:ℂ)*v‖ ≤ k at H + have hk := (norm_nonneg _).trans H + have hh : k=r-t := rfl + have H1 : t* ‖w-v‖ ≤ 2*k := by + rw [show t* ‖w-v‖=‖(t:ℂ)*(w-v)‖ by rw [norm_scaleC,abs_of_nonneg ht]] + rw [show (t:ℂ)*(w-v)=(r*w-t*v)-(k:ℂ)*w by rw [hh]; push_cast; ring] + apply le_trans (norm_sub_le _ _) + rw [norm_scaleC,abs_of_nonneg hk] + have HH : ‖w‖ ≤ 1 := hw.2 + nlinarith + have Hw : ‖w‖ ≤ 1 := hw.2 + have Hv : ‖v‖ ≤ 1 := hv.2 + have HE : ‖w-v‖ ≤ 2 := (norm_sub_le _ _).trans (by linarith) + constructor + · change t* ‖v-w‖^2 ≤ _ + rw [norm_sub_rev] + nlinarith [mul_le_mul_of_nonneg_left HE ht,norm_nonneg (w-v), + mul_le_mul_of_nonneg_left HE (mul_nonneg ht (norm_nonneg (w-v)))] + · change |r* ‖w‖^2-t* ‖v‖^2| ≤ _ + rw [show r* ‖w‖^2-t* ‖v‖^2=k*‖w‖^2+t*(‖w‖-‖v‖)*(‖w‖+‖v‖) by rw [hh]; ring] + apply le_trans (abs_add_le _ _) + have ht' : |‖w‖-‖v‖| ≤ ‖w-v‖ := abs_norm_sub_norm_le w v + rw [abs_of_nonneg (mul_nonneg hk (sq_nonneg _)), abs_mul,abs_mul, + abs_of_nonneg ht,abs_of_nonneg (add_nonneg (norm_nonneg w) (norm_nonneg v))] + have HE' : t*|‖w‖-‖v‖| ≤ 2*k := (mul_le_mul_of_nonneg_left ht' ht).trans H1 + have hW' : k*‖w‖^2 ≤ k := mul_le_of_le_one_right hk + (pow_le_one₀ (norm_nonneg _) Hw) + nlinarith [norm_nonneg v,norm_nonneg w] + +lemma energy_drain {b : Beat M} (h : Ideal b.pre b.post) (u) : + |energy b.pre u-energy b.post u| ≤ 8*killed b := by + rw [sun_energy h.fit,sun_energy h.climb.fit,h.kill_eq,←matter_sum h.fit,←matter_sum h.climb.fit, + ← Finset.sum_sub_distrib,← Finset.sum_sub_distrib,Finset.mul_sum] + apply (Finset.abs_sum_le_sum_abs _ _).trans + exact Finset.sum_le_sum fun i _ => (drain_changes h u i).2 + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Flash.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Flash.lean index 73e16bc457bbb743f4dcebd584116243a0980d60..b95868581124b41201e21df2e80039da4bf9cfba 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Flash.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Flash.lean @@ -1,201 +1,201 @@ -import OAI.Analysis.FourierLLogL.Paths.Bridge -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Beam - -/-- Camera anchored at a jumping post. -/ -structure Lamp (M : ℕ) where - u : Grid M - width : ℝ - W : Paths.Vector M - site : Scene M - -namespace Lamp -def glint (e : Lamp M) (z : Grid M) := - glow (e.W z) (letter e.site e.u z) (e.width*matter e.site z) -def first (e : Lamp M) (z : Grid M) := - e.glint z 0 (factor e.site e.u z) (e.width*shell e.site z) -def second (e : Lamp M) (r : Bool) (q : Scene M) (z : Grid M) := - e.glint z ((curl r:ℂ)*factor e.site e.u z) (factor q e.u z) (e.width*shell q z) -def start (e : Lamp M) := ∑ z, first e z -def delayed (e : Lamp M) (r : Bool) (q : Scene M) := ∑ z, second e r q z - -/-- update of accumulated same-parity output. -/ -def carry (e : Lamp M) (r t : Bool) (z : Grid M) := - e.W z + (curl t:ℂ)*((curl r:ℂ)*factor e.site e.u z) - -lemma flash_cell (e : Lamp M) (h : Climb e.site) (r t : Bool) (q : Scene M) - (hq : Ideal (e.site.smaller r) q) (ha : 0 < e.width) (he : e.width ≤ 1) - (z : Grid M) : - let u := e.u - let a := e.width - let s := e.site - let k := matter (s.smaller r) z-matter q z - let C := 20*(pierce s z+pierce q z) + 400*((k+dew u s z+dew u q z)/a) - prism (q.smaller t) u (e.carry r t) a z ≤ - prism s u e.W a z + curl r * e.first z + curl t * e.second r q z + 2*k+4*C := by - intro u a s k C - let w := s.smaller r; let y := q.smaller t - let R := prism s u e.W a z - let f (s) := letter s u z; let m (s) := matter s z - let P (s) := factor s u z; let D (s : Scene M) := a*shell s z - let X := curl r; let Y := curl t; let A := e.W z - let F (x : Scene M) := a*m x - let kill : ℂ := f q-f w; let lost : ℝ := F q-F w - let g₀ := (↑X:ℂ)*P s; let g₁ := (↑Y:ℂ)*P q - let gain := (↑Y:ℂ)*g₀; let plusf := g₀+kill+g₁ - let plusm := X*D s + lost + Y*D q - have hk : ‖kill‖ ≤ k := by unfold kill; rw [norm_sub_rev]; exact matter_drain hq u z - have hl : lost= -(a*k) := by unfold lost k F w m; ring - have hp := (norm_nonneg kill).trans hk - have hm : |lost|=a*k := by rw [hl,abs_neg,abs_of_nonneg (mul_nonneg ha.le hp)] - have hs : |lost| ≤ k := by rw [hm]; nlinarith - have hA : f y=f s+plusf := by - have h1 : f w=f s+g₀ := letter_jump h r u z - have h2 : f y=f q+g₁ := letter_jump hq.climb t u z - rw [h2]; unfold plusf kill; rw [h1]; ring - have hB : F y=F s+plusm := by - have h1 : m w=m s+X*shell s z := matter_jump h r z - have h2 : m y=m q+Y*shell q z := matter_jump hq.climb t z - unfold F plusm lost D; unfold F; rw [h1,h2]; ring - have h0 : 0 ≤ R := ray_pos _ _ _ - have hC : 0 ≤ C := by - have h1 := pierce_nonneg h.fit z - have h2 := pierce_nonneg hq.climb.fit z - have h3 : 0 ≤ k+dew u s z+dew u q z := add_nonneg - (add_nonneg hp (dew_nonneg u h z)) (dew_nonneg u hq.climb z) - exact add_nonneg (mul_nonneg (by norm_num) (add_nonneg h1 h2)) - (mul_nonneg (by norm_num) (div_nonneg h3 ha.le)) - have hx : |X|=1 := curl_unit _; have hy : |Y|=1 := curl_unit _ - have HG : content gain plusf plusm ≤ R*(4*C) := by - have HH := contest g₀ kill g₁ (X*D s) lost (Y*D q) - have Heq : ‖g₀‖=‖P s‖ := by unfold g₀; rw [norm_scaleC,hx,one_mul] - have Hf : ‖gain‖=‖g₀‖ := by unfold gain; rw [norm_scaleC,hy,one_mul] - have Hf' : ‖g₁‖=‖P q‖ := by unfold g₁; rw [norm_scaleC,hy,one_mul] - have HV : ‖P s‖^2+‖P q‖^2+D s^2+D q^2+ - ‖kill‖^2+(a*k)^2 ≤ R*C := by - have H := extra_joints h u z r hq a ha he e.W - unfold kill; rw [norm_sub_rev]; exact H - rw [show content gain plusf plusm=‖gain‖^2+(‖plusf‖^2+plusm^2) by unfold content; ring,Hf,Heq] - change ‖plusf‖^2 + plusm^2 ≤ _ at HH - rw [Heq,Hf',mul_pow X (D s),mul_pow Y (D q),show X^2=1 from curl_sq _,show Y^2=1 from curl_sq _,hl] at HH - nlinarith [sq_nonneg (D s),sq_nonneg (D q),sq_nonneg ‖P q‖,sq_nonneg ‖kill‖,sq_nonneg (a*k)] - let glow' := glow A (f s) (F s) - have HL : glow' gain plusf plusm = - X*e.first z+glow' 0 kill lost+Y*e.second r q z := by - unfold gain plusf plusm g₀ g₁ first second glint X Y glow' - let F' := factor e.site e.u z - let a' := curl r; let k' := curl t - - let L := e.glint z - change L ((k':ℂ)*((a':ℂ)*F')) ((a':ℂ)*F'+kill+(k':ℂ)*P q) - (a'*D s+lost+k'*D q)=a'*L 0 F' (D s)+L 0 kill lost+k'*L ((a':ℂ)*F') (P q) (D q) - have h1 (v w p i : ℂ) (u y : ℝ) : - L (v+p) (w+i) (u+y)=L v w u+L p i y := glow_add _ _ _ _ _ _ _ _ _ - have h2 (v : ℝ) (y : ℝ) (q w : ℂ) : - L ((v:ℂ)*q) ((v:ℂ)*w) (v*y)=v*L q w y := glow_scale _ _ _ _ _ _ _ - rw [← h2,← h2,← h1,← h1] - congr 1; ring - have Hpill : glow' 0 kill lost ≤ 2*k := by - apply le_trans (le_abs_self _); apply le_trans (glow_limit _ _ _ _ _ _) - rw [norm_zero]; linarith - change ray (A+gain) (f y) (F y) ≤ ray A (f s) (F s)+X*e.first z+Y*e.second r q z+2*k+4*C - rw [hA,hB] - apply (ray_shift A (f s) gain plusf (F s) plusm _ (by positivity) HG).trans - change ray A _ _+glow' gain plusf plusm+_ ≤ _ - rw [HL]; linarith - -lemma flash (e : Lamp M) (h : Climb e.site) (r : Bool) (b : Beat M) - (hb : b.pre=e.site.smaller r) (hq : Ideal b.pre b.post) - (ha : 0 < e.width) (he : e.width ≤ 1) : - let s := e.site - let u := e.u - let a := e.width - let q := b.post - let t := b.bit - let k := killed b - let C := 20*(fax s+fax q)+400*((k+shower u s+shower u q)/a) - watt (winds b) u (carry e r t) a ≤ watt s u e.W a + curl r *start e + - curl t *delayed e r q + 2*k + 4*C := by - intro s u a q t k C - let K z := matter (s.smaller r) z - matter q z - have hK : k=∑ z,K z := by - unfold K k; rw [hq.kill_eq]; rw [← matter_sum hq.fit, ← matter_sum hq.climb.fit, - Finset.sum_sub_distrib] - rw [hb] - let B z := 20*(pierce s z+pierce q z)+400*((K z+dew u s z+dew u q z)/a) - have hC : C=∑ z,B z := by - unfold C B fax shower - rw [hK] - rw [Finset.sum_add_distrib,←Finset.mul_sum,←Finset.mul_sum,← Finset.sum_div]; simp_rw [Finset.sum_add_distrib] - rfl - rw [hK,hC,watt,watt,start,delayed, Finset.mul_sum, Finset.mul_sum, Finset.mul_sum,Finset.mul_sum, - ← Finset.sum_add_distrib, ← Finset.sum_add_distrib,← Finset.sum_add_distrib,← Finset.sum_add_distrib] - apply Finset.sum_le_sum; intro i _ - exact flash_cell e h r t q (hb ▸ hq) ha he i - -def lit (e : Lamp M) (s : Scene M) := (energy s e.u + e.width^2)*fax s + shower e.u s - -lemma start_cost (e : Lamp M) (h : Climb e.site) (ha : 0 ≤ e.width) : - start e^2 ≤ 100*lit e e.site := by - let a := e.width; let W := e.W; let s := e.site; let u := e.u - let x := ∑ z,‖factor s u z‖; let y := ∑ z, |shell s z| - have H : |start e| ≤ x + a*y := by - apply le_trans (Finset.abs_sum_le_sum_abs _ _) - unfold x y; rw [Finset.mul_sum,← Finset.sum_add_distrib] - apply Finset.sum_le_sum; intro i _ - apply (glow_limit _ _ _ _ _ _).trans - rw [norm_zero,zero_add,abs_mul,abs_of_nonneg ha] - have hX : x^2 ≤ _ := factor_bill _ h u - have hY : y^2 ≤ _ := shell_bill _ h - have hT : (start e)^2 ≤ (x+a*y)^2 := by - rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) H _ - have hV : (a*y)^2 ≤ a^2*(2*fax s) := by rw [mul_pow]; gcongr - change _ ≤ 100*((energy s u+a^2)*fax s+shower u s) - nlinarith [mul_nonneg (energy_small h.fit u).1 (fax_floor h).1,shower_nonneg (u:=u) h,sq_nonneg (x-a*y), - mul_nonneg (sq_nonneg a) (fax_floor h).1] - -lemma delayed_cost (e : Lamp M) (h : Climb e.site) {q : Scene M} (hq : Climb q) - (ha : 0 ≤ e.width) (r : Bool) : - delayed e r q^2 ≤ 100*(lit e e.site+lit e q) := by - let a := e.width; let W := e.W; let s := e.site; let u := e.u - let x (s : Scene M) := ∑ z,‖factor s u z‖ - let y := ∑ z, |shell q z| - have H : |delayed e r q| ≤ x s+x q + a*y := by - apply le_trans (Finset.abs_sum_le_sum_abs _ _) - unfold x y; rw [Finset.mul_sum,← Finset.sum_add_distrib,← Finset.sum_add_distrib] - apply Finset.sum_le_sum; intro i _ - apply (glow_limit _ _ _ _ _ _).trans - rw [norm_scaleC,curl_unit,one_mul,abs_mul,abs_of_nonneg ha] - have hX₁ : x s^2 ≤ _ := factor_bill _ h u - have hX₂ : x q^2 ≤ _ := factor_bill _ hq u - have hY : y^2 ≤ _ := shell_bill _ hq - have hT : delayed e r q^2 ≤ (x s+x q+a*y)^2 := by - rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) H _ - have hV : (a*y)^2 ≤ a^2*(2*fax q) := by rw [mul_pow]; gcongr - let A (s : Scene M) := (energy s u+a^2)*fax s+shower u s - have HI (w : Scene M) (hw : Climb w) : 0 ≤ energy w u*fax w ∧ 0 ≤ a^2*fax w := - ⟨mul_nonneg (energy_small hw.fit u).1 (fax_floor hw).1,mul_nonneg (sq_nonneg a) (fax_floor hw).1⟩ - have h1 := HI s h; have h2 := HI q hq - change _ ≤ 100*((energy s u+a^2)*fax s+shower u s+((energy q u+a^2)*fax q+shower u q)) - nlinarith [shower_nonneg (u:=u) h, shower_nonneg (u:=u) hq,sq_nonneg (x s-x q),sq_nonneg (x q-a*y),sq_nonneg (x s-a*y)] - -end Lamp -end Beam - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Bridge +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Beam + +/-- Camera anchored at a jumping post. -/ +structure Lamp (M : ℕ) where + u : Grid M + width : ℝ + W : Paths.Vector M + site : Scene M + +namespace Lamp +def glint (e : Lamp M) (z : Grid M) := + glow (e.W z) (letter e.site e.u z) (e.width*matter e.site z) +def first (e : Lamp M) (z : Grid M) := + e.glint z 0 (factor e.site e.u z) (e.width*shell e.site z) +def second (e : Lamp M) (r : Bool) (q : Scene M) (z : Grid M) := + e.glint z ((curl r:ℂ)*factor e.site e.u z) (factor q e.u z) (e.width*shell q z) +def start (e : Lamp M) := ∑ z, first e z +def delayed (e : Lamp M) (r : Bool) (q : Scene M) := ∑ z, second e r q z + +/-- update of accumulated same-parity output. -/ +def carry (e : Lamp M) (r t : Bool) (z : Grid M) := + e.W z + (curl t:ℂ)*((curl r:ℂ)*factor e.site e.u z) + +lemma flash_cell (e : Lamp M) (h : Climb e.site) (r t : Bool) (q : Scene M) + (hq : Ideal (e.site.smaller r) q) (ha : 0 < e.width) (he : e.width ≤ 1) + (z : Grid M) : + let u := e.u + let a := e.width + let s := e.site + let k := matter (s.smaller r) z-matter q z + let C := 20*(pierce s z+pierce q z) + 400*((k+dew u s z+dew u q z)/a) + prism (q.smaller t) u (e.carry r t) a z ≤ + prism s u e.W a z + curl r * e.first z + curl t * e.second r q z + 2*k+4*C := by + intro u a s k C + let w := s.smaller r; let y := q.smaller t + let R := prism s u e.W a z + let f (s) := letter s u z; let m (s) := matter s z + let P (s) := factor s u z; let D (s : Scene M) := a*shell s z + let X := curl r; let Y := curl t; let A := e.W z + let F (x : Scene M) := a*m x + let kill : ℂ := f q-f w; let lost : ℝ := F q-F w + let g₀ := (↑X:ℂ)*P s; let g₁ := (↑Y:ℂ)*P q + let gain := (↑Y:ℂ)*g₀; let plusf := g₀+kill+g₁ + let plusm := X*D s + lost + Y*D q + have hk : ‖kill‖ ≤ k := by unfold kill; rw [norm_sub_rev]; exact matter_drain hq u z + have hl : lost= -(a*k) := by unfold lost k F w m; ring + have hp := (norm_nonneg kill).trans hk + have hm : |lost|=a*k := by rw [hl,abs_neg,abs_of_nonneg (mul_nonneg ha.le hp)] + have hs : |lost| ≤ k := by rw [hm]; nlinarith + have hA : f y=f s+plusf := by + have h1 : f w=f s+g₀ := letter_jump h r u z + have h2 : f y=f q+g₁ := letter_jump hq.climb t u z + rw [h2]; unfold plusf kill; rw [h1]; ring + have hB : F y=F s+plusm := by + have h1 : m w=m s+X*shell s z := matter_jump h r z + have h2 : m y=m q+Y*shell q z := matter_jump hq.climb t z + unfold F plusm lost D; unfold F; rw [h1,h2]; ring + have h0 : 0 ≤ R := ray_pos _ _ _ + have hC : 0 ≤ C := by + have h1 := pierce_nonneg h.fit z + have h2 := pierce_nonneg hq.climb.fit z + have h3 : 0 ≤ k+dew u s z+dew u q z := add_nonneg + (add_nonneg hp (dew_nonneg u h z)) (dew_nonneg u hq.climb z) + exact add_nonneg (mul_nonneg (by norm_num) (add_nonneg h1 h2)) + (mul_nonneg (by norm_num) (div_nonneg h3 ha.le)) + have hx : |X|=1 := curl_unit _; have hy : |Y|=1 := curl_unit _ + have HG : content gain plusf plusm ≤ R*(4*C) := by + have HH := contest g₀ kill g₁ (X*D s) lost (Y*D q) + have Heq : ‖g₀‖=‖P s‖ := by unfold g₀; rw [norm_scaleC,hx,one_mul] + have Hf : ‖gain‖=‖g₀‖ := by unfold gain; rw [norm_scaleC,hy,one_mul] + have Hf' : ‖g₁‖=‖P q‖ := by unfold g₁; rw [norm_scaleC,hy,one_mul] + have HV : ‖P s‖^2+‖P q‖^2+D s^2+D q^2+ + ‖kill‖^2+(a*k)^2 ≤ R*C := by + have H := extra_joints h u z r hq a ha he e.W + unfold kill; rw [norm_sub_rev]; exact H + rw [show content gain plusf plusm=‖gain‖^2+(‖plusf‖^2+plusm^2) by unfold content; ring,Hf,Heq] + change ‖plusf‖^2 + plusm^2 ≤ _ at HH + rw [Heq,Hf',mul_pow X (D s),mul_pow Y (D q),show X^2=1 from curl_sq _,show Y^2=1 from curl_sq _,hl] at HH + nlinarith [sq_nonneg (D s),sq_nonneg (D q),sq_nonneg ‖P q‖,sq_nonneg ‖kill‖,sq_nonneg (a*k)] + let glow' := glow A (f s) (F s) + have HL : glow' gain plusf plusm = + X*e.first z+glow' 0 kill lost+Y*e.second r q z := by + unfold gain plusf plusm g₀ g₁ first second glint X Y glow' + let F' := factor e.site e.u z + let a' := curl r; let k' := curl t + + let L := e.glint z + change L ((k':ℂ)*((a':ℂ)*F')) ((a':ℂ)*F'+kill+(k':ℂ)*P q) + (a'*D s+lost+k'*D q)=a'*L 0 F' (D s)+L 0 kill lost+k'*L ((a':ℂ)*F') (P q) (D q) + have h1 (v w p i : ℂ) (u y : ℝ) : + L (v+p) (w+i) (u+y)=L v w u+L p i y := glow_add _ _ _ _ _ _ _ _ _ + have h2 (v : ℝ) (y : ℝ) (q w : ℂ) : + L ((v:ℂ)*q) ((v:ℂ)*w) (v*y)=v*L q w y := glow_scale _ _ _ _ _ _ _ + rw [← h2,← h2,← h1,← h1] + congr 1; ring + have Hpill : glow' 0 kill lost ≤ 2*k := by + apply le_trans (le_abs_self _); apply le_trans (glow_limit _ _ _ _ _ _) + rw [norm_zero]; linarith + change ray (A+gain) (f y) (F y) ≤ ray A (f s) (F s)+X*e.first z+Y*e.second r q z+2*k+4*C + rw [hA,hB] + apply (ray_shift A (f s) gain plusf (F s) plusm _ (by positivity) HG).trans + change ray A _ _+glow' gain plusf plusm+_ ≤ _ + rw [HL]; linarith + +lemma flash (e : Lamp M) (h : Climb e.site) (r : Bool) (b : Beat M) + (hb : b.pre=e.site.smaller r) (hq : Ideal b.pre b.post) + (ha : 0 < e.width) (he : e.width ≤ 1) : + let s := e.site + let u := e.u + let a := e.width + let q := b.post + let t := b.bit + let k := killed b + let C := 20*(fax s+fax q)+400*((k+shower u s+shower u q)/a) + watt (winds b) u (carry e r t) a ≤ watt s u e.W a + curl r *start e + + curl t *delayed e r q + 2*k + 4*C := by + intro s u a q t k C + let K z := matter (s.smaller r) z - matter q z + have hK : k=∑ z,K z := by + unfold K k; rw [hq.kill_eq]; rw [← matter_sum hq.fit, ← matter_sum hq.climb.fit, + Finset.sum_sub_distrib] + rw [hb] + let B z := 20*(pierce s z+pierce q z)+400*((K z+dew u s z+dew u q z)/a) + have hC : C=∑ z,B z := by + unfold C B fax shower + rw [hK] + rw [Finset.sum_add_distrib,←Finset.mul_sum,←Finset.mul_sum,← Finset.sum_div]; simp_rw [Finset.sum_add_distrib] + rfl + rw [hK,hC,watt,watt,start,delayed, Finset.mul_sum, Finset.mul_sum, Finset.mul_sum,Finset.mul_sum, + ← Finset.sum_add_distrib, ← Finset.sum_add_distrib,← Finset.sum_add_distrib,← Finset.sum_add_distrib] + apply Finset.sum_le_sum; intro i _ + exact flash_cell e h r t q (hb ▸ hq) ha he i + +def lit (e : Lamp M) (s : Scene M) := (energy s e.u + e.width^2)*fax s + shower e.u s + +lemma start_cost (e : Lamp M) (h : Climb e.site) (ha : 0 ≤ e.width) : + start e^2 ≤ 100*lit e e.site := by + let a := e.width; let W := e.W; let s := e.site; let u := e.u + let x := ∑ z,‖factor s u z‖; let y := ∑ z, |shell s z| + have H : |start e| ≤ x + a*y := by + apply le_trans (Finset.abs_sum_le_sum_abs _ _) + unfold x y; rw [Finset.mul_sum,← Finset.sum_add_distrib] + apply Finset.sum_le_sum; intro i _ + apply (glow_limit _ _ _ _ _ _).trans + rw [norm_zero,zero_add,abs_mul,abs_of_nonneg ha] + have hX : x^2 ≤ _ := factor_bill _ h u + have hY : y^2 ≤ _ := shell_bill _ h + have hT : (start e)^2 ≤ (x+a*y)^2 := by + rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) H _ + have hV : (a*y)^2 ≤ a^2*(2*fax s) := by rw [mul_pow]; gcongr + change _ ≤ 100*((energy s u+a^2)*fax s+shower u s) + nlinarith [mul_nonneg (energy_small h.fit u).1 (fax_floor h).1,shower_nonneg (u:=u) h,sq_nonneg (x-a*y), + mul_nonneg (sq_nonneg a) (fax_floor h).1] + +lemma delayed_cost (e : Lamp M) (h : Climb e.site) {q : Scene M} (hq : Climb q) + (ha : 0 ≤ e.width) (r : Bool) : + delayed e r q^2 ≤ 100*(lit e e.site+lit e q) := by + let a := e.width; let W := e.W; let s := e.site; let u := e.u + let x (s : Scene M) := ∑ z,‖factor s u z‖ + let y := ∑ z, |shell q z| + have H : |delayed e r q| ≤ x s+x q + a*y := by + apply le_trans (Finset.abs_sum_le_sum_abs _ _) + unfold x y; rw [Finset.mul_sum,← Finset.sum_add_distrib,← Finset.sum_add_distrib] + apply Finset.sum_le_sum; intro i _ + apply (glow_limit _ _ _ _ _ _).trans + rw [norm_scaleC,curl_unit,one_mul,abs_mul,abs_of_nonneg ha] + have hX₁ : x s^2 ≤ _ := factor_bill _ h u + have hX₂ : x q^2 ≤ _ := factor_bill _ hq u + have hY : y^2 ≤ _ := shell_bill _ hq + have hT : delayed e r q^2 ≤ (x s+x q+a*y)^2 := by + rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) H _ + have hV : (a*y)^2 ≤ a^2*(2*fax q) := by rw [mul_pow]; gcongr + let A (s : Scene M) := (energy s u+a^2)*fax s+shower u s + have HI (w : Scene M) (hw : Climb w) : 0 ≤ energy w u*fax w ∧ 0 ≤ a^2*fax w := + ⟨mul_nonneg (energy_small hw.fit u).1 (fax_floor hw).1,mul_nonneg (sq_nonneg a) (fax_floor hw).1⟩ + have h1 := HI s h; have h2 := HI q hq + change _ ≤ 100*((energy s u+a^2)*fax s+shower u s+((energy q u+a^2)*fax q+shower u q)) + nlinarith [shower_nonneg (u:=u) h, shower_nonneg (u:=u) hq,sq_nonneg (x s-x q),sq_nonneg (x q-a*y),sq_nonneg (x s-a*y)] + +end Lamp +end Beam + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Frost.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Frost.lean index 2bd4839923e344e17b98eafd5d0ec20022663bbe..d6e7fdd45fb920170438ffe3861f038af85f3d75 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Frost.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Frost.lean @@ -1,234 +1,234 @@ -import OAI.Analysis.FourierLLogL.Paths.Tilt -import OAI.Analysis.FourierLLogL.Paths.Wager -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -open Desk - -namespace Lens - -def quest (g : Lens M) : Desk M Unit where - stake _ _ s := -(g.swing s)/(2000*g.rate) - stepAt _ _ := () -def dusk (g : Lens M) (l : Path M) := - subtotal (fun i => (g.coin i.post)^2) l - -lemma stipend (g : Lens M) {b : Beat M} (h : Ideal b.pre b.post) (hp : 0 < g.rate) : - let r := g.rate - (g.coin b.post)^2/2 ≤ - g.hero (winds b)-g.hero b.pre+30*r*killed b+2000*r*((quest g).pay () b) := by - intro r - rw [winds,g.hero_walk] - let x := g.swing b.post; let l := 2000*r - have hi : 0 < r := hp - have hh : 0 < l := by unfold l; positivity - have hD := coin_flow g hp.le h.climb - have hF := swing_flow g hp.le h.climb - change x^2 ≤ 900*r*_ at hF - have hx : l* (g.quest.pay () b) = -curl b.bit*x - x^2/(2*l) := by - change l* (curl b.bit*(-x/l)-(-x/l)^2/2)=_ - field_simp - have hE : x^2/(2*l) ≤ g.flow b.post/2 := by - apply (div_le_iff₀ (by positivity)).mpr - have HG : 0 ≤ g.flow b.post := le_trans (sq_nonneg _) hD.1 - unfold l - nlinarith [mul_nonneg hi.le HG] - have HW := (abs_le.mp (hero_drain g hi.le h)).2 - change _ ≤ _+curl b.bit*x - g.hero b.pre + 30*r*killed b + l*_ - rw [hx]; change _ ≤ 30*r *_ at HW - linarith [hD.1] - -lemma dusk_tally (g : Lens M) {x : Grid M} {l : Path M} (h : Trip x s l) (hp : 0 < g.rate) : - g.dusk l /2 ≤ 120*g.rate-g.hero s+30*g.rate*fullKills l + 2000*g.rate*(g.quest.tally () l) := by - induction l generalizing s with - | nil => - have H := (g.hero_limit hp.le (trip_head h).1).2 - change 0/2 ≤ _-hero g s+_*0+_*0; linarith - | cons b l ih => - obtain ⟨rfl,ha,hb⟩ := h - have hh := ih hb - have hi := stipend g ha hp - change _ ≤ _-g.hero b.pre+_+_ at hi - change ((g.coin b.post)^2+g.dusk l)/2 ≤ _+ _*(killed b+fullKills l)+_*(g.quest.pay () b+g.quest.tally () l) - linarith - -def ease (g : Lens M) := √g.rate -def shifted (g : Lens M) : Desk M ℝ where - stake t _ _ := t / g.ease - stepAt _ b := curl b.bit * g.coin b.post -def enters (c : ℝ) : Path M → ℝ -| [] => 0 -| b::_ => curl b.bit*c - -lemma last_lag (g : Lens M) {x : Grid M} {l : Path M} (h : Trip x s l) - (hp : 0 < g.rate) (c : ℝ) : - let a := g.ease - enters c l + part g.face (subtotal (frosty x g.focus g.rate) l) ≤ - a*(g.shifted.tally c l)+(g.dusk l+c^2)/(2*a)+10*g.rate := by - intro a - have he : 0 < a := Real.sqrt_pos.2 hp - induction l generalizing s c with - | nil => - change 0+part g.face 0 ≤ _*0 + (0+c^2)/(2*a)+10*g.rate - rw [part_zero]; have hh : 0 ≤ (0+c^2)/(2*a) := by positivity - linarith - | cons b l ih => - let k := g.coin b.post; let t := curl b.bit*k - have HE : a*g.shifted.pay c b = curl b.bit*c-c^2/(2*a) := by - change a*((curl _)*(c/a)-(c/a)^2/2)=_ - field_simp - rw [subtotal_cons,part_add] - rw [frosty,part_mul] - rw [Desk.tally,mul_add,HE] - let A := frosty x g.focus g.rate - change curl b.bit*c+(blow x b*k+part g.face (subtotal A l)) ≤ - curl b.bit*c-c^2/(2*a)+a*g.shifted.tally t l +(k^2+g.dusk l+c^2)/(2*a)+10*g.rate - rw [add_div] - cases l with - | nil => - have hi : |blow x b*k| ≤ 10*g.rate := by - rw [abs_mul,trip_final h,one_mul]; exact (part_bounded _ _).trans (prize_norm hp.le _ _) - have hh : 0 ≤ (k^2+0)/(2*a) := by positivity - rw [subtotal_nil,part_zero,tally] - rw [show g.dusk []=0 from rfl] - linarith [(abs_le.mp hi).2] - | cons d l => - have hh := ih h.2.2 t - have hH : enters t (d::l)=blow x b*k := by rw [trip_delay h]; unfold enters t; ring - have hE : t^2=k^2 := by unfold t; rw [mul_pow,curl_sq,one_mul] - rw [hH,hE,add_comm (g.dusk _)] at hh; change _+part g.face (subtotal A _) ≤ _ at hh - linarith - -lemma face_fire (g : Lens M) {x : Grid M} (l m : Path M) (h : Trip x s l) - (hp : 0 < g.rate) (he : g.rate ≤ 1) (t : ℝ) - (ht : 0 ≤ t) - (h1 : 2*log (g.quest.heatW () (l++m)) ≤ t) (h2 : 2*log (g.shifted.heatW 0 (l++m)) ≤ t) : - part g.face (subtotal (frosty x g.focus g.rate) l) ≤ 3000*g.ease*(1+fullKills l+t) := by - let r := g.rate; let a := g.ease; let K := fullKills l - let V := g.dusk l - have HI := last_lag g h hp 0 - have h3 : 0 < a := Real.sqrt_pos.2 hp - have h4 : a^2=r := Real.sq_sqrt hp.le - change r ≤ _ at he - change _ ≤ a*_+(V+0^2)/(2*a)+10*r at HI - have hx := (g.quest.edge_score () l m).trans h1 - have hx' := (g.shifted.edge_score 0 l m).trans h2 - rw [show enters 0 l=0 by cases l <;> simp [enters]] at HI - have HH := dusk_tally g h hp - have hw := g.hero_limit hp.le (trip_head h).1 - have HS : V/(2*a) ≤ a*(120+30*K+2000*t) := by - apply (div_le_iff₀ (by positivity)).mpr - have HB : a*(120+30*K+2000*t)*(2*a) = 2*r*(120+30*K+2000*t) := by rw [← h4]; ring - rw [HB] - change V/2 ≤ 120*r-hero g s+30*r*K+2000*r*_ at HH - change 0 < r at hp - nlinarith [hw.1] - have h6 : a ≤ 1 := by nlinarith - have h8 : r ≤ a := by nlinarith - have HQ : 0 ≤ K := trip_K h - change _ ≤ 3000*a*(1+K+t) - rw [show V+0^2=V by ring] at HI - nlinarith - -end Lens - -def coldScore (u : Grid M) (b : ℝ) (l : Path M) := - 1 + ∑ d : Face, let g := Lens.mk u b d; g.quest.heatW () l+g.shifted.heatW 0 l - -private lemma cold_share (u : Grid M) (b : ℝ) (l : Path M) (d : Face) : - let g := Lens.mk u b d; g.quest.heatW () l + g.shifted.heatW 0 l ≤ coldScore u b l := by - intro g - unfold coldScore - rw [add_comm (1:ℝ)] - apply le_trans _ (le_add_of_nonneg_right (by norm_num : (0:ℝ) ≤ 1)) - apply Finset.single_le_sum (f:=fun d : Face => (Lens.mk u b d).quest.heatW () l+ - (Lens.mk u b d).shifted.heatW 0 l) _ (Finset.mem_univ d) - intro x hx; apply add_nonneg - · linarith [(Lens.mk u b x).quest.heatW_one () l] - · linarith [(Lens.mk u b x).shifted.heatW_one 0 l] - -lemma cold_one (u : Grid M) (b : ℝ) (l : Path M) : 1 ≤ coldScore u b l := by - let g := Lens.mk u b (false,false) - have H := cold_share u b l (false,false) - change g.quest.heatW () l + g.shifted.heatW 0 l ≤ _ at H - linarith [g.shifted.heatW_one 0 l,g.quest.heatW_one () l] - -lemma cold_mean (u : Grid M) (b : ℝ) (t : Thread M s) (h : t.Following j J f) : - (s.here.dice M).Ex (fun x => coldScore u b (t.stream x)) ≤ 20 := by - unfold coldScore; rw [Ex_add _ (f:=fun _ => _),Ex_const,Ex_finset_sum] - let U (d : Face) := Lens.mk u b d - let P := s.here.dice M - let F (d) := P.Ex fun x => (U d).quest.heatW () (t.stream x)+(U d).shifted.heatW 0 (t.stream x) - change 1+∑ d,F d ≤ _ - have H (d) : F d ≤ 4 := by - unfold F; rw [Ex_add] - have HH : P.Ex (fun x => (U d).quest.heatW () (t.stream x)) ≤ 2 := - heat_bound _ t h _ - have HI : P.Ex (fun x => (U d).shifted.heatW 0 (t.stream x)) ≤ 2 := - heat_bound _ t h _ - linarith - have HH : ∑ d,F d ≤ ∑ _d : Face,(4:ℝ) := Finset.sum_le_sum fun i _ => H i - norm_num [Face] at HH - linarith - -private lemma fire_pref {x : Grid M} (u : Grid M) {b : ℝ} (hb : 0 < b) (hi : b ≤ 1) - (l m : Path M) (h : Trip x s l) (t : ℝ) (ht : 2*log (coldScore u b (l++m)) ≤ t) : - ‖subtotal (frosty x u b) l‖ ≤ 6000*√b*(1+fullKills l+t) := by - let v := subtotal (frosty x u b) l; let a := 3000*√b*(1+fullKills l+t) - let score := coldScore u b (l++m) - have H (d : Face) : part d v ≤ a := by - let g := Lens.mk u b d - have h1 : 1 ≤ g.shifted.heatW 0 (l++m) := heatW_one _ _ _ - have h2 : 1 ≤ g.quest.heatW () (l++m) := heatW_one _ _ _ - have h3 : g.quest.heatW () (l++m) + g.shifted.heatW 0 (l++m) ≤ score := cold_share _ _ _ d - have H (x : ℝ) (h : 1 ≤ x) (hh : x ≤ score) : 2*log x ≤ t := - le_trans (mul_le_mul_of_nonneg_left (log_le_log (by linarith) hh) (by norm_num)) ht - apply g.face_fire l m h hb hi - · exact le_trans (mul_nonneg (by norm_num) (log_nonneg (cold_one _ _ _))) ht - · exact H _ h2 (by linarith) - · exact H _ h1 (by linarith) - have h1 : |v.re| ≤ a := by - rw [abs_le] - have He := H (false,true); have Hf := H (false,false); simp only [part,curl,↓reduceIte, - Bool.false_eq_true,neg_mul,one_mul] at He Hf - constructor <;> linarith - have h2 : |v.im| ≤ a := by - rw [abs_le] - have He := H (true,true); have Hf := H (true,false); simp only [part,curl,↓reduceIte, - Bool.false_eq_true,neg_mul,one_mul] at He Hf - constructor <;> linarith - change ‖v‖ ≤ _ - unfold a at *; linarith [Complex.norm_le_abs_re_add_abs_im v] - -lemma frosty_portion {x : Grid M} (u : Grid M) {b : ℝ} (hb : 0 < b) (hi : b ≤ 1) - (l r m : Path M) (h : Trip x s (l++r++m)) (t : ℝ) - (ht : 2*log (coldScore u b (l++r++m)) ≤ t) : - ‖subtotal (frosty x u b) r‖ ≤ 12000*√b*(1+fullKills (l++r++m)+t) := by - obtain ⟨ha,w,hc⟩ := trip_cut h - obtain ⟨hd,v,he⟩ := trip_cut ha - have H := fire_pref u hb hi _ _ ha t ht - rw [List.append_assoc] at ht - have HE := fire_pref u hb hi _ _ hd t ht - let F := frosty x u b - rw [show subtotal (frosty x u b) r=subtotal F (l++r)-subtotal F l by rw [subtotal_app]; abel] - apply (norm_sub_le _ _).trans - have HH : fullKills (l++r++m)=fullKills l+fullKills r+fullKills m := by simp [fullKills,subtotal_app,add_assoc] - have HI : fullKills (l++r)=fullKills l+fullKills r := by simp [fullKills,subtotal_app] - rw [HH]; rw [HI] at H; unfold F - nlinarith [trip_K hc,trip_K he,sqrt_pos.2 hb] - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Tilt +import OAI.Analysis.FourierLLogL.Paths.Wager +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +open Desk + +namespace Lens + +def quest (g : Lens M) : Desk M Unit where + stake _ _ s := -(g.swing s)/(2000*g.rate) + stepAt _ _ := () +def dusk (g : Lens M) (l : Path M) := + subtotal (fun i => (g.coin i.post)^2) l + +lemma stipend (g : Lens M) {b : Beat M} (h : Ideal b.pre b.post) (hp : 0 < g.rate) : + let r := g.rate + (g.coin b.post)^2/2 ≤ + g.hero (winds b)-g.hero b.pre+30*r*killed b+2000*r*((quest g).pay () b) := by + intro r + rw [winds,g.hero_walk] + let x := g.swing b.post; let l := 2000*r + have hi : 0 < r := hp + have hh : 0 < l := by unfold l; positivity + have hD := coin_flow g hp.le h.climb + have hF := swing_flow g hp.le h.climb + change x^2 ≤ 900*r*_ at hF + have hx : l* (g.quest.pay () b) = -curl b.bit*x - x^2/(2*l) := by + change l* (curl b.bit*(-x/l)-(-x/l)^2/2)=_ + field_simp + have hE : x^2/(2*l) ≤ g.flow b.post/2 := by + apply (div_le_iff₀ (by positivity)).mpr + have HG : 0 ≤ g.flow b.post := le_trans (sq_nonneg _) hD.1 + unfold l + nlinarith [mul_nonneg hi.le HG] + have HW := (abs_le.mp (hero_drain g hi.le h)).2 + change _ ≤ _+curl b.bit*x - g.hero b.pre + 30*r*killed b + l*_ + rw [hx]; change _ ≤ 30*r *_ at HW + linarith [hD.1] + +lemma dusk_tally (g : Lens M) {x : Grid M} {l : Path M} (h : Trip x s l) (hp : 0 < g.rate) : + g.dusk l /2 ≤ 120*g.rate-g.hero s+30*g.rate*fullKills l + 2000*g.rate*(g.quest.tally () l) := by + induction l generalizing s with + | nil => + have H := (g.hero_limit hp.le (trip_head h).1).2 + change 0/2 ≤ _-hero g s+_*0+_*0; linarith + | cons b l ih => + obtain ⟨rfl,ha,hb⟩ := h + have hh := ih hb + have hi := stipend g ha hp + change _ ≤ _-g.hero b.pre+_+_ at hi + change ((g.coin b.post)^2+g.dusk l)/2 ≤ _+ _*(killed b+fullKills l)+_*(g.quest.pay () b+g.quest.tally () l) + linarith + +def ease (g : Lens M) := √g.rate +def shifted (g : Lens M) : Desk M ℝ where + stake t _ _ := t / g.ease + stepAt _ b := curl b.bit * g.coin b.post +def enters (c : ℝ) : Path M → ℝ +| [] => 0 +| b::_ => curl b.bit*c + +lemma last_lag (g : Lens M) {x : Grid M} {l : Path M} (h : Trip x s l) + (hp : 0 < g.rate) (c : ℝ) : + let a := g.ease + enters c l + part g.face (subtotal (frosty x g.focus g.rate) l) ≤ + a*(g.shifted.tally c l)+(g.dusk l+c^2)/(2*a)+10*g.rate := by + intro a + have he : 0 < a := Real.sqrt_pos.2 hp + induction l generalizing s c with + | nil => + change 0+part g.face 0 ≤ _*0 + (0+c^2)/(2*a)+10*g.rate + rw [part_zero]; have hh : 0 ≤ (0+c^2)/(2*a) := by positivity + linarith + | cons b l ih => + let k := g.coin b.post; let t := curl b.bit*k + have HE : a*g.shifted.pay c b = curl b.bit*c-c^2/(2*a) := by + change a*((curl _)*(c/a)-(c/a)^2/2)=_ + field_simp + rw [subtotal_cons,part_add] + rw [frosty,part_mul] + rw [Desk.tally,mul_add,HE] + let A := frosty x g.focus g.rate + change curl b.bit*c+(blow x b*k+part g.face (subtotal A l)) ≤ + curl b.bit*c-c^2/(2*a)+a*g.shifted.tally t l +(k^2+g.dusk l+c^2)/(2*a)+10*g.rate + rw [add_div] + cases l with + | nil => + have hi : |blow x b*k| ≤ 10*g.rate := by + rw [abs_mul,trip_final h,one_mul]; exact (part_bounded _ _).trans (prize_norm hp.le _ _) + have hh : 0 ≤ (k^2+0)/(2*a) := by positivity + rw [subtotal_nil,part_zero,tally] + rw [show g.dusk []=0 from rfl] + linarith [(abs_le.mp hi).2] + | cons d l => + have hh := ih h.2.2 t + have hH : enters t (d::l)=blow x b*k := by rw [trip_delay h]; unfold enters t; ring + have hE : t^2=k^2 := by unfold t; rw [mul_pow,curl_sq,one_mul] + rw [hH,hE,add_comm (g.dusk _)] at hh; change _+part g.face (subtotal A _) ≤ _ at hh + linarith + +lemma face_fire (g : Lens M) {x : Grid M} (l m : Path M) (h : Trip x s l) + (hp : 0 < g.rate) (he : g.rate ≤ 1) (t : ℝ) + (ht : 0 ≤ t) + (h1 : 2*log (g.quest.heatW () (l++m)) ≤ t) (h2 : 2*log (g.shifted.heatW 0 (l++m)) ≤ t) : + part g.face (subtotal (frosty x g.focus g.rate) l) ≤ 3000*g.ease*(1+fullKills l+t) := by + let r := g.rate; let a := g.ease; let K := fullKills l + let V := g.dusk l + have HI := last_lag g h hp 0 + have h3 : 0 < a := Real.sqrt_pos.2 hp + have h4 : a^2=r := Real.sq_sqrt hp.le + change r ≤ _ at he + change _ ≤ a*_+(V+0^2)/(2*a)+10*r at HI + have hx := (g.quest.edge_score () l m).trans h1 + have hx' := (g.shifted.edge_score 0 l m).trans h2 + rw [show enters 0 l=0 by cases l <;> simp [enters]] at HI + have HH := dusk_tally g h hp + have hw := g.hero_limit hp.le (trip_head h).1 + have HS : V/(2*a) ≤ a*(120+30*K+2000*t) := by + apply (div_le_iff₀ (by positivity)).mpr + have HB : a*(120+30*K+2000*t)*(2*a) = 2*r*(120+30*K+2000*t) := by rw [← h4]; ring + rw [HB] + change V/2 ≤ 120*r-hero g s+30*r*K+2000*r*_ at HH + change 0 < r at hp + nlinarith [hw.1] + have h6 : a ≤ 1 := by nlinarith + have h8 : r ≤ a := by nlinarith + have HQ : 0 ≤ K := trip_K h + change _ ≤ 3000*a*(1+K+t) + rw [show V+0^2=V by ring] at HI + nlinarith + +end Lens + +def coldScore (u : Grid M) (b : ℝ) (l : Path M) := + 1 + ∑ d : Face, let g := Lens.mk u b d; g.quest.heatW () l+g.shifted.heatW 0 l + +private lemma cold_share (u : Grid M) (b : ℝ) (l : Path M) (d : Face) : + let g := Lens.mk u b d; g.quest.heatW () l + g.shifted.heatW 0 l ≤ coldScore u b l := by + intro g + unfold coldScore + rw [add_comm (1:ℝ)] + apply le_trans _ (le_add_of_nonneg_right (by norm_num : (0:ℝ) ≤ 1)) + apply Finset.single_le_sum (f:=fun d : Face => (Lens.mk u b d).quest.heatW () l+ + (Lens.mk u b d).shifted.heatW 0 l) _ (Finset.mem_univ d) + intro x hx; apply add_nonneg + · linarith [(Lens.mk u b x).quest.heatW_one () l] + · linarith [(Lens.mk u b x).shifted.heatW_one 0 l] + +lemma cold_one (u : Grid M) (b : ℝ) (l : Path M) : 1 ≤ coldScore u b l := by + let g := Lens.mk u b (false,false) + have H := cold_share u b l (false,false) + change g.quest.heatW () l + g.shifted.heatW 0 l ≤ _ at H + linarith [g.shifted.heatW_one 0 l,g.quest.heatW_one () l] + +lemma cold_mean (u : Grid M) (b : ℝ) (t : Thread M s) (h : t.Following j J f) : + (s.here.dice M).Ex (fun x => coldScore u b (t.stream x)) ≤ 20 := by + unfold coldScore; rw [Ex_add _ (f:=fun _ => _),Ex_const,Ex_finset_sum] + let U (d : Face) := Lens.mk u b d + let P := s.here.dice M + let F (d) := P.Ex fun x => (U d).quest.heatW () (t.stream x)+(U d).shifted.heatW 0 (t.stream x) + change 1+∑ d,F d ≤ _ + have H (d) : F d ≤ 4 := by + unfold F; rw [Ex_add] + have HH : P.Ex (fun x => (U d).quest.heatW () (t.stream x)) ≤ 2 := + heat_bound _ t h _ + have HI : P.Ex (fun x => (U d).shifted.heatW 0 (t.stream x)) ≤ 2 := + heat_bound _ t h _ + linarith + have HH : ∑ d,F d ≤ ∑ _d : Face,(4:ℝ) := Finset.sum_le_sum fun i _ => H i + norm_num [Face] at HH + linarith + +private lemma fire_pref {x : Grid M} (u : Grid M) {b : ℝ} (hb : 0 < b) (hi : b ≤ 1) + (l m : Path M) (h : Trip x s l) (t : ℝ) (ht : 2*log (coldScore u b (l++m)) ≤ t) : + ‖subtotal (frosty x u b) l‖ ≤ 6000*√b*(1+fullKills l+t) := by + let v := subtotal (frosty x u b) l; let a := 3000*√b*(1+fullKills l+t) + let score := coldScore u b (l++m) + have H (d : Face) : part d v ≤ a := by + let g := Lens.mk u b d + have h1 : 1 ≤ g.shifted.heatW 0 (l++m) := heatW_one _ _ _ + have h2 : 1 ≤ g.quest.heatW () (l++m) := heatW_one _ _ _ + have h3 : g.quest.heatW () (l++m) + g.shifted.heatW 0 (l++m) ≤ score := cold_share _ _ _ d + have H (x : ℝ) (h : 1 ≤ x) (hh : x ≤ score) : 2*log x ≤ t := + le_trans (mul_le_mul_of_nonneg_left (log_le_log (by linarith) hh) (by norm_num)) ht + apply g.face_fire l m h hb hi + · exact le_trans (mul_nonneg (by norm_num) (log_nonneg (cold_one _ _ _))) ht + · exact H _ h2 (by linarith) + · exact H _ h1 (by linarith) + have h1 : |v.re| ≤ a := by + rw [abs_le] + have He := H (false,true); have Hf := H (false,false); simp only [part,curl,↓reduceIte, + Bool.false_eq_true,neg_mul,one_mul] at He Hf + constructor <;> linarith + have h2 : |v.im| ≤ a := by + rw [abs_le] + have He := H (true,true); have Hf := H (true,false); simp only [part,curl,↓reduceIte, + Bool.false_eq_true,neg_mul,one_mul] at He Hf + constructor <;> linarith + change ‖v‖ ≤ _ + unfold a at *; linarith [Complex.norm_le_abs_re_add_abs_im v] + +lemma frosty_portion {x : Grid M} (u : Grid M) {b : ℝ} (hb : 0 < b) (hi : b ≤ 1) + (l r m : Path M) (h : Trip x s (l++r++m)) (t : ℝ) + (ht : 2*log (coldScore u b (l++r++m)) ≤ t) : + ‖subtotal (frosty x u b) r‖ ≤ 12000*√b*(1+fullKills (l++r++m)+t) := by + obtain ⟨ha,w,hc⟩ := trip_cut h + obtain ⟨hd,v,he⟩ := trip_cut ha + have H := fire_pref u hb hi _ _ ha t ht + rw [List.append_assoc] at ht + have HE := fire_pref u hb hi _ _ hd t ht + let F := frosty x u b + rw [show subtotal (frosty x u b) r=subtotal F (l++r)-subtotal F l by rw [subtotal_app]; abel] + apply (norm_sub_le _ _).trans + have HH : fullKills (l++r++m)=fullKills l+fullKills r+fullKills m := by simp [fullKills,subtotal_app,add_assoc] + have HI : fullKills (l++r)=fullKills l+fullKills r := by simp [fullKills,subtotal_app] + rw [HH]; rw [HI] at H; unfold F + nlinarith [trip_K hc,trip_K he,sqrt_pos.2 hb] + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Gust.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Gust.lean index f15b7d6c2a52e971fd222839cc8e4138d8952571..e9bd0c248b950b1f7bfc54d218c9da74c713dfba 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Gust.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Gust.lean @@ -1,127 +1,127 @@ -import OAI.Analysis.FourierLLogL.Paths.Energies -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -lemma sky_variance (s : Scene M) (h : Climb s) (u : Grid M) (L : ℝ) - (hp : ∀ b, energy (s.smaller b) u ≤ L) : - gust s u^2 ≤ 100*L*(fax s+shower u s) := by - let v := meanV s u; let n := nub s u - let m := matter s; let g := shell s - let d := fun i z => mist u s (s.smaller i) z - let G := energy s u; let J := fax s; let V := shower u s - let A (z) := g z*‖v z‖^2; let B (z) := duet (v z) (n z) - let D (i : Bool) := ∑ z, d i z - have H (b : Bool) : - energy (s.smaller b) u=G+curl b*((∑ z,A z)+2*∑ z,B z)+D b := by - unfold D G - rw [sun_energy (h.child b),sun_energy h.fit] - simp_rw [sun_shift h,Finset.sum_add_distrib, mul_add, Finset.sum_add_distrib,Finset.mul_sum] - exact le_antisymm le_rfl le_rfl - have hm := matter_nonneg h.fit - have hA : (∑ z,A z)^2 ≤ G*J := by - unfold G J fax - rw [sun_energy h.fit] - refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul Finset.univ - (fun i _ => (sun_rays h.fit _ _).1) - (fun i _ => pierce_nonneg h.fit i) (fun i _ => ?_) - change (shell s i*‖v i‖^2)^2 ≤ (m i*‖v i‖^2)*pierce s i - rw [mul_pow,shell_sq h i] - have hN : ‖v i‖^2 ≤ 1 := pow_le_one₀ (norm_nonneg _) (mean_hold s h.fit _ _).2 - change (m i * _) * _ ≤ _ - rw [pow_two (‖v i‖^2)] - have HE : 0 ≤ (m i*pierce s i)*‖v i‖^2 := - mul_nonneg (mul_nonneg (hm _) (pierce_nonneg h.fit i)) (sq_nonneg _) - nlinarith [mul_le_mul_of_nonneg_left hN HE] - have hB : (∑ z,B z)^2 ≤ G*(4*V) := by - unfold V shower G - rw [sun_energy h.fit, Finset.mul_sum] - refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul Finset.univ - (fun i _ => (sun_rays h.fit _ _).1) - (fun i _ => mul_nonneg (by norm_num) (dew_nonneg _ h _)) (fun i _ => ?_) - have Hq := nub_innovation s h u i - have HE : (B i)^2 ≤ (‖v i‖ * ‖n i‖)^2 := by - rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) (duet_limit _ _) _ - rw [mul_pow] at HE - change _ ≤ m i*‖v i‖^2 * _ - change ‖n i‖^2 ≤ 4*m i*_ at Hq - nlinarith [mul_le_mul_of_nonneg_left Hq (sq_nonneg ‖v i‖)] - let R := (D false-D true)/2 - have hD : V=(D false+D true)/2 := by - unfold V D shower dew; rw [← Finset.sum_div,Finset.sum_add_distrib] - have hH : |R| ≤ V := by - have HH (i) : 0 ≤ D i := Finset.sum_nonneg (fun _ _ => mist_nonneg u s _ (h.child i) _) - rw [abs_le,hD]; unfold R; constructor <;> linarith [HH false,HH true] - have hL₁ := (energy_small h.fit u).1 - have hL₂ : 0 ≤ V := shower_nonneg h - have hF : 0 ≤ J := (fax_floor h).1 - have hQ : G+V ≤ L := by rw [show G+V=_ from (wet_average s h u).symm]; linarith [hp false,hp true] - have hr : R^2 ≤ V*L := by - change 0 ≤ G at hL₁ - have HH := abs_le.mp hH - nlinarith - rw [gust,H false,H true] - simp only [curl,Bool.false_eq_true,↓reduceIte,neg_mul,one_mul] - let a := ∑ z,A z; let b := ∑ z,B z - change ((G+(a+2*b)+D false-(G+ -(a+2*b)+D true))/2)^2 ≤ 100*L*(J+V) - rw [show (G+(a+2*b)+D false-(G+ -(a+2*b)+D true))/2=a+2*b+R by unfold R; ring] - change a^2 ≤ _ at hA; change b^2 ≤ _ at hB - have hG : 0 ≤ G := hL₁ - have hT : (a+2*b+R)^2 ≤ 3*(a^2+(2*b)^2+R^2) := by nlinarith [sq_nonneg (a-2*b),sq_nonneg (2*b-R),sq_nonneg (a-R)] - nlinarith - -lemma factor_bill (s : Scene M) (h : Climb s) (u : Grid M) : - (∑ z : Grid M, ‖factor s u z‖)^2 ≤ - 16*(energy s u* fax s + shower u s) := by - let n := nub s u; let v := meanV s u; let m := matter s; let g := shell s - let G := energy s u; let J := fax s; let V := shower u s - let f (z : Grid M) := |g z| * ‖v z‖; let k (z : Grid M) := ‖n z‖ - have hm := matter_nonneg h.fit - have H (z : Grid M) : ‖factor s u z‖ ≤ f z+k z := by - rw [show factor s u z=(↑(g z):ℂ)*v z+n z by unfold n v g nub; ring] - apply (norm_add_le _ _).trans; rw [norm_scaleC] - have hF : (∑ z,f z)^2 ≤ G*J := by - unfold G J fax; rw [sun_energy h.fit] - apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ - (fun i _ => (sun_rays h.fit _ _).1) (fun i _ => pierce_nonneg h.fit _) (fun i _ => ?_) - change (|shell s i| * ‖v i‖)^2 ≤ matter s i* ‖v i‖^2 * pierce s i - rw [mul_pow,sq_abs,shell_sq h] - exact le_of_eq (by ring) - have hK : (∑ z,k z)^2 ≤ 2*(4*V) := by - apply le_trans (b:=share s*(4*V)) - · rw [← matter_sum h.fit] - unfold V shower; rw [Finset.mul_sum] - apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => hm i) - (fun i _ => mul_nonneg (by norm_num) (dew_nonneg _ h _)) (fun i _ => ?_) - have Hh := nub_innovation _ h u i; change ‖nub s u i‖^2 ≤ _ - linarith - · exact mul_le_mul_of_nonneg_right h.tight (mul_nonneg (by norm_num) (shower_nonneg h)) - let a := ∑ z,f z; let b := ∑ z,k z; let c := ∑ z,‖factor s u z‖ - have he : c ≤ a+b := by unfold c a b; rw [← Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => H i - have hc : 0 ≤ c := Finset.sum_nonneg fun i _ => norm_nonneg _ - change a^2 ≤ _ at hF; change b^2 ≤ _ at hK; change c^2 ≤ 16*(G*J+V) - nlinarith [sq_nonneg (a-b)] - -lemma shell_bill (s : Scene M) (h : Climb s) : - (∑ z,|shell s z|)^2 ≤ 2*fax s := by - apply le_trans (b:=share s*fax s) - · rw [←matter_sum h.fit] - apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => matter_nonneg h.fit i) - (fun i _ => pierce_nonneg h.fit i) (fun i _ => ?_) - rw [sq_abs,shell_sq h] - exact mul_le_mul_of_nonneg_right h.tight (fax_floor h).1 - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Energies +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +lemma sky_variance (s : Scene M) (h : Climb s) (u : Grid M) (L : ℝ) + (hp : ∀ b, energy (s.smaller b) u ≤ L) : + gust s u^2 ≤ 100*L*(fax s+shower u s) := by + let v := meanV s u; let n := nub s u + let m := matter s; let g := shell s + let d := fun i z => mist u s (s.smaller i) z + let G := energy s u; let J := fax s; let V := shower u s + let A (z) := g z*‖v z‖^2; let B (z) := duet (v z) (n z) + let D (i : Bool) := ∑ z, d i z + have H (b : Bool) : + energy (s.smaller b) u=G+curl b*((∑ z,A z)+2*∑ z,B z)+D b := by + unfold D G + rw [sun_energy (h.child b),sun_energy h.fit] + simp_rw [sun_shift h,Finset.sum_add_distrib, mul_add, Finset.sum_add_distrib,Finset.mul_sum] + exact le_antisymm le_rfl le_rfl + have hm := matter_nonneg h.fit + have hA : (∑ z,A z)^2 ≤ G*J := by + unfold G J fax + rw [sun_energy h.fit] + refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul Finset.univ + (fun i _ => (sun_rays h.fit _ _).1) + (fun i _ => pierce_nonneg h.fit i) (fun i _ => ?_) + change (shell s i*‖v i‖^2)^2 ≤ (m i*‖v i‖^2)*pierce s i + rw [mul_pow,shell_sq h i] + have hN : ‖v i‖^2 ≤ 1 := pow_le_one₀ (norm_nonneg _) (mean_hold s h.fit _ _).2 + change (m i * _) * _ ≤ _ + rw [pow_two (‖v i‖^2)] + have HE : 0 ≤ (m i*pierce s i)*‖v i‖^2 := + mul_nonneg (mul_nonneg (hm _) (pierce_nonneg h.fit i)) (sq_nonneg _) + nlinarith [mul_le_mul_of_nonneg_left hN HE] + have hB : (∑ z,B z)^2 ≤ G*(4*V) := by + unfold V shower G + rw [sun_energy h.fit, Finset.mul_sum] + refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul Finset.univ + (fun i _ => (sun_rays h.fit _ _).1) + (fun i _ => mul_nonneg (by norm_num) (dew_nonneg _ h _)) (fun i _ => ?_) + have Hq := nub_innovation s h u i + have HE : (B i)^2 ≤ (‖v i‖ * ‖n i‖)^2 := by + rw [←sq_abs]; exact pow_le_pow_left₀ (abs_nonneg _) (duet_limit _ _) _ + rw [mul_pow] at HE + change _ ≤ m i*‖v i‖^2 * _ + change ‖n i‖^2 ≤ 4*m i*_ at Hq + nlinarith [mul_le_mul_of_nonneg_left Hq (sq_nonneg ‖v i‖)] + let R := (D false-D true)/2 + have hD : V=(D false+D true)/2 := by + unfold V D shower dew; rw [← Finset.sum_div,Finset.sum_add_distrib] + have hH : |R| ≤ V := by + have HH (i) : 0 ≤ D i := Finset.sum_nonneg (fun _ _ => mist_nonneg u s _ (h.child i) _) + rw [abs_le,hD]; unfold R; constructor <;> linarith [HH false,HH true] + have hL₁ := (energy_small h.fit u).1 + have hL₂ : 0 ≤ V := shower_nonneg h + have hF : 0 ≤ J := (fax_floor h).1 + have hQ : G+V ≤ L := by rw [show G+V=_ from (wet_average s h u).symm]; linarith [hp false,hp true] + have hr : R^2 ≤ V*L := by + change 0 ≤ G at hL₁ + have HH := abs_le.mp hH + nlinarith + rw [gust,H false,H true] + simp only [curl,Bool.false_eq_true,↓reduceIte,neg_mul,one_mul] + let a := ∑ z,A z; let b := ∑ z,B z + change ((G+(a+2*b)+D false-(G+ -(a+2*b)+D true))/2)^2 ≤ 100*L*(J+V) + rw [show (G+(a+2*b)+D false-(G+ -(a+2*b)+D true))/2=a+2*b+R by unfold R; ring] + change a^2 ≤ _ at hA; change b^2 ≤ _ at hB + have hG : 0 ≤ G := hL₁ + have hT : (a+2*b+R)^2 ≤ 3*(a^2+(2*b)^2+R^2) := by nlinarith [sq_nonneg (a-2*b),sq_nonneg (2*b-R),sq_nonneg (a-R)] + nlinarith + +lemma factor_bill (s : Scene M) (h : Climb s) (u : Grid M) : + (∑ z : Grid M, ‖factor s u z‖)^2 ≤ + 16*(energy s u* fax s + shower u s) := by + let n := nub s u; let v := meanV s u; let m := matter s; let g := shell s + let G := energy s u; let J := fax s; let V := shower u s + let f (z : Grid M) := |g z| * ‖v z‖; let k (z : Grid M) := ‖n z‖ + have hm := matter_nonneg h.fit + have H (z : Grid M) : ‖factor s u z‖ ≤ f z+k z := by + rw [show factor s u z=(↑(g z):ℂ)*v z+n z by unfold n v g nub; ring] + apply (norm_add_le _ _).trans; rw [norm_scaleC] + have hF : (∑ z,f z)^2 ≤ G*J := by + unfold G J fax; rw [sun_energy h.fit] + apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ + (fun i _ => (sun_rays h.fit _ _).1) (fun i _ => pierce_nonneg h.fit _) (fun i _ => ?_) + change (|shell s i| * ‖v i‖)^2 ≤ matter s i* ‖v i‖^2 * pierce s i + rw [mul_pow,sq_abs,shell_sq h] + exact le_of_eq (by ring) + have hK : (∑ z,k z)^2 ≤ 2*(4*V) := by + apply le_trans (b:=share s*(4*V)) + · rw [← matter_sum h.fit] + unfold V shower; rw [Finset.mul_sum] + apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => hm i) + (fun i _ => mul_nonneg (by norm_num) (dew_nonneg _ h _)) (fun i _ => ?_) + have Hh := nub_innovation _ h u i; change ‖nub s u i‖^2 ≤ _ + linarith + · exact mul_le_mul_of_nonneg_right h.tight (mul_nonneg (by norm_num) (shower_nonneg h)) + let a := ∑ z,f z; let b := ∑ z,k z; let c := ∑ z,‖factor s u z‖ + have he : c ≤ a+b := by unfold c a b; rw [← Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => H i + have hc : 0 ≤ c := Finset.sum_nonneg fun i _ => norm_nonneg _ + change a^2 ≤ _ at hF; change b^2 ≤ _ at hK; change c^2 ≤ 16*(G*J+V) + nlinarith [sq_nonneg (a-b)] + +lemma shell_bill (s : Scene M) (h : Climb s) : + (∑ z,|shell s z|)^2 ≤ 2*fax s := by + apply le_trans (b:=share s*fax s) + · rw [←matter_sum h.fit] + apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ (fun i _ => matter_nonneg h.fit i) + (fun i _ => pierce_nonneg h.fit i) (fun i _ => ?_) + rw [sq_abs,shell_sq h] + exact mul_le_mul_of_nonneg_right h.tight (fax_floor h).1 + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Launch.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Launch.lean index 5b3a905e7ded8e15c2d3180a31f9d564fd3630ba..67ec1d5792d36ca17b6f40799d306e10fdd29008 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Launch.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Launch.lean @@ -1,163 +1,163 @@ -import OAI.Analysis.FourierLLogL.Paths.Encore -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot - -/-- Path additionally follows birth rules. -/ -def Cast (j J : ℕ) (f : Grid M → ℝ) (s : Scene M) : Path M → Prop -| [] => s.Safe j J f -| b::l => b.pre=s ∧ (∃ k, Following j J f (ran b.pre b.post k)) ∧ Cast j J f (winds b) l - -lemma stream_cast {s : Scene M} (v : Thread M s) (h : Following j J f v) - (x : Grid M) : Cast j J f s (v.stream x) := by - induction v with - | blank => exact h - | ran s q k ih => - let b := (step q).bit x - exact ⟨rfl,⟨k,h⟩,ih b (h.2.2.2.2.2 b)⟩ - -def quarrel (p : Plot) (c : Board M) : Path M → Board M -| [] => c -| b::l => quarrel p (turn p c b.post) l -lemma quarrel_chart (p : Plot) (c : Board M) (l : Path M) : - (quarrel p c l).chart = goal p.ix p.main c.chart l := by - induction l generalizing c with - | nil => rfl - | cons b l ih => exact ih _ - -lemma quarrel_concat (p : Plot) (c : Board M) (l r : Path M) : - quarrel p c (l++r)=quarrel p (quarrel p c l) r := by - induction l generalizing c with - | nil => rfl - | cons b l ih => exact ih _ - -def oiled (p : Plot) (c : Board M) (l : Path M) := - cardStep p (quarrel p c l) - -def heroPlot (g : Grade) (l : Path M) := stage g (chooseCap g l) - -lemma oil_hero (g : Grade) (l : Path M) : - oiled (heroPlot g l) (vacant M) l := by - unfold oiled cardStep; rw [quarrel_chart]; exact (choice_bounds g l).2.1 - -lemma still_head {s : Scene M} (l : Path M) (h : Cast j J f s l) - (p : Plot) (c : Board M) (he : Comes c s j J f) : - c.chart.items ⊆ (quarrel p c l).chart.items := by - induction l generalizing s c with - | nil => exact Finset.Subset.refl _ - | cons b l ih => - obtain ⟨rfl,⟨k,hk⟩,ht⟩ := h - apply Finset.Subset.trans _ (ih ht (turn p c b.post) (comes_down p hk he b.bit)) - exact (zoom_spec _ he.valid p.ix p.main hk).extend - -lemma oily_cons {s : Scene M} (p : Plot) {c : Board M} {b : Beat M} {l : Path M} - (h : Cast j J f s (b::l)) (ht : Comes c s j J f) (hv : oiled p c (b::l)) : - boom p c b.post ∧ oiled p (turn p c b.post) l ∧ Comes (turn p c b.post) (winds b) j J f := by - obtain ⟨rfl,⟨k,hk⟩,he⟩ := h - have hi := comes_down p hk ht b.bit - exact ⟨le_trans (Finset.card_le_card (still_head l he p _ hi)) hv,hv,hi⟩ - -lemma birth_just (p : Plot) (c : Board M) (s : Scene M) {b : Beat M} (l r : Path M) - (h : Cast j J f s (l++b::r)) - (ht : Comes c s j J f) (hv : oiled p c (l++b::r)) : - Comes (quarrel p c l) b.pre j J f ∧ - (∃ k, Following j J f (ran b.pre b.post k)) ∧ boom p (quarrel p c l) b.post := by - induction l generalizing s c with - | nil => - exact ⟨h.1.symm ▸ ht,h.2.1,(oily_cons p h ht hv).1⟩ - | cons a l ih => - have H := (oily_cons p h ht hv) - exact ih (turn p c a.post) _ h.2.2 H.2.2 H.2.1 - -lemma still_rec {s : Scene M} (p : Plot) {c : Board M} (b : Beat M) - (l m : Path M) (h : Cast j J f s (l++b::m)) (he : Comes c s j J f) - (hv : oiled p c (l++b::m)) (q : Scene M) - (hh : (turn p (quarrel p c l) b.post).previous=some q) - (x : Grid M) (hx : x∈net q j p.ix p.main) : - c.previous=some q ∨ ∃ u w : Path M, l=u++w ∧ - recital (grade p) x (w++(b::m)) ≤ showScore p c j (l++b::m) := by - - - induction l generalizing s c with - | nil => - have H := oily_cons p h he hv - change (turn p c b.post).previous=some q at hh - rw [prior_step _ _ _ H.1] at hh - by_cases hi : p.freshAt c b.post - · rw [ite_eq_left hi] at hh - cases Option.some.inj hh - refine Or.inr ⟨[],[],rfl,?_⟩ - change recital _ _ (b::m) ≤ _ - simp only [List.nil_append,showScore,ite_eq_left H.1,ite_eq_left hi] - apply le_trans (b:=burst p b.post j (b::m)) - · exact Finset.single_le_sum (f:=fun x => recital (grade p) x (b::m)) - (fun x _ => le_trans (by norm_num) (rec_one _ _ _)) hx - linarith [show_one p (turn p c b.post) j m] - · exact Or.inl ((ite_eq_right hi).symm.trans hh) - | cons a l ih => - let d := turn p c a.post - have H := oily_cons p h he hv - have HE : Cast j J f (winds a) (l++b::m) := h.2.2 - rcases ih (c:=d) HE H.2.2 H.2.1 hh with hv|⟨u,w,hw,hv⟩ - · unfold d at hv; rw [prior_step p c a.post H.1] at hv - by_cases hi : freshAt p c a.post - · rw [ite_eq_left hi] at hv - cases Option.some.inj hv - refine Or.inr ⟨[],a::l,rfl,?_⟩ - let r := a::(l++b::m) - change recital _ _ r ≤ showScore _ _ _ (a::(l++b::m)) - simp only [showScore,ite_eq_left H.1,ite_eq_left hi] - apply le_trans (b:=burst p a.post j r) - · exact Finset.single_le_sum (f:=fun x => recital (grade p) x r) - (fun x _ => le_trans (by norm_num) (rec_one _ _ _)) hx - linarith [show_one p (turn p c a.post) j (l++b::m)] - · exact Or.inl ((ite_eq_right hi).symm.trans hv) - · apply Or.inr; refine ⟨a::u,w,by rw [hw]; rfl,?_⟩ - change _ ≤ showScore p c j (a::(l++b::m)) - rw [showScore,ite_eq_left H.1]; apply hv.trans - apply le_add_of_nonneg_left; split; apply burst_pos; norm_num - -def ringing (u : Grid M) (p : Plot) (j : ℕ) (l : Path M) (b : Beat M) := - p.catches b.post (quarrel p (vacant M) l) j u - -lemma live_rec : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ (u : Grid M) (g : Grade) (l r : Path M) (b : Beat M), - let L := l++b::r - let p := heroPlot g L - Cast j J f s L → entitled g s j → ringing u p j l b → - ∃ x : Grid M, p.aligned b.post j u x ∧ ∃ a k : Path M, l=a++k ∧ - 2*log (recital g x (k++b::r)) ≤ timex g j L := by - filter_upwards [currently] with j h M J s f u g l r b L p hC hE hp - let c := vacant M - have HC := vacant_comes (j:=j) (J:=J) (f:=f) s - have He := oil_hero g L - obtain ⟨ha,⟨k,hk⟩,hK⟩ := birth_just p c s l r hC HC He - obtain ⟨q,H,hQ,w,hw,hW⟩ := h p (fun hx => (hE hx).1) M J f b.pre b.post k hk _ ha hK u hp - refine ⟨w,hW,?_⟩ - rcases still_rec p b l r hC HC He q H w hw with h1|⟨v,z,hz,ht⟩ - · cases h1 - refine ⟨v,z,hz,?_⟩ - apply le_trans _ (timex_scores g j L).2 - apply mul_le_mul_of_nonneg_left _ (by norm_num) - exact log_le_log (lt_of_lt_of_le (by norm_num) (rec_one g _ _)) ht - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Encore +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot + +/-- Path additionally follows birth rules. -/ +def Cast (j J : ℕ) (f : Grid M → ℝ) (s : Scene M) : Path M → Prop +| [] => s.Safe j J f +| b::l => b.pre=s ∧ (∃ k, Following j J f (ran b.pre b.post k)) ∧ Cast j J f (winds b) l + +lemma stream_cast {s : Scene M} (v : Thread M s) (h : Following j J f v) + (x : Grid M) : Cast j J f s (v.stream x) := by + induction v with + | blank => exact h + | ran s q k ih => + let b := (step q).bit x + exact ⟨rfl,⟨k,h⟩,ih b (h.2.2.2.2.2 b)⟩ + +def quarrel (p : Plot) (c : Board M) : Path M → Board M +| [] => c +| b::l => quarrel p (turn p c b.post) l +lemma quarrel_chart (p : Plot) (c : Board M) (l : Path M) : + (quarrel p c l).chart = goal p.ix p.main c.chart l := by + induction l generalizing c with + | nil => rfl + | cons b l ih => exact ih _ + +lemma quarrel_concat (p : Plot) (c : Board M) (l r : Path M) : + quarrel p c (l++r)=quarrel p (quarrel p c l) r := by + induction l generalizing c with + | nil => rfl + | cons b l ih => exact ih _ + +def oiled (p : Plot) (c : Board M) (l : Path M) := + cardStep p (quarrel p c l) + +def heroPlot (g : Grade) (l : Path M) := stage g (chooseCap g l) + +lemma oil_hero (g : Grade) (l : Path M) : + oiled (heroPlot g l) (vacant M) l := by + unfold oiled cardStep; rw [quarrel_chart]; exact (choice_bounds g l).2.1 + +lemma still_head {s : Scene M} (l : Path M) (h : Cast j J f s l) + (p : Plot) (c : Board M) (he : Comes c s j J f) : + c.chart.items ⊆ (quarrel p c l).chart.items := by + induction l generalizing s c with + | nil => exact Finset.Subset.refl _ + | cons b l ih => + obtain ⟨rfl,⟨k,hk⟩,ht⟩ := h + apply Finset.Subset.trans _ (ih ht (turn p c b.post) (comes_down p hk he b.bit)) + exact (zoom_spec _ he.valid p.ix p.main hk).extend + +lemma oily_cons {s : Scene M} (p : Plot) {c : Board M} {b : Beat M} {l : Path M} + (h : Cast j J f s (b::l)) (ht : Comes c s j J f) (hv : oiled p c (b::l)) : + boom p c b.post ∧ oiled p (turn p c b.post) l ∧ Comes (turn p c b.post) (winds b) j J f := by + obtain ⟨rfl,⟨k,hk⟩,he⟩ := h + have hi := comes_down p hk ht b.bit + exact ⟨le_trans (Finset.card_le_card (still_head l he p _ hi)) hv,hv,hi⟩ + +lemma birth_just (p : Plot) (c : Board M) (s : Scene M) {b : Beat M} (l r : Path M) + (h : Cast j J f s (l++b::r)) + (ht : Comes c s j J f) (hv : oiled p c (l++b::r)) : + Comes (quarrel p c l) b.pre j J f ∧ + (∃ k, Following j J f (ran b.pre b.post k)) ∧ boom p (quarrel p c l) b.post := by + induction l generalizing s c with + | nil => + exact ⟨h.1.symm ▸ ht,h.2.1,(oily_cons p h ht hv).1⟩ + | cons a l ih => + have H := (oily_cons p h ht hv) + exact ih (turn p c a.post) _ h.2.2 H.2.2 H.2.1 + +lemma still_rec {s : Scene M} (p : Plot) {c : Board M} (b : Beat M) + (l m : Path M) (h : Cast j J f s (l++b::m)) (he : Comes c s j J f) + (hv : oiled p c (l++b::m)) (q : Scene M) + (hh : (turn p (quarrel p c l) b.post).previous=some q) + (x : Grid M) (hx : x∈net q j p.ix p.main) : + c.previous=some q ∨ ∃ u w : Path M, l=u++w ∧ + recital (grade p) x (w++(b::m)) ≤ showScore p c j (l++b::m) := by + + + induction l generalizing s c with + | nil => + have H := oily_cons p h he hv + change (turn p c b.post).previous=some q at hh + rw [prior_step _ _ _ H.1] at hh + by_cases hi : p.freshAt c b.post + · rw [ite_eq_left hi] at hh + cases Option.some.inj hh + refine Or.inr ⟨[],[],rfl,?_⟩ + change recital _ _ (b::m) ≤ _ + simp only [List.nil_append,showScore,ite_eq_left H.1,ite_eq_left hi] + apply le_trans (b:=burst p b.post j (b::m)) + · exact Finset.single_le_sum (f:=fun x => recital (grade p) x (b::m)) + (fun x _ => le_trans (by norm_num) (rec_one _ _ _)) hx + linarith [show_one p (turn p c b.post) j m] + · exact Or.inl ((ite_eq_right hi).symm.trans hh) + | cons a l ih => + let d := turn p c a.post + have H := oily_cons p h he hv + have HE : Cast j J f (winds a) (l++b::m) := h.2.2 + rcases ih (c:=d) HE H.2.2 H.2.1 hh with hv|⟨u,w,hw,hv⟩ + · unfold d at hv; rw [prior_step p c a.post H.1] at hv + by_cases hi : freshAt p c a.post + · rw [ite_eq_left hi] at hv + cases Option.some.inj hv + refine Or.inr ⟨[],a::l,rfl,?_⟩ + let r := a::(l++b::m) + change recital _ _ r ≤ showScore _ _ _ (a::(l++b::m)) + simp only [showScore,ite_eq_left H.1,ite_eq_left hi] + apply le_trans (b:=burst p a.post j r) + · exact Finset.single_le_sum (f:=fun x => recital (grade p) x r) + (fun x _ => le_trans (by norm_num) (rec_one _ _ _)) hx + linarith [show_one p (turn p c a.post) j (l++b::m)] + · exact Or.inl ((ite_eq_right hi).symm.trans hv) + · apply Or.inr; refine ⟨a::u,w,by rw [hw]; rfl,?_⟩ + change _ ≤ showScore p c j (a::(l++b::m)) + rw [showScore,ite_eq_left H.1]; apply hv.trans + apply le_add_of_nonneg_left; split; apply burst_pos; norm_num + +def ringing (u : Grid M) (p : Plot) (j : ℕ) (l : Path M) (b : Beat M) := + p.catches b.post (quarrel p (vacant M) l) j u + +lemma live_rec : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ (u : Grid M) (g : Grade) (l r : Path M) (b : Beat M), + let L := l++b::r + let p := heroPlot g L + Cast j J f s L → entitled g s j → ringing u p j l b → + ∃ x : Grid M, p.aligned b.post j u x ∧ ∃ a k : Path M, l=a++k ∧ + 2*log (recital g x (k++b::r)) ≤ timex g j L := by + filter_upwards [currently] with j h M J s f u g l r b L p hC hE hp + let c := vacant M + have HC := vacant_comes (j:=j) (J:=J) (f:=f) s + have He := oil_hero g L + obtain ⟨ha,⟨k,hk⟩,hK⟩ := birth_just p c s l r hC HC He + obtain ⟨q,H,hQ,w,hw,hW⟩ := h p (fun hx => (hE hx).1) M J f b.pre b.post k hk _ ha hK u hp + refine ⟨w,hW,?_⟩ + rcases still_rec p b l r hC HC He q H w hw with h1|⟨v,z,hz,ht⟩ + · cases h1 + refine ⟨v,z,hz,?_⟩ + apply le_trans _ (timex_scores g j L).2 + apply mul_le_mul_of_nonneg_left _ (by norm_num) + exact log_le_log (lt_of_lt_of_le (by norm_num) (rec_one g _ _)) ht + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Letters.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Letters.lean index 930a35e41bc6fd1f8a5c993806f6acaaca5eb0db..1c9abd40fe36f1b472f29f56e13303fabab727b2 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Letters.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Letters.lean @@ -1,137 +1,137 @@ -import OAI.Analysis.FourierLLogL.Paths.Trip -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -abbrev Vector (M : ℕ) := Grid M → ℂ - -def dye (s : Scene M) := s.fund.mood -def matter (s : Scene M) := hist s (channel (dye s)) -def letter (s : Scene M) (u z : Grid M) := fz s.here s.wage s.norm (dye s) u z - -def shell (s : Scene M) := Procedure.diff s (channel (dye s)) -def factor (s : Scene M) (u z : Grid M) := - (letter (s.smaller false) u z-letter (s.smaller true) u z)/2 -def gain (s : Scene M) (u : Grid M) := - (scalar (s.smaller false) u-scalar (s.smaller true) u)/2 -def fax (s : Scene M) := fisher s (channel (dye s)) - -lemma matter_nonneg {s : Scene M} (h : Fit s) (z) : 0 ≤ matter s z := - nonnegative_hist s h.ready.2 h.pos.le _ _ -lemma matter_sum {s : Scene M} (h : Fit s) : ∑ z, matter s z=share s := - total_hist _ h.ready.1 _ -lemma matter_norm {s : Scene M} (h : Fit s) (u z) : ‖letter s u z‖ ≤ matter s z := - fz_norm _ h.ready.2 h.pos.le _ _ _ -lemma scalar_label (s : Scene M) (u : Grid M) : scalar s u=∑ z, letter s u z := - scalar_series s _ u - -lemma scalar_norm {s : Scene M} (h : Fit s) (u) : ‖scalar s u‖ ≤ share s := by - rw [scalar_label, ← matter_sum h] - exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => matter_norm h u i) - -lemma matter_split {s : Scene M} (h : Climb s) (z : Grid M) : - matter (s.smaller false) z+matter (s.smaller true) z=2*matter s z := - hist_split s h.fit.ready.1.1 h.deep _ _ - -lemma letter_split {s : Scene M} (h : Climb s) (u z : Grid M) : - letter (s.smaller false) u z+letter (s.smaller true) u z=2*letter s u z := - fz_split s h.fit.ready.1.1 h.deep _ _ _ - -lemma scalar_split' {s : Scene M} (h : Climb s) (u) : - scalar (s.smaller false) u+scalar (s.smaller true) u=2*scalar s u := by - simp_rw [scalar_label,←Finset.sum_add_distrib,Finset.mul_sum,letter_split h] - -lemma matter_jump {s : Scene M} (h : Climb s) (b) (z) : - matter (s.smaller b) z=matter s z+curl b* shell s z := by - have hi := matter_split h z - have H : shell s z=(matter (s.smaller false) z-matter (s.smaller true) z)/2 := rfl - cases b <;> simp [H,curl] <;> linarith - -private lemma symmetric_curl (f : Bool → ℂ) (a : ℂ) - (h : f false+f true=2*a) (b : Bool) : - f b=a+(curl b:ℂ)*((f false-f true)/2) := by - rw [show a=(f false+f true)/2 by rw [h]; ring] - cases b <;> simp only [curl,Bool.false_eq_true,↓reduceIte] - all_goals push_cast; ring - -lemma letter_jump {s : Scene M} (h : Climb s) (b) (u z : Grid M) : - letter (s.smaller b) u z=letter s u z+(curl b:ℂ)*factor s u z := - symmetric_curl (fun b => letter (s.smaller b) u z) _ (letter_split h u z) _ -lemma scalar_jump {s : Scene M} (h : Climb s) (b u) : - scalar (s.smaller b) u=scalar s u+(curl b:ℂ)*gain s u := - symmetric_curl (fun b => scalar (s.smaller b) u) _ (scalar_split' h u) _ - -lemma matter_drain {s q : Scene M} (hh : Ideal s q) (u z : Grid M) : - ‖letter s u z-letter q u z‖ ≤ matter s z-matter q z := by - classical - unfold matter hist letter fz - unfold omegaObs dye wt - rw [hh.lower.here,hh.lower.flat,hh.lower.fund,← Finset.sum_sub_distrib,← Finset.sum_sub_distrib] - apply le_trans (norm_sum_le _ _) - apply Finset.sum_le_sum; intro i _ - rw [← sub_mul,← Complex.ofReal_sub,norm_mul,Complex.norm_real,Real.norm_eq_abs,norm_chi,mul_one] - refine le_of_eq (abs_of_nonneg (sub_nonneg.mpr ?_)) - apply mul_le_mul_of_nonneg_right _ ((channel _ _).nonneg _) - apply div_le_div_of_nonneg_right _ hh.fit.pos.le - apply mul_le_mul_of_nonneg_left (hh.lower.antitone hh.fit.ready.2 i) ((s.here.dice M).nonneg _) - -lemma scalar_drain {b : Beat M} (h : Ideal b.pre b.post) (u) : - ‖scalar b.pre u-scalar b.post u‖ ≤ killed b := by - simp_rw [scalar_label] - rw [h.kill_eq,← matter_sum h.fit,← matter_sum h.climb.fit,← Finset.sum_sub_distrib,← Finset.sum_sub_distrib] - exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => matter_drain h u i) - -lemma shell_abs {s : Scene M} (h : Climb s) (z) : - |shell s z| ≤ matter s z := by - have H (b) := matter_nonneg (h.child b) z - simp_rw [matter_jump h] at H - rw [abs_le] - constructor <;> linarith [show 0 ≤ matter s z+1*shell s z from H false, - show 0 ≤ matter s z+-1*shell s z from H true] - -lemma norm_halfcomplex (x y : ℂ) : - ‖(x-y)/2‖ ≤ (‖x‖+‖y‖)/2 := by - rw [norm_div]; norm_num; gcongr; exact norm_sub_le x y - -lemma factor_norm {s : Scene M} (h : Climb s) (u z : Grid M) : - ‖factor s u z‖ ≤ matter s z := by - have H (b) := matter_norm (h.child b) u z - apply (norm_halfcomplex _ _).trans - linarith [matter_split h z,H true,H false] - -def billFax (b : Beat M) := fax b.post -def fullFax (l : Path M) := subtotal billFax l - -lemma fax_floor {s : Scene M} (h : Climb s) : 0 ≤ fax s ∧ fax s ≤ 2 := - let H := fisher_range s h.fit.ready.1.1 h.deep h.fit.ready.2 h.fit.pos.le (channel (dye s)) - ⟨H.1,H.2.trans h.tight⟩ - -lemma trip_fax {x : Grid M} {l : Path M} (h : Trip x s l) : 0 ≤ fullFax l := - trip_nonneg h (fun _ hh => (fax_floor hh.climb).1) - -/-- Normalized scalar transform along part (plateau jumps) on path x. -/ -def proceeds (x u : Grid M) (b : Beat M) := - (blow x b:ℂ)*gain b.post u -def duties (x u : Grid M) (b : Beat M) (z : Grid M) := - (blow x b:ℂ)*factor b.post u z - -lemma gain_series (u : Grid M) (s : Scene M) : gain s u = ∑ z, factor s u z := by - simp_rw [gain,factor, ← Finset.sum_div, Finset.sum_sub_distrib,scalar_label] -lemma gain_norm {s : Scene M} (h : Climb s) (u : Grid M) : ‖gain s u‖ ≤ share s := by - rw [gain_series,← matter_sum h.fit] - exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => factor_norm h u i) - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Trip +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +abbrev Vector (M : ℕ) := Grid M → ℂ + +def dye (s : Scene M) := s.fund.mood +def matter (s : Scene M) := hist s (channel (dye s)) +def letter (s : Scene M) (u z : Grid M) := fz s.here s.wage s.norm (dye s) u z + +def shell (s : Scene M) := Procedure.diff s (channel (dye s)) +def factor (s : Scene M) (u z : Grid M) := + (letter (s.smaller false) u z-letter (s.smaller true) u z)/2 +def gain (s : Scene M) (u : Grid M) := + (scalar (s.smaller false) u-scalar (s.smaller true) u)/2 +def fax (s : Scene M) := fisher s (channel (dye s)) + +lemma matter_nonneg {s : Scene M} (h : Fit s) (z) : 0 ≤ matter s z := + nonnegative_hist s h.ready.2 h.pos.le _ _ +lemma matter_sum {s : Scene M} (h : Fit s) : ∑ z, matter s z=share s := + total_hist _ h.ready.1 _ +lemma matter_norm {s : Scene M} (h : Fit s) (u z) : ‖letter s u z‖ ≤ matter s z := + fz_norm _ h.ready.2 h.pos.le _ _ _ +lemma scalar_label (s : Scene M) (u : Grid M) : scalar s u=∑ z, letter s u z := + scalar_series s _ u + +lemma scalar_norm {s : Scene M} (h : Fit s) (u) : ‖scalar s u‖ ≤ share s := by + rw [scalar_label, ← matter_sum h] + exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => matter_norm h u i) + +lemma matter_split {s : Scene M} (h : Climb s) (z : Grid M) : + matter (s.smaller false) z+matter (s.smaller true) z=2*matter s z := + hist_split s h.fit.ready.1.1 h.deep _ _ + +lemma letter_split {s : Scene M} (h : Climb s) (u z : Grid M) : + letter (s.smaller false) u z+letter (s.smaller true) u z=2*letter s u z := + fz_split s h.fit.ready.1.1 h.deep _ _ _ + +lemma scalar_split' {s : Scene M} (h : Climb s) (u) : + scalar (s.smaller false) u+scalar (s.smaller true) u=2*scalar s u := by + simp_rw [scalar_label,←Finset.sum_add_distrib,Finset.mul_sum,letter_split h] + +lemma matter_jump {s : Scene M} (h : Climb s) (b) (z) : + matter (s.smaller b) z=matter s z+curl b* shell s z := by + have hi := matter_split h z + have H : shell s z=(matter (s.smaller false) z-matter (s.smaller true) z)/2 := rfl + cases b <;> simp [H,curl] <;> linarith + +private lemma symmetric_curl (f : Bool → ℂ) (a : ℂ) + (h : f false+f true=2*a) (b : Bool) : + f b=a+(curl b:ℂ)*((f false-f true)/2) := by + rw [show a=(f false+f true)/2 by rw [h]; ring] + cases b <;> simp only [curl,Bool.false_eq_true,↓reduceIte] + all_goals push_cast; ring + +lemma letter_jump {s : Scene M} (h : Climb s) (b) (u z : Grid M) : + letter (s.smaller b) u z=letter s u z+(curl b:ℂ)*factor s u z := + symmetric_curl (fun b => letter (s.smaller b) u z) _ (letter_split h u z) _ +lemma scalar_jump {s : Scene M} (h : Climb s) (b u) : + scalar (s.smaller b) u=scalar s u+(curl b:ℂ)*gain s u := + symmetric_curl (fun b => scalar (s.smaller b) u) _ (scalar_split' h u) _ + +lemma matter_drain {s q : Scene M} (hh : Ideal s q) (u z : Grid M) : + ‖letter s u z-letter q u z‖ ≤ matter s z-matter q z := by + classical + unfold matter hist letter fz + unfold omegaObs dye wt + rw [hh.lower.here,hh.lower.flat,hh.lower.fund,← Finset.sum_sub_distrib,← Finset.sum_sub_distrib] + apply le_trans (norm_sum_le _ _) + apply Finset.sum_le_sum; intro i _ + rw [← sub_mul,← Complex.ofReal_sub,norm_mul,Complex.norm_real,Real.norm_eq_abs,norm_chi,mul_one] + refine le_of_eq (abs_of_nonneg (sub_nonneg.mpr ?_)) + apply mul_le_mul_of_nonneg_right _ ((channel _ _).nonneg _) + apply div_le_div_of_nonneg_right _ hh.fit.pos.le + apply mul_le_mul_of_nonneg_left (hh.lower.antitone hh.fit.ready.2 i) ((s.here.dice M).nonneg _) + +lemma scalar_drain {b : Beat M} (h : Ideal b.pre b.post) (u) : + ‖scalar b.pre u-scalar b.post u‖ ≤ killed b := by + simp_rw [scalar_label] + rw [h.kill_eq,← matter_sum h.fit,← matter_sum h.climb.fit,← Finset.sum_sub_distrib,← Finset.sum_sub_distrib] + exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => matter_drain h u i) + +lemma shell_abs {s : Scene M} (h : Climb s) (z) : + |shell s z| ≤ matter s z := by + have H (b) := matter_nonneg (h.child b) z + simp_rw [matter_jump h] at H + rw [abs_le] + constructor <;> linarith [show 0 ≤ matter s z+1*shell s z from H false, + show 0 ≤ matter s z+-1*shell s z from H true] + +lemma norm_halfcomplex (x y : ℂ) : + ‖(x-y)/2‖ ≤ (‖x‖+‖y‖)/2 := by + rw [norm_div]; norm_num; gcongr; exact norm_sub_le x y + +lemma factor_norm {s : Scene M} (h : Climb s) (u z : Grid M) : + ‖factor s u z‖ ≤ matter s z := by + have H (b) := matter_norm (h.child b) u z + apply (norm_halfcomplex _ _).trans + linarith [matter_split h z,H true,H false] + +def billFax (b : Beat M) := fax b.post +def fullFax (l : Path M) := subtotal billFax l + +lemma fax_floor {s : Scene M} (h : Climb s) : 0 ≤ fax s ∧ fax s ≤ 2 := + let H := fisher_range s h.fit.ready.1.1 h.deep h.fit.ready.2 h.fit.pos.le (channel (dye s)) + ⟨H.1,H.2.trans h.tight⟩ + +lemma trip_fax {x : Grid M} {l : Path M} (h : Trip x s l) : 0 ≤ fullFax l := + trip_nonneg h (fun _ hh => (fax_floor hh.climb).1) + +/-- Normalized scalar transform along part (plateau jumps) on path x. -/ +def proceeds (x u : Grid M) (b : Beat M) := + (blow x b:ℂ)*gain b.post u +def duties (x u : Grid M) (b : Beat M) (z : Grid M) := + (blow x b:ℂ)*factor b.post u z + +lemma gain_series (u : Grid M) (s : Scene M) : gain s u = ∑ z, factor s u z := by + simp_rw [gain,factor, ← Finset.sum_div, Finset.sum_sub_distrib,scalar_label] +lemma gain_norm {s : Scene M} (h : Climb s) (u : Grid M) : ‖gain s u‖ ≤ share s := by + rw [gain_series,← matter_sum h.fit] + exact (norm_sum_le _ _).trans (Finset.sum_le_sum fun i _ => factor_norm h u i) + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Ray.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Ray.lean index 751e8027b2870e38fe3cf01101052a788eb81e96..3a3ed5d33b60ba617facf4b633361397d008aef1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Ray.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Ray.lean @@ -1,148 +1,148 @@ -import OAI.Analysis.FourierLLogL.Paths.Gust -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Beam -def content (v w : ℂ) (x : ℝ) := ‖v‖^2+‖w‖^2+x^2 -def ray (v w : ℂ) (x : ℝ) := √(content v w x) - -lemma content_pos (v w : ℂ) (x : ℝ) : 0 ≤ content v w x := by unfold content; positivity -lemma ray_pos (v w : ℂ) (x : ℝ) : 0 ≤ ray v w x := sqrt_nonneg _ -lemma ray_sq (v w : ℂ) (x : ℝ) : ray v w x^2=content v w x := - Real.sq_sqrt (content_pos _ _ _) -lemma ray_bounds (v w : ℂ) (x : ℝ) : let r := ray v w x - ‖v‖ ≤ r ∧ ‖w‖ ≤ r ∧ |x| ≤ r ∧ r ≤ ‖v‖+‖w‖+|x| := by - intro r - have hr : 0 ≤ r := ray_pos _ _ _ - have HH : r^2=‖v‖^2+‖w‖^2+x^2 := ray_sq _ _ _ - have hv := norm_nonneg v; have hw := norm_nonneg w; have hx := abs_nonneg x - refine ⟨?_,?_,?_,?_⟩ <;> nlinarith [sq_abs x] - -/-- Unit linearization of an output triple -/ -def glow (v w : ℂ) (x : ℝ) (a b : ℂ) (t : ℝ) := - (duet v a+duet w b+x*t) / ray v w x - -lemma glow_add (v w : ℂ) (x : ℝ) (a b c d : ℂ) (h k : ℝ) : - glow v w x (a+c) (b+d) (h+k)=glow v w x a b h+glow v w x c d k := by - rw [glow,glow,glow,duet_add,duet_add]; ring -lemma glow_scale (v w : ℂ) (x y h : ℝ) (a b : ℂ) : - glow v w x ((y:ℂ)*a) ((y:ℂ)*b) (y*h)=y*glow v w x a b h := by - rw [glow,glow,duet_scale,duet_scale]; ring - -lemma glow_limit (v w : ℂ) (x : ℝ) (a b : ℂ) (t : ℝ) : - |glow v w x a b t| ≤ ‖a‖+‖b‖+|t| := by - let r := ray v w x - have H := ray_bounds v w x - have hr : 0 ≤ r := ray_pos v w x - unfold glow - rcases eq_or_lt_of_le hr with h|h - · change |_/r| ≤ _ - rw [←h,div_zero,abs_zero]; positivity - change |_/r| ≤ _ - rw [abs_div,abs_of_nonneg hr] - apply (div_le_iff₀ h).mpr - have he := abs_add_le (duet v a+duet w b) (x*t) - have hi := abs_add_le (duet v a) (duet w b) - rw [abs_mul] at he - change ‖v‖ ≤ r ∧ _ at H - have h1 : ‖v‖ ≤ r := H.1 - have h2 : ‖w‖ ≤ r := H.2.1 - have h3 : |x| ≤ r := H.2.2.1 - nlinarith [norm_nonneg a,norm_nonneg b,abs_nonneg t,duet_limit v a,duet_limit w b] - -lemma ray_shift (v w a b : ℂ) (x t : ℝ) (C : ℝ) (hc : 0 ≤ C) - (h : content a b t ≤ ray v w x*C) : - ray (v+a) (w+b) (x+t) ≤ ray v w x+glow v w x a b t+C := by - let r := ray v w x; let d := glow v w x a b t - rcases eq_or_lt_of_le (show 0 ≤ r from ray_pos _ _ _) with he|he - · have hh : content a b t ≤ 0 := by change _ ≤ r*C at h; rwa [←he,zero_mul] at h - unfold content at hh - have hA : a=0 := norm_le_zero_iff.mp (by nlinarith) - have hB : b=0 := norm_le_zero_iff.mp (by nlinarith) - have hT : t=0 := by nlinarith - simp [hA,hB,hT,glow,duet_zero,hc] - · let R := ray (v+a) (w+b) (x+t) - have hH : R^2=r^2+2*r*d+content a b t := by - have H : r*d=duet v a+duet w b+x*t := by - unfold d glow; change r*(_/r)=_ - field_simp - rw [show 2*r*d=2*(r*d) by ring,H] - unfold R r - rw [ray_sq,ray_sq,content,content,content,duet_inc,duet_inc] - ring - change R ≤ r+d+C - change _ ≤ r*C at h - nlinarith [sq_nonneg (R-r)] - -lemma ray_slide (v w : ℂ) (x : ℝ) (a : ℂ) (t : ℝ) : - ray v (w+a) (x+t) ≤ ray v w x + ‖a‖ + |t| := by - let r := ray v w x; let d := ray v (w+a) (x+t) - have H := ray_bounds v w x - have hR : r^2 = ‖v‖^2+‖w‖^2+x^2 := ray_sq _ _ _ - have hr : 0 ≤ r := ray_pos _ _ _ - have hd : d^2=‖v‖^2+‖w+a‖^2+(x+t)^2 := ray_sq _ _ _ - have hi : ‖w‖ ≤ r := H.2.1 - have Hq : |x| ≤ r := H.2.2.1 - change d ≤ _ - have HH : ‖w+a‖^2 ≤ (‖w‖+‖a‖)^2 := pow_le_pow_left₀ (norm_nonneg _) (norm_add_le w a) _ - have Ht : x*t ≤ r*|t| := (le_abs_self _).trans - (by rw [abs_mul]; exact mul_le_mul_of_nonneg_right Hq (abs_nonneg t)) - change d ≤ r+_ - + |t| - nlinarith [sq_abs t,norm_nonneg a,abs_nonneg t] - - -def prism (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) (z : Grid M) := - ray (W z) (letter s u z) (a*matter s z) -def watt (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) := ∑ z, prism s u W a z -def yieldW (W : Paths.Vector M) := ∑ i : Grid M, ‖W i‖ -lemma yield_step (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) : - yieldW W ≤ watt s u W a := Finset.sum_le_sum fun i _ => (ray_bounds (W i) _ _).1 - -lemma watt_idle {s : Scene M} (h : Fit s) (u : Grid M) (a : ℝ) (ha : 0 ≤ a) (hx : a ≤ 1) : - watt s u 0 a ≤ 8 := by - apply le_trans (b:=∑ i,2*matter s i) - · apply Finset.sum_le_sum; intro i _ - change ray 0 _ _ ≤ _ - apply le_trans (ray_bounds _ _ _).2.2.2 - have HM := matter_norm h u i; have hM := matter_nonneg h i - rw [abs_of_nonneg (mul_nonneg ha hM),norm_zero] - nlinarith - rw [←Finset.mul_sum,matter_sum h] - linarith [h.lift] - -lemma watt_slide {b : Beat M} (h : Ideal b.pre b.post) (u : Grid M) (W : Paths.Vector M) (a : ℝ) - (ha : 0 ≤ a) (he : a ≤ 1) : - watt b.post u W a ≤ watt b.pre u W a + 2*killed b := by - let f := letter b.pre u; let g := letter b.post u - let p := matter b.pre; let q := matter b.post - rw [h.kill_eq,←matter_sum h.fit,←matter_sum h.climb.fit,← Finset.sum_sub_distrib, - Finset.mul_sum,watt,watt,← Finset.sum_add_distrib] - apply Finset.sum_le_sum; intro i _ - change ray (W i) (g i) (a*q i) ≤ ray (W i) (f i) (a*p i)+_ - conv_lhs => - rw [show g i=f i+(g i-f i) by ring,show a*q i=a*p i+a*(q i-p i) by ring] - apply le_trans (ray_slide (W i) _ _ _ _) - have H : ‖f i-g i‖ ≤ p i-q i := matter_drain h u i - have HH := (norm_nonneg _).trans H - rw [abs_mul,abs_of_nonneg ha,abs_sub_comm,abs_of_nonneg HH,norm_sub_rev] - change _ ≤ _+2*(p i-q i) - nlinarith - -end Beam - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Gust +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Beam +def content (v w : ℂ) (x : ℝ) := ‖v‖^2+‖w‖^2+x^2 +def ray (v w : ℂ) (x : ℝ) := √(content v w x) + +lemma content_pos (v w : ℂ) (x : ℝ) : 0 ≤ content v w x := by unfold content; positivity +lemma ray_pos (v w : ℂ) (x : ℝ) : 0 ≤ ray v w x := sqrt_nonneg _ +lemma ray_sq (v w : ℂ) (x : ℝ) : ray v w x^2=content v w x := + Real.sq_sqrt (content_pos _ _ _) +lemma ray_bounds (v w : ℂ) (x : ℝ) : let r := ray v w x + ‖v‖ ≤ r ∧ ‖w‖ ≤ r ∧ |x| ≤ r ∧ r ≤ ‖v‖+‖w‖+|x| := by + intro r + have hr : 0 ≤ r := ray_pos _ _ _ + have HH : r^2=‖v‖^2+‖w‖^2+x^2 := ray_sq _ _ _ + have hv := norm_nonneg v; have hw := norm_nonneg w; have hx := abs_nonneg x + refine ⟨?_,?_,?_,?_⟩ <;> nlinarith [sq_abs x] + +/-- Unit linearization of an output triple -/ +def glow (v w : ℂ) (x : ℝ) (a b : ℂ) (t : ℝ) := + (duet v a+duet w b+x*t) / ray v w x + +lemma glow_add (v w : ℂ) (x : ℝ) (a b c d : ℂ) (h k : ℝ) : + glow v w x (a+c) (b+d) (h+k)=glow v w x a b h+glow v w x c d k := by + rw [glow,glow,glow,duet_add,duet_add]; ring +lemma glow_scale (v w : ℂ) (x y h : ℝ) (a b : ℂ) : + glow v w x ((y:ℂ)*a) ((y:ℂ)*b) (y*h)=y*glow v w x a b h := by + rw [glow,glow,duet_scale,duet_scale]; ring + +lemma glow_limit (v w : ℂ) (x : ℝ) (a b : ℂ) (t : ℝ) : + |glow v w x a b t| ≤ ‖a‖+‖b‖+|t| := by + let r := ray v w x + have H := ray_bounds v w x + have hr : 0 ≤ r := ray_pos v w x + unfold glow + rcases eq_or_lt_of_le hr with h|h + · change |_/r| ≤ _ + rw [←h,div_zero,abs_zero]; positivity + change |_/r| ≤ _ + rw [abs_div,abs_of_nonneg hr] + apply (div_le_iff₀ h).mpr + have he := abs_add_le (duet v a+duet w b) (x*t) + have hi := abs_add_le (duet v a) (duet w b) + rw [abs_mul] at he + change ‖v‖ ≤ r ∧ _ at H + have h1 : ‖v‖ ≤ r := H.1 + have h2 : ‖w‖ ≤ r := H.2.1 + have h3 : |x| ≤ r := H.2.2.1 + nlinarith [norm_nonneg a,norm_nonneg b,abs_nonneg t,duet_limit v a,duet_limit w b] + +lemma ray_shift (v w a b : ℂ) (x t : ℝ) (C : ℝ) (hc : 0 ≤ C) + (h : content a b t ≤ ray v w x*C) : + ray (v+a) (w+b) (x+t) ≤ ray v w x+glow v w x a b t+C := by + let r := ray v w x; let d := glow v w x a b t + rcases eq_or_lt_of_le (show 0 ≤ r from ray_pos _ _ _) with he|he + · have hh : content a b t ≤ 0 := by change _ ≤ r*C at h; rwa [←he,zero_mul] at h + unfold content at hh + have hA : a=0 := norm_le_zero_iff.mp (by nlinarith) + have hB : b=0 := norm_le_zero_iff.mp (by nlinarith) + have hT : t=0 := by nlinarith + simp [hA,hB,hT,glow,duet_zero,hc] + · let R := ray (v+a) (w+b) (x+t) + have hH : R^2=r^2+2*r*d+content a b t := by + have H : r*d=duet v a+duet w b+x*t := by + unfold d glow; change r*(_/r)=_ + field_simp + rw [show 2*r*d=2*(r*d) by ring,H] + unfold R r + rw [ray_sq,ray_sq,content,content,content,duet_inc,duet_inc] + ring + change R ≤ r+d+C + change _ ≤ r*C at h + nlinarith [sq_nonneg (R-r)] + +lemma ray_slide (v w : ℂ) (x : ℝ) (a : ℂ) (t : ℝ) : + ray v (w+a) (x+t) ≤ ray v w x + ‖a‖ + |t| := by + let r := ray v w x; let d := ray v (w+a) (x+t) + have H := ray_bounds v w x + have hR : r^2 = ‖v‖^2+‖w‖^2+x^2 := ray_sq _ _ _ + have hr : 0 ≤ r := ray_pos _ _ _ + have hd : d^2=‖v‖^2+‖w+a‖^2+(x+t)^2 := ray_sq _ _ _ + have hi : ‖w‖ ≤ r := H.2.1 + have Hq : |x| ≤ r := H.2.2.1 + change d ≤ _ + have HH : ‖w+a‖^2 ≤ (‖w‖+‖a‖)^2 := pow_le_pow_left₀ (norm_nonneg _) (norm_add_le w a) _ + have Ht : x*t ≤ r*|t| := (le_abs_self _).trans + (by rw [abs_mul]; exact mul_le_mul_of_nonneg_right Hq (abs_nonneg t)) + change d ≤ r+_ + + |t| + nlinarith [sq_abs t,norm_nonneg a,abs_nonneg t] + + +def prism (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) (z : Grid M) := + ray (W z) (letter s u z) (a*matter s z) +def watt (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) := ∑ z, prism s u W a z +def yieldW (W : Paths.Vector M) := ∑ i : Grid M, ‖W i‖ +lemma yield_step (s : Scene M) (u : Grid M) (W : Paths.Vector M) (a : ℝ) : + yieldW W ≤ watt s u W a := Finset.sum_le_sum fun i _ => (ray_bounds (W i) _ _).1 + +lemma watt_idle {s : Scene M} (h : Fit s) (u : Grid M) (a : ℝ) (ha : 0 ≤ a) (hx : a ≤ 1) : + watt s u 0 a ≤ 8 := by + apply le_trans (b:=∑ i,2*matter s i) + · apply Finset.sum_le_sum; intro i _ + change ray 0 _ _ ≤ _ + apply le_trans (ray_bounds _ _ _).2.2.2 + have HM := matter_norm h u i; have hM := matter_nonneg h i + rw [abs_of_nonneg (mul_nonneg ha hM),norm_zero] + nlinarith + rw [←Finset.mul_sum,matter_sum h] + linarith [h.lift] + +lemma watt_slide {b : Beat M} (h : Ideal b.pre b.post) (u : Grid M) (W : Paths.Vector M) (a : ℝ) + (ha : 0 ≤ a) (he : a ≤ 1) : + watt b.post u W a ≤ watt b.pre u W a + 2*killed b := by + let f := letter b.pre u; let g := letter b.post u + let p := matter b.pre; let q := matter b.post + rw [h.kill_eq,←matter_sum h.fit,←matter_sum h.climb.fit,← Finset.sum_sub_distrib, + Finset.mul_sum,watt,watt,← Finset.sum_add_distrib] + apply Finset.sum_le_sum; intro i _ + change ray (W i) (g i) (a*q i) ≤ ray (W i) (f i) (a*p i)+_ + conv_lhs => + rw [show g i=f i+(g i-f i) by ring,show a*q i=a*p i+a*(q i-p i) by ring] + apply le_trans (ray_slide (W i) _ _ _ _) + have H : ‖f i-g i‖ ≤ p i-q i := matter_drain h u i + have HH := (norm_nonneg _).trans H + rw [abs_mul,abs_of_nonneg ha,abs_sub_comm,abs_of_nonneg HH,norm_sub_rev] + change _ ≤ _+2*(p i-q i) + nlinarith + +end Beam + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Ribbon.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Ribbon.lean index f3801120cd104a55209885576938e2e2a6ac0fc0..f14293ab27a4f137ec27900fd083e257a12ae500 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Ribbon.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Ribbon.lean @@ -1,201 +1,201 @@ -import OAI.Analysis.FourierLLogL.Paths.Run -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Beam -lemma yield_zero : yieldW (0:Paths.Vector M)=0 := by simp [yieldW] -lemma yield_pos (u : Paths.Vector M) : 0 ≤ yieldW u := Finset.sum_nonneg fun _ _ => norm_nonneg _ -lemma yield_add (v w : Paths.Vector M) : - yieldW (v+w) ≤ yieldW v + yieldW w := by - unfold yieldW; rw [←Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => norm_add_le _ _ -lemma yield_sub (v w : Paths.Vector M) : - yieldW (v-w) ≤ yieldW v + yieldW w := by - unfold yieldW; rw [←Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => norm_sub_le _ _ - -lemma single_yield {x : Grid M} {s : Scene M} {b : Beat M} {l : Path M} (h : Trip x s (b::l)) (u) : - yieldW (duties x u b) ≤ 2 := by - have H := h.2.1.climb - apply le_trans _ H.tight - rw [← matter_sum H.fit] - apply Finset.sum_le_sum; intro i _ - rw [duties,norm_scaleC,trip_final h,one_mul]; exact factor_norm H _ _ -end Beam - -namespace Run.Streak -open Beam Lamp Tray Desk - -/-- One parity of output, sum of odd-numbered positions. -/ -def weave (x u : Grid M) : Path M → Paths.Vector M - | [] => 0 - | [b] => duties x u b - | b::_c::l => duties x u b + weave x u l - -def hem (x u : Grid M) : Path M → Paths.Vector M - | [] => 0 - | _::l => weave x u l -lemma ribbon_eq (x u : Grid M) (l : Path M) : - weave x u l+hem x u l=subtotal (duties x u) l := by - induction l with - | nil => change 0 + (0:Paths.Vector M)=0; rw [add_zero] - | cons b l ih => - have hi : weave x u (b::l)= duties x u b+hem x u l := by - cases l - · simp [hem,weave] - · rfl - rw [hi,hem,subtotal_cons,← ih]; abel - -private lemma count_both (e : Streak M) (w : Paths.Vector M) - {b c : Beat M} {l : Path M} {s : Scene M} {x : Grid M} - (h : Trip x s (b::c::l)) : - carry (aimed e w b.post) b.bit c.bit=w+duties x e.u b := by - funext z - change w z + (curl c.bit:ℂ)*((curl b.bit:ℂ)*factor b.post e.u z)=w z+ _ - rw [duties,trip_delay h]; push_cast; ring - -lemma write_down (e : Streak M) {s : Scene M} - {x : Grid M} (l : Path M) (h : Trip x s l) (hp : ∀ b∈l,e.outer b.post) - (w : Paths.Vector M) : - ∃ p q : Path M, l=p++q ∧ - yieldW (w+weave x e.u l) ≤ - watt s e.u w e.thick + 2 + e.thick * ((dealer e).tally (bare w) p) + - subtotal (beamRent e) l := by - match l with - | [] => - refine ⟨[],[],rfl,?_⟩ - change yieldW (w+0) ≤ _+2+_*0+0 - rw [add_zero] - linarith [yield_step s e.u w e.thick] - | [b] => - refine ⟨[],[b],rfl,?_⟩ - change yieldW (w+duties x e.u b) ≤ watt s e.u _ _ + 2 + _*0+(beamRent e b+0) - apply (yield_add _ _).trans - linarith [single_yield h e.u,rent_nonneg e b h.2.1,yield_step s e.u w e.thick] - | b::c::l => - let w' := carry (aimed e w b.post) b.bit c.bit - have HE : w'=w+duties x e.u b := count_both e w h - obtain ⟨rfl,h1,h2,h3,h4⟩ := h - obtain ⟨p,q,H,HH⟩ := write_down e l h4 (fun z hz => hp z (by simp [hz])) w' - have hD := light_bill e w b c h1 h3 h2 (hp b (by simp)) (hp c (by simp)) - let v := dealer e - refine ⟨b::c::p,q,by simp [H],?_⟩ - rw [weave,← add_assoc,← HE,tally,tally,subtotal_cons,subtotal_cons] - change _ ≤ watt b.pre _ _ _ + e.thick*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+_+_ at hD - change watt (winds c) e.u w' e.thick ≤ _ at hD - change yieldW _ ≤ _ + _*(v.pay (bare w) b+(v.pay (v.stepAt (bare w) b) c+v.tally (bare w') p))+_ - change yieldW (w'+_) ≤ _+_ + _*(v.tally _ p)+_ at HH; linarith - -lemma ribbon_pref (e : Streak M) {x : Grid M} (l m : Path M) (h : Trip x s l) - (hp : ∀ b∈l,e.outer b.post) (t : ℝ) (he : 0 ≤ t) - (h1 : 2*log ((dealer e).heatW (bare 0) (l++m)) ≤ t) - (h2 : 2*log ((dealer e).heatW sleep (l++m)) ≤ t) : - yieldW (subtotal (duties x e.u) l) ≤ 20+2*e.thick*t+2*subtotal (beamRent e) l := by - let a := e.thick; let u := e.u; let r := e.beamRent - have ha : 0 < a := e.thick_rate.1 - have hi : a ≤ 1 := e.thick_rate.2.1 - let d := dealer e - rw [←ribbon_eq] - apply (yield_add _ _).trans - have HH₁ : yieldW (weave x u l) ≤ 10+a*t+subtotal r l := by - obtain ⟨p,q,H,hh⟩ := write_down e l h hp 0 - have heq : l++m=p++(q++m) := by rw [H,List.append_assoc] - have HV : d.tally (bare 0) p ≤ t := by - apply ((dealer e).edge_score (bare 0) p (q++m)).trans - rw [← heq]; exact h1 - rw [zero_add] at hh - change yieldW (weave x u l) ≤ watt s _ 0 a+2+a*(d.tally _ _)+_ at hh - apply hh.trans - linarith [watt_idle (trip_head h).1 u a ha.le hi,mul_le_mul_of_nonneg_left HV ha.le] - change yieldW (weave x u l)+_ ≤ 20+2*a*t+_ - have HH₂ : yieldW (hem x u l) ≤ 10+a*t+subtotal r l := by - match l with - | [] => - rw [hem,yield_zero,subtotal_nil] - nlinarith - | b::l => - obtain ⟨H,hb,hk⟩ := h - obtain ⟨p,q,H,hh⟩ := write_down e l hk (fun i hi => hp i (by simp [hi])) 0 - have heq : (b::l)++m=(b::p)++(q++m) := by simp [H,List.append_assoc] - have HV : d.tally (bare 0) p ≤ t := by - have HX : d.tally sleep (b::p) ≤ t := by - apply ((dealer e).edge_score sleep (b::p) (q++m)).trans - rw [← heq]; exact h2 - change single 0 b.bit+d.tally (bare 0) p ≤ t at HX - simpa only [single,mul_zero,ne_eq,OfNat.ofNat_ne_zero,not_false_eq_true,zero_pow,zero_div,sub_zero,zero_add] using HX - rw [zero_add] at hh - rw [hem,subtotal_cons] - change yieldW (weave x u l) ≤ watt (winds b) _ 0 a+2+a*(d.tally _ _)+_ at hh - apply hh.trans - have HH : 0 ≤ r b := rent_nonneg e b hb - linarith [watt_idle (trip_head hk).1 u a ha.le hi,mul_le_mul_of_nonneg_left HV ha.le] - change yieldW _ + yieldW (hem x u l) ≤ 20+2*a*t+2*subtotal r l; linarith - -def bling (e : Streak M) (l : Path M) := - (rain e).heatW () l+(dealer e).heatW (bare 0) l+(dealer e).heatW sleep l -lemma bling_share (e : Streak M) (l : Path M) : - let a := (rain e).heatW () l - let b := (dealer e).heatW (bare 0) l - let c := (dealer e).heatW sleep l - let z := e.bling l - 1 ≤ z ∧ 1 ≤ a ∧ a ≤ z ∧ 1 ≤ b ∧ b ≤ z ∧ 1 ≤ c ∧ c ≤ z := by - intro a b c z - have H1 : 1 ≤ a := heatW_one _ _ _ - have H2 : 1 ≤ b := heatW_one _ _ _ - have H3 : 1 ≤ c := heatW_one _ _ _ - change 1 ≤ a+b+c ∧ 1 ≤ a ∧ a ≤ a+b+c ∧ 1 ≤ b ∧ b ≤ a+b+c ∧ 1 ≤ c ∧ c ≤ a+b+c - exact ⟨by linarith,H1,by linarith,H2,by linarith,H3,by linarith⟩ -lemma bling_mean (e : Streak M) (v : Thread M s) (h : v.Following j J f) : - (s.here.dice M).Ex (fun x => bling e (v.stream x)) ≤ 6 := by - simp_rw [bling,Ex_add]; let d := dealer e - linarith [heat_bound d v h sleep,heat_bound d v h (bare 0),heat_bound (rain e) v h ()] - -lemma ribbon_bound (e : Streak M) {x : Grid M} (l m : Path M) (h : Trip x s l) - (hp : ∀ b∈l,e.outer b.post) - (t : ℝ) (he : 2*log (e.bling (l++m)) ≤ t) : - yieldW (subtotal (duties x e.u) l) ≤ - 10^14*(fullFax l+(1+fullKills l+flagged e l)/e.thick+e.thick*t) := by - let a := e.thick; let u := e.u; let r := e.b - have ha : 0 < a := e.thick_rate.1 - have hi : a ≤ 1 := e.thick_rate.2.1 - have hx := bling_share e (l++m) - have hR (x : ℝ) (h : 1 ≤ x) (hf : x ≤ e.bling (l++m)) : 2* log x ≤ t := - le_trans (mul_le_mul_of_nonneg_left (log_le_log (by linarith) hf) (by norm_num)) he - obtain ⟨ha₀,ha₁,ha₂,ha₃,ha₄,ha₅,ha₆⟩ := hx - have HH : 0 ≤ t := le_trans (mul_nonneg (by norm_num) (log_nonneg ha₀)) he - apply (ribbon_pref e l m h hp t HH (hR _ ha₃ ha₄) (hR _ ha₅ ha₆)).trans - rw [rent_total] - let V := showers e l; let K := fullKills l; let E := flagged e l; let J := fullFax l - have hJ : 0 ≤ J := trip_fax h; have hK : 0 ≤ K := trip_K h; have hE : 0 ≤ E := flagged_pos e h - have hM := e.cave_whole h hp - have HE := ((rain e).edge_score () l m).trans (hR _ ha₁ ha₂) - have hV : V ≤ 2*(4+8*K+4*E+r*J+10^5*r*t) := by - have HR : 0 < r := e.rate.1 - change V/2 ≤ _-energy s u+8*K+4*E+r*J+10^5*r*_ at hM - nlinarith [(energy_small (trip_head h).1 u).1] - have heq : a^2=r := thick_rate e |>.2.2 - change 20+2*a*t+2*(10^6*(J+(K+E+V)/a)) ≤ 10^14*(J+(1+K+E)/a+a*t) - apply le_of_mul_le_mul_left _ ha - have HD (x y z : ℝ) : a*(x*(y+z/a))=x*(a*y+z) := by field_simp - simp_rw [mul_add] - have HG (x k : ℝ) : a*(x*(k/a))=x*k := by field_simp - - field_simp - nlinarith [mul_nonneg ha.le HH,show a*a*J ≤ a*J from by exact mul_le_mul_of_nonneg_right (by nlinarith) hJ, - show a*(a*t)=r*t by rw [← heq]; ring] - -end Run.Streak - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Run +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Beam +lemma yield_zero : yieldW (0:Paths.Vector M)=0 := by simp [yieldW] +lemma yield_pos (u : Paths.Vector M) : 0 ≤ yieldW u := Finset.sum_nonneg fun _ _ => norm_nonneg _ +lemma yield_add (v w : Paths.Vector M) : + yieldW (v+w) ≤ yieldW v + yieldW w := by + unfold yieldW; rw [←Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => norm_add_le _ _ +lemma yield_sub (v w : Paths.Vector M) : + yieldW (v-w) ≤ yieldW v + yieldW w := by + unfold yieldW; rw [←Finset.sum_add_distrib]; exact Finset.sum_le_sum fun i _ => norm_sub_le _ _ + +lemma single_yield {x : Grid M} {s : Scene M} {b : Beat M} {l : Path M} (h : Trip x s (b::l)) (u) : + yieldW (duties x u b) ≤ 2 := by + have H := h.2.1.climb + apply le_trans _ H.tight + rw [← matter_sum H.fit] + apply Finset.sum_le_sum; intro i _ + rw [duties,norm_scaleC,trip_final h,one_mul]; exact factor_norm H _ _ +end Beam + +namespace Run.Streak +open Beam Lamp Tray Desk + +/-- One parity of output, sum of odd-numbered positions. -/ +def weave (x u : Grid M) : Path M → Paths.Vector M + | [] => 0 + | [b] => duties x u b + | b::_c::l => duties x u b + weave x u l + +def hem (x u : Grid M) : Path M → Paths.Vector M + | [] => 0 + | _::l => weave x u l +lemma ribbon_eq (x u : Grid M) (l : Path M) : + weave x u l+hem x u l=subtotal (duties x u) l := by + induction l with + | nil => change 0 + (0:Paths.Vector M)=0; rw [add_zero] + | cons b l ih => + have hi : weave x u (b::l)= duties x u b+hem x u l := by + cases l + · simp [hem,weave] + · rfl + rw [hi,hem,subtotal_cons,← ih]; abel + +private lemma count_both (e : Streak M) (w : Paths.Vector M) + {b c : Beat M} {l : Path M} {s : Scene M} {x : Grid M} + (h : Trip x s (b::c::l)) : + carry (aimed e w b.post) b.bit c.bit=w+duties x e.u b := by + funext z + change w z + (curl c.bit:ℂ)*((curl b.bit:ℂ)*factor b.post e.u z)=w z+ _ + rw [duties,trip_delay h]; push_cast; ring + +lemma write_down (e : Streak M) {s : Scene M} + {x : Grid M} (l : Path M) (h : Trip x s l) (hp : ∀ b∈l,e.outer b.post) + (w : Paths.Vector M) : + ∃ p q : Path M, l=p++q ∧ + yieldW (w+weave x e.u l) ≤ + watt s e.u w e.thick + 2 + e.thick * ((dealer e).tally (bare w) p) + + subtotal (beamRent e) l := by + match l with + | [] => + refine ⟨[],[],rfl,?_⟩ + change yieldW (w+0) ≤ _+2+_*0+0 + rw [add_zero] + linarith [yield_step s e.u w e.thick] + | [b] => + refine ⟨[],[b],rfl,?_⟩ + change yieldW (w+duties x e.u b) ≤ watt s e.u _ _ + 2 + _*0+(beamRent e b+0) + apply (yield_add _ _).trans + linarith [single_yield h e.u,rent_nonneg e b h.2.1,yield_step s e.u w e.thick] + | b::c::l => + let w' := carry (aimed e w b.post) b.bit c.bit + have HE : w'=w+duties x e.u b := count_both e w h + obtain ⟨rfl,h1,h2,h3,h4⟩ := h + obtain ⟨p,q,H,HH⟩ := write_down e l h4 (fun z hz => hp z (by simp [hz])) w' + have hD := light_bill e w b c h1 h3 h2 (hp b (by simp)) (hp c (by simp)) + let v := dealer e + refine ⟨b::c::p,q,by simp [H],?_⟩ + rw [weave,← add_assoc,← HE,tally,tally,subtotal_cons,subtotal_cons] + change _ ≤ watt b.pre _ _ _ + e.thick*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+_+_ at hD + change watt (winds c) e.u w' e.thick ≤ _ at hD + change yieldW _ ≤ _ + _*(v.pay (bare w) b+(v.pay (v.stepAt (bare w) b) c+v.tally (bare w') p))+_ + change yieldW (w'+_) ≤ _+_ + _*(v.tally _ p)+_ at HH; linarith + +lemma ribbon_pref (e : Streak M) {x : Grid M} (l m : Path M) (h : Trip x s l) + (hp : ∀ b∈l,e.outer b.post) (t : ℝ) (he : 0 ≤ t) + (h1 : 2*log ((dealer e).heatW (bare 0) (l++m)) ≤ t) + (h2 : 2*log ((dealer e).heatW sleep (l++m)) ≤ t) : + yieldW (subtotal (duties x e.u) l) ≤ 20+2*e.thick*t+2*subtotal (beamRent e) l := by + let a := e.thick; let u := e.u; let r := e.beamRent + have ha : 0 < a := e.thick_rate.1 + have hi : a ≤ 1 := e.thick_rate.2.1 + let d := dealer e + rw [←ribbon_eq] + apply (yield_add _ _).trans + have HH₁ : yieldW (weave x u l) ≤ 10+a*t+subtotal r l := by + obtain ⟨p,q,H,hh⟩ := write_down e l h hp 0 + have heq : l++m=p++(q++m) := by rw [H,List.append_assoc] + have HV : d.tally (bare 0) p ≤ t := by + apply ((dealer e).edge_score (bare 0) p (q++m)).trans + rw [← heq]; exact h1 + rw [zero_add] at hh + change yieldW (weave x u l) ≤ watt s _ 0 a+2+a*(d.tally _ _)+_ at hh + apply hh.trans + linarith [watt_idle (trip_head h).1 u a ha.le hi,mul_le_mul_of_nonneg_left HV ha.le] + change yieldW (weave x u l)+_ ≤ 20+2*a*t+_ + have HH₂ : yieldW (hem x u l) ≤ 10+a*t+subtotal r l := by + match l with + | [] => + rw [hem,yield_zero,subtotal_nil] + nlinarith + | b::l => + obtain ⟨H,hb,hk⟩ := h + obtain ⟨p,q,H,hh⟩ := write_down e l hk (fun i hi => hp i (by simp [hi])) 0 + have heq : (b::l)++m=(b::p)++(q++m) := by simp [H,List.append_assoc] + have HV : d.tally (bare 0) p ≤ t := by + have HX : d.tally sleep (b::p) ≤ t := by + apply ((dealer e).edge_score sleep (b::p) (q++m)).trans + rw [← heq]; exact h2 + change single 0 b.bit+d.tally (bare 0) p ≤ t at HX + simpa only [single,mul_zero,ne_eq,OfNat.ofNat_ne_zero,not_false_eq_true,zero_pow,zero_div,sub_zero,zero_add] using HX + rw [zero_add] at hh + rw [hem,subtotal_cons] + change yieldW (weave x u l) ≤ watt (winds b) _ 0 a+2+a*(d.tally _ _)+_ at hh + apply hh.trans + have HH : 0 ≤ r b := rent_nonneg e b hb + linarith [watt_idle (trip_head hk).1 u a ha.le hi,mul_le_mul_of_nonneg_left HV ha.le] + change yieldW _ + yieldW (hem x u l) ≤ 20+2*a*t+2*subtotal r l; linarith + +def bling (e : Streak M) (l : Path M) := + (rain e).heatW () l+(dealer e).heatW (bare 0) l+(dealer e).heatW sleep l +lemma bling_share (e : Streak M) (l : Path M) : + let a := (rain e).heatW () l + let b := (dealer e).heatW (bare 0) l + let c := (dealer e).heatW sleep l + let z := e.bling l + 1 ≤ z ∧ 1 ≤ a ∧ a ≤ z ∧ 1 ≤ b ∧ b ≤ z ∧ 1 ≤ c ∧ c ≤ z := by + intro a b c z + have H1 : 1 ≤ a := heatW_one _ _ _ + have H2 : 1 ≤ b := heatW_one _ _ _ + have H3 : 1 ≤ c := heatW_one _ _ _ + change 1 ≤ a+b+c ∧ 1 ≤ a ∧ a ≤ a+b+c ∧ 1 ≤ b ∧ b ≤ a+b+c ∧ 1 ≤ c ∧ c ≤ a+b+c + exact ⟨by linarith,H1,by linarith,H2,by linarith,H3,by linarith⟩ +lemma bling_mean (e : Streak M) (v : Thread M s) (h : v.Following j J f) : + (s.here.dice M).Ex (fun x => bling e (v.stream x)) ≤ 6 := by + simp_rw [bling,Ex_add]; let d := dealer e + linarith [heat_bound d v h sleep,heat_bound d v h (bare 0),heat_bound (rain e) v h ()] + +lemma ribbon_bound (e : Streak M) {x : Grid M} (l m : Path M) (h : Trip x s l) + (hp : ∀ b∈l,e.outer b.post) + (t : ℝ) (he : 2*log (e.bling (l++m)) ≤ t) : + yieldW (subtotal (duties x e.u) l) ≤ + 10^14*(fullFax l+(1+fullKills l+flagged e l)/e.thick+e.thick*t) := by + let a := e.thick; let u := e.u; let r := e.b + have ha : 0 < a := e.thick_rate.1 + have hi : a ≤ 1 := e.thick_rate.2.1 + have hx := bling_share e (l++m) + have hR (x : ℝ) (h : 1 ≤ x) (hf : x ≤ e.bling (l++m)) : 2* log x ≤ t := + le_trans (mul_le_mul_of_nonneg_left (log_le_log (by linarith) hf) (by norm_num)) he + obtain ⟨ha₀,ha₁,ha₂,ha₃,ha₄,ha₅,ha₆⟩ := hx + have HH : 0 ≤ t := le_trans (mul_nonneg (by norm_num) (log_nonneg ha₀)) he + apply (ribbon_pref e l m h hp t HH (hR _ ha₃ ha₄) (hR _ ha₅ ha₆)).trans + rw [rent_total] + let V := showers e l; let K := fullKills l; let E := flagged e l; let J := fullFax l + have hJ : 0 ≤ J := trip_fax h; have hK : 0 ≤ K := trip_K h; have hE : 0 ≤ E := flagged_pos e h + have hM := e.cave_whole h hp + have HE := ((rain e).edge_score () l m).trans (hR _ ha₁ ha₂) + have hV : V ≤ 2*(4+8*K+4*E+r*J+10^5*r*t) := by + have HR : 0 < r := e.rate.1 + change V/2 ≤ _-energy s u+8*K+4*E+r*J+10^5*r*_ at hM + nlinarith [(energy_small (trip_head h).1 u).1] + have heq : a^2=r := thick_rate e |>.2.2 + change 20+2*a*t+2*(10^6*(J+(K+E+V)/a)) ≤ 10^14*(J+(1+K+E)/a+a*t) + apply le_of_mul_le_mul_left _ ha + have HD (x y z : ℝ) : a*(x*(y+z/a))=x*(a*y+z) := by field_simp + simp_rw [mul_add] + have HG (x k : ℝ) : a*(x*(k/a))=x*k := by field_simp + + field_simp + nlinarith [mul_nonneg ha.le HH,show a*a*J ≤ a*J from by exact mul_le_mul_of_nonneg_right (by nlinarith) hJ, + show a*(a*t)=r*t by rw [← heq]; ring] + +end Run.Streak + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Run.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Run.lean index 03d917f2e6669e810c709773ef826b8490603f66..85ef91efca29f4a4de519bbb3b6dc93aec1946d0 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Run.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Run.lean @@ -1,228 +1,228 @@ -import OAI.Analysis.FourierLLogL.Paths.Flash -import OAI.Analysis.FourierLLogL.Paths.Wager -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Run -open Beam Lamp Desk - -structure Streak (M : ℕ) where - i : ℕ - u : Grid M - -namespace Streak -def b (e : Streak M) := side e.i -def thick (e : Streak M) := Real.sqrt e.b -def rogue (e : Streak M) (s : Scene M) := 0 < e.i ∧ hazard s true (e.i-1) -def every (e : Streak M) (s : Scene M) (L : ℝ) := - ∀ i, energy (s.smaller i) e.u ≤ L*e.b^2 -def outer (e : Streak M) (s : Scene M) := e.rogue s ∨ e.every s 256 -def innerV (e : Streak M) (s : Scene M) := e.rogue s ∨ e.every s 64 -def flag (e : Streak M) (s : Scene M) := if e.rogue s then (1:ℝ) else 0 -def flagged (e : Streak M) (l : Path M) := subtotal (fun b => flag e b.post) l -def showers (e : Streak M) (l : Path M) := subtotal (fun b => shower e.u b.post) l - -lemma rate (e : Streak M) : 0 < e.b ∧ e.b ≤ 1 := ⟨side_pos _,side_le _⟩ -lemma thick_rate (e : Streak M) : 0 < e.thick ∧ e.thick ≤ 1 ∧ e.thick^2=e.b := - ⟨sqrt_pos.2 e.rate.1,by unfold thick; simpa using Real.sqrt_le_sqrt e.rate.2, - sq_sqrt e.rate.1.le⟩ -lemma flag_nonneg (e : Streak M) (s : Scene M) : 0 ≤ flag e s := by unfold flag; split <;> norm_num -lemma flagged_pos {l : Path M} {x : Grid M} (e : Streak M) (h : Trip x s l) : 0 ≤ flagged e l := - trip_nonneg h (fun b _ => flag_nonneg e b.post) -lemma showers_pos {l : Path M} {x : Grid M} (e : Streak M) (h : Trip x s l) : 0 ≤ showers e l := - trip_nonneg h (fun _ H => shower_nonneg H.climb) -lemma inner_outer (e : Streak M) (s : Scene M) (h : innerV e s) : outer e s := - h.elim Or.inl fun H => Or.inr fun i => by nlinarith [H i] - -def rain (e : Streak M) : Desk M Unit where - stake _ _ s := if e.rogue s then 0 else -gust s e.u/(10^5 * e.b) - stepAt _ _ := () - -lemma cave (e : Streak M) (b : Beat M) (hp : Ideal b.pre b.post) (hv : e.outer b.post) : - let u := e.u - shower u b.post/2 ≤ energy (winds b) u-energy b.pre u+8*killed b+4*flag e b.post+ - e.b*fax b.post+10^5*e.b*((rain e).pay () b) := by - intro u - rw [winds,wet_walk _ hp.climb] - let s := b.post; let x := e.b; let y := gust s u - let a : ℝ := 10^5*x - have hx : 0 < x := e.rate.1; have he : x ≤ 1 := e.rate.2 - have hr : 0 < a := by unfold a; positivity - have hT := (abs_le.mp (energy_drain hp u)).2 - have hJ : 0 ≤ fax s := (fax_floor hp.climb).1 - have hG : 0 ≤ shower u s := shower_nonneg hp.climb - change shower u s /2 ≤ energy s u+shower u s+curl b.bit*y - energy b.pre u+ - 8*killed b+4*flag e s+x*fax s+a*(rain e).pay () b - by_cases h : e.rogue b.post - · rw [show flag e s=1 from ite_eq_left h] - have H : (rain e).pay () b=0 := by simp [rain,pay,h,single] - have HV (i) : 0 ≤ energy (s.smaller i) u ∧ energy (s.smaller i) u ≤ 4 := - let H := energy_small (hp.climb.child i) u - ⟨H.1,H.2.trans (hp.climb.child i).lift⟩ - have hY : |curl b.bit*y| ≤ 4 := by - rw [abs_mul,curl_unit,one_mul,abs_le]; unfold y gust - constructor <;> linarith [(HV false).1,(HV true).1,(HV true).2,(HV false).2] - rw [H,mul_zero]; unfold s at *; nlinarith [(abs_le.mp hY).1] - · rw [show flag e s=0 from ite_eq_right h] - have H : a*(rain e).pay () b= -curl b.bit*y-y^2/(2*a) := by - have HE : (rain e).stake () b.pre b.post = -y/a := by unfold rain; simp [h]; rfl - rw [pay,HE,single]; field_simp - have he₁ : y^2 ≤ 100*(256*x^2)*(fax s+shower u s) := - sky_variance s hp.climb u _ (hv.resolve_left h) - have hx' : y^2/(2*a) ≤ x/2*(fax s+shower u s) := by - apply (div_le_iff₀ (by positivity)).mpr - unfold a; nlinarith [mul_nonneg (sq_nonneg x) (add_nonneg hJ hG)] - rw [H] - unfold s at * - nlinarith - -lemma cave_whole (e : Streak M) {l : Path M} {x : Grid M} (h : Trip x s l) - (hh : ∀ b∈l,e.outer b.post) : - let u := e.u - showers e l/2 ≤ 4-energy s u + 8*fullKills l+4*flagged e l+e.b*fullFax l+ - 10^5*e.b*((rain e).tally () l) := by - intro u - induction l generalizing s with - | nil => - have H := trip_head h - have HH := (energy_small H.1 u).2.trans H.1.lift - change 0/2 ≤ 4-_+_*0+_*0+_*0+_*0 - linarith - | cons b l ih => - obtain ⟨rfl,ha,hb⟩ := h - have H := e.cave b ha (hh b (List.mem_cons_self)) - change shower u b.post /2 ≤ _ at H - have hi := ih hb (fun b hi => hh b (List.mem_cons_of_mem _ hi)) - change (shower u b.post+showers e l)/2 ≤ 4-energy b.pre u+ - 8*(killed b+fullKills l)+4*(flag e b.post+flagged e l)+ - _*(fax b.post+fullFax l)+_*((rain e).pay () b+(rain e).tally () l) - linarith - -def aimed (e : Streak M) (W : Paths.Vector M) (s : Scene M) : Lamp M := ⟨e.u,e.thick,W,s⟩ -lemma lit_upper (e : Streak M) (W : Paths.Vector M) (t : Scene M) (h : Climb s) (hp : e.outer s) : - let a := e.thick - lit (e.aimed W t) s ≤ 300*(a*fax s+flag e s+shower e.u s) := by - intro a - let J := fax s; let q := shower e.u s; let g := energy s e.u; let r := e.b - have hJ : 0 ≤ J ∧ J ≤ 2 := fax_floor h - have hq : 0 ≤ q := shower_nonneg h - have hα : 0 < a ∧ a ≤ 1 ∧ a^2=r := e.thick_rate - have hr : 0 < r ∧ r ≤ 1 := e.rate - change (g+a^2)*J+q ≤ 300*(a*J+flag e s+q) - by_cases hh : rogue e s - · have hg : g ≤ 4 := (energy_small h.fit e.u).2.trans h.fit.lift - rw [flag,ite_eq_left hh]; nlinarith - · have HE : g ≤ 256*r^2 := by - have HV := hp.resolve_left hh - have H : g+q=(energy (s.smaller false) e.u+energy (s.smaller true) e.u)/2 := - (wet_average s h e.u).symm - unfold r; linarith [HV true,HV false] - have hx : r^2 ≤ a := by nlinarith - have Hx : g+a^2 ≤ 257*a := by nlinarith - rw [flag,ite_eq_right hh]; nlinarith - -inductive Tray (M : ℕ) where -| sleep -| bare (W : Paths.Vector M) -| lite (e : Lamp M) (r : Bool) -open Tray - -def dealer (e : Streak M) : Desk M (Tray M) where - stake z _ q := match z with - | sleep => 0 - | bare W => start (aimed e W q)/e.thick - | lite s r => delayed s r q/e.thick - stepAt z b := match z with - | sleep => bare 0 - | bare W => lite (aimed e W b.post) b.bit - | lite s r => bare (carry s r b.bit) - -def beamRent (e : Streak M) (b : Beat M) := - let s := b.post - 10^6*(fax s+(killed b+flag e s+shower e.u s)/e.thick) -lemma rent_nonneg (e : Streak M) (b : Beat M) (hp : Ideal b.pre b.post) : - 0 ≤ beamRent e b := mul_nonneg (by norm_num) - (add_nonneg (fax_floor hp.climb).1 (div_nonneg - (add_nonneg (add_nonneg hp.kill_nonneg (flag_nonneg e _)) (shower_nonneg hp.climb)) e.thick_rate.1.le)) -lemma rent_total (e : Streak M) (l : Path M) : - subtotal (beamRent e) l=10^6*(fullFax l+(fullKills l+flagged e l+showers e l)/e.thick) := by - induction l with - | nil => change (0:ℝ)=10^6*(0+(0+0+0)/_); ring - | cons b l ih => - rw [subtotal_cons,ih] - change 10^6*(fax b.post+(killed b+flag e b.post+shower e.u b.post)/e.thick)+ - 10^6*(fullFax l+(fullKills l+flagged e l+showers e l)/e.thick)= - 10^6*((fax b.post+fullFax l)+(killed b+fullKills l+(flag e b.post+flagged e l)+ - (shower e.u b.post+showers e l))/e.thick) - ring - -lemma light_bill (e : Streak M) (w : Paths.Vector M) - (b c : Beat M) (hb : Ideal b.pre b.post) (hc : Ideal c.pre c.post) - (hh : c.pre=winds b) (hp : e.outer b.post) (he : e.outer c.post) : - let r := aimed e w b.post - let v := dealer e - let a := e.thick - watt (winds c) e.u (carry r b.bit c.bit) a ≤ watt b.pre e.u w a + - a*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+beamRent e b+beamRent e c := by - intro r v a - let x := start r; let y := delayed r b.bit c.post - have ha : 0 < a := e.thick_rate.1 - have hi : a ≤ 1 := e.thick_rate.2.1 - have HE : curl b.bit*x+curl c.bit*y= - a*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+(x^2+y^2)/(2*a) := by - change curl _*_+curl _ *_ = a*(single (x/a) b.bit+single (y/a) c.bit)+_ - unfold single; field_simp; ring - let J (b : Beat M) := fax b.post; let G (b : Beat M) := shower e.u b.post - let Q (b : Beat M) := flag e b.post - have H1 : x^2 ≤ 30000*(a*J b+Q b+G b) := by - have H : x^2 ≤ 100*lit r b.post := start_cost r hb.climb ha.le - have H' : lit r b.post ≤ 300*(a*J b+Q b+G b) := lit_upper e w b.post hb.climb hp - linarith - have H2 : y^2 ≤ 30000*((a*J b+Q b+G b)+(a*J c+Q c+G c)) := by - have H : y^2 ≤ 100*(lit r b.post+lit r c.post) := delayed_cost r hb.climb hc.climb ha.le b.bit - have Hx : lit r b.post ≤ 300*(a*J b+Q b+G b) := lit_upper e w b.post hb.climb hp - have Hy : lit r c.post ≤ 300*(a*J c+Q c+G c) := lit_upper e w b.post hc.climb he - linarith - let K (b : Beat M) := killed b - let A := J b+J c - let B := Q b+Q c - let C := G b+G c - have HG (b : Beat M) (hb : Ideal b.pre b.post) : - 0 ≤ J b ∧ 0 ≤ Q b ∧ 0 ≤ G b ∧ 0 ≤ K b := - ⟨(fax_floor hb.climb).1,flag_nonneg _ _, shower_nonneg hb.climb,hb.kill_nonneg⟩ - obtain ⟨h1,h2,h3,h4⟩ := HG b hb - obtain ⟨h5,h6,h7,h8⟩ := HG c hc - have HX : (x^2+y^2)/(2*a) ≤ 10^5*(A+(B+C)/a) := by - apply (div_le_iff₀ (by positivity)).mpr - rw [show 10^5*(A+(B+C)/a)*(2*a)=200000*(a*A+B+C) by field_simp; ring] - unfold A B C; nlinarith - have hH := flash r hb.climb b.bit c hh hc ha hi - change watt (winds c) e.u (carry r b.bit c.bit) a ≤ watt b.post e.u w a+ - curl b.bit*x+curl c.bit*y+ 2*K c+4*(20*A+400*((K c+G b+G c)/a)) at hH - have HF : 2*K b+2*K c+4*(20*A+400*((K c+G b+G c)/a))+10^5*(A+(B+C)/a) ≤ - beamRent e b+beamRent e c := by - change _ ≤ 10^6*(J b+(K b+Q b+G b)/a)+10^6*(J c+(K c+Q c+G c)/a) - have hz : 1 ≤ a⁻¹ := (one_le_inv₀ ha).mpr hi - unfold A B C - simp_rw [div_eq_mul_inv] - nlinarith - linarith [watt_slide hb e.u w a ha.le hi] - -end Streak -end Run - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Flash +import OAI.Analysis.FourierLLogL.Paths.Wager +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Run +open Beam Lamp Desk + +structure Streak (M : ℕ) where + i : ℕ + u : Grid M + +namespace Streak +def b (e : Streak M) := side e.i +def thick (e : Streak M) := Real.sqrt e.b +def rogue (e : Streak M) (s : Scene M) := 0 < e.i ∧ hazard s true (e.i-1) +def every (e : Streak M) (s : Scene M) (L : ℝ) := + ∀ i, energy (s.smaller i) e.u ≤ L*e.b^2 +def outer (e : Streak M) (s : Scene M) := e.rogue s ∨ e.every s 256 +def innerV (e : Streak M) (s : Scene M) := e.rogue s ∨ e.every s 64 +def flag (e : Streak M) (s : Scene M) := if e.rogue s then (1:ℝ) else 0 +def flagged (e : Streak M) (l : Path M) := subtotal (fun b => flag e b.post) l +def showers (e : Streak M) (l : Path M) := subtotal (fun b => shower e.u b.post) l + +lemma rate (e : Streak M) : 0 < e.b ∧ e.b ≤ 1 := ⟨side_pos _,side_le _⟩ +lemma thick_rate (e : Streak M) : 0 < e.thick ∧ e.thick ≤ 1 ∧ e.thick^2=e.b := + ⟨sqrt_pos.2 e.rate.1,by unfold thick; simpa using Real.sqrt_le_sqrt e.rate.2, + sq_sqrt e.rate.1.le⟩ +lemma flag_nonneg (e : Streak M) (s : Scene M) : 0 ≤ flag e s := by unfold flag; split <;> norm_num +lemma flagged_pos {l : Path M} {x : Grid M} (e : Streak M) (h : Trip x s l) : 0 ≤ flagged e l := + trip_nonneg h (fun b _ => flag_nonneg e b.post) +lemma showers_pos {l : Path M} {x : Grid M} (e : Streak M) (h : Trip x s l) : 0 ≤ showers e l := + trip_nonneg h (fun _ H => shower_nonneg H.climb) +lemma inner_outer (e : Streak M) (s : Scene M) (h : innerV e s) : outer e s := + h.elim Or.inl fun H => Or.inr fun i => by nlinarith [H i] + +def rain (e : Streak M) : Desk M Unit where + stake _ _ s := if e.rogue s then 0 else -gust s e.u/(10^5 * e.b) + stepAt _ _ := () + +lemma cave (e : Streak M) (b : Beat M) (hp : Ideal b.pre b.post) (hv : e.outer b.post) : + let u := e.u + shower u b.post/2 ≤ energy (winds b) u-energy b.pre u+8*killed b+4*flag e b.post+ + e.b*fax b.post+10^5*e.b*((rain e).pay () b) := by + intro u + rw [winds,wet_walk _ hp.climb] + let s := b.post; let x := e.b; let y := gust s u + let a : ℝ := 10^5*x + have hx : 0 < x := e.rate.1; have he : x ≤ 1 := e.rate.2 + have hr : 0 < a := by unfold a; positivity + have hT := (abs_le.mp (energy_drain hp u)).2 + have hJ : 0 ≤ fax s := (fax_floor hp.climb).1 + have hG : 0 ≤ shower u s := shower_nonneg hp.climb + change shower u s /2 ≤ energy s u+shower u s+curl b.bit*y - energy b.pre u+ + 8*killed b+4*flag e s+x*fax s+a*(rain e).pay () b + by_cases h : e.rogue b.post + · rw [show flag e s=1 from ite_eq_left h] + have H : (rain e).pay () b=0 := by simp [rain,pay,h,single] + have HV (i) : 0 ≤ energy (s.smaller i) u ∧ energy (s.smaller i) u ≤ 4 := + let H := energy_small (hp.climb.child i) u + ⟨H.1,H.2.trans (hp.climb.child i).lift⟩ + have hY : |curl b.bit*y| ≤ 4 := by + rw [abs_mul,curl_unit,one_mul,abs_le]; unfold y gust + constructor <;> linarith [(HV false).1,(HV true).1,(HV true).2,(HV false).2] + rw [H,mul_zero]; unfold s at *; nlinarith [(abs_le.mp hY).1] + · rw [show flag e s=0 from ite_eq_right h] + have H : a*(rain e).pay () b= -curl b.bit*y-y^2/(2*a) := by + have HE : (rain e).stake () b.pre b.post = -y/a := by unfold rain; simp [h]; rfl + rw [pay,HE,single]; field_simp + have he₁ : y^2 ≤ 100*(256*x^2)*(fax s+shower u s) := + sky_variance s hp.climb u _ (hv.resolve_left h) + have hx' : y^2/(2*a) ≤ x/2*(fax s+shower u s) := by + apply (div_le_iff₀ (by positivity)).mpr + unfold a; nlinarith [mul_nonneg (sq_nonneg x) (add_nonneg hJ hG)] + rw [H] + unfold s at * + nlinarith + +lemma cave_whole (e : Streak M) {l : Path M} {x : Grid M} (h : Trip x s l) + (hh : ∀ b∈l,e.outer b.post) : + let u := e.u + showers e l/2 ≤ 4-energy s u + 8*fullKills l+4*flagged e l+e.b*fullFax l+ + 10^5*e.b*((rain e).tally () l) := by + intro u + induction l generalizing s with + | nil => + have H := trip_head h + have HH := (energy_small H.1 u).2.trans H.1.lift + change 0/2 ≤ 4-_+_*0+_*0+_*0+_*0 + linarith + | cons b l ih => + obtain ⟨rfl,ha,hb⟩ := h + have H := e.cave b ha (hh b (List.mem_cons_self)) + change shower u b.post /2 ≤ _ at H + have hi := ih hb (fun b hi => hh b (List.mem_cons_of_mem _ hi)) + change (shower u b.post+showers e l)/2 ≤ 4-energy b.pre u+ + 8*(killed b+fullKills l)+4*(flag e b.post+flagged e l)+ + _*(fax b.post+fullFax l)+_*((rain e).pay () b+(rain e).tally () l) + linarith + +def aimed (e : Streak M) (W : Paths.Vector M) (s : Scene M) : Lamp M := ⟨e.u,e.thick,W,s⟩ +lemma lit_upper (e : Streak M) (W : Paths.Vector M) (t : Scene M) (h : Climb s) (hp : e.outer s) : + let a := e.thick + lit (e.aimed W t) s ≤ 300*(a*fax s+flag e s+shower e.u s) := by + intro a + let J := fax s; let q := shower e.u s; let g := energy s e.u; let r := e.b + have hJ : 0 ≤ J ∧ J ≤ 2 := fax_floor h + have hq : 0 ≤ q := shower_nonneg h + have hα : 0 < a ∧ a ≤ 1 ∧ a^2=r := e.thick_rate + have hr : 0 < r ∧ r ≤ 1 := e.rate + change (g+a^2)*J+q ≤ 300*(a*J+flag e s+q) + by_cases hh : rogue e s + · have hg : g ≤ 4 := (energy_small h.fit e.u).2.trans h.fit.lift + rw [flag,ite_eq_left hh]; nlinarith + · have HE : g ≤ 256*r^2 := by + have HV := hp.resolve_left hh + have H : g+q=(energy (s.smaller false) e.u+energy (s.smaller true) e.u)/2 := + (wet_average s h e.u).symm + unfold r; linarith [HV true,HV false] + have hx : r^2 ≤ a := by nlinarith + have Hx : g+a^2 ≤ 257*a := by nlinarith + rw [flag,ite_eq_right hh]; nlinarith + +inductive Tray (M : ℕ) where +| sleep +| bare (W : Paths.Vector M) +| lite (e : Lamp M) (r : Bool) +open Tray + +def dealer (e : Streak M) : Desk M (Tray M) where + stake z _ q := match z with + | sleep => 0 + | bare W => start (aimed e W q)/e.thick + | lite s r => delayed s r q/e.thick + stepAt z b := match z with + | sleep => bare 0 + | bare W => lite (aimed e W b.post) b.bit + | lite s r => bare (carry s r b.bit) + +def beamRent (e : Streak M) (b : Beat M) := + let s := b.post + 10^6*(fax s+(killed b+flag e s+shower e.u s)/e.thick) +lemma rent_nonneg (e : Streak M) (b : Beat M) (hp : Ideal b.pre b.post) : + 0 ≤ beamRent e b := mul_nonneg (by norm_num) + (add_nonneg (fax_floor hp.climb).1 (div_nonneg + (add_nonneg (add_nonneg hp.kill_nonneg (flag_nonneg e _)) (shower_nonneg hp.climb)) e.thick_rate.1.le)) +lemma rent_total (e : Streak M) (l : Path M) : + subtotal (beamRent e) l=10^6*(fullFax l+(fullKills l+flagged e l+showers e l)/e.thick) := by + induction l with + | nil => change (0:ℝ)=10^6*(0+(0+0+0)/_); ring + | cons b l ih => + rw [subtotal_cons,ih] + change 10^6*(fax b.post+(killed b+flag e b.post+shower e.u b.post)/e.thick)+ + 10^6*(fullFax l+(fullKills l+flagged e l+showers e l)/e.thick)= + 10^6*((fax b.post+fullFax l)+(killed b+fullKills l+(flag e b.post+flagged e l)+ + (shower e.u b.post+showers e l))/e.thick) + ring + +lemma light_bill (e : Streak M) (w : Paths.Vector M) + (b c : Beat M) (hb : Ideal b.pre b.post) (hc : Ideal c.pre c.post) + (hh : c.pre=winds b) (hp : e.outer b.post) (he : e.outer c.post) : + let r := aimed e w b.post + let v := dealer e + let a := e.thick + watt (winds c) e.u (carry r b.bit c.bit) a ≤ watt b.pre e.u w a + + a*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+beamRent e b+beamRent e c := by + intro r v a + let x := start r; let y := delayed r b.bit c.post + have ha : 0 < a := e.thick_rate.1 + have hi : a ≤ 1 := e.thick_rate.2.1 + have HE : curl b.bit*x+curl c.bit*y= + a*(v.pay (bare w) b+v.pay (v.stepAt (bare w) b) c)+(x^2+y^2)/(2*a) := by + change curl _*_+curl _ *_ = a*(single (x/a) b.bit+single (y/a) c.bit)+_ + unfold single; field_simp; ring + let J (b : Beat M) := fax b.post; let G (b : Beat M) := shower e.u b.post + let Q (b : Beat M) := flag e b.post + have H1 : x^2 ≤ 30000*(a*J b+Q b+G b) := by + have H : x^2 ≤ 100*lit r b.post := start_cost r hb.climb ha.le + have H' : lit r b.post ≤ 300*(a*J b+Q b+G b) := lit_upper e w b.post hb.climb hp + linarith + have H2 : y^2 ≤ 30000*((a*J b+Q b+G b)+(a*J c+Q c+G c)) := by + have H : y^2 ≤ 100*(lit r b.post+lit r c.post) := delayed_cost r hb.climb hc.climb ha.le b.bit + have Hx : lit r b.post ≤ 300*(a*J b+Q b+G b) := lit_upper e w b.post hb.climb hp + have Hy : lit r c.post ≤ 300*(a*J c+Q c+G c) := lit_upper e w b.post hc.climb he + linarith + let K (b : Beat M) := killed b + let A := J b+J c + let B := Q b+Q c + let C := G b+G c + have HG (b : Beat M) (hb : Ideal b.pre b.post) : + 0 ≤ J b ∧ 0 ≤ Q b ∧ 0 ≤ G b ∧ 0 ≤ K b := + ⟨(fax_floor hb.climb).1,flag_nonneg _ _, shower_nonneg hb.climb,hb.kill_nonneg⟩ + obtain ⟨h1,h2,h3,h4⟩ := HG b hb + obtain ⟨h5,h6,h7,h8⟩ := HG c hc + have HX : (x^2+y^2)/(2*a) ≤ 10^5*(A+(B+C)/a) := by + apply (div_le_iff₀ (by positivity)).mpr + rw [show 10^5*(A+(B+C)/a)*(2*a)=200000*(a*A+B+C) by field_simp; ring] + unfold A B C; nlinarith + have hH := flash r hb.climb b.bit c hh hc ha hi + change watt (winds c) e.u (carry r b.bit c.bit) a ≤ watt b.post e.u w a+ + curl b.bit*x+curl c.bit*y+ 2*K c+4*(20*A+400*((K c+G b+G c)/a)) at hH + have HF : 2*K b+2*K c+4*(20*A+400*((K c+G b+G c)/a))+10^5*(A+(B+C)/a) ≤ + beamRent e b+beamRent e c := by + change _ ≤ 10^6*(J b+(K b+Q b+G b)/a)+10^6*(J c+(K c+Q c+G c)/a) + have hz : 1 ≤ a⁻¹ := (one_le_inv₀ ha).mpr hi + unfold A B C + simp_rw [div_eq_mul_inv] + nlinarith + linarith [watt_slide hb e.u w a ha.le hi] + +end Streak +end Run + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Screen.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Screen.lean index cb703cb4a92b88a711b395bce2107b3aa3fcfac6..6af1e1c6441d5360be6b2ad3a6ad5d7250da9797 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Screen.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Screen.lean @@ -1,175 +1,175 @@ -import OAI.Analysis.FourierLLogL.Paths.Launch -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot - -def depthB (b : Beat M) := b.post.here.lev - -lemma depth_next (b : Beat M) : (winds b).here.lev=depthB b+1 := by - unfold winds depthB smaller - cases b.bit <;> rfl - -def muted (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) : Path M → Prop -| [] => True -| b::l => ¬ p.catches b.post c j u ∧ muted u p (turn p c b.post) j l - -lemma mute_cat (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) (l r : Path M) : - muted u p c j (l++r) ↔ muted u p c j l ∧ muted u p (quarrel p c l) j r := by - induction l generalizing c with - | nil => simp [muted,quarrel] - | cons b l ih => - change (_∧muted u p (p.turn c b.post) j (l++r)) ↔ (_∧_)∧_ - rw [ih]; simp only [and_assoc]; rfl - -lemma first_on (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) (l : Path M) : - muted u p c j l ∨ ∃ a b r,l=a++b::r ∧ muted u p c j a ∧ - p.catches b.post (quarrel p c a) j u := by - induction l generalizing c with - | nil => exact Or.inl True.intro - | cons b l ih => - by_cases h : p.catches b.post c j u - · exact Or.inr ⟨[],b,l,rfl,True.intro,h⟩ - rcases ih (turn p c b.post) with H|⟨a,d,r,H,h1,h2⟩ - · exact Or.inl ⟨h,H⟩ - exact Or.inr ⟨b::a,d,r,by rw [H]; rfl,⟨h,h1⟩,h2⟩ - -private lemma close_persists {A B : Finset (Grid M)} (h : A ⊆ B) (n : ℕ) - {x : Grid M} (hv : enclosed A n x) : enclosed B n x := by - classical - obtain ⟨f,hf,rfl⟩ := hv - let k (x : Grid M) := if x∈A then f x else 0 - refine ⟨k,fun i hi => ?_,?_⟩ - · unfold k; split_ifs with hh; exact hf _ hh; simp - · have H : ∑ i∈A,k i • i=∑ i∈B,k i • i := Finset.sum_subset h (fun i _ h => by simp [k,h]) - rw [← H]; exact Finset.sum_congr rfl fun _ hx => by simp [k,hx] - -private lemma reach_persists {A B : Finset (Grid M)} (h : A ⊆ B) (r t : ℝ) - {x : Grid M} (hh : r ≤ t) (hv : atReach A r x) : - atReach B t x := by - obtain ⟨i,hi,hE⟩ := hv - exact ⟨i,close_persists h 1 hi,hE.trans hh⟩ - -/-- snag only denotes a nonbad high depth. -/ -def snag (g : Grade) (x : Grid M) (b : Beat M) := - intense g.main b.post (side g.i) x ∧ ¬ hazard b.post g.main g.i - -def ink (s : Scene M) (g : Grade) (j : ℕ) := spotLimit s j g.i g.main - -lemma mute_distance (u : Grid M) (p : Plot) - (c : Board M) (s : Scene M) (l r : Path M) - (h : Cast j J f s (l++r)) (he : Comes c s j J f) (hh : muted u p c j l) - (k : ℝ) (hp : atReach c.chart.items k u) : - ∀ b∈l, snag (grade p) u b → - s.here.lev ≤ depthB b ∧ ink b.post (grade p) j < k := by - induction l generalizing s c with - | nil => simp - | cons b l ih => - intro z hz hx - obtain ⟨rfl,⟨t,ht⟩,h₁⟩ := h - have hC := zoom_spec _ he.valid p.ix p.main ht - let d := turn p c b.post - have HR : atReach d.chart.items k u := reach_persists hC.extend k k le_rfl hp - have hi : b.post.here=b.pre.here := ht.2.1.1.here - rcases List.mem_cons.mp hz with rfl|hz - · refine ⟨le_of_eq (congrArg Node.lev hi.symm),lt_of_not_ge fun H => ?_⟩ - exact hh.1 ⟨hx.1,reach_persists hC.extend k _ H hp⟩ - obtain ⟨ha,hb⟩ := ih d (winds b) h₁ (comes_down p ht he b.bit) hh.2 HR z hz hx - rw [depth_next] at ha; change b.pre.here.lev ≤ _ - ∧ _ - rw [← hi]; exact ⟨le_trans (Nat.le_add_right (depthB b) 1) ha,hb⟩ - -def volumeAt (p : Plot) (s : Scene M) (c : Board M) := - localSize s.here p.ix c.chart - -def betweenS (a H : ℕ) (b : Beat M) := - a ≤ depthB b ∧ depthB b < a+H - -lemma screen_time (u : Grid M) (p : Plot) - (c : Board M) (s : Scene M) (L : Path M) - (h : Cast j J f s L) (he : Comes c s j J f) (hp : volumeAt p s c) - (hv : oiled p c L) : - ∃ a : ℕ, ∀ l r : Path M, L=l++r → muted u p c j l → - ∀ b∈l, snag (grade p) u b → betweenS a (latency p j) b := by - induction L generalizing c s with - | nil => - refine ⟨0,?_⟩; intro l r hh hp b hb; cases l - · cases hb - · cases hh - | cons b L ih => - have hi := oily_cons p h he hv - obtain ⟨rfl,⟨t,ht⟩,h₁⟩ := h - have hC := zoom_spec _ he.valid p.ix p.main ht - have hH := ht.2.1.1.here - let d := turn p c b.post; let s := b.post - have HC : localSize s.here p.ix d.chart := hC.expands (hH.symm ▸ hp) - by_cases hx : snag (grade p) u b - · let I := horizon s.here p.ix d.chart.size - have heq : d.chart=(now p c b.post) := rfl - have HB : atReach d.chart.items I u := by - have HE := hC.awake hx.2 u hx.1 - change atReach d.chart.items (d.chart.wide+speed s.here p.ix d.chart.size) u at HE - apply reach_persists (Finset.Subset.refl _) _ _ ?_ HE - unfold localSize at HC - unfold I horizon; nlinarith - refine ⟨depthB b,?_⟩ - intro l r hL hh z hz hZ - cases l with - | nil => cases hz - | cons w l => - change b::L=w::(l++r) at hL; cases hL - rcases List.mem_cons.mp hz with rfl|hz - · unfold betweenS latency; omega - obtain ⟨ha,hb⟩ := mute_distance u p d (winds b) l r h₁ hi.2.2 hh.2 I HB z hz hZ - refine ⟨?_, lt_of_not_ge (fun H => ?_)⟩ - · rw [depth_next] at ha; omega - have Hx' := waiting p j s.here z.post.here H d.chart.size hi.1 - exact hb.not_ge Hx' - · have HV : volumeAt p (winds b) d := by - unfold volumeAt localSize; unfold localSize at HC - apply HC.trans - apply mul_le_mul_of_nonneg_left _ (by positivity) - apply speed_le _ le_rfl - rw [depth_next]; exact Nat.le_add_right _ _ - obtain ⟨a,ha⟩ := ih d _ h₁ hi.2.2 HV hi.2.1 - refine ⟨a,?_⟩ - intro l r hL hh z hz hZ - cases l with - | nil => cases hz - | cons w l => - change b::L=w::(l++r) at hL - have hw := List.cons.inj hL - cases hw.1 - rcases List.mem_cons.mp hz with rfl|hz - · exact (hx hZ).elim - exact ha l r (List.cons.inj hL).2 hh.2 z hz hZ - -def stall (u : Grid M) (g : Grade) (j : ℕ) (L : Path M) (l : Path M) := - muted u (heroPlot g L) (vacant M) j l -def billWait (g : Grade) (j : ℕ) (L : Path M) := - latency (heroPlot g L) j -lemma bound_stage {u : Grid M} {s : Scene M} (L : Path M) - (h : Cast j J f s L) (g : Grade) : - ∃ a : ℕ, ∀ l r : Path M, L=l++r → stall u g j L l → - ∀ b∈l, snag g u b → betweenS a (billWait g j L) b := - screen_time u (heroPlot g L) (vacant M) s L h (vacant_comes s) - (by simp [volumeAt,vacant,classic,Chart.size,localSize]) (oil_hero g L) - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Launch +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot + +def depthB (b : Beat M) := b.post.here.lev + +lemma depth_next (b : Beat M) : (winds b).here.lev=depthB b+1 := by + unfold winds depthB smaller + cases b.bit <;> rfl + +def muted (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) : Path M → Prop +| [] => True +| b::l => ¬ p.catches b.post c j u ∧ muted u p (turn p c b.post) j l + +lemma mute_cat (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) (l r : Path M) : + muted u p c j (l++r) ↔ muted u p c j l ∧ muted u p (quarrel p c l) j r := by + induction l generalizing c with + | nil => simp [muted,quarrel] + | cons b l ih => + change (_∧muted u p (p.turn c b.post) j (l++r)) ↔ (_∧_)∧_ + rw [ih]; simp only [and_assoc]; rfl + +lemma first_on (u : Grid M) (p : Plot) (c : Board M) (j : ℕ) (l : Path M) : + muted u p c j l ∨ ∃ a b r,l=a++b::r ∧ muted u p c j a ∧ + p.catches b.post (quarrel p c a) j u := by + induction l generalizing c with + | nil => exact Or.inl True.intro + | cons b l ih => + by_cases h : p.catches b.post c j u + · exact Or.inr ⟨[],b,l,rfl,True.intro,h⟩ + rcases ih (turn p c b.post) with H|⟨a,d,r,H,h1,h2⟩ + · exact Or.inl ⟨h,H⟩ + exact Or.inr ⟨b::a,d,r,by rw [H]; rfl,⟨h,h1⟩,h2⟩ + +private lemma close_persists {A B : Finset (Grid M)} (h : A ⊆ B) (n : ℕ) + {x : Grid M} (hv : enclosed A n x) : enclosed B n x := by + classical + obtain ⟨f,hf,rfl⟩ := hv + let k (x : Grid M) := if x∈A then f x else 0 + refine ⟨k,fun i hi => ?_,?_⟩ + · unfold k; split_ifs with hh; exact hf _ hh; simp + · have H : ∑ i∈A,k i • i=∑ i∈B,k i • i := Finset.sum_subset h (fun i _ h => by simp [k,h]) + rw [← H]; exact Finset.sum_congr rfl fun _ hx => by simp [k,hx] + +private lemma reach_persists {A B : Finset (Grid M)} (h : A ⊆ B) (r t : ℝ) + {x : Grid M} (hh : r ≤ t) (hv : atReach A r x) : + atReach B t x := by + obtain ⟨i,hi,hE⟩ := hv + exact ⟨i,close_persists h 1 hi,hE.trans hh⟩ + +/-- snag only denotes a nonbad high depth. -/ +def snag (g : Grade) (x : Grid M) (b : Beat M) := + intense g.main b.post (side g.i) x ∧ ¬ hazard b.post g.main g.i + +def ink (s : Scene M) (g : Grade) (j : ℕ) := spotLimit s j g.i g.main + +lemma mute_distance (u : Grid M) (p : Plot) + (c : Board M) (s : Scene M) (l r : Path M) + (h : Cast j J f s (l++r)) (he : Comes c s j J f) (hh : muted u p c j l) + (k : ℝ) (hp : atReach c.chart.items k u) : + ∀ b∈l, snag (grade p) u b → + s.here.lev ≤ depthB b ∧ ink b.post (grade p) j < k := by + induction l generalizing s c with + | nil => simp + | cons b l ih => + intro z hz hx + obtain ⟨rfl,⟨t,ht⟩,h₁⟩ := h + have hC := zoom_spec _ he.valid p.ix p.main ht + let d := turn p c b.post + have HR : atReach d.chart.items k u := reach_persists hC.extend k k le_rfl hp + have hi : b.post.here=b.pre.here := ht.2.1.1.here + rcases List.mem_cons.mp hz with rfl|hz + · refine ⟨le_of_eq (congrArg Node.lev hi.symm),lt_of_not_ge fun H => ?_⟩ + exact hh.1 ⟨hx.1,reach_persists hC.extend k _ H hp⟩ + obtain ⟨ha,hb⟩ := ih d (winds b) h₁ (comes_down p ht he b.bit) hh.2 HR z hz hx + rw [depth_next] at ha; change b.pre.here.lev ≤ _ + ∧ _ + rw [← hi]; exact ⟨le_trans (Nat.le_add_right (depthB b) 1) ha,hb⟩ + +def volumeAt (p : Plot) (s : Scene M) (c : Board M) := + localSize s.here p.ix c.chart + +def betweenS (a H : ℕ) (b : Beat M) := + a ≤ depthB b ∧ depthB b < a+H + +lemma screen_time (u : Grid M) (p : Plot) + (c : Board M) (s : Scene M) (L : Path M) + (h : Cast j J f s L) (he : Comes c s j J f) (hp : volumeAt p s c) + (hv : oiled p c L) : + ∃ a : ℕ, ∀ l r : Path M, L=l++r → muted u p c j l → + ∀ b∈l, snag (grade p) u b → betweenS a (latency p j) b := by + induction L generalizing c s with + | nil => + refine ⟨0,?_⟩; intro l r hh hp b hb; cases l + · cases hb + · cases hh + | cons b L ih => + have hi := oily_cons p h he hv + obtain ⟨rfl,⟨t,ht⟩,h₁⟩ := h + have hC := zoom_spec _ he.valid p.ix p.main ht + have hH := ht.2.1.1.here + let d := turn p c b.post; let s := b.post + have HC : localSize s.here p.ix d.chart := hC.expands (hH.symm ▸ hp) + by_cases hx : snag (grade p) u b + · let I := horizon s.here p.ix d.chart.size + have heq : d.chart=(now p c b.post) := rfl + have HB : atReach d.chart.items I u := by + have HE := hC.awake hx.2 u hx.1 + change atReach d.chart.items (d.chart.wide+speed s.here p.ix d.chart.size) u at HE + apply reach_persists (Finset.Subset.refl _) _ _ ?_ HE + unfold localSize at HC + unfold I horizon; nlinarith + refine ⟨depthB b,?_⟩ + intro l r hL hh z hz hZ + cases l with + | nil => cases hz + | cons w l => + change b::L=w::(l++r) at hL; cases hL + rcases List.mem_cons.mp hz with rfl|hz + · unfold betweenS latency; omega + obtain ⟨ha,hb⟩ := mute_distance u p d (winds b) l r h₁ hi.2.2 hh.2 I HB z hz hZ + refine ⟨?_, lt_of_not_ge (fun H => ?_)⟩ + · rw [depth_next] at ha; omega + have Hx' := waiting p j s.here z.post.here H d.chart.size hi.1 + exact hb.not_ge Hx' + · have HV : volumeAt p (winds b) d := by + unfold volumeAt localSize; unfold localSize at HC + apply HC.trans + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply speed_le _ le_rfl + rw [depth_next]; exact Nat.le_add_right _ _ + obtain ⟨a,ha⟩ := ih d _ h₁ hi.2.2 HV hi.2.1 + refine ⟨a,?_⟩ + intro l r hL hh z hz hZ + cases l with + | nil => cases hz + | cons w l => + change b::L=w::(l++r) at hL + have hw := List.cons.inj hL + cases hw.1 + rcases List.mem_cons.mp hz with rfl|hz + · exact (hx hZ).elim + exact ha l r (List.cons.inj hL).2 hh.2 z hz hZ + +def stall (u : Grid M) (g : Grade) (j : ℕ) (L : Path M) (l : Path M) := + muted u (heroPlot g L) (vacant M) j l +def billWait (g : Grade) (j : ℕ) (L : Path M) := + latency (heroPlot g L) j +lemma bound_stage {u : Grid M} {s : Scene M} (L : Path M) + (h : Cast j J f s L) (g : Grade) : + ∃ a : ℕ, ∀ l r : Path M, L=l++r → stall u g j L l → + ∀ b∈l, snag g u b → betweenS a (billWait g j L) b := + screen_time u (heroPlot g L) (vacant M) s L h (vacant_comes s) + (by simp [volumeAt,vacant,classic,Chart.size,localSize]) (oil_hero g L) + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Ticket.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Ticket.lean index bdc8f3c08f65f91b67c193376bb11d1549cb8ae0..7a9365d8c0ca722f9af9a5d3e27d166b31ccdee1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Ticket.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Ticket.lean @@ -1,177 +1,177 @@ -import OAI.Analysis.FourierLLogL.Paths.Troupe -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe -open Plot - -def entry (s : Scene M) := s.norm=s.mass - -lemma entry_share {s : Scene M} (h : Fit s) (e : entry s) : share s=1 := by - unfold share entry at *; rw [←e,div_self h.pos.ne'] - -lemma old_budget : - ∀ᶠ j : ℕ in atTop, - (minimumT j)⁻¹ ≤ height j ∧ - ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - s.Safe j J f → Fit s → entry s → - (∀ x, f x ≤ exp (height j)) → - ∀ b : Bool, deficit s (channel (optic s b))+(minimumT j)⁻¹ ≤ 10^24*amount s j := by - filter_upwards [side_deficit,hpoly 12800 32] with j ht hh - have H : (minimumT j)⁻¹ ≤ height j := by unfold minimumT; rw [one_div,inv_inv]; exact hh - refine ⟨H,?_⟩ - intro M J s f h hp e hf b - let x := height j; let y := amount s j - have hm : 0 < s.mass := e ▸ hp.pos - have hx := (amount_limits s j).2 - have hy : -log s.mass ≤ y := by - unfold entry at e; rw [←e]; unfold y amount - linarith [d_pos j,le_max_left (-log s.norm) 0] - have hZ := ht M J s f h hf hm - cases b - · unfold optic; rw [ite_eq_right (by simp),funext no_label] - have H₁ := local_deficit_dry s (uniform (Grid M)) hp.ready.1 hp.ready.2 hm x - (fun i => (h.sub i).trans (hf i)) - unfold y x at * - linarith [d_pos j] - · simp only [optic,ite_true] - unfold y at *; linarith [d_pos j] - -def scaleC (i : ℕ) : ℝ := 2^i -lemma scale_one (i : ℕ) : 1 ≤ scaleC i := one_le_pow₀ (by norm_num) -lemma scale_pos (i : ℕ) : 0 < scaleC i := lt_of_lt_of_le zero_lt_one (scale_one i) -lemma scale_side (i : ℕ) : side i = (scaleC i)⁻¹ := by unfold scaleC; rw [side_pow_two] - -lemma prescale (p : Plot) : (precision p.ix p.cap:ℝ) ≤ 100*(scaleC p.ix* scaleC p.cap) := by - have H (i : ℕ) : (i:ℝ) ≤ scaleC i := by - unfold scaleC; exact_mod_cast Nat.lt_two_pow_self.le - have hi := scale_one p.cap; have he := scale_one p.ix - unfold precision - push_cast; nlinarith [H p.cap,H p.ix] - -lemma marker_one (p : Plot) : p.marker ≤ 1 ∧ 1/p.marker ≤ 10^14 * (scaleC p.ix)^12 := by - let x := side p.ix; let v := scaleC p.ix - have hx : 0 < x := side_pos _; have hv : 0 < v := scale_pos _ - have ht : x = v⁻¹ := scale_side _ - have he₁ : x^6 ≤ 1 := pow_le_one₀ hx.le (side_le _) - unfold marker sharp; change (x^6/32768)^2/16 ≤ 1 ∧ _ ≤ _ - constructor - · nlinarith [pow_pos hx 6] - · change 1/((x^6/32768)^2/16) ≤ 10^14*v^12 - rw [ht] - field_simp - nlinarith [pow_pos hv 12] - -def broadMass (l : Path M) (g : Grade) : ℕ := - (goal g.i g.main (Chart.classic M) l).size - -lemma prelude : - ∀ᶠ j : ℕ in atTop, - ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, - t.Following j J f → Fit s → entry s → - (∀ x, f x ≤ exp (height j)) → - ∀ p : Plot, - let x := amount s j - let g := grade p - let B := scaleC p.ix - let D := scaleC p.cap - stats t (minted p (vacant M)) ≤ 10^50*B^14*x*D^4 ∧ - stats t (fun l => (broadMass l g:ℝ)) ≤ 10^31*B^6*x := by - filter_upwards [old_budget] with j hj M J s f t h hp hh hF p x g B D - have he : 1 ≤ x := (amount_limits s j).1 - have HD : 1 ≤ D := scale_one p.cap; have HB : 1 ≤ B := scale_one p.ix - let c := vacant M - have hc : c.chart.Sound := classic_sound M - have hS : share s=1 := entry_share hp hh - have HT := mint_small p c s t h hc - (show soundPrev c from fun s hs => by cases hs) - (show p.cardStep c from by simp [cardStep,c,vacant,classic,Chart.size]) hp.pos - have HP : deficit s (channel (optic s p.main))+(minimumT j)⁻¹ ≤ 10^24*x := - hj.2 M J s f (Thread.rooted _ h) hp hh hF _ - have hh : (p.creditsC c)=D := by simp [creditsC,c,vacant,classic,rangeIx,D,scaleC,Chart.size] - constructor - · have HH : p.debtC c s j ≤ 10^24*x+100*(B*D^3) := by - have H₁ := (sight_budget s p.main c.chart.items (precision p.ix p.cap)).2 - change deficit s (p.kObs c s) ≤ _ at H₁ - rw [show c.chart.items.card=0 by simp [c,vacant,classic],Nat.cast_zero,zero_mul,add_zero] at H₁ - unfold debtC; rw [hh] - have hF₁ : D*p.coinC ≤ 100*(B*D^3) := by - have H := prescale p - have hZ : p.coinC = D*(precision p.ix p.cap) := by simp [coinC,rangeIx,D,scaleC] - rw [hZ] - change _ ≤ 100*(B*D) at H - have hv := mul_le_mul_of_nonneg_left H (sq_nonneg D); linarith - linarith - have hh₂ : p.slotC c ≤ D+1 := by - unfold slotC; rw [hh,ite_eq_left (show c.previous=none from rfl)] - exact mul_le_of_le_one_left (by positivity) (marker_one p).1 - have hh₃ : p.expenses c s j ≤ 10^26* (B*x*D^4) := by - unfold expenses; rw [hS,show p.deltaC c s=0 from rfl] - have HX : 0 ≤ B*D^3 := by positivity - have HL : B*D^3 ≤ B*x*D^4 := by gcongr; all_goals nlinarith - have HT : D ≤ B*D^3 := (by calc - D ≤ D^3 := by simpa using pow_le_pow_right₀ HD (show 1 ≤ 3 by omega) - _ ≤ _ := by nlinarith [pow_nonneg (le_trans (by norm_num) HD) 3]) - have HZ : x ≤ B*x*D^4 := by apply le_trans (b:=B*x) (by nlinarith); exact le_mul_of_one_le_right (by positivity) (one_le_pow₀ HD) - nlinarith - have HI : stats t (minted p c) ≤ (1/p.marker)*p.expenses c s j := by - rw [one_div,← div_eq_inv_mul] - apply (le_div_iff₀ (p.marker_pos)).mpr - rw [mul_comm]; exact HT - apply HI.trans - have hz := (marker_one p).2 - calc - _ ≤ (10^14*B^12)*(10^26*(B*x*D^4)) := by - exact mul_le_mul hz hh₃ (expenses_nonneg p c _ j - (show p.cardStep c from by simp [cardStep,c,vacant,classic,Chart.size]) hp.ready - (show soundPrev c from fun s hs => by cases hs) hp.pos) (by positivity) - _ ≤ _ := by - have HV := pow_le_pow_right₀ HB (show 13≤14 by omega) - nlinarith [mul_le_mul_of_nonneg_right HV (show 0 ≤ x*D^4 by positivity), - show 0 ≤ B^14*x*D^4 by positivity] - · have H := buildup_total s t f h p.main p.ix c.chart hc hp.pos - let r := amp p.ix p.main; let m := stats t (fun l => (broadMass l g:ℝ)) - have hr := amp_pos p.ix p.main; change 0 < r at hr - have Heq (x' : Grid M) : - buildup p.ix p.main c.chart (t.stream x')=(broadMass (t.stream x') g:ℝ) := by - unfold buildup; rw [show c.chart.size=0 by simp [c,vacant,classic,Chart.size]] - simp only [Nat.cast_zero,sub_zero]; rfl - simp_rw [Heq] at H - rw [show s.mass/s.norm=_ from hS,one_mul - ] at H - have hh : chartDebt s j c.chart p.main ≤ 10^24*x := by - unfold chartDebt; apply le_trans _ HP - change flex s p.main (fun _ _ => 1)+_ ≤ _; rw [flex_one] - change (r/2*(r^2/2048))*m ≤ _ at H - have hi : 1/B^2 ≤ r := by - have HE := (strength_bounds p.main (side_pos p.ix) (side_le p.ix)).2.2 - rw [scale_side] at HE - unfold r amp; rw [scale_side]; unfold B; simpa only [one_div,inv_pow] using HE - change m ≤ _ - have hT : 4096 * 10^24*x / r^3 ≤ 4096*10^24*x*B^6 := by - have hB : 0 < B := by positivity - calc - _ ≤ 4096*10^24*x/(1/B^2)^3 := by gcongr - _ = _ := by field_simp - apply le_trans (le_trans ?_ hT) - · have H : 0 ≤ x*B^6 := by positivity - linarith - apply (le_div_iff₀ (pow_pos hr _)).mpr; nlinarith - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Troupe +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe +open Plot + +def entry (s : Scene M) := s.norm=s.mass + +lemma entry_share {s : Scene M} (h : Fit s) (e : entry s) : share s=1 := by + unfold share entry at *; rw [←e,div_self h.pos.ne'] + +lemma old_budget : + ∀ᶠ j : ℕ in atTop, + (minimumT j)⁻¹ ≤ height j ∧ + ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + s.Safe j J f → Fit s → entry s → + (∀ x, f x ≤ exp (height j)) → + ∀ b : Bool, deficit s (channel (optic s b))+(minimumT j)⁻¹ ≤ 10^24*amount s j := by + filter_upwards [side_deficit,hpoly 12800 32] with j ht hh + have H : (minimumT j)⁻¹ ≤ height j := by unfold minimumT; rw [one_div,inv_inv]; exact hh + refine ⟨H,?_⟩ + intro M J s f h hp e hf b + let x := height j; let y := amount s j + have hm : 0 < s.mass := e ▸ hp.pos + have hx := (amount_limits s j).2 + have hy : -log s.mass ≤ y := by + unfold entry at e; rw [←e]; unfold y amount + linarith [d_pos j,le_max_left (-log s.norm) 0] + have hZ := ht M J s f h hf hm + cases b + · unfold optic; rw [ite_eq_right (by simp),funext no_label] + have H₁ := local_deficit_dry s (uniform (Grid M)) hp.ready.1 hp.ready.2 hm x + (fun i => (h.sub i).trans (hf i)) + unfold y x at * + linarith [d_pos j] + · simp only [optic,ite_true] + unfold y at *; linarith [d_pos j] + +def scaleC (i : ℕ) : ℝ := 2^i +lemma scale_one (i : ℕ) : 1 ≤ scaleC i := one_le_pow₀ (by norm_num) +lemma scale_pos (i : ℕ) : 0 < scaleC i := lt_of_lt_of_le zero_lt_one (scale_one i) +lemma scale_side (i : ℕ) : side i = (scaleC i)⁻¹ := by unfold scaleC; rw [side_pow_two] + +lemma prescale (p : Plot) : (precision p.ix p.cap:ℝ) ≤ 100*(scaleC p.ix* scaleC p.cap) := by + have H (i : ℕ) : (i:ℝ) ≤ scaleC i := by + unfold scaleC; exact_mod_cast Nat.lt_two_pow_self.le + have hi := scale_one p.cap; have he := scale_one p.ix + unfold precision + push_cast; nlinarith [H p.cap,H p.ix] + +lemma marker_one (p : Plot) : p.marker ≤ 1 ∧ 1/p.marker ≤ 10^14 * (scaleC p.ix)^12 := by + let x := side p.ix; let v := scaleC p.ix + have hx : 0 < x := side_pos _; have hv : 0 < v := scale_pos _ + have ht : x = v⁻¹ := scale_side _ + have he₁ : x^6 ≤ 1 := pow_le_one₀ hx.le (side_le _) + unfold marker sharp; change (x^6/32768)^2/16 ≤ 1 ∧ _ ≤ _ + constructor + · nlinarith [pow_pos hx 6] + · change 1/((x^6/32768)^2/16) ≤ 10^14*v^12 + rw [ht] + field_simp + nlinarith [pow_pos hv 12] + +def broadMass (l : Path M) (g : Grade) : ℕ := + (goal g.i g.main (Chart.classic M) l).size + +lemma prelude : + ∀ᶠ j : ℕ in atTop, + ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ t : Thread M s, + t.Following j J f → Fit s → entry s → + (∀ x, f x ≤ exp (height j)) → + ∀ p : Plot, + let x := amount s j + let g := grade p + let B := scaleC p.ix + let D := scaleC p.cap + stats t (minted p (vacant M)) ≤ 10^50*B^14*x*D^4 ∧ + stats t (fun l => (broadMass l g:ℝ)) ≤ 10^31*B^6*x := by + filter_upwards [old_budget] with j hj M J s f t h hp hh hF p x g B D + have he : 1 ≤ x := (amount_limits s j).1 + have HD : 1 ≤ D := scale_one p.cap; have HB : 1 ≤ B := scale_one p.ix + let c := vacant M + have hc : c.chart.Sound := classic_sound M + have hS : share s=1 := entry_share hp hh + have HT := mint_small p c s t h hc + (show soundPrev c from fun s hs => by cases hs) + (show p.cardStep c from by simp [cardStep,c,vacant,classic,Chart.size]) hp.pos + have HP : deficit s (channel (optic s p.main))+(minimumT j)⁻¹ ≤ 10^24*x := + hj.2 M J s f (Thread.rooted _ h) hp hh hF _ + have hh : (p.creditsC c)=D := by simp [creditsC,c,vacant,classic,rangeIx,D,scaleC,Chart.size] + constructor + · have HH : p.debtC c s j ≤ 10^24*x+100*(B*D^3) := by + have H₁ := (sight_budget s p.main c.chart.items (precision p.ix p.cap)).2 + change deficit s (p.kObs c s) ≤ _ at H₁ + rw [show c.chart.items.card=0 by simp [c,vacant,classic],Nat.cast_zero,zero_mul,add_zero] at H₁ + unfold debtC; rw [hh] + have hF₁ : D*p.coinC ≤ 100*(B*D^3) := by + have H := prescale p + have hZ : p.coinC = D*(precision p.ix p.cap) := by simp [coinC,rangeIx,D,scaleC] + rw [hZ] + change _ ≤ 100*(B*D) at H + have hv := mul_le_mul_of_nonneg_left H (sq_nonneg D); linarith + linarith + have hh₂ : p.slotC c ≤ D+1 := by + unfold slotC; rw [hh,ite_eq_left (show c.previous=none from rfl)] + exact mul_le_of_le_one_left (by positivity) (marker_one p).1 + have hh₃ : p.expenses c s j ≤ 10^26* (B*x*D^4) := by + unfold expenses; rw [hS,show p.deltaC c s=0 from rfl] + have HX : 0 ≤ B*D^3 := by positivity + have HL : B*D^3 ≤ B*x*D^4 := by gcongr; all_goals nlinarith + have HT : D ≤ B*D^3 := (by calc + D ≤ D^3 := by simpa using pow_le_pow_right₀ HD (show 1 ≤ 3 by omega) + _ ≤ _ := by nlinarith [pow_nonneg (le_trans (by norm_num) HD) 3]) + have HZ : x ≤ B*x*D^4 := by apply le_trans (b:=B*x) (by nlinarith); exact le_mul_of_one_le_right (by positivity) (one_le_pow₀ HD) + nlinarith + have HI : stats t (minted p c) ≤ (1/p.marker)*p.expenses c s j := by + rw [one_div,← div_eq_inv_mul] + apply (le_div_iff₀ (p.marker_pos)).mpr + rw [mul_comm]; exact HT + apply HI.trans + have hz := (marker_one p).2 + calc + _ ≤ (10^14*B^12)*(10^26*(B*x*D^4)) := by + exact mul_le_mul hz hh₃ (expenses_nonneg p c _ j + (show p.cardStep c from by simp [cardStep,c,vacant,classic,Chart.size]) hp.ready + (show soundPrev c from fun s hs => by cases hs) hp.pos) (by positivity) + _ ≤ _ := by + have HV := pow_le_pow_right₀ HB (show 13≤14 by omega) + nlinarith [mul_le_mul_of_nonneg_right HV (show 0 ≤ x*D^4 by positivity), + show 0 ≤ B^14*x*D^4 by positivity] + · have H := buildup_total s t f h p.main p.ix c.chart hc hp.pos + let r := amp p.ix p.main; let m := stats t (fun l => (broadMass l g:ℝ)) + have hr := amp_pos p.ix p.main; change 0 < r at hr + have Heq (x' : Grid M) : + buildup p.ix p.main c.chart (t.stream x')=(broadMass (t.stream x') g:ℝ) := by + unfold buildup; rw [show c.chart.size=0 by simp [c,vacant,classic,Chart.size]] + simp only [Nat.cast_zero,sub_zero]; rfl + simp_rw [Heq] at H + rw [show s.mass/s.norm=_ from hS,one_mul + ] at H + have hh : chartDebt s j c.chart p.main ≤ 10^24*x := by + unfold chartDebt; apply le_trans _ HP + change flex s p.main (fun _ _ => 1)+_ ≤ _; rw [flex_one] + change (r/2*(r^2/2048))*m ≤ _ at H + have hi : 1/B^2 ≤ r := by + have HE := (strength_bounds p.main (side_pos p.ix) (side_le p.ix)).2.2 + rw [scale_side] at HE + unfold r amp; rw [scale_side]; unfold B; simpa only [one_div,inv_pow] using HE + change m ≤ _ + have hT : 4096 * 10^24*x / r^3 ≤ 4096*10^24*x*B^6 := by + have hB : 0 < B := by positivity + calc + _ ≤ 4096*10^24*x/(1/B^2)^3 := by gcongr + _ = _ := by field_simp + apply le_trans (le_trans ?_ hT) + · have H : 0 ≤ x*B^6 := by positivity + linarith + apply (le_div_iff₀ (pow_pos hr _)).mpr; nlinarith + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Tilt.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Tilt.lean index 11ba28c24b95aea1774b1f74a2bd8911e49ee5c7..a72f3335e8de535a35cc7ef77b130d2b9918930e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Tilt.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Tilt.lean @@ -1,205 +1,205 @@ -import OAI.Analysis.FourierLLogL.Paths.Letters -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -/-! At scalar levels it is convenient to analyze each of the four real projections (± real, ± imaginary). -/ - -abbrev Face := Bool × Bool -def part (d : Face) (x : ℂ) := curl d.2*(if d.1=true then x.im else x.re) -lemma part_bounded (d : Face) (x : ℂ) : |part d x| ≤ ‖x‖ := by - rw [part,abs_mul,curl_unit,one_mul] - split; exact Complex.abs_im_le_norm _; exact Complex.abs_re_le_norm _ -lemma part_zero (d : Face) : part d 0=0 := by rcases d with ⟨b,c⟩; cases b <;> simp [part] -lemma part_add (d : Face) (x y : ℂ) : part d (x+y)=part d x+part d y := by - rcases d with ⟨b,c⟩; cases b <;> simp [part,mul_add] -lemma part_diff (d : Face) (x y : ℂ) : part d (x-y)=part d x-part d y := by - rcases d with ⟨b,c⟩; cases b <;> simp [part,mul_sub] -lemma part_mul (d : Face) (x : ℂ) (c : ℝ) : part d (c*x)=c*part d x := by - rcases d with ⟨b,r⟩; cases b <;> simp [part,mul_left_comm] - -/-- Local Huber function on a window of length 10 b. -/ -private def slide (w x : ℝ) := if x < -w then -w else if w < x then w else x -private def smooth (w x : ℝ) := - 2*x*slide w x-(slide w x)^2 -private lemma slide_range {w x : ℝ} (hw : 0 ≤ w) : |slide w x| ≤ w := by - rw [abs_le]; unfold slide - split_ifs <;> constructor <;> linarith - -private lemma slide_inner {w x : ℝ} (hx : |x| ≤ w) : slide w x=x := by - rw [abs_le] at hx; unfold slide - split_ifs <;> linarith - -private lemma smooth_inner {w x : ℝ} (hx : |x| ≤ w) : smooth w x=x^2 := by - rw [smooth,slide_inner hx]; ring - -private lemma slide_pay {w x y : ℝ} (_hw : 0 ≤ w) (hy : |y| ≤ w) : - 2*x*y-y^2 ≤ smooth w x := by - rw [abs_le] at hy - unfold smooth slide; split_ifs with h h' - · nlinarith - · nlinarith - nlinarith [sq_nonneg (x-y)] - -private lemma smooth_mid {w : ℝ} (x u : ℝ) (hw : 0 ≤ w) : - smooth w x ≤ (smooth w (x+u)+smooth w (x-u))/2 := by - have H (t : ℝ) := slide_pay (x:=t) hw (slide_range (x:=x) hw) - rw [smooth] - nlinarith [H (x+u), H (x-u)] - -private lemma smooth_nonneg {w : ℝ} (x : ℝ) (hw : 0 ≤ w) : 0 ≤ smooth w x := by - have H := slide_pay (x:=x) hw (by simpa using hw : |(0:ℝ)| ≤ w) - simpa using H - -private lemma smooth_lip {w : ℝ} (x y : ℝ) (hw : 0 ≤ w) : - |smooth w x-smooth w y| ≤ 2*w * |x-y| := by - have H (x y : ℝ) : smooth w x-smooth w y ≤ 2*w*|x-y| := by - have He := slide_range (x:=x) hw - have hi := slide_pay (x:=y) hw He - have hh : (x-y)*slide w x ≤ |x-y| *w := by - apply le_trans (le_abs_self _) - rw [abs_mul]; gcongr - conv_lhs => lhs; rw [smooth] - linarith - rw [abs_le] - have hx := H y x; rw [abs_sub_comm] at hx - constructor <;> linarith [H x y] - -private lemma sq_lip (x y : ℝ) (hx : |x| ≤ 4) (hy : |y| ≤ 4) : - |x^2-y^2| ≤ 8*|x-y| := by - rw [show x^2-y^2=(x+y)*(x-y) by ring,abs_mul] - apply mul_le_mul_of_nonneg_right _ (abs_nonneg _) - linarith [abs_add_le x y] - -def tilt (b x : ℝ) := smooth (10*b) x+b*x^2 -lemma tilt_lip (b : ℝ) (h : 0 ≤ b) (x y : ℝ) (hx : |x| ≤ 4) (hy : |y| ≤ 4) : - |tilt b x-tilt b y| ≤ 30*b* |x-y| := by - let w := 10*b - have he := smooth_lip (w:=w) x y (by unfold w; positivity) - rw [tilt,tilt,show smooth (10*b) x+b*x^2-(smooth (10*b) y+b*y^2)=smooth w x-smooth w y+b*(x^2-y^2) by unfold w; ring] - apply le_trans (abs_add_le _ _) - rw [abs_mul,abs_of_nonneg h] - have H := mul_le_mul_of_nonneg_left (sq_lip x y hx hy) h - unfold w at * - nlinarith [abs_nonneg (x-y),mul_nonneg h (abs_nonneg (x-y))] - -lemma tilt_bounds (b : ℝ) (h : 0 ≤ b) (x : ℝ) (hx : |x| ≤ 4) : - 0 ≤ tilt b x ∧ tilt b x ≤ 120*b := by - have he : 0 ≤ 10*b := by positivity - have hh : tilt b 0=0 := by rw [tilt,smooth_inner (by simpa using he)]; ring - refine ⟨add_nonneg (smooth_nonneg x he) (by positivity),?_⟩ - have hi := tilt_lip b h x 0 hx (by norm_num); rw [hh,sub_zero,sub_zero] at hi - exact ((le_abs_self _).trans hi).trans (by nlinarith) - -lemma tilt_rise (b : ℝ) (h : 0 ≤ b) (x u : ℝ) : - let D := (tilt b (x+u)+tilt b (x-u))/2-tilt b x - b*u^2 ≤ D ∧ (|x+u| ≤ 10*b → |x-u| ≤ 10*b → u^2 ≤ D) := by - let w := 10*b - intro D - have H1 : D=(smooth w (x+u)+smooth w (x-u))/2-smooth w x+b*u^2 := by - unfold D w tilt; ring - rw [H1] - constructor - · have HE := smooth_mid x u (by unfold w; positivity : 0 ≤ w); linarith - intro hx hy - change _ ≤ w at hx hy - rw [smooth_inner hx,smooth_inner hy,smooth_inner (x:=x)]; nlinarith [mul_nonneg h (sq_nonneg u)] - rw [abs_le] at hx hy ⊢; constructor <;> linarith - -def guarded (u : Grid M) (b : ℝ) (s : Scene M) : Prop := - ∀ i, ‖scalar (s.smaller i) u‖ ≤ 10*b - -def prize (u : Grid M) (b : ℝ) (s : Scene M) := if guarded u b s then gain s u else 0 -def frosty (x u : Grid M) (b : ℝ) (B : Beat M) := - (blow x B:ℂ)*prize u b B.post -lemma prize_norm {b : ℝ} (hb : 0 ≤ b) (u : Grid M) (s : Scene M) : - ‖prize u b s‖ ≤ 10*b := by - unfold prize; split_ifs with H - · apply (norm_halfcomplex _ _).trans; linarith [H false,H true] - simp; linarith - -/-- analyzing scalar path with a chosen signed component -/ -structure Lens (M : ℕ) where - focus : Grid M - rate : ℝ - face : Face -namespace Lens -def dot (g : Lens M) (s : Scene M) := part g.face (scalar s g.focus) -def cash (g : Lens M) (s : Scene M) := part g.face (gain s g.focus) -def coin (g : Lens M) (s : Scene M) := part g.face (prize g.focus g.rate s) -def hero (g : Lens M) (s : Scene M) := tilt g.rate (g.dot s) -def flow (g : Lens M) (s : Scene M) := - (g.hero (s.smaller false)+g.hero (s.smaller true))/2-g.hero s -def swing (g : Lens M) (s : Scene M) := - (g.hero (s.smaller false)-g.hero (s.smaller true))/2 - -lemma hero_walk (g : Lens M) (s : Scene M) (b : Bool) : - hero g (s.smaller b)=g.hero s+g.flow s+ curl b*g.swing s := by - unfold flow swing - cases b <;> norm_num [curl] <;> ring - -lemma dot_walk (g : Lens M) (s : Scene M) (hs : Climb s) (b) : - g.dot (s.smaller b) = g.dot s+curl b*g.cash s := by - rw [dot,scalar_jump hs b,part_add,part_mul]; rfl - -lemma dot_limit (g : Lens M) (s : Scene M) (hp : Fit s) : |g.dot s| ≤ 4 := - (part_bounded g.face _).trans ((scalar_norm hp _).trans hp.lift) - -lemma hero_limit (g : Lens M) (hr : 0 ≤ g.rate) (hp : Fit s) : - 0 ≤ g.hero s ∧ g.hero s ≤ 120*g.rate := - tilt_bounds g.rate hr _ (dot_limit g s hp) - -lemma coin_flow (g : Lens M) (hp : 0 ≤ g.rate) (hs : Climb s) : - (g.coin s)^2 ≤ g.flow s ∧ g.rate*(g.cash s)^2 ≤ g.flow s := by - have Hx := g.dot_walk s hs false - have Hy := g.dot_walk s hs true - simp only [curl,↓reduceIte,Bool.false_eq_true,one_mul,neg_mul,← sub_eq_add_neg] at Hx Hy - have HE := tilt_rise _ hp (g.dot s) (g.cash s) - unfold flow hero - rw [Hx,Hy] - refine ⟨?_,HE.1⟩ - unfold coin prize; split_ifs with H - · have He (b : Bool) : |g.dot (s.smaller b)| ≤ 10*g.rate := (part_bounded _ _).trans (H _) - apply HE.2 (Hx ▸ He false) (Hy ▸ He true) - rw [part_zero] - exact le_trans (by simpa only [ne_eq,OfNat.ofNat_ne_zero,not_false_eq_true,zero_pow] using - (mul_nonneg hp (sq_nonneg (cash g s)))) HE.1 - -lemma swing_flow (g : Lens M) (hp : 0 ≤ g.rate) (hs : Climb s) : - (g.swing s)^2 ≤ 900*g.rate*(g.flow s) := by - have Hx := g.dot_limit _ (hs.child false); have Hy := g.dot_limit _ (hs.child true) - have H := tilt_lip _ hp _ _ Hx Hy - have he : dot g (s.smaller false)-dot g (s.smaller true)=2*cash g s := by - rw [dot_walk _ _ hs, dot_walk _ _ hs]; norm_num [curl]; ring - rw [he,abs_mul,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] at H - change |hero g _-hero g _| ≤ _ at H - have HH : |swing g s| ≤ 30*g.rate* |g.cash s| := by - rw [swing,abs_div,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] - linarith - have HV := pow_le_pow_left₀ (abs_nonneg _) HH 2 - rw [sq_abs,mul_pow,sq_abs] at HV - nlinarith [mul_le_mul_of_nonneg_left (coin_flow g hp hs).2 hp] - -lemma hero_drain (g : Lens M) (hp : 0 ≤ g.rate) {b : Beat M} (hs : Ideal b.pre b.post) : - |g.hero b.pre-g.hero b.post| ≤ 30*g.rate*killed b := by - apply (tilt_lip _ hp _ _ (g.dot_limit _ hs.fit) (g.dot_limit _ hs.climb.fit)).trans - apply mul_le_mul_of_nonneg_left _ (by positivity) - unfold dot; rw [←part_diff] - exact (part_bounded _ _).trans (scalar_drain hs _) - -end Lens - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Letters +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +/-! At scalar levels it is convenient to analyze each of the four real projections (± real, ± imaginary). -/ + +abbrev Face := Bool × Bool +def part (d : Face) (x : ℂ) := curl d.2*(if d.1=true then x.im else x.re) +lemma part_bounded (d : Face) (x : ℂ) : |part d x| ≤ ‖x‖ := by + rw [part,abs_mul,curl_unit,one_mul] + split; exact Complex.abs_im_le_norm _; exact Complex.abs_re_le_norm _ +lemma part_zero (d : Face) : part d 0=0 := by rcases d with ⟨b,c⟩; cases b <;> simp [part] +lemma part_add (d : Face) (x y : ℂ) : part d (x+y)=part d x+part d y := by + rcases d with ⟨b,c⟩; cases b <;> simp [part,mul_add] +lemma part_diff (d : Face) (x y : ℂ) : part d (x-y)=part d x-part d y := by + rcases d with ⟨b,c⟩; cases b <;> simp [part,mul_sub] +lemma part_mul (d : Face) (x : ℂ) (c : ℝ) : part d (c*x)=c*part d x := by + rcases d with ⟨b,r⟩; cases b <;> simp [part,mul_left_comm] + +/-- Local Huber function on a window of length 10 b. -/ +private def slide (w x : ℝ) := if x < -w then -w else if w < x then w else x +private def smooth (w x : ℝ) := + 2*x*slide w x-(slide w x)^2 +private lemma slide_range {w x : ℝ} (hw : 0 ≤ w) : |slide w x| ≤ w := by + rw [abs_le]; unfold slide + split_ifs <;> constructor <;> linarith + +private lemma slide_inner {w x : ℝ} (hx : |x| ≤ w) : slide w x=x := by + rw [abs_le] at hx; unfold slide + split_ifs <;> linarith + +private lemma smooth_inner {w x : ℝ} (hx : |x| ≤ w) : smooth w x=x^2 := by + rw [smooth,slide_inner hx]; ring + +private lemma slide_pay {w x y : ℝ} (_hw : 0 ≤ w) (hy : |y| ≤ w) : + 2*x*y-y^2 ≤ smooth w x := by + rw [abs_le] at hy + unfold smooth slide; split_ifs with h h' + · nlinarith + · nlinarith + nlinarith [sq_nonneg (x-y)] + +private lemma smooth_mid {w : ℝ} (x u : ℝ) (hw : 0 ≤ w) : + smooth w x ≤ (smooth w (x+u)+smooth w (x-u))/2 := by + have H (t : ℝ) := slide_pay (x:=t) hw (slide_range (x:=x) hw) + rw [smooth] + nlinarith [H (x+u), H (x-u)] + +private lemma smooth_nonneg {w : ℝ} (x : ℝ) (hw : 0 ≤ w) : 0 ≤ smooth w x := by + have H := slide_pay (x:=x) hw (by simpa using hw : |(0:ℝ)| ≤ w) + simpa using H + +private lemma smooth_lip {w : ℝ} (x y : ℝ) (hw : 0 ≤ w) : + |smooth w x-smooth w y| ≤ 2*w * |x-y| := by + have H (x y : ℝ) : smooth w x-smooth w y ≤ 2*w*|x-y| := by + have He := slide_range (x:=x) hw + have hi := slide_pay (x:=y) hw He + have hh : (x-y)*slide w x ≤ |x-y| *w := by + apply le_trans (le_abs_self _) + rw [abs_mul]; gcongr + conv_lhs => lhs; rw [smooth] + linarith + rw [abs_le] + have hx := H y x; rw [abs_sub_comm] at hx + constructor <;> linarith [H x y] + +private lemma sq_lip (x y : ℝ) (hx : |x| ≤ 4) (hy : |y| ≤ 4) : + |x^2-y^2| ≤ 8*|x-y| := by + rw [show x^2-y^2=(x+y)*(x-y) by ring,abs_mul] + apply mul_le_mul_of_nonneg_right _ (abs_nonneg _) + linarith [abs_add_le x y] + +def tilt (b x : ℝ) := smooth (10*b) x+b*x^2 +lemma tilt_lip (b : ℝ) (h : 0 ≤ b) (x y : ℝ) (hx : |x| ≤ 4) (hy : |y| ≤ 4) : + |tilt b x-tilt b y| ≤ 30*b* |x-y| := by + let w := 10*b + have he := smooth_lip (w:=w) x y (by unfold w; positivity) + rw [tilt,tilt,show smooth (10*b) x+b*x^2-(smooth (10*b) y+b*y^2)=smooth w x-smooth w y+b*(x^2-y^2) by unfold w; ring] + apply le_trans (abs_add_le _ _) + rw [abs_mul,abs_of_nonneg h] + have H := mul_le_mul_of_nonneg_left (sq_lip x y hx hy) h + unfold w at * + nlinarith [abs_nonneg (x-y),mul_nonneg h (abs_nonneg (x-y))] + +lemma tilt_bounds (b : ℝ) (h : 0 ≤ b) (x : ℝ) (hx : |x| ≤ 4) : + 0 ≤ tilt b x ∧ tilt b x ≤ 120*b := by + have he : 0 ≤ 10*b := by positivity + have hh : tilt b 0=0 := by rw [tilt,smooth_inner (by simpa using he)]; ring + refine ⟨add_nonneg (smooth_nonneg x he) (by positivity),?_⟩ + have hi := tilt_lip b h x 0 hx (by norm_num); rw [hh,sub_zero,sub_zero] at hi + exact ((le_abs_self _).trans hi).trans (by nlinarith) + +lemma tilt_rise (b : ℝ) (h : 0 ≤ b) (x u : ℝ) : + let D := (tilt b (x+u)+tilt b (x-u))/2-tilt b x + b*u^2 ≤ D ∧ (|x+u| ≤ 10*b → |x-u| ≤ 10*b → u^2 ≤ D) := by + let w := 10*b + intro D + have H1 : D=(smooth w (x+u)+smooth w (x-u))/2-smooth w x+b*u^2 := by + unfold D w tilt; ring + rw [H1] + constructor + · have HE := smooth_mid x u (by unfold w; positivity : 0 ≤ w); linarith + intro hx hy + change _ ≤ w at hx hy + rw [smooth_inner hx,smooth_inner hy,smooth_inner (x:=x)]; nlinarith [mul_nonneg h (sq_nonneg u)] + rw [abs_le] at hx hy ⊢; constructor <;> linarith + +def guarded (u : Grid M) (b : ℝ) (s : Scene M) : Prop := + ∀ i, ‖scalar (s.smaller i) u‖ ≤ 10*b + +def prize (u : Grid M) (b : ℝ) (s : Scene M) := if guarded u b s then gain s u else 0 +def frosty (x u : Grid M) (b : ℝ) (B : Beat M) := + (blow x B:ℂ)*prize u b B.post +lemma prize_norm {b : ℝ} (hb : 0 ≤ b) (u : Grid M) (s : Scene M) : + ‖prize u b s‖ ≤ 10*b := by + unfold prize; split_ifs with H + · apply (norm_halfcomplex _ _).trans; linarith [H false,H true] + simp; linarith + +/-- analyzing scalar path with a chosen signed component -/ +structure Lens (M : ℕ) where + focus : Grid M + rate : ℝ + face : Face +namespace Lens +def dot (g : Lens M) (s : Scene M) := part g.face (scalar s g.focus) +def cash (g : Lens M) (s : Scene M) := part g.face (gain s g.focus) +def coin (g : Lens M) (s : Scene M) := part g.face (prize g.focus g.rate s) +def hero (g : Lens M) (s : Scene M) := tilt g.rate (g.dot s) +def flow (g : Lens M) (s : Scene M) := + (g.hero (s.smaller false)+g.hero (s.smaller true))/2-g.hero s +def swing (g : Lens M) (s : Scene M) := + (g.hero (s.smaller false)-g.hero (s.smaller true))/2 + +lemma hero_walk (g : Lens M) (s : Scene M) (b : Bool) : + hero g (s.smaller b)=g.hero s+g.flow s+ curl b*g.swing s := by + unfold flow swing + cases b <;> norm_num [curl] <;> ring + +lemma dot_walk (g : Lens M) (s : Scene M) (hs : Climb s) (b) : + g.dot (s.smaller b) = g.dot s+curl b*g.cash s := by + rw [dot,scalar_jump hs b,part_add,part_mul]; rfl + +lemma dot_limit (g : Lens M) (s : Scene M) (hp : Fit s) : |g.dot s| ≤ 4 := + (part_bounded g.face _).trans ((scalar_norm hp _).trans hp.lift) + +lemma hero_limit (g : Lens M) (hr : 0 ≤ g.rate) (hp : Fit s) : + 0 ≤ g.hero s ∧ g.hero s ≤ 120*g.rate := + tilt_bounds g.rate hr _ (dot_limit g s hp) + +lemma coin_flow (g : Lens M) (hp : 0 ≤ g.rate) (hs : Climb s) : + (g.coin s)^2 ≤ g.flow s ∧ g.rate*(g.cash s)^2 ≤ g.flow s := by + have Hx := g.dot_walk s hs false + have Hy := g.dot_walk s hs true + simp only [curl,↓reduceIte,Bool.false_eq_true,one_mul,neg_mul,← sub_eq_add_neg] at Hx Hy + have HE := tilt_rise _ hp (g.dot s) (g.cash s) + unfold flow hero + rw [Hx,Hy] + refine ⟨?_,HE.1⟩ + unfold coin prize; split_ifs with H + · have He (b : Bool) : |g.dot (s.smaller b)| ≤ 10*g.rate := (part_bounded _ _).trans (H _) + apply HE.2 (Hx ▸ He false) (Hy ▸ He true) + rw [part_zero] + exact le_trans (by simpa only [ne_eq,OfNat.ofNat_ne_zero,not_false_eq_true,zero_pow] using + (mul_nonneg hp (sq_nonneg (cash g s)))) HE.1 + +lemma swing_flow (g : Lens M) (hp : 0 ≤ g.rate) (hs : Climb s) : + (g.swing s)^2 ≤ 900*g.rate*(g.flow s) := by + have Hx := g.dot_limit _ (hs.child false); have Hy := g.dot_limit _ (hs.child true) + have H := tilt_lip _ hp _ _ Hx Hy + have he : dot g (s.smaller false)-dot g (s.smaller true)=2*cash g s := by + rw [dot_walk _ _ hs, dot_walk _ _ hs]; norm_num [curl]; ring + rw [he,abs_mul,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] at H + change |hero g _-hero g _| ≤ _ at H + have HH : |swing g s| ≤ 30*g.rate* |g.cash s| := by + rw [swing,abs_div,abs_of_nonneg (by norm_num : (0:ℝ)≤2)] + linarith + have HV := pow_le_pow_left₀ (abs_nonneg _) HH 2 + rw [sq_abs,mul_pow,sq_abs] at HV + nlinarith [mul_le_mul_of_nonneg_left (coin_flow g hp hs).2 hp] + +lemma hero_drain (g : Lens M) (hp : 0 ≤ g.rate) {b : Beat M} (hs : Ideal b.pre b.post) : + |g.hero b.pre-g.hero b.post| ≤ 30*g.rate*killed b := by + apply (tilt_lip _ hp _ _ (g.dot_limit _ hs.fit) (g.dot_limit _ hs.climb.fit)).trans + apply mul_le_mul_of_nonneg_left _ (by positivity) + unfold dot; rw [←part_diff] + exact (part_bounded _ _).trans (scalar_drain hs _) + +end Lens + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Trip.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Trip.lean index 20d7afb3c7754c76c0cd11680b08fd699fe2d356..bb5908709c58ba052bcf9d0299ab53673ec68749 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Trip.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Trip.lean @@ -1,173 +1,173 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Activate -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -abbrev Path (M : ℕ) := List (Beat M) - -def winds (b : Beat M) := b.post.smaller b.bit -def curl (b : Bool) : ℝ := if b=true then -1 else 1 - -lemma curl_unit (b : Bool) : |curl b|=1 := by cases b <;> norm_num [curl] -lemma curl_sq (b : Bool) : (curl b)^2=1 := by cases b <;> norm_num [curl] -lemma fair_av (f : Bool→ℝ) : (uniform Bool).Ex f = (f false+f true)/2 := by - simp only [Ex,Fintype.sum_bool,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] - ring - -structure Fit (s : Scene M) : Prop where - ready : ReadyAt s - pos : 0 < s.norm - lift : share s ≤ 4 - -structure Climb (s : Scene M) : Prop where - fit : Fit s - deep : s.here.lev < M - tight : share s ≤ 2 - -structure Ideal (pre post : Scene M) : Prop where - fit : Fit pre - climb : Climb post - lower : Less pre post - -lemma jump_ideal {k} (h : Following j J f (.ran s q k)) - (hq : share s ≤ 4) : Ideal s q := by - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - obtain ⟨hz,hH⟩ := share_small hd - refine ⟨⟨⟨ha.within,ha.positive⟩,hb.1.flat ▸ hz,hq⟩,?_,hb.1⟩ - exact ⟨⟨⟨hc.within,hc.positive⟩,hz,hH.trans (by norm_num)⟩,(rooted _ (hk false)).within.2,hH⟩ - -lemma Climb.child {q : Scene M} (h : Climb q) (b) : Fit (q.smaller b) := - let hx := h.fit.ready - ⟨⟨q.here.child_ok hx.1.1 h.deep b,hx.2⟩,h.fit.pos, - share_child q hx.1.1 h.deep hx.2 h.fit.pos h.tight b⟩ - -lemma Ideal.kill_eq {B : Beat M} (h : Ideal B.pre B.post) : - killed B = share B.pre-share B.post := by - rw [killed,sub_div]; unfold share; rw [h.lower.flat] -lemma Ideal.kill_nonneg {B : Beat M} (h : Ideal B.pre B.post) : - 0 ≤ killed B := div_nonneg (sub_nonneg.mpr - (h.lower.mass_le h.fit.ready.2 h.fit.ready.1)) h.fit.pos.le - -/-- deterministic integrity along a plateau list. -/ -def Trip (x : Grid M) (s : Scene M) : Path M → Prop - | [] => Fit s ∧ x∈s.here.block M - | b::l => - b.pre=s ∧ Ideal b.pre b.post ∧ Trip x (winds b) l - -lemma trip_head {x : Grid M} {l : Path M} (h : Trip x s l) : - Fit s ∧ x∈s.here.block M := by - induction l generalizing s with - | nil => exact h - | cons b l ih => - obtain ⟨rfl,ha,hb⟩ := h - refine ⟨ha.fit,?_⟩ - have hi := (ih hb).2 - rw [← ha.lower.here] - exact block_of_below (child_below _ _) _ hi - -lemma stream_child {s q : Scene M} {x : Grid M} (h : Less s q) (hx : x∈s.here.block M) : - x ∈ (q.smaller ((step q).bit x)).here.block M := by - change x ∈ (child q.here _).block M - let b (x : Grid M) := decide (x∈q.here.R.block M) - change x ∈ (child q.here (b x)).block M - by_cases HH : x∈q.here.R.block M - · rw [show b x=true from decide_eq_true HH]; exact HH - · rw [show b x=false from decide_eq_false HH] - rw [← h.here,block_union] at hx - exact hx.resolve_right HH - -lemma trip_stream {x : Grid M} (t : Thread M s) (h : t.Following j J f) - (hf : Fit s) (hx : x∈s.here.block M) : Trip x s (stream t x) := by - induction t with - | blank => exact ⟨hf,hx⟩ - | ran s q k ih => - have H := jump_ideal h hf.lift - let b := (step q).bit x - exact ⟨rfl,H,ih b (h.2.2.2.2.2 b) (H.climb.child b) (stream_child H.lower hx)⟩ - -lemma trip_cut {x : Grid M} {l r : Path M} (h : Trip x s (l++r)) : - Trip x s l ∧ ∃ s, Trip x s r := by - induction l generalizing s with - | nil => exact ⟨trip_head h,⟨_,h⟩⟩ - | cons b l ih => - obtain ⟨rfl,hh,hk⟩ := h - obtain ⟨h₁,h₂⟩ := ih hk - exact ⟨⟨rfl,hh,h₁⟩,h₂⟩ - -lemma heav_bit (p : Node) (b : Bool) (x : Grid M) (h : x∈(p.child b).block M) : - p.Hi (pos x)=curl b := by cases b; exact p.Hi_L h; exact p.Hi_R h - -lemma Gi_half (p : Node) (b : Bool) (x : Grid M) (h : x∈(p.child b).block M) : - p.Gi (pos x)=curl b * (p.child b).Hi (pos x) := by - rw [Gi_eq] - have H := Set.disjoint_left.mp (p.block_separate M) - cases b - · rw [p.R.Hi_out (show x ∉ p.R.block M from H h)] - change _=1*p.L.Hi (pos x); ring - · rw [p.L.Hi_out (show x∉ p.L.block M from fun H' => H H' h)] - change _= -1*p.R.Hi (pos x); ring - -def blow (x : Grid M) (b : Beat M) := b.post.here.Gi (pos x) - -lemma trip_delay {x : Grid M} {c d : Beat M} {l : Path M} - (h : Trip x s (c::d::l)) : blow x c=curl c.bit*curl d.bit := by - have hx := (trip_head h.2.2).2 - obtain ⟨ha,hb,hH,hd,he⟩ := h - have hE : (c.post.here.child c.bit)=d.post.here := by - change (winds c).here=_ - rw [← hH,hd.lower.here] - unfold blow; rw [Gi_half _ _ _ hx,hE] - rw [heav_bit _ _ _ (trip_head he).2] - -lemma trip_final {x : Grid M} {b : Beat M} {l : Path M} - (h : Trip x s (b::l)) : |blow x b|=1 := by - have hx := (trip_head h.2.2).2 - unfold blow; rw [Gi_half _ _ _ hx,abs_mul,curl_unit,one_mul] - let p := (winds b).here - change x ∈ p.block M at hx - change |p.Hi (pos x)|=1 - rw [block_union] at hx - rcases hx with hx|hx - · rw [p.Hi_L hx]; norm_num - · rw [p.Hi_R hx]; norm_num - -def subtotal {E : Type*} [AddMonoid E] (f : Beat M→E) (l : Path M) := (l.map f).sum - -lemma subtotal_nil {E : Type*} [AddMonoid E] (f : Beat M→E) : subtotal f [] = 0 := rfl -lemma subtotal_cons {E : Type*} [AddMonoid E] (f : Beat M→E) (b : Beat M) (l : Path M) : - subtotal f (b::l)=f b+subtotal f l := rfl -lemma subtotal_app {E : Type*} [AddMonoid E] (f : Beat M→E) (l r : Path M) : - subtotal f (l++r)=subtotal f l+subtotal f r := by simp only [subtotal, List.map_append,List.sum_append] - -lemma trip_subtotal {x : Grid M} {f g : Beat M → ℝ} {l : Path M} - (h : Trip x s l) (hp : ∀ b, Ideal b.pre b.post → f b ≤ g b) : - subtotal f l ≤ subtotal g l := by - induction l generalizing s with - | nil => simp only [subtotal_nil,le_refl] - | cons b l ih => exact add_le_add (hp b h.2.1) (ih h.2.2) -lemma trip_nonneg {x : Grid M} {f : Beat M → ℝ} {l : Path M} - (h : Trip x s l) (hp : ∀ b, Ideal b.pre b.post → 0 ≤ f b) : - 0 ≤ subtotal f l := by - induction l generalizing s with - | nil => exact le_rfl - | cons b l ih => exact add_nonneg (hp _ h.2.1) (ih h.2.2) - -def fullKills (l : Path M) := subtotal killed l - -lemma trip_K {x : Grid M} {l : Path M} (h : Trip x s l) : 0 ≤ fullKills l := - trip_nonneg h (fun _ H => H.kill_nonneg) - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Activate +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +abbrev Path (M : ℕ) := List (Beat M) + +def winds (b : Beat M) := b.post.smaller b.bit +def curl (b : Bool) : ℝ := if b=true then -1 else 1 + +lemma curl_unit (b : Bool) : |curl b|=1 := by cases b <;> norm_num [curl] +lemma curl_sq (b : Bool) : (curl b)^2=1 := by cases b <;> norm_num [curl] +lemma fair_av (f : Bool→ℝ) : (uniform Bool).Ex f = (f false+f true)/2 := by + simp only [Ex,Fintype.sum_bool,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] + ring + +structure Fit (s : Scene M) : Prop where + ready : ReadyAt s + pos : 0 < s.norm + lift : share s ≤ 4 + +structure Climb (s : Scene M) : Prop where + fit : Fit s + deep : s.here.lev < M + tight : share s ≤ 2 + +structure Ideal (pre post : Scene M) : Prop where + fit : Fit pre + climb : Climb post + lower : Less pre post + +lemma jump_ideal {k} (h : Following j J f (.ran s q k)) + (hq : share s ≤ 4) : Ideal s q := by + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + obtain ⟨hz,hH⟩ := share_small hd + refine ⟨⟨⟨ha.within,ha.positive⟩,hb.1.flat ▸ hz,hq⟩,?_,hb.1⟩ + exact ⟨⟨⟨hc.within,hc.positive⟩,hz,hH.trans (by norm_num)⟩,(rooted _ (hk false)).within.2,hH⟩ + +lemma Climb.child {q : Scene M} (h : Climb q) (b) : Fit (q.smaller b) := + let hx := h.fit.ready + ⟨⟨q.here.child_ok hx.1.1 h.deep b,hx.2⟩,h.fit.pos, + share_child q hx.1.1 h.deep hx.2 h.fit.pos h.tight b⟩ + +lemma Ideal.kill_eq {B : Beat M} (h : Ideal B.pre B.post) : + killed B = share B.pre-share B.post := by + rw [killed,sub_div]; unfold share; rw [h.lower.flat] +lemma Ideal.kill_nonneg {B : Beat M} (h : Ideal B.pre B.post) : + 0 ≤ killed B := div_nonneg (sub_nonneg.mpr + (h.lower.mass_le h.fit.ready.2 h.fit.ready.1)) h.fit.pos.le + +/-- deterministic integrity along a plateau list. -/ +def Trip (x : Grid M) (s : Scene M) : Path M → Prop + | [] => Fit s ∧ x∈s.here.block M + | b::l => + b.pre=s ∧ Ideal b.pre b.post ∧ Trip x (winds b) l + +lemma trip_head {x : Grid M} {l : Path M} (h : Trip x s l) : + Fit s ∧ x∈s.here.block M := by + induction l generalizing s with + | nil => exact h + | cons b l ih => + obtain ⟨rfl,ha,hb⟩ := h + refine ⟨ha.fit,?_⟩ + have hi := (ih hb).2 + rw [← ha.lower.here] + exact block_of_below (child_below _ _) _ hi + +lemma stream_child {s q : Scene M} {x : Grid M} (h : Less s q) (hx : x∈s.here.block M) : + x ∈ (q.smaller ((step q).bit x)).here.block M := by + change x ∈ (child q.here _).block M + let b (x : Grid M) := decide (x∈q.here.R.block M) + change x ∈ (child q.here (b x)).block M + by_cases HH : x∈q.here.R.block M + · rw [show b x=true from decide_eq_true HH]; exact HH + · rw [show b x=false from decide_eq_false HH] + rw [← h.here,block_union] at hx + exact hx.resolve_right HH + +lemma trip_stream {x : Grid M} (t : Thread M s) (h : t.Following j J f) + (hf : Fit s) (hx : x∈s.here.block M) : Trip x s (stream t x) := by + induction t with + | blank => exact ⟨hf,hx⟩ + | ran s q k ih => + have H := jump_ideal h hf.lift + let b := (step q).bit x + exact ⟨rfl,H,ih b (h.2.2.2.2.2 b) (H.climb.child b) (stream_child H.lower hx)⟩ + +lemma trip_cut {x : Grid M} {l r : Path M} (h : Trip x s (l++r)) : + Trip x s l ∧ ∃ s, Trip x s r := by + induction l generalizing s with + | nil => exact ⟨trip_head h,⟨_,h⟩⟩ + | cons b l ih => + obtain ⟨rfl,hh,hk⟩ := h + obtain ⟨h₁,h₂⟩ := ih hk + exact ⟨⟨rfl,hh,h₁⟩,h₂⟩ + +lemma heav_bit (p : Node) (b : Bool) (x : Grid M) (h : x∈(p.child b).block M) : + p.Hi (pos x)=curl b := by cases b; exact p.Hi_L h; exact p.Hi_R h + +lemma Gi_half (p : Node) (b : Bool) (x : Grid M) (h : x∈(p.child b).block M) : + p.Gi (pos x)=curl b * (p.child b).Hi (pos x) := by + rw [Gi_eq] + have H := Set.disjoint_left.mp (p.block_separate M) + cases b + · rw [p.R.Hi_out (show x ∉ p.R.block M from H h)] + change _=1*p.L.Hi (pos x); ring + · rw [p.L.Hi_out (show x∉ p.L.block M from fun H' => H H' h)] + change _= -1*p.R.Hi (pos x); ring + +def blow (x : Grid M) (b : Beat M) := b.post.here.Gi (pos x) + +lemma trip_delay {x : Grid M} {c d : Beat M} {l : Path M} + (h : Trip x s (c::d::l)) : blow x c=curl c.bit*curl d.bit := by + have hx := (trip_head h.2.2).2 + obtain ⟨ha,hb,hH,hd,he⟩ := h + have hE : (c.post.here.child c.bit)=d.post.here := by + change (winds c).here=_ + rw [← hH,hd.lower.here] + unfold blow; rw [Gi_half _ _ _ hx,hE] + rw [heav_bit _ _ _ (trip_head he).2] + +lemma trip_final {x : Grid M} {b : Beat M} {l : Path M} + (h : Trip x s (b::l)) : |blow x b|=1 := by + have hx := (trip_head h.2.2).2 + unfold blow; rw [Gi_half _ _ _ hx,abs_mul,curl_unit,one_mul] + let p := (winds b).here + change x ∈ p.block M at hx + change |p.Hi (pos x)|=1 + rw [block_union] at hx + rcases hx with hx|hx + · rw [p.Hi_L hx]; norm_num + · rw [p.Hi_R hx]; norm_num + +def subtotal {E : Type*} [AddMonoid E] (f : Beat M→E) (l : Path M) := (l.map f).sum + +lemma subtotal_nil {E : Type*} [AddMonoid E] (f : Beat M→E) : subtotal f [] = 0 := rfl +lemma subtotal_cons {E : Type*} [AddMonoid E] (f : Beat M→E) (b : Beat M) (l : Path M) : + subtotal f (b::l)=f b+subtotal f l := rfl +lemma subtotal_app {E : Type*} [AddMonoid E] (f : Beat M→E) (l r : Path M) : + subtotal f (l++r)=subtotal f l+subtotal f r := by simp only [subtotal, List.map_append,List.sum_append] + +lemma trip_subtotal {x : Grid M} {f g : Beat M → ℝ} {l : Path M} + (h : Trip x s l) (hp : ∀ b, Ideal b.pre b.post → f b ≤ g b) : + subtotal f l ≤ subtotal g l := by + induction l generalizing s with + | nil => simp only [subtotal_nil,le_refl] + | cons b l ih => exact add_le_add (hp b h.2.1) (ih h.2.2) +lemma trip_nonneg {x : Grid M} {f : Beat M → ℝ} {l : Path M} + (h : Trip x s l) (hp : ∀ b, Ideal b.pre b.post → 0 ≤ f b) : + 0 ≤ subtotal f l := by + induction l generalizing s with + | nil => exact le_rfl + | cons b l ih => exact add_nonneg (hp _ h.2.1) (ih h.2.2) + +def fullKills (l : Path M) := subtotal killed l + +lemma trip_K {x : Grid M} {l : Path M} (h : Trip x s l) : 0 ≤ fullKills l := + trip_nonneg h (fun _ H => H.kill_nonneg) + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Troupe.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Troupe.lean index 93016f88b669b0070ab1c5e4445d70f91a232b25..97dd657eaa5a00d032ba3cc154abde116a6cadcd 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Troupe.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Troupe.lean @@ -1,160 +1,160 @@ -import OAI.Analysis.FourierLLogL.Paths.Checkpoint -import OAI.Analysis.FourierLLogL.Paths.Frost -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -namespace Troupe - -structure Grade where - main : Bool - i : ℕ -def stage (g : Grade) (m : ℕ) : Plot := ⟨g.main,g.i,m⟩ -def grade (p : Plot) : Grade := ⟨p.main,p.ix⟩ - -/-- height parameter allowing very small mass static entries. -/ -def amount (s : Scene M) (j : ℕ) := height j+max (-log s.norm) 0+2 -def rose (g : Grade) (u : Grid M) : Run.Streak M := ⟨g.i,u⟩ -def recital (g : Grade) (u : Grid M) (l : Path M) := - if g.main then (rose g u).sumbling none l else coldScore u (side g.i) l -lemma rec_one (g : Grade) (u : Grid M) (l : Path M) : 1 ≤ recital g u l := by - unfold recital; split; exact Run.Streak.sumbling_one _ _ _; exact cold_one _ _ _ -lemma amount_limits (s : Scene M) (j : ℕ) : 1 ≤ amount s j ∧ height j ≤ amount s j := - let H := d_pos j - have hh : 0 ≤ max (-log s.norm) 0 := le_max_right _ _ - by unfold amount; constructor <;> linarith - -def entitled (g : Grade) (s : Scene M) (j : ℕ) := - g.main=true → g.i ≤ mainIx j ∧ s.live=true -def cutBook (g : Grade) (s : Scene M) (j : ℕ) := - if g.main then kappa j (codeWeight capA) (g.i+1) else scLimit s g.i j -lemma cut_nonneg (g : Grade) (s : Scene M) (j : ℕ) : 0 ≤ cutBook g s j := by - unfold cutBook scLimit; split - · exact (kappa_pos _ (by unfold codeWeight; - unfold capA reserve; norm_num) j _).le - · have he := d_pos j - have hh := le_max_right (-log s.norm) 0 - positivity - -lemma cover_limit (s : Scene M) (g : Grade) (j J : ℕ) (hs : s.Safe j J f) - (hp : Climb s) (h : s.settled j) (hg : entitled g s j) - (hF : ∀ i,f i ≤ exp (height j)): - ((net s j g.i g.main).card:ℝ) ≤ exp (cutBook g s j) := by - by_cases hx : g.main=true - · rw [cutBook,ite_eq_left hx,hx] - apply net_len_main _ _ _ _ h (hg hx).2 - have HH := (hg hx).1 - unfold testIx; omega - unfold cutBook; rw [ite_eq_right hx] - rw [show g.main=false from Bool.eq_false_iff.mpr hx] - exact net_len_scalar _ _ hp.fit.ready hp.deep hp.fit.pos hp.tight (fun i => (hs.sub i).trans (hF i)) - -open Plot - -def boom (p : Plot) (c : Board M) (s : Scene M) := cardStep p (p.turn c s) -def burst (p : Plot) (s : Scene M) (j : ℕ) (l : Path M) := - ∑ x∈ net s j p.ix p.main, recital (grade p) x l -def showScore (p : Plot) (c : Board M) (j : ℕ) : Path M → ℝ -| [] => 1 -| b::l => if boom p c b.post then - (if freshAt p c b.post then burst p b.post j (b::l) else 0)+showScore p (p.turn c b.post) j l else 1 - -lemma burst_pos (p : Plot) (s : Scene M) (j : ℕ) (l : Path M) : - 0 ≤ burst p s j l := - Finset.sum_nonneg fun i _ => le_trans (by norm_num) (rec_one (grade p) i l) -lemma show_one (p : Plot) (c : Board M) (j : ℕ) (l : Path M) : - 1 ≤ showScore p c j l := by - induction l generalizing c with - | nil => exact le_rfl - | cons b l ih => - rw [showScore] - split; exact le_trans (ih _) (le_add_of_nonneg_left (by split; apply burst_pos; norm_num)) - exact le_rfl - -def sticker (s : Scene M) (g : Grade) (j : ℕ) := - 10^30*(amount s j)^3 * exp (cutBook g s j) - -/-- summing conditional estimate-envelope costs over births. -/ -lemma paint_birth : - ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ) (s : Scene M) - (t : Thread M s) (f : Grid M → ℝ) (p : Plot), - Following j J f t → Fit s → entitled (grade p) s j → - (∀ x, f x ≤ exp (height j)) → ∀ c : Board M, - stats t (showScore p c j) ≤ 1+sticker s (grade p) j*stats t (minted p c) := by - filter_upwards [Run.Streak.expected_bling] with j hB M J s t f p h hh hG hF c - let g := grade p - induction t generalizing c with - | blank s => - rw [stats_nil,stats_nil] - change (1:ℝ) ≤ 1+_*0; ring_nf; exact le_rfl - | ran s q k ih => - let t := ran s q k - let v := turn p c q - let R := sticker s g j - let h₀ := h - have hK := jump_ideal h hh.lift - have hC : cutBook g q j=cutBook g s j := by unfold cutBook scLimit; rw [hK.lower.flat] - have HE : amount q j=amount s j := by unfold amount; rw [hK.lower.flat] - have hR (b : Bool) : sticker (q.smaller b) g j=R := by change sticker q g j=R; unfold R sticker; rw [hC, HE] - have hz : entitled g q j := fun hi => ⟨(hG hi).1,hK.lower.mode.trans (hG hi).2⟩ - obtain ⟨ha,hb,hc,hd,ht,hk⟩ := h - let x := amount s j; let C (l : Path M) := burst p q j l - have Hc : stats t C ≤ R := by - let e := net q j p.ix p.main - have HX : (e.card:ℝ) ≤ exp (cutBook g s j) := hC ▸ cover_limit q g j J hc hK.climb ht hz hF - have Hi (u) : stats t (recital g u) ≤ 10^30*x^3 := by - have h1 : 1 ≤ x ∧ height j ≤ x := amount_limits s j - have h3 : 1 ≤ x^3 := one_le_pow₀ h1.1 - unfold recital - by_cases H : g.main=true - · simp_rw [ite_eq_left H] - apply le_trans (hB M J s q f k h₀ hh (hz H).2 hF (rose g u) (hG H).1) - exact mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (d_pos j).le h1.2 _) (by norm_num) - simp_rw [ite_eq_right H] - exact le_trans (cold_mean _ _ t h₀) (by linarith) - calc - _ = ∑ y∈e, stats t (recital g y) := Ex_finset_sum _ _ _ - _ ≤ ∑ _y∈e, (10:ℝ)^30*x^3 := Finset.sum_le_sum fun y _ => Hi y - _ ≤ _ := by - simp only [Finset.sum_const,nsmul_eq_mul] - unfold R sticker; change (e.card:ℝ)*_ ≤ 10^30*x^3*_ - rw [mul_comm (10^30*x^3)] - apply mul_le_mul_of_nonneg_right HX - have h1 : 1 ≤ x := (amount_limits s j).1; positivity - have HV := stats_cons h₀ - rw [stats_cons h₀,stats_cons h₀] - have HI (b : Bool) : stats (k b) (showScore p v j) ≤ 1+R*stats (k b) (minted p v) := - let H := ih b (hk b) (hK.climb.child b) (show entitled g _ j from hz) v - hR b ▸ H - let U := uniform Bool - have H := U.Ex_mono HI - rw [Ex_add (f:=fun _ => _),Ex_const,Ex_lmul] at H - change stats (ran s q k) C ≤ R at Hc - rw [stats_cons h₀] at Hc - by_cases HA : cardStep p (turn p c q) <;> by_cases HP : freshAt p c q - all_goals - simp_rw [showScore, boom, minted] - simp only [HA,HP,birth,ite_false,ite_true] - unfold t v R C U at * - unfold stats at * - try simp_rw [Ex_add] - simp_rw [Ex_const] - unfold g grade at * - linarith - -end Troupe - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Checkpoint +import OAI.Analysis.FourierLLogL.Paths.Frost +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +namespace Troupe + +structure Grade where + main : Bool + i : ℕ +def stage (g : Grade) (m : ℕ) : Plot := ⟨g.main,g.i,m⟩ +def grade (p : Plot) : Grade := ⟨p.main,p.ix⟩ + +/-- height parameter allowing very small mass static entries. -/ +def amount (s : Scene M) (j : ℕ) := height j+max (-log s.norm) 0+2 +def rose (g : Grade) (u : Grid M) : Run.Streak M := ⟨g.i,u⟩ +def recital (g : Grade) (u : Grid M) (l : Path M) := + if g.main then (rose g u).sumbling none l else coldScore u (side g.i) l +lemma rec_one (g : Grade) (u : Grid M) (l : Path M) : 1 ≤ recital g u l := by + unfold recital; split; exact Run.Streak.sumbling_one _ _ _; exact cold_one _ _ _ +lemma amount_limits (s : Scene M) (j : ℕ) : 1 ≤ amount s j ∧ height j ≤ amount s j := + let H := d_pos j + have hh : 0 ≤ max (-log s.norm) 0 := le_max_right _ _ + by unfold amount; constructor <;> linarith + +def entitled (g : Grade) (s : Scene M) (j : ℕ) := + g.main=true → g.i ≤ mainIx j ∧ s.live=true +def cutBook (g : Grade) (s : Scene M) (j : ℕ) := + if g.main then kappa j (codeWeight capA) (g.i+1) else scLimit s g.i j +lemma cut_nonneg (g : Grade) (s : Scene M) (j : ℕ) : 0 ≤ cutBook g s j := by + unfold cutBook scLimit; split + · exact (kappa_pos _ (by unfold codeWeight; + unfold capA reserve; norm_num) j _).le + · have he := d_pos j + have hh := le_max_right (-log s.norm) 0 + positivity + +lemma cover_limit (s : Scene M) (g : Grade) (j J : ℕ) (hs : s.Safe j J f) + (hp : Climb s) (h : s.settled j) (hg : entitled g s j) + (hF : ∀ i,f i ≤ exp (height j)): + ((net s j g.i g.main).card:ℝ) ≤ exp (cutBook g s j) := by + by_cases hx : g.main=true + · rw [cutBook,ite_eq_left hx,hx] + apply net_len_main _ _ _ _ h (hg hx).2 + have HH := (hg hx).1 + unfold testIx; omega + unfold cutBook; rw [ite_eq_right hx] + rw [show g.main=false from Bool.eq_false_iff.mpr hx] + exact net_len_scalar _ _ hp.fit.ready hp.deep hp.fit.pos hp.tight (fun i => (hs.sub i).trans (hF i)) + +open Plot + +def boom (p : Plot) (c : Board M) (s : Scene M) := cardStep p (p.turn c s) +def burst (p : Plot) (s : Scene M) (j : ℕ) (l : Path M) := + ∑ x∈ net s j p.ix p.main, recital (grade p) x l +def showScore (p : Plot) (c : Board M) (j : ℕ) : Path M → ℝ +| [] => 1 +| b::l => if boom p c b.post then + (if freshAt p c b.post then burst p b.post j (b::l) else 0)+showScore p (p.turn c b.post) j l else 1 + +lemma burst_pos (p : Plot) (s : Scene M) (j : ℕ) (l : Path M) : + 0 ≤ burst p s j l := + Finset.sum_nonneg fun i _ => le_trans (by norm_num) (rec_one (grade p) i l) +lemma show_one (p : Plot) (c : Board M) (j : ℕ) (l : Path M) : + 1 ≤ showScore p c j l := by + induction l generalizing c with + | nil => exact le_rfl + | cons b l ih => + rw [showScore] + split; exact le_trans (ih _) (le_add_of_nonneg_left (by split; apply burst_pos; norm_num)) + exact le_rfl + +def sticker (s : Scene M) (g : Grade) (j : ℕ) := + 10^30*(amount s j)^3 * exp (cutBook g s j) + +/-- summing conditional estimate-envelope costs over births. -/ +lemma paint_birth : + ∀ᶠ j : ℕ in atTop, ∀ (M J : ℕ) (s : Scene M) + (t : Thread M s) (f : Grid M → ℝ) (p : Plot), + Following j J f t → Fit s → entitled (grade p) s j → + (∀ x, f x ≤ exp (height j)) → ∀ c : Board M, + stats t (showScore p c j) ≤ 1+sticker s (grade p) j*stats t (minted p c) := by + filter_upwards [Run.Streak.expected_bling] with j hB M J s t f p h hh hG hF c + let g := grade p + induction t generalizing c with + | blank s => + rw [stats_nil,stats_nil] + change (1:ℝ) ≤ 1+_*0; ring_nf; exact le_rfl + | ran s q k ih => + let t := ran s q k + let v := turn p c q + let R := sticker s g j + let h₀ := h + have hK := jump_ideal h hh.lift + have hC : cutBook g q j=cutBook g s j := by unfold cutBook scLimit; rw [hK.lower.flat] + have HE : amount q j=amount s j := by unfold amount; rw [hK.lower.flat] + have hR (b : Bool) : sticker (q.smaller b) g j=R := by change sticker q g j=R; unfold R sticker; rw [hC, HE] + have hz : entitled g q j := fun hi => ⟨(hG hi).1,hK.lower.mode.trans (hG hi).2⟩ + obtain ⟨ha,hb,hc,hd,ht,hk⟩ := h + let x := amount s j; let C (l : Path M) := burst p q j l + have Hc : stats t C ≤ R := by + let e := net q j p.ix p.main + have HX : (e.card:ℝ) ≤ exp (cutBook g s j) := hC ▸ cover_limit q g j J hc hK.climb ht hz hF + have Hi (u) : stats t (recital g u) ≤ 10^30*x^3 := by + have h1 : 1 ≤ x ∧ height j ≤ x := amount_limits s j + have h3 : 1 ≤ x^3 := one_le_pow₀ h1.1 + unfold recital + by_cases H : g.main=true + · simp_rw [ite_eq_left H] + apply le_trans (hB M J s q f k h₀ hh (hz H).2 hF (rose g u) (hG H).1) + exact mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (d_pos j).le h1.2 _) (by norm_num) + simp_rw [ite_eq_right H] + exact le_trans (cold_mean _ _ t h₀) (by linarith) + calc + _ = ∑ y∈e, stats t (recital g y) := Ex_finset_sum _ _ _ + _ ≤ ∑ _y∈e, (10:ℝ)^30*x^3 := Finset.sum_le_sum fun y _ => Hi y + _ ≤ _ := by + simp only [Finset.sum_const,nsmul_eq_mul] + unfold R sticker; change (e.card:ℝ)*_ ≤ 10^30*x^3*_ + rw [mul_comm (10^30*x^3)] + apply mul_le_mul_of_nonneg_right HX + have h1 : 1 ≤ x := (amount_limits s j).1; positivity + have HV := stats_cons h₀ + rw [stats_cons h₀,stats_cons h₀] + have HI (b : Bool) : stats (k b) (showScore p v j) ≤ 1+R*stats (k b) (minted p v) := + let H := ih b (hk b) (hK.climb.child b) (show entitled g _ j from hz) v + hR b ▸ H + let U := uniform Bool + have H := U.Ex_mono HI + rw [Ex_add (f:=fun _ => _),Ex_const,Ex_lmul] at H + change stats (ran s q k) C ≤ R at Hc + rw [stats_cons h₀] at Hc + by_cases HA : cardStep p (turn p c q) <;> by_cases HP : freshAt p c q + all_goals + simp_rw [showScore, boom, minted] + simp only [HA,HP,birth,ite_false,ite_true] + unfold t v R C U at * + unfold stats at * + try simp_rw [Ex_add] + simp_rw [Ex_const] + unfold g grade at * + linarith + +end Troupe + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Upkeep.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Upkeep.lean index 03e4fd9e686fbf4a970026093220b9d03cce5f05..eada9060b328282bd58f2051270c294357659046 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Upkeep.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Upkeep.lean @@ -1,234 +1,234 @@ -import OAI.Analysis.FourierLLogL.Paths.Ribbon -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -/-! # Expected forward sums of marked jump quantities -/ -open Beam Run Run.Streak - -/-- A local bit-Fisher budget -/ -def caveat (s : Scene M) (j : ℕ) := - share s*(deficit s (channel (dye s))+(minimumT j)⁻¹) - -lemma caveat_pos (s : Scene M) (j : ℕ) (h : Fit s) : 0 ≤ caveat s j := by - have he : 0 ≤ s.mass := avg_nonneg _ fun x _ => h.ready.2 _ - unfold caveat - rcases eq_or_lt_of_le he with he|he - · unfold share; rw [← he,zero_div,zero_mul] - exact mul_nonneg (div_nonneg he.le h.pos.le) - (add_nonneg (deficit_nonneg s h.ready.1 h.ready.2 he _) - (inv_nonneg.mpr (minimum_pos j).le)) - -lemma caveat_step {s q : Scene M} {l} (h : Following j J f (ran s q l)) (hh : Ideal s q) : - (uniform Bool).Ex (fun b => caveat (q.smaller b) j) + (1/4:ℝ)*fax q ≤ caveat s j := by - let U := (step q).probs; let v := (keep s q).snip; let Q := v.probs - let Z := channel (dye q); let H (x) := deficit x Z; let a := (minimumT j)⁻¹ - have hm := pre_pos h - have HE : Q.entropy ≤ a*Q false := debt_at h - obtain ⟨hh1,hh2,h3⟩ := hh - obtain ⟨ha,hb,hv,hp',ht,hs⟩ := h - have hR : q.here.lev < M := (rooted _ (hs false)).within.2 - have hS : ReadyAt s := ⟨ha.within,ha.positive⟩; have hS' : ReadyAt q := ⟨hv.within,hv.positive⟩ - have hd := hh1.pos - have hQ := hp'.1.2.2; have hZ := hh2.fit.pos - have hM := keep_true hb.1 hS hS' hm - have h₂ : Q true*(H q+a) ≤ H s+a := by - have hi (b : Bool) : levels (v.dst b)=levels s := by - unfold levels; rw [show (v.dst b).here=s.here from keep_here hb.1 _] - have hz : Q.Ex (fun b => H (v.dst b)) ≤ H s+Q.entropy := by - have hh := old_entropy_cost v Z - have hh' := conditioned_balance Z v hM - unfold H deficit; simp_rw [hi,Ex_sub (f:=fun _ => levels s),Ex_const] - change v.probs.Ex _+ _ = s.actor.privateH Z at hh' - unfold Q; linarith - have hZ : 0 ≤ Q false * H (v.dst false) := by - rcases eq_or_lt_of_le (Q.nonneg false) with h|h - · rw [← h,zero_mul] - have HH := other_ok hb.1 hS - exact mul_nonneg h.le (deficit_nonneg (v.dst false) HH.1 HH.2 ((true_mass hM).mp h) Z) - have ht₁ : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total - rw [Ex,Fintype.sum_bool] at hz - rw [show v.dst true=q from rfl] at hz - nlinarith [show a*(Q true+Q false)=a by rw [ht₁]; ring] - have ht₁ := space_deficit q Z hv.within.1 hR hv.positive hQ hZ - let D (b : Bool) := H (q.smaller b)+a - have h₁ : - (uniform Bool).Ex (fun b => caveat (q.smaller b) j) = - share q * U.Ex D := by - have HI (b : Bool) : caveat (q.smaller b) j = - (q.smaller b).mass * (q.norm⁻¹ * D b) := by unfold caveat D H; simp only [smaller]; unfold share Z dye a; dsimp; ring - simp_rw [HI] - rw [← output_split _ hv.within.1 hR hv.positive hQ (fun b => q.norm⁻¹*D b), - Ex_lmul] - unfold share U; ring - have HZ : share q * ((1/4:ℝ)*(q.norm/q.mass)*fax q)=1/4*fax q := by - unfold share; field_simp - have hEq : share q = share s * Q true := by - have H := kept_mass hb.1 hS hS' hm - unfold share - nth_rw 1 [H,hb.1.flat] - unfold Q v; ring - rw [h₁,← HZ,← mul_add] - unfold D; rw [Ex_add (g:=fun b => a),Ex_const] - have hDC : caveat s j=share s*(H s+a) := by unfold caveat H Z dye a; rw [hb.1.fund] - have hsq : 0 ≤ share q := div_nonneg hQ.le hZ.le - calc - _ ≤ share q*(H q+a) := by - apply mul_le_mul_of_nonneg_left _ hsq - change U.Ex (fun i => H (q.smaller i))+(1/4:ℝ)*(q.norm/q.mass)*fax q ≤ H q at ht₁ - linarith - _ ≤ _ := by - rw [hEq,mul_assoc,hDC] - exact mul_le_mul_of_nonneg_left h₂ (div_nonneg hm.le hd.le) - -/-- average of forward statistics under uniform output path -/ -def stats {s : Scene M} (t : Thread M s) (L : Path M → ℝ) := - (s.here.dice M).Ex (fun x => L (t.stream x)) - -lemma stats_cons {s q : Scene M} {k} (h : Following j J f (ran s q k)) (F : Path M → ℝ) : - stats (ran s q k) F = - (uniform Bool).Ex (fun b => stats (k b) (fun l => F (⟨s,q,b⟩::l))) := - stream_branch k h (fun l _ => F l) - -lemma stat_pay (s : Scene M) (t : Thread M s) (g : Beat M → ℝ) : - stats t (subtotal g)=Thread.fee t g := rfl -lemma stats_nil (s : Scene M) (F : Path M → ℝ) : stats (blank s) F=F [] := by simp [stats,stream,Ex_const] -lemma stats_plus {s : Scene M} (t : Thread M s) (f g : Path M → ℝ) : - stats t (fun l => f l+g l)=stats t f+stats t g := - Ex_add _ (f:=fun _ => f _) (g:=fun _ => g _) -lemma stats_fix {s : Scene M} (t : Thread M s) (r : ℝ) : stats t (fun _ => r)=r := - Ex_const _ _ - -lemma caveat_total {s : Scene M} (t : Thread M s) (h : Following j J f t) (hp : Fit s) : - stats t fullFax ≤ 4*caveat s j := by - change t.fee billFax ≤ _ - induction t with - | blank s => - change stats (blank s) fullFax ≤ _ - rw [stats_nil, show fullFax (M:=M) [] = 0 from rfl] - linarith [caveat_pos s j hp] - | ran s q k ih => - have hx := jump_ideal h hp.lift - have HH := caveat_step h hx - rw [fee_ran _ h,Ex_add,show (uniform Bool).Ex (fun b : Bool => billFax ⟨s,q,b⟩) = fax q from by unfold billFax; exact Ex_const (uniform Bool) (fax q)] - apply le_trans (b:=fax q+(uniform Bool).Ex fun b => 4*caveat (q.smaller b) j) - · gcongr - apply Ex_mono - intro b - exact ih b (h.2.2.2.2.2 b) (hx.climb.child b) - rw [Ex_lmul]; linarith - -lemma showers_total (e : Streak M) - {s : Scene M} (t : Thread M s) (h : Following j J f t) (hp : Fit s) : - stats t e.showers ≤ 4-energy s e.u+8*stats t fullKills := by - let g (b : Beat M) := shower e.u b.post - change t.fee g ≤ 4-energy s e.u+8*t.fee killed - induction t with - | blank s => - have Hh := (energy_small hp e.u).2.trans hp.lift - simp [Thread.fee,stream,Ex_const]; linarith - | ran s q k ih => - let U := uniform Bool - have H := jump_ideal h hp.lift - have HE (b : Bool) := ih b (h.2.2.2.2.2 b) (H.climb.child _) - rw [fee_ran _ h,fee_ran _ h] - simp_rw [Ex_add] - rw [Ex_const U (shower e.u q)] - have HH : U.Ex (fun b => energy (q.smaller b) e.u) = energy q e.u+shower e.u q := by - rw [show U=uniform Bool from rfl,fair_av]; exact wet_average q H.climb e.u - have HF := (energy_drain (b:=⟨s,q,false⟩) H e.u) - let K := killed (Thread.Beat.mk s q false) - have HR : (uniform Bool).Ex (fun b => killed ⟨s,q,b⟩) = K := Ex_const U K - have HT := U.Ex_mono HE - rw [Ex_add,Ex_lmul,Ex_sub (f:=fun _=> 4),Ex_const,HH] at HT - rw [HR] - change |energy s _-energy q _| ≤ 8*K at HF - change shower e.u q + U.Ex _ ≤ _ +8*(K+U.Ex _) - linarith [(abs_le.mp HF).2] - -lemma flag_point (s : Scene M) (e : Streak M) : - e.b^2* e.flag s ≤ 128*surcharge s true (e.i-1) := by - unfold Streak.flag - split_ifs with h - · rw [surcharge,ite_eq_left h.2] - apply le_trans (b:=128*gateWidth true (e.i-1)) - · change side e.i^2*1 ≤ 128*(side (e.i-1)^2 / 128) - have hh : side e.i ≤ side (e.i-1) := - pow_le_pow_of_le_one (by norm_num) (by norm_num) (by omega) - nlinarith [side_pos e.i] - · exact mul_le_mul_of_nonneg_left h.2 (by norm_num) - rw [mul_zero] - exact mul_nonneg (by norm_num) (surcharge_nonneg s true _) - -lemma flag_total (e : Streak M) {s : Scene M} (t : Thread M s) : - e.b^2 * stats t e.flagged ≤ - 128 * Thread.fee t (fun b => surcharge b.post true (e.i-1)) := by - change e.b^2 * t.fee _ ≤ _ - - unfold Thread.fee - rw [← Ex_lmul,← Ex_lmul] - apply Ex_mono; intro x - generalize t.stream x = l - induction l with - | nil => change _*0 ≤ _*0; exact le_of_eq (by ring) - | cons b l ih => - rw [List.map_cons,List.sum_cons,List.map_cons,List.sum_cons] - linarith [flag_point b.post e] - -lemma sum_budget_run : - ∀ᶠ j : ℕ in atTop, - ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s q : Scene M, ∀ k, - Following j J f (.ran s q k) → Fit s → q.live=true → - (∀ x, f x ≤ exp (height j)) → - ∀ e : Streak M, - let v := ran s q k - stats v fullKills ≤ 4 ∧ stats v e.showers ≤ 36 ∧ - stats v fullFax ≤ 10^26*height j ∧ e.b^2 * stats v e.flagged ≤ 10^6*height j := by - filter_upwards [segment_cost,side_deficit, hpoly 12800 32] with j h1 h2 ht - M J f s q k h hh hx hF e v - have hk := jump_ideal h hh.lift - have hq := pre_pos h - have hR : q.mass ≤ s.mass := hk.lower.mass_le hh.ready.2 hh.ready.1 - have hT := h.2.2.2.1.2.1 - let d := height j - have hM : exp (-d) ≤ q.mass := le_of_not_gt (fun H => hT ⟨hx,Or.inr H⟩) - have hE : -d ≤ log s.mass := by - rw [←log_exp (-d)] - exact log_le_log (exp_pos _) (hM.trans hR) - have he : (minimumT j)⁻¹ ≤ height j := by unfold minimumT; rw [one_div,inv_inv]; exact ht - have HP : stats v fullKills ≤ 4 := - le_trans (deaths v h hh.pos) hh.lift - refine ⟨HP, le_trans (showers_total e v h hh) - (by linarith [(energy_small hh e.u).1]),le_trans (caveat_total v h hh) ?_, - le_trans (flag_total e v) ?_⟩ - · have HL := h2 M J s f (rooted _ h) hF hq - have HJ : 0 ≤ share s ∧ share s ≤ 4 := ⟨share_pos' _ hh.ready hh.pos.le,hh.lift⟩ - have HY : 0 ≤ deficit s (channel (dye s)) := deficit_nonneg s hh.ready.1 hh.ready.2 hq _ - unfold caveat - have h₃ : deficit s (channel (dye s)) + (minimumT j)⁻¹ ≤ 10^23*d+d+d := by - change deficit s _ ≤ _ at HL; unfold dye d at *; linarith - have h₄ : 0 ≤ d := (d_pos j).le - change _ ≤ 10^26*d - nlinarith - · have H₁ := h1 M J s f v h hF hq hh.pos (fun _ => e.i-1) true - have H₂ : 0 ≤ s.mass/s.norm := div_nonneg hq.le hh.pos.le - have H₃ : s.mass/s.norm ≤ 4 := hh.lift - have H₄ : 128*(2*height j-log s.mass) ≤ 384*d := by unfold d at *; linarith - have h₄ : 0 ≤ d := (d_pos j).le - nlinarith - - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Ribbon +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +/-! # Expected forward sums of marked jump quantities -/ +open Beam Run Run.Streak + +/-- A local bit-Fisher budget -/ +def caveat (s : Scene M) (j : ℕ) := + share s*(deficit s (channel (dye s))+(minimumT j)⁻¹) + +lemma caveat_pos (s : Scene M) (j : ℕ) (h : Fit s) : 0 ≤ caveat s j := by + have he : 0 ≤ s.mass := avg_nonneg _ fun x _ => h.ready.2 _ + unfold caveat + rcases eq_or_lt_of_le he with he|he + · unfold share; rw [← he,zero_div,zero_mul] + exact mul_nonneg (div_nonneg he.le h.pos.le) + (add_nonneg (deficit_nonneg s h.ready.1 h.ready.2 he _) + (inv_nonneg.mpr (minimum_pos j).le)) + +lemma caveat_step {s q : Scene M} {l} (h : Following j J f (ran s q l)) (hh : Ideal s q) : + (uniform Bool).Ex (fun b => caveat (q.smaller b) j) + (1/4:ℝ)*fax q ≤ caveat s j := by + let U := (step q).probs; let v := (keep s q).snip; let Q := v.probs + let Z := channel (dye q); let H (x) := deficit x Z; let a := (minimumT j)⁻¹ + have hm := pre_pos h + have HE : Q.entropy ≤ a*Q false := debt_at h + obtain ⟨hh1,hh2,h3⟩ := hh + obtain ⟨ha,hb,hv,hp',ht,hs⟩ := h + have hR : q.here.lev < M := (rooted _ (hs false)).within.2 + have hS : ReadyAt s := ⟨ha.within,ha.positive⟩; have hS' : ReadyAt q := ⟨hv.within,hv.positive⟩ + have hd := hh1.pos + have hQ := hp'.1.2.2; have hZ := hh2.fit.pos + have hM := keep_true hb.1 hS hS' hm + have h₂ : Q true*(H q+a) ≤ H s+a := by + have hi (b : Bool) : levels (v.dst b)=levels s := by + unfold levels; rw [show (v.dst b).here=s.here from keep_here hb.1 _] + have hz : Q.Ex (fun b => H (v.dst b)) ≤ H s+Q.entropy := by + have hh := old_entropy_cost v Z + have hh' := conditioned_balance Z v hM + unfold H deficit; simp_rw [hi,Ex_sub (f:=fun _ => levels s),Ex_const] + change v.probs.Ex _+ _ = s.actor.privateH Z at hh' + unfold Q; linarith + have hZ : 0 ≤ Q false * H (v.dst false) := by + rcases eq_or_lt_of_le (Q.nonneg false) with h|h + · rw [← h,zero_mul] + have HH := other_ok hb.1 hS + exact mul_nonneg h.le (deficit_nonneg (v.dst false) HH.1 HH.2 ((true_mass hM).mp h) Z) + have ht₁ : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total + rw [Ex,Fintype.sum_bool] at hz + rw [show v.dst true=q from rfl] at hz + nlinarith [show a*(Q true+Q false)=a by rw [ht₁]; ring] + have ht₁ := space_deficit q Z hv.within.1 hR hv.positive hQ hZ + let D (b : Bool) := H (q.smaller b)+a + have h₁ : + (uniform Bool).Ex (fun b => caveat (q.smaller b) j) = + share q * U.Ex D := by + have HI (b : Bool) : caveat (q.smaller b) j = + (q.smaller b).mass * (q.norm⁻¹ * D b) := by unfold caveat D H; simp only [smaller]; unfold share Z dye a; dsimp; ring + simp_rw [HI] + rw [← output_split _ hv.within.1 hR hv.positive hQ (fun b => q.norm⁻¹*D b), + Ex_lmul] + unfold share U; ring + have HZ : share q * ((1/4:ℝ)*(q.norm/q.mass)*fax q)=1/4*fax q := by + unfold share; field_simp + have hEq : share q = share s * Q true := by + have H := kept_mass hb.1 hS hS' hm + unfold share + nth_rw 1 [H,hb.1.flat] + unfold Q v; ring + rw [h₁,← HZ,← mul_add] + unfold D; rw [Ex_add (g:=fun b => a),Ex_const] + have hDC : caveat s j=share s*(H s+a) := by unfold caveat H Z dye a; rw [hb.1.fund] + have hsq : 0 ≤ share q := div_nonneg hQ.le hZ.le + calc + _ ≤ share q*(H q+a) := by + apply mul_le_mul_of_nonneg_left _ hsq + change U.Ex (fun i => H (q.smaller i))+(1/4:ℝ)*(q.norm/q.mass)*fax q ≤ H q at ht₁ + linarith + _ ≤ _ := by + rw [hEq,mul_assoc,hDC] + exact mul_le_mul_of_nonneg_left h₂ (div_nonneg hm.le hd.le) + +/-- average of forward statistics under uniform output path -/ +def stats {s : Scene M} (t : Thread M s) (L : Path M → ℝ) := + (s.here.dice M).Ex (fun x => L (t.stream x)) + +lemma stats_cons {s q : Scene M} {k} (h : Following j J f (ran s q k)) (F : Path M → ℝ) : + stats (ran s q k) F = + (uniform Bool).Ex (fun b => stats (k b) (fun l => F (⟨s,q,b⟩::l))) := + stream_branch k h (fun l _ => F l) + +lemma stat_pay (s : Scene M) (t : Thread M s) (g : Beat M → ℝ) : + stats t (subtotal g)=Thread.fee t g := rfl +lemma stats_nil (s : Scene M) (F : Path M → ℝ) : stats (blank s) F=F [] := by simp [stats,stream,Ex_const] +lemma stats_plus {s : Scene M} (t : Thread M s) (f g : Path M → ℝ) : + stats t (fun l => f l+g l)=stats t f+stats t g := + Ex_add _ (f:=fun _ => f _) (g:=fun _ => g _) +lemma stats_fix {s : Scene M} (t : Thread M s) (r : ℝ) : stats t (fun _ => r)=r := + Ex_const _ _ + +lemma caveat_total {s : Scene M} (t : Thread M s) (h : Following j J f t) (hp : Fit s) : + stats t fullFax ≤ 4*caveat s j := by + change t.fee billFax ≤ _ + induction t with + | blank s => + change stats (blank s) fullFax ≤ _ + rw [stats_nil, show fullFax (M:=M) [] = 0 from rfl] + linarith [caveat_pos s j hp] + | ran s q k ih => + have hx := jump_ideal h hp.lift + have HH := caveat_step h hx + rw [fee_ran _ h,Ex_add,show (uniform Bool).Ex (fun b : Bool => billFax ⟨s,q,b⟩) = fax q from by unfold billFax; exact Ex_const (uniform Bool) (fax q)] + apply le_trans (b:=fax q+(uniform Bool).Ex fun b => 4*caveat (q.smaller b) j) + · gcongr + apply Ex_mono + intro b + exact ih b (h.2.2.2.2.2 b) (hx.climb.child b) + rw [Ex_lmul]; linarith + +lemma showers_total (e : Streak M) + {s : Scene M} (t : Thread M s) (h : Following j J f t) (hp : Fit s) : + stats t e.showers ≤ 4-energy s e.u+8*stats t fullKills := by + let g (b : Beat M) := shower e.u b.post + change t.fee g ≤ 4-energy s e.u+8*t.fee killed + induction t with + | blank s => + have Hh := (energy_small hp e.u).2.trans hp.lift + simp [Thread.fee,stream,Ex_const]; linarith + | ran s q k ih => + let U := uniform Bool + have H := jump_ideal h hp.lift + have HE (b : Bool) := ih b (h.2.2.2.2.2 b) (H.climb.child _) + rw [fee_ran _ h,fee_ran _ h] + simp_rw [Ex_add] + rw [Ex_const U (shower e.u q)] + have HH : U.Ex (fun b => energy (q.smaller b) e.u) = energy q e.u+shower e.u q := by + rw [show U=uniform Bool from rfl,fair_av]; exact wet_average q H.climb e.u + have HF := (energy_drain (b:=⟨s,q,false⟩) H e.u) + let K := killed (Thread.Beat.mk s q false) + have HR : (uniform Bool).Ex (fun b => killed ⟨s,q,b⟩) = K := Ex_const U K + have HT := U.Ex_mono HE + rw [Ex_add,Ex_lmul,Ex_sub (f:=fun _=> 4),Ex_const,HH] at HT + rw [HR] + change |energy s _-energy q _| ≤ 8*K at HF + change shower e.u q + U.Ex _ ≤ _ +8*(K+U.Ex _) + linarith [(abs_le.mp HF).2] + +lemma flag_point (s : Scene M) (e : Streak M) : + e.b^2* e.flag s ≤ 128*surcharge s true (e.i-1) := by + unfold Streak.flag + split_ifs with h + · rw [surcharge,ite_eq_left h.2] + apply le_trans (b:=128*gateWidth true (e.i-1)) + · change side e.i^2*1 ≤ 128*(side (e.i-1)^2 / 128) + have hh : side e.i ≤ side (e.i-1) := + pow_le_pow_of_le_one (by norm_num) (by norm_num) (by omega) + nlinarith [side_pos e.i] + · exact mul_le_mul_of_nonneg_left h.2 (by norm_num) + rw [mul_zero] + exact mul_nonneg (by norm_num) (surcharge_nonneg s true _) + +lemma flag_total (e : Streak M) {s : Scene M} (t : Thread M s) : + e.b^2 * stats t e.flagged ≤ + 128 * Thread.fee t (fun b => surcharge b.post true (e.i-1)) := by + change e.b^2 * t.fee _ ≤ _ + + unfold Thread.fee + rw [← Ex_lmul,← Ex_lmul] + apply Ex_mono; intro x + generalize t.stream x = l + induction l with + | nil => change _*0 ≤ _*0; exact le_of_eq (by ring) + | cons b l ih => + rw [List.map_cons,List.sum_cons,List.map_cons,List.sum_cons] + linarith [flag_point b.post e] + +lemma sum_budget_run : + ∀ᶠ j : ℕ in atTop, + ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s q : Scene M, ∀ k, + Following j J f (.ran s q k) → Fit s → q.live=true → + (∀ x, f x ≤ exp (height j)) → + ∀ e : Streak M, + let v := ran s q k + stats v fullKills ≤ 4 ∧ stats v e.showers ≤ 36 ∧ + stats v fullFax ≤ 10^26*height j ∧ e.b^2 * stats v e.flagged ≤ 10^6*height j := by + filter_upwards [segment_cost,side_deficit, hpoly 12800 32] with j h1 h2 ht + M J f s q k h hh hx hF e v + have hk := jump_ideal h hh.lift + have hq := pre_pos h + have hR : q.mass ≤ s.mass := hk.lower.mass_le hh.ready.2 hh.ready.1 + have hT := h.2.2.2.1.2.1 + let d := height j + have hM : exp (-d) ≤ q.mass := le_of_not_gt (fun H => hT ⟨hx,Or.inr H⟩) + have hE : -d ≤ log s.mass := by + rw [←log_exp (-d)] + exact log_le_log (exp_pos _) (hM.trans hR) + have he : (minimumT j)⁻¹ ≤ height j := by unfold minimumT; rw [one_div,inv_inv]; exact ht + have HP : stats v fullKills ≤ 4 := + le_trans (deaths v h hh.pos) hh.lift + refine ⟨HP, le_trans (showers_total e v h hh) + (by linarith [(energy_small hh e.u).1]),le_trans (caveat_total v h hh) ?_, + le_trans (flag_total e v) ?_⟩ + · have HL := h2 M J s f (rooted _ h) hF hq + have HJ : 0 ≤ share s ∧ share s ≤ 4 := ⟨share_pos' _ hh.ready hh.pos.le,hh.lift⟩ + have HY : 0 ≤ deficit s (channel (dye s)) := deficit_nonneg s hh.ready.1 hh.ready.2 hq _ + unfold caveat + have h₃ : deficit s (channel (dye s)) + (minimumT j)⁻¹ ≤ 10^23*d+d+d := by + change deficit s _ ≤ _ at HL; unfold dye d at *; linarith + have h₄ : 0 ≤ d := (d_pos j).le + change _ ≤ 10^26*d + nlinarith + · have H₁ := h1 M J s f v h hF hq hh.pos (fun _ => e.i-1) true + have H₂ : 0 ≤ s.mass/s.norm := div_nonneg hq.le hh.pos.le + have H₃ : s.mass/s.norm ≤ 4 := hh.lift + have H₄ : 128*(2*height j-log s.mass) ≤ 384*d := by unfold d at *; linarith + have h₄ : 0 ≤ d := (d_pos j).le + nlinarith + + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Paths/Wager.lean b/lean/OAI/Analysis/FourierLLogL/Paths/Wager.lean index ceabbc69e39c47a0920449bc843dd2805f26a942..f1af93a8bb26cb6a74113e4401a1993acee78da5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Paths/Wager.lean +++ b/lean/OAI/Analysis/FourierLLogL/Paths/Wager.lean @@ -1,113 +1,113 @@ -import OAI.Analysis.FourierLLogL.Paths.Trip -/-! # The uniform output path -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Paths -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot -variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - - -variable {E : Type*} - -/-- A predictable linear wager driven by the revealed output bit, with quadratic cost. -/ -structure Desk (M : ℕ) (E : Type*) where - stake : E → Scene M → Scene M → ℝ - stepAt : E → Beat M → E - -namespace Desk - -def single (x : ℝ) (i : Bool) := curl i*x-x^2/2 -def pay (A : Desk M E) (e : E) (b : Beat M) := - single (A.stake e b.pre b.post) b.bit -def tally (A : Desk M E) (e : E) : Path M → ℝ - | [] => 0 - | b::l => pay A e b + tally A (stepAt A e b) l - -def peak (A : Desk M E) (e : E) (a r : ℝ) : Path M → ℝ - | [] => r - | b::l => let x := a+ A.pay e b; peak A (stepAt A e b) x (max r x) l - -lemma peak_base (A : Desk M E) (e : E) (a r : ℝ) (l : Path M) : - r ≤ peak A e a r l := by - induction l generalizing e a r with - | nil => exact le_rfl - | cons b l ih => - apply le_trans (le_max_left r (a+A.pay e b)) - apply ih - -lemma peak_on (A : Desk M E) (e : E) (a r : ℝ) (h : a ≤ r) (l m : Path M) : - a+tally A e l ≤ peak A e a r (l++m) := by - induction l generalizing e a r with - | nil => simpa only [List.nil_append,tally,add_zero] using h.trans (peak_base A e a r m) - | cons b l ih => - rw [tally,← add_assoc]; exact ih _ _ _ (le_max_right r _) - -def heatW (A : Desk M E) (e : E) (l : Path M) := - exp (peak A e 0 0 l/2) - -lemma heatW_one (A : Desk M E) (e : E) (l : Path M) : 1 ≤ heatW A e l := - one_le_exp (by linarith [peak_base A e 0 0 l]) -lemma edge_score (A : Desk M E) (e : E) (l m : Path M) : - A.tally e l ≤ 2*log (heatW A e (l++m)) := by - rw [heatW,log_exp]; linarith [peak_on A e 0 0 (le_refl _) l m] - -private def purses (a r : ℝ) := exp (r/2)+exp (a-r/2) -private lemma purses_high (a r : ℝ) : purses a (max r a) ≤ purses a r := by - cases le_total a r with - | inl h => rw [max_eq_left h] - | inr h => - rw [max_eq_right h]; unfold purses - let x := exp (r/4); let y := exp (a/2-r/4) - have h1 : exp (r/2)=x^2 := by unfold x; rw [pow_two,← exp_add]; ring_nf - have h2 : exp (a-r/2)=y^2 := by unfold y; rw [pow_two,← exp_add]; ring_nf - have h3 : exp (a/2)=x*y := by unfold x y; rw [← exp_add]; ring_nf - rw [show a-a/2=a/2 by ring,h1,h2,h3]; nlinarith [sq_nonneg (x-y)] - -private lemma single_mean (x : ℝ) : - (uniform Bool).Ex (fun i => exp (single x i)) ≤ 1 := by - rw [fair_av]; unfold single - change (exp (1*x-x^2/2)+exp (-1*x-x^2/2))/2 ≤ _ - rw [one_mul,neg_one_mul,exp_sub,exp_sub,← add_div, - div_right_comm (_+_) (exp _),← cosh_eq] - exact (div_le_one (exp_pos _)).mpr (cosh_le_exp_half_sq _) - -lemma peak_mean (A : Desk M E) (t : Thread M s) (h : t.Following j J f) (e : E) (a r : ℝ) : - (s.here.dice M).Ex (fun x => exp (peak A e a r (t.stream x)/2)) ≤ - exp (r/2)+exp (a-r/2) := by - induction t generalizing e a r with - | blank s => - change (s.here.dice M).Ex (fun _ => exp (r/2)) ≤ _ - rw [Ex_const]; exact le_add_of_nonneg_right (exp_pos _).le - | ran s q k ih => - rw [stream_branch _ h (fun l _ => exp (peak A e a r l/2))] - let p := A.stake e s q - let w (b : Bool) := a+single p b - let P := uniform Bool - apply le_trans (b:=P.Ex fun i => purses (w i) r) - · apply Ex_mono; intro b - exact le_trans (ih b (h.2.2.2.2.2 b) _ _ _) (purses_high (w b) r) - simp only [purses,w] - have H (i : Bool) : exp (a+single p i-r/2)=exp (a-r/2)*exp (single p i) := by - rw [← exp_add]; congr 1; ring - simp_rw [H,Ex_add _ (f:=fun _ => _),Ex_const,Ex_lmul] - apply add_le_add_right - exact mul_le_of_le_one_right (exp_pos _).le (single_mean _) - -lemma heat_bound (A : Desk M E) (t : Thread M s) (h : t.Following j J f) (e : E) : - (s.here.dice M).Ex (fun x => A.heatW e (t.stream x)) ≤ 2 := by - have H := A.peak_mean t h e 0 0 - change (s.here.dice M).Ex (fun x => A.heatW e (t.stream x)) ≤ _ at H - norm_num at H; exact H - -end Desk - - -end FourierLLogL.Dyadic.Procedure.Paths - -end OAI +import OAI.Analysis.FourierLLogL.Paths.Trip +/-! # The uniform output path -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Paths +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart Snapshot +variable {M j J : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + + +variable {E : Type*} + +/-- A predictable linear wager driven by the revealed output bit, with quadratic cost. -/ +structure Desk (M : ℕ) (E : Type*) where + stake : E → Scene M → Scene M → ℝ + stepAt : E → Beat M → E + +namespace Desk + +def single (x : ℝ) (i : Bool) := curl i*x-x^2/2 +def pay (A : Desk M E) (e : E) (b : Beat M) := + single (A.stake e b.pre b.post) b.bit +def tally (A : Desk M E) (e : E) : Path M → ℝ + | [] => 0 + | b::l => pay A e b + tally A (stepAt A e b) l + +def peak (A : Desk M E) (e : E) (a r : ℝ) : Path M → ℝ + | [] => r + | b::l => let x := a+ A.pay e b; peak A (stepAt A e b) x (max r x) l + +lemma peak_base (A : Desk M E) (e : E) (a r : ℝ) (l : Path M) : + r ≤ peak A e a r l := by + induction l generalizing e a r with + | nil => exact le_rfl + | cons b l ih => + apply le_trans (le_max_left r (a+A.pay e b)) + apply ih + +lemma peak_on (A : Desk M E) (e : E) (a r : ℝ) (h : a ≤ r) (l m : Path M) : + a+tally A e l ≤ peak A e a r (l++m) := by + induction l generalizing e a r with + | nil => simpa only [List.nil_append,tally,add_zero] using h.trans (peak_base A e a r m) + | cons b l ih => + rw [tally,← add_assoc]; exact ih _ _ _ (le_max_right r _) + +def heatW (A : Desk M E) (e : E) (l : Path M) := + exp (peak A e 0 0 l/2) + +lemma heatW_one (A : Desk M E) (e : E) (l : Path M) : 1 ≤ heatW A e l := + one_le_exp (by linarith [peak_base A e 0 0 l]) +lemma edge_score (A : Desk M E) (e : E) (l m : Path M) : + A.tally e l ≤ 2*log (heatW A e (l++m)) := by + rw [heatW,log_exp]; linarith [peak_on A e 0 0 (le_refl _) l m] + +private def purses (a r : ℝ) := exp (r/2)+exp (a-r/2) +private lemma purses_high (a r : ℝ) : purses a (max r a) ≤ purses a r := by + cases le_total a r with + | inl h => rw [max_eq_left h] + | inr h => + rw [max_eq_right h]; unfold purses + let x := exp (r/4); let y := exp (a/2-r/4) + have h1 : exp (r/2)=x^2 := by unfold x; rw [pow_two,← exp_add]; ring_nf + have h2 : exp (a-r/2)=y^2 := by unfold y; rw [pow_two,← exp_add]; ring_nf + have h3 : exp (a/2)=x*y := by unfold x y; rw [← exp_add]; ring_nf + rw [show a-a/2=a/2 by ring,h1,h2,h3]; nlinarith [sq_nonneg (x-y)] + +private lemma single_mean (x : ℝ) : + (uniform Bool).Ex (fun i => exp (single x i)) ≤ 1 := by + rw [fair_av]; unfold single + change (exp (1*x-x^2/2)+exp (-1*x-x^2/2))/2 ≤ _ + rw [one_mul,neg_one_mul,exp_sub,exp_sub,← add_div, + div_right_comm (_+_) (exp _),← cosh_eq] + exact (div_le_one (exp_pos _)).mpr (cosh_le_exp_half_sq _) + +lemma peak_mean (A : Desk M E) (t : Thread M s) (h : t.Following j J f) (e : E) (a r : ℝ) : + (s.here.dice M).Ex (fun x => exp (peak A e a r (t.stream x)/2)) ≤ + exp (r/2)+exp (a-r/2) := by + induction t generalizing e a r with + | blank s => + change (s.here.dice M).Ex (fun _ => exp (r/2)) ≤ _ + rw [Ex_const]; exact le_add_of_nonneg_right (exp_pos _).le + | ran s q k ih => + rw [stream_branch _ h (fun l _ => exp (peak A e a r l/2))] + let p := A.stake e s q + let w (b : Bool) := a+single p b + let P := uniform Bool + apply le_trans (b:=P.Ex fun i => purses (w i) r) + · apply Ex_mono; intro b + exact le_trans (ih b (h.2.2.2.2.2 b) _ _ _) (purses_high (w b) r) + simp only [purses,w] + have H (i : Bool) : exp (a+single p i-r/2)=exp (a-r/2)*exp (single p i) := by + rw [← exp_add]; congr 1; ring + simp_rw [H,Ex_add _ (f:=fun _ => _),Ex_const,Ex_lmul] + apply add_le_add_right + exact mul_le_of_le_one_right (exp_pos _).le (single_mean _) + +lemma heat_bound (A : Desk M E) (t : Thread M s) (h : t.Following j J f) (e : E) : + (s.here.dice M).Ex (fun x => A.heatW e (t.stream x)) ≤ 2 := by + have H := A.peak_mean t h e 0 0 + change (s.here.dice M).Ex (fun x => A.heatW e (t.stream x)) ≤ _ at H + norm_num at H; exact H + +end Desk + + +end FourierLLogL.Dyadic.Procedure.Paths + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Plateau/Features.lean b/lean/OAI/Analysis/FourierLLogL/Plateau/Features.lean index 0f12038ec10678e663745f2edffc83129f266de7..c9e1e4fa3b7519b4c6561550a8a21bcb69517e7b 100644 --- a/lean/OAI/Analysis/FourierLLogL/Plateau/Features.lean +++ b/lean/OAI/Analysis/FourierLLogL/Plateau/Features.lean @@ -1,159 +1,159 @@ -import OAI.Analysis.FourierLLogL.Plateau.Output -import OAI.Analysis.FourierLLogL.Finite.Tail - -/-! # Finite joint observation moments -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread -variable {M J j : ℕ} {s q : Scene M} {α β : Type} [Fintype α] [Fintype β] -open scoped Classical - -def share (s : Scene M) := s.mass/s.norm -def ante (s : Scene M) (x : Grid M) := wt s.here s.wage s.norm x - -lemma ante_nonneg (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (x : Grid M) : 0 ≤ ante s x := - wt_nonneg _ hs hp _ -lemma ante_total (s : Scene M) (hp : s.here.ok M) : ∑ x, ante s x = share s := wt_total _ hp _ _ - -lemma ante_split (s : Scene M) (hp : s.here.valid) (hM : s.here.lev < M) (x : Grid M) : - ante (s.smaller false) x + ante (s.smaller true) x=2*ante s x := by - unfold ante - simp only [smaller, wt_half hp hM] - by_cases hh : x∈s.here.block M - · have h₁ := Set.disjoint_left.mp (s.here.block_separate M) - rw [block_union] at hh - rcases hh with hh|hh - · simp [child,hh,h₁ hh] - · have h2 : x∉s.here.L.block M := fun he => h₁ he hh - simp [child,hh,h2] - · have h (b : Bool) : ¬ x∈(s.here.child b).block M := fun h => hh - (block_of_below (child_below _ _) _ h) - simp_rw [ite_eq_right (h _),wt,dice_apply ⟨hp,hM.le⟩,ite_eq_right hh,zero_mul,zero_div] - ring - -lemma share_split (s : Scene M) : - share (s.smaller false)+share (s.smaller true)=2*share s := by - unfold share mass; rw [show s.here.avg s.wage = _ from s.here.avg_split_real s.wage] - dsimp only [smaller,child]; ring - -def Packet (M : ℕ) (α : Type) := Grid M → α → ℝ - -def addMass (s : Scene M) (k : Grid M → Law α) (x : Grid M) (i : α) := - ante s x*k x i - -def rung (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℝ) := - ∑ x, ∑ i, addMass s k x i * f x i - -def rungC (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℂ) := - ∑ x, ∑ i, (addMass s k x i:ℂ) * f x i - -lemma nonnegative_hist (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (k : Grid M → Law α) - (z : α) : 0 ≤ hist s k z := - Finset.sum_nonneg fun i _ => mul_nonneg (ante_nonneg s hs hp _) ((k i).nonneg _) - -lemma total_hist (s : Scene M) (hp : s.here.ok M) (k : Grid M → Law α) : - ∑ z, hist s k z=share s := by - unfold hist - rw [Finset.sum_comm] - simp_rw [← Finset.mul_sum, Law.total,mul_one] - exact ante_total s hp - -lemma rung_mono (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (k : Grid M → Law α) - (f g : Grid M → α → ℝ) (h : ∀ i j, f i j ≤ g i j) : - rung s k f ≤ rung s k g := - Finset.sum_le_sum fun _ _ => Finset.sum_le_sum fun _ _ => mul_le_mul_of_nonneg_left (h _ _) - (mul_nonneg (ante_nonneg _ hs hp _) ((k _).nonneg _)) - -lemma rung_const (s : Scene M) (hp : s.here.ok M) (k : Grid M → Law α) (c : ℝ) : - rung s k (fun _ _ => c) = share s*c := by - unfold rung; rw [Finset.sum_comm] - simp_rw [← Finset.sum_mul] - rw [← total_hist s hp k] - rfl - -lemma rung_add (s : Scene M) (k : Grid M → Law α) (f g : Grid M → α → ℝ) : - rung s k (fun i j => f i j + g i j)=rung s k f+rung s k g := by - unfold rung; simp_rw [mul_add,Finset.sum_add_distrib] - -lemma rung_lmul (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℝ) (c : ℝ) : - rung s k (fun i j => c*f i j)=c*rung s k f := by - unfold rung - simp_rw [Finset.mul_sum,mul_left_comm c] - -lemma rung_error (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (hv : s.here.ok M) - (k : Grid M → Law α) {f g : Grid M → α → ℝ} {r : ℝ} - (hh : ∀ i j, |f i j-g i j| ≤ r) : - |rung s k f-rung s k g| ≤ share s*r := by - have he {f g : Grid M → α → ℝ} (h : ∀ i j, f i j ≤ r+g i j) : - rung s k f ≤ share s*r+rung s k g := by - apply le_trans (rung_mono s hs hp _ _ _ h) - rw [rung_add (f:=fun _ _ => r),rung_const _ hv] - have h1 := he (f:=f) (g:=g) fun i j => by linarith [(abs_le.mp (hh i j)).2] - have h2 := he (f:=g) (g:=f) fun i j => by linarith [(abs_le.mp (hh i j)).1] - rw [abs_le]; constructor <;> linarith - -lemma rung_hist (s : Scene M) (k : Grid M → Law α) (f : α→ℝ) : - ∑ i, hist s k i*f i = rung s k (fun _ i => f i) := by - unfold rung; rw [Finset.sum_comm]; simp_rw [hist,Finset.sum_mul] - rfl - -lemma rung_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (k : Grid M → Law α) (f : Grid M → α → ℝ) : - rung (s.smaller false) k f+rung (s.smaller true) k f= 2*rung s k f := by - unfold rung - rw [← Finset.sum_add_distrib]; simp_rw [← Finset.sum_add_distrib,Finset.mul_sum] - refine Finset.sum_congr rfl fun i _ => ?_; refine Finset.sum_congr rfl fun j _ => ?_ - rw [addMass,addMass,← add_mul,← add_mul,ante_split s hp hh] - unfold addMass; ring - -lemma hist_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (k : Grid M → Law α) (z : α) : - hist (s.smaller false) k z + hist (s.smaller true) k z = 2*hist s k z := by - unfold hist; rw [← Finset.sum_add_distrib,Finset.mul_sum] - refine Finset.sum_congr rfl fun i hi => ?_ - have H := ante_split s hp hh i - unfold ante at H - rw [← add_mul,H,mul_assoc] - -lemma hist_le {s q : Scene M} (h : Less s q) (hs : ReadyAt s) (hh : 0 ≤ s.norm) - (k : Grid M → Law α) (z : α) : - hist q k z ≤ hist s k z := by - unfold hist wt; rw [h.here,h.flat] - refine Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_right ?_ ((k _).nonneg _) - exact div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left (h.antitone hs.2 i) - ((s.here.dice M).nonneg i)) hh - -lemma fisher_range (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (hs : 0 ≤ s.wage) (he : 0 ≤ s.norm) (k : Grid M → Law α) : 0 ≤ fisher s k ∧ fisher s k ≤ share s := by - have hM := nonnegative_hist s hs he k - constructor - exact Finset.sum_nonneg fun i _ => div_nonneg (sq_nonneg _) (hM i) - rw [← total_hist s ⟨hp,hh.le⟩ k] - apply Finset.sum_le_sum; intro i _ - have h1 (b : Bool) := nonnegative_hist (s.smaller b) hs he k i - have h2 := hist_split _ hp hh k i - unfold diff - rcases eq_or_lt_of_le (hM i) with hm|hm - · rw [← hm,div_zero] - apply (div_le_iff₀ hm).mpr - nlinarith [h1 true,h1 false,mul_nonneg (h1 true) (h1 false)] - -lemma fz_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (A : Form M) (i z : Grid M) : - fz s.here.L s.wage s.norm A i z + fz s.here.R s.wage s.norm A i z = - 2*fz s.here s.wage s.norm A i z := by - unfold fz omegaObs - rw [← Finset.sum_add_distrib,Finset.mul_sum] - refine Finset.sum_congr rfl fun x hx => ?_ - have H := ante_split s hp hh x - simp_rw [ante,smaller,child] at H - rw [← add_mul,← Complex.ofReal_add,← add_mul,H] - push_cast; ring - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Plateau.Output +import OAI.Analysis.FourierLLogL.Finite.Tail + +/-! # Finite joint observation moments -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread +variable {M J j : ℕ} {s q : Scene M} {α β : Type} [Fintype α] [Fintype β] +open scoped Classical + +def share (s : Scene M) := s.mass/s.norm +def ante (s : Scene M) (x : Grid M) := wt s.here s.wage s.norm x + +lemma ante_nonneg (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (x : Grid M) : 0 ≤ ante s x := + wt_nonneg _ hs hp _ +lemma ante_total (s : Scene M) (hp : s.here.ok M) : ∑ x, ante s x = share s := wt_total _ hp _ _ + +lemma ante_split (s : Scene M) (hp : s.here.valid) (hM : s.here.lev < M) (x : Grid M) : + ante (s.smaller false) x + ante (s.smaller true) x=2*ante s x := by + unfold ante + simp only [smaller, wt_half hp hM] + by_cases hh : x∈s.here.block M + · have h₁ := Set.disjoint_left.mp (s.here.block_separate M) + rw [block_union] at hh + rcases hh with hh|hh + · simp [child,hh,h₁ hh] + · have h2 : x∉s.here.L.block M := fun he => h₁ he hh + simp [child,hh,h2] + · have h (b : Bool) : ¬ x∈(s.here.child b).block M := fun h => hh + (block_of_below (child_below _ _) _ h) + simp_rw [ite_eq_right (h _),wt,dice_apply ⟨hp,hM.le⟩,ite_eq_right hh,zero_mul,zero_div] + ring + +lemma share_split (s : Scene M) : + share (s.smaller false)+share (s.smaller true)=2*share s := by + unfold share mass; rw [show s.here.avg s.wage = _ from s.here.avg_split_real s.wage] + dsimp only [smaller,child]; ring + +def Packet (M : ℕ) (α : Type) := Grid M → α → ℝ + +def addMass (s : Scene M) (k : Grid M → Law α) (x : Grid M) (i : α) := + ante s x*k x i + +def rung (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℝ) := + ∑ x, ∑ i, addMass s k x i * f x i + +def rungC (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℂ) := + ∑ x, ∑ i, (addMass s k x i:ℂ) * f x i + +lemma nonnegative_hist (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (k : Grid M → Law α) + (z : α) : 0 ≤ hist s k z := + Finset.sum_nonneg fun i _ => mul_nonneg (ante_nonneg s hs hp _) ((k i).nonneg _) + +lemma total_hist (s : Scene M) (hp : s.here.ok M) (k : Grid M → Law α) : + ∑ z, hist s k z=share s := by + unfold hist + rw [Finset.sum_comm] + simp_rw [← Finset.mul_sum, Law.total,mul_one] + exact ante_total s hp + +lemma rung_mono (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (k : Grid M → Law α) + (f g : Grid M → α → ℝ) (h : ∀ i j, f i j ≤ g i j) : + rung s k f ≤ rung s k g := + Finset.sum_le_sum fun _ _ => Finset.sum_le_sum fun _ _ => mul_le_mul_of_nonneg_left (h _ _) + (mul_nonneg (ante_nonneg _ hs hp _) ((k _).nonneg _)) + +lemma rung_const (s : Scene M) (hp : s.here.ok M) (k : Grid M → Law α) (c : ℝ) : + rung s k (fun _ _ => c) = share s*c := by + unfold rung; rw [Finset.sum_comm] + simp_rw [← Finset.sum_mul] + rw [← total_hist s hp k] + rfl + +lemma rung_add (s : Scene M) (k : Grid M → Law α) (f g : Grid M → α → ℝ) : + rung s k (fun i j => f i j + g i j)=rung s k f+rung s k g := by + unfold rung; simp_rw [mul_add,Finset.sum_add_distrib] + +lemma rung_lmul (s : Scene M) (k : Grid M → Law α) (f : Grid M → α → ℝ) (c : ℝ) : + rung s k (fun i j => c*f i j)=c*rung s k f := by + unfold rung + simp_rw [Finset.mul_sum,mul_left_comm c] + +lemma rung_error (s : Scene M) (hs : 0 ≤ s.wage) (hp : 0 ≤ s.norm) (hv : s.here.ok M) + (k : Grid M → Law α) {f g : Grid M → α → ℝ} {r : ℝ} + (hh : ∀ i j, |f i j-g i j| ≤ r) : + |rung s k f-rung s k g| ≤ share s*r := by + have he {f g : Grid M → α → ℝ} (h : ∀ i j, f i j ≤ r+g i j) : + rung s k f ≤ share s*r+rung s k g := by + apply le_trans (rung_mono s hs hp _ _ _ h) + rw [rung_add (f:=fun _ _ => r),rung_const _ hv] + have h1 := he (f:=f) (g:=g) fun i j => by linarith [(abs_le.mp (hh i j)).2] + have h2 := he (f:=g) (g:=f) fun i j => by linarith [(abs_le.mp (hh i j)).1] + rw [abs_le]; constructor <;> linarith + +lemma rung_hist (s : Scene M) (k : Grid M → Law α) (f : α→ℝ) : + ∑ i, hist s k i*f i = rung s k (fun _ i => f i) := by + unfold rung; rw [Finset.sum_comm]; simp_rw [hist,Finset.sum_mul] + rfl + +lemma rung_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (k : Grid M → Law α) (f : Grid M → α → ℝ) : + rung (s.smaller false) k f+rung (s.smaller true) k f= 2*rung s k f := by + unfold rung + rw [← Finset.sum_add_distrib]; simp_rw [← Finset.sum_add_distrib,Finset.mul_sum] + refine Finset.sum_congr rfl fun i _ => ?_; refine Finset.sum_congr rfl fun j _ => ?_ + rw [addMass,addMass,← add_mul,← add_mul,ante_split s hp hh] + unfold addMass; ring + +lemma hist_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (k : Grid M → Law α) (z : α) : + hist (s.smaller false) k z + hist (s.smaller true) k z = 2*hist s k z := by + unfold hist; rw [← Finset.sum_add_distrib,Finset.mul_sum] + refine Finset.sum_congr rfl fun i hi => ?_ + have H := ante_split s hp hh i + unfold ante at H + rw [← add_mul,H,mul_assoc] + +lemma hist_le {s q : Scene M} (h : Less s q) (hs : ReadyAt s) (hh : 0 ≤ s.norm) + (k : Grid M → Law α) (z : α) : + hist q k z ≤ hist s k z := by + unfold hist wt; rw [h.here,h.flat] + refine Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_right ?_ ((k _).nonneg _) + exact div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left (h.antitone hs.2 i) + ((s.here.dice M).nonneg i)) hh + +lemma fisher_range (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (hs : 0 ≤ s.wage) (he : 0 ≤ s.norm) (k : Grid M → Law α) : 0 ≤ fisher s k ∧ fisher s k ≤ share s := by + have hM := nonnegative_hist s hs he k + constructor + exact Finset.sum_nonneg fun i _ => div_nonneg (sq_nonneg _) (hM i) + rw [← total_hist s ⟨hp,hh.le⟩ k] + apply Finset.sum_le_sum; intro i _ + have h1 (b : Bool) := nonnegative_hist (s.smaller b) hs he k i + have h2 := hist_split _ hp hh k i + unfold diff + rcases eq_or_lt_of_le (hM i) with hm|hm + · rw [← hm,div_zero] + apply (div_le_iff₀ hm).mpr + nlinarith [h1 true,h1 false,mul_nonneg (h1 true) (h1 false)] + +lemma fz_split (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (A : Form M) (i z : Grid M) : + fz s.here.L s.wage s.norm A i z + fz s.here.R s.wage s.norm A i z = + 2*fz s.here s.wage s.norm A i z := by + unfold fz omegaObs + rw [← Finset.sum_add_distrib,Finset.mul_sum] + refine Finset.sum_congr rfl fun x hx => ?_ + have H := ante_split s hp hh x + simp_rw [ante,smaller,child] at H + rw [← add_mul,← Complex.ofReal_add,← add_mul,H] + push_cast; ring + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Plateau/Local.lean b/lean/OAI/Analysis/FourierLLogL/Plateau/Local.lean index 65d204c1c29800751998dab999191bb03ba52e2d..debc4470c1856493086ecb463a8d5f7bcb1509ed 100644 --- a/lean/OAI/Analysis/FourierLLogL/Plateau/Local.lean +++ b/lean/OAI/Analysis/FourierLLogL/Plateau/Local.lean @@ -1,151 +1,151 @@ -import OAI.Analysis.FourierLLogL.Plateau.Output - -/-! # Local costs and projected edge bounds -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Thread -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {M J j : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - -lemma debt_at {s q : Scene M} {l} (h : Following j J f (ran s q l)) : - let P := (keep s q).snip.probs - entropy P ≤ (minimumT j)⁻¹*P false := by - let v := keep s q - intro P - have hm := pre_pos h - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - have hf := kept_mass hb.1 ⟨ha.within,ha.positive⟩ ⟨hc.within,hc.positive⟩ hm - change q.mass = P true * s.mass at hf - have Ht : P true+P false=1 := by simpa [Fintype.sum_bool] using P.total - rcases hb.2 with hh | hh - · have hi : P true=1 := by nlinarith - have hj : P false=0 := by linarith - unfold entropy Law.crossE Ex - simp [hi,hj] - have hi : minimumT j ≤ P false := by nlinarith - have hj : 1 ≤ (minimumT j)⁻¹ * P false := by - rw [← div_eq_inv_mul]; exact (one_le_div (minimum_pos _)).mpr hi - have HH := entropy_le_log_card P - simp only [Fintype.card_bool,Nat.cast_ofNat] at HH - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] - -lemma debt_total (t : Thread M s) (h : Following j J f t) : - (unfoldW t).spotDebt ≤ (minimumT j)⁻¹ := by - let a := (minimumT j)⁻¹ - have hp := (inv_pos.mpr (minimum_pos j)).le - induction t with - | blank => rw [unfoldW,extraDebt]; exact hp - | ran s q l ih => - let v := (keep s q).snip - have hx := h.2.2.2.2.2 - have h0 : ∀ b, (unfoldW (l b)).spotDebt ≤ a := fun b => ih b (hx b) - let K := Wire.link q true fun b => unfoldW (l b) - have HE : K.spotDebt ≤ a := (Ex_mono _ h0).trans (Ex_const _ _).le - have HM := debt_at h - rw [unfoldW,splice,spotDebt,Ex,Fintype.sum_bool] - change v.probs.entropy+(v.probs true*K.spotDebt+v.probs false*spotDebt (extra (other s q) _)) ≤ a - rw [extraDebt,mul_zero,add_zero] - have hQ : v.probs true + v.probs false = 1 := by simpa [Fintype.sum_bool] using v.probs.total - change v.probs.entropy ≤ a * v.probs false at HM - nlinarith [v.probs.nonneg true] - -lemma segment_cost : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Thread M s, Following j J f t → (∀ x, f x ≤ exp (height j)) → - 0 < s.mass → 0 < s.norm → - ∀ (p : Policy M) (b : Bool), t.fee (fun i => surcharge i.post b (p i.post)) ≤ - s.mass/s.norm * (128*(2*height j-log s.mass)) := by - filter_upwards [hpoly 12800 32] with j hg M J s f t h hf hm hn p b - rw [surcharge_wire _ _ _ h hm hn] - let T := unfoldW t - have hu := rooted _ h - have hk := splice_valid t h - have H := edge_total p b T hk hm 0 (fun _ => Or.inl rfl) - have HX := due_all T true hk hm - have HD : T.spotDebt ≤ height j := - (debt_total t h).trans (by - unfold minimumT; rw [one_div,inv_inv]; exact hg) - have hi := local_deficit_dry s (uniform Unit) hu.within hu.positive hm (height j) (fun x => le_trans (hu.sub x) (hf x)) - rw [deficit,privateH_const] at hi - refine mul_le_mul_of_nonneg_left ?_ (by positivity) - change T.expenditure _ ≤ _ - norm_num at *; linarith - - -lemma side_deficit : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - s.Safe j J f → (∀ x, f x ≤ exp (height j)) → 0 < s.mass → - deficit s (channel s.fund.mood) ≤ - 10^23*height j-log s.mass := by - filter_upwards [Fund.fund_est, Fund.envelope_scale, hpoly (3000*capA) 1] with j hH hv h - M J s f hs hf hm - let p := s.here - let c := s.fund - let A := c.mood; let k := channel A - let P := s.actor; let d := height j - let v := fun _ : Grid M => uniform Unit - let Q := channel (place c.anchor M) (c.anchor.start M) - have hp := hs.capital - have h1 := replace_signal P v k (fun _ => Q) (fun i t => ch_pos _ _ _) - have hE : (P.withNext v).Ex (fun x => (k x.1).kl Q) ≤ F A-log c.anchor.invlen+300 := by - rw [Ex_withNext]; simp_rw [Ex_const] - rw [← Ex_const P (F A-log c.anchor.invlen+300)] - apply Ex_mono_mem _ - intro x hx - have he : x ∈ c.anchor.block M := - block_of_below hp.domain M (piece_support hs.within (actor_def hs.within hs.positive hm) _ hx) - exact (kl_ex _ _).le.trans (side_entropy hp.effects _ he) - have ht := local_deficit_dry s (uniform Unit) hs.within hs.positive hm d - (fun x => (hs.sub x).trans (hf x)) - have hi := hp.solvent - have H := hp.relative - have HE := ((hH M).2 s.fund s.here hp).1 - have hr : 1 ≤ d := hv.2.2.1 - change c.bank ≤ (1000+ reserve capA)*d at hi - change F A ≤ _+3000*c.uses+400*c.spend at H - change c.spend ≤ 2*(c.bank+1) at HE - have hT : 3000*(c.uses:ℝ) ≤ d := by - rw [pow_one] at h - have hx := hp.under; unfold d c; linarith - change (P.withNext v).Ex _ ≤ F A-log c.anchor.invlen+300 at hE - change levels s - P.privateH v ≤ _ at ht - change levels s - P.privateH k ≤ 10^23*d-log s.mass - apply le_trans (b:=d-log s.mass+ (F A-log c.anchor.invlen+300)) - · linarith - unfold reserve capA at * - change F A ≤ log c.anchor.invlen + _ + _ at H; linarith - -def killed (b : Beat M) := (b.pre.mass-b.post.mass) / b.pre.norm - -/-- Averaging future removals along uniform output paths, not actual input-choice paths. -/ -lemma deaths (t : Thread M s) (h : Following j J f t) (hr : 0 < s.norm) : - t.fee killed ≤ s.mass/s.norm := by - induction t with - | blank => - simp [fee,stream,Ex_const]; exact div_nonneg (Thread.rooted _ h).mass_lower hr.le - | ran s q k ih => - rw [fee_ran _ h] - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - let P := uniform Bool - let F := fun b : Bool => (q.smaller b).mass - have hi (b : Bool) : (k b).fee killed ≤ F b/q.norm := by - apply ih b (hk b) - change 0 < q.norm; rw [hb.1.flat]; exact hr - rw [Ex_add,show (fun b : Bool => killed ⟨s,q,b⟩) = (fun _ => (s.mass-q.mass)/s.norm) from rfl,Ex_const] - have hh := P.Ex_mono hi - rw [show P.Ex (fun b => F b / q.norm)=P.Ex F / q.norm by simp_rw [div_eq_mul_inv, Ex_mul]] at hh - have HX : P.Ex F=q.mass := by - unfold P F mass; rw [Ex,Fintype.sum_bool, uniform_apply,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] - simp only [smaller,child] - rw [avg_split_real q.here]; ring - rw [HX,hb.1.flat] at hh - change _ + P.Ex _ ≤ _ - linarith [show (s.mass-q.mass)/s.norm=s.mass/s.norm-q.mass/s.norm by ring] - -end FourierLLogL.Dyadic.Procedure.Thread - -end OAI +import OAI.Analysis.FourierLLogL.Plateau.Output + +/-! # Local costs and projected edge bounds -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Thread +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {M J j : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + +lemma debt_at {s q : Scene M} {l} (h : Following j J f (ran s q l)) : + let P := (keep s q).snip.probs + entropy P ≤ (minimumT j)⁻¹*P false := by + let v := keep s q + intro P + have hm := pre_pos h + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + have hf := kept_mass hb.1 ⟨ha.within,ha.positive⟩ ⟨hc.within,hc.positive⟩ hm + change q.mass = P true * s.mass at hf + have Ht : P true+P false=1 := by simpa [Fintype.sum_bool] using P.total + rcases hb.2 with hh | hh + · have hi : P true=1 := by nlinarith + have hj : P false=0 := by linarith + unfold entropy Law.crossE Ex + simp [hi,hj] + have hi : minimumT j ≤ P false := by nlinarith + have hj : 1 ≤ (minimumT j)⁻¹ * P false := by + rw [← div_eq_inv_mul]; exact (one_le_div (minimum_pos _)).mpr hi + have HH := entropy_le_log_card P + simp only [Fintype.card_bool,Nat.cast_ofNat] at HH + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] + +lemma debt_total (t : Thread M s) (h : Following j J f t) : + (unfoldW t).spotDebt ≤ (minimumT j)⁻¹ := by + let a := (minimumT j)⁻¹ + have hp := (inv_pos.mpr (minimum_pos j)).le + induction t with + | blank => rw [unfoldW,extraDebt]; exact hp + | ran s q l ih => + let v := (keep s q).snip + have hx := h.2.2.2.2.2 + have h0 : ∀ b, (unfoldW (l b)).spotDebt ≤ a := fun b => ih b (hx b) + let K := Wire.link q true fun b => unfoldW (l b) + have HE : K.spotDebt ≤ a := (Ex_mono _ h0).trans (Ex_const _ _).le + have HM := debt_at h + rw [unfoldW,splice,spotDebt,Ex,Fintype.sum_bool] + change v.probs.entropy+(v.probs true*K.spotDebt+v.probs false*spotDebt (extra (other s q) _)) ≤ a + rw [extraDebt,mul_zero,add_zero] + have hQ : v.probs true + v.probs false = 1 := by simpa [Fintype.sum_bool] using v.probs.total + change v.probs.entropy ≤ a * v.probs false at HM + nlinarith [v.probs.nonneg true] + +lemma segment_cost : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Thread M s, Following j J f t → (∀ x, f x ≤ exp (height j)) → + 0 < s.mass → 0 < s.norm → + ∀ (p : Policy M) (b : Bool), t.fee (fun i => surcharge i.post b (p i.post)) ≤ + s.mass/s.norm * (128*(2*height j-log s.mass)) := by + filter_upwards [hpoly 12800 32] with j hg M J s f t h hf hm hn p b + rw [surcharge_wire _ _ _ h hm hn] + let T := unfoldW t + have hu := rooted _ h + have hk := splice_valid t h + have H := edge_total p b T hk hm 0 (fun _ => Or.inl rfl) + have HX := due_all T true hk hm + have HD : T.spotDebt ≤ height j := + (debt_total t h).trans (by + unfold minimumT; rw [one_div,inv_inv]; exact hg) + have hi := local_deficit_dry s (uniform Unit) hu.within hu.positive hm (height j) (fun x => le_trans (hu.sub x) (hf x)) + rw [deficit,privateH_const] at hi + refine mul_le_mul_of_nonneg_left ?_ (by positivity) + change T.expenditure _ ≤ _ + norm_num at *; linarith + + +lemma side_deficit : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + s.Safe j J f → (∀ x, f x ≤ exp (height j)) → 0 < s.mass → + deficit s (channel s.fund.mood) ≤ + 10^23*height j-log s.mass := by + filter_upwards [Fund.fund_est, Fund.envelope_scale, hpoly (3000*capA) 1] with j hH hv h + M J s f hs hf hm + let p := s.here + let c := s.fund + let A := c.mood; let k := channel A + let P := s.actor; let d := height j + let v := fun _ : Grid M => uniform Unit + let Q := channel (place c.anchor M) (c.anchor.start M) + have hp := hs.capital + have h1 := replace_signal P v k (fun _ => Q) (fun i t => ch_pos _ _ _) + have hE : (P.withNext v).Ex (fun x => (k x.1).kl Q) ≤ F A-log c.anchor.invlen+300 := by + rw [Ex_withNext]; simp_rw [Ex_const] + rw [← Ex_const P (F A-log c.anchor.invlen+300)] + apply Ex_mono_mem _ + intro x hx + have he : x ∈ c.anchor.block M := + block_of_below hp.domain M (piece_support hs.within (actor_def hs.within hs.positive hm) _ hx) + exact (kl_ex _ _).le.trans (side_entropy hp.effects _ he) + have ht := local_deficit_dry s (uniform Unit) hs.within hs.positive hm d + (fun x => (hs.sub x).trans (hf x)) + have hi := hp.solvent + have H := hp.relative + have HE := ((hH M).2 s.fund s.here hp).1 + have hr : 1 ≤ d := hv.2.2.1 + change c.bank ≤ (1000+ reserve capA)*d at hi + change F A ≤ _+3000*c.uses+400*c.spend at H + change c.spend ≤ 2*(c.bank+1) at HE + have hT : 3000*(c.uses:ℝ) ≤ d := by + rw [pow_one] at h + have hx := hp.under; unfold d c; linarith + change (P.withNext v).Ex _ ≤ F A-log c.anchor.invlen+300 at hE + change levels s - P.privateH v ≤ _ at ht + change levels s - P.privateH k ≤ 10^23*d-log s.mass + apply le_trans (b:=d-log s.mass+ (F A-log c.anchor.invlen+300)) + · linarith + unfold reserve capA at * + change F A ≤ log c.anchor.invlen + _ + _ at H; linarith + +def killed (b : Beat M) := (b.pre.mass-b.post.mass) / b.pre.norm + +/-- Averaging future removals along uniform output paths, not actual input-choice paths. -/ +lemma deaths (t : Thread M s) (h : Following j J f t) (hr : 0 < s.norm) : + t.fee killed ≤ s.mass/s.norm := by + induction t with + | blank => + simp [fee,stream,Ex_const]; exact div_nonneg (Thread.rooted _ h).mass_lower hr.le + | ran s q k ih => + rw [fee_ran _ h] + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + let P := uniform Bool + let F := fun b : Bool => (q.smaller b).mass + have hi (b : Bool) : (k b).fee killed ≤ F b/q.norm := by + apply ih b (hk b) + change 0 < q.norm; rw [hb.1.flat]; exact hr + rw [Ex_add,show (fun b : Bool => killed ⟨s,q,b⟩) = (fun _ => (s.mass-q.mass)/s.norm) from rfl,Ex_const] + have hh := P.Ex_mono hi + rw [show P.Ex (fun b => F b / q.norm)=P.Ex F / q.norm by simp_rw [div_eq_mul_inv, Ex_mul]] at hh + have HX : P.Ex F=q.mass := by + unfold P F mass; rw [Ex,Fintype.sum_bool, uniform_apply,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] + simp only [smaller,child] + rw [avg_split_real q.here]; ring + rw [HX,hb.1.flat] at hh + change _ + P.Ex _ ≤ _ + linarith [show (s.mass-q.mass)/s.norm=s.mass/s.norm-q.mass/s.norm by ring] + +end FourierLLogL.Dyadic.Procedure.Thread + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Plateau/Output.lean b/lean/OAI/Analysis/FourierLLogL/Plateau/Output.lean index 229ea3fb0de8718f9be1154c7ec40926b5dc4efe..f77d25da38c43ee6da33c89a7584115958cde957 100644 --- a/lean/OAI/Analysis/FourierLLogL/Plateau/Output.lean +++ b/lean/OAI/Analysis/FourierLLogL/Plateau/Output.lean @@ -1,193 +1,193 @@ -import OAI.Analysis.FourierLLogL.Plateau.Thread - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Thread -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {M J j : ℕ} {s q : Scene M} {f : Grid M → ℝ} -open scoped Classical - -def other (s q : Scene M) := {s with wage := s.wage-q.wage} -def keep (s q : Scene M) : TossUp s where - dst := fun b => if b then q else other s q - coord := fun x => decide (0 < q.wage x) - -lemma keep_here (h : Less s q) (b) : ((keep s q).dst b).here=s.here := by cases b; rfl; exact h.here - -lemma other_ok (h : Less s q) (he : ReadyAt s) : ReadyAt (other s q) := - ⟨he.1,sub_nonneg.mpr (h.antitone he.2)⟩ - -lemma keep_true (h : Less s q) (hs : ReadyAt s) (ht : ReadyAt q) (hm : 0 < s.mass) : - (keep s q).snip.TrueLaw := by - refine ⟨weight_pos.mpr hm,?_⟩ - intro b x - have HR := actor_weight (s:=s) hs.1 hs.2 x - have HQ := actor_weight (s:=q) ht.1 ht.2 x - have he := other_ok h hs - have HE := actor_weight (s:=other s q) he.1 he.2 x - rw [show (keep s q).snip.src=s from rfl,HR] - cases b - case true => - change q.weight* q.actor x = _ - rw [HQ]; unfold insideW; rw [h.here] - by_cases HH : 0 < q.wage x - · have hx := (h.coins x).resolve_right HH.ne' - simp only [TossUp.snip,keep,decide_eq_true HH,ite_true]; rw [hx] - have hx : q.wage x = 0 := by linarith [show 0 ≤ q.wage x from ht.2 x] - simp only [TossUp.snip,keep,hx]; simp - case false => - change (other s q).weight*(other s q).actor x = _ - rw [HE]; unfold insideW - by_cases HH : 0 < q.wage x - · have hx := (h.coins x).resolve_right HH.ne' - simp only [TossUp.snip,keep,other,decide_eq_true HH,Pi.sub_apply]; rw [hx]; simp - have hx : q.wage x=0 := by linarith [show 0 ≤ q.wage x from ht.2 x] - simp only [TossUp.snip,keep,other,Pi.sub_apply,hx,sub_zero]; simp - -/-- law before membership pays q proportionally -/ -lemma kept_mass (h : Less s q) (hs : ReadyAt s) (ht : ReadyAt q) (hm : 0 < s.mass) : - q.mass = (keep s q).snip.probs true*s.mass := by - have H := balance (keep_true h hs ht hm) true - apply mul_left_cancel₀ s.here.len_pos.ne' - change q.here.len*q.mass=s.here.len*s.mass*_ at H - rw [h.here] at H; rw [H]; ring - -lemma pre_pos {t : ∀ b, Thread M (q.smaller b)} - (h : Following j J f (.ran s q t)) : 0 < s.mass := - lt_of_lt_of_le h.2.2.2.1.1.2.2 (h.2.1.1.mass_le h.1.positive h.1.within) - -def splice (s q : Scene M) (k : ∀ b, Wire M (q.smaller b)) := - Wire.spot s (keep s q) fun b => match b with - | false => extra (other s q) (M-s.here.lev) - | true => link q true k - -def unfoldW {s : Scene M} (t : Thread M s) : Wire M s := - match t with - | blank s => extra s (M-s.here.lev) - | ran s q k => splice s q fun b => unfoldW (k b) - -lemma splice_valid (t : Thread M s) (h : Following j J f t) : (unfoldW t).Valid true := by - have H (s : Scene M) (h : Safe s j J f) : ReadyAt s := ⟨h.within,h.positive⟩ - induction t with - | blank => - exact extra_ok _ _ true (H _ h) (Nat.add_sub_of_le (H _ h).1.2) - | ran s q k ih => - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - have hx : q.here.lev < M := (rooted _ (hk false)).within.2 - have hU := other_ok hb.1 (H _ ha) - refine ⟨H _ ha,keep_here hb.1,keep_true hb.1 (H _ ha) (H _ hc),?_⟩ - intro b; cases b - · exact extra_ok _ _ true hU (Nat.add_sub_of_le (H _ ha).1.2) - exact ⟨H _ hc,hx,id,fun b => ih _ (hk b)⟩ - -/-- `massLift` true-recursion accumulator pays post costs selected at Jump nodes -/ -def massLift {s : Scene M} (t : Thread M s) (f : Scene M → Scene M → ℝ) : ℝ := match t with -| .blank _ => 0 -| .ran s q k => - (keep s q).snip.probs true*(f s q+ (step q).probs.Ex (fun b => massLift (k b) f)) - -private lemma die_average (Q : Law Bool) (f : Bool → ℝ) : Q.Ex f = Q true*f true + Q false*f false := by - simp [Ex] - -lemma output_split (q : Scene M) (hv : q.here.valid) (hh : q.here.lev < M) - (hp : 0 ≤ q.wage) (hm : 0 < q.mass) (f : Bool → ℝ) : - q.mass * (step q).probs.Ex f = (uniform Bool).Ex fun b => (q.smaller b).mass*f b := by - have H := step_true hv hh hp hm - rw [die_average,die_average] - simp only [step_mass H,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] - ring - -lemma output_rule (t : Thread M s) (g : Scene M → Scene M → ℝ) (h : Following j J f t) : - s.mass * t.massLift g = - t.fee fun b => b.post.mass*g b.pre b.post := by - classical - induction t with - | blank => simp [fee,Thread.massLift,stream,Ex_const] - | ran s q k ih => - rw [fee_ran k h] - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - have hm' := hd.1.2.2 - have hM : 0 < s.mass := hm'.trans_le (hb.1.mass_le ha.positive ha.within) - have hm := kept_mass hb.1 ⟨ha.within,ha.positive⟩ ⟨hc.within,hc.positive⟩ hM - have Hi : q.here.lev < M := (rooted _ (hk false)).within.2 - have H := output_split q hc.within.1 Hi hc.positive hm' fun b => (k b).massLift g - rw [Thread.massLift,←mul_assoc,mul_comm s.mass,← hm,mul_add,H] - have hh (b : Bool) := ih b (hk b) - simp only [hh,Ex_add (uniform Bool) (f:=fun _ => q.mass*g s q),Ex_const] - -lemma spliced_cost (p : Policy M) (b : Bool) (t : Thread M s) (h : Following j J f t) - (hm : 0 < s.mass) : - (unfoldW t).expenditure (toll p b)= - t.massLift fun _ q => q.norm/q.mass * surcharge q b (p q) := by - induction t with - | blank s => - exact extra_bill _ _ _ (fun i hi => by simp [toll,hi]) (Valid.atSrc (splice_valid _ h)) - hm (Nat.add_sub_of_le (Valid.atSrc (splice_valid _ h)).1.2) - | ran s q k ih => - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - let S := keep s q - let P := S.snip.probs - let l (b : Bool) := unfoldW (k b) - have hs : ReadyAt s := ⟨ha.within,ha.positive⟩ - have ht := keep_true hb.1 hs (show ReadyAt q from ⟨hc.within,hc.positive⟩) hm - let v := S.snip - unfold unfoldW splice; rw [fee_spot (keep s q) _ _ ht] - have hp : q.here.lev < M := (rooted _ (hk false)).within.2 - have hf := step_true hc.within.1 hp hc.positive hd.1.2.2 - let g := fun (s q : Scene M) => q.norm/q.mass * surcharge q b (p q) - have H : expenditure (link q true l) (toll p b) = - g s q+(step q).probs.Ex fun b => (k b).massLift g := by - rw [fee_link _ _ _ hf,Ex_add] - have hh := hd.2.2 - simp only [toll,true_and, ite_eq_left hh,Ex_const] - congr 1 - apply Ex_congr_mem - intro i hi - exact ih i (hk _) ((true_mass hf).mp hi) - rw [die_average] - let L := expenditure (extra (other s q) (M-s.here.lev)) (toll p b) - change P true * expenditure (link q true l) (toll p b) + P false*L = _ - rw [H] - have HX : P false*L=0 := by - rcases eq_or_lt_of_le (P.nonneg false) with hi|hi; rw [←hi,zero_mul] - have He : L=0 := extra_bill _ _ _ (fun i hi => by simp [toll,hi]) (other_ok hb.1 hs) - ((true_mass ht).mp hi) (Nat.add_sub_of_le ha.within.2) - rw [He,mul_zero] - rw [HX,add_zero] - rfl - -lemma surcharge_wire (p : Policy M) (b : Bool) (t : Thread M s) (h : Following j J f t) - (hm : 0 < s.mass) (hp : 0 < s.norm) : - t.fee (fun i => surcharge i.post b (p i.post)) = - s.mass / s.norm * (unfoldW t).expenditure (toll p b) := by - rw [spliced_cost _ _ _ h hm] - apply mul_left_cancel₀ hp.ne' - let g (i : Beat M) := surcharge i.post b (p i.post) - let F (x y : Scene M) := y.norm/y.mass * surcharge y b (p y) - have H (s : Scene M) (t : Thread M s) (h : Following j J f t) : - s.norm*t.fee g = t.fee fun i => i.post.mass*F i.pre i.post := by - - induction t with - | blank => simp [fee,stream,Ex_const] - | ran s q k ih => - rw [fee_ran k h,fee_ran k h,← Ex_lmul] - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - have Hq := hd.1.2.2 - congr 1; funext i; rw [mul_add] - rw [← hb.1.flat] - have HE := ih i (hk i) - change q.norm*g _+ (q.smaller i).norm*_=_ - rw [HE] - dsimp only [g,F] - rw [show q.mass*(q.norm/q.mass* surcharge q b (p q))= q.norm * surcharge q b (p q) by field_simp] - rw [H _ t h,← output_rule t _ h] - change s.mass*t.massLift F = s.norm*(s.mass/s.norm*t.massLift F) - generalize massLift t F = x - field_simp - -end FourierLLogL.Dyadic.Procedure.Thread - -end OAI +import OAI.Analysis.FourierLLogL.Plateau.Thread + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Thread +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {M J j : ℕ} {s q : Scene M} {f : Grid M → ℝ} +open scoped Classical + +def other (s q : Scene M) := {s with wage := s.wage-q.wage} +def keep (s q : Scene M) : TossUp s where + dst := fun b => if b then q else other s q + coord := fun x => decide (0 < q.wage x) + +lemma keep_here (h : Less s q) (b) : ((keep s q).dst b).here=s.here := by cases b; rfl; exact h.here + +lemma other_ok (h : Less s q) (he : ReadyAt s) : ReadyAt (other s q) := + ⟨he.1,sub_nonneg.mpr (h.antitone he.2)⟩ + +lemma keep_true (h : Less s q) (hs : ReadyAt s) (ht : ReadyAt q) (hm : 0 < s.mass) : + (keep s q).snip.TrueLaw := by + refine ⟨weight_pos.mpr hm,?_⟩ + intro b x + have HR := actor_weight (s:=s) hs.1 hs.2 x + have HQ := actor_weight (s:=q) ht.1 ht.2 x + have he := other_ok h hs + have HE := actor_weight (s:=other s q) he.1 he.2 x + rw [show (keep s q).snip.src=s from rfl,HR] + cases b + case true => + change q.weight* q.actor x = _ + rw [HQ]; unfold insideW; rw [h.here] + by_cases HH : 0 < q.wage x + · have hx := (h.coins x).resolve_right HH.ne' + simp only [TossUp.snip,keep,decide_eq_true HH,ite_true]; rw [hx] + have hx : q.wage x = 0 := by linarith [show 0 ≤ q.wage x from ht.2 x] + simp only [TossUp.snip,keep,hx]; simp + case false => + change (other s q).weight*(other s q).actor x = _ + rw [HE]; unfold insideW + by_cases HH : 0 < q.wage x + · have hx := (h.coins x).resolve_right HH.ne' + simp only [TossUp.snip,keep,other,decide_eq_true HH,Pi.sub_apply]; rw [hx]; simp + have hx : q.wage x=0 := by linarith [show 0 ≤ q.wage x from ht.2 x] + simp only [TossUp.snip,keep,other,Pi.sub_apply,hx,sub_zero]; simp + +/-- law before membership pays q proportionally -/ +lemma kept_mass (h : Less s q) (hs : ReadyAt s) (ht : ReadyAt q) (hm : 0 < s.mass) : + q.mass = (keep s q).snip.probs true*s.mass := by + have H := balance (keep_true h hs ht hm) true + apply mul_left_cancel₀ s.here.len_pos.ne' + change q.here.len*q.mass=s.here.len*s.mass*_ at H + rw [h.here] at H; rw [H]; ring + +lemma pre_pos {t : ∀ b, Thread M (q.smaller b)} + (h : Following j J f (.ran s q t)) : 0 < s.mass := + lt_of_lt_of_le h.2.2.2.1.1.2.2 (h.2.1.1.mass_le h.1.positive h.1.within) + +def splice (s q : Scene M) (k : ∀ b, Wire M (q.smaller b)) := + Wire.spot s (keep s q) fun b => match b with + | false => extra (other s q) (M-s.here.lev) + | true => link q true k + +def unfoldW {s : Scene M} (t : Thread M s) : Wire M s := + match t with + | blank s => extra s (M-s.here.lev) + | ran s q k => splice s q fun b => unfoldW (k b) + +lemma splice_valid (t : Thread M s) (h : Following j J f t) : (unfoldW t).Valid true := by + have H (s : Scene M) (h : Safe s j J f) : ReadyAt s := ⟨h.within,h.positive⟩ + induction t with + | blank => + exact extra_ok _ _ true (H _ h) (Nat.add_sub_of_le (H _ h).1.2) + | ran s q k ih => + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + have hx : q.here.lev < M := (rooted _ (hk false)).within.2 + have hU := other_ok hb.1 (H _ ha) + refine ⟨H _ ha,keep_here hb.1,keep_true hb.1 (H _ ha) (H _ hc),?_⟩ + intro b; cases b + · exact extra_ok _ _ true hU (Nat.add_sub_of_le (H _ ha).1.2) + exact ⟨H _ hc,hx,id,fun b => ih _ (hk b)⟩ + +/-- `massLift` true-recursion accumulator pays post costs selected at Jump nodes -/ +def massLift {s : Scene M} (t : Thread M s) (f : Scene M → Scene M → ℝ) : ℝ := match t with +| .blank _ => 0 +| .ran s q k => + (keep s q).snip.probs true*(f s q+ (step q).probs.Ex (fun b => massLift (k b) f)) + +private lemma die_average (Q : Law Bool) (f : Bool → ℝ) : Q.Ex f = Q true*f true + Q false*f false := by + simp [Ex] + +lemma output_split (q : Scene M) (hv : q.here.valid) (hh : q.here.lev < M) + (hp : 0 ≤ q.wage) (hm : 0 < q.mass) (f : Bool → ℝ) : + q.mass * (step q).probs.Ex f = (uniform Bool).Ex fun b => (q.smaller b).mass*f b := by + have H := step_true hv hh hp hm + rw [die_average,die_average] + simp only [step_mass H,uniform_apply,Fintype.card_bool,Nat.cast_ofNat] + ring + +lemma output_rule (t : Thread M s) (g : Scene M → Scene M → ℝ) (h : Following j J f t) : + s.mass * t.massLift g = + t.fee fun b => b.post.mass*g b.pre b.post := by + classical + induction t with + | blank => simp [fee,Thread.massLift,stream,Ex_const] + | ran s q k ih => + rw [fee_ran k h] + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + have hm' := hd.1.2.2 + have hM : 0 < s.mass := hm'.trans_le (hb.1.mass_le ha.positive ha.within) + have hm := kept_mass hb.1 ⟨ha.within,ha.positive⟩ ⟨hc.within,hc.positive⟩ hM + have Hi : q.here.lev < M := (rooted _ (hk false)).within.2 + have H := output_split q hc.within.1 Hi hc.positive hm' fun b => (k b).massLift g + rw [Thread.massLift,←mul_assoc,mul_comm s.mass,← hm,mul_add,H] + have hh (b : Bool) := ih b (hk b) + simp only [hh,Ex_add (uniform Bool) (f:=fun _ => q.mass*g s q),Ex_const] + +lemma spliced_cost (p : Policy M) (b : Bool) (t : Thread M s) (h : Following j J f t) + (hm : 0 < s.mass) : + (unfoldW t).expenditure (toll p b)= + t.massLift fun _ q => q.norm/q.mass * surcharge q b (p q) := by + induction t with + | blank s => + exact extra_bill _ _ _ (fun i hi => by simp [toll,hi]) (Valid.atSrc (splice_valid _ h)) + hm (Nat.add_sub_of_le (Valid.atSrc (splice_valid _ h)).1.2) + | ran s q k ih => + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + let S := keep s q + let P := S.snip.probs + let l (b : Bool) := unfoldW (k b) + have hs : ReadyAt s := ⟨ha.within,ha.positive⟩ + have ht := keep_true hb.1 hs (show ReadyAt q from ⟨hc.within,hc.positive⟩) hm + let v := S.snip + unfold unfoldW splice; rw [fee_spot (keep s q) _ _ ht] + have hp : q.here.lev < M := (rooted _ (hk false)).within.2 + have hf := step_true hc.within.1 hp hc.positive hd.1.2.2 + let g := fun (s q : Scene M) => q.norm/q.mass * surcharge q b (p q) + have H : expenditure (link q true l) (toll p b) = + g s q+(step q).probs.Ex fun b => (k b).massLift g := by + rw [fee_link _ _ _ hf,Ex_add] + have hh := hd.2.2 + simp only [toll,true_and, ite_eq_left hh,Ex_const] + congr 1 + apply Ex_congr_mem + intro i hi + exact ih i (hk _) ((true_mass hf).mp hi) + rw [die_average] + let L := expenditure (extra (other s q) (M-s.here.lev)) (toll p b) + change P true * expenditure (link q true l) (toll p b) + P false*L = _ + rw [H] + have HX : P false*L=0 := by + rcases eq_or_lt_of_le (P.nonneg false) with hi|hi; rw [←hi,zero_mul] + have He : L=0 := extra_bill _ _ _ (fun i hi => by simp [toll,hi]) (other_ok hb.1 hs) + ((true_mass ht).mp hi) (Nat.add_sub_of_le ha.within.2) + rw [He,mul_zero] + rw [HX,add_zero] + rfl + +lemma surcharge_wire (p : Policy M) (b : Bool) (t : Thread M s) (h : Following j J f t) + (hm : 0 < s.mass) (hp : 0 < s.norm) : + t.fee (fun i => surcharge i.post b (p i.post)) = + s.mass / s.norm * (unfoldW t).expenditure (toll p b) := by + rw [spliced_cost _ _ _ h hm] + apply mul_left_cancel₀ hp.ne' + let g (i : Beat M) := surcharge i.post b (p i.post) + let F (x y : Scene M) := y.norm/y.mass * surcharge y b (p y) + have H (s : Scene M) (t : Thread M s) (h : Following j J f t) : + s.norm*t.fee g = t.fee fun i => i.post.mass*F i.pre i.post := by + + induction t with + | blank => simp [fee,stream,Ex_const] + | ran s q k ih => + rw [fee_ran k h,fee_ran k h,← Ex_lmul] + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + have Hq := hd.1.2.2 + congr 1; funext i; rw [mul_add] + rw [← hb.1.flat] + have HE := ih i (hk i) + change q.norm*g _+ (q.smaller i).norm*_=_ + rw [HE] + dsimp only [g,F] + rw [show q.mass*(q.norm/q.mass* surcharge q b (p q))= q.norm * surcharge q b (p q) by field_simp] + rw [H _ t h,← output_rule t _ h] + change s.mass*t.massLift F = s.norm*(s.mass/s.norm*t.massLift F) + generalize massLift t F = x + field_simp + +end FourierLLogL.Dyadic.Procedure.Thread + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Plateau/Thread.lean b/lean/OAI/Analysis/FourierLLogL/Plateau/Thread.lean index 47619694fef4c9deb515b6943ec564b9ac3df1d0..3b9134b519767813abca5c96eb5bb62b0894749d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Plateau/Thread.lean +++ b/lean/OAI/Analysis/FourierLLogL/Plateau/Thread.lean @@ -1,163 +1,163 @@ -import OAI.Analysis.FourierLLogL.Edges.Global -/-! # A plateau without its restart markers -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -def minimumT (j : ℕ) := 1/(12800*Thermal.H j^32) - -lemma minimum_pos (j) : 0 < minimumT j := by unfold minimumT; have := H_pos j; positivity - -structure Less (s q : Scene M) : Prop where - here : q.here=s.here - fund : q.fund=s.fund - flat : q.norm=s.norm - mode : q.live=s.live - coins (x) : q.wage x=s.wage x ∨ q.wage x=0 - -lemma less_self (s : Scene M) : Less s s := - ⟨rfl,rfl,rfl,rfl,fun _ => Or.inl rfl⟩ - -lemma Less.trans {s b c : Scene M} (h : Less s b) (h' : Less b c) : Less s c := - ⟨h'.here.trans h.here,h'.fund.trans h.fund,h'.flat.trans h.flat,h'.mode.trans h.mode, - fun x => (h'.coins x).elim (fun e => e.symm ▸ h.coins x) Or.inr⟩ - -lemma less_lost {s : Scene M} (u : Update j M s.here) : Less s (s.lose u) := - ⟨rfl,rfl,rfl,rfl,fun x => by rw [lose_eq]; split_ifs <;> simp⟩ - -lemma Less.antitone {s q : Scene M} (h : Less s q) (hp : 0 ≤ s.wage) : q.wage ≤ s.wage := - fun x => (h.coins x).elim (fun e => le_of_eq e) (fun e => e ▸ hp x) -lemma Less.mass_le {s q : Scene M} (h : Less s q) (hp : 0 ≤ s.wage) (hv : s.here.ok M) : - q.mass ≤ s.mass := by - unfold mass; rw [h.here,avg_ex hv,avg_ex hv] - exact Ex_mono _ (h.antitone hp) - -def shrink (s q : Scene M) (j : ℕ) := - Less s q ∧ (q.mass=s.mass ∨ q.mass ≤ (1-minimumT j)*s.mass) - -inductive Thread (M : ℕ) : Scene M → Type -| blank (s : Scene M) : Thread M s -| ran (s q : Scene M) (k : ∀ b : Bool, Thread M (q.smaller b)) : Thread M s - -namespace Thread -def Following (j J : ℕ) (f : Grid M → ℝ) {s : Scene M} (t : Thread M s) := - match t with - | blank s => s.Safe j J f - | ran s q k => s.Safe j J f ∧ shrink s q j ∧ q.Safe j J f ∧ q.work j J f ∧ - q.settled j ∧ ∀ b, Following j J f (k b) - -def reattach {s : Scene M} (q : Scene M) (t : Thread M s) : Thread M q := - match t with - | .blank _ => .blank q - | .ran _ s k => .ran q s k - -def project {s : Scene M} (r : Play j M s) : Thread M s := - match r with - | .jump s l r => .ran s s fun b => match b with - | false => project l - | true => project r - | .attempt s _ l r => reattach s (project l) - | .rest s => .blank s - | .freeze s _ => .blank s - | .ticket s _ => .blank s - -lemma rooted {s : Scene M} (t : Thread M s) (h : Following j J f t) : s.Safe j J f := - match t with | .blank _ => h | .ran _ _ _ => h.1 - -lemma projected (r : Play j M s) (h : r.Follows J f) : Following j J f (project r) := by - induction r with - | rest | ticket | freeze => exact h.1 - | jump s l r il ir => - obtain ⟨ha,hb,hc,hd,he⟩ := h - refine ⟨ha,⟨less_self _,Or.inl rfl⟩,ha,hb,hc,?_⟩ - intro b; cases b; exact il hd; exact ir he - | attempt s u l r il ir => - obtain ⟨ha,hb,hc,hu,hl,hr⟩ := h - have ht := il hl - have hp := ha.positive - change Following j J f (reattach s _) - cases H : project l with - | blank => exact ha - | ran t k => - rw [H] at ht - obtain ⟨h₁,h₂,h₃,h₄,h₅,h₆⟩ := ht - refine ⟨ha,⟨(less_lost u).trans h₂.1,?_⟩,h₃,h₄,h₅,h₆⟩ - have hv : (toss s u).TrueLaw := - toss_true _ ha.within ha.positive hb.1.2.2 - have he : (s.lose u).mass = (toss s u).probs false * s.mass := toss_mass hv false - apply Or.inr - apply le_trans (h₂.1.mass_le h₁.positive h₁.within) - rw [he] - apply mul_le_mul_of_nonneg_right _ ha.mass_lower - have Hx := ((main_bounds _ hu.ix_ok).1) - have hF := hu.bias - rw [← success_prob] at hF - have HT : minimumT j ≤ beta u.ix/2 := by - unfold minimumT - apply le_trans (b:=((1/6400)/Thermal.H j^32)/2) ?_ (by linarith) - exact le_of_eq (by ring) - have HQ := (toss s u).probs.total; rw [Fintype.sum_bool] at HQ; linarith - -/-- Local output paths, excluding terminal removals (unnecessary for transform). -/ -structure Beat (M : ℕ) where - pre : Scene M - post : Scene M - bit : Bool - -def stream {s : Scene M} (t : Thread M s) (x : Grid M) : List (Beat M) := match t with -| .blank _ => [] -| .ran s q k => - let b := (step q).bit x - ⟨s,q,b⟩::(k b).stream x - -lemma step_dice_child (p : Node) (hp : p.valid) (hh : p.lev < M) (b : Bool) - {x : Grid M} (hx : 0 < (p.child b).dice M x) : - (decide (x∈p.R.block M):Bool)=b := by - have h1 := (dice_positive (child_ok p hp hh b)).mp hx - cases b - · exact decide_eq_false (fun H => Set.disjoint_left.mp (p.block_separate M) h1 H) - exact decide_eq_true h1 - -lemma stream_branch {s q : Scene M} (k : ∀ b, Thread M (q.smaller b)) - (h : Following j J f (.ran s q k)) (T : List (Beat M) → Grid M → ℝ) : - (s.here.dice M).Ex (fun x => T (stream (.ran s q k) x) x) = - (uniform Bool).Ex (fun b => ((q.smaller b).here.dice M).Ex fun x => - T (⟨s,q,b⟩::(k b).stream x) x) := by - obtain ⟨ha,hb,hc,hd,he,hk⟩ := h - let p := q.here - have hh : p.lev < M := (rooted _ (hk false)).within.2 - rw [← hb.1.here,dice_child p hc.within.1 hh] - let Q (b : Bool) := ((q.smaller b).here.dice M).Ex fun x => T (⟨s,q,b⟩::(k b).stream x) x - have hi (b : Bool) : - ((q.smaller b).here.dice M).Ex (fun x => T (stream (.ran s q k) x) x) = Q b := by - refine Ex_congr_mem _ fun x hx => ?_ - let f (b) := T (⟨s,q,b⟩::(k b).stream x) x - have h : (step q).bit x = b := step_dice_child p hc.within.1 hh b hx - change f _=f b - rw [h] - change (((q.smaller false).here.dice M).Ex _ + ((q.smaller true).here.dice M).Ex _)/2 = - (uniform Bool).Ex Q - rw [hi false,hi true] - rw [Ex,Fintype.sum_bool]; simp [uniform_apply]; ring - -def fee (t : Thread M s) (f : Beat M → ℝ) := - (s.here.dice M).Ex fun x => (((t.stream x).map f).sum) - -lemma fee_ran {s q : Scene M} (k : ∀ b, Thread M (q.smaller b)) {g : Beat M → ℝ} - (h : Following j J f (.ran s q k)) : - fee (.ran s q k) g = - (uniform Bool).Ex (fun b => g ⟨s,q,b⟩+(k b).fee g) := by - unfold fee - rw [stream_branch _ h (fun l _ => (l.map g).sum)] - simp_rw [List.map_cons,List.sum_cons,Ex_add,Ex_const] - -end Thread -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Edges.Global +/-! # A plateau without its restart markers -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +def minimumT (j : ℕ) := 1/(12800*Thermal.H j^32) + +lemma minimum_pos (j) : 0 < minimumT j := by unfold minimumT; have := H_pos j; positivity + +structure Less (s q : Scene M) : Prop where + here : q.here=s.here + fund : q.fund=s.fund + flat : q.norm=s.norm + mode : q.live=s.live + coins (x) : q.wage x=s.wage x ∨ q.wage x=0 + +lemma less_self (s : Scene M) : Less s s := + ⟨rfl,rfl,rfl,rfl,fun _ => Or.inl rfl⟩ + +lemma Less.trans {s b c : Scene M} (h : Less s b) (h' : Less b c) : Less s c := + ⟨h'.here.trans h.here,h'.fund.trans h.fund,h'.flat.trans h.flat,h'.mode.trans h.mode, + fun x => (h'.coins x).elim (fun e => e.symm ▸ h.coins x) Or.inr⟩ + +lemma less_lost {s : Scene M} (u : Update j M s.here) : Less s (s.lose u) := + ⟨rfl,rfl,rfl,rfl,fun x => by rw [lose_eq]; split_ifs <;> simp⟩ + +lemma Less.antitone {s q : Scene M} (h : Less s q) (hp : 0 ≤ s.wage) : q.wage ≤ s.wage := + fun x => (h.coins x).elim (fun e => le_of_eq e) (fun e => e ▸ hp x) +lemma Less.mass_le {s q : Scene M} (h : Less s q) (hp : 0 ≤ s.wage) (hv : s.here.ok M) : + q.mass ≤ s.mass := by + unfold mass; rw [h.here,avg_ex hv,avg_ex hv] + exact Ex_mono _ (h.antitone hp) + +def shrink (s q : Scene M) (j : ℕ) := + Less s q ∧ (q.mass=s.mass ∨ q.mass ≤ (1-minimumT j)*s.mass) + +inductive Thread (M : ℕ) : Scene M → Type +| blank (s : Scene M) : Thread M s +| ran (s q : Scene M) (k : ∀ b : Bool, Thread M (q.smaller b)) : Thread M s + +namespace Thread +def Following (j J : ℕ) (f : Grid M → ℝ) {s : Scene M} (t : Thread M s) := + match t with + | blank s => s.Safe j J f + | ran s q k => s.Safe j J f ∧ shrink s q j ∧ q.Safe j J f ∧ q.work j J f ∧ + q.settled j ∧ ∀ b, Following j J f (k b) + +def reattach {s : Scene M} (q : Scene M) (t : Thread M s) : Thread M q := + match t with + | .blank _ => .blank q + | .ran _ s k => .ran q s k + +def project {s : Scene M} (r : Play j M s) : Thread M s := + match r with + | .jump s l r => .ran s s fun b => match b with + | false => project l + | true => project r + | .attempt s _ l r => reattach s (project l) + | .rest s => .blank s + | .freeze s _ => .blank s + | .ticket s _ => .blank s + +lemma rooted {s : Scene M} (t : Thread M s) (h : Following j J f t) : s.Safe j J f := + match t with | .blank _ => h | .ran _ _ _ => h.1 + +lemma projected (r : Play j M s) (h : r.Follows J f) : Following j J f (project r) := by + induction r with + | rest | ticket | freeze => exact h.1 + | jump s l r il ir => + obtain ⟨ha,hb,hc,hd,he⟩ := h + refine ⟨ha,⟨less_self _,Or.inl rfl⟩,ha,hb,hc,?_⟩ + intro b; cases b; exact il hd; exact ir he + | attempt s u l r il ir => + obtain ⟨ha,hb,hc,hu,hl,hr⟩ := h + have ht := il hl + have hp := ha.positive + change Following j J f (reattach s _) + cases H : project l with + | blank => exact ha + | ran t k => + rw [H] at ht + obtain ⟨h₁,h₂,h₃,h₄,h₅,h₆⟩ := ht + refine ⟨ha,⟨(less_lost u).trans h₂.1,?_⟩,h₃,h₄,h₅,h₆⟩ + have hv : (toss s u).TrueLaw := + toss_true _ ha.within ha.positive hb.1.2.2 + have he : (s.lose u).mass = (toss s u).probs false * s.mass := toss_mass hv false + apply Or.inr + apply le_trans (h₂.1.mass_le h₁.positive h₁.within) + rw [he] + apply mul_le_mul_of_nonneg_right _ ha.mass_lower + have Hx := ((main_bounds _ hu.ix_ok).1) + have hF := hu.bias + rw [← success_prob] at hF + have HT : minimumT j ≤ beta u.ix/2 := by + unfold minimumT + apply le_trans (b:=((1/6400)/Thermal.H j^32)/2) ?_ (by linarith) + exact le_of_eq (by ring) + have HQ := (toss s u).probs.total; rw [Fintype.sum_bool] at HQ; linarith + +/-- Local output paths, excluding terminal removals (unnecessary for transform). -/ +structure Beat (M : ℕ) where + pre : Scene M + post : Scene M + bit : Bool + +def stream {s : Scene M} (t : Thread M s) (x : Grid M) : List (Beat M) := match t with +| .blank _ => [] +| .ran s q k => + let b := (step q).bit x + ⟨s,q,b⟩::(k b).stream x + +lemma step_dice_child (p : Node) (hp : p.valid) (hh : p.lev < M) (b : Bool) + {x : Grid M} (hx : 0 < (p.child b).dice M x) : + (decide (x∈p.R.block M):Bool)=b := by + have h1 := (dice_positive (child_ok p hp hh b)).mp hx + cases b + · exact decide_eq_false (fun H => Set.disjoint_left.mp (p.block_separate M) h1 H) + exact decide_eq_true h1 + +lemma stream_branch {s q : Scene M} (k : ∀ b, Thread M (q.smaller b)) + (h : Following j J f (.ran s q k)) (T : List (Beat M) → Grid M → ℝ) : + (s.here.dice M).Ex (fun x => T (stream (.ran s q k) x) x) = + (uniform Bool).Ex (fun b => ((q.smaller b).here.dice M).Ex fun x => + T (⟨s,q,b⟩::(k b).stream x) x) := by + obtain ⟨ha,hb,hc,hd,he,hk⟩ := h + let p := q.here + have hh : p.lev < M := (rooted _ (hk false)).within.2 + rw [← hb.1.here,dice_child p hc.within.1 hh] + let Q (b : Bool) := ((q.smaller b).here.dice M).Ex fun x => T (⟨s,q,b⟩::(k b).stream x) x + have hi (b : Bool) : + ((q.smaller b).here.dice M).Ex (fun x => T (stream (.ran s q k) x) x) = Q b := by + refine Ex_congr_mem _ fun x hx => ?_ + let f (b) := T (⟨s,q,b⟩::(k b).stream x) x + have h : (step q).bit x = b := step_dice_child p hc.within.1 hh b hx + change f _=f b + rw [h] + change (((q.smaller false).here.dice M).Ex _ + ((q.smaller true).here.dice M).Ex _)/2 = + (uniform Bool).Ex Q + rw [hi false,hi true] + rw [Ex,Fintype.sum_bool]; simp [uniform_apply]; ring + +def fee (t : Thread M s) (f : Beat M → ℝ) := + (s.here.dice M).Ex fun x => (((t.stream x).map f).sum) + +lemma fee_ran {s q : Scene M} (k : ∀ b, Thread M (q.smaller b)) {g : Beat M → ℝ} + (h : Following j J f (.ran s q k)) : + fee (.ran s q k) g = + (uniform Bool).Ex (fun b => g ⟨s,q,b⟩+(k b).fee g) := by + unfold fee + rw [stream_branch _ h (fun l _ => (l.map g).sum)] + simp_rw [List.map_cons,List.sum_cons,Ex_add,Ex_const] + +end Thread +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Addition.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Addition.lean index 9aef9335c0bf3327eb9e07cf3fd288efd0834cf1..8cc20df2fb5ca9cda1677bc73b24f24bf7a24636 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Addition.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Addition.lean @@ -1,164 +1,164 @@ -import OAI.Analysis.FourierLLogL.Procedure.Deficit -import OAI.Analysis.FourierLLogL.Thermal.Posterior -/-! # Cost of revealing the new main observation -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -private def measureRate (p : Node) (A : Form M) (z v : Grid M) := - side M * (boltz A (z-v)* boltz (place p M) (start p M-v)) - -private lemma weight_pos' (p : Node) (A : Form M) (z v : Grid M) : - 0 < measureRate p A z v := - mul_pos (side_pos _) (mul_pos (boltz_pos _ _) (boltz_pos _ _)) - -private lemma rate_sum_pos (p : Node) (A : Form M) (z : Grid M) : - 0 < ∑ v, measureRate p A z v := - Finset.sum_pos (fun _ _ => weight_pos' _ _ _ _) Finset.univ_nonempty - -private lemma rate_sum_le (p : Node) (A : Form M) (hA : 0 ≤ A) (z : Grid M) : - ∑ v, measureRate p A z v ≤ exp (-F (A+place p M)) := by - let s : Profile M := [(A,z), (place p M,start p M)] - have ht : mixForm s=A+place p M := by simp [s,mixForm] - have hV (x : Grid M) : measureRate p A z x = side M*tune s x := by - unfold s measureRate; rw [tune_cons,tune_cons] - simp [tune,sumE] - have H := profile_envelope (s:=s) (by - intro i hi - simp only [s,List.mem_cons,List.not_mem_nil,or_false] at hi - rcases hi with h|h <;> (subst i; first | exact hA | exact place_valid _ _)) 0 - simp_rw [hV,← Finset.mul_sum] - unfold fc at H - simp only [ht,neg_zero,chi_zero_left,mul_one,expU_real,Complex.norm_real,Real.norm_eq_abs] at H - change expU (fun x => tune s x) ≤ _ - rw [F_origin]; exact (le_abs_self _).trans H - -private def matchLabel (p : Node) (A : Form M) (z : Grid M) : Law (Grid M) := - proportional _ (fun i => (weight_pos' p A z i).le) (rate_sum_pos p A z) - -private lemma match_pos (p : Node) (A : Form M) (z v : Grid M) : - 0 < matchLabel p A z v := - div_pos (weight_pos' _ _ _ _) (rate_sum_pos _ _ _) - -private lemma KL_bound (p : Node) (A B : Form M) (hA : 0 ≤ A) (h : A+place p M ≤ B) - (x z : Grid M) : - let q := matchLabel p A - (channel B x).kl (q z) ≤ F B-F (A+place p M) + - (channel B x).Ex (fun y => form A (z-y) + form (place p M) (start p M-y)) := by - intro q - have hB : 0 ≤ B := (add_nonneg hA (place_valid _ _)).trans h - rw [kl_ex] - conv_rhs => lhs; rw [← Ex_const (channel B x) (F B-F (A+place p M))] - rw [← Ex_add]; apply Ex_mono; intro i - let S := place p M; let t := start p M - change log (channel B x i)-log (measureRate p A z i/∑ v, measureRate p A z v) ≤ - F B-F (A+S) + (form A (z-i)+form S (t-i)) - have H := log_le_log (rate_sum_pos p A z) (rate_sum_le p A hA z) - rw [log_exp] at H - rw [ch_K,K_eq,log_mul (side_pos _).ne' (exp_pos _).ne',log_exp, - Real.log_div (weight_pos' _ _ _ _).ne' (rate_sum_pos _ _ _).ne', - measureRate, log_mul (side_pos _).ne' (mul_pos (boltz_pos _ _) (boltz_pos _ _)).ne', - log_mul (boltz_pos _ _).ne' (boltz_pos _ _).ne',boltz,boltz,log_exp,log_exp] - change _ ≤ -F (A+S) at H - unfold S t at * - linarith [form_nonneg hB (i-x)] - -theorem added_deficit (s : Scene M) (p : Node) (A B : Form M) - (hp : s.here.ok M) (he : s.here=p) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) - (hA : 0 ≤ A) (hB : A+place p M ≤ B) : - deficit s (channel B) ≤ deficit s (channel A)+ - (F B-F (A+place p M)+4*(label A).Ex (form A)+264) := by - subst p - let q := s.here; let S := place q M - let k := channel A; let v := channel B - let T := matchLabel q A - let P := s.actor; let H := P.withNext k - let x := F B-F (A+S) - let l := (label A).Ex (form A) - let y := x+4*l+264 - have hi : H.Ex (fun z => (v z.1).kl (T z.2)) ≤ y := by - unfold H; rw [Ex_withNext] - have hhA : 0 ≤ S := place_valid q _ - have hx : S ≤ B := le_trans (le_add_of_nonneg_left hA) hB - have hv : A ≤ B := le_trans (le_add_of_nonneg_right hhA) hB - let m := q.start M - have hm' (i) (hi : 0 < P i) : - (k i).Ex (fun z => (v i).kl (T z)) ≤ y := by - have he : i∈s.here.block M := piece_support hp (actor_def hp hh hm) _ hi - have hE := place_internal (start_mem hp.1 hp.2) he - have hh (z w : Grid M) : form A (z-w)+form S (m-w) ≤ - 2*(form A (z-i)+form S (m-i))+ 2*(form A (i-w)+form S (i-w)) := by - have h1 := form_triangle hA (z-i) (i-w) - have h2 := form_triangle hhA (m-i) (i-w) - rw [sub_add_sub_cancel] at h1 h2 - linarith - have hG (v : Form M) (A : Form M) : - (channel A i).Ex (fun y => form v (i-y)) = (label A).Ex (form v) := by - have h1 (y : Grid M) : form v (i-y) = form v (y-i) := by - rw [show i-y= -(y-i) by ring, form_even _ _] - simp_rw [h1]; exact ch_noise _ _ _ - have Hta (z : Grid M) : - (v i).kl (T z) ≤ x+2*(form A (z-i)+form S (m-i))+ - 2*(l+100) := by - have hi := KL_bound q A B hA hB i z - apply hi.trans - have he := (v i).Ex_mono (hh z) - rw [Ex_add _ (f:=fun _ => 2*(form A (z-i)+form S (m-i))), Ex_const,Ex_lmul,Ex_add (f:=fun w => form A (i-w))] at he - unfold v at he - rw [hG,hG] at he - have h1 := S_energy q.invlen q.invlen_ge (loc_scale hp.2) B hx - have h2 := form_energy_mono hA hv hA - unfold x l m S q place at * - linarith - apply le_trans ((k i).Ex_mono Hta); unfold k - rw [Ex_add,Ex_add,Ex_lmul,Ex_add,Ex_const,Ex_const,Ex_const,ch_noise A (form A)] - unfold y x S m l q at *; linarith - exact (P.Ex_mono_mem hm').trans (P.Ex_const y).le - have he := (replace_signal P k v T (match_pos q A)).trans hi - unfold deficit; change levels s-P.privateH v ≤ levels s-P.privateH k+y - linarith - -lemma priced_change : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ u : Update j M s.here, Safe s j J f → Safe (s.win u) j J f → 0 < (s.win u).mass → - u.Good capA s.fund.mood s.actor → - let A := s.fund.mood - deficit (s.win u) (channel (u.next A)) ≤ deficit (s.win u) (channel A) + 500*u.credit := by - filter_upwards [h_large 10,stiff_budget capA cap_ok] with j hp hx M J s f u hs hS hm hU - intro A - have H := added_deficit (win s u) s.here A (u.next A) hS.within rfl hS.positive hm - hs.capital.effects.nonneg (le_add_of_nonneg_right hU.positive) - have hh := hs.capital.effects.energy - let a := Thermal.H j - let d := height j - have he : 1 ≤ a := H_ge _ - have ht : 0 ≤ plateErr j := by unfold plateErr; exact div_nonneg (d_pos _).le (pow_nonneg (H_pos _).le _) - have hy := hU.reserves.1 - have hX := hs.capital.under - apply H.trans - have H : 4*(label A).Ex (form A)+264 ≤ 100*u.credit := by - have hw : - (label A).Ex (form A) ≤ 2*(capA*a*(plateErr j + 1000*a)) := by - apply le_trans hh - calc - _ ≤ (capA*a)*(2*(plateErr j+1000*a)) := by gcongr <;> linarith - _ = _ := by ring - change capA*a*(plateErr j + 1000*a)+2000 ≤ d/a^4 at hx - have hZ : d/a^4 ≤ u.credit := by - apply le_trans _ hy; unfold d a - gcongr <;> first | exact (d_pos j).le | exact H_ge j | omega - have h0 : 0 ≤ capA*a*(plateErr j + 1000*a) := - mul_nonneg (mul_nonneg (le_trans zero_le_one cap_ok) (by linarith)) (by linarith) - linarith - linarith [hU.cost] - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Deficit +import OAI.Analysis.FourierLLogL.Thermal.Posterior +/-! # Cost of revealing the new main observation -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +private def measureRate (p : Node) (A : Form M) (z v : Grid M) := + side M * (boltz A (z-v)* boltz (place p M) (start p M-v)) + +private lemma weight_pos' (p : Node) (A : Form M) (z v : Grid M) : + 0 < measureRate p A z v := + mul_pos (side_pos _) (mul_pos (boltz_pos _ _) (boltz_pos _ _)) + +private lemma rate_sum_pos (p : Node) (A : Form M) (z : Grid M) : + 0 < ∑ v, measureRate p A z v := + Finset.sum_pos (fun _ _ => weight_pos' _ _ _ _) Finset.univ_nonempty + +private lemma rate_sum_le (p : Node) (A : Form M) (hA : 0 ≤ A) (z : Grid M) : + ∑ v, measureRate p A z v ≤ exp (-F (A+place p M)) := by + let s : Profile M := [(A,z), (place p M,start p M)] + have ht : mixForm s=A+place p M := by simp [s,mixForm] + have hV (x : Grid M) : measureRate p A z x = side M*tune s x := by + unfold s measureRate; rw [tune_cons,tune_cons] + simp [tune,sumE] + have H := profile_envelope (s:=s) (by + intro i hi + simp only [s,List.mem_cons,List.not_mem_nil,or_false] at hi + rcases hi with h|h <;> (subst i; first | exact hA | exact place_valid _ _)) 0 + simp_rw [hV,← Finset.mul_sum] + unfold fc at H + simp only [ht,neg_zero,chi_zero_left,mul_one,expU_real,Complex.norm_real,Real.norm_eq_abs] at H + change expU (fun x => tune s x) ≤ _ + rw [F_origin]; exact (le_abs_self _).trans H + +private def matchLabel (p : Node) (A : Form M) (z : Grid M) : Law (Grid M) := + proportional _ (fun i => (weight_pos' p A z i).le) (rate_sum_pos p A z) + +private lemma match_pos (p : Node) (A : Form M) (z v : Grid M) : + 0 < matchLabel p A z v := + div_pos (weight_pos' _ _ _ _) (rate_sum_pos _ _ _) + +private lemma KL_bound (p : Node) (A B : Form M) (hA : 0 ≤ A) (h : A+place p M ≤ B) + (x z : Grid M) : + let q := matchLabel p A + (channel B x).kl (q z) ≤ F B-F (A+place p M) + + (channel B x).Ex (fun y => form A (z-y) + form (place p M) (start p M-y)) := by + intro q + have hB : 0 ≤ B := (add_nonneg hA (place_valid _ _)).trans h + rw [kl_ex] + conv_rhs => lhs; rw [← Ex_const (channel B x) (F B-F (A+place p M))] + rw [← Ex_add]; apply Ex_mono; intro i + let S := place p M; let t := start p M + change log (channel B x i)-log (measureRate p A z i/∑ v, measureRate p A z v) ≤ + F B-F (A+S) + (form A (z-i)+form S (t-i)) + have H := log_le_log (rate_sum_pos p A z) (rate_sum_le p A hA z) + rw [log_exp] at H + rw [ch_K,K_eq,log_mul (side_pos _).ne' (exp_pos _).ne',log_exp, + Real.log_div (weight_pos' _ _ _ _).ne' (rate_sum_pos _ _ _).ne', + measureRate, log_mul (side_pos _).ne' (mul_pos (boltz_pos _ _) (boltz_pos _ _)).ne', + log_mul (boltz_pos _ _).ne' (boltz_pos _ _).ne',boltz,boltz,log_exp,log_exp] + change _ ≤ -F (A+S) at H + unfold S t at * + linarith [form_nonneg hB (i-x)] + +theorem added_deficit (s : Scene M) (p : Node) (A B : Form M) + (hp : s.here.ok M) (he : s.here=p) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) + (hA : 0 ≤ A) (hB : A+place p M ≤ B) : + deficit s (channel B) ≤ deficit s (channel A)+ + (F B-F (A+place p M)+4*(label A).Ex (form A)+264) := by + subst p + let q := s.here; let S := place q M + let k := channel A; let v := channel B + let T := matchLabel q A + let P := s.actor; let H := P.withNext k + let x := F B-F (A+S) + let l := (label A).Ex (form A) + let y := x+4*l+264 + have hi : H.Ex (fun z => (v z.1).kl (T z.2)) ≤ y := by + unfold H; rw [Ex_withNext] + have hhA : 0 ≤ S := place_valid q _ + have hx : S ≤ B := le_trans (le_add_of_nonneg_left hA) hB + have hv : A ≤ B := le_trans (le_add_of_nonneg_right hhA) hB + let m := q.start M + have hm' (i) (hi : 0 < P i) : + (k i).Ex (fun z => (v i).kl (T z)) ≤ y := by + have he : i∈s.here.block M := piece_support hp (actor_def hp hh hm) _ hi + have hE := place_internal (start_mem hp.1 hp.2) he + have hh (z w : Grid M) : form A (z-w)+form S (m-w) ≤ + 2*(form A (z-i)+form S (m-i))+ 2*(form A (i-w)+form S (i-w)) := by + have h1 := form_triangle hA (z-i) (i-w) + have h2 := form_triangle hhA (m-i) (i-w) + rw [sub_add_sub_cancel] at h1 h2 + linarith + have hG (v : Form M) (A : Form M) : + (channel A i).Ex (fun y => form v (i-y)) = (label A).Ex (form v) := by + have h1 (y : Grid M) : form v (i-y) = form v (y-i) := by + rw [show i-y= -(y-i) by ring, form_even _ _] + simp_rw [h1]; exact ch_noise _ _ _ + have Hta (z : Grid M) : + (v i).kl (T z) ≤ x+2*(form A (z-i)+form S (m-i))+ + 2*(l+100) := by + have hi := KL_bound q A B hA hB i z + apply hi.trans + have he := (v i).Ex_mono (hh z) + rw [Ex_add _ (f:=fun _ => 2*(form A (z-i)+form S (m-i))), Ex_const,Ex_lmul,Ex_add (f:=fun w => form A (i-w))] at he + unfold v at he + rw [hG,hG] at he + have h1 := S_energy q.invlen q.invlen_ge (loc_scale hp.2) B hx + have h2 := form_energy_mono hA hv hA + unfold x l m S q place at * + linarith + apply le_trans ((k i).Ex_mono Hta); unfold k + rw [Ex_add,Ex_add,Ex_lmul,Ex_add,Ex_const,Ex_const,Ex_const,ch_noise A (form A)] + unfold y x S m l q at *; linarith + exact (P.Ex_mono_mem hm').trans (P.Ex_const y).le + have he := (replace_signal P k v T (match_pos q A)).trans hi + unfold deficit; change levels s-P.privateH v ≤ levels s-P.privateH k+y + linarith + +lemma priced_change : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ u : Update j M s.here, Safe s j J f → Safe (s.win u) j J f → 0 < (s.win u).mass → + u.Good capA s.fund.mood s.actor → + let A := s.fund.mood + deficit (s.win u) (channel (u.next A)) ≤ deficit (s.win u) (channel A) + 500*u.credit := by + filter_upwards [h_large 10,stiff_budget capA cap_ok] with j hp hx M J s f u hs hS hm hU + intro A + have H := added_deficit (win s u) s.here A (u.next A) hS.within rfl hS.positive hm + hs.capital.effects.nonneg (le_add_of_nonneg_right hU.positive) + have hh := hs.capital.effects.energy + let a := Thermal.H j + let d := height j + have he : 1 ≤ a := H_ge _ + have ht : 0 ≤ plateErr j := by unfold plateErr; exact div_nonneg (d_pos _).le (pow_nonneg (H_pos _).le _) + have hy := hU.reserves.1 + have hX := hs.capital.under + apply H.trans + have H : 4*(label A).Ex (form A)+264 ≤ 100*u.credit := by + have hw : + (label A).Ex (form A) ≤ 2*(capA*a*(plateErr j + 1000*a)) := by + apply le_trans hh + calc + _ ≤ (capA*a)*(2*(plateErr j+1000*a)) := by gcongr <;> linarith + _ = _ := by ring + change capA*a*(plateErr j + 1000*a)+2000 ≤ d/a^4 at hx + have hZ : d/a^4 ≤ u.credit := by + apply le_trans _ hy; unfold d a + gcongr <;> first | exact (d_pos j).le | exact H_ge j | omega + have h0 : 0 ≤ capA*a*(plateErr j + 1000*a) := + mul_nonneg (mul_nonneg (le_trans zero_le_one cap_ok) (by linarith)) (by linarith) + linarith + linarith [hU.cost] + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Capital.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Capital.lean index 4fc4250c1ddc723be859349a75942d8246894bf7..6c8c85fc947ef22423c8063e697a8b65ef0068c3 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Capital.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Capital.lean @@ -1,153 +1,153 @@ -import OAI.Analysis.FourierLLogL.Thermal.Update -/-! # Deterministic budget state and caps -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal - -def capA : ℝ := 10^16 -lemma cap_ok : 1 ≤ capA := by norm_num [capA] - -variable {M j : ℕ} - -structure Fund (M : ℕ) where - mood : Form M - anchor : Node - uses : ℕ - spend : ℝ - bank : ℝ - -namespace Fund -def initial (M : ℕ) : Fund M := ⟨0,root,0,0,0⟩ -def advance (p : Node) (c : Fund M) (u : Update j M p) : Fund M := - ⟨u.next c.mood,p,c.uses+1,c.spend+u.credit,c.bank+u.bill⟩ - -def breached (j : ℕ) (c : Fund M) := 1000*height j < c.bank -def fuel (j : ℕ) := eps*(height j / Thermal.H j) - -structure Ok (c : Fund M) (p : Node) (j : ℕ) : Prop where - domain : below p c.anchor - effects : Certified (H j) (plateErr j) c.uses c.anchor c.mood - under : (c.uses:ℝ) ≤ capA*H j - nonneg : 0 ≤ c.bank ∧ c.bank ≤ c.spend - solvent : c.bank ≤ (1000+reserve capA)*height j - floor_ok : c.uses*fuel j ≤ c.spend - relative : F c.mood ≤ log c.anchor.invlen + 3000*c.uses+400*c.spend - verified : gap c.mood (track capA j c.uses) ≤ 402*c.bank-5000*(c.spend-c.bank) - -lemma Ok.restrict {p q : Node} {c : Fund M} (h : c.Ok p j) (he : below q p) : c.Ok q j := - { h with domain := below_trans he h.domain } - -lemma track_add (j n : ℕ) : - track capA j (n+1)=track capA j n+ stepRad capA j := by simp only [track]; push_cast; ring - -lemma envelope_scale : - ∀ᶠ j : ℕ in atTop, sepRad j ≤ 1 ∧ fuel j > 0 ∧ 1 ≤ height j ∧ 1 ≤ height j/(H j)^2 := by - filter_upwards [spacing capA cap_ok,hpoly 1 2] with j hj hh - have hx := H_pos j; have hd := d_pos j - refine ⟨hj.2.2.2.2.1,mul_pos eps_pos (div_pos hd hx),one_le_pow₀ (by norm_num),?_⟩ - exact (le_div_iff₀ (by positivity)).mpr hh - -lemma fund_est : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, (initial M).Ok root j ∧ - ∀ c : Fund M, ∀ p, Ok c p j → - c.spend ≤ 2*(c.bank+1) ∧ - (¬ breached j c → p.ok M → Press capA j p c.mood ∧ - c.uses+1 ≤ capA*H j ∧ - ∀ (u : Update j M p) P, u.Good capA c.mood P → - Ok (c.advance p u) p j) := by - filter_upwards [envelope_scale,hpoly (3000*capA) 1,kappa_scales (codeWeight capA) (code_ge cap_ok)] with j hj h ht M - have hr := hj.1 - have hp := H_ge j - let a := H j; let d := height j - have hD : 1 ≤ d := hj.2.2.1 - have ha : 1 ≤ a := hp; have ha' : 0 < a := H_pos _ - constructor - · refine ⟨below_refl _,certified_start M,?_,by simp [initial],?_,?_,?_,?_⟩ - · simp only [initial,Nat.cast_zero]; linarith [show (1:ℝ) ≤ capA*H j from one_le_mul_of_one_le_of_one_le cap_ok hp] - · change 0 ≤ _; have := reserve_ge cap_ok; have := d_pos j; positivity - · simp [initial] - · simp [initial, invlen_top,F_zero] - have he := r_steps cap_ok j 0 (show ((0:ℕ):ℝ) ≤ _ by - push_cast; have := one_le_mul_of_one_le_of_one_le cap_ok hp; linarith) - dsimp [initial] - rw [gap_zero he.1 (by have := he.2; linarith)] - simp - intro c p hh - have hA := hh.effects.nonneg - have HX := r_steps cap_ok _ _ hh.under - have Hx := hh.verified - have hy := gap_lower hA HX.1 (by linarith [HX.2]) - have hnp := hh.nonneg - have hz : c.spend ≤ 2*(c.bank+1) := by linarith [hnp.1] - refine ⟨hz,?_⟩ - intro hd he - rw [breached] at hd - have HH : c.spend ≤ 2002*d := by change ¬1000*d<_ at hd; linarith - have ht' := hh.floor_ok - have hI : c.uses+1 ≤ capA*H j := by - have hk : (c.uses:ℝ) ≤ 20020000*a := by - have HL := ht'.trans HH - unfold fuel eps at HL; change (c.uses:ℝ)*(1/10000*(d/a)) ≤ _ at HL - apply le_of_mul_le_mul_left _ (show 0 < d by linarith) - field_simp at HL; nlinarith - unfold capA; nlinarith [show H j=a from rfl] - refine ⟨⟨he,c.uses,c.anchor,hh.domain,hh.effects,hh.under,?_⟩,hI,?_⟩ - · have ht : 3000*c.uses ≤ d := by - have h1 := hh.under - change _*H j^1 ≤ d at h; rw [pow_one] at h - change 3000*(c.uses:ℝ) ≤ d - linarith - change _ ≤ _+capA*d - have hL := hh.relative - unfold capA; nlinarith - intro u P hu - let v := c.advance p u - have hK := certified_add hh.effects hp he hu.positive hu.plate - have hR : Ok v p j := by - have hf : fuel j ≤ u.credit := by - apply le_trans _ hu.duration - apply mul_le_mul_of_nonneg_left _ eps_pos.le - apply le_trans _ (ht _ hu.ix_ok).2.2.1 - apply div_le_div_of_nonneg_right _ (H_pos _).le - exact le_mul_of_one_le_left (d_pos j).le (code_ge cap_ok) - have hc : 0 ≤ u.credit := hj.2.1.le.trans hf - have hJ := hu.gap_est _ HX.1 HX.2 - refine ⟨below_refl _,hK,?_,?_,?_,?_,?_,?_⟩ - · unfold v advance; dsimp; push_cast; exact hI - · dsimp only [v,advance] - have he : 0 ≤ u.bill ∧ u.bill ≤ u.credit := by unfold Update.bill; split <;> simp [hc] - constructor - exact add_nonneg hnp.1 he.1 - exact add_le_add hnp.2 he.2 - · change c.bank+u.bill ≤ _ - unfold Update.bill; split - · have hg := hu.reserves.2; linarith [hh.solvent] - simp only [add_zero, hh.solvent] - · change ((c.uses+1:ℕ):ℝ) * _ ≤ c.spend + u.credit - push_cast; linarith - · change F (u.next c.mood) ≤ _ - dsimp only [v,advance] - push_cast - have HZ := space_increment hh.effects hh.domain he - linarith [hu.cost,hh.relative] - · change gap (u.next c.mood) (track capA j (c.uses+1)) ≤ _ - rw [track_add] - change gap _ _ ≤ 402*(c.bank+u.bill)-5000*((c.spend+u.credit)-(c.bank+u.bill)) - unfold Update.bill - by_cases hh : u.regular = true - · rw [ite_eq_left hh] at * - linarith - rw [ite_eq_right hh] at * - have ht := hu.cheap (by simpa using hh) - unfold eps at ht - linarith - exact hR -end Fund - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Update +/-! # Deterministic budget state and caps -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal + +def capA : ℝ := 10^16 +lemma cap_ok : 1 ≤ capA := by norm_num [capA] + +variable {M j : ℕ} + +structure Fund (M : ℕ) where + mood : Form M + anchor : Node + uses : ℕ + spend : ℝ + bank : ℝ + +namespace Fund +def initial (M : ℕ) : Fund M := ⟨0,root,0,0,0⟩ +def advance (p : Node) (c : Fund M) (u : Update j M p) : Fund M := + ⟨u.next c.mood,p,c.uses+1,c.spend+u.credit,c.bank+u.bill⟩ + +def breached (j : ℕ) (c : Fund M) := 1000*height j < c.bank +def fuel (j : ℕ) := eps*(height j / Thermal.H j) + +structure Ok (c : Fund M) (p : Node) (j : ℕ) : Prop where + domain : below p c.anchor + effects : Certified (H j) (plateErr j) c.uses c.anchor c.mood + under : (c.uses:ℝ) ≤ capA*H j + nonneg : 0 ≤ c.bank ∧ c.bank ≤ c.spend + solvent : c.bank ≤ (1000+reserve capA)*height j + floor_ok : c.uses*fuel j ≤ c.spend + relative : F c.mood ≤ log c.anchor.invlen + 3000*c.uses+400*c.spend + verified : gap c.mood (track capA j c.uses) ≤ 402*c.bank-5000*(c.spend-c.bank) + +lemma Ok.restrict {p q : Node} {c : Fund M} (h : c.Ok p j) (he : below q p) : c.Ok q j := + { h with domain := below_trans he h.domain } + +lemma track_add (j n : ℕ) : + track capA j (n+1)=track capA j n+ stepRad capA j := by simp only [track]; push_cast; ring + +lemma envelope_scale : + ∀ᶠ j : ℕ in atTop, sepRad j ≤ 1 ∧ fuel j > 0 ∧ 1 ≤ height j ∧ 1 ≤ height j/(H j)^2 := by + filter_upwards [spacing capA cap_ok,hpoly 1 2] with j hj hh + have hx := H_pos j; have hd := d_pos j + refine ⟨hj.2.2.2.2.1,mul_pos eps_pos (div_pos hd hx),one_le_pow₀ (by norm_num),?_⟩ + exact (le_div_iff₀ (by positivity)).mpr hh + +lemma fund_est : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, (initial M).Ok root j ∧ + ∀ c : Fund M, ∀ p, Ok c p j → + c.spend ≤ 2*(c.bank+1) ∧ + (¬ breached j c → p.ok M → Press capA j p c.mood ∧ + c.uses+1 ≤ capA*H j ∧ + ∀ (u : Update j M p) P, u.Good capA c.mood P → + Ok (c.advance p u) p j) := by + filter_upwards [envelope_scale,hpoly (3000*capA) 1,kappa_scales (codeWeight capA) (code_ge cap_ok)] with j hj h ht M + have hr := hj.1 + have hp := H_ge j + let a := H j; let d := height j + have hD : 1 ≤ d := hj.2.2.1 + have ha : 1 ≤ a := hp; have ha' : 0 < a := H_pos _ + constructor + · refine ⟨below_refl _,certified_start M,?_,by simp [initial],?_,?_,?_,?_⟩ + · simp only [initial,Nat.cast_zero]; linarith [show (1:ℝ) ≤ capA*H j from one_le_mul_of_one_le_of_one_le cap_ok hp] + · change 0 ≤ _; have := reserve_ge cap_ok; have := d_pos j; positivity + · simp [initial] + · simp [initial, invlen_top,F_zero] + have he := r_steps cap_ok j 0 (show ((0:ℕ):ℝ) ≤ _ by + push_cast; have := one_le_mul_of_one_le_of_one_le cap_ok hp; linarith) + dsimp [initial] + rw [gap_zero he.1 (by have := he.2; linarith)] + simp + intro c p hh + have hA := hh.effects.nonneg + have HX := r_steps cap_ok _ _ hh.under + have Hx := hh.verified + have hy := gap_lower hA HX.1 (by linarith [HX.2]) + have hnp := hh.nonneg + have hz : c.spend ≤ 2*(c.bank+1) := by linarith [hnp.1] + refine ⟨hz,?_⟩ + intro hd he + rw [breached] at hd + have HH : c.spend ≤ 2002*d := by change ¬1000*d<_ at hd; linarith + have ht' := hh.floor_ok + have hI : c.uses+1 ≤ capA*H j := by + have hk : (c.uses:ℝ) ≤ 20020000*a := by + have HL := ht'.trans HH + unfold fuel eps at HL; change (c.uses:ℝ)*(1/10000*(d/a)) ≤ _ at HL + apply le_of_mul_le_mul_left _ (show 0 < d by linarith) + field_simp at HL; nlinarith + unfold capA; nlinarith [show H j=a from rfl] + refine ⟨⟨he,c.uses,c.anchor,hh.domain,hh.effects,hh.under,?_⟩,hI,?_⟩ + · have ht : 3000*c.uses ≤ d := by + have h1 := hh.under + change _*H j^1 ≤ d at h; rw [pow_one] at h + change 3000*(c.uses:ℝ) ≤ d + linarith + change _ ≤ _+capA*d + have hL := hh.relative + unfold capA; nlinarith + intro u P hu + let v := c.advance p u + have hK := certified_add hh.effects hp he hu.positive hu.plate + have hR : Ok v p j := by + have hf : fuel j ≤ u.credit := by + apply le_trans _ hu.duration + apply mul_le_mul_of_nonneg_left _ eps_pos.le + apply le_trans _ (ht _ hu.ix_ok).2.2.1 + apply div_le_div_of_nonneg_right _ (H_pos _).le + exact le_mul_of_one_le_left (d_pos j).le (code_ge cap_ok) + have hc : 0 ≤ u.credit := hj.2.1.le.trans hf + have hJ := hu.gap_est _ HX.1 HX.2 + refine ⟨below_refl _,hK,?_,?_,?_,?_,?_,?_⟩ + · unfold v advance; dsimp; push_cast; exact hI + · dsimp only [v,advance] + have he : 0 ≤ u.bill ∧ u.bill ≤ u.credit := by unfold Update.bill; split <;> simp [hc] + constructor + exact add_nonneg hnp.1 he.1 + exact add_le_add hnp.2 he.2 + · change c.bank+u.bill ≤ _ + unfold Update.bill; split + · have hg := hu.reserves.2; linarith [hh.solvent] + simp only [add_zero, hh.solvent] + · change ((c.uses+1:ℕ):ℝ) * _ ≤ c.spend + u.credit + push_cast; linarith + · change F (u.next c.mood) ≤ _ + dsimp only [v,advance] + push_cast + have HZ := space_increment hh.effects hh.domain he + linarith [hu.cost,hh.relative] + · change gap (u.next c.mood) (track capA j (c.uses+1)) ≤ _ + rw [track_add] + change gap _ _ ≤ 402*(c.bank+u.bill)-5000*((c.spend+u.credit)-(c.bank+u.bill)) + unfold Update.bill + by_cases hh : u.regular = true + · rw [ite_eq_left hh] at * + linarith + rw [ite_eq_right hh] at * + have ht := hu.cheap (by simpa using hh) + unfold eps at ht + linarith + exact hR +end Fund + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Counts.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Counts.lean index fc165734429cea7186f7bfd8894de203f171cc10..267a5def154b37dda717d65b8ebddb1423a7962e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Counts.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Counts.lean @@ -1,236 +1,236 @@ -import OAI.Analysis.FourierLLogL.Procedure.Likelihood -/-! # Expected starts, entropy and attempt counts -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -namespace Road - -def flatFee (t : Road j M s) (f : Event j M → ℝ) := - t.expHike (fun l _ => l.fee f) - -lemma flat_finish (s : Scene M) (b : Bool) (f : Event j M → ℝ) : - flatFee (.finish s b) f = 0 := by simp [flatFee,expHike,hike,fee,Ex_const] - -lemma flat_push (v : Pace) (t : Road j M (s.pace v)) (f : Event j M → ℝ) : - flatFee (.push s v t) f = f (Event.pace s v)+t.flatFee f := by - simp_rw [flatFee,Ex_hike_push,fee_pre,expHike,Ex_add (f:=fun _ => f (Event.pace s v)),Ex_const] - -lemma flat_fork (v : Turn j s) (t : ∀ b, Road j M (v.fork.dst b)) - (h : (.fork s v t : Road j M s).Trusted J f) (g : Event j M → ℝ) : - flatFee (.fork s v t) g = v.fork.probs.Ex (fun b => g (.march s v b)+(t b).flatFee g) := by - simp_rw [flatFee,Ex_hike_fork h,fee_pre,expHike,Ex_add,Ex_const] - -lemma total_step (v : Turn j s) : - v.fork.probs.Ex (fun b => exp (-likelihood (.march s v b))) ≤ 1 := by - cases v with - | ask u => simp [Event.likelihood, Ex_const] - | move => - let Q := (step s).probs - change Q.Ex (fun b => exp (-log (2*Q b))) ≤ 1 - have HH (b : Bool) : Q b * exp (-log (2*Q b)) ≤ 1/2 := by - rcases eq_or_lt_of_le (Q.nonneg b) with H|H - · rw [← H,zero_mul]; norm_num - rw [Real.exp_neg,Real.exp_log (by positivity)] - field_simp - rfl - unfold Ex; rw [Fintype.sum_bool]; linarith [HH true,HH false] - -def beginFee : Event j M → ℝ -| .pace _ .renew => 1 -| _ => 0 - - -private def headroom (x : ℝ) := x+exp (-x)-1 -private lemma headroom_nonneg (x : ℝ) : 0 ≤ headroom x := by - unfold headroom; linarith [Real.add_one_le_exp (-x)] - -private lemma headroom_up {x : ℝ} (hx : log 2 ≤ x) : 1/10 ≤ headroom x := by - unfold headroom - let t := log 2 - have h0 : 63/100 ≤ t := by unfold t; linarith [log_two_gt_d9] - have h1 := Real.add_one_le_exp (t-x) - have he : exp (-x) = exp (t-x)/2 := by - rw [sub_eq_add_neg,exp_add]; unfold t; rw [exp_log (by norm_num)]; ring - rw [he] - change t ≤ x at hx; linarith - -private def startup (s : Scene M) := - (if s.live then (1/10:ℝ) else 0)+(if s.norm=0 then (1/10:ℝ) else 0) - -private lemma startup_ok (s : Scene M) : 0 ≤ startup s ∧ startup s ≤ 2/10 := by - unfold startup - split_ifs <;> norm_num - -lemma ticket_count (t : Road j M s) (h : t.Trusted J f) - (hm : 0 < s.mass) (x : ℝ) (hX : 0 < s.norm → log s.mass - log s.norm ≤ x) : - t.flatFee beginFee /10 ≤ t.flatFee likelihood + t.flatFee count /10 + headroom x + startup s := by - classical - induction t generalizing x with - | finish s _ => - simp_rw [flat_finish]; linarith [headroom_nonneg x,(startup_ok s).1] - | push s v t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - simp_rw [flat_push,Event.likelihood,Event.count,zero_add] - have hM := pace_mass s v - cases v with - | cross => - have HH := ih hd (hM.symm ▸ hm) 0 (by simp [Scene.pace,out]) - have he : startup (s.pace .cross) ≤ startup s := by - have Hs : s.live=true := hc.1 - simp [startup,Scene.pace,out,Hs] - split_ifs <;> norm_num - rw [headroom,neg_zero,exp_zero] at HH - have := headroom_nonneg x - change (0+_)/10 ≤ _ - linarith - | renew => - have he : (s.pace .renew).norm=s.mass := rfl - have HH := ih hd (hM.symm ▸ hm) 0 (by intro h; rw [he,hM,sub_self]) - rw [headroom,neg_zero,exp_zero] at HH - change (1+_)/10 ≤ _ - by_cases H : s.norm=0 - · have Hs : startup s = startup (s.pace .renew)+1/10 := by - unfold startup; rw [he,ite_eq_left H,ite_eq_right hm.ne'] - simp [Scene.pace, reset] - linarith [headroom_nonneg x] - have Hs : startup s = startup (s.pace .renew) := by - unfold startup; rw [he,ite_eq_right H,ite_eq_right hm.ne']; rfl - have hp := lt_of_le_of_ne ha.norm_ok (Ne.symm H) - have HH' := log_le_log (show 0 < 2 *s.norm from by positivity) hc.2.le - rw [log_mul (by norm_num) hp.ne'] at HH' - have ht := headroom_up (x:=x) (by linarith [hX hp]) - linarith - | fork s v t ih => - have ha := h.1 - have hρ := branchTrusted h - have hc := h.2.1 - have hn : 0 < s.norm := by by_contra! he; apply hc.2.2; unfold enter; linarith - let Q := v.fork.probs - let L (b : Bool) := likelihood (.march s v b) - have hx (b : Bool) (hb : 0 < Q b) : - (t b).flatFee beginFee /10 ≤ (t b).flatFee likelihood + (t b).flatFee count /10 + - headroom (x+L b) + startup (v.fork.dst b) := by - have hp := (true_mass hρ).mp hb - apply ih b (h.2.2.2 b) hp (x+L b) - have HH := price_space v b hρ hm hb - intro hy - have he : (v.fork.dst b).norm=s.norm := by - cases v with - | move => rfl - | ask u => cases b - · rfl - · exfalso; change (0:ℝ)<0 at hy; linarith - rw [he] - unfold L - have ht := fee_bounds (.march s v b) - unfold Event.memFee at HH - linarith [ht.1,ht.2.1,hX hn] - have hh : Q.Ex (fun b => headroom (x+L b)) ≤ Q.Ex L + headroom x := by - have he (b : Bool) : headroom (x+L b) = (x+L b)+(exp (-x)*exp (-L b)-1) := by - unfold headroom; rw [←exp_add]; ring_nf - simp_rw [he,Ex_add,Ex_sub,Ex_lmul,Ex_const] - have H : Q.Ex (fun b => exp (-L b)) ≤ 1 := total_step v - unfold headroom; nlinarith [exp_pos (-x)] - have HH : Q.Ex (fun b => startup (v.fork.dst b)) ≤ startup s + Q.Ex fun b => - count (.march s v b)/10 := by - conv_rhs => lhs; rw [← Ex_const Q (startup s)] - rw [← Ex_add] - apply Ex_mono; intro b - cases v with - | move => simp [startup,Turn.fork,step,smaller,count] - | ask u => - cases b <;> simp [startup,Turn.fork,toss,lose,win,hn.ne',count] - simp_rw [flat_fork _ _ h] - have ht := Q.Ex_mono_mem hx - simp_rw [Ex_add] at * - - have hZ : v.fork.probs.Ex (fun b => beginFee (.march s v b)) = 0 := by - simp only [beginFee,Ex_const] - rw [hZ] - simp_rw [div_eq_mul_inv,Ex_mul] at * - unfold L Q at * - norm_num at * - linarith - -lemma final_coarse : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → - t.flatFee likelihood ≤ 2*limitC * H j ∧ - t.flatFee count ≤ capA * H j ∧ - (s.norm=0 → t.flatFee beginFee ≤ 100*limitC*H j) := by - filter_upwards [mem_budget,cutoff_log] with j hj h M J s f t hh hM - have hp := H_pos j; have he := H_ge j - have hm : 0 < s.mass := lt_of_lt_of_le (by positivity : 0 < 1/H j) hM - have Hl := (hj M J s f t hh hm).2 - have hX : t.flatFee likelihood ≤ 2*limitC*H j := by - have hQ (x : Grid M) (hx : 0 < s.actor x) : (t.hike x).fee likelihood ≤ - 30000*H j+(t.hike x).membership := by - let l := t.hike x - obtain ⟨h1,h2,h3,h4,h5⟩ := final_sound hh hm hx - have H₃ : log l.done.mass ≤ 20000*H j := (log_le_log h3 h1.budget).trans h - have hz := log_le_log (by positivity : 0 < 1/H j) hM - rw [one_div,Real.log_inv] at hz - change l.like ≤ _ + l.membership - change l.like = _ at h4 - linarith [log_le_sub_one_of_pos hp] - unfold flatFee expHike - apply (s.actor.Ex_mono_mem hQ).trans - rw [Ex_add,Ex_const]; unfold expHike limitC at *; nlinarith - have hY : t.flatFee count ≤ capA*H j := by - have hQ (x : Grid M) (hx : 0 < s.actor x) : (t.hike x).fee count ≤ capA*H j := by - obtain ⟨h1,h2,h3,h4,h5,_⟩ := final_sound hh hm hx - linarith [h1.capital.under,Nat.cast_nonneg (α:=ℝ) s.fund.uses] - unfold flatFee expHike; apply le_trans (s.actor.Ex_mono_mem hQ); rw [Ex_const] - refine ⟨hX,hY, fun hn => ?_⟩ - have HZ := ticket_count t hh hm 0 (by simp [hn]) - rw [headroom,neg_zero,exp_zero] at HZ - have hZ := (startup_ok s).2 - unfold limitC capA at *; linarith - -lemma attempt_count : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → - t.flatFee tried ≤ 12800*capA*H j^33 := by - filter_upwards [final_coarse] with j hg M J s f t h hm - let a := 12800*H j^32 - have H (s : Scene M) (t : Road j M s) (h : t.Trusted J f) : - t.flatFee tried ≤ a*t.flatFee count := by - induction t with - | finish => simp [flat_finish] - | push s v t ih => - simp only [flat_push,tried,count,zero_add]; exact ih h.2.2.2 - | fork s v t ih => - simp_rw [flat_fork v t h, Ex_add] - rw [mul_add] - apply add_le_add ?_ - · rw [← Ex_lmul]; exact Ex_mono _ (fun b => ih b (h.2.2.2 b)) - cases v with - | move => simp [tried,count,Ex_const] - | ask u => - have ht := h.2.2.1.2 - have HX : beta u.ix/2 ≤ (toss s u).probs true := by rw [success_prob]; exact ht.bias - have hp := (main_bounds j ht.ix_ok).1 - simp [tried,count,Ex,Turn.fork] - have hh : (toss s u).probs true + (toss s u).probs false = 1 := by simpa [Fintype.sum_bool] using (toss s u).probs.total - rw [hh] - calc - 1 ≤ a*(beta u.ix/2) := by have := H_pos j; unfold a; field_simp at *; nlinarith - _ ≤ _ := by exact mul_le_mul_of_nonneg_left HX (by unfold a; positivity) - have hx := (hg M J s f t h hm).2.1 - apply (H s t h).trans - calc - _ ≤ a*(capA*Thermal.H j) := by exact mul_le_mul_of_nonneg_left hx (by unfold a; positivity) - _ = _ := by unfold a; ring - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Likelihood +/-! # Expected starts, entropy and attempt counts -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +namespace Road + +def flatFee (t : Road j M s) (f : Event j M → ℝ) := + t.expHike (fun l _ => l.fee f) + +lemma flat_finish (s : Scene M) (b : Bool) (f : Event j M → ℝ) : + flatFee (.finish s b) f = 0 := by simp [flatFee,expHike,hike,fee,Ex_const] + +lemma flat_push (v : Pace) (t : Road j M (s.pace v)) (f : Event j M → ℝ) : + flatFee (.push s v t) f = f (Event.pace s v)+t.flatFee f := by + simp_rw [flatFee,Ex_hike_push,fee_pre,expHike,Ex_add (f:=fun _ => f (Event.pace s v)),Ex_const] + +lemma flat_fork (v : Turn j s) (t : ∀ b, Road j M (v.fork.dst b)) + (h : (.fork s v t : Road j M s).Trusted J f) (g : Event j M → ℝ) : + flatFee (.fork s v t) g = v.fork.probs.Ex (fun b => g (.march s v b)+(t b).flatFee g) := by + simp_rw [flatFee,Ex_hike_fork h,fee_pre,expHike,Ex_add,Ex_const] + +lemma total_step (v : Turn j s) : + v.fork.probs.Ex (fun b => exp (-likelihood (.march s v b))) ≤ 1 := by + cases v with + | ask u => simp [Event.likelihood, Ex_const] + | move => + let Q := (step s).probs + change Q.Ex (fun b => exp (-log (2*Q b))) ≤ 1 + have HH (b : Bool) : Q b * exp (-log (2*Q b)) ≤ 1/2 := by + rcases eq_or_lt_of_le (Q.nonneg b) with H|H + · rw [← H,zero_mul]; norm_num + rw [Real.exp_neg,Real.exp_log (by positivity)] + field_simp + rfl + unfold Ex; rw [Fintype.sum_bool]; linarith [HH true,HH false] + +def beginFee : Event j M → ℝ +| .pace _ .renew => 1 +| _ => 0 + + +private def headroom (x : ℝ) := x+exp (-x)-1 +private lemma headroom_nonneg (x : ℝ) : 0 ≤ headroom x := by + unfold headroom; linarith [Real.add_one_le_exp (-x)] + +private lemma headroom_up {x : ℝ} (hx : log 2 ≤ x) : 1/10 ≤ headroom x := by + unfold headroom + let t := log 2 + have h0 : 63/100 ≤ t := by unfold t; linarith [log_two_gt_d9] + have h1 := Real.add_one_le_exp (t-x) + have he : exp (-x) = exp (t-x)/2 := by + rw [sub_eq_add_neg,exp_add]; unfold t; rw [exp_log (by norm_num)]; ring + rw [he] + change t ≤ x at hx; linarith + +private def startup (s : Scene M) := + (if s.live then (1/10:ℝ) else 0)+(if s.norm=0 then (1/10:ℝ) else 0) + +private lemma startup_ok (s : Scene M) : 0 ≤ startup s ∧ startup s ≤ 2/10 := by + unfold startup + split_ifs <;> norm_num + +lemma ticket_count (t : Road j M s) (h : t.Trusted J f) + (hm : 0 < s.mass) (x : ℝ) (hX : 0 < s.norm → log s.mass - log s.norm ≤ x) : + t.flatFee beginFee /10 ≤ t.flatFee likelihood + t.flatFee count /10 + headroom x + startup s := by + classical + induction t generalizing x with + | finish s _ => + simp_rw [flat_finish]; linarith [headroom_nonneg x,(startup_ok s).1] + | push s v t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + simp_rw [flat_push,Event.likelihood,Event.count,zero_add] + have hM := pace_mass s v + cases v with + | cross => + have HH := ih hd (hM.symm ▸ hm) 0 (by simp [Scene.pace,out]) + have he : startup (s.pace .cross) ≤ startup s := by + have Hs : s.live=true := hc.1 + simp [startup,Scene.pace,out,Hs] + split_ifs <;> norm_num + rw [headroom,neg_zero,exp_zero] at HH + have := headroom_nonneg x + change (0+_)/10 ≤ _ + linarith + | renew => + have he : (s.pace .renew).norm=s.mass := rfl + have HH := ih hd (hM.symm ▸ hm) 0 (by intro h; rw [he,hM,sub_self]) + rw [headroom,neg_zero,exp_zero] at HH + change (1+_)/10 ≤ _ + by_cases H : s.norm=0 + · have Hs : startup s = startup (s.pace .renew)+1/10 := by + unfold startup; rw [he,ite_eq_left H,ite_eq_right hm.ne'] + simp [Scene.pace, reset] + linarith [headroom_nonneg x] + have Hs : startup s = startup (s.pace .renew) := by + unfold startup; rw [he,ite_eq_right H,ite_eq_right hm.ne']; rfl + have hp := lt_of_le_of_ne ha.norm_ok (Ne.symm H) + have HH' := log_le_log (show 0 < 2 *s.norm from by positivity) hc.2.le + rw [log_mul (by norm_num) hp.ne'] at HH' + have ht := headroom_up (x:=x) (by linarith [hX hp]) + linarith + | fork s v t ih => + have ha := h.1 + have hρ := branchTrusted h + have hc := h.2.1 + have hn : 0 < s.norm := by by_contra! he; apply hc.2.2; unfold enter; linarith + let Q := v.fork.probs + let L (b : Bool) := likelihood (.march s v b) + have hx (b : Bool) (hb : 0 < Q b) : + (t b).flatFee beginFee /10 ≤ (t b).flatFee likelihood + (t b).flatFee count /10 + + headroom (x+L b) + startup (v.fork.dst b) := by + have hp := (true_mass hρ).mp hb + apply ih b (h.2.2.2 b) hp (x+L b) + have HH := price_space v b hρ hm hb + intro hy + have he : (v.fork.dst b).norm=s.norm := by + cases v with + | move => rfl + | ask u => cases b + · rfl + · exfalso; change (0:ℝ)<0 at hy; linarith + rw [he] + unfold L + have ht := fee_bounds (.march s v b) + unfold Event.memFee at HH + linarith [ht.1,ht.2.1,hX hn] + have hh : Q.Ex (fun b => headroom (x+L b)) ≤ Q.Ex L + headroom x := by + have he (b : Bool) : headroom (x+L b) = (x+L b)+(exp (-x)*exp (-L b)-1) := by + unfold headroom; rw [←exp_add]; ring_nf + simp_rw [he,Ex_add,Ex_sub,Ex_lmul,Ex_const] + have H : Q.Ex (fun b => exp (-L b)) ≤ 1 := total_step v + unfold headroom; nlinarith [exp_pos (-x)] + have HH : Q.Ex (fun b => startup (v.fork.dst b)) ≤ startup s + Q.Ex fun b => + count (.march s v b)/10 := by + conv_rhs => lhs; rw [← Ex_const Q (startup s)] + rw [← Ex_add] + apply Ex_mono; intro b + cases v with + | move => simp [startup,Turn.fork,step,smaller,count] + | ask u => + cases b <;> simp [startup,Turn.fork,toss,lose,win,hn.ne',count] + simp_rw [flat_fork _ _ h] + have ht := Q.Ex_mono_mem hx + simp_rw [Ex_add] at * + + have hZ : v.fork.probs.Ex (fun b => beginFee (.march s v b)) = 0 := by + simp only [beginFee,Ex_const] + rw [hZ] + simp_rw [div_eq_mul_inv,Ex_mul] at * + unfold L Q at * + norm_num at * + linarith + +lemma final_coarse : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → + t.flatFee likelihood ≤ 2*limitC * H j ∧ + t.flatFee count ≤ capA * H j ∧ + (s.norm=0 → t.flatFee beginFee ≤ 100*limitC*H j) := by + filter_upwards [mem_budget,cutoff_log] with j hj h M J s f t hh hM + have hp := H_pos j; have he := H_ge j + have hm : 0 < s.mass := lt_of_lt_of_le (by positivity : 0 < 1/H j) hM + have Hl := (hj M J s f t hh hm).2 + have hX : t.flatFee likelihood ≤ 2*limitC*H j := by + have hQ (x : Grid M) (hx : 0 < s.actor x) : (t.hike x).fee likelihood ≤ + 30000*H j+(t.hike x).membership := by + let l := t.hike x + obtain ⟨h1,h2,h3,h4,h5⟩ := final_sound hh hm hx + have H₃ : log l.done.mass ≤ 20000*H j := (log_le_log h3 h1.budget).trans h + have hz := log_le_log (by positivity : 0 < 1/H j) hM + rw [one_div,Real.log_inv] at hz + change l.like ≤ _ + l.membership + change l.like = _ at h4 + linarith [log_le_sub_one_of_pos hp] + unfold flatFee expHike + apply (s.actor.Ex_mono_mem hQ).trans + rw [Ex_add,Ex_const]; unfold expHike limitC at *; nlinarith + have hY : t.flatFee count ≤ capA*H j := by + have hQ (x : Grid M) (hx : 0 < s.actor x) : (t.hike x).fee count ≤ capA*H j := by + obtain ⟨h1,h2,h3,h4,h5,_⟩ := final_sound hh hm hx + linarith [h1.capital.under,Nat.cast_nonneg (α:=ℝ) s.fund.uses] + unfold flatFee expHike; apply le_trans (s.actor.Ex_mono_mem hQ); rw [Ex_const] + refine ⟨hX,hY, fun hn => ?_⟩ + have HZ := ticket_count t hh hm 0 (by simp [hn]) + rw [headroom,neg_zero,exp_zero] at HZ + have hZ := (startup_ok s).2 + unfold limitC capA at *; linarith + +lemma attempt_count : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → + t.flatFee tried ≤ 12800*capA*H j^33 := by + filter_upwards [final_coarse] with j hg M J s f t h hm + let a := 12800*H j^32 + have H (s : Scene M) (t : Road j M s) (h : t.Trusted J f) : + t.flatFee tried ≤ a*t.flatFee count := by + induction t with + | finish => simp [flat_finish] + | push s v t ih => + simp only [flat_push,tried,count,zero_add]; exact ih h.2.2.2 + | fork s v t ih => + simp_rw [flat_fork v t h, Ex_add] + rw [mul_add] + apply add_le_add ?_ + · rw [← Ex_lmul]; exact Ex_mono _ (fun b => ih b (h.2.2.2 b)) + cases v with + | move => simp [tried,count,Ex_const] + | ask u => + have ht := h.2.2.1.2 + have HX : beta u.ix/2 ≤ (toss s u).probs true := by rw [success_prob]; exact ht.bias + have hp := (main_bounds j ht.ix_ok).1 + simp [tried,count,Ex,Turn.fork] + have hh : (toss s u).probs true + (toss s u).probs false = 1 := by simpa [Fintype.sum_bool] using (toss s u).probs.total + rw [hh] + calc + 1 ≤ a*(beta u.ix/2) := by have := H_pos j; unfold a; field_simp at *; nlinarith + _ ≤ _ := by exact mul_le_mul_of_nonneg_left HX (by unfold a; positivity) + have hx := (hg M J s f t h hm).2.1 + apply (H s t h).trans + calc + _ ≤ a*(capA*Thermal.H j) := by exact mul_le_mul_of_nonneg_left hx (by unfold a; positivity) + _ = _ := by unfold a; ring + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Deficit.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Deficit.lean index 9c6dcf3698a8d0aec3c2d19f0b636e2d7c8c5b51..d35c1b04900634a35b20a559ab09eeef04406d38 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Deficit.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Deficit.lean @@ -1,175 +1,175 @@ -import OAI.Analysis.FourierLLogL.Procedure.Fork -import OAI.Analysis.FourierLLogL.Finite.Signal - -/-! # Conditional entropy on a fork with a fixed input signal -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -variable {α : Type} [Fintype α] -open scoped Classical - -/-- Remaining number of bits in nats -/ -def levels (s : Scene M) := ((M-s.here.lev:ℕ):ℝ)*log 2 -def deficit (s : Scene M) (k : Grid M → Law α) := - levels s - s.actor.privateH k -def sideH (v : Fork M) (k : Grid M → Law α) := - (v.src.actor.withNext k).Hcond (fun i => v.bit i.1) Prod.snd - - -lemma cap_entropy (s : Scene M) (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) : - s.actor.entropy ≤ levels s := by - apply le_trans (entropy_le_log_size _ (s.here.group M) ?_) - · rw [group_card hp.1 hp.2,wid]; push_cast; rw [log_pow] - exact le_rfl - intro i hi - exact (mem_group _ _).mpr (piece_support hp (actor_def hp hh hm) i hi) - -lemma deficit_nonneg (s : Scene M) (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) - (k : Grid M → Law α) : 0 ≤ deficit s k := - sub_nonneg.mpr ((privateH_le _ _).trans (cap_entropy _ hp hh hm)) - -lemma levels_child (s : Scene M) (hb : s.here.lev < M) (b : Bool) : - levels (s.smaller b) = levels s - log 2 := by - unfold levels - have He : M - s.here.lev = M - (s.smaller b).here.lev +1 := by - have H : (s.smaller b).here.lev = s.here.lev+1 := by cases b <;> rfl - omega - rw [He]; push_cast; ring - -lemma conditioned_balance (k : Grid M → Law α) (v : Fork M) (h : v.TrueLaw) : - v.probs.Ex (fun b => (v.dst b).actor.privateH k) + sideH v k = - v.src.actor.privateH k := by - have hi (b) (hi : 0 < v.probs b) : - v.src.actor.after v.bit b=(v.dst b).actor := by - apply Law.ext; intro x - apply mul_left_cancel₀ hi.ne' - rw [local_law h] - exact send_mul_after _ _ _ _ - have He := det_channel v.src.actor v.bit k - rw [← He] - unfold sideH - congr 1 - exact Ex_congr_mem _ (fun x hx => by rw [hi x hx]) - -lemma old_entropy_cost (v : Fork M) (k : Grid M → Law α) : - sideH v k ≤ v.probs.entropy := by - let P := v.src.actor.withNext k; let B : Grid M × α → Bool := fun x => v.bit x.1 - have he : P.send B = v.probs := by - have HH := send_send P Prod.fst v.bit - rw [send_fst_withNext v.src.actor k] at HH - exact HH.symm - apply le_trans (Hcond_le_H P B Prod.snd); rw [Law.H,he] - -/-- Observation mass relative to s.norm. -/ -def hist (s : Scene M) (k : Grid M → Law α) (i : α) := - ∑ x : Grid M, wt s.here s.wage s.norm x*k x i -def diff (s : Scene M) (k : Grid M → Law α) (i : α) := - (hist (s.smaller false) k i - hist (s.smaller true) k i)/2 - -def fisher (s : Scene M) (k : Grid M → Law α) := - ∑ i : α, (diff s k i)^2 / hist s k i - -lemma hist_special (s : Scene M) (i : Grid M) (A : Form M) : - hist s (channel A) i = Mz s.here s.wage s.norm A i := rfl - -lemma hist_source (k : Grid M → Law α) (s : Scene M) (hp : s.here.ok M) - (hh : 0 ≤ s.wage) (i : α) : - hist s k i = (s.mass / s.norm) * s.actor.through k i := by - rw [through_apply] - unfold hist wt Ex; rw [Finset.mul_sum] - exact Finset.sum_congr rfl fun x hx => by rw [← mass_actor hp hh]; ring - -lemma bit_cost (Q : Law Bool) : - (Q false-Q true)^2 ≤ 4*(log 2-Q.entropy) := by - have H := tv_sq_le_kl (P:=Q) (Q:=uniform Bool) - (fun i hi => by rw [uniform_apply]; positivity) - have Ht : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total - unfold tv kl crossE at H - simp_rw [Fintype.sum_bool, uniform_apply,Fintype.card_bool, Nat.cast_ofNat, Ex_const, - Real.log_inv] at H - rw [show Q false-2⁻¹= -(Q true-2⁻¹) by linarith,abs_neg] at H - rw [show Q false = 1-Q true by linarith] - nlinarith [sq_abs (Q true-2⁻¹)] - -lemma bit_def (s : Scene M) (k : Grid M → Law α) (hp : s.here.valid) - (he : s.here.lev < M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (hn : 0 < s.norm) : - s.norm/s.mass * fisher s k ≤ 4*(log 2-sideH (step s) k) := by - let v := step s - have HV : v.TrueLaw := step_true hp he hh hm - let R := s.actor.withNext k - let P := R.send Prod.snd - let z := fun x : Grid M × α => v.bit x.1 - let Q (i) := (R.after Prod.snd i).send z - have H0 b i : - v.probs b * ((s.smaller b).actor.through k) i = R.send (fun x => (z x,x.2)) (b,i) := by - let C := fun x : Grid M × α => (z x,x.2) - rw [through_apply, Ex,Finset.mul_sum] - simp_rw [← mul_assoc] - rw [send_apply,prob_eq,Fintype.sum_prod_type] - refine Finset.sum_congr rfl fun x hx => ?_ - change (v.probs b*(v.dst b).actor x)*k x i = ∑ y, if C (x,y)=(b,i) then R (x,y) else 0 - rw [local_law HV] - by_cases h : v.bit x=b - · rw [ite_eq_left h] - simp [C,z,h,R,withNext_apply] - - exact Or.inl rfl - rw [ite_eq_right h]; simp [C,z,h] - have HH (b i) : hist (s.smaller b) k i = - (s.mass/s.norm)*(2*P i*Q i b) := by - rw [hist_source k (s.smaller b) (child_ok _ hp he b) hh,step_mass HV] - have HC := H0 b i - rw [joint_after R z Prod.snd] at HC - change _ = P i*Q i b at HC - change 2*v.probs b*s.mass/s.norm*((s.smaller b).actor.through k i) = _ - rw [mul_assoc (a:=2) (b:=P i),← HC]; ring - have hP (i) : - hist s k i = s.mass/s.norm*P i := by rw [hist_source _ _ ⟨hp,he.le⟩ hh]; unfold P R; rw [send_snd_withNext] - have ht i : s.norm/s.mass*((diff s k i)^2 / hist s k i) = P i*(Q i false-Q i true)^2 := by - unfold diff; rw [HH,HH,hP] - rcases eq_or_lt_of_le (P.nonneg i) with He|He - · simp [← He] - field_simp - let F (i : α) := log 2-(Q i).entropy - calc - _ = P.Ex fun i => (Q i false-Q i true)^2 := by rw [fisher,Finset.mul_sum]; simp_rw [ht]; rfl - _ ≤ P.Ex fun i => 4*F i := Ex_mono _ (fun i => bit_cost _) - _ = _ := by - unfold F sideH; rw [Ex_lmul, Ex_sub (f:=fun i => log 2),Ex_const,Hcond_after] - rfl - -lemma space_deficit (s : Scene M) (k : Grid M → Law α) (hp : s.here.valid) - (he : s.here.lev < M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (hn : 0 < s.norm) : - (step s).probs.Ex (fun b => deficit (s.smaller b) k) + - (1/4:ℝ)*(s.norm/s.mass)*fisher s k ≤ deficit s k := by - let v := step s - have H := conditioned_balance k v (step_true hp he hh hm) - have HH := bit_def s k hp he hh hm hn - unfold deficit - simp_rw [levels_child s he, Ex_sub (f:=fun _ => levels s - log 2),Ex_const] - change (step s).probs.Ex (fun b => (s.smaller b).actor.privateH k)+sideH (step s) k=s.actor.privateH k at H - dsimp only [step] at * - rw [mul_assoc] - linarith - -lemma member_deficit (s : Scene M) (k : Grid M → Law α) (u : Update j M s.here) - (H : (toss s u).TrueLaw) : - let v := toss s u - v.probs.Ex (fun b => deficit (v.dst b) k) ≤ deficit s k + v.probs.entropy := by - intro v - have he := conditioned_balance k v H - have hV : ∀ b, levels (v.dst b)=levels s := by intro b; cases b <;> rfl - unfold deficit - simp_rw [hV,Ex_sub (f:=fun _ => levels s),Ex_const] - change _+_ = s.actor.privateH k at he - linarith [old_entropy_cost v k] - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Fork +import OAI.Analysis.FourierLLogL.Finite.Signal + +/-! # Conditional entropy on a fork with a fixed input signal -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +variable {α : Type} [Fintype α] +open scoped Classical + +/-- Remaining number of bits in nats -/ +def levels (s : Scene M) := ((M-s.here.lev:ℕ):ℝ)*log 2 +def deficit (s : Scene M) (k : Grid M → Law α) := + levels s - s.actor.privateH k +def sideH (v : Fork M) (k : Grid M → Law α) := + (v.src.actor.withNext k).Hcond (fun i => v.bit i.1) Prod.snd + + +lemma cap_entropy (s : Scene M) (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) : + s.actor.entropy ≤ levels s := by + apply le_trans (entropy_le_log_size _ (s.here.group M) ?_) + · rw [group_card hp.1 hp.2,wid]; push_cast; rw [log_pow] + exact le_rfl + intro i hi + exact (mem_group _ _).mpr (piece_support hp (actor_def hp hh hm) i hi) + +lemma deficit_nonneg (s : Scene M) (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) + (k : Grid M → Law α) : 0 ≤ deficit s k := + sub_nonneg.mpr ((privateH_le _ _).trans (cap_entropy _ hp hh hm)) + +lemma levels_child (s : Scene M) (hb : s.here.lev < M) (b : Bool) : + levels (s.smaller b) = levels s - log 2 := by + unfold levels + have He : M - s.here.lev = M - (s.smaller b).here.lev +1 := by + have H : (s.smaller b).here.lev = s.here.lev+1 := by cases b <;> rfl + omega + rw [He]; push_cast; ring + +lemma conditioned_balance (k : Grid M → Law α) (v : Fork M) (h : v.TrueLaw) : + v.probs.Ex (fun b => (v.dst b).actor.privateH k) + sideH v k = + v.src.actor.privateH k := by + have hi (b) (hi : 0 < v.probs b) : + v.src.actor.after v.bit b=(v.dst b).actor := by + apply Law.ext; intro x + apply mul_left_cancel₀ hi.ne' + rw [local_law h] + exact send_mul_after _ _ _ _ + have He := det_channel v.src.actor v.bit k + rw [← He] + unfold sideH + congr 1 + exact Ex_congr_mem _ (fun x hx => by rw [hi x hx]) + +lemma old_entropy_cost (v : Fork M) (k : Grid M → Law α) : + sideH v k ≤ v.probs.entropy := by + let P := v.src.actor.withNext k; let B : Grid M × α → Bool := fun x => v.bit x.1 + have he : P.send B = v.probs := by + have HH := send_send P Prod.fst v.bit + rw [send_fst_withNext v.src.actor k] at HH + exact HH.symm + apply le_trans (Hcond_le_H P B Prod.snd); rw [Law.H,he] + +/-- Observation mass relative to s.norm. -/ +def hist (s : Scene M) (k : Grid M → Law α) (i : α) := + ∑ x : Grid M, wt s.here s.wage s.norm x*k x i +def diff (s : Scene M) (k : Grid M → Law α) (i : α) := + (hist (s.smaller false) k i - hist (s.smaller true) k i)/2 + +def fisher (s : Scene M) (k : Grid M → Law α) := + ∑ i : α, (diff s k i)^2 / hist s k i + +lemma hist_special (s : Scene M) (i : Grid M) (A : Form M) : + hist s (channel A) i = Mz s.here s.wage s.norm A i := rfl + +lemma hist_source (k : Grid M → Law α) (s : Scene M) (hp : s.here.ok M) + (hh : 0 ≤ s.wage) (i : α) : + hist s k i = (s.mass / s.norm) * s.actor.through k i := by + rw [through_apply] + unfold hist wt Ex; rw [Finset.mul_sum] + exact Finset.sum_congr rfl fun x hx => by rw [← mass_actor hp hh]; ring + +lemma bit_cost (Q : Law Bool) : + (Q false-Q true)^2 ≤ 4*(log 2-Q.entropy) := by + have H := tv_sq_le_kl (P:=Q) (Q:=uniform Bool) + (fun i hi => by rw [uniform_apply]; positivity) + have Ht : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total + unfold tv kl crossE at H + simp_rw [Fintype.sum_bool, uniform_apply,Fintype.card_bool, Nat.cast_ofNat, Ex_const, + Real.log_inv] at H + rw [show Q false-2⁻¹= -(Q true-2⁻¹) by linarith,abs_neg] at H + rw [show Q false = 1-Q true by linarith] + nlinarith [sq_abs (Q true-2⁻¹)] + +lemma bit_def (s : Scene M) (k : Grid M → Law α) (hp : s.here.valid) + (he : s.here.lev < M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (hn : 0 < s.norm) : + s.norm/s.mass * fisher s k ≤ 4*(log 2-sideH (step s) k) := by + let v := step s + have HV : v.TrueLaw := step_true hp he hh hm + let R := s.actor.withNext k + let P := R.send Prod.snd + let z := fun x : Grid M × α => v.bit x.1 + let Q (i) := (R.after Prod.snd i).send z + have H0 b i : + v.probs b * ((s.smaller b).actor.through k) i = R.send (fun x => (z x,x.2)) (b,i) := by + let C := fun x : Grid M × α => (z x,x.2) + rw [through_apply, Ex,Finset.mul_sum] + simp_rw [← mul_assoc] + rw [send_apply,prob_eq,Fintype.sum_prod_type] + refine Finset.sum_congr rfl fun x hx => ?_ + change (v.probs b*(v.dst b).actor x)*k x i = ∑ y, if C (x,y)=(b,i) then R (x,y) else 0 + rw [local_law HV] + by_cases h : v.bit x=b + · rw [ite_eq_left h] + simp [C,z,h,R,withNext_apply] + + exact Or.inl rfl + rw [ite_eq_right h]; simp [C,z,h] + have HH (b i) : hist (s.smaller b) k i = + (s.mass/s.norm)*(2*P i*Q i b) := by + rw [hist_source k (s.smaller b) (child_ok _ hp he b) hh,step_mass HV] + have HC := H0 b i + rw [joint_after R z Prod.snd] at HC + change _ = P i*Q i b at HC + change 2*v.probs b*s.mass/s.norm*((s.smaller b).actor.through k i) = _ + rw [mul_assoc (a:=2) (b:=P i),← HC]; ring + have hP (i) : + hist s k i = s.mass/s.norm*P i := by rw [hist_source _ _ ⟨hp,he.le⟩ hh]; unfold P R; rw [send_snd_withNext] + have ht i : s.norm/s.mass*((diff s k i)^2 / hist s k i) = P i*(Q i false-Q i true)^2 := by + unfold diff; rw [HH,HH,hP] + rcases eq_or_lt_of_le (P.nonneg i) with He|He + · simp [← He] + field_simp + let F (i : α) := log 2-(Q i).entropy + calc + _ = P.Ex fun i => (Q i false-Q i true)^2 := by rw [fisher,Finset.mul_sum]; simp_rw [ht]; rfl + _ ≤ P.Ex fun i => 4*F i := Ex_mono _ (fun i => bit_cost _) + _ = _ := by + unfold F sideH; rw [Ex_lmul, Ex_sub (f:=fun i => log 2),Ex_const,Hcond_after] + rfl + +lemma space_deficit (s : Scene M) (k : Grid M → Law α) (hp : s.here.valid) + (he : s.here.lev < M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) (hn : 0 < s.norm) : + (step s).probs.Ex (fun b => deficit (s.smaller b) k) + + (1/4:ℝ)*(s.norm/s.mass)*fisher s k ≤ deficit s k := by + let v := step s + have H := conditioned_balance k v (step_true hp he hh hm) + have HH := bit_def s k hp he hh hm hn + unfold deficit + simp_rw [levels_child s he, Ex_sub (f:=fun _ => levels s - log 2),Ex_const] + change (step s).probs.Ex (fun b => (s.smaller b).actor.privateH k)+sideH (step s) k=s.actor.privateH k at H + dsimp only [step] at * + rw [mul_assoc] + linarith + +lemma member_deficit (s : Scene M) (k : Grid M → Law α) (u : Update j M s.here) + (H : (toss s u).TrueLaw) : + let v := toss s u + v.probs.Ex (fun b => deficit (v.dst b) k) ≤ deficit s k + v.probs.entropy := by + intro v + have he := conditioned_balance k v H + have hV : ∀ b, levels (v.dst b)=levels s := by intro b; cases b <;> rfl + unfold deficit + simp_rw [hV,Ex_sub (f:=fun _ => levels s),Ex_const] + change _+_ = s.actor.privateH k at he + linarith [old_entropy_cost v k] + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Fisher.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Fisher.lean index 36b6954cb757468799ce4b82f2bc55f799e0efd7..703eba4c296e7e3e28aaf6f0990c3ffac4f3d7c0 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Fisher.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Fisher.lean @@ -1,180 +1,180 @@ -import OAI.Analysis.FourierLLogL.Procedure.Addition -import OAI.Analysis.FourierLLogL.Procedure.Counts - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal - -variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -/-- locally charge entropy to height. useful for static. -/ -lemma local_deficit_dry (s : Scene M) {α : Type} [Fintype α] (P : Law α) - (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) - (d : ℝ) (h : ∀ x, s.wage x ≤ exp d) : - deficit s (fun _ => P) ≤ d-log s.mass := by - rw [deficit,privateH_const] - let q := s.actor - suffices H : levels s + q.Ex (fun x => log (q x)) ≤ d-log s.mass by - change levels s - entropy q ≤ _ - unfold entropy crossE; rw [Ex_neg]; linarith - rcases s with ⟨p,f,c,e,n⟩ - let s : Scene M := ⟨p,f,c,e,n⟩ - change levels s + _ ≤ _ - conv_lhs => lhs; rw [← Ex_const q (levels s)] - rw [← Ex_add] - refine le_trans (b:=q.Ex (fun _ => d-log s.mass)) (Ex_mono_mem _ fun x hx => ?_) (by rw [Ex_const]) - have hi := actor_def (s:=s) hp hh hm - have ht : x ∈ p.block M := piece_support hp hi x hx - have H (x' : Grid M) (hx' : x'∈p.block M) : - log (p.dice M x') = -levels s := by - have H1 : p.lev ≤ M := hp.2 - rw [dice_apply hp,ite_eq_left hx', - log_mul (side_pos _).ne' (inv_pos.mpr p.len_pos).ne',levels,len] - simp_rw [log_inv,side_pow_two, Real.log_inv,log_pow] - dsimp [s]; rw [Nat.cast_sub H1]; ring - have he : q x ≤ p.dice M x * exp d / s.mass := by - rw [show q x = _ from hi x] - unfold wt - exact div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left (h _) ((p.dice M).nonneg _)) hm.le - have hi' : 0 < p.dice M x := (dice_positive hp).mpr ht - have hc' := log_le_log hx he - rw [log_div (mul_pos hi' (exp_pos _)).ne' hm.ne',log_mul hi'.ne' (exp_pos _).ne', - log_exp,H _ ht] at hc' - change levels s+log (q x) ≤ _ - linarith - -lemma no_label (x : Grid M) : channel (0:Form M) x = uniform _ := by - apply Law.ext; intro i - rw [ch_K,K_eq,F_zero,form_zero,sub_zero,exp_zero,uniform_side,mul_one] - -def givenD (s : Scene M) := deficit s (channel s.fund.mood) - -def massInfo (s : Scene M) := - 1/4*(s.norm/s.mass)*fisher s (channel s.fund.mood) - -namespace Event -def infoFee (e : Event j M) := match e with - | .march s .move _ => massInfo s - | _ => 0 -end Event - -namespace Road - -lemma one_split_price : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ v : Turn j s, - ∀ t : (b : Bool) → Road j M (v.fork.dst b), Trusted J f (.fork s v t) → - - v.fork.probs.Ex (fun b => - givenD (v.fork.dst b) - 500*(v.fork.dst b).fund.spend + - infoFee (.march s v b) - memFee (.march s v b)) ≤ - givenD s - 500*s.fund.spend := by - filter_upwards [priced_change] with j hj M J s f v t h - have hb := branchTrusted h - obtain ⟨ha,hh,he,h⟩ := h - have hL := hh.1.2.2 - let k := channel s.fund.mood - cases v with - | move => - have hx : s.here.lev < M := (rootSafe (h false)).within.2 - have hZ := space_deficit s k ha.within.1 hx ha.positive hL (by - by_contra! hv; exact hh.2.2 (show enter s from by unfold enter; linarith)) - change (step s).probs.Ex (fun b => deficit (s.smaller b) k - - 500*s.fund.spend + _ - (0+0)) ≤ _ - simp only [add_zero,sub_zero,infoFee] - rw [Ex_add,Ex_sub,Ex_const,Ex_const] - unfold massInfo givenD k at *; linarith - | ask u => - let v := toss s u - let d (b : Bool) := deficit (v.dst b) k - have hX (b : Bool) (hv : 0 < v.probs b) : - givenD (v.dst b) - 500*(v.dst b).fund.spend ≤ d b - 500*s.fund.spend := by - cases b with - | false => exact le_rfl - | true => - have hh := hj M J s f u ha (rootSafe (h true)) ((true_mass hb).mp hv) he.2 - change deficit (s.win u) (channel (u.next s.fund.mood)) -500*(s.fund.spend+u.credit) ≤ - deficit (s.win u) (channel s.fund.mood) - 500*s.fund.spend - linarith - let Q := v.probs - have ht : Q.Ex (fun b => memFee (.march s (.ask u) b)) = Q.entropy := by - unfold entropy crossE; congr 1 - funext b; cases b <;> simp [memFee,failFee,winFee,surprise] <;> rfl - have hv := member_deficit s k u hb - change Q.Ex (fun b => givenD (v.dst b) - 500*(v.dst b).fund.spend + infoFee (.march s (.ask u) b) - memFee (.march s (.ask u) b)) ≤ _ - rw [Ex_sub,Ex_add,ht] - simp only [infoFee,Ex_const,add_zero] - have HE := Q.Ex_mono_mem hX - rw [Ex_sub (g:=fun _ => _)] at HE - unfold Q at *; change _ ≤ deficit s k - _ - change v.probs.Ex d ≤ _ at hv - rw [Ex_const] at HE; change v.probs.Ex d ≤ deficit s k + v.probs.entropy at hv - linarith - -lemma fisher_bound : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, - (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Road j M s, Trusted J f t → 1/Thermal.H j ≤ s.mass → - t.flatFee infoFee ≤ limitC* height j := by - filter_upwards [one_split_price,Fund.fund_est,mem_budget,hpoly limitC 1,Fund.envelope_scale] - with j he hg hs hC ha M J f hF s t ht hm - let a := Thermal.H j; let d := height j; let V := (10:ℝ)^19*d - have HI (s : Scene M) (t : Road j M s) (h : t.Trusted J f) (hM : 0 < s.mass) : - t.flatFee infoFee - t.flatFee memFee ≤ givenD s - 500*s.fund.spend + 500*V := by - induction t with - | finish s b => - have hh := h.1 - have hf := ((hg M).2 _ _ hh.capital).1 - simp_rw [flat_finish] - have h1 : 0 ≤ givenD s := deficit_nonneg _ hh.within hh.positive hM _ - have h2 := hh.capital.solvent - unfold reserve capA at h2 - unfold V d - have he : 1 ≤ height j := ha.2.2.1 - linarith - | push s v t ih => - have hi := ih h.2.2.2 (by rw [pace_mass]; exact hM) - rw [flat_push,flat_push] - simp only [infoFee,memFee, failFee,winFee,zero_add] - cases v <;> exact hi - | fork s v t ih => - let Q := v.fork.probs - let F := fun b => givenD (v.fork.dst b)-500*(v.fork.dst b).fund.spend - have hX (b : Bool) (hb : 0 < Q b) := ih b (h.2.2.2 b) ((true_mass (branchTrusted h)).mp hb) - have hi := Q.Ex_mono_mem hX - have hv := he M J s f v t h - simp_rw [flat_fork _ _ h, Ex_add] - change Q.Ex (fun b => F b + _ - _) ≤ _ at hv - change Q.Ex _ ≤ Q.Ex (fun b => F b + _) at hi - rw [Ex_sub _,Ex_add _ (g:=fun _ => _)] at hi - rw [Ex_const] at hi - rw [Ex_sub,Ex_add] at hv - unfold Q at *; linarith - have hp : 0 < a := H_pos _ - have hC' : limitC*a ≤ d := by simpa only [pow_one] using hC - have hm' : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hm - have hf := rootSafe ht - have Ht : givenD s ≤ d+a := by - change deficit s (channel 0) ≤ d+a - rw [funext no_label] - apply le_trans (local_deficit_dry s (uniform (Grid M)) hf.within hf.positive hm' d hF) - have he := log_le_log (by positivity : 0<1/a) hm - rw [one_div,log_inv] at he - linarith [log_le_sub_one_of_pos hp] - have HZ := HI _ _ ht hm' - have hx := (hs M J s f t ht hm').2 - change t.flatFee memFee ≤ limitC*a at hx - change _ ≤ _-500*0+_ at HZ - change _ ≤ limitC*d - unfold limitC V at * - nlinarith - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Addition +import OAI.Analysis.FourierLLogL.Procedure.Counts + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal + +variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +/-- locally charge entropy to height. useful for static. -/ +lemma local_deficit_dry (s : Scene M) {α : Type} [Fintype α] (P : Law α) + (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) + (d : ℝ) (h : ∀ x, s.wage x ≤ exp d) : + deficit s (fun _ => P) ≤ d-log s.mass := by + rw [deficit,privateH_const] + let q := s.actor + suffices H : levels s + q.Ex (fun x => log (q x)) ≤ d-log s.mass by + change levels s - entropy q ≤ _ + unfold entropy crossE; rw [Ex_neg]; linarith + rcases s with ⟨p,f,c,e,n⟩ + let s : Scene M := ⟨p,f,c,e,n⟩ + change levels s + _ ≤ _ + conv_lhs => lhs; rw [← Ex_const q (levels s)] + rw [← Ex_add] + refine le_trans (b:=q.Ex (fun _ => d-log s.mass)) (Ex_mono_mem _ fun x hx => ?_) (by rw [Ex_const]) + have hi := actor_def (s:=s) hp hh hm + have ht : x ∈ p.block M := piece_support hp hi x hx + have H (x' : Grid M) (hx' : x'∈p.block M) : + log (p.dice M x') = -levels s := by + have H1 : p.lev ≤ M := hp.2 + rw [dice_apply hp,ite_eq_left hx', + log_mul (side_pos _).ne' (inv_pos.mpr p.len_pos).ne',levels,len] + simp_rw [log_inv,side_pow_two, Real.log_inv,log_pow] + dsimp [s]; rw [Nat.cast_sub H1]; ring + have he : q x ≤ p.dice M x * exp d / s.mass := by + rw [show q x = _ from hi x] + unfold wt + exact div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left (h _) ((p.dice M).nonneg _)) hm.le + have hi' : 0 < p.dice M x := (dice_positive hp).mpr ht + have hc' := log_le_log hx he + rw [log_div (mul_pos hi' (exp_pos _)).ne' hm.ne',log_mul hi'.ne' (exp_pos _).ne', + log_exp,H _ ht] at hc' + change levels s+log (q x) ≤ _ + linarith + +lemma no_label (x : Grid M) : channel (0:Form M) x = uniform _ := by + apply Law.ext; intro i + rw [ch_K,K_eq,F_zero,form_zero,sub_zero,exp_zero,uniform_side,mul_one] + +def givenD (s : Scene M) := deficit s (channel s.fund.mood) + +def massInfo (s : Scene M) := + 1/4*(s.norm/s.mass)*fisher s (channel s.fund.mood) + +namespace Event +def infoFee (e : Event j M) := match e with + | .march s .move _ => massInfo s + | _ => 0 +end Event + +namespace Road + +lemma one_split_price : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ v : Turn j s, + ∀ t : (b : Bool) → Road j M (v.fork.dst b), Trusted J f (.fork s v t) → + + v.fork.probs.Ex (fun b => + givenD (v.fork.dst b) - 500*(v.fork.dst b).fund.spend + + infoFee (.march s v b) - memFee (.march s v b)) ≤ + givenD s - 500*s.fund.spend := by + filter_upwards [priced_change] with j hj M J s f v t h + have hb := branchTrusted h + obtain ⟨ha,hh,he,h⟩ := h + have hL := hh.1.2.2 + let k := channel s.fund.mood + cases v with + | move => + have hx : s.here.lev < M := (rootSafe (h false)).within.2 + have hZ := space_deficit s k ha.within.1 hx ha.positive hL (by + by_contra! hv; exact hh.2.2 (show enter s from by unfold enter; linarith)) + change (step s).probs.Ex (fun b => deficit (s.smaller b) k - + 500*s.fund.spend + _ - (0+0)) ≤ _ + simp only [add_zero,sub_zero,infoFee] + rw [Ex_add,Ex_sub,Ex_const,Ex_const] + unfold massInfo givenD k at *; linarith + | ask u => + let v := toss s u + let d (b : Bool) := deficit (v.dst b) k + have hX (b : Bool) (hv : 0 < v.probs b) : + givenD (v.dst b) - 500*(v.dst b).fund.spend ≤ d b - 500*s.fund.spend := by + cases b with + | false => exact le_rfl + | true => + have hh := hj M J s f u ha (rootSafe (h true)) ((true_mass hb).mp hv) he.2 + change deficit (s.win u) (channel (u.next s.fund.mood)) -500*(s.fund.spend+u.credit) ≤ + deficit (s.win u) (channel s.fund.mood) - 500*s.fund.spend + linarith + let Q := v.probs + have ht : Q.Ex (fun b => memFee (.march s (.ask u) b)) = Q.entropy := by + unfold entropy crossE; congr 1 + funext b; cases b <;> simp [memFee,failFee,winFee,surprise] <;> rfl + have hv := member_deficit s k u hb + change Q.Ex (fun b => givenD (v.dst b) - 500*(v.dst b).fund.spend + infoFee (.march s (.ask u) b) - memFee (.march s (.ask u) b)) ≤ _ + rw [Ex_sub,Ex_add,ht] + simp only [infoFee,Ex_const,add_zero] + have HE := Q.Ex_mono_mem hX + rw [Ex_sub (g:=fun _ => _)] at HE + unfold Q at *; change _ ≤ deficit s k - _ + change v.probs.Ex d ≤ _ at hv + rw [Ex_const] at HE; change v.probs.Ex d ≤ deficit s k + v.probs.entropy at hv + linarith + +lemma fisher_bound : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ f : Grid M → ℝ, + (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, Trusted J f t → 1/Thermal.H j ≤ s.mass → + t.flatFee infoFee ≤ limitC* height j := by + filter_upwards [one_split_price,Fund.fund_est,mem_budget,hpoly limitC 1,Fund.envelope_scale] + with j he hg hs hC ha M J f hF s t ht hm + let a := Thermal.H j; let d := height j; let V := (10:ℝ)^19*d + have HI (s : Scene M) (t : Road j M s) (h : t.Trusted J f) (hM : 0 < s.mass) : + t.flatFee infoFee - t.flatFee memFee ≤ givenD s - 500*s.fund.spend + 500*V := by + induction t with + | finish s b => + have hh := h.1 + have hf := ((hg M).2 _ _ hh.capital).1 + simp_rw [flat_finish] + have h1 : 0 ≤ givenD s := deficit_nonneg _ hh.within hh.positive hM _ + have h2 := hh.capital.solvent + unfold reserve capA at h2 + unfold V d + have he : 1 ≤ height j := ha.2.2.1 + linarith + | push s v t ih => + have hi := ih h.2.2.2 (by rw [pace_mass]; exact hM) + rw [flat_push,flat_push] + simp only [infoFee,memFee, failFee,winFee,zero_add] + cases v <;> exact hi + | fork s v t ih => + let Q := v.fork.probs + let F := fun b => givenD (v.fork.dst b)-500*(v.fork.dst b).fund.spend + have hX (b : Bool) (hb : 0 < Q b) := ih b (h.2.2.2 b) ((true_mass (branchTrusted h)).mp hb) + have hi := Q.Ex_mono_mem hX + have hv := he M J s f v t h + simp_rw [flat_fork _ _ h, Ex_add] + change Q.Ex (fun b => F b + _ - _) ≤ _ at hv + change Q.Ex _ ≤ Q.Ex (fun b => F b + _) at hi + rw [Ex_sub _,Ex_add _ (g:=fun _ => _)] at hi + rw [Ex_const] at hi + rw [Ex_sub,Ex_add] at hv + unfold Q at *; linarith + have hp : 0 < a := H_pos _ + have hC' : limitC*a ≤ d := by simpa only [pow_one] using hC + have hm' : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hm + have hf := rootSafe ht + have Ht : givenD s ≤ d+a := by + change deficit s (channel 0) ≤ d+a + rw [funext no_label] + apply le_trans (local_deficit_dry s (uniform (Grid M)) hf.within hf.positive hm' d hF) + have he := log_le_log (by positivity : 0<1/a) hm + rw [one_div,log_inv] at he + linarith [log_le_sub_one_of_pos hp] + have HZ := HI _ _ ht hm' + have hx := (hs M J s f t ht hm').2 + change t.flatFee memFee ≤ limitC*a at hx + change _ ≤ _-500*0+_ at HZ + change _ ≤ limitC*d + unfold limitC V at * + nlinarith + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Fork.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Fork.lean index bf6540dec8cb4746215777c7799deec30946720f..517c4757989e62339dc289271082c3615a8aa4f2 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Fork.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Fork.lean @@ -1,174 +1,174 @@ -import OAI.Analysis.FourierLLogL.Procedure.Tree - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene - -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -namespace Scene - -/-- Absolute mass in the common top grid -/ -def weight (s : Scene M) := s.here.len * s.mass -def insideW (s : Scene M) (x : Grid M) : ℝ := - if x∈s.here.block M then s.wage x else 0 -def weightE (s : Scene M) (f : Grid M → ℝ) : ℝ := - expU fun x => s.insideW x * f x - -lemma mass_actor {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (x : Grid M) : - s.mass * s.actor x = s.here.dice M x * s.wage x := by - rcases eq_or_lt_of_le (avg_nonneg s.here (fun x _ => hh x)) with h|h - · change 0=s.mass at h; rw [← h,zero_mul] - unfold mass at h - rw [avg_ex hp,Ex] at h - have H (x) : 0 ≤ s.here.dice M x*s.wage x := mul_nonneg ((s.here.dice M).nonneg _) (hh _) - exact (H x).antisymm (le_trans (Finset.single_le_sum (fun i _ => H i) (Finset.mem_univ _)) h.ge) - · rw [actor_def hp hh h x,wt]; unfold mass; field_simp - -lemma actor_weight {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (x : Grid M) : - s.weight * s.actor x = side M * s.insideW x := by - rw [weight,mul_assoc,mass_actor hp hh,dice_apply hp,insideW]; split_ifs - · field_simp [s.here.len_pos.ne'] - ring - -lemma Ex_weight {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (g : Grid M → ℝ) : - s.weight * s.actor.Ex g = s.weightE g := by - simp_rw [Ex,Finset.mul_sum,← mul_assoc,actor_weight hp hh,weightE,expU,smul_eq_mul, - Finset.mul_sum,mul_assoc] - -lemma weight_pos {s : Scene M} : 0 < s.weight ↔ 0 < s.mass := - mul_pos_iff_of_pos_left s.here.len_pos - -lemma actor_reset (s : Scene M) : s.reset.actor = s.actor := rfl -lemma actor_out (s : Scene M) : s.out.actor = s.actor := rfl - -end Scene - -/-- Binary choices, left/right normalized. -/ -structure Fork (M : ℕ) where - src : Scene M - dst : Bool → Scene M - bit : Grid M → Bool - -namespace Fork - -def probs (s : Fork M) : Law Bool := s.src.actor.send s.bit - -/-- Include balance even to a zero-mass destination. -/ -structure TrueLaw (q : Fork M) : Prop where - vertex : 0 < q.src.weight - law : ∀ b x, (q.dst b).weight * (q.dst b).actor x = - if q.bit x=b then q.src.weight * q.src.actor x else 0 - -def step (s : Scene M) : Fork M := - { src := s - dst := s.smaller - bit := fun x => decide (x ∈ s.here.R.block M) } - -def toss (s : Scene M) (u : Update j M s.here) : Fork M := - { src := s - dst := fun b => if b then s.win u else s.lose u - bit := fun x => decide (u.take s.fund.mood x) } - -lemma step_true {s : Scene M} (hp : s.here.valid) (he : s.here.lev < M) (hh : 0 ≤ s.wage) - (hm : 0 < s.mass) : (step s).TrueLaw := by - constructor - · exact weight_pos.mpr hm - intro b x - have h1 := child_ok s.here hp he b - have h2 : s.here.ok M := ⟨hp,he.le⟩ - change (s.smaller b).weight * (s.smaller b).actor x = - if decide (x ∈ s.here.R.block M)=b then _ else 0 - rw [actor_weight (s:=s.smaller b) h1 hh, show (step s).src=s from rfl, actor_weight h2 hh] - unfold insideW - change (side M * if x∈(s.here.child b).block M then s.wage x else 0) = - if decide (x ∈ s.here.R.block M) = b then _ else _ - rw [block_union s.here] - have heq : x∈s.here.L.block M → x∈s.here.R.block M → False := fun ha hb => Set.disjoint_left.mp (s.here.block_separate M) ha hb - cases b <;> by_cases H : x∈s.here.L.block M <;> by_cases H' : x∈s.here.R.block M <;> - simp_all [child] - -lemma toss_true {s : Scene M} (u : Update j M s.here) - (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) : - (toss s u).TrueLaw := by - constructor - · exact weight_pos.mpr hm - intro b x - have hX := split_pos u hh - have he : - (s.win u).weight*(s.win u).actor x = side M*(s.win u).insideW x := - actor_weight (s:=s.win u) hp hX.2.1 x - have he' : (s.lose u).weight*(s.lose u).actor x = side M*(s.lose u).insideW x := - actor_weight (s:=s.lose u) hp hX.1.1 x - cases b <;> - simp only [toss,Bool.false_eq_true, ↓reduceIte,he,he',actor_weight hp hh] - all_goals - by_cases H : u.take s.fund.mood x <;> by_cases H' : x∈s.here.block M <;> - simp_all [insideW, win,lose] - -variable {v : Fork M} - -lemma balance (h : v.TrueLaw) (b : Bool) : - (v.dst b).weight = v.src.weight * v.probs b := by - rw [probs,send_apply,prob_eq,Finset.mul_sum] - calc - _ = ∑ x, (v.dst b).weight * (v.dst b).actor x := by - rw [← Finset.mul_sum,(v.dst b).actor.total]; simp - _ = _ := by simp only [h.law, mul_ite,mul_zero] - -lemma local_law (h : v.TrueLaw) (b : Bool) (x : Grid M) : - v.probs b * (v.dst b).actor x = if v.bit x=b then v.src.actor x else 0 := by - have H := h.law b x; rw [balance h b,mul_assoc] at H - apply mul_left_cancel₀ h.vertex.ne' - split_ifs at H ⊢ with hi <;> simpa [hi] using H - -lemma expect_bit (h : v.TrueLaw) (f : Bool → Grid M → ℝ) : - v.probs.Ex (fun b => (v.dst b).actor.Ex (f b)) = - v.src.actor.Ex fun x => f (v.bit x) x := by - unfold Ex - simp_rw [Finset.mul_sum,← mul_assoc,local_law h,ite_mul,zero_mul] - rw [Finset.sum_comm] - refine Finset.sum_congr rfl fun i _ => ?_ - simp - -lemma true_mass (h : v.TrueLaw) {b : Bool} : - 0 < v.probs b ↔ 0 < (v.dst b).mass := by - rw [← weight_pos,balance h b,mul_pos_iff_of_pos_left h.vertex] - -lemma branch_pos (h : v.TrueLaw) {x : Grid M} (hh : 0 < v.src.actor x) : - 0 < v.probs (v.bit x) ∧ 0 < (v.dst (v.bit x)).actor x := by - have H := local_law h (v.bit x) x - rw [ite_eq_left rfl] at H - have He : 0 < v.probs (v.bit x) * (v.dst (v.bit x)).actor x := H.symm ▸ hh - exact ⟨pos_of_mul_pos_left He ((v.dst _).actor.nonneg _), - pos_of_mul_pos_right He (v.probs.nonneg _)⟩ - -lemma step_mass {s : Scene M} (h : (step s).TrueLaw) (b : Bool) : - (s.smaller b).mass = 2*((step s).probs b)*s.mass := by - have H := balance h b - change (s.here.child b).len * (s.smaller b).mass = s.here.len*s.mass* _ at H - rw [child_len] at H; nlinarith [s.here.len_pos] - -lemma toss_mass {s : Scene M} {u : Update j M s.here} (h : (toss s u).TrueLaw) (b : Bool) : - ((toss s u).dst b).mass = (toss s u).probs b*s.mass := by - have H := balance h b - have hh : ((toss s u).dst b).here=s.here := by cases b <;> rfl - rw [weight,hh,weight] at H - apply mul_left_cancel₀ s.here.len_pos.ne' - rw [H] - dsimp only [toss] - ring - -lemma success_prob {s : Scene M} (u : Update j M s.here) : - (toss s u).probs true = u.chances s.actor s.fund.mood := by - unfold probs; rw [send_apply]; simp [toss,Update.chances] - -end Fork -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Tree + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene + +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +namespace Scene + +/-- Absolute mass in the common top grid -/ +def weight (s : Scene M) := s.here.len * s.mass +def insideW (s : Scene M) (x : Grid M) : ℝ := + if x∈s.here.block M then s.wage x else 0 +def weightE (s : Scene M) (f : Grid M → ℝ) : ℝ := + expU fun x => s.insideW x * f x + +lemma mass_actor {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (x : Grid M) : + s.mass * s.actor x = s.here.dice M x * s.wage x := by + rcases eq_or_lt_of_le (avg_nonneg s.here (fun x _ => hh x)) with h|h + · change 0=s.mass at h; rw [← h,zero_mul] + unfold mass at h + rw [avg_ex hp,Ex] at h + have H (x) : 0 ≤ s.here.dice M x*s.wage x := mul_nonneg ((s.here.dice M).nonneg _) (hh _) + exact (H x).antisymm (le_trans (Finset.single_le_sum (fun i _ => H i) (Finset.mem_univ _)) h.ge) + · rw [actor_def hp hh h x,wt]; unfold mass; field_simp + +lemma actor_weight {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (x : Grid M) : + s.weight * s.actor x = side M * s.insideW x := by + rw [weight,mul_assoc,mass_actor hp hh,dice_apply hp,insideW]; split_ifs + · field_simp [s.here.len_pos.ne'] + ring + +lemma Ex_weight {s : Scene M} (hp : s.here.ok M) (hh : 0 ≤ s.wage) (g : Grid M → ℝ) : + s.weight * s.actor.Ex g = s.weightE g := by + simp_rw [Ex,Finset.mul_sum,← mul_assoc,actor_weight hp hh,weightE,expU,smul_eq_mul, + Finset.mul_sum,mul_assoc] + +lemma weight_pos {s : Scene M} : 0 < s.weight ↔ 0 < s.mass := + mul_pos_iff_of_pos_left s.here.len_pos + +lemma actor_reset (s : Scene M) : s.reset.actor = s.actor := rfl +lemma actor_out (s : Scene M) : s.out.actor = s.actor := rfl + +end Scene + +/-- Binary choices, left/right normalized. -/ +structure Fork (M : ℕ) where + src : Scene M + dst : Bool → Scene M + bit : Grid M → Bool + +namespace Fork + +def probs (s : Fork M) : Law Bool := s.src.actor.send s.bit + +/-- Include balance even to a zero-mass destination. -/ +structure TrueLaw (q : Fork M) : Prop where + vertex : 0 < q.src.weight + law : ∀ b x, (q.dst b).weight * (q.dst b).actor x = + if q.bit x=b then q.src.weight * q.src.actor x else 0 + +def step (s : Scene M) : Fork M := + { src := s + dst := s.smaller + bit := fun x => decide (x ∈ s.here.R.block M) } + +def toss (s : Scene M) (u : Update j M s.here) : Fork M := + { src := s + dst := fun b => if b then s.win u else s.lose u + bit := fun x => decide (u.take s.fund.mood x) } + +lemma step_true {s : Scene M} (hp : s.here.valid) (he : s.here.lev < M) (hh : 0 ≤ s.wage) + (hm : 0 < s.mass) : (step s).TrueLaw := by + constructor + · exact weight_pos.mpr hm + intro b x + have h1 := child_ok s.here hp he b + have h2 : s.here.ok M := ⟨hp,he.le⟩ + change (s.smaller b).weight * (s.smaller b).actor x = + if decide (x ∈ s.here.R.block M)=b then _ else 0 + rw [actor_weight (s:=s.smaller b) h1 hh, show (step s).src=s from rfl, actor_weight h2 hh] + unfold insideW + change (side M * if x∈(s.here.child b).block M then s.wage x else 0) = + if decide (x ∈ s.here.R.block M) = b then _ else _ + rw [block_union s.here] + have heq : x∈s.here.L.block M → x∈s.here.R.block M → False := fun ha hb => Set.disjoint_left.mp (s.here.block_separate M) ha hb + cases b <;> by_cases H : x∈s.here.L.block M <;> by_cases H' : x∈s.here.R.block M <;> + simp_all [child] + +lemma toss_true {s : Scene M} (u : Update j M s.here) + (hp : s.here.ok M) (hh : 0 ≤ s.wage) (hm : 0 < s.mass) : + (toss s u).TrueLaw := by + constructor + · exact weight_pos.mpr hm + intro b x + have hX := split_pos u hh + have he : + (s.win u).weight*(s.win u).actor x = side M*(s.win u).insideW x := + actor_weight (s:=s.win u) hp hX.2.1 x + have he' : (s.lose u).weight*(s.lose u).actor x = side M*(s.lose u).insideW x := + actor_weight (s:=s.lose u) hp hX.1.1 x + cases b <;> + simp only [toss,Bool.false_eq_true, ↓reduceIte,he,he',actor_weight hp hh] + all_goals + by_cases H : u.take s.fund.mood x <;> by_cases H' : x∈s.here.block M <;> + simp_all [insideW, win,lose] + +variable {v : Fork M} + +lemma balance (h : v.TrueLaw) (b : Bool) : + (v.dst b).weight = v.src.weight * v.probs b := by + rw [probs,send_apply,prob_eq,Finset.mul_sum] + calc + _ = ∑ x, (v.dst b).weight * (v.dst b).actor x := by + rw [← Finset.mul_sum,(v.dst b).actor.total]; simp + _ = _ := by simp only [h.law, mul_ite,mul_zero] + +lemma local_law (h : v.TrueLaw) (b : Bool) (x : Grid M) : + v.probs b * (v.dst b).actor x = if v.bit x=b then v.src.actor x else 0 := by + have H := h.law b x; rw [balance h b,mul_assoc] at H + apply mul_left_cancel₀ h.vertex.ne' + split_ifs at H ⊢ with hi <;> simpa [hi] using H + +lemma expect_bit (h : v.TrueLaw) (f : Bool → Grid M → ℝ) : + v.probs.Ex (fun b => (v.dst b).actor.Ex (f b)) = + v.src.actor.Ex fun x => f (v.bit x) x := by + unfold Ex + simp_rw [Finset.mul_sum,← mul_assoc,local_law h,ite_mul,zero_mul] + rw [Finset.sum_comm] + refine Finset.sum_congr rfl fun i _ => ?_ + simp + +lemma true_mass (h : v.TrueLaw) {b : Bool} : + 0 < v.probs b ↔ 0 < (v.dst b).mass := by + rw [← weight_pos,balance h b,mul_pos_iff_of_pos_left h.vertex] + +lemma branch_pos (h : v.TrueLaw) {x : Grid M} (hh : 0 < v.src.actor x) : + 0 < v.probs (v.bit x) ∧ 0 < (v.dst (v.bit x)).actor x := by + have H := local_law h (v.bit x) x + rw [ite_eq_left rfl] at H + have He : 0 < v.probs (v.bit x) * (v.dst (v.bit x)).actor x := H.symm ▸ hh + exact ⟨pos_of_mul_pos_left He ((v.dst _).actor.nonneg _), + pos_of_mul_pos_right He (v.probs.nonneg _)⟩ + +lemma step_mass {s : Scene M} (h : (step s).TrueLaw) (b : Bool) : + (s.smaller b).mass = 2*((step s).probs b)*s.mass := by + have H := balance h b + change (s.here.child b).len * (s.smaller b).mass = s.here.len*s.mass* _ at H + rw [child_len] at H; nlinarith [s.here.len_pos] + +lemma toss_mass {s : Scene M} {u : Update j M s.here} (h : (toss s u).TrueLaw) (b : Bool) : + ((toss s u).dst b).mass = (toss s u).probs b*s.mass := by + have H := balance h b + have hh : ((toss s u).dst b).here=s.here := by cases b <;> rfl + rw [weight,hh,weight] at H + apply mul_left_cancel₀ s.here.len_pos.ne' + rw [H] + dsimp only [toss] + ring + +lemma success_prob {s : Scene M} (u : Update j M s.here) : + (toss s u).probs true = u.chances s.actor s.fund.mood := by + unfold probs; rw [send_apply]; simp [toss,Update.chances] + +end Fork +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Likelihood.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Likelihood.lean index 7f33020ad577444a7237835ff56d9c52c7457a53..a0e5526c4dfc86a1f0ee9de0073176d8cf2e602c 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Likelihood.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Likelihood.lean @@ -1,222 +1,222 @@ -import OAI.Analysis.FourierLLogL.Procedure.Surprisal - -/-! # Likelihood estimates through any terminal step -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal - -variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -lemma cutoff_log : - ∀ᶠ j : ℕ in atTop, log (2*cutoff j) ≤ 20000*H j := by - filter_upwards [evt_slow, eventually_ge_atTop 1] with j hj hi - have h1 := hj.2 - have hp : (1:ℝ) ≤ j := by exact_mod_cast hi - unfold cutoff; rw [log_mul (by norm_num) (by positivity),log_pow] - have hZ := log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2) - have he : (j:ℝ)<2^(3000*slow j) := by exact_mod_cast h1 - have HH := log_le_log (by linarith : 0 < (j:ℝ)+1) (show (j:ℝ)+1≤ 2* (2:ℝ)^(3000*slow j) by linarith) - rw [log_mul (by norm_num) (by positivity), log_pow] at HH - have hg := H_ge j - unfold Thermal.H at *; push_cast at * - nlinarith - -namespace Journal - -lemma fee_neg (l : Journal j M) (f : Event j M → ℝ) : - l.fee (fun x => - f x) = -l.fee f := by - unfold fee - suffices HH : ∀ l : List (Event j M), (l.map (fun x => -f x)).sum= -(l.map f).sum from HH _ - intro L; induction L with - | nil => simp - | cons i t ih => simp [ih,add_comm] - -lemma fee_div_sub (l : Journal j M) (f g : Event j M → ℝ) (r : ℝ) : - l.fee (fun x => f x/r-g x) = l.fee f/r-l.fee g := by - unfold fee - suffices H : ∀ l : List (Event j M), (l.map (fun x => f x/r-g x)).sum = - (l.map f).sum/r - (l.map g).sum from H _ - intro a; induction a with - | nil => simp - | cons i t ih => simp only [List.map_cons,List.sum_cons,ih]; ring - -end Journal - -namespace Road - -lemma minus_like_cap (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : - t.expHike (fun l _ => exp (-l.like)) ≤ 1 := by - have hi := capital_bound (s:=s) (fun i => -i.likelihood) - (by intro i v; simp [Event.likelihood]) (by - intro s v; cases v with - | ask u => - simp only [likelihood,neg_zero,exp_zero,Ex_const]; exact le_rfl - | move => - let Q := (step s).probs - change Q.Ex (fun b => exp (-log (2*Q b))) ≤ _ - have H (b : Bool) : Q b * exp (-log (2*Q b)) ≤ 1/2 := by - rcases eq_or_lt_of_le (Q.nonneg b) with hx|hx - · rw [← hx,zero_mul]; norm_num - rw [exp_neg,exp_log (by positivity)] - field_simp; rfl - simp only [Ex,Fintype.sum_bool]; linarith [H true,H false]) t h hm - simpa only [Journal.fee_neg,like] using hi - -lemma half_debit_cap (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : - t.expHike (fun l _ => exp (l.debit/2-l.fee count)) ≤ 1 := by - have H' := capital_bound (s:=s) (fun i => i.failFee/2-i.count) - (by intro x v; simp [failFee,count]) (by - intro s v; cases v with - | move => - simp [Ex_const,failFee,count] - | ask u => - let Q := (toss s u).probs - have Hq : Q true+Q false=1 := by simp only [← Q.total,Fintype.sum_bool] - have he₁ : Q true*exp (-1) ≤ Q true/2 := by - have HP : Q true ≤ 1 := Q.le_one _ - have hx := Q.nonneg true - rw [exp_neg] - have he : 2 ≤ exp 1 := Real.exp_one_gt_two.le - field_simp; nlinarith - have he₂ : Q false*exp ((-log (Q false))/2) ≤ 1-Q true/2 := by - rcases eq_or_lt_of_le (Q.nonneg false) with Hx|Hx - · rw [← Hx,zero_mul] - linarith - have Ht := Real.exp_pos ((-log (Q false))/2) - let q := Q false - have hs : exp ((-log q)/2)^2 = 1/q := by - rw [pow_two,←exp_add, add_halves,exp_neg,exp_log Hx]; ring - have HH : (q *exp ((-log q)/2))^2=q := by rw [mul_pow,hs]; field_simp - change _ + q = 1 at Hq - change _*exp ((-log q)/2) ≤ _ - change 0 < q at Hx - change q*exp ((-log q)/2) ≤ _ - nlinarith [sq_nonneg (q-1)] - unfold Ex - rw [Fintype.sum_bool] - simp only [failFee,count, surprise,Turn.fork,zero_div,zero_sub,sub_zero] - unfold Q at *; linarith) t h hm - simpa only [debit,fee_div_sub] using H' - -private lemma negative_square (x : ℝ) : max (-x) 0 ^2 ≤ 4*exp (-x) := by - by_cases H : 0 ≤ -x - · rw [max_eq_left H] - have hh := Real.add_one_le_exp ((-x)/2) - rw [show exp (-x)=exp ((-x)/2)*exp ((-x)/2) from - Eq.symm (by rw [← exp_add,add_halves])] - nlinarith - rw [max_eq_right (by linarith)] - simp only [ne_eq, OfNat.ofNat_ne_zero, not_false_eq_true, zero_pow] - positivity - -lemma neg_like_budget (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : - t.expHike (fun l _ => (max (-l.like) 0)^2) ≤ 4 := by - unfold expHike - apply le_trans (Ex_mono _ fun x => negative_square ((t.hike x).like)) - rw [Ex_lmul] - have hh := minus_like_cap t h hm; unfold expHike at hh - linarith - -private lemma spent_top : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ c : Fund M, ∀ p : Node, Ok c p j → - c.spend ≤ 10^19*height j := by - filter_upwards [Fund.fund_est,Fund.envelope_scale] with j H hi M c p h - have h0 := ((H M).2 c p h).1 - have h1 := h.solvent - have h2 : 1 ≤ height j := hi.2.2.1 - unfold reserve capA at h1 - norm_num at *; linarith - -def limitC : ℝ := 10^30 - -lemma mem_budget : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Road j M s, t.Trusted J f → 0 < s.mass → - t.expHike (fun l _ => membership l ^2) ≤ (limitC*H j)^2 ∧ - t.expHike (fun l _ => membership l) ≤ limitC*H j := by - classical - filter_upwards [spent_top] with j hg M J s f t h hm - let a := H j; let d := height j - have ha : 1 ≤ a := H_ge _ - have hb : 0 ≤ a := le_trans zero_le_one ha - have hd : 0 < d := d_pos _ - let F (x) := membership (t.hike x) - let e (x) := debit (t.hike x)/2 - (t.hike x).fee count - have hf : s.actor.Ex (fun x => F x ^2) ≤ (limitC*a)^2 := by - have H (x : Grid M) (hx : 0 < s.actor x) : - F x ^2 ≤ 10^58*a^2 + 128*exp (e x) := by - obtain ⟨hs,hh,h1,h2,h3,h4⟩ := final_sound h hm hx - let l := t.hike x - have hi : l.fee count ≤ capA*a := by - have ht := hs.capital.under - change _ + l.fee count=_ at h3 - change ((l.done.fund.uses:ℕ):ℝ) ≤ capA*a at ht - linarith [Nat.cast_nonneg (α:=ℝ) s.fund.uses] - have hW : creditSum l ≤ 10^26*a := by - have HP : l.done.fund.spend-s.fund.spend ≤ 10^19*d := by - have hk := (rootSafe h).capital.nonneg - exact le_trans (sub_le_self _ (hk.1.trans hk.2)) (hg M l.done.fund l.done.here hs.capital) - apply h4.trans - calc - _ ≤ 2000000*a/d*(10^19*d) := mul_le_mul_of_nonneg_left HP (by positivity : 0 ≤ 2000000*a/d) - _ ≤ _ := by field_simp; nlinarith - have hWW : F x ≤ (10^26+2*capA)*a+ 2*max (e x) 0 := by - change membership l ≤ _ - rw [mem_parts] - have hh := le_max_left (e x) 0 - change debit l/2-l.fee count ≤ _ at hh - unfold capA at *; linarith - have hr' := negative_square (-(e x)) - rw [neg_neg] at hr' - have hr := le_max_right (e x) 0 - have hZ : 0 ≤ F x := l.positive_mem - apply le_trans (pow_le_pow_left₀ hZ hWW 2) - unfold capA; nlinarith [exp_nonneg (e x), - sq_nonneg ((10^26+2*(10:ℝ)^16)*a-2*max (e x) 0)] - apply le_trans (Ex_mono_mem _ H) - rw [Ex_add,Ex_const,Ex_lmul] - have Hh : s.actor.Ex (fun x => exp (e x)) ≤ 1 := half_debit_cap t h hm - unfold limitC; norm_num at *; nlinarith - change s.actor.Ex (fun x => F x^2) ≤ (limitC*a)^2 ∧ s.actor.Ex F ≤ limitC*a - exact ⟨hf,by nlinarith [sq_Ex s.actor F,show 0 ≤ limitC*a by unfold limitC; positivity]⟩ - -lemma log_mass_budget : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → - t.expHike (fun l _ => (max (-log l.done.mass) 0)^2) ≤ (10*limitC*H j)^2 := by - filter_upwards [mem_budget] with j hj M J s f t h hm - let a := H j - have hp : 0 < a := H_pos j - have he : 1 ≤ a := H_ge j - have hs : 0 < s.mass := lt_of_lt_of_le (by positivity : 0 < 1/a) hm - have Hl : -log s.mass ≤ a := by - have HH := log_le_log (by positivity : 0 < 1/a) hm - rw [one_div,Real.log_inv] at HH - linarith [log_le_sub_one_of_pos hp] - have Hx (x : Grid M) (hx : 0 < s.actor x) : - let l := t.hike x - (max (-log l.done.mass) 0)^2 ≤ 3*a^2 + 3*l.membership^2 + 3*max (-l.like) 0 ^2 := by - intro l; obtain ⟨_,_,_,h1,_,_⟩ := final_sound h hs hx - have hP := l.positive_mem - have hi := le_max_right (-l.like) 0 - have h := le_max_left (-l.like) 0 - have heq : max (-log l.done.mass) 0 ≤ a + l.membership + max (-l.like) 0 := by - apply max_le <;> first | (dsimp only [l] at *; linarith) - nlinarith [show 0 ≤ max (-log l.done.mass) 0 from le_max_right _ _, - sq_nonneg (a-l.membership),sq_nonneg (a-max (-l.like) 0),sq_nonneg (l.membership-max (-l.like) 0)] - have HH := (hj M J s f t h hs).1 - have HE := neg_like_budget t h hs - unfold expHike at * - apply le_trans (Ex_mono_mem _ Hx) - rw [Ex_add,Ex_add,Ex_lmul,Ex_const,Ex_lmul,Ex_lmul] - unfold limitC at *; nlinarith [show H j=a from rfl] - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Surprisal + +/-! # Likelihood estimates through any terminal step -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal + +variable {J M j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +lemma cutoff_log : + ∀ᶠ j : ℕ in atTop, log (2*cutoff j) ≤ 20000*H j := by + filter_upwards [evt_slow, eventually_ge_atTop 1] with j hj hi + have h1 := hj.2 + have hp : (1:ℝ) ≤ j := by exact_mod_cast hi + unfold cutoff; rw [log_mul (by norm_num) (by positivity),log_pow] + have hZ := log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2) + have he : (j:ℝ)<2^(3000*slow j) := by exact_mod_cast h1 + have HH := log_le_log (by linarith : 0 < (j:ℝ)+1) (show (j:ℝ)+1≤ 2* (2:ℝ)^(3000*slow j) by linarith) + rw [log_mul (by norm_num) (by positivity), log_pow] at HH + have hg := H_ge j + unfold Thermal.H at *; push_cast at * + nlinarith + +namespace Journal + +lemma fee_neg (l : Journal j M) (f : Event j M → ℝ) : + l.fee (fun x => - f x) = -l.fee f := by + unfold fee + suffices HH : ∀ l : List (Event j M), (l.map (fun x => -f x)).sum= -(l.map f).sum from HH _ + intro L; induction L with + | nil => simp + | cons i t ih => simp [ih,add_comm] + +lemma fee_div_sub (l : Journal j M) (f g : Event j M → ℝ) (r : ℝ) : + l.fee (fun x => f x/r-g x) = l.fee f/r-l.fee g := by + unfold fee + suffices H : ∀ l : List (Event j M), (l.map (fun x => f x/r-g x)).sum = + (l.map f).sum/r - (l.map g).sum from H _ + intro a; induction a with + | nil => simp + | cons i t ih => simp only [List.map_cons,List.sum_cons,ih]; ring + +end Journal + +namespace Road + +lemma minus_like_cap (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : + t.expHike (fun l _ => exp (-l.like)) ≤ 1 := by + have hi := capital_bound (s:=s) (fun i => -i.likelihood) + (by intro i v; simp [Event.likelihood]) (by + intro s v; cases v with + | ask u => + simp only [likelihood,neg_zero,exp_zero,Ex_const]; exact le_rfl + | move => + let Q := (step s).probs + change Q.Ex (fun b => exp (-log (2*Q b))) ≤ _ + have H (b : Bool) : Q b * exp (-log (2*Q b)) ≤ 1/2 := by + rcases eq_or_lt_of_le (Q.nonneg b) with hx|hx + · rw [← hx,zero_mul]; norm_num + rw [exp_neg,exp_log (by positivity)] + field_simp; rfl + simp only [Ex,Fintype.sum_bool]; linarith [H true,H false]) t h hm + simpa only [Journal.fee_neg,like] using hi + +lemma half_debit_cap (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : + t.expHike (fun l _ => exp (l.debit/2-l.fee count)) ≤ 1 := by + have H' := capital_bound (s:=s) (fun i => i.failFee/2-i.count) + (by intro x v; simp [failFee,count]) (by + intro s v; cases v with + | move => + simp [Ex_const,failFee,count] + | ask u => + let Q := (toss s u).probs + have Hq : Q true+Q false=1 := by simp only [← Q.total,Fintype.sum_bool] + have he₁ : Q true*exp (-1) ≤ Q true/2 := by + have HP : Q true ≤ 1 := Q.le_one _ + have hx := Q.nonneg true + rw [exp_neg] + have he : 2 ≤ exp 1 := Real.exp_one_gt_two.le + field_simp; nlinarith + have he₂ : Q false*exp ((-log (Q false))/2) ≤ 1-Q true/2 := by + rcases eq_or_lt_of_le (Q.nonneg false) with Hx|Hx + · rw [← Hx,zero_mul] + linarith + have Ht := Real.exp_pos ((-log (Q false))/2) + let q := Q false + have hs : exp ((-log q)/2)^2 = 1/q := by + rw [pow_two,←exp_add, add_halves,exp_neg,exp_log Hx]; ring + have HH : (q *exp ((-log q)/2))^2=q := by rw [mul_pow,hs]; field_simp + change _ + q = 1 at Hq + change _*exp ((-log q)/2) ≤ _ + change 0 < q at Hx + change q*exp ((-log q)/2) ≤ _ + nlinarith [sq_nonneg (q-1)] + unfold Ex + rw [Fintype.sum_bool] + simp only [failFee,count, surprise,Turn.fork,zero_div,zero_sub,sub_zero] + unfold Q at *; linarith) t h hm + simpa only [debit,fee_div_sub] using H' + +private lemma negative_square (x : ℝ) : max (-x) 0 ^2 ≤ 4*exp (-x) := by + by_cases H : 0 ≤ -x + · rw [max_eq_left H] + have hh := Real.add_one_le_exp ((-x)/2) + rw [show exp (-x)=exp ((-x)/2)*exp ((-x)/2) from + Eq.symm (by rw [← exp_add,add_halves])] + nlinarith + rw [max_eq_right (by linarith)] + simp only [ne_eq, OfNat.ofNat_ne_zero, not_false_eq_true, zero_pow] + positivity + +lemma neg_like_budget (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) : + t.expHike (fun l _ => (max (-l.like) 0)^2) ≤ 4 := by + unfold expHike + apply le_trans (Ex_mono _ fun x => negative_square ((t.hike x).like)) + rw [Ex_lmul] + have hh := minus_like_cap t h hm; unfold expHike at hh + linarith + +private lemma spent_top : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ c : Fund M, ∀ p : Node, Ok c p j → + c.spend ≤ 10^19*height j := by + filter_upwards [Fund.fund_est,Fund.envelope_scale] with j H hi M c p h + have h0 := ((H M).2 c p h).1 + have h1 := h.solvent + have h2 : 1 ≤ height j := hi.2.2.1 + unfold reserve capA at h1 + norm_num at *; linarith + +def limitC : ℝ := 10^30 + +lemma mem_budget : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Road j M s, t.Trusted J f → 0 < s.mass → + t.expHike (fun l _ => membership l ^2) ≤ (limitC*H j)^2 ∧ + t.expHike (fun l _ => membership l) ≤ limitC*H j := by + classical + filter_upwards [spent_top] with j hg M J s f t h hm + let a := H j; let d := height j + have ha : 1 ≤ a := H_ge _ + have hb : 0 ≤ a := le_trans zero_le_one ha + have hd : 0 < d := d_pos _ + let F (x) := membership (t.hike x) + let e (x) := debit (t.hike x)/2 - (t.hike x).fee count + have hf : s.actor.Ex (fun x => F x ^2) ≤ (limitC*a)^2 := by + have H (x : Grid M) (hx : 0 < s.actor x) : + F x ^2 ≤ 10^58*a^2 + 128*exp (e x) := by + obtain ⟨hs,hh,h1,h2,h3,h4⟩ := final_sound h hm hx + let l := t.hike x + have hi : l.fee count ≤ capA*a := by + have ht := hs.capital.under + change _ + l.fee count=_ at h3 + change ((l.done.fund.uses:ℕ):ℝ) ≤ capA*a at ht + linarith [Nat.cast_nonneg (α:=ℝ) s.fund.uses] + have hW : creditSum l ≤ 10^26*a := by + have HP : l.done.fund.spend-s.fund.spend ≤ 10^19*d := by + have hk := (rootSafe h).capital.nonneg + exact le_trans (sub_le_self _ (hk.1.trans hk.2)) (hg M l.done.fund l.done.here hs.capital) + apply h4.trans + calc + _ ≤ 2000000*a/d*(10^19*d) := mul_le_mul_of_nonneg_left HP (by positivity : 0 ≤ 2000000*a/d) + _ ≤ _ := by field_simp; nlinarith + have hWW : F x ≤ (10^26+2*capA)*a+ 2*max (e x) 0 := by + change membership l ≤ _ + rw [mem_parts] + have hh := le_max_left (e x) 0 + change debit l/2-l.fee count ≤ _ at hh + unfold capA at *; linarith + have hr' := negative_square (-(e x)) + rw [neg_neg] at hr' + have hr := le_max_right (e x) 0 + have hZ : 0 ≤ F x := l.positive_mem + apply le_trans (pow_le_pow_left₀ hZ hWW 2) + unfold capA; nlinarith [exp_nonneg (e x), + sq_nonneg ((10^26+2*(10:ℝ)^16)*a-2*max (e x) 0)] + apply le_trans (Ex_mono_mem _ H) + rw [Ex_add,Ex_const,Ex_lmul] + have Hh : s.actor.Ex (fun x => exp (e x)) ≤ 1 := half_debit_cap t h hm + unfold limitC; norm_num at *; nlinarith + change s.actor.Ex (fun x => F x^2) ≤ (limitC*a)^2 ∧ s.actor.Ex F ≤ limitC*a + exact ⟨hf,by nlinarith [sq_Ex s.actor F,show 0 ≤ limitC*a by unfold limitC; positivity]⟩ + +lemma log_mass_budget : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Road j M s, t.Trusted J f → 1/H j ≤ s.mass → + t.expHike (fun l _ => (max (-log l.done.mass) 0)^2) ≤ (10*limitC*H j)^2 := by + filter_upwards [mem_budget] with j hj M J s f t h hm + let a := H j + have hp : 0 < a := H_pos j + have he : 1 ≤ a := H_ge j + have hs : 0 < s.mass := lt_of_lt_of_le (by positivity : 0 < 1/a) hm + have Hl : -log s.mass ≤ a := by + have HH := log_le_log (by positivity : 0 < 1/a) hm + rw [one_div,Real.log_inv] at HH + linarith [log_le_sub_one_of_pos hp] + have Hx (x : Grid M) (hx : 0 < s.actor x) : + let l := t.hike x + (max (-log l.done.mass) 0)^2 ≤ 3*a^2 + 3*l.membership^2 + 3*max (-l.like) 0 ^2 := by + intro l; obtain ⟨_,_,_,h1,_,_⟩ := final_sound h hs hx + have hP := l.positive_mem + have hi := le_max_right (-l.like) 0 + have h := le_max_left (-l.like) 0 + have heq : max (-log l.done.mass) 0 ≤ a + l.membership + max (-l.like) 0 := by + apply max_le <;> first | (dsimp only [l] at *; linarith) + nlinarith [show 0 ≤ max (-log l.done.mass) 0 from le_max_right _ _, + sq_nonneg (a-l.membership),sq_nonneg (a-max (-l.like) 0),sq_nonneg (l.membership-max (-l.like) 0)] + have HH := (hj M J s f t h hs).1 + have HE := neg_like_budget t h hs + unfold expHike at * + apply le_trans (Ex_mono_mem _ Hx) + rw [Ex_add,Ex_add,Ex_lmul,Ex_const,Ex_lmul,Ex_lmul] + unfold limitC at *; nlinarith [show H j=a from rfl] + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Memory.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Memory.lean index a13821d274b4fd67b94c7f8096713de911e83c3d..e05e229af0832dac038428ead387577adfea64b4 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Memory.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Memory.lean @@ -1,189 +1,189 @@ -import OAI.Analysis.FourierLLogL.Procedure.Road -import OAI.Analysis.FourierLLogL.Thermal.True - -/-! # Observation records in the comparison proof -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -structure Memory (M : ℕ) where - profile : Profile M - current : Grid M - -namespace Memory -def piece (A B : Form M) (w : Clutch j M) : Profile M := - (B,w.1) :: List.ofFn (fun i => (A,w.2 i)) - -def stash (m : Memory M) (A B : Form M) (w : Clutch j M) : - Memory M := ⟨piece A B w ++ m.profile,w.1⟩ - -structure Ready (m : Memory M) (j : ℕ) (c : Fund M) : Prop where - ordered : ∀ i ∈ m.profile, 0 ≤ i.1 ∧ i.1 ≤ c.mood - has : (c.mood, m.current)∈m.profile - num : m.profile.length = 1+c.uses*(1+clones j) - - -def temp (j : ℕ) := cushion j / (capA * H j) - -def loadE (A B : Form M) (w : Clutch j M) (x : Grid M) := - form B (w.1-x) + ∑ i, form A (w.2 i-x) - -lemma stash_ready (m : Memory M) {s : Scene M} (h : Ready m j s.fund) - (u : Update j M s.here) (hD : 0 ≤ u.changeForm) (hA : 0 ≤ s.fund.mood) (w : Clutch j M) : - Ready (m.stash s.fund.mood (u.next s.fund.mood) w) j (s.fund.advance s.here u) := by - let B := u.next s.fund.mood - have H : s.fund.mood ≤ B := - (le_add_of_nonneg_right (place_valid _ _)).trans (le_add_of_nonneg_right hD) - constructor - · intro t ht - simp only [stash,piece,List.mem_append,List.mem_cons,List.mem_ofFn] at ht - rcases ht with (he|he)|he - · subst t; exact ⟨hA.trans H,le_rfl⟩ - · obtain ⟨i,rfl⟩ := he; exact ⟨hA,H⟩ - obtain ⟨hl,hr⟩ := h.ordered t he - exact ⟨hl,hr.trans H⟩ - · simp [advance,stash,piece] - · simp only [stash,List.length_append,piece,List.length_cons,List.length_ofFn,h.num,advance] - ring - -lemma stash_length_bound (m : Memory M) {s : Scene M} (h : Ready m j s.fund) - (hg : Safe s j J f) : - ↑(m.profile.length) ≤ observationBudget capA j := by - rw [h.num,observationBudget] - push_cast; gcongr - exact hg.capital.under - -lemma sumE_piece (A B : Form M) (w : Clutch j M) (x : Grid M) : - sumE (piece A B w) x = loadE A B w x := by - simp only [sumE,piece,List.map_cons,List.map_ofFn,Function.comp_def, - List.sum_cons,List.sum_ofFn,loadE] - -lemma sumE_stash (m : Memory M) (A B : Form M) (w : Clutch j M) (x : Grid M) : - sumE (m.stash A B w).profile x = loadE A B w x + sumE m.profile x := by - rw [←sumE_piece] - simp only [stash,sumE,List.map_append,List.sum_append] - -lemma form_in_past (m : Memory M) (p : Form M × Grid M) - (h : ∀ i∈m.profile, 0 ≤ i.1) (hp : p ∈ m.profile) (x : Grid M) : - form p.1 (p.2-x) ≤ sumE m.profile x := by - suffices HH : ∀ l : Profile M, (∀ i∈l, 0 ≤ i.1) → p∈l → - form p.1 (p.2-x) ≤ sumE l x from HH _ h hp - intro l; induction l with - | nil => simp - | cons i l ih => - intro h h' - have he : 0 ≤ i.1 := h i List.mem_cons_self - have hh : ∀ i∈l, 0 ≤ i.1 := fun i hi => h i (List.mem_cons_of_mem _ hi) - simp only [sumE,List.map_cons,List.sum_cons] - rcases List.mem_cons.mp h' with rfl|H - · apply le_add_of_nonneg_right - apply List.sum_nonneg - intro i hi - obtain ⟨x,hx,rfl⟩ := List.mem_map.mp hi - exact form_nonneg (hh _ hx) _ - exact (ih hh H).trans (le_add_of_nonneg_left (form_nonneg he _)) - -def stashRate (A B : Form M) (j : ℕ) := - (side M*exp (F B)) * (side M*exp (F A))^(clones j) - -lemma multiply_stash (m : Memory M) (A B : Form M) (p : Node) (x : Grid M) - (w : Clutch j M) : - prefixE m.profile p x * fresh A B j x w = - stashRate A B j * prefixE (m.stash A B w).profile p x := by - have hi (A : Form M) (v) : - channel A x v = (side M*exp (F A)) * exp (-form A (v-x)) := by - rw [ch_K,K_eq,sub_eq_add_neg,exp_add,mul_assoc] - have H (v : Fin (clones j) → Grid M) : - ∏ i, channel A x (v i) = (side M*exp (F A))^(clones j) * - exp (-(∑ i, form A (v i-x))) := by - simp_rw [hi,Finset.prod_mul_distrib,← Real.exp_sum,Finset.sum_neg_distrib] - simp [mul_pow,exp_nat_mul] - unfold prefixE - split_ifs with hh - · obtain ⟨v,c⟩ := w - rw [fresh,tensorP_apply,pack_apply,H,hi,stashRate] - simp_rw [tune,sumE_stash,loadE,neg_add,Real.exp_add] - ring - · ring - -lemma rate_positive (j : ℕ) (A B : Form M) : 0 ≤ stashRate A B j := by - unfold stashRate; have := side_pos M; positivity - -lemma batch_expected (A B : Form M) (x : Grid M) (j : ℕ) : - (fresh A B j x).Ex (fun w => loadE A B w x) = - (label B).Ex (form B) + clones j * (label A).Ex (form A) := by - simp_rw [fresh,Ex_tensorP,loadE,Ex_add,Ex_const,Ex_finset_sum, - Ex_pack_proj (fun _ => channel A x) _ (fun z => form A (z-x))] - rw [ch_noise B (form B)] - simp_rw [ch_noise A (form A)] - simp - -lemma batch_scale : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ x : Grid M, - ∀ u : Update j M s.here, s.Safe j J f → (s.win u).Safe j J f → - let A := s.fund.mood - let B := u.next A - (fresh A B j x).prob (fun w => temp j < loadE A B w x) ≤ 1/4 := by - filter_upwards [h_large (100000*capA^2),hpoly (100000*capA^2) 230] with j h1 h2 M J s f x u ha hb A B - let T := fresh A B j x - have hA : 0 ≤ A := ha.capital.effects.nonneg - have hB : 0 ≤ B := hb.capital.effects.nonneg - have hT (w : Clutch j M) : 0 ≤ loadE A B w x := add_nonneg (form_nonneg hB _) - (Finset.sum_nonneg fun i _ => form_nonneg hA _) - let p (w : Clutch j M) : Prop := temp j < loadE A B w x - have he := upperTail T p (fun w => loadE A B w x) hT (temp j) fun _ hx => hx.le - let h := H j - have hh : 1 ≤ h := H_ge j - have hc := cap_ok; have hp : 0 < h := H_pos j - let d := height j; have hd : 0 < d := d_pos j - have ht : 0 < temp j := by - have he : 0 < capA := by linarith - change 0 < d/h^100/(capA*h) - positivity - let L := 2*plateErr j +100 - have hL : 0 ≤ L := by - change 0 ≤ 2*(d/h^400)+100; positivity - have Hx (s : Scene M) (hS : s.Safe j J f) : - (label s.fund.mood).Ex (form s.fund.mood) ≤ capA*h*L := - hS.capital.effects.energy.trans (mul_le_mul_of_nonneg_right hS.capital.under hL) - have hw : - temp j * T.prob p ≤ capA*h*L+h^120*(capA*h*L) := by - apply he.trans - unfold T; rw [batch_expected,clones_eq] - exact add_le_add (Hx (s.win u) hb) (mul_le_mul_of_nonneg_left (Hx s ha) (by positivity)) - change _ ≤ (1/4:ℝ) - refine le_of_mul_le_mul_left (hw.trans ?_) ht - have hP : 1 ≤ h^120 := one_le_pow₀ hh - have HC : 0 < capA := by linarith - change capA*h*L+h^120*(capA*h*L) ≤ d/h^100/(capA*h)*(1/4) - have h3 : 8*capA^2*h^222*L ≤ d := by - unfold L - change 8*capA^2*h^222*(2*(d/h^400)+100) ≤ d - have Hz : 100000*capA^2 ≤ h^178 := by - change 100000*capA^2 ≤ h at h1 - exact h1.trans (by simpa using pow_le_pow_right₀ hh (by omega : 1 ≤ 178)) - have Hi : 8*capA^2*h^222 * (2*(d/h^400)) ≤ d/2 := by - rw [show h^400=h^222*h^178 by ring] - field_simp - nlinarith - rw [mul_add] - change 100000*capA^2*h^230 ≤ d at h2 - have Hx := mul_le_mul_of_nonneg_left (pow_le_pow_right₀ hh (show 222 ≤ 230 by omega)) - (show 0 ≤ 100000*capA^2 by positivity) - nlinarith - calc - _ ≤ 2*(h^120*(capA*h*L)) := by - have h1 : 0 ≤ capA*h*L := by positivity - nlinarith [mul_le_mul_of_nonneg_right hP h1] - _ ≤ _ := by field_simp; nlinarith - -end Memory -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Road +import OAI.Analysis.FourierLLogL.Thermal.True + +/-! # Observation records in the comparison proof -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +structure Memory (M : ℕ) where + profile : Profile M + current : Grid M + +namespace Memory +def piece (A B : Form M) (w : Clutch j M) : Profile M := + (B,w.1) :: List.ofFn (fun i => (A,w.2 i)) + +def stash (m : Memory M) (A B : Form M) (w : Clutch j M) : + Memory M := ⟨piece A B w ++ m.profile,w.1⟩ + +structure Ready (m : Memory M) (j : ℕ) (c : Fund M) : Prop where + ordered : ∀ i ∈ m.profile, 0 ≤ i.1 ∧ i.1 ≤ c.mood + has : (c.mood, m.current)∈m.profile + num : m.profile.length = 1+c.uses*(1+clones j) + + +def temp (j : ℕ) := cushion j / (capA * H j) + +def loadE (A B : Form M) (w : Clutch j M) (x : Grid M) := + form B (w.1-x) + ∑ i, form A (w.2 i-x) + +lemma stash_ready (m : Memory M) {s : Scene M} (h : Ready m j s.fund) + (u : Update j M s.here) (hD : 0 ≤ u.changeForm) (hA : 0 ≤ s.fund.mood) (w : Clutch j M) : + Ready (m.stash s.fund.mood (u.next s.fund.mood) w) j (s.fund.advance s.here u) := by + let B := u.next s.fund.mood + have H : s.fund.mood ≤ B := + (le_add_of_nonneg_right (place_valid _ _)).trans (le_add_of_nonneg_right hD) + constructor + · intro t ht + simp only [stash,piece,List.mem_append,List.mem_cons,List.mem_ofFn] at ht + rcases ht with (he|he)|he + · subst t; exact ⟨hA.trans H,le_rfl⟩ + · obtain ⟨i,rfl⟩ := he; exact ⟨hA,H⟩ + obtain ⟨hl,hr⟩ := h.ordered t he + exact ⟨hl,hr.trans H⟩ + · simp [advance,stash,piece] + · simp only [stash,List.length_append,piece,List.length_cons,List.length_ofFn,h.num,advance] + ring + +lemma stash_length_bound (m : Memory M) {s : Scene M} (h : Ready m j s.fund) + (hg : Safe s j J f) : + ↑(m.profile.length) ≤ observationBudget capA j := by + rw [h.num,observationBudget] + push_cast; gcongr + exact hg.capital.under + +lemma sumE_piece (A B : Form M) (w : Clutch j M) (x : Grid M) : + sumE (piece A B w) x = loadE A B w x := by + simp only [sumE,piece,List.map_cons,List.map_ofFn,Function.comp_def, + List.sum_cons,List.sum_ofFn,loadE] + +lemma sumE_stash (m : Memory M) (A B : Form M) (w : Clutch j M) (x : Grid M) : + sumE (m.stash A B w).profile x = loadE A B w x + sumE m.profile x := by + rw [←sumE_piece] + simp only [stash,sumE,List.map_append,List.sum_append] + +lemma form_in_past (m : Memory M) (p : Form M × Grid M) + (h : ∀ i∈m.profile, 0 ≤ i.1) (hp : p ∈ m.profile) (x : Grid M) : + form p.1 (p.2-x) ≤ sumE m.profile x := by + suffices HH : ∀ l : Profile M, (∀ i∈l, 0 ≤ i.1) → p∈l → + form p.1 (p.2-x) ≤ sumE l x from HH _ h hp + intro l; induction l with + | nil => simp + | cons i l ih => + intro h h' + have he : 0 ≤ i.1 := h i List.mem_cons_self + have hh : ∀ i∈l, 0 ≤ i.1 := fun i hi => h i (List.mem_cons_of_mem _ hi) + simp only [sumE,List.map_cons,List.sum_cons] + rcases List.mem_cons.mp h' with rfl|H + · apply le_add_of_nonneg_right + apply List.sum_nonneg + intro i hi + obtain ⟨x,hx,rfl⟩ := List.mem_map.mp hi + exact form_nonneg (hh _ hx) _ + exact (ih hh H).trans (le_add_of_nonneg_left (form_nonneg he _)) + +def stashRate (A B : Form M) (j : ℕ) := + (side M*exp (F B)) * (side M*exp (F A))^(clones j) + +lemma multiply_stash (m : Memory M) (A B : Form M) (p : Node) (x : Grid M) + (w : Clutch j M) : + prefixE m.profile p x * fresh A B j x w = + stashRate A B j * prefixE (m.stash A B w).profile p x := by + have hi (A : Form M) (v) : + channel A x v = (side M*exp (F A)) * exp (-form A (v-x)) := by + rw [ch_K,K_eq,sub_eq_add_neg,exp_add,mul_assoc] + have H (v : Fin (clones j) → Grid M) : + ∏ i, channel A x (v i) = (side M*exp (F A))^(clones j) * + exp (-(∑ i, form A (v i-x))) := by + simp_rw [hi,Finset.prod_mul_distrib,← Real.exp_sum,Finset.sum_neg_distrib] + simp [mul_pow,exp_nat_mul] + unfold prefixE + split_ifs with hh + · obtain ⟨v,c⟩ := w + rw [fresh,tensorP_apply,pack_apply,H,hi,stashRate] + simp_rw [tune,sumE_stash,loadE,neg_add,Real.exp_add] + ring + · ring + +lemma rate_positive (j : ℕ) (A B : Form M) : 0 ≤ stashRate A B j := by + unfold stashRate; have := side_pos M; positivity + +lemma batch_expected (A B : Form M) (x : Grid M) (j : ℕ) : + (fresh A B j x).Ex (fun w => loadE A B w x) = + (label B).Ex (form B) + clones j * (label A).Ex (form A) := by + simp_rw [fresh,Ex_tensorP,loadE,Ex_add,Ex_const,Ex_finset_sum, + Ex_pack_proj (fun _ => channel A x) _ (fun z => form A (z-x))] + rw [ch_noise B (form B)] + simp_rw [ch_noise A (form A)] + simp + +lemma batch_scale : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, ∀ x : Grid M, + ∀ u : Update j M s.here, s.Safe j J f → (s.win u).Safe j J f → + let A := s.fund.mood + let B := u.next A + (fresh A B j x).prob (fun w => temp j < loadE A B w x) ≤ 1/4 := by + filter_upwards [h_large (100000*capA^2),hpoly (100000*capA^2) 230] with j h1 h2 M J s f x u ha hb A B + let T := fresh A B j x + have hA : 0 ≤ A := ha.capital.effects.nonneg + have hB : 0 ≤ B := hb.capital.effects.nonneg + have hT (w : Clutch j M) : 0 ≤ loadE A B w x := add_nonneg (form_nonneg hB _) + (Finset.sum_nonneg fun i _ => form_nonneg hA _) + let p (w : Clutch j M) : Prop := temp j < loadE A B w x + have he := upperTail T p (fun w => loadE A B w x) hT (temp j) fun _ hx => hx.le + let h := H j + have hh : 1 ≤ h := H_ge j + have hc := cap_ok; have hp : 0 < h := H_pos j + let d := height j; have hd : 0 < d := d_pos j + have ht : 0 < temp j := by + have he : 0 < capA := by linarith + change 0 < d/h^100/(capA*h) + positivity + let L := 2*plateErr j +100 + have hL : 0 ≤ L := by + change 0 ≤ 2*(d/h^400)+100; positivity + have Hx (s : Scene M) (hS : s.Safe j J f) : + (label s.fund.mood).Ex (form s.fund.mood) ≤ capA*h*L := + hS.capital.effects.energy.trans (mul_le_mul_of_nonneg_right hS.capital.under hL) + have hw : + temp j * T.prob p ≤ capA*h*L+h^120*(capA*h*L) := by + apply he.trans + unfold T; rw [batch_expected,clones_eq] + exact add_le_add (Hx (s.win u) hb) (mul_le_mul_of_nonneg_left (Hx s ha) (by positivity)) + change _ ≤ (1/4:ℝ) + refine le_of_mul_le_mul_left (hw.trans ?_) ht + have hP : 1 ≤ h^120 := one_le_pow₀ hh + have HC : 0 < capA := by linarith + change capA*h*L+h^120*(capA*h*L) ≤ d/h^100/(capA*h)*(1/4) + have h3 : 8*capA^2*h^222*L ≤ d := by + unfold L + change 8*capA^2*h^222*(2*(d/h^400)+100) ≤ d + have Hz : 100000*capA^2 ≤ h^178 := by + change 100000*capA^2 ≤ h at h1 + exact h1.trans (by simpa using pow_le_pow_right₀ hh (by omega : 1 ≤ 178)) + have Hi : 8*capA^2*h^222 * (2*(d/h^400)) ≤ d/2 := by + rw [show h^400=h^222*h^178 by ring] + field_simp + nlinarith + rw [mul_add] + change 100000*capA^2*h^230 ≤ d at h2 + have Hx := mul_le_mul_of_nonneg_left (pow_le_pow_right₀ hh (show 222 ≤ 230 by omega)) + (show 0 ≤ 100000*capA^2 by positivity) + nlinarith + calc + _ ≤ 2*(h^120*(capA*h*L)) := by + have h1 : 0 ≤ capA*h*L := by positivity + nlinarith [mul_le_mul_of_nonneg_right hP h1] + _ ≤ _ := by field_simp; nlinarith + +end Memory +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Play.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Play.lean index 1ca40673cb3328ab090a2dd7eef8e450a3a4c14a..f37d3e73b5ebc6bc3bea6324217c52bea04a6dcb 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Play.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Play.lean @@ -1,119 +1,119 @@ -import OAI.Analysis.FourierLLogL.Procedure.Capital - -/-! # The finite spatial and spectral play -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund -open scoped Classical -variable {M J j : ℕ} - -structure Scene (M : ℕ) where - here : Node - wage : Grid M → ℝ - fund : Fund M - live : Bool - norm : ℝ - -def cutoff (j : ℕ) : ℝ := ((j:ℝ)+1)^3 - -namespace Scene -def mass (s : Scene M) := s.here.avg s.wage -def actor (s : Scene M) : Law (Grid M) := - if h : ∃ P : Law (Grid M), PieceLaw s.here s.wage P then Classical.choose h - else uniform _ -def openHere (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) := - s.here.lev < J ∧ s.here.avg f ≤ cutoff j ∧ 0 < s.mass -def retire (s : Scene M) (j : ℕ) := - s.live=true ∧ (s.fund.breached j ∨ s.mass < exp (-height j)) -def enter (s : Scene M) := 2*s.norm < s.mass -def work (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) := - s.openHere j J f ∧ ¬ s.retire j ∧ ¬ s.enter -def settled (s : Scene M) (j : ℕ) := - s.live=true → ∀ i, i ≤ testIx j → - ¬ request s.fund.mood j s.here s.wage s.norm (side i) (kappa j (codeWeight capA) i) - -def reset (s : Scene M) := {s with norm := s.mass} -def out (s : Scene M) := {s with norm := 0, live := false} -def smaller (s : Scene M) (b : Bool) := {s with here := s.here.child b} -def win (s : Scene M) (u : Update j M s.here) : Scene M := - {s with wage := (Set.ofPred (u.take s.fund.mood)).indicator s.wage - norm := 0 - fund := s.fund.advance s.here u} -def lose (s : Scene M) (u : Update j M s.here) : Scene M := - {s with wage := (Set.ofPred fun x => ¬ u.take s.fund.mood x).indicator s.wage} - - - -def beginPlay (f : Grid M → ℝ) (b : Bool) : Scene M := - { here:=root - wage:=f - fund:=initial M - live:=b - norm:=0 } - -structure Safe (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) : Prop where - within : s.here.ok M - depth_ok : s.here.lev ≤ J - capital : s.fund.Ok s.here j - norm_ok : 0 ≤ s.norm - positive : 0 ≤ s.wage - sub : s.wage ≤ f - cut : s.here.avg f ≤ 2*cutoff j - -variable {f : Grid M → ℝ} {s : Scene M} - -lemma actor_def (hp : s.here.ok M) (hv : 0 ≤ s.wage) (he : 0 < s.mass) : - PieceLaw s.here s.wage s.actor := by - unfold actor; split - · exact Classical.choose_spec _ - · rename_i hh - exact (hh (make_piece _ hp hv he)).elim - -lemma Safe.mass_lower (hh : s.Safe j J f) : 0 ≤ s.mass := - avg_nonneg _ (fun x _ => hh.positive x) -lemma Safe.mass_upper (hh : s.Safe j J f) : - s.mass ≤ s.here.avg f := by - unfold mass; rw [avg_ex hh.within,avg_ex hh.within]; exact Ex_mono _ hh.sub -lemma Safe.budget (hh : s.Safe j J f) : s.mass ≤ 2*cutoff j := - hh.mass_upper.trans hh.cut - -end Scene - -open Scene - -inductive Play (j M : ℕ) : Scene M → Type -| rest (s : Scene M) : Play j M s -| freeze (s : Scene M) (t : Play j M s.out) : Play j M s -| ticket (s : Scene M) (t : Play j M s.reset) : Play j M s -| jump (s : Scene M) - (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) : Play j M s -| attempt (s : Scene M) (u : Update j M s.here) - (l : Play j M (s.lose u)) (r : Play j M (s.win u)) : Play j M s - -namespace Play -variable {s : Scene M} - -def Follows (J : ℕ) (f : Grid M → ℝ) : {s : Scene M} → Play j M s → Prop - | _, .rest s => s.Safe j J f ∧ ¬s.openHere j J f - | _, .ticket s t => s.Safe j J f ∧ s.openHere j J f ∧ ¬s.retire j ∧ - s.enter ∧ Follows J f t - | _, .freeze s t => s.Safe j J f ∧ s.openHere j J f ∧ s.retire j ∧ Follows J f t - | _, .jump s l r => s.Safe j J f ∧ s.work j J f ∧ s.settled j ∧ - Follows J f l ∧ Follows J f r - | _, .attempt s u l r => s.Safe j J f ∧ s.work j J f ∧ s.live=true ∧ - u.Good capA s.fund.mood s.actor ∧ Follows J f l ∧ Follows J f r - -variable {t : Play j M s} {f : Grid M → ℝ} -lemma first (h : t.Follows J f) : s.Safe j J f := by - cases t <;> exact h.1 - -end Play -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Capital + +/-! # The finite spatial and spectral play -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund +open scoped Classical +variable {M J j : ℕ} + +structure Scene (M : ℕ) where + here : Node + wage : Grid M → ℝ + fund : Fund M + live : Bool + norm : ℝ + +def cutoff (j : ℕ) : ℝ := ((j:ℝ)+1)^3 + +namespace Scene +def mass (s : Scene M) := s.here.avg s.wage +def actor (s : Scene M) : Law (Grid M) := + if h : ∃ P : Law (Grid M), PieceLaw s.here s.wage P then Classical.choose h + else uniform _ +def openHere (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) := + s.here.lev < J ∧ s.here.avg f ≤ cutoff j ∧ 0 < s.mass +def retire (s : Scene M) (j : ℕ) := + s.live=true ∧ (s.fund.breached j ∨ s.mass < exp (-height j)) +def enter (s : Scene M) := 2*s.norm < s.mass +def work (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) := + s.openHere j J f ∧ ¬ s.retire j ∧ ¬ s.enter +def settled (s : Scene M) (j : ℕ) := + s.live=true → ∀ i, i ≤ testIx j → + ¬ request s.fund.mood j s.here s.wage s.norm (side i) (kappa j (codeWeight capA) i) + +def reset (s : Scene M) := {s with norm := s.mass} +def out (s : Scene M) := {s with norm := 0, live := false} +def smaller (s : Scene M) (b : Bool) := {s with here := s.here.child b} +def win (s : Scene M) (u : Update j M s.here) : Scene M := + {s with wage := (Set.ofPred (u.take s.fund.mood)).indicator s.wage + norm := 0 + fund := s.fund.advance s.here u} +def lose (s : Scene M) (u : Update j M s.here) : Scene M := + {s with wage := (Set.ofPred fun x => ¬ u.take s.fund.mood x).indicator s.wage} + + + +def beginPlay (f : Grid M → ℝ) (b : Bool) : Scene M := + { here:=root + wage:=f + fund:=initial M + live:=b + norm:=0 } + +structure Safe (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) : Prop where + within : s.here.ok M + depth_ok : s.here.lev ≤ J + capital : s.fund.Ok s.here j + norm_ok : 0 ≤ s.norm + positive : 0 ≤ s.wage + sub : s.wage ≤ f + cut : s.here.avg f ≤ 2*cutoff j + +variable {f : Grid M → ℝ} {s : Scene M} + +lemma actor_def (hp : s.here.ok M) (hv : 0 ≤ s.wage) (he : 0 < s.mass) : + PieceLaw s.here s.wage s.actor := by + unfold actor; split + · exact Classical.choose_spec _ + · rename_i hh + exact (hh (make_piece _ hp hv he)).elim + +lemma Safe.mass_lower (hh : s.Safe j J f) : 0 ≤ s.mass := + avg_nonneg _ (fun x _ => hh.positive x) +lemma Safe.mass_upper (hh : s.Safe j J f) : + s.mass ≤ s.here.avg f := by + unfold mass; rw [avg_ex hh.within,avg_ex hh.within]; exact Ex_mono _ hh.sub +lemma Safe.budget (hh : s.Safe j J f) : s.mass ≤ 2*cutoff j := + hh.mass_upper.trans hh.cut + +end Scene + +open Scene + +inductive Play (j M : ℕ) : Scene M → Type +| rest (s : Scene M) : Play j M s +| freeze (s : Scene M) (t : Play j M s.out) : Play j M s +| ticket (s : Scene M) (t : Play j M s.reset) : Play j M s +| jump (s : Scene M) + (l : Play j M (s.smaller false)) (r : Play j M (s.smaller true)) : Play j M s +| attempt (s : Scene M) (u : Update j M s.here) + (l : Play j M (s.lose u)) (r : Play j M (s.win u)) : Play j M s + +namespace Play +variable {s : Scene M} + +def Follows (J : ℕ) (f : Grid M → ℝ) : {s : Scene M} → Play j M s → Prop + | _, .rest s => s.Safe j J f ∧ ¬s.openHere j J f + | _, .ticket s t => s.Safe j J f ∧ s.openHere j J f ∧ ¬s.retire j ∧ + s.enter ∧ Follows J f t + | _, .freeze s t => s.Safe j J f ∧ s.openHere j J f ∧ s.retire j ∧ Follows J f t + | _, .jump s l r => s.Safe j J f ∧ s.work j J f ∧ s.settled j ∧ + Follows J f l ∧ Follows J f r + | _, .attempt s u l r => s.Safe j J f ∧ s.work j J f ∧ s.live=true ∧ + u.Good capA s.fund.mood s.actor ∧ Follows J f l ∧ Follows J f r + +variable {t : Play j M s} {f : Grid M → ℝ} +lemma first (h : t.Follows J f) : s.Safe j J f := by + cases t <;> exact h.1 + +end Play +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Rare.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Rare.lean index 4976b6b060a5493cb0d73ce88de37c9eef1c8208..78ab58eb881046de8c46507f6de3ec423efdf7e0 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Rare.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Rare.lean @@ -1,165 +1,165 @@ -import OAI.Analysis.FourierLLogL.Procedure.Rating - -/-! # Weight of transfers to static processing -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Real Node Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory - -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -namespace Road -def prior (M : ℕ) : Memory M := ⟨[(0,0)],0⟩ -lemma prior_ready (j M : ℕ) : Ready (prior M) j (initial M) := by - constructor <;> simp [prior,initial] -lemma prior_tempered (j : ℕ) (f : Grid M → ℝ) (x) : - tempered (prior M) j (beginPlay f true) x := by - simp [prior,tempered,initial,beginPlay,sumE,form_zero] -lemma prior_weight (x : Grid M) : prefixE (prior M).profile root x = 1 := by - have h : x ∈ root.block M := (mem_group _ _).mp (by rw [group_top]; exact Finset.mem_univ _) - unfold prefixE; simp [h,tune,prior,sumE,form_zero] - -theorem rating_bound : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → - ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Road j M s, Trusted J f t → 1/H j ≤ s.mass → - t.expHike (fun l _ => exp (rating s l)) ≤ H j * exp (height j) := by - filter_upwards [stake_upper,stake_lower] with j hi h M J hJ f hF s t ht hm - have hv := rootSafe ht - have he := H_pos j - have hx : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/H j) hm - let m := prior M - have hu : expU (t.score m) ≤ 1 := by - have H := hi M J hJ _ f t ht m (prior_ready j M) - have HC : prefixE m.profile s.here = fun _ => 1 := funext prior_weight - simp_rw [HC,one_mul,expU_ex,Ex_const] at H - exact (expU_ex _).le.trans H - let F := fun x => t.score m x - have HX (x) : s.actor x ≤ (H j * exp (height j))* side M := by - have H := mass_actor (s:=s) hv.within hv.positive x - have HH : x∈s.here.block M := (mem_group _ _).mp (by - change x∈root.group M; rw [group_top]; exact Finset.mem_univ _) - rw [dice_apply hv.within,ite_eq_left HH,show s.here=root from rfl,len_top,inv_one,mul_one] at H - apply le_of_mul_le_mul_left _ hx - have h0 := side_pos M; have h1 := exp_pos (height j) - calc - _ ≤ side M*exp (height j) := - H.le.trans (mul_le_mul_of_nonneg_left (hF _) h0.le) - _ = (1/Thermal.H j)*(Thermal.H j*exp (height j)*side M) := by field_simp - _ ≤ _ := by gcongr - have HN : s.actor.Ex F ≤ H j* exp (height j) := calc - _ ≤ ∑ i, Thermal.H j * exp (height j) * side M * F i := - Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_right (HX _) (score_nonneg _ _ _) - _ = (H j * exp (height j)) * expU F := by - simp [expU,smul_eq_mul,Finset.mul_sum,mul_assoc] - _ ≤ _ := mul_le_of_le_one_right (by positivity) hu - unfold expHike - apply le_trans _ HN - exact Ex_mono_mem _ fun x hh => - h M J s f t ht m (prior_ready _ _) hx x hh (prior_tempered _ _ _) - -theorem rare : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → - ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → - let s := beginPlay f true - ∀ t : Road j M s, Trusted J f t → 1/H j ≤ s.mass → - s.actor.prob (fun x => (t.hike x).froze = true) ≤ 1/H j^10 := by - classical - let C := limitC - have hC : 1 ≤ C := by unfold C limitC; norm_num - filter_upwards [rating_bound,mem_budget,log_mass_budget,hpoly capA 1,hpoly (1000*C) 11] - with j hi hh h ht h' M J hJ f hx s t hT hm - let a := H j; let d := height j - have he : 1 ≤ a := H_ge _ - have hp : 0 < a := H_pos _ - have hd : 0 < d := d_pos _ - let Q := s.actor - have hM : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hm - let V (x : Grid M) := (t.hike x).membership - let W (x : Grid M) := max (-log (t.hike x).done.mass) 0 - let Y (x : Grid M) := rating s (t.hike x) - have HE (x : Grid M) (hv : 0 < Q x) : - (if (t.hike x).froze = true then (1:ℝ) else 0) ≤ - (V x^2+W x^2)*(d^2)⁻¹ + exp (-4*d)*exp (Y x) := by - split_ifs with H - · obtain ⟨h1,h2,h3,h4,h5,H6⟩ := final_sound hT hM hv - rcases (h2 H).2 with Hh|Hh - · by_cases HB : d ≤ V x - · apply le_trans (b:=(V x^2+W x^2)*(d^2)⁻¹) - · rw [← div_eq_mul_inv] - apply (le_div_iff₀ (by positivity)).mpr; nlinarith [sq_nonneg (W x)] - linarith [show 0 ≤ exp (-4*d)*exp (Y x) by positivity] - apply le_trans (b:= exp (-4*d)*exp (Y x)) - · rw [←exp_add] - apply one_le_exp - unfold Y rating - have hX := h1.capital.under - rw [pow_one] at ht - change capA*a ≤ d at ht - change 1000*d < _ at Hh - change -4*d+(((_-0)/8 - V x)-_) ≥ 0 - rw [show s.fund.uses=0 from rfl,Nat.cast_zero] at h5 - unfold a d at *; unfold V at *; linarith - exact le_add_of_nonneg_left (by positivity) - · apply le_trans (b:=(V x^2+W x^2)*(d^2)⁻¹) - · unfold W - let v := log (t.hike x).done.mass - have he' : d ≤ max (-v) 0 := by - apply le_trans _ (le_max_left _ _) - have hh := log_lt_log h3 Hh - rw [Real.log_exp] at hh - unfold v d; linarith - rw [← div_eq_mul_inv] - apply (le_div_iff₀ (by positivity)).mpr - change _ ≤ _+max (-v) 0 ^2 - nlinarith [sq_nonneg (V x)] - linarith [show 0 ≤ exp (-4*d)*exp (Y x) by positivity] - positivity - have hV : Q.Ex (fun x => V x^2) ≤ (C*a)^2 := (hh M J s f t hT hM).1 - have hW : Q.Ex (fun x => W x^2) ≤ (10*C*a)^2 := h M J s f t hT hm - have hY : Q.Ex (fun x => exp (Y x)) ≤ a * exp d := hi M J hJ f hx t hT hm - have hZ := Q.Ex_mono_mem HE - have HH : - (Q.Ex (fun x => V x^2) + Q.Ex (fun x => W x^2))*(d^2)⁻¹ ≤ (1/2)/a^10 := by - have Hp := add_le_add hV hW - refine (mul_le_mul_of_nonneg_right Hp (by positivity)).trans ?_ - change 1000*C*a^11 ≤ d at h' - have hv : 1000*C*a^6 ≤ d := - (calc - _ ≤ 1000*C*a^11 := by gcongr; norm_num - _ ≤ _ := h') - rw [← div_eq_mul_inv] - field_simp - nlinarith [show 0 ≤ C*a^6 by positivity, - pow_le_pow_left₀ (show 0 ≤ 1000*C*a^6 by positivity) hv 2] - have HY : exp (-4*d)*Q.Ex (fun x => exp (Y x)) ≤ (1/2)/a^10 := by - change 1000*C*a^11 ≤ d at h' - calc - _ ≤ exp (-4*d)*(a*exp d) := mul_le_mul_of_nonneg_left hY (exp_nonneg _) - _ = a*(exp (3*d))⁻¹ := by - rw [mul_left_comm,←exp_add,←Real.exp_neg]; congr 2; ring - _ ≤ _ := by - rw [← div_eq_mul_inv] - apply (div_le_div_iff₀ (exp_pos _) (by positivity)).mpr - have hz := add_one_le_exp (3*d) - nlinarith [show 0 ≤ a^11 by positivity] - unfold prob - - have HZ : Q.prob (fun x => (t.hike x).froze=true)= - Q.Ex (fun x => if (t.hike x).froze=true then 1 else 0) := by rw [prob_eq]; simp [Ex] - change Q.prob _ ≤ _ - rw [HZ] - rw [Ex_add,Ex_mul,Ex_add,Ex_lmul] at hZ - apply hZ.trans - apply (add_le_add HH HY).trans - exact le_of_eq (by unfold a; ring) - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Rating + +/-! # Weight of transfers to static processing -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Real Node Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory + +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +namespace Road +def prior (M : ℕ) : Memory M := ⟨[(0,0)],0⟩ +lemma prior_ready (j M : ℕ) : Ready (prior M) j (initial M) := by + constructor <;> simp [prior,initial] +lemma prior_tempered (j : ℕ) (f : Grid M → ℝ) (x) : + tempered (prior M) j (beginPlay f true) x := by + simp [prior,tempered,initial,beginPlay,sumE,form_zero] +lemma prior_weight (x : Grid M) : prefixE (prior M).profile root x = 1 := by + have h : x ∈ root.block M := (mem_group _ _).mp (by rw [group_top]; exact Finset.mem_univ _) + unfold prefixE; simp [h,tune,prior,sumE,form_zero] + +theorem rating_bound : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → + ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, Trusted J f t → 1/H j ≤ s.mass → + t.expHike (fun l _ => exp (rating s l)) ≤ H j * exp (height j) := by + filter_upwards [stake_upper,stake_lower] with j hi h M J hJ f hF s t ht hm + have hv := rootSafe ht + have he := H_pos j + have hx : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/H j) hm + let m := prior M + have hu : expU (t.score m) ≤ 1 := by + have H := hi M J hJ _ f t ht m (prior_ready j M) + have HC : prefixE m.profile s.here = fun _ => 1 := funext prior_weight + simp_rw [HC,one_mul,expU_ex,Ex_const] at H + exact (expU_ex _).le.trans H + let F := fun x => t.score m x + have HX (x) : s.actor x ≤ (H j * exp (height j))* side M := by + have H := mass_actor (s:=s) hv.within hv.positive x + have HH : x∈s.here.block M := (mem_group _ _).mp (by + change x∈root.group M; rw [group_top]; exact Finset.mem_univ _) + rw [dice_apply hv.within,ite_eq_left HH,show s.here=root from rfl,len_top,inv_one,mul_one] at H + apply le_of_mul_le_mul_left _ hx + have h0 := side_pos M; have h1 := exp_pos (height j) + calc + _ ≤ side M*exp (height j) := + H.le.trans (mul_le_mul_of_nonneg_left (hF _) h0.le) + _ = (1/Thermal.H j)*(Thermal.H j*exp (height j)*side M) := by field_simp + _ ≤ _ := by gcongr + have HN : s.actor.Ex F ≤ H j* exp (height j) := calc + _ ≤ ∑ i, Thermal.H j * exp (height j) * side M * F i := + Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_right (HX _) (score_nonneg _ _ _) + _ = (H j * exp (height j)) * expU F := by + simp [expU,smul_eq_mul,Finset.mul_sum,mul_assoc] + _ ≤ _ := mul_le_of_le_one_right (by positivity) hu + unfold expHike + apply le_trans _ HN + exact Ex_mono_mem _ fun x hh => + h M J s f t ht m (prior_ready _ _) hx x hh (prior_tempered _ _ _) + +theorem rare : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → + ∀ f : Grid M → ℝ, (∀ x, f x ≤ exp (height j)) → + let s := beginPlay f true + ∀ t : Road j M s, Trusted J f t → 1/H j ≤ s.mass → + s.actor.prob (fun x => (t.hike x).froze = true) ≤ 1/H j^10 := by + classical + let C := limitC + have hC : 1 ≤ C := by unfold C limitC; norm_num + filter_upwards [rating_bound,mem_budget,log_mass_budget,hpoly capA 1,hpoly (1000*C) 11] + with j hi hh h ht h' M J hJ f hx s t hT hm + let a := H j; let d := height j + have he : 1 ≤ a := H_ge _ + have hp : 0 < a := H_pos _ + have hd : 0 < d := d_pos _ + let Q := s.actor + have hM : 0 < s.mass := lt_of_lt_of_le (by positivity : 0<1/a) hm + let V (x : Grid M) := (t.hike x).membership + let W (x : Grid M) := max (-log (t.hike x).done.mass) 0 + let Y (x : Grid M) := rating s (t.hike x) + have HE (x : Grid M) (hv : 0 < Q x) : + (if (t.hike x).froze = true then (1:ℝ) else 0) ≤ + (V x^2+W x^2)*(d^2)⁻¹ + exp (-4*d)*exp (Y x) := by + split_ifs with H + · obtain ⟨h1,h2,h3,h4,h5,H6⟩ := final_sound hT hM hv + rcases (h2 H).2 with Hh|Hh + · by_cases HB : d ≤ V x + · apply le_trans (b:=(V x^2+W x^2)*(d^2)⁻¹) + · rw [← div_eq_mul_inv] + apply (le_div_iff₀ (by positivity)).mpr; nlinarith [sq_nonneg (W x)] + linarith [show 0 ≤ exp (-4*d)*exp (Y x) by positivity] + apply le_trans (b:= exp (-4*d)*exp (Y x)) + · rw [←exp_add] + apply one_le_exp + unfold Y rating + have hX := h1.capital.under + rw [pow_one] at ht + change capA*a ≤ d at ht + change 1000*d < _ at Hh + change -4*d+(((_-0)/8 - V x)-_) ≥ 0 + rw [show s.fund.uses=0 from rfl,Nat.cast_zero] at h5 + unfold a d at *; unfold V at *; linarith + exact le_add_of_nonneg_left (by positivity) + · apply le_trans (b:=(V x^2+W x^2)*(d^2)⁻¹) + · unfold W + let v := log (t.hike x).done.mass + have he' : d ≤ max (-v) 0 := by + apply le_trans _ (le_max_left _ _) + have hh := log_lt_log h3 Hh + rw [Real.log_exp] at hh + unfold v d; linarith + rw [← div_eq_mul_inv] + apply (le_div_iff₀ (by positivity)).mpr + change _ ≤ _+max (-v) 0 ^2 + nlinarith [sq_nonneg (V x)] + linarith [show 0 ≤ exp (-4*d)*exp (Y x) by positivity] + positivity + have hV : Q.Ex (fun x => V x^2) ≤ (C*a)^2 := (hh M J s f t hT hM).1 + have hW : Q.Ex (fun x => W x^2) ≤ (10*C*a)^2 := h M J s f t hT hm + have hY : Q.Ex (fun x => exp (Y x)) ≤ a * exp d := hi M J hJ f hx t hT hm + have hZ := Q.Ex_mono_mem HE + have HH : + (Q.Ex (fun x => V x^2) + Q.Ex (fun x => W x^2))*(d^2)⁻¹ ≤ (1/2)/a^10 := by + have Hp := add_le_add hV hW + refine (mul_le_mul_of_nonneg_right Hp (by positivity)).trans ?_ + change 1000*C*a^11 ≤ d at h' + have hv : 1000*C*a^6 ≤ d := + (calc + _ ≤ 1000*C*a^11 := by gcongr; norm_num + _ ≤ _ := h') + rw [← div_eq_mul_inv] + field_simp + nlinarith [show 0 ≤ C*a^6 by positivity, + pow_le_pow_left₀ (show 0 ≤ 1000*C*a^6 by positivity) hv 2] + have HY : exp (-4*d)*Q.Ex (fun x => exp (Y x)) ≤ (1/2)/a^10 := by + change 1000*C*a^11 ≤ d at h' + calc + _ ≤ exp (-4*d)*(a*exp d) := mul_le_mul_of_nonneg_left hY (exp_nonneg _) + _ = a*(exp (3*d))⁻¹ := by + rw [mul_left_comm,←exp_add,←Real.exp_neg]; congr 2; ring + _ ≤ _ := by + rw [← div_eq_mul_inv] + apply (div_le_div_iff₀ (exp_pos _) (by positivity)).mpr + have hz := add_one_le_exp (3*d) + nlinarith [show 0 ≤ a^11 by positivity] + unfold prob + + have HZ : Q.prob (fun x => (t.hike x).froze=true)= + Q.Ex (fun x => if (t.hike x).froze=true then 1 else 0) := by rw [prob_eq]; simp [Ex] + change Q.prob _ ≤ _ + rw [HZ] + rw [Ex_add,Ex_mul,Ex_add,Ex_lmul] at hZ + apply hZ.trans + apply (add_le_add HH HY).trans + exact le_of_eq (by unfold a; ring) + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Rating.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Rating.lean index caf54ed2d526ea3608cd6f813cb6c752f2baa294..081b4be56a2e53c0def7d863e363b19f9d07d08e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Rating.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Rating.lean @@ -1,200 +1,200 @@ -import OAI.Analysis.FourierLLogL.Procedure.Score - -/-! # The score includes sizeable payments even conditional on the actual input -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -namespace Road - -/-- Require each batch's total own-energy below temp; at success the available past -temp is expanded. -/ -def tempered (m : Memory M) (j : ℕ) (s : Scene M) (x : Grid M) := - sumE m.profile x ≤ s.fund.uses* temp j - -lemma temperature (m : Memory M) {f : Grid M → ℝ} (s : Scene M) - (h : s.Safe j J f) (x : Grid M) (hh : tempered m j s x) : - sumE m.profile x ≤ cushion j := by - have hi : 0 < capA*H j := mul_pos (by norm_num [capA]) (H_pos _) - have hp : 0 ≤ cushion j := by unfold cushion; have := d_pos j; positivity - calc - _ ≤ s.fund.uses*temp j := hh - _ ≤ (capA*H j)*temp j := mul_le_mul_of_nonneg_right h.capital.under (div_nonneg hp hi.le) - _ = cushion j := by unfold temp; rw [mul_comm,div_mul_cancel₀ _ hi.ne'] - -private lemma good_payment : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ m : Memory M, ∀ x : Grid M, - ∀ f : Grid M → ℝ, ∀ u : Update j M s.here, Safe s j J f → Safe (s.win u) j J f → - u.Good capA s.fund.mood s.actor → - Ready m j s.fund → x∈s.here.block M → u.take s.fund.mood x → tempered m j s x → - 1/2 ≤ (fresh s.fund.mood (u.next s.fund.mood) j x).prob (fun w => gate m s u w ∧ - tempered (stash m s.fund.mood (u.next s.fund.mood) w) j (s.win u) x) := by - filter_upwards [Memory.batch_scale,Update.quiet_scale capA cap_ok,h_large 16] - with j hj hl hp M J s m x f u hs hw hu hm hx he ht - let A := s.fund.mood; let B := u.next A - let Q := fresh A B j x - let E (w : Clutch j M) := temp j < loadE A B w x - let L (w : Clutch j M) := u.regular=true ∧ Update.quiet u w - have hE : Q.prob E ≤ 1/4 := hj M J s f x u hs hw - have hL : Q.prob L ≤ 1/4 := by - by_cases h : u.regular=true - · have H : L= u.quiet := by simp [L,h] - rw [H] - refine le_trans (hl _ _ _ _ u hu h _ he) ?_ - have hh : Thermal.H j ≤ Thermal.H j ^40 := by - have ht := pow_le_pow_right₀ (H_ge j) (show 1 ≤ 40 by omega) - simpa using ht - field_simp; linarith - unfold prob L; simp [h, Ex_const] - let p (w : Clutch j M) := gate m s u w ∧ tempered (stash m A B w) j (s.win u) x - have HA := hs.capital.effects.nonneg - have hStash w : Ready (stash m A B w) j (s.win u).fund := stash_ready m hm u hu.positive HA _ - have hR : ∀ i∈m.profile, 0 ≤ i.1 := fun i hi => (hm.ordered i hi).1 - have hU (w) (hh : ¬ E w ∧ ¬ L w) : p w := by - let m' := stash m A B w - have he' : tempered m' j (s.win u) x := by - unfold tempered at * - have Hg : loadE A B w x ≤ temp j := not_lt.mp hh.1 - unfold m'; rw [sumE_stash] - change _+_ ≤ ((s.fund.uses+1:ℕ):ℝ)*_ - push_cast; linarith - refine ⟨?_,he'⟩ - unfold gate; split - case isTrue h1 => - have HX := temperature m s hs x ht - refine ⟨⟨x,hx,HX⟩,Update.consistent_triangle u HA (y:=x) ?_ ?_ - (fun H => hh.2 ⟨h1,H⟩)⟩ - · apply le_trans _ HX - exact form_in_past m (A,m.current) hR hm.has x - intro i - apply le_trans _ (temperature m' (s.win u) hw x he') - have hz := hStash w - apply form_in_past m' (A,w.2 i) (fun i hi => (hz.ordered i hi).1) ?_ x - simp only [m',stash,piece,List.mem_append,List.mem_cons,List.mem_ofFn] - exact Or.inl (Or.inr ⟨i,rfl⟩) - trivial - change 1/2 ≤ Q.prob p - have H : Q.prob (fun w => ¬ p w) ≤ 1/4+1/4 := by - apply le_trans (b:=Q.prob (fun w => E w ∨ L w)) - · exact Q.prob_mono _ _ fun w hi => by by_contra! ht; exact hi (hU _ ht) - apply le_trans (prob_union Q E L) (add_le_add hE hL) - rw [prob_compl] at H; linarith - -def rating (s : Scene M) (l : Journal j M) : ℝ := - (l.done.fund.bank-s.fund.bank)/8 - l.membership - l.fee count - -lemma stake_lower : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, - ∀ t : Road j M s, Trusted J f t → ∀ (m : Memory M), Ready m j s.fund → - 0 < s.mass → ∀ x : Grid M, 0 < s.actor x → tempered m j s x → - exp (rating s (t.hike x)) ≤ t.score m x := by - filter_upwards [good_payment] with j hj M J s f t h m ht hm x hx he - induction t generalizing m with - | finish s b => simp [hike,rating,membership,fee,score] - | push s v t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - have hS : (s.pace v).fund=s.fund := by cases v <;> rfl - have Hi := ih hd m (by rw [hS]; exact ht) (by rw [pace_mass]; exact hm) - (by rw [pace_actor]; exact hx) (by unfold tempered;rw [hS]; exact he) - rw [show rating s ((push s v t).hike x) = rating (s.pace v) (t.hike x) by - simp [rating,hike,prepend,membership,fee,count,hS,memFee,failFee,winFee]] - exact Hi - | fork s v t ih => - have hB := branchTrusted h - obtain ⟨hx1,hx2⟩ := Fork.branch_pos hB (x:=x) (by rw [v.source]; exact hx) - have hx3 := (true_mass hB).mp hx1 - obtain ⟨ha,hb,hc,hd⟩ := h - cases v with - | move => - let b := (step s).bit x - have Hi := ih b (hd b) m ht hx3 hx2 he - change exp (rating s (((t b).hike x).prepend (.march s .move b))) ≤ _ - apply le_trans _ Hi - dsimp only [rating] - simp [prepend,membership,fee, count, memFee,failFee,winFee,Turn.fork,step,smaller] - | ask u => - let v : Turn j s := .ask u - let T := toss s u - let A := s.fund.mood - let B := u.next A - let P := T.probs - let Z := fresh A B j x - have Hx (b : Bool) (hv : v.fork.bit x=b) : - exp (rating s ((fork s v t).hike x)) = - P b * exp (rating (v.fork.dst b) ((t b).hike x) + (if b then u.bill/8-1 else 0)) := by - change 0 < P (v.fork.bit x) at hx1 - rw [hv] at hx1 - let w := (t b).hike x - have Hz (b : Bool) : memFee (.march s v b) = -log (P b) := by - cases b <;> simp only [memFee,v,winFee,failFee,surprise,zero_add,add_zero] <;> rfl - have Heq (q : Journal j M) (b) : - rating s (q.prepend (.march s v b)) = - log (P b)+ (rating (v.fork.dst b) q + if b then u.bill/8-1 else 0) := by - unfold rating membership - rw [fee_pre,fee_pre,Hz] - cases b - · change (q.done.fund.bank-s.fund.bank)/8 - (- log (P false) + _) - (0+q.fee _) = - log (P false)+((q.done.fund.bank-s.fund.bank)/8-_-_+0) - ring - · change (q.done.fund.bank-s.fund.bank)/8 - (- log (P true) + _) - (1+q.fee _) = - log (P true)+((q.done.fund.bank-(s.fund.bank+u.bill))/8-_-_+(u.bill/8-1)) - ring - have H1 : - (fork s v t).hike x = w.prepend (.march s v b) := by - unfold w - let F (b : Bool) := ((t b).hike x).prepend (.march s v b) - change F (v.fork.bit x)=F b - exact congrArg _ hv - rw [H1,Heq,exp_add,exp_log hx1] - by_cases H : u.take A x - · have hb' : v.fork.bit x=true := decide_eq_true H - have hu' : u.Good capA A s.actor := hc.2 - have HY : x∈s.here.block M := - piece_support ha.within (actor_def ha.within ha.positive hm) _ hx - let Q (w : Clutch j M) := gate m s u w ∧ tempered (stash m A B w) j (s.win u) x - have HHq : 1/2 ≤ Z.prob Q := hj M J s m x f u ha (rootSafe (hd true)) hu' ht HY H he - let d := exp (rating (s.win u) ((t true).hike x)) - let W (w : Clutch j M) := stash m A B w - have HN (w) (hw : Q w) : d ≤ score (t true) (W w) x := by - change 0 < (v.fork.dst (v.fork.bit x)).mass at hx3 - change 0 < (v.fork.dst (v.fork.bit x)).actor x at hx2 - rw [hb'] at hx3 hx2 - exact ih _ (hd _) _ (stash_ready m ht u hu'.positive ha.capital.effects.nonneg w) - hx3 hx2 hw.2 - let M' (w : Clutch j M) := if gate m s u w then score (t true) (W w) x else 0 - have HE' : d/2 ≤ Z.Ex M' := by - have hpos (w) : 0 ≤ M' w := by unfold M'; split_ifs; exact score_nonneg _ _ _; norm_num - have Ht := Z.upperTail Q M' hpos d (by intro i hi; unfold M'; rw [ite_eq_left hi.1]; exact HN i hi) - have hh : 0 ≤ d := exp_nonneg _ - nlinarith - have Heq : exp (-1) ≤ 1/2 := by rw [exp_neg,← one_div]; exact one_div_le_one_div_of_le (by norm_num) exp_one_gt_two.le - rw [Hx _ hb'] - change P true * exp (rating (s.win u) ((t true).hike x) + (if _ then _ else _)) ≤ - (if u.take A x then P true * (exp (u.bill/8)*Z.Ex M') else _) - rw [ite_eq_left rfl,ite_eq_left H,exp_add,sub_eq_add_neg,exp_add] - apply mul_le_mul_of_nonneg_left _ (P.nonneg _) - change d * _ ≤ _ - rw [mul_left_comm] - have hh : d * exp (-1) ≤ Z.Ex M' := by have hh : 0 ≤ d := exp_nonneg _; nlinarith - exact mul_le_mul_of_nonneg_left hh (exp_nonneg _) - have hb' : v.fork.bit x=false := decide_eq_false H - change 0 < (v.fork.dst (v.fork.bit x)).mass at hx3 - change 0 < (v.fork.dst (v.fork.bit x)).actor x at hx2 - rw [hb'] at hx3 hx2 - have HX := ih false (hd false) m ht hx3 hx2 he - rw [Hx _ hb'] - change P false * _ ≤ (if u.take A x then _ else P false * score (t false) m x) - rw [ite_eq_right H,ite_eq_right (by decide),add_zero] - exact mul_le_mul_of_nonneg_left HX (P.nonneg _) - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Score + +/-! # The score includes sizeable payments even conditional on the actual input -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +namespace Road + +/-- Require each batch's total own-energy below temp; at success the available past +temp is expanded. -/ +def tempered (m : Memory M) (j : ℕ) (s : Scene M) (x : Grid M) := + sumE m.profile x ≤ s.fund.uses* temp j + +lemma temperature (m : Memory M) {f : Grid M → ℝ} (s : Scene M) + (h : s.Safe j J f) (x : Grid M) (hh : tempered m j s x) : + sumE m.profile x ≤ cushion j := by + have hi : 0 < capA*H j := mul_pos (by norm_num [capA]) (H_pos _) + have hp : 0 ≤ cushion j := by unfold cushion; have := d_pos j; positivity + calc + _ ≤ s.fund.uses*temp j := hh + _ ≤ (capA*H j)*temp j := mul_le_mul_of_nonneg_right h.capital.under (div_nonneg hp hi.le) + _ = cushion j := by unfold temp; rw [mul_comm,div_mul_cancel₀ _ hi.ne'] + +private lemma good_payment : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ m : Memory M, ∀ x : Grid M, + ∀ f : Grid M → ℝ, ∀ u : Update j M s.here, Safe s j J f → Safe (s.win u) j J f → + u.Good capA s.fund.mood s.actor → + Ready m j s.fund → x∈s.here.block M → u.take s.fund.mood x → tempered m j s x → + 1/2 ≤ (fresh s.fund.mood (u.next s.fund.mood) j x).prob (fun w => gate m s u w ∧ + tempered (stash m s.fund.mood (u.next s.fund.mood) w) j (s.win u) x) := by + filter_upwards [Memory.batch_scale,Update.quiet_scale capA cap_ok,h_large 16] + with j hj hl hp M J s m x f u hs hw hu hm hx he ht + let A := s.fund.mood; let B := u.next A + let Q := fresh A B j x + let E (w : Clutch j M) := temp j < loadE A B w x + let L (w : Clutch j M) := u.regular=true ∧ Update.quiet u w + have hE : Q.prob E ≤ 1/4 := hj M J s f x u hs hw + have hL : Q.prob L ≤ 1/4 := by + by_cases h : u.regular=true + · have H : L= u.quiet := by simp [L,h] + rw [H] + refine le_trans (hl _ _ _ _ u hu h _ he) ?_ + have hh : Thermal.H j ≤ Thermal.H j ^40 := by + have ht := pow_le_pow_right₀ (H_ge j) (show 1 ≤ 40 by omega) + simpa using ht + field_simp; linarith + unfold prob L; simp [h, Ex_const] + let p (w : Clutch j M) := gate m s u w ∧ tempered (stash m A B w) j (s.win u) x + have HA := hs.capital.effects.nonneg + have hStash w : Ready (stash m A B w) j (s.win u).fund := stash_ready m hm u hu.positive HA _ + have hR : ∀ i∈m.profile, 0 ≤ i.1 := fun i hi => (hm.ordered i hi).1 + have hU (w) (hh : ¬ E w ∧ ¬ L w) : p w := by + let m' := stash m A B w + have he' : tempered m' j (s.win u) x := by + unfold tempered at * + have Hg : loadE A B w x ≤ temp j := not_lt.mp hh.1 + unfold m'; rw [sumE_stash] + change _+_ ≤ ((s.fund.uses+1:ℕ):ℝ)*_ + push_cast; linarith + refine ⟨?_,he'⟩ + unfold gate; split + case isTrue h1 => + have HX := temperature m s hs x ht + refine ⟨⟨x,hx,HX⟩,Update.consistent_triangle u HA (y:=x) ?_ ?_ + (fun H => hh.2 ⟨h1,H⟩)⟩ + · apply le_trans _ HX + exact form_in_past m (A,m.current) hR hm.has x + intro i + apply le_trans _ (temperature m' (s.win u) hw x he') + have hz := hStash w + apply form_in_past m' (A,w.2 i) (fun i hi => (hz.ordered i hi).1) ?_ x + simp only [m',stash,piece,List.mem_append,List.mem_cons,List.mem_ofFn] + exact Or.inl (Or.inr ⟨i,rfl⟩) + trivial + change 1/2 ≤ Q.prob p + have H : Q.prob (fun w => ¬ p w) ≤ 1/4+1/4 := by + apply le_trans (b:=Q.prob (fun w => E w ∨ L w)) + · exact Q.prob_mono _ _ fun w hi => by by_contra! ht; exact hi (hU _ ht) + apply le_trans (prob_union Q E L) (add_le_add hE hL) + rw [prob_compl] at H; linarith + +def rating (s : Scene M) (l : Journal j M) : ℝ := + (l.done.fund.bank-s.fund.bank)/8 - l.membership - l.fee count + +lemma stake_lower : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ s : Scene M, ∀ f : Grid M → ℝ, + ∀ t : Road j M s, Trusted J f t → ∀ (m : Memory M), Ready m j s.fund → + 0 < s.mass → ∀ x : Grid M, 0 < s.actor x → tempered m j s x → + exp (rating s (t.hike x)) ≤ t.score m x := by + filter_upwards [good_payment] with j hj M J s f t h m ht hm x hx he + induction t generalizing m with + | finish s b => simp [hike,rating,membership,fee,score] + | push s v t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + have hS : (s.pace v).fund=s.fund := by cases v <;> rfl + have Hi := ih hd m (by rw [hS]; exact ht) (by rw [pace_mass]; exact hm) + (by rw [pace_actor]; exact hx) (by unfold tempered;rw [hS]; exact he) + rw [show rating s ((push s v t).hike x) = rating (s.pace v) (t.hike x) by + simp [rating,hike,prepend,membership,fee,count,hS,memFee,failFee,winFee]] + exact Hi + | fork s v t ih => + have hB := branchTrusted h + obtain ⟨hx1,hx2⟩ := Fork.branch_pos hB (x:=x) (by rw [v.source]; exact hx) + have hx3 := (true_mass hB).mp hx1 + obtain ⟨ha,hb,hc,hd⟩ := h + cases v with + | move => + let b := (step s).bit x + have Hi := ih b (hd b) m ht hx3 hx2 he + change exp (rating s (((t b).hike x).prepend (.march s .move b))) ≤ _ + apply le_trans _ Hi + dsimp only [rating] + simp [prepend,membership,fee, count, memFee,failFee,winFee,Turn.fork,step,smaller] + | ask u => + let v : Turn j s := .ask u + let T := toss s u + let A := s.fund.mood + let B := u.next A + let P := T.probs + let Z := fresh A B j x + have Hx (b : Bool) (hv : v.fork.bit x=b) : + exp (rating s ((fork s v t).hike x)) = + P b * exp (rating (v.fork.dst b) ((t b).hike x) + (if b then u.bill/8-1 else 0)) := by + change 0 < P (v.fork.bit x) at hx1 + rw [hv] at hx1 + let w := (t b).hike x + have Hz (b : Bool) : memFee (.march s v b) = -log (P b) := by + cases b <;> simp only [memFee,v,winFee,failFee,surprise,zero_add,add_zero] <;> rfl + have Heq (q : Journal j M) (b) : + rating s (q.prepend (.march s v b)) = + log (P b)+ (rating (v.fork.dst b) q + if b then u.bill/8-1 else 0) := by + unfold rating membership + rw [fee_pre,fee_pre,Hz] + cases b + · change (q.done.fund.bank-s.fund.bank)/8 - (- log (P false) + _) - (0+q.fee _) = + log (P false)+((q.done.fund.bank-s.fund.bank)/8-_-_+0) + ring + · change (q.done.fund.bank-s.fund.bank)/8 - (- log (P true) + _) - (1+q.fee _) = + log (P true)+((q.done.fund.bank-(s.fund.bank+u.bill))/8-_-_+(u.bill/8-1)) + ring + have H1 : + (fork s v t).hike x = w.prepend (.march s v b) := by + unfold w + let F (b : Bool) := ((t b).hike x).prepend (.march s v b) + change F (v.fork.bit x)=F b + exact congrArg _ hv + rw [H1,Heq,exp_add,exp_log hx1] + by_cases H : u.take A x + · have hb' : v.fork.bit x=true := decide_eq_true H + have hu' : u.Good capA A s.actor := hc.2 + have HY : x∈s.here.block M := + piece_support ha.within (actor_def ha.within ha.positive hm) _ hx + let Q (w : Clutch j M) := gate m s u w ∧ tempered (stash m A B w) j (s.win u) x + have HHq : 1/2 ≤ Z.prob Q := hj M J s m x f u ha (rootSafe (hd true)) hu' ht HY H he + let d := exp (rating (s.win u) ((t true).hike x)) + let W (w : Clutch j M) := stash m A B w + have HN (w) (hw : Q w) : d ≤ score (t true) (W w) x := by + change 0 < (v.fork.dst (v.fork.bit x)).mass at hx3 + change 0 < (v.fork.dst (v.fork.bit x)).actor x at hx2 + rw [hb'] at hx3 hx2 + exact ih _ (hd _) _ (stash_ready m ht u hu'.positive ha.capital.effects.nonneg w) + hx3 hx2 hw.2 + let M' (w : Clutch j M) := if gate m s u w then score (t true) (W w) x else 0 + have HE' : d/2 ≤ Z.Ex M' := by + have hpos (w) : 0 ≤ M' w := by unfold M'; split_ifs; exact score_nonneg _ _ _; norm_num + have Ht := Z.upperTail Q M' hpos d (by intro i hi; unfold M'; rw [ite_eq_left hi.1]; exact HN i hi) + have hh : 0 ≤ d := exp_nonneg _ + nlinarith + have Heq : exp (-1) ≤ 1/2 := by rw [exp_neg,← one_div]; exact one_div_le_one_div_of_le (by norm_num) exp_one_gt_two.le + rw [Hx _ hb'] + change P true * exp (rating (s.win u) ((t true).hike x) + (if _ then _ else _)) ≤ + (if u.take A x then P true * (exp (u.bill/8)*Z.Ex M') else _) + rw [ite_eq_left rfl,ite_eq_left H,exp_add,sub_eq_add_neg,exp_add] + apply mul_le_mul_of_nonneg_left _ (P.nonneg _) + change d * _ ≤ _ + rw [mul_left_comm] + have hh : d * exp (-1) ≤ Z.Ex M' := by have hh : 0 ≤ d := exp_nonneg _; nlinarith + exact mul_le_mul_of_nonneg_left hh (exp_nonneg _) + have hb' : v.fork.bit x=false := decide_eq_false H + change 0 < (v.fork.dst (v.fork.bit x)).mass at hx3 + change 0 < (v.fork.dst (v.fork.bit x)).actor x at hx2 + rw [hb'] at hx3 hx2 + have HX := ih false (hd false) m ht hx3 hx2 he + rw [Hx _ hb'] + change P false * _ ≤ (if u.take A x then _ else P false * score (t false) m x) + rw [ite_eq_right H,ite_eq_right (by decide),add_zero] + exact mul_le_mul_of_nonneg_left HX (P.nonneg _) + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Road.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Road.lean index 4cdff05c81a949fa1a11571d1819fdb56ba1d89a..a033f437c590861ca82aa61dcf64e4f3ce46cca4 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Road.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Road.lean @@ -1,177 +1,177 @@ -import OAI.Analysis.FourierLLogL.Procedure.Fork - -/-! # The road seen from the input. This representation can truncate the play at transfers. -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -/-- Unary pace: entering the static system, or starting a new plateau -/ -inductive Pace -| renew | cross deriving DecidableEq - -def Scene.pace (s : Scene M) (v : Pace) := - match v with | .renew => s.reset | .cross => s.out - -lemma pace_mass (s : Scene M) (v : Pace) : (s.pace v).mass=s.mass := by cases v <;> rfl -lemma pace_actor (s : Scene M) (v : Pace) : (s.pace v).actor=s.actor := by cases v <;> rfl - -inductive Turn (j : ℕ) (s : Scene M) : Type -| ask (u : Update j M s.here) : Turn j s -| move : Turn j s - -def Turn.fork (v : Turn j s) := - match v with | .ask u => toss s u | .move => step s - -lemma Turn.source (v : Turn j s) : v.fork.src=s := by cases v <;> rfl - -inductive Road (j M : ℕ) : Scene M → Type -| finish (s : Scene M) (transferred : Bool) : Road j M s -| push (s : Scene M) (v : Pace) (t : Road j M (s.pace v)) : Road j M s -| fork (s : Scene M) (v : Turn j s) (t : (b : Bool) → Road j M (v.fork.dst b)) : Road j M s - -namespace Road - -/-- `extend` means continuing into the static system. -/ -def trace (throughCold : Bool) {s : Scene M} (t : Play j M s) : Road j M s := - match t with - | .rest s => .finish s false - | .freeze s t => if throughCold then .push s .cross (trace throughCold t) else .finish s true - | .ticket s t => .push s .renew (trace throughCold t) - | .jump s l r => .fork s .move fun b => - match b with | false => trace throughCold l | true => trace throughCold r - | .attempt s u l r => .fork s (.ask u) fun b => - match b with | false => trace throughCold l | true => trace throughCold r - -variable {f : Grid M → ℝ} - -def pace_ok (s : Scene M) (v : Pace) (j : ℕ) := - match v with | .renew => ¬ s.retire j ∧ s.enter | .cross => s.retire j -def turn_ok {s : Scene M} (v : Turn j s) := - match v with - | .ask u => s.live=true ∧ u.Good capA s.fund.mood s.actor - | .move => s.settled j - -/-- f and J ambient original data. -/ -def Trusted (J : ℕ) (f : Grid M → ℝ) {s : Scene M} (r : Road j M s) : Prop := - match r with - | .finish s b => s.Safe j J f ∧ (if b then s.openHere j J f ∧ s.retire j else ¬ s.openHere j J f) - | .push s v t => s.Safe j J f ∧ s.openHere j J f ∧ pace_ok s v j ∧ Trusted J f t - | .fork s v t => s.Safe j J f ∧ s.work j J f ∧ turn_ok v ∧ ∀ b, Trusted J f (t b) - -lemma traced {s : Scene M} (t : Play j M s) (b : Bool) (h : t.Follows J f) : - (trace b t).Trusted J f := by - induction t with - | rest s => exact ⟨h.1,h.2⟩ - | freeze s t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - cases b; exact ⟨ha,hb,hc⟩; exact ⟨ha,hb,hc,ih hd⟩ - | ticket s t ih => - obtain ⟨ha,hb,hc,hd,he⟩ := h - exact ⟨ha,hb,⟨hc,hd⟩,ih he⟩ - | jump s l r il ir => - obtain ⟨ha,hb,hc,hd,he⟩ := h - refine ⟨ha,hb,hc,?_⟩ - intro b; cases b; exact il hd; exact ir he - | attempt s u l r il ir => - obtain ⟨ha,hb,hc,hd,he,hf⟩ := h - refine ⟨ha,hb,⟨hc,hd⟩,?_⟩ - intro b; cases b; exact il he; exact ir hf - -lemma rootSafe {t : Road j M s} (h : t.Trusted J f) : Safe s j J f := by - cases t <;> exact h.1 - -lemma branchTrusted {v : Turn j s} {t : ∀ b, Road j M (v.fork.dst b)} - (h : (Road.fork s v t).Trusted J f) : v.fork.TrueLaw := by - obtain ⟨ha,hb,hc,hd⟩ := h - cases v with - | ask u => exact toss_true _ ha.within ha.positive hb.1.2.2 - | move => - have Hh := (rootSafe (hd false)).within.2 - exact step_true ha.within.1 Hh ha.positive hb.1.2.2 - -end Road - -/-- One edge actually seen. -/ -inductive Event (j M : ℕ) -| march (s : Scene M) (v : Turn j s) (b : Bool) : Event j M -| pace (s : Scene M) (v : Pace) : Event j M - -def Event.dest (p : Event j M) : Scene M := - match p with | .pace s v => s.pace v | .march _ v b => v.fork.dst b -def Event.src (p : Event j M) : Scene M := - match p with | .pace s _ | .march s _ _ => s - -structure Journal (j M : ℕ) where - done : Scene M - froze : Bool - events : List (Event j M) - -namespace Journal -def prepend (e : Event j M) (z : Journal j M) := {z with events:=e::z.events} -def fee (z : Journal j M) (f : Event j M → ℝ) := (z.events.map f).sum - -lemma fee_pre (e : Event j M) (z : Journal j M) (f : Event j M → ℝ) : - (z.prepend e).fee f = f e + z.fee f := by simp [prepend,fee] - -end Journal - -namespace Road -open Event Journal - -def hike {s : Scene M} (t : Road j M s) (x : Grid M) : Journal j M := - match t with - | .finish s b => ⟨s,b,[]⟩ - | .push s v t => (t.hike x).prepend (Event.pace s v) - | .fork s v t => - let b := v.fork.bit x - ((t b).hike x).prepend (Event.march s v b) - -/-- True induction along the observed path. -/ -lemma travel {f : Grid M → ℝ} {good : Scene M → Journal j M → Prop} - (he : ∀ s b, Safe s j J f → (if b then s.openHere j J f ∧ s.retire j else ¬ s.openHere j J f) → - 0 < s.mass → good s ⟨s,b,[]⟩) - (h1 : ∀ (s : Scene M) (v : Pace), Safe s j J f → s.openHere j J f → pace_ok s v j → - ∀ l : Journal j M, good (s.pace v) l → good s (l.prepend (Event.pace s v))) - (h2 : ∀ (s : Scene M) (v : Turn j s), Safe s j J f → s.work j J f → turn_ok v → - v.fork.TrueLaw → - ∀ b : Bool, 0 < v.fork.probs b → ∀ l, good (v.fork.dst b) l → good s (l.prepend (.march s v b))) - (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) {x : Grid M} (hx : 0 < s.actor x) : - good s (t.hike x) := by - induction t with - | finish s b => exact he _ _ h.1 h.2 hm - | push s v t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - exact h1 s v ha hb hc _ (ih hd (by rw [pace_mass]; exact hm) (by rw [pace_actor]; exact hx)) - | fork s v t ih => - have hf := branchTrusted h - obtain ⟨hx1,hx2⟩ := Fork.branch_pos hf (x:=x) (by rw [Turn.source]; exact hx) - obtain ⟨ha,hb,hc,hd⟩ := h - exact h2 s v ha hb hc hf _ hx1 _ (ih _ (hd _) ((true_mass hf).mp hx1) hx2) - -def expHike (t : Road j M s) (f : Journal j M → Grid M → ℝ) := - s.actor.Ex (fun x => f (hike t x) x) - -lemma Ex_hike_fork {f : Grid M → ℝ} {v : Turn j s} {t : ∀ b, Road j M (v.fork.dst b)} - (h : (Road.fork s v t).Trusted J f) (F : Journal j M → Grid M → ℝ) : - expHike (Road.fork s v t) F = v.fork.probs.Ex (fun b => - expHike (t b) (fun l x => F (l.prepend (.march s v b)) x)) := by - unfold expHike - let g := fun (b : Bool) x => F ((hike (t b) x).prepend (.march s v b)) x - have hi := expect_bit (branchTrusted h) g - rw [Turn.source] at hi - exact hi.symm - -lemma Ex_hike_push {v : Pace} {t : Road j M (s.pace v)} (F : Journal j M → Grid M → ℝ) : - expHike (.push s v t) F = expHike t (fun l x => F (l.prepend (.pace s v)) x) := by - simp only [expHike,pace_actor,hike] - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Fork + +/-! # The road seen from the input. This representation can truncate the play at transfers. -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +/-- Unary pace: entering the static system, or starting a new plateau -/ +inductive Pace +| renew | cross deriving DecidableEq + +def Scene.pace (s : Scene M) (v : Pace) := + match v with | .renew => s.reset | .cross => s.out + +lemma pace_mass (s : Scene M) (v : Pace) : (s.pace v).mass=s.mass := by cases v <;> rfl +lemma pace_actor (s : Scene M) (v : Pace) : (s.pace v).actor=s.actor := by cases v <;> rfl + +inductive Turn (j : ℕ) (s : Scene M) : Type +| ask (u : Update j M s.here) : Turn j s +| move : Turn j s + +def Turn.fork (v : Turn j s) := + match v with | .ask u => toss s u | .move => step s + +lemma Turn.source (v : Turn j s) : v.fork.src=s := by cases v <;> rfl + +inductive Road (j M : ℕ) : Scene M → Type +| finish (s : Scene M) (transferred : Bool) : Road j M s +| push (s : Scene M) (v : Pace) (t : Road j M (s.pace v)) : Road j M s +| fork (s : Scene M) (v : Turn j s) (t : (b : Bool) → Road j M (v.fork.dst b)) : Road j M s + +namespace Road + +/-- `extend` means continuing into the static system. -/ +def trace (throughCold : Bool) {s : Scene M} (t : Play j M s) : Road j M s := + match t with + | .rest s => .finish s false + | .freeze s t => if throughCold then .push s .cross (trace throughCold t) else .finish s true + | .ticket s t => .push s .renew (trace throughCold t) + | .jump s l r => .fork s .move fun b => + match b with | false => trace throughCold l | true => trace throughCold r + | .attempt s u l r => .fork s (.ask u) fun b => + match b with | false => trace throughCold l | true => trace throughCold r + +variable {f : Grid M → ℝ} + +def pace_ok (s : Scene M) (v : Pace) (j : ℕ) := + match v with | .renew => ¬ s.retire j ∧ s.enter | .cross => s.retire j +def turn_ok {s : Scene M} (v : Turn j s) := + match v with + | .ask u => s.live=true ∧ u.Good capA s.fund.mood s.actor + | .move => s.settled j + +/-- f and J ambient original data. -/ +def Trusted (J : ℕ) (f : Grid M → ℝ) {s : Scene M} (r : Road j M s) : Prop := + match r with + | .finish s b => s.Safe j J f ∧ (if b then s.openHere j J f ∧ s.retire j else ¬ s.openHere j J f) + | .push s v t => s.Safe j J f ∧ s.openHere j J f ∧ pace_ok s v j ∧ Trusted J f t + | .fork s v t => s.Safe j J f ∧ s.work j J f ∧ turn_ok v ∧ ∀ b, Trusted J f (t b) + +lemma traced {s : Scene M} (t : Play j M s) (b : Bool) (h : t.Follows J f) : + (trace b t).Trusted J f := by + induction t with + | rest s => exact ⟨h.1,h.2⟩ + | freeze s t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + cases b; exact ⟨ha,hb,hc⟩; exact ⟨ha,hb,hc,ih hd⟩ + | ticket s t ih => + obtain ⟨ha,hb,hc,hd,he⟩ := h + exact ⟨ha,hb,⟨hc,hd⟩,ih he⟩ + | jump s l r il ir => + obtain ⟨ha,hb,hc,hd,he⟩ := h + refine ⟨ha,hb,hc,?_⟩ + intro b; cases b; exact il hd; exact ir he + | attempt s u l r il ir => + obtain ⟨ha,hb,hc,hd,he,hf⟩ := h + refine ⟨ha,hb,⟨hc,hd⟩,?_⟩ + intro b; cases b; exact il he; exact ir hf + +lemma rootSafe {t : Road j M s} (h : t.Trusted J f) : Safe s j J f := by + cases t <;> exact h.1 + +lemma branchTrusted {v : Turn j s} {t : ∀ b, Road j M (v.fork.dst b)} + (h : (Road.fork s v t).Trusted J f) : v.fork.TrueLaw := by + obtain ⟨ha,hb,hc,hd⟩ := h + cases v with + | ask u => exact toss_true _ ha.within ha.positive hb.1.2.2 + | move => + have Hh := (rootSafe (hd false)).within.2 + exact step_true ha.within.1 Hh ha.positive hb.1.2.2 + +end Road + +/-- One edge actually seen. -/ +inductive Event (j M : ℕ) +| march (s : Scene M) (v : Turn j s) (b : Bool) : Event j M +| pace (s : Scene M) (v : Pace) : Event j M + +def Event.dest (p : Event j M) : Scene M := + match p with | .pace s v => s.pace v | .march _ v b => v.fork.dst b +def Event.src (p : Event j M) : Scene M := + match p with | .pace s _ | .march s _ _ => s + +structure Journal (j M : ℕ) where + done : Scene M + froze : Bool + events : List (Event j M) + +namespace Journal +def prepend (e : Event j M) (z : Journal j M) := {z with events:=e::z.events} +def fee (z : Journal j M) (f : Event j M → ℝ) := (z.events.map f).sum + +lemma fee_pre (e : Event j M) (z : Journal j M) (f : Event j M → ℝ) : + (z.prepend e).fee f = f e + z.fee f := by simp [prepend,fee] + +end Journal + +namespace Road +open Event Journal + +def hike {s : Scene M} (t : Road j M s) (x : Grid M) : Journal j M := + match t with + | .finish s b => ⟨s,b,[]⟩ + | .push s v t => (t.hike x).prepend (Event.pace s v) + | .fork s v t => + let b := v.fork.bit x + ((t b).hike x).prepend (Event.march s v b) + +/-- True induction along the observed path. -/ +lemma travel {f : Grid M → ℝ} {good : Scene M → Journal j M → Prop} + (he : ∀ s b, Safe s j J f → (if b then s.openHere j J f ∧ s.retire j else ¬ s.openHere j J f) → + 0 < s.mass → good s ⟨s,b,[]⟩) + (h1 : ∀ (s : Scene M) (v : Pace), Safe s j J f → s.openHere j J f → pace_ok s v j → + ∀ l : Journal j M, good (s.pace v) l → good s (l.prepend (Event.pace s v))) + (h2 : ∀ (s : Scene M) (v : Turn j s), Safe s j J f → s.work j J f → turn_ok v → + v.fork.TrueLaw → + ∀ b : Bool, 0 < v.fork.probs b → ∀ l, good (v.fork.dst b) l → good s (l.prepend (.march s v b))) + (t : Road j M s) (h : t.Trusted J f) (hm : 0 < s.mass) {x : Grid M} (hx : 0 < s.actor x) : + good s (t.hike x) := by + induction t with + | finish s b => exact he _ _ h.1 h.2 hm + | push s v t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + exact h1 s v ha hb hc _ (ih hd (by rw [pace_mass]; exact hm) (by rw [pace_actor]; exact hx)) + | fork s v t ih => + have hf := branchTrusted h + obtain ⟨hx1,hx2⟩ := Fork.branch_pos hf (x:=x) (by rw [Turn.source]; exact hx) + obtain ⟨ha,hb,hc,hd⟩ := h + exact h2 s v ha hb hc hf _ hx1 _ (ih _ (hd _) ((true_mass hf).mp hx1) hx2) + +def expHike (t : Road j M s) (f : Journal j M → Grid M → ℝ) := + s.actor.Ex (fun x => f (hike t x) x) + +lemma Ex_hike_fork {f : Grid M → ℝ} {v : Turn j s} {t : ∀ b, Road j M (v.fork.dst b)} + (h : (Road.fork s v t).Trusted J f) (F : Journal j M → Grid M → ℝ) : + expHike (Road.fork s v t) F = v.fork.probs.Ex (fun b => + expHike (t b) (fun l x => F (l.prepend (.march s v b)) x)) := by + unfold expHike + let g := fun (b : Bool) x => F ((hike (t b) x).prepend (.march s v b)) x + have hi := expect_bit (branchTrusted h) g + rw [Turn.source] at hi + exact hi.symm + +lemma Ex_hike_push {v : Pace} {t : Road j M (s.pace v)} (F : Journal j M → Grid M → ℝ) : + expHike (.push s v t) F = expHike t (fun l x => F (l.prepend (.pace s v)) x) := by + simp only [expHike,pace_actor,hike] + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Score.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Score.lean index 8b9e6ab2aae0bc96d7334293bb082200a71d8c56..a3701dc4abeaadfd37545bac86782d58649a4ee7 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Score.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Score.lean @@ -1,220 +1,220 @@ -import OAI.Analysis.FourierLLogL.Procedure.Counts -import OAI.Analysis.FourierLLogL.Procedure.Memory - -/-! # Scoring surviving reference histories. We require evidence at successes only, -zeroing the score for regular successes without it. -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -def gate (m : Memory M) (s : Scene M) (u : Update j M s.here) (w : Clutch j M) := - if u.regular then admissible m.profile s.here (cushion j) ∧ u.alarm s.fund.mood m.current w - else True - -def sampleWinner (s : Scene M) (u : Update j M s.here) (m : Memory M) (x : Grid M) - (f : Memory M → Grid M → ℝ) := - let A := s.fund.mood - let B := u.next A - exp (u.bill/8) * (fresh A B j x).Ex (fun w => if gate m s u w then f (m.stash A B w) x else 0) - -def turnScore (s : Scene M) (v : Turn j s) (m : Memory M) - (x : Grid M) (f : Bool → Memory M → Grid M → ℝ) := - match v with - | .move => f ((step s).bit x) m x - | .ask u => - if u.take s.fund.mood x then - (toss s u).probs true * sampleWinner s u m x (f true) - else - (toss s u).probs false * f false m x - -namespace Road -def score {s : Scene M} (t : Road j M s) (m : Memory M) (x : Grid M) := - match t with - | .finish _ _ => 1 - | .push _ _ t => score t m x - | .fork s v t => turnScore s v m x (fun b m x => score (t b) m x) - -lemma score_nonneg {s : Scene M} (t : Road j M s) (m : Memory M) (x : Grid M) : - 0 ≤ score t m x := by - induction t generalizing m x with - | finish => change (0:ℝ)≤1; norm_num - | push s v t ih => exact ih m x - | fork s v t ih => - cases v - · unfold score turnScore; dsimp only - split_ifs - · apply mul_nonneg (Law.nonneg _ _) - unfold sampleWinner; dsimp only - apply mul_nonneg (exp_nonneg _) - refine Ex_nonneg _ fun z => ?_; split_ifs - · exact ih true _ _ - · norm_num - exact mul_nonneg (Law.nonneg _ _) (ih false _ _) - exact ih _ _ _ - -private lemma total_gate {m : Memory M} (s : Scene M) (u : Update j M s.here) (f : Memory M → Grid M → ℝ) - (hh : ∀ w : Clutch j M, - let t := m.stash s.fund.mood (u.next s.fund.mood) w - expU (fun x => prefixE t.profile s.here x * f t x) ≤ expU (prefixE t.profile s.here)) : - let A := s.fund.mood; let B := u.next A - let N := prefixE m.profile s.here - expU (fun x => N x * sampleWinner s u m x f) ≤ exp (u.bill/8) * - expU (fun x => N x*(fresh A B j x).prob (gate m s u)) := by - intro A B N - let c := exp (u.bill/8) - let rate := stashRate A B j - have he (x : Grid M) (w : Clutch j M) : - N x * fresh A B j x w = rate * prefixE (stash m A B w).profile s.here x := multiply_stash _ _ _ _ _ _ - let U := uniform (Grid M) - have step (w : Clutch j M) : - U.Ex (fun x => N x*(fresh A B j x w * (if gate m s u w then f (stash m A B w) x else 0))) ≤ - U.Ex (fun x => N x * (fresh A B j x w * (if gate m s u w then 1 else 0))) := by - by_cases H : gate m s u w - · simp_rw [ite_eq_left H, ← mul_assoc,he,mul_assoc,mul_one,Ex_lmul] - have Hh := hh w - simp_rw [expU_ex] at Hh - exact mul_le_mul_of_nonneg_left Hh (rate_positive _ _ _) - simp [H] - have H0 (x : Grid M) : - N x*sampleWinner s u m x f = - c*∑ w, N x*(fresh A B j x w * if gate m s u w then f (stash m A B w) x else 0) := by - unfold sampleWinner; dsimp only - rw [← Finset.mul_sum]; unfold Ex - dsimp only [B,A,c]; rw [mul_left_comm] - have H1 (x : Grid M) : - N x*(fresh A B j x).prob (gate m s u) = - ∑ w, N x * (fresh A B j x w * if gate m s u w then 1 else 0) := by - unfold prob Ex; rw [Finset.mul_sum] - simp_rw [expU_ex,H0,H1,Ex_lmul,Ex_finset_sum] - exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun i _ => step i) (exp_nonneg _) - -lemma stake_upper : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → - ∀ (s : Scene M) (f : Grid M → ℝ) (t : Road j M s), - Trusted J f t → ∀ m : Memory M, m.Ready j s.fund → - let g := prefixE m.profile s.here - expU (fun x => g x*score t m x) ≤ expU g := by - filter_upwards [conditional_bound capA cap_ok,Fund.fund_est] with j hu hf - intro M J hJ s f t h m he - induction t generalizing m with - | finish s b => - intro g; simp [score] - | push s v t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - have hF : (s.pace v).fund = s.fund := by cases v <;> rfl - have hP : (s.pace v).here = s.here := by cases v <;> rfl - have HH := ih hd m (hF.symm ▸ he) - rw [hP] at HH - exact HH - | fork s v t ih => - obtain ⟨ha,hb,hc,hd⟩ := h - intro g - let V := uniform (Grid M) - cases v with - | move => - let Q (b : Bool) := prefixE m.profile (s.smaller b).here - have h1 (b : Bool) : - V.Ex (fun x => Q b x * score (t b) m x) ≤ V.Ex (Q b) := by - have H := ih b (hd b) m (show Ready m _ _ from he) - simp_rw [expU_ex] at H - exact H - have hi (x : Grid M) : - g x*score (.fork s .move t) m x = - Q false x*score (t false) m x + Q true x*score (t true) m x ∧ - g x = Q false x+Q true x := by - have hh : x∈s.here.L.block M → x∈s.here.R.block M → False := - fun h h' => Set.disjoint_left.mp (s.here.block_separate M) h h' - change (g x*score (t ((step s).bit x)) m x = _ ∧ _) - unfold g Q - unfold prefixE - by_cases hl : x∈s.here.L.block M <;> by_cases hr : x∈s.here.R.block M - all_goals - first - | exact (hh hl hr).elim - | have hx : x∈s.here.block M ↔ _ := Set.ext_iff.mp (block_union _ _) x - first - | rw [show (step s).bit x = true from decide_eq_true hr] - simp [smaller,child,hx, hl,hr] - | rw [show (step s).bit x = false from decide_eq_false hr] - simp [smaller,child,hx, hl,hr] - rw [funext (fun x => (hi x).1),funext (fun x => (hi x).2)] - simp_rw [expU_ex,Ex_add] - unfold V at h1 - exact add_le_add (h1 false) (h1 true) - | ask u => - let A := s.fund.mood - let B := u.next A - have hA : 0 ≤ A := ha.capital.effects.nonneg - let T := toss s u - let P := T.probs - let F (b : Bool) := score (t b) - have HI : V.Ex (fun x => g x*F false m x) ≤ V.Ex g := by - have H := ih false (hd _) m (show Ready _ _ _ from he) - simp_rw [expU_ex] at H; exact H - have HH : V.Ex (fun x => g x* sampleWinner s u m x (F true)) ≤ V.Ex g := by - unfold V; simp_rw [← expU_ex] - have hG := total_gate s u (F true) (m:=m) (by - intro w; exact ih true (hd true) _ (stash_ready m (s:=s) he u hc.2.positive hA w)) - apply hG.trans - cases H : u.regular with - | false => - change u.regular=false at H - have Hp (x : Grid M) : (fresh A B j x).prob (gate m s u)=1 := by - unfold prob; simp [gate,H,Ex_const] - change exp (u.bill/8) * expU (fun x => g x * (fresh A B j x).prob _) ≤ _ - simp_rw [Hp,mul_one] - simp [Update.bill,H] - | true => - have Hh : u.bill=u.credit := by simp [Update.bill,H] - change exp (u.bill/8) * expU (fun x => g x * (fresh A B j x).prob _) ≤ _ - rw [Hh] - by_cases hN : admissible m.profile s.here (cushion j) - · have HE : gate m s u = u.alarm A m.current := by - funext w; simp [gate,H,hN]; rfl - rw [HE] - have HP := ((hf M).2 s.fund s.here ha.capital).2 - (fun h => hb.2.1 ⟨hc.1,Or.inl h⟩) ha.within |>.1 - have Ht := hu M s.here s.fund.mood HP (by - apply le_trans _ hJ - gcongr - exact pow_le_pow_right₀ (by norm_num) ha.depth_ok) - s.actor u hc.2 H m.profile he.ordered (stash_length_bound m he ha) hN m.current - apply le_trans (mul_le_mul_of_nonneg_left Ht (Real.exp_nonneg _)) - rw [← mul_assoc,← exp_add,add_neg_cancel,exp_zero,one_mul] - · have Hp (x : Grid M) : (fresh A B j x).prob (gate m s u)=0 := by - unfold gate prob - simp [hN,H,Ex_const] - simp_rw [Hp,mul_zero,expU_ex,Ex_const,mul_zero] - exact Ex_nonneg _ fun i => prefixE_nonneg _ _ _ - rw [expU_ex,expU_ex] - let R (x) := sampleWinner s u m x (F true) - calc - _ ≤ V.Ex fun x => P false * (g x * F false m x) + P true * (g x * R x) := - Ex_mono _ fun x => by - change g x * (if u.take A x then P true*R x else P false*F false m x) ≤ _ - have h1 : 0 ≤ F false m x := score_nonneg _ _ _ - have h2 : 0 ≤ R x := by - unfold R sampleWinner; dsimp only - apply mul_nonneg (exp_nonneg _) (Ex_nonneg _ _) - intro i - split_ifs <;> first | exact score_nonneg _ _ _ | exact le_rfl - have hG : 0 ≤ g x := prefixE_nonneg _ _ _ - split_ifs - · rw [mul_left_comm]; exact le_add_of_nonneg_left (mul_nonneg (P.nonneg _) (mul_nonneg hG h1)) - · rw [mul_left_comm]; exact le_add_of_nonneg_right (mul_nonneg (P.nonneg _) (mul_nonneg hG h2)) - _ = P false*(V.Ex (fun x => g x*F false m x)) + P true*(V.Ex (fun x => g x*R x)) := by - rw [Ex_add,Ex_lmul,Ex_lmul] - _ ≤ P false*V.Ex g + P true*V.Ex g := - add_le_add (mul_le_mul_of_nonneg_left HI (P.nonneg _)) (mul_le_mul_of_nonneg_left HH (P.nonneg _)) - _ = _ := by rw [← add_mul,show P false + P true = 1 by simpa only [Fintype.sum_bool,add_comm] using P.total,one_mul] - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Counts +import OAI.Analysis.FourierLLogL.Procedure.Memory + +/-! # Scoring surviving reference histories. We require evidence at successes only, +zeroing the score for regular successes without it. -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Memory +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +def gate (m : Memory M) (s : Scene M) (u : Update j M s.here) (w : Clutch j M) := + if u.regular then admissible m.profile s.here (cushion j) ∧ u.alarm s.fund.mood m.current w + else True + +def sampleWinner (s : Scene M) (u : Update j M s.here) (m : Memory M) (x : Grid M) + (f : Memory M → Grid M → ℝ) := + let A := s.fund.mood + let B := u.next A + exp (u.bill/8) * (fresh A B j x).Ex (fun w => if gate m s u w then f (m.stash A B w) x else 0) + +def turnScore (s : Scene M) (v : Turn j s) (m : Memory M) + (x : Grid M) (f : Bool → Memory M → Grid M → ℝ) := + match v with + | .move => f ((step s).bit x) m x + | .ask u => + if u.take s.fund.mood x then + (toss s u).probs true * sampleWinner s u m x (f true) + else + (toss s u).probs false * f false m x + +namespace Road +def score {s : Scene M} (t : Road j M s) (m : Memory M) (x : Grid M) := + match t with + | .finish _ _ => 1 + | .push _ _ t => score t m x + | .fork s v t => turnScore s v m x (fun b m x => score (t b) m x) + +lemma score_nonneg {s : Scene M} (t : Road j M s) (m : Memory M) (x : Grid M) : + 0 ≤ score t m x := by + induction t generalizing m x with + | finish => change (0:ℝ)≤1; norm_num + | push s v t ih => exact ih m x + | fork s v t ih => + cases v + · unfold score turnScore; dsimp only + split_ifs + · apply mul_nonneg (Law.nonneg _ _) + unfold sampleWinner; dsimp only + apply mul_nonneg (exp_nonneg _) + refine Ex_nonneg _ fun z => ?_; split_ifs + · exact ih true _ _ + · norm_num + exact mul_nonneg (Law.nonneg _ _) (ih false _ _) + exact ih _ _ _ + +private lemma total_gate {m : Memory M} (s : Scene M) (u : Update j M s.here) (f : Memory M → Grid M → ℝ) + (hh : ∀ w : Clutch j M, + let t := m.stash s.fund.mood (u.next s.fund.mood) w + expU (fun x => prefixE t.profile s.here x * f t x) ≤ expU (prefixE t.profile s.here)) : + let A := s.fund.mood; let B := u.next A + let N := prefixE m.profile s.here + expU (fun x => N x * sampleWinner s u m x f) ≤ exp (u.bill/8) * + expU (fun x => N x*(fresh A B j x).prob (gate m s u)) := by + intro A B N + let c := exp (u.bill/8) + let rate := stashRate A B j + have he (x : Grid M) (w : Clutch j M) : + N x * fresh A B j x w = rate * prefixE (stash m A B w).profile s.here x := multiply_stash _ _ _ _ _ _ + let U := uniform (Grid M) + have step (w : Clutch j M) : + U.Ex (fun x => N x*(fresh A B j x w * (if gate m s u w then f (stash m A B w) x else 0))) ≤ + U.Ex (fun x => N x * (fresh A B j x w * (if gate m s u w then 1 else 0))) := by + by_cases H : gate m s u w + · simp_rw [ite_eq_left H, ← mul_assoc,he,mul_assoc,mul_one,Ex_lmul] + have Hh := hh w + simp_rw [expU_ex] at Hh + exact mul_le_mul_of_nonneg_left Hh (rate_positive _ _ _) + simp [H] + have H0 (x : Grid M) : + N x*sampleWinner s u m x f = + c*∑ w, N x*(fresh A B j x w * if gate m s u w then f (stash m A B w) x else 0) := by + unfold sampleWinner; dsimp only + rw [← Finset.mul_sum]; unfold Ex + dsimp only [B,A,c]; rw [mul_left_comm] + have H1 (x : Grid M) : + N x*(fresh A B j x).prob (gate m s u) = + ∑ w, N x * (fresh A B j x w * if gate m s u w then 1 else 0) := by + unfold prob Ex; rw [Finset.mul_sum] + simp_rw [expU_ex,H0,H1,Ex_lmul,Ex_finset_sum] + exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun i _ => step i) (exp_nonneg _) + +lemma stake_upper : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, 1000*((2:ℝ)^J) ≤ 2^M → + ∀ (s : Scene M) (f : Grid M → ℝ) (t : Road j M s), + Trusted J f t → ∀ m : Memory M, m.Ready j s.fund → + let g := prefixE m.profile s.here + expU (fun x => g x*score t m x) ≤ expU g := by + filter_upwards [conditional_bound capA cap_ok,Fund.fund_est] with j hu hf + intro M J hJ s f t h m he + induction t generalizing m with + | finish s b => + intro g; simp [score] + | push s v t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + have hF : (s.pace v).fund = s.fund := by cases v <;> rfl + have hP : (s.pace v).here = s.here := by cases v <;> rfl + have HH := ih hd m (hF.symm ▸ he) + rw [hP] at HH + exact HH + | fork s v t ih => + obtain ⟨ha,hb,hc,hd⟩ := h + intro g + let V := uniform (Grid M) + cases v with + | move => + let Q (b : Bool) := prefixE m.profile (s.smaller b).here + have h1 (b : Bool) : + V.Ex (fun x => Q b x * score (t b) m x) ≤ V.Ex (Q b) := by + have H := ih b (hd b) m (show Ready m _ _ from he) + simp_rw [expU_ex] at H + exact H + have hi (x : Grid M) : + g x*score (.fork s .move t) m x = + Q false x*score (t false) m x + Q true x*score (t true) m x ∧ + g x = Q false x+Q true x := by + have hh : x∈s.here.L.block M → x∈s.here.R.block M → False := + fun h h' => Set.disjoint_left.mp (s.here.block_separate M) h h' + change (g x*score (t ((step s).bit x)) m x = _ ∧ _) + unfold g Q + unfold prefixE + by_cases hl : x∈s.here.L.block M <;> by_cases hr : x∈s.here.R.block M + all_goals + first + | exact (hh hl hr).elim + | have hx : x∈s.here.block M ↔ _ := Set.ext_iff.mp (block_union _ _) x + first + | rw [show (step s).bit x = true from decide_eq_true hr] + simp [smaller,child,hx, hl,hr] + | rw [show (step s).bit x = false from decide_eq_false hr] + simp [smaller,child,hx, hl,hr] + rw [funext (fun x => (hi x).1),funext (fun x => (hi x).2)] + simp_rw [expU_ex,Ex_add] + unfold V at h1 + exact add_le_add (h1 false) (h1 true) + | ask u => + let A := s.fund.mood + let B := u.next A + have hA : 0 ≤ A := ha.capital.effects.nonneg + let T := toss s u + let P := T.probs + let F (b : Bool) := score (t b) + have HI : V.Ex (fun x => g x*F false m x) ≤ V.Ex g := by + have H := ih false (hd _) m (show Ready _ _ _ from he) + simp_rw [expU_ex] at H; exact H + have HH : V.Ex (fun x => g x* sampleWinner s u m x (F true)) ≤ V.Ex g := by + unfold V; simp_rw [← expU_ex] + have hG := total_gate s u (F true) (m:=m) (by + intro w; exact ih true (hd true) _ (stash_ready m (s:=s) he u hc.2.positive hA w)) + apply hG.trans + cases H : u.regular with + | false => + change u.regular=false at H + have Hp (x : Grid M) : (fresh A B j x).prob (gate m s u)=1 := by + unfold prob; simp [gate,H,Ex_const] + change exp (u.bill/8) * expU (fun x => g x * (fresh A B j x).prob _) ≤ _ + simp_rw [Hp,mul_one] + simp [Update.bill,H] + | true => + have Hh : u.bill=u.credit := by simp [Update.bill,H] + change exp (u.bill/8) * expU (fun x => g x * (fresh A B j x).prob _) ≤ _ + rw [Hh] + by_cases hN : admissible m.profile s.here (cushion j) + · have HE : gate m s u = u.alarm A m.current := by + funext w; simp [gate,H,hN]; rfl + rw [HE] + have HP := ((hf M).2 s.fund s.here ha.capital).2 + (fun h => hb.2.1 ⟨hc.1,Or.inl h⟩) ha.within |>.1 + have Ht := hu M s.here s.fund.mood HP (by + apply le_trans _ hJ + gcongr + exact pow_le_pow_right₀ (by norm_num) ha.depth_ok) + s.actor u hc.2 H m.profile he.ordered (stash_length_bound m he ha) hN m.current + apply le_trans (mul_le_mul_of_nonneg_left Ht (Real.exp_nonneg _)) + rw [← mul_assoc,← exp_add,add_neg_cancel,exp_zero,one_mul] + · have Hp (x : Grid M) : (fresh A B j x).prob (gate m s u)=0 := by + unfold gate prob + simp [hN,H,Ex_const] + simp_rw [Hp,mul_zero,expU_ex,Ex_const,mul_zero] + exact Ex_nonneg _ fun i => prefixE_nonneg _ _ _ + rw [expU_ex,expU_ex] + let R (x) := sampleWinner s u m x (F true) + calc + _ ≤ V.Ex fun x => P false * (g x * F false m x) + P true * (g x * R x) := + Ex_mono _ fun x => by + change g x * (if u.take A x then P true*R x else P false*F false m x) ≤ _ + have h1 : 0 ≤ F false m x := score_nonneg _ _ _ + have h2 : 0 ≤ R x := by + unfold R sampleWinner; dsimp only + apply mul_nonneg (exp_nonneg _) (Ex_nonneg _ _) + intro i + split_ifs <;> first | exact score_nonneg _ _ _ | exact le_rfl + have hG : 0 ≤ g x := prefixE_nonneg _ _ _ + split_ifs + · rw [mul_left_comm]; exact le_add_of_nonneg_left (mul_nonneg (P.nonneg _) (mul_nonneg hG h1)) + · rw [mul_left_comm]; exact le_add_of_nonneg_right (mul_nonneg (P.nonneg _) (mul_nonneg hG h2)) + _ = P false*(V.Ex (fun x => g x*F false m x)) + P true*(V.Ex (fun x => g x*R x)) := by + rw [Ex_add,Ex_lmul,Ex_lmul] + _ ≤ P false*V.Ex g + P true*V.Ex g := + add_le_add (mul_le_mul_of_nonneg_left HI (P.nonneg _)) (mul_le_mul_of_nonneg_left HH (P.nonneg _)) + _ = _ := by rw [← add_mul,show P false + P true = 1 by simpa only [Fintype.sum_bool,add_comm] using P.total,one_mul] + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Surprisal.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Surprisal.lean index dae36d26da40162390cd448e8391731bcc685c27..83f00571a8374ae67f527de9b874ff4047f98817 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Surprisal.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Surprisal.lean @@ -1,200 +1,200 @@ -import OAI.Analysis.FourierLLogL.Procedure.Road -/-! # Increments in the input model -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Journal Event -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -namespace Event -def surprise (v : Turn j s) (b : Bool) := -log (v.fork.probs b) -def failFee (q : Event j M) : ℝ := match q with - | .march _ (.ask u) false => surprise (.ask u) false - | _ => 0 -def winFee (q : Event j M) : ℝ := match q with - | .march _ (.ask u) true => surprise (.ask u) true - | _ => 0 -def memFee (q : Event j M) := winFee q + failFee q -def count (q : Event j M) : ℝ := match q with - | .march _ (.ask _) true => 1 - | _ => 0 -def tried (q : Event j M) : ℝ := match q with - | .march _ (.ask _) _ => 1 - | _ => 0 -def likelihood (q : Event j M) : ℝ := match q with - | .march s .move b => log (2*(step s).probs b) - | _ => 0 - -lemma surprise_nonneg (v : Turn j s) (b : Bool) : 0 ≤ surprise v b := - neg_nonneg.mpr (Real.log_nonpos (v.fork.probs.nonneg b) (v.fork.probs.le_one b)) -lemma fee_bounds (q : Event j M) : 0 ≤ failFee q ∧ 0 ≤ winFee q ∧ 0 ≤ tried q := by - cases q with - | pace _ _ => exact ⟨le_rfl,le_rfl,le_rfl⟩ - | march s v b => - cases v with - | move => exact ⟨le_rfl,le_rfl,le_rfl⟩ - | ask u => cases b - · exact ⟨surprise_nonneg _ _,le_rfl, zero_le_one⟩ - · exact ⟨le_rfl,surprise_nonneg _ _,zero_le_one⟩ - -lemma price_space (v : Turn j s) (b : Bool) (h : v.fork.TrueLaw) (hm : 0 < s.mass) - (hh : 0 < v.fork.probs b) : - likelihood (.march s v b) = - log (v.fork.dst b).mass - log s.mass + memFee (.march s v b) := by - cases v with - | move => - change log _ = log (s.smaller b).mass-log s.mass+(0+0) - change 0 < (step s).probs b at hh - rw [step_mass h b,log_mul (mul_pos (by norm_num : (0:ℝ)<2) hh).ne' hm.ne'] - simp - | ask u => - have he (b) : - memFee (.march s (.ask u) b) = -log ((toss s u).probs b) := by - cases b <;> simp [memFee,failFee,winFee,surprise,Turn.fork] - rw [he] - change 0 = log ((toss s u).dst b).mass - log s.mass + -log ((toss s u).probs b) - change 0 < (toss s u).probs b at hh - rw [toss_mass h b,Real.log_mul hh.ne' hm.ne']; ring - -lemma surprisal_cost (s : Scene M) (u : Update j M s.here) - (h : u.Good capA s.fund.mood s.actor) : - surprise (.ask u) true ≤ - 2000000*H j/height j * u.credit := by - let c := surprise (.ask u) true - let p := (toss s u).probs true - let i := u.ix - have hp : beta i /2 ≤ p := by unfold p; rw [success_prob]; exact h.bias - have hc : c ≤ 200 * (1+i) := by - have h1 := log_le_log (div_pos (beta_pos i) (by norm_num)) hp - rw [log_div (beta_pos _).ne' (by norm_num)] at h1 - change -log p ≤ _ - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),beta_log i, - Nat.cast_nonneg (α:=ℝ) i] - have he := h.duration - let d := height j - let a := H j - have hD : 0 < d := d_pos j - have ha : 0 < a := H_pos j - calc - _ ≤ _ := hc - _ ≤ (2000000*a/d) * (eps * u.encoded capA) := by - change (200:ℝ)* (1+i) ≤ (2000000*a/d) * ((1/10000)*(codeWeight capA*d/a*(1+i))) - have hW := code_ge cap_ok - field_simp - nlinarith [show 0 < (1:ℝ)+i by positivity] - _ ≤ _ := mul_le_mul_of_nonneg_left he (by positivity : 0 ≤ 2000000*a/d) - -end Event - -namespace Journal -def membership (z : Journal j M) := z.fee memFee -def creditSum (z : Journal j M) := z.fee winFee -def debit (z : Journal j M) := z.fee failFee -def like (z : Journal j M) := z.fee likelihood - -lemma mem_parts (l : Journal j M) : membership l = creditSum l+debit l := by - unfold membership creditSum debit fee - suffices HH : ∀ l : List (Event j M), - (l.map memFee).sum=(l.map winFee).sum+(l.map failFee).sum from HH _ - intro l; induction l with - | nil => simp - | cons i t ih => simp only [List.map_cons,List.sum_cons,ih,memFee]; ring -lemma positive_fee (l : Journal j M) (f : Event j M → ℝ) (h : ∀ i, 0 ≤ f i) : 0 ≤ l.fee f := by - unfold fee; apply List.sum_nonneg; simpa using fun i (_ : i∈l.events) => h i -lemma positive_mem (l : Journal j M) : 0 ≤ membership l := - l.positive_fee memFee (fun i => add_nonneg (fee_bounds i).2.1 (fee_bounds i).1) - -end Journal - -namespace Road - -lemma final_sound {t : Road j M s} {f : Grid M → ℝ} (h : t.Trusted J f) - (hm : 0 < s.mass) {x : Grid M} (hx : 0 < s.actor x) : - let l := hike t x - l.done.Safe j J f ∧ (l.froze=true → l.done.retire j) ∧ 0 < l.done.mass ∧ - l.like = log l.done.mass-log s.mass+l.membership ∧ - (s.fund.uses:ℝ)+l.fee count=l.done.fund.uses ∧ - l.creditSum ≤ 2000000*H j/height j * (l.done.fund.spend-s.fund.spend) := by - classical - let a := 2000000*H j/height j - intro l - let g := fun (s : Scene M) (l : Journal j M) => l.done.Safe j J f ∧ - (l.froze=true → l.done.retire j) ∧ 0 < l.done.mass ∧ - l.like = log l.done.mass-log s.mass+l.membership ∧ - (s.fund.uses:ℝ)+l.fee count=l.done.fund.uses ∧ - l.creditSum ≤ a*(l.done.fund.spend-s.fund.spend) - change g s l - refine travel (good:=g) ?_ ?_ ?_ t h hm hx - · intro s b h hb hm - refine ⟨h,fun hi => ?_,hm,?_⟩ - · change b=true at hi; rw [ite_eq_left hi] at hb; exact hb.2 - simp [creditSum,membership,like,fee] - · intro s v h hb hv l H - rcases H with ⟨H₁,H₂,H₃,H₄,H₅,H₆⟩ - refine ⟨H₁,H₂,H₃,?_⟩ - rw [pace_mass] at H₄ - have he : (s.pace v).fund=s.fund := by cases v <;> rfl - rw [he] at H₅ H₆ - simp only [like,membership,creditSum] at * - rw [fee_pre,fee_pre,fee_pre,fee_pre] - dsimp only [likelihood,memFee,Event.count,winFee,failFee,prepend] - simp only [zero_add,add_zero] - exact ⟨H₄,H₅,H₆⟩ - - · intro s v h he hl hv b hh l H - obtain ⟨Hx,Hy,Hz,H₁,H₂,H₃⟩ := H - refine ⟨Hx,Hy,Hz,?_,?_⟩ - · unfold like membership - rw [fee_pre,fee_pre] - have Hi := price_space v b hv he.1.2.2 hh - unfold like membership at H₁ - dsimp only [prepend] - linarith - unfold creditSum; rw [fee_pre,fee_pre]; unfold creditSum at H₃ - dsimp only [prepend] - cases v with - | move => - change (s.fund.uses:ℝ)+(0+l.fee count)=_ ∧ 0 + _ ≤ _ - rw [zero_add,zero_add] - exact ⟨H₂,H₃⟩ - | ask u => - have HH : surprise (Turn.ask (j:=j) u) true ≤ a*u.credit := - surprisal_cost s u hl.2 - cases b - · change (s.fund.uses:ℝ)+(0+l.fee count)=_ ∧ 0+_ ≤ _ - rw [zero_add,zero_add]; exact ⟨H₂,H₃⟩ - · change ((s.fund.uses+1:ℕ):ℝ)+l.fee count= _ at H₂ - change l.fee winFee ≤ a * (_-(s.fund.spend+u.credit)) at H₃ - change _+(1+_)=_ ∧ surprise (.ask u) _+_ ≤ _ - push_cast at * - constructor; linarith; linarith - -/-- Positive exponential fee supported on path prefixes. -/ -lemma capital_bound {s : Scene M} (F : Event j M → ℝ) - (hp : ∀ s : Scene M, ∀ v : Pace, F (Event.pace s v)=0) - (hf : ∀ s : Scene M, ∀ v : Turn j s, - v.fork.probs.Ex (fun b => exp (F (.march s v b))) ≤ 1) - (t : Road j M s) {f : Grid M → ℝ} (h : t.Trusted J f) (hm : 0 < s.mass) : - t.expHike (fun l _x => exp (l.fee F)) ≤ 1 := by - induction t with - | finish s b => simp [expHike,hike, fee,Ex_const] - | push s v t ih => - rw [Ex_hike_push] - simp_rw [fee_pre,hp,zero_add] - exact ih h.2.2.2 (by rw [pace_mass]; exact hm) - | fork s v t ih => - rw [Ex_hike_fork h]; simp_rw [fee_pre, exp_add,expHike,Ex_lmul] - apply le_trans _ (hf s v) - apply Ex_mono_mem; intro b hb - have hi := ih b (h.2.2.2 b) ((true_mass (branchTrusted h)).mp hb) - exact mul_le_of_le_one_right (exp_nonneg _) hi - -end Road -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Road +/-! # Increments in the input model -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Journal Event +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +namespace Event +def surprise (v : Turn j s) (b : Bool) := -log (v.fork.probs b) +def failFee (q : Event j M) : ℝ := match q with + | .march _ (.ask u) false => surprise (.ask u) false + | _ => 0 +def winFee (q : Event j M) : ℝ := match q with + | .march _ (.ask u) true => surprise (.ask u) true + | _ => 0 +def memFee (q : Event j M) := winFee q + failFee q +def count (q : Event j M) : ℝ := match q with + | .march _ (.ask _) true => 1 + | _ => 0 +def tried (q : Event j M) : ℝ := match q with + | .march _ (.ask _) _ => 1 + | _ => 0 +def likelihood (q : Event j M) : ℝ := match q with + | .march s .move b => log (2*(step s).probs b) + | _ => 0 + +lemma surprise_nonneg (v : Turn j s) (b : Bool) : 0 ≤ surprise v b := + neg_nonneg.mpr (Real.log_nonpos (v.fork.probs.nonneg b) (v.fork.probs.le_one b)) +lemma fee_bounds (q : Event j M) : 0 ≤ failFee q ∧ 0 ≤ winFee q ∧ 0 ≤ tried q := by + cases q with + | pace _ _ => exact ⟨le_rfl,le_rfl,le_rfl⟩ + | march s v b => + cases v with + | move => exact ⟨le_rfl,le_rfl,le_rfl⟩ + | ask u => cases b + · exact ⟨surprise_nonneg _ _,le_rfl, zero_le_one⟩ + · exact ⟨le_rfl,surprise_nonneg _ _,zero_le_one⟩ + +lemma price_space (v : Turn j s) (b : Bool) (h : v.fork.TrueLaw) (hm : 0 < s.mass) + (hh : 0 < v.fork.probs b) : + likelihood (.march s v b) = + log (v.fork.dst b).mass - log s.mass + memFee (.march s v b) := by + cases v with + | move => + change log _ = log (s.smaller b).mass-log s.mass+(0+0) + change 0 < (step s).probs b at hh + rw [step_mass h b,log_mul (mul_pos (by norm_num : (0:ℝ)<2) hh).ne' hm.ne'] + simp + | ask u => + have he (b) : + memFee (.march s (.ask u) b) = -log ((toss s u).probs b) := by + cases b <;> simp [memFee,failFee,winFee,surprise,Turn.fork] + rw [he] + change 0 = log ((toss s u).dst b).mass - log s.mass + -log ((toss s u).probs b) + change 0 < (toss s u).probs b at hh + rw [toss_mass h b,Real.log_mul hh.ne' hm.ne']; ring + +lemma surprisal_cost (s : Scene M) (u : Update j M s.here) + (h : u.Good capA s.fund.mood s.actor) : + surprise (.ask u) true ≤ + 2000000*H j/height j * u.credit := by + let c := surprise (.ask u) true + let p := (toss s u).probs true + let i := u.ix + have hp : beta i /2 ≤ p := by unfold p; rw [success_prob]; exact h.bias + have hc : c ≤ 200 * (1+i) := by + have h1 := log_le_log (div_pos (beta_pos i) (by norm_num)) hp + rw [log_div (beta_pos _).ne' (by norm_num)] at h1 + change -log p ≤ _ + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),beta_log i, + Nat.cast_nonneg (α:=ℝ) i] + have he := h.duration + let d := height j + let a := H j + have hD : 0 < d := d_pos j + have ha : 0 < a := H_pos j + calc + _ ≤ _ := hc + _ ≤ (2000000*a/d) * (eps * u.encoded capA) := by + change (200:ℝ)* (1+i) ≤ (2000000*a/d) * ((1/10000)*(codeWeight capA*d/a*(1+i))) + have hW := code_ge cap_ok + field_simp + nlinarith [show 0 < (1:ℝ)+i by positivity] + _ ≤ _ := mul_le_mul_of_nonneg_left he (by positivity : 0 ≤ 2000000*a/d) + +end Event + +namespace Journal +def membership (z : Journal j M) := z.fee memFee +def creditSum (z : Journal j M) := z.fee winFee +def debit (z : Journal j M) := z.fee failFee +def like (z : Journal j M) := z.fee likelihood + +lemma mem_parts (l : Journal j M) : membership l = creditSum l+debit l := by + unfold membership creditSum debit fee + suffices HH : ∀ l : List (Event j M), + (l.map memFee).sum=(l.map winFee).sum+(l.map failFee).sum from HH _ + intro l; induction l with + | nil => simp + | cons i t ih => simp only [List.map_cons,List.sum_cons,ih,memFee]; ring +lemma positive_fee (l : Journal j M) (f : Event j M → ℝ) (h : ∀ i, 0 ≤ f i) : 0 ≤ l.fee f := by + unfold fee; apply List.sum_nonneg; simpa using fun i (_ : i∈l.events) => h i +lemma positive_mem (l : Journal j M) : 0 ≤ membership l := + l.positive_fee memFee (fun i => add_nonneg (fee_bounds i).2.1 (fee_bounds i).1) + +end Journal + +namespace Road + +lemma final_sound {t : Road j M s} {f : Grid M → ℝ} (h : t.Trusted J f) + (hm : 0 < s.mass) {x : Grid M} (hx : 0 < s.actor x) : + let l := hike t x + l.done.Safe j J f ∧ (l.froze=true → l.done.retire j) ∧ 0 < l.done.mass ∧ + l.like = log l.done.mass-log s.mass+l.membership ∧ + (s.fund.uses:ℝ)+l.fee count=l.done.fund.uses ∧ + l.creditSum ≤ 2000000*H j/height j * (l.done.fund.spend-s.fund.spend) := by + classical + let a := 2000000*H j/height j + intro l + let g := fun (s : Scene M) (l : Journal j M) => l.done.Safe j J f ∧ + (l.froze=true → l.done.retire j) ∧ 0 < l.done.mass ∧ + l.like = log l.done.mass-log s.mass+l.membership ∧ + (s.fund.uses:ℝ)+l.fee count=l.done.fund.uses ∧ + l.creditSum ≤ a*(l.done.fund.spend-s.fund.spend) + change g s l + refine travel (good:=g) ?_ ?_ ?_ t h hm hx + · intro s b h hb hm + refine ⟨h,fun hi => ?_,hm,?_⟩ + · change b=true at hi; rw [ite_eq_left hi] at hb; exact hb.2 + simp [creditSum,membership,like,fee] + · intro s v h hb hv l H + rcases H with ⟨H₁,H₂,H₃,H₄,H₅,H₆⟩ + refine ⟨H₁,H₂,H₃,?_⟩ + rw [pace_mass] at H₄ + have he : (s.pace v).fund=s.fund := by cases v <;> rfl + rw [he] at H₅ H₆ + simp only [like,membership,creditSum] at * + rw [fee_pre,fee_pre,fee_pre,fee_pre] + dsimp only [likelihood,memFee,Event.count,winFee,failFee,prepend] + simp only [zero_add,add_zero] + exact ⟨H₄,H₅,H₆⟩ + + · intro s v h he hl hv b hh l H + obtain ⟨Hx,Hy,Hz,H₁,H₂,H₃⟩ := H + refine ⟨Hx,Hy,Hz,?_,?_⟩ + · unfold like membership + rw [fee_pre,fee_pre] + have Hi := price_space v b hv he.1.2.2 hh + unfold like membership at H₁ + dsimp only [prepend] + linarith + unfold creditSum; rw [fee_pre,fee_pre]; unfold creditSum at H₃ + dsimp only [prepend] + cases v with + | move => + change (s.fund.uses:ℝ)+(0+l.fee count)=_ ∧ 0 + _ ≤ _ + rw [zero_add,zero_add] + exact ⟨H₂,H₃⟩ + | ask u => + have HH : surprise (Turn.ask (j:=j) u) true ≤ a*u.credit := + surprisal_cost s u hl.2 + cases b + · change (s.fund.uses:ℝ)+(0+l.fee count)=_ ∧ 0+_ ≤ _ + rw [zero_add,zero_add]; exact ⟨H₂,H₃⟩ + · change ((s.fund.uses+1:ℕ):ℝ)+l.fee count= _ at H₂ + change l.fee winFee ≤ a * (_-(s.fund.spend+u.credit)) at H₃ + change _+(1+_)=_ ∧ surprise (.ask u) _+_ ≤ _ + push_cast at * + constructor; linarith; linarith + +/-- Positive exponential fee supported on path prefixes. -/ +lemma capital_bound {s : Scene M} (F : Event j M → ℝ) + (hp : ∀ s : Scene M, ∀ v : Pace, F (Event.pace s v)=0) + (hf : ∀ s : Scene M, ∀ v : Turn j s, + v.fork.probs.Ex (fun b => exp (F (.march s v b))) ≤ 1) + (t : Road j M s) {f : Grid M → ℝ} (h : t.Trusted J f) (hm : 0 < s.mass) : + t.expHike (fun l _x => exp (l.fee F)) ≤ 1 := by + induction t with + | finish s b => simp [expHike,hike, fee,Ex_const] + | push s v t ih => + rw [Ex_hike_push] + simp_rw [fee_pre,hp,zero_add] + exact ih h.2.2.2 (by rw [pace_mass]; exact hm) + | fork s v t ih => + rw [Ex_hike_fork h]; simp_rw [fee_pre, exp_add,expHike,Ex_lmul] + apply le_trans _ (hf s v) + apply Ex_mono_mem; intro b hb + have hi := ih b (h.2.2.2 b) ((true_mass (branchTrusted h)).mp hb) + exact mul_le_of_le_one_right (exp_nonneg _) hi + +end Road +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Procedure/Tree.lean b/lean/OAI/Analysis/FourierLLogL/Procedure/Tree.lean index 5c1ade037c3c1c8122aa91e181003fd66b361616..f8cc357c6a105360e7e37a8ecde20ff3c4085ac6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Procedure/Tree.lean +++ b/lean/OAI/Analysis/FourierLLogL/Procedure/Tree.lean @@ -1,185 +1,185 @@ -import OAI.Analysis.FourierLLogL.Procedure.Play - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure -open Law Node Real Period Compress.Params Thermal Fund Scene - -variable {M J j : ℕ} {s : Scene M} -open scoped Classical - -lemma top_avg {E : Type*} [AddCommMonoid E] [MulAction ℝ E] (f : Grid M → E) : - root.avg f = expU f := by - rw [avg_sum, group_top,len_top,inv_one,one_smul] - rfl - -lemma lose_eq (u : Update j M s.here) (x : Grid M) : - (lose s u).wage x = if u.take s.fund.mood x then 0 else s.wage x := by - by_cases H : u.take s.fund.mood x <;> simp [lose,Set.indicator,H] -lemma win_eq (u : Update j M s.here) (x : Grid M) : - (win s u).wage x = if u.take s.fund.mood x then s.wage x else 0 := by - by_cases H : u.take s.fund.mood x <;> simp [win,Set.indicator,H] - -lemma split_pos (u : Update j M s.here) (h : 0 ≤ s.wage) : - (0 ≤ (lose s u).wage ∧ (lose s u).wage ≤ s.wage) ∧ - (0 ≤ (win s u).wage ∧ (win s u).wage ≤ s.wage) := by - refine ⟨⟨?_,?_⟩,?_,?_⟩ - all_goals - intro i - dsimp only [Pi.zero_apply] - first | rw [lose_eq] | rw [win_eq] - split_ifs <;> first | exact h i | exact le_rfl - -private def bonus (s : Scene M) : ℕ := - (if s.live then 2 else 0)+(if s.enter then 1 else 0) -private def playing (s : Scene M) := - Finset.univ.filter fun x => 0 < s.wage x - -private def totalUse (j : ℕ) := Nat.ceil (capA*H j) -private def stage (j : ℕ) (s : Scene M) : ℕ := - ((totalUse j-s.fund.uses) * (Fintype.card (Grid M)+1)+(playing s).card)*4+bonus s - -private lemma begin_ok : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ (f : Grid M → ℝ) (b : Bool), 0 ≤ f → expU f ≤ 2 → - (beginPlay f b).Safe j J f := by - filter_upwards [Fund.fund_est] with j hj - intro M J f b hp h - refine ⟨root_ok _,Nat.zero_le _,(hj M).1,le_rfl,hp,le_rfl,?_⟩ - change root.avg f ≤ _ - rw [top_avg] - unfold cutoff; have hi := Nat.cast_nonneg (α:=ℝ) j - have h1 : 1 ≤ ((j:ℝ)+1)^3 := one_le_pow₀ (by linarith) - linarith - -theorem plant : - ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, J ≤ M → ∀ f : Grid M → ℝ, - 0 ≤ f → expU f ≤ 2 → (∀ x, f x ≤ exp (height j)) → - ∀ b : Bool, ∃ t : Play j M (beginPlay f b), t.Follows J f := by - classical - filter_upwards [begin_ok, Fund.fund_est, make_update capA cap_ok] with j hb hK hM - intro M J hJ f h0 ht he b - let OK (s : Scene M) := ∃ t : Play j M s, t.Follows J f - suffices HH : ∀ k : ℕ, ∀ s : Scene M, s.here.lev+k=J → - s.Safe j J f → OK s by - exact HH J _ (by simp [beginPlay,root]) (hb M J f b h0 ht) - intro k; induction k using Nat.strong_induction_on with - | h k ih => - intro s hi hy - suffices IH : ∀ n : ℕ, ∀ s : Scene M, s.here.lev+k=J → - s.Safe j J f → stage j s=n → OK s from IH _ s hi hy rfl - intro n; induction n using Nat.strong_induction_on with - | h n Now => - intro s hi hy hn - subst n - by_cases ho : openHere s j J f - case neg => exact ⟨Play.rest s, hy, ho⟩ - have hh1 := hy.positive - have hm : 0 < s.mass := ho.2.2 - have hL : s.here.lev < J := ho.1 - have hR : s.here.lev < M := hL.trans_le hJ - have hp : s.here.valid := hy.within.1 - by_cases h : retire s j - · have hq : s.out.Safe j J f := by - obtain ⟨a,b,c,d,e,f,g⟩ := hy - exact ⟨a,b,c,le_rfl,e,f,g⟩ - obtain ⟨t,h1⟩ := Now _ (show stage j s.out < _ by - have heq : playing s.out = playing s := rfl - have hmass : 0 < s.here.avg s.wage := hm - have hbonusOut : bonus s.out = 1 := by simp [bonus,out,enter,mass,hmass] - have hbonusS : 2 ≤ bonus s := by simp [bonus,h.1] - have hfund : s.out.fund.uses = s.fund.uses := rfl - rw [stage,stage,heq,hfund] - rw [hbonusOut] - omega) - s.out hi hq rfl - exact ⟨Play.freeze s t, hy,ho,h,h1⟩ - by_cases ha : enter s - · have ha' : ¬ enter s.reset := by unfold enter reset; change ¬ 2*s.mass rfl - apply ih (k-1) (by omega) - · omega - refine ⟨s.here.child_ok hp hR b,by rw [H]; exact hL, - hy.capital.restrict (s.here.child_below _), hy.norm_ok, hh1,hy.sub,?_⟩ - have H1 := avg_nonneg s.here.L (f:=f) fun x _ => h0 x - have H2 := avg_nonneg s.here.R (f:=f) fun x _ => h0 x - have H3 := ho.2.1 - rw [avg_split_real s.here] at H3 - cases b - · change s.here.L.avg f ≤ _; linarith - · change s.here.R.avg f ≤ _; linarith - obtain ⟨l,hl⟩ := hE false - obtain ⟨r,hr⟩ := hE true - exact ⟨Play.jump s l r,hy,⟨ho,h,ha⟩,HH,hl,hr⟩ - unfold settled at HH; push Not at HH - obtain ⟨hl,i,HH',HE⟩ := HH - have hb : ¬ breached j s.fund := fun he => h ⟨hl,Or.inl he⟩ - have hx : exp (-height j) ≤ s.mass := by - apply le_of_not_gt; exact fun hi => h ⟨hl,Or.inr hi⟩ - have hW := ((hK M).2 s.fund s.here hy.capital).2 hb hy.within - have hV := actor_def hy.within hy.positive hm - unfold enter at ha - obtain ⟨u,rfl,hu⟩ := hM _ _ _ hR hW.1 s.wage hh1 - (fun x => le_trans (hy.sub x) (he x)) hx _ _ hV - (by linarith) (le_of_not_gt ha) i HH' HE - have h₁ := split_pos u hh1 - have h₂ : ∀ s : Scene M, bonus s ≤ 3 := fun s => by unfold bonus; split_ifs <;> omega - have hY₁ : Safe (s.lose u) j J f := - ⟨hy.within,hy.depth_ok,hy.capital,hy.norm_ok,h₁.1.1,h₁.1.2.trans hy.sub,hy.cut⟩ - have hY₂ : Safe (s.win u) j J f := - ⟨hy.within,hy.depth_ok,hW.2.2 u _ hu,le_rfl,h₁.2.1,h₁.2.2.trans hy.sub,hy.cut⟩ - have hww : stage j (s.win u) < stage j s := by - let N := totalUse j - have h4 : s.fund.uses+1 ≤ N := by - have HH := (Nat.le_ceil (capA*H j)) - have He : ((s.fund.uses+1:ℕ):ℝ) ≤ ↑N := by push_cast; exact hW.2.1.trans HH - exact_mod_cast He - have h3 := h₂ (s.win u) - let L := Fintype.card (Grid M)+1 - have h5 : (s.win u).fund.uses = s.fund.uses+1 := rfl - have h6 : (playing (s.win u)).card < L := Nat.lt_succ_of_le (Finset.card_le_univ _) - unfold stage; rw [h5,show totalUse j - s.fund.uses= N-(s.fund.uses+1)+1 by omega]; simp only [add_mul, one_mul] - dsimp only [N,L] at *; omega - have hll : stage j (s.lose u) < stage j s := by - have h4 : (playing (s.lose u)).card < (playing s).card := by - apply Finset.card_lt_card - refine Finset.ssubset_iff_of_subset (s₁:=playing (s.lose u)) ?_ |>.mpr ?_ - · intro x hx - simp only [playing,Finset.mem_filter,Finset.mem_univ,true_and] at * - exact lt_of_lt_of_le hx (h₁.1.2 _) - have HE := lt_of_lt_of_le (show 0 < beta u.ix /2 from by have := beta_pos u.ix; positivity) hu.bias - obtain ⟨x,hx,hx'⟩ := (prob_pos_iff _ _).mp HE - have hf : 0 < s.wage x := by - by_contra! H - have hh := H.antisymm (hh1 x) - rw [hV _,wt,hh,mul_zero,zero_div] at hx' - exact lt_irrefl _ hx' - refine ⟨x,?_,?_⟩ <;> simp [playing,hf,lose_eq, hx] - unfold stage - rw [show (s.lose u).fund=s.fund from rfl] - have hh := h₂ (s.lose u); omega - obtain ⟨l,hl'⟩ := Now _ hll (s.lose u) hi hY₁ rfl - obtain ⟨r,hr'⟩ := Now _ hww (s.win u) hi hY₂ rfl - exact ⟨Play.attempt s u l r, hy, ⟨ho,h,ha⟩,hl,hu,hl',hr'⟩ - -end FourierLLogL.Dyadic.Procedure - -end OAI +import OAI.Analysis.FourierLLogL.Procedure.Play + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure +open Law Node Real Period Compress.Params Thermal Fund Scene + +variable {M J j : ℕ} {s : Scene M} +open scoped Classical + +lemma top_avg {E : Type*} [AddCommMonoid E] [MulAction ℝ E] (f : Grid M → E) : + root.avg f = expU f := by + rw [avg_sum, group_top,len_top,inv_one,one_smul] + rfl + +lemma lose_eq (u : Update j M s.here) (x : Grid M) : + (lose s u).wage x = if u.take s.fund.mood x then 0 else s.wage x := by + by_cases H : u.take s.fund.mood x <;> simp [lose,Set.indicator,H] +lemma win_eq (u : Update j M s.here) (x : Grid M) : + (win s u).wage x = if u.take s.fund.mood x then s.wage x else 0 := by + by_cases H : u.take s.fund.mood x <;> simp [win,Set.indicator,H] + +lemma split_pos (u : Update j M s.here) (h : 0 ≤ s.wage) : + (0 ≤ (lose s u).wage ∧ (lose s u).wage ≤ s.wage) ∧ + (0 ≤ (win s u).wage ∧ (win s u).wage ≤ s.wage) := by + refine ⟨⟨?_,?_⟩,?_,?_⟩ + all_goals + intro i + dsimp only [Pi.zero_apply] + first | rw [lose_eq] | rw [win_eq] + split_ifs <;> first | exact h i | exact le_rfl + +private def bonus (s : Scene M) : ℕ := + (if s.live then 2 else 0)+(if s.enter then 1 else 0) +private def playing (s : Scene M) := + Finset.univ.filter fun x => 0 < s.wage x + +private def totalUse (j : ℕ) := Nat.ceil (capA*H j) +private def stage (j : ℕ) (s : Scene M) : ℕ := + ((totalUse j-s.fund.uses) * (Fintype.card (Grid M)+1)+(playing s).card)*4+bonus s + +private lemma begin_ok : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, ∀ (f : Grid M → ℝ) (b : Bool), 0 ≤ f → expU f ≤ 2 → + (beginPlay f b).Safe j J f := by + filter_upwards [Fund.fund_est] with j hj + intro M J f b hp h + refine ⟨root_ok _,Nat.zero_le _,(hj M).1,le_rfl,hp,le_rfl,?_⟩ + change root.avg f ≤ _ + rw [top_avg] + unfold cutoff; have hi := Nat.cast_nonneg (α:=ℝ) j + have h1 : 1 ≤ ((j:ℝ)+1)^3 := one_le_pow₀ (by linarith) + linarith + +theorem plant : + ∀ᶠ j : ℕ in atTop, ∀ M J : ℕ, J ≤ M → ∀ f : Grid M → ℝ, + 0 ≤ f → expU f ≤ 2 → (∀ x, f x ≤ exp (height j)) → + ∀ b : Bool, ∃ t : Play j M (beginPlay f b), t.Follows J f := by + classical + filter_upwards [begin_ok, Fund.fund_est, make_update capA cap_ok] with j hb hK hM + intro M J hJ f h0 ht he b + let OK (s : Scene M) := ∃ t : Play j M s, t.Follows J f + suffices HH : ∀ k : ℕ, ∀ s : Scene M, s.here.lev+k=J → + s.Safe j J f → OK s by + exact HH J _ (by simp [beginPlay,root]) (hb M J f b h0 ht) + intro k; induction k using Nat.strong_induction_on with + | h k ih => + intro s hi hy + suffices IH : ∀ n : ℕ, ∀ s : Scene M, s.here.lev+k=J → + s.Safe j J f → stage j s=n → OK s from IH _ s hi hy rfl + intro n; induction n using Nat.strong_induction_on with + | h n Now => + intro s hi hy hn + subst n + by_cases ho : openHere s j J f + case neg => exact ⟨Play.rest s, hy, ho⟩ + have hh1 := hy.positive + have hm : 0 < s.mass := ho.2.2 + have hL : s.here.lev < J := ho.1 + have hR : s.here.lev < M := hL.trans_le hJ + have hp : s.here.valid := hy.within.1 + by_cases h : retire s j + · have hq : s.out.Safe j J f := by + obtain ⟨a,b,c,d,e,f,g⟩ := hy + exact ⟨a,b,c,le_rfl,e,f,g⟩ + obtain ⟨t,h1⟩ := Now _ (show stage j s.out < _ by + have heq : playing s.out = playing s := rfl + have hmass : 0 < s.here.avg s.wage := hm + have hbonusOut : bonus s.out = 1 := by simp [bonus,out,enter,mass,hmass] + have hbonusS : 2 ≤ bonus s := by simp [bonus,h.1] + have hfund : s.out.fund.uses = s.fund.uses := rfl + rw [stage,stage,heq,hfund] + rw [hbonusOut] + omega) + s.out hi hq rfl + exact ⟨Play.freeze s t, hy,ho,h,h1⟩ + by_cases ha : enter s + · have ha' : ¬ enter s.reset := by unfold enter reset; change ¬ 2*s.mass rfl + apply ih (k-1) (by omega) + · omega + refine ⟨s.here.child_ok hp hR b,by rw [H]; exact hL, + hy.capital.restrict (s.here.child_below _), hy.norm_ok, hh1,hy.sub,?_⟩ + have H1 := avg_nonneg s.here.L (f:=f) fun x _ => h0 x + have H2 := avg_nonneg s.here.R (f:=f) fun x _ => h0 x + have H3 := ho.2.1 + rw [avg_split_real s.here] at H3 + cases b + · change s.here.L.avg f ≤ _; linarith + · change s.here.R.avg f ≤ _; linarith + obtain ⟨l,hl⟩ := hE false + obtain ⟨r,hr⟩ := hE true + exact ⟨Play.jump s l r,hy,⟨ho,h,ha⟩,HH,hl,hr⟩ + unfold settled at HH; push Not at HH + obtain ⟨hl,i,HH',HE⟩ := HH + have hb : ¬ breached j s.fund := fun he => h ⟨hl,Or.inl he⟩ + have hx : exp (-height j) ≤ s.mass := by + apply le_of_not_gt; exact fun hi => h ⟨hl,Or.inr hi⟩ + have hW := ((hK M).2 s.fund s.here hy.capital).2 hb hy.within + have hV := actor_def hy.within hy.positive hm + unfold enter at ha + obtain ⟨u,rfl,hu⟩ := hM _ _ _ hR hW.1 s.wage hh1 + (fun x => le_trans (hy.sub x) (he x)) hx _ _ hV + (by linarith) (le_of_not_gt ha) i HH' HE + have h₁ := split_pos u hh1 + have h₂ : ∀ s : Scene M, bonus s ≤ 3 := fun s => by unfold bonus; split_ifs <;> omega + have hY₁ : Safe (s.lose u) j J f := + ⟨hy.within,hy.depth_ok,hy.capital,hy.norm_ok,h₁.1.1,h₁.1.2.trans hy.sub,hy.cut⟩ + have hY₂ : Safe (s.win u) j J f := + ⟨hy.within,hy.depth_ok,hW.2.2 u _ hu,le_rfl,h₁.2.1,h₁.2.2.trans hy.sub,hy.cut⟩ + have hww : stage j (s.win u) < stage j s := by + let N := totalUse j + have h4 : s.fund.uses+1 ≤ N := by + have HH := (Nat.le_ceil (capA*H j)) + have He : ((s.fund.uses+1:ℕ):ℝ) ≤ ↑N := by push_cast; exact hW.2.1.trans HH + exact_mod_cast He + have h3 := h₂ (s.win u) + let L := Fintype.card (Grid M)+1 + have h5 : (s.win u).fund.uses = s.fund.uses+1 := rfl + have h6 : (playing (s.win u)).card < L := Nat.lt_succ_of_le (Finset.card_le_univ _) + unfold stage; rw [h5,show totalUse j - s.fund.uses= N-(s.fund.uses+1)+1 by omega]; simp only [add_mul, one_mul] + dsimp only [N,L] at *; omega + have hll : stage j (s.lose u) < stage j s := by + have h4 : (playing (s.lose u)).card < (playing s).card := by + apply Finset.card_lt_card + refine Finset.ssubset_iff_of_subset (s₁:=playing (s.lose u)) ?_ |>.mpr ?_ + · intro x hx + simp only [playing,Finset.mem_filter,Finset.mem_univ,true_and] at * + exact lt_of_lt_of_le hx (h₁.1.2 _) + have HE := lt_of_lt_of_le (show 0 < beta u.ix /2 from by have := beta_pos u.ix; positivity) hu.bias + obtain ⟨x,hx,hx'⟩ := (prob_pos_iff _ _).mp HE + have hf : 0 < s.wage x := by + by_contra! H + have hh := H.antisymm (hh1 x) + rw [hV _,wt,hh,mul_zero,zero_div] at hx' + exact lt_irrefl _ hx' + refine ⟨x,?_,?_⟩ <;> simp [playing,hf,lose_eq, hx] + unfold stage + rw [show (s.lose u).fund=s.fund from rfl] + have hh := h₂ (s.lose u); omega + obtain ⟨l,hl'⟩ := Now _ hll (s.lose u) hi hY₁ rfl + obtain ⟨r,hr'⟩ := Now _ hww (s.win u) hi hY₂ rfl + exact ⟨Play.attempt s u l r, hy, ⟨ho,h,ha⟩,hl,hu,hl',hr'⟩ + +end FourierLLogL.Dyadic.Procedure + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Activate.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Activate.lean index df6e44ee58d831a1dff8b75d113813175592ce55..1092490661ab3d8b85b1d5ea9a0fedd4b8b32fe9 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Activate.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Activate.lean @@ -1,209 +1,209 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Rec - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart -variable {M j J : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -namespace Plot - -structure Comes (w : Board M) (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) : Prop where - valid : w.chart.Sound - stored : - ∀ q, w.previous=some q → - Safe q j J f ∧ work q j J f ∧ settled q j ∧ - q.here.lev < M ∧ below s.here q.here ∧ - q.fund=s.fund ∧ q.live=s.live ∧ q.norm=s.norm - -lemma vacant_comes (s : Scene M) : Comes (vacant M) s j J f := by - refine ⟨classic_sound _,?_⟩ - simp [vacant] -lemma comes_down (p : Plot) {s q : Scene M} {l} - (h : Following j J f (.ran s q l)) {g : Board M} - (hp : Comes g s j J f) (b) : - Comes (p.turn g q) (q.smaller b) j J f := by - have hZ := zoom_spec _ hp.valid p.ix p.main h - obtain ⟨ha,hb,hc,hv,hE,hk⟩ := h - refine ⟨hZ.sonic,?_⟩ - intro x hx - simp only [turn] at hx - split_ifs at hx with hX hY - · obtain rfl := Option.some.inj hx - exact ⟨hc,hv,hE,(rooted _ (hk false)).within.2,child_below _ _,rfl,rfl,rfl⟩ - obtain ⟨ha,hc,hE,h₁,h₂,h₃,h₄,h₅⟩ := hp.stored _ hx - change _∧_∧_∧_∧below (q.here.child b) _ ∧ x.fund=q.fund ∧ x.live=q.live ∧ x.norm=q.norm - rw [hb.1.fund,hb.1.flat,hb.1.mode,hb.1.here] - exact ⟨ha,hc,hE,h₁,below_trans (child_below _ _) h₂,h₃,h₄,h₅⟩ - -def catches (p : Plot) (s : Scene M) (c : Board M) (j : ℕ) (x : Grid M) := - intense p.main s (side p.ix) x ∧ atReach (p.now c s).items (spotLimit s j p.ix p.main) x - -def aligned (p : Plot) (s : Scene M) (j : ℕ) (x y : Grid M) := - let r := 2*spotLimit s j p.ix p.main - if p.main then ∃ k, delta s.fund.mood k ≤ sepRad j ∧ rad (x-y-k) ≤ r else rad (x-y) ≤ r - -lemma effort_bounds : - ∀ᶠ j : ℕ in atTop, ∀ p : Plot, (p.main=true → p.ix ≤ mainIx j) → - 64*tolerance j p.ix p.main +20*sharp p.ix ≤ amp p.ix p.main / 2 := by - filter_upwards [h_large 100000] with j hg p hp - let b := side p.ix - have he := side_pos p.ix - have h6 : b^6 ≤ b^2 := pow_le_pow_of_le_one he.le (side_le _) (by omega) - have h4 : b^4 ≤ b := by simpa using pow_le_pow_of_le_one he.le (side_le _) (by omega : 1 ≤ 4) - cases hm : p.main with - | false => - have hb : b^2 ≤ b := by simpa using pow_le_pow_of_le_one he.le (side_le _) (by omega : 1 ≤ 2) - change 64*(b^4/65536)+20*(b^6/32768) ≤ b/2 - nlinarith [show 0 < b from he] - | true => - change 64*(1/Thermal.H j^40)+20*(b^6/32768) ≤ b^2/2 - have hx := (main_bounds j (show p.ix ≤ testIx j from Nat.le_trans (hp hm) (by unfold testIx; omega))).1 - change _ ≤ b^2/100 at hx - let a := Thermal.H j - have hh : a ≤ a^8 := by simpa using pow_le_pow_right₀ (H_ge j) (by omega : 1 ≤ 8) - have ht : 64*(1/a^40) ≤ b^2/4 := by - apply le_trans (b:=25*((1/6400)/a^32)) _ (by unfold a; linarith) - have hp' : 0 < a := H_pos j - field_simp - change 100000 ≤ a at hg - nlinarith - unfold a at * - nlinarith [sq_nonneg b] - -lemma currently : - ∀ᶠ j : ℕ in atTop, ∀ (p : Plot), (p.main=true → p.ix ≤ mainIx j) → - ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s q : Scene M, ∀ l, - Following j J f (.ran s q l) → ∀ g : Board M, Comes g s j J f → - cardStep p (turn p g q) → ∀ x, catches p q g j x → - ∃ v : Scene M, (turn p g q).previous=some v ∧ below q.here v.here ∧ - ∃ y∈net v j p.ix p.main, aligned p q j x y := by - filter_upwards [effort_bounds] with j h p hp M J f s q l hl c hc hC x hx - classical - obtain ⟨ha,hb,hh,hd,hQ,hk⟩ := hl - obtain ⟨⟨b,hy⟩,v,hv,hD⟩ := hx - let t := q.smaller b - let B := p.main - let r := spotLimit q j p.ix B - have he₁ := share_small hd - have hv' := (rooted _ (hk b)) - have hX : ReadyAt t := ⟨hv'.within,hv'.positive⟩ - have hqt : q.here.lev < M := (rooted _ (hk false)).within.2 - have hτ := h p hp - let V := amp p.ix B - let L := tolerance j p.ix p.main - have hD' := share_child q hh.within.1 hqt hh.positive he₁.1 he₁.2 b - have HX : V ≤ magnitude B t v+64*L := by - apply le_trans hy - have H := magnitude_near B t x v hX he₁.1.le 4 hD' - have HE : 4*(16*t.here.len*rad (x-v)) ≤ 64*L := by - rw [show t.here.len=q.here.len/2 from child_len _ _] - have hp := q.here.len_pos; change rad _ ≤ L*q.here.invlen at hD - calc - _ ≤ 4*(16*(q.here.len/2)*(L*q.here.invlen)) := by gcongr - _ = 32*L := by rw [rinv]; field_simp; ring - _ ≤ 64*L := by linarith [show 0 < L from tolerance_pos _ _ _] - linarith - have he : strength B (side (p.ix+1)) ≤ V/2 := by - unfold V amp; rw [side_succ] - cases B with - | false => exact le_rfl - | true => - change (_/_)^2 ≤ _^2/2 - nlinarith [side_pos p.ix] - have HW (s : Scene M) (hh : below q.here s.here) (hv : s.fund=q.fund) - (H : intense p.main s (side (p.ix+1)) v) : - ∃ y∈net s j p.ix p.main, aligned p q j x y := by - obtain ⟨y,hy,hQ⟩ := (net_spec s j p.ix p.main).2.2 _ H - have hR : spotLimit s j p.ix p.main ≤ spotLimit q j p.ix p.main := - mul_le_mul_of_nonneg_left (scale_below hh) (tolerance_pos _ _ _).le - refine ⟨y,hy,?_⟩ - dsimp only [aligned] - unfold adjacent at hQ - by_cases Hz : p.main=true - · rw [ite_eq_left Hz] at hQ ⊢ - obtain ⟨k,hk,hv'⟩ := hQ - refine ⟨k,hv ▸ hk,?_⟩ - have HH := rad_add (x-v) (v-y-k) - rw [show x-v+(v-y-k)=x-y-k by ring] at HH; linarith - · rw [ite_eq_right Hz] at hQ ⊢ - have HH := rad_add (x-v) (v-y); rw [sub_add_sub_cancel] at HH; linarith - rw [prior_step _ _ _ hC] - change 64*L+20*sharp p.ix ≤ V/2 at hτ - split_ifs with hF - · refine ⟨q,rfl,below_refl _,HW q (below_refl _) rfl ⟨b,?_⟩⟩ - change strength B _ ≤ magnitude B t v - linarith [sharp_pos p.ix] - unfold freshAt at hF - cases hA : c.previous with - | none => simp [hA] at hF - | some w => - obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8⟩ := hc.stored w hA - have he₀ : below q.here w.here := hb.1.here.symm ▸ h5 - have he' : w.fund=q.fund := h6.trans hb.1.fund.symm - refine ⟨w,rfl,he₀,HW w he₀ he' ?_⟩ - rw [hA] at hF - have hF' : (zoom q p.ix p.main c.chart).items=c.chart.items ∧ ∀ b, - p.closer c (q.smaller b) w := by push Not at hF; exact hF - have ht := share_small h2 - have H : magnitude B t v ≤ magnitude B w v +20*sharp p.ix := by - have hZ : (turn p c q).chart.items=c.chart.items := hF'.1 - have hE : (now p c q).items=c.chart.items := hF'.1 - apply close_display t w B c.chart.items p.ix p.cap _ _ _ _ hX - ⟨h1.within,h1.positive⟩ he₁.1.le ht.1.le hD' (ht.2.trans (by norm_num)) - · rw [← hZ]; exact hC - · rwa [hE] at hv - exact hF'.2 b - obtain ⟨c,hq⟩ := sup_children B w h1.within.1 h4 h1.positive ht.1.le v - exact ⟨c,by change strength B _ ≤ _; linarith⟩ - - -def latency (p : Plot) (j : ℕ) := - 1000*(1+p.cap+p.ix+ell j) -lemma waiting (p : Plot) (j : ℕ) (s t : Node) - (h : s.lev+latency p j ≤ t.lev) - (q : ℕ) (hh : q ≤ rangeIx p.cap) : - horizon s p.ix q ≤ tolerance j p.ix p.main * t.invlen := by - let c := p.cap; let i := p.ix - let n := latency p j - let k := 16+4*i+40*ell j - unfold horizon speed band - have h1 : 1+(q:ℝ) ≤ 2^(c+1) := by - suffices HH : 1+q ≤ 2^(c+1) by exact_mod_cast HH - have H : 0 < 2^c := by positivity - change q ≤ 2^c at hh; rw [pow_succ]; omega - have h2 : 2*((offset i q:ℝ)+10) ≤ 2^(i+c+10) := by - suffices HH : 2*(offset i q+10) ≤ 2^(i+c+10) by exact_mod_cast HH - unfold offset; change q ≤ 2^c at hh - have H1 : i ≤ 2^(i+c) := Nat.le_trans Nat.lt_two_pow_self.le (Nat.pow_le_pow_right (by omega) (by omega)) - have H2 : q ≤ 2^(i+c) := Nat.le_trans hh (Nat.pow_le_pow_right (by omega) (by omega)) - rw [pow_add]; have H : 0 < 2^(i+c) := by positivity - norm_num; omega - have H : (1:ℝ)/2^k ≤ tolerance j i p.main := by - unfold tolerance - split - · unfold sig slow - push_cast; rw [← pow_mul] - apply one_div_le_one_div_of_le (by positivity); unfold k; apply pow_le_pow_right₀ (by norm_num) (by omega) - rw [side_pow_two,inv_pow,← pow_mul] - rw [one_div,div_eq_mul_inv,← mul_inv] - unfold k; rw [inv_le_inv₀ (by positivity) (by positivity)] - rw [show (65536:ℝ)=2^16 by norm_num,← pow_add]; apply pow_le_pow_right₀ (by norm_num) (by omega) - apply le_trans _ (mul_le_mul_of_nonneg_right H t.invlen_pos.le) - unfold invlen - apply le_trans (b:=2^(c+1) * ((2:ℝ)^(s.lev+reach i+10)*2^(i+c+10))) (by gcongr) - rw [← pow_add,← pow_add,one_div,mul_comm _ ((2:ℝ)^t.lev),←div_eq_mul_inv] - apply (le_div_iff₀ (by positivity : 0<(2:ℝ)^k)).mpr - rw [← pow_add] - apply pow_le_pow_right₀ (by norm_num) - unfold k i c latency reach at * - omega - -end Plot -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Rec + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart +variable {M j J : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +namespace Plot + +structure Comes (w : Board M) (s : Scene M) (j J : ℕ) (f : Grid M → ℝ) : Prop where + valid : w.chart.Sound + stored : + ∀ q, w.previous=some q → + Safe q j J f ∧ work q j J f ∧ settled q j ∧ + q.here.lev < M ∧ below s.here q.here ∧ + q.fund=s.fund ∧ q.live=s.live ∧ q.norm=s.norm + +lemma vacant_comes (s : Scene M) : Comes (vacant M) s j J f := by + refine ⟨classic_sound _,?_⟩ + simp [vacant] +lemma comes_down (p : Plot) {s q : Scene M} {l} + (h : Following j J f (.ran s q l)) {g : Board M} + (hp : Comes g s j J f) (b) : + Comes (p.turn g q) (q.smaller b) j J f := by + have hZ := zoom_spec _ hp.valid p.ix p.main h + obtain ⟨ha,hb,hc,hv,hE,hk⟩ := h + refine ⟨hZ.sonic,?_⟩ + intro x hx + simp only [turn] at hx + split_ifs at hx with hX hY + · obtain rfl := Option.some.inj hx + exact ⟨hc,hv,hE,(rooted _ (hk false)).within.2,child_below _ _,rfl,rfl,rfl⟩ + obtain ⟨ha,hc,hE,h₁,h₂,h₃,h₄,h₅⟩ := hp.stored _ hx + change _∧_∧_∧_∧below (q.here.child b) _ ∧ x.fund=q.fund ∧ x.live=q.live ∧ x.norm=q.norm + rw [hb.1.fund,hb.1.flat,hb.1.mode,hb.1.here] + exact ⟨ha,hc,hE,h₁,below_trans (child_below _ _) h₂,h₃,h₄,h₅⟩ + +def catches (p : Plot) (s : Scene M) (c : Board M) (j : ℕ) (x : Grid M) := + intense p.main s (side p.ix) x ∧ atReach (p.now c s).items (spotLimit s j p.ix p.main) x + +def aligned (p : Plot) (s : Scene M) (j : ℕ) (x y : Grid M) := + let r := 2*spotLimit s j p.ix p.main + if p.main then ∃ k, delta s.fund.mood k ≤ sepRad j ∧ rad (x-y-k) ≤ r else rad (x-y) ≤ r + +lemma effort_bounds : + ∀ᶠ j : ℕ in atTop, ∀ p : Plot, (p.main=true → p.ix ≤ mainIx j) → + 64*tolerance j p.ix p.main +20*sharp p.ix ≤ amp p.ix p.main / 2 := by + filter_upwards [h_large 100000] with j hg p hp + let b := side p.ix + have he := side_pos p.ix + have h6 : b^6 ≤ b^2 := pow_le_pow_of_le_one he.le (side_le _) (by omega) + have h4 : b^4 ≤ b := by simpa using pow_le_pow_of_le_one he.le (side_le _) (by omega : 1 ≤ 4) + cases hm : p.main with + | false => + have hb : b^2 ≤ b := by simpa using pow_le_pow_of_le_one he.le (side_le _) (by omega : 1 ≤ 2) + change 64*(b^4/65536)+20*(b^6/32768) ≤ b/2 + nlinarith [show 0 < b from he] + | true => + change 64*(1/Thermal.H j^40)+20*(b^6/32768) ≤ b^2/2 + have hx := (main_bounds j (show p.ix ≤ testIx j from Nat.le_trans (hp hm) (by unfold testIx; omega))).1 + change _ ≤ b^2/100 at hx + let a := Thermal.H j + have hh : a ≤ a^8 := by simpa using pow_le_pow_right₀ (H_ge j) (by omega : 1 ≤ 8) + have ht : 64*(1/a^40) ≤ b^2/4 := by + apply le_trans (b:=25*((1/6400)/a^32)) _ (by unfold a; linarith) + have hp' : 0 < a := H_pos j + field_simp + change 100000 ≤ a at hg + nlinarith + unfold a at * + nlinarith [sq_nonneg b] + +lemma currently : + ∀ᶠ j : ℕ in atTop, ∀ (p : Plot), (p.main=true → p.ix ≤ mainIx j) → + ∀ M J : ℕ, ∀ f : Grid M → ℝ, ∀ s q : Scene M, ∀ l, + Following j J f (.ran s q l) → ∀ g : Board M, Comes g s j J f → + cardStep p (turn p g q) → ∀ x, catches p q g j x → + ∃ v : Scene M, (turn p g q).previous=some v ∧ below q.here v.here ∧ + ∃ y∈net v j p.ix p.main, aligned p q j x y := by + filter_upwards [effort_bounds] with j h p hp M J f s q l hl c hc hC x hx + classical + obtain ⟨ha,hb,hh,hd,hQ,hk⟩ := hl + obtain ⟨⟨b,hy⟩,v,hv,hD⟩ := hx + let t := q.smaller b + let B := p.main + let r := spotLimit q j p.ix B + have he₁ := share_small hd + have hv' := (rooted _ (hk b)) + have hX : ReadyAt t := ⟨hv'.within,hv'.positive⟩ + have hqt : q.here.lev < M := (rooted _ (hk false)).within.2 + have hτ := h p hp + let V := amp p.ix B + let L := tolerance j p.ix p.main + have hD' := share_child q hh.within.1 hqt hh.positive he₁.1 he₁.2 b + have HX : V ≤ magnitude B t v+64*L := by + apply le_trans hy + have H := magnitude_near B t x v hX he₁.1.le 4 hD' + have HE : 4*(16*t.here.len*rad (x-v)) ≤ 64*L := by + rw [show t.here.len=q.here.len/2 from child_len _ _] + have hp := q.here.len_pos; change rad _ ≤ L*q.here.invlen at hD + calc + _ ≤ 4*(16*(q.here.len/2)*(L*q.here.invlen)) := by gcongr + _ = 32*L := by rw [rinv]; field_simp; ring + _ ≤ 64*L := by linarith [show 0 < L from tolerance_pos _ _ _] + linarith + have he : strength B (side (p.ix+1)) ≤ V/2 := by + unfold V amp; rw [side_succ] + cases B with + | false => exact le_rfl + | true => + change (_/_)^2 ≤ _^2/2 + nlinarith [side_pos p.ix] + have HW (s : Scene M) (hh : below q.here s.here) (hv : s.fund=q.fund) + (H : intense p.main s (side (p.ix+1)) v) : + ∃ y∈net s j p.ix p.main, aligned p q j x y := by + obtain ⟨y,hy,hQ⟩ := (net_spec s j p.ix p.main).2.2 _ H + have hR : spotLimit s j p.ix p.main ≤ spotLimit q j p.ix p.main := + mul_le_mul_of_nonneg_left (scale_below hh) (tolerance_pos _ _ _).le + refine ⟨y,hy,?_⟩ + dsimp only [aligned] + unfold adjacent at hQ + by_cases Hz : p.main=true + · rw [ite_eq_left Hz] at hQ ⊢ + obtain ⟨k,hk,hv'⟩ := hQ + refine ⟨k,hv ▸ hk,?_⟩ + have HH := rad_add (x-v) (v-y-k) + rw [show x-v+(v-y-k)=x-y-k by ring] at HH; linarith + · rw [ite_eq_right Hz] at hQ ⊢ + have HH := rad_add (x-v) (v-y); rw [sub_add_sub_cancel] at HH; linarith + rw [prior_step _ _ _ hC] + change 64*L+20*sharp p.ix ≤ V/2 at hτ + split_ifs with hF + · refine ⟨q,rfl,below_refl _,HW q (below_refl _) rfl ⟨b,?_⟩⟩ + change strength B _ ≤ magnitude B t v + linarith [sharp_pos p.ix] + unfold freshAt at hF + cases hA : c.previous with + | none => simp [hA] at hF + | some w => + obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8⟩ := hc.stored w hA + have he₀ : below q.here w.here := hb.1.here.symm ▸ h5 + have he' : w.fund=q.fund := h6.trans hb.1.fund.symm + refine ⟨w,rfl,he₀,HW w he₀ he' ?_⟩ + rw [hA] at hF + have hF' : (zoom q p.ix p.main c.chart).items=c.chart.items ∧ ∀ b, + p.closer c (q.smaller b) w := by push Not at hF; exact hF + have ht := share_small h2 + have H : magnitude B t v ≤ magnitude B w v +20*sharp p.ix := by + have hZ : (turn p c q).chart.items=c.chart.items := hF'.1 + have hE : (now p c q).items=c.chart.items := hF'.1 + apply close_display t w B c.chart.items p.ix p.cap _ _ _ _ hX + ⟨h1.within,h1.positive⟩ he₁.1.le ht.1.le hD' (ht.2.trans (by norm_num)) + · rw [← hZ]; exact hC + · rwa [hE] at hv + exact hF'.2 b + obtain ⟨c,hq⟩ := sup_children B w h1.within.1 h4 h1.positive ht.1.le v + exact ⟨c,by change strength B _ ≤ _; linarith⟩ + + +def latency (p : Plot) (j : ℕ) := + 1000*(1+p.cap+p.ix+ell j) +lemma waiting (p : Plot) (j : ℕ) (s t : Node) + (h : s.lev+latency p j ≤ t.lev) + (q : ℕ) (hh : q ≤ rangeIx p.cap) : + horizon s p.ix q ≤ tolerance j p.ix p.main * t.invlen := by + let c := p.cap; let i := p.ix + let n := latency p j + let k := 16+4*i+40*ell j + unfold horizon speed band + have h1 : 1+(q:ℝ) ≤ 2^(c+1) := by + suffices HH : 1+q ≤ 2^(c+1) by exact_mod_cast HH + have H : 0 < 2^c := by positivity + change q ≤ 2^c at hh; rw [pow_succ]; omega + have h2 : 2*((offset i q:ℝ)+10) ≤ 2^(i+c+10) := by + suffices HH : 2*(offset i q+10) ≤ 2^(i+c+10) by exact_mod_cast HH + unfold offset; change q ≤ 2^c at hh + have H1 : i ≤ 2^(i+c) := Nat.le_trans Nat.lt_two_pow_self.le (Nat.pow_le_pow_right (by omega) (by omega)) + have H2 : q ≤ 2^(i+c) := Nat.le_trans hh (Nat.pow_le_pow_right (by omega) (by omega)) + rw [pow_add]; have H : 0 < 2^(i+c) := by positivity + norm_num; omega + have H : (1:ℝ)/2^k ≤ tolerance j i p.main := by + unfold tolerance + split + · unfold sig slow + push_cast; rw [← pow_mul] + apply one_div_le_one_div_of_le (by positivity); unfold k; apply pow_le_pow_right₀ (by norm_num) (by omega) + rw [side_pow_two,inv_pow,← pow_mul] + rw [one_div,div_eq_mul_inv,← mul_inv] + unfold k; rw [inv_le_inv₀ (by positivity) (by positivity)] + rw [show (65536:ℝ)=2^16 by norm_num,← pow_add]; apply pow_le_pow_right₀ (by norm_num) (by omega) + apply le_trans _ (mul_le_mul_of_nonneg_right H t.invlen_pos.le) + unfold invlen + apply le_trans (b:=2^(c+1) * ((2:ℝ)^(s.lev+reach i+10)*2^(i+c+10))) (by gcongr) + rw [← pow_add,← pow_add,one_div,mul_comm _ ((2:ℝ)^t.lev),←div_eq_mul_inv] + apply (le_div_iff₀ (by positivity : 0<(2:ℝ)^k)).mpr + rw [← pow_add] + apply pow_le_pow_right₀ (by norm_num) + unfold k i c latency reach at * + omega + +end Plot +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Difference.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Difference.lean index 906a25648ecf186e1d1cc4a8dfe3d1f40b314ed6..71b93ae95347d925afa625a5b0153940d38aa8c3 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Difference.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Difference.lean @@ -1,126 +1,126 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Display -/-! # An unnormalized divergence paying for changes of portrait -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture -variable {M : ℕ} {α β : Type} [Fintype α] [Fintype β] -open scoped Classical - -def grain (a b : ℝ) := (a-b)^2/(a+b) -def cloud (a b : α → ℝ) := ∑ i, grain (a i) (b i) - -lemma grain_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ grain a b := - div_nonneg (sq_nonneg _) (by linarith) - -lemma cloud_nonneg (a b : α → ℝ) (ha : ∀ i, 0 ≤ a i) (hb : ∀ i, 0 ≤ b i) : - 0 ≤ cloud a b := Finset.sum_nonneg fun _ _ => grain_nonneg (ha _) (hb _) - -lemma cloud_same (a : α → ℝ) : cloud a a=0 := by simp [cloud,grain] - -lemma distance_cloud (a b : α → ℝ) (ha : ∀ i, 0 ≤ a i) (hb : ∀ i, 0 ≤ b i) : - (distance a b)^2 ≤ ((∑ i,a i)+ ∑ i,b i)*cloud a b := by - unfold distance cloud; rw [← Finset.sum_add_distrib] - refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ - (fun i _ => add_nonneg (ha _) (hb _)) (fun i _ => grain_nonneg (ha i) (hb i)) ?_ - intro i _ - rw [sq_abs,grain] - rcases lt_or_eq_of_le (add_nonneg (ha i) (hb i)) with hi|hi - · exact le_of_eq (by field_simp) - · have hx := ha i; have hy := hb i - rw [show a i = 0 by linarith,show b i = 0 by linarith]; simp - -private lemma shift_formula (a b : ℝ) (h : a+b ≠ 0) : - grain a b=a-3*b+4*b^2/(a+b) := by unfold grain; field_simp; ring - -lemma grain_shrinks (a b t : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (h : b ≤ a) (ht : 0 ≤ t) : - grain b t ≤ grain a t + 4*(a-b) := by - rcases eq_or_lt_of_le ht with H|H - · have HH (a : ℝ) : grain a 0=a := by - unfold grain; by_cases h : a=0 <;> simp [h, pow_two] - rw [← H,HH,HH]; linarith - · rw [shift_formula _ _ (by linarith),shift_formula _ _ (by linarith)] - apply le_of_sub_nonneg - have hx : 0 < (a+t)*(b+t) := by positivity - apply le_of_mul_le_mul_left ?_ hx - rw [mul_zero] - have HE : t^2 ≤ (a+t)*(b+t) := by nlinarith - have HT := mul_le_mul_of_nonneg_right HE (show 0 ≤ a-b by linarith) - have HZ : - (a+t)*(b+t)*((a-3*t+4*t^2/(a+t)+4*(a-b)) - (b-3*t+4*t^2/(b+t))) = - (a-b)*(5*((a+t)*(b+t))-4*t^2) := by field_simp; ring - rw [HZ]; nlinarith - -/-- comparison curvature cost at a spatial step is controlled by the Fisher charge -/ -lemma grain_average (a b m t : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (ht : 0 ≤ t) - (h : a+b=2*m) : - (grain a t+grain b t)/2 ≤ grain m t +4*((a-b)/2)^2/m := by - rcases eq_or_lt_of_le ht with H|H - · have he (a : ℝ) : grain a 0=a := by - unfold grain; by_cases h : a=0 <;> simp [h,pow_two] - rw [← H,he,he,he] - have HH : 0 ≤ 4*((a-b)/2)^2/m := div_nonneg (by positivity) (by linarith) - linarith - have hm : 0 ≤ m := by linarith - rcases eq_or_lt_of_le hm with Hm|Hm - · have he : a=m := by linarith - have hh : b=m := by linarith - rw [he,hh]; ring_nf; exact le_rfl - rw [shift_formula _ _ (by linarith),shift_formula _ _ (by linarith),shift_formula _ _ (by linarith)] - let d := (a-b)/2 - have he : t^2*m ≤ (a+t)*(b+t)*(m+t) := by - have hH : t^2 ≤ (a+t)*(b+t) := by nlinarith - exact mul_le_mul hH (by linarith) hm (by positivity) - have hi : - ((a-3*t+4*t^2/(a+t))+(b-3*t+4*t^2/(b+t)))/2 - (m-3*t+4*t^2/(m+t)) - = 4*t^2*d^2/((a+t)*(b+t)*(m+t)) := by - unfold d - field_simp - rw [show m=(a+b)/2 by linarith] - ring - rw [add_comm (m-3*t+4*t^2/(m+t))]; apply sub_le_iff_le_add.mp - - rw [hi] - change 4*t^2*d^2/((a+t)*(b+t)*(m+t)) ≤ 4*d^2/m - apply (div_le_div_iff₀ (by positivity) Hm).mpr - nlinarith [mul_le_mul_of_nonneg_left he (sq_nonneg d)] - -lemma cloud_kill {s q : Scene M} (h : Less s q) (hh : ReadyAt s) (hv : ReadyAt q) - (hp : 0 ≤ s.norm) (k : Grid M → Law α) (a : α → ℝ) (ha : ∀ i, 0 ≤ a i) : - cloud (hist q k) a ≤ cloud (hist s k) a+4*(share s-share q) := by - have H : 0 ≤ q.norm := by rw [h.flat]; exact hp - rw [← total_hist _ hh.1 k, ← total_hist _ hv.1 k] - unfold cloud; rw [← Finset.sum_sub_distrib,Finset.mul_sum,←Finset.sum_add_distrib] - exact Finset.sum_le_sum fun i _ => grain_shrinks _ _ _ - (nonnegative_hist s hh.2 hp k i) (nonnegative_hist q hv.2 H k i) (hist_le h hh hp k i) (ha _) - -lemma cloud_drift (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) - (hs : 0 ≤ s.wage) (he : 0 ≤ s.norm) (k : Grid M → Law α) - (t : α → ℝ) (ht : ∀ i, 0 ≤ t i) : - (uniform Bool).Ex (fun b => cloud (hist (s.smaller b) k) t) ≤ - cloud (hist s k) t+4*fisher s k := by - let p (i) := hist (s.smaller false) k i; let q (i) := hist (s.smaller true) k i - let m (i) := hist s k i - have hi i : (grain (p i) (t i)+grain (q i) (t i))/2 ≤ - grain (m i) (t i) +4*(Procedure.diff s k i)^2/m i := by - apply grain_average - · exact nonnegative_hist (s.smaller false) hs he k _ - · exact nonnegative_hist (s.smaller true) hs he k _ - · exact ht _ - · exact hist_split _ hp hh k _ - calc - _ = ∑ i,(grain (p i) (t i)+grain (q i) (t i))/2 := by - unfold cloud; rw [← Finset.sum_div,Finset.sum_add_distrib,Ex,Fintype.sum_bool] - simp only [uniform_apply,Fintype.card_bool,Nat.cast_ofNat] - unfold p q; ring - _ ≤ _ := Finset.sum_le_sum (s:=Finset.univ) fun i _ => hi i - _ = _ := by simp_rw [fisher,cloud,mul_div_assoc,Finset.mul_sum,Finset.sum_add_distrib]; rfl - -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Display +/-! # An unnormalized divergence paying for changes of portrait -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture +variable {M : ℕ} {α β : Type} [Fintype α] [Fintype β] +open scoped Classical + +def grain (a b : ℝ) := (a-b)^2/(a+b) +def cloud (a b : α → ℝ) := ∑ i, grain (a i) (b i) + +lemma grain_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ grain a b := + div_nonneg (sq_nonneg _) (by linarith) + +lemma cloud_nonneg (a b : α → ℝ) (ha : ∀ i, 0 ≤ a i) (hb : ∀ i, 0 ≤ b i) : + 0 ≤ cloud a b := Finset.sum_nonneg fun _ _ => grain_nonneg (ha _) (hb _) + +lemma cloud_same (a : α → ℝ) : cloud a a=0 := by simp [cloud,grain] + +lemma distance_cloud (a b : α → ℝ) (ha : ∀ i, 0 ≤ a i) (hb : ∀ i, 0 ≤ b i) : + (distance a b)^2 ≤ ((∑ i,a i)+ ∑ i,b i)*cloud a b := by + unfold distance cloud; rw [← Finset.sum_add_distrib] + refine Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul _ + (fun i _ => add_nonneg (ha _) (hb _)) (fun i _ => grain_nonneg (ha i) (hb i)) ?_ + intro i _ + rw [sq_abs,grain] + rcases lt_or_eq_of_le (add_nonneg (ha i) (hb i)) with hi|hi + · exact le_of_eq (by field_simp) + · have hx := ha i; have hy := hb i + rw [show a i = 0 by linarith,show b i = 0 by linarith]; simp + +private lemma shift_formula (a b : ℝ) (h : a+b ≠ 0) : + grain a b=a-3*b+4*b^2/(a+b) := by unfold grain; field_simp; ring + +lemma grain_shrinks (a b t : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (h : b ≤ a) (ht : 0 ≤ t) : + grain b t ≤ grain a t + 4*(a-b) := by + rcases eq_or_lt_of_le ht with H|H + · have HH (a : ℝ) : grain a 0=a := by + unfold grain; by_cases h : a=0 <;> simp [h, pow_two] + rw [← H,HH,HH]; linarith + · rw [shift_formula _ _ (by linarith),shift_formula _ _ (by linarith)] + apply le_of_sub_nonneg + have hx : 0 < (a+t)*(b+t) := by positivity + apply le_of_mul_le_mul_left ?_ hx + rw [mul_zero] + have HE : t^2 ≤ (a+t)*(b+t) := by nlinarith + have HT := mul_le_mul_of_nonneg_right HE (show 0 ≤ a-b by linarith) + have HZ : + (a+t)*(b+t)*((a-3*t+4*t^2/(a+t)+4*(a-b)) - (b-3*t+4*t^2/(b+t))) = + (a-b)*(5*((a+t)*(b+t))-4*t^2) := by field_simp; ring + rw [HZ]; nlinarith + +/-- comparison curvature cost at a spatial step is controlled by the Fisher charge -/ +lemma grain_average (a b m t : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (ht : 0 ≤ t) + (h : a+b=2*m) : + (grain a t+grain b t)/2 ≤ grain m t +4*((a-b)/2)^2/m := by + rcases eq_or_lt_of_le ht with H|H + · have he (a : ℝ) : grain a 0=a := by + unfold grain; by_cases h : a=0 <;> simp [h,pow_two] + rw [← H,he,he,he] + have HH : 0 ≤ 4*((a-b)/2)^2/m := div_nonneg (by positivity) (by linarith) + linarith + have hm : 0 ≤ m := by linarith + rcases eq_or_lt_of_le hm with Hm|Hm + · have he : a=m := by linarith + have hh : b=m := by linarith + rw [he,hh]; ring_nf; exact le_rfl + rw [shift_formula _ _ (by linarith),shift_formula _ _ (by linarith),shift_formula _ _ (by linarith)] + let d := (a-b)/2 + have he : t^2*m ≤ (a+t)*(b+t)*(m+t) := by + have hH : t^2 ≤ (a+t)*(b+t) := by nlinarith + exact mul_le_mul hH (by linarith) hm (by positivity) + have hi : + ((a-3*t+4*t^2/(a+t))+(b-3*t+4*t^2/(b+t)))/2 - (m-3*t+4*t^2/(m+t)) + = 4*t^2*d^2/((a+t)*(b+t)*(m+t)) := by + unfold d + field_simp + rw [show m=(a+b)/2 by linarith] + ring + rw [add_comm (m-3*t+4*t^2/(m+t))]; apply sub_le_iff_le_add.mp + + rw [hi] + change 4*t^2*d^2/((a+t)*(b+t)*(m+t)) ≤ 4*d^2/m + apply (div_le_div_iff₀ (by positivity) Hm).mpr + nlinarith [mul_le_mul_of_nonneg_left he (sq_nonneg d)] + +lemma cloud_kill {s q : Scene M} (h : Less s q) (hh : ReadyAt s) (hv : ReadyAt q) + (hp : 0 ≤ s.norm) (k : Grid M → Law α) (a : α → ℝ) (ha : ∀ i, 0 ≤ a i) : + cloud (hist q k) a ≤ cloud (hist s k) a+4*(share s-share q) := by + have H : 0 ≤ q.norm := by rw [h.flat]; exact hp + rw [← total_hist _ hh.1 k, ← total_hist _ hv.1 k] + unfold cloud; rw [← Finset.sum_sub_distrib,Finset.mul_sum,←Finset.sum_add_distrib] + exact Finset.sum_le_sum fun i _ => grain_shrinks _ _ _ + (nonnegative_hist s hh.2 hp k i) (nonnegative_hist q hv.2 H k i) (hist_le h hh hp k i) (ha _) + +lemma cloud_drift (s : Scene M) (hp : s.here.valid) (hh : s.here.lev < M) + (hs : 0 ≤ s.wage) (he : 0 ≤ s.norm) (k : Grid M → Law α) + (t : α → ℝ) (ht : ∀ i, 0 ≤ t i) : + (uniform Bool).Ex (fun b => cloud (hist (s.smaller b) k) t) ≤ + cloud (hist s k) t+4*fisher s k := by + let p (i) := hist (s.smaller false) k i; let q (i) := hist (s.smaller true) k i + let m (i) := hist s k i + have hi i : (grain (p i) (t i)+grain (q i) (t i))/2 ≤ + grain (m i) (t i) +4*(Procedure.diff s k i)^2/m i := by + apply grain_average + · exact nonnegative_hist (s.smaller false) hs he k _ + · exact nonnegative_hist (s.smaller true) hs he k _ + · exact ht _ + · exact hist_split _ hp hh k _ + calc + _ = ∑ i,(grain (p i) (t i)+grain (q i) (t i))/2 := by + unfold cloud; rw [← Finset.sum_div,Finset.sum_add_distrib,Ex,Fintype.sum_bool] + simp only [uniform_apply,Fintype.card_bool,Nat.cast_ofNat] + unfold p q; ring + _ ≤ _ := Finset.sum_le_sum (s:=Finset.univ) fun i _ => hi i + _ = _ := by simp_rw [fisher,cloud,mul_div_assoc,Finset.mul_sum,Finset.sum_add_distrib]; rfl + +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Display.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Display.lean index aeab821ea972afc095a066440f978a7bbcb333fe..d83e383c88fa8d2d86fb216e67417b0250e2bce6 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Display.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Display.lean @@ -1,96 +1,96 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Measure - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture -variable {M : ℕ} {α β : Type} [Fintype α] [Fintype β] -open scoped Classical - -def rangeIx (m : ℕ) := 2^m -def precision (i m : ℕ) := 25+6*i+m -def sharp (i : ℕ) := side i ^6 / 32768 -def portrait (b : Bool) (s : Scene M) (d : Finset (Grid M)) (i m : ℕ) := - hist s (sight s b d (precision i m)) - -def distance (f g : α → ℝ) := ∑ x, |f x-g x| - -lemma sharp_pos (i : ℕ) : 0 < sharp i := div_pos (pow_pos (side_pos i) _) (by norm_num) - -lemma coordinate_le (i m : ℕ) {n : ℕ} (h : n ≤ rangeIx m) : - (n:ℝ)*(8*side (precision i m)) ≤ sharp i := by - apply le_trans (b:= (2:ℝ)^m*(8*side (precision i m))) (by - apply mul_le_mul_of_nonneg_right (by exact_mod_cast h) - (by have := side_pos (precision i m); positivity)) - unfold precision sharp - have heq : side (25+6*i+m) = side 25 * side i ^6 * side m := by - simp [side,pow_add,pow_mul,mul_comm 6 i] - rw [heq] - rw [show (2:ℝ)^m * (8*(side 25 * side i^6 * side m))= - (side m*(2:ℝ)^m) *(8*side 25)*side i^6 by ring,side_card] - rw [one_mul, div_eq_inv_mul] - gcongr; norm_num [side] - -lemma image_income (s : Scene M) (d : Finset (Grid M)) (b : Bool) - (i m : ℕ) (f : Dome (precision i m) d → ℝ) : - ∑ x, portrait b s d i m x * f x = - rung s (channel (optic s b)) (fun x y => f (y,tiles (precision i m) d x)) := by - unfold portrait; rw [rung_hist] - unfold rung addMass - refine Finset.sum_congr rfl fun j _ => ?_ - simp_rw [mul_assoc,← Finset.mul_sum] - congr 1 - exact Ex_send _ _ _ - -lemma portrait_compare (s : Scene M) (b : Bool) (d : Finset (Grid M)) - (i m : ℕ) (c : Grid M → ℂ) (p : Grid M) - (F : TileN (precision i m) d → ℂ) (h : ∀ x, ‖chi p x-F (tiles (precision i m) d x)‖ ≤ sharp i) - (hc : ∀ i, ‖c i‖ ≤ 1) (hp : s.here.ok M) - (ha : 0 ≤ s.wage) (hv : 0 ≤ s.norm) (hs : share s ≤ 4) : - |sales b s c p - ∑ x, portrait b s d i m x * utility b (c x.1) (F x.2)| ≤ - 8*sharp i := by - rw [image_income _ _ _ _ _ (fun x => utility b (c x.1) (F x.2))] - unfold sales - apply le_trans _ (show share s*(2*sharp i) ≤ 8*sharp i from by nlinarith [sharp_pos i]) - apply rung_error s ha hv hp - intro j k - exact (utility_diff _ _ _ _ (hc k)).trans (mul_le_mul_of_nonneg_left (h j) (by norm_num)) - -/-- Scenes with close portrait. No equality of scene or area needed. -/ -lemma close_display (s t : Scene M) (b : Bool) (d : Finset (Grid M)) (i m : ℕ) (hd : d.card ≤ rangeIx m) - (p : Grid M) (he : enclosed d 1 p) (hh : distance (portrait b s d i m) (portrait b t d i m) ≤ sharp i) - (hs : ReadyAt s) (ht : ReadyAt t) (ha : 0 ≤ s.norm) (hb : 0 ≤ t.norm) - (hc : share s ≤ 4) (hC : share t ≤ 4) : - magnitude b s p ≤ magnitude b t p +20*sharp i := by - classical - obtain ⟨f,hf,hF⟩ := painted _ _ he (precision i m) - have hx (x) := (hF x).trans (coordinate_le _ _ hd) - obtain ⟨g,hg,hG⟩ := (test_complete b _ hs.2 ha p).2 - let Q (x : Dome (precision i m) d) := utility b (g x.1) (f x.2) - let P (s : Scene M) := portrait b s d i m - let V (s : Scene M) := ∑ x, P s x*Q x - have hV : V s ≤ V t +3*sharp i := by - rw [add_comm]; apply sub_le_iff_le_add.mp - unfold V; rw [← Finset.sum_sub_distrib] - calc - _ ≤ ∑ x, 3* |P s x-P t x| := Finset.sum_le_sum fun x _ => by - rw [← sub_mul] - apply le_trans (le_abs_self _) - rw [abs_mul,mul_comm] - exact mul_le_mul_of_nonneg_right (utility_bounds _ _ _ (hg _) (hf _)) (abs_nonneg _) - _ ≤ _ := by rw [← Finset.mul_sum]; exact mul_le_mul_of_nonneg_left hh (by norm_num) - have h1 : |sales b s g p - V s| ≤ 8*sharp i := - portrait_compare _ _ _ _ _ _ _ _ hx hg hs.1 hs.2 ha hc - have h2 : |sales b t g p - V t| ≤ 8*sharp i := - portrait_compare _ _ _ _ _ _ _ _ hx hg ht.1 ht.2 hb hC - have hi := (test_complete b t ht.2 hb p).1 _ hg - rw [hG] at h1 - linarith [sharp_pos i,(abs_le.mp h1).2,(abs_le.mp h2).1] - -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Measure + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture +variable {M : ℕ} {α β : Type} [Fintype α] [Fintype β] +open scoped Classical + +def rangeIx (m : ℕ) := 2^m +def precision (i m : ℕ) := 25+6*i+m +def sharp (i : ℕ) := side i ^6 / 32768 +def portrait (b : Bool) (s : Scene M) (d : Finset (Grid M)) (i m : ℕ) := + hist s (sight s b d (precision i m)) + +def distance (f g : α → ℝ) := ∑ x, |f x-g x| + +lemma sharp_pos (i : ℕ) : 0 < sharp i := div_pos (pow_pos (side_pos i) _) (by norm_num) + +lemma coordinate_le (i m : ℕ) {n : ℕ} (h : n ≤ rangeIx m) : + (n:ℝ)*(8*side (precision i m)) ≤ sharp i := by + apply le_trans (b:= (2:ℝ)^m*(8*side (precision i m))) (by + apply mul_le_mul_of_nonneg_right (by exact_mod_cast h) + (by have := side_pos (precision i m); positivity)) + unfold precision sharp + have heq : side (25+6*i+m) = side 25 * side i ^6 * side m := by + simp [side,pow_add,pow_mul,mul_comm 6 i] + rw [heq] + rw [show (2:ℝ)^m * (8*(side 25 * side i^6 * side m))= + (side m*(2:ℝ)^m) *(8*side 25)*side i^6 by ring,side_card] + rw [one_mul, div_eq_inv_mul] + gcongr; norm_num [side] + +lemma image_income (s : Scene M) (d : Finset (Grid M)) (b : Bool) + (i m : ℕ) (f : Dome (precision i m) d → ℝ) : + ∑ x, portrait b s d i m x * f x = + rung s (channel (optic s b)) (fun x y => f (y,tiles (precision i m) d x)) := by + unfold portrait; rw [rung_hist] + unfold rung addMass + refine Finset.sum_congr rfl fun j _ => ?_ + simp_rw [mul_assoc,← Finset.mul_sum] + congr 1 + exact Ex_send _ _ _ + +lemma portrait_compare (s : Scene M) (b : Bool) (d : Finset (Grid M)) + (i m : ℕ) (c : Grid M → ℂ) (p : Grid M) + (F : TileN (precision i m) d → ℂ) (h : ∀ x, ‖chi p x-F (tiles (precision i m) d x)‖ ≤ sharp i) + (hc : ∀ i, ‖c i‖ ≤ 1) (hp : s.here.ok M) + (ha : 0 ≤ s.wage) (hv : 0 ≤ s.norm) (hs : share s ≤ 4) : + |sales b s c p - ∑ x, portrait b s d i m x * utility b (c x.1) (F x.2)| ≤ + 8*sharp i := by + rw [image_income _ _ _ _ _ (fun x => utility b (c x.1) (F x.2))] + unfold sales + apply le_trans _ (show share s*(2*sharp i) ≤ 8*sharp i from by nlinarith [sharp_pos i]) + apply rung_error s ha hv hp + intro j k + exact (utility_diff _ _ _ _ (hc k)).trans (mul_le_mul_of_nonneg_left (h j) (by norm_num)) + +/-- Scenes with close portrait. No equality of scene or area needed. -/ +lemma close_display (s t : Scene M) (b : Bool) (d : Finset (Grid M)) (i m : ℕ) (hd : d.card ≤ rangeIx m) + (p : Grid M) (he : enclosed d 1 p) (hh : distance (portrait b s d i m) (portrait b t d i m) ≤ sharp i) + (hs : ReadyAt s) (ht : ReadyAt t) (ha : 0 ≤ s.norm) (hb : 0 ≤ t.norm) + (hc : share s ≤ 4) (hC : share t ≤ 4) : + magnitude b s p ≤ magnitude b t p +20*sharp i := by + classical + obtain ⟨f,hf,hF⟩ := painted _ _ he (precision i m) + have hx (x) := (hF x).trans (coordinate_le _ _ hd) + obtain ⟨g,hg,hG⟩ := (test_complete b _ hs.2 ha p).2 + let Q (x : Dome (precision i m) d) := utility b (g x.1) (f x.2) + let P (s : Scene M) := portrait b s d i m + let V (s : Scene M) := ∑ x, P s x*Q x + have hV : V s ≤ V t +3*sharp i := by + rw [add_comm]; apply sub_le_iff_le_add.mp + unfold V; rw [← Finset.sum_sub_distrib] + calc + _ ≤ ∑ x, 3* |P s x-P t x| := Finset.sum_le_sum fun x _ => by + rw [← sub_mul] + apply le_trans (le_abs_self _) + rw [abs_mul,mul_comm] + exact mul_le_mul_of_nonneg_right (utility_bounds _ _ _ (hg _) (hf _)) (abs_nonneg _) + _ ≤ _ := by rw [← Finset.mul_sum]; exact mul_le_mul_of_nonneg_left hh (by norm_num) + have h1 : |sales b s g p - V s| ≤ 8*sharp i := + portrait_compare _ _ _ _ _ _ _ _ hx hg hs.1 hs.2 ha hc + have h2 : |sales b t g p - V t| ≤ 8*sharp i := + portrait_compare _ _ _ _ _ _ _ _ hx hg ht.1 ht.2 hb hC + have hi := (test_complete b t ht.2 hb p).1 _ hg + rw [hG] at h1 + linarith [sharp_pos i,(abs_le.mp h1).2,(abs_le.mp h2).1] + +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Measure.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Measure.lean index 583cec6cd3546aba839a96b746ce7232eef433f2..794a66ae3664ae7473816ba1bdd4fadbc76bce95 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Measure.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Measure.lean @@ -1,156 +1,156 @@ -import OAI.Analysis.FourierLLogL.Capture.Dual -import OAI.Analysis.FourierLLogL.Capture.Curve - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture -variable {M : ℕ} -open scoped Classical - -abbrev RingN (e M : ℕ) := Grid (min e M) -def shade (e : ℕ) (x s : Grid M) : RingN e M := - down (min_le_right e M) (s*x) -def shine (e M : ℕ) (z : RingN e M) : ℂ := - chi 1 (raise (min_le_right e M) z) - -lemma shade_close (e : ℕ) (x s : Grid M) : ‖chi s x-shine e M (shade e x s)‖ ≤ 8*side e := by - let p := s*x - let r := raise (min_le_right e M) (shade e x s) - have HE : chi s x=chi 1 p := by unfold chi p; rw [one_mul] - have hp : ‖chi 1 p-chi 1 r‖ = ‖1-chi 1 (p-r)‖ := by - rw [show chi 1 p-chi 1 r = (chi 1 (p-r)-1)*chi 1 r by - rw [sub_mul,← chi_add,sub_add_cancel,one_mul],norm_mul,norm_chi,mul_one,norm_sub_rev] - have hi : Dyadic.diam (p-r) ≤ side e := by - let b := min e M - have hV : rad (p-r) ≤ 2^(M-b)-1 := snap_dist _ p - rcases le_total e M with he|he - · have hh : side M*(2:ℝ)^(M-e)=side e := (descale (p:=⟨e,0⟩) he).symm - rw [show b=e from min_eq_left he] at hV - unfold Dyadic.diam; rw [←hh] - exact mul_le_mul_of_nonneg_left (by linarith) (side_pos _).le - rw [show b=M from min_eq_right he] at hV - rw [Nat.sub_self,pow_zero,sub_self] at hV - unfold Dyadic.diam; have H := hV.antisymm (rad_nonneg _) - rw [H,mul_zero]; exact (side_pos _).le - rw [HE]; change ‖chi 1 p-chi 1 r‖ ≤ _ - rw [hp] - have HH := norm_phase_sq (1:Grid M) (p-r) - nlinarith [(cost_diam (p-r)).2,side_pos e,diam_nonneg (p-r)] - -abbrev TileN (e : ℕ) (d : Finset (Grid M)) := d → RingN e M - -def tiles (e : ℕ) (d : Finset (Grid M)) (x : Grid M) : TileN e d := - fun i => shade e x i - -abbrev Dome (e : ℕ) (d : Finset (Grid M)) := Grid M × TileN e d - -def sight (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) (x : Grid M) : - Law (Dome e d) := (channel (optic s b) x).send (fun y => (y,tiles e d x)) - - -private lemma see_as_H (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) : - let P := tracker s b - s.actor.privateH (sight s b d e) = - P.Hcond Prod.fst (fun i => (i.2, tiles e d i.1)) := by - intro P - let g : Grid M × Grid M → Grid M × Dome e d := - fun i => (i.1,i.2,tiles e d i.1) - have H : s.actor.withNext (sight s b d e) = P.send g := by - apply ext_of_Ex; intro f - simp_rw [P,tracker,Ex_send,Ex_withNext,sight,Ex_send] - rfl - rw [privateH,H,Hcond_send] - -lemma sight_budget (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) : - let v := deficit s (channel (optic s b)) - v ≤ deficit s (sight s b d e) ∧ deficit s (sight s b d e) ≤ v+d.card*e := by - unfold deficit; intro v - let P := tracker s b - let X : Grid M × Grid M → TileN e d := fun i => tiles e d i.1 - let g := fun i : Grid M × Grid M => (i.2,X i) - change v ≤ levels s-_ ∧ _ - rw [see_as_H] - have H1 : s.actor.privateH (channel (optic s b))=P.Hcond Prod.fst Prod.snd := rfl - change levels s-_≤levels s-P.Hcond _ g ∧ levels s-P.Hcond _ g ≤ levels s-_+_ - rw [H1] - have H := Hcond_antitone P Prod.fst (Y:=g) (Z:=Prod.snd) (fun i j hi => congrArg Prod.fst hi) - refine ⟨by linarith [H],?_⟩ - have hX := P.H_card X - have HX : P.H X ≤ (d.card:ℝ)*e := by - apply hX.trans - simp only [TileN,RingN,Grid,Fintype.card_fun,Fintype.card_coe,ZMod.card] - push_cast; rw [Real.log_pow,Real.log_pow] - gcongr - have he : (min e M:ℕ) ≤ e := by omega - have hi : ((min e M:ℕ):ℝ) ≤ e := by exact_mod_cast he - nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), - Nat.cast_nonneg (α:=ℝ) (min e M)] - have h₂ : - P.Hcond Prod.fst Prod.snd = P.Hcond (fun i => (i.1,X i)) Prod.snd := by - unfold Hcond; congr 1; apply H_partition; simp [X] - rw [h₂, Hcond_chain] - have H₃ : P.Hcond Prod.fst (fun i => (X i,i.2)) = P.Hcond Prod.fst g := - P.Hcond_right_partition _ (by simp [g,and_comm]) - rw [H₃] - linarith [Hcond_le_H P X Prod.snd] - - -private def switch (n : ℤ) (z : ℂ) := - if n=0 then 1 else if n=1 then z else conj z -theorem painted (u : Grid M) (d : Finset (Grid M)) (h : enclosed d 1 u) (e : ℕ) : - ∃ f : TileN e d → ℂ, (∀ i, ‖f i‖=1) ∧ - ∀ x, ‖chi u x-f (tiles e d x)‖ ≤ d.card*(8*side e) := by - obtain ⟨v,hv,rfl⟩ := h - let F (i : TileN e d) := ∏ x : d, switch (v x) (shine e M (i x)) - have Hs (i : ℤ) (z : ℂ) (h : ‖z‖=1) : ‖switch i z‖=1 := by unfold switch; split_ifs <;> simp [h] - refine ⟨F,?_,?_⟩ - · intro i; unfold F; rw [Complex.norm_prod] - exact (Finset.prod_eq_one fun j _ => Hs _ _ (norm_chi _ _)) - intro x - have h1 (i : d) : - chi (v i • (i.val)) x = switch (v i.val) (chi (i.val) x) := by - have hx := abs_le.mp (hv i.val i.property) - have hh : v i.val = -1 ∨ v i.val=0 ∨ v i.val=1 := by omega - rcases hh with H|H|H <;> simp [H,switch,chi_zero_left,chi_neg'] - have hx : chi (∑ i∈d,v i • i) x = ∏ i : d, switch (v i) (chi (i:Grid M) x) := by - simp_rw [← h1]; rw [← Finset.sum_attach d,chi_comm,←phaseChar_apply] - have H := Compress.char_sum (phaseChar x) (fun i : d => (v i.val) • i.val) - change (phaseChar x) (∑ i : d, v i.val • i.val) = _ - rw [H]; exact Finset.prod_congr rfl fun i _ => chi_comm x _ - let p (i : d) := switch (v i) (chi (i:Grid M) x) - let q (i : d) := switch (v i) (shine e M (tiles e d x i)) - have hp i : ‖p i‖=1 := Hs _ _ (norm_chi _ _) - have hq i : ‖q i‖=1 := Hs _ _ (norm_chi _ _) - have hh (S : Finset d) : - ‖∏ i∈S,p i - ∏ i∈S,q i‖ ≤ S.card * (8*side e) := by - induction S using Finset.induction_on with - | empty => simp - | @insert i t hi ih => - let a := ∏ y∈t,p y - let b := ∏ y∈t,q y - rw [Finset.prod_insert hi,Finset.prod_insert hi] - change ‖p i*a-q i*b‖ ≤ _ - have he : ‖p i-q i‖ ≤ 8*side e := by - unfold p q tiles switch - split_ifs - · simp; exact side_pos e |>.le - · exact shade_close _ _ _ - · rw [← map_sub,Complex.norm_conj] - exact shade_close _ _ _ - have hA : ‖a‖=1 := by unfold a; rw [Complex.norm_prod]; exact Finset.prod_eq_one (fun i _ => hp i) - rw [show p i*a-q i*b=(p i-q i)*a+q i*(a-b) by ring] - apply le_trans (norm_add_le _ _) - rw [norm_mul,norm_mul,hA,hq,mul_one,one_mul,Finset.card_insert_of_notMem hi]; push_cast - change ‖a-b‖ ≤ _ at ih - nlinarith - rw [hx] - simpa only [Finset.card_univ,Fintype.card_coe] using hh Finset.univ - -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Capture.Dual +import OAI.Analysis.FourierLLogL.Capture.Curve + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture +variable {M : ℕ} +open scoped Classical + +abbrev RingN (e M : ℕ) := Grid (min e M) +def shade (e : ℕ) (x s : Grid M) : RingN e M := + down (min_le_right e M) (s*x) +def shine (e M : ℕ) (z : RingN e M) : ℂ := + chi 1 (raise (min_le_right e M) z) + +lemma shade_close (e : ℕ) (x s : Grid M) : ‖chi s x-shine e M (shade e x s)‖ ≤ 8*side e := by + let p := s*x + let r := raise (min_le_right e M) (shade e x s) + have HE : chi s x=chi 1 p := by unfold chi p; rw [one_mul] + have hp : ‖chi 1 p-chi 1 r‖ = ‖1-chi 1 (p-r)‖ := by + rw [show chi 1 p-chi 1 r = (chi 1 (p-r)-1)*chi 1 r by + rw [sub_mul,← chi_add,sub_add_cancel,one_mul],norm_mul,norm_chi,mul_one,norm_sub_rev] + have hi : Dyadic.diam (p-r) ≤ side e := by + let b := min e M + have hV : rad (p-r) ≤ 2^(M-b)-1 := snap_dist _ p + rcases le_total e M with he|he + · have hh : side M*(2:ℝ)^(M-e)=side e := (descale (p:=⟨e,0⟩) he).symm + rw [show b=e from min_eq_left he] at hV + unfold Dyadic.diam; rw [←hh] + exact mul_le_mul_of_nonneg_left (by linarith) (side_pos _).le + rw [show b=M from min_eq_right he] at hV + rw [Nat.sub_self,pow_zero,sub_self] at hV + unfold Dyadic.diam; have H := hV.antisymm (rad_nonneg _) + rw [H,mul_zero]; exact (side_pos _).le + rw [HE]; change ‖chi 1 p-chi 1 r‖ ≤ _ + rw [hp] + have HH := norm_phase_sq (1:Grid M) (p-r) + nlinarith [(cost_diam (p-r)).2,side_pos e,diam_nonneg (p-r)] + +abbrev TileN (e : ℕ) (d : Finset (Grid M)) := d → RingN e M + +def tiles (e : ℕ) (d : Finset (Grid M)) (x : Grid M) : TileN e d := + fun i => shade e x i + +abbrev Dome (e : ℕ) (d : Finset (Grid M)) := Grid M × TileN e d + +def sight (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) (x : Grid M) : + Law (Dome e d) := (channel (optic s b) x).send (fun y => (y,tiles e d x)) + + +private lemma see_as_H (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) : + let P := tracker s b + s.actor.privateH (sight s b d e) = + P.Hcond Prod.fst (fun i => (i.2, tiles e d i.1)) := by + intro P + let g : Grid M × Grid M → Grid M × Dome e d := + fun i => (i.1,i.2,tiles e d i.1) + have H : s.actor.withNext (sight s b d e) = P.send g := by + apply ext_of_Ex; intro f + simp_rw [P,tracker,Ex_send,Ex_withNext,sight,Ex_send] + rfl + rw [privateH,H,Hcond_send] + +lemma sight_budget (s : Scene M) (b : Bool) (d : Finset (Grid M)) (e : ℕ) : + let v := deficit s (channel (optic s b)) + v ≤ deficit s (sight s b d e) ∧ deficit s (sight s b d e) ≤ v+d.card*e := by + unfold deficit; intro v + let P := tracker s b + let X : Grid M × Grid M → TileN e d := fun i => tiles e d i.1 + let g := fun i : Grid M × Grid M => (i.2,X i) + change v ≤ levels s-_ ∧ _ + rw [see_as_H] + have H1 : s.actor.privateH (channel (optic s b))=P.Hcond Prod.fst Prod.snd := rfl + change levels s-_≤levels s-P.Hcond _ g ∧ levels s-P.Hcond _ g ≤ levels s-_+_ + rw [H1] + have H := Hcond_antitone P Prod.fst (Y:=g) (Z:=Prod.snd) (fun i j hi => congrArg Prod.fst hi) + refine ⟨by linarith [H],?_⟩ + have hX := P.H_card X + have HX : P.H X ≤ (d.card:ℝ)*e := by + apply hX.trans + simp only [TileN,RingN,Grid,Fintype.card_fun,Fintype.card_coe,ZMod.card] + push_cast; rw [Real.log_pow,Real.log_pow] + gcongr + have he : (min e M:ℕ) ≤ e := by omega + have hi : ((min e M:ℕ):ℝ) ≤ e := by exact_mod_cast he + nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), + Nat.cast_nonneg (α:=ℝ) (min e M)] + have h₂ : + P.Hcond Prod.fst Prod.snd = P.Hcond (fun i => (i.1,X i)) Prod.snd := by + unfold Hcond; congr 1; apply H_partition; simp [X] + rw [h₂, Hcond_chain] + have H₃ : P.Hcond Prod.fst (fun i => (X i,i.2)) = P.Hcond Prod.fst g := + P.Hcond_right_partition _ (by simp [g,and_comm]) + rw [H₃] + linarith [Hcond_le_H P X Prod.snd] + + +private def switch (n : ℤ) (z : ℂ) := + if n=0 then 1 else if n=1 then z else conj z +theorem painted (u : Grid M) (d : Finset (Grid M)) (h : enclosed d 1 u) (e : ℕ) : + ∃ f : TileN e d → ℂ, (∀ i, ‖f i‖=1) ∧ + ∀ x, ‖chi u x-f (tiles e d x)‖ ≤ d.card*(8*side e) := by + obtain ⟨v,hv,rfl⟩ := h + let F (i : TileN e d) := ∏ x : d, switch (v x) (shine e M (i x)) + have Hs (i : ℤ) (z : ℂ) (h : ‖z‖=1) : ‖switch i z‖=1 := by unfold switch; split_ifs <;> simp [h] + refine ⟨F,?_,?_⟩ + · intro i; unfold F; rw [Complex.norm_prod] + exact (Finset.prod_eq_one fun j _ => Hs _ _ (norm_chi _ _)) + intro x + have h1 (i : d) : + chi (v i • (i.val)) x = switch (v i.val) (chi (i.val) x) := by + have hx := abs_le.mp (hv i.val i.property) + have hh : v i.val = -1 ∨ v i.val=0 ∨ v i.val=1 := by omega + rcases hh with H|H|H <;> simp [H,switch,chi_zero_left,chi_neg'] + have hx : chi (∑ i∈d,v i • i) x = ∏ i : d, switch (v i) (chi (i:Grid M) x) := by + simp_rw [← h1]; rw [← Finset.sum_attach d,chi_comm,←phaseChar_apply] + have H := Compress.char_sum (phaseChar x) (fun i : d => (v i.val) • i.val) + change (phaseChar x) (∑ i : d, v i.val • i.val) = _ + rw [H]; exact Finset.prod_congr rfl fun i _ => chi_comm x _ + let p (i : d) := switch (v i) (chi (i:Grid M) x) + let q (i : d) := switch (v i) (shine e M (tiles e d x i)) + have hp i : ‖p i‖=1 := Hs _ _ (norm_chi _ _) + have hq i : ‖q i‖=1 := Hs _ _ (norm_chi _ _) + have hh (S : Finset d) : + ‖∏ i∈S,p i - ∏ i∈S,q i‖ ≤ S.card * (8*side e) := by + induction S using Finset.induction_on with + | empty => simp + | @insert i t hi ih => + let a := ∏ y∈t,p y + let b := ∏ y∈t,q y + rw [Finset.prod_insert hi,Finset.prod_insert hi] + change ‖p i*a-q i*b‖ ≤ _ + have he : ‖p i-q i‖ ≤ 8*side e := by + unfold p q tiles switch + split_ifs + · simp; exact side_pos e |>.le + · exact shade_close _ _ _ + · rw [← map_sub,Complex.norm_conj] + exact shade_close _ _ _ + have hA : ‖a‖=1 := by unfold a; rw [Complex.norm_prod]; exact Finset.prod_eq_one (fun i _ => hp i) + rw [show p i*a-q i*b=(p i-q i)*a+q i*(a-b) by ring] + apply le_trans (norm_add_le _ _) + rw [norm_mul,norm_mul,hA,hq,mul_one,one_mul,Finset.card_insert_of_notMem hi]; push_cast + change ‖a-b‖ ≤ _ at ih + nlinarith + rw [hx] + simpa only [Finset.card_univ,Fintype.card_coe] using hh Finset.univ + +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Near.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Near.lean index 0ba70c2a670c57006eb4fa790367fd39e3a2ec0a..ee39b4c0d3b0f501420bc3915eeb80699d7bea95 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Near.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Near.lean @@ -1,153 +1,153 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Representative - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -def scLimit (s : Scene M) (i j : ℕ) := - 1000*(height j+max (-log s.norm) 0 + i +1) - -lemma net_len_scalar {s : Scene M} (j i : ℕ) (h : ReadyAt s) (hm : s.here.lev - nlinarith [(abs_le.mp hB).1,sq_nonneg (X (x+k) false),sq_nonneg (X (x+k) true)] - have HL (i':Bool) : ∑ x : Grid M, X x i'^2 ≤ 8*q.invlen*d/p := by - have H := scalar_power (s.smaller i') (hk _) hx d 4 he (hs _) (exp_nonneg _) - have hV : (s.smaller i').here.invlen=2*q.invlen := by - change (s.here.child _).invlen=_ - rw [invlen,child_depth,pow_succ]; unfold q invlen; ring - rw [hV] at H - exact H.trans (le_of_eq (by unfold p; simp only [smaller]; ring)) - have ht : t.card*u*(b/4)^2 ≤ 16*q.invlen*d/p := by - calc - _ ≤ t.card*(U.card:ℝ)*(b/4)^2 := by gcongr - _ = ∑ _x∈V, (b/4)^2 := by simp [V] - _ ≤ ∑ x∈V, Y (a x) := Finset.sum_le_sum Hnear - _ = ∑ x∈V.image a, Y x := by rw [Finset.sum_image]; exact fun x hx y hy H => hI hx hy H - _ ≤ ∑ x, Y x := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) - (fun i _ _ => add_nonneg (sq_nonneg _) (sq_nonneg _)) - _ ≤ 16*q.invlen*d/p := by - unfold Y; rw [Finset.sum_add_distrib] - rw [show 16*q.invlen*d/p=8*q.invlen*d/p+8*q.invlen*d/p by ring] - exact add_le_add (HL false) (HL true) - have hr : 0 < p := hx - have hd : 0 < d := exp_pos _ - have h_u : (t.card:ℝ) ≤ ((2:ℝ)^26*d)/(p*b^6) := by - unfold u at ht; rw [heq] at ht - field_simp at ht ⊢ - nlinarith - change (t.card:ℝ) ≤ _ - apply h_u.trans - have h1 : log ((2^26*d)/(p*b^6)) ≤ scLimit s i j := by - rw [Real.log_div (by positivity) (by positivity),Real.log_mul (by positivity) hd.ne', - Real.log_mul hr.ne' (by positivity), log_pow, log_pow] - unfold d b scLimit; rw [log_exp,side_pow_two,log_inv,log_pow] - push_cast - have Hp' : -log p ≤ max (-log s.norm) 0 := le_max_left _ _ - have Hi : 0 ≤ max (-log s.norm) 0 := le_max_right _ _ - nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),d_pos j,Nat.cast_nonneg (α:=ℝ) i] - exact le_trans (b:=exp (log _)) (le_of_eq (exp_log (by positivity)).symm) (exp_monotone h1) - - -lemma magnitude_near (main : Bool) (s : Scene M) (i k : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) - (b : ℝ) (hb : share s ≤ b) : - magnitude main s i ≤ magnitude main s k + b * (16*s.here.len*rad (i-k)) := by - let p := s.here - obtain ⟨g,hg,hG⟩ := (test_complete main s hp.2 hh i).2 - let z := chi (i-k) (p.start M) - have hZ : ‖z‖=1 := norm_chi _ _ - let f (x : Grid M) := g x*z - have hf (x) : ‖f x‖≤1 := by unfold f; rw [norm_mul,hZ,mul_one]; exact hg _ - let T := 16*p.len*rad (i-k) - have ht : 0 ≤ T := by have he := p.len_pos; have hv := rad_nonneg (i-k); unfold T; positivity - have HI (x : Grid M) (hx : x∈p.block M) : - ‖chi i x-z * chi k x‖ ≤ 8*p.len*rad (i-k) := by - have HE := phase_distance (i-k) x (p.start M) - rw [show chi i x-z*chi k x=(chi (i-k) x-z)*chi k x by rw [sub_mul,←chi_add',sub_add_cancel],norm_mul,norm_chi,mul_one] - have hx' := diam_block hx (start_mem hp.1.1 hp.1.2) - apply HE.trans; rw [mul_comm (rad _)]; gcongr; exact rad_nonneg _ - let P := channel (optic s main) - let w (p : Grid M → ℂ) (i x y : Grid M) := utility main (p y) (chi i x) - have hhU : - sales main s g i ≤ sales main s f k + share s*T := by - rw [show sales main s f k=rung s P (w f k) from rfl,← rung_const s hp.1 P T,←rung_add] - refine Finset.sum_le_sum fun x _ => Finset.sum_le_sum fun y _ => ?_ - by_cases hx : x∈p.block M - · have heq : w f k x y = utility main (g y) (z*chi k x) := by unfold w utility f; rw [mul_assoc,norm_mul,hZ,mul_one] - have hl := utility_diff main (g y) (chi i x) (z*chi k x) (hg _) - apply mul_le_mul_of_nonneg_left - · change w g i x y ≤ _ - unfold w at heq ⊢; dsimp only; rw [heq] - unfold T; linarith [(abs_le.mp hl).2,HI x hx] - exact mul_nonneg (ante_nonneg _ hp.2 hh _) (P _ |>.nonneg _) - · have H : ante s x=0 := by unfold ante wt; rw [dice_apply hp.1,ite_eq_right hx,zero_mul,zero_div] - change addMass s P x y*_ ≤ addMass s P x y*_ - rw [addMass,H]; ring_nf; exact le_rfl - have HH := (test_complete main s hp.2 hh k).1 f hf - rw [← hG]; apply hhU.trans - exact add_le_add HH (mul_le_mul_of_nonneg_right hb ht) - -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Representative + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +def scLimit (s : Scene M) (i j : ℕ) := + 1000*(height j+max (-log s.norm) 0 + i +1) + +lemma net_len_scalar {s : Scene M} (j i : ℕ) (h : ReadyAt s) (hm : s.here.lev + nlinarith [(abs_le.mp hB).1,sq_nonneg (X (x+k) false),sq_nonneg (X (x+k) true)] + have HL (i':Bool) : ∑ x : Grid M, X x i'^2 ≤ 8*q.invlen*d/p := by + have H := scalar_power (s.smaller i') (hk _) hx d 4 he (hs _) (exp_nonneg _) + have hV : (s.smaller i').here.invlen=2*q.invlen := by + change (s.here.child _).invlen=_ + rw [invlen,child_depth,pow_succ]; unfold q invlen; ring + rw [hV] at H + exact H.trans (le_of_eq (by unfold p; simp only [smaller]; ring)) + have ht : t.card*u*(b/4)^2 ≤ 16*q.invlen*d/p := by + calc + _ ≤ t.card*(U.card:ℝ)*(b/4)^2 := by gcongr + _ = ∑ _x∈V, (b/4)^2 := by simp [V] + _ ≤ ∑ x∈V, Y (a x) := Finset.sum_le_sum Hnear + _ = ∑ x∈V.image a, Y x := by rw [Finset.sum_image]; exact fun x hx y hy H => hI hx hy H + _ ≤ ∑ x, Y x := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) + (fun i _ _ => add_nonneg (sq_nonneg _) (sq_nonneg _)) + _ ≤ 16*q.invlen*d/p := by + unfold Y; rw [Finset.sum_add_distrib] + rw [show 16*q.invlen*d/p=8*q.invlen*d/p+8*q.invlen*d/p by ring] + exact add_le_add (HL false) (HL true) + have hr : 0 < p := hx + have hd : 0 < d := exp_pos _ + have h_u : (t.card:ℝ) ≤ ((2:ℝ)^26*d)/(p*b^6) := by + unfold u at ht; rw [heq] at ht + field_simp at ht ⊢ + nlinarith + change (t.card:ℝ) ≤ _ + apply h_u.trans + have h1 : log ((2^26*d)/(p*b^6)) ≤ scLimit s i j := by + rw [Real.log_div (by positivity) (by positivity),Real.log_mul (by positivity) hd.ne', + Real.log_mul hr.ne' (by positivity), log_pow, log_pow] + unfold d b scLimit; rw [log_exp,side_pow_two,log_inv,log_pow] + push_cast + have Hp' : -log p ≤ max (-log s.norm) 0 := le_max_left _ _ + have Hi : 0 ≤ max (-log s.norm) 0 := le_max_right _ _ + nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),d_pos j,Nat.cast_nonneg (α:=ℝ) i] + exact le_trans (b:=exp (log _)) (le_of_eq (exp_log (by positivity)).symm) (exp_monotone h1) + + +lemma magnitude_near (main : Bool) (s : Scene M) (i k : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) + (b : ℝ) (hb : share s ≤ b) : + magnitude main s i ≤ magnitude main s k + b * (16*s.here.len*rad (i-k)) := by + let p := s.here + obtain ⟨g,hg,hG⟩ := (test_complete main s hp.2 hh i).2 + let z := chi (i-k) (p.start M) + have hZ : ‖z‖=1 := norm_chi _ _ + let f (x : Grid M) := g x*z + have hf (x) : ‖f x‖≤1 := by unfold f; rw [norm_mul,hZ,mul_one]; exact hg _ + let T := 16*p.len*rad (i-k) + have ht : 0 ≤ T := by have he := p.len_pos; have hv := rad_nonneg (i-k); unfold T; positivity + have HI (x : Grid M) (hx : x∈p.block M) : + ‖chi i x-z * chi k x‖ ≤ 8*p.len*rad (i-k) := by + have HE := phase_distance (i-k) x (p.start M) + rw [show chi i x-z*chi k x=(chi (i-k) x-z)*chi k x by rw [sub_mul,←chi_add',sub_add_cancel],norm_mul,norm_chi,mul_one] + have hx' := diam_block hx (start_mem hp.1.1 hp.1.2) + apply HE.trans; rw [mul_comm (rad _)]; gcongr; exact rad_nonneg _ + let P := channel (optic s main) + let w (p : Grid M → ℂ) (i x y : Grid M) := utility main (p y) (chi i x) + have hhU : + sales main s g i ≤ sales main s f k + share s*T := by + rw [show sales main s f k=rung s P (w f k) from rfl,← rung_const s hp.1 P T,←rung_add] + refine Finset.sum_le_sum fun x _ => Finset.sum_le_sum fun y _ => ?_ + by_cases hx : x∈p.block M + · have heq : w f k x y = utility main (g y) (z*chi k x) := by unfold w utility f; rw [mul_assoc,norm_mul,hZ,mul_one] + have hl := utility_diff main (g y) (chi i x) (z*chi k x) (hg _) + apply mul_le_mul_of_nonneg_left + · change w g i x y ≤ _ + unfold w at heq ⊢; dsimp only; rw [heq] + unfold T; linarith [(abs_le.mp hl).2,HI x hx] + exact mul_nonneg (ante_nonneg _ hp.2 hh _) (P _ |>.nonneg _) + · have H : ante s x=0 := by unfold ante wt; rw [dice_apply hp.1,ite_eq_right hx,zero_mul,zero_div] + change addMass s P x y*_ ≤ addMass s P x y*_ + rw [addMass,H]; ring_nf; exact le_rfl + have HH := (test_complete main s hp.2 hh k).1 f hf + rw [← hG]; apply hhU.trans + exact add_le_add HH (mul_le_mul_of_nonneg_right hb ht) + +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Rec.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Rec.lean index 0f9a725adb9732e5b9868caf2c05586218223d30..bb13e597a6a914b2136cad9996a1226eb1780c5a 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Rec.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Rec.lean @@ -1,395 +1,395 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Difference -import OAI.Analysis.FourierLLogL.Snapshot.Near - -/-! # Timing of snapshot replacement and local charges -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart -variable {M j J : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -structure Board (M : ℕ) where - chart : Chart M - previous : Option (Scene M) - -def vacant (M : ℕ) : Board M := ⟨classic M,none⟩ - -structure Plot where - main : Bool - ix : ℕ - cap : ℕ -namespace Plot -def closer (a : Plot) (g : Board M) (p q : Scene M) := - distance (portrait a.main p g.chart.items a.ix a.cap) (portrait a.main q g.chart.items a.ix a.cap) ≤ sharp a.ix - -/-- refresh test. Ignore when breaching the cap. -/ -def freshAt (a : Plot) (g : Board M) (x : Scene M) : Prop := - match g.previous with - | none => True - | some q => (zoom x a.ix a.main g.chart).items ≠ g.chart.items ∨ - ∃ b, ¬ a.closer g (x.smaller b) q - -def now (a : Plot) (g : Board M) (s : Scene M) := - zoom s a.ix a.main g.chart -def cardStep (a : Plot) (g : Board M) := g.chart.size ≤ rangeIx a.cap - -def turn (a : Plot) (g : Board M) (s : Scene M) : Board M := - { chart:=a.now g s - previous:=if cardStep a (⟨a.now g s,none⟩:Board M) then - if a.freshAt g s then some s else g.previous else none } - -lemma turn_chart (a : Plot) (g : Board M) (s : Scene M) : - (a.turn g s).chart = a.now g s := rfl - -lemma prior_step (a : Plot) (g : Board M) (s : Scene M) (h : a.cardStep (a.turn g s)) : - (a.turn g s).previous= if a.freshAt g s then some s else g.previous := by - unfold turn; dsimp only - have hh : cardStep a ⟨a.now g s, none⟩ := h - rw [ite_eq_left hh] - -def deltaC (a : Plot) (g : Board M) (x : Scene M) := - match g.previous with - | none => 0 - | some q => cloud (portrait a.main x g.chart.items a.ix a.cap) - (portrait a.main q g.chart.items a.ix a.cap) - -def birth (a : Plot) (g : Board M) (x : Scene M) := if a.freshAt g x then (1:ℝ) else 0 - -def marker (a : Plot) := (sharp a.ix)^2 / 16 - -lemma marker_pos (a : Plot) : 0 < a.marker := div_pos (pow_pos (sharp_pos _) _) (by norm_num) - - -def soundPrev (g : Board M) := - ∀ s, g.previous=some s → ReadyAt s ∧ 0 ≤ s.norm ∧ share s ≤ 2 - -def creditsC (a : Plot) (g : Board M) : ℝ := - ↑(rangeIx a.cap) - (g.chart.size:ℝ) - -def slotC (a : Plot) (g : Board M) := - a.marker * (a.creditsC g + (if g.previous=none then 1 else 0)) - -def kObs (a : Plot) (g : Board M) (s : Scene M) := sight s a.main g.chart.items (precision a.ix a.cap) - -lemma deltaC_pos (a : Plot) {g : Board M} (h : soundPrev g) (s : Scene M) - (hp : ReadyAt s) (hv : 0 ≤ s.norm) : 0 ≤ a.deltaC g s := by - unfold deltaC; cases H : g.previous with - | none => simp only [le_refl] - | some q => - obtain ⟨h1,h2,h3⟩ := h q H - exact cloud_nonneg _ _ (nonnegative_hist s hp.2 hv _) (nonnegative_hist q h1.2 h2 _) - -lemma costC_pos (a : Plot) {g : Board M} (h : a.cardStep g) : 0 ≤ a.creditsC g := by - unfold creditsC; exact sub_nonneg.mpr (by exact_mod_cast h) -lemma slot_pos (a : Plot) {g : Board M} (h : a.cardStep g) : 0 ≤ a.slotC g := - mul_nonneg a.marker_pos.le (by have h := a.costC_pos h; split_ifs <;> linarith) -lemma turn_prev (a : Plot) {g : Board M} (h : soundPrev g) (s : Scene M) - (h₁ : ReadyAt s) (h₂ : 0 ≤ s.norm) (h₃ : share s ≤ 2) : soundPrev (a.turn g s) := by - intro x H; simp only [turn] at H - split_ifs at H with heq he - · rw [Option.some.injEq] at H; subst x; exact ⟨h₁,h₂,h₃⟩ - · exact h x H - -private lemma changed_point {p : Plot} {g : Board M} {s q : Scene M} {u} - (h : Following j J f (.ran s q u)) (ht : soundPrev g) (hd : 0 < s.norm) - (hg : g.chart.Sound) (hk : cardStep p (p.turn g q)) : - let C := p.turn g q - p.marker*p.birth g q+ - (uniform Bool).Ex (fun b => p.slotC C + p.deltaC C (q.smaller b)) - ≤ p.slotC g + p.deltaC g s +4*(share s-share q) +8*fisher q (p.kObs C q) := by - classical - intro C - let Q := uniform Bool - have HV : Widen q p.ix p.main g.chart C.chart := zoom_spec g.chart hg p.ix p.main h - obtain ⟨ha,hb,hc,hx,hy,hu⟩ := h - have hs : ReadyAt s := ⟨ha.within,ha.positive⟩ - have hq : ReadyAt q := ⟨hc.within,hc.positive⟩ - obtain ⟨h₁,h₂⟩ := share_small hx - have hI : q.here.lev < M := (rooted _ (hu false)).within.2 - have ht' (b : Bool) : ReadyAt (q.smaller b) := ⟨child_ok q.here hc.within.1 hI b,hc.positive⟩ - have hY (b : Bool) : share (q.smaller b) ≤ 4 := share_child q hc.within.1 hI hc.positive h₁ h₂ b - let V (i : Scene M) := portrait p.main i C.chart.items p.ix p.cap - let k := p.kObs C q - let F (j : Scene M) (s : Scene M) := cloud (V s) (V j) - have Hx (t : Scene M) (ht : ReadyAt t) (hv : 0 ≤ t.norm) : - Q.Ex (fun b => F t (q.smaller b)) ≤ F t q+4*fisher q k := - cloud_drift q hc.within.1 hI hc.positive h₁.le k (V t) (nonnegative_hist _ ht.2 hv _) - have hp : p.creditsC C ≤ p.creditsC g := by - have hZ : g.chart.size ≤ C.chart.size := Finset.card_le_card HV.extend - exact sub_le_sub_left (by exact_mod_cast hZ) _ - have hi : 0 ≤ share s-share q := by - unfold share; rw [hb.1.flat] - exact sub_nonneg.mpr (div_le_div_of_nonneg_right (hb.1.mass_le ha.positive ha.within) hd.le) - have hF : 0 ≤ fisher q k := (fisher_range _ hc.within.1 hI hc.positive h₁.le _).1 - have hcN := p.deltaC_pos ht s hs hd.le - - have H {w : Scene M} (hw : g.previous=some w) (heq : C.chart.items=g.chart.items) : - Q.Ex (fun b => F w (q.smaller b)) ≤ p.deltaC g s + 4*(share s-share q)+4*fisher q k := by - have hT := ht _ hw - apply le_trans (Hx _ hT.1 hT.2.1) - apply add_le_add_left - have H : p.deltaC g s = cloud (hist s k) (V w) := by - unfold deltaC; rw [hw] - unfold V k kObs; rw [heq] - unfold portrait sight optic; rw [hb.1.fund] - rw [H] - exact cloud_kill hb.1 hs hq hd.le k _ (nonnegative_hist _ hT.1.2 hT.2.1 _) - have hh : C.previous = if p.freshAt g q then some q else g.previous := p.prior_step _ _ hk - rw [Ex_add (f:=fun _ => slotC p C),Ex_const] - by_cases he : p.freshAt g q - · rw [ite_eq_left he] at hh - have Hs : Q.Ex (fun b => p.deltaC C (q.smaller b)) ≤ 4*fisher q k := by - have hx := Hx _ hq h₁.le - have ht (x : Scene M) : p.deltaC C x=F q x := by unfold deltaC; rw [hh] - simp_rw [ht] - have hc : F q q = 0 := cloud_same _ - rwa [hc,zero_add] at hx - rw [birth,ite_eq_left he,mul_one] - change _ + (_+Q.Ex _) ≤ _+8*fisher q k - have hSlot : p.slotC C = p.marker*p.creditsC C := by unfold slotC; simp [hh] - rw [hSlot] - cases hv : g.previous with - | none => - unfold slotC; rw [ite_eq_left hv,mul_add,mul_one] - nlinarith [marker_pos p] - | some w => - unfold slotC; rw [ite_eq_right (by rw [hv]; simp),add_zero] - unfold freshAt at he; rw [hv] at he - rcases he with he|he - · have H : C.chart.items≠g.chart.items := he - have HX : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨HV.extend, Ne.symm H⟩ - have hz : p.creditsC C+1 ≤ p.creditsC g := by - have hi : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card HX - unfold creditsC; linarith - nlinarith [marker_pos p] - · - by_cases H0 : C.chart.items=g.chart.items - case neg => - have HX : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨HV.extend, Ne.symm H0⟩ - have hz : p.creditsC C+1 ≤ p.creditsC g := by - have hi : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card HX - unfold creditsC; linarith - nlinarith [marker_pos p] - obtain ⟨b,hG⟩ := he - have HV := H hv H0 - have HH : p.marker ≤ Q.Ex (fun b => F w (q.smaller b)) := by - let l := q.smaller b - obtain ⟨hN1,hN2,hN3⟩ := ht _ hv - have hZ (i) : 0 ≤ F w (q.smaller i) := cloud_nonneg _ _ - (nonnegative_hist _ (ht' i).2 h₁.le _) (nonnegative_hist _ hN1.2 hN2 _) - have hY' : (sharp p.ix)^2 ≤ 6*F w l := by - have Hv : sharp p.ix ≤ distance (V l) (V w) := by - unfold V; rw [H0]; exact le_of_not_ge hG - apply le_trans (pow_le_pow_left₀ (sharp_pos _).le Hv _) - apply le_trans (distance_cloud _ _ (nonnegative_hist _ (ht' _).2 h₁.le _) - (nonnegative_hist _ hN1.2 hN2 _)) - have ht : ∑ i,V w i = share w := total_hist w hN1.1 _ - have ht' : ∑ i,V l i = share l := total_hist l (ht' b).1 _ - change ((∑ i, V l i)+ ∑ i,V w i)*F w l ≤ _ - rw [ht,ht'] - apply mul_le_mul_of_nonneg_right (by linarith [show share l ≤ 4 from hY b]) (hZ b) - unfold Q Ex - rw [Fintype.sum_bool,uniform_apply,uniform_apply] - unfold marker; unfold l at * - cases b <;> simp only [Fintype.card_bool,Nat.cast_ofNat] <;> nlinarith [hZ true,hZ false] - nlinarith [marker_pos p] - - rw [ite_eq_right he] at hh - obtain ⟨w,hv⟩ : ∃ w, g.previous=some w := by - cases hx : g.previous - · simp [freshAt,hx] at he - · exact ⟨_,rfl⟩ - have hs : p.birth g q=0 := ite_eq_right he - have heq : C.chart.items=g.chart.items := by - unfold freshAt at he; rw [hv] at he; exact Classical.byContradiction (fun H => he (Or.inl H)) - have hS' : p.slotC C=p.slotC g := by simp [slotC,creditsC,Chart.size, hh,heq] - have hV' (x : Scene M) : p.deltaC C x=F w x := by unfold deltaC; rw [hh,hv] - rw [hs,mul_zero,hS']; simp_rw [hV'] - have ht := H hv heq - change _ ≤ _+8*fisher q k - unfold Q at *; linarith - - -def coinC (p : Plot) := ((rangeIx p.cap : ℕ):ℝ)*precision p.ix p.cap - -def debtC (p : Plot) (g : Board M) (x : Scene M) (j : ℕ) := - deficit x (p.kObs g x)+(p.creditsC g*p.coinC+(minimumT j)⁻¹) - -private lemma debt_recur (p : Plot) (g : Board M) {s q : Scene M} {k} - (h : Following j J f (.ran s q k)) (hd : 0 < s.norm) (hg : g.chart.Sound) - (hk : cardStep p g) (hp : cardStep p (p.turn g q)) : - let C := p.turn g q - (uniform Bool).Ex (fun b => share (q.smaller b)*debtC p C (q.smaller b) j) - + (1/4:ℝ)*fisher q (p.kObs C q) - ≤ share s * debtC p g s j := by - intro C - have hm := pre_pos h - have hC : Widen q p.ix p.main g.chart C.chart := zoom_spec _ hg _ _ h - let v := (keep s q).snip - let U := (step q).probs - let Q := v.probs - let Z (c : Board M) := p.kObs c q - let F := Z g; let W := Z C - let H (x) := deficit x F - let E (i) := deficit (q.smaller i) W - let V (c : Board M) := p.creditsC c*p.coinC+(minimumT j)⁻¹ - have HE : Q.entropy ≤ (minimumT j)⁻¹* Q false := debt_at h - obtain ⟨ha,hb,hv,hp',ht,hs⟩ := h - have hT (b : Bool) := hs b - have hR : q.here.lev < M := (rooted _ (hs false)).within.2 - have hS : ReadyAt s := ⟨ha.within,ha.positive⟩ - have hS' : ReadyAt q := ⟨hv.within,hv.positive⟩ - have hQ := hp'.1.2.2 - have hZ : 0 < q.norm := hb.1.flat.symm ▸ hd - have hM := keep_true hb.1 hS hS' hm - have hi₁ : deficit q W+V C ≤ H q+V g := by - by_cases hi : C.chart.items=g.chart.items - · unfold W Z H F V kObs creditsC size; rw [hi]; exact le_rfl - have hG : p.creditsC C+1 ≤ p.creditsC g := by - have hh : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨hC.extend, Ne.symm hi⟩ - have he : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card hh - unfold creditsC; linarith - have hh1 : 0 ≤ p.coinC := by unfold coinC; positivity - have h₁ := (sight_budget q p.main g.chart.items (precision p.ix p.cap)).1 - have h₂ := (sight_budget q p.main C.chart.items (precision p.ix p.cap)).2 - change deficit q W ≤ _ at h₂ - change _ ≤ H q at h₁ - have h₃ : (C.chart.items.card :ℝ)* precision p.ix p.cap ≤ p.coinC := by - unfold coinC - exact mul_le_mul_of_nonneg_right (by exact_mod_cast hp) (by positivity) - unfold V - nlinarith - have hv₀ : (minimumT j)⁻¹ ≤ V g := - le_add_of_nonneg_left (mul_nonneg (costC_pos _ hk) (by unfold coinC; positivity)) - have h₂ : Q true*(H q+V g) ≤ H s+V g := by - have hi (b : Bool) : levels (v.dst b)=levels s := by - unfold levels; rw [show (v.dst b).here=s.here from keep_here hb.1 _] - have hz : Q.Ex (fun b => H (v.dst b)) ≤ H s+Q.entropy := by - have hh := old_entropy_cost v F - have hh' := conditioned_balance F v hM - unfold H deficit; simp_rw [hi,Ex_sub (f:=fun _ => levels s),Ex_const] - change v.probs.Ex _+ _ = s.actor.privateH F at hh' - unfold Q; linarith - have hZ : 0 ≤ Q false * H (v.dst false) := by - rcases eq_or_lt_of_le (Q.nonneg false) with h|h - · rw [← h,zero_mul] - have HH := other_ok hb.1 hS - exact mul_nonneg h.le (deficit_nonneg (v.dst false) HH.1 HH.2 ((true_mass hM).mp h) F) - have ht₁ : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total - rw [Ex,Fintype.sum_bool] at hz - rw [show v.dst true=q from rfl] at hz - nlinarith [mul_le_mul_of_nonneg_right hv₀ (Q.nonneg false),show - V g*(Q true+Q false)=V g by rw [ht₁]; ring] - have ht₁ := space_deficit q W hv.within.1 hR hv.positive hQ hZ - have hx b : debtC p C (q.smaller b) j=E b+V C := rfl - let D (b : Bool) := E b+V C - have h₁ : - (uniform Bool).Ex (fun b => share (q.smaller b)*debtC p C (q.smaller b) j)= - share q * U.Ex D := by - have HI (b : Bool) : share (q.smaller b)*debtC p C (q.smaller b) j = - (q.smaller b).mass * (q.norm⁻¹ * D b) := by rw [hx]; unfold share D; simp only [smaller]; ring - simp_rw [HI] - rw [← output_split _ hv.within.1 hR hv.positive hQ (fun b => q.norm⁻¹*D b), - Ex_lmul] - unfold share U; ring - have HZ : share q * ((1/4:ℝ)*(q.norm/q.mass)*fisher q W)=1/4*fisher q W := by - unfold share; field_simp - have hEq : share q = share s * Q true := by - have H := kept_mass hb.1 hS hS' hm - unfold share - nth_rw 1 [H,hb.1.flat] - unfold Q v; ring - rw [h₁,← HZ,← mul_add] - unfold D; rw [Ex_add (g:=fun b => V C),Ex_const] - have hDC : debtC p g s j=H s+V g := by - unfold debtC H F V Z kObs sight optic - rw [hb.1.fund] - have hsq : 0 ≤ share q := div_nonneg hQ.le hZ.le - calc - _ ≤ share q * (deficit q W + V C) := by - change U.Ex E + _ ≤ deficit q W at ht₁ - apply mul_le_mul_of_nonneg_left _ hsq; linarith - _ ≤ share q*(H q+V g) := mul_le_mul_of_nonneg_left hi₁ hsq - _ ≤ _ := by - rw [hEq,mul_assoc,hDC] - exact mul_le_mul_of_nonneg_left h₂ (div_nonneg hm.le hd.le) - -def expenses (p : Plot) (g : Board M) (s : Scene M) (j : ℕ) := - p.slotC g + p.deltaC g s + 4*share s + 32*(share s*debtC p g s j) - -lemma expenses_nonneg (p : Plot) (g : Board M) (s : Scene M) (j : ℕ) (hg : p.cardStep g) - (h : ReadyAt s) (hp : soundPrev g) (hv : 0 < s.norm) : - 0 ≤ p.expenses g s j := by - have hm : 0 ≤ s.mass := avg_nonneg _ fun i _ => h.2 i - have hsc := deltaC_pos p hp s h hv.le - have Ht := slot_pos p hg - unfold expenses; have hS : 0 ≤ share s := div_nonneg hm hv.le - suffices HH : 0 ≤ share s*debtC p g s j by linarith - rcases eq_or_lt_of_le hm with H|H - · unfold share; rw [← H,zero_div,zero_mul] - apply mul_nonneg hS - unfold debtC - have hC := p.costC_pos hg - exact add_nonneg (deficit_nonneg _ h.1 h.2 H _) (add_nonneg - (mul_nonneg hC (by unfold coinC; positivity)) (inv_nonneg.mpr (minimum_pos j).le)) - -/-- number born before cap breach; representatives from these births may be retained for -delayed estimates on further jumps -/ -def minted (p : Plot) (g : Board M) : List (Beat M) → ℝ -| [] => 0 -| b::l => let c := p.turn g b.post - if p.cardStep c then birth p g b.post + minted p c l else 0 - -lemma mint_small (p : Plot) (g : Board M) (s : Scene M) (t : Thread M s) - (h : Following j J f t) (hp : g.chart.Sound) (hg : soundPrev g) (hv : p.cardStep g) - (hs : 0 < s.norm) : - p.marker*(s.here.dice M).Ex (fun x => minted p g (t.stream x)) ≤ expenses p g s j := by - induction t generalizing g with - | blank => - have h1 := rooted _ h - simpa only [stream,minted,Ex_const,mul_zero] using - expenses_nonneg p g _ j hv ⟨h1.within,h1.positive⟩ hg hs - | ran s q k ih => - rw [stream_branch k h (fun l _ => minted p g l)] - let C := p.turn g q - by_cases hh : p.cardStep C - case neg => - have h1 := rooted _ h - dsimp only [C] at hh - simpa only [minted, ite_eq_right hh, Ex_const,mul_zero] using - expenses_nonneg p g s j hv ⟨h1.within,h1.positive⟩ hg hs - have hD := changed_point h hg hs hp hh - have hF := debt_recur p _ h hs hp hv hh - have hZ := zoom_spec g.chart hp p.ix p.main h - obtain ⟨ha,hb,hc,hx,hd,hk⟩ := h - have hv := turn_prev p hg q ⟨hc.within,hc.positive⟩ (share_small hx).1.le (share_small hx).2 - have hq : (uniform Bool).Ex (fun b => share (q.smaller b))=share q := by - simp only [Ex,uniform_apply,Fintype.sum_bool,Fintype.card_bool,Nat.cast_ofNat] - have ht := share_split q; linarith - let f (i : Bool) := ((q.smaller i).here.dice M).Ex fun x => minted p C ((k i).stream x) - have HM : - p.marker*((uniform Bool).Ex f) ≤ (uniform Bool).Ex fun i => expenses p C (q.smaller i) j := by - rw [← Ex_lmul]; apply Ex_mono; intro i - exact ih i C (hk i) hZ.sonic hv hh (share_small hx).1 - unfold expenses at * - simp_rw [Ex_add] at * - simp_rw [Ex_const,Ex_lmul] at HM hD - rw [hq] at HM - - have HH : p.cardStep (p.turn g q) := hh - simp only [minted, ite_eq_left HH] - simp_rw [Ex_add,Ex_const] - change p.marker*(p.birth g q+(uniform Bool).Ex f) ≤ _ - dsimp only [C] at * - nlinarith - -end Plot -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Difference +import OAI.Analysis.FourierLLogL.Snapshot.Near + +/-! # Timing of snapshot replacement and local charges -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture Chart +variable {M j J : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +structure Board (M : ℕ) where + chart : Chart M + previous : Option (Scene M) + +def vacant (M : ℕ) : Board M := ⟨classic M,none⟩ + +structure Plot where + main : Bool + ix : ℕ + cap : ℕ +namespace Plot +def closer (a : Plot) (g : Board M) (p q : Scene M) := + distance (portrait a.main p g.chart.items a.ix a.cap) (portrait a.main q g.chart.items a.ix a.cap) ≤ sharp a.ix + +/-- refresh test. Ignore when breaching the cap. -/ +def freshAt (a : Plot) (g : Board M) (x : Scene M) : Prop := + match g.previous with + | none => True + | some q => (zoom x a.ix a.main g.chart).items ≠ g.chart.items ∨ + ∃ b, ¬ a.closer g (x.smaller b) q + +def now (a : Plot) (g : Board M) (s : Scene M) := + zoom s a.ix a.main g.chart +def cardStep (a : Plot) (g : Board M) := g.chart.size ≤ rangeIx a.cap + +def turn (a : Plot) (g : Board M) (s : Scene M) : Board M := + { chart:=a.now g s + previous:=if cardStep a (⟨a.now g s,none⟩:Board M) then + if a.freshAt g s then some s else g.previous else none } + +lemma turn_chart (a : Plot) (g : Board M) (s : Scene M) : + (a.turn g s).chart = a.now g s := rfl + +lemma prior_step (a : Plot) (g : Board M) (s : Scene M) (h : a.cardStep (a.turn g s)) : + (a.turn g s).previous= if a.freshAt g s then some s else g.previous := by + unfold turn; dsimp only + have hh : cardStep a ⟨a.now g s, none⟩ := h + rw [ite_eq_left hh] + +def deltaC (a : Plot) (g : Board M) (x : Scene M) := + match g.previous with + | none => 0 + | some q => cloud (portrait a.main x g.chart.items a.ix a.cap) + (portrait a.main q g.chart.items a.ix a.cap) + +def birth (a : Plot) (g : Board M) (x : Scene M) := if a.freshAt g x then (1:ℝ) else 0 + +def marker (a : Plot) := (sharp a.ix)^2 / 16 + +lemma marker_pos (a : Plot) : 0 < a.marker := div_pos (pow_pos (sharp_pos _) _) (by norm_num) + + +def soundPrev (g : Board M) := + ∀ s, g.previous=some s → ReadyAt s ∧ 0 ≤ s.norm ∧ share s ≤ 2 + +def creditsC (a : Plot) (g : Board M) : ℝ := + ↑(rangeIx a.cap) - (g.chart.size:ℝ) + +def slotC (a : Plot) (g : Board M) := + a.marker * (a.creditsC g + (if g.previous=none then 1 else 0)) + +def kObs (a : Plot) (g : Board M) (s : Scene M) := sight s a.main g.chart.items (precision a.ix a.cap) + +lemma deltaC_pos (a : Plot) {g : Board M} (h : soundPrev g) (s : Scene M) + (hp : ReadyAt s) (hv : 0 ≤ s.norm) : 0 ≤ a.deltaC g s := by + unfold deltaC; cases H : g.previous with + | none => simp only [le_refl] + | some q => + obtain ⟨h1,h2,h3⟩ := h q H + exact cloud_nonneg _ _ (nonnegative_hist s hp.2 hv _) (nonnegative_hist q h1.2 h2 _) + +lemma costC_pos (a : Plot) {g : Board M} (h : a.cardStep g) : 0 ≤ a.creditsC g := by + unfold creditsC; exact sub_nonneg.mpr (by exact_mod_cast h) +lemma slot_pos (a : Plot) {g : Board M} (h : a.cardStep g) : 0 ≤ a.slotC g := + mul_nonneg a.marker_pos.le (by have h := a.costC_pos h; split_ifs <;> linarith) +lemma turn_prev (a : Plot) {g : Board M} (h : soundPrev g) (s : Scene M) + (h₁ : ReadyAt s) (h₂ : 0 ≤ s.norm) (h₃ : share s ≤ 2) : soundPrev (a.turn g s) := by + intro x H; simp only [turn] at H + split_ifs at H with heq he + · rw [Option.some.injEq] at H; subst x; exact ⟨h₁,h₂,h₃⟩ + · exact h x H + +private lemma changed_point {p : Plot} {g : Board M} {s q : Scene M} {u} + (h : Following j J f (.ran s q u)) (ht : soundPrev g) (hd : 0 < s.norm) + (hg : g.chart.Sound) (hk : cardStep p (p.turn g q)) : + let C := p.turn g q + p.marker*p.birth g q+ + (uniform Bool).Ex (fun b => p.slotC C + p.deltaC C (q.smaller b)) + ≤ p.slotC g + p.deltaC g s +4*(share s-share q) +8*fisher q (p.kObs C q) := by + classical + intro C + let Q := uniform Bool + have HV : Widen q p.ix p.main g.chart C.chart := zoom_spec g.chart hg p.ix p.main h + obtain ⟨ha,hb,hc,hx,hy,hu⟩ := h + have hs : ReadyAt s := ⟨ha.within,ha.positive⟩ + have hq : ReadyAt q := ⟨hc.within,hc.positive⟩ + obtain ⟨h₁,h₂⟩ := share_small hx + have hI : q.here.lev < M := (rooted _ (hu false)).within.2 + have ht' (b : Bool) : ReadyAt (q.smaller b) := ⟨child_ok q.here hc.within.1 hI b,hc.positive⟩ + have hY (b : Bool) : share (q.smaller b) ≤ 4 := share_child q hc.within.1 hI hc.positive h₁ h₂ b + let V (i : Scene M) := portrait p.main i C.chart.items p.ix p.cap + let k := p.kObs C q + let F (j : Scene M) (s : Scene M) := cloud (V s) (V j) + have Hx (t : Scene M) (ht : ReadyAt t) (hv : 0 ≤ t.norm) : + Q.Ex (fun b => F t (q.smaller b)) ≤ F t q+4*fisher q k := + cloud_drift q hc.within.1 hI hc.positive h₁.le k (V t) (nonnegative_hist _ ht.2 hv _) + have hp : p.creditsC C ≤ p.creditsC g := by + have hZ : g.chart.size ≤ C.chart.size := Finset.card_le_card HV.extend + exact sub_le_sub_left (by exact_mod_cast hZ) _ + have hi : 0 ≤ share s-share q := by + unfold share; rw [hb.1.flat] + exact sub_nonneg.mpr (div_le_div_of_nonneg_right (hb.1.mass_le ha.positive ha.within) hd.le) + have hF : 0 ≤ fisher q k := (fisher_range _ hc.within.1 hI hc.positive h₁.le _).1 + have hcN := p.deltaC_pos ht s hs hd.le + + have H {w : Scene M} (hw : g.previous=some w) (heq : C.chart.items=g.chart.items) : + Q.Ex (fun b => F w (q.smaller b)) ≤ p.deltaC g s + 4*(share s-share q)+4*fisher q k := by + have hT := ht _ hw + apply le_trans (Hx _ hT.1 hT.2.1) + apply add_le_add_left + have H : p.deltaC g s = cloud (hist s k) (V w) := by + unfold deltaC; rw [hw] + unfold V k kObs; rw [heq] + unfold portrait sight optic; rw [hb.1.fund] + rw [H] + exact cloud_kill hb.1 hs hq hd.le k _ (nonnegative_hist _ hT.1.2 hT.2.1 _) + have hh : C.previous = if p.freshAt g q then some q else g.previous := p.prior_step _ _ hk + rw [Ex_add (f:=fun _ => slotC p C),Ex_const] + by_cases he : p.freshAt g q + · rw [ite_eq_left he] at hh + have Hs : Q.Ex (fun b => p.deltaC C (q.smaller b)) ≤ 4*fisher q k := by + have hx := Hx _ hq h₁.le + have ht (x : Scene M) : p.deltaC C x=F q x := by unfold deltaC; rw [hh] + simp_rw [ht] + have hc : F q q = 0 := cloud_same _ + rwa [hc,zero_add] at hx + rw [birth,ite_eq_left he,mul_one] + change _ + (_+Q.Ex _) ≤ _+8*fisher q k + have hSlot : p.slotC C = p.marker*p.creditsC C := by unfold slotC; simp [hh] + rw [hSlot] + cases hv : g.previous with + | none => + unfold slotC; rw [ite_eq_left hv,mul_add,mul_one] + nlinarith [marker_pos p] + | some w => + unfold slotC; rw [ite_eq_right (by rw [hv]; simp),add_zero] + unfold freshAt at he; rw [hv] at he + rcases he with he|he + · have H : C.chart.items≠g.chart.items := he + have HX : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨HV.extend, Ne.symm H⟩ + have hz : p.creditsC C+1 ≤ p.creditsC g := by + have hi : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card HX + unfold creditsC; linarith + nlinarith [marker_pos p] + · + by_cases H0 : C.chart.items=g.chart.items + case neg => + have HX : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨HV.extend, Ne.symm H0⟩ + have hz : p.creditsC C+1 ≤ p.creditsC g := by + have hi : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card HX + unfold creditsC; linarith + nlinarith [marker_pos p] + obtain ⟨b,hG⟩ := he + have HV := H hv H0 + have HH : p.marker ≤ Q.Ex (fun b => F w (q.smaller b)) := by + let l := q.smaller b + obtain ⟨hN1,hN2,hN3⟩ := ht _ hv + have hZ (i) : 0 ≤ F w (q.smaller i) := cloud_nonneg _ _ + (nonnegative_hist _ (ht' i).2 h₁.le _) (nonnegative_hist _ hN1.2 hN2 _) + have hY' : (sharp p.ix)^2 ≤ 6*F w l := by + have Hv : sharp p.ix ≤ distance (V l) (V w) := by + unfold V; rw [H0]; exact le_of_not_ge hG + apply le_trans (pow_le_pow_left₀ (sharp_pos _).le Hv _) + apply le_trans (distance_cloud _ _ (nonnegative_hist _ (ht' _).2 h₁.le _) + (nonnegative_hist _ hN1.2 hN2 _)) + have ht : ∑ i,V w i = share w := total_hist w hN1.1 _ + have ht' : ∑ i,V l i = share l := total_hist l (ht' b).1 _ + change ((∑ i, V l i)+ ∑ i,V w i)*F w l ≤ _ + rw [ht,ht'] + apply mul_le_mul_of_nonneg_right (by linarith [show share l ≤ 4 from hY b]) (hZ b) + unfold Q Ex + rw [Fintype.sum_bool,uniform_apply,uniform_apply] + unfold marker; unfold l at * + cases b <;> simp only [Fintype.card_bool,Nat.cast_ofNat] <;> nlinarith [hZ true,hZ false] + nlinarith [marker_pos p] + + rw [ite_eq_right he] at hh + obtain ⟨w,hv⟩ : ∃ w, g.previous=some w := by + cases hx : g.previous + · simp [freshAt,hx] at he + · exact ⟨_,rfl⟩ + have hs : p.birth g q=0 := ite_eq_right he + have heq : C.chart.items=g.chart.items := by + unfold freshAt at he; rw [hv] at he; exact Classical.byContradiction (fun H => he (Or.inl H)) + have hS' : p.slotC C=p.slotC g := by simp [slotC,creditsC,Chart.size, hh,heq] + have hV' (x : Scene M) : p.deltaC C x=F w x := by unfold deltaC; rw [hh,hv] + rw [hs,mul_zero,hS']; simp_rw [hV'] + have ht := H hv heq + change _ ≤ _+8*fisher q k + unfold Q at *; linarith + + +def coinC (p : Plot) := ((rangeIx p.cap : ℕ):ℝ)*precision p.ix p.cap + +def debtC (p : Plot) (g : Board M) (x : Scene M) (j : ℕ) := + deficit x (p.kObs g x)+(p.creditsC g*p.coinC+(minimumT j)⁻¹) + +private lemma debt_recur (p : Plot) (g : Board M) {s q : Scene M} {k} + (h : Following j J f (.ran s q k)) (hd : 0 < s.norm) (hg : g.chart.Sound) + (hk : cardStep p g) (hp : cardStep p (p.turn g q)) : + let C := p.turn g q + (uniform Bool).Ex (fun b => share (q.smaller b)*debtC p C (q.smaller b) j) + + (1/4:ℝ)*fisher q (p.kObs C q) + ≤ share s * debtC p g s j := by + intro C + have hm := pre_pos h + have hC : Widen q p.ix p.main g.chart C.chart := zoom_spec _ hg _ _ h + let v := (keep s q).snip + let U := (step q).probs + let Q := v.probs + let Z (c : Board M) := p.kObs c q + let F := Z g; let W := Z C + let H (x) := deficit x F + let E (i) := deficit (q.smaller i) W + let V (c : Board M) := p.creditsC c*p.coinC+(minimumT j)⁻¹ + have HE : Q.entropy ≤ (minimumT j)⁻¹* Q false := debt_at h + obtain ⟨ha,hb,hv,hp',ht,hs⟩ := h + have hT (b : Bool) := hs b + have hR : q.here.lev < M := (rooted _ (hs false)).within.2 + have hS : ReadyAt s := ⟨ha.within,ha.positive⟩ + have hS' : ReadyAt q := ⟨hv.within,hv.positive⟩ + have hQ := hp'.1.2.2 + have hZ : 0 < q.norm := hb.1.flat.symm ▸ hd + have hM := keep_true hb.1 hS hS' hm + have hi₁ : deficit q W+V C ≤ H q+V g := by + by_cases hi : C.chart.items=g.chart.items + · unfold W Z H F V kObs creditsC size; rw [hi]; exact le_rfl + have hG : p.creditsC C+1 ≤ p.creditsC g := by + have hh : g.chart.items ⊂ C.chart.items := Finset.ssubset_iff_subset_ne.mpr ⟨hC.extend, Ne.symm hi⟩ + have he : (g.chart.size:ℝ)+1 ≤ C.chart.size := by exact_mod_cast Finset.card_lt_card hh + unfold creditsC; linarith + have hh1 : 0 ≤ p.coinC := by unfold coinC; positivity + have h₁ := (sight_budget q p.main g.chart.items (precision p.ix p.cap)).1 + have h₂ := (sight_budget q p.main C.chart.items (precision p.ix p.cap)).2 + change deficit q W ≤ _ at h₂ + change _ ≤ H q at h₁ + have h₃ : (C.chart.items.card :ℝ)* precision p.ix p.cap ≤ p.coinC := by + unfold coinC + exact mul_le_mul_of_nonneg_right (by exact_mod_cast hp) (by positivity) + unfold V + nlinarith + have hv₀ : (minimumT j)⁻¹ ≤ V g := + le_add_of_nonneg_left (mul_nonneg (costC_pos _ hk) (by unfold coinC; positivity)) + have h₂ : Q true*(H q+V g) ≤ H s+V g := by + have hi (b : Bool) : levels (v.dst b)=levels s := by + unfold levels; rw [show (v.dst b).here=s.here from keep_here hb.1 _] + have hz : Q.Ex (fun b => H (v.dst b)) ≤ H s+Q.entropy := by + have hh := old_entropy_cost v F + have hh' := conditioned_balance F v hM + unfold H deficit; simp_rw [hi,Ex_sub (f:=fun _ => levels s),Ex_const] + change v.probs.Ex _+ _ = s.actor.privateH F at hh' + unfold Q; linarith + have hZ : 0 ≤ Q false * H (v.dst false) := by + rcases eq_or_lt_of_le (Q.nonneg false) with h|h + · rw [← h,zero_mul] + have HH := other_ok hb.1 hS + exact mul_nonneg h.le (deficit_nonneg (v.dst false) HH.1 HH.2 ((true_mass hM).mp h) F) + have ht₁ : Q true + Q false = 1 := by simpa only [Fintype.sum_bool] using Q.total + rw [Ex,Fintype.sum_bool] at hz + rw [show v.dst true=q from rfl] at hz + nlinarith [mul_le_mul_of_nonneg_right hv₀ (Q.nonneg false),show + V g*(Q true+Q false)=V g by rw [ht₁]; ring] + have ht₁ := space_deficit q W hv.within.1 hR hv.positive hQ hZ + have hx b : debtC p C (q.smaller b) j=E b+V C := rfl + let D (b : Bool) := E b+V C + have h₁ : + (uniform Bool).Ex (fun b => share (q.smaller b)*debtC p C (q.smaller b) j)= + share q * U.Ex D := by + have HI (b : Bool) : share (q.smaller b)*debtC p C (q.smaller b) j = + (q.smaller b).mass * (q.norm⁻¹ * D b) := by rw [hx]; unfold share D; simp only [smaller]; ring + simp_rw [HI] + rw [← output_split _ hv.within.1 hR hv.positive hQ (fun b => q.norm⁻¹*D b), + Ex_lmul] + unfold share U; ring + have HZ : share q * ((1/4:ℝ)*(q.norm/q.mass)*fisher q W)=1/4*fisher q W := by + unfold share; field_simp + have hEq : share q = share s * Q true := by + have H := kept_mass hb.1 hS hS' hm + unfold share + nth_rw 1 [H,hb.1.flat] + unfold Q v; ring + rw [h₁,← HZ,← mul_add] + unfold D; rw [Ex_add (g:=fun b => V C),Ex_const] + have hDC : debtC p g s j=H s+V g := by + unfold debtC H F V Z kObs sight optic + rw [hb.1.fund] + have hsq : 0 ≤ share q := div_nonneg hQ.le hZ.le + calc + _ ≤ share q * (deficit q W + V C) := by + change U.Ex E + _ ≤ deficit q W at ht₁ + apply mul_le_mul_of_nonneg_left _ hsq; linarith + _ ≤ share q*(H q+V g) := mul_le_mul_of_nonneg_left hi₁ hsq + _ ≤ _ := by + rw [hEq,mul_assoc,hDC] + exact mul_le_mul_of_nonneg_left h₂ (div_nonneg hm.le hd.le) + +def expenses (p : Plot) (g : Board M) (s : Scene M) (j : ℕ) := + p.slotC g + p.deltaC g s + 4*share s + 32*(share s*debtC p g s j) + +lemma expenses_nonneg (p : Plot) (g : Board M) (s : Scene M) (j : ℕ) (hg : p.cardStep g) + (h : ReadyAt s) (hp : soundPrev g) (hv : 0 < s.norm) : + 0 ≤ p.expenses g s j := by + have hm : 0 ≤ s.mass := avg_nonneg _ fun i _ => h.2 i + have hsc := deltaC_pos p hp s h hv.le + have Ht := slot_pos p hg + unfold expenses; have hS : 0 ≤ share s := div_nonneg hm hv.le + suffices HH : 0 ≤ share s*debtC p g s j by linarith + rcases eq_or_lt_of_le hm with H|H + · unfold share; rw [← H,zero_div,zero_mul] + apply mul_nonneg hS + unfold debtC + have hC := p.costC_pos hg + exact add_nonneg (deficit_nonneg _ h.1 h.2 H _) (add_nonneg + (mul_nonneg hC (by unfold coinC; positivity)) (inv_nonneg.mpr (minimum_pos j).le)) + +/-- number born before cap breach; representatives from these births may be retained for +delayed estimates on further jumps -/ +def minted (p : Plot) (g : Board M) : List (Beat M) → ℝ +| [] => 0 +| b::l => let c := p.turn g b.post + if p.cardStep c then birth p g b.post + minted p c l else 0 + +lemma mint_small (p : Plot) (g : Board M) (s : Scene M) (t : Thread M s) + (h : Following j J f t) (hp : g.chart.Sound) (hg : soundPrev g) (hv : p.cardStep g) + (hs : 0 < s.norm) : + p.marker*(s.here.dice M).Ex (fun x => minted p g (t.stream x)) ≤ expenses p g s j := by + induction t generalizing g with + | blank => + have h1 := rooted _ h + simpa only [stream,minted,Ex_const,mul_zero] using + expenses_nonneg p g _ j hv ⟨h1.within,h1.positive⟩ hg hs + | ran s q k ih => + rw [stream_branch k h (fun l _ => minted p g l)] + let C := p.turn g q + by_cases hh : p.cardStep C + case neg => + have h1 := rooted _ h + dsimp only [C] at hh + simpa only [minted, ite_eq_right hh, Ex_const,mul_zero] using + expenses_nonneg p g s j hv ⟨h1.within,h1.positive⟩ hg hs + have hD := changed_point h hg hs hp hh + have hF := debt_recur p _ h hs hp hv hh + have hZ := zoom_spec g.chart hp p.ix p.main h + obtain ⟨ha,hb,hc,hx,hd,hk⟩ := h + have hv := turn_prev p hg q ⟨hc.within,hc.positive⟩ (share_small hx).1.le (share_small hx).2 + have hq : (uniform Bool).Ex (fun b => share (q.smaller b))=share q := by + simp only [Ex,uniform_apply,Fintype.sum_bool,Fintype.card_bool,Nat.cast_ofNat] + have ht := share_split q; linarith + let f (i : Bool) := ((q.smaller i).here.dice M).Ex fun x => minted p C ((k i).stream x) + have HM : + p.marker*((uniform Bool).Ex f) ≤ (uniform Bool).Ex fun i => expenses p C (q.smaller i) j := by + rw [← Ex_lmul]; apply Ex_mono; intro i + exact ih i C (hk i) hZ.sonic hv hh (share_small hx).1 + unfold expenses at * + simp_rw [Ex_add] at * + simp_rw [Ex_const,Ex_lmul] at HM hD + rw [hq] at HM + + have HH : p.cardStep (p.turn g q) := hh + simp only [minted, ite_eq_left HH] + simp_rw [Ex_add,Ex_const] + change p.marker*(p.birth g q+(uniform Bool).Ex f) ≤ _ + dsimp only [C] at * + nlinarith + +end Plot +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Snapshot/Representative.lean b/lean/OAI/Analysis/FourierLLogL/Snapshot/Representative.lean index b14b187df22fbd8992a102693a755d7378414a6b..84ff87ec7908131bf77dfab8184322bc39ad3bd1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Snapshot/Representative.lean +++ b/lean/OAI/Analysis/FourierLLogL/Snapshot/Representative.lean @@ -1,188 +1,188 @@ -import OAI.Analysis.FourierLLogL.Snapshot.Display - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Procedure.Snapshot -open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture -variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} -open scoped Classical - -def tolerance (j i : ℕ) (main : Bool) := - if main then sig j else side i ^4/65536 -def spotLimit (s : Scene M) (j i : ℕ) (main : Bool) := - tolerance j i main * s.here.invlen - -lemma tolerance_pos (j i : ℕ) (b : Bool) : 0 < tolerance j i b := by - unfold tolerance sig; have H := slow_pos j; have HH := side_pos i; split <;> positivity -lemma spotLimit_pos (s : Scene M) (j i : ℕ) (b : Bool) : 0 < spotLimit s j i b := - mul_pos (tolerance_pos _ _ _) s.here.invlen_pos - -def adjacent (s : Scene M) (j i : ℕ) (main : Bool) (x : Grid M) := - if main then fnear s.fund.mood (sepRad j) (spotLimit s j i main) x else rad x ≤ spotLimit s j i main - -lemma adjacent_zero (s : Scene M) (j i : ℕ) (b : Bool) : - adjacent s j i b 0 := by - have H := spotLimit_pos s j i b - cases b - · change rad _ ≤ _ - simpa [rad] using H.le - · refine ⟨0,?_,?_⟩ - · rw [delta_zero]; unfold sepRad; positivity - simp only [sub_self] - simpa [rad] using H.le - -lemma adjacent_neg (s : Scene M) (j i : ℕ) (b : Bool) {x : Grid M} (hx : adjacent s j i b x) : - adjacent s j i b (-x) := by - cases b with - | false => - change rad (-_) ≤ _ - rw [rad_even]; exact hx - | true => - obtain ⟨v,hv,h⟩ := (show fnear _ _ _ x from hx) - refine ⟨-v,?_,?_⟩ - · rw [delta_even _ v]; exact hv - rw [show -x- -v= -(x-v) by ring,rad_even]; exact h - -def net_ok (s : Scene M) (j i : ℕ) (b : Bool) (d : Finset (Grid M)) := - (∀ x∈d, intense b s (side (i+1)) x) ∧ - (∀ x ∈ d,∀ y∈d,x≠y → ¬ adjacent s j i b (x-y)) ∧ - ∀ x, intense b s (side (i+1)) x → ∃ y∈d, adjacent s j i b (x-y) -lemma exists_net (s : Scene M) (j i : ℕ) (b : Bool) : ∃ d, net_ok s j i b d := by - classical - let L := intense b s (side (i+1)); let H := adjacent s j i b - let P (d : Finset (Grid M)) := (∀ i∈d, L i) ∧ (∀ i∈d,∀ u∈d,i≠u → ¬ H (i-u)) - let F := Finset.univ.filter P - have he : (∅:Finset (Grid M))∈F := by simp [F,P] - obtain ⟨d,hd,hh⟩ := Finset.exists_max_image F Finset.card ⟨_,he⟩ - have hp := (Finset.mem_filter.mp hd).2 - refine ⟨d,hp.1,hp.2,?_⟩ - intro x hx - by_contra! Hh - have hn : x∉d := by intro he; apply Hh _ he; rw [sub_self]; exact adjacent_zero _ _ _ _ - have hh' : insert x d ∈ F := by - apply Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩ - refine ⟨?_,?_⟩ - · intro w ht - rcases Finset.mem_insert.mp ht with h|h; exact h ▸ hx; exact hp.1 _ h - intro w hw q hq h' - rcases Finset.mem_insert.mp hw with rfl|hw - · exact Hh q ((Finset.mem_insert.mp hq).resolve_left (Ne.symm h')) - rcases Finset.mem_insert.mp hq with he|he - · subst q - intro hc - apply Hh w hw - rw [show x-w= -(w-x) by ring] - exact adjacent_neg _ _ _ _ hc - exact hp.2 _ hw _ he h' - have hi := hh _ hh' - rw [Finset.card_insert_of_notMem hn] at hi; omega - -def net (s : Scene M) (j i : ℕ) (b : Bool) := Classical.choose (exists_net s j i b) -lemma net_spec (s : Scene M) (j i : ℕ) (b : Bool) : - net_ok s j i b (net s j i b) := Classical.choose_spec _ - -lemma net_len_main (s : Scene M) (j i : ℕ) (h : i+1 ≤ testIx j) (hs : s.settled j) - (he : s.live=true) : ((net s j i true).card:ℝ) ≤ exp (kappa j (codeWeight capA) (i+1)) := by - apply le_of_not_gt - intro hh - obtain ⟨H,H1,H2⟩ := net_spec s j i true - exact hs he _ h ⟨_,hh.le,H,H1⟩ - -lemma share_pos' (s : Scene M) (hs : ReadyAt s) (hp : 0 ≤ s.norm) : 0 ≤ share s := - div_nonneg (avg_nonneg _ (fun x _ => hs.2 x)) hp -lemma share_small {s : Scene M} (hb : work s j J f) : 0 < s.norm ∧ share s ≤ 2 := by - have hi := hb.2.2 - have hx := hb.1.2.2 - unfold enter at hi - refine ⟨by linarith,?_⟩ - exact (div_le_iff₀ (by linarith)).mpr (le_of_not_gt hi) -lemma share_child (s : Scene M) (hp : s.here.valid) (h : s.here.lev < M) - (hs : 0 ≤ s.wage) (hh : 0 < s.norm) (hb : share s ≤ 2) (b) : - share (s.smaller b) ≤ 4 := by - have H := share_split s - have HF (i : Bool) : 0 ≤ share (s.smaller i) := share_pos' (s.smaller i) - ⟨s.here.child_ok hp h i,hs⟩ hh.le - cases b <;> linarith [HF false,HF true] - -lemma scalar_point (s : Scene M) (i : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) - {p : Node} (ht : below s.here p) (x : Grid M) (he : x∈p.block M) - (k : Grid M) : - ‖scalar s i-chi (i-k) x*scalar s k‖ ≤ share s * (8 * p.len * rad (i-k)) := by - have HF y (hy : y∈s.here.block M) : - ‖chi i y-chi (i-k) x*chi k y‖ ≤ 8*p.len * rad (i-k) := by - rw [show chi i y-chi (i-k) x*chi k y=(chi (i-k) y-chi (i-k) x)*chi k y by - rw [sub_mul,← chi_add',sub_add_cancel],norm_mul,norm_chi,mul_one] - have h := phase_distance (i-k) y x - apply h.trans - rw [mul_comm (rad _)] - have hi := diam_block (block_of_below ht _ hy) he - gcongr - exact rad_nonneg _ - unfold scalar; rw [Finset.mul_sum,←Finset.sum_sub_distrib] - apply le_trans (norm_sum_le _ _) - rw [← ante_total s hp.1, Finset.sum_mul] - apply Finset.sum_le_sum; intro y hy - unfold ante; rw [mul_left_comm,← mul_sub,norm_mul, Complex.norm_real,Real.norm_eq_abs, - abs_of_nonneg (wt_nonneg s.here hp.2 hh _)] - by_cases H : y∈s.here.block M - · exact mul_le_mul_of_nonneg_left (HF _ H) (wt_nonneg s.here hp.2 hh _) - rw [wt,dice_apply hp.1,ite_eq_right H,zero_mul,zero_div] - simp - -lemma scalar_compare (s : Scene M) (i k : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) - (b : ℝ) (hb : share s ≤ b) : - |‖scalar s i‖-‖scalar s k‖| ≤ b * (8*s.here.len*rad (i-k)) := by - have Hs := hp.1 - let x := s.here.start M - have HH := abs_norm_sub_norm_le (scalar s i) (chi (i-k) x*scalar s k) - rw [norm_mul,norm_chi,one_mul] at HH - apply HH.trans ((scalar_point s i hp hh (below_refl _) x (start_mem Hs.1 Hs.2) k).trans ?_) - exact mul_le_mul_of_nonneg_right hb (by have := s.here.len_pos; have := rad_nonneg (i-k); positivity) - - -lemma scalar_power (s : Scene M) (hp : ReadyAt s) (hh : 0 < s.norm) (d b : ℝ) - (h : ∀ x, s.wage x ≤ d) (he : share s ≤ b) (hd : 0 ≤ d) : - ∑ i, ‖scalar s i‖^2 ≤ s.here.invlen * b * d / s.norm := by - let p := s.here - let P := ante s - let f (i : Grid M) : ℂ := ((P i / side M:ℝ):ℂ) - have hb := side_pos M - have Hχ (i : Grid M) : scalar s i = fc f (-i) := by - unfold scalar fc expU; rw [neg_neg, Complex.real_smul,Finset.mul_sum] - exact Finset.sum_congr rfl fun _ _ => by unfold f P ante; push_cast; field_simp [(Complex.ofReal_ne_zero.mpr hb.ne')] - have HT (i : Grid M) : - side M*‖f i‖^2 ≤ P i * (p.invlen * d / s.norm) := by - have hi : P i ≤ side M*p.invlen * d / s.norm := by - unfold P ante wt; rw [dice_apply hp.1] - split_ifs with hx - · rw [rinv p] - apply div_le_div_of_nonneg_right _ hh.le - exact mul_le_mul_of_nonneg_left (h i) (by have := s.here.len_pos; positivity) - · simp only [zero_mul,zero_div]; have := p.invlen_pos; positivity - unfold f - have he : 0 ≤ P i := ante_nonneg s hp.2 hh.le _ - rw [Complex.norm_real,Real.norm_eq_abs,sq_abs] - calc - _ = P i * (P i / side M) := by field_simp - _ ≤ _ := by - apply mul_le_mul_of_nonneg_left _ he - apply (div_le_iff₀ hb).mpr - apply hi.trans; exact le_of_eq (by ring) - calc - _ = ∑ i, ‖fc f i‖^2 := by simp_rw [Hχ]; exact Fintype.sum_equiv (Equiv.neg _) _ _ (fun _ => rfl) - _ = _ := fc_parseval f - _ ≤ ∑ i, P i * (p.invlen *d/s.norm) := by - unfold expU; rw [smul_eq_mul,Finset.mul_sum]; exact Finset.sum_le_sum fun i _ => HT i - _ ≤ _ := by - rw [← Finset.sum_mul,show ∑ i,P i = _ from ante_total s hp.1] - apply (mul_le_mul_of_nonneg_right he (show 0 ≤ p.invlen*d/s.norm by - have := p.invlen_pos; positivity)).trans - exact le_of_eq (by unfold p; ring) - -end FourierLLogL.Dyadic.Procedure.Snapshot - -end OAI +import OAI.Analysis.FourierLLogL.Snapshot.Display + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Procedure.Snapshot +open Law Node Real Period Compress.Params Thermal Fund Scene Fork Road Event Journal Wire Pulse Thread Capture +variable {M J j : ℕ} {s : Scene M} {f : Grid M → ℝ} +open scoped Classical + +def tolerance (j i : ℕ) (main : Bool) := + if main then sig j else side i ^4/65536 +def spotLimit (s : Scene M) (j i : ℕ) (main : Bool) := + tolerance j i main * s.here.invlen + +lemma tolerance_pos (j i : ℕ) (b : Bool) : 0 < tolerance j i b := by + unfold tolerance sig; have H := slow_pos j; have HH := side_pos i; split <;> positivity +lemma spotLimit_pos (s : Scene M) (j i : ℕ) (b : Bool) : 0 < spotLimit s j i b := + mul_pos (tolerance_pos _ _ _) s.here.invlen_pos + +def adjacent (s : Scene M) (j i : ℕ) (main : Bool) (x : Grid M) := + if main then fnear s.fund.mood (sepRad j) (spotLimit s j i main) x else rad x ≤ spotLimit s j i main + +lemma adjacent_zero (s : Scene M) (j i : ℕ) (b : Bool) : + adjacent s j i b 0 := by + have H := spotLimit_pos s j i b + cases b + · change rad _ ≤ _ + simpa [rad] using H.le + · refine ⟨0,?_,?_⟩ + · rw [delta_zero]; unfold sepRad; positivity + simp only [sub_self] + simpa [rad] using H.le + +lemma adjacent_neg (s : Scene M) (j i : ℕ) (b : Bool) {x : Grid M} (hx : adjacent s j i b x) : + adjacent s j i b (-x) := by + cases b with + | false => + change rad (-_) ≤ _ + rw [rad_even]; exact hx + | true => + obtain ⟨v,hv,h⟩ := (show fnear _ _ _ x from hx) + refine ⟨-v,?_,?_⟩ + · rw [delta_even _ v]; exact hv + rw [show -x- -v= -(x-v) by ring,rad_even]; exact h + +def net_ok (s : Scene M) (j i : ℕ) (b : Bool) (d : Finset (Grid M)) := + (∀ x∈d, intense b s (side (i+1)) x) ∧ + (∀ x ∈ d,∀ y∈d,x≠y → ¬ adjacent s j i b (x-y)) ∧ + ∀ x, intense b s (side (i+1)) x → ∃ y∈d, adjacent s j i b (x-y) +lemma exists_net (s : Scene M) (j i : ℕ) (b : Bool) : ∃ d, net_ok s j i b d := by + classical + let L := intense b s (side (i+1)); let H := adjacent s j i b + let P (d : Finset (Grid M)) := (∀ i∈d, L i) ∧ (∀ i∈d,∀ u∈d,i≠u → ¬ H (i-u)) + let F := Finset.univ.filter P + have he : (∅:Finset (Grid M))∈F := by simp [F,P] + obtain ⟨d,hd,hh⟩ := Finset.exists_max_image F Finset.card ⟨_,he⟩ + have hp := (Finset.mem_filter.mp hd).2 + refine ⟨d,hp.1,hp.2,?_⟩ + intro x hx + by_contra! Hh + have hn : x∉d := by intro he; apply Hh _ he; rw [sub_self]; exact adjacent_zero _ _ _ _ + have hh' : insert x d ∈ F := by + apply Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩ + refine ⟨?_,?_⟩ + · intro w ht + rcases Finset.mem_insert.mp ht with h|h; exact h ▸ hx; exact hp.1 _ h + intro w hw q hq h' + rcases Finset.mem_insert.mp hw with rfl|hw + · exact Hh q ((Finset.mem_insert.mp hq).resolve_left (Ne.symm h')) + rcases Finset.mem_insert.mp hq with he|he + · subst q + intro hc + apply Hh w hw + rw [show x-w= -(w-x) by ring] + exact adjacent_neg _ _ _ _ hc + exact hp.2 _ hw _ he h' + have hi := hh _ hh' + rw [Finset.card_insert_of_notMem hn] at hi; omega + +def net (s : Scene M) (j i : ℕ) (b : Bool) := Classical.choose (exists_net s j i b) +lemma net_spec (s : Scene M) (j i : ℕ) (b : Bool) : + net_ok s j i b (net s j i b) := Classical.choose_spec _ + +lemma net_len_main (s : Scene M) (j i : ℕ) (h : i+1 ≤ testIx j) (hs : s.settled j) + (he : s.live=true) : ((net s j i true).card:ℝ) ≤ exp (kappa j (codeWeight capA) (i+1)) := by + apply le_of_not_gt + intro hh + obtain ⟨H,H1,H2⟩ := net_spec s j i true + exact hs he _ h ⟨_,hh.le,H,H1⟩ + +lemma share_pos' (s : Scene M) (hs : ReadyAt s) (hp : 0 ≤ s.norm) : 0 ≤ share s := + div_nonneg (avg_nonneg _ (fun x _ => hs.2 x)) hp +lemma share_small {s : Scene M} (hb : work s j J f) : 0 < s.norm ∧ share s ≤ 2 := by + have hi := hb.2.2 + have hx := hb.1.2.2 + unfold enter at hi + refine ⟨by linarith,?_⟩ + exact (div_le_iff₀ (by linarith)).mpr (le_of_not_gt hi) +lemma share_child (s : Scene M) (hp : s.here.valid) (h : s.here.lev < M) + (hs : 0 ≤ s.wage) (hh : 0 < s.norm) (hb : share s ≤ 2) (b) : + share (s.smaller b) ≤ 4 := by + have H := share_split s + have HF (i : Bool) : 0 ≤ share (s.smaller i) := share_pos' (s.smaller i) + ⟨s.here.child_ok hp h i,hs⟩ hh.le + cases b <;> linarith [HF false,HF true] + +lemma scalar_point (s : Scene M) (i : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) + {p : Node} (ht : below s.here p) (x : Grid M) (he : x∈p.block M) + (k : Grid M) : + ‖scalar s i-chi (i-k) x*scalar s k‖ ≤ share s * (8 * p.len * rad (i-k)) := by + have HF y (hy : y∈s.here.block M) : + ‖chi i y-chi (i-k) x*chi k y‖ ≤ 8*p.len * rad (i-k) := by + rw [show chi i y-chi (i-k) x*chi k y=(chi (i-k) y-chi (i-k) x)*chi k y by + rw [sub_mul,← chi_add',sub_add_cancel],norm_mul,norm_chi,mul_one] + have h := phase_distance (i-k) y x + apply h.trans + rw [mul_comm (rad _)] + have hi := diam_block (block_of_below ht _ hy) he + gcongr + exact rad_nonneg _ + unfold scalar; rw [Finset.mul_sum,←Finset.sum_sub_distrib] + apply le_trans (norm_sum_le _ _) + rw [← ante_total s hp.1, Finset.sum_mul] + apply Finset.sum_le_sum; intro y hy + unfold ante; rw [mul_left_comm,← mul_sub,norm_mul, Complex.norm_real,Real.norm_eq_abs, + abs_of_nonneg (wt_nonneg s.here hp.2 hh _)] + by_cases H : y∈s.here.block M + · exact mul_le_mul_of_nonneg_left (HF _ H) (wt_nonneg s.here hp.2 hh _) + rw [wt,dice_apply hp.1,ite_eq_right H,zero_mul,zero_div] + simp + +lemma scalar_compare (s : Scene M) (i k : Grid M) (hp : ReadyAt s) (hh : 0 ≤ s.norm) + (b : ℝ) (hb : share s ≤ b) : + |‖scalar s i‖-‖scalar s k‖| ≤ b * (8*s.here.len*rad (i-k)) := by + have Hs := hp.1 + let x := s.here.start M + have HH := abs_norm_sub_norm_le (scalar s i) (chi (i-k) x*scalar s k) + rw [norm_mul,norm_chi,one_mul] at HH + apply HH.trans ((scalar_point s i hp hh (below_refl _) x (start_mem Hs.1 Hs.2) k).trans ?_) + exact mul_le_mul_of_nonneg_right hb (by have := s.here.len_pos; have := rad_nonneg (i-k); positivity) + + +lemma scalar_power (s : Scene M) (hp : ReadyAt s) (hh : 0 < s.norm) (d b : ℝ) + (h : ∀ x, s.wage x ≤ d) (he : share s ≤ b) (hd : 0 ≤ d) : + ∑ i, ‖scalar s i‖^2 ≤ s.here.invlen * b * d / s.norm := by + let p := s.here + let P := ante s + let f (i : Grid M) : ℂ := ((P i / side M:ℝ):ℂ) + have hb := side_pos M + have Hχ (i : Grid M) : scalar s i = fc f (-i) := by + unfold scalar fc expU; rw [neg_neg, Complex.real_smul,Finset.mul_sum] + exact Finset.sum_congr rfl fun _ _ => by unfold f P ante; push_cast; field_simp [(Complex.ofReal_ne_zero.mpr hb.ne')] + have HT (i : Grid M) : + side M*‖f i‖^2 ≤ P i * (p.invlen * d / s.norm) := by + have hi : P i ≤ side M*p.invlen * d / s.norm := by + unfold P ante wt; rw [dice_apply hp.1] + split_ifs with hx + · rw [rinv p] + apply div_le_div_of_nonneg_right _ hh.le + exact mul_le_mul_of_nonneg_left (h i) (by have := s.here.len_pos; positivity) + · simp only [zero_mul,zero_div]; have := p.invlen_pos; positivity + unfold f + have he : 0 ≤ P i := ante_nonneg s hp.2 hh.le _ + rw [Complex.norm_real,Real.norm_eq_abs,sq_abs] + calc + _ = P i * (P i / side M) := by field_simp + _ ≤ _ := by + apply mul_le_mul_of_nonneg_left _ he + apply (div_le_iff₀ hb).mpr + apply hi.trans; exact le_of_eq (by ring) + calc + _ = ∑ i, ‖fc f i‖^2 := by simp_rw [Hχ]; exact Fintype.sum_equiv (Equiv.neg _) _ _ (fun _ => rfl) + _ = _ := fc_parseval f + _ ≤ ∑ i, P i * (p.invlen *d/s.norm) := by + unfold expU; rw [smul_eq_mul,Finset.mul_sum]; exact Finset.sum_le_sum fun i _ => HT i + _ ≤ _ := by + rw [← Finset.sum_mul,show ∑ i,P i = _ from ante_total s hp.1] + apply (mul_le_mul_of_nonneg_right he (show 0 ≤ p.invlen*d/s.norm by + have := p.invlen_pos; positivity)).trans + exact le_of_eq (by unfold p; ring) + +end FourierLLogL.Dyadic.Procedure.Snapshot + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Alarm.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Alarm.lean index a923aa33d9e77bba56c3f383f5fef637466ae42a..403dc4d26662763c85fb7845e0d08c0d3ee1bd89 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Alarm.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Alarm.lean @@ -1,210 +1,210 @@ -import OAI.Analysis.FourierLLogL.Thermal.Posterior - -/-! # Probability cost of clone evidence under the comparison model -/ -open Set Function Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M j : ℕ} -open scoped Classical - -def cushion (j : ℕ) := height j/H j^100 -def clones (j : ℕ) := slow j ^120 - -abbrev Clutch (j M : ℕ) := Grid M × (Fin (clones j) → Grid M) - -def fresh (A B : Form M) (j : ℕ) (y : Grid M) : Law (Clutch j M) := - (channel B y).tensorP (pack (fun _ => channel A y)) - -namespace Update -variable {p : Node} - -def consistent (_u : Update j M p) (A : Form M) (z z' : Grid M) : Prop := - form A (z'-z) ≤ 4*cushion j - -def single (u : Update j M p) (A : Form M) (z : Grid M) (v z' : Grid M) : Prop := - u.consistent A z z' ∧ beta u.ix /4 ≤ ‖u.app v z'‖ - -def alarm (u : Update j M p) (A : Form M) (z : Grid M) (x : Clutch j M) : Prop := - ∃ i, u.single A z x.1 (x.2 i) - - -def alarmQ (u : Update j M p) (A : Form M) (z y : Grid M) := - (fresh A (u.next A) j y).prob (u.alarm A z) - -def singleQ (u : Update j M p) (A : Form M) (z y : Grid M) := - (channel (u.next A) y).Ex fun v => (channel A y).prob (u.single A z v) - -lemma alarm_le (u : Update j M p) (A : Form M) (z y : Grid M) : - alarmQ u A z y ≤ clones j * singleQ u A z y := by - classical - let T := clones j - let L := fresh A (u.next A) j y - let F (i : Fin T) (w : Clutch j M) : ℝ := if u.single A z w.1 (w.2 i) then 1 else 0 - have hi (i : Fin T) : L.Ex (F i) = u.singleQ A z y := by - unfold L fresh F singleQ prob; rw [Ex_tensorP] - refine congrArg _ (funext fun v => ?_) - let f (z' : Grid M) := if u.single A z v z' then (1:ℝ) else 0 - exact Ex_pack_proj (fun _ => channel A y) i f - unfold alarmQ prob; apply le_trans (b:=L.Ex (fun w => ∑ i, F i w)) - · apply Ex_mono - intro w - split_ifs with H - · obtain ⟨i,hi⟩ := H - have HE := Finset.single_le_sum (f:=fun i => F i w) (fun i _ => by - unfold F; split_ifs <;> norm_num) (Finset.mem_univ i) - rwa [show F i w=1 from ite_eq_left hi] at HE - exact Finset.sum_nonneg fun i _ => by unfold F; split_ifs <;> norm_num - rw [Ex_finset_sum] - simp_rw [hi, Finset.sum_const, Finset.card_univ, Fintype.card_fin,nsmul_eq_mul] - exact le_rfl - -lemma shift_app (u : Update j M p) (x y z : Grid M) : - u.app (x+y) z = ply (snap_le (M:=M) j p) u.V - (fun i => u.v i z * chi (raise (snap_le (M:=M) j p) i) y) x := by - unfold app ply - refine congrArg _ ?_; funext i - rw [chi_add]; ring - -private lemma one_moment {A : Form M} {p : Node} (hp : p.ok M) (hA : 0 ≤ A) - {P : Law (Grid M)} {C : ℝ} (u : Update j M p) (HG : u.Good C A P) (HR : u.regular) - (s : Profile M) (hS : ∀ i∈s, 0 ≤ i.1) - (HH : A+place p M + mixForm s ≤ tilde (H j) A p) - (z' d : Grid M) : - expU (fun y => prefixE s p y * boltz A (z'-y) * ‖u.app (y+d) z'‖^(2*sLen j)) ≤ - exp (32 -F (A+place p M)-u.credit/4) := by - let t := p.start M - let B := place p M; let U := A+B; let V := tilde (H j) A p - let W : Profile M := (B,t)::(A,z')::s - have he (y : Grid M) : prefixE s p y * boltz A (z'-y) ≤ exp 32 * tune W y := by - unfold prefixE; split_ifs with hi - · have H : form B (t-y) ≤ 32 := place_internal (start_mem hp.1 hp.2) hi - have HH : 1 ≤ exp 32 * boltz B (t-y) := by unfold boltz; rw [← exp_add]; apply one_le_exp; linarith - unfold W; rw [tune_cons,tune_cons] - have hx : 0 ≤ tune s y * boltz A (z'-y) := mul_nonneg (tune_pos _ _).le (boltz_pos _ _).le - apply le_trans (le_mul_of_one_le_left hx HH) - exact le_of_eq (by ring) - · rw [zero_mul] - exact mul_nonneg (exp_nonneg _) (tune_pos _ _).le - let f (i) := u.v i z' * chi (raise (snap_le (M:=M) j p) i) d - have hf (i) : ‖f i‖ ≤ 1 := by - unfold f; rw [norm_mul,norm_chi,mul_one]; exact HG.unit _ _ - have hi (i : Grid M) : ‖expU (fun y => (↑(tune W y):ℂ)*chi i y)‖ ≤ exp (-F U)*cred V i := by - have HB := place_valid p M - rw [expU_coeff (fun y => (↑(tune W y):ℂ))] - apply le_trans (profile_envelope (s:=W) ?_ _) - · rw [q_even _ _] - have Heq : mixForm W = U+mixForm s := by unfold W U; rw [mix_cons,mix_cons]; abel - rw [Heq] - exact q_compare (add_nonneg hA HB) (le_add_of_nonneg_right (mix_nonneg hS)) HH _ - intro z hz - simp only [W,List.mem_cons] at hz - rcases hz with hh|hh|hh <;> - first | exact hS z hh | (subst z; first | exact HB | exact hA) - have hN := weighted_envelope (sLen j) (snap_le _ _) V u.V f hf (tune W) _ hi - let T := sLen j - calc - _ ≤ exp 32 * expU (fun y => tune W y * ‖u.app (y+d) z'‖^(2*T)) := by - rw [expU_ex,expU_ex,← Ex_lmul]; apply Ex_mono; intro x - rw [← mul_assoc] - exact mul_le_mul_of_nonneg_right (he x) (by positivity) - _ = exp 32 * expU (fun y => tune W y * ‖ply (snap_le _ _) u.V f y‖^(2*T)) := by - simp only [shift_app]; rfl - _ ≤ exp 32*(exp (-F U)*HeatM (snap_le _ _) V u.V T) := - mul_le_mul_of_nonneg_left hN (exp_nonneg _) - _ ≤ _ := by - rw [← heat_exp,← exp_add,← exp_add]; apply exp_monotone - have H : u.credit/4 ≤ levelCost (snap_le _ _) V u.V T := HG.evidence HR - unfold U B; linarith - -private lemma consistent_length (u : Update j M p) (z : Grid M) (A : Form M) : - (uniform (Grid M)).prob (u.consistent A z) ≤ exp (4*cushion j-F A) := by - unfold consistent - rw [prob_translate (fun w => form A w ≤ 4*cushion j)] - unfold prob - let f := 4*cushion j - have hi : (uniform _).Ex (fun w => exp (-f)*(if form A w ≤ 4*cushion j then 1 else 0)) ≤ - q A 0 := by - rw [q_origin,expU_ex]; refine Ex_mono _ fun i => ?_ - split_ifs with H - · rw [mul_one]; exact exp_monotone (neg_le_neg H) - rw [mul_zero]; exact (boltz_pos _ _).le - rw [Ex_lmul,← F_origin] at hi - - refine le_of_mul_le_mul_left ?_ (exp_pos (-f)) - rw [← exp_add] - rw [show -f+(4*cushion j-F A)=-F A by unfold f; ring] - simpa only using hi - -theorem one_clone_tail {A : Form M} {p : Node} (hp : p.ok M) (hA : 0 ≤ A) - {P : Law (Grid M)} {C : ℝ} (u : Update j M p) (HG : u.Good C A P) (HR : u.regular) - (s : Profile M) (hS : ∀ i∈s, 0 ≤ i.1) - (HH : A+place p M + mixForm s ≤ tilde (H j) A p) (z : Grid M) : - (beta u.ix/4)^(2*sLen j) * expU (fun y => prefixE s p y * singleQ u A z y) ≤ - exp (32 - F (A+place p M)-u.credit/4+4*cushion j) := by - let f := prefixE s p - let L := label (u.next A) - let V := uniform (Grid M) - let b := beta u.ix/4 - let N := 2*sLen j - let M0 := fun y d => u.single A z (y+d) - let S := fun z' : Grid M => if u.consistent A z z' then (1:ℝ) else 0 - have he (y : Grid M) : b^N * (f y*singleQ u A z y) ≤ exp (F A) * - L.Ex (fun d => V.Ex fun z' => S z' * (f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N)) := by - unfold singleQ; rw [ch_ex] - have hc (y z : Grid M) : channel A y z = exp (F A) * (V z * boltz A (z-y)) := by - rw [ch_K,K_eq,boltz,show V z = side M from uniform_side z, - sub_eq_add_neg,exp_add] - ring - rw [← mul_assoc] - change (b^N*f y)*L.Ex _ ≤ _ - simp_rw [← Ex_lmul L] - refine Ex_mono _ fun d => ?_ - unfold prob Ex - rw [Finset.mul_sum,Finset.mul_sum] - refine Finset.sum_le_sum fun z' _ => ?_ - have hp := prefixE_nonneg s p y - change 0 ≤ f y at hp - dsimp - split_ifs with H - · unfold S; obtain ⟨h1,h2⟩ := H - simp only [ite_eq_left h1,mul_one,one_mul] - have hf : b^N ≤ ‖u.app (y+d) z'‖^N := - pow_le_pow_left₀ (by have := beta_pos u.ix; positivity) h2 _ - convert mul_le_mul_of_nonneg_left hf - (mul_nonneg hp ((channel A y).nonneg z')) using 1 <;> - (simp only [hc]; ring) - rw [mul_zero,mul_zero] - unfold S - split_ifs <;> (have := V.nonneg z'; have := boltz_pos A (z'-y); positivity) - let E := 32-F (A+place p M)-u.credit/4 - have hm (d z' : Grid M) : - V.Ex (fun y => f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N) ≤ exp E := by - unfold V; rw [←expU_ex] - exact one_moment hp hA u HG HR s hS HH z' d - rw [expU_ex]; change b^N * V.Ex (fun y => f y * u.singleQ A z y) ≤ _ - rw [← Ex_lmul] - apply le_trans (V.Ex_mono he) - rw [Ex_lmul,Ex_swap V L] - have hiD (d : Grid M) : - V.Ex (fun y => V.Ex fun z' => S z' * (f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N)) - ≤ exp (4*cushion j-F A)*exp E := by - rw [Ex_swap V V]; simp_rw [Ex_lmul] - apply le_trans (b:=V.Ex fun z' => S z'*exp E) - · exact V.Ex_mono fun z' => mul_le_mul_of_nonneg_left (hm d z') (by unfold S; split_ifs <;> norm_num) - rw [Ex_mul] - have hC : V.Ex S = V.prob _ := rfl - exact mul_le_mul_of_nonneg_right (hC.le.trans (consistent_length u z A)) (exp_nonneg E) - calc - _ ≤ exp (F A)*L.Ex (fun _ => exp (4*cushion j-F A)*exp E) := - mul_le_mul_of_nonneg_left (L.Ex_mono hiD) (exp_nonneg _) - _ = _ := by rw [Ex_const,←exp_add,←exp_add]; unfold E; congr 1; ring - -end Update -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Posterior + +/-! # Probability cost of clone evidence under the comparison model -/ +open Set Function Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +variable {M j : ℕ} +open scoped Classical + +def cushion (j : ℕ) := height j/H j^100 +def clones (j : ℕ) := slow j ^120 + +abbrev Clutch (j M : ℕ) := Grid M × (Fin (clones j) → Grid M) + +def fresh (A B : Form M) (j : ℕ) (y : Grid M) : Law (Clutch j M) := + (channel B y).tensorP (pack (fun _ => channel A y)) + +namespace Update +variable {p : Node} + +def consistent (_u : Update j M p) (A : Form M) (z z' : Grid M) : Prop := + form A (z'-z) ≤ 4*cushion j + +def single (u : Update j M p) (A : Form M) (z : Grid M) (v z' : Grid M) : Prop := + u.consistent A z z' ∧ beta u.ix /4 ≤ ‖u.app v z'‖ + +def alarm (u : Update j M p) (A : Form M) (z : Grid M) (x : Clutch j M) : Prop := + ∃ i, u.single A z x.1 (x.2 i) + + +def alarmQ (u : Update j M p) (A : Form M) (z y : Grid M) := + (fresh A (u.next A) j y).prob (u.alarm A z) + +def singleQ (u : Update j M p) (A : Form M) (z y : Grid M) := + (channel (u.next A) y).Ex fun v => (channel A y).prob (u.single A z v) + +lemma alarm_le (u : Update j M p) (A : Form M) (z y : Grid M) : + alarmQ u A z y ≤ clones j * singleQ u A z y := by + classical + let T := clones j + let L := fresh A (u.next A) j y + let F (i : Fin T) (w : Clutch j M) : ℝ := if u.single A z w.1 (w.2 i) then 1 else 0 + have hi (i : Fin T) : L.Ex (F i) = u.singleQ A z y := by + unfold L fresh F singleQ prob; rw [Ex_tensorP] + refine congrArg _ (funext fun v => ?_) + let f (z' : Grid M) := if u.single A z v z' then (1:ℝ) else 0 + exact Ex_pack_proj (fun _ => channel A y) i f + unfold alarmQ prob; apply le_trans (b:=L.Ex (fun w => ∑ i, F i w)) + · apply Ex_mono + intro w + split_ifs with H + · obtain ⟨i,hi⟩ := H + have HE := Finset.single_le_sum (f:=fun i => F i w) (fun i _ => by + unfold F; split_ifs <;> norm_num) (Finset.mem_univ i) + rwa [show F i w=1 from ite_eq_left hi] at HE + exact Finset.sum_nonneg fun i _ => by unfold F; split_ifs <;> norm_num + rw [Ex_finset_sum] + simp_rw [hi, Finset.sum_const, Finset.card_univ, Fintype.card_fin,nsmul_eq_mul] + exact le_rfl + +lemma shift_app (u : Update j M p) (x y z : Grid M) : + u.app (x+y) z = ply (snap_le (M:=M) j p) u.V + (fun i => u.v i z * chi (raise (snap_le (M:=M) j p) i) y) x := by + unfold app ply + refine congrArg _ ?_; funext i + rw [chi_add]; ring + +private lemma one_moment {A : Form M} {p : Node} (hp : p.ok M) (hA : 0 ≤ A) + {P : Law (Grid M)} {C : ℝ} (u : Update j M p) (HG : u.Good C A P) (HR : u.regular) + (s : Profile M) (hS : ∀ i∈s, 0 ≤ i.1) + (HH : A+place p M + mixForm s ≤ tilde (H j) A p) + (z' d : Grid M) : + expU (fun y => prefixE s p y * boltz A (z'-y) * ‖u.app (y+d) z'‖^(2*sLen j)) ≤ + exp (32 -F (A+place p M)-u.credit/4) := by + let t := p.start M + let B := place p M; let U := A+B; let V := tilde (H j) A p + let W : Profile M := (B,t)::(A,z')::s + have he (y : Grid M) : prefixE s p y * boltz A (z'-y) ≤ exp 32 * tune W y := by + unfold prefixE; split_ifs with hi + · have H : form B (t-y) ≤ 32 := place_internal (start_mem hp.1 hp.2) hi + have HH : 1 ≤ exp 32 * boltz B (t-y) := by unfold boltz; rw [← exp_add]; apply one_le_exp; linarith + unfold W; rw [tune_cons,tune_cons] + have hx : 0 ≤ tune s y * boltz A (z'-y) := mul_nonneg (tune_pos _ _).le (boltz_pos _ _).le + apply le_trans (le_mul_of_one_le_left hx HH) + exact le_of_eq (by ring) + · rw [zero_mul] + exact mul_nonneg (exp_nonneg _) (tune_pos _ _).le + let f (i) := u.v i z' * chi (raise (snap_le (M:=M) j p) i) d + have hf (i) : ‖f i‖ ≤ 1 := by + unfold f; rw [norm_mul,norm_chi,mul_one]; exact HG.unit _ _ + have hi (i : Grid M) : ‖expU (fun y => (↑(tune W y):ℂ)*chi i y)‖ ≤ exp (-F U)*cred V i := by + have HB := place_valid p M + rw [expU_coeff (fun y => (↑(tune W y):ℂ))] + apply le_trans (profile_envelope (s:=W) ?_ _) + · rw [q_even _ _] + have Heq : mixForm W = U+mixForm s := by unfold W U; rw [mix_cons,mix_cons]; abel + rw [Heq] + exact q_compare (add_nonneg hA HB) (le_add_of_nonneg_right (mix_nonneg hS)) HH _ + intro z hz + simp only [W,List.mem_cons] at hz + rcases hz with hh|hh|hh <;> + first | exact hS z hh | (subst z; first | exact HB | exact hA) + have hN := weighted_envelope (sLen j) (snap_le _ _) V u.V f hf (tune W) _ hi + let T := sLen j + calc + _ ≤ exp 32 * expU (fun y => tune W y * ‖u.app (y+d) z'‖^(2*T)) := by + rw [expU_ex,expU_ex,← Ex_lmul]; apply Ex_mono; intro x + rw [← mul_assoc] + exact mul_le_mul_of_nonneg_right (he x) (by positivity) + _ = exp 32 * expU (fun y => tune W y * ‖ply (snap_le _ _) u.V f y‖^(2*T)) := by + simp only [shift_app]; rfl + _ ≤ exp 32*(exp (-F U)*HeatM (snap_le _ _) V u.V T) := + mul_le_mul_of_nonneg_left hN (exp_nonneg _) + _ ≤ _ := by + rw [← heat_exp,← exp_add,← exp_add]; apply exp_monotone + have H : u.credit/4 ≤ levelCost (snap_le _ _) V u.V T := HG.evidence HR + unfold U B; linarith + +private lemma consistent_length (u : Update j M p) (z : Grid M) (A : Form M) : + (uniform (Grid M)).prob (u.consistent A z) ≤ exp (4*cushion j-F A) := by + unfold consistent + rw [prob_translate (fun w => form A w ≤ 4*cushion j)] + unfold prob + let f := 4*cushion j + have hi : (uniform _).Ex (fun w => exp (-f)*(if form A w ≤ 4*cushion j then 1 else 0)) ≤ + q A 0 := by + rw [q_origin,expU_ex]; refine Ex_mono _ fun i => ?_ + split_ifs with H + · rw [mul_one]; exact exp_monotone (neg_le_neg H) + rw [mul_zero]; exact (boltz_pos _ _).le + rw [Ex_lmul,← F_origin] at hi + + refine le_of_mul_le_mul_left ?_ (exp_pos (-f)) + rw [← exp_add] + rw [show -f+(4*cushion j-F A)=-F A by unfold f; ring] + simpa only using hi + +theorem one_clone_tail {A : Form M} {p : Node} (hp : p.ok M) (hA : 0 ≤ A) + {P : Law (Grid M)} {C : ℝ} (u : Update j M p) (HG : u.Good C A P) (HR : u.regular) + (s : Profile M) (hS : ∀ i∈s, 0 ≤ i.1) + (HH : A+place p M + mixForm s ≤ tilde (H j) A p) (z : Grid M) : + (beta u.ix/4)^(2*sLen j) * expU (fun y => prefixE s p y * singleQ u A z y) ≤ + exp (32 - F (A+place p M)-u.credit/4+4*cushion j) := by + let f := prefixE s p + let L := label (u.next A) + let V := uniform (Grid M) + let b := beta u.ix/4 + let N := 2*sLen j + let M0 := fun y d => u.single A z (y+d) + let S := fun z' : Grid M => if u.consistent A z z' then (1:ℝ) else 0 + have he (y : Grid M) : b^N * (f y*singleQ u A z y) ≤ exp (F A) * + L.Ex (fun d => V.Ex fun z' => S z' * (f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N)) := by + unfold singleQ; rw [ch_ex] + have hc (y z : Grid M) : channel A y z = exp (F A) * (V z * boltz A (z-y)) := by + rw [ch_K,K_eq,boltz,show V z = side M from uniform_side z, + sub_eq_add_neg,exp_add] + ring + rw [← mul_assoc] + change (b^N*f y)*L.Ex _ ≤ _ + simp_rw [← Ex_lmul L] + refine Ex_mono _ fun d => ?_ + unfold prob Ex + rw [Finset.mul_sum,Finset.mul_sum] + refine Finset.sum_le_sum fun z' _ => ?_ + have hp := prefixE_nonneg s p y + change 0 ≤ f y at hp + dsimp + split_ifs with H + · unfold S; obtain ⟨h1,h2⟩ := H + simp only [ite_eq_left h1,mul_one,one_mul] + have hf : b^N ≤ ‖u.app (y+d) z'‖^N := + pow_le_pow_left₀ (by have := beta_pos u.ix; positivity) h2 _ + convert mul_le_mul_of_nonneg_left hf + (mul_nonneg hp ((channel A y).nonneg z')) using 1 <;> + (simp only [hc]; ring) + rw [mul_zero,mul_zero] + unfold S + split_ifs <;> (have := V.nonneg z'; have := boltz_pos A (z'-y); positivity) + let E := 32-F (A+place p M)-u.credit/4 + have hm (d z' : Grid M) : + V.Ex (fun y => f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N) ≤ exp E := by + unfold V; rw [←expU_ex] + exact one_moment hp hA u HG HR s hS HH z' d + rw [expU_ex]; change b^N * V.Ex (fun y => f y * u.singleQ A z y) ≤ _ + rw [← Ex_lmul] + apply le_trans (V.Ex_mono he) + rw [Ex_lmul,Ex_swap V L] + have hiD (d : Grid M) : + V.Ex (fun y => V.Ex fun z' => S z' * (f y * boltz A (z'-y) * ‖u.app (y+d) z'‖^N)) + ≤ exp (4*cushion j-F A)*exp E := by + rw [Ex_swap V V]; simp_rw [Ex_lmul] + apply le_trans (b:=V.Ex fun z' => S z'*exp E) + · exact V.Ex_mono fun z' => mul_le_mul_of_nonneg_left (hm d z') (by unfold S; split_ifs <;> norm_num) + rw [Ex_mul] + have hC : V.Ex S = V.prob _ := rfl + exact mul_le_mul_of_nonneg_right (hC.le.trans (consistent_length u z A)) (exp_nonneg E) + calc + _ ≤ exp (F A)*L.Ex (fun _ => exp (4*cushion j-F A)*exp E) := + mul_le_mul_of_nonneg_left (L.Ex_mono hiD) (exp_nonneg _) + _ = _ := by rw [Ex_const,←exp_add,←exp_add]; unfold E; congr 1; ring + +end Update +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Bias.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Bias.lean index e1f96ace2f3e77dc7efd66d9f042d2cf015fdc95..cc9e43cc52b973d6f89ddbc9e956fbb66123e7c5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Bias.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Bias.lean @@ -1,177 +1,177 @@ -import OAI.Analysis.FourierLLogL.Thermal.Piece -import OAI.Analysis.FourierLLogL.Thermal.GridFreq - -/-! # Extracting the biased spectrum -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Period Real Compress.Params - -variable {M l : ℕ} -open scoped Classical - -def ball (A : Form M) (r : ℝ) : Finset (Grid M) := Finset.univ.filter fun i => delta A i ≤ r - -lemma mem_ball (A : Form M) (r : ℝ) (x : Grid M) : x ∈ ball A r ↔ delta A x ≤ r := by simp [ball] - -def fnear (A : Form M) (r R : ℝ) (x : Grid M) := - ∃ n, delta A n ≤ r ∧ rad (x-n) ≤ R - -def separated {B : Type*} [DecidableEq B] (tag : B → Grid M) (A : Form M) (r R : ℝ) - (s : Finset B) : Prop := - ∀ x∈s, ∀ y∈s, x≠y → ¬ fnear A r R (tag x-tag y) - -def request (A : Form M) (j : ℕ) (p : Node) (a : Grid M → ℝ) (α b k : ℝ) := - ∃ s : Finset (Grid M), exp k ≤ (s.card:ℝ) ∧ - (∀ x∈s, high p a α A b x) ∧ separated id A (sepRad j) (sig j*p.invlen) s - -/-- Bias tagged to subgroup characters χ_gx, side coordinate and the fixed child. -/ -def biases (P : Law (Grid M)) (p : Node) (A : Form M) (hl : l ≤ M) - (s : Finset (Grid l)) (B : Bool) (v : Grid l → Grid M → ℂ) (β : ℝ) := - (∀ i z, ‖v i z‖ ≤ 1) ∧ ∀ i∈s, β ≤ P.Ex (fun x => - if x ∈ (child p B).block M then - (channel A x).Ex (fun z => (v i z * chi (raise hl i) x).re) - else 0) - -private lemma nearby_bias {p : Node} (hp : ok p M) - {a : Grid M → ℝ} {P : Law (Grid M)} (hP : PieceLaw p a P) - (A : Form M) (u i : Grid M) (B : Bool) {v : Grid M → ℂ} (hv : ∀ z, ‖v z‖ ≤ 1) - (E : ℝ) (hE : rad (u-i) ≤ E * p.invlen) : - let test (s : Grid M) (v : Grid M → ℂ) := P.Ex (fun x => - if x ∈ (child p B).block M then - (channel A x).Ex (fun z => (v z * chi s x).re) else 0) - ∃ w : Grid M → ℂ, (∀ z, ‖w z‖ ≤ 1) ∧ test u v -8*E ≤ test i w := by - intro test - let x' := p.start M - let w (z : Grid M) := v z * chi (u-i) x' - have hp0 : 0 ≤ E := nonneg_of_mul_nonneg_left ((rad_nonneg _).trans hE) p.invlen_pos - have hn (z : Grid M) : ‖w z‖ ≤ 1 := by simp_rw [w,norm_mul,norm_chi,mul_one]; exact hv z - have he (x : Grid M) (hx : 0 < P x) : - (channel A x).Ex (fun z => (v z * chi u x).re) -8*E ≤ - (channel A x).Ex (fun z => (w z * chi i x).re) := by - calc - _ = (channel A x).Ex (fun z => (v z * chi u x).re-8*E) := by rw [Ex_sub,Ex_const] - _ ≤ _ := Ex_mono _ fun z => by - have hh := phase_distance (u-i) x x' - have heq : ‖chi (u-i) x-chi (u-i) x'‖ ≤ 8*E := by - apply le_trans hh - calc - _ ≤ E * p.invlen * (8 * p.len) := by - have H := p.invlen_pos - gcongr - exact diam_block (piece_support hp hP _ hx) (start_mem hp.1 hp.2) - _ = _ := by rw [rinv]; field_simp [p.len_pos.ne'] - have Hg : (v z*chi u x).re-(w z*chi i x).re ≤ 8*E := - calc - _ = (v z*chi i x * (chi (u-i) x-chi (u-i) x')).re := by - unfold w - rw [show v z*chi i x * (chi (u-i) x-chi (u-i) x') = - v z*(chi (u-i) x * chi i x)-v z*chi (u-i) x'*chi i x by ring, - ← chi_add',sub_add_cancel,Complex.sub_re] - _ ≤ _ := by - apply le_trans (le_abs_self _) - apply le_trans (Complex.abs_re_le_norm _) - rw [norm_mul,norm_mul,norm_chi,mul_one] - exact (mul_le_of_le_one_left (by positivity) (hv z)).trans heq - linarith - refine ⟨w,hn,?_⟩ - unfold test - rw [← Ex_const P (8*E),← Ex_sub] - refine P.Ex_mono_mem fun x hx => ?_ - split_ifs - · exact he x hx - linarith - -lemma extract_spectrum (j : ℕ) {A : Form M} {p : Node} {a : Grid M → ℝ} {α b k : ℝ} - (hp : p.valid) (hv : p.lev < M) - (ha : 0 ≤ a) (hα : 0 < α) {P : Law (Grid M)} (hP : PieceLaw p a P) - (hm : 0 < p.avg a) (hm' : p.avg a ≤ 2*α) - (he : 8*sig j ≤ b^2) (hr : request A j p a α b k) : - let l := snapGrid j p M - ∃ (s : Finset (Grid l)) (B : Bool) (v : Grid l → Grid M → ℂ), - s.Nonempty ∧ k-log 2 ≤ log (s.card:ℝ) ∧ - separated (raise (snap_le _ _)) A (sepRad j) (sig j*p.invlen/2) s ∧ - biases P p A (snap_le _ _) s B v (b^2/100) := by - classical - intro l - have hl := snap_le (M:=M) j p - let g := down hl - let d := raise hl - let R := sig j*p.invlen - let D := sepRad j - obtain ⟨s,hs,hb,hr⟩ := hr - - let t := s.filter (fun x => b^2 ≤ energyZ p.L a α A x) - have hh : ∃ B, ∃ t' ⊆ s, s.card ≤ 2*t'.card ∧ - ∀ x∈t', b^2 ≤ energyZ (p.child B) a α A x := by - by_cases H : s.card ≤ 2*t.card - · exact ⟨false,t,Finset.filter_subset _ _,H,fun i hi => (Finset.mem_filter.mp hi).2⟩ - · refine ⟨true,s \ t, Finset.sdiff_subset, ?_,?_⟩ - · have hh : t ⊆ s := Finset.filter_subset _ _ - rw [Finset.card_sdiff_of_subset hh] - have hi := Finset.card_le_card hh; omega - intro x hx - obtain ⟨hx,hx'⟩ := Finset.mem_sdiff.mp hx - obtain ⟨B,hu⟩ := hb x hx - cases B - · exact (hx' (Finset.mem_filter.mpr ⟨hx,hu⟩)).elim - · exact hu - obtain ⟨B,t,ht,h1,h2⟩ := hh - let U := t.image g - have hp' : ok p M := ⟨hp,hv.le⟩ - have hX (x : Grid M) : rad (x-d (g x)) ≤ R/20 := (step_scale j hp'.2).2.2.2 x - have hY (x) (hx : x∈s) (y) (hy : y∈s) (hne : x≠y) : - ¬ fnear A D (R/2) (d (g x)-d (g y)) := by - intro hn - obtain ⟨i,hi,h⟩ := hn - apply hr _ hx _ hy hne - refine ⟨i,hi,?_⟩ - simp only [id_eq] - rw [show x-y-i = (d (g x)-d (g y)-i)+(x-d (g x))+ -(y-d (g y)) by ring] - apply le_trans (rad_add _ _) - have hh := rad_add (d (g x)-d (g y)-i) (x-d (g x)) - rw [rad_even] - linarith [hX x,hX y,rad_nonneg (x-d (g x))] - have hi : Set.InjOn g s := by - intro x hx y hy H - by_contra he - apply hY x hx y hy he - refine ⟨0,?_,?_⟩ - · rw [delta_zero]; unfold D sepRad; positivity - rw [H,sub_self,sub_self] - have hh : 0 ≤ R := by have hhi := hX x; linarith [rad_nonneg (x-d (g x))] - simpa [rad] using div_nonneg hh (by norm_num : (0:ℝ)≤2) - have Hc : exp k ≤ 2*U.card := by - rw [show U.card=t.card from Finset.card_image_of_injOn (hi.mono ht)] - exact hs.trans (by exact_mod_cast h1) - have Hc' : (0:ℝ) < U.card := by linarith [exp_pos k] - have Hcp : 0 < U.card := by exact_mod_cast Hc' - have hvv (i : Grid l) : - ∃ v : Grid M → ℂ, (∀ z, ‖v z‖ ≤ 1) ∧ (i∈U → - b^2/100 ≤ P.Ex fun x => if x∈(p.child B).block M then - (channel A x).Ex fun z => (v z*chi (d i) x).re else 0) := by - by_cases hh : i∈U - · obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp hh - obtain ⟨v,hv1,hv2⟩ := piece_child_bias hp hv ha hα hP hm hm' A x B (h2 x hx) - obtain ⟨w,hw1,hw2⟩ := nearby_bias hp' hP A x (d (g x)) B hv1 (sig j/20) - ((hX x).trans (by unfold R; rw [div_mul_eq_mul_div])) - refine ⟨w,hw1,fun _ => ?_⟩ - dsimp only at hw2 - nlinarith - exact ⟨fun _ => 0,fun _ => by simp, fun h => (hh h).elim⟩ - choose v hv1 hv2 using hvv - refine ⟨U,B,v,Finset.card_pos.mp Hcp,?_,?_,hv1,hv2⟩ - · have hh := log_le_log (exp_pos k) Hc - rw [log_exp,Real.log_mul (by norm_num) Hc'.ne'] at hh - linarith - intro i hi j hj he - obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp hi - obtain ⟨y,hy,rfl⟩ := Finset.mem_image.mp hj - exact hY x (ht hx) y (ht hy) (fun h => he (congrArg g h)) - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Piece +import OAI.Analysis.FourierLLogL.Thermal.GridFreq + +/-! # Extracting the biased spectrum -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Period Real Compress.Params + +variable {M l : ℕ} +open scoped Classical + +def ball (A : Form M) (r : ℝ) : Finset (Grid M) := Finset.univ.filter fun i => delta A i ≤ r + +lemma mem_ball (A : Form M) (r : ℝ) (x : Grid M) : x ∈ ball A r ↔ delta A x ≤ r := by simp [ball] + +def fnear (A : Form M) (r R : ℝ) (x : Grid M) := + ∃ n, delta A n ≤ r ∧ rad (x-n) ≤ R + +def separated {B : Type*} [DecidableEq B] (tag : B → Grid M) (A : Form M) (r R : ℝ) + (s : Finset B) : Prop := + ∀ x∈s, ∀ y∈s, x≠y → ¬ fnear A r R (tag x-tag y) + +def request (A : Form M) (j : ℕ) (p : Node) (a : Grid M → ℝ) (α b k : ℝ) := + ∃ s : Finset (Grid M), exp k ≤ (s.card:ℝ) ∧ + (∀ x∈s, high p a α A b x) ∧ separated id A (sepRad j) (sig j*p.invlen) s + +/-- Bias tagged to subgroup characters χ_gx, side coordinate and the fixed child. -/ +def biases (P : Law (Grid M)) (p : Node) (A : Form M) (hl : l ≤ M) + (s : Finset (Grid l)) (B : Bool) (v : Grid l → Grid M → ℂ) (β : ℝ) := + (∀ i z, ‖v i z‖ ≤ 1) ∧ ∀ i∈s, β ≤ P.Ex (fun x => + if x ∈ (child p B).block M then + (channel A x).Ex (fun z => (v i z * chi (raise hl i) x).re) + else 0) + +private lemma nearby_bias {p : Node} (hp : ok p M) + {a : Grid M → ℝ} {P : Law (Grid M)} (hP : PieceLaw p a P) + (A : Form M) (u i : Grid M) (B : Bool) {v : Grid M → ℂ} (hv : ∀ z, ‖v z‖ ≤ 1) + (E : ℝ) (hE : rad (u-i) ≤ E * p.invlen) : + let test (s : Grid M) (v : Grid M → ℂ) := P.Ex (fun x => + if x ∈ (child p B).block M then + (channel A x).Ex (fun z => (v z * chi s x).re) else 0) + ∃ w : Grid M → ℂ, (∀ z, ‖w z‖ ≤ 1) ∧ test u v -8*E ≤ test i w := by + intro test + let x' := p.start M + let w (z : Grid M) := v z * chi (u-i) x' + have hp0 : 0 ≤ E := nonneg_of_mul_nonneg_left ((rad_nonneg _).trans hE) p.invlen_pos + have hn (z : Grid M) : ‖w z‖ ≤ 1 := by simp_rw [w,norm_mul,norm_chi,mul_one]; exact hv z + have he (x : Grid M) (hx : 0 < P x) : + (channel A x).Ex (fun z => (v z * chi u x).re) -8*E ≤ + (channel A x).Ex (fun z => (w z * chi i x).re) := by + calc + _ = (channel A x).Ex (fun z => (v z * chi u x).re-8*E) := by rw [Ex_sub,Ex_const] + _ ≤ _ := Ex_mono _ fun z => by + have hh := phase_distance (u-i) x x' + have heq : ‖chi (u-i) x-chi (u-i) x'‖ ≤ 8*E := by + apply le_trans hh + calc + _ ≤ E * p.invlen * (8 * p.len) := by + have H := p.invlen_pos + gcongr + exact diam_block (piece_support hp hP _ hx) (start_mem hp.1 hp.2) + _ = _ := by rw [rinv]; field_simp [p.len_pos.ne'] + have Hg : (v z*chi u x).re-(w z*chi i x).re ≤ 8*E := + calc + _ = (v z*chi i x * (chi (u-i) x-chi (u-i) x')).re := by + unfold w + rw [show v z*chi i x * (chi (u-i) x-chi (u-i) x') = + v z*(chi (u-i) x * chi i x)-v z*chi (u-i) x'*chi i x by ring, + ← chi_add',sub_add_cancel,Complex.sub_re] + _ ≤ _ := by + apply le_trans (le_abs_self _) + apply le_trans (Complex.abs_re_le_norm _) + rw [norm_mul,norm_mul,norm_chi,mul_one] + exact (mul_le_of_le_one_left (by positivity) (hv z)).trans heq + linarith + refine ⟨w,hn,?_⟩ + unfold test + rw [← Ex_const P (8*E),← Ex_sub] + refine P.Ex_mono_mem fun x hx => ?_ + split_ifs + · exact he x hx + linarith + +lemma extract_spectrum (j : ℕ) {A : Form M} {p : Node} {a : Grid M → ℝ} {α b k : ℝ} + (hp : p.valid) (hv : p.lev < M) + (ha : 0 ≤ a) (hα : 0 < α) {P : Law (Grid M)} (hP : PieceLaw p a P) + (hm : 0 < p.avg a) (hm' : p.avg a ≤ 2*α) + (he : 8*sig j ≤ b^2) (hr : request A j p a α b k) : + let l := snapGrid j p M + ∃ (s : Finset (Grid l)) (B : Bool) (v : Grid l → Grid M → ℂ), + s.Nonempty ∧ k-log 2 ≤ log (s.card:ℝ) ∧ + separated (raise (snap_le _ _)) A (sepRad j) (sig j*p.invlen/2) s ∧ + biases P p A (snap_le _ _) s B v (b^2/100) := by + classical + intro l + have hl := snap_le (M:=M) j p + let g := down hl + let d := raise hl + let R := sig j*p.invlen + let D := sepRad j + obtain ⟨s,hs,hb,hr⟩ := hr + + let t := s.filter (fun x => b^2 ≤ energyZ p.L a α A x) + have hh : ∃ B, ∃ t' ⊆ s, s.card ≤ 2*t'.card ∧ + ∀ x∈t', b^2 ≤ energyZ (p.child B) a α A x := by + by_cases H : s.card ≤ 2*t.card + · exact ⟨false,t,Finset.filter_subset _ _,H,fun i hi => (Finset.mem_filter.mp hi).2⟩ + · refine ⟨true,s \ t, Finset.sdiff_subset, ?_,?_⟩ + · have hh : t ⊆ s := Finset.filter_subset _ _ + rw [Finset.card_sdiff_of_subset hh] + have hi := Finset.card_le_card hh; omega + intro x hx + obtain ⟨hx,hx'⟩ := Finset.mem_sdiff.mp hx + obtain ⟨B,hu⟩ := hb x hx + cases B + · exact (hx' (Finset.mem_filter.mpr ⟨hx,hu⟩)).elim + · exact hu + obtain ⟨B,t,ht,h1,h2⟩ := hh + let U := t.image g + have hp' : ok p M := ⟨hp,hv.le⟩ + have hX (x : Grid M) : rad (x-d (g x)) ≤ R/20 := (step_scale j hp'.2).2.2.2 x + have hY (x) (hx : x∈s) (y) (hy : y∈s) (hne : x≠y) : + ¬ fnear A D (R/2) (d (g x)-d (g y)) := by + intro hn + obtain ⟨i,hi,h⟩ := hn + apply hr _ hx _ hy hne + refine ⟨i,hi,?_⟩ + simp only [id_eq] + rw [show x-y-i = (d (g x)-d (g y)-i)+(x-d (g x))+ -(y-d (g y)) by ring] + apply le_trans (rad_add _ _) + have hh := rad_add (d (g x)-d (g y)-i) (x-d (g x)) + rw [rad_even] + linarith [hX x,hX y,rad_nonneg (x-d (g x))] + have hi : Set.InjOn g s := by + intro x hx y hy H + by_contra he + apply hY x hx y hy he + refine ⟨0,?_,?_⟩ + · rw [delta_zero]; unfold D sepRad; positivity + rw [H,sub_self,sub_self] + have hh : 0 ≤ R := by have hhi := hX x; linarith [rad_nonneg (x-d (g x))] + simpa [rad] using div_nonneg hh (by norm_num : (0:ℝ)≤2) + have Hc : exp k ≤ 2*U.card := by + rw [show U.card=t.card from Finset.card_image_of_injOn (hi.mono ht)] + exact hs.trans (by exact_mod_cast h1) + have Hc' : (0:ℝ) < U.card := by linarith [exp_pos k] + have Hcp : 0 < U.card := by exact_mod_cast Hc' + have hvv (i : Grid l) : + ∃ v : Grid M → ℂ, (∀ z, ‖v z‖ ≤ 1) ∧ (i∈U → + b^2/100 ≤ P.Ex fun x => if x∈(p.child B).block M then + (channel A x).Ex fun z => (v z*chi (d i) x).re else 0) := by + by_cases hh : i∈U + · obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp hh + obtain ⟨v,hv1,hv2⟩ := piece_child_bias hp hv ha hα hP hm hm' A x B (h2 x hx) + obtain ⟨w,hw1,hw2⟩ := nearby_bias hp' hP A x (d (g x)) B hv1 (sig j/20) + ((hX x).trans (by unfold R; rw [div_mul_eq_mul_div])) + refine ⟨w,hw1,fun _ => ?_⟩ + dsimp only at hw2 + nlinarith + exact ⟨fun _ => 0,fun _ => by simp, fun h => (hh h).elim⟩ + choose v hv1 hv2 using hvv + refine ⟨U,B,v,Finset.card_pos.mp Hcp,?_,?_,hv1,hv2⟩ + · have hh := log_le_log (exp_pos k) Hc + rw [log_exp,Real.log_mul (by norm_num) Hc'.ne'] at hh + linarith + intro i hi j hj he + obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp hi + obtain ⟨y,hy,rfl⟩ := Finset.mem_image.mp hj + exact hY x (ht hx) y (ht hy) (fun h => he (congrArg g h)) + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Flat.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Flat.lean index ce49cae72b42a52866bbd966a5cd3c0e49897563..0bec925d903516a81b1f534e9c8a55164f852867 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Flat.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Flat.lean @@ -1,114 +1,114 @@ -import OAI.Analysis.FourierLLogL.Thermal.Levels - -/-! # Selecting a flat coefficient -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -/-- Find `λ` after the width estimate. -/ -lemma flat_selection (D : ℝ) (hD : 1 ≤ D) : - ∀ᶠ j : ℕ in atTop, ∀ (M : ℕ) (A B : Form M), 0 ≤ A → 0 ≤ B → - ∀ t : ℝ, t ≤ D*height j → - F (A+long j • B)-F A ≤ 400*t → - ∃ c : ℝ, height j * H j^200 ≤ c ∧ H j^140*c ≤ long j ∧ - flat (H j) (plateErr j) A (c • B) ∧ - F (A+c • B)-F A ≤ 400*t ∧ - (label (A+c • B)).Ex (form B) ≤ 400*t/c := by - filter_upwards [nat_pow_small_slow 402 10000,h_large (1000*D)] with j hj h M A B hA hB t ht hw - let d := height j - let a := H j - have H1 : 1 ≤ a := H_ge j - have hp : 0 < a := H_pos j - have hd : 0 < d := d_pos j - let m := slow j ^ 401 - let curve (i : ℕ) := d*a^200 * (2*a^140)^i - have hm : 0 < m := pow_pos (slow_pos j) _ - have he : curve m ≤ long j := by - have Hm : ell j *200 + (1+ell j *140)*m ≤ wide j := by - apply le_trans _ hj - have h1 := ell_le_h j - have h2 : 1 ≤ slow j := slow_pos j - have h3 : slow j ≤ slow j ^402 := le_self_pow h2 (by decide) - have h4 : 1+ell j * 140 ≤ 141 * slow j := by omega - calc - _ ≤ ell j *200 + (141*slow j)*m := Nat.add_le_add_left (Nat.mul_le_mul_right m h4) _ - _ = ell j *200 + 141*slow j^402 := by unfold m; ring - _ ≤ 10000*slow j^402 := by omega - have HE : a = (2:ℝ)^(ell j) := by unfold a H slow; push_cast; rfl - unfold curve long - calc - _ = d*(2:ℝ)^(ell j*200+(1+ell j*140)*m) := by - rw [HE, pow_add, pow_mul,pow_mul,pow_add,pow_one,pow_mul]; ring - _ ≤ _ := by - change d*_ ≤ d*_ - gcongr; norm_num - have h_step (i : ℕ) : a^140*curve i = 2⁻¹*curve (i+1) := by unfold curve; rw [pow_succ]; ring - have hn (i) : d*a^200 ≤ curve i := by - apply le_mul_of_one_le_right (by positivity) - exact one_le_pow₀ (by have := one_le_pow₀ H1 (n:=140); linarith) - have hu (i : ℕ) (hi : i < m) : a^140*curve i ≤ long j := by - rw [h_step] - refine le_trans ?_ he - have HE : curve (i+1) ≤ curve m := - mul_le_mul_of_nonneg_left (pow_le_pow_right₀ - (by have := one_le_pow₀ H1 (n:=140); linarith) hi) (by positivity) - have HP : 0 ≤ curve m := by unfold curve; positivity - norm_num; linarith - let E (i : ℕ) := F (A+(2⁻¹*curve i) • B) - have Hlow (i : ℕ) : F A ≤ E i := by - apply F_mono (le_add_of_nonneg_right _) - intro x - apply mul_nonneg _ (hB x) - unfold curve; positivity - have Hhi : E m ≤ F A + 400*t := by - apply le_trans (b:=F (A+long j • B)) - · refine F_mono (add_le_add_right ?_ _) - intro x - have hhP : 0 ≤ curve m := by unfold curve; positivity - have hh : 2⁻¹*curve m ≤ long j := by norm_num; linarith - exact mul_le_mul_of_nonneg_right hh (hB x) - linarith - have hi' : ∃ i, i < m ∧ E (i+1)-E i ≤ plateErr j := by - by_contra! Hh - have hi : ∀ l : ℕ, l ≤ m → E 0+l*plateErr j ≤ E l := by - intro l hll; induction l with - | zero => simp - | succ l ih => - have hh := ih (by omega) - have hl := Hh l hll - push_cast; linarith - have HH := hi m le_rfl - have HG : (m:ℝ)*plateErr j = a*d := by - have He : (m:ℝ) = a^401 := by unfold m a H; push_cast; rfl - rw [He] - change a^401 * (d/a^400)=a*d - rw [pow_succ]; field_simp - have HL := Hlow 0 - rw [HG] at HH - change _ ≤ a at h - change _ ≤ D*d at ht - nlinarith - obtain ⟨i,hi,hF⟩ := hi' - have hiP : 0 < curve i := by unfold curve; positivity - have heq := hu _ hi - have HH : F (A+curve i • B)-F A ≤ 400*t := by - refine (sub_le_sub_right (F_mono (add_le_add_right ?_ A)) _).trans hw - intro x - exact mul_le_mul_of_nonneg_right ((le_mul_of_one_le_left hiP.le (one_le_pow₀ H1)).trans heq) (hB x) - refine ⟨curve i, hn i,heq,?_,HH,?_⟩ - · unfold flat - rw [smul_smul,smul_smul] - exact (by rw [show H j ^140*curve i = _ from h_step i]; exact hF) - apply (le_div_iff₀ hiP).mpr - have H := mean_energy A (curve i • B) - rw [funext (fun x => form_smul B (curve i) x), Ex_lmul] at H - rw [mul_comm] - exact H.trans HH - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Levels + +/-! # Selecting a flat coefficient -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +/-- Find `λ` after the width estimate. -/ +lemma flat_selection (D : ℝ) (hD : 1 ≤ D) : + ∀ᶠ j : ℕ in atTop, ∀ (M : ℕ) (A B : Form M), 0 ≤ A → 0 ≤ B → + ∀ t : ℝ, t ≤ D*height j → + F (A+long j • B)-F A ≤ 400*t → + ∃ c : ℝ, height j * H j^200 ≤ c ∧ H j^140*c ≤ long j ∧ + flat (H j) (plateErr j) A (c • B) ∧ + F (A+c • B)-F A ≤ 400*t ∧ + (label (A+c • B)).Ex (form B) ≤ 400*t/c := by + filter_upwards [nat_pow_small_slow 402 10000,h_large (1000*D)] with j hj h M A B hA hB t ht hw + let d := height j + let a := H j + have H1 : 1 ≤ a := H_ge j + have hp : 0 < a := H_pos j + have hd : 0 < d := d_pos j + let m := slow j ^ 401 + let curve (i : ℕ) := d*a^200 * (2*a^140)^i + have hm : 0 < m := pow_pos (slow_pos j) _ + have he : curve m ≤ long j := by + have Hm : ell j *200 + (1+ell j *140)*m ≤ wide j := by + apply le_trans _ hj + have h1 := ell_le_h j + have h2 : 1 ≤ slow j := slow_pos j + have h3 : slow j ≤ slow j ^402 := le_self_pow h2 (by decide) + have h4 : 1+ell j * 140 ≤ 141 * slow j := by omega + calc + _ ≤ ell j *200 + (141*slow j)*m := Nat.add_le_add_left (Nat.mul_le_mul_right m h4) _ + _ = ell j *200 + 141*slow j^402 := by unfold m; ring + _ ≤ 10000*slow j^402 := by omega + have HE : a = (2:ℝ)^(ell j) := by unfold a H slow; push_cast; rfl + unfold curve long + calc + _ = d*(2:ℝ)^(ell j*200+(1+ell j*140)*m) := by + rw [HE, pow_add, pow_mul,pow_mul,pow_add,pow_one,pow_mul]; ring + _ ≤ _ := by + change d*_ ≤ d*_ + gcongr; norm_num + have h_step (i : ℕ) : a^140*curve i = 2⁻¹*curve (i+1) := by unfold curve; rw [pow_succ]; ring + have hn (i) : d*a^200 ≤ curve i := by + apply le_mul_of_one_le_right (by positivity) + exact one_le_pow₀ (by have := one_le_pow₀ H1 (n:=140); linarith) + have hu (i : ℕ) (hi : i < m) : a^140*curve i ≤ long j := by + rw [h_step] + refine le_trans ?_ he + have HE : curve (i+1) ≤ curve m := + mul_le_mul_of_nonneg_left (pow_le_pow_right₀ + (by have := one_le_pow₀ H1 (n:=140); linarith) hi) (by positivity) + have HP : 0 ≤ curve m := by unfold curve; positivity + norm_num; linarith + let E (i : ℕ) := F (A+(2⁻¹*curve i) • B) + have Hlow (i : ℕ) : F A ≤ E i := by + apply F_mono (le_add_of_nonneg_right _) + intro x + apply mul_nonneg _ (hB x) + unfold curve; positivity + have Hhi : E m ≤ F A + 400*t := by + apply le_trans (b:=F (A+long j • B)) + · refine F_mono (add_le_add_right ?_ _) + intro x + have hhP : 0 ≤ curve m := by unfold curve; positivity + have hh : 2⁻¹*curve m ≤ long j := by norm_num; linarith + exact mul_le_mul_of_nonneg_right hh (hB x) + linarith + have hi' : ∃ i, i < m ∧ E (i+1)-E i ≤ plateErr j := by + by_contra! Hh + have hi : ∀ l : ℕ, l ≤ m → E 0+l*plateErr j ≤ E l := by + intro l hll; induction l with + | zero => simp + | succ l ih => + have hh := ih (by omega) + have hl := Hh l hll + push_cast; linarith + have HH := hi m le_rfl + have HG : (m:ℝ)*plateErr j = a*d := by + have He : (m:ℝ) = a^401 := by unfold m a H; push_cast; rfl + rw [He] + change a^401 * (d/a^400)=a*d + rw [pow_succ]; field_simp + have HL := Hlow 0 + rw [HG] at HH + change _ ≤ a at h + change _ ≤ D*d at ht + nlinarith + obtain ⟨i,hi,hF⟩ := hi' + have hiP : 0 < curve i := by unfold curve; positivity + have heq := hu _ hi + have HH : F (A+curve i • B)-F A ≤ 400*t := by + refine (sub_le_sub_right (F_mono (add_le_add_right ?_ A)) _).trans hw + intro x + exact mul_le_mul_of_nonneg_right ((le_mul_of_one_le_left hiP.le (one_le_pow₀ H1)).trans heq) (hB x) + refine ⟨curve i, hn i,heq,?_,HH,?_⟩ + · unfold flat + rw [smul_smul,smul_smul] + exact (by rw [show H j ^140*curve i = _ from h_step i]; exact hF) + apply (le_div_iff₀ hiP).mpr + have H := mean_energy A (curve i • B) + rw [funext (fun x => form_smul B (curve i) x), Ex_lmul] at H + rw [mul_comm] + exact H.trans HH + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Form.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Form.lean index 0b9ddff99dd1a8e9bd7caefc300649020472c1a6..f33f675cd53091dbb26be4c07465656c717480e1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Form.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Form.lean @@ -1,198 +1,198 @@ -import OAI.Analysis.FourierLLogL.Dyadic.Discrete -/-! # Thermal forms on the grid -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate NNReal -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Real Period - -variable {M : ℕ} - -/-- coefficient form. Throughout, nonnegativity of the coefficients means `0 ≤ A`. -/ -abbrev Form (M : ℕ) := Grid M → ℝ - -def wave (s y : Grid M) : ℝ := (chi s y).re -def cost (s y : Grid M) : ℝ := 1-wave s y -def form (A : Form M) : Grid M → ℝ := fun y => ∑ s, A s * cost s y - -lemma wave_even (s : Grid M) : (wave s).Even := by intro x; simp [wave,chi_neg] -lemma wave_comm (s y : Grid M) : wave s y = wave y s := by unfold wave; rw [chi_comm] -lemma wave_abs (s y : Grid M) : |wave s y| ≤ 1 := - (Complex.abs_re_le_norm (chi s y)).trans (norm_chi _ _).le -lemma cost_even (s : Grid M) : (cost s).Even := by intro x; unfold cost; rw [wave_even _ x] -lemma cost_symm (s y : Grid M) : cost s y = cost y s := by unfold cost; rw [wave_comm] -lemma cost_bounds (s y : Grid M) : 0 ≤ cost s y ∧ cost s y ≤ 2 := by - unfold cost - rcases abs_le.mp (wave_abs s y) with ⟨h1,h2⟩ - constructor <;> linarith - -lemma norm_phase_sq (s x : Grid M) : ‖1-chi s x‖^2 = 2*cost s x := by - unfold cost wave - have H := Complex.sq_norm (chi s x) - rw [norm_chi] at H - rw [Complex.sq_norm] - simp only [Complex.normSq_apply,Complex.sub_re,Complex.sub_im,Complex.one_re, Complex.one_im] at * - nlinarith - -lemma norm_phase_add (s x y : Grid M) : - ‖1-chi s (x+y)‖ ≤ ‖1-chi s x‖+‖1-chi s y‖ := by - rw [chi_add,show 1-chi s x*chi s y = 1-chi s x + chi s x*(1-chi s y) by ring] - refine (norm_add_le _ _).trans ?_ - rw [norm_mul,norm_chi,one_mul] - -lemma cost_add (s x y : Grid M) : cost s (x+y) ≤ 2*(cost s x+cost s y) := by - have H1 := norm_phase_sq s x - have H2 := norm_phase_sq s y - have H3 := norm_phase_sq s (x+y) - have H4 := norm_phase_add s x y - nlinarith [norm_nonneg (1-chi s (x+y)),sq_nonneg (‖1-chi s x‖-‖1-chi s y‖)] - -lemma form_zero (x : Grid M) : form 0 x = 0 := by simp [form] - -lemma form_at_zero (A : Form M) : form A 0 = 0 := by - simp [form, cost, wave, chi_zero_right] - -lemma form_add (A D : Form M) (x : Grid M) : form (A+D) x = form A x + form D x := by - simp [form, add_mul, Finset.sum_add_distrib] - -lemma form_smul (A : Form M) (t : ℝ) (x : Grid M) : form (t • A) x = t*form A x := by - simp [form, mul_assoc, Finset.mul_sum] - -lemma form_sum {ι : Type*} (s : Finset ι) (f : ι → Form M) (x : Grid M) : - form (∑ i ∈ s, f i) x = ∑ i ∈ s, form (f i) x := by - classical - induction s using Finset.induction_on with - | empty => simp [form_zero] - | @insert i s hi ih => rw [Finset.sum_insert hi,Finset.sum_insert hi,form_add,ih] - -lemma form_even (A : Form M) : (form A).Even := by intro x; simp [form, cost_even _ x] - -lemma form_mono {A D : Form M} (h : A ≤ D) : form A ≤ form D := by - intro y - exact Finset.sum_le_sum fun s _ => mul_le_mul_of_nonneg_right (h s) (cost_bounds _ _).1 - -lemma form_nonneg {A : Form M} (h : 0 ≤ A) (y : Grid M) : 0 ≤ form A y := by - simpa only [form_zero] using form_mono h y - -lemma form_triangle {A : Form M} (h : 0 ≤ A) (x y : Grid M) : - form A (x+y) ≤ 2*(form A x+form A y) := by - unfold form - rw [← Finset.sum_add_distrib,Finset.mul_sum] - apply Finset.sum_le_sum; intro i hi - apply le_trans (mul_le_mul_of_nonneg_left (cost_add _ _ _) (h i)) - exact le_of_eq (by ring) - -def boltz (A : Form M) (y : Grid M) : ℝ := exp (-form A y) -def q (A : Form M) (u : Grid M) : ℝ := (fc (fun y => (↑(boltz A y):ℂ)) u).re - -lemma boltz_even (A : Form M) : (boltz A).Even := by intro x; simp [boltz,form_even _ x] -lemma boltz_pos (A : Form M) (y : Grid M) : 0 < boltz A y := exp_pos _ -lemma boltz_sum (A D : Form M) (y : Grid M) : - boltz (A+D) y = boltz A y * boltz D y := by - simp [boltz,form_add, exp_add,mul_comm] - -lemma q_even (A : Form M) : (q A).Even := fun x => congrArg Complex.re <| - fc_neg_even (fun y => (↑(boltz A y):ℂ)) (fun y => by simp [boltz_even _ y]) x - -lemma cq (A : Form M) (x : Grid M) : - (↑(q A x):ℂ) = fc (fun y => (↑(boltz A y):ℂ)) x := real_fc_even _ (boltz_even _) _ - -lemma q_origin (A : Form M) : q A 0 = expU (boltz A) := by - apply Complex.ofReal_inj.mp - rw [cq, ← expU_real,fc] - simp_rw [neg_zero,chi_zero_left,mul_one] - -lemma q_origin_pos (A : Form M) : 0 < q A 0 := by - rw [q_origin]; unfold expU - rw [smul_eq_mul] - exact mul_pos (side_pos _) <| Finset.sum_pos (fun i _ => boltz_pos _ _) Finset.univ_nonempty - -lemma q_total (A : Form M) : ∑ x, q A x = 1 := by - have H := invert_fc (fun y => (↑(boltz A y):ℂ)) 0 - simp_rw [← cq,chi_zero_right,mul_one,← Complex.ofReal_sum] at H - simpa only [boltz,form_at_zero,neg_zero,Real.exp_zero,Complex.ofReal_inj] using H - -lemma q_add (A D : Form M) (u : Grid M) : - q (A+D) u = ∑ v : Grid M, q A v * q D (u-v) := by - apply Complex.ofReal_inj.mp - rw [cq]; push_cast - simp_rw [cq,boltz_sum,Complex.ofReal_mul] - exact fc_prod _ _ _ - -lemma boltz_series (A : Form M) (u : Grid M) : - ∑ v : Grid M, q A v * wave v u = boltz A u := by - have H := congrArg Complex.re <| invert_fc (fun y => (↑(boltz A y):ℂ)) u - simp only [← cq,Complex.re_sum, Complex.re_ofReal_mul,Complex.ofReal_re] at H - exact H - -def F (A : Form M) := -log (q A 0) -def K (A : Form M) (x : Grid M) : ℝ := boltz A x / q A 0 - -lemma F_origin (A : Form M) : exp (-F A) = q A 0 := by rw [F,neg_neg,exp_log (q_origin_pos A)] -lemma F_zero : F (0:Form M) = 0 := by - simp_rw [F,q_origin,expU_ex]; unfold boltz - simp_rw [form_zero] - simp only [neg_zero, exp_zero, Ex_const, log_one, neg_zero] - -lemma F_mono {A B : Form M} (h : A ≤ B) : F A ≤ F B := by - unfold F - apply neg_le_neg - apply Real.log_le_log (q_origin_pos _) - rw [q_origin,q_origin,expU_ex,expU_ex] - exact Ex_mono _ fun x => exp_monotone (neg_le_neg (form_mono h x)) - -lemma K_eq (A : Form M) (x : Grid M) : K A x = exp (F A-form A x) := by - rw [K,boltz,← F_origin] - rw [← exp_sub] - congr 1 - ring - -lemma K_even (A : Form M) : (K A).Even := fun y => by simp [K,boltz_even _ y] - -lemma K_pos (A : Form M) (x : Grid M) : 0 < K A x := div_pos (boltz_pos _ _) (q_origin_pos _) - -lemma K_total (A : Form M) : expU (K A) = 1 := by - unfold K - simp_rw [expU_ex,div_eq_mul_inv,Ex_mul,← expU_ex,← q_origin,mul_inv_cancel₀ (q_origin_pos _).ne'] - -/-- Label noise law centered at zero relative to counting measure. -/ -def label (A : Form M) : Law (Grid M) := - ⟨fun x => side M * K A x, fun x => mul_nonneg (side_pos _).le (K_pos _ _).le, by - rw [← Finset.mul_sum] - exact K_total _⟩ - -lemma label_apply (A : Form M) (x : Grid M) : label A x = side M * K A x := rfl - -lemma label_pos (A : Form M) (x : Grid M) : 0 < label A x := - mul_pos (side_pos _) (K_pos _ _) - -lemma label_ex (A : Form M) (f : Grid M → ℝ) : - (label A).Ex f = expU fun x => K A x * f x := by - simp_rw [expU_ex,Ex,uniform_side,label_apply,mul_assoc] - -lemma label_cx (A : Form M) (f : Grid M → ℂ) : - (label A).Cx f = expU fun x => (K A x:ℂ) * f x := by - simp_rw [expU_cx,Cx,uniform_side,label_apply,Complex.ofReal_mul,mul_assoc] - -def cred (A : Form M) (u : Grid M) := q A u/q A 0 - -lemma cred_eq (A : Form M) (u : Grid M) : (↑(cred A u):ℂ) = (label A).Cx (chi u) := by - rw [label_cx] - simp_rw [K,div_eq_inv_mul,Complex.ofReal_mul,mul_assoc] - rw [expU_lmul,expU_coeff,cred,div_eq_inv_mul,Complex.ofReal_mul,← cq,q_even _ u] - -lemma cred_ex (A : Form M) (u : Grid M) : cred A u = (label A).Ex (wave u) := by - unfold wave - simpa only [re_Cx,Complex.ofReal_re,wave] using congrArg Complex.re (cred_eq A u) - -lemma cred_norm (A : Form M) (u : Grid M) : |cred A u| ≤ 1 := by - have H := norm_Cx_unit (label A) (chi u) (fun x => (norm_chi _ _).le) - rwa [← cred_eq,Complex.norm_real, Real.norm_eq_abs] at H - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Dyadic.Discrete +/-! # Thermal forms on the grid -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate NNReal +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Real Period + +variable {M : ℕ} + +/-- coefficient form. Throughout, nonnegativity of the coefficients means `0 ≤ A`. -/ +abbrev Form (M : ℕ) := Grid M → ℝ + +def wave (s y : Grid M) : ℝ := (chi s y).re +def cost (s y : Grid M) : ℝ := 1-wave s y +def form (A : Form M) : Grid M → ℝ := fun y => ∑ s, A s * cost s y + +lemma wave_even (s : Grid M) : (wave s).Even := by intro x; simp [wave,chi_neg] +lemma wave_comm (s y : Grid M) : wave s y = wave y s := by unfold wave; rw [chi_comm] +lemma wave_abs (s y : Grid M) : |wave s y| ≤ 1 := + (Complex.abs_re_le_norm (chi s y)).trans (norm_chi _ _).le +lemma cost_even (s : Grid M) : (cost s).Even := by intro x; unfold cost; rw [wave_even _ x] +lemma cost_symm (s y : Grid M) : cost s y = cost y s := by unfold cost; rw [wave_comm] +lemma cost_bounds (s y : Grid M) : 0 ≤ cost s y ∧ cost s y ≤ 2 := by + unfold cost + rcases abs_le.mp (wave_abs s y) with ⟨h1,h2⟩ + constructor <;> linarith + +lemma norm_phase_sq (s x : Grid M) : ‖1-chi s x‖^2 = 2*cost s x := by + unfold cost wave + have H := Complex.sq_norm (chi s x) + rw [norm_chi] at H + rw [Complex.sq_norm] + simp only [Complex.normSq_apply,Complex.sub_re,Complex.sub_im,Complex.one_re, Complex.one_im] at * + nlinarith + +lemma norm_phase_add (s x y : Grid M) : + ‖1-chi s (x+y)‖ ≤ ‖1-chi s x‖+‖1-chi s y‖ := by + rw [chi_add,show 1-chi s x*chi s y = 1-chi s x + chi s x*(1-chi s y) by ring] + refine (norm_add_le _ _).trans ?_ + rw [norm_mul,norm_chi,one_mul] + +lemma cost_add (s x y : Grid M) : cost s (x+y) ≤ 2*(cost s x+cost s y) := by + have H1 := norm_phase_sq s x + have H2 := norm_phase_sq s y + have H3 := norm_phase_sq s (x+y) + have H4 := norm_phase_add s x y + nlinarith [norm_nonneg (1-chi s (x+y)),sq_nonneg (‖1-chi s x‖-‖1-chi s y‖)] + +lemma form_zero (x : Grid M) : form 0 x = 0 := by simp [form] + +lemma form_at_zero (A : Form M) : form A 0 = 0 := by + simp [form, cost, wave, chi_zero_right] + +lemma form_add (A D : Form M) (x : Grid M) : form (A+D) x = form A x + form D x := by + simp [form, add_mul, Finset.sum_add_distrib] + +lemma form_smul (A : Form M) (t : ℝ) (x : Grid M) : form (t • A) x = t*form A x := by + simp [form, mul_assoc, Finset.mul_sum] + +lemma form_sum {ι : Type*} (s : Finset ι) (f : ι → Form M) (x : Grid M) : + form (∑ i ∈ s, f i) x = ∑ i ∈ s, form (f i) x := by + classical + induction s using Finset.induction_on with + | empty => simp [form_zero] + | @insert i s hi ih => rw [Finset.sum_insert hi,Finset.sum_insert hi,form_add,ih] + +lemma form_even (A : Form M) : (form A).Even := by intro x; simp [form, cost_even _ x] + +lemma form_mono {A D : Form M} (h : A ≤ D) : form A ≤ form D := by + intro y + exact Finset.sum_le_sum fun s _ => mul_le_mul_of_nonneg_right (h s) (cost_bounds _ _).1 + +lemma form_nonneg {A : Form M} (h : 0 ≤ A) (y : Grid M) : 0 ≤ form A y := by + simpa only [form_zero] using form_mono h y + +lemma form_triangle {A : Form M} (h : 0 ≤ A) (x y : Grid M) : + form A (x+y) ≤ 2*(form A x+form A y) := by + unfold form + rw [← Finset.sum_add_distrib,Finset.mul_sum] + apply Finset.sum_le_sum; intro i hi + apply le_trans (mul_le_mul_of_nonneg_left (cost_add _ _ _) (h i)) + exact le_of_eq (by ring) + +def boltz (A : Form M) (y : Grid M) : ℝ := exp (-form A y) +def q (A : Form M) (u : Grid M) : ℝ := (fc (fun y => (↑(boltz A y):ℂ)) u).re + +lemma boltz_even (A : Form M) : (boltz A).Even := by intro x; simp [boltz,form_even _ x] +lemma boltz_pos (A : Form M) (y : Grid M) : 0 < boltz A y := exp_pos _ +lemma boltz_sum (A D : Form M) (y : Grid M) : + boltz (A+D) y = boltz A y * boltz D y := by + simp [boltz,form_add, exp_add,mul_comm] + +lemma q_even (A : Form M) : (q A).Even := fun x => congrArg Complex.re <| + fc_neg_even (fun y => (↑(boltz A y):ℂ)) (fun y => by simp [boltz_even _ y]) x + +lemma cq (A : Form M) (x : Grid M) : + (↑(q A x):ℂ) = fc (fun y => (↑(boltz A y):ℂ)) x := real_fc_even _ (boltz_even _) _ + +lemma q_origin (A : Form M) : q A 0 = expU (boltz A) := by + apply Complex.ofReal_inj.mp + rw [cq, ← expU_real,fc] + simp_rw [neg_zero,chi_zero_left,mul_one] + +lemma q_origin_pos (A : Form M) : 0 < q A 0 := by + rw [q_origin]; unfold expU + rw [smul_eq_mul] + exact mul_pos (side_pos _) <| Finset.sum_pos (fun i _ => boltz_pos _ _) Finset.univ_nonempty + +lemma q_total (A : Form M) : ∑ x, q A x = 1 := by + have H := invert_fc (fun y => (↑(boltz A y):ℂ)) 0 + simp_rw [← cq,chi_zero_right,mul_one,← Complex.ofReal_sum] at H + simpa only [boltz,form_at_zero,neg_zero,Real.exp_zero,Complex.ofReal_inj] using H + +lemma q_add (A D : Form M) (u : Grid M) : + q (A+D) u = ∑ v : Grid M, q A v * q D (u-v) := by + apply Complex.ofReal_inj.mp + rw [cq]; push_cast + simp_rw [cq,boltz_sum,Complex.ofReal_mul] + exact fc_prod _ _ _ + +lemma boltz_series (A : Form M) (u : Grid M) : + ∑ v : Grid M, q A v * wave v u = boltz A u := by + have H := congrArg Complex.re <| invert_fc (fun y => (↑(boltz A y):ℂ)) u + simp only [← cq,Complex.re_sum, Complex.re_ofReal_mul,Complex.ofReal_re] at H + exact H + +def F (A : Form M) := -log (q A 0) +def K (A : Form M) (x : Grid M) : ℝ := boltz A x / q A 0 + +lemma F_origin (A : Form M) : exp (-F A) = q A 0 := by rw [F,neg_neg,exp_log (q_origin_pos A)] +lemma F_zero : F (0:Form M) = 0 := by + simp_rw [F,q_origin,expU_ex]; unfold boltz + simp_rw [form_zero] + simp only [neg_zero, exp_zero, Ex_const, log_one, neg_zero] + +lemma F_mono {A B : Form M} (h : A ≤ B) : F A ≤ F B := by + unfold F + apply neg_le_neg + apply Real.log_le_log (q_origin_pos _) + rw [q_origin,q_origin,expU_ex,expU_ex] + exact Ex_mono _ fun x => exp_monotone (neg_le_neg (form_mono h x)) + +lemma K_eq (A : Form M) (x : Grid M) : K A x = exp (F A-form A x) := by + rw [K,boltz,← F_origin] + rw [← exp_sub] + congr 1 + ring + +lemma K_even (A : Form M) : (K A).Even := fun y => by simp [K,boltz_even _ y] + +lemma K_pos (A : Form M) (x : Grid M) : 0 < K A x := div_pos (boltz_pos _ _) (q_origin_pos _) + +lemma K_total (A : Form M) : expU (K A) = 1 := by + unfold K + simp_rw [expU_ex,div_eq_mul_inv,Ex_mul,← expU_ex,← q_origin,mul_inv_cancel₀ (q_origin_pos _).ne'] + +/-- Label noise law centered at zero relative to counting measure. -/ +def label (A : Form M) : Law (Grid M) := + ⟨fun x => side M * K A x, fun x => mul_nonneg (side_pos _).le (K_pos _ _).le, by + rw [← Finset.mul_sum] + exact K_total _⟩ + +lemma label_apply (A : Form M) (x : Grid M) : label A x = side M * K A x := rfl + +lemma label_pos (A : Form M) (x : Grid M) : 0 < label A x := + mul_pos (side_pos _) (K_pos _ _) + +lemma label_ex (A : Form M) (f : Grid M → ℝ) : + (label A).Ex f = expU fun x => K A x * f x := by + simp_rw [expU_ex,Ex,uniform_side,label_apply,mul_assoc] + +lemma label_cx (A : Form M) (f : Grid M → ℂ) : + (label A).Cx f = expU fun x => (K A x:ℂ) * f x := by + simp_rw [expU_cx,Cx,uniform_side,label_apply,Complex.ofReal_mul,mul_assoc] + +def cred (A : Form M) (u : Grid M) := q A u/q A 0 + +lemma cred_eq (A : Form M) (u : Grid M) : (↑(cred A u):ℂ) = (label A).Cx (chi u) := by + rw [label_cx] + simp_rw [K,div_eq_inv_mul,Complex.ofReal_mul,mul_assoc] + rw [expU_lmul,expU_coeff,cred,div_eq_inv_mul,Complex.ofReal_mul,← cq,q_even _ u] + +lemma cred_ex (A : Form M) (u : Grid M) : cred A u = (label A).Ex (wave u) := by + unfold wave + simpa only [re_Cx,Complex.ofReal_re,wave] using congrArg Complex.re (cred_eq A u) + +lemma cred_norm (A : Form M) (u : Grid M) : |cred A u| ≤ 1 := by + have H := norm_Cx_unit (label A) (chi u) (fun x => (norm_chi _ _).le) + rwa [← cred_eq,Complex.norm_real, Real.norm_eq_abs] at H + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Ginibre.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Ginibre.lean index a48d76226cc8730e26c93ae29318881adf9fafbb..ccac9a61cd8130ba37a7fd75bf13bb1caaae36ce 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Ginibre.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Ginibre.lean @@ -1,241 +1,241 @@ -import OAI.Analysis.FourierLLogL.Thermal.Positive - -/-! # Cosine covariance. On an even cyclic group the two-copy change of variables -uses two branches (one of them half-translated). -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Period Real - -variable {M : ℕ} - -private lemma chi_one_ne {s : Grid M} (hs : s ≠ 0) : chi s 1 ≠ 1 := by - intro H - have hi (x : Grid M) : chi s x = 1 := by - rw [chi_comm,← ZMod.natCast_zmod_val x,← nsmul_one,chi_nsmul,chi_comm 1 s,H,one_pow] - have hh := expU_chi s - rw [ite_eq_right hs,funext hi,← funext chi_zero_left,expU_chi,ite_eq_left rfl] at hh - norm_num at hh - -private lemma odd_phase {s : Grid M} (hs : s ≠ 0) (h : s+s=0) : chi s 1 = -1 := by - have H1 : chi s 1 * chi s 1 = 1 := by rw [← chi_add', h,chi_zero_left] - have H2 : (chi s 1 - 1)*(chi s 1+1)=0 := by rw [show - (chi s 1 - 1)*(chi s 1+1)= chi s 1 * chi s 1-1 by ring,H1,sub_self] - rcases eq_zero_or_eq_zero_of_mul_eq_zero H2 with h1|h1 - · exact (chi_one_ne hs (sub_eq_zero.mp h1)).elim - · exact eq_neg_of_add_eq_zero_left h1 - -private lemma ch_parity (s : Grid M) : - expU (fun i => chi s (i+i)) + expU (fun i => chi s (1+(i+i))) = 2 * expU (chi s) := by - have hi (i : Grid M) : chi s (i+i) = chi (s+s) i := by rw [chi_add,chi_add'] - simp_rw [chi_add _ 1,hi] - rw [expU_lmul,expU_chi,expU_chi] - by_cases hs : s=0 - · subst s; simp [chi_zero_left]; ring - rw [ite_eq_right hs] - split_ifs with H - · rw [odd_phase hs H]; ring - ring - -private lemma c_parity (f : Grid M → ℂ) : - expU (fun i => f (i+i)) + expU (fun i => f (1+(i+i))) = 2 * expU f := by - let g (x : Grid M) := ∑ s, fc f s * chi s x - suffices HH : expU (fun i => g (i+i)) + expU (fun i => g (1+(i+i))) = 2 * expU g by - have hi : g=f := funext (invert_fc _) - rwa [hi] at HH - unfold g - simp_rw [expU_sum,expU_lmul] - rw [← Finset.sum_add_distrib,Finset.mul_sum] - refine Finset.sum_congr rfl fun i _ => ?_ - rw [← mul_add,ch_parity]; ring - -private lemma r_parity (f : Grid M → ℝ) : - expU (fun i => f (i+i)) + expU (fun i => f (1+(i+i))) = 2 * expU f := by - have hi := c_parity (fun x => (↑(f x):ℂ)) - simp_rw [expU_real] at hi - exact_mod_cast hi - -private def dblmean (f : Grid M → Grid M → ℝ) : ℝ := expU fun x => expU (f x) -private lemma dbl_ex (f : Grid M → Grid M → ℝ) (P : Law (Grid M)) - (hP : P = uniform _) : dblmean f = P.Ex fun x => P.Ex (f x) := by simp_rw [hP,dblmean,expU_ex] - -private def branch (f : Grid M → Grid M → ℝ) (c : Grid M) := - dblmean (fun i j => f (c+i+j) (i-j)) - -private lemma branch_eq (f : Grid M → Grid M → ℝ) (c : Grid M) : - branch f c = expU (fun u => expU fun i => f u (c+(i+i)-u)) := by - unfold branch dblmean - rw [expU_prod (fun u i : Grid M => f u (c+(i+i)-u))] - refine congrArg expU (funext fun i => ?_) - dsimp - rw [← expU_translate (fun u => f u (c+(i+i)-u)) (c+i)] - refine congrArg expU (funext fun j => ?_) - have H : c+(i+i)-(c+i+j)=i-j := by ring - simp only [H] - -private lemma branch_sum (f : Grid M → Grid M → ℝ) : - branch f 0 + branch f 1 = 2 * dblmean f := by - simp_rw [branch_eq,dblmean,zero_add, expU_ex] - rw [← Ex_add, ← Ex_lmul] - refine congrArg (uniform (Grid M)).Ex (funext fun u => ?_) - simp_rw [← expU_ex] - rw [r_parity (fun a => f u (a-u))] - rw [show (fun a => f u (a-u)) = (fun a => f u (-u+a)) from funext fun a => by - have H : a-u=-u+a := by ring - rw [H]] - rw [expU_translate] - -private def halfWave (s c : Grid M) : ℂ := - phase (s.val : ℝ) (pos c/2) - -private def unitWave (s c i : Grid M) := halfWave s c * chi s i - -private lemma half_mul (s c : Grid M) : halfWave s c * halfWave s c = chi s c := by - rw [halfWave,← phase_add, show pos c /2+pos c/2=pos c by ring] - have H := chi_cast (s.val:ℤ) c - simpa only [Int.cast_natCast,ZMod.natCast_zmod_val] using H.symm - -private lemma branch_waves (s c i j : Grid M) : - wave s (c+i+j) + wave s (i-j) = - 2*(unitWave s c i).re*(unitWave s c j).re ∧ - wave s (c+i+j) - wave s (i-j) = - -(2*(unitWave s c i).im*(unitWave s c j).im) := by - have Hn : halfWave s c * conj (halfWave s c) = 1 := by - rw [Complex.mul_conj,←Complex.sq_norm,(show ‖halfWave s c‖=1 from phase_unimod _ _)] - simp - have He : chi s (c+i+j) = unitWave s c i * unitWave s c j := by - unfold unitWave - rw [chi_add,chi_add,← half_mul] - ring - have Hf : chi s (i-j) = unitWave s c i * conj (unitWave s c j) := by - unfold unitWave - rw [chi_sub,map_mul] - rw [show halfWave s c * chi s i * (conj (halfWave s c) * conj (chi s j)) - = (chi s i * conj (chi s j)) * (halfWave s c * conj (halfWave s c)) by ring,Hn,mul_one] - simp only [wave,He,Hf,Complex.mul_re, Complex.conj_re, Complex.conj_im] - constructor <;> ring - -private def bil (f : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : ℝ := - dblmean fun i j => h i*h j * f i j - -private def pairPot (A : Form M) (R : Grid M → Grid M → ℝ) (i j : Grid M) := - ∑ s, A s * R s i * R s j - -private lemma bil_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → Grid M → ℝ) (h : Grid M → ℝ) : - bil (fun i j => ∑ k ∈ s, f k i j) h = ∑ k ∈ s, bil (f k) h := by - simp_rw [bil,dblmean, Finset.mul_sum, expU_sum] - -private lemma dblmean_scale (c : ℝ) (f : Grid M → Grid M → ℝ) : - dblmean (fun i j => c*f i j) = c * dblmean f := by - simp_rw [dbl_ex _ (uniform _) rfl,Ex_lmul] - -private lemma bil_scale (c : ℝ) (f : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : - bil (fun i j => c*f i j) h = c*bil f h := by - unfold bil - rw [← dblmean_scale] - congr 1; funext i j; ring - -private lemma pair_nonneg {A : Form M} (hA : 0 ≤ A) (R : Grid M → Grid M → ℝ) (h : Grid M → ℝ) - (n : ℕ) : - 0 ≤ bil (fun i j => (pairPot A R i j)^n) h := by - induction n generalizing h with - | zero => - unfold bil dblmean - simp_rw [pow_zero, mul_one, expU_ex, Ex_lmul,Ex_mul] - exact mul_self_nonneg _ - | succ n ih => - let f := pairPot A R - have heq : - bil (fun i j => (f i j)^(n+1)) h = - ∑ s : Grid M, A s * bil (fun i j => (f i j)^n) (fun i => h i * R s i) := by - have He (i j : Grid M) : f i j ^(n+1) = - ∑ s, A s*(R s i*R s j*(f i j)^n) := by - rw [pow_succ] - dsimp only [f,pairPot]; rw [Finset.mul_sum] - exact Finset.sum_congr rfl fun _ _ => by ring - simp_rw [He,bil_sum,bil_scale] - refine Finset.sum_congr rfl fun s _ => congrArg (A s*·) ?_ - unfold bil; refine congrArg dblmean ?_; funext i j; ring - rw [heq] - exact Finset.sum_nonneg fun s _ => mul_nonneg (hA s) (ih _) - -private lemma pair_exp_nonneg {A : Form M} (hA : 0 ≤ A) (R : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : - 0 ≤ bil (fun i j => exp (pairPot A R i j)) h := by - let f := pairPot A R - have hi : Tendsto (fun n => bil (fun i j => aprox n (f i j)) h) - atTop (𝓝 (bil (fun i j => exp (f i j)) h)) := by - simp only [bil,dblmean,expU,smul_eq_mul] - refine Tendsto.const_mul _ <| tendsto_finsetSum _ fun i _ => ?_ - refine Tendsto.const_mul _ <| tendsto_finsetSum _ fun j _ => ?_ - exact Tendsto.const_mul _ (aprox_lim _) - apply ge_of_tendsto hi (Eventually.of_forall fun n => ?_) - simp_rw [aprox,bil_sum,bil_scale] - exact Finset.sum_nonneg fun s _ => mul_nonneg (by positivity) (pair_nonneg hA _ _ s) - -private lemma branch_pos {A : Form M} (hA : 0 ≤ A) (s t c : Grid M) : - 0 ≤ branch (fun y z => K A y*K A z * ((wave s y-wave s z)*(wave t y-wave t z))) c := by - let U := fun s i => unitWave s c i - let R := fun s i => (U s i).re - let h := fun i => (U s i).im*(U t i).im - let D := (2:ℝ) • A - let w := 2*(F A-∑ i, A i) - have HK (i j : Grid M) : - K A (c+i+j)* K A (i-j) = exp w * exp (pairPot D R i j) := by - simp_rw [K_eq,← exp_add] - have He : form A (c+i+j) + form A (i-j) = - 2*∑ i, A i - pairPot D R i j := by - dsimp [form,pairPot,D]; rw [← Finset.sum_add_distrib,Finset.mul_sum,← Finset.sum_sub_distrib] - exact Finset.sum_congr rfl fun k _ => by - dsimp only [cost,smul_eq_mul,R,U] - have H := (branch_waves k c i j).1 - linear_combination -(A k) * H - unfold w - congr 1 - linarith - unfold branch - have Heq : - (fun i j => K A (c+i+j) * K A (i-j) * - ((wave s (c+i+j)-wave s (i-j)) * (wave t (c+i+j)-wave t (i-j)))) = - (fun i j => 4*exp w * (h i * h j * exp (pairPot D R i j))) := by - funext i j - rw [HK,(branch_waves _ _ _ _).2,(branch_waves _ _ _ _).2] - unfold h U; ring - rw [Heq,dblmean_scale] - exact mul_nonneg (by positivity) <| pair_exp_nonneg (by - intro i - change 0 ≤ 2*A i - exact mul_nonneg (by norm_num) (hA i)) R h - -theorem ginibre {A : Form M} (hA : 0 ≤ A) (s t : Grid M) : - cred A s * cred A t ≤ (label A).Ex (fun i => wave s i * wave t i) := by - let P := label A - let f : Grid M → Grid M → ℝ := - fun i j => (wave s i-wave s j) * (wave t i-wave t j) - let g : Grid M → Grid M → ℝ := - fun i j => K A i*K A j*f i j - have H := branch_sum g - have H1 := branch_pos hA s t 0 - have H2 := branch_pos hA s t 1 - change 0 ≤ branch g _ at H1 H2 - have heq : - dblmean g = P.Ex fun i => P.Ex (f i) := by - unfold P; simp_rw [label_ex,dblmean,expU_ex,g,mul_assoc,Ex_lmul] - have He (i j : Grid M) : f i j = - ((wave s i * wave t i + wave s j * wave t j) - wave s j * wave t i) - wave s i*wave t j := by - unfold f; ring - rw [heq] at H - have Heq (i : Grid M) : P.Ex (f i) = - ((wave s i*wave t i + P.Ex (fun i => wave s i * wave t i)) - - P.Ex (wave s)*wave t i) - wave s i * P.Ex (wave t) := by - rw [funext (He i)]; simp_rw [Ex_sub,Ex_add,Ex_lmul,Ex_mul,Ex_const] - simp_rw [Heq,Ex_sub,Ex_add,Ex_lmul,Ex_mul,Ex_const] at H - rw [cred_ex,cred_ex] - change P.Ex _ * P.Ex _ ≤ P.Ex _ - nlinarith - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Positive + +/-! # Cosine covariance. On an even cyclic group the two-copy change of variables +uses two branches (one of them half-translated). -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Period Real + +variable {M : ℕ} + +private lemma chi_one_ne {s : Grid M} (hs : s ≠ 0) : chi s 1 ≠ 1 := by + intro H + have hi (x : Grid M) : chi s x = 1 := by + rw [chi_comm,← ZMod.natCast_zmod_val x,← nsmul_one,chi_nsmul,chi_comm 1 s,H,one_pow] + have hh := expU_chi s + rw [ite_eq_right hs,funext hi,← funext chi_zero_left,expU_chi,ite_eq_left rfl] at hh + norm_num at hh + +private lemma odd_phase {s : Grid M} (hs : s ≠ 0) (h : s+s=0) : chi s 1 = -1 := by + have H1 : chi s 1 * chi s 1 = 1 := by rw [← chi_add', h,chi_zero_left] + have H2 : (chi s 1 - 1)*(chi s 1+1)=0 := by rw [show + (chi s 1 - 1)*(chi s 1+1)= chi s 1 * chi s 1-1 by ring,H1,sub_self] + rcases eq_zero_or_eq_zero_of_mul_eq_zero H2 with h1|h1 + · exact (chi_one_ne hs (sub_eq_zero.mp h1)).elim + · exact eq_neg_of_add_eq_zero_left h1 + +private lemma ch_parity (s : Grid M) : + expU (fun i => chi s (i+i)) + expU (fun i => chi s (1+(i+i))) = 2 * expU (chi s) := by + have hi (i : Grid M) : chi s (i+i) = chi (s+s) i := by rw [chi_add,chi_add'] + simp_rw [chi_add _ 1,hi] + rw [expU_lmul,expU_chi,expU_chi] + by_cases hs : s=0 + · subst s; simp [chi_zero_left]; ring + rw [ite_eq_right hs] + split_ifs with H + · rw [odd_phase hs H]; ring + ring + +private lemma c_parity (f : Grid M → ℂ) : + expU (fun i => f (i+i)) + expU (fun i => f (1+(i+i))) = 2 * expU f := by + let g (x : Grid M) := ∑ s, fc f s * chi s x + suffices HH : expU (fun i => g (i+i)) + expU (fun i => g (1+(i+i))) = 2 * expU g by + have hi : g=f := funext (invert_fc _) + rwa [hi] at HH + unfold g + simp_rw [expU_sum,expU_lmul] + rw [← Finset.sum_add_distrib,Finset.mul_sum] + refine Finset.sum_congr rfl fun i _ => ?_ + rw [← mul_add,ch_parity]; ring + +private lemma r_parity (f : Grid M → ℝ) : + expU (fun i => f (i+i)) + expU (fun i => f (1+(i+i))) = 2 * expU f := by + have hi := c_parity (fun x => (↑(f x):ℂ)) + simp_rw [expU_real] at hi + exact_mod_cast hi + +private def dblmean (f : Grid M → Grid M → ℝ) : ℝ := expU fun x => expU (f x) +private lemma dbl_ex (f : Grid M → Grid M → ℝ) (P : Law (Grid M)) + (hP : P = uniform _) : dblmean f = P.Ex fun x => P.Ex (f x) := by simp_rw [hP,dblmean,expU_ex] + +private def branch (f : Grid M → Grid M → ℝ) (c : Grid M) := + dblmean (fun i j => f (c+i+j) (i-j)) + +private lemma branch_eq (f : Grid M → Grid M → ℝ) (c : Grid M) : + branch f c = expU (fun u => expU fun i => f u (c+(i+i)-u)) := by + unfold branch dblmean + rw [expU_prod (fun u i : Grid M => f u (c+(i+i)-u))] + refine congrArg expU (funext fun i => ?_) + dsimp + rw [← expU_translate (fun u => f u (c+(i+i)-u)) (c+i)] + refine congrArg expU (funext fun j => ?_) + have H : c+(i+i)-(c+i+j)=i-j := by ring + simp only [H] + +private lemma branch_sum (f : Grid M → Grid M → ℝ) : + branch f 0 + branch f 1 = 2 * dblmean f := by + simp_rw [branch_eq,dblmean,zero_add, expU_ex] + rw [← Ex_add, ← Ex_lmul] + refine congrArg (uniform (Grid M)).Ex (funext fun u => ?_) + simp_rw [← expU_ex] + rw [r_parity (fun a => f u (a-u))] + rw [show (fun a => f u (a-u)) = (fun a => f u (-u+a)) from funext fun a => by + have H : a-u=-u+a := by ring + rw [H]] + rw [expU_translate] + +private def halfWave (s c : Grid M) : ℂ := + phase (s.val : ℝ) (pos c/2) + +private def unitWave (s c i : Grid M) := halfWave s c * chi s i + +private lemma half_mul (s c : Grid M) : halfWave s c * halfWave s c = chi s c := by + rw [halfWave,← phase_add, show pos c /2+pos c/2=pos c by ring] + have H := chi_cast (s.val:ℤ) c + simpa only [Int.cast_natCast,ZMod.natCast_zmod_val] using H.symm + +private lemma branch_waves (s c i j : Grid M) : + wave s (c+i+j) + wave s (i-j) = + 2*(unitWave s c i).re*(unitWave s c j).re ∧ + wave s (c+i+j) - wave s (i-j) = + -(2*(unitWave s c i).im*(unitWave s c j).im) := by + have Hn : halfWave s c * conj (halfWave s c) = 1 := by + rw [Complex.mul_conj,←Complex.sq_norm,(show ‖halfWave s c‖=1 from phase_unimod _ _)] + simp + have He : chi s (c+i+j) = unitWave s c i * unitWave s c j := by + unfold unitWave + rw [chi_add,chi_add,← half_mul] + ring + have Hf : chi s (i-j) = unitWave s c i * conj (unitWave s c j) := by + unfold unitWave + rw [chi_sub,map_mul] + rw [show halfWave s c * chi s i * (conj (halfWave s c) * conj (chi s j)) + = (chi s i * conj (chi s j)) * (halfWave s c * conj (halfWave s c)) by ring,Hn,mul_one] + simp only [wave,He,Hf,Complex.mul_re, Complex.conj_re, Complex.conj_im] + constructor <;> ring + +private def bil (f : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : ℝ := + dblmean fun i j => h i*h j * f i j + +private def pairPot (A : Form M) (R : Grid M → Grid M → ℝ) (i j : Grid M) := + ∑ s, A s * R s i * R s j + +private lemma bil_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → Grid M → ℝ) (h : Grid M → ℝ) : + bil (fun i j => ∑ k ∈ s, f k i j) h = ∑ k ∈ s, bil (f k) h := by + simp_rw [bil,dblmean, Finset.mul_sum, expU_sum] + +private lemma dblmean_scale (c : ℝ) (f : Grid M → Grid M → ℝ) : + dblmean (fun i j => c*f i j) = c * dblmean f := by + simp_rw [dbl_ex _ (uniform _) rfl,Ex_lmul] + +private lemma bil_scale (c : ℝ) (f : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : + bil (fun i j => c*f i j) h = c*bil f h := by + unfold bil + rw [← dblmean_scale] + congr 1; funext i j; ring + +private lemma pair_nonneg {A : Form M} (hA : 0 ≤ A) (R : Grid M → Grid M → ℝ) (h : Grid M → ℝ) + (n : ℕ) : + 0 ≤ bil (fun i j => (pairPot A R i j)^n) h := by + induction n generalizing h with + | zero => + unfold bil dblmean + simp_rw [pow_zero, mul_one, expU_ex, Ex_lmul,Ex_mul] + exact mul_self_nonneg _ + | succ n ih => + let f := pairPot A R + have heq : + bil (fun i j => (f i j)^(n+1)) h = + ∑ s : Grid M, A s * bil (fun i j => (f i j)^n) (fun i => h i * R s i) := by + have He (i j : Grid M) : f i j ^(n+1) = + ∑ s, A s*(R s i*R s j*(f i j)^n) := by + rw [pow_succ] + dsimp only [f,pairPot]; rw [Finset.mul_sum] + exact Finset.sum_congr rfl fun _ _ => by ring + simp_rw [He,bil_sum,bil_scale] + refine Finset.sum_congr rfl fun s _ => congrArg (A s*·) ?_ + unfold bil; refine congrArg dblmean ?_; funext i j; ring + rw [heq] + exact Finset.sum_nonneg fun s _ => mul_nonneg (hA s) (ih _) + +private lemma pair_exp_nonneg {A : Form M} (hA : 0 ≤ A) (R : Grid M → Grid M → ℝ) (h : Grid M → ℝ) : + 0 ≤ bil (fun i j => exp (pairPot A R i j)) h := by + let f := pairPot A R + have hi : Tendsto (fun n => bil (fun i j => aprox n (f i j)) h) + atTop (𝓝 (bil (fun i j => exp (f i j)) h)) := by + simp only [bil,dblmean,expU,smul_eq_mul] + refine Tendsto.const_mul _ <| tendsto_finsetSum _ fun i _ => ?_ + refine Tendsto.const_mul _ <| tendsto_finsetSum _ fun j _ => ?_ + exact Tendsto.const_mul _ (aprox_lim _) + apply ge_of_tendsto hi (Eventually.of_forall fun n => ?_) + simp_rw [aprox,bil_sum,bil_scale] + exact Finset.sum_nonneg fun s _ => mul_nonneg (by positivity) (pair_nonneg hA _ _ s) + +private lemma branch_pos {A : Form M} (hA : 0 ≤ A) (s t c : Grid M) : + 0 ≤ branch (fun y z => K A y*K A z * ((wave s y-wave s z)*(wave t y-wave t z))) c := by + let U := fun s i => unitWave s c i + let R := fun s i => (U s i).re + let h := fun i => (U s i).im*(U t i).im + let D := (2:ℝ) • A + let w := 2*(F A-∑ i, A i) + have HK (i j : Grid M) : + K A (c+i+j)* K A (i-j) = exp w * exp (pairPot D R i j) := by + simp_rw [K_eq,← exp_add] + have He : form A (c+i+j) + form A (i-j) = + 2*∑ i, A i - pairPot D R i j := by + dsimp [form,pairPot,D]; rw [← Finset.sum_add_distrib,Finset.mul_sum,← Finset.sum_sub_distrib] + exact Finset.sum_congr rfl fun k _ => by + dsimp only [cost,smul_eq_mul,R,U] + have H := (branch_waves k c i j).1 + linear_combination -(A k) * H + unfold w + congr 1 + linarith + unfold branch + have Heq : + (fun i j => K A (c+i+j) * K A (i-j) * + ((wave s (c+i+j)-wave s (i-j)) * (wave t (c+i+j)-wave t (i-j)))) = + (fun i j => 4*exp w * (h i * h j * exp (pairPot D R i j))) := by + funext i j + rw [HK,(branch_waves _ _ _ _).2,(branch_waves _ _ _ _).2] + unfold h U; ring + rw [Heq,dblmean_scale] + exact mul_nonneg (by positivity) <| pair_exp_nonneg (by + intro i + change 0 ≤ 2*A i + exact mul_nonneg (by norm_num) (hA i)) R h + +theorem ginibre {A : Form M} (hA : 0 ≤ A) (s t : Grid M) : + cred A s * cred A t ≤ (label A).Ex (fun i => wave s i * wave t i) := by + let P := label A + let f : Grid M → Grid M → ℝ := + fun i j => (wave s i-wave s j) * (wave t i-wave t j) + let g : Grid M → Grid M → ℝ := + fun i j => K A i*K A j*f i j + have H := branch_sum g + have H1 := branch_pos hA s t 0 + have H2 := branch_pos hA s t 1 + change 0 ≤ branch g _ at H1 H2 + have heq : + dblmean g = P.Ex fun i => P.Ex (f i) := by + unfold P; simp_rw [label_ex,dblmean,expU_ex,g,mul_assoc,Ex_lmul] + have He (i j : Grid M) : f i j = + ((wave s i * wave t i + wave s j * wave t j) - wave s j * wave t i) - wave s i*wave t j := by + unfold f; ring + rw [heq] at H + have Heq (i : Grid M) : P.Ex (f i) = + ((wave s i*wave t i + P.Ex (fun i => wave s i * wave t i)) + - P.Ex (wave s)*wave t i) - wave s i * P.Ex (wave t) := by + rw [funext (He i)]; simp_rw [Ex_sub,Ex_add,Ex_lmul,Ex_mul,Ex_const] + simp_rw [Heq,Ex_sub,Ex_add,Ex_lmul,Ex_mul,Ex_const] at H + rw [cred_ex,cred_ex] + change P.Ex _ * P.Ex _ ≤ P.Ex _ + nlinarith + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/GridFreq.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/GridFreq.lean index 88a6b4307f80537f19542fda02a4b6fbc65eca4f..d96e752ba50e8f7ed24b7ed0b9754b0f81aaea58 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/GridFreq.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/GridFreq.lean @@ -1,168 +1,168 @@ -import OAI.Analysis.FourierLLogL.Thermal.History -import OAI.Analysis.FourierLLogL.Dyadic.Snap -import OAI.Analysis.FourierLLogL.Compress.Params - -/-! # Applying compression on a subgrid -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Period Real Compress.Params - -variable {M l : ℕ} - -/-- Sum of coeffs on multiples equals expectation over annihilator -/ -def summesh (h : l ≤ M) (A : Form M) : ℝ := ∑ i : Grid l, q A (raise h i) -private lemma expU_wave (v : Grid M) : expU (wave v) = if v=0 then 1 else 0 := by - have hi := congrArg Complex.re (expU_chi v) - rw [expU_cx,re_Cx] at hi - rw [expU_ex] - split_ifs with H <;> simp only [H,ite_eq_left,Complex.one_re] at hi - · subst v; exact hi - · exact hi - -lemma summesh_eq (h : l ≤ M) (A : Form M) : - summesh h A = expU fun x => boltz A (raise (show M-l ≤ M by omega) x) := by - classical - let k := M-l - have hk : k ≤ M := by unfold k; omega - have he (i : Grid k) : boltz A (raise hk i) = - ∑ t, q A t * wave (cut hk t) i := by - rw [← boltz_series] - refine Finset.sum_congr rfl fun t _ => ?_ - rw [wave,chi_comm,cut_chi,chi_comm i _,wave] - change summesh h A = expU fun x : Grid k => boltz A (raise hk x) - simp_rw [he,expU_sum,expU_ex, Ex_lmul, ← expU_ex,expU_wave,mul_ite,mul_one,mul_zero] - let s : Finset (Grid M) := Finset.univ.image (raise h) - have H : Finset.univ.filter (fun i => cut hk i=0) = s := by - ext x - simp only [s,Finset.mem_image,Finset.mem_filter,Finset.mem_univ,true_and] - exact annihilator h x - rw [← Finset.sum_filter,H] - unfold s summesh - exact (Finset.sum_image (by intro i _ j _ he; exact raise_inj _ he)).symm - -lemma summesh_bounds (h : l ≤ M) {A : Form M} {R : ℝ} (hi : 2^(M-l) ≤ R) (hA : S M R ≤ A) : - side (M-l) ≤ summesh h A ∧ summesh h A ≤ 12*side (M-l) := by - rw [summesh_eq] - let r := M-l - have hr : r ≤ M := Nat.sub_le _ _ - let f (x : Grid r) := boltz A (raise hr x) - change side r ≤ expU f ∧ _ - constructor - · have H := Finset.single_le_sum (s:=Finset.univ) (f:=f) - (fun i _ => (boltz_pos _ _).le) (Finset.mem_univ (0 : Grid r)) - have Hz : f 0 = 1 := by simp [f,boltz,form_at_zero] - unfold expU - rw [smul_eq_mul] - rw [Hz] at H - have := side_pos r; nlinarith - · let g : ℝ := 2^r - have hg : 0 ≤ g := by unfold g; positivity - have he (x : Grid r) : - form (S M g) (raise hr x) = form (S r g) x := by - simp_rw [form_S,cost,wave_at_one,raised_pos] - apply le_trans (b:=q (S r g) 0) - · rw [q_origin,expU_ex,expU_ex] - exact Ex_mono _ fun i => exp_monotone <| neg_le_neg <| (he i).ge.trans <| - form_mono ((S_mono hg hi).trans hA) _ - · have HH := q_S_upper' g (by unfold g; positivity) (M:=r) le_rfl - rwa [side_pow_two,← div_eq_mul_inv] - -lemma S_one_pos {R : ℝ} (h : 1 ≤ R) : 0 < S M R 1 := by - simp [S]; nlinarith - -def fq (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) : Law (Grid l) := - proportional (fun i : Grid l => q A (raise hh i)) (fun _ => q_nonneg hA _) (by - have H := Finset.single_le_sum (s:=Finset.univ) (f:=fun i => q A (raise hh i)) - (fun i _ => q_nonneg hA _) (Finset.mem_univ (0:Grid l)) - rw [map_zero] at H - exact lt_of_lt_of_le (q_origin_pos A) H) - -lemma fq_apply (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) (i : Grid l) : - fq hh A hA i = q A (raise hh i) / summesh hh A := rfl - -def fq_cost (hh : l ≤ M) (A : Form M) (i : Grid l) := -log (cred A (raise hh i)) - -def raiseChar (hh : l ≤ M) (t : Grid M) : AddChar (Grid l) ℂ := - phaseChar (cut hh t) - -lemma raiseChar_eq (hh : l ≤ M) (t : Grid M) (i : Grid l) : - raiseChar hh t i = chi (raise hh i) t := by rw [cut_chi,chi_comm]; rfl - -lemma fq_spec (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) - {R : ℝ} (hr : 2^(M-l) ≤ R) (h : S M R ≤ A) : - let p := fq hh A hA - let c := fq_cost hh A - (∀ x, 0 < p x) ∧ (∀ x, 0 ≤ c x) ∧ c 0=0 ∧ - (∀ x, (↑(exp (-c x)):ℂ)=(label A).Cx (fun t => raiseChar hh t x)) ∧ - (∀ x, -log (p x) = -log (p 0) + c x) ∧ - -log (p 0) ≤ F A - log (2^(M-l):ℝ)+12 := by - have hi := summesh_bounds hh hr h - have hp : 0 < summesh hh A := lt_of_lt_of_le (side_pos _) hi.1 - have hz (x : Grid M) : 0 < q A x := q_pos_one hA - (lt_of_lt_of_le (S_one_pos (le_trans (one_le_pow₀ (by norm_num : (1:ℝ)≤2)) hr)) (h _)) x - have he (x : Grid M) : 0 < cred A x := div_pos (hz x) (hz 0) - refine ⟨fun x => div_pos (hz _) hp,fun x => neg_nonneg.mpr ?_,?_,?_,?_,?_⟩ - · apply Real.log_nonpos (he _).le - exact (le_abs_self _).trans (cred_norm _ _) - · simp [fq_cost,cred,(hz _).ne'] - · intro x - simp_rw [fq_cost,neg_neg,exp_log (he _),raiseChar_eq] - exact cred_eq _ _ - · intro x - simp_rw [fq_apply,Real.log_div (hz _).ne' hp.ne',fq_cost,map_zero,cred, - Real.log_div (hz _).ne' (hz _).ne'] - ring - · rw [fq_apply,map_zero,Real.log_div (hz _).ne' hp.ne'] - have HH := log_le_log hp hi.2 - rw [log_mul (by norm_num) (side_pos _).ne', side_pow_two, Real.log_inv] at HH - unfold F - linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<12)] - -/-- Snap parameter l associated with a node and h. -/ -def snapExp (j : ℕ) (p : Node) := - p.lev-(40*ell j+6) -def snapGrid (j : ℕ) (p : Node) (M : ℕ) := M-snapExp j p -def sig (j : ℕ) := 1/(slow j:ℝ)^40 -def sepRad (j : ℕ) := 1/(slow j:ℝ)^30 - -lemma snap_le (j : ℕ) (p : Node) : snapGrid j p M ≤ M := Nat.sub_le _ _ -lemma step_scale (j : ℕ) {p : Node} (hp : p.lev ≤ M) : - let l := snapGrid j p M - M-l ≤ p.lev ∧ (2:ℝ)^(M-l) ≤ p.invlen ∧ - p.invlen ≤ 64*(slow j:ℝ)^40 * 2^(M-l) ∧ - ∀ u : Grid M, rad (u-raise (snap_le j p) (down (snap_le j p) u)) ≤ sig j*p.invlen/20 := by - intro l - let n := M-l - have he : n = p.lev - (40*ell j+6) := by dsimp only [n,l,snapGrid,snapExp]; omega - have hn : n ≤ p.lev := by omega - have ht : (slow j:ℝ)^40*64 = (2:ℝ)^(40*ell j+6) := by - simp only [slow,Nat.cast_pow,Nat.cast_ofNat] - rw [← pow_mul,pow_add]; norm_num; rw [mul_comm (ell j)] - refine ⟨hn,pow_le_pow_right₀ (by norm_num) hn,?_,?_⟩ - · rw [mul_comm 64,ht,← pow_add] - exact pow_le_pow_right₀ (by norm_num) (by omega) - intro u - have HH := snap_dist (snap_le (M:=M) j p) u - apply HH.trans - have hc : 0 < (slow j:ℝ)^40 := pow_pos (by exact_mod_cast slow_pos j) _ - by_cases hd : n=0 - · change (2:ℝ)^n-1 ≤ _ - rw [hd,pow_zero,sub_self] - have := p.invlen_pos; unfold sig; positivity - have hv : p.invlen = (2:ℝ)^n * ((slow j:ℝ)^40*64) := by - rw [ht,← pow_add]; unfold invlen - congr 1; omega - rw [hv,sig] - change (2:ℝ)^n-1 ≤ _ - have hs : 0 < (slow j:ℝ) := by exact_mod_cast slow_pos j - field_simp - have H : 0 < (2:ℝ)^n := by positivity - nlinarith - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.History +import OAI.Analysis.FourierLLogL.Dyadic.Snap +import OAI.Analysis.FourierLLogL.Compress.Params + +/-! # Applying compression on a subgrid -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Period Real Compress.Params + +variable {M l : ℕ} + +/-- Sum of coeffs on multiples equals expectation over annihilator -/ +def summesh (h : l ≤ M) (A : Form M) : ℝ := ∑ i : Grid l, q A (raise h i) +private lemma expU_wave (v : Grid M) : expU (wave v) = if v=0 then 1 else 0 := by + have hi := congrArg Complex.re (expU_chi v) + rw [expU_cx,re_Cx] at hi + rw [expU_ex] + split_ifs with H <;> simp only [H,ite_eq_left,Complex.one_re] at hi + · subst v; exact hi + · exact hi + +lemma summesh_eq (h : l ≤ M) (A : Form M) : + summesh h A = expU fun x => boltz A (raise (show M-l ≤ M by omega) x) := by + classical + let k := M-l + have hk : k ≤ M := by unfold k; omega + have he (i : Grid k) : boltz A (raise hk i) = + ∑ t, q A t * wave (cut hk t) i := by + rw [← boltz_series] + refine Finset.sum_congr rfl fun t _ => ?_ + rw [wave,chi_comm,cut_chi,chi_comm i _,wave] + change summesh h A = expU fun x : Grid k => boltz A (raise hk x) + simp_rw [he,expU_sum,expU_ex, Ex_lmul, ← expU_ex,expU_wave,mul_ite,mul_one,mul_zero] + let s : Finset (Grid M) := Finset.univ.image (raise h) + have H : Finset.univ.filter (fun i => cut hk i=0) = s := by + ext x + simp only [s,Finset.mem_image,Finset.mem_filter,Finset.mem_univ,true_and] + exact annihilator h x + rw [← Finset.sum_filter,H] + unfold s summesh + exact (Finset.sum_image (by intro i _ j _ he; exact raise_inj _ he)).symm + +lemma summesh_bounds (h : l ≤ M) {A : Form M} {R : ℝ} (hi : 2^(M-l) ≤ R) (hA : S M R ≤ A) : + side (M-l) ≤ summesh h A ∧ summesh h A ≤ 12*side (M-l) := by + rw [summesh_eq] + let r := M-l + have hr : r ≤ M := Nat.sub_le _ _ + let f (x : Grid r) := boltz A (raise hr x) + change side r ≤ expU f ∧ _ + constructor + · have H := Finset.single_le_sum (s:=Finset.univ) (f:=f) + (fun i _ => (boltz_pos _ _).le) (Finset.mem_univ (0 : Grid r)) + have Hz : f 0 = 1 := by simp [f,boltz,form_at_zero] + unfold expU + rw [smul_eq_mul] + rw [Hz] at H + have := side_pos r; nlinarith + · let g : ℝ := 2^r + have hg : 0 ≤ g := by unfold g; positivity + have he (x : Grid r) : + form (S M g) (raise hr x) = form (S r g) x := by + simp_rw [form_S,cost,wave_at_one,raised_pos] + apply le_trans (b:=q (S r g) 0) + · rw [q_origin,expU_ex,expU_ex] + exact Ex_mono _ fun i => exp_monotone <| neg_le_neg <| (he i).ge.trans <| + form_mono ((S_mono hg hi).trans hA) _ + · have HH := q_S_upper' g (by unfold g; positivity) (M:=r) le_rfl + rwa [side_pow_two,← div_eq_mul_inv] + +lemma S_one_pos {R : ℝ} (h : 1 ≤ R) : 0 < S M R 1 := by + simp [S]; nlinarith + +def fq (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) : Law (Grid l) := + proportional (fun i : Grid l => q A (raise hh i)) (fun _ => q_nonneg hA _) (by + have H := Finset.single_le_sum (s:=Finset.univ) (f:=fun i => q A (raise hh i)) + (fun i _ => q_nonneg hA _) (Finset.mem_univ (0:Grid l)) + rw [map_zero] at H + exact lt_of_lt_of_le (q_origin_pos A) H) + +lemma fq_apply (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) (i : Grid l) : + fq hh A hA i = q A (raise hh i) / summesh hh A := rfl + +def fq_cost (hh : l ≤ M) (A : Form M) (i : Grid l) := -log (cred A (raise hh i)) + +def raiseChar (hh : l ≤ M) (t : Grid M) : AddChar (Grid l) ℂ := + phaseChar (cut hh t) + +lemma raiseChar_eq (hh : l ≤ M) (t : Grid M) (i : Grid l) : + raiseChar hh t i = chi (raise hh i) t := by rw [cut_chi,chi_comm]; rfl + +lemma fq_spec (hh : l ≤ M) (A : Form M) (hA : 0 ≤ A) + {R : ℝ} (hr : 2^(M-l) ≤ R) (h : S M R ≤ A) : + let p := fq hh A hA + let c := fq_cost hh A + (∀ x, 0 < p x) ∧ (∀ x, 0 ≤ c x) ∧ c 0=0 ∧ + (∀ x, (↑(exp (-c x)):ℂ)=(label A).Cx (fun t => raiseChar hh t x)) ∧ + (∀ x, -log (p x) = -log (p 0) + c x) ∧ + -log (p 0) ≤ F A - log (2^(M-l):ℝ)+12 := by + have hi := summesh_bounds hh hr h + have hp : 0 < summesh hh A := lt_of_lt_of_le (side_pos _) hi.1 + have hz (x : Grid M) : 0 < q A x := q_pos_one hA + (lt_of_lt_of_le (S_one_pos (le_trans (one_le_pow₀ (by norm_num : (1:ℝ)≤2)) hr)) (h _)) x + have he (x : Grid M) : 0 < cred A x := div_pos (hz x) (hz 0) + refine ⟨fun x => div_pos (hz _) hp,fun x => neg_nonneg.mpr ?_,?_,?_,?_,?_⟩ + · apply Real.log_nonpos (he _).le + exact (le_abs_self _).trans (cred_norm _ _) + · simp [fq_cost,cred,(hz _).ne'] + · intro x + simp_rw [fq_cost,neg_neg,exp_log (he _),raiseChar_eq] + exact cred_eq _ _ + · intro x + simp_rw [fq_apply,Real.log_div (hz _).ne' hp.ne',fq_cost,map_zero,cred, + Real.log_div (hz _).ne' (hz _).ne'] + ring + · rw [fq_apply,map_zero,Real.log_div (hz _).ne' hp.ne'] + have HH := log_le_log hp hi.2 + rw [log_mul (by norm_num) (side_pos _).ne', side_pow_two, Real.log_inv] at HH + unfold F + linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<12)] + +/-- Snap parameter l associated with a node and h. -/ +def snapExp (j : ℕ) (p : Node) := + p.lev-(40*ell j+6) +def snapGrid (j : ℕ) (p : Node) (M : ℕ) := M-snapExp j p +def sig (j : ℕ) := 1/(slow j:ℝ)^40 +def sepRad (j : ℕ) := 1/(slow j:ℝ)^30 + +lemma snap_le (j : ℕ) (p : Node) : snapGrid j p M ≤ M := Nat.sub_le _ _ +lemma step_scale (j : ℕ) {p : Node} (hp : p.lev ≤ M) : + let l := snapGrid j p M + M-l ≤ p.lev ∧ (2:ℝ)^(M-l) ≤ p.invlen ∧ + p.invlen ≤ 64*(slow j:ℝ)^40 * 2^(M-l) ∧ + ∀ u : Grid M, rad (u-raise (snap_le j p) (down (snap_le j p) u)) ≤ sig j*p.invlen/20 := by + intro l + let n := M-l + have he : n = p.lev - (40*ell j+6) := by dsimp only [n,l,snapGrid,snapExp]; omega + have hn : n ≤ p.lev := by omega + have ht : (slow j:ℝ)^40*64 = (2:ℝ)^(40*ell j+6) := by + simp only [slow,Nat.cast_pow,Nat.cast_ofNat] + rw [← pow_mul,pow_add]; norm_num; rw [mul_comm (ell j)] + refine ⟨hn,pow_le_pow_right₀ (by norm_num) hn,?_,?_⟩ + · rw [mul_comm 64,ht,← pow_add] + exact pow_le_pow_right₀ (by norm_num) (by omega) + intro u + have HH := snap_dist (snap_le (M:=M) j p) u + apply HH.trans + have hc : 0 < (slow j:ℝ)^40 := pow_pos (by exact_mod_cast slow_pos j) _ + by_cases hd : n=0 + · change (2:ℝ)^n-1 ≤ _ + rw [hd,pow_zero,sub_self] + have := p.invlen_pos; unfold sig; positivity + have hv : p.invlen = (2:ℝ)^n * ((slow j:ℝ)^40*64) := by + rw [ht,← pow_add]; unfold invlen + congr 1; omega + rw [hv,sig] + change (2:ℝ)^n-1 ≤ _ + have hs : 0 < (slow j:ℝ) := by exact_mod_cast slow_pos j + field_simp + have H : 0 < (2:ℝ)^n := by positivity + nlinarith + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/History.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/History.lean index 568e33f9939583273dce9d1fc9e52a672acf1cb6..f0bb12b946dfe7a751601b128f10af03418ea30c 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/History.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/History.lean @@ -1,227 +1,227 @@ -import OAI.Analysis.FourierLLogL.Thermal.Spatial -import OAI.Analysis.FourierLLogL.Dyadic.Node - -/-! # Thermal certificates and tightening -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Node Law Period Real - -variable {M : ℕ} - -def place (p : Node) (M : ℕ) := S M p.invlen - -lemma place_valid (p : Node) (M : ℕ) : 0 ≤ place p M := S_nonneg _ _ - -lemma loc_scale {p : Node} (hp : p.lev ≤ M) : invlen p ≤ 2^M := pow_le_pow_right₀ (by norm_num) hp - -lemma place_internal {p : Node} {x y : Grid M} (hx : x ∈ p.block M) (hy : y ∈ p.block M) : - form (place p M) (x-y) ≤ 32 := by - have hi := diam_block hx hy - have hh := (diam_le_diff _ _).trans hi - unfold place; rw [form_S] - calc - _ ≤ p.invlen^2 * (32 * Dyadic.diam (x-y)^2) := by gcongr; exact (cost_diam _).2 - _ ≤ p.invlen^2 * (32 * p.len^2) := by gcongr; exact diam_nonneg _ - _ = 32 := by rw [rinv]; field_simp [p.len_pos.ne'] - -/-- A plate distributes the free energy cost over a long range of factors. -/ -def flat (h err : ℝ) (A D : Form M) : Prop := - F (A+(h^140 : ℝ) • D)-F (A+(2⁻¹:ℝ) • D) ≤ err - -/-- certificate of a main form. n is the addition count and p the latest interval. -/ -structure Certified (h err : ℝ) (n : ℕ) (p : Node) (A : Form M) : Prop where - fits : p.ok M - nonneg : 0 ≤ A - prev : (p=root ∧ A=0) ∨ place p M ≤ A - tighten : F ((h^130:ℝ) • A) - F A ≤ n*(err + 1000*h) - energy : (label A).Ex (form A) ≤ n*(2*err+100) - -variable {h err : ℝ} -lemma certified_start (M : ℕ) : Certified h err 0 root (0:Form M) := by - refine ⟨root_ok _,le_rfl,Or.inl ⟨rfl,rfl⟩,by simp,?_⟩ - rw [show form (0:Form M) = fun _ => 0 from funext form_zero,Ex_const] - simp - -lemma certified_add {A D : Form M} {p q : Node} {n : ℕ} (H : Certified h err n p A) - (hh : 1 ≤ h) (hq : q.ok M) - (hd : 0 ≤ D) (hF : flat h err (A+place q M) D) : - Certified h err (n+1) q ((A+place q M)+D) := by - let B := place q M - let U := A+B+D - let w := h^130 - have hp : 0 < h := by linarith - have hw : 1 ≤ w := one_le_pow₀ hh - have hB : 0 ≤ B := place_valid _ _ - have hA := H.nonneg - have hAB := add_nonneg hA hB - have hAU : A ≤ U := by - apply le_trans (le_add_of_nonneg_right hB) (le_add_of_nonneg_right hd) - have hBU : B ≤ U := by - apply le_trans (le_add_of_nonneg_left hA) (le_add_of_nonneg_right hd) - refine ⟨hq, hA.trans hAU, Or.inr hBU, ?_, ?_⟩ - · - let D' := w • D-D - have hw' {E : Form M} (h : 0 ≤ E) : E ≤ w • E := by - intro x - exact le_mul_of_one_le_left (h x) hw - have hD' : 0 ≤ D' := sub_nonneg.mpr (hw' hd) - let V := w • A + w • B + D - have heq : w • U = V+D' := by unfold U V D'; rw [smul_add,smul_add]; abel - have h1 : U ≤ V := add_le_add_left (add_le_add (hw' hA) (hw' hB)) _ - have h2 : F (V+D')-F V ≤ err := by - apply le_trans (F_increment (hA.trans hAU) h1 hD') - have hh1 : U+D' ≤ A+B+(h^140:ℝ) • D := by - have hP : w ≤ h^140 := pow_le_pow_right₀ hh (by omega) - unfold U D' - rw [show A+B+D+(w • D-D) = A+B+w • D by abel] - refine add_le_add_right ?_ _ - intro x; exact mul_le_mul_of_nonneg_right hP (hd x) - have hh2 : A+B + (2⁻¹:ℝ) • D ≤ U := by - refine add_le_add_right ?_ _ - intro x; change 2⁻¹*D x ≤ D x; have hx := hd x; change 0 ≤ D x at hx; norm_num; linarith - exact le_trans (sub_le_sub (F_mono hh1) (F_mono hh2)) hF - let W := w • A+B+D - have h3 : F W-F U ≤ F (w • A)-F A := by - have ht := F_increment hA hAU (sub_nonneg.mpr (hw' hA)) - rw [show A+(w • A - A) = w • A by abel, show U+(w • A-A)=W by unfold U W; abel] at ht - exact ht - have h4 : F V-F W ≤ F (w • B)- F B := by - have hBW : B ≤ W := le_trans hBU (add_le_add_left (add_le_add_left (hw' hA) _) _) - have ht := F_increment hB hBW (sub_nonneg.mpr (hw' hB)) - rw [show B+(w • B - B) = w • B by abel, show W+(w • B-B)=V by unfold V W; abel] at ht - exact ht - have h5 : F (w • B) - F B ≤ 1000*h := by - have he : w • B = S M (h^65 * q.invlen) := by - rw [show w = (h^65)^2 by unfold w; rw [← pow_mul]]; unfold B place; rw [scale_S] - have hs := one_le_pow₀ hh (n:=65) - have hc := q.invlen_ge - have hp' := q.invlen_pos - rw [he] - have H1 := F_S_upper M _ (one_le_mul_of_one_le_of_one_le hs hc) - have H2 := F_S_lower _ hp' (loc_scale hq.2) - rw [log_mul (by positivity) hp'.ne',log_pow] at H1 - unfold B place; push_cast at * - linarith [Real.log_le_sub_one_of_pos hp] - have H6 := H.tighten - change F (w • U) - F U ≤ _ - change F (w • A)-F A ≤ _ at H6 - rw [heq] - push_cast; linarith - - let L := (2⁻¹:ℝ) • D - have heq : U = A+B+L+L := by - unfold L U - rw [add_assoc (A+B) _ _,← add_smul] - norm_num - have h1 : F U-F (A+B+L) ≤ err := by - have he : D ≤ h^140 • D := fun x => le_mul_of_one_le_left (hd x) (one_le_pow₀ hh) - exact (sub_le_sub_right (F_mono (add_le_add_right he (A+B))) _).trans hF - have hG : (label U).Ex (form D) ≤ 2*err := by - have hh1 := mean_energy (A+B+L) L - rw [← heq] at hh1 - have h2 (x : Grid M) : form L x = 2⁻¹ * form D x := form_smul _ _ _ - rw [funext h2,Ex_lmul] at hh1 - norm_num at hh1; linarith - have hG' : (label U).Ex (form B) ≤ 100 := - S_energy q.invlen q.invlen_ge (loc_scale hq.2) U hBU - change (label U).Ex (form U) ≤ _ - have h3 (x : Grid M) : form U x = form A x+form B x+form D x := by - unfold U; rw [form_add,form_add] - rw [funext h3,Ex_add,Ex_add] - have hh1 := form_energy_mono hA hAU hA - have hh2 := H.energy - push_cast; linarith - -/-- tightening used by compression and evidence posterior bounds. -/ -def tilde (h : ℝ) (A : Form M) (p : Node) : Form M := - h^130 • A + S M (1000*p.invlen) - -lemma tilde_le {h : ℝ} (hh : 1 ≤ h) {A : Form M} (hA : 0 ≤ A) (p : Node) : - A+place p M ≤ tilde h A p := by - apply add_le_add _ (S_mono p.invlen_pos.le (by linarith [p.invlen_pos])) - intro i; exact le_mul_of_one_le_left (hA i) (one_le_pow₀ hh) - -lemma tilde_tight {n : ℕ} {A : Form M} {p q : Node} (H : Certified h err n p A) - (hh : 1 ≤ h) (hq : q.ok M) : - F (tilde h A q)-F (A + place q M) ≤ n*(err+1000*h)+2000 := by - let B := place q M - let A' := h^130 • A - let B' := S M (1000*q.invlen) - have hA := H.nonneg - have hB : 0 ≤ B := place_valid _ _ - have hA' : A ≤ A' := fun i => le_mul_of_one_le_left (hA i) (one_le_pow₀ hh) - have hB' : B ≤ B' := S_mono q.invlen_pos.le (by linarith [q.invlen_pos]) - have hX := F_increment hA hA' hB - have hY := F_increment hB hB' (hA.trans hA') - have hc := q.invlen_ge - have hc' := q.invlen_pos - have hZ : F B'-F B ≤ 2000 := by - have h1 := F_S_upper M (1000*q.invlen) (by linarith) - have h2 := F_S_lower _ hc' (loc_scale hq.2) - rw [log_mul (by norm_num) hc'.ne'] at h1 - unfold B B' place - linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<1000)] - have HH := H.tighten - rw [add_comm B _,add_comm B' _] at hY - change F A'-_ ≤ _ at HH - change F (A'+B')-F (A+B) ≤ _ - linarith - -lemma space_increment {n : ℕ} {A : Form M} {p q' : Node} (H : Certified h err n p A) - (h : q'.below p) (hq : q'.ok M) : - F (A+place q' M)-F A ≤ log q'.invlen - log p.invlen + 3000 := by - let B := place q' M - have hh := scale_below h - have hn := q'.invlen_ge - have hp := q'.invlen_pos - have hz := p.invlen_pos - rcases H.prev with ⟨he,h'⟩|he - · rw [h',he,F_zero,zero_add,invlen_top,log_one] - unfold place - linarith [F_S_upper M _ hn] - let r := p.invlen/20 - let s := arc M r - have hr : 0 < r := by unfold r; positivity - have hs : r ≤ s.card := arc_card_lo _ _ hr.le (by - unfold r; linarith [loc_scale H.fits.2]) - have hB := place_valid q' M - let L := (1/120:ℝ) - have hl : 0 < L := by unfold L; norm_num - have hx1 (i : Grid M) (hi : i ∈ s) : - q A 0 * (L/q'.invlen) ≤ q A i * q B (-i) := by - have hx := (mem_arc _ _).mp hi - have h1 := cred_short _ p.invlen_ge (loc_scale H.fits.2) _ he i hx - have h2 := q_short _ hn (loc_scale hq.2) i (by unfold r at hx; linarith) - calc - _ = (exp (-F A)*(1/2))*((1/60)/q'.invlen) := by rw [F_origin]; unfold L; ring - _ ≤ (exp (-F A)*cred A i)* q B i := - mul_le_mul (by gcongr) h2 (by positivity) (mul_nonneg - (Real.exp_nonneg _) (cred_nonneg H.nonneg _)) - _ = _ := by rw [← q_express, q_even _ _] - have HX : - q A 0 * ((p.invlen / q'.invlen) / 2400) ≤ q (A+B) 0 := by - have ha := q_origin_pos A - rw [q_add]; simp_rw [zero_sub] - calc - _ = r * (q A 0 * (L/q'.invlen)) := by unfold r L; ring - _ ≤ ↑s.card*(q A 0 * (L/q'.invlen)) := by gcongr - _ = _ := (show ∑ _i ∈ s, (q A 0 * (L/q'.invlen)) = _ by simp).symm - _ ≤ ∑ i ∈ s, q A i * q B (-i) := Finset.sum_le_sum hx1 - _ ≤ ∑ i, q A i * q B (-i) := - Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) - (fun i _ _ => mul_nonneg (q_nonneg H.nonneg _) (q_nonneg hB _)) - have hhA := q_origin_pos A - have Hl := log_le_log (by positivity) HX - rw [log_mul (by positivity) (by positivity),log_div (by positivity) (by norm_num), - log_div (by positivity) (by positivity)] at Hl - change F (A+B)-_ ≤ _ - unfold F - linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2400)] - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Spatial +import OAI.Analysis.FourierLLogL.Dyadic.Node + +/-! # Thermal certificates and tightening -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Node Law Period Real + +variable {M : ℕ} + +def place (p : Node) (M : ℕ) := S M p.invlen + +lemma place_valid (p : Node) (M : ℕ) : 0 ≤ place p M := S_nonneg _ _ + +lemma loc_scale {p : Node} (hp : p.lev ≤ M) : invlen p ≤ 2^M := pow_le_pow_right₀ (by norm_num) hp + +lemma place_internal {p : Node} {x y : Grid M} (hx : x ∈ p.block M) (hy : y ∈ p.block M) : + form (place p M) (x-y) ≤ 32 := by + have hi := diam_block hx hy + have hh := (diam_le_diff _ _).trans hi + unfold place; rw [form_S] + calc + _ ≤ p.invlen^2 * (32 * Dyadic.diam (x-y)^2) := by gcongr; exact (cost_diam _).2 + _ ≤ p.invlen^2 * (32 * p.len^2) := by gcongr; exact diam_nonneg _ + _ = 32 := by rw [rinv]; field_simp [p.len_pos.ne'] + +/-- A plate distributes the free energy cost over a long range of factors. -/ +def flat (h err : ℝ) (A D : Form M) : Prop := + F (A+(h^140 : ℝ) • D)-F (A+(2⁻¹:ℝ) • D) ≤ err + +/-- certificate of a main form. n is the addition count and p the latest interval. -/ +structure Certified (h err : ℝ) (n : ℕ) (p : Node) (A : Form M) : Prop where + fits : p.ok M + nonneg : 0 ≤ A + prev : (p=root ∧ A=0) ∨ place p M ≤ A + tighten : F ((h^130:ℝ) • A) - F A ≤ n*(err + 1000*h) + energy : (label A).Ex (form A) ≤ n*(2*err+100) + +variable {h err : ℝ} +lemma certified_start (M : ℕ) : Certified h err 0 root (0:Form M) := by + refine ⟨root_ok _,le_rfl,Or.inl ⟨rfl,rfl⟩,by simp,?_⟩ + rw [show form (0:Form M) = fun _ => 0 from funext form_zero,Ex_const] + simp + +lemma certified_add {A D : Form M} {p q : Node} {n : ℕ} (H : Certified h err n p A) + (hh : 1 ≤ h) (hq : q.ok M) + (hd : 0 ≤ D) (hF : flat h err (A+place q M) D) : + Certified h err (n+1) q ((A+place q M)+D) := by + let B := place q M + let U := A+B+D + let w := h^130 + have hp : 0 < h := by linarith + have hw : 1 ≤ w := one_le_pow₀ hh + have hB : 0 ≤ B := place_valid _ _ + have hA := H.nonneg + have hAB := add_nonneg hA hB + have hAU : A ≤ U := by + apply le_trans (le_add_of_nonneg_right hB) (le_add_of_nonneg_right hd) + have hBU : B ≤ U := by + apply le_trans (le_add_of_nonneg_left hA) (le_add_of_nonneg_right hd) + refine ⟨hq, hA.trans hAU, Or.inr hBU, ?_, ?_⟩ + · + let D' := w • D-D + have hw' {E : Form M} (h : 0 ≤ E) : E ≤ w • E := by + intro x + exact le_mul_of_one_le_left (h x) hw + have hD' : 0 ≤ D' := sub_nonneg.mpr (hw' hd) + let V := w • A + w • B + D + have heq : w • U = V+D' := by unfold U V D'; rw [smul_add,smul_add]; abel + have h1 : U ≤ V := add_le_add_left (add_le_add (hw' hA) (hw' hB)) _ + have h2 : F (V+D')-F V ≤ err := by + apply le_trans (F_increment (hA.trans hAU) h1 hD') + have hh1 : U+D' ≤ A+B+(h^140:ℝ) • D := by + have hP : w ≤ h^140 := pow_le_pow_right₀ hh (by omega) + unfold U D' + rw [show A+B+D+(w • D-D) = A+B+w • D by abel] + refine add_le_add_right ?_ _ + intro x; exact mul_le_mul_of_nonneg_right hP (hd x) + have hh2 : A+B + (2⁻¹:ℝ) • D ≤ U := by + refine add_le_add_right ?_ _ + intro x; change 2⁻¹*D x ≤ D x; have hx := hd x; change 0 ≤ D x at hx; norm_num; linarith + exact le_trans (sub_le_sub (F_mono hh1) (F_mono hh2)) hF + let W := w • A+B+D + have h3 : F W-F U ≤ F (w • A)-F A := by + have ht := F_increment hA hAU (sub_nonneg.mpr (hw' hA)) + rw [show A+(w • A - A) = w • A by abel, show U+(w • A-A)=W by unfold U W; abel] at ht + exact ht + have h4 : F V-F W ≤ F (w • B)- F B := by + have hBW : B ≤ W := le_trans hBU (add_le_add_left (add_le_add_left (hw' hA) _) _) + have ht := F_increment hB hBW (sub_nonneg.mpr (hw' hB)) + rw [show B+(w • B - B) = w • B by abel, show W+(w • B-B)=V by unfold V W; abel] at ht + exact ht + have h5 : F (w • B) - F B ≤ 1000*h := by + have he : w • B = S M (h^65 * q.invlen) := by + rw [show w = (h^65)^2 by unfold w; rw [← pow_mul]]; unfold B place; rw [scale_S] + have hs := one_le_pow₀ hh (n:=65) + have hc := q.invlen_ge + have hp' := q.invlen_pos + rw [he] + have H1 := F_S_upper M _ (one_le_mul_of_one_le_of_one_le hs hc) + have H2 := F_S_lower _ hp' (loc_scale hq.2) + rw [log_mul (by positivity) hp'.ne',log_pow] at H1 + unfold B place; push_cast at * + linarith [Real.log_le_sub_one_of_pos hp] + have H6 := H.tighten + change F (w • U) - F U ≤ _ + change F (w • A)-F A ≤ _ at H6 + rw [heq] + push_cast; linarith + + let L := (2⁻¹:ℝ) • D + have heq : U = A+B+L+L := by + unfold L U + rw [add_assoc (A+B) _ _,← add_smul] + norm_num + have h1 : F U-F (A+B+L) ≤ err := by + have he : D ≤ h^140 • D := fun x => le_mul_of_one_le_left (hd x) (one_le_pow₀ hh) + exact (sub_le_sub_right (F_mono (add_le_add_right he (A+B))) _).trans hF + have hG : (label U).Ex (form D) ≤ 2*err := by + have hh1 := mean_energy (A+B+L) L + rw [← heq] at hh1 + have h2 (x : Grid M) : form L x = 2⁻¹ * form D x := form_smul _ _ _ + rw [funext h2,Ex_lmul] at hh1 + norm_num at hh1; linarith + have hG' : (label U).Ex (form B) ≤ 100 := + S_energy q.invlen q.invlen_ge (loc_scale hq.2) U hBU + change (label U).Ex (form U) ≤ _ + have h3 (x : Grid M) : form U x = form A x+form B x+form D x := by + unfold U; rw [form_add,form_add] + rw [funext h3,Ex_add,Ex_add] + have hh1 := form_energy_mono hA hAU hA + have hh2 := H.energy + push_cast; linarith + +/-- tightening used by compression and evidence posterior bounds. -/ +def tilde (h : ℝ) (A : Form M) (p : Node) : Form M := + h^130 • A + S M (1000*p.invlen) + +lemma tilde_le {h : ℝ} (hh : 1 ≤ h) {A : Form M} (hA : 0 ≤ A) (p : Node) : + A+place p M ≤ tilde h A p := by + apply add_le_add _ (S_mono p.invlen_pos.le (by linarith [p.invlen_pos])) + intro i; exact le_mul_of_one_le_left (hA i) (one_le_pow₀ hh) + +lemma tilde_tight {n : ℕ} {A : Form M} {p q : Node} (H : Certified h err n p A) + (hh : 1 ≤ h) (hq : q.ok M) : + F (tilde h A q)-F (A + place q M) ≤ n*(err+1000*h)+2000 := by + let B := place q M + let A' := h^130 • A + let B' := S M (1000*q.invlen) + have hA := H.nonneg + have hB : 0 ≤ B := place_valid _ _ + have hA' : A ≤ A' := fun i => le_mul_of_one_le_left (hA i) (one_le_pow₀ hh) + have hB' : B ≤ B' := S_mono q.invlen_pos.le (by linarith [q.invlen_pos]) + have hX := F_increment hA hA' hB + have hY := F_increment hB hB' (hA.trans hA') + have hc := q.invlen_ge + have hc' := q.invlen_pos + have hZ : F B'-F B ≤ 2000 := by + have h1 := F_S_upper M (1000*q.invlen) (by linarith) + have h2 := F_S_lower _ hc' (loc_scale hq.2) + rw [log_mul (by norm_num) hc'.ne'] at h1 + unfold B B' place + linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<1000)] + have HH := H.tighten + rw [add_comm B _,add_comm B' _] at hY + change F A'-_ ≤ _ at HH + change F (A'+B')-F (A+B) ≤ _ + linarith + +lemma space_increment {n : ℕ} {A : Form M} {p q' : Node} (H : Certified h err n p A) + (h : q'.below p) (hq : q'.ok M) : + F (A+place q' M)-F A ≤ log q'.invlen - log p.invlen + 3000 := by + let B := place q' M + have hh := scale_below h + have hn := q'.invlen_ge + have hp := q'.invlen_pos + have hz := p.invlen_pos + rcases H.prev with ⟨he,h'⟩|he + · rw [h',he,F_zero,zero_add,invlen_top,log_one] + unfold place + linarith [F_S_upper M _ hn] + let r := p.invlen/20 + let s := arc M r + have hr : 0 < r := by unfold r; positivity + have hs : r ≤ s.card := arc_card_lo _ _ hr.le (by + unfold r; linarith [loc_scale H.fits.2]) + have hB := place_valid q' M + let L := (1/120:ℝ) + have hl : 0 < L := by unfold L; norm_num + have hx1 (i : Grid M) (hi : i ∈ s) : + q A 0 * (L/q'.invlen) ≤ q A i * q B (-i) := by + have hx := (mem_arc _ _).mp hi + have h1 := cred_short _ p.invlen_ge (loc_scale H.fits.2) _ he i hx + have h2 := q_short _ hn (loc_scale hq.2) i (by unfold r at hx; linarith) + calc + _ = (exp (-F A)*(1/2))*((1/60)/q'.invlen) := by rw [F_origin]; unfold L; ring + _ ≤ (exp (-F A)*cred A i)* q B i := + mul_le_mul (by gcongr) h2 (by positivity) (mul_nonneg + (Real.exp_nonneg _) (cred_nonneg H.nonneg _)) + _ = _ := by rw [← q_express, q_even _ _] + have HX : + q A 0 * ((p.invlen / q'.invlen) / 2400) ≤ q (A+B) 0 := by + have ha := q_origin_pos A + rw [q_add]; simp_rw [zero_sub] + calc + _ = r * (q A 0 * (L/q'.invlen)) := by unfold r L; ring + _ ≤ ↑s.card*(q A 0 * (L/q'.invlen)) := by gcongr + _ = _ := (show ∑ _i ∈ s, (q A 0 * (L/q'.invlen)) = _ by simp).symm + _ ≤ ∑ i ∈ s, q A i * q B (-i) := Finset.sum_le_sum hx1 + _ ≤ ∑ i, q A i * q B (-i) := + Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) + (fun i _ _ => mul_nonneg (q_nonneg H.nonneg _) (q_nonneg hB _)) + have hhA := q_origin_pos A + have Hl := log_le_log (by positivity) HX + rw [log_mul (by positivity) (by positivity),log_div (by positivity) (by norm_num), + log_div (by positivity) (by positivity)] at Hl + change F (A+B)-_ ≤ _ + unfold F + linarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2400)] + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Levels.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Levels.lean index 283252e3540d9616926600a789a16e5ea3134070..dfad3635c597336e74cae238e3ecfd007019ad1d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Levels.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Levels.lean @@ -1,168 +1,168 @@ -import OAI.Analysis.FourierLLogL.Thermal.GridFreq - -/-! # Asymptotic scales for the dyadic bound -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -def H (j : ℕ) : ℝ := slow j - -def plateErr (j : ℕ) : ℝ := height j/H j^400 -/-- Index depth i denotes b=2^{-i}; b₀=h^{-16}. -/ -def mainIx (j : ℕ) := 16*ell j -def testIx (j : ℕ) := mainIx j+3 -def beta (i : ℕ) := side i^2/100 -/-- Linear growth is enough for the normalization coding sizes -/ -def kappa (j : ℕ) (A0 : ℝ) (i : ℕ) := A0 * height j / H j * (1+i) -def eps : ℝ := 1/10000 - -lemma H_ge (j : ℕ) : 1 ≤ H j := by unfold H; exact_mod_cast slow_pos j -lemma H_pos (j : ℕ) : 0 < H j := lt_of_lt_of_le zero_lt_one (H_ge j) -lemma d_pos (j : ℕ) : 0 < height j := by unfold height; positivity -lemma beta_pos (i : ℕ) : 0 < beta i := by unfold beta; have := side_pos i; positivity -lemma beta_le (i : ℕ) : beta i ≤ 1 := by unfold beta; nlinarith [side_le i, side_pos i] -lemma eps_pos : 0 < eps := by norm_num [eps] -lemma kappa_pos (A0 : ℝ) (hA : 0 < A0) (j i : ℕ) : 0 < kappa j A0 i := by - unfold kappa; have := H_pos j; have := d_pos j; positivity - -lemma beta_log (i : ℕ) : -log (beta i) ≤ 100 + 2*i := by - rw [beta,Real.log_div (by have := side_pos i; positivity) (by norm_num), - side_pow_two,Real.log_pow,Real.log_inv,Real.log_pow] - push_cast - nlinarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), - Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<100),Nat.cast_nonneg (α:=ℝ) i] - -lemma h_large (A : ℝ) : ∀ᶠ j : ℕ in atTop, A ≤ H j := by - obtain ⟨n,hn⟩ := exists_nat_gt A - filter_upwards [slow_tendsto.eventually (eventually_ge_atTop n)] with j hj - exact hn.le.trans (show (n:ℝ)≤ slow j from Nat.cast_le.mpr hj) - -lemma hpow_index (A : ℝ) (n : ℕ) : ∀ᶠ j : ℕ in atTop, A*H j^n ≤ (j:ℝ) := by - filter_upwards [wide_le_j,eventually_pow_small_slow n A] with j hj h - exact h.trans (by exact_mod_cast hj) - -lemma hpoly (A : ℝ) (n : ℕ) : - ∀ᶠ j : ℕ in atTop, A*H j^n ≤ height j := by - filter_upwards [hpow_index A n, eventually_pow_small 1 1] with j hh h - exact hh.trans (by simp only [pow_one,one_mul] at h; exact h) - -lemma hpoly_mix (A : ℝ) (n m : ℕ) : - ∀ᶠ j : ℕ in atTop, A*H j^n*(j:ℝ)^m ≤ height j := by - filter_upwards [hpow_index A n,eventually_pow_small (m+1) 1] with j hh h - refine (mul_le_mul_of_nonneg_right hh (by positivity : 0 ≤ (j:ℝ)^m)).trans ?_ - rwa [pow_succ,one_mul,mul_comm] at h - -lemma bottom_level (j : ℕ) : side (mainIx j) = 1/H j^16 := by - simp only [side_pow_two,H,slow,mainIx,Nat.cast_pow,Nat.cast_ofNat,← pow_mul,one_div,mul_comm 16] - -lemma main_bounds (j : ℕ) {i : ℕ} (hi : i ≤ testIx j) : - ((1/6400)/H j^32 ≤ beta i) ∧ (i:ℝ) ≤ 19*H j ∧ - -log (beta i) ≤ 200*H j := by - have hX : i ≤ 19*slow j := by - have hx := ell_le_h j; have h' := slow_pos j - unfold testIx mainIx at hi; omega - have hx : (i:ℝ) ≤ 19*H j := by unfold H; exact_mod_cast hX - have hn := H_ge j - have hi : side (testIx j) ≤ side i := - pow_le_pow_of_le_one (by norm_num) (by norm_num) hi - have he : side (testIx j) = side (mainIx j)/8 := by simp [side,testIx,pow_add]; ring - refine ⟨?_,hx,by linarith [beta_log i]⟩ - unfold beta - calc - _ = side (testIx j)^2/100 := by rw [he,bottom_level]; unfold H; ring - _ ≤ _ := by have := side_pos (testIx j); gcongr - -lemma sigma_small : - ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → 8*sig j ≤ side i^2 := by - filter_upwards [h_large 1000] with j hj i hi - have hn := (main_bounds j hi).1 - unfold beta at hn - change 8*(1/H j^40) ≤ _ - have he : 8*(1/H j^40) ≤ 100*((1/6400)/H j^32) := by - have hh : H j ≤ H j ^ 8 := by - have := pow_le_pow_right₀ (by linarith : 1 ≤ H j) (show 1 ≤ 8 by omega) - simpa using this - have hc : 512 ≤ H j ^ 8 := by linarith - have := H_pos j - rw [show H j^40=H j^8 * H j^32 by ring] - apply (mul_le_mul_iff_left₀ (show 0 < H j^32 by positivity)).mp - field_simp - nlinarith - linarith - -lemma Tk_bounds : - ∀ᶠ j : ℕ in atTop, - (sLen j:ℝ)* H j ^6 = height j ∧ 0 < sLen j := by - filter_upwards [t_expr] with j hj - constructor - · unfold H height; exact_mod_cast hj.1 - · exact pow_pos_nat _ - -lemma kappa_scales (A0 : ℝ) (hA : 1 ≤ A0) : - ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → - height j/H j^2 ≤ eps*kappa j A0 i ∧ kappa j A0 i ≤ height j ∧ - A0 * height j / H j ≤ kappa j A0 i ∧ 100 ≤ kappa j A0 i := by - - filter_upwards [ell_tendsto.eventually (eventually_pow_small 1 (20*A0)), - ell_tendsto.eventually (eventually_ge_atTop 1),h_large 10000,hpoly 100 2] - with j he he' hj hh i hi - let x := H j; let d := height j - have hx : 10000 ≤ x := hj - have hx' : 0 < x := H_pos j - have hd : 0 < d := d_pos j - have hk3 : A0 * height j / H j ≤ kappa j A0 i := by - unfold kappa; have hp : 0 < A0*d/x := div_pos (mul_pos (by linarith) hd) hx' - exact le_mul_of_one_le_right hp.le (le_add_of_nonneg_right (by positivity)) - refine ⟨le_trans ?_ (mul_le_mul_of_nonneg_left hk3 eps_pos.le),?_,hk3, le_trans ?_ hk3⟩ - · change d/x^2 ≤ 1/10000*(A0*d/x) - apply (div_le_iff₀ (sq_pos_of_pos hx')).mpr - rw [show 1/10000*(A0*d/x)*x^2 = (A0*x/10000)*d by field_simp] - have hi : 10000 ≤ A0*x := by nlinarith - nlinarith - · have hI : i+1 ≤ 20*ell j := by unfold testIx mainIx at hi; omega - have hX : A0*(1+(i:ℝ)) ≤ x := by - have hE : 20*A0*(ell j) ≤ x := by - unfold x H slow; push_cast; simpa using he - have hx2 : (i:ℝ)+1 ≤ 20*ell j := by exact_mod_cast hI - nlinarith - unfold kappa - change A0*d/x*(1+i) ≤ d - rw [div_mul_eq_mul_div] - apply (div_le_iff₀ hx').mpr - exact le_trans (le_of_eq (by ring : A0*d*((1:ℝ)+i)=d*(A0*(1+i)))) - ((mul_le_mul_of_nonneg_left hX hd.le).trans (le_of_eq rfl)) - · change 100 ≤ A0*d/x; apply (le_div_iff₀ hx').mpr - change _ ≤ d at hh - change 100*x^2 ≤ d at hh - nlinarith - -lemma stiff_budget (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, C*H j*(plateErr j + 1000*H j)+2000 ≤ height j/H j^4 := by - filter_upwards [h_large (2*C),hpoly (10000*C) 6] with j h h' - let d := height j; let a := H j - have ha : 2*C ≤ a := h - have Ha : 1 ≤ a := H_ge _ - have ha' : 0 < a := H_pos _ - have hd : 0 < d := d_pos _ - change 10000*C*a^6 ≤ d at h' - change C*a*(d/a^400+1000*a)+2000 ≤ d/a^4 - apply (le_div_iff₀ (pow_pos ha' _)).mpr - have he : a^6 ≤ a^400 := pow_le_pow_right₀ Ha (by omega) - have he1 : C*a*(d/a^400)*a^4 ≤ d/2 := by - calc - _ ≤ (a/2)*a*(d/a^6)*a^4 := by gcongr; linarith - _ = _ := by field_simp - have he2 : C*a*(1000*a)*a^4+2000*a^4 ≤ d/2 := by - rw [show C*a*(1000*a)*a^4=1000*C*a^6 by ring] - have hg : a^4 ≤ C*a^6 := le_trans (pow_le_pow_right₀ Ha (by omega)) - (le_mul_of_one_le_left (by positivity) hC) - linarith - rw [mul_add,add_mul, add_mul]; linarith - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.GridFreq + +/-! # Asymptotic scales for the dyadic bound -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +def H (j : ℕ) : ℝ := slow j + +def plateErr (j : ℕ) : ℝ := height j/H j^400 +/-- Index depth i denotes b=2^{-i}; b₀=h^{-16}. -/ +def mainIx (j : ℕ) := 16*ell j +def testIx (j : ℕ) := mainIx j+3 +def beta (i : ℕ) := side i^2/100 +/-- Linear growth is enough for the normalization coding sizes -/ +def kappa (j : ℕ) (A0 : ℝ) (i : ℕ) := A0 * height j / H j * (1+i) +def eps : ℝ := 1/10000 + +lemma H_ge (j : ℕ) : 1 ≤ H j := by unfold H; exact_mod_cast slow_pos j +lemma H_pos (j : ℕ) : 0 < H j := lt_of_lt_of_le zero_lt_one (H_ge j) +lemma d_pos (j : ℕ) : 0 < height j := by unfold height; positivity +lemma beta_pos (i : ℕ) : 0 < beta i := by unfold beta; have := side_pos i; positivity +lemma beta_le (i : ℕ) : beta i ≤ 1 := by unfold beta; nlinarith [side_le i, side_pos i] +lemma eps_pos : 0 < eps := by norm_num [eps] +lemma kappa_pos (A0 : ℝ) (hA : 0 < A0) (j i : ℕ) : 0 < kappa j A0 i := by + unfold kappa; have := H_pos j; have := d_pos j; positivity + +lemma beta_log (i : ℕ) : -log (beta i) ≤ 100 + 2*i := by + rw [beta,Real.log_div (by have := side_pos i; positivity) (by norm_num), + side_pow_two,Real.log_pow,Real.log_inv,Real.log_pow] + push_cast + nlinarith [Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), + Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<100),Nat.cast_nonneg (α:=ℝ) i] + +lemma h_large (A : ℝ) : ∀ᶠ j : ℕ in atTop, A ≤ H j := by + obtain ⟨n,hn⟩ := exists_nat_gt A + filter_upwards [slow_tendsto.eventually (eventually_ge_atTop n)] with j hj + exact hn.le.trans (show (n:ℝ)≤ slow j from Nat.cast_le.mpr hj) + +lemma hpow_index (A : ℝ) (n : ℕ) : ∀ᶠ j : ℕ in atTop, A*H j^n ≤ (j:ℝ) := by + filter_upwards [wide_le_j,eventually_pow_small_slow n A] with j hj h + exact h.trans (by exact_mod_cast hj) + +lemma hpoly (A : ℝ) (n : ℕ) : + ∀ᶠ j : ℕ in atTop, A*H j^n ≤ height j := by + filter_upwards [hpow_index A n, eventually_pow_small 1 1] with j hh h + exact hh.trans (by simp only [pow_one,one_mul] at h; exact h) + +lemma hpoly_mix (A : ℝ) (n m : ℕ) : + ∀ᶠ j : ℕ in atTop, A*H j^n*(j:ℝ)^m ≤ height j := by + filter_upwards [hpow_index A n,eventually_pow_small (m+1) 1] with j hh h + refine (mul_le_mul_of_nonneg_right hh (by positivity : 0 ≤ (j:ℝ)^m)).trans ?_ + rwa [pow_succ,one_mul,mul_comm] at h + +lemma bottom_level (j : ℕ) : side (mainIx j) = 1/H j^16 := by + simp only [side_pow_two,H,slow,mainIx,Nat.cast_pow,Nat.cast_ofNat,← pow_mul,one_div,mul_comm 16] + +lemma main_bounds (j : ℕ) {i : ℕ} (hi : i ≤ testIx j) : + ((1/6400)/H j^32 ≤ beta i) ∧ (i:ℝ) ≤ 19*H j ∧ + -log (beta i) ≤ 200*H j := by + have hX : i ≤ 19*slow j := by + have hx := ell_le_h j; have h' := slow_pos j + unfold testIx mainIx at hi; omega + have hx : (i:ℝ) ≤ 19*H j := by unfold H; exact_mod_cast hX + have hn := H_ge j + have hi : side (testIx j) ≤ side i := + pow_le_pow_of_le_one (by norm_num) (by norm_num) hi + have he : side (testIx j) = side (mainIx j)/8 := by simp [side,testIx,pow_add]; ring + refine ⟨?_,hx,by linarith [beta_log i]⟩ + unfold beta + calc + _ = side (testIx j)^2/100 := by rw [he,bottom_level]; unfold H; ring + _ ≤ _ := by have := side_pos (testIx j); gcongr + +lemma sigma_small : + ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → 8*sig j ≤ side i^2 := by + filter_upwards [h_large 1000] with j hj i hi + have hn := (main_bounds j hi).1 + unfold beta at hn + change 8*(1/H j^40) ≤ _ + have he : 8*(1/H j^40) ≤ 100*((1/6400)/H j^32) := by + have hh : H j ≤ H j ^ 8 := by + have := pow_le_pow_right₀ (by linarith : 1 ≤ H j) (show 1 ≤ 8 by omega) + simpa using this + have hc : 512 ≤ H j ^ 8 := by linarith + have := H_pos j + rw [show H j^40=H j^8 * H j^32 by ring] + apply (mul_le_mul_iff_left₀ (show 0 < H j^32 by positivity)).mp + field_simp + nlinarith + linarith + +lemma Tk_bounds : + ∀ᶠ j : ℕ in atTop, + (sLen j:ℝ)* H j ^6 = height j ∧ 0 < sLen j := by + filter_upwards [t_expr] with j hj + constructor + · unfold H height; exact_mod_cast hj.1 + · exact pow_pos_nat _ + +lemma kappa_scales (A0 : ℝ) (hA : 1 ≤ A0) : + ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → + height j/H j^2 ≤ eps*kappa j A0 i ∧ kappa j A0 i ≤ height j ∧ + A0 * height j / H j ≤ kappa j A0 i ∧ 100 ≤ kappa j A0 i := by + + filter_upwards [ell_tendsto.eventually (eventually_pow_small 1 (20*A0)), + ell_tendsto.eventually (eventually_ge_atTop 1),h_large 10000,hpoly 100 2] + with j he he' hj hh i hi + let x := H j; let d := height j + have hx : 10000 ≤ x := hj + have hx' : 0 < x := H_pos j + have hd : 0 < d := d_pos j + have hk3 : A0 * height j / H j ≤ kappa j A0 i := by + unfold kappa; have hp : 0 < A0*d/x := div_pos (mul_pos (by linarith) hd) hx' + exact le_mul_of_one_le_right hp.le (le_add_of_nonneg_right (by positivity)) + refine ⟨le_trans ?_ (mul_le_mul_of_nonneg_left hk3 eps_pos.le),?_,hk3, le_trans ?_ hk3⟩ + · change d/x^2 ≤ 1/10000*(A0*d/x) + apply (div_le_iff₀ (sq_pos_of_pos hx')).mpr + rw [show 1/10000*(A0*d/x)*x^2 = (A0*x/10000)*d by field_simp] + have hi : 10000 ≤ A0*x := by nlinarith + nlinarith + · have hI : i+1 ≤ 20*ell j := by unfold testIx mainIx at hi; omega + have hX : A0*(1+(i:ℝ)) ≤ x := by + have hE : 20*A0*(ell j) ≤ x := by + unfold x H slow; push_cast; simpa using he + have hx2 : (i:ℝ)+1 ≤ 20*ell j := by exact_mod_cast hI + nlinarith + unfold kappa + change A0*d/x*(1+i) ≤ d + rw [div_mul_eq_mul_div] + apply (div_le_iff₀ hx').mpr + exact le_trans (le_of_eq (by ring : A0*d*((1:ℝ)+i)=d*(A0*(1+i)))) + ((mul_le_mul_of_nonneg_left hX hd.le).trans (le_of_eq rfl)) + · change 100 ≤ A0*d/x; apply (le_div_iff₀ hx').mpr + change _ ≤ d at hh + change 100*x^2 ≤ d at hh + nlinarith + +lemma stiff_budget (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, C*H j*(plateErr j + 1000*H j)+2000 ≤ height j/H j^4 := by + filter_upwards [h_large (2*C),hpoly (10000*C) 6] with j h h' + let d := height j; let a := H j + have ha : 2*C ≤ a := h + have Ha : 1 ≤ a := H_ge _ + have ha' : 0 < a := H_pos _ + have hd : 0 < d := d_pos _ + change 10000*C*a^6 ≤ d at h' + change C*a*(d/a^400+1000*a)+2000 ≤ d/a^4 + apply (le_div_iff₀ (pow_pos ha' _)).mpr + have he : a^6 ≤ a^400 := pow_le_pow_right₀ Ha (by omega) + have he1 : C*a*(d/a^400)*a^4 ≤ d/2 := by + calc + _ ≤ (a/2)*a*(d/a^6)*a^4 := by gcongr; linarith + _ = _ := by field_simp + have he2 : C*a*(1000*a)*a^4+2000*a^4 ≤ d/2 := by + rw [show C*a*(1000*a)*a^4=1000*C*a^6 by ring] + have hg : a^4 ≤ C*a^6 := le_trans (pow_le_pow_right₀ Ha (by omega)) + (le_mul_of_one_le_left (by positivity) hC) + linarith + rw [mul_add,add_mul, add_mul]; linarith + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Obtain.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Obtain.lean index aa31878cea144805757d76869b88a170e347654a..0b40e51704159bdbb1695bbb6776fcfc8cdb91c1 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Obtain.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Obtain.lean @@ -1,212 +1,212 @@ -import OAI.Analysis.FourierLLogL.Thermal.Levels -import OAI.Analysis.FourierLLogL.Thermal.Tilted -import OAI.Analysis.FourierLLogL.Thermal.Shrink - -/-! # Obtaining the compressed set with available volume -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M l : ℕ} -open scoped Classical - -def reserve (C : ℝ) := C+20 -def codeWeight (C : ℝ) := 100000000*reserve C -lemma reserve_ge {C : ℝ} (h : 1 ≤ C) : 1 ≤ reserve C := by unfold reserve; linarith -lemma code_ge {C : ℝ} (h : 1 ≤ C) : 1 ≤ codeWeight C := by unfold codeWeight reserve; linarith - -lemma log_mesh (j : ℕ) {p : Node} (hp : p.lev ≤ M) : - log p.invlen - log ((2:ℝ)^(M-snapGrid j p M)) ≤ 1000*H j := by - let E : ℝ := 2^(M-snapGrid j p M) - have hx : p.invlen ≤ 64*H j^40 * E := (step_scale j hp).2.2.1 - have hH := H_pos j - have he : 0 < E := by unfold E; positivity - have ht := log_le_log p.invlen_pos hx - rw [log_mul (by positivity) (by positivity), log_mul (by norm_num) (by positivity),log_pow] at ht - change log p.invlen-log E ≤ _ - push_cast at ht - linarith [H_ge j,Real.log_le_sub_one_of_pos hH,log_le_sub_one_of_pos (by norm_num : (0:ℝ)<64)] - -private lemma levelcost_scale : - ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → - 5000-log (beta i)+2*sLen j*(1-log (beta i)) ≤ height j := by - filter_upwards [Tk_bounds,hpoly 20000 1,h_large 2000] with j ⟨he,hT⟩ h0 h1 i hi - let h := H j; let d := height j - have hl : -log (beta i) ≤ 200*h := (main_bounds _ hi).2.2 - have hp : 1 ≤ h := H_ge _ - have hi : 402*h ≤ h^6/2 := by - have ht : h^2 ≤ h^6 := pow_le_pow_right₀ hp (by omega) - change 2000 ≤ h at h1 - nlinarith - change _ ≤ d - have ht : 0 ≤ (sLen j:ℝ) := by positivity - change (sLen j:ℝ)*h^6=d at he - change _*h^1 ≤ d at h0; rw [pow_one] at h0 - nlinarith - -private lemma span_scale (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ B : ℝ, 0 ≤ B → ∀ i, i ≤ testIx j → - B*(beta i^2/8) ≤ reserve C * height j → - ∀ n : ℕ, n ≤ 2^(2*j) → - B*log (2*n+1:ℝ) ≤ height j * (j:ℝ)^2 := by - filter_upwards [eventually_ge_atTop 2, hpow_index (10000000000*reserve C) 64] - with j hj hh B hBp i hi hB n hn - let h := H j; let d := height j; let b := beta i - have he : (1/6400)/h^32 ≤ b := (main_bounds _ hi).1 - have hp : 0 < h := H_pos _ - have hd : 0 ≤ d := (d_pos j).le - have hY : B ≤ reserve C*d*(400000000*h^64) := by - have hX : B * (((1/6400)/h^32)^2/8) ≤ reserve C*d := - le_trans (mul_le_mul_of_nonneg_left (by gcongr) hBp) hB - field_simp at hX - have hv : h^32^2 = h^((32:ℕ)^2) := rfl - nlinarith [show 0 ≤ reserve C*d*h^64 from mul_nonneg - (mul_nonneg (by linarith [reserve_ge hC]) hd) (by positivity),show (h^32)^2=h^64 by ring] - have hI : log (2*n+1:ℝ) ≤ 4*(j:ℝ) := by - have h₁ : (1:ℝ) ≤ 2^(2*j) := one_le_pow₀ (by norm_num) - have h₂ : (n:ℝ) ≤ 2^(2*j) := by exact_mod_cast hn - apply le_trans (log_le_log (by positivity) (by linarith : (2*n+1:ℝ) ≤ 3*2^(2*j))) - rw [Real.log_mul (by norm_num) (by positivity),Real.log_pow] - have ht : 2 ≤ (j:ℝ) := by exact_mod_cast hj - push_cast - nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<3)] - change _ ≤ d*(j:ℝ)^2 - calc - _ ≤ B*(4*(j:ℝ)) := mul_le_mul_of_nonneg_left hI hBp - _ ≤ reserve C*d*(400000000*h^64)*(4*(j:ℝ)) := by gcongr - _ = d*(1600000000*reserve C*h^64)*(j:ℝ) := by ring - _ ≤ d*j*(j:ℝ) := by - have h1 := reserve_ge hC - have h3 : 1600000000*reserve C*h^64 ≤ (j:ℝ) := - (by calc - _ ≤ (10000000000*reserve C)*h^64 := by gcongr; norm_num - _ ≤ _ := hh) - gcongr - _ = _ := by ring - -private lemma charge_small {C : ℝ} (hC : 1 ≤ C) {j i : ℕ} : - 4*shrinkScale (reserve C) j/(eps*kappa j (codeWeight C) i) ≤ - kappa j (codeWeight C) i /10 := by - let D := reserve C; let a := codeWeight C; let d := height j; let h := H j - let k := kappa j a i - have hD : 1 ≤ D := reserve_ge hC - have ha : 0 < a := lt_of_lt_of_le zero_lt_one (code_ge hC) - have hd : 0 < d := d_pos _; have hh : 0 < h := H_pos _ - have hk' : 0 < k := kappa_pos _ ha _ _ - have hkp : a*d/h ≤ k := le_mul_of_one_le_right (by positivity) - (le_add_of_nonneg_right (by positivity)) - change 4*(300000*D^2*d^2/h^2)/(eps*k) ≤ k/10 - apply (div_le_iff₀ (mul_pos eps_pos hk')).mpr - calc - _ ≤ (a*d/h)/10*(eps*(a*d/h)) := by - change 4*(300000*D^2*d^2/h^2) ≤ - (100000000*D*d/h)/10*(eps*(100000000*D*d/h)) - unfold eps - field_simp - nlinarith [show 0 ≤ D^2*d^2 by positivity] - _ ≤ _ := by have he := eps_pos; gcongr - -structure Press {M : ℕ} (C : ℝ) (j : ℕ) (p : Node) (A : Form M) : Prop where - fits : p.ok M - cap : ∃ n p', p.below p' ∧ Certified (H j) (plateErr j) n p' A ∧ (n:ℝ) ≤ C*H j ∧ - F A ≤ log p'.invlen + C*height j - -lemma obtain_compressed (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ (M : ℕ) (A : Form M) (p : Node), p.lev < M → Press C j p A → - ∀ (a : Grid M → ℝ), 0 ≤ a → (∀ x, a x ≤ exp (height j)) → exp (-height j) ≤ p.avg a → - ∀ (P : Law (Grid M)) (α : ℝ), PieceLaw p a P → 0 < α → p.avg a ≤ 2*α → - ∀ i : ℕ, i ≤ testIx j → - request A j p a α (side i) (kappa j (codeWeight C) i) → - let l := snapGrid j p M - let hl := snap_le (M:=M) j p - let A' := tilde (H j) A p - let k := kappa j (codeWeight C) i - ∃ (w : Finset (Grid l)) (x : Grid l) (B : Bool) - (v : Grid l → Grid M → ℂ) (V : Law (Grid l)) (t : ℝ), - eps*k ≤ t ∧ t ≤ reserve C * height j ∧ w.Nonempty ∧ supported w V ∧ - separated (raise hl) A (sepRad j) (sig j*p.invlen/2) w ∧ - biases P p A hl w B v (beta i) ∧ - 4*k/5 ≤ log (w.card:ℝ) ∧ - F (A' + long j • averageCost hl V x)- F A' ≤ 350*t ∧ - ((t=eps*k ∧ evenly w V) ∨ t/4 ≤ levelCost hl A' V (sLen j)) := by - filter_upwards [sigma_small, span_scale C hC,stiff_budget C hC,hpoly 5000 1,levelcost_scale, - kappa_scales (codeWeight C) (code_ge hC),spectral_compression (reserve C) (reserve_ge hC)] - with j hs hsp hh hz hL hkl hcp - intro M A p hp hP a ha hA hm P α hPA hα hm' i hi hreq l hl A' k - let c := eps*k; let d := height j; let h := H j; let D := reserve C - have hq := hP.fits - obtain ⟨n,p',ht,HG,hN,hF⟩ := hP.cap - obtain ⟨s,B,v,hs0,hs1,hs2,hB⟩ := extract_spectrum j hq.1 hp ha hα hPA - (lt_of_lt_of_le (exp_pos _) hm) hm' (hs i hi) hreq - have hM := step_scale j hq.2 - have hyp : A + place p M ≤ A' := tilde_le (H_ge j) HG.nonneg p - have ht' : place p M ≤ A' := (le_add_of_nonneg_left HG.nonneg).trans hyp - have hA' : 0 ≤ A' := (place_valid _ _).trans ht' - have hD0 : 1 ≤ D := reserve_ge hC - have hh' : 1 ≤ h := H_ge _ - have ha0 : 5000*h ≤ d := by simpa only [pow_one] using hz - have hd : 0 < d := d_pos j - have hu : F A - log p'.invlen ≤ C*d := by unfold d; linarith - have hR := log_mesh j hq.2 - obtain ⟨hk0,hk1,hk2,hk3⟩ := hkl i hi - have heps := eps_pos - have hC' : 0 < c := by unfold c k; exact mul_pos eps_pos (kappa_pos _ (lt_of_lt_of_le zero_lt_one (code_ge hC)) _ _) - have hY : - -log (fq hl A' hA' 0) ≤ D*d := by - obtain ⟨_,_,_,_,_,HE⟩ := fq_spec hl A' hA' hM.2.1 ht' - apply HE.trans - have hbr : F A'-F (A+place p M) ≤ d := by - apply le_trans (tilde_tight HG (H_ge j) hq) - apply le_trans (b:=C*h*(plateErr j + 1000*h)+2000) _ (le_trans hh ?_) - · have he : 0 ≤ plateErr j := - div_nonneg hd.le (pow_nonneg (by linarith : 0 ≤ h) _) - change (n:ℝ) * (plateErr j+1000*h)+_ ≤ _ - have hx : 0 ≤ plateErr j+1000*h := by linarith - gcongr - · exact div_le_self hd.le (one_le_pow₀ hh') - have Hx := space_increment HG ht hq - change log p.invlen-log (2^(M-l):ℝ) ≤ 1000*h at hR - change _ ≤ _+C*d at hF - unfold D reserve; linarith - have hList (u : Finset (Grid l)) (hU : u⊆s) (m : ℕ) (he : m ≤ 2^(2*j)) : - Compress.lnS (Fin m) (fun x : u => (x:Grid l)) ≤ height j * (j:ℝ)^2 := by - obtain ⟨t,h1,h2⟩ := cheap_span hq ht HG hPA hl hM.1 hA hm - (beta_pos i).le (beta_le i) hB - apply le_trans (sumset_est (s:=u) (t:=t) (fun x hx => h1 _ (hU hx)) _) - refine hsp _ (by positivity) i hi ?_ m he - apply h2.trans - change log p.invlen-log (2^(M-l):ℝ) ≤ 1000*h at hR - unfold d h at *; unfold reserve; linarith - have hCost (V : Law (Grid l)) (hw : supported s V) : - levelCost hl A' V (sLen j) ≤ D*d := by - have Hx := uniform_level_cost hq ht HG hyp hPA hl hA hm (beta_pos i) (beta_le i) - hB V hw (sLen j) (pow_pos_nat _) - unfold D reserve; unfold d at *; linarith [hL i hi] - have hmem : - c ≤ D*d := by - calc - _ ≤ eps*d := by - unfold c k - gcongr - _ ≤ _ := by gcongr; unfold eps; linarith - have hZ : 0 ≤ shrinkScale D j := by unfold shrinkScale; positivity - obtain ⟨x,w,V,t,h1,h2,h3,h4,h5,h6,h7,h8,h9⟩ := - choose_compression hC' hZ hmem - (fun u hu hv t hx hy hQ => hcp M l hl A' p.invlen hA' hM.2.1 ht' hY u hv - (hList u hu) t (hk0.trans hx) hy hQ) hs0 hCost - refine ⟨w,x,B,v,V,t,h1,h2,h5,h6,?_,?_,?_,h8,h9⟩ - · intro x hx y hy; exact hs2 x (h4 hx) y (h4 hy) - · exact ⟨hB.1, fun x hx => hB.2 x (h4 hx)⟩ - have ht := h7.trans (charge_small hC) - change k-log 2 ≤ _ at hs1 - change _ ≤ k/10 at ht - change 100 ≤ k at hk3 - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Levels +import OAI.Analysis.FourierLLogL.Thermal.Tilted +import OAI.Analysis.FourierLLogL.Thermal.Shrink + +/-! # Obtaining the compressed set with available volume -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +variable {M l : ℕ} +open scoped Classical + +def reserve (C : ℝ) := C+20 +def codeWeight (C : ℝ) := 100000000*reserve C +lemma reserve_ge {C : ℝ} (h : 1 ≤ C) : 1 ≤ reserve C := by unfold reserve; linarith +lemma code_ge {C : ℝ} (h : 1 ≤ C) : 1 ≤ codeWeight C := by unfold codeWeight reserve; linarith + +lemma log_mesh (j : ℕ) {p : Node} (hp : p.lev ≤ M) : + log p.invlen - log ((2:ℝ)^(M-snapGrid j p M)) ≤ 1000*H j := by + let E : ℝ := 2^(M-snapGrid j p M) + have hx : p.invlen ≤ 64*H j^40 * E := (step_scale j hp).2.2.1 + have hH := H_pos j + have he : 0 < E := by unfold E; positivity + have ht := log_le_log p.invlen_pos hx + rw [log_mul (by positivity) (by positivity), log_mul (by norm_num) (by positivity),log_pow] at ht + change log p.invlen-log E ≤ _ + push_cast at ht + linarith [H_ge j,Real.log_le_sub_one_of_pos hH,log_le_sub_one_of_pos (by norm_num : (0:ℝ)<64)] + +private lemma levelcost_scale : + ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → + 5000-log (beta i)+2*sLen j*(1-log (beta i)) ≤ height j := by + filter_upwards [Tk_bounds,hpoly 20000 1,h_large 2000] with j ⟨he,hT⟩ h0 h1 i hi + let h := H j; let d := height j + have hl : -log (beta i) ≤ 200*h := (main_bounds _ hi).2.2 + have hp : 1 ≤ h := H_ge _ + have hi : 402*h ≤ h^6/2 := by + have ht : h^2 ≤ h^6 := pow_le_pow_right₀ hp (by omega) + change 2000 ≤ h at h1 + nlinarith + change _ ≤ d + have ht : 0 ≤ (sLen j:ℝ) := by positivity + change (sLen j:ℝ)*h^6=d at he + change _*h^1 ≤ d at h0; rw [pow_one] at h0 + nlinarith + +private lemma span_scale (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ B : ℝ, 0 ≤ B → ∀ i, i ≤ testIx j → + B*(beta i^2/8) ≤ reserve C * height j → + ∀ n : ℕ, n ≤ 2^(2*j) → + B*log (2*n+1:ℝ) ≤ height j * (j:ℝ)^2 := by + filter_upwards [eventually_ge_atTop 2, hpow_index (10000000000*reserve C) 64] + with j hj hh B hBp i hi hB n hn + let h := H j; let d := height j; let b := beta i + have he : (1/6400)/h^32 ≤ b := (main_bounds _ hi).1 + have hp : 0 < h := H_pos _ + have hd : 0 ≤ d := (d_pos j).le + have hY : B ≤ reserve C*d*(400000000*h^64) := by + have hX : B * (((1/6400)/h^32)^2/8) ≤ reserve C*d := + le_trans (mul_le_mul_of_nonneg_left (by gcongr) hBp) hB + field_simp at hX + have hv : h^32^2 = h^((32:ℕ)^2) := rfl + nlinarith [show 0 ≤ reserve C*d*h^64 from mul_nonneg + (mul_nonneg (by linarith [reserve_ge hC]) hd) (by positivity),show (h^32)^2=h^64 by ring] + have hI : log (2*n+1:ℝ) ≤ 4*(j:ℝ) := by + have h₁ : (1:ℝ) ≤ 2^(2*j) := one_le_pow₀ (by norm_num) + have h₂ : (n:ℝ) ≤ 2^(2*j) := by exact_mod_cast hn + apply le_trans (log_le_log (by positivity) (by linarith : (2*n+1:ℝ) ≤ 3*2^(2*j))) + rw [Real.log_mul (by norm_num) (by positivity),Real.log_pow] + have ht : 2 ≤ (j:ℝ) := by exact_mod_cast hj + push_cast + nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<3)] + change _ ≤ d*(j:ℝ)^2 + calc + _ ≤ B*(4*(j:ℝ)) := mul_le_mul_of_nonneg_left hI hBp + _ ≤ reserve C*d*(400000000*h^64)*(4*(j:ℝ)) := by gcongr + _ = d*(1600000000*reserve C*h^64)*(j:ℝ) := by ring + _ ≤ d*j*(j:ℝ) := by + have h1 := reserve_ge hC + have h3 : 1600000000*reserve C*h^64 ≤ (j:ℝ) := + (by calc + _ ≤ (10000000000*reserve C)*h^64 := by gcongr; norm_num + _ ≤ _ := hh) + gcongr + _ = _ := by ring + +private lemma charge_small {C : ℝ} (hC : 1 ≤ C) {j i : ℕ} : + 4*shrinkScale (reserve C) j/(eps*kappa j (codeWeight C) i) ≤ + kappa j (codeWeight C) i /10 := by + let D := reserve C; let a := codeWeight C; let d := height j; let h := H j + let k := kappa j a i + have hD : 1 ≤ D := reserve_ge hC + have ha : 0 < a := lt_of_lt_of_le zero_lt_one (code_ge hC) + have hd : 0 < d := d_pos _; have hh : 0 < h := H_pos _ + have hk' : 0 < k := kappa_pos _ ha _ _ + have hkp : a*d/h ≤ k := le_mul_of_one_le_right (by positivity) + (le_add_of_nonneg_right (by positivity)) + change 4*(300000*D^2*d^2/h^2)/(eps*k) ≤ k/10 + apply (div_le_iff₀ (mul_pos eps_pos hk')).mpr + calc + _ ≤ (a*d/h)/10*(eps*(a*d/h)) := by + change 4*(300000*D^2*d^2/h^2) ≤ + (100000000*D*d/h)/10*(eps*(100000000*D*d/h)) + unfold eps + field_simp + nlinarith [show 0 ≤ D^2*d^2 by positivity] + _ ≤ _ := by have he := eps_pos; gcongr + +structure Press {M : ℕ} (C : ℝ) (j : ℕ) (p : Node) (A : Form M) : Prop where + fits : p.ok M + cap : ∃ n p', p.below p' ∧ Certified (H j) (plateErr j) n p' A ∧ (n:ℝ) ≤ C*H j ∧ + F A ≤ log p'.invlen + C*height j + +lemma obtain_compressed (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ (M : ℕ) (A : Form M) (p : Node), p.lev < M → Press C j p A → + ∀ (a : Grid M → ℝ), 0 ≤ a → (∀ x, a x ≤ exp (height j)) → exp (-height j) ≤ p.avg a → + ∀ (P : Law (Grid M)) (α : ℝ), PieceLaw p a P → 0 < α → p.avg a ≤ 2*α → + ∀ i : ℕ, i ≤ testIx j → + request A j p a α (side i) (kappa j (codeWeight C) i) → + let l := snapGrid j p M + let hl := snap_le (M:=M) j p + let A' := tilde (H j) A p + let k := kappa j (codeWeight C) i + ∃ (w : Finset (Grid l)) (x : Grid l) (B : Bool) + (v : Grid l → Grid M → ℂ) (V : Law (Grid l)) (t : ℝ), + eps*k ≤ t ∧ t ≤ reserve C * height j ∧ w.Nonempty ∧ supported w V ∧ + separated (raise hl) A (sepRad j) (sig j*p.invlen/2) w ∧ + biases P p A hl w B v (beta i) ∧ + 4*k/5 ≤ log (w.card:ℝ) ∧ + F (A' + long j • averageCost hl V x)- F A' ≤ 350*t ∧ + ((t=eps*k ∧ evenly w V) ∨ t/4 ≤ levelCost hl A' V (sLen j)) := by + filter_upwards [sigma_small, span_scale C hC,stiff_budget C hC,hpoly 5000 1,levelcost_scale, + kappa_scales (codeWeight C) (code_ge hC),spectral_compression (reserve C) (reserve_ge hC)] + with j hs hsp hh hz hL hkl hcp + intro M A p hp hP a ha hA hm P α hPA hα hm' i hi hreq l hl A' k + let c := eps*k; let d := height j; let h := H j; let D := reserve C + have hq := hP.fits + obtain ⟨n,p',ht,HG,hN,hF⟩ := hP.cap + obtain ⟨s,B,v,hs0,hs1,hs2,hB⟩ := extract_spectrum j hq.1 hp ha hα hPA + (lt_of_lt_of_le (exp_pos _) hm) hm' (hs i hi) hreq + have hM := step_scale j hq.2 + have hyp : A + place p M ≤ A' := tilde_le (H_ge j) HG.nonneg p + have ht' : place p M ≤ A' := (le_add_of_nonneg_left HG.nonneg).trans hyp + have hA' : 0 ≤ A' := (place_valid _ _).trans ht' + have hD0 : 1 ≤ D := reserve_ge hC + have hh' : 1 ≤ h := H_ge _ + have ha0 : 5000*h ≤ d := by simpa only [pow_one] using hz + have hd : 0 < d := d_pos j + have hu : F A - log p'.invlen ≤ C*d := by unfold d; linarith + have hR := log_mesh j hq.2 + obtain ⟨hk0,hk1,hk2,hk3⟩ := hkl i hi + have heps := eps_pos + have hC' : 0 < c := by unfold c k; exact mul_pos eps_pos (kappa_pos _ (lt_of_lt_of_le zero_lt_one (code_ge hC)) _ _) + have hY : + -log (fq hl A' hA' 0) ≤ D*d := by + obtain ⟨_,_,_,_,_,HE⟩ := fq_spec hl A' hA' hM.2.1 ht' + apply HE.trans + have hbr : F A'-F (A+place p M) ≤ d := by + apply le_trans (tilde_tight HG (H_ge j) hq) + apply le_trans (b:=C*h*(plateErr j + 1000*h)+2000) _ (le_trans hh ?_) + · have he : 0 ≤ plateErr j := + div_nonneg hd.le (pow_nonneg (by linarith : 0 ≤ h) _) + change (n:ℝ) * (plateErr j+1000*h)+_ ≤ _ + have hx : 0 ≤ plateErr j+1000*h := by linarith + gcongr + · exact div_le_self hd.le (one_le_pow₀ hh') + have Hx := space_increment HG ht hq + change log p.invlen-log (2^(M-l):ℝ) ≤ 1000*h at hR + change _ ≤ _+C*d at hF + unfold D reserve; linarith + have hList (u : Finset (Grid l)) (hU : u⊆s) (m : ℕ) (he : m ≤ 2^(2*j)) : + Compress.lnS (Fin m) (fun x : u => (x:Grid l)) ≤ height j * (j:ℝ)^2 := by + obtain ⟨t,h1,h2⟩ := cheap_span hq ht HG hPA hl hM.1 hA hm + (beta_pos i).le (beta_le i) hB + apply le_trans (sumset_est (s:=u) (t:=t) (fun x hx => h1 _ (hU hx)) _) + refine hsp _ (by positivity) i hi ?_ m he + apply h2.trans + change log p.invlen-log (2^(M-l):ℝ) ≤ 1000*h at hR + unfold d h at *; unfold reserve; linarith + have hCost (V : Law (Grid l)) (hw : supported s V) : + levelCost hl A' V (sLen j) ≤ D*d := by + have Hx := uniform_level_cost hq ht HG hyp hPA hl hA hm (beta_pos i) (beta_le i) + hB V hw (sLen j) (pow_pos_nat _) + unfold D reserve; unfold d at *; linarith [hL i hi] + have hmem : + c ≤ D*d := by + calc + _ ≤ eps*d := by + unfold c k + gcongr + _ ≤ _ := by gcongr; unfold eps; linarith + have hZ : 0 ≤ shrinkScale D j := by unfold shrinkScale; positivity + obtain ⟨x,w,V,t,h1,h2,h3,h4,h5,h6,h7,h8,h9⟩ := + choose_compression hC' hZ hmem + (fun u hu hv t hx hy hQ => hcp M l hl A' p.invlen hA' hM.2.1 ht' hY u hv + (hList u hu) t (hk0.trans hx) hy hQ) hs0 hCost + refine ⟨w,x,B,v,V,t,h1,h2,h5,h6,?_,?_,?_,h8,h9⟩ + · intro x hx y hy; exact hs2 x (h4 hx) y (h4 hy) + · exact ⟨hB.1, fun x hx => hB.2 x (h4 hx)⟩ + have ht := h7.trans (charge_small hC) + change k-log 2 ≤ _ at hs1 + change _ ≤ k/10 at ht + change 100 ≤ k at hk3 + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Order.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Order.lean index 74c58d4d60578ac84237daafd0d2fef22656e9df..3cc99ee4c49b531aec31630049c80ddb4991ceff 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Order.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Order.lean @@ -1,143 +1,143 @@ -import OAI.Analysis.FourierLLogL.Thermal.Ginibre -import OAI.Analysis.FourierLLogL.Finite.Quadratic - -/-! # Comparisons in form order -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Period Real - -variable {M : ℕ} - -lemma K_add (A D : Form M) (u : Grid M) : - K (A+D) u = exp (F (A+D)-F A) * (K A u * boltz D u) := by - simp only [K_eq, boltz, form_add,← exp_add] - congr 1; ring - -lemma ex_tilt (A D : Form M) (f : Grid M → ℝ) : - (label (A+D)).Ex f = - exp (F (A+D)-F A) * (label A).Ex (fun y => boltz D y * f y) := by - simp_rw [label_ex,K_add, expU_ex,mul_assoc,Ex_lmul] - -lemma ex_untilt (A D : Form M) (f : Grid M → ℝ) : - exp (F A-F (A+D)) * (label (A+D)).Ex f = (label A).Ex (fun y => boltz D y * f y) := by - rw [ex_tilt,← mul_assoc,← exp_add,sub_add_sub_cancel,sub_self,exp_zero,one_mul] - -lemma boltz_mean (A D : Form M) : - (label A).Ex (boltz D) = exp (F A - F (A+D)) := by - simpa only [Ex_const,mul_one] using (ex_untilt A D fun _ => 1).symm - -lemma cred_add_mono {A D : Form M} (hA : 0 ≤ A) (hD : 0 ≤ D) (s : Grid M) : - cred A s ≤ cred (A+D) s := by - apply le_of_mul_le_mul_left _ (exp_pos (F A-F (A+D))) - rw [cred_ex (A+D) s,ex_untilt,← boltz_mean] - have he (x : Grid M) : boltz D x = ∑ t, q D t * wave t x := (boltz_series _ _).symm - have hf : boltz D = fun x => ∑ t, q D t * wave t x := funext he - simp_rw [hf,Finset.sum_mul,mul_assoc, Ex_finset_sum,Ex_lmul] - rw [Finset.sum_mul] - apply Finset.sum_le_sum; intro x _ - rw [mul_assoc,← cred_ex A x] - exact mul_le_mul_of_nonneg_left (ginibre hA _ _) (q_nonneg hD _) - -lemma cred_mono {A B : Form M} (hA : 0 ≤ A) (h : A ≤ B) (s : Grid M) : - cred A s ≤ cred B s := by - have he : B = A+(B-A) := by abel - simpa only [← he] using cred_add_mono hA (sub_nonneg.mpr h) s - -lemma F_increment {A B D : Form M} (hA : 0 ≤ A) (h : A ≤ B) (hD : 0 ≤ D) : - F (B+D)-F B ≤ F (A+D)-F A := by - suffices HH : F A-F (A+D) ≤ F B-F (B+D) by linarith - apply Real.exp_le_exp.mp - rw [← boltz_mean,← boltz_mean] - rw [show boltz D = fun x => ∑ t, q D t * wave t x from funext fun x => (boltz_series _ _).symm] - simp_rw [Ex_finset_sum,Ex_lmul,← cred_ex] - exact Finset.sum_le_sum fun i _ => - mul_le_mul_of_nonneg_left (cred_mono hA h i) (q_nonneg hD _) - -lemma mean_energy (A D : Form M) : - (label (A+D)).Ex (form D) ≤ F (A+D)-F A := by - apply Real.exp_le_exp.mp - apply le_trans ((label (A+D)).Ex_exp (form D)) - rw [ex_tilt] - have H (x : Grid M) : boltz D x * exp (form D x) = 1 := by simp [boltz, ← exp_add] - simp_rw [H,Ex_const,mul_one]; exact le_rfl - -lemma cred_cost (A : Form M) (s : Grid M) : (label A).Ex (cost s) = 1 - cred A s := by - unfold cost - simp_rw [Ex_sub,Ex_const,cred_ex] - -lemma form_energy_mono {A B D : Form M} (hA : 0 ≤ A) (h : A ≤ B) (hD : 0 ≤ D) : - (label B).Ex (form D) ≤ (label A).Ex (form D) := by - unfold form; simp_rw [Ex_finset_sum,Ex_lmul,cred_cost] - refine Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_left ?_ (hD i) - rw [sub_le_sub_iff_left] - exact cred_mono hA h i - -lemma q_express (A : Form M) (x : Grid M) : q A x = exp (-F A) * cred A x := by - rw [cred,F_origin,mul_comm,div_mul_cancel₀] - exact (q_origin_pos _).ne' - -lemma q_compare {A P B : Form M} (hA : 0 ≤ A) (h : A ≤ P) (hh : P ≤ B) (s : Grid M) : - q P s ≤ exp (-F A) * cred B s := by - rw [q_express] - exact mul_le_mul (exp_monotone (neg_le_neg (F_mono h))) (cred_mono (hA.trans h) hh s) - (cred_nonneg (hA.trans h) s) (exp_nonneg _) - -def delta (A : Form M) (s : Grid M) := √((label A).Ex fun i => ‖1-chi s i‖^2) - -lemma delta_sq (A : Form M) (s : Grid M) : delta A s ^ 2 = 2*(1-cred A s) := by - unfold delta - rw [sq_sqrt ((label A).Ex_nonneg (fun i => sq_nonneg _))] - simp_rw [norm_phase_sq,Ex_lmul,cred_cost] - -lemma delta_zero (A : Form M) : delta A 0 = 0 := by - simp_rw [delta,chi_zero_left] - simp [Ex_const] - -lemma delta_even (A : Form M) : (delta A).Even := by - intro x - have H := delta_sq A x - have H' := delta_sq A (-x) - simp_rw [cred,q_even _ _] at H H' - exact pow_left_inj₀ (sqrt_nonneg _) (sqrt_nonneg _) (by norm_num : 2≠0) |>.mp (H'.trans H.symm) - -lemma delta_mono {A B : Form M} (hA : 0 ≤ A) (h : A ≤ B) (s : Grid M) : - delta B s ≤ delta A s := by - have h1 := delta_sq A s - have h2 := delta_sq B s - nlinarith [cred_mono hA h s,(show 0 ≤ delta A s from sqrt_nonneg _)] - -lemma delta_triangle (A : Form M) (s t : Grid M) : - delta A (s+t) ≤ delta A s + delta A t := by - let f (s i : Grid M) : ℝ := ‖1-chi s i‖ - refine le_trans ?_ <| quadratic_minkowski (label A) (f s) (f t) - apply Real.sqrt_le_sqrt ((label A).Ex_mono fun i => ?_) - apply pow_le_pow_left₀ (norm_nonneg _) _ 2 - dsimp only [f]; rw [chi_comm,chi_comm s _,chi_comm t _] - exact norm_phase_add _ _ _ - -lemma delta_nsmul (A : Form M) (s : Grid M) (k : ℕ) : - delta A (k•s) ≤ k * delta A s := by - induction k with - | zero => simp [delta_zero] - | succ k ih => - rw [add_smul,one_smul] - exact le_trans (delta_triangle _ _ _) (by push_cast; linarith) - -lemma delta_int (A : Form M) (k : ℤ) : - delta A (k:Grid M) ≤ |(k:ℝ)| * delta A 1 := by - cases k with - | ofNat k => - simpa [abs_of_nonneg] using delta_nsmul A 1 k - | negSucc k => - have H := delta_even A ((k+1:ℕ):Grid M) - rw [show ((Int.negSucc k:ℤ):Grid M) = -((k+1:ℕ):Grid M) by simp,H] - convert delta_nsmul A 1 (k+1) using 1 <;> simp - left; rw [abs_of_nonpos (by have H := Nat.cast_nonneg (α := ℝ) k; linarith)]; ring - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Ginibre +import OAI.Analysis.FourierLLogL.Finite.Quadratic + +/-! # Comparisons in form order -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Period Real + +variable {M : ℕ} + +lemma K_add (A D : Form M) (u : Grid M) : + K (A+D) u = exp (F (A+D)-F A) * (K A u * boltz D u) := by + simp only [K_eq, boltz, form_add,← exp_add] + congr 1; ring + +lemma ex_tilt (A D : Form M) (f : Grid M → ℝ) : + (label (A+D)).Ex f = + exp (F (A+D)-F A) * (label A).Ex (fun y => boltz D y * f y) := by + simp_rw [label_ex,K_add, expU_ex,mul_assoc,Ex_lmul] + +lemma ex_untilt (A D : Form M) (f : Grid M → ℝ) : + exp (F A-F (A+D)) * (label (A+D)).Ex f = (label A).Ex (fun y => boltz D y * f y) := by + rw [ex_tilt,← mul_assoc,← exp_add,sub_add_sub_cancel,sub_self,exp_zero,one_mul] + +lemma boltz_mean (A D : Form M) : + (label A).Ex (boltz D) = exp (F A - F (A+D)) := by + simpa only [Ex_const,mul_one] using (ex_untilt A D fun _ => 1).symm + +lemma cred_add_mono {A D : Form M} (hA : 0 ≤ A) (hD : 0 ≤ D) (s : Grid M) : + cred A s ≤ cred (A+D) s := by + apply le_of_mul_le_mul_left _ (exp_pos (F A-F (A+D))) + rw [cred_ex (A+D) s,ex_untilt,← boltz_mean] + have he (x : Grid M) : boltz D x = ∑ t, q D t * wave t x := (boltz_series _ _).symm + have hf : boltz D = fun x => ∑ t, q D t * wave t x := funext he + simp_rw [hf,Finset.sum_mul,mul_assoc, Ex_finset_sum,Ex_lmul] + rw [Finset.sum_mul] + apply Finset.sum_le_sum; intro x _ + rw [mul_assoc,← cred_ex A x] + exact mul_le_mul_of_nonneg_left (ginibre hA _ _) (q_nonneg hD _) + +lemma cred_mono {A B : Form M} (hA : 0 ≤ A) (h : A ≤ B) (s : Grid M) : + cred A s ≤ cred B s := by + have he : B = A+(B-A) := by abel + simpa only [← he] using cred_add_mono hA (sub_nonneg.mpr h) s + +lemma F_increment {A B D : Form M} (hA : 0 ≤ A) (h : A ≤ B) (hD : 0 ≤ D) : + F (B+D)-F B ≤ F (A+D)-F A := by + suffices HH : F A-F (A+D) ≤ F B-F (B+D) by linarith + apply Real.exp_le_exp.mp + rw [← boltz_mean,← boltz_mean] + rw [show boltz D = fun x => ∑ t, q D t * wave t x from funext fun x => (boltz_series _ _).symm] + simp_rw [Ex_finset_sum,Ex_lmul,← cred_ex] + exact Finset.sum_le_sum fun i _ => + mul_le_mul_of_nonneg_left (cred_mono hA h i) (q_nonneg hD _) + +lemma mean_energy (A D : Form M) : + (label (A+D)).Ex (form D) ≤ F (A+D)-F A := by + apply Real.exp_le_exp.mp + apply le_trans ((label (A+D)).Ex_exp (form D)) + rw [ex_tilt] + have H (x : Grid M) : boltz D x * exp (form D x) = 1 := by simp [boltz, ← exp_add] + simp_rw [H,Ex_const,mul_one]; exact le_rfl + +lemma cred_cost (A : Form M) (s : Grid M) : (label A).Ex (cost s) = 1 - cred A s := by + unfold cost + simp_rw [Ex_sub,Ex_const,cred_ex] + +lemma form_energy_mono {A B D : Form M} (hA : 0 ≤ A) (h : A ≤ B) (hD : 0 ≤ D) : + (label B).Ex (form D) ≤ (label A).Ex (form D) := by + unfold form; simp_rw [Ex_finset_sum,Ex_lmul,cred_cost] + refine Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_left ?_ (hD i) + rw [sub_le_sub_iff_left] + exact cred_mono hA h i + +lemma q_express (A : Form M) (x : Grid M) : q A x = exp (-F A) * cred A x := by + rw [cred,F_origin,mul_comm,div_mul_cancel₀] + exact (q_origin_pos _).ne' + +lemma q_compare {A P B : Form M} (hA : 0 ≤ A) (h : A ≤ P) (hh : P ≤ B) (s : Grid M) : + q P s ≤ exp (-F A) * cred B s := by + rw [q_express] + exact mul_le_mul (exp_monotone (neg_le_neg (F_mono h))) (cred_mono (hA.trans h) hh s) + (cred_nonneg (hA.trans h) s) (exp_nonneg _) + +def delta (A : Form M) (s : Grid M) := √((label A).Ex fun i => ‖1-chi s i‖^2) + +lemma delta_sq (A : Form M) (s : Grid M) : delta A s ^ 2 = 2*(1-cred A s) := by + unfold delta + rw [sq_sqrt ((label A).Ex_nonneg (fun i => sq_nonneg _))] + simp_rw [norm_phase_sq,Ex_lmul,cred_cost] + +lemma delta_zero (A : Form M) : delta A 0 = 0 := by + simp_rw [delta,chi_zero_left] + simp [Ex_const] + +lemma delta_even (A : Form M) : (delta A).Even := by + intro x + have H := delta_sq A x + have H' := delta_sq A (-x) + simp_rw [cred,q_even _ _] at H H' + exact pow_left_inj₀ (sqrt_nonneg _) (sqrt_nonneg _) (by norm_num : 2≠0) |>.mp (H'.trans H.symm) + +lemma delta_mono {A B : Form M} (hA : 0 ≤ A) (h : A ≤ B) (s : Grid M) : + delta B s ≤ delta A s := by + have h1 := delta_sq A s + have h2 := delta_sq B s + nlinarith [cred_mono hA h s,(show 0 ≤ delta A s from sqrt_nonneg _)] + +lemma delta_triangle (A : Form M) (s t : Grid M) : + delta A (s+t) ≤ delta A s + delta A t := by + let f (s i : Grid M) : ℝ := ‖1-chi s i‖ + refine le_trans ?_ <| quadratic_minkowski (label A) (f s) (f t) + apply Real.sqrt_le_sqrt ((label A).Ex_mono fun i => ?_) + apply pow_le_pow_left₀ (norm_nonneg _) _ 2 + dsimp only [f]; rw [chi_comm,chi_comm s _,chi_comm t _] + exact norm_phase_add _ _ _ + +lemma delta_nsmul (A : Form M) (s : Grid M) (k : ℕ) : + delta A (k•s) ≤ k * delta A s := by + induction k with + | zero => simp [delta_zero] + | succ k ih => + rw [add_smul,one_smul] + exact le_trans (delta_triangle _ _ _) (by push_cast; linarith) + +lemma delta_int (A : Form M) (k : ℤ) : + delta A (k:Grid M) ≤ |(k:ℝ)| * delta A 1 := by + cases k with + | ofNat k => + simpa [abs_of_nonneg] using delta_nsmul A 1 k + | negSucc k => + have H := delta_even A ((k+1:ℕ):Grid M) + rw [show ((Int.negSucc k:ℤ):Grid M) = -((k+1:ℕ):Grid M) by simp,H] + convert delta_nsmul A 1 (k+1) using 1 <;> simp + left; rw [abs_of_nonpos (by have H := Nat.cast_nonneg (α := ℝ) k; linarith)]; ring + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Packing.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Packing.lean index 9bbfe95d8e31fe05df571a8dc584e283caf2dcc4..faadc271ba8d49afe8f4ddf6bfcc99ab775558a5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Packing.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Packing.lean @@ -1,239 +1,239 @@ -import OAI.Analysis.FourierLLogL.Thermal.Shrink -import OAI.Analysis.FourierLLogL.Thermal.Bias - -/-! # Gap of a metric ball -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Pointwise -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period - -variable {M l : ℕ} -open scoped Classical - -def gap (A : Form M) (r : ℝ) := F A - log ((ball A r).card) - -lemma ball_pos (A : Form M) {r : ℝ} (hr : 0 ≤ r) : (0:ℝ) < (ball A r).card := by - exact_mod_cast Finset.card_pos.mpr ⟨0,(mem_ball A r 0).mpr (by rw [delta_zero]; exact hr)⟩ - -lemma gap_zero {r : ℝ} (hr : 0 ≤ r) (hr' : r ≤ 1) : - gap (0:Form M) r = 0 := by - have H : ball (0:Form M) r = {0} := by - ext x; simp only [mem_ball,Finset.mem_singleton] - constructor - · intro hx - by_contra hh - have HE := delta_sq (0:Form M) x - have HQ : q (0:Form M) x = 0 := by - unfold q fc - simp_rw [boltz,form_zero,neg_zero,exp_zero,Complex.ofReal_one,one_mul,expU_chi] - simp [hh] - rw [cred,HQ,zero_div] at HE - have h1 : 0 ≤ delta (0:Form M) x := sqrt_nonneg _ - nlinarith - · rintro rfl; rw [delta_zero]; exact hr - simp [gap,F_zero,H] - -lemma half_q_ball (A : Form M) (x : Grid M) (h : delta A x ≤ 1) : - q A 0 /2 ≤ q A x := by - have hi : (1/2:ℝ) ≤ cred A x := by - have hx : 0 ≤ delta A x := sqrt_nonneg _ - nlinarith [delta_sq A x] - nth_rw 2 [q_express] - rw [F_origin] - nlinarith [q_origin_pos A] - -lemma gap_lower {A : Form M} (hA : 0 ≤ A) {r : ℝ} (hr : 0 ≤ r) (hr' : r ≤ 1) : - -2 ≤ gap A r := by - let s := ball A r - have hi : s.card * q A 0 /2 ≤ 1 := calc - _ = ∑ _u∈s, q A 0 /2 := by simp; ring - _ ≤ ∑ u∈s, q A u := Finset.sum_le_sum fun i hi => half_q_ball A i (((mem_ball _ _ _).mp hi).trans hr') - _ ≤ ∑ u : Grid M, q A u := Finset.sum_le_sum_of_subset_of_nonneg - (Finset.subset_univ _) (fun _ _ _ => q_nonneg hA _) - _ = 1 := q_total _ - have hp : 0 < (s.card:ℝ) := ball_pos _ hr - have hh := Real.log_le_log (mul_pos hp (q_origin_pos A)) (y:=2) (by linarith) - rw [log_mul hp.ne' (q_origin_pos A).ne'] at hh - change -2 ≤ -log (q A 0)-log (s.card:ℝ) - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] - -private lemma overlap_bound (U D : Finset (Grid M)) : - U.card*D.card ≤ (U+D).card*((U-U)∩(D-D)).card := by - classical - let V := U ×ˢ D - let f : Grid M × Grid M → Grid M := fun v => v.1+v.2 - have hV : MapsTo f V (↑(U+D):Set (Grid M)) := fun i hi => - Finset.add_mem_add (Finset.mem_product.mp hi).1 (Finset.mem_product.mp hi).2 - let L := (U-U)∩(D-D) - have hi (i : Grid M) (hh : i ∈ U+D) : (V.filter fun v => f v=i).card ≤ L.card := by - obtain ⟨x,hx,y,hy,rfl⟩ := Finset.mem_add.mp hh - let g : Grid M × Grid M → Grid M := fun i => i.1-x - apply Finset.card_le_card_of_injOn g - · rintro ⟨t,v⟩ ht - obtain ⟨hh,h⟩ := Finset.mem_filter.mp ht - obtain ⟨hh1,hh2⟩ := Finset.mem_product.mp hh - change t+v=x+y at h - have he : t-x=y-v := by linear_combination h - exact Finset.mem_inter.mpr ⟨Finset.sub_mem_sub hh1 hx,by dsimp [g]; rw [he]; exact Finset.sub_mem_sub hy hh2⟩ - · rintro ⟨t,v⟩ ht ⟨b,c⟩ hs H - have H1 := (Finset.mem_filter.mp ht).2 - have H2 := (Finset.mem_filter.mp hs).2 - change t-x=b-x at H - dsimp only [f] at H1 H2 - have hh : t=b := sub_left_inj.mp H - have he : v=c := add_left_cancel (hh ▸ H1.trans H2.symm) - rw [hh,he] - rw [← Finset.card_product] - change V.card ≤ _ - calc - _ = ∑ i ∈ U+D, (V.filter (fun v => f v=i)).card := Finset.card_eq_sum_card_fiberwise hV - _ ≤ ∑ _i∈U+D, L.card := Finset.sum_le_sum hi - _ = _ := by simp [L] - -lemma delta_diff {A : Form M} {r : ℝ} {x y : Grid M} (hx : x∈ball A r) - (hy : y∈ball A r) : delta A (x-y) ≤ 2*r := by - rw [sub_eq_add_neg]; apply le_trans (delta_triangle _ _ _) - rw [delta_even _ y] - linarith [(mem_ball _ _ _).mp hx,(mem_ball _ _ _).mp hy] -lemma rad_diff {r : ℝ} {x y : Grid M} (hx : x∈arc M r) (hy : y∈arc M r) : - rad (x-y) ≤ 2*r := by - rw [sub_eq_add_neg]; apply le_trans (rad_add _ _) - rw [rad_even] - linarith [(mem_arc _ _).mp hx,(mem_arc _ _).mp hy] - -private lemma log_pair {p : Node} (hp : p.ok M) {r σ : ℝ} (hr : 0 ≤ r) (hr' : 2*r ≤ 1) - (hs : 0 < σ) (hs' : σ ≤ 1/4) {A : Form M} (hA : 0 ≤ A) : - let U := ball A r - let D := arc M (σ*p.invlen/10) - F (A+place p M)-F A + log (U.card:ℝ) ≤ log ((U+D).card:ℝ) + 130 - log σ := by - intro U D - let R := p.invlen; let d := σ*R/10 - let B := place p M - let L := (U-U)∩(D-D) - have hR : 0 < R := p.invlen_pos - have hd : 0 < d := by unfold d; positivity - have hDD : d ≤ ↑D.card := by - have HE := loc_scale hp.2 - change R ≤ _ at HE - apply arc_card_lo M _ hd.le - unfold d; nlinarith - have h₁ : (0:ℝ)< U.card := ball_pos _ hr - have h₂ : (0:ℝ)< D.card := hd.trans_le hDD - have hU : 0∈U := (mem_ball _ _ _).mpr (by rw [delta_zero]; exact hr) - have hD : 0∈D := by - apply (mem_arc _ _).mpr - simpa only [rad, ZMod.val_zero,neg_zero,min_self,Nat.cast_zero] using hd.le - have hL : (0:Grid M)∈L := - Finset.mem_inter.mpr ⟨by simpa using Finset.sub_mem_sub hU hU, - by simpa using Finset.sub_mem_sub hD hD⟩ - have h₃ : (0:ℝ) < L.card := by exact_mod_cast Finset.card_pos.mpr ⟨0,hL⟩ - have h₄ : (0:ℝ) < (U+D).card := by - exact_mod_cast Finset.card_pos.mpr ⟨_,Finset.add_mem_add hU hD⟩ - have hi (x : Grid M) (hi : x∈L) : - q A 0 * ((1/120)/R) ≤ q A x * q B (-x) := by - obtain ⟨hx,hy⟩ := Finset.mem_inter.mp hi - have h1 : delta A x ≤ 1 := by - obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_sub.mp hx - exact (delta_diff hu hv).trans hr' - have h2 : rad x ≤ R/20 := by - obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_sub.mp hy - have hh : rad (u-v) ≤ 2*d := rad_diff hu hv - unfold d at hh; nlinarith - calc - _ = q A 0 /2 * ((1/60:ℝ)/R) := by ring - _ ≤ q A x * q B x := mul_le_mul (half_q_ball _ _ h1) - (q_short R p.invlen_ge (loc_scale hp.2) x h2) - (by positivity) (q_nonneg hA _) - _ = _ := by rw [q_even _ _] - have H : L.card * (q A 0 * ((1/120:ℝ)/R)) ≤ q (A+B) 0 := calc - _ = ∑ i∈L, q A 0*((1/120:ℝ)/R) := by simp - _ ≤ ∑ i∈L, q A i*q B (-i) := Finset.sum_le_sum hi - _ ≤ ∑ i, q A i*q B (-i) := Finset.sum_le_sum_of_subset_of_nonneg - (Finset.subset_univ _) (fun _ _ _ => mul_nonneg (q_nonneg hA _) (q_nonneg (place_valid _ _) _)) - _ = _ := by simp_rw [q_add,zero_sub] - have HH : (U.card:ℝ)*D.card ≤ (U+D).card*(L.card:ℝ) := by exact_mod_cast overlap_bound U D - have H₁ := log_le_log (mul_pos h₁ h₂) HH - have H₂ := log_le_log (show 0 < (L.card:ℝ)*(q A 0*((1/120:ℝ)/R)) by - have := q_origin_pos A; positivity) H - have H₃ := log_le_log hd hDD - rw [Real.log_mul h₁.ne' h₂.ne',Real.log_mul h₄.ne' h₃.ne'] at H₁ - rw [Real.log_mul h₃.ne' (by have := q_origin_pos A; positivity), - Real.log_mul (q_origin_pos _).ne' (by positivity), Real.log_div (by norm_num) hR.ne', - Real.log_div (by norm_num) (by norm_num), Real.log_one] at H₂ - unfold d at H₃ - rw [Real.log_div (by positivity) (by norm_num),Real.log_mul hs.ne' hR.ne'] at H₃ - unfold F; change -log (q (A+B) 0) - _ + _ ≤ _ - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<120), - log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10)] - -lemma gap_packing {p : Node} (hp : p.ok M) {r σ η : ℝ} (hr : 0 ≤ r) (hr' : 2*r ≤ 1) - (hs : 0 < σ) (hs' : σ ≤ 1/4) (he : 4*σ ≤ η) {A C : Form M} (hA : 0 ≤ A) - (hC : A+place p M ≤ C) (f : Grid l → Grid M) (s : Finset (Grid l)) (hn : s.Nonempty) - (h : separated f A (2*r) (σ*p.invlen/2) s) - (hc : ∀ x∈s, delta C (f x) ≤ η/2) : - gap C (r+η) - gap A r ≤ F C - F (A+place p M) + 130 - log σ - log (s.card:ℝ) := by - let U := ball A r; let R := p.invlen; let d := σ*R/10; let D := arc M d - have he' : 0 < η := by linarith - let W := U+D - have hR : 0 < R := p.invlen_pos - have hD : 0 ∈ D := by - apply (mem_arc _ _).mpr - simp [rad] - unfold d; positivity - have hU : 0∈U := (mem_ball _ _ _).mpr (by rw [delta_zero]; exact hr) - have hW : 0 ∈ W := by simpa using Finset.add_mem_add hU hD - have hB := ball_pos C (show 0 ≤ r+η by linarith) - have hsW : 0 < (W.card:ℝ) := by exact_mod_cast Finset.card_pos.mpr ⟨_,hW⟩ - have hC1 : place p M ≤ C := (le_add_of_nonneg_left hA).trans hC - have hC2 : A ≤ C := (le_add_of_nonneg_right (place_valid _ _)).trans hC - let F' : Grid l × Grid M → Grid M := fun i => f i.1+i.2 - have ha : s.card*W.card ≤ (ball C (r+η)).card := by - rw [← Finset.card_product] - apply Finset.card_le_card_of_injOn F' - · rintro ⟨i,j⟩ hi - obtain ⟨hi,hj⟩ := Finset.mem_product.mp hi - obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_add.mp hj - apply (mem_ball _ _ _).mpr - have h1 := hc i hi - have hV : delta C u ≤ r := (delta_mono hA hC2 _).trans ((mem_ball _ _ _).mp hu) - have hV' : delta C v ≤ η/2 := by - have ht := delta_S R p.invlen_ge (loc_scale hp.2) _ hC1 v - have hQ := (mem_arc _ _).mp hv - exact ht.trans (by - calc - _ ≤ d * (20/R) := mul_le_mul_of_nonneg_right hQ (by positivity) - _ = 2*σ := by unfold d; field_simp; ring - _ ≤ _ := by linarith) - change delta C (f i + (u+v)) ≤ _ - linarith [delta_triangle C u v,delta_triangle C (f i) (u+v)] - · rintro ⟨i,j⟩ hi ⟨x,y⟩ hj HH - obtain ⟨hi,hv⟩ := Finset.mem_product.mp hi - obtain ⟨hx,hy⟩ := Finset.mem_product.mp hj - change f i + j=f x+y at HH - have Hx : i=x := by - by_contra hh - apply h i hi x hx hh - obtain ⟨u,hu,t,ht,rfl⟩ := Finset.mem_add.mp hv - obtain ⟨v,hv,q,hq,rfl⟩ := Finset.mem_add.mp hy - refine ⟨v-u,delta_diff hv hu,?_⟩ - rw [show f i-f x-(v-u)=q-t by linear_combination HH] - change _ ≤ σ*R/2 - have hE : rad (q-t) ≤ 2*d := rad_diff hq ht - unfold d at hE; nlinarith - subst x; rw [add_right_inj] at HH - simp [HH] - have h1 : (s.card:ℝ)*W.card ≤ (ball C (r+η)).card := by exact_mod_cast ha - have hV : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.mpr hn - have H := log_le_log (mul_pos hV hsW) h1 - rw [log_mul hV.ne' hsW.ne'] at H - have H' := log_pair hp hr hr' hs hs' hA - change _ + log (U.card:ℝ) ≤ log (W.card:ℝ)+130-_ at H' - unfold gap; change F C-_-(F A-log (U.card:ℝ)) ≤ _ - linarith - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Shrink +import OAI.Analysis.FourierLLogL.Thermal.Bias + +/-! # Gap of a metric ball -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Pointwise +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period + +variable {M l : ℕ} +open scoped Classical + +def gap (A : Form M) (r : ℝ) := F A - log ((ball A r).card) + +lemma ball_pos (A : Form M) {r : ℝ} (hr : 0 ≤ r) : (0:ℝ) < (ball A r).card := by + exact_mod_cast Finset.card_pos.mpr ⟨0,(mem_ball A r 0).mpr (by rw [delta_zero]; exact hr)⟩ + +lemma gap_zero {r : ℝ} (hr : 0 ≤ r) (hr' : r ≤ 1) : + gap (0:Form M) r = 0 := by + have H : ball (0:Form M) r = {0} := by + ext x; simp only [mem_ball,Finset.mem_singleton] + constructor + · intro hx + by_contra hh + have HE := delta_sq (0:Form M) x + have HQ : q (0:Form M) x = 0 := by + unfold q fc + simp_rw [boltz,form_zero,neg_zero,exp_zero,Complex.ofReal_one,one_mul,expU_chi] + simp [hh] + rw [cred,HQ,zero_div] at HE + have h1 : 0 ≤ delta (0:Form M) x := sqrt_nonneg _ + nlinarith + · rintro rfl; rw [delta_zero]; exact hr + simp [gap,F_zero,H] + +lemma half_q_ball (A : Form M) (x : Grid M) (h : delta A x ≤ 1) : + q A 0 /2 ≤ q A x := by + have hi : (1/2:ℝ) ≤ cred A x := by + have hx : 0 ≤ delta A x := sqrt_nonneg _ + nlinarith [delta_sq A x] + nth_rw 2 [q_express] + rw [F_origin] + nlinarith [q_origin_pos A] + +lemma gap_lower {A : Form M} (hA : 0 ≤ A) {r : ℝ} (hr : 0 ≤ r) (hr' : r ≤ 1) : + -2 ≤ gap A r := by + let s := ball A r + have hi : s.card * q A 0 /2 ≤ 1 := calc + _ = ∑ _u∈s, q A 0 /2 := by simp; ring + _ ≤ ∑ u∈s, q A u := Finset.sum_le_sum fun i hi => half_q_ball A i (((mem_ball _ _ _).mp hi).trans hr') + _ ≤ ∑ u : Grid M, q A u := Finset.sum_le_sum_of_subset_of_nonneg + (Finset.subset_univ _) (fun _ _ _ => q_nonneg hA _) + _ = 1 := q_total _ + have hp : 0 < (s.card:ℝ) := ball_pos _ hr + have hh := Real.log_le_log (mul_pos hp (q_origin_pos A)) (y:=2) (by linarith) + rw [log_mul hp.ne' (q_origin_pos A).ne'] at hh + change -2 ≤ -log (q A 0)-log (s.card:ℝ) + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] + +private lemma overlap_bound (U D : Finset (Grid M)) : + U.card*D.card ≤ (U+D).card*((U-U)∩(D-D)).card := by + classical + let V := U ×ˢ D + let f : Grid M × Grid M → Grid M := fun v => v.1+v.2 + have hV : MapsTo f V (↑(U+D):Set (Grid M)) := fun i hi => + Finset.add_mem_add (Finset.mem_product.mp hi).1 (Finset.mem_product.mp hi).2 + let L := (U-U)∩(D-D) + have hi (i : Grid M) (hh : i ∈ U+D) : (V.filter fun v => f v=i).card ≤ L.card := by + obtain ⟨x,hx,y,hy,rfl⟩ := Finset.mem_add.mp hh + let g : Grid M × Grid M → Grid M := fun i => i.1-x + apply Finset.card_le_card_of_injOn g + · rintro ⟨t,v⟩ ht + obtain ⟨hh,h⟩ := Finset.mem_filter.mp ht + obtain ⟨hh1,hh2⟩ := Finset.mem_product.mp hh + change t+v=x+y at h + have he : t-x=y-v := by linear_combination h + exact Finset.mem_inter.mpr ⟨Finset.sub_mem_sub hh1 hx,by dsimp [g]; rw [he]; exact Finset.sub_mem_sub hy hh2⟩ + · rintro ⟨t,v⟩ ht ⟨b,c⟩ hs H + have H1 := (Finset.mem_filter.mp ht).2 + have H2 := (Finset.mem_filter.mp hs).2 + change t-x=b-x at H + dsimp only [f] at H1 H2 + have hh : t=b := sub_left_inj.mp H + have he : v=c := add_left_cancel (hh ▸ H1.trans H2.symm) + rw [hh,he] + rw [← Finset.card_product] + change V.card ≤ _ + calc + _ = ∑ i ∈ U+D, (V.filter (fun v => f v=i)).card := Finset.card_eq_sum_card_fiberwise hV + _ ≤ ∑ _i∈U+D, L.card := Finset.sum_le_sum hi + _ = _ := by simp [L] + +lemma delta_diff {A : Form M} {r : ℝ} {x y : Grid M} (hx : x∈ball A r) + (hy : y∈ball A r) : delta A (x-y) ≤ 2*r := by + rw [sub_eq_add_neg]; apply le_trans (delta_triangle _ _ _) + rw [delta_even _ y] + linarith [(mem_ball _ _ _).mp hx,(mem_ball _ _ _).mp hy] +lemma rad_diff {r : ℝ} {x y : Grid M} (hx : x∈arc M r) (hy : y∈arc M r) : + rad (x-y) ≤ 2*r := by + rw [sub_eq_add_neg]; apply le_trans (rad_add _ _) + rw [rad_even] + linarith [(mem_arc _ _).mp hx,(mem_arc _ _).mp hy] + +private lemma log_pair {p : Node} (hp : p.ok M) {r σ : ℝ} (hr : 0 ≤ r) (hr' : 2*r ≤ 1) + (hs : 0 < σ) (hs' : σ ≤ 1/4) {A : Form M} (hA : 0 ≤ A) : + let U := ball A r + let D := arc M (σ*p.invlen/10) + F (A+place p M)-F A + log (U.card:ℝ) ≤ log ((U+D).card:ℝ) + 130 - log σ := by + intro U D + let R := p.invlen; let d := σ*R/10 + let B := place p M + let L := (U-U)∩(D-D) + have hR : 0 < R := p.invlen_pos + have hd : 0 < d := by unfold d; positivity + have hDD : d ≤ ↑D.card := by + have HE := loc_scale hp.2 + change R ≤ _ at HE + apply arc_card_lo M _ hd.le + unfold d; nlinarith + have h₁ : (0:ℝ)< U.card := ball_pos _ hr + have h₂ : (0:ℝ)< D.card := hd.trans_le hDD + have hU : 0∈U := (mem_ball _ _ _).mpr (by rw [delta_zero]; exact hr) + have hD : 0∈D := by + apply (mem_arc _ _).mpr + simpa only [rad, ZMod.val_zero,neg_zero,min_self,Nat.cast_zero] using hd.le + have hL : (0:Grid M)∈L := + Finset.mem_inter.mpr ⟨by simpa using Finset.sub_mem_sub hU hU, + by simpa using Finset.sub_mem_sub hD hD⟩ + have h₃ : (0:ℝ) < L.card := by exact_mod_cast Finset.card_pos.mpr ⟨0,hL⟩ + have h₄ : (0:ℝ) < (U+D).card := by + exact_mod_cast Finset.card_pos.mpr ⟨_,Finset.add_mem_add hU hD⟩ + have hi (x : Grid M) (hi : x∈L) : + q A 0 * ((1/120)/R) ≤ q A x * q B (-x) := by + obtain ⟨hx,hy⟩ := Finset.mem_inter.mp hi + have h1 : delta A x ≤ 1 := by + obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_sub.mp hx + exact (delta_diff hu hv).trans hr' + have h2 : rad x ≤ R/20 := by + obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_sub.mp hy + have hh : rad (u-v) ≤ 2*d := rad_diff hu hv + unfold d at hh; nlinarith + calc + _ = q A 0 /2 * ((1/60:ℝ)/R) := by ring + _ ≤ q A x * q B x := mul_le_mul (half_q_ball _ _ h1) + (q_short R p.invlen_ge (loc_scale hp.2) x h2) + (by positivity) (q_nonneg hA _) + _ = _ := by rw [q_even _ _] + have H : L.card * (q A 0 * ((1/120:ℝ)/R)) ≤ q (A+B) 0 := calc + _ = ∑ i∈L, q A 0*((1/120:ℝ)/R) := by simp + _ ≤ ∑ i∈L, q A i*q B (-i) := Finset.sum_le_sum hi + _ ≤ ∑ i, q A i*q B (-i) := Finset.sum_le_sum_of_subset_of_nonneg + (Finset.subset_univ _) (fun _ _ _ => mul_nonneg (q_nonneg hA _) (q_nonneg (place_valid _ _) _)) + _ = _ := by simp_rw [q_add,zero_sub] + have HH : (U.card:ℝ)*D.card ≤ (U+D).card*(L.card:ℝ) := by exact_mod_cast overlap_bound U D + have H₁ := log_le_log (mul_pos h₁ h₂) HH + have H₂ := log_le_log (show 0 < (L.card:ℝ)*(q A 0*((1/120:ℝ)/R)) by + have := q_origin_pos A; positivity) H + have H₃ := log_le_log hd hDD + rw [Real.log_mul h₁.ne' h₂.ne',Real.log_mul h₄.ne' h₃.ne'] at H₁ + rw [Real.log_mul h₃.ne' (by have := q_origin_pos A; positivity), + Real.log_mul (q_origin_pos _).ne' (by positivity), Real.log_div (by norm_num) hR.ne', + Real.log_div (by norm_num) (by norm_num), Real.log_one] at H₂ + unfold d at H₃ + rw [Real.log_div (by positivity) (by norm_num),Real.log_mul hs.ne' hR.ne'] at H₃ + unfold F; change -log (q (A+B) 0) - _ + _ ≤ _ + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<120), + log_le_sub_one_of_pos (by norm_num : (0:ℝ)<10)] + +lemma gap_packing {p : Node} (hp : p.ok M) {r σ η : ℝ} (hr : 0 ≤ r) (hr' : 2*r ≤ 1) + (hs : 0 < σ) (hs' : σ ≤ 1/4) (he : 4*σ ≤ η) {A C : Form M} (hA : 0 ≤ A) + (hC : A+place p M ≤ C) (f : Grid l → Grid M) (s : Finset (Grid l)) (hn : s.Nonempty) + (h : separated f A (2*r) (σ*p.invlen/2) s) + (hc : ∀ x∈s, delta C (f x) ≤ η/2) : + gap C (r+η) - gap A r ≤ F C - F (A+place p M) + 130 - log σ - log (s.card:ℝ) := by + let U := ball A r; let R := p.invlen; let d := σ*R/10; let D := arc M d + have he' : 0 < η := by linarith + let W := U+D + have hR : 0 < R := p.invlen_pos + have hD : 0 ∈ D := by + apply (mem_arc _ _).mpr + simp [rad] + unfold d; positivity + have hU : 0∈U := (mem_ball _ _ _).mpr (by rw [delta_zero]; exact hr) + have hW : 0 ∈ W := by simpa using Finset.add_mem_add hU hD + have hB := ball_pos C (show 0 ≤ r+η by linarith) + have hsW : 0 < (W.card:ℝ) := by exact_mod_cast Finset.card_pos.mpr ⟨_,hW⟩ + have hC1 : place p M ≤ C := (le_add_of_nonneg_left hA).trans hC + have hC2 : A ≤ C := (le_add_of_nonneg_right (place_valid _ _)).trans hC + let F' : Grid l × Grid M → Grid M := fun i => f i.1+i.2 + have ha : s.card*W.card ≤ (ball C (r+η)).card := by + rw [← Finset.card_product] + apply Finset.card_le_card_of_injOn F' + · rintro ⟨i,j⟩ hi + obtain ⟨hi,hj⟩ := Finset.mem_product.mp hi + obtain ⟨u,hu,v,hv,rfl⟩ := Finset.mem_add.mp hj + apply (mem_ball _ _ _).mpr + have h1 := hc i hi + have hV : delta C u ≤ r := (delta_mono hA hC2 _).trans ((mem_ball _ _ _).mp hu) + have hV' : delta C v ≤ η/2 := by + have ht := delta_S R p.invlen_ge (loc_scale hp.2) _ hC1 v + have hQ := (mem_arc _ _).mp hv + exact ht.trans (by + calc + _ ≤ d * (20/R) := mul_le_mul_of_nonneg_right hQ (by positivity) + _ = 2*σ := by unfold d; field_simp; ring + _ ≤ _ := by linarith) + change delta C (f i + (u+v)) ≤ _ + linarith [delta_triangle C u v,delta_triangle C (f i) (u+v)] + · rintro ⟨i,j⟩ hi ⟨x,y⟩ hj HH + obtain ⟨hi,hv⟩ := Finset.mem_product.mp hi + obtain ⟨hx,hy⟩ := Finset.mem_product.mp hj + change f i + j=f x+y at HH + have Hx : i=x := by + by_contra hh + apply h i hi x hx hh + obtain ⟨u,hu,t,ht,rfl⟩ := Finset.mem_add.mp hv + obtain ⟨v,hv,q,hq,rfl⟩ := Finset.mem_add.mp hy + refine ⟨v-u,delta_diff hv hu,?_⟩ + rw [show f i-f x-(v-u)=q-t by linear_combination HH] + change _ ≤ σ*R/2 + have hE : rad (q-t) ≤ 2*d := rad_diff hq ht + unfold d at hE; nlinarith + subst x; rw [add_right_inj] at HH + simp [HH] + have h1 : (s.card:ℝ)*W.card ≤ (ball C (r+η)).card := by exact_mod_cast ha + have hV : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.mpr hn + have H := log_le_log (mul_pos hV hsW) h1 + rw [log_mul hV.ne' hsW.ne'] at H + have H' := log_pair hp hr hr' hs hs' hA + change _ + log (U.card:ℝ) ≤ log (W.card:ℝ)+130-_ at H' + unfold gap; change F C-_-(F A-log (U.card:ℝ)) ≤ _ + linarith + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Piece.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Piece.lean index b9de298c4aefb03522837521b97cadc9fc6d1730..892010d45ce9436270ebe28d119d3c545b3f8ba9 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Piece.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Piece.lean @@ -1,204 +1,204 @@ -import OAI.Analysis.FourierLLogL.Thermal.History - -/-! # Finite input pieces and spectra, using counting measure for the observation coordinate -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic -open Law Node Period Real -namespace Node -variable {M : ℕ} -def child (p : Node) : Bool → Node -| false => p.L -| true => p.R -lemma child_ok (p : Node) (h : p.valid) (hv : p.lev < M) (b) : - ok (p.child b) M := by cases b <;> exact ⟨by first | exact p.valid_L.mpr h | exact p.valid_R.mpr h,hv⟩ -lemma child_len (p : Node) (b : Bool) : (p.child b).len=p.len/2 := by - cases b; exact p.len_L; exact p.len_R -lemma child_below (p : Node) (b : Bool) : (p.child b).below p := by - cases b; exact p.below_left; exact p.below_right -end Node - -namespace Thermal -variable {M : ℕ} -open scoped Classical - -/-- Observation type-A. -/ -def channel (A : Form M) (x : Grid M) : Law (Grid M) := - (label A).send (fun z => x+z) - -lemma channel_apply (A : Form M) (x z : Grid M) : - channel A x z = label A (z-x) := by - rw [channel,send_apply,prob_eq] - simp_rw [add_comm x, ← eq_sub_iff_add_eq] - simp - -lemma ch_K (A : Form M) (x z : Grid M) : - channel A x z = side M * K A (z-x) := by rw [channel_apply,label_apply] - -lemma ch_pos (A : Form M) (x z : Grid M) : 0 < channel A x z := by rw [channel_apply]; exact label_pos _ _ - -lemma ch_ex (A : Form M) (f : Grid M → ℝ) (x : Grid M) : - (channel A x).Ex f = (label A).Ex fun z => f (x+z) := Ex_send _ _ _ - -lemma ch_noise (A : Form M) (f : Grid M → ℝ) (x : Grid M) : - (channel A x).Ex (fun z => f (z-x)) = (label A).Ex f := by - rw [ch_ex]; simp - -/-- Local normalized subdensity (as mass weights, counting-grid). Its total is m/α. -/ -def wt (p : Node) (a : Grid M → ℝ) (α : ℝ) (y : Grid M) : ℝ := - p.dice M y * a y / α - -lemma wt_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) (y) : - 0 ≤ wt p a α y := div_nonneg (mul_nonneg ((p.dice M).nonneg y) (ha y)) hα - -lemma wt_total (p : Node) (hp : p.ok M) (a : Grid M → ℝ) (α : ℝ) : - ∑ y, wt p a α y = p.avg a / α := by simp_rw [wt, ← Finset.sum_div, avg_ex hp,Ex] - -/-- Law of a local piece when normalization is its positive mean; used as proof-carrying -relation to any specified law. -/ -def PieceLaw (p : Node) (a : Grid M → ℝ) (P : Law (Grid M)) := ∀ y, P y = wt p a (p.avg a) y - -lemma make_piece (p : Node) (hp : p.ok M) {a : Grid M → ℝ} (ha : 0 ≤ a) (hm : 0 < p.avg a) : - ∃ P : Law (Grid M), PieceLaw p a P := by - refine ⟨⟨wt p a (p.avg a),wt_nonneg p ha hm.le,by rw [wt_total _ hp,div_self hm.ne']⟩,fun i => rfl⟩ - -lemma piece_support {p : Node} (hp : p.ok M) {a : Grid M → ℝ} - {P : Law (Grid M)} (h : PieceLaw p a P) (x : Grid M) (hx : 0 < P x) : - x ∈ p.block M := by - by_contra hh; rw [h,wt,dice_apply hp,ite_eq_right hh,zero_mul,zero_div] at hx - exact lt_irrefl _ hx - -lemma piece_density {p : Node} {a : Grid M → ℝ} {P : Law (Grid M)} - (h : PieceLaw p a P) {d : ℝ} (h1 : ∀ x, a x ≤ exp d) - (hm : exp (-d) ≤ p.avg a) (x : Grid M) : - P x ≤ exp (2*d)*p.dice M x := by - rw [h,wt] - apply (div_le_iff₀ (lt_of_lt_of_le (exp_pos _) hm)).mpr - have HE : exp (2*d)*exp (-d) = exp d := by rw [← exp_add]; congr; ring - calc - _ ≤ p.dice M x * exp d := by - exact mul_le_mul_of_nonneg_left (h1 _) ((p.dice M).nonneg _) - _ = exp (2*d)*p.dice M x * exp (-d) := by rw [mul_comm _ (p.dice _ _),mul_assoc,HE] - _ ≤ _ := mul_le_mul_of_nonneg_left hm (mul_nonneg (exp_nonneg _) ((p.dice M).nonneg _)) - -lemma wt_half {p : Node} (h : p.valid) (hp : p.lev < M) (a : Grid M → ℝ) (α : ℝ) - (x : Grid M) (b : Bool) : - wt (p.child b) a α x = if x ∈ (p.child b).block M then 2*wt p a α x else 0 := by - rw [wt,dice_apply (child_ok p h hp b)] - split_ifs with hh - · rw [wt,dice_apply ⟨h,hp.le⟩,ite_eq_left (block_of_below (child_below _ _) _ hh),child_len] - field_simp - simp - -/-- Observation law weighted by the local subpiece. -/ -def omegaObs (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (y z : Grid M) := - wt p a α y * channel A y z -def Mz (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (z : Grid M) := - ∑ y, omegaObs p a α A y z -def fz (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (u z : Grid M) : ℂ := - ∑ y, (omegaObs p a α A y z:ℂ) * chi u y -def energyZ (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (u : Grid M) := - ∑ z, ‖fz p a α A u z‖^2 / Mz p a α A z - -def high (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (b : ℝ) (u : Grid M) := - ∃ B, b^2 ≤ energyZ (p.child B) a α A u - -lemma density_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) - (A : Form M) (y z : Grid M) : 0 ≤ omegaObs p a α A y z := - mul_nonneg (wt_nonneg p ha hα _) ((channel A y).nonneg z) - -lemma mass_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) - (A : Form M) (z : Grid M) : 0 ≤ Mz p a α A z := - Finset.sum_nonneg fun _ _ => density_nonneg _ ha hα _ _ _ - -lemma mass_total (p : Node) (hp : p.ok M) (a : Grid M → ℝ) (α : ℝ) - (A : Form M) : ∑ z, Mz p a α A z = p.avg a / α := by - unfold Mz; rw [Finset.sum_comm]; simp_rw [omegaObs,← Finset.mul_sum,Law.total,mul_one] - rw [wt_total _ hp] - -lemma fz_norm (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) - (A : Form M) (u z : Grid M) : ‖fz p a α A u z‖ ≤ Mz p a α A z := by - apply le_trans (norm_sum_le _ _) - simp_rw [norm_mul, Complex.norm_real,norm_chi,mul_one, Real.norm_eq_abs, - abs_of_nonneg (density_nonneg _ ha hα _ _ _)] - exact le_rfl - -lemma energyZ_le (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) - (A : Form M) (u : Grid M) : energyZ p a α A u ≤ ∑ z, ‖fz p a α A u z‖ := by - apply Finset.sum_le_sum; intro z _ - have hi := fz_norm p ha hα A u z - rcases eq_or_lt_of_le (mass_nonneg _ ha hα _ z) with H | H - · rw [← H,div_zero]; exact norm_nonneg _ - · refine (div_le_iff₀ H).mpr ?_ - nlinarith [norm_nonneg (fz p a α A u z)] - - -def polar (z : ℂ) := ((↑(‖z‖⁻¹:ℝ)) : ℂ) * conj z - -lemma polar_identity (z : ℂ) : (polar z * z).re = ‖z‖ := by - by_cases hz : z=0 - · subst z; simp - · unfold polar - rw [mul_assoc,mul_comm (conj z),Complex.mul_conj,← Complex.sq_norm,← Complex.ofReal_mul, - Complex.ofReal_re] - have hN : ‖z‖≠0 := norm_ne_zero_iff.mpr hz - field_simp - -lemma polar_unit (z : ℂ) : ‖polar z‖ ≤ 1 := by - by_cases hz : z=0 - · subst z; simp [polar] - rw [polar,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (by positivity), - Complex.norm_conj, inv_mul_cancel₀ (norm_ne_zero_iff.mpr hz)] - -lemma piece_child_bias {p : Node} (hp : p.valid) (hv : p.lev < M) - {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 < α) - {P : Law (Grid M)} (hP : PieceLaw p a P) (hm : 0 < p.avg a) (hm' : p.avg a ≤ 2*α) - (A : Form M) (u : Grid M) (B : Bool) {b : ℝ} - (hb : b^2 ≤ energyZ (p.child B) a α A u) : - ∃ v : Grid M → ℂ, (∀ z, ‖v z‖ ≤ 1) ∧ - b^2/4 ≤ P.Ex fun x => if x∈(p.child B).block M then - (channel A x).Ex fun z => (v z*chi u x).re else 0 := by - let f := fz (p.child B) a α A u - let v : Grid M → ℂ := fun z => polar (f z) - let J := P.Ex fun x => if x∈(p.child B).block M then - (channel A x).Ex fun z => (v z*chi u x).re else 0 - have he1 (y z : Grid M) : - omegaObs (p.child B) a α A y z = if y∈(p.child B).block M - then (2*p.avg a / α) * (P y*channel A y z) else 0 := by - rw [omegaObs,wt_half hp hv,hP] - split_ifs - · rw [wt,wt]; field_simp - exact zero_mul _ - have he2 (z : Grid M) : - ‖f z‖ = ∑ y, omegaObs (p.child B) a α A y z * (v z*chi u y).re := by - rw [← polar_identity] - change (v z* (_)).re = _ - dsimp only [f,fz] - rw [Finset.mul_sum,Complex.re_sum] - refine Finset.sum_congr rfl fun i _ => ?_ - rw [mul_left_comm,Complex.re_ofReal_mul] - have he : - ∑ z, ‖f z‖ = (2*p.avg a / α)*J := by - simp_rw [he2,he1] - rw [Finset.sum_comm] - unfold J Ex - rw [Finset.mul_sum] - refine Finset.sum_congr rfl fun i hi => ?_ - by_cases h : i ∈ (child p B).block M - · simp_rw [ite_eq_left h,Finset.mul_sum,mul_assoc] - simp [h] - have h0 := hb.trans (energyZ_le _ ha hα.le _ _) - change b^2 ≤ ∑ z, ‖f z‖ at h0; rw [he] at h0 - have hp1 : 0 < 2*p.avg a/α := by positivity - have hi1 : 2*p.avg a/α ≤ 4 := by apply (div_le_iff₀ hα).mpr; linarith - have hj : 0 ≤ J := nonneg_of_mul_nonneg_right ((sq_nonneg _).trans h0) hp1 - exact ⟨v,fun i => polar_unit _,by change b^2/4 ≤ J; nlinarith⟩ - -end Thermal -end FourierLLogL.Dyadic - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.History + +/-! # Finite input pieces and spectra, using counting measure for the observation coordinate -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic +open Law Node Period Real +namespace Node +variable {M : ℕ} +def child (p : Node) : Bool → Node +| false => p.L +| true => p.R +lemma child_ok (p : Node) (h : p.valid) (hv : p.lev < M) (b) : + ok (p.child b) M := by cases b <;> exact ⟨by first | exact p.valid_L.mpr h | exact p.valid_R.mpr h,hv⟩ +lemma child_len (p : Node) (b : Bool) : (p.child b).len=p.len/2 := by + cases b; exact p.len_L; exact p.len_R +lemma child_below (p : Node) (b : Bool) : (p.child b).below p := by + cases b; exact p.below_left; exact p.below_right +end Node + +namespace Thermal +variable {M : ℕ} +open scoped Classical + +/-- Observation type-A. -/ +def channel (A : Form M) (x : Grid M) : Law (Grid M) := + (label A).send (fun z => x+z) + +lemma channel_apply (A : Form M) (x z : Grid M) : + channel A x z = label A (z-x) := by + rw [channel,send_apply,prob_eq] + simp_rw [add_comm x, ← eq_sub_iff_add_eq] + simp + +lemma ch_K (A : Form M) (x z : Grid M) : + channel A x z = side M * K A (z-x) := by rw [channel_apply,label_apply] + +lemma ch_pos (A : Form M) (x z : Grid M) : 0 < channel A x z := by rw [channel_apply]; exact label_pos _ _ + +lemma ch_ex (A : Form M) (f : Grid M → ℝ) (x : Grid M) : + (channel A x).Ex f = (label A).Ex fun z => f (x+z) := Ex_send _ _ _ + +lemma ch_noise (A : Form M) (f : Grid M → ℝ) (x : Grid M) : + (channel A x).Ex (fun z => f (z-x)) = (label A).Ex f := by + rw [ch_ex]; simp + +/-- Local normalized subdensity (as mass weights, counting-grid). Its total is m/α. -/ +def wt (p : Node) (a : Grid M → ℝ) (α : ℝ) (y : Grid M) : ℝ := + p.dice M y * a y / α + +lemma wt_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) (y) : + 0 ≤ wt p a α y := div_nonneg (mul_nonneg ((p.dice M).nonneg y) (ha y)) hα + +lemma wt_total (p : Node) (hp : p.ok M) (a : Grid M → ℝ) (α : ℝ) : + ∑ y, wt p a α y = p.avg a / α := by simp_rw [wt, ← Finset.sum_div, avg_ex hp,Ex] + +/-- Law of a local piece when normalization is its positive mean; used as proof-carrying +relation to any specified law. -/ +def PieceLaw (p : Node) (a : Grid M → ℝ) (P : Law (Grid M)) := ∀ y, P y = wt p a (p.avg a) y + +lemma make_piece (p : Node) (hp : p.ok M) {a : Grid M → ℝ} (ha : 0 ≤ a) (hm : 0 < p.avg a) : + ∃ P : Law (Grid M), PieceLaw p a P := by + refine ⟨⟨wt p a (p.avg a),wt_nonneg p ha hm.le,by rw [wt_total _ hp,div_self hm.ne']⟩,fun i => rfl⟩ + +lemma piece_support {p : Node} (hp : p.ok M) {a : Grid M → ℝ} + {P : Law (Grid M)} (h : PieceLaw p a P) (x : Grid M) (hx : 0 < P x) : + x ∈ p.block M := by + by_contra hh; rw [h,wt,dice_apply hp,ite_eq_right hh,zero_mul,zero_div] at hx + exact lt_irrefl _ hx + +lemma piece_density {p : Node} {a : Grid M → ℝ} {P : Law (Grid M)} + (h : PieceLaw p a P) {d : ℝ} (h1 : ∀ x, a x ≤ exp d) + (hm : exp (-d) ≤ p.avg a) (x : Grid M) : + P x ≤ exp (2*d)*p.dice M x := by + rw [h,wt] + apply (div_le_iff₀ (lt_of_lt_of_le (exp_pos _) hm)).mpr + have HE : exp (2*d)*exp (-d) = exp d := by rw [← exp_add]; congr; ring + calc + _ ≤ p.dice M x * exp d := by + exact mul_le_mul_of_nonneg_left (h1 _) ((p.dice M).nonneg _) + _ = exp (2*d)*p.dice M x * exp (-d) := by rw [mul_comm _ (p.dice _ _),mul_assoc,HE] + _ ≤ _ := mul_le_mul_of_nonneg_left hm (mul_nonneg (exp_nonneg _) ((p.dice M).nonneg _)) + +lemma wt_half {p : Node} (h : p.valid) (hp : p.lev < M) (a : Grid M → ℝ) (α : ℝ) + (x : Grid M) (b : Bool) : + wt (p.child b) a α x = if x ∈ (p.child b).block M then 2*wt p a α x else 0 := by + rw [wt,dice_apply (child_ok p h hp b)] + split_ifs with hh + · rw [wt,dice_apply ⟨h,hp.le⟩,ite_eq_left (block_of_below (child_below _ _) _ hh),child_len] + field_simp + simp + +/-- Observation law weighted by the local subpiece. -/ +def omegaObs (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (y z : Grid M) := + wt p a α y * channel A y z +def Mz (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (z : Grid M) := + ∑ y, omegaObs p a α A y z +def fz (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (u z : Grid M) : ℂ := + ∑ y, (omegaObs p a α A y z:ℂ) * chi u y +def energyZ (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (u : Grid M) := + ∑ z, ‖fz p a α A u z‖^2 / Mz p a α A z + +def high (p : Node) (a : Grid M → ℝ) (α : ℝ) (A : Form M) (b : ℝ) (u : Grid M) := + ∃ B, b^2 ≤ energyZ (p.child B) a α A u + +lemma density_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) + (A : Form M) (y z : Grid M) : 0 ≤ omegaObs p a α A y z := + mul_nonneg (wt_nonneg p ha hα _) ((channel A y).nonneg z) + +lemma mass_nonneg (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) + (A : Form M) (z : Grid M) : 0 ≤ Mz p a α A z := + Finset.sum_nonneg fun _ _ => density_nonneg _ ha hα _ _ _ + +lemma mass_total (p : Node) (hp : p.ok M) (a : Grid M → ℝ) (α : ℝ) + (A : Form M) : ∑ z, Mz p a α A z = p.avg a / α := by + unfold Mz; rw [Finset.sum_comm]; simp_rw [omegaObs,← Finset.mul_sum,Law.total,mul_one] + rw [wt_total _ hp] + +lemma fz_norm (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) + (A : Form M) (u z : Grid M) : ‖fz p a α A u z‖ ≤ Mz p a α A z := by + apply le_trans (norm_sum_le _ _) + simp_rw [norm_mul, Complex.norm_real,norm_chi,mul_one, Real.norm_eq_abs, + abs_of_nonneg (density_nonneg _ ha hα _ _ _)] + exact le_rfl + +lemma energyZ_le (p : Node) {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 ≤ α) + (A : Form M) (u : Grid M) : energyZ p a α A u ≤ ∑ z, ‖fz p a α A u z‖ := by + apply Finset.sum_le_sum; intro z _ + have hi := fz_norm p ha hα A u z + rcases eq_or_lt_of_le (mass_nonneg _ ha hα _ z) with H | H + · rw [← H,div_zero]; exact norm_nonneg _ + · refine (div_le_iff₀ H).mpr ?_ + nlinarith [norm_nonneg (fz p a α A u z)] + + +def polar (z : ℂ) := ((↑(‖z‖⁻¹:ℝ)) : ℂ) * conj z + +lemma polar_identity (z : ℂ) : (polar z * z).re = ‖z‖ := by + by_cases hz : z=0 + · subst z; simp + · unfold polar + rw [mul_assoc,mul_comm (conj z),Complex.mul_conj,← Complex.sq_norm,← Complex.ofReal_mul, + Complex.ofReal_re] + have hN : ‖z‖≠0 := norm_ne_zero_iff.mpr hz + field_simp + +lemma polar_unit (z : ℂ) : ‖polar z‖ ≤ 1 := by + by_cases hz : z=0 + · subst z; simp [polar] + rw [polar,norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_nonneg (by positivity), + Complex.norm_conj, inv_mul_cancel₀ (norm_ne_zero_iff.mpr hz)] + +lemma piece_child_bias {p : Node} (hp : p.valid) (hv : p.lev < M) + {a : Grid M → ℝ} (ha : 0 ≤ a) {α : ℝ} (hα : 0 < α) + {P : Law (Grid M)} (hP : PieceLaw p a P) (hm : 0 < p.avg a) (hm' : p.avg a ≤ 2*α) + (A : Form M) (u : Grid M) (B : Bool) {b : ℝ} + (hb : b^2 ≤ energyZ (p.child B) a α A u) : + ∃ v : Grid M → ℂ, (∀ z, ‖v z‖ ≤ 1) ∧ + b^2/4 ≤ P.Ex fun x => if x∈(p.child B).block M then + (channel A x).Ex fun z => (v z*chi u x).re else 0 := by + let f := fz (p.child B) a α A u + let v : Grid M → ℂ := fun z => polar (f z) + let J := P.Ex fun x => if x∈(p.child B).block M then + (channel A x).Ex fun z => (v z*chi u x).re else 0 + have he1 (y z : Grid M) : + omegaObs (p.child B) a α A y z = if y∈(p.child B).block M + then (2*p.avg a / α) * (P y*channel A y z) else 0 := by + rw [omegaObs,wt_half hp hv,hP] + split_ifs + · rw [wt,wt]; field_simp + exact zero_mul _ + have he2 (z : Grid M) : + ‖f z‖ = ∑ y, omegaObs (p.child B) a α A y z * (v z*chi u y).re := by + rw [← polar_identity] + change (v z* (_)).re = _ + dsimp only [f,fz] + rw [Finset.mul_sum,Complex.re_sum] + refine Finset.sum_congr rfl fun i _ => ?_ + rw [mul_left_comm,Complex.re_ofReal_mul] + have he : + ∑ z, ‖f z‖ = (2*p.avg a / α)*J := by + simp_rw [he2,he1] + rw [Finset.sum_comm] + unfold J Ex + rw [Finset.mul_sum] + refine Finset.sum_congr rfl fun i hi => ?_ + by_cases h : i ∈ (child p B).block M + · simp_rw [ite_eq_left h,Finset.mul_sum,mul_assoc] + simp [h] + have h0 := hb.trans (energyZ_le _ ha hα.le _ _) + change b^2 ≤ ∑ z, ‖f z‖ at h0; rw [he] at h0 + have hp1 : 0 < 2*p.avg a/α := by positivity + have hi1 : 2*p.avg a/α ≤ 4 := by apply (div_le_iff₀ hα).mpr; linarith + have hj : 0 ≤ J := nonneg_of_mul_nonneg_right ((sq_nonneg _).trans h0) hp1 + exact ⟨v,fun i => polar_unit _,by change b^2/4 ≤ J; nlinarith⟩ + +end Thermal +end FourierLLogL.Dyadic + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Positive.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Positive.lean index ecf5467ed67936b8342e505bdd5e91b2c0e1e013..be2902dd744098676c84be5941eb47ba3950bcaa 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Positive.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Positive.lean @@ -1,179 +1,179 @@ -import OAI.Analysis.FourierLLogL.Thermal.Form - -/-! # Positive Fourier expansions -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section - -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Real Period - -variable {M : ℕ} - -private def ac (f : Grid M → ℝ) (x : Grid M) := (fc (fun y => (↑(f y):ℂ)) x).re -private lemma ac_mul (f g : Grid M → ℝ) (hf : f.Even) (hg : g.Even) (x : Grid M) : - ac (fun y => f y*g y) x = ∑ u : Grid M, ac f u * ac g (x-u) := by - unfold ac - simp_rw [Complex.ofReal_mul]; rw [fc_prod,Complex.re_sum] - apply Finset.sum_congr rfl; intro i _ - rw [← real_fc_even f hf i,← real_fc_even g hg (x-i)] - simp - -private lemma ac_scale (f : Grid M → ℝ) (a : ℝ) (x : Grid M) : - ac (fun y => a*f y) x = a*ac f x := by - simp_rw [ac,Complex.ofReal_mul] - rw [fc_lmul]; simp - -private lemma ac_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → ℝ) (x : Grid M) : - ac (fun y => ∑ i ∈ s, f i y) x = ∑ i ∈ s, ac (f i) x := by - simp_rw [ac,Complex.ofReal_sum]; rw [fc_sum,Complex.re_sum] - -private lemma ac_const (x : Grid M) : ac (fun _ => 1) x = - if x=0 then 1 else 0 := by - unfold ac - simp_rw [Complex.ofReal_one] - rw [← funext chi_zero_left,fc_chi] - split_ifs <;> simp_all - -private lemma ac_wave (s x : Grid M) : - ac (wave s) x = ((if s=x then 1 else 0)+(if -s=x then 1 else 0))/2 := by - have he (y : Grid M) : (↑(wave s y):ℂ) = 2⁻¹*(chi s y + chi (-s) y) := by - rw [wave,chi_neg'] - rw [Complex.re_eq_add_conj] - ring - unfold ac; simp_rw [he] - rw [fc_lmul,fc_add,fc_chi,fc_chi] - split_ifs <;> norm_num - -/-- cosine series Taylor argument -/ -private def blend (A : Form M) (y : Grid M) := ∑ i, A i * wave i y - -private lemma blend_even (A : Form M) : (blend A).Even := by intro i; simp [blend,wave_even _ i] -private lemma blend_boltz (A : Form M) (x : Grid M) : - boltz A x = exp (-(∑ i, A i)) * exp (blend A x) := by - simp_rw [boltz,form,blend,cost,mul_sub,Finset.sum_sub_distrib,mul_one,← exp_add] - congr 1; ring - -private lemma blend_spec (A : Form M) (x : Grid M) : - ac (blend A) x = ∑ i, A i * ac (wave i) x := by - unfold blend - simp_rw [ac_sum,ac_scale] - -private lemma blend_ac_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : - 0 ≤ ac (blend A) x := by - rw [blend_spec] - apply Finset.sum_nonneg; intro i _ - apply mul_nonneg (hA i) - rw [ac_wave]; split_ifs <;> norm_num - -private lemma blend_ac_pos {A : Form M} (hA : 0 ≤ A) {x : Grid M} - (hx : 0 < A x) : 0 < ac (blend A) x := by - rw [blend_spec] - apply lt_of_lt_of_le (b:=A x * ac (wave x) x) - · apply mul_pos hx; rw [ac_wave,ite_eq_left rfl]; split_ifs <;> norm_num - apply Finset.single_le_sum (f:=fun i => A i * ac (wave i) x) _ (Finset.mem_univ x) - intro i _ - apply mul_nonneg (hA i); rw [ac_wave]; split_ifs <;> norm_num - -private lemma blend_pow_even (A : Form M) (n : ℕ) : (fun y => blend A y^n).Even := - fun y => by dsimp; rw [blend_even _ y] - -private lemma blend_pow_ac_nonneg {A : Form M} (hA : 0 ≤ A) (n : ℕ) (x : Grid M) : - 0 ≤ ac (fun y => blend A y^n) x := by - induction n generalizing x with - | zero => simp_rw [pow_zero,ac_const]; split_ifs <;> norm_num - | succ n ih => - simp_rw [pow_succ]; rw [ac_mul _ _ (blend_pow_even _ _) (blend_even _)] - exact Finset.sum_nonneg fun i _ => mul_nonneg (ih i) (blend_ac_nonneg hA _) - -private lemma blend_pow_ac_pos {A : Form M} (hA : 0 ≤ A) (n : ℕ) {x : Grid M} - (hx : 0 < A x) : 0 < ac (fun y => blend A y^n) (n • x) := by - induction n with - | zero => simp [ac_const] - | succ n ih => - simp_rw [pow_succ]; rw [ac_mul _ _ (blend_pow_even _ _) (blend_even _)] - apply lt_of_lt_of_le (b:=ac (fun y => blend A y ^n) (n • x)*ac (blend A) ((n+1)•x-n•x)) - · rw [add_smul,one_smul,add_sub_cancel_left] - exact mul_pos ih (blend_ac_pos hA hx) - apply Finset.single_le_sum (f:=fun u => ac (fun y => blend A y ^n) u * ac (blend A) ((n+1)•x-u)) _ (Finset.mem_univ (n•x)) - intro i _ - exact mul_nonneg (blend_pow_ac_nonneg hA n _) (blend_ac_nonneg hA _) - -/-- Exp series truncated. -/ -def aprox (n : ℕ) (v : ℝ) := ∑ i ∈ Finset.range n, (i.factorial:ℝ)⁻¹*v^i - -lemma aprox_lim (v : ℝ) : Tendsto (fun n : ℕ => aprox n v) atTop (𝓝 (exp v)) := by - simpa only [aprox, Real.exp_eq_exp_ℝ,div_eq_inv_mul] using - (NormedSpace.expSeries_div_hasSum_exp v).tendsto_sum_nat - -private lemma ac_lim (f : ℕ → Grid M → ℝ) (g : Grid M → ℝ) (x : Grid M) - (h : ∀ y, Tendsto (fun n => f n y) atTop (𝓝 (g y))) : - Tendsto (fun n => ac (f n) x) atTop (𝓝 (ac g x)) := by - unfold ac fc expU - apply Complex.continuous_re.continuousAt.tendsto.comp - simp only [Complex.real_smul] - refine Tendsto.const_mul _ <| tendsto_finsetSum _ ?_ - intro y _ - exact Tendsto.mul (Complex.continuous_ofReal.continuousAt.tendsto.comp (h y)) tendsto_const_nhds - -private lemma blend_part_le_exp {A : Form M} (hA : 0 ≤ A) (x : Grid M) (n : ℕ) : - (n.factorial:ℝ)⁻¹ * ac (fun y => blend A y ^ n) x ≤ - ac (fun y => exp (blend A y)) x := by - apply ge_of_tendsto (ac_lim (fun n y => aprox n (blend A y)) _ x - (fun y => aprox_lim _)) - filter_upwards [eventually_ge_atTop (n+1)] with k hk - simp_rw [aprox,ac_sum,ac_scale] - apply Finset.single_le_sum _ (Finset.mem_range.mpr hk) - intro i _ - exact mul_nonneg (by positivity) (blend_pow_ac_nonneg hA _ _) - -lemma q_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : 0 ≤ q A x := by - change 0 ≤ ac (boltz A) x - rw [show boltz A = _ from funext (blend_boltz A),ac_scale] - exact mul_nonneg (exp_nonneg _) (le_trans - (mul_nonneg (by positivity) (blend_pow_ac_nonneg hA 0 _)) (blend_part_le_exp hA x 0)) - -lemma q_pos_one {A : Form M} (hA : 0 ≤ A) (h : 0 < A 1) (x : Grid M) : 0 < q A x := by - change 0 < ac (boltz A) x - rw [show boltz A = _ from funext (blend_boltz A),ac_scale] - apply mul_pos (exp_pos _) - apply lt_of_lt_of_le _ (blend_part_le_exp hA x x.val) - have hi : 0 < ac (fun y => blend A y ^ x.val) (x.val • 1) := blend_pow_ac_pos hA _ h - rw [nsmul_one, ZMod.natCast_zmod_val] at hi - exact mul_pos (by positivity) hi - -lemma cred_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : 0 ≤ cred A x := - div_nonneg (q_nonneg hA x) (q_origin_pos A).le - -lemma norm_fc_translate_boltz {A : Form M} (hA : 0 ≤ A) (a u : Grid M) : - ‖fc (fun y => (↑(boltz A (a-y)):ℂ)) u‖ = q A u := by - have hi (y : Grid M) : boltz A (a-y) = boltz A (y-a) := by - rw [show a-y = -(y-a) by ring,boltz_even _ _] - simp_rw [hi] - rw [fc_right_translate (fun y => (↑(boltz A y):ℂ)) a u,norm_mul,norm_chi,one_mul,← cq, Complex.norm_real, - Real.norm_eq_abs, abs_of_nonneg (q_nonneg hA _)] - -lemma envelope {ι : Type*} (s : Finset ι) (f : ι → Form M) (h : ∀ i ∈ s, 0 ≤ f i) - (a : ι → Grid M) (u : Grid M) : - ‖fc (fun y => ∏ i ∈ s, (↑(boltz (f i) (a i-y)):ℂ)) u‖ ≤ q (∑ i ∈ s, f i) u := by - classical - induction s using Finset.induction_on generalizing u with - | empty => - apply le_of_eq - convert norm_fc_translate_boltz (M:=M) (le_refl (0:Form M)) 0 u using 1 <;> - simp [boltz,form_zero] - | @insert i s hi ih => - have H := h i (Finset.mem_insert_self _ _) - have HH (j) (hj : j∈s) : 0 ≤ f j := h j (Finset.mem_insert_of_mem hj) - simp_rw [Finset.sum_insert hi, Finset.prod_insert hi] - rw [q_add,fc_prod] - apply (norm_sum_le _ _).trans - refine Finset.sum_le_sum fun j _ => ?_ - rw [norm_mul,norm_fc_translate_boltz H] - exact mul_le_mul_of_nonneg_left (ih HH _) (q_nonneg H j) - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Form + +/-! # Positive Fourier expansions -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section + +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Real Period + +variable {M : ℕ} + +private def ac (f : Grid M → ℝ) (x : Grid M) := (fc (fun y => (↑(f y):ℂ)) x).re +private lemma ac_mul (f g : Grid M → ℝ) (hf : f.Even) (hg : g.Even) (x : Grid M) : + ac (fun y => f y*g y) x = ∑ u : Grid M, ac f u * ac g (x-u) := by + unfold ac + simp_rw [Complex.ofReal_mul]; rw [fc_prod,Complex.re_sum] + apply Finset.sum_congr rfl; intro i _ + rw [← real_fc_even f hf i,← real_fc_even g hg (x-i)] + simp + +private lemma ac_scale (f : Grid M → ℝ) (a : ℝ) (x : Grid M) : + ac (fun y => a*f y) x = a*ac f x := by + simp_rw [ac,Complex.ofReal_mul] + rw [fc_lmul]; simp + +private lemma ac_sum {ι : Type*} (s : Finset ι) (f : ι → Grid M → ℝ) (x : Grid M) : + ac (fun y => ∑ i ∈ s, f i y) x = ∑ i ∈ s, ac (f i) x := by + simp_rw [ac,Complex.ofReal_sum]; rw [fc_sum,Complex.re_sum] + +private lemma ac_const (x : Grid M) : ac (fun _ => 1) x = + if x=0 then 1 else 0 := by + unfold ac + simp_rw [Complex.ofReal_one] + rw [← funext chi_zero_left,fc_chi] + split_ifs <;> simp_all + +private lemma ac_wave (s x : Grid M) : + ac (wave s) x = ((if s=x then 1 else 0)+(if -s=x then 1 else 0))/2 := by + have he (y : Grid M) : (↑(wave s y):ℂ) = 2⁻¹*(chi s y + chi (-s) y) := by + rw [wave,chi_neg'] + rw [Complex.re_eq_add_conj] + ring + unfold ac; simp_rw [he] + rw [fc_lmul,fc_add,fc_chi,fc_chi] + split_ifs <;> norm_num + +/-- cosine series Taylor argument -/ +private def blend (A : Form M) (y : Grid M) := ∑ i, A i * wave i y + +private lemma blend_even (A : Form M) : (blend A).Even := by intro i; simp [blend,wave_even _ i] +private lemma blend_boltz (A : Form M) (x : Grid M) : + boltz A x = exp (-(∑ i, A i)) * exp (blend A x) := by + simp_rw [boltz,form,blend,cost,mul_sub,Finset.sum_sub_distrib,mul_one,← exp_add] + congr 1; ring + +private lemma blend_spec (A : Form M) (x : Grid M) : + ac (blend A) x = ∑ i, A i * ac (wave i) x := by + unfold blend + simp_rw [ac_sum,ac_scale] + +private lemma blend_ac_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : + 0 ≤ ac (blend A) x := by + rw [blend_spec] + apply Finset.sum_nonneg; intro i _ + apply mul_nonneg (hA i) + rw [ac_wave]; split_ifs <;> norm_num + +private lemma blend_ac_pos {A : Form M} (hA : 0 ≤ A) {x : Grid M} + (hx : 0 < A x) : 0 < ac (blend A) x := by + rw [blend_spec] + apply lt_of_lt_of_le (b:=A x * ac (wave x) x) + · apply mul_pos hx; rw [ac_wave,ite_eq_left rfl]; split_ifs <;> norm_num + apply Finset.single_le_sum (f:=fun i => A i * ac (wave i) x) _ (Finset.mem_univ x) + intro i _ + apply mul_nonneg (hA i); rw [ac_wave]; split_ifs <;> norm_num + +private lemma blend_pow_even (A : Form M) (n : ℕ) : (fun y => blend A y^n).Even := + fun y => by dsimp; rw [blend_even _ y] + +private lemma blend_pow_ac_nonneg {A : Form M} (hA : 0 ≤ A) (n : ℕ) (x : Grid M) : + 0 ≤ ac (fun y => blend A y^n) x := by + induction n generalizing x with + | zero => simp_rw [pow_zero,ac_const]; split_ifs <;> norm_num + | succ n ih => + simp_rw [pow_succ]; rw [ac_mul _ _ (blend_pow_even _ _) (blend_even _)] + exact Finset.sum_nonneg fun i _ => mul_nonneg (ih i) (blend_ac_nonneg hA _) + +private lemma blend_pow_ac_pos {A : Form M} (hA : 0 ≤ A) (n : ℕ) {x : Grid M} + (hx : 0 < A x) : 0 < ac (fun y => blend A y^n) (n • x) := by + induction n with + | zero => simp [ac_const] + | succ n ih => + simp_rw [pow_succ]; rw [ac_mul _ _ (blend_pow_even _ _) (blend_even _)] + apply lt_of_lt_of_le (b:=ac (fun y => blend A y ^n) (n • x)*ac (blend A) ((n+1)•x-n•x)) + · rw [add_smul,one_smul,add_sub_cancel_left] + exact mul_pos ih (blend_ac_pos hA hx) + apply Finset.single_le_sum (f:=fun u => ac (fun y => blend A y ^n) u * ac (blend A) ((n+1)•x-u)) _ (Finset.mem_univ (n•x)) + intro i _ + exact mul_nonneg (blend_pow_ac_nonneg hA n _) (blend_ac_nonneg hA _) + +/-- Exp series truncated. -/ +def aprox (n : ℕ) (v : ℝ) := ∑ i ∈ Finset.range n, (i.factorial:ℝ)⁻¹*v^i + +lemma aprox_lim (v : ℝ) : Tendsto (fun n : ℕ => aprox n v) atTop (𝓝 (exp v)) := by + simpa only [aprox, Real.exp_eq_exp_ℝ,div_eq_inv_mul] using + (NormedSpace.expSeries_div_hasSum_exp v).tendsto_sum_nat + +private lemma ac_lim (f : ℕ → Grid M → ℝ) (g : Grid M → ℝ) (x : Grid M) + (h : ∀ y, Tendsto (fun n => f n y) atTop (𝓝 (g y))) : + Tendsto (fun n => ac (f n) x) atTop (𝓝 (ac g x)) := by + unfold ac fc expU + apply Complex.continuous_re.continuousAt.tendsto.comp + simp only [Complex.real_smul] + refine Tendsto.const_mul _ <| tendsto_finsetSum _ ?_ + intro y _ + exact Tendsto.mul (Complex.continuous_ofReal.continuousAt.tendsto.comp (h y)) tendsto_const_nhds + +private lemma blend_part_le_exp {A : Form M} (hA : 0 ≤ A) (x : Grid M) (n : ℕ) : + (n.factorial:ℝ)⁻¹ * ac (fun y => blend A y ^ n) x ≤ + ac (fun y => exp (blend A y)) x := by + apply ge_of_tendsto (ac_lim (fun n y => aprox n (blend A y)) _ x + (fun y => aprox_lim _)) + filter_upwards [eventually_ge_atTop (n+1)] with k hk + simp_rw [aprox,ac_sum,ac_scale] + apply Finset.single_le_sum _ (Finset.mem_range.mpr hk) + intro i _ + exact mul_nonneg (by positivity) (blend_pow_ac_nonneg hA _ _) + +lemma q_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : 0 ≤ q A x := by + change 0 ≤ ac (boltz A) x + rw [show boltz A = _ from funext (blend_boltz A),ac_scale] + exact mul_nonneg (exp_nonneg _) (le_trans + (mul_nonneg (by positivity) (blend_pow_ac_nonneg hA 0 _)) (blend_part_le_exp hA x 0)) + +lemma q_pos_one {A : Form M} (hA : 0 ≤ A) (h : 0 < A 1) (x : Grid M) : 0 < q A x := by + change 0 < ac (boltz A) x + rw [show boltz A = _ from funext (blend_boltz A),ac_scale] + apply mul_pos (exp_pos _) + apply lt_of_lt_of_le _ (blend_part_le_exp hA x x.val) + have hi : 0 < ac (fun y => blend A y ^ x.val) (x.val • 1) := blend_pow_ac_pos hA _ h + rw [nsmul_one, ZMod.natCast_zmod_val] at hi + exact mul_pos (by positivity) hi + +lemma cred_nonneg {A : Form M} (hA : 0 ≤ A) (x : Grid M) : 0 ≤ cred A x := + div_nonneg (q_nonneg hA x) (q_origin_pos A).le + +lemma norm_fc_translate_boltz {A : Form M} (hA : 0 ≤ A) (a u : Grid M) : + ‖fc (fun y => (↑(boltz A (a-y)):ℂ)) u‖ = q A u := by + have hi (y : Grid M) : boltz A (a-y) = boltz A (y-a) := by + rw [show a-y = -(y-a) by ring,boltz_even _ _] + simp_rw [hi] + rw [fc_right_translate (fun y => (↑(boltz A y):ℂ)) a u,norm_mul,norm_chi,one_mul,← cq, Complex.norm_real, + Real.norm_eq_abs, abs_of_nonneg (q_nonneg hA _)] + +lemma envelope {ι : Type*} (s : Finset ι) (f : ι → Form M) (h : ∀ i ∈ s, 0 ≤ f i) + (a : ι → Grid M) (u : Grid M) : + ‖fc (fun y => ∏ i ∈ s, (↑(boltz (f i) (a i-y)):ℂ)) u‖ ≤ q (∑ i ∈ s, f i) u := by + classical + induction s using Finset.induction_on generalizing u with + | empty => + apply le_of_eq + convert norm_fc_translate_boltz (M:=M) (le_refl (0:Form M)) 0 u using 1 <;> + simp [boltz,form_zero] + | @insert i s hi ih => + have H := h i (Finset.mem_insert_self _ _) + have HH (j) (hj : j∈s) : 0 ≤ f j := h j (Finset.mem_insert_of_mem hj) + simp_rw [Finset.sum_insert hi, Finset.prod_insert hi] + rw [q_add,fc_prod] + apply (norm_sum_le _ _).trans + refine Finset.sum_le_sum fun j _ => ?_ + rw [norm_mul,norm_fc_translate_boltz H] + exact mul_le_mul_of_nonneg_left (ih HH _) (q_nonneg H j) + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Posterior.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Posterior.lean index 0b75f5d63176512fef6d3ce0821fb363173758e4..8c4dc1879cf30d3be2902f06f63b9fc4aa243a8f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Posterior.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Posterior.lean @@ -1,203 +1,203 @@ -import OAI.Analysis.FourierLLogL.Thermal.Update - -/-! # Past observation profiles and posterior lower bound -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M : ℕ} -open scoped Classical - -abbrev Profile (M : ℕ) := List (Form M × Grid M) - -/-- Energy of a history against test input. -/ -def sumE (s : Profile M) (x : Grid M) : ℝ := (s.map fun t => form t.1 (t.2-x)).sum -def mixForm (s : Profile M) : Form M := (s.map Prod.fst).sum -def tune (s : Profile M) (x : Grid M) := exp (-sumE s x) - -def admissible (s : Profile M) (p : Node) (r : ℝ) := - ∃ x, x ∈ p.block M ∧ sumE s x ≤ r - -def prefixE (s : Profile M) (p : Node) (x : Grid M) := if x∈p.block M then tune s x else 0 - -lemma tune_pos (s : Profile M) (x : Grid M) : 0 < tune s x := exp_pos _ -lemma prefixE_nonneg (s : Profile M) (p : Node) (x : Grid M) : 0 ≤ prefixE s p x := by - unfold prefixE; split_ifs - exact (tune_pos _ _).le - exact le_rfl - -lemma tune_cons (s : Profile M) (A : Form M) (x y : Grid M) : - tune ((A,x)::s) y = boltz A (x-y)*tune s y := by - simp only [tune,sumE,List.map_cons,List.sum_cons,boltz, ← exp_add] - congr 1; ring - -lemma mix_cons (s : Profile M) (A : Form M) (x : Grid M) : - mixForm ((A,x)::s) = A+mixForm s := by simp [mixForm] - -lemma mix_bound {s : Profile M} {A : Form M} (h : ∀ i∈s, i.1 ≤ A) (x : Grid M) : - mixForm s x ≤ s.length*A x := by - induction s with - | nil => - unfold mixForm - simp - | cons i s ih => - obtain ⟨a,y⟩ := i - rw [mix_cons] - have hx := ih (by intro i hi; exact h i (List.mem_cons_of_mem _ hi)) - have hh := h (a,y) List.mem_cons_self x - simp only [Pi.add_apply,List.length_cons] - change a x ≤ _ at hh - push_cast; linarith - -lemma mix_nonneg {s : Profile M} (h : ∀ i∈s, 0 ≤ i.1) : - 0 ≤ mixForm s := by - induction s with - | nil => simp [mixForm] - | cons i s ih => - obtain ⟨a,y⟩ := i; rw [mix_cons] - exact add_nonneg (h (a,y) List.mem_cons_self) (ih fun i hi => h i (List.mem_cons_of_mem _ hi)) - -lemma sumE_compare {s : Profile M} (x y : Grid M) (h : ∀ i∈s, 0 ≤ i.1) : - sumE s y ≤ 2*(sumE s x + form (mixForm s) (x-y)) := by - induction s with - | nil => simp [mixForm,sumE,form_zero] - | cons i s ih => - obtain ⟨a,z⟩ := i - have hh := ih (fun i hi => h i (List.mem_cons_of_mem _ hi)) - have hx : 0 ≤ a := h (a,z) List.mem_cons_self - simp only [mix_cons, sumE, List.map_cons, List.sum_cons, form_add] - unfold sumE at hh - have H := form_triangle hx (z-x) (x-y) - rw [sub_add_sub_cancel] at H - linarith - -lemma profile_envelope {s : Profile M} (h : ∀ i∈s, 0 ≤ i.1) (x : Grid M) : - ‖fc (fun y => (↑(tune s y):ℂ)) x‖ ≤ q (mixForm s) x := by - induction s generalizing x with - | nil => - rw [show mixForm ([]:Profile M)=0 from by simp [mixForm]] - apply le_of_eq - convert norm_fc_translate_boltz (M:=M) (le_refl (0:Form M)) 0 x using 1 - simp [boltz,tune,sumE,form_zero] - | cons i s ih => - obtain ⟨a,y⟩ := i - simp_rw [mix_cons,tune_cons,Complex.ofReal_mul,fc_prod,q_add] - apply le_trans (norm_sum_le _ _) - refine Finset.sum_le_sum fun i _ => ?_ - have hx : 0 ≤ a := h (a,y) List.mem_cons_self - have hh := ih (fun i hi => h i (List.mem_cons_of_mem _ hi)) (x-i) - rw [norm_mul,norm_fc_translate_boltz hx] - exact mul_le_mul_of_nonneg_left hh (q_nonneg hx _) - -private lemma close_kept {p : Node} (hp : p.ok M) {x v : Grid M} (hx : x ∈ p.block M) - (h : Dyadic.diam v < p.len/2) : - x+v ∈ p.block M ∨ x-v ∈ p.block M := by - have he (v : Grid M) (hv : pos v < p.len/2) : - x+v ∈ p.block M ∨ x-v ∈ p.block M := by - have hh1 : p.idx M ≤ x.val ∧ x.val < p.idx M+p.wid M := by - rw [mem_block_iff hp.2] at hx; unfold idx wid; rw [add_mul,one_mul] at hx; exact hx - have hh2 : 2*v.val < p.wid M := by - have H := side_pos M - have he : p.len = (p.wid M : ℝ)*side M := by - unfold wid; rw [descale hp.2]; push_cast; ring - rw [he,pos] at hv - have hi : 2*(v.val : ℝ)< p.wid M := by nlinarith - exact_mod_cast hi - have heq (x : Grid M) : - x∈p.block M ↔ p.idx M ≤ x.val ∧ x.val < p.idx M+p.wid M := by - unfold idx wid; rw [mem_block_iff hp.2,add_mul,one_mul] - rw [heq,heq] - by_cases H : x.val+v.val < p.idx M+p.wid M - · left; rw [ZMod.val_add_of_lt (H.trans_le (p.fitting hp.1 hp.2))] - omega - right; rw [ZMod.val_sub (by omega : v.val ≤ x.val)] - omega - rw [diam_min,min_lt_iff] at h - rcases h with h|h - · exact he v h - · simpa only [sub_neg_eq_add,← sub_eq_add_neg] using (he (-v) h).symm - -private lemma even_prob (A : Form M) (p : Grid M → Prop) : - (label A).prob (fun x => p (-x)) = (label A).prob p := by - rw [prob_eq,prob_eq] - apply Fintype.sum_equiv (Equiv.neg (Grid M)) - intro i - simp only [Equiv.neg_apply,label_apply,K_even _ _] - -private lemma label_node_inside {p : Node} (hp : p.ok M) {A : Form M} - (hA : S M (1000*p.invlen) ≤ A) (h : 1000*p.invlen ≤ 2^M) - (x : Grid M) (hx : x∈p.block M) : - 1/4 ≤ (label A).prob (fun z => x+z ∈ p.block M) := by - let R := p.invlen; let r := p.len/2; let P := label A - let u (z : Grid M) : Prop := Dyadic.diam z < r - let v (z : Grid M) : Prop := x+z ∈ p.block M - have hR : 1 ≤ R := p.invlen_ge - have hr : 0 < r := by unfold r; have := p.len_pos; positivity - have he : P.prob (fun x => ¬ u x) ≤ 1/2 := by - have he : (P.prob (fun x => ¬u x))*r^2 ≤ 100/(1000*R)^2 := by - apply le_trans _ (S_variance _ (by linarith) h _ hA) - unfold prob - rw [← Ex_mul] - refine Ex_mono _ fun i => ?_ - dsimp only; split_ifs with H - · rw [zero_mul]; positivity - · rw [one_mul] - apply pow_le_pow_left₀ hr.le _ - exact not_lt.mp H - have ht : 100/(1000*R)^2 ≤ (1/2)*r^2 := by - unfold R r; rw [rinv] - have h0 := p.len_pos - field_simp; nlinarith - exact le_of_mul_le_mul_right (he.trans ht) (by positivity) - have hb : P.prob u ≤ P.prob v + (P.prob fun z => v (-z)) := by - unfold prob; rw [← Ex_add]; refine Ex_mono _ fun i => ?_ - by_cases hi : u i - · have HH : v i ∨ v (-i) := by - simpa only [v,sub_eq_add_neg] using close_kept hp hx hi - rcases HH with HH|HH <;> simp only [hi,HH,ite_true] <;> - split_ifs <;> norm_num - · simp only [hi,ite_false]; split_ifs <;> norm_num - rw [prob_compl] at he - rw [even_prob A v] at hb - change _ ≤ P.prob v; linarith - -lemma posterior_denominator {p : Node} (hp : p.ok M) {A : Form M} - (hA : S M (1000*p.invlen) ≤ A) (h : 1000*p.invlen ≤ 2^M) - (s : Profile M) {r : ℝ} (hr : admissible s p r) - (hn : ∀ i∈s, 0 ≤ i.1) (hh : (2:ℝ) • mixForm s ≤ A) : - exp (- (2*r)-F A)/4 ≤ expU (prefixE s p) := by - obtain ⟨x,hx,h1⟩ := hr - let Q := label A - let c := exp (- (2*r)-F A) - have hc : 0 < c := exp_pos _ - have HE (y : Grid M) : c*(K A y * (if x+y∈p.block M then 1 else 0)) ≤ - prefixE s p (x+y) := by - unfold prefixE; split_ifs with hiv - · rw [mul_one,K_eq,tune] - unfold c; rw [← exp_add]; apply Real.exp_monotone - have hi := sumE_compare x (x+y) hn - have h2 := form_mono hh (x-(x+y)) - rw [form_smul, show x-(x+y)=-y by ring,form_even A y] at h2 - rw [show x-(x+y)=-y by ring] at hi - linarith - · simp - have hh' := Ex_mono (uniform _) HE - simp only [Ex_lmul,← expU_ex,expU_translate,← label_ex] at hh' - apply le_trans _ hh' - have hh2 := label_node_inside hp hA h x hx - let f (y : Grid M) := if x+y∈p.block M then (1:ℝ) else 0 - have Heq : (label A).prob (fun z => x+z∈p.block M)=(label A).Ex f := by - - rw [prob_eq]; simp [f,Ex] - rw [Heq] at hh2 - change c/4 ≤ c*(label A).Ex f - nlinarith - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Update + +/-! # Past observation profiles and posterior lower bound -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +variable {M : ℕ} +open scoped Classical + +abbrev Profile (M : ℕ) := List (Form M × Grid M) + +/-- Energy of a history against test input. -/ +def sumE (s : Profile M) (x : Grid M) : ℝ := (s.map fun t => form t.1 (t.2-x)).sum +def mixForm (s : Profile M) : Form M := (s.map Prod.fst).sum +def tune (s : Profile M) (x : Grid M) := exp (-sumE s x) + +def admissible (s : Profile M) (p : Node) (r : ℝ) := + ∃ x, x ∈ p.block M ∧ sumE s x ≤ r + +def prefixE (s : Profile M) (p : Node) (x : Grid M) := if x∈p.block M then tune s x else 0 + +lemma tune_pos (s : Profile M) (x : Grid M) : 0 < tune s x := exp_pos _ +lemma prefixE_nonneg (s : Profile M) (p : Node) (x : Grid M) : 0 ≤ prefixE s p x := by + unfold prefixE; split_ifs + exact (tune_pos _ _).le + exact le_rfl + +lemma tune_cons (s : Profile M) (A : Form M) (x y : Grid M) : + tune ((A,x)::s) y = boltz A (x-y)*tune s y := by + simp only [tune,sumE,List.map_cons,List.sum_cons,boltz, ← exp_add] + congr 1; ring + +lemma mix_cons (s : Profile M) (A : Form M) (x : Grid M) : + mixForm ((A,x)::s) = A+mixForm s := by simp [mixForm] + +lemma mix_bound {s : Profile M} {A : Form M} (h : ∀ i∈s, i.1 ≤ A) (x : Grid M) : + mixForm s x ≤ s.length*A x := by + induction s with + | nil => + unfold mixForm + simp + | cons i s ih => + obtain ⟨a,y⟩ := i + rw [mix_cons] + have hx := ih (by intro i hi; exact h i (List.mem_cons_of_mem _ hi)) + have hh := h (a,y) List.mem_cons_self x + simp only [Pi.add_apply,List.length_cons] + change a x ≤ _ at hh + push_cast; linarith + +lemma mix_nonneg {s : Profile M} (h : ∀ i∈s, 0 ≤ i.1) : + 0 ≤ mixForm s := by + induction s with + | nil => simp [mixForm] + | cons i s ih => + obtain ⟨a,y⟩ := i; rw [mix_cons] + exact add_nonneg (h (a,y) List.mem_cons_self) (ih fun i hi => h i (List.mem_cons_of_mem _ hi)) + +lemma sumE_compare {s : Profile M} (x y : Grid M) (h : ∀ i∈s, 0 ≤ i.1) : + sumE s y ≤ 2*(sumE s x + form (mixForm s) (x-y)) := by + induction s with + | nil => simp [mixForm,sumE,form_zero] + | cons i s ih => + obtain ⟨a,z⟩ := i + have hh := ih (fun i hi => h i (List.mem_cons_of_mem _ hi)) + have hx : 0 ≤ a := h (a,z) List.mem_cons_self + simp only [mix_cons, sumE, List.map_cons, List.sum_cons, form_add] + unfold sumE at hh + have H := form_triangle hx (z-x) (x-y) + rw [sub_add_sub_cancel] at H + linarith + +lemma profile_envelope {s : Profile M} (h : ∀ i∈s, 0 ≤ i.1) (x : Grid M) : + ‖fc (fun y => (↑(tune s y):ℂ)) x‖ ≤ q (mixForm s) x := by + induction s generalizing x with + | nil => + rw [show mixForm ([]:Profile M)=0 from by simp [mixForm]] + apply le_of_eq + convert norm_fc_translate_boltz (M:=M) (le_refl (0:Form M)) 0 x using 1 + simp [boltz,tune,sumE,form_zero] + | cons i s ih => + obtain ⟨a,y⟩ := i + simp_rw [mix_cons,tune_cons,Complex.ofReal_mul,fc_prod,q_add] + apply le_trans (norm_sum_le _ _) + refine Finset.sum_le_sum fun i _ => ?_ + have hx : 0 ≤ a := h (a,y) List.mem_cons_self + have hh := ih (fun i hi => h i (List.mem_cons_of_mem _ hi)) (x-i) + rw [norm_mul,norm_fc_translate_boltz hx] + exact mul_le_mul_of_nonneg_left hh (q_nonneg hx _) + +private lemma close_kept {p : Node} (hp : p.ok M) {x v : Grid M} (hx : x ∈ p.block M) + (h : Dyadic.diam v < p.len/2) : + x+v ∈ p.block M ∨ x-v ∈ p.block M := by + have he (v : Grid M) (hv : pos v < p.len/2) : + x+v ∈ p.block M ∨ x-v ∈ p.block M := by + have hh1 : p.idx M ≤ x.val ∧ x.val < p.idx M+p.wid M := by + rw [mem_block_iff hp.2] at hx; unfold idx wid; rw [add_mul,one_mul] at hx; exact hx + have hh2 : 2*v.val < p.wid M := by + have H := side_pos M + have he : p.len = (p.wid M : ℝ)*side M := by + unfold wid; rw [descale hp.2]; push_cast; ring + rw [he,pos] at hv + have hi : 2*(v.val : ℝ)< p.wid M := by nlinarith + exact_mod_cast hi + have heq (x : Grid M) : + x∈p.block M ↔ p.idx M ≤ x.val ∧ x.val < p.idx M+p.wid M := by + unfold idx wid; rw [mem_block_iff hp.2,add_mul,one_mul] + rw [heq,heq] + by_cases H : x.val+v.val < p.idx M+p.wid M + · left; rw [ZMod.val_add_of_lt (H.trans_le (p.fitting hp.1 hp.2))] + omega + right; rw [ZMod.val_sub (by omega : v.val ≤ x.val)] + omega + rw [diam_min,min_lt_iff] at h + rcases h with h|h + · exact he v h + · simpa only [sub_neg_eq_add,← sub_eq_add_neg] using (he (-v) h).symm + +private lemma even_prob (A : Form M) (p : Grid M → Prop) : + (label A).prob (fun x => p (-x)) = (label A).prob p := by + rw [prob_eq,prob_eq] + apply Fintype.sum_equiv (Equiv.neg (Grid M)) + intro i + simp only [Equiv.neg_apply,label_apply,K_even _ _] + +private lemma label_node_inside {p : Node} (hp : p.ok M) {A : Form M} + (hA : S M (1000*p.invlen) ≤ A) (h : 1000*p.invlen ≤ 2^M) + (x : Grid M) (hx : x∈p.block M) : + 1/4 ≤ (label A).prob (fun z => x+z ∈ p.block M) := by + let R := p.invlen; let r := p.len/2; let P := label A + let u (z : Grid M) : Prop := Dyadic.diam z < r + let v (z : Grid M) : Prop := x+z ∈ p.block M + have hR : 1 ≤ R := p.invlen_ge + have hr : 0 < r := by unfold r; have := p.len_pos; positivity + have he : P.prob (fun x => ¬ u x) ≤ 1/2 := by + have he : (P.prob (fun x => ¬u x))*r^2 ≤ 100/(1000*R)^2 := by + apply le_trans _ (S_variance _ (by linarith) h _ hA) + unfold prob + rw [← Ex_mul] + refine Ex_mono _ fun i => ?_ + dsimp only; split_ifs with H + · rw [zero_mul]; positivity + · rw [one_mul] + apply pow_le_pow_left₀ hr.le _ + exact not_lt.mp H + have ht : 100/(1000*R)^2 ≤ (1/2)*r^2 := by + unfold R r; rw [rinv] + have h0 := p.len_pos + field_simp; nlinarith + exact le_of_mul_le_mul_right (he.trans ht) (by positivity) + have hb : P.prob u ≤ P.prob v + (P.prob fun z => v (-z)) := by + unfold prob; rw [← Ex_add]; refine Ex_mono _ fun i => ?_ + by_cases hi : u i + · have HH : v i ∨ v (-i) := by + simpa only [v,sub_eq_add_neg] using close_kept hp hx hi + rcases HH with HH|HH <;> simp only [hi,HH,ite_true] <;> + split_ifs <;> norm_num + · simp only [hi,ite_false]; split_ifs <;> norm_num + rw [prob_compl] at he + rw [even_prob A v] at hb + change _ ≤ P.prob v; linarith + +lemma posterior_denominator {p : Node} (hp : p.ok M) {A : Form M} + (hA : S M (1000*p.invlen) ≤ A) (h : 1000*p.invlen ≤ 2^M) + (s : Profile M) {r : ℝ} (hr : admissible s p r) + (hn : ∀ i∈s, 0 ≤ i.1) (hh : (2:ℝ) • mixForm s ≤ A) : + exp (- (2*r)-F A)/4 ≤ expU (prefixE s p) := by + obtain ⟨x,hx,h1⟩ := hr + let Q := label A + let c := exp (- (2*r)-F A) + have hc : 0 < c := exp_pos _ + have HE (y : Grid M) : c*(K A y * (if x+y∈p.block M then 1 else 0)) ≤ + prefixE s p (x+y) := by + unfold prefixE; split_ifs with hiv + · rw [mul_one,K_eq,tune] + unfold c; rw [← exp_add]; apply Real.exp_monotone + have hi := sumE_compare x (x+y) hn + have h2 := form_mono hh (x-(x+y)) + rw [form_smul, show x-(x+y)=-y by ring,form_even A y] at h2 + rw [show x-(x+y)=-y by ring] at hi + linarith + · simp + have hh' := Ex_mono (uniform _) HE + simp only [Ex_lmul,← expU_ex,expU_translate,← label_ex] at hh' + apply le_trans _ hh' + have hh2 := label_node_inside hp hA h x hx + let f (y : Grid M) := if x+y∈p.block M then (1:ℝ) else 0 + have Heq : (label A).prob (fun z => x+z∈p.block M)=(label A).Ex f := by + + rw [prob_eq]; simp [f,Ex] + rw [Heq] at hh2 + change c/4 ≤ c*(label A).Ex f + nlinarith + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Power.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Power.lean index 6ade2eda11f703af470b4b5fd27b6dae0b1599ae..75b115ef0703e0ca192371ec2dd7464e96fa8d8e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Power.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Power.lean @@ -1,164 +1,164 @@ -import OAI.Analysis.FourierLLogL.Thermal.GridFreq -import OAI.Analysis.FourierLLogL.Compress.Phase -import OAI.Analysis.FourierLLogL.Compress.Variational - -/-! # Moment calculation on the snapped spectrum -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period - -variable {M l : ℕ} {ι Γ : Type} [Fintype ι] [DecidableEq ι] [Fintype Γ] - -/-- Polynomial evaluated at points of the full grid. -/ -def ply (hl : l ≤ M) (p : Law (Grid l)) (f : Grid l → ℂ) (y : Grid M) := - p.Cx (fun x => f x * chi (raise hl x) y) -def HeatM (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) := - (label A).Ex fun y => ‖ply hl p 1 y‖^(2*T) -def levelCost (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) := - -log (HeatM hl A p T) - -lemma ply_unit (hl : l ≤ M) (p : Law (Grid l)) (f : Grid l → ℂ) (hf : ∀ x, ‖f x‖ ≤ 1) (y) : - ‖ply hl p f y‖ ≤ 1 := norm_Cx_unit _ _ fun x => by rw [norm_mul,norm_chi,mul_one]; exact hf _ - -lemma ply_one_zero (hl : l ≤ M) (p : Law (Grid l)) : ply hl p 1 0 = 1 := by - unfold ply; simp_rw [chi_zero_right, mul_one,Pi.one_apply] - rw [show (fun _ : Grid l => (1:ℂ)) = fun _ => (↑(1:ℝ):ℂ) by simp, Cx_ofReal,Ex_const] - rfl - -lemma HeatM_bounds (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : - 0 < HeatM hl A p T ∧ HeatM hl A p T ≤ 1 := by - constructor - · unfold HeatM Ex - apply lt_of_lt_of_le (b:=label A 0 * ‖ply hl p 1 0‖^(2*T)) - · rw [ply_one_zero,norm_one,one_pow,mul_one]; exact label_pos _ _ - refine Finset.single_le_sum (f:=fun y => label A y * ‖ply hl p 1 y‖^(2*T)) - (fun i _ => mul_nonneg (label_pos _ _).le (by positivity)) (Finset.mem_univ _) - · unfold HeatM - refine le_trans (b:=(label A).Ex fun _ => 1) (Ex_mono _ fun x => ?_) (by rw [Ex_const]) - exact pow_le_one₀ (norm_nonneg _) (ply_unit hl p 1 (fun _ => by simp) _) - -lemma heat_exp (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : - exp (-levelCost hl A p T) = HeatM hl A p T := by - rw [levelCost,neg_neg, exp_log (HeatM_bounds _ _ _ _).1] - -lemma heat_nonneg (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : - 0 ≤ levelCost hl A p T := neg_nonneg.mpr (log_nonpos - (HeatM_bounds _ _ _ _).1.le (HeatM_bounds _ _ _ _).2) - -lemma Cx_send (P : Law Γ) (X : Γ → Grid l) (f : Grid l → ℂ) : - (P.send X).Cx f = P.Cx fun i => f (X i) := by - unfold Cx - classical - simp_rw [send_apply,prob_eq,Complex.ofReal_sum,Finset.sum_mul] - rw [Finset.sum_comm] - refine Finset.sum_congr rfl fun i _ => ?_ - rw [Finset.sum_eq_single (X i),ite_eq_left rfl] - · intro j _ hj - rw [ite_eq_right hj.symm, Complex.ofReal_zero,zero_mul] - simp - -lemma pack_ply_expand (T : ℕ) (hl : l ≤ M) (p : Law Γ) (tag : Γ → Grid l) - (f : Grid l → ℂ) (y : Grid M) : - let Q : Law (Fin T → Γ) := pack fun _ => p - let W (X : Fin T → Γ) := ∏ i, f (tag (X i)) - let t := Compress.tot tag (ι:=Fin T) - ‖ply hl (p.send tag) f y‖^(2*T) = Q.Ex fun X => Q.Ex fun X' => - (W X * conj (W X') * chi (raise hl (t X-t X')) y).re := by - classical - intro Q W t - let u := fun x : Grid l => chi (raise hl x) y - let g := fun x : Γ => f (tag x)*u (tag x) - have hu (X : Fin T → Γ) : - ∏ i, g (X i) = W X * u (t X) := by - unfold g W; rw [Finset.prod_mul_distrib] - have H (x : Grid l) : u x = raiseChar hl y x := (raiseChar_eq hl _ _).symm - simp_rw [H, t,Compress.tot,Compress.char_sum] - have he : ply hl (p.send tag) f y ^ T = Q.Cx fun X => W X * u (t X) := by - - have HX : ply hl (p.send tag) f y = p.Cx g := Cx_send _ _ _ - simp only [HX] - unfold Q - simp_rw [← hu, Cx_prod (fun _ : Fin T => p) (fun _ => g)] - simp - have hn (X X' : Fin T → Γ) : W X* conj (W X')*chi (raise hl (t X-t X')) y = - (W X * u (t X)) * conj (W X'*u (t X')) := by - unfold u; simp_rw [map_sub,map_mul,sub_eq_add_neg,chi_add',chi_neg'] - ring - rw [mul_comm 2 _,pow_mul,← norm_pow,he,sq_norm_Cx]; simp_rw [hn] - -lemma selected_heat (T : ℕ) (hl : l ≤ M) (A : Form M) - (hA : ∀ i : Grid M, 0 < cred A i) (p : Law Γ) (tag : Γ → Grid l) : - Compress.heatMoment (Fin T) p tag (fq_cost hl A) = HeatM hl A (p.send tag) T := by - let Q : Law (Fin T → Γ) := pack fun _ => p - let t (f : Fin T → Γ) := Compress.tot tag f - change (Q.Ex fun X => Q.Ex fun X' => exp ( -fq_cost hl A (t X-t X'))) = _ - unfold HeatM - have h (y : Grid M) : ‖ply hl (p.send tag) 1 y‖^(2*T) = Q.Ex (fun X => Q.Ex - fun X' => wave (raise hl (t X-t X')) y) := by - simp only [pack_ply_expand,Finset.prod_const_one,Pi.one_apply,map_one,one_mul] - rfl - simp_rw [h,Ex_swap (label A) Q,wave] - have he (i : Grid l) : exp ( -fq_cost hl A i) = (label A).Ex fun y => (chi (raise hl i) y).re := by - rw [fq_cost,neg_neg,exp_log (hA _),cred_ex]; rfl - simp_rw [he] - -lemma weighted_envelope (T : ℕ) (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) - (f : Grid l → ℂ) (hf : ∀ x, ‖f x‖ ≤ 1) (W : Grid M → ℝ) (c : ℝ) - (H : ∀ i, ‖expU (fun y => (W y:ℂ)*chi i y)‖ ≤ c*cred A i) : - expU (fun y => W y * ‖ply hl p f y‖^(2*T)) ≤ c * HeatM hl A p T := by - let P := label A - let Q := pack fun _ : Fin T => p - let t (X : Fin T → Grid l) := ∑ i, X i - let v (X X' : Fin T → Grid l) := raise hl (t X-t X') - let V (X : Fin T → Grid l) := ∏ i, f (X i) - have hp : p.send (fun x => x) = p := send_id _ - have he (y : Grid M) : ‖ply hl p f y‖^(2*T)= Q.Ex fun X => Q.Ex fun X' => - (V X * conj (V X') * chi (v X X') y).re := by - have hi := pack_ply_expand T hl p (fun x => x) f y - rw [hp] at hi - exact hi - have he' (y : Grid M) : ‖ply hl p 1 y‖^(2*T)= Q.Ex fun X => Q.Ex fun X' => - wave (v X X') y := by - have hi := pack_ply_expand T hl p (fun x => x) 1 y - simp only [hp, Pi.one_apply,Finset.prod_const_one,map_one,one_mul] at hi - exact hi - have hV (X : Fin T → Grid l) : ‖V X‖ ≤ 1 := by - unfold V; rw [Complex.norm_prod] - apply le_trans (b:=∏ _i : Fin T, (1:ℝ)) (Finset.prod_le_prod₀ (fun i _ => ?_) (fun i _ => ?_)) (by simp) - · positivity - · exact hf _ - let L := Law.uniform (Grid M) - have hX (X X' : Fin T → Grid l) : L.Ex (fun y => W y * (V X*conj (V X')*chi (v X X') y).re) ≤ - c * P.Ex (wave (v X X')) := by - let Z := V X * conj (V X') - let s := v X X' - change L.Ex (fun y => W y * (Z*chi s y).re) ≤ c*P.Ex (wave s) - have he : L.Ex (fun y => W y * (Z*chi s y).re) = (Z * expU (fun y => (W y:ℂ)*chi s y)).re := by - rw [expU_cx,← Cx_lmul,re_Cx] - refine congrArg _ (funext fun y => ?_); rw [mul_left_comm,Complex.re_ofReal_mul] - rw [he, show P.Ex (wave s)=cred A s from (cred_ex _ _).symm] - apply le_trans (le_abs_self _) - apply le_trans (Complex.abs_re_le_norm _) - rw [norm_mul] - have hZ : ‖Z‖ ≤ 1 := by - unfold Z; rw [norm_mul,Complex.norm_conj] - calc - ‖V X‖ * ‖V X'‖ ≤ 1 * ‖V X'‖ := - mul_le_mul_of_nonneg_right (hV X) (norm_nonneg _) - _ ≤ 1 := by simpa using hV X' - exact (mul_le_of_le_one_left (norm_nonneg _) hZ).trans (H s) - unfold HeatM - simp_rw [he,he',expU_ex] - - simp_rw [← Ex_lmul Q, Ex_swap (label A) Q,Ex_swap (uniform (Grid M)) Q] - simp_rw [← Ex_lmul Q] - exact Q.Ex_mono fun X => Q.Ex_mono fun X' => hX X X' - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.GridFreq +import OAI.Analysis.FourierLLogL.Compress.Phase +import OAI.Analysis.FourierLLogL.Compress.Variational + +/-! # Moment calculation on the snapped spectrum -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period + +variable {M l : ℕ} {ι Γ : Type} [Fintype ι] [DecidableEq ι] [Fintype Γ] + +/-- Polynomial evaluated at points of the full grid. -/ +def ply (hl : l ≤ M) (p : Law (Grid l)) (f : Grid l → ℂ) (y : Grid M) := + p.Cx (fun x => f x * chi (raise hl x) y) +def HeatM (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) := + (label A).Ex fun y => ‖ply hl p 1 y‖^(2*T) +def levelCost (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) := + -log (HeatM hl A p T) + +lemma ply_unit (hl : l ≤ M) (p : Law (Grid l)) (f : Grid l → ℂ) (hf : ∀ x, ‖f x‖ ≤ 1) (y) : + ‖ply hl p f y‖ ≤ 1 := norm_Cx_unit _ _ fun x => by rw [norm_mul,norm_chi,mul_one]; exact hf _ + +lemma ply_one_zero (hl : l ≤ M) (p : Law (Grid l)) : ply hl p 1 0 = 1 := by + unfold ply; simp_rw [chi_zero_right, mul_one,Pi.one_apply] + rw [show (fun _ : Grid l => (1:ℂ)) = fun _ => (↑(1:ℝ):ℂ) by simp, Cx_ofReal,Ex_const] + rfl + +lemma HeatM_bounds (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : + 0 < HeatM hl A p T ∧ HeatM hl A p T ≤ 1 := by + constructor + · unfold HeatM Ex + apply lt_of_lt_of_le (b:=label A 0 * ‖ply hl p 1 0‖^(2*T)) + · rw [ply_one_zero,norm_one,one_pow,mul_one]; exact label_pos _ _ + refine Finset.single_le_sum (f:=fun y => label A y * ‖ply hl p 1 y‖^(2*T)) + (fun i _ => mul_nonneg (label_pos _ _).le (by positivity)) (Finset.mem_univ _) + · unfold HeatM + refine le_trans (b:=(label A).Ex fun _ => 1) (Ex_mono _ fun x => ?_) (by rw [Ex_const]) + exact pow_le_one₀ (norm_nonneg _) (ply_unit hl p 1 (fun _ => by simp) _) + +lemma heat_exp (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : + exp (-levelCost hl A p T) = HeatM hl A p T := by + rw [levelCost,neg_neg, exp_log (HeatM_bounds _ _ _ _).1] + +lemma heat_nonneg (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) (T : ℕ) : + 0 ≤ levelCost hl A p T := neg_nonneg.mpr (log_nonpos + (HeatM_bounds _ _ _ _).1.le (HeatM_bounds _ _ _ _).2) + +lemma Cx_send (P : Law Γ) (X : Γ → Grid l) (f : Grid l → ℂ) : + (P.send X).Cx f = P.Cx fun i => f (X i) := by + unfold Cx + classical + simp_rw [send_apply,prob_eq,Complex.ofReal_sum,Finset.sum_mul] + rw [Finset.sum_comm] + refine Finset.sum_congr rfl fun i _ => ?_ + rw [Finset.sum_eq_single (X i),ite_eq_left rfl] + · intro j _ hj + rw [ite_eq_right hj.symm, Complex.ofReal_zero,zero_mul] + simp + +lemma pack_ply_expand (T : ℕ) (hl : l ≤ M) (p : Law Γ) (tag : Γ → Grid l) + (f : Grid l → ℂ) (y : Grid M) : + let Q : Law (Fin T → Γ) := pack fun _ => p + let W (X : Fin T → Γ) := ∏ i, f (tag (X i)) + let t := Compress.tot tag (ι:=Fin T) + ‖ply hl (p.send tag) f y‖^(2*T) = Q.Ex fun X => Q.Ex fun X' => + (W X * conj (W X') * chi (raise hl (t X-t X')) y).re := by + classical + intro Q W t + let u := fun x : Grid l => chi (raise hl x) y + let g := fun x : Γ => f (tag x)*u (tag x) + have hu (X : Fin T → Γ) : + ∏ i, g (X i) = W X * u (t X) := by + unfold g W; rw [Finset.prod_mul_distrib] + have H (x : Grid l) : u x = raiseChar hl y x := (raiseChar_eq hl _ _).symm + simp_rw [H, t,Compress.tot,Compress.char_sum] + have he : ply hl (p.send tag) f y ^ T = Q.Cx fun X => W X * u (t X) := by + + have HX : ply hl (p.send tag) f y = p.Cx g := Cx_send _ _ _ + simp only [HX] + unfold Q + simp_rw [← hu, Cx_prod (fun _ : Fin T => p) (fun _ => g)] + simp + have hn (X X' : Fin T → Γ) : W X* conj (W X')*chi (raise hl (t X-t X')) y = + (W X * u (t X)) * conj (W X'*u (t X')) := by + unfold u; simp_rw [map_sub,map_mul,sub_eq_add_neg,chi_add',chi_neg'] + ring + rw [mul_comm 2 _,pow_mul,← norm_pow,he,sq_norm_Cx]; simp_rw [hn] + +lemma selected_heat (T : ℕ) (hl : l ≤ M) (A : Form M) + (hA : ∀ i : Grid M, 0 < cred A i) (p : Law Γ) (tag : Γ → Grid l) : + Compress.heatMoment (Fin T) p tag (fq_cost hl A) = HeatM hl A (p.send tag) T := by + let Q : Law (Fin T → Γ) := pack fun _ => p + let t (f : Fin T → Γ) := Compress.tot tag f + change (Q.Ex fun X => Q.Ex fun X' => exp ( -fq_cost hl A (t X-t X'))) = _ + unfold HeatM + have h (y : Grid M) : ‖ply hl (p.send tag) 1 y‖^(2*T) = Q.Ex (fun X => Q.Ex + fun X' => wave (raise hl (t X-t X')) y) := by + simp only [pack_ply_expand,Finset.prod_const_one,Pi.one_apply,map_one,one_mul] + rfl + simp_rw [h,Ex_swap (label A) Q,wave] + have he (i : Grid l) : exp ( -fq_cost hl A i) = (label A).Ex fun y => (chi (raise hl i) y).re := by + rw [fq_cost,neg_neg,exp_log (hA _),cred_ex]; rfl + simp_rw [he] + +lemma weighted_envelope (T : ℕ) (hl : l ≤ M) (A : Form M) (p : Law (Grid l)) + (f : Grid l → ℂ) (hf : ∀ x, ‖f x‖ ≤ 1) (W : Grid M → ℝ) (c : ℝ) + (H : ∀ i, ‖expU (fun y => (W y:ℂ)*chi i y)‖ ≤ c*cred A i) : + expU (fun y => W y * ‖ply hl p f y‖^(2*T)) ≤ c * HeatM hl A p T := by + let P := label A + let Q := pack fun _ : Fin T => p + let t (X : Fin T → Grid l) := ∑ i, X i + let v (X X' : Fin T → Grid l) := raise hl (t X-t X') + let V (X : Fin T → Grid l) := ∏ i, f (X i) + have hp : p.send (fun x => x) = p := send_id _ + have he (y : Grid M) : ‖ply hl p f y‖^(2*T)= Q.Ex fun X => Q.Ex fun X' => + (V X * conj (V X') * chi (v X X') y).re := by + have hi := pack_ply_expand T hl p (fun x => x) f y + rw [hp] at hi + exact hi + have he' (y : Grid M) : ‖ply hl p 1 y‖^(2*T)= Q.Ex fun X => Q.Ex fun X' => + wave (v X X') y := by + have hi := pack_ply_expand T hl p (fun x => x) 1 y + simp only [hp, Pi.one_apply,Finset.prod_const_one,map_one,one_mul] at hi + exact hi + have hV (X : Fin T → Grid l) : ‖V X‖ ≤ 1 := by + unfold V; rw [Complex.norm_prod] + apply le_trans (b:=∏ _i : Fin T, (1:ℝ)) (Finset.prod_le_prod₀ (fun i _ => ?_) (fun i _ => ?_)) (by simp) + · positivity + · exact hf _ + let L := Law.uniform (Grid M) + have hX (X X' : Fin T → Grid l) : L.Ex (fun y => W y * (V X*conj (V X')*chi (v X X') y).re) ≤ + c * P.Ex (wave (v X X')) := by + let Z := V X * conj (V X') + let s := v X X' + change L.Ex (fun y => W y * (Z*chi s y).re) ≤ c*P.Ex (wave s) + have he : L.Ex (fun y => W y * (Z*chi s y).re) = (Z * expU (fun y => (W y:ℂ)*chi s y)).re := by + rw [expU_cx,← Cx_lmul,re_Cx] + refine congrArg _ (funext fun y => ?_); rw [mul_left_comm,Complex.re_ofReal_mul] + rw [he, show P.Ex (wave s)=cred A s from (cred_ex _ _).symm] + apply le_trans (le_abs_self _) + apply le_trans (Complex.abs_re_le_norm _) + rw [norm_mul] + have hZ : ‖Z‖ ≤ 1 := by + unfold Z; rw [norm_mul,Complex.norm_conj] + calc + ‖V X‖ * ‖V X'‖ ≤ 1 * ‖V X'‖ := + mul_le_mul_of_nonneg_right (hV X) (norm_nonneg _) + _ ≤ 1 := by simpa using hV X' + exact (mul_le_of_le_one_left (norm_nonneg _) hZ).trans (H s) + unfold HeatM + simp_rw [he,he',expU_ex] + + simp_rw [← Ex_lmul Q, Ex_swap (label A) Q,Ex_swap (uniform (Grid M)) Q] + simp_rw [← Ex_lmul Q] + exact Q.Ex_mono fun X => Q.Ex_mono fun X' => hX X X' + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Product.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Product.lean index dc2e6dedcc49cd047dd0a9255b1970764afedd7d..d50230e898c1d9048af3ed307ed710abb0743663 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Product.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Product.lean @@ -1,181 +1,181 @@ -import OAI.Analysis.FourierLLogL.Thermal.Side -import OAI.Analysis.FourierLLogL.Dyadic.Span -import OAI.Analysis.FourierLLogL.Compress.Thinning - -/-! # Side-labeled greedy logarithmic sumset bound -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period - -variable {M l : ℕ} -open scoped Classical - -lemma log_shift_bound (s : ℝ) (h : |s| ≤ 1/2) : s-2*s^2 ≤ log (1+s) := by - obtain ⟨hx,hx'⟩ := abs_le.mp h - have hh : 0 < 1+s := by linarith - have H := Real.log_le_sub_one_of_pos (inv_pos.mpr hh) - rw [Real.log_inv] at H - apply le_trans (b:= -( (1+s)⁻¹-1)) - · apply le_of_mul_le_mul_right _ hh - rw [neg_mul,sub_mul,sub_mul,inv_mul_cancel₀ hh.ne',one_mul] - nlinarith [mul_nonneg (by linarith : 0 ≤ (2*s+1)) (sq_nonneg s)] - linarith - -private def factorQ (c : ℝ) (v : ℂ) (i x : Grid l) : ℝ := - 1 + c*(v*chi i x).re - -private lemma test_abs (v : ℂ) (i x : Grid l) (h : ‖v‖ ≤ 1) : - |(v * chi i x).re| ≤ 1 := - (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact h) - -private lemma factor_bounds (β : ℝ) (v : ℂ) (i x : Grid l) (hp : 0 ≤ β) (hh : β ≤ 1) - (hv : ‖v‖ ≤ 1) : - 0 < factorQ (β/4) v i x ∧ - β/4 * (v*chi i x).re - β^2/8 ≤ log (factorQ (β/4) v i x) := by - have he := test_abs v i x hv - let t := β/4 * (v*chi i x).re - have hi : |t| ≤ β/4 := by - unfold t; rw [abs_mul,abs_of_nonneg (by positivity)] - exact mul_le_of_le_one_right (by positivity) he - have hX := abs_le.mp hi - unfold factorQ - change 0 < 1+t ∧ t-_ ≤ _ - have hl := log_shift_bound t (hi.trans (by linarith)) - constructor - · linarith [hX.1] - apply le_trans _ hl - nlinarith [sq_abs t,abs_nonneg t] - -private def Qp (s : Finset (Grid l)) (c : ℝ) (v : Grid l → Grid M → ℂ) - (i : Grid l) (z : Grid M) := ∏ j∈s, factorQ c (v j z) j i - -private lemma q_exp_constant {f : Grid l → ℝ} : - (↑(expU f):ℂ) = fc (fun i => (↑(f i):ℂ)) 0 := by - rw [fc,← expU_real]; simp_rw [neg_zero,chi_zero_left,mul_one] - -private lemma q_step {s : Finset (Grid l)} {c : ℝ} {v : Grid l → Grid M → ℂ} {x : Grid l} - (hx : ¬ enclosed s 1 x) {z : Grid M} - (h : spanned s (fun i => (↑(Qp s c v i z):ℂ))) - (h' : expU (fun i => Qp s c v i z) = 1) : - spanned (insert x s) (fun i => (↑(Qp (insert x s) c v i z):ℂ)) ∧ - expU (fun i => Qp (insert x s) c v i z) = 1 := by - have HE (i : Grid l) : - (↑(Qp (insert x s) c v i z):ℂ) = - (↑(Qp s c v i z):ℂ) * (1+ (↑(((c:ℂ)*v x z*chi x i).re):ℂ)) := by - unfold Qp - rw [Finset.prod_insert (fun hh => hx (enclosed_mem s hh)),mul_comm] - push_cast - rw [mul_assoc,Complex.re_ofReal_mul,factorQ]; push_cast; rfl - obtain ⟨ht,ht'⟩ := span_augment h ((c:ℂ)*v x z) x hx - - constructor - · simp_rw [HE]; exact ht - apply Complex.ofReal_inj.mp - rw [q_exp_constant] - simp_rw [HE] - rw [ht',← q_exp_constant,h'] - -private lemma span_collection (s : Finset (Grid l)) (c : ℝ) (v : Grid l → Grid M → ℂ) : - ∃ t ⊆ s, (∀ x∈s, enclosed t 1 x) ∧ ∀ z, expU (fun i => Qp t c v i z)=1 := by - let R := fun t : Finset (Grid l) => ∀ z, spanned t (fun i => (↑(Qp t c v i z):ℂ)) ∧ - expU (fun i => Qp t c v i z)=1 - suffices HH : ∀ k : ℕ, ∀ t ⊆ s, s.card-t.card=k → R t → - ∃ t ⊆ s, (∀ x∈s, enclosed t 1 x) ∧ ∀ z, expU (fun i => Qp t c v i z)=1 by - refine HH _ ∅ (by simp) rfl fun z => ⟨?_,?_⟩ - · simp only [Qp,Finset.prod_empty, Complex.ofReal_one]; exact spanned_one _ - simp only [Qp,Finset.prod_empty,expU_ex,Ex_const] - intro k; induction k using Nat.strong_induction_on with - | h k ih => - intro t ht hk hr - by_cases H : ∀ x∈s, enclosed t 1 x - · exact ⟨t,ht,H,fun z => (hr z).2⟩ - push Not at H - obtain ⟨x,hx,H⟩ := H - have hh : insert x t ⊆ s := Finset.insert_subset hx ht - refine ih (s.card-(insert x t).card) ?_ _ hh rfl fun z => q_step H (hr z).1 (hr z).2 - have he := Finset.card_le_card hh - rw [Finset.card_insert_of_notMem (fun hh => H (enclosed_mem t hh))] at * - omega - -lemma conditional_if_ex {α β : Type} [Fintype α] [Fintype β] - (P : Law α) (k : α → Law β) (Q : α → Prop) (f : α → β → ℝ) : - (P.withNext k).Ex (fun i => if Q i.1 then f i.1 i.2 else 0) = - P.Ex (fun a => if Q a then (k a).Ex (f a) else 0) := by - rw [Ex_withNext]; refine congrArg _ (funext fun a => ?_) - by_cases h : Q a <;> simp only [h,ite_false,ite_true,Ex_const] - -private lemma append_cost {p : Node} {P : Law (Grid M)} {B : Bool} {A : Form M} - {hl : l ≤ M} {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 ≤ β) (hb : β ≤ 1) - (H : biases P p A hl s B v β) (i : Grid l) (hi : i∈s) : - let C (x : Grid M) : Prop := x∈(p.child B).block M - β^2/8 ≤ (P.withNext (channel A)).Ex fun xz => - if C xz.1 then log (factorQ (β/4) (v i xz.2) i (cut hl xz.1)) else 0 := by - intro C; let L := channel A; let T := P.withNext L - let f (xz : Grid M × Grid M) := if C xz.1 then (v i xz.2 * chi i (cut hl xz.1)).re else 0 - have H' : β ≤ T.Ex f := by - unfold T f; rw [conditional_if_ex P L C (fun y z => (v i z * chi i (cut hl y)).re)]; have he := H.2 i hi - simp_rw [cut_chi] at he - exact he - calc - _ ≤ (β/4)* T.Ex f - β^2/8 := by nlinarith - _ = T.Ex (fun xz => β/4*f xz - β^2/8) := by - rw [Ex_sub,Ex_const,Ex_lmul] - _ ≤ _ := Ex_mono T fun x => by - unfold f - split_ifs - · exact (factor_bounds β _ _ _ h0 hb (H.1 _ _)).2 - nlinarith - -lemma cheap_span {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') - {n : ℕ} {h err : ℝ} {A : Form M} (H : Certified h err n p' A) - {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) (hh : M-l ≤ p.lev) - {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) {B : Bool} - {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 ≤ β) (hb : β ≤ 1) - (HG : biases P p A hl s B v β) : - ∃ t : Finset (Grid l), (∀ x∈s, enclosed t 1 x) ∧ - t.card*(β^2/8) ≤ 2*d + log p.invlen - log ((2:ℝ)^(M-l)) + F A - log p'.invlen + 301 := by - obtain ⟨t,htt,hE,hq⟩ := span_collection s (β/4) v - let Q := Qp t (β/4) v - let C (x : Grid M) : Prop := x∈(child p B).block M - let U := P.withNext (channel A) - have hQ (i z) : 0 < Q i z := Finset.prod_pos fun x _ => (factor_bounds _ _ _ _ h0 hb (HG.1 _ _)).1 - let f (i : Grid l) (x : Grid M × Grid M) := if C x.1 then - log (factorQ (β/4) (v i x.2) i (cut hl x.1)) else 0 - refine ⟨t,hE,le_trans ?_ (product_entropy_bound hp hv H ht hl hh h1 hm B Q hQ fun z => (hq z).le)⟩ - change _ ≤ U.Ex (fun x => if C x.1 then _ else _) - have HE (x : Grid M × Grid M) : (if C x.1 then log (Q (cut hl x.1) x.2) else 0) = ∑ i∈t, f i x := by - unfold Q Qp f - split_ifs with hh - · rw [Real.log_prod] - exact fun i _ => (factor_bounds _ _ _ _ h0 hb (HG.1 _ _)).1.ne' - · simp - simp_rw [HE,Ex_finset_sum] - calc - _ = ∑ _i∈t, β^2/8 := by simp - _ ≤ _ := Finset.sum_le_sum fun i hi => append_cost h0 hb HG i (htt hi) - -lemma sumset_est {s t : Finset (Grid l)} (h : ∀ x∈s, enclosed t 1 x) (n : ℕ) : - Compress.lnS (Fin n) (fun x : s => (x:Grid l)) ≤ t.card * log (2*(n:ℝ)+1) := by - let S := Compress.sums (Fin n) (fun x : s => (x:Grid l)) - have Hi : S ⊆ palace t n := by - intro i hi - obtain ⟨f,_,rfl⟩ := Finset.mem_image.mp hi - apply Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩ - simpa [Compress.tot] using enclosed_sum t (fun i => (f i:Grid l)) (fun i => h _ (f i).property) - have HE := (Finset.card_le_card Hi).trans (palace_size _ _) - change log (S.card:ℝ) ≤ _ - by_cases H : S.card=0 - · simp only [H,Nat.cast_zero,log_zero] - exact mul_nonneg (by positivity) (Real.log_nonneg (by have H := Nat.cast_nonneg (α:=ℝ) n; linarith)) - · have Hh : 0 < (S.card:ℝ) := by exact_mod_cast Nat.pos_of_ne_zero H - have HE' := log_le_log Hh (by exact_mod_cast HE : (S.card:ℝ) ≤ (2*(n:ℝ)+1)^t.card) - rwa [log_pow] at HE' - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Side +import OAI.Analysis.FourierLLogL.Dyadic.Span +import OAI.Analysis.FourierLLogL.Compress.Thinning + +/-! # Side-labeled greedy logarithmic sumset bound -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period + +variable {M l : ℕ} +open scoped Classical + +lemma log_shift_bound (s : ℝ) (h : |s| ≤ 1/2) : s-2*s^2 ≤ log (1+s) := by + obtain ⟨hx,hx'⟩ := abs_le.mp h + have hh : 0 < 1+s := by linarith + have H := Real.log_le_sub_one_of_pos (inv_pos.mpr hh) + rw [Real.log_inv] at H + apply le_trans (b:= -( (1+s)⁻¹-1)) + · apply le_of_mul_le_mul_right _ hh + rw [neg_mul,sub_mul,sub_mul,inv_mul_cancel₀ hh.ne',one_mul] + nlinarith [mul_nonneg (by linarith : 0 ≤ (2*s+1)) (sq_nonneg s)] + linarith + +private def factorQ (c : ℝ) (v : ℂ) (i x : Grid l) : ℝ := + 1 + c*(v*chi i x).re + +private lemma test_abs (v : ℂ) (i x : Grid l) (h : ‖v‖ ≤ 1) : + |(v * chi i x).re| ≤ 1 := + (Complex.abs_re_le_norm _).trans (by rw [norm_mul,norm_chi,mul_one]; exact h) + +private lemma factor_bounds (β : ℝ) (v : ℂ) (i x : Grid l) (hp : 0 ≤ β) (hh : β ≤ 1) + (hv : ‖v‖ ≤ 1) : + 0 < factorQ (β/4) v i x ∧ + β/4 * (v*chi i x).re - β^2/8 ≤ log (factorQ (β/4) v i x) := by + have he := test_abs v i x hv + let t := β/4 * (v*chi i x).re + have hi : |t| ≤ β/4 := by + unfold t; rw [abs_mul,abs_of_nonneg (by positivity)] + exact mul_le_of_le_one_right (by positivity) he + have hX := abs_le.mp hi + unfold factorQ + change 0 < 1+t ∧ t-_ ≤ _ + have hl := log_shift_bound t (hi.trans (by linarith)) + constructor + · linarith [hX.1] + apply le_trans _ hl + nlinarith [sq_abs t,abs_nonneg t] + +private def Qp (s : Finset (Grid l)) (c : ℝ) (v : Grid l → Grid M → ℂ) + (i : Grid l) (z : Grid M) := ∏ j∈s, factorQ c (v j z) j i + +private lemma q_exp_constant {f : Grid l → ℝ} : + (↑(expU f):ℂ) = fc (fun i => (↑(f i):ℂ)) 0 := by + rw [fc,← expU_real]; simp_rw [neg_zero,chi_zero_left,mul_one] + +private lemma q_step {s : Finset (Grid l)} {c : ℝ} {v : Grid l → Grid M → ℂ} {x : Grid l} + (hx : ¬ enclosed s 1 x) {z : Grid M} + (h : spanned s (fun i => (↑(Qp s c v i z):ℂ))) + (h' : expU (fun i => Qp s c v i z) = 1) : + spanned (insert x s) (fun i => (↑(Qp (insert x s) c v i z):ℂ)) ∧ + expU (fun i => Qp (insert x s) c v i z) = 1 := by + have HE (i : Grid l) : + (↑(Qp (insert x s) c v i z):ℂ) = + (↑(Qp s c v i z):ℂ) * (1+ (↑(((c:ℂ)*v x z*chi x i).re):ℂ)) := by + unfold Qp + rw [Finset.prod_insert (fun hh => hx (enclosed_mem s hh)),mul_comm] + push_cast + rw [mul_assoc,Complex.re_ofReal_mul,factorQ]; push_cast; rfl + obtain ⟨ht,ht'⟩ := span_augment h ((c:ℂ)*v x z) x hx + + constructor + · simp_rw [HE]; exact ht + apply Complex.ofReal_inj.mp + rw [q_exp_constant] + simp_rw [HE] + rw [ht',← q_exp_constant,h'] + +private lemma span_collection (s : Finset (Grid l)) (c : ℝ) (v : Grid l → Grid M → ℂ) : + ∃ t ⊆ s, (∀ x∈s, enclosed t 1 x) ∧ ∀ z, expU (fun i => Qp t c v i z)=1 := by + let R := fun t : Finset (Grid l) => ∀ z, spanned t (fun i => (↑(Qp t c v i z):ℂ)) ∧ + expU (fun i => Qp t c v i z)=1 + suffices HH : ∀ k : ℕ, ∀ t ⊆ s, s.card-t.card=k → R t → + ∃ t ⊆ s, (∀ x∈s, enclosed t 1 x) ∧ ∀ z, expU (fun i => Qp t c v i z)=1 by + refine HH _ ∅ (by simp) rfl fun z => ⟨?_,?_⟩ + · simp only [Qp,Finset.prod_empty, Complex.ofReal_one]; exact spanned_one _ + simp only [Qp,Finset.prod_empty,expU_ex,Ex_const] + intro k; induction k using Nat.strong_induction_on with + | h k ih => + intro t ht hk hr + by_cases H : ∀ x∈s, enclosed t 1 x + · exact ⟨t,ht,H,fun z => (hr z).2⟩ + push Not at H + obtain ⟨x,hx,H⟩ := H + have hh : insert x t ⊆ s := Finset.insert_subset hx ht + refine ih (s.card-(insert x t).card) ?_ _ hh rfl fun z => q_step H (hr z).1 (hr z).2 + have he := Finset.card_le_card hh + rw [Finset.card_insert_of_notMem (fun hh => H (enclosed_mem t hh))] at * + omega + +lemma conditional_if_ex {α β : Type} [Fintype α] [Fintype β] + (P : Law α) (k : α → Law β) (Q : α → Prop) (f : α → β → ℝ) : + (P.withNext k).Ex (fun i => if Q i.1 then f i.1 i.2 else 0) = + P.Ex (fun a => if Q a then (k a).Ex (f a) else 0) := by + rw [Ex_withNext]; refine congrArg _ (funext fun a => ?_) + by_cases h : Q a <;> simp only [h,ite_false,ite_true,Ex_const] + +private lemma append_cost {p : Node} {P : Law (Grid M)} {B : Bool} {A : Form M} + {hl : l ≤ M} {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 ≤ β) (hb : β ≤ 1) + (H : biases P p A hl s B v β) (i : Grid l) (hi : i∈s) : + let C (x : Grid M) : Prop := x∈(p.child B).block M + β^2/8 ≤ (P.withNext (channel A)).Ex fun xz => + if C xz.1 then log (factorQ (β/4) (v i xz.2) i (cut hl xz.1)) else 0 := by + intro C; let L := channel A; let T := P.withNext L + let f (xz : Grid M × Grid M) := if C xz.1 then (v i xz.2 * chi i (cut hl xz.1)).re else 0 + have H' : β ≤ T.Ex f := by + unfold T f; rw [conditional_if_ex P L C (fun y z => (v i z * chi i (cut hl y)).re)]; have he := H.2 i hi + simp_rw [cut_chi] at he + exact he + calc + _ ≤ (β/4)* T.Ex f - β^2/8 := by nlinarith + _ = T.Ex (fun xz => β/4*f xz - β^2/8) := by + rw [Ex_sub,Ex_const,Ex_lmul] + _ ≤ _ := Ex_mono T fun x => by + unfold f + split_ifs + · exact (factor_bounds β _ _ _ h0 hb (H.1 _ _)).2 + nlinarith + +lemma cheap_span {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') + {n : ℕ} {h err : ℝ} {A : Form M} (H : Certified h err n p' A) + {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) (hh : M-l ≤ p.lev) + {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) {B : Bool} + {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 ≤ β) (hb : β ≤ 1) + (HG : biases P p A hl s B v β) : + ∃ t : Finset (Grid l), (∀ x∈s, enclosed t 1 x) ∧ + t.card*(β^2/8) ≤ 2*d + log p.invlen - log ((2:ℝ)^(M-l)) + F A - log p'.invlen + 301 := by + obtain ⟨t,htt,hE,hq⟩ := span_collection s (β/4) v + let Q := Qp t (β/4) v + let C (x : Grid M) : Prop := x∈(child p B).block M + let U := P.withNext (channel A) + have hQ (i z) : 0 < Q i z := Finset.prod_pos fun x _ => (factor_bounds _ _ _ _ h0 hb (HG.1 _ _)).1 + let f (i : Grid l) (x : Grid M × Grid M) := if C x.1 then + log (factorQ (β/4) (v i x.2) i (cut hl x.1)) else 0 + refine ⟨t,hE,le_trans ?_ (product_entropy_bound hp hv H ht hl hh h1 hm B Q hQ fun z => (hq z).le)⟩ + change _ ≤ U.Ex (fun x => if C x.1 then _ else _) + have HE (x : Grid M × Grid M) : (if C x.1 then log (Q (cut hl x.1) x.2) else 0) = ∑ i∈t, f i x := by + unfold Q Qp f + split_ifs with hh + · rw [Real.log_prod] + exact fun i _ => (factor_bounds _ _ _ _ h0 hb (HG.1 _ _)).1.ne' + · simp + simp_rw [HE,Ex_finset_sum] + calc + _ = ∑ _i∈t, β^2/8 := by simp + _ ≤ _ := Finset.sum_le_sum fun i hi => append_cost h0 hb HG i (htt hi) + +lemma sumset_est {s t : Finset (Grid l)} (h : ∀ x∈s, enclosed t 1 x) (n : ℕ) : + Compress.lnS (Fin n) (fun x : s => (x:Grid l)) ≤ t.card * log (2*(n:ℝ)+1) := by + let S := Compress.sums (Fin n) (fun x : s => (x:Grid l)) + have Hi : S ⊆ palace t n := by + intro i hi + obtain ⟨f,_,rfl⟩ := Finset.mem_image.mp hi + apply Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩ + simpa [Compress.tot] using enclosed_sum t (fun i => (f i:Grid l)) (fun i => h _ (f i).property) + have HE := (Finset.card_le_card Hi).trans (palace_size _ _) + change log (S.card:ℝ) ≤ _ + by_cases H : S.card=0 + · simp only [H,Nat.cast_zero,log_zero] + exact mul_nonneg (by positivity) (Real.log_nonneg (by have H := Nat.cast_nonneg (α:=ℝ) n; linarith)) + · have Hh : 0 < (S.card:ℝ) := by exact_mod_cast Nat.pos_of_ne_zero H + have HE' := log_le_log Hh (by exact_mod_cast HE : (S.card:ℝ) ≤ (2*(n:ℝ)+1)^t.card) + rwa [log_pow] at HE' + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Reference.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Reference.lean index 43c7b391d6985296cf1af987596ec6b9c4394bdd..89ee40e2c69388866f708bc686434b0cef1460d5 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Reference.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Reference.lean @@ -1,167 +1,167 @@ -import OAI.Analysis.FourierLLogL.Thermal.Alarm - -/-! # Asymptotic evidence in the comparison experiment -/ -open Set Function Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -lemma clones_eq (j : ℕ) : (clones j:ℝ)= H j^120 := by - unfold clones H; push_cast; rfl -lemma clones_pos (j : ℕ) : 0 < clones j := pow_pos (slow_pos _) _ - -def observationBudget (C : ℝ) (j : ℕ) := 1+C*H j*(1+clones j) - -lemma obs_scale (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, 2+2*observationBudget C j ≤ H j^130 := by - filter_upwards [h_large (10*C)] with j h - let a := H j - have hp := H_ge j - have hm : (clones j:ℝ)=a^120 := clones_eq _ - unfold observationBudget; rw [hm] - change 2+2*(1+C*a*(1+a^120)) ≤ a^130 - change 10*C ≤ a at h; change 1 ≤ a at hp - have h1 : a^122 ≤ a^130 := pow_le_pow_right₀ hp (by omega) - have h2 : 1 ≤ a^120 := one_le_pow₀ hp - have h3 : 10*C*a*(a^120) ≤ a^122 := - calc - _ ≤ a*a*a^120 := by gcongr - _ = _ := by ring - have hs : 1 ≤ C*a := one_le_mul_of_one_le_of_one_le hC hp - have h4 : C*a ≤ C*a*a^120 := le_mul_of_one_le_right (by linarith) h2 - linarith - -private lemma alarm_scale : - ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → ∀ c : ℝ, - height j/H j^2 ≤ c → - log (clones j)+log 4+32+6*cushion j+height j/H j^4 - - (2*(sLen j):ℕ)*log (beta i/4) ≤ c/8 := by - filter_upwards [Tk_bounds,h_large 100000,hpoly 1000000 123] with j ⟨hx,hy⟩ hp1 hp2 i hi c hh - let d := height j; let a := H j - have h := H_ge j; have hp := H_pos j - have he := (main_bounds j hi).2.2 - have hz := log_le_sub_one_of_pos (show 0 < (clones j:ℝ) by rw [clones_eq]; positivity) - rw [clones_eq] at * - have h4 := log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4) - rw [Real.log_div (beta_pos i).ne' (by norm_num)] - push_cast - let t : ℝ := sLen j - have ht : 0 ≤ t := by unfold t; positivity - change 1 ≤ a at h; change 0 < a at hp; change 100000 ≤ a at hp1 - change t*a^6=d at hx - change -log (beta i) ≤ 200*a at he - change 1000000*a^123 ≤ d at hp2 - have h₁ : log (a^120)+log 4+32 ≤ d/(64*a^2) := by - change log (a^120) ≤ _ at hz - apply (le_div_iff₀ (by positivity : 0<64*a^2)).mpr - have hr : a^122 ≤ a^123 := pow_le_pow_right₀ h (by omega) - calc - _ ≤ (100*a^120)*(64*a^2) := by gcongr; linarith [one_le_pow₀ h (n:=120)] - _ ≤ _ := by nlinarith [one_le_pow₀ h (n:=122)] - have h₂ : 6*d/a^100 + d/a^4 ≤ d/(64*a^2) := by - have hd : 0 ≤ d := by unfold d; exact (d_pos _).le - calc - _ ≤ 6*d/a^4+d/a^4 := by gcongr; norm_num - _ = 7/(a^4)*d := by ring - _ ≤ _ := by - rw [div_eq_mul_inv d,mul_comm d] - have hi : 7/a^4 ≤ (64*a^2)⁻¹ := by field_simp; nlinarith - exact mul_le_mul_of_nonneg_right hi hd - have h₃ : -(2*t)*(log (beta i)-log 4) ≤ d/(64*a^2) := by - calc - _ ≤ 500*t*a := by nlinarith - _ ≤ _ := by - apply (le_div_iff₀ (by positivity : 0<64*a^2)).mpr - rw [← hx] - rw [show 500*t*a*(64*a^2)=t*(32000*a^3) by ring] - gcongr - calc - _ ≤ a*a^3 := by gcongr; linarith - _ = a^4 := by ring - _ ≤ _ := pow_le_pow_right₀ h (by omega) - change log (a^120)+_+32+6*(d/a^100)+d/a^4-2*t*(log _-_) ≤ _ - have ht : d/(64*a^2)*64 ≤ c := by apply le_trans _ hh; exact le_of_eq (by ring) - rw [← mul_div_assoc] - have : 0 ≤ d/(64*a^2) := by unfold d; have := d_pos j; positivity - linarith - -/-- Probability form: unnormalized comparison weights are tune × 1_p -/ -theorem conditional_bound (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, - ∀ A : Form M, Press C j p A → 1000*p.invlen ≤ 2^M → - ∀ P : Law (Grid M), ∀ u : Update j M p, u.Good C A P → - u.regular → ∀ s : Profile M, - (∀ i∈s, 0 ≤ i.1 ∧ i.1 ≤ A) → s.length ≤ observationBudget C j → - admissible s p (cushion j) → ∀ z : Grid M, - expU (fun x => prefixE s p x * u.alarmQ A z x) ≤ - exp (-(u.credit/8)) * expU (prefixE s p) := by - filter_upwards [obs_scale C hC,alarm_scale,paid_tighten C hC] with j h ht hC' - intro M p A hP hq P u Hu Hr s hS hn hs z - let a := H j - let E := u.credit - let Q := tilde a A p - have hp := hP.fits - have hh : 1 ≤ a := H_ge j - have hA : 0 ≤ A := by obtain ⟨_,_,_,ha,_⟩ := hP.cap; exact ha.nonneg - have hZ : 2+2*(s.length:ℝ) ≤ a^130 := by linarith [show _ ≤ a^130 from h] - have hM (x : Grid M) : mixForm s x ≤ s.length*A x := mix_bound (fun i hi => (hS i hi).2) x - have hR : 0 ≤ p.invlen := p.invlen_pos.le - have hX : S M (1000*p.invlen) ≤ Q := by - apply le_add_of_nonneg_left - intro i; exact mul_nonneg (pow_nonneg (by linarith : 0 ≤ a) _) (hA i) - have hS' (i) (hi:i∈s) : 0 ≤ i.1 := (hS i hi).1 - have hF₁ : (2:ℝ) • mixForm s ≤ Q := by - intro i - have hh := hM i - have h2 := S_nonneg M (1000*p.invlen) i - have h3 : 0 ≤ A i := hA i - change 0 ≤ S M (1000*p.invlen) i at h2 - change 2*mixForm s i ≤ a^130*A i+_ - nlinarith - have hF₂ : A+place p M+mixForm s ≤ Q := by - intro i - have hh := hM i - have h₂ := S_mono (M:=M) (b:=1000*p.invlen) hR (by linarith) i - have h3 : 0 ≤ A i := hA i - change A i+S M _ i + _ ≤ a^130*A i+S M (1000*p.invlen) i - have hi : 0 ≤ (s.length:ℝ) := by positivity - nlinarith - have hlo := posterior_denominator hp hX hq s hs hS' hF₁ - have hhi := u.one_clone_tail hp hA Hu Hr s hS' hF₂ z - let D := cushion j - let L := clones j - let T := 2*sLen j - let b := beta u.ix/4 - have hL : 0 < (L:ℝ) := by unfold L; exact_mod_cast clones_pos j - have hb : 0 < b := div_pos (beta_pos _) (by norm_num) - apply le_of_mul_le_mul_left _ (pow_pos hb T) - have H₀ : expU (fun x => prefixE s p x * u.alarmQ A z x) ≤ - L * expU (fun x => prefixE s p x * u.singleQ A z x) := by - rw [expU_ex,expU_ex,← Ex_lmul]; refine Ex_mono _ fun x => ?_ - exact le_trans (mul_le_mul_of_nonneg_left (u.alarm_le _ _ _) (prefixE_nonneg _ _ _)) (le_of_eq (by ring)) - calc - _ ≤ b^T*(L * expU (fun x => prefixE s p x * u.singleQ A z x)) := by gcongr - _ ≤ ↑L * exp (32-F (A+place p M)-E/4+4*D) := by - rw [mul_left_comm] - exact mul_le_mul_of_nonneg_left hhi hL.le - _ ≤ b^T*(exp (- (E/8)) * (exp (- (2*D)-F Q)/4)) := by - have hc := exp_pos - apply le_of_not_gt - intro he - have hz₀ := log_lt_log (show 0 < b^T*(exp ( - (E/8))*(exp (- (2*D)-F Q)/4)) by positivity) he - rw [log_mul,log_pow,log_mul,Real.log_div (x:=exp (-(2*D)-F Q)) (y:=4),log_mul] at hz₀ - · simp_rw [log_exp] at hz₀ - have hY : log (L:ℝ)+ log 4 +32+6*D+height j / a^4 - (T:ℝ)*log b ≤ E/8 := - ht u.ix Hu.ix_ok _ Hu.reserves.1 - have hh' : F Q - F (A+place p M) ≤ height j/a^4 := hC' M p A hP - linarith - all_goals positivity - _ ≤ _ := by - gcongr - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Alarm + +/-! # Asymptotic evidence in the comparison experiment -/ +open Set Function Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +lemma clones_eq (j : ℕ) : (clones j:ℝ)= H j^120 := by + unfold clones H; push_cast; rfl +lemma clones_pos (j : ℕ) : 0 < clones j := pow_pos (slow_pos _) _ + +def observationBudget (C : ℝ) (j : ℕ) := 1+C*H j*(1+clones j) + +lemma obs_scale (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, 2+2*observationBudget C j ≤ H j^130 := by + filter_upwards [h_large (10*C)] with j h + let a := H j + have hp := H_ge j + have hm : (clones j:ℝ)=a^120 := clones_eq _ + unfold observationBudget; rw [hm] + change 2+2*(1+C*a*(1+a^120)) ≤ a^130 + change 10*C ≤ a at h; change 1 ≤ a at hp + have h1 : a^122 ≤ a^130 := pow_le_pow_right₀ hp (by omega) + have h2 : 1 ≤ a^120 := one_le_pow₀ hp + have h3 : 10*C*a*(a^120) ≤ a^122 := + calc + _ ≤ a*a*a^120 := by gcongr + _ = _ := by ring + have hs : 1 ≤ C*a := one_le_mul_of_one_le_of_one_le hC hp + have h4 : C*a ≤ C*a*a^120 := le_mul_of_one_le_right (by linarith) h2 + linarith + +private lemma alarm_scale : + ∀ᶠ j : ℕ in atTop, ∀ i, i ≤ testIx j → ∀ c : ℝ, + height j/H j^2 ≤ c → + log (clones j)+log 4+32+6*cushion j+height j/H j^4 - + (2*(sLen j):ℕ)*log (beta i/4) ≤ c/8 := by + filter_upwards [Tk_bounds,h_large 100000,hpoly 1000000 123] with j ⟨hx,hy⟩ hp1 hp2 i hi c hh + let d := height j; let a := H j + have h := H_ge j; have hp := H_pos j + have he := (main_bounds j hi).2.2 + have hz := log_le_sub_one_of_pos (show 0 < (clones j:ℝ) by rw [clones_eq]; positivity) + rw [clones_eq] at * + have h4 := log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4) + rw [Real.log_div (beta_pos i).ne' (by norm_num)] + push_cast + let t : ℝ := sLen j + have ht : 0 ≤ t := by unfold t; positivity + change 1 ≤ a at h; change 0 < a at hp; change 100000 ≤ a at hp1 + change t*a^6=d at hx + change -log (beta i) ≤ 200*a at he + change 1000000*a^123 ≤ d at hp2 + have h₁ : log (a^120)+log 4+32 ≤ d/(64*a^2) := by + change log (a^120) ≤ _ at hz + apply (le_div_iff₀ (by positivity : 0<64*a^2)).mpr + have hr : a^122 ≤ a^123 := pow_le_pow_right₀ h (by omega) + calc + _ ≤ (100*a^120)*(64*a^2) := by gcongr; linarith [one_le_pow₀ h (n:=120)] + _ ≤ _ := by nlinarith [one_le_pow₀ h (n:=122)] + have h₂ : 6*d/a^100 + d/a^4 ≤ d/(64*a^2) := by + have hd : 0 ≤ d := by unfold d; exact (d_pos _).le + calc + _ ≤ 6*d/a^4+d/a^4 := by gcongr; norm_num + _ = 7/(a^4)*d := by ring + _ ≤ _ := by + rw [div_eq_mul_inv d,mul_comm d] + have hi : 7/a^4 ≤ (64*a^2)⁻¹ := by field_simp; nlinarith + exact mul_le_mul_of_nonneg_right hi hd + have h₃ : -(2*t)*(log (beta i)-log 4) ≤ d/(64*a^2) := by + calc + _ ≤ 500*t*a := by nlinarith + _ ≤ _ := by + apply (le_div_iff₀ (by positivity : 0<64*a^2)).mpr + rw [← hx] + rw [show 500*t*a*(64*a^2)=t*(32000*a^3) by ring] + gcongr + calc + _ ≤ a*a^3 := by gcongr; linarith + _ = a^4 := by ring + _ ≤ _ := pow_le_pow_right₀ h (by omega) + change log (a^120)+_+32+6*(d/a^100)+d/a^4-2*t*(log _-_) ≤ _ + have ht : d/(64*a^2)*64 ≤ c := by apply le_trans _ hh; exact le_of_eq (by ring) + rw [← mul_div_assoc] + have : 0 ≤ d/(64*a^2) := by unfold d; have := d_pos j; positivity + linarith + +/-- Probability form: unnormalized comparison weights are tune × 1_p -/ +theorem conditional_bound (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, + ∀ A : Form M, Press C j p A → 1000*p.invlen ≤ 2^M → + ∀ P : Law (Grid M), ∀ u : Update j M p, u.Good C A P → + u.regular → ∀ s : Profile M, + (∀ i∈s, 0 ≤ i.1 ∧ i.1 ≤ A) → s.length ≤ observationBudget C j → + admissible s p (cushion j) → ∀ z : Grid M, + expU (fun x => prefixE s p x * u.alarmQ A z x) ≤ + exp (-(u.credit/8)) * expU (prefixE s p) := by + filter_upwards [obs_scale C hC,alarm_scale,paid_tighten C hC] with j h ht hC' + intro M p A hP hq P u Hu Hr s hS hn hs z + let a := H j + let E := u.credit + let Q := tilde a A p + have hp := hP.fits + have hh : 1 ≤ a := H_ge j + have hA : 0 ≤ A := by obtain ⟨_,_,_,ha,_⟩ := hP.cap; exact ha.nonneg + have hZ : 2+2*(s.length:ℝ) ≤ a^130 := by linarith [show _ ≤ a^130 from h] + have hM (x : Grid M) : mixForm s x ≤ s.length*A x := mix_bound (fun i hi => (hS i hi).2) x + have hR : 0 ≤ p.invlen := p.invlen_pos.le + have hX : S M (1000*p.invlen) ≤ Q := by + apply le_add_of_nonneg_left + intro i; exact mul_nonneg (pow_nonneg (by linarith : 0 ≤ a) _) (hA i) + have hS' (i) (hi:i∈s) : 0 ≤ i.1 := (hS i hi).1 + have hF₁ : (2:ℝ) • mixForm s ≤ Q := by + intro i + have hh := hM i + have h2 := S_nonneg M (1000*p.invlen) i + have h3 : 0 ≤ A i := hA i + change 0 ≤ S M (1000*p.invlen) i at h2 + change 2*mixForm s i ≤ a^130*A i+_ + nlinarith + have hF₂ : A+place p M+mixForm s ≤ Q := by + intro i + have hh := hM i + have h₂ := S_mono (M:=M) (b:=1000*p.invlen) hR (by linarith) i + have h3 : 0 ≤ A i := hA i + change A i+S M _ i + _ ≤ a^130*A i+S M (1000*p.invlen) i + have hi : 0 ≤ (s.length:ℝ) := by positivity + nlinarith + have hlo := posterior_denominator hp hX hq s hs hS' hF₁ + have hhi := u.one_clone_tail hp hA Hu Hr s hS' hF₂ z + let D := cushion j + let L := clones j + let T := 2*sLen j + let b := beta u.ix/4 + have hL : 0 < (L:ℝ) := by unfold L; exact_mod_cast clones_pos j + have hb : 0 < b := div_pos (beta_pos _) (by norm_num) + apply le_of_mul_le_mul_left _ (pow_pos hb T) + have H₀ : expU (fun x => prefixE s p x * u.alarmQ A z x) ≤ + L * expU (fun x => prefixE s p x * u.singleQ A z x) := by + rw [expU_ex,expU_ex,← Ex_lmul]; refine Ex_mono _ fun x => ?_ + exact le_trans (mul_le_mul_of_nonneg_left (u.alarm_le _ _ _) (prefixE_nonneg _ _ _)) (le_of_eq (by ring)) + calc + _ ≤ b^T*(L * expU (fun x => prefixE s p x * u.singleQ A z x)) := by gcongr + _ ≤ ↑L * exp (32-F (A+place p M)-E/4+4*D) := by + rw [mul_left_comm] + exact mul_le_mul_of_nonneg_left hhi hL.le + _ ≤ b^T*(exp (- (E/8)) * (exp (- (2*D)-F Q)/4)) := by + have hc := exp_pos + apply le_of_not_gt + intro he + have hz₀ := log_lt_log (show 0 < b^T*(exp ( - (E/8))*(exp (- (2*D)-F Q)/4)) by positivity) he + rw [log_mul,log_pow,log_mul,Real.log_div (x:=exp (-(2*D)-F Q)) (y:=4),log_mul] at hz₀ + · simp_rw [log_exp] at hz₀ + have hY : log (L:ℝ)+ log 4 +32+6*D+height j / a^4 - (T:ℝ)*log b ≤ E/8 := + ht u.ix Hu.ix_ok _ Hu.reserves.1 + have hh' : F Q - F (A+place p M) ≤ height j/a^4 := hC' M p A hP + linarith + all_goals positivity + _ ≤ _ := by + gcongr + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Shrink.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Shrink.lean index 50c7d453c5f30591d1e92b21443f7b8a8d1caea0..ccb845be29f65dd97c9b443b75943edb5e44539a 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Shrink.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Shrink.lean @@ -1,186 +1,186 @@ -import OAI.Analysis.FourierLLogL.Compress.Endpoint -import OAI.Analysis.FourierLLogL.Thermal.Power - -/-! # Iterated spectral compression -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M l : ℕ} -open scoped Classical - -def supported (s : Finset (Grid l)) (P : Law (Grid l)) := ∀ i, 0 < P i → i∈s - -def evenly (s : Finset (Grid l)) (P : Law (Grid l)) := - ∀ i∈s, P i = (s.card:ℝ)⁻¹ - -lemma exists_uniform_sub {s : Finset (Grid l)} (hs : s.Nonempty) : - ∃ p : Law (Grid l), supported s p ∧ evenly s p := by - classical - have : Nonempty (s:Type) := Finset.Nonempty.to_subtype hs - let f : s → Grid l := fun x => x.val - refine ⟨(uniform _).send f, ?_,?_⟩ - · intro i hi; obtain ⟨x,rfl,_⟩ := (send_pos_iff _ _ _).mp hi - exact x.property - intro i hi - rw [send_apply,prob_eq] - simp_rw [uniform_apply,Fintype.card_coe] - rw [Finset.sum_eq_single (⟨i,hi⟩:s),ite_eq_left rfl] - · intro j _ hj - exact ite_eq_right (fun h => hj (Subtype.ext h)) - simp - -lemma represent_law {s : Finset (Grid l)} {P : Law (Grid l)} - (hp : supported s P) : - ∃ q : Law s, q.send (fun x => (x:Grid l))=P := by - obtain ⟨x,hx⟩ := mem_nonempty P - have hx' := hp x hx - let f (i : Grid l) : s := if hi : i∈s then ⟨i,hi⟩ else ⟨x,hx'⟩ - refine ⟨P.send f,?_⟩ - rw [send_send] - rw [send_congr_mem P (Y:=fun x => x),send_id] - intro y hy - simp [f,hp y hy] - -/-- averaged defect form. -/ -def averageCost (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) : Form M := - fun i => p.prob fun y => i = raise hl (y-x) - -lemma averageCost_pos (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) : - 0 ≤ averageCost hl p x := fun _ => prob_nonneg _ _ - -lemma average_form (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) (t : Grid M) : - form (averageCost hl p x) t = p.Ex (fun y => cost (raise hl (y-x)) t) := by - simp_rw [form,averageCost,prob,← Ex_mul,← Ex_finset_sum] - congr 1; funext i - simp only [ite_mul, one_mul,zero_mul] - simp - -def shrinkScale (D : ℝ) (j : ℕ) := - 300000*D^2*height j^2/(slow j:ℝ)^2 - -lemma spectral_compression (D : ℝ) (hD : 1 ≤ D) : - ∀ᶠ j : ℕ in atTop, ∀ (M l : ℕ) (hl : l ≤ M) (A : Form M) - (R : ℝ) (hA : 0 ≤ A), 2^(M-l) ≤ R → S M R ≤ A → - -log (fq hl A hA 0) ≤ D*height j → - ∀ (s : Finset (Grid l)), s.Nonempty → - (∀ n : ℕ, n ≤ 2^(2*j) → Compress.lnS (Fin n) (fun x : s => (x:Grid l)) ≤ height j * (j:ℝ)^2) → - ∀ t : ℝ, height j/(slow j:ℝ)^2 ≤ t → t ≤ D*height j → - (∀ P : Law (Grid l), supported s P → levelCost hl A P (sLen j) ≤ t) → - ∃ (x : Grid l) (u : Finset (Grid l)), x∈s ∧ u⊆s ∧ u.Nonempty ∧ - log (s.card:ℝ)-log (u.card:ℝ) ≤ shrinkScale D j/t ∧ - ∀ P : Law (Grid l), supported u P → - F (A + long j • averageCost hl P x) - F A ≤ 350*t := by - classical - filter_upwards [Compress.entropy_compression D hD] with j hj - intro M l hl A R hA hR hH hp s hs hL t ht ht' hT - have : Nonempty (s:Type) := Finset.Nonempty.to_subtype hs - let f : s → Grid l := fun x => x - obtain ⟨h0,h1,h2,h3,h4,_⟩ := fq_spec hl A hA hR hH - obtain ⟨x,u,hu,hv,H⟩ := hj s (Grid l) (Grid M) _ _ _ inferInstance _ _ f Subtype.val_injective - (fq hl A hA) h0 (fq_cost hl A) h1 h2 (label A) (raiseChar hl) - (by intro t i; rw [raiseChar_eq,norm_chi]) h3 h4 hp hL t ht ht' (by - intro p - let P := p.send f - have HH := (hT P fun i hi => by - obtain ⟨x,rfl,_⟩ := (send_pos_iff _ _ _).mp hi - exact x.property) - have hn (i : Grid M) : 0 < cred A i := by - unfold cred - exact div_pos (q_pos_one hA (lt_of_lt_of_le (S_one_pos - ((one_le_pow₀ (by norm_num : (1:ℝ)≤2)).trans hR)) (hH _)) _) (q_origin_pos _) - rw [selected_heat _ hl A hn p f,← heat_exp] - exact exp_monotone (neg_le_neg HH)) - let w := u.image f - have hw : w⊆s := by - intro a ha - obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp ha - exact x.property - refine ⟨x,w,x.property,hw,hu.image f,?_,?_⟩ - · rw [Fintype.card_coe] at hv - rw [show w.card=u.card from Finset.card_image_of_injective u Subtype.val_injective] - apply hv.trans; unfold shrinkScale; exact le_of_eq (by ring) - intro P hP - obtain ⟨Q,hQ⟩ := represent_law (s:=s) (show supported s P from fun x hx => hw (hP x hx)) - have hU (y) (hy : 0 < Q y) : y∈u := by - have he : 0 < P (f y) := hQ ▸ Q.send_pos f hy - obtain ⟨x,hx,H⟩ := Finset.mem_image.mp (hP _ he) - rwa [Subtype.ext H] at hx - have He := H Q hU - let B := averageCost hl P x - have hX (y : Grid M) : - exp ((-long j)*Q.Ex (fun i => Compress.defect f (raiseChar hl) i x y)) = - boltz (long j • B) y := by - unfold B boltz; rw [form_smul,average_form,← hQ,Ex_send] - refine congrArg exp ?_; rw [neg_mul] - simp_rw [Compress.defect,raiseChar_eq,cost,wave] - rfl - simp_rw [hX,boltz_mean, Real.exp_le_exp] at He - change F (A+long j • B)-F A ≤ _ - linarith - -lemma choose_compression {hl : l ≤ M} {A : Form M} {T : ℕ} {s : Finset (Grid l)} {b c Z L : ℝ} - (hc : 0 < c) (hZ : 0 ≤ Z) (hb : c ≤ b) - (H : ∀ u : Finset (Grid l), u ⊆ s → u.Nonempty → - ∀ t : ℝ, c ≤ t → t ≤ b → - (∀ P : Law (Grid l), supported u P → levelCost hl A P T ≤ t) → - ∃ (x : Grid l) (w : Finset (Grid l)), x∈u ∧ w⊆u ∧ w.Nonempty ∧ - log (u.card:ℝ)-log (w.card:ℝ) ≤ Z/t ∧ - ∀ P : Law (Grid l), supported w P → - F (A + L • averageCost hl P x) - F A ≤ 350*t) - (hs : s.Nonempty) (hM : ∀ P : Law (Grid l), supported s P → levelCost hl A P T ≤ b) : - ∃ (x : Grid l) (w : Finset (Grid l)) (P : Law (Grid l)) (t : ℝ), - c ≤ t ∧ t ≤ b ∧ x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ - log (s.card:ℝ)-log (w.card:ℝ) ≤ 4*Z/c ∧ - F (A + L • averageCost hl P x) - F A ≤ 350*t ∧ - ((t = c ∧ evenly w P) ∨ t/4 ≤ levelCost hl A P T) := by - let Good (w : Finset (Grid l)) (x : Grid l) (P : Law (Grid l)) (t : ℝ) : Prop := - F (A + L • averageCost hl P x) - F A ≤ 350*t ∧ - ((t=c ∧ evenly w P) ∨ t/4 ≤ levelCost hl A P T) - have bottom (u : Finset (Grid l)) (hu : u⊆s) (hv : u.Nonempty) - (ht : ∀ P : Law (Grid l), supported u P → levelCost hl A P T ≤ c) : - ∃ (x : Grid l) (w : Finset (Grid l)) (P : Law (Grid l)), - x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ - log (u.card:ℝ)-log (w.card:ℝ) ≤ Z/c ∧ Good w x P c := by - obtain ⟨x,w,hx,hw,hS,h1,h2⟩ := H u hu hv c le_rfl hb ht - obtain ⟨P,h3,h4⟩ := exists_uniform_sub hS - exact ⟨x,w,P,hu hx,Finset.Subset.trans hw hu,hS,h3,h1,h2 _ h3,Or.inl ⟨rfl,h4⟩⟩ - let K := fun (u : Finset (Grid l)) (f : ℝ) => ∃ (x : Grid l) - (w : Finset (Grid l)) (P : Law (Grid l)) (t : ℝ), - c ≤ t ∧ t ≤ b ∧ x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ - log (u.card:ℝ)-log (w.card:ℝ) ≤ 4*Z/c-Z/f ∧ Good w x P t - have HH (n : ℕ) (u : Finset (Grid l)) (hu : u⊆s) (h0 : u.Nonempty) - (f : ℝ) (hf : c ≤ f) (hB : f ≤ b) (he : f ≤ n*c) (hi : ∀ P : Law (Grid l), - supported u P → levelCost hl A P T ≤ f) : K u f := by - induction n generalizing u f with - | zero => norm_num at he; linarith - | succ n ih => - have hp : 0 < f := lt_of_lt_of_le hc hf - have h1 : Z/f ≤ Z/c := by gcongr - have hN : 0 ≤ Z/c := div_nonneg hZ hc.le - obtain ⟨x,w,hx,hw,hS,hG,hM⟩ := H _ hu h0 f hf hB hi - by_cases h : ∃ P : Law (Grid l), supported w P ∧ f/4 ≤ levelCost hl A P T - · rcases h with ⟨P,hP,hE⟩ - exact ⟨x,w,P,f,hf,hB,hu hx,Finset.Subset.trans hw hu,hS,hP,by rw [mul_div_assoc]; linarith,hM _ hP,Or.inr hE⟩ - have h2 (P : Law (Grid l)) (hP : supported w P) : levelCost hl A P T ≤ f/4 := - le_of_lt <| lt_of_not_ge fun hh => h ⟨P,hP,hh⟩ - rcases le_total (f/4) c with hy|hy - · obtain ⟨x,V,P,h1',h2',h3,h4,h5,h6⟩ := bottom w (Finset.Subset.trans hw hu) hS - (fun x hx => (h2 x hx).trans hy) - exact ⟨x,V,P,c,le_rfl,hb,h1',h2',h3,h4,by rw [mul_div_assoc]; linarith,h6⟩ - obtain ⟨x,V,P,t,h1',h2',h3,h4,h5,h6,h7,h8⟩ := ih w (Finset.Subset.trans hw hu) hS - (f/4) hy (by linarith) (by push_cast at he; nlinarith) h2 - rw [show Z/(f/4)=4*(Z/f) by ring] at h7 - exact ⟨x,V,P,t,h1',h2',h3,h4,h5,h6,by linarith [div_nonneg hZ hp.le],h8⟩ - obtain ⟨n,hn⟩ := exists_nat_gt (b/c) - obtain ⟨x,w,P,t,h1,h2,h3,h4,h5,h6,h7,h8⟩ := - HH n s (Finset.Subset.refl _) hs b hb le_rfl ((div_lt_iff₀ hc).mp hn).le hM - exact ⟨x,w,P,t,h1,h2,h3,h4,h5,h6,h7.trans (sub_le_self _ (div_nonneg hZ (hc.le.trans hb))),h8⟩ - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Compress.Endpoint +import OAI.Analysis.FourierLLogL.Thermal.Power + +/-! # Iterated spectral compression -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +variable {M l : ℕ} +open scoped Classical + +def supported (s : Finset (Grid l)) (P : Law (Grid l)) := ∀ i, 0 < P i → i∈s + +def evenly (s : Finset (Grid l)) (P : Law (Grid l)) := + ∀ i∈s, P i = (s.card:ℝ)⁻¹ + +lemma exists_uniform_sub {s : Finset (Grid l)} (hs : s.Nonempty) : + ∃ p : Law (Grid l), supported s p ∧ evenly s p := by + classical + have : Nonempty (s:Type) := Finset.Nonempty.to_subtype hs + let f : s → Grid l := fun x => x.val + refine ⟨(uniform _).send f, ?_,?_⟩ + · intro i hi; obtain ⟨x,rfl,_⟩ := (send_pos_iff _ _ _).mp hi + exact x.property + intro i hi + rw [send_apply,prob_eq] + simp_rw [uniform_apply,Fintype.card_coe] + rw [Finset.sum_eq_single (⟨i,hi⟩:s),ite_eq_left rfl] + · intro j _ hj + exact ite_eq_right (fun h => hj (Subtype.ext h)) + simp + +lemma represent_law {s : Finset (Grid l)} {P : Law (Grid l)} + (hp : supported s P) : + ∃ q : Law s, q.send (fun x => (x:Grid l))=P := by + obtain ⟨x,hx⟩ := mem_nonempty P + have hx' := hp x hx + let f (i : Grid l) : s := if hi : i∈s then ⟨i,hi⟩ else ⟨x,hx'⟩ + refine ⟨P.send f,?_⟩ + rw [send_send] + rw [send_congr_mem P (Y:=fun x => x),send_id] + intro y hy + simp [f,hp y hy] + +/-- averaged defect form. -/ +def averageCost (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) : Form M := + fun i => p.prob fun y => i = raise hl (y-x) + +lemma averageCost_pos (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) : + 0 ≤ averageCost hl p x := fun _ => prob_nonneg _ _ + +lemma average_form (hl : l ≤ M) (p : Law (Grid l)) (x : Grid l) (t : Grid M) : + form (averageCost hl p x) t = p.Ex (fun y => cost (raise hl (y-x)) t) := by + simp_rw [form,averageCost,prob,← Ex_mul,← Ex_finset_sum] + congr 1; funext i + simp only [ite_mul, one_mul,zero_mul] + simp + +def shrinkScale (D : ℝ) (j : ℕ) := + 300000*D^2*height j^2/(slow j:ℝ)^2 + +lemma spectral_compression (D : ℝ) (hD : 1 ≤ D) : + ∀ᶠ j : ℕ in atTop, ∀ (M l : ℕ) (hl : l ≤ M) (A : Form M) + (R : ℝ) (hA : 0 ≤ A), 2^(M-l) ≤ R → S M R ≤ A → + -log (fq hl A hA 0) ≤ D*height j → + ∀ (s : Finset (Grid l)), s.Nonempty → + (∀ n : ℕ, n ≤ 2^(2*j) → Compress.lnS (Fin n) (fun x : s => (x:Grid l)) ≤ height j * (j:ℝ)^2) → + ∀ t : ℝ, height j/(slow j:ℝ)^2 ≤ t → t ≤ D*height j → + (∀ P : Law (Grid l), supported s P → levelCost hl A P (sLen j) ≤ t) → + ∃ (x : Grid l) (u : Finset (Grid l)), x∈s ∧ u⊆s ∧ u.Nonempty ∧ + log (s.card:ℝ)-log (u.card:ℝ) ≤ shrinkScale D j/t ∧ + ∀ P : Law (Grid l), supported u P → + F (A + long j • averageCost hl P x) - F A ≤ 350*t := by + classical + filter_upwards [Compress.entropy_compression D hD] with j hj + intro M l hl A R hA hR hH hp s hs hL t ht ht' hT + have : Nonempty (s:Type) := Finset.Nonempty.to_subtype hs + let f : s → Grid l := fun x => x + obtain ⟨h0,h1,h2,h3,h4,_⟩ := fq_spec hl A hA hR hH + obtain ⟨x,u,hu,hv,H⟩ := hj s (Grid l) (Grid M) _ _ _ inferInstance _ _ f Subtype.val_injective + (fq hl A hA) h0 (fq_cost hl A) h1 h2 (label A) (raiseChar hl) + (by intro t i; rw [raiseChar_eq,norm_chi]) h3 h4 hp hL t ht ht' (by + intro p + let P := p.send f + have HH := (hT P fun i hi => by + obtain ⟨x,rfl,_⟩ := (send_pos_iff _ _ _).mp hi + exact x.property) + have hn (i : Grid M) : 0 < cred A i := by + unfold cred + exact div_pos (q_pos_one hA (lt_of_lt_of_le (S_one_pos + ((one_le_pow₀ (by norm_num : (1:ℝ)≤2)).trans hR)) (hH _)) _) (q_origin_pos _) + rw [selected_heat _ hl A hn p f,← heat_exp] + exact exp_monotone (neg_le_neg HH)) + let w := u.image f + have hw : w⊆s := by + intro a ha + obtain ⟨x,hx,rfl⟩ := Finset.mem_image.mp ha + exact x.property + refine ⟨x,w,x.property,hw,hu.image f,?_,?_⟩ + · rw [Fintype.card_coe] at hv + rw [show w.card=u.card from Finset.card_image_of_injective u Subtype.val_injective] + apply hv.trans; unfold shrinkScale; exact le_of_eq (by ring) + intro P hP + obtain ⟨Q,hQ⟩ := represent_law (s:=s) (show supported s P from fun x hx => hw (hP x hx)) + have hU (y) (hy : 0 < Q y) : y∈u := by + have he : 0 < P (f y) := hQ ▸ Q.send_pos f hy + obtain ⟨x,hx,H⟩ := Finset.mem_image.mp (hP _ he) + rwa [Subtype.ext H] at hx + have He := H Q hU + let B := averageCost hl P x + have hX (y : Grid M) : + exp ((-long j)*Q.Ex (fun i => Compress.defect f (raiseChar hl) i x y)) = + boltz (long j • B) y := by + unfold B boltz; rw [form_smul,average_form,← hQ,Ex_send] + refine congrArg exp ?_; rw [neg_mul] + simp_rw [Compress.defect,raiseChar_eq,cost,wave] + rfl + simp_rw [hX,boltz_mean, Real.exp_le_exp] at He + change F (A+long j • B)-F A ≤ _ + linarith + +lemma choose_compression {hl : l ≤ M} {A : Form M} {T : ℕ} {s : Finset (Grid l)} {b c Z L : ℝ} + (hc : 0 < c) (hZ : 0 ≤ Z) (hb : c ≤ b) + (H : ∀ u : Finset (Grid l), u ⊆ s → u.Nonempty → + ∀ t : ℝ, c ≤ t → t ≤ b → + (∀ P : Law (Grid l), supported u P → levelCost hl A P T ≤ t) → + ∃ (x : Grid l) (w : Finset (Grid l)), x∈u ∧ w⊆u ∧ w.Nonempty ∧ + log (u.card:ℝ)-log (w.card:ℝ) ≤ Z/t ∧ + ∀ P : Law (Grid l), supported w P → + F (A + L • averageCost hl P x) - F A ≤ 350*t) + (hs : s.Nonempty) (hM : ∀ P : Law (Grid l), supported s P → levelCost hl A P T ≤ b) : + ∃ (x : Grid l) (w : Finset (Grid l)) (P : Law (Grid l)) (t : ℝ), + c ≤ t ∧ t ≤ b ∧ x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ + log (s.card:ℝ)-log (w.card:ℝ) ≤ 4*Z/c ∧ + F (A + L • averageCost hl P x) - F A ≤ 350*t ∧ + ((t = c ∧ evenly w P) ∨ t/4 ≤ levelCost hl A P T) := by + let Good (w : Finset (Grid l)) (x : Grid l) (P : Law (Grid l)) (t : ℝ) : Prop := + F (A + L • averageCost hl P x) - F A ≤ 350*t ∧ + ((t=c ∧ evenly w P) ∨ t/4 ≤ levelCost hl A P T) + have bottom (u : Finset (Grid l)) (hu : u⊆s) (hv : u.Nonempty) + (ht : ∀ P : Law (Grid l), supported u P → levelCost hl A P T ≤ c) : + ∃ (x : Grid l) (w : Finset (Grid l)) (P : Law (Grid l)), + x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ + log (u.card:ℝ)-log (w.card:ℝ) ≤ Z/c ∧ Good w x P c := by + obtain ⟨x,w,hx,hw,hS,h1,h2⟩ := H u hu hv c le_rfl hb ht + obtain ⟨P,h3,h4⟩ := exists_uniform_sub hS + exact ⟨x,w,P,hu hx,Finset.Subset.trans hw hu,hS,h3,h1,h2 _ h3,Or.inl ⟨rfl,h4⟩⟩ + let K := fun (u : Finset (Grid l)) (f : ℝ) => ∃ (x : Grid l) + (w : Finset (Grid l)) (P : Law (Grid l)) (t : ℝ), + c ≤ t ∧ t ≤ b ∧ x∈s ∧ w⊆s ∧ w.Nonempty ∧ supported w P ∧ + log (u.card:ℝ)-log (w.card:ℝ) ≤ 4*Z/c-Z/f ∧ Good w x P t + have HH (n : ℕ) (u : Finset (Grid l)) (hu : u⊆s) (h0 : u.Nonempty) + (f : ℝ) (hf : c ≤ f) (hB : f ≤ b) (he : f ≤ n*c) (hi : ∀ P : Law (Grid l), + supported u P → levelCost hl A P T ≤ f) : K u f := by + induction n generalizing u f with + | zero => norm_num at he; linarith + | succ n ih => + have hp : 0 < f := lt_of_lt_of_le hc hf + have h1 : Z/f ≤ Z/c := by gcongr + have hN : 0 ≤ Z/c := div_nonneg hZ hc.le + obtain ⟨x,w,hx,hw,hS,hG,hM⟩ := H _ hu h0 f hf hB hi + by_cases h : ∃ P : Law (Grid l), supported w P ∧ f/4 ≤ levelCost hl A P T + · rcases h with ⟨P,hP,hE⟩ + exact ⟨x,w,P,f,hf,hB,hu hx,Finset.Subset.trans hw hu,hS,hP,by rw [mul_div_assoc]; linarith,hM _ hP,Or.inr hE⟩ + have h2 (P : Law (Grid l)) (hP : supported w P) : levelCost hl A P T ≤ f/4 := + le_of_lt <| lt_of_not_ge fun hh => h ⟨P,hP,hh⟩ + rcases le_total (f/4) c with hy|hy + · obtain ⟨x,V,P,h1',h2',h3,h4,h5,h6⟩ := bottom w (Finset.Subset.trans hw hu) hS + (fun x hx => (h2 x hx).trans hy) + exact ⟨x,V,P,c,le_rfl,hb,h1',h2',h3,h4,by rw [mul_div_assoc]; linarith,h6⟩ + obtain ⟨x,V,P,t,h1',h2',h3,h4,h5,h6,h7,h8⟩ := ih w (Finset.Subset.trans hw hu) hS + (f/4) hy (by linarith) (by push_cast at he; nlinarith) h2 + rw [show Z/(f/4)=4*(Z/f) by ring] at h7 + exact ⟨x,V,P,t,h1',h2',h3,h4,h5,h6,by linarith [div_nonneg hZ hp.le],h8⟩ + obtain ⟨n,hn⟩ := exists_nat_gt (b/c) + obtain ⟨x,w,P,t,h1,h2,h3,h4,h5,h6,h7,h8⟩ := + HH n s (Finset.Subset.refl _) hs b hb le_rfl ((div_lt_iff₀ hc).mp hn).le hM + exact ⟨x,w,P,t,h1,h2,h3,h4,h5,h6,h7.trans (sub_le_self _ (div_nonneg hZ (hc.le.trans hb))),h8⟩ + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Side.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Side.lean index 9292cebd1a4d9b7e0a16dfdb786f60267d6c1f92..818b986708da8c38c6e9a5312825ed4f10f2642f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Side.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Side.lean @@ -1,183 +1,183 @@ -import OAI.Analysis.FourierLLogL.Thermal.Bias - -/-! # Accounting for the side label -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period - -variable {M l : ℕ} - -lemma cert_S_energy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} (H : Certified h err n p A) : - (label A).Ex (form (place p M)) ≤ 100 := by - rcases H.prev with ⟨he,_⟩ | he - · subst p - refine le_trans (b:=(label A).Ex (fun _ => 2)) (Ex_mono _ fun x => ?_) - (by rw [Ex_const]; norm_num) - rw [place, invlen_top, form_S]; simp [(cost_bounds 1 x).2] - exact S_energy _ p.invlen_ge (loc_scale H.fits.2) A he - -lemma local_channel_energy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} - (H : Certified h err n p A) (x : Grid M) (hx : x ∈ p.block M) : - (channel A x).Ex (fun z => form (place p M) (z-start p M)) ≤ 264 := by - let P := label A - let B := place p M - let t := start p M - rw [ch_ex] - change P.Ex (fun z => form B (x+z-t)) ≤ _ - calc - _ ≤ P.Ex (fun z => 2*(form B z+form B (x-t))) := Ex_mono _ fun z => by - rw [show x+z-t=z+(x-t) by ring] - exact form_triangle (place_valid _ _) _ _ - _ ≤ _ := by - rw [Ex_lmul,Ex_add (g:=fun _ => form B (x-t)),Ex_const] - have hh : P.Ex (form B) ≤ 100 := cert_S_energy H - have he : form B (x-t) ≤ 32 := place_internal hx (start_mem H.fits.1 H.fits.2) - linarith - -/-- Absolute information cap wrt a fixed comparison law on z. -/ -lemma side_entropy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} - (H : Certified h err n p A) (x : Grid M) (hx : x ∈ p.block M) : - (channel A x).Ex (fun z => log (channel A x z) - - log (channel (place p M) (p.start M) z)) ≤ F A-log p.invlen+300 := by - let B := place p M - let t := start p M - let P := channel A x - refine le_trans (b:=P.Ex (fun z => F A-F B+form B (z-t))) (Ex_mono _ fun z => ?_) ?_ - · change log (channel A x z) - log (channel B t z) ≤ _ - rw [ch_K,ch_K, log_mul (side_pos _).ne' (K_pos _ _).ne', - log_mul (side_pos _).ne' (K_pos _ _).ne', K_eq, K_eq,log_exp,log_exp] - linarith [form_nonneg H.nonneg (z-x)] - · rw [Ex_add,Ex_const] - have he := local_channel_energy H x hx - have hi : log p.invlen-12 ≤ F B := - F_S_lower _ p.invlen_pos (loc_scale H.fits.2) - unfold P B t at *; linarith - -lemma log_input_bound {p : Node} (hp : p.ok M) {a : Grid M → ℝ} - {P : Law (Grid M)} (h : PieceLaw p a P) {d : ℝ} (h1 : ∀ x, a x ≤ exp d) - (hm : exp (-d) ≤ p.avg a) (x : Grid M) (hx : 0 < P x) : - log (P x) ≤ 2*d+log (side M)+log p.invlen := by - have Hi := piece_support hp h _ hx - have H := log_le_log hx (piece_density h h1 hm x) - rw [dice_apply hp,ite_eq_left Hi,← rinv] at H - rw [log_mul (exp_pos _).ne' (by have := side_pos M; have := p.invlen_pos; positivity), - log_mul (side_pos _).ne' p.invlen_pos.ne', log_exp] at H - linarith - -open scoped Classical - -private lemma cut_sum_bound (hp : l ≤ M) {p : Node} (hv : p.ok M) (hh : M-l ≤ p.lev) - (f : Grid l → ℝ) (hf : ∀ x, 0 ≤ f x) : - ∑ x ∈ p.group M, f (cut hp x) ≤ ∑ x, f x := by - rw [← Finset.sum_image (s:=p.group M)] - · exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) (fun x _ _ => hf x) - intro x hx y hy he - exact cut_injOn hp hv.2 hh ((mem_group _ _).mp hx) ((mem_group _ _).mp hy) he - -lemma log_side_ratio (hl : l ≤ M) : log (side M)-log (side l) = -log ((2:ℝ)^(M-l)) := by - simp_rw [side_pow_two, Real.log_inv] - simp_rw [Real.log_pow] - rw [Nat.cast_sub hl] - ring - -/-- General positive-product estimate. The extra factor of two in the testing volume -allows a predictable child mask. -/ -lemma product_entropy_bound {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') - {n : ℕ} {h err : ℝ} {A : Form M} (H : Certified h err n p' A) - {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) (hh : M-l ≤ p.lev) - {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) (B : Bool) - (Q : Grid l → Grid M → ℝ) (hQ : ∀ x z, 0 < Q x z) - (hQ' : ∀ z, expU (fun x => Q x z) ≤ 1) : - (P.withNext (channel A)).Ex (fun xz => if xz.1∈(p.child B).block M then - log (Q (cut hl xz.1) xz.2) else 0) ≤ - 2*d + log p.invlen - log ((2:ℝ)^(M-l)) + F A - log p'.invlen + 301 := by - classical - let C (x : Grid M) : Prop := x ∈ (p.child B).block M - let T (x z : Grid M) := if C x then Q (cut hl x) z else 1 - have hT (x z) : 0 < T x z := by unfold T; split_ifs; exact hQ _ _; norm_num - let L := channel (place p' M) (p'.start M) - let v (xz : Grid M × Grid M) := if xz.1∈p.block M then side l * (L xz.2 * T xz.1 xz.2)/2 else 0 - have hvL (z) : 0 < L z := ch_pos _ _ _ - have hvV (xz) : 0 ≤ v xz := by - unfold v; split_ifs - · have := side_pos l; have := hvL xz.2; have := hT xz.1 xz.2; positivity - · exact le_rfl - have hcV : ∑ xz, v xz ≤ 1 := by - rw [Fintype.sum_prod_type,Finset.sum_comm,← L.total] - refine Finset.sum_le_sum fun z _ => ?_ - have he : ∑ x ∈ p.group M, side l * T x z ≤ 2 := - calc - _ ≤ ∑ x ∈ p.group M, side l * (1+Q (cut hl x) z) := - Finset.sum_le_sum fun x hx => by - apply mul_le_mul_of_nonneg_left _ (side_pos _).le - unfold T; split_ifs - · linarith - · linarith [hQ (cut hl x) z] - _ ≤ ∑ x, side l * (1+Q x z) := cut_sum_bound hl hp hh (fun x => side l*(1+Q x z)) - (fun x => by have := side_pos l; have := hQ x z; positivity) - _ ≤ _ := by - rw [← Finset.mul_sum] - change expU (fun x => 1+Q x z) ≤ _ - rw [expU_ex,Ex_add,Ex_const,← expU_ex] - linarith [hQ' z] - have hA : (p.group M)=Finset.univ.filter fun x => x∈p.block M := by ext x; simp [mem_group] - calc - _ = (L z / 2) * (∑ x ∈ p.group M, side l*T x z) := by - unfold v; rw [Finset.mul_sum,hA,Finset.sum_filter] - refine Finset.sum_congr rfl fun x _ => ?_ - split_ifs <;> ring - _ ≤ _ := by nlinarith [hvL z] - let U := P.withNext (channel A) - have hUS (x z : Grid M) (hx : 0 < U (x,z)) : 0 < P x := by - rw [show U (x,z) = P x * channel A x z from rfl] at hx - exact pos_of_mul_pos_left hx (ch_pos _ _ _).le - have hhV (xz : Grid M × Grid M) (hx : 0 < U xz) : 0 < v xz := by - obtain ⟨x,z⟩ := xz - change 0 < if x∈p.block M then _ else _ - rw [ite_eq_left (piece_support hp ht _ (hUS x z hx))] - have := side_pos l; have := hvL z; have := hT x z; positivity - have He := entropy_le_cross_aux U v hvV hcV hhV - unfold entropy crossE at He - simp_rw [Ex_neg] at He - let f := fun xz : Grid M × Grid M => log (P xz.1) - let g := fun xz : Grid M × Grid M => log (channel A xz.1 xz.2)-log (L xz.2) - let W := fun xz : Grid M × Grid M => if xz.1∈(p.child B).block M then - log (Q (cut hl xz.1) xz.2) else 0 - have hE : U.Ex W ≤ U.Ex f + U.Ex g + log 2-log (side l) := by - have Hx (p0 : Grid M × Grid M) (hx : 0 < U p0) : - W p0 = f p0+g p0+(log 2-log (side l)) + (log (v p0)-log (U p0)) := by - obtain ⟨x,z⟩ := p0 - have hx' := hUS x z hx - unfold W f g v; dsimp - rw [ite_eq_left (piece_support hp ht _ hx')] - change (if x∈_ then _ else _) = log (P x)+_+_+(log (side l*(L z*T x z)/2)-log (P x * channel A x z)) - rw [Real.log_div (by have := side_pos l; have := hvL z; have := hT x z; positivity) (by norm_num), - log_mul (side_pos _).ne' (mul_pos (hvL _) (hT _ _)).ne', - log_mul (hvL z).ne' (hT _ _).ne', - log_mul hx'.ne' (ch_pos _ _ _).ne'] - unfold T C; split_ifs - · ring - rw [log_one]; ring - rw [Ex_congr_mem _ Hx] - simp_rw [Ex_add,Ex_sub,Ex_const] - linarith - have Hf : U.Ex f ≤ 2*d+log (side M)+log p.invlen := by - unfold U f - simp_rw [Ex_withNext,Ex_const] - rw [← Ex_const P (2*d+log (side M)+log p.invlen)] - exact Ex_mono_mem _ (log_input_bound hp ht h1 hm) - have Hg : U.Ex g ≤ F A-log p'.invlen+300 := by - unfold U g - rw [Ex_withNext,← Ex_const P (F A-log p'.invlen+300)] - exact Ex_mono_mem _ fun x hx => side_entropy H x (block_of_below hv M (piece_support hp ht _ hx)) - have Ht := log_side_ratio hl - change U.Ex W ≤ _ - linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Bias + +/-! # Accounting for the side label -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period + +variable {M l : ℕ} + +lemma cert_S_energy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} (H : Certified h err n p A) : + (label A).Ex (form (place p M)) ≤ 100 := by + rcases H.prev with ⟨he,_⟩ | he + · subst p + refine le_trans (b:=(label A).Ex (fun _ => 2)) (Ex_mono _ fun x => ?_) + (by rw [Ex_const]; norm_num) + rw [place, invlen_top, form_S]; simp [(cost_bounds 1 x).2] + exact S_energy _ p.invlen_ge (loc_scale H.fits.2) A he + +lemma local_channel_energy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} + (H : Certified h err n p A) (x : Grid M) (hx : x ∈ p.block M) : + (channel A x).Ex (fun z => form (place p M) (z-start p M)) ≤ 264 := by + let P := label A + let B := place p M + let t := start p M + rw [ch_ex] + change P.Ex (fun z => form B (x+z-t)) ≤ _ + calc + _ ≤ P.Ex (fun z => 2*(form B z+form B (x-t))) := Ex_mono _ fun z => by + rw [show x+z-t=z+(x-t) by ring] + exact form_triangle (place_valid _ _) _ _ + _ ≤ _ := by + rw [Ex_lmul,Ex_add (g:=fun _ => form B (x-t)),Ex_const] + have hh : P.Ex (form B) ≤ 100 := cert_S_energy H + have he : form B (x-t) ≤ 32 := place_internal hx (start_mem H.fits.1 H.fits.2) + linarith + +/-- Absolute information cap wrt a fixed comparison law on z. -/ +lemma side_entropy {n : ℕ} {p : Node} {h err : ℝ} {A : Form M} + (H : Certified h err n p A) (x : Grid M) (hx : x ∈ p.block M) : + (channel A x).Ex (fun z => log (channel A x z) - + log (channel (place p M) (p.start M) z)) ≤ F A-log p.invlen+300 := by + let B := place p M + let t := start p M + let P := channel A x + refine le_trans (b:=P.Ex (fun z => F A-F B+form B (z-t))) (Ex_mono _ fun z => ?_) ?_ + · change log (channel A x z) - log (channel B t z) ≤ _ + rw [ch_K,ch_K, log_mul (side_pos _).ne' (K_pos _ _).ne', + log_mul (side_pos _).ne' (K_pos _ _).ne', K_eq, K_eq,log_exp,log_exp] + linarith [form_nonneg H.nonneg (z-x)] + · rw [Ex_add,Ex_const] + have he := local_channel_energy H x hx + have hi : log p.invlen-12 ≤ F B := + F_S_lower _ p.invlen_pos (loc_scale H.fits.2) + unfold P B t at *; linarith + +lemma log_input_bound {p : Node} (hp : p.ok M) {a : Grid M → ℝ} + {P : Law (Grid M)} (h : PieceLaw p a P) {d : ℝ} (h1 : ∀ x, a x ≤ exp d) + (hm : exp (-d) ≤ p.avg a) (x : Grid M) (hx : 0 < P x) : + log (P x) ≤ 2*d+log (side M)+log p.invlen := by + have Hi := piece_support hp h _ hx + have H := log_le_log hx (piece_density h h1 hm x) + rw [dice_apply hp,ite_eq_left Hi,← rinv] at H + rw [log_mul (exp_pos _).ne' (by have := side_pos M; have := p.invlen_pos; positivity), + log_mul (side_pos _).ne' p.invlen_pos.ne', log_exp] at H + linarith + +open scoped Classical + +private lemma cut_sum_bound (hp : l ≤ M) {p : Node} (hv : p.ok M) (hh : M-l ≤ p.lev) + (f : Grid l → ℝ) (hf : ∀ x, 0 ≤ f x) : + ∑ x ∈ p.group M, f (cut hp x) ≤ ∑ x, f x := by + rw [← Finset.sum_image (s:=p.group M)] + · exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) (fun x _ _ => hf x) + intro x hx y hy he + exact cut_injOn hp hv.2 hh ((mem_group _ _).mp hx) ((mem_group _ _).mp hy) he + +lemma log_side_ratio (hl : l ≤ M) : log (side M)-log (side l) = -log ((2:ℝ)^(M-l)) := by + simp_rw [side_pow_two, Real.log_inv] + simp_rw [Real.log_pow] + rw [Nat.cast_sub hl] + ring + +/-- General positive-product estimate. The extra factor of two in the testing volume +allows a predictable child mask. -/ +lemma product_entropy_bound {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') + {n : ℕ} {h err : ℝ} {A : Form M} (H : Certified h err n p' A) + {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) (hh : M-l ≤ p.lev) + {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) (B : Bool) + (Q : Grid l → Grid M → ℝ) (hQ : ∀ x z, 0 < Q x z) + (hQ' : ∀ z, expU (fun x => Q x z) ≤ 1) : + (P.withNext (channel A)).Ex (fun xz => if xz.1∈(p.child B).block M then + log (Q (cut hl xz.1) xz.2) else 0) ≤ + 2*d + log p.invlen - log ((2:ℝ)^(M-l)) + F A - log p'.invlen + 301 := by + classical + let C (x : Grid M) : Prop := x ∈ (p.child B).block M + let T (x z : Grid M) := if C x then Q (cut hl x) z else 1 + have hT (x z) : 0 < T x z := by unfold T; split_ifs; exact hQ _ _; norm_num + let L := channel (place p' M) (p'.start M) + let v (xz : Grid M × Grid M) := if xz.1∈p.block M then side l * (L xz.2 * T xz.1 xz.2)/2 else 0 + have hvL (z) : 0 < L z := ch_pos _ _ _ + have hvV (xz) : 0 ≤ v xz := by + unfold v; split_ifs + · have := side_pos l; have := hvL xz.2; have := hT xz.1 xz.2; positivity + · exact le_rfl + have hcV : ∑ xz, v xz ≤ 1 := by + rw [Fintype.sum_prod_type,Finset.sum_comm,← L.total] + refine Finset.sum_le_sum fun z _ => ?_ + have he : ∑ x ∈ p.group M, side l * T x z ≤ 2 := + calc + _ ≤ ∑ x ∈ p.group M, side l * (1+Q (cut hl x) z) := + Finset.sum_le_sum fun x hx => by + apply mul_le_mul_of_nonneg_left _ (side_pos _).le + unfold T; split_ifs + · linarith + · linarith [hQ (cut hl x) z] + _ ≤ ∑ x, side l * (1+Q x z) := cut_sum_bound hl hp hh (fun x => side l*(1+Q x z)) + (fun x => by have := side_pos l; have := hQ x z; positivity) + _ ≤ _ := by + rw [← Finset.mul_sum] + change expU (fun x => 1+Q x z) ≤ _ + rw [expU_ex,Ex_add,Ex_const,← expU_ex] + linarith [hQ' z] + have hA : (p.group M)=Finset.univ.filter fun x => x∈p.block M := by ext x; simp [mem_group] + calc + _ = (L z / 2) * (∑ x ∈ p.group M, side l*T x z) := by + unfold v; rw [Finset.mul_sum,hA,Finset.sum_filter] + refine Finset.sum_congr rfl fun x _ => ?_ + split_ifs <;> ring + _ ≤ _ := by nlinarith [hvL z] + let U := P.withNext (channel A) + have hUS (x z : Grid M) (hx : 0 < U (x,z)) : 0 < P x := by + rw [show U (x,z) = P x * channel A x z from rfl] at hx + exact pos_of_mul_pos_left hx (ch_pos _ _ _).le + have hhV (xz : Grid M × Grid M) (hx : 0 < U xz) : 0 < v xz := by + obtain ⟨x,z⟩ := xz + change 0 < if x∈p.block M then _ else _ + rw [ite_eq_left (piece_support hp ht _ (hUS x z hx))] + have := side_pos l; have := hvL z; have := hT x z; positivity + have He := entropy_le_cross_aux U v hvV hcV hhV + unfold entropy crossE at He + simp_rw [Ex_neg] at He + let f := fun xz : Grid M × Grid M => log (P xz.1) + let g := fun xz : Grid M × Grid M => log (channel A xz.1 xz.2)-log (L xz.2) + let W := fun xz : Grid M × Grid M => if xz.1∈(p.child B).block M then + log (Q (cut hl xz.1) xz.2) else 0 + have hE : U.Ex W ≤ U.Ex f + U.Ex g + log 2-log (side l) := by + have Hx (p0 : Grid M × Grid M) (hx : 0 < U p0) : + W p0 = f p0+g p0+(log 2-log (side l)) + (log (v p0)-log (U p0)) := by + obtain ⟨x,z⟩ := p0 + have hx' := hUS x z hx + unfold W f g v; dsimp + rw [ite_eq_left (piece_support hp ht _ hx')] + change (if x∈_ then _ else _) = log (P x)+_+_+(log (side l*(L z*T x z)/2)-log (P x * channel A x z)) + rw [Real.log_div (by have := side_pos l; have := hvL z; have := hT x z; positivity) (by norm_num), + log_mul (side_pos _).ne' (mul_pos (hvL _) (hT _ _)).ne', + log_mul (hvL z).ne' (hT _ _).ne', + log_mul hx'.ne' (ch_pos _ _ _).ne'] + unfold T C; split_ifs + · ring + rw [log_one]; ring + rw [Ex_congr_mem _ Hx] + simp_rw [Ex_add,Ex_sub,Ex_const] + linarith + have Hf : U.Ex f ≤ 2*d+log (side M)+log p.invlen := by + unfold U f + simp_rw [Ex_withNext,Ex_const] + rw [← Ex_const P (2*d+log (side M)+log p.invlen)] + exact Ex_mono_mem _ (log_input_bound hp ht h1 hm) + have Hg : U.Ex g ≤ F A-log p'.invlen+300 := by + unfold U g + rw [Ex_withNext,← Ex_const P (F A-log p'.invlen+300)] + exact Ex_mono_mem _ fun x hx => side_entropy H x (block_of_below hv M (piece_support hp ht _ hx)) + have Ht := log_side_ratio hl + change U.Ex W ≤ _ + linarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2)] + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Spatial.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Spatial.lean index e4a8ba3449e19e139db5833f0de31a8be915c6d7..2041c583e9d6da5a31c85138a0fa20cc5372d45d 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Spatial.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Spatial.lean @@ -1,257 +1,257 @@ -import OAI.Analysis.FourierLLogL.Thermal.Order -import OAI.Analysis.FourierLLogL.Dyadic.Circle - -/-! # The spatial form and the discrete Gaussian bounds -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate Real -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Period Real - -variable {M : ℕ} - -lemma wave_at_one (x : Grid M) : - wave 1 x = cos (2*π * pos x) := by - have H := chi_cast (1:ℤ) x - unfold wave; norm_num [phase] at H - rw [H,Complex.exp_re] - simp - -lemma wave_at_diam (x : Grid M) : - wave 1 x = cos (2*π * Dyadic.diam x) := by - rw [diam_min] - rcases le_total (pos x) (pos (-x)) with h | h - · rw [min_eq_left h,wave_at_one] - rw [min_eq_right h,← wave_at_one,wave_even _ _] - -theorem cost_diam (x : Grid M) : - 8*(Dyadic.diam x)^2 ≤ cost 1 x ∧ cost 1 x ≤ 32*(Dyadic.diam x)^2 := by - let y := Dyadic.diam x - have hy := diam_nonneg x - have hY := diam_le_half x - unfold cost - rw [wave_at_diam] - have hp : 0 ≤ 2*π*y := mul_nonneg (by positivity) hy - have he : |2*π*y| ≤ π := by rw [abs_of_nonneg hp]; unfold y at *; nlinarith [pi_pos] - have H := cos_le_one_sub_mul_cos_sq he - have H' := @one_sub_sq_div_two_le_cos (2*π*y) - have hh : 2/π^2 * (2*π*y)^2 = 8*y^2 := by field_simp [pi_pos.ne']; ring - have hh' : (2*π*y)^2/2 ≤ 32*y^2 := by - calc - _ = (2*π^2)*y^2 := by ring - _ ≤ _ := by gcongr; nlinarith [pi_le_four,pi_pos] - rw [hh] at H - change 8*y^2 ≤ _ ∧ _ ≤ 32*y^2 - dsimp only [y] at * - constructor <;> linarith - -/-- Spatial form, specified by inverse width -/ -def S (M : ℕ) (R : ℝ) : Form M := fun s => if s=1 then R^2 else 0 - -lemma S_nonneg (M : ℕ) (R : ℝ) : 0 ≤ S M R := by - intro i - change 0 ≤ if i=1 then _ else _ - split_ifs <;> positivity - -lemma form_S (R : ℝ) (x : Grid M) : form (S M R) x = R^2*cost 1 x := by simp [form,S,ite_mul] - -lemma scale_S (M : ℕ) (t R : ℝ) : t^2 • S M R = S M (t*R) := by - funext i - change t^2*(if i=1 then _ else _) = if i=1 then _ else _ - split_ifs <;> ring - -lemma S_mono {a b : ℝ} (ha : 0 ≤ a) (h : a ≤ b) : S M a ≤ S M b := by - intro i; unfold S - split_ifs <;> nlinarith - -private lemma expU_ray (R : ℝ) (h : 0 < R) : - expU (fun x : Grid M => exp (-R * pos x)) ≤ side M + 1/R := by - let a := R*side M - have ha : 0 < a := mul_pos h (side_pos M) - let q := exp (-a) - have hq : 0 ≤ q := exp_nonneg _ - let m := 2^M - let b := ∑ i ∈ Finset.range m, q^i - have heq : expU (fun x : Grid M => exp (-R*pos x)) = side M * b := by - have he (x : Grid M) : exp (-R*pos x) = q^x.val := by - unfold q pos a - rw [← Real.exp_nat_mul]; congr 1; ring - simp_rw [he,expU,smul_eq_mul] - rw [sum_grid (M:=M) (fun i => q^i)] - have h1 : b * (1-q) ≤ 1 := by - rw [show b*(1-q) = _ from geom_sum_mul_neg q m] - have hh : 0 ≤ q^m := pow_nonneg hq _ - linarith - have h2 : 1-q ≥ a/(1+a) := by - have H := Real.add_one_le_exp a - have HH : q*exp a = 1 := by unfold q; rw [← exp_add]; simp - apply (div_le_iff₀ (by linarith)).mpr; nlinarith - have hb : 0 ≤ b := Finset.sum_nonneg fun i _ => pow_nonneg hq _ - have hu : b*a ≤ 1+a := by - apply (div_le_one (by linarith)).mp - rw [mul_div_assoc] - exact (mul_le_mul_of_nonneg_left h2 hb).trans h1 - rw [heq,← mul_le_mul_iff_left₀ h] - unfold a at hu - field_simp - nlinarith - -lemma q_S_upper (R : ℝ) (h : 0 < R) : - q (S M R) 0 ≤ 6*(side M+1/R) := by - let f (x : Grid M) := exp (-R*pos x) - have H (x : Grid M) : - boltz (S M R) x ≤ 3*(f x + f (-x)) := by - let y := Dyadic.diam x - have H := mul_le_mul_of_nonneg_left (cost_diam x).1 (sq_nonneg R) - calc - _ ≤ exp (1 + -R*y) := by - unfold boltz - rw [form_S] - apply Real.exp_monotone - dsimp only [y] - nlinarith [sq_nonneg (4*R*Dyadic.diam x-1/4)] - _ = exp 1 * exp (-R*y) := exp_add _ _ - _ ≤ _ := by - apply mul_le_mul Real.exp_one_lt_three.le _ (Real.exp_nonneg _) (by norm_num : (0:ℝ) ≤ 3) - dsimp only [y]; rw [diam_min] - rcases le_total (pos x) (pos (-x)) with hh|hh - · rw [min_eq_left hh]; exact le_add_of_nonneg_right (exp_nonneg _) - rw [min_eq_right hh]; exact le_add_of_nonneg_left (exp_nonneg _) - rw [q_origin,expU_ex] - apply le_trans (Ex_mono _ H) - rw [Ex_lmul,Ex_add] - simp_rw [← expU_ex,expU_reflect] - have he : expU f ≤ side M+1/R := expU_ray R h - simp only [div_eq_mul_inv] at *; linarith - -lemma q_S_lower (R : ℝ) (h : 1 ≤ R) : - (1/30:ℝ)/R ≤ q (S M R) 0 := by - rw [q_origin] - let m : ℝ := 2^M - let r := (m/10)/R - let s := arc M r - have hR : 0 < R := by linarith - have hm : 0 < m := by unfold m; positivity - have hr : 0 ≤ r := by unfold r; positivity - have hsr : side M * m = 1 := side_card _ - have hsr' : side M * r = (1/10)/R := by unfold r; rw [← hsr]; ring - have h1 : r ≤ s.card := arc_card_lo _ _ hr (by - unfold r; apply (div_le_iff₀ hR).mpr; change m/10 ≤ m*R; nlinarith) - have h2 (i : Grid M) (hi : i∈s) : 1/3 ≤ boltz (S M R) i := by - have hy : 0 ≤ Dyadic.diam i := diam_nonneg i - have hh : Dyadic.diam i * R ≤ 1/10 := by - apply (le_div_iff₀ hR).mp - rw [← hsr'] - exact mul_le_mul_of_nonneg_left ((mem_arc _ _).mp hi) (side_pos _).le - have hj : form (S M R) i ≤ 1 := by - rw [form_S] - apply le_trans (mul_le_mul_of_nonneg_left (cost_diam i).2 (sq_nonneg _)) - have H : (Dyadic.diam i * R)^2 ≤ (1/10:ℝ)^2 := - pow_le_pow_left₀ (mul_nonneg hy hR.le) hh 2 - nlinarith - apply le_trans (b:=exp (-1)) - · rw [exp_neg,one_div]; exact (inv_le_inv₀ (by norm_num : (0:ℝ) < 3) (exp_pos 1)).mpr Real.exp_one_lt_three.le - exact Real.exp_monotone (by linarith) - unfold expU; rw [smul_eq_mul] - calc - _ = side M * r * (1/3) := by rw [hsr']; ring - _ ≤ side M * s.card * (1/3) := by have := side_pos M; gcongr - _ = side M * (∑ _i ∈ s, (1/3:ℝ)) := by simp; ring - _ ≤ side M * ∑ i ∈ s, boltz (S M R) i := - mul_le_mul_of_nonneg_left (Finset.sum_le_sum h2) (side_pos M).le - _ ≤ side M * ∑ i, boltz (S M R) i := - mul_le_mul_of_nonneg_left (Finset.sum_le_sum_of_subset_of_nonneg - (Finset.subset_univ _) (fun _ _ _ => (boltz_pos _ _).le)) (side_pos M).le - -lemma q_S_upper' (R : ℝ) (h : 0 < R) (hR : R ≤ 2^M) : - q (S M R) 0 ≤ 12/R := by - apply le_trans (q_S_upper R h) - have hh : side M ≤ 1/R := by rw [side_pow_two,one_div]; exact (inv_le_inv₀ (by positivity : (0:ℝ) < 2^M) h).mpr hR - simp only [div_eq_mul_inv] at *; linarith - -lemma F_S_lower (R : ℝ) (h : 0 < R) (hR : R ≤ 2^M) : - log R - 12 ≤ F (S M R) := by - have H := log_le_log (q_origin_pos _) (q_S_upper' R h hR) - rw [Real.log_div (by norm_num) h.ne'] at H - unfold F - linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<12 by norm_num)] - -lemma F_S_upper (M : ℕ) (R : ℝ) (h : 1 ≤ R) : - F (S M R) ≤ log R+30 := by - have hp : 0 < R := by linarith - have H := log_le_log (by positivity) (q_S_lower (M:=M) R h) - rw [Real.log_div (by norm_num) hp.ne',one_div,Real.log_inv] at H - unfold F - linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<30 by norm_num)] - -lemma S_energy (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : - (label A).Ex (form (S M R)) ≤ 100 := by - let B := S M R - apply le_trans (form_energy_mono (S_nonneg _ _) hA (S_nonneg _ _)) - let C := S M (R/2) - have He : C+(3/4:ℝ) • B = B := by - have HH := scale_S M (1/2) R - rw [show 1/2*R=R/2 by ring] at HH - unfold B C - rw [← HH,← add_smul,show (1/2:ℝ)^2+3/4=1 by norm_num,one_smul] - have H := mean_energy C ((3/4:ℝ) • B) - rw [He,show form ((3/4:ℝ) • B) = _ from funext (fun x => form_smul _ _ x),Ex_lmul] at H - have hP : 0 < R := by linarith - have h1 : F B ≤ log R + 30 := F_S_upper M R h - have h2 : log (R/2)-12 ≤ F C := F_S_lower _ (by positivity) (by linarith) - rw [log_div hP.ne' (by norm_num)] at h2 - change (label B).Ex (form B) ≤ 100 - linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num)] - -lemma S_cost (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : - (label A).Ex (cost 1) ≤ 100/R^2 := by - have H := S_energy R h hR A hA - rw [show form (S M R) = _ from funext (form_S R),Ex_lmul] at H - exact (le_div_iff₀ (sq_pos_of_pos (by linarith : 0 < R))).mpr (by rwa [mul_comm]) - -lemma S_variance (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : - (label A).Ex (fun i => (Dyadic.diam i)^2) ≤ 100/R^2 := by - apply le_trans _ (S_cost _ h hR A hA) - exact Ex_mono _ fun x => (le_mul_of_one_le_left (sq_nonneg _) (by norm_num : (1:ℝ)≤8)).trans (cost_diam x).1 - -lemma delta_S (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) - (s : Grid M) : delta A s ≤ rad s * (20/R) := by - have hp : 0 < R := by linarith - have h1 := S_cost R h hR A hA - rw [cred_cost] at h1 - have h2 := delta_sq A 1 - have h3 : delta A 1 ≤ 20/R := by - have h4 : delta A 1^2 ≤ (20/R)^2 := by - apply le_trans (b:=2*(100/R^2)); rw [h2]; linarith - rw [div_pow,← mul_div_assoc]; apply div_le_div_of_nonneg_right _ (sq_nonneg _); norm_num - nlinarith [show 0 < 20/R by positivity] - calc - _ ≤ rad s * delta A 1 := by simpa only [rad_abs,liftInt_cast] using delta_int A (liftInt s) - _ ≤ _ := mul_le_mul_of_nonneg_left h3 (rad_nonneg _) - -lemma cred_short (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) - (s : Grid M) (hs : rad s ≤ R/20) : - 1/2 ≤ cred A s := by - have hp : 0 < R := by linarith - have hi : delta A s ≤ 1 := calc - _ ≤ _ := delta_S _ h hR A hA s - _ ≤ (R/20) * (20/R) := by gcongr - _ = 1 := by field_simp - nlinarith [delta_sq A s,(show 0 ≤ delta A s from sqrt_nonneg _)] - -lemma q_short (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) - (s : Grid M) (hs : rad s ≤ R/20) : - (1/60:ℝ)/R ≤ q (S M R) s := by - let B := S M R - change _ ≤ q B s - have hh := cred_short R h hR B le_rfl s hs - rw [q_express,F_origin] - calc - (1/60:ℝ)/R = ((1/30)/R)*(1/2) := by ring - _ ≤ _ := mul_le_mul (q_S_lower R h) hh (by norm_num) (q_origin_pos _).le - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Order +import OAI.Analysis.FourierLLogL.Dyadic.Circle + +/-! # The spatial form and the discrete Gaussian bounds -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate Real +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Period Real + +variable {M : ℕ} + +lemma wave_at_one (x : Grid M) : + wave 1 x = cos (2*π * pos x) := by + have H := chi_cast (1:ℤ) x + unfold wave; norm_num [phase] at H + rw [H,Complex.exp_re] + simp + +lemma wave_at_diam (x : Grid M) : + wave 1 x = cos (2*π * Dyadic.diam x) := by + rw [diam_min] + rcases le_total (pos x) (pos (-x)) with h | h + · rw [min_eq_left h,wave_at_one] + rw [min_eq_right h,← wave_at_one,wave_even _ _] + +theorem cost_diam (x : Grid M) : + 8*(Dyadic.diam x)^2 ≤ cost 1 x ∧ cost 1 x ≤ 32*(Dyadic.diam x)^2 := by + let y := Dyadic.diam x + have hy := diam_nonneg x + have hY := diam_le_half x + unfold cost + rw [wave_at_diam] + have hp : 0 ≤ 2*π*y := mul_nonneg (by positivity) hy + have he : |2*π*y| ≤ π := by rw [abs_of_nonneg hp]; unfold y at *; nlinarith [pi_pos] + have H := cos_le_one_sub_mul_cos_sq he + have H' := @one_sub_sq_div_two_le_cos (2*π*y) + have hh : 2/π^2 * (2*π*y)^2 = 8*y^2 := by field_simp [pi_pos.ne']; ring + have hh' : (2*π*y)^2/2 ≤ 32*y^2 := by + calc + _ = (2*π^2)*y^2 := by ring + _ ≤ _ := by gcongr; nlinarith [pi_le_four,pi_pos] + rw [hh] at H + change 8*y^2 ≤ _ ∧ _ ≤ 32*y^2 + dsimp only [y] at * + constructor <;> linarith + +/-- Spatial form, specified by inverse width -/ +def S (M : ℕ) (R : ℝ) : Form M := fun s => if s=1 then R^2 else 0 + +lemma S_nonneg (M : ℕ) (R : ℝ) : 0 ≤ S M R := by + intro i + change 0 ≤ if i=1 then _ else _ + split_ifs <;> positivity + +lemma form_S (R : ℝ) (x : Grid M) : form (S M R) x = R^2*cost 1 x := by simp [form,S,ite_mul] + +lemma scale_S (M : ℕ) (t R : ℝ) : t^2 • S M R = S M (t*R) := by + funext i + change t^2*(if i=1 then _ else _) = if i=1 then _ else _ + split_ifs <;> ring + +lemma S_mono {a b : ℝ} (ha : 0 ≤ a) (h : a ≤ b) : S M a ≤ S M b := by + intro i; unfold S + split_ifs <;> nlinarith + +private lemma expU_ray (R : ℝ) (h : 0 < R) : + expU (fun x : Grid M => exp (-R * pos x)) ≤ side M + 1/R := by + let a := R*side M + have ha : 0 < a := mul_pos h (side_pos M) + let q := exp (-a) + have hq : 0 ≤ q := exp_nonneg _ + let m := 2^M + let b := ∑ i ∈ Finset.range m, q^i + have heq : expU (fun x : Grid M => exp (-R*pos x)) = side M * b := by + have he (x : Grid M) : exp (-R*pos x) = q^x.val := by + unfold q pos a + rw [← Real.exp_nat_mul]; congr 1; ring + simp_rw [he,expU,smul_eq_mul] + rw [sum_grid (M:=M) (fun i => q^i)] + have h1 : b * (1-q) ≤ 1 := by + rw [show b*(1-q) = _ from geom_sum_mul_neg q m] + have hh : 0 ≤ q^m := pow_nonneg hq _ + linarith + have h2 : 1-q ≥ a/(1+a) := by + have H := Real.add_one_le_exp a + have HH : q*exp a = 1 := by unfold q; rw [← exp_add]; simp + apply (div_le_iff₀ (by linarith)).mpr; nlinarith + have hb : 0 ≤ b := Finset.sum_nonneg fun i _ => pow_nonneg hq _ + have hu : b*a ≤ 1+a := by + apply (div_le_one (by linarith)).mp + rw [mul_div_assoc] + exact (mul_le_mul_of_nonneg_left h2 hb).trans h1 + rw [heq,← mul_le_mul_iff_left₀ h] + unfold a at hu + field_simp + nlinarith + +lemma q_S_upper (R : ℝ) (h : 0 < R) : + q (S M R) 0 ≤ 6*(side M+1/R) := by + let f (x : Grid M) := exp (-R*pos x) + have H (x : Grid M) : + boltz (S M R) x ≤ 3*(f x + f (-x)) := by + let y := Dyadic.diam x + have H := mul_le_mul_of_nonneg_left (cost_diam x).1 (sq_nonneg R) + calc + _ ≤ exp (1 + -R*y) := by + unfold boltz + rw [form_S] + apply Real.exp_monotone + dsimp only [y] + nlinarith [sq_nonneg (4*R*Dyadic.diam x-1/4)] + _ = exp 1 * exp (-R*y) := exp_add _ _ + _ ≤ _ := by + apply mul_le_mul Real.exp_one_lt_three.le _ (Real.exp_nonneg _) (by norm_num : (0:ℝ) ≤ 3) + dsimp only [y]; rw [diam_min] + rcases le_total (pos x) (pos (-x)) with hh|hh + · rw [min_eq_left hh]; exact le_add_of_nonneg_right (exp_nonneg _) + rw [min_eq_right hh]; exact le_add_of_nonneg_left (exp_nonneg _) + rw [q_origin,expU_ex] + apply le_trans (Ex_mono _ H) + rw [Ex_lmul,Ex_add] + simp_rw [← expU_ex,expU_reflect] + have he : expU f ≤ side M+1/R := expU_ray R h + simp only [div_eq_mul_inv] at *; linarith + +lemma q_S_lower (R : ℝ) (h : 1 ≤ R) : + (1/30:ℝ)/R ≤ q (S M R) 0 := by + rw [q_origin] + let m : ℝ := 2^M + let r := (m/10)/R + let s := arc M r + have hR : 0 < R := by linarith + have hm : 0 < m := by unfold m; positivity + have hr : 0 ≤ r := by unfold r; positivity + have hsr : side M * m = 1 := side_card _ + have hsr' : side M * r = (1/10)/R := by unfold r; rw [← hsr]; ring + have h1 : r ≤ s.card := arc_card_lo _ _ hr (by + unfold r; apply (div_le_iff₀ hR).mpr; change m/10 ≤ m*R; nlinarith) + have h2 (i : Grid M) (hi : i∈s) : 1/3 ≤ boltz (S M R) i := by + have hy : 0 ≤ Dyadic.diam i := diam_nonneg i + have hh : Dyadic.diam i * R ≤ 1/10 := by + apply (le_div_iff₀ hR).mp + rw [← hsr'] + exact mul_le_mul_of_nonneg_left ((mem_arc _ _).mp hi) (side_pos _).le + have hj : form (S M R) i ≤ 1 := by + rw [form_S] + apply le_trans (mul_le_mul_of_nonneg_left (cost_diam i).2 (sq_nonneg _)) + have H : (Dyadic.diam i * R)^2 ≤ (1/10:ℝ)^2 := + pow_le_pow_left₀ (mul_nonneg hy hR.le) hh 2 + nlinarith + apply le_trans (b:=exp (-1)) + · rw [exp_neg,one_div]; exact (inv_le_inv₀ (by norm_num : (0:ℝ) < 3) (exp_pos 1)).mpr Real.exp_one_lt_three.le + exact Real.exp_monotone (by linarith) + unfold expU; rw [smul_eq_mul] + calc + _ = side M * r * (1/3) := by rw [hsr']; ring + _ ≤ side M * s.card * (1/3) := by have := side_pos M; gcongr + _ = side M * (∑ _i ∈ s, (1/3:ℝ)) := by simp; ring + _ ≤ side M * ∑ i ∈ s, boltz (S M R) i := + mul_le_mul_of_nonneg_left (Finset.sum_le_sum h2) (side_pos M).le + _ ≤ side M * ∑ i, boltz (S M R) i := + mul_le_mul_of_nonneg_left (Finset.sum_le_sum_of_subset_of_nonneg + (Finset.subset_univ _) (fun _ _ _ => (boltz_pos _ _).le)) (side_pos M).le + +lemma q_S_upper' (R : ℝ) (h : 0 < R) (hR : R ≤ 2^M) : + q (S M R) 0 ≤ 12/R := by + apply le_trans (q_S_upper R h) + have hh : side M ≤ 1/R := by rw [side_pow_two,one_div]; exact (inv_le_inv₀ (by positivity : (0:ℝ) < 2^M) h).mpr hR + simp only [div_eq_mul_inv] at *; linarith + +lemma F_S_lower (R : ℝ) (h : 0 < R) (hR : R ≤ 2^M) : + log R - 12 ≤ F (S M R) := by + have H := log_le_log (q_origin_pos _) (q_S_upper' R h hR) + rw [Real.log_div (by norm_num) h.ne'] at H + unfold F + linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<12 by norm_num)] + +lemma F_S_upper (M : ℕ) (R : ℝ) (h : 1 ≤ R) : + F (S M R) ≤ log R+30 := by + have hp : 0 < R := by linarith + have H := log_le_log (by positivity) (q_S_lower (M:=M) R h) + rw [Real.log_div (by norm_num) hp.ne',one_div,Real.log_inv] at H + unfold F + linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<30 by norm_num)] + +lemma S_energy (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : + (label A).Ex (form (S M R)) ≤ 100 := by + let B := S M R + apply le_trans (form_energy_mono (S_nonneg _ _) hA (S_nonneg _ _)) + let C := S M (R/2) + have He : C+(3/4:ℝ) • B = B := by + have HH := scale_S M (1/2) R + rw [show 1/2*R=R/2 by ring] at HH + unfold B C + rw [← HH,← add_smul,show (1/2:ℝ)^2+3/4=1 by norm_num,one_smul] + have H := mean_energy C ((3/4:ℝ) • B) + rw [He,show form ((3/4:ℝ) • B) = _ from funext (fun x => form_smul _ _ x),Ex_lmul] at H + have hP : 0 < R := by linarith + have h1 : F B ≤ log R + 30 := F_S_upper M R h + have h2 : log (R/2)-12 ≤ F C := F_S_lower _ (by positivity) (by linarith) + rw [log_div hP.ne' (by norm_num)] at h2 + change (label B).Ex (form B) ≤ 100 + linarith [Real.log_le_sub_one_of_pos (show (0:ℝ)<2 by norm_num)] + +lemma S_cost (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : + (label A).Ex (cost 1) ≤ 100/R^2 := by + have H := S_energy R h hR A hA + rw [show form (S M R) = _ from funext (form_S R),Ex_lmul] at H + exact (le_div_iff₀ (sq_pos_of_pos (by linarith : 0 < R))).mpr (by rwa [mul_comm]) + +lemma S_variance (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) : + (label A).Ex (fun i => (Dyadic.diam i)^2) ≤ 100/R^2 := by + apply le_trans _ (S_cost _ h hR A hA) + exact Ex_mono _ fun x => (le_mul_of_one_le_left (sq_nonneg _) (by norm_num : (1:ℝ)≤8)).trans (cost_diam x).1 + +lemma delta_S (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) + (s : Grid M) : delta A s ≤ rad s * (20/R) := by + have hp : 0 < R := by linarith + have h1 := S_cost R h hR A hA + rw [cred_cost] at h1 + have h2 := delta_sq A 1 + have h3 : delta A 1 ≤ 20/R := by + have h4 : delta A 1^2 ≤ (20/R)^2 := by + apply le_trans (b:=2*(100/R^2)); rw [h2]; linarith + rw [div_pow,← mul_div_assoc]; apply div_le_div_of_nonneg_right _ (sq_nonneg _); norm_num + nlinarith [show 0 < 20/R by positivity] + calc + _ ≤ rad s * delta A 1 := by simpa only [rad_abs,liftInt_cast] using delta_int A (liftInt s) + _ ≤ _ := mul_le_mul_of_nonneg_left h3 (rad_nonneg _) + +lemma cred_short (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) (A : Form M) (hA : S M R ≤ A) + (s : Grid M) (hs : rad s ≤ R/20) : + 1/2 ≤ cred A s := by + have hp : 0 < R := by linarith + have hi : delta A s ≤ 1 := calc + _ ≤ _ := delta_S _ h hR A hA s + _ ≤ (R/20) * (20/R) := by gcongr + _ = 1 := by field_simp + nlinarith [delta_sq A s,(show 0 ≤ delta A s from sqrt_nonneg _)] + +lemma q_short (R : ℝ) (h : 1 ≤ R) (hR : R ≤ 2^M) + (s : Grid M) (hs : rad s ≤ R/20) : + (1/60:ℝ)/R ≤ q (S M R) s := by + let B := S M R + change _ ≤ q B s + have hh := cred_short R h hR B le_rfl s hs + rw [q_express,F_origin] + calc + (1/60:ℝ)/R = ((1/30)/R)*(1/2) := by ring + _ ≤ _ := mul_le_mul (q_S_lower R h) hh (by norm_num) (q_origin_pos _).le + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Stretch.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Stretch.lean index 63a271a71e1924da60f7f693135e4a6421c7e774..62e0b6b579241825b2237be7e17b0d8af48c1798 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Stretch.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Stretch.lean @@ -1,170 +1,170 @@ -import OAI.Analysis.FourierLLogL.Thermal.Packing -import OAI.Analysis.FourierLLogL.Thermal.Obtain - -/-! # The packing consequence for weighted update choices -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M l : ℕ} -open scoped Classical - -private lemma most_delta {s : Finset (Grid l)} (hh : s.Nonempty) (P : Law (Grid l)) - (he : evenly s P) (hl : l ≤ M) (A : Form M) (x : Grid l) (r : ℝ) (hr : 0 < r) - (H : 4*(label A).Ex (form (averageCost hl P x)) ≤ r^2) : - ∃ u : Finset (Grid l), u⊆s ∧ s.card ≤ 2*u.card ∧ ∀ y∈u, delta A (raise hl (y-x)) ≤ r := by - classical - let f := fun y : Grid l => delta A (raise hl (y-x)) - have hH : 2*P.Ex (fun i => f i^2) ≤ r^2 := by - have he (i : Grid l) : f i^2 = (label A).Ex (fun y => 2*cost (raise hl (i-x)) y) := by - rw [Ex_lmul,cred_cost]; exact delta_sq A _ - rw [funext (average_form hl P x)] at H - rw [Ex_swap (label A) P] at H - simp_rw [he,Ex_lmul] - linarith - let u := s.filter (fun i => f i ≤ r) - refine ⟨u,Finset.filter_subset _ _,?_,fun y hy => (Finset.mem_filter.mp hy).2⟩ - have hs : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.mpr hh - have HE : ((s\u).card:ℝ)/s.card*r^2 ≤ P.Ex (fun i => f i^2) := calc - _ = ∑ i∈(s\u), (s.card:ℝ)⁻¹*r^2 := by simp; ring - _ ≤ ∑ i∈(s\u), P i*f i^2 := Finset.sum_le_sum fun i hi => by - obtain ⟨h1,h2⟩ := Finset.mem_sdiff.mp hi - rw [he i h1] - have hi' : r < f i := lt_of_not_ge (fun hh => h2 (Finset.mem_filter.mpr ⟨h1,hh⟩)) - gcongr - _ ≤ _ := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) - fun i _ _ => mul_nonneg (P.nonneg _) (sq_nonneg _) - have hu : (2:ℝ)*(s\u).card ≤ s.card := by - field_simp at HE - by_contra! ht - have hQ : 0 < r^2 := pow_pos hr _ - nlinarith - have h1 : 2*(s\u).card ≤ s.card := by exact_mod_cast hu - rw [Finset.card_sdiff_of_subset (Finset.filter_subset _ _)] at h1 - unfold u; omega - -def stepRad (C : ℝ) (j : ℕ) := sepRad j/(10*C*H j) -def track (C : ℝ) (j n : ℕ) := sepRad j/4 + n*stepRad C j - -lemma spacing (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, - let η := stepRad C j - 0 < sig j ∧ sig j ≤ 1/4 ∧ 4*sig j ≤ η ∧ - 0 < sepRad j ∧ sepRad j ≤ 1 ∧ - 4*(400*reserve C/H j^200) ≤ (η/2)^2 ∧ - 200-log (sig j) ≤ height j/H j^2 := by - filter_upwards [h_large (10^8*reserve C),hpoly 1000 3] with j h h' - intro eta; let a := H j; let s := sig j; let r := sepRad j; let D := reserve C - have hp : 0 < a := H_pos j; have hh : 1 ≤ a := H_ge j - have hD : 1 ≤ D := reserve_ge hC - change 10^8*D ≤ a at h - have he : s=1/a^40 := rfl - have hr : r=1/a^30 := rfl - have hi : eta=1/(10*C*a^31) := by - unfold eta stepRad; change (1/a^30)/(10*C*a) = _ - ring - have hC' : 0 < C := by linarith - have hl : C ≤ D := by unfold D reserve; linarith - have hx : 100*C ≤ a := by nlinarith - have h_a (n : ℕ) (ht : 1 ≤ n) : a ≤ a^n := by - simpa using pow_le_pow_right₀ hh ht - change 0 < s ∧ s ≤ 1/4 ∧ 4*s ≤ eta ∧ - 0 < r ∧ r ≤ 1 ∧ 4*(400*D/a^200) ≤ (eta/2)^2 ∧ 200-log s ≤ _/a^2 - rw [he, hr,hi] - refine ⟨by positivity, ?_, ?_,by positivity,div_le_self (by norm_num) (one_le_pow₀ hh), ?_, ?_⟩ - · apply one_div_le_one_div_of_le (by norm_num) _ - linarith [h_a 40 (by omega)] - · have ht : 40*C ≤ a^9 := by linarith [h_a 9 (by omega)] - rw [show a^40=a^9*a^31 by ring] - rw [← mul_div_assoc,mul_one] - apply (div_le_div_iff₀ (by positivity) (by positivity)).mpr - have hh := mul_le_mul_of_nonneg_right ht (by positivity : 0 ≤ a^31) - nlinarith - · have ht : 10000*D ≤ a := by linarith - have hZ : 640000*C^2*D ≤ a^138 := by - apply le_trans (b:=a^3) _ (pow_le_pow_right₀ hh (by omega)) - calc - _ ≤ 640000*(a/100)^2*(a/10000) := by gcongr <;> linarith - _ ≤ _ := by nlinarith [show 0 ≤ a^3 by positivity] - rw [show a^200 = a^138*(a^31)^2 by ring] - field_simp - nlinarith - rw [log_div (by norm_num) (by positivity),log_one,log_pow] - apply (le_div_iff₀ (by positivity : 0 < a^2)).mpr - have he := log_le_sub_one_of_pos hp - change 1000*a^3 ≤ _ at h' - push_cast - nlinarith - -lemma r_steps {C : ℝ} (hC : 1 ≤ C) (j n : ℕ) (h : (n:ℝ) ≤ C*H j) : - 0 ≤ track C j n ∧ track C j n ≤ sepRad j / 2 := by - have hp : 0 < H j := H_pos j - have he : 0 ≤ sepRad j := by change 0 ≤ 1/H j^30; positivity - have hC' : 0 < C := by linarith - unfold track stepRad - refine ⟨by positivity,?_⟩ - calc - _ ≤ sepRad j/4 + (C*H j)*(sepRad j/(10*C*H j)) := by gcongr - _ = sepRad j/4 + sepRad j/10 := by field_simp - _ ≤ _ := by linarith - -lemma step_gap (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ (M l : ℕ) (hl : l ≤ M) (p : Node), p.ok M → - ∀ (A V : Form M), 0 ≤ A → A+place p M ≤ V → - ∀ (x : Grid l) (P : Law (Grid l)) (mult t r : ℝ), - height j/H j^2 ≤ t → t ≤ reserve C*height j → - height j*H j^200 ≤ mult → (label V).Ex (form (averageCost hl P x)) ≤ 400*t/mult → - F V-F (A+place p M) ≤ 400*t → 0 ≤ r → r ≤ sepRad j/2 → - gap V (r+stepRad C j) - gap A r ≤ 402*t ∧ - (∀ (s : Finset (Grid l)) (k : ℝ), - s.Nonempty → separated (raise hl) A (sepRad j) (sig j*p.invlen/2) s → - evenly s P → 4*k/5 ≤ log (s.card:ℝ) → t = eps*k → - gap V (r+stepRad C j) -gap A r ≤ -(k/2)) := by - classical - filter_upwards [spacing C hC] with j h - intro M l hl p hp A V hA hV x P mult t r ht hD hF hG hE hr hr' - obtain ⟨hs,hs',he,hh,hh',hq,hS⟩ := h - have hR : 2*r ≤ 1 := by linarith - have hη : 0 < stepRad C j := by linarith - have hI := H_pos j; have hI' := d_pos j - have hT : 0 < t := lt_of_lt_of_le (by positivity) ht - constructor - · let f := fun _ : Grid l => (0:Grid M) - have hw := gap_packing hp hr hR hs hs' he hA hV f {x} ⟨_,Finset.mem_singleton_self x⟩ - (by intro i hi j hj hn; simp_all) - (fun i hi => by unfold f; rw [delta_zero]; linarith) - simp only [Finset.card_singleton,Nat.cast_one,log_one,sub_zero] at hw - linarith - intro s k h1 h2 h3 h4 h5 - obtain ⟨u,hu,hcard,hdelta⟩ := - most_delta h1 P h3 hl V x (stepRad C j/2) (by linarith) (by - apply le_trans _ hq - have hz : 0 < height j* H j^200 := by positivity - have hY : 400*t/mult ≤ 400*reserve C/H j^200 := calc - _ ≤ 400*(reserve C*height j)/(height j*H j^200) := by have H := lt_of_lt_of_le zero_lt_one (reserve_ge hC); gcongr - _ = _ := by field_simp - linarith) - have hn : u.Nonempty := Finset.card_pos.mp (by have HH := Finset.card_pos.mpr h1; omega) - let f := fun i : Grid l => raise hl (i-x) - have hi : separated f A (2*r) (sig j*p.invlen/2) u := by - intro i hi b hb H H' - obtain ⟨v,hv,hR⟩ := H' - apply h2 _ (hu hi) _ (hu hb) H - use v - refine ⟨by linarith,?_⟩ - unfold f at hR; simp only [map_sub, sub_sub_sub_cancel_right] at hR; exact hR - have hw := gap_packing hp hr hR hs hs' he hA hV f u hn hi hdelta - have hu0 : (0:ℝ) delta A (raise hl (y-x)) + have hH : 2*P.Ex (fun i => f i^2) ≤ r^2 := by + have he (i : Grid l) : f i^2 = (label A).Ex (fun y => 2*cost (raise hl (i-x)) y) := by + rw [Ex_lmul,cred_cost]; exact delta_sq A _ + rw [funext (average_form hl P x)] at H + rw [Ex_swap (label A) P] at H + simp_rw [he,Ex_lmul] + linarith + let u := s.filter (fun i => f i ≤ r) + refine ⟨u,Finset.filter_subset _ _,?_,fun y hy => (Finset.mem_filter.mp hy).2⟩ + have hs : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.mpr hh + have HE : ((s\u).card:ℝ)/s.card*r^2 ≤ P.Ex (fun i => f i^2) := calc + _ = ∑ i∈(s\u), (s.card:ℝ)⁻¹*r^2 := by simp; ring + _ ≤ ∑ i∈(s\u), P i*f i^2 := Finset.sum_le_sum fun i hi => by + obtain ⟨h1,h2⟩ := Finset.mem_sdiff.mp hi + rw [he i h1] + have hi' : r < f i := lt_of_not_ge (fun hh => h2 (Finset.mem_filter.mpr ⟨h1,hh⟩)) + gcongr + _ ≤ _ := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) + fun i _ _ => mul_nonneg (P.nonneg _) (sq_nonneg _) + have hu : (2:ℝ)*(s\u).card ≤ s.card := by + field_simp at HE + by_contra! ht + have hQ : 0 < r^2 := pow_pos hr _ + nlinarith + have h1 : 2*(s\u).card ≤ s.card := by exact_mod_cast hu + rw [Finset.card_sdiff_of_subset (Finset.filter_subset _ _)] at h1 + unfold u; omega + +def stepRad (C : ℝ) (j : ℕ) := sepRad j/(10*C*H j) +def track (C : ℝ) (j n : ℕ) := sepRad j/4 + n*stepRad C j + +lemma spacing (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, + let η := stepRad C j + 0 < sig j ∧ sig j ≤ 1/4 ∧ 4*sig j ≤ η ∧ + 0 < sepRad j ∧ sepRad j ≤ 1 ∧ + 4*(400*reserve C/H j^200) ≤ (η/2)^2 ∧ + 200-log (sig j) ≤ height j/H j^2 := by + filter_upwards [h_large (10^8*reserve C),hpoly 1000 3] with j h h' + intro eta; let a := H j; let s := sig j; let r := sepRad j; let D := reserve C + have hp : 0 < a := H_pos j; have hh : 1 ≤ a := H_ge j + have hD : 1 ≤ D := reserve_ge hC + change 10^8*D ≤ a at h + have he : s=1/a^40 := rfl + have hr : r=1/a^30 := rfl + have hi : eta=1/(10*C*a^31) := by + unfold eta stepRad; change (1/a^30)/(10*C*a) = _ + ring + have hC' : 0 < C := by linarith + have hl : C ≤ D := by unfold D reserve; linarith + have hx : 100*C ≤ a := by nlinarith + have h_a (n : ℕ) (ht : 1 ≤ n) : a ≤ a^n := by + simpa using pow_le_pow_right₀ hh ht + change 0 < s ∧ s ≤ 1/4 ∧ 4*s ≤ eta ∧ + 0 < r ∧ r ≤ 1 ∧ 4*(400*D/a^200) ≤ (eta/2)^2 ∧ 200-log s ≤ _/a^2 + rw [he, hr,hi] + refine ⟨by positivity, ?_, ?_,by positivity,div_le_self (by norm_num) (one_le_pow₀ hh), ?_, ?_⟩ + · apply one_div_le_one_div_of_le (by norm_num) _ + linarith [h_a 40 (by omega)] + · have ht : 40*C ≤ a^9 := by linarith [h_a 9 (by omega)] + rw [show a^40=a^9*a^31 by ring] + rw [← mul_div_assoc,mul_one] + apply (div_le_div_iff₀ (by positivity) (by positivity)).mpr + have hh := mul_le_mul_of_nonneg_right ht (by positivity : 0 ≤ a^31) + nlinarith + · have ht : 10000*D ≤ a := by linarith + have hZ : 640000*C^2*D ≤ a^138 := by + apply le_trans (b:=a^3) _ (pow_le_pow_right₀ hh (by omega)) + calc + _ ≤ 640000*(a/100)^2*(a/10000) := by gcongr <;> linarith + _ ≤ _ := by nlinarith [show 0 ≤ a^3 by positivity] + rw [show a^200 = a^138*(a^31)^2 by ring] + field_simp + nlinarith + rw [log_div (by norm_num) (by positivity),log_one,log_pow] + apply (le_div_iff₀ (by positivity : 0 < a^2)).mpr + have he := log_le_sub_one_of_pos hp + change 1000*a^3 ≤ _ at h' + push_cast + nlinarith + +lemma r_steps {C : ℝ} (hC : 1 ≤ C) (j n : ℕ) (h : (n:ℝ) ≤ C*H j) : + 0 ≤ track C j n ∧ track C j n ≤ sepRad j / 2 := by + have hp : 0 < H j := H_pos j + have he : 0 ≤ sepRad j := by change 0 ≤ 1/H j^30; positivity + have hC' : 0 < C := by linarith + unfold track stepRad + refine ⟨by positivity,?_⟩ + calc + _ ≤ sepRad j/4 + (C*H j)*(sepRad j/(10*C*H j)) := by gcongr + _ = sepRad j/4 + sepRad j/10 := by field_simp + _ ≤ _ := by linarith + +lemma step_gap (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ (M l : ℕ) (hl : l ≤ M) (p : Node), p.ok M → + ∀ (A V : Form M), 0 ≤ A → A+place p M ≤ V → + ∀ (x : Grid l) (P : Law (Grid l)) (mult t r : ℝ), + height j/H j^2 ≤ t → t ≤ reserve C*height j → + height j*H j^200 ≤ mult → (label V).Ex (form (averageCost hl P x)) ≤ 400*t/mult → + F V-F (A+place p M) ≤ 400*t → 0 ≤ r → r ≤ sepRad j/2 → + gap V (r+stepRad C j) - gap A r ≤ 402*t ∧ + (∀ (s : Finset (Grid l)) (k : ℝ), + s.Nonempty → separated (raise hl) A (sepRad j) (sig j*p.invlen/2) s → + evenly s P → 4*k/5 ≤ log (s.card:ℝ) → t = eps*k → + gap V (r+stepRad C j) -gap A r ≤ -(k/2)) := by + classical + filter_upwards [spacing C hC] with j h + intro M l hl p hp A V hA hV x P mult t r ht hD hF hG hE hr hr' + obtain ⟨hs,hs',he,hh,hh',hq,hS⟩ := h + have hR : 2*r ≤ 1 := by linarith + have hη : 0 < stepRad C j := by linarith + have hI := H_pos j; have hI' := d_pos j + have hT : 0 < t := lt_of_lt_of_le (by positivity) ht + constructor + · let f := fun _ : Grid l => (0:Grid M) + have hw := gap_packing hp hr hR hs hs' he hA hV f {x} ⟨_,Finset.mem_singleton_self x⟩ + (by intro i hi j hj hn; simp_all) + (fun i hi => by unfold f; rw [delta_zero]; linarith) + simp only [Finset.card_singleton,Nat.cast_one,log_one,sub_zero] at hw + linarith + intro s k h1 h2 h3 h4 h5 + obtain ⟨u,hu,hcard,hdelta⟩ := + most_delta h1 P h3 hl V x (stepRad C j/2) (by linarith) (by + apply le_trans _ hq + have hz : 0 < height j* H j^200 := by positivity + have hY : 400*t/mult ≤ 400*reserve C/H j^200 := calc + _ ≤ 400*(reserve C*height j)/(height j*H j^200) := by have H := lt_of_lt_of_le zero_lt_one (reserve_ge hC); gcongr + _ = _ := by field_simp + linarith) + have hn : u.Nonempty := Finset.card_pos.mp (by have HH := Finset.card_pos.mpr h1; omega) + let f := fun i : Grid l => raise hl (i-x) + have hi : separated f A (2*r) (sig j*p.invlen/2) u := by + intro i hi b hb H H' + obtain ⟨v,hv,hR⟩ := H' + apply h2 _ (hu hi) _ (hu hb) H + use v + refine ⟨by linarith,?_⟩ + unfold f at hR; simp only [map_sub, sub_sub_sub_cancel_right] at hR; exact hR + have hw := gap_packing hp hr hR hs hs' he hA hV f u hn hi hdelta + have hu0 : (0:ℝ) v (z-t)) = (uniform _).prob v := by - unfold prob - simp_rw [← expU_ex, show ∀ z, z-t = -t+z from fun z => by ring] - rw [expU_translate (fun z => if v z then (1:ℝ) else 0)] - -/-- label cutoff by reference radius -/ -def dome (p : Node) (M : ℕ) (s : ℝ) (z : Grid M) := - Dyadic.diam (z-start p M) ≤ s / p.invlen - -lemma dome_prob (p : Node) (hp : p.ok M) (s : ℝ) (hs : 1 ≤ s) : - (uniform _).prob (dome p M s) ≤ 4*s/p.invlen := by - have hr := p.invlen_pos - have he : (uniform _).prob (dome p M s) ≤ 2*(s/p.invlen + side M) := by - unfold dome - rw [prob_translate (fun z => Dyadic.diam z ≤ s/p.invlen)] - exact arc_length _ _ (by apply div_nonneg <;> linarith) - have hi : side M ≤ 1/p.invlen := by - rw [side_pow_two,one_div] - exact (inv_le_inv₀ (by positivity) hr).mpr (loc_scale hp.2) - apply le_trans he - have hx := (le_div_iff₀ hr).mp hi - field_simp; linarith - -lemma prob_as_ex {α : Type} [Fintype α] (P : Law α) (p : α → Prop) [DecidablePred p] : - P.prob p = P.Ex (fun x => if p x then 1 else 0) := by - rw [prob_eq]; unfold Ex; simp - -lemma ch_dome_bound {p p' : Node} (hv : p.below p') {n : ℕ} {h err : ℝ} {A : Form M} - (H : Certified h err n p' A) (x : Grid M) (hx : x∈p.block M) (s : ℝ) (hs : 0 < s) : - (channel A x).prob (fun z => ¬dome p' M s z) ≤ 264/(8*s^2) := by - let C := channel A x - - - have hi : C.Ex (fun z => (8*s^2)*(if ¬dome p' M s z then 1 else 0)) ≤ 264 := - le_trans (C.Ex_mono (g:=fun z => form (place p' M) (z-p'.start M)) fun z => by - unfold dome; split_ifs with Hh - · rw [mul_zero] - exact form_nonneg (place_valid _ _) _ - · have hp := p'.invlen_pos - rw [not_le] at Hh - rw [mul_one,place,form_S] - apply le_trans (b:=p'.invlen^2*(8*(Dyadic.diam (z-start p' M))^2)) - · rw [div_lt_iff₀ hp] at Hh - have hh := pow_le_pow_left₀ hs.le Hh.le 2 - nlinarith - · exact mul_le_mul_of_nonneg_left (cost_diam _).1 (sq_nonneg _)) (local_channel_energy H x (block_of_below hv _ hx)) - rw [Ex_lmul,mul_comm] at hi - apply (le_div_iff₀ (by positivity : 0 < 8*s^2)).mpr - have HH : C.prob (fun x => ¬ dome p' M s x) = - C.Ex (fun z => if ¬dome p' M s z then 1 else 0) := by - refine @prob_as_ex _ _ _ _ (fun z => instDecidableNot) - rw [show (channel A x) = C from rfl,HH]; exact hi - -/-- factorization for the two center-shifted weights used in the unconditional cap -/ -private lemma two_envelope {A B C : Form M} (hA : 0 ≤ A) (hB : 0 ≤ B) (hh : A+B ≤ C) - (i j s : Grid M) : - ‖expU (fun y => (↑(boltz A (i-y)*boltz B (j-y)):ℂ)*chi s y)‖ ≤ exp (-F (A+B))*cred C s := by - let f := fun b : Bool => if b then B else A - let v := fun b : Bool => if b then j else i - have H := envelope Finset.univ f (fun i _ => by cases i; exact hA; exact hB) v (-s) - simp only [Fintype.prod_bool,Fintype.sum_bool,f,v,Bool.false_eq_true,↓reduceIte,q_even _ _] at H - rw [add_comm] at H - have HE := q_compare (add_nonneg hA hB) (le_refl (A+B)) hh s - apply le_trans ?_ HE - rw [expU_coeff (fun y => (↑(boltz A (i-y)*boltz B (j-y)):ℂ)) s] - apply le_trans _ H - push_cast; simp_rw [mul_comm ((boltz A _):ℂ)] - exact le_rfl - -lemma moments_bias {p : Node} {P : Law (Grid M)} (hl : l ≤ M) - {B : Bool} {A : Form M} {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} - {β : ℝ} (hG : biases P p A hl s B v β) (Q : Law (Grid l)) (hQ : ∀ x, 0 < Q x → x∈s) : - let U := P.withNext (channel A) - β ≤ U.Ex (fun xz => if xz.1∈(p.child B).block M then - (ply hl Q (fun i => v i xz.2) xz.1).re else 0) := by - intro U - let D (i : Grid l) (x : Grid M × Grid M) := - if x.1∈(p.child B).block M then (v i x.2*chi (raise hl i) x.1).re else 0 - have H (i : Grid l) (hi : 0 < Q i) : β ≤ U.Ex (D i) := by - unfold U D; rw [conditional_if_ex P (channel A) (fun x => x∈(p.child B).block M) - (fun x z => (v i z*chi (raise hl i) x).re)] - exact hG.2 _ (hQ _ hi) - calc - _ = Q.Ex (fun _ => β) := (Q.Ex_const _).symm - _ ≤ Q.Ex (fun i => U.Ex (D i)) := Q.Ex_mono_mem H - _ = _ := by - rw [Ex_swap Q U] - refine congrArg U.Ex (funext fun x => ?_) - unfold D - split_ifs with hh - · rw [ply,re_Cx] - rw [Ex_const] - -private lemma ex_pow_bound {α : Type} [Fintype α] - (P : Law α) (f : α → ℝ) (hf : ∀ x, 0 ≤ f x) (T : ℕ) : - P.Ex f ^ T ≤ P.Ex (fun i => f i ^ T) := by - have H := (convexOn_pow (𝕜:=ℝ) T).map_sum_le (t:=Finset.univ) - (p:=f) (w:=fun i => P i) (fun x _ => P.nonneg x) P.total (fun i _ => hf i) - exact H - -lemma uniform_level_cost {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') - {n : ℕ} {h err : ℝ} {A A' : Form M} (H : Certified h err n p' A) - (HH : A+place p M ≤ A') - {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) {s : Finset (Grid l)} - {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) {B : Bool} - {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 < β) (hb : β ≤ 1) - (HG : biases P p A hl s B v β) (Q : Law (Grid l)) (hQ : ∀ x, 0 < Q x → x∈s) (T : ℕ) - (hT : 0 < T) : - levelCost hl A' Q T ≤ 2*d + F A - log p'.invlen + 5000 - log β + - 2*T*(1-log β) := by - classical - let R := p.invlen - let R' := p'.invlen - let E := levelCost hl A' Q T - let K := place p M - let fp := F (A+K) - have hpR : 0 < R := p.invlen_pos - have hpR' : 0 < R' := p'.invlen_pos - let L := channel A - let U := P.withNext L - let a0 := 1000/β - have ha : 1 ≤ a0 := (le_div_iff₀ h0).mpr (by linarith) - let D := dome p' M a0 - have hD : U.prob (fun xz => ¬ D xz.2) ≤ β/2 := by - unfold prob; dsimp only [U]; rw [Ex_withNext] - rw [← Ex_const P (β/2)] - refine Ex_mono_mem _ fun x hx => ?_ - have hH := ch_dome_bound hv H x (piece_support hp ht x hx) a0 (by linarith) - apply le_trans (show (L x).prob _ ≤ _ from hH) - unfold a0; field_simp; nlinarith - let W (x z : Grid M) := ply hl Q (fun i => v i z) x - have HW (x z : Grid M) : ‖W x z‖≤1 := ply_unit _ _ _ (fun i => HG.1 i z) _ - let f (xz : Grid M × Grid M) := if D xz.2 then ‖W xz.1 xz.2‖ else 0 - have hf (xz) : 0 ≤ f xz := by unfold f; split_ifs <;> positivity - have he : β/2 ≤ U.Ex f := by - let V := fun xz : Grid M × Grid M => if ¬ D xz.2 then (1 : ℝ) else 0 - have hG' : β ≤ U.Ex fun xz => f xz+V xz := by - apply le_trans (moments_bias hl HG Q hQ) - refine Ex_mono _ fun xz => ?_ - unfold f V - change (if _ then (W _ _).re else _) ≤ _ - have hh := HW xz.1 xz.2 - have hh' := Complex.abs_re_le_norm (W xz.1 xz.2) - by_cases hh0 : D xz.2 <;> simp only [hh0,ite_true, ite_false,not_false_eq_true, not_true,add_zero,zero_add] <;> - split_ifs <;> first | positivity | linarith [(abs_le.mp hh').2] - have hD' : U.Ex V ≤ β/2 := by - rw [@prob_as_ex _ _ _ _ (fun z => instDecidableNot)] at hD - exact hD - rw [Ex_add] at hG'; linarith - let N := 2*T - have he1 : (β/2)^N ≤ U.Ex (fun i => f i ^ N) := - (pow_le_pow_left₀ (by positivity) he N).trans (ex_pow_bound _ _ hf _) - let V (z y : Grid M) := boltz A (z-y)*boltz K (p.start M-y) - have hV (z) : expU (fun y => V z y * ‖W y z‖^N) ≤ exp (-fp) * exp (-E) := by - unfold E - rw [heat_exp] - exact weighted_envelope T hl A' Q _ (fun x => HG.1 x z) _ _ - (fun s => two_envelope H.nonneg (place_valid _ _) HH _ _ _) - let c := R*exp (2*d+F A+32) - have hc : 0 ≤ c := by unfold c; positivity - let b := side M - have hbb : 0 < b := side_pos _ - have hpoint (x z : Grid M) : U (x,z) ≤ b*c*b*V z x := by - have Hx := piece_density ht h1 hm x - rw [dice_apply hp] at Hx - have h1 : 0 < V z x := mul_pos (boltz_pos _ _) (boltz_pos _ _) - by_cases h : x∈p.block M - · rw [ite_eq_left h,← rinv] at Hx - have HE := place_internal (start_mem hp.1 hp.2) h - have hi : 1 ≤ exp 32 * boltz K (p.start M-x) := by - unfold boltz; rw [← exp_add] - apply Real.one_le_exp _; unfold K; linarith - calc - _ = P x * (b*(exp (F A)*boltz A (z-x))) := by - unfold U L b - rw [withNext_apply,ch_K,K_eq,boltz,← exp_add]; congr 4 - _ ≤ (exp (2*d)*(b*R)) * (b*(exp (F A)*boltz A (z-x))) := - mul_le_mul_of_nonneg_right Hx (by have := boltz_pos A (z-x); positivity) - _ ≤ (exp (2*d)*(b*R)) * (b*(exp (F A)*boltz A (z-x))) * - (exp 32*boltz K (p.start M-x)) := - le_mul_of_one_le_right (by have := boltz_pos A (z-x); positivity) hi - _ = _ := by unfold c V; rw [exp_add,exp_add]; ring - · rw [ite_eq_right h,mul_zero] at Hx - unfold U; rw [withNext_apply,Hx.antisymm (P.nonneg _),zero_mul] - positivity - have HUB (z : Grid M) : ∑ x : Grid M, U (x,z)*f (x,z)^N ≤ - (if D z then b else 0) * (c*(exp (-fp)*exp (-E))) := by - split_ifs with HHz - · calc - _ ≤ ∑ x : Grid M, b*c*b*V z x*‖W x z‖^N := Finset.sum_le_sum fun x _ => by - simp only [f,HHz,ite_true] - exact mul_le_mul_of_nonneg_right (hpoint x z) (by positivity) - _ = b*c*expU (fun x => V z x*‖W x z‖^N) := by - unfold b expU; simp only [smul_eq_mul,Finset.mul_sum,mul_assoc] - _ ≤ _ := by - rw [← mul_assoc b c]; exact mul_le_mul_of_nonneg_left (hV z) (by positivity) - · have HH : N ≠ 0 := by unfold N; omega - simp [f,HHz,HH] - have HM : - (β/2)^N ≤ 4*a0/R' * (c*(exp (-fp)*exp (-E))) := calc - _ ≤ U.Ex (fun i => f i ^ N) := he1 - _ = ∑ z, ∑ x : Grid M, U (x,z)*f (x,z)^N := by unfold Ex; rw [Fintype.sum_prod_type,Finset.sum_comm] - _ ≤ ∑ z, (if D z then b else 0) * (c*(exp (-fp)*exp (-E))) := Finset.sum_le_sum (fun z _ => HUB z) - _ = (uniform _).prob D * (c*(exp (-fp)*exp (-E))) := by - rw [prob_eq]; simp_rw [uniform_side,← Finset.sum_mul]; rfl - _ ≤ _ := mul_le_mul_of_nonneg_right (dome_prob p' H.fits a0 ha) (by positivity) - have hrel : log R - 12 ≤ fp := (F_S_lower _ p.invlen_pos (loc_scale hp.2)).trans - (F_mono (le_add_of_nonneg_left H.nonneg)) - have hlog := log_le_log (by positivity) HM - unfold a0 c N at hlog - rw [show 4*(1000/β)=(4000:ℝ)/β by ring] at hlog - have hc' : 0 < R * exp (2*d + F A + 32) := by positivity - rw [log_pow,Real.log_div h0.ne' (by norm_num)] at hlog - rw [Real.log_mul (by positivity) (by positivity),Real.log_div (by positivity) hpR'.ne', - Real.log_div (by norm_num) h0.ne', Real.log_mul hc'.ne' (by positivity), - Real.log_mul hpR.ne' (by positivity),Real.log_mul (by positivity) (by positivity)] at hlog - push_cast at hlog - simp_rw [log_exp] at hlog - change E ≤ 2*d + F A - log R' + 5000-log β+_ - - nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4000),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), - Nat.cast_nonneg (α:=ℝ) T] - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Power +import OAI.Analysis.FourierLLogL.Thermal.Product + +/-! # Bounding a characteristic-function power -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period + +variable {M l : ℕ} +open scoped Classical + +lemma prob_translate (v : Grid M → Prop) (t : Grid M) : + (uniform _).prob (fun z => v (z-t)) = (uniform _).prob v := by + unfold prob + simp_rw [← expU_ex, show ∀ z, z-t = -t+z from fun z => by ring] + rw [expU_translate (fun z => if v z then (1:ℝ) else 0)] + +/-- label cutoff by reference radius -/ +def dome (p : Node) (M : ℕ) (s : ℝ) (z : Grid M) := + Dyadic.diam (z-start p M) ≤ s / p.invlen + +lemma dome_prob (p : Node) (hp : p.ok M) (s : ℝ) (hs : 1 ≤ s) : + (uniform _).prob (dome p M s) ≤ 4*s/p.invlen := by + have hr := p.invlen_pos + have he : (uniform _).prob (dome p M s) ≤ 2*(s/p.invlen + side M) := by + unfold dome + rw [prob_translate (fun z => Dyadic.diam z ≤ s/p.invlen)] + exact arc_length _ _ (by apply div_nonneg <;> linarith) + have hi : side M ≤ 1/p.invlen := by + rw [side_pow_two,one_div] + exact (inv_le_inv₀ (by positivity) hr).mpr (loc_scale hp.2) + apply le_trans he + have hx := (le_div_iff₀ hr).mp hi + field_simp; linarith + +lemma prob_as_ex {α : Type} [Fintype α] (P : Law α) (p : α → Prop) [DecidablePred p] : + P.prob p = P.Ex (fun x => if p x then 1 else 0) := by + rw [prob_eq]; unfold Ex; simp + +lemma ch_dome_bound {p p' : Node} (hv : p.below p') {n : ℕ} {h err : ℝ} {A : Form M} + (H : Certified h err n p' A) (x : Grid M) (hx : x∈p.block M) (s : ℝ) (hs : 0 < s) : + (channel A x).prob (fun z => ¬dome p' M s z) ≤ 264/(8*s^2) := by + let C := channel A x + + + have hi : C.Ex (fun z => (8*s^2)*(if ¬dome p' M s z then 1 else 0)) ≤ 264 := + le_trans (C.Ex_mono (g:=fun z => form (place p' M) (z-p'.start M)) fun z => by + unfold dome; split_ifs with Hh + · rw [mul_zero] + exact form_nonneg (place_valid _ _) _ + · have hp := p'.invlen_pos + rw [not_le] at Hh + rw [mul_one,place,form_S] + apply le_trans (b:=p'.invlen^2*(8*(Dyadic.diam (z-start p' M))^2)) + · rw [div_lt_iff₀ hp] at Hh + have hh := pow_le_pow_left₀ hs.le Hh.le 2 + nlinarith + · exact mul_le_mul_of_nonneg_left (cost_diam _).1 (sq_nonneg _)) (local_channel_energy H x (block_of_below hv _ hx)) + rw [Ex_lmul,mul_comm] at hi + apply (le_div_iff₀ (by positivity : 0 < 8*s^2)).mpr + have HH : C.prob (fun x => ¬ dome p' M s x) = + C.Ex (fun z => if ¬dome p' M s z then 1 else 0) := by + refine @prob_as_ex _ _ _ _ (fun z => instDecidableNot) + rw [show (channel A x) = C from rfl,HH]; exact hi + +/-- factorization for the two center-shifted weights used in the unconditional cap -/ +private lemma two_envelope {A B C : Form M} (hA : 0 ≤ A) (hB : 0 ≤ B) (hh : A+B ≤ C) + (i j s : Grid M) : + ‖expU (fun y => (↑(boltz A (i-y)*boltz B (j-y)):ℂ)*chi s y)‖ ≤ exp (-F (A+B))*cred C s := by + let f := fun b : Bool => if b then B else A + let v := fun b : Bool => if b then j else i + have H := envelope Finset.univ f (fun i _ => by cases i; exact hA; exact hB) v (-s) + simp only [Fintype.prod_bool,Fintype.sum_bool,f,v,Bool.false_eq_true,↓reduceIte,q_even _ _] at H + rw [add_comm] at H + have HE := q_compare (add_nonneg hA hB) (le_refl (A+B)) hh s + apply le_trans ?_ HE + rw [expU_coeff (fun y => (↑(boltz A (i-y)*boltz B (j-y)):ℂ)) s] + apply le_trans _ H + push_cast; simp_rw [mul_comm ((boltz A _):ℂ)] + exact le_rfl + +lemma moments_bias {p : Node} {P : Law (Grid M)} (hl : l ≤ M) + {B : Bool} {A : Form M} {s : Finset (Grid l)} {v : Grid l → Grid M → ℂ} + {β : ℝ} (hG : biases P p A hl s B v β) (Q : Law (Grid l)) (hQ : ∀ x, 0 < Q x → x∈s) : + let U := P.withNext (channel A) + β ≤ U.Ex (fun xz => if xz.1∈(p.child B).block M then + (ply hl Q (fun i => v i xz.2) xz.1).re else 0) := by + intro U + let D (i : Grid l) (x : Grid M × Grid M) := + if x.1∈(p.child B).block M then (v i x.2*chi (raise hl i) x.1).re else 0 + have H (i : Grid l) (hi : 0 < Q i) : β ≤ U.Ex (D i) := by + unfold U D; rw [conditional_if_ex P (channel A) (fun x => x∈(p.child B).block M) + (fun x z => (v i z*chi (raise hl i) x).re)] + exact hG.2 _ (hQ _ hi) + calc + _ = Q.Ex (fun _ => β) := (Q.Ex_const _).symm + _ ≤ Q.Ex (fun i => U.Ex (D i)) := Q.Ex_mono_mem H + _ = _ := by + rw [Ex_swap Q U] + refine congrArg U.Ex (funext fun x => ?_) + unfold D + split_ifs with hh + · rw [ply,re_Cx] + rw [Ex_const] + +private lemma ex_pow_bound {α : Type} [Fintype α] + (P : Law α) (f : α → ℝ) (hf : ∀ x, 0 ≤ f x) (T : ℕ) : + P.Ex f ^ T ≤ P.Ex (fun i => f i ^ T) := by + have H := (convexOn_pow (𝕜:=ℝ) T).map_sum_le (t:=Finset.univ) + (p:=f) (w:=fun i => P i) (fun x _ => P.nonneg x) P.total (fun i _ => hf i) + exact H + +lemma uniform_level_cost {p p' : Node} {a : Grid M → ℝ} (hp : p.ok M) (hv : p.below p') + {n : ℕ} {h err : ℝ} {A A' : Form M} (H : Certified h err n p' A) + (HH : A+place p M ≤ A') + {P : Law (Grid M)} (ht : PieceLaw p a P) (hl : l ≤ M) {s : Finset (Grid l)} + {d : ℝ} (h1 : ∀ x, a x ≤ exp d) (hm : exp (-d) ≤ p.avg a) {B : Bool} + {v : Grid l → Grid M → ℂ} {β : ℝ} (h0 : 0 < β) (hb : β ≤ 1) + (HG : biases P p A hl s B v β) (Q : Law (Grid l)) (hQ : ∀ x, 0 < Q x → x∈s) (T : ℕ) + (hT : 0 < T) : + levelCost hl A' Q T ≤ 2*d + F A - log p'.invlen + 5000 - log β + + 2*T*(1-log β) := by + classical + let R := p.invlen + let R' := p'.invlen + let E := levelCost hl A' Q T + let K := place p M + let fp := F (A+K) + have hpR : 0 < R := p.invlen_pos + have hpR' : 0 < R' := p'.invlen_pos + let L := channel A + let U := P.withNext L + let a0 := 1000/β + have ha : 1 ≤ a0 := (le_div_iff₀ h0).mpr (by linarith) + let D := dome p' M a0 + have hD : U.prob (fun xz => ¬ D xz.2) ≤ β/2 := by + unfold prob; dsimp only [U]; rw [Ex_withNext] + rw [← Ex_const P (β/2)] + refine Ex_mono_mem _ fun x hx => ?_ + have hH := ch_dome_bound hv H x (piece_support hp ht x hx) a0 (by linarith) + apply le_trans (show (L x).prob _ ≤ _ from hH) + unfold a0; field_simp; nlinarith + let W (x z : Grid M) := ply hl Q (fun i => v i z) x + have HW (x z : Grid M) : ‖W x z‖≤1 := ply_unit _ _ _ (fun i => HG.1 i z) _ + let f (xz : Grid M × Grid M) := if D xz.2 then ‖W xz.1 xz.2‖ else 0 + have hf (xz) : 0 ≤ f xz := by unfold f; split_ifs <;> positivity + have he : β/2 ≤ U.Ex f := by + let V := fun xz : Grid M × Grid M => if ¬ D xz.2 then (1 : ℝ) else 0 + have hG' : β ≤ U.Ex fun xz => f xz+V xz := by + apply le_trans (moments_bias hl HG Q hQ) + refine Ex_mono _ fun xz => ?_ + unfold f V + change (if _ then (W _ _).re else _) ≤ _ + have hh := HW xz.1 xz.2 + have hh' := Complex.abs_re_le_norm (W xz.1 xz.2) + by_cases hh0 : D xz.2 <;> simp only [hh0,ite_true, ite_false,not_false_eq_true, not_true,add_zero,zero_add] <;> + split_ifs <;> first | positivity | linarith [(abs_le.mp hh').2] + have hD' : U.Ex V ≤ β/2 := by + rw [@prob_as_ex _ _ _ _ (fun z => instDecidableNot)] at hD + exact hD + rw [Ex_add] at hG'; linarith + let N := 2*T + have he1 : (β/2)^N ≤ U.Ex (fun i => f i ^ N) := + (pow_le_pow_left₀ (by positivity) he N).trans (ex_pow_bound _ _ hf _) + let V (z y : Grid M) := boltz A (z-y)*boltz K (p.start M-y) + have hV (z) : expU (fun y => V z y * ‖W y z‖^N) ≤ exp (-fp) * exp (-E) := by + unfold E + rw [heat_exp] + exact weighted_envelope T hl A' Q _ (fun x => HG.1 x z) _ _ + (fun s => two_envelope H.nonneg (place_valid _ _) HH _ _ _) + let c := R*exp (2*d+F A+32) + have hc : 0 ≤ c := by unfold c; positivity + let b := side M + have hbb : 0 < b := side_pos _ + have hpoint (x z : Grid M) : U (x,z) ≤ b*c*b*V z x := by + have Hx := piece_density ht h1 hm x + rw [dice_apply hp] at Hx + have h1 : 0 < V z x := mul_pos (boltz_pos _ _) (boltz_pos _ _) + by_cases h : x∈p.block M + · rw [ite_eq_left h,← rinv] at Hx + have HE := place_internal (start_mem hp.1 hp.2) h + have hi : 1 ≤ exp 32 * boltz K (p.start M-x) := by + unfold boltz; rw [← exp_add] + apply Real.one_le_exp _; unfold K; linarith + calc + _ = P x * (b*(exp (F A)*boltz A (z-x))) := by + unfold U L b + rw [withNext_apply,ch_K,K_eq,boltz,← exp_add]; congr 4 + _ ≤ (exp (2*d)*(b*R)) * (b*(exp (F A)*boltz A (z-x))) := + mul_le_mul_of_nonneg_right Hx (by have := boltz_pos A (z-x); positivity) + _ ≤ (exp (2*d)*(b*R)) * (b*(exp (F A)*boltz A (z-x))) * + (exp 32*boltz K (p.start M-x)) := + le_mul_of_one_le_right (by have := boltz_pos A (z-x); positivity) hi + _ = _ := by unfold c V; rw [exp_add,exp_add]; ring + · rw [ite_eq_right h,mul_zero] at Hx + unfold U; rw [withNext_apply,Hx.antisymm (P.nonneg _),zero_mul] + positivity + have HUB (z : Grid M) : ∑ x : Grid M, U (x,z)*f (x,z)^N ≤ + (if D z then b else 0) * (c*(exp (-fp)*exp (-E))) := by + split_ifs with HHz + · calc + _ ≤ ∑ x : Grid M, b*c*b*V z x*‖W x z‖^N := Finset.sum_le_sum fun x _ => by + simp only [f,HHz,ite_true] + exact mul_le_mul_of_nonneg_right (hpoint x z) (by positivity) + _ = b*c*expU (fun x => V z x*‖W x z‖^N) := by + unfold b expU; simp only [smul_eq_mul,Finset.mul_sum,mul_assoc] + _ ≤ _ := by + rw [← mul_assoc b c]; exact mul_le_mul_of_nonneg_left (hV z) (by positivity) + · have HH : N ≠ 0 := by unfold N; omega + simp [f,HHz,HH] + have HM : + (β/2)^N ≤ 4*a0/R' * (c*(exp (-fp)*exp (-E))) := calc + _ ≤ U.Ex (fun i => f i ^ N) := he1 + _ = ∑ z, ∑ x : Grid M, U (x,z)*f (x,z)^N := by unfold Ex; rw [Fintype.sum_prod_type,Finset.sum_comm] + _ ≤ ∑ z, (if D z then b else 0) * (c*(exp (-fp)*exp (-E))) := Finset.sum_le_sum (fun z _ => HUB z) + _ = (uniform _).prob D * (c*(exp (-fp)*exp (-E))) := by + rw [prob_eq]; simp_rw [uniform_side,← Finset.sum_mul]; rfl + _ ≤ _ := mul_le_mul_of_nonneg_right (dome_prob p' H.fits a0 ha) (by positivity) + have hrel : log R - 12 ≤ fp := (F_S_lower _ p.invlen_pos (loc_scale hp.2)).trans + (F_mono (le_add_of_nonneg_left H.nonneg)) + have hlog := log_le_log (by positivity) HM + unfold a0 c N at hlog + rw [show 4*(1000/β)=(4000:ℝ)/β by ring] at hlog + have hc' : 0 < R * exp (2*d + F A + 32) := by positivity + rw [log_pow,Real.log_div h0.ne' (by norm_num)] at hlog + rw [Real.log_mul (by positivity) (by positivity),Real.log_div (by positivity) hpR'.ne', + Real.log_div (by norm_num) h0.ne', Real.log_mul hc'.ne' (by positivity), + Real.log_mul hpR.ne' (by positivity),Real.log_mul (by positivity) (by positivity)] at hlog + push_cast at hlog + simp_rw [log_exp] at hlog + change E ≤ 2*d + F A - log R' + 5000-log β+_ + + nlinarith [log_le_sub_one_of_pos (by norm_num : (0:ℝ)<4000),log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2), + Nat.cast_nonneg (α:=ℝ) T] + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/True.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/True.lean index db690d44f60899a95d8412d30cbe6de69f11970a..e3189b9a4f1e9301fb5f135d317bc769c5f9d867 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/True.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/True.lean @@ -1,162 +1,162 @@ -import OAI.Analysis.FourierLLogL.Thermal.Reference -import OAI.Analysis.FourierLLogL.Finite.Tail - -/-! # True-law estimates used to supervise all attempts -/ -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -namespace Update -variable {p : Node} {j M : ℕ} - -/-- Failure of loudness only. -/ -def quiet (u : Update j M p) (x : Clutch j M) := - ∀ i, ‖app u x.1 (x.2 i)‖ < beta u.ix/4 - -lemma consistent_triangle {A : Form M} (u : Update j M p) (hA : 0 ≤ A) {x : Clutch j M} - {y z : Grid M} (h : form A (z-y) ≤ cushion j) - (h' : ∀ i, form A (x.2 i-y) ≤ cushion j) - (H : ¬ u.quiet x) : u.alarm A z x := by - unfold quiet at H - push Not at H - obtain ⟨i,hi⟩ := H - refine ⟨i,?_,hi⟩ - unfold consistent - have he := form_triangle hA (x.2 i-y) (-(z-y)) - rw [form_even _ _,show x.2 i-y+ -(z-y)=x.2 i-z by ring] at he - linarith [h' i] - -private lemma diff_cost (u : Update j M p) (hv : ∀ i z, ‖u.v i z‖ ≤ 1) (x z d : Grid M) : - (‖u.app x z‖- ‖u.app (x+d) z‖)^2 ≤ 2*form u.adjust d := by - let P := u.V; let r := raise (snap_le (M:=M) j p); let f := fun i => u.v i z - let c := chi (r u.x) d - have hn (x : Grid M) : u.app x z = P.Cx (fun i => f i * chi (r i) x) := rfl - have he (i : Grid (snapGrid j p M)) : c*chi (r (i-u.x)) d = chi (r i) d := by - unfold c; rw [← chi_add',← map_add] - congr 2; ring - let F (i : Grid (snapGrid j p M)) := f i * chi (r i) x * c * - (1-chi (r (i-u.x)) d) - have HQ : c*u.app x z-u.app (x+d) z=P.Cx F := by - simp_rw [hn,← Cx_lmul,Cx,← Finset.sum_sub_distrib,chi_add _ x] - exact Finset.sum_congr rfl fun i _ => by - rw [← he]; unfold F; ring - have hi (i) : ‖F i‖ ≤ ‖1-chi (r (i-u.x)) d‖ := by - unfold F c; simp_rw [norm_mul,norm_chi,mul_one] - exact mul_le_of_le_one_left (by positivity) (hv _ z) - have H := abs_norm_sub_norm_le (c*u.app x z) (u.app (x+d) z) - rw [HQ,norm_mul,show ‖c‖=1 from norm_chi _ _,one_mul] at H - calc - _ ≤ ‖P.Cx F‖^2 := by - rw [← sq_abs] - exact pow_le_pow_left₀ (abs_nonneg _) H _ - _ ≤ _ := sq_Cx_norm P F - _ ≤ P.Ex fun i => ‖1-chi (r (i-u.x)) d‖^2 := Ex_mono P (fun i => by gcongr; exact hi i) - _ = _ := by simp_rw [norm_phase_sq, Ex_lmul, adjust,average_form]; rfl - -open scoped Classical - -lemma quiet_scale (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, ∀ P : Law (Grid M), - ∀ (A : Form M) (u : Update j M p), u.Good C A P → u.regular → ∀ x : Grid M, - u.take A x → (fresh A (u.next A) j x).prob (quiet u) ≤ 2/H j^40 := by - filter_upwards [h_large (10^16*reserve C)] with j hh M p P A u hu hr x hx - let b := beta u.ix; let a := H j; let d := height j; let D := reserve C - let V := u.next A - let L := clones j - have hL : (L:ℝ) = a^120 := clones_eq j - have hb : 0 < b := beta_pos _ - have hI : 1 ≤ a := H_ge _ - have ha : 0 < a := H_pos _ - have hD : 1 ≤ D := reserve_ge hC - change 10^16*D ≤ a at hh - have he : (1/6400)/a^32 ≤ b := (main_bounds _ hu.ix_ok).1 - let T := channel A x - let G := channel V x - - let bad (i) : Prop := ‖u.app x i‖ < 3*b/8 - have hi : 1/a^33 ≤ 1-T.prob bad := by - unfold take at hx - rw [ite_eq_left hr] at hx - have hu' := hx.trans (Cx_norm T _) - have hE := lowerTail T (fun x => ¬ bad x) (3*b/8) 1 (fun i => ‖u.app x i‖) - (fun i _ => by - have HE : ‖u.app x i‖ ≤ 1 := ply_unit _ _ _ (fun j => hu.unit j _) _ - linarith) - (fun i hi => (show ‖u.app x i‖ < 3*b/8 from Classical.byContradiction (p:=bad i) hi).le) - rw [prob_compl] at hE - apply le_trans (b:=b/8) _ (by change b/2 ≤ _ at hu'; linarith) - apply le_trans (b:= ((1/6400)/a^32)/8) ?_ (by linarith) - field_simp - nlinarith - have hf : (T.prob bad)^L ≤ 1/a^40 := by - let t := T.prob bad - have he' := bernoulli_trial (prob_nonneg T bad) (prob_le_one T bad) L - rw [hL] at he' - change a^120*t^L*(1-t) ≤ _ at he' - have ht : 0 ≤ t^L := pow_nonneg (prob_nonneg _ _) _ - change t^L ≤ _ - have he : a^120*t^L * (1/a^33) ≤ 1 := - (mul_le_mul_of_nonneg_left hi (mul_nonneg (pow_nonneg ha.le 120) ht)).trans he' - have hZZ : a^40 ≤ a^87 := pow_le_pow_right₀ hI (by omega) - rw [show a^120*t^L*(1/a^33)=a^87*t^L by - rw [show a^120=a^87*a^33 by ring]; field_simp] at he - apply (le_div_iff₀ (by positivity : 0 < a^40)).mpr - nlinarith - - let other (z : Grid M) := (b/8)^2 ≤ 2*form u.adjust (z-x) - have hM : G.prob other ≤ 1/a^40 := by - have hp := upperTail G other (fun z => 2*form u.adjust (z-x)) (fun i => - mul_nonneg (by norm_num) (form_nonneg (averageCost_pos _ _ _) _)) ((b/8)^2) (fun i hi => hi) - rw [Ex_lmul, show G.Ex (fun z => form u.adjust (z-x)) = - (label V).Ex (form u.adjust) from ch_noise _ _ _] at hp - have hc := hu.high - have HE : (b/8)^2*G.prob other ≤ 800*D/a^200 := by - apply hp.trans - have HT : 0 < d*a^200 := mul_pos (d_pos j) (pow_pos ha 200) - apply le_trans (b:=2*(400*u.credit/u.scale)) (mul_le_mul_of_nonneg_left hu.energy (by norm_num)) - calc - _ ≤ 2*(400*(D*d)/(d*a^200)) := by - have H₁ : d*a^200 ≤ u.scale := hu.high - have H₂ : u.credit ≤ D*d := hu.reserves.2 - have hx : 0 < d := d_pos _ - gcongr - _ = _ := by have hx : 0 < d := d_pos _; field_simp; norm_num - apply le_of_mul_le_mul_left _ (show 0 < (b/8)^2 by positivity) - calc - _ ≤ _ := HE - _ ≤ (((1/6400)/a^32)/8)^2 * (1/a^40) := by - have Ht : a ≤ a^96 := by - have := pow_le_pow_right₀ hI (show 1 ≤ 96 by omega); simpa using this - field_simp - nlinarith - _ ≤ _ := by gcongr - let Q := fresh A V j x - have heQ : Q.prob (quiet u) ≤ G.prob other + (T.prob bad)^L := by - let S : Law (Fin L → Grid M) := pack fun _ => T - have HH (y z : Grid M) (hy : ¬ other y) (hz : ¬ bad z) : b/4 ≤ ‖u.app y z‖ := by - have HE := diff_cost u hu.unit x z (y-x) - rw [show x+(y-x)=y by ring] at HE - unfold other bad b at *; nlinarith - calc - Q.prob (quiet u) ≤ Q.prob (fun xz => other xz.1 ∨ ∀ i, bad (xz.2 i)) := - prob_mono Q _ _ fun x hx => by - by_contra! H - obtain ⟨i,hi⟩ := H.2 - exact (hx i).not_ge (HH _ _ H.1 hi) - _ ≤ Q.prob (fun x => other x.1) + Q.prob (fun x => ∀ i, bad (x.2 i)) := Q.prob_union _ _ - _ = _ := by - rw [← prob_send Q Prod.fst other, ← prob_send Q Prod.snd (fun x => ∀ i, bad (x i))] - unfold Q fresh - rw [send_fst_tensorP,send_snd_tensorP]; congr 1 - exact independent T bad L - change Q.prob _ ≤ 2/a^40 - apply heQ.trans; rw [show 2/a^40=1/a^40+1/a^40 by ring]; linarith - -end Update -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Reference +import OAI.Analysis.FourierLLogL.Finite.Tail + +/-! # True-law estimates used to supervise all attempts -/ +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +namespace Update +variable {p : Node} {j M : ℕ} + +/-- Failure of loudness only. -/ +def quiet (u : Update j M p) (x : Clutch j M) := + ∀ i, ‖app u x.1 (x.2 i)‖ < beta u.ix/4 + +lemma consistent_triangle {A : Form M} (u : Update j M p) (hA : 0 ≤ A) {x : Clutch j M} + {y z : Grid M} (h : form A (z-y) ≤ cushion j) + (h' : ∀ i, form A (x.2 i-y) ≤ cushion j) + (H : ¬ u.quiet x) : u.alarm A z x := by + unfold quiet at H + push Not at H + obtain ⟨i,hi⟩ := H + refine ⟨i,?_,hi⟩ + unfold consistent + have he := form_triangle hA (x.2 i-y) (-(z-y)) + rw [form_even _ _,show x.2 i-y+ -(z-y)=x.2 i-z by ring] at he + linarith [h' i] + +private lemma diff_cost (u : Update j M p) (hv : ∀ i z, ‖u.v i z‖ ≤ 1) (x z d : Grid M) : + (‖u.app x z‖- ‖u.app (x+d) z‖)^2 ≤ 2*form u.adjust d := by + let P := u.V; let r := raise (snap_le (M:=M) j p); let f := fun i => u.v i z + let c := chi (r u.x) d + have hn (x : Grid M) : u.app x z = P.Cx (fun i => f i * chi (r i) x) := rfl + have he (i : Grid (snapGrid j p M)) : c*chi (r (i-u.x)) d = chi (r i) d := by + unfold c; rw [← chi_add',← map_add] + congr 2; ring + let F (i : Grid (snapGrid j p M)) := f i * chi (r i) x * c * + (1-chi (r (i-u.x)) d) + have HQ : c*u.app x z-u.app (x+d) z=P.Cx F := by + simp_rw [hn,← Cx_lmul,Cx,← Finset.sum_sub_distrib,chi_add _ x] + exact Finset.sum_congr rfl fun i _ => by + rw [← he]; unfold F; ring + have hi (i) : ‖F i‖ ≤ ‖1-chi (r (i-u.x)) d‖ := by + unfold F c; simp_rw [norm_mul,norm_chi,mul_one] + exact mul_le_of_le_one_left (by positivity) (hv _ z) + have H := abs_norm_sub_norm_le (c*u.app x z) (u.app (x+d) z) + rw [HQ,norm_mul,show ‖c‖=1 from norm_chi _ _,one_mul] at H + calc + _ ≤ ‖P.Cx F‖^2 := by + rw [← sq_abs] + exact pow_le_pow_left₀ (abs_nonneg _) H _ + _ ≤ _ := sq_Cx_norm P F + _ ≤ P.Ex fun i => ‖1-chi (r (i-u.x)) d‖^2 := Ex_mono P (fun i => by gcongr; exact hi i) + _ = _ := by simp_rw [norm_phase_sq, Ex_lmul, adjust,average_form]; rfl + +open scoped Classical + +lemma quiet_scale (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, ∀ P : Law (Grid M), + ∀ (A : Form M) (u : Update j M p), u.Good C A P → u.regular → ∀ x : Grid M, + u.take A x → (fresh A (u.next A) j x).prob (quiet u) ≤ 2/H j^40 := by + filter_upwards [h_large (10^16*reserve C)] with j hh M p P A u hu hr x hx + let b := beta u.ix; let a := H j; let d := height j; let D := reserve C + let V := u.next A + let L := clones j + have hL : (L:ℝ) = a^120 := clones_eq j + have hb : 0 < b := beta_pos _ + have hI : 1 ≤ a := H_ge _ + have ha : 0 < a := H_pos _ + have hD : 1 ≤ D := reserve_ge hC + change 10^16*D ≤ a at hh + have he : (1/6400)/a^32 ≤ b := (main_bounds _ hu.ix_ok).1 + let T := channel A x + let G := channel V x + + let bad (i) : Prop := ‖u.app x i‖ < 3*b/8 + have hi : 1/a^33 ≤ 1-T.prob bad := by + unfold take at hx + rw [ite_eq_left hr] at hx + have hu' := hx.trans (Cx_norm T _) + have hE := lowerTail T (fun x => ¬ bad x) (3*b/8) 1 (fun i => ‖u.app x i‖) + (fun i _ => by + have HE : ‖u.app x i‖ ≤ 1 := ply_unit _ _ _ (fun j => hu.unit j _) _ + linarith) + (fun i hi => (show ‖u.app x i‖ < 3*b/8 from Classical.byContradiction (p:=bad i) hi).le) + rw [prob_compl] at hE + apply le_trans (b:=b/8) _ (by change b/2 ≤ _ at hu'; linarith) + apply le_trans (b:= ((1/6400)/a^32)/8) ?_ (by linarith) + field_simp + nlinarith + have hf : (T.prob bad)^L ≤ 1/a^40 := by + let t := T.prob bad + have he' := bernoulli_trial (prob_nonneg T bad) (prob_le_one T bad) L + rw [hL] at he' + change a^120*t^L*(1-t) ≤ _ at he' + have ht : 0 ≤ t^L := pow_nonneg (prob_nonneg _ _) _ + change t^L ≤ _ + have he : a^120*t^L * (1/a^33) ≤ 1 := + (mul_le_mul_of_nonneg_left hi (mul_nonneg (pow_nonneg ha.le 120) ht)).trans he' + have hZZ : a^40 ≤ a^87 := pow_le_pow_right₀ hI (by omega) + rw [show a^120*t^L*(1/a^33)=a^87*t^L by + rw [show a^120=a^87*a^33 by ring]; field_simp] at he + apply (le_div_iff₀ (by positivity : 0 < a^40)).mpr + nlinarith + + let other (z : Grid M) := (b/8)^2 ≤ 2*form u.adjust (z-x) + have hM : G.prob other ≤ 1/a^40 := by + have hp := upperTail G other (fun z => 2*form u.adjust (z-x)) (fun i => + mul_nonneg (by norm_num) (form_nonneg (averageCost_pos _ _ _) _)) ((b/8)^2) (fun i hi => hi) + rw [Ex_lmul, show G.Ex (fun z => form u.adjust (z-x)) = + (label V).Ex (form u.adjust) from ch_noise _ _ _] at hp + have hc := hu.high + have HE : (b/8)^2*G.prob other ≤ 800*D/a^200 := by + apply hp.trans + have HT : 0 < d*a^200 := mul_pos (d_pos j) (pow_pos ha 200) + apply le_trans (b:=2*(400*u.credit/u.scale)) (mul_le_mul_of_nonneg_left hu.energy (by norm_num)) + calc + _ ≤ 2*(400*(D*d)/(d*a^200)) := by + have H₁ : d*a^200 ≤ u.scale := hu.high + have H₂ : u.credit ≤ D*d := hu.reserves.2 + have hx : 0 < d := d_pos _ + gcongr + _ = _ := by have hx : 0 < d := d_pos _; field_simp; norm_num + apply le_of_mul_le_mul_left _ (show 0 < (b/8)^2 by positivity) + calc + _ ≤ _ := HE + _ ≤ (((1/6400)/a^32)/8)^2 * (1/a^40) := by + have Ht : a ≤ a^96 := by + have := pow_le_pow_right₀ hI (show 1 ≤ 96 by omega); simpa using this + field_simp + nlinarith + _ ≤ _ := by gcongr + let Q := fresh A V j x + have heQ : Q.prob (quiet u) ≤ G.prob other + (T.prob bad)^L := by + let S : Law (Fin L → Grid M) := pack fun _ => T + have HH (y z : Grid M) (hy : ¬ other y) (hz : ¬ bad z) : b/4 ≤ ‖u.app y z‖ := by + have HE := diff_cost u hu.unit x z (y-x) + rw [show x+(y-x)=y by ring] at HE + unfold other bad b at *; nlinarith + calc + Q.prob (quiet u) ≤ Q.prob (fun xz => other xz.1 ∨ ∀ i, bad (xz.2 i)) := + prob_mono Q _ _ fun x hx => by + by_contra! H + obtain ⟨i,hi⟩ := H.2 + exact (hx i).not_ge (HH _ _ H.1 hi) + _ ≤ Q.prob (fun x => other x.1) + Q.prob (fun x => ∀ i, bad (x.2 i)) := Q.prob_union _ _ + _ = _ := by + rw [← prob_send Q Prod.fst other, ← prob_send Q Prod.snd (fun x => ∀ i, bad (x i))] + unfold Q fresh + rw [send_fst_tensorP,send_snd_tensorP]; congr 1 + exact independent T bad L + change Q.prob _ ≤ 2/a^40 + apply heQ.trans; rw [show 2/a^40=1/a^40+1/a^40 by ring]; linarith + +end Update +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Thermal/Update.lean b/lean/OAI/Analysis/FourierLLogL/Thermal/Update.lean index ad016a014494f6d06aab39d88aa8ff1aa788c9ea..61d3cc57fa8c77f1dd5ee9990b957c526a1722f2 100644 --- a/lean/OAI/Analysis/FourierLLogL/Thermal/Update.lean +++ b/lean/OAI/Analysis/FourierLLogL/Thermal/Update.lean @@ -1,180 +1,180 @@ -import OAI.Analysis.FourierLLogL.Thermal.Stretch -import OAI.Analysis.FourierLLogL.Thermal.Flat - -/-! # Update records and validity -/ - -open Function Set Filter -open scoped Topology BigOperators ComplexConjugate -noncomputable section -namespace OAI - -namespace FourierLLogL.Dyadic.Thermal -open Law Node Real Period Compress.Params - -variable {M l j : ℕ} -open scoped Classical - -/-- Data determining an update. -/ -structure Update (j M : ℕ) (p : Node) where - ix : ℕ - V : Law (Grid (snapGrid j p M)) - x : Grid (snapGrid j p M) - v : Grid (snapGrid j p M) → Grid M → ℂ - scale : ℝ - credit : ℝ - regular : Bool - -variable {p : Node} - -namespace Update -def adjust (u : Update j M p) : Form M := averageCost (snap_le _ _) u.V u.x -def changeForm (u : Update j M p) : Form M := - u.scale • u.adjust -def next (u : Update j M p) (A : Form M) : Form M := A+place p M+u.changeForm -def app (u : Update j M p) (y z : Grid M) : ℂ := - ply (snap_le _ _) u.V (fun i => u.v i z) y - -/-- At a regular attempt membership uses the *specified* old-channel conditional mean, -never a sample. -/ -def tiltMean (u : Update j M p) (A : Form M) (y : Grid M) : ℂ := - (channel A y).Cx (u.app y) -def take (u : Update j M p) (A : Form M) (y : Grid M) : Prop := - if u.regular then beta u.ix /2 ≤ ‖u.tiltMean A y‖ else True -def chances (u : Update j M p) (P : Law (Grid M)) (A : Form M) := P.prob (u.take A) -def encoded (u : Update j M p) (C : ℝ) := kappa j (codeWeight C) u.ix -def bill (u : Update j M p) := if u.regular then u.credit else 0 - -structure Good (u : Update j M p) (C : ℝ) (A : Form M) (P : Law (Grid M)) : Prop where - ix_ok : u.ix ≤ testIx j - duration : eps * u.encoded C ≤ u.credit - reserves : height j/H j ^2 ≤ u.credit ∧ u.credit ≤ reserve C * height j - high : height j * H j ^200 ≤ u.scale - unit : ∀ i z, ‖u.v i z‖ ≤ 1 - positive : 0 ≤ u.changeForm - plate : flat (H j) (plateErr j) (A+place p M) u.changeForm - cost : F (u.next A) - F (A+place p M) ≤ 400 * u.credit - energy : (label (u.next A)).Ex (form u.adjust) ≤ 400 * u.credit/u.scale - bias : beta u.ix / 2 ≤ u.chances P A - cheap : u.regular = false → u.credit = eps * u.encoded C - evidence : u.regular → u.credit / 4 ≤ - levelCost (snap_le _ _) (tilde (H j) A p) u.V (sLen j) - gap_est : ∀ r : ℝ, 0 ≤ r → r ≤ sepRad j/2 → gap (u.next A) (r+stepRad C j) - gap A r ≤ - if u.regular then 402*u.credit else -(u.encoded C/2) - -lemma chance_bias {u : Update j M p} {A : Form M} {P : Law (Grid M)} - {w : Finset (Grid (snapGrid j p M))} {B : Bool} - (hQ : supported w u.V) - (hB : biases P p A (snap_le _ _) w B u.v (beta u.ix)) : - beta u.ix /2 ≤ u.chances P A := by - classical - let T := u.tiltMean A - have ht (x) : ‖T x‖ ≤ 1 := - norm_Cx_unit _ _ (fun y => ply_unit _ _ _ (fun i => hB.1 i y) _) - have he := moments_bias _ hB u.V hQ - change beta u.ix ≤ (P.withNext (channel A)).Ex (fun i => if i.1 ∈ (p.child B).block M then (u.app i.1 i.2).re else 0) at he - rw [conditional_if_ex P (channel A) (fun x => x∈(child p B).block M) (fun x z => (u.app x z).re)] at he - let b := beta u.ix - have hU : b ≤ P.Ex (fun x => ‖T x‖) := by - apply he.trans - refine Ex_mono _ fun x => ?_ - split_ifs - · rw [← re_Cx (channel A x) (u.app x)] - exact (le_abs_self _).trans (Complex.abs_re_le_norm _) - · positivity - unfold chances prob - have hs := P.Ex_mono (f:=fun x => ‖T x‖) - (g:=fun x => (if u.take A x then 1 else 0)+b/2) (fun x => by - by_cases H : u.take A x - · rw [ite_eq_left H]; have hi : 0 ≤ b := (beta_pos _).le - linarith [ht x] - · rw [ite_eq_right H,zero_add] - by_contra! he - apply H - unfold take; split - · exact he.le - trivial) - rw [Ex_add,Ex_const] at hs - change b/2 ≤ _ - linarith - -end Update - -lemma paid_tighten (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, ∀ A : Form M, Press C j p A → - F (tilde (H j) A p)-F (A+place p M) ≤ height j/H j^4 := by - filter_upwards [stiff_budget C hC] with j hh M p A H1 - obtain ⟨hq,n,q,hp,hG,hN,hF⟩ := H1 - have hp' := H_ge j - have hd := d_pos j - have ht : 0 ≤ plateErr j := by unfold plateErr; apply div_nonneg hd.le; exact pow_nonneg (H_pos j).le _ - apply (tilde_tight hG hp' hq).trans - apply le_trans _ hh; gcongr - -lemma make_update (C : ℝ) (hC : 1 ≤ C) : - ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ A : Form M, ∀ p : Node, - p.lev < M → Press C j p A → - ∀ (a : Grid M → ℝ), 0 ≤ a → (∀ x, a x ≤ exp (height j)) → exp (-height j) ≤ p.avg a → - ∀ (P : Law (Grid M)) (α : ℝ), PieceLaw p a P → 0 < α → p.avg a ≤ 2*α → - ∀ i : ℕ, i ≤ testIx j → - request A j p a α (side i) (kappa j (codeWeight C) i) → - ∃ u : Update j M p, u.ix = i ∧ u.Good C A P := by - filter_upwards [obtain_compressed C hC, flat_selection (reserve C) (reserve_ge hC), - paid_tighten C hC,kappa_scales (codeWeight C) (code_ge hC),step_gap C hC] - with j hC' hflat hT hL h - intro M A p hp H' a ha he hm P α hP hα hm' i hi hI - obtain ⟨w,x,B,v,V,t,h0,h1,h2,h3,h4,h5,h6,h7,h8⟩ := - hC' M A p hp H' a ha he hm P α hP hα hm' i hi hI - let d := height j - let L := long j - let k := kappa j (codeWeight C) i - let hZ := H j - let E := averageCost (snap_le (M:=M) j p) V x - let S := place p M - let R := A+S - let Q := tilde hZ A p - have Hq : p.ok M := H'.fits - have HH : 0 ≤ A := by obtain ⟨_,_,_,h,_⟩ := H'.cap; exact h.nonneg - have HE : 0 ≤ E := averageCost_pos _ _ _ - have Hh : 1 ≤ hZ := H_ge _ - have Hp : 0 < hZ := H_pos _ - have HQ : R ≤ Q := tilde_le Hh HH p - have hd : 0 < d := d_pos j - have ht : d/hZ^2 ≤ t := (hL i hi).1.trans h0 - have htp : 0 < t := lt_of_lt_of_le (by positivity) ht - have hi : F (R+L • E)- F R ≤ 400*t := by - have hh := F_mono (add_le_add_left HQ (L • E)) - have hi : F Q-F R ≤ d/hZ^4 := hT _ p A H' - change F (Q+L • E)-F Q ≤ 350*t at h7 - have hE : d/hZ^4 ≤ d/hZ^2 := by gcongr; norm_num - linarith - obtain ⟨c,hc,hc',hb,hb',hb''⟩ := hflat M R E - (add_nonneg HH (place_valid _ _)) HE t h1 hi - let D := c • E; let N := R+D - have HC : 0 < c := lt_of_lt_of_le (mul_pos hd (by positivity)) hc - have HD : 0 ≤ D := smul_nonneg HC.le HE - have Hstep (r : ℝ) (hr : 0 ≤ r) (hr' : r ≤ sepRad j/2) : - gap N (r+stepRad C j) - gap A r ≤ 402*t ∧ - (evenly w V → t=eps*k → gap N (r+stepRad C j)-gap A r ≤ -(k/2)) := by - obtain ⟨H1,H2⟩ := h M _ (snap_le _ _) p Hq A N HH (le_add_of_nonneg_right HD) - x V c t r ht h1 hc hb'' hb' hr hr' - exact ⟨H1,fun hh ht => H2 w k h2 h4 hh h6 ht⟩ - rcases h8 with ⟨Hc,Hv⟩ | HR - · let u : Update j M p := ⟨i,V,x,v,c,t,false⟩ - refine ⟨u,rfl,?_⟩ - refine ⟨?_,h0,⟨ht,h1⟩,hc,h5.1,HD,hb,hb',hb'',u.chance_bias h3 h5,fun _ => Hc,?_,?_⟩ - · change i ≤ _ - assumption - · simp [u] - intro r hr hr' - exact (Hstep r hr hr').2 Hv Hc - let u : Update j M p := ⟨i,V,x,v,c,t,true⟩ - refine ⟨u,rfl,?_⟩ - refine ⟨?_,h0,⟨ht,h1⟩,hc,h5.1,HD,hb,hb',hb'',u.chance_bias h3 h5,?_,fun _ => HR,?_⟩ - · change i ≤ _ - assumption - · simp [u] - intro r hr hr'; exact (Hstep r hr hr').1 - -end FourierLLogL.Dyadic.Thermal - -end OAI +import OAI.Analysis.FourierLLogL.Thermal.Stretch +import OAI.Analysis.FourierLLogL.Thermal.Flat + +/-! # Update records and validity -/ + +open Function Set Filter +open scoped Topology BigOperators ComplexConjugate +noncomputable section +namespace OAI + +namespace FourierLLogL.Dyadic.Thermal +open Law Node Real Period Compress.Params + +variable {M l j : ℕ} +open scoped Classical + +/-- Data determining an update. -/ +structure Update (j M : ℕ) (p : Node) where + ix : ℕ + V : Law (Grid (snapGrid j p M)) + x : Grid (snapGrid j p M) + v : Grid (snapGrid j p M) → Grid M → ℂ + scale : ℝ + credit : ℝ + regular : Bool + +variable {p : Node} + +namespace Update +def adjust (u : Update j M p) : Form M := averageCost (snap_le _ _) u.V u.x +def changeForm (u : Update j M p) : Form M := + u.scale • u.adjust +def next (u : Update j M p) (A : Form M) : Form M := A+place p M+u.changeForm +def app (u : Update j M p) (y z : Grid M) : ℂ := + ply (snap_le _ _) u.V (fun i => u.v i z) y + +/-- At a regular attempt membership uses the *specified* old-channel conditional mean, +never a sample. -/ +def tiltMean (u : Update j M p) (A : Form M) (y : Grid M) : ℂ := + (channel A y).Cx (u.app y) +def take (u : Update j M p) (A : Form M) (y : Grid M) : Prop := + if u.regular then beta u.ix /2 ≤ ‖u.tiltMean A y‖ else True +def chances (u : Update j M p) (P : Law (Grid M)) (A : Form M) := P.prob (u.take A) +def encoded (u : Update j M p) (C : ℝ) := kappa j (codeWeight C) u.ix +def bill (u : Update j M p) := if u.regular then u.credit else 0 + +structure Good (u : Update j M p) (C : ℝ) (A : Form M) (P : Law (Grid M)) : Prop where + ix_ok : u.ix ≤ testIx j + duration : eps * u.encoded C ≤ u.credit + reserves : height j/H j ^2 ≤ u.credit ∧ u.credit ≤ reserve C * height j + high : height j * H j ^200 ≤ u.scale + unit : ∀ i z, ‖u.v i z‖ ≤ 1 + positive : 0 ≤ u.changeForm + plate : flat (H j) (plateErr j) (A+place p M) u.changeForm + cost : F (u.next A) - F (A+place p M) ≤ 400 * u.credit + energy : (label (u.next A)).Ex (form u.adjust) ≤ 400 * u.credit/u.scale + bias : beta u.ix / 2 ≤ u.chances P A + cheap : u.regular = false → u.credit = eps * u.encoded C + evidence : u.regular → u.credit / 4 ≤ + levelCost (snap_le _ _) (tilde (H j) A p) u.V (sLen j) + gap_est : ∀ r : ℝ, 0 ≤ r → r ≤ sepRad j/2 → gap (u.next A) (r+stepRad C j) - gap A r ≤ + if u.regular then 402*u.credit else -(u.encoded C/2) + +lemma chance_bias {u : Update j M p} {A : Form M} {P : Law (Grid M)} + {w : Finset (Grid (snapGrid j p M))} {B : Bool} + (hQ : supported w u.V) + (hB : biases P p A (snap_le _ _) w B u.v (beta u.ix)) : + beta u.ix /2 ≤ u.chances P A := by + classical + let T := u.tiltMean A + have ht (x) : ‖T x‖ ≤ 1 := + norm_Cx_unit _ _ (fun y => ply_unit _ _ _ (fun i => hB.1 i y) _) + have he := moments_bias _ hB u.V hQ + change beta u.ix ≤ (P.withNext (channel A)).Ex (fun i => if i.1 ∈ (p.child B).block M then (u.app i.1 i.2).re else 0) at he + rw [conditional_if_ex P (channel A) (fun x => x∈(child p B).block M) (fun x z => (u.app x z).re)] at he + let b := beta u.ix + have hU : b ≤ P.Ex (fun x => ‖T x‖) := by + apply he.trans + refine Ex_mono _ fun x => ?_ + split_ifs + · rw [← re_Cx (channel A x) (u.app x)] + exact (le_abs_self _).trans (Complex.abs_re_le_norm _) + · positivity + unfold chances prob + have hs := P.Ex_mono (f:=fun x => ‖T x‖) + (g:=fun x => (if u.take A x then 1 else 0)+b/2) (fun x => by + by_cases H : u.take A x + · rw [ite_eq_left H]; have hi : 0 ≤ b := (beta_pos _).le + linarith [ht x] + · rw [ite_eq_right H,zero_add] + by_contra! he + apply H + unfold take; split + · exact he.le + trivial) + rw [Ex_add,Ex_const] at hs + change b/2 ≤ _ + linarith + +end Update + +lemma paid_tighten (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ p : Node, ∀ A : Form M, Press C j p A → + F (tilde (H j) A p)-F (A+place p M) ≤ height j/H j^4 := by + filter_upwards [stiff_budget C hC] with j hh M p A H1 + obtain ⟨hq,n,q,hp,hG,hN,hF⟩ := H1 + have hp' := H_ge j + have hd := d_pos j + have ht : 0 ≤ plateErr j := by unfold plateErr; apply div_nonneg hd.le; exact pow_nonneg (H_pos j).le _ + apply (tilde_tight hG hp' hq).trans + apply le_trans _ hh; gcongr + +lemma make_update (C : ℝ) (hC : 1 ≤ C) : + ∀ᶠ j : ℕ in atTop, ∀ M : ℕ, ∀ A : Form M, ∀ p : Node, + p.lev < M → Press C j p A → + ∀ (a : Grid M → ℝ), 0 ≤ a → (∀ x, a x ≤ exp (height j)) → exp (-height j) ≤ p.avg a → + ∀ (P : Law (Grid M)) (α : ℝ), PieceLaw p a P → 0 < α → p.avg a ≤ 2*α → + ∀ i : ℕ, i ≤ testIx j → + request A j p a α (side i) (kappa j (codeWeight C) i) → + ∃ u : Update j M p, u.ix = i ∧ u.Good C A P := by + filter_upwards [obtain_compressed C hC, flat_selection (reserve C) (reserve_ge hC), + paid_tighten C hC,kappa_scales (codeWeight C) (code_ge hC),step_gap C hC] + with j hC' hflat hT hL h + intro M A p hp H' a ha he hm P α hP hα hm' i hi hI + obtain ⟨w,x,B,v,V,t,h0,h1,h2,h3,h4,h5,h6,h7,h8⟩ := + hC' M A p hp H' a ha he hm P α hP hα hm' i hi hI + let d := height j + let L := long j + let k := kappa j (codeWeight C) i + let hZ := H j + let E := averageCost (snap_le (M:=M) j p) V x + let S := place p M + let R := A+S + let Q := tilde hZ A p + have Hq : p.ok M := H'.fits + have HH : 0 ≤ A := by obtain ⟨_,_,_,h,_⟩ := H'.cap; exact h.nonneg + have HE : 0 ≤ E := averageCost_pos _ _ _ + have Hh : 1 ≤ hZ := H_ge _ + have Hp : 0 < hZ := H_pos _ + have HQ : R ≤ Q := tilde_le Hh HH p + have hd : 0 < d := d_pos j + have ht : d/hZ^2 ≤ t := (hL i hi).1.trans h0 + have htp : 0 < t := lt_of_lt_of_le (by positivity) ht + have hi : F (R+L • E)- F R ≤ 400*t := by + have hh := F_mono (add_le_add_left HQ (L • E)) + have hi : F Q-F R ≤ d/hZ^4 := hT _ p A H' + change F (Q+L • E)-F Q ≤ 350*t at h7 + have hE : d/hZ^4 ≤ d/hZ^2 := by gcongr; norm_num + linarith + obtain ⟨c,hc,hc',hb,hb',hb''⟩ := hflat M R E + (add_nonneg HH (place_valid _ _)) HE t h1 hi + let D := c • E; let N := R+D + have HC : 0 < c := lt_of_lt_of_le (mul_pos hd (by positivity)) hc + have HD : 0 ≤ D := smul_nonneg HC.le HE + have Hstep (r : ℝ) (hr : 0 ≤ r) (hr' : r ≤ sepRad j/2) : + gap N (r+stepRad C j) - gap A r ≤ 402*t ∧ + (evenly w V → t=eps*k → gap N (r+stepRad C j)-gap A r ≤ -(k/2)) := by + obtain ⟨H1,H2⟩ := h M _ (snap_le _ _) p Hq A N HH (le_add_of_nonneg_right HD) + x V c t r ht h1 hc hb'' hb' hr hr' + exact ⟨H1,fun hh ht => H2 w k h2 h4 hh h6 ht⟩ + rcases h8 with ⟨Hc,Hv⟩ | HR + · let u : Update j M p := ⟨i,V,x,v,c,t,false⟩ + refine ⟨u,rfl,?_⟩ + refine ⟨?_,h0,⟨ht,h1⟩,hc,h5.1,HD,hb,hb',hb'',u.chance_bias h3 h5,fun _ => Hc,?_,?_⟩ + · change i ≤ _ + assumption + · simp [u] + intro r hr hr' + exact (Hstep r hr hr').2 Hv Hc + let u : Update j M p := ⟨i,V,x,v,c,t,true⟩ + refine ⟨u,rfl,?_⟩ + refine ⟨?_,h0,⟨ht,h1⟩,hc,h5.1,HD,hb,hb',hb'',u.chance_bias h3 h5,?_,fun _ => HR,?_⟩ + · change i ≤ _ + assumption + · simp [u] + intro r hr hr'; exact (Hstep r hr hr').1 + +end FourierLLogL.Dyadic.Thermal + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Cover.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Cover.lean index 90190126973f887c2a7d009e22129d0ffec31513..80cc1b564facc5a51b92e6d6596ed57992d2287e 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Cover.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Cover.lean @@ -1,124 +1,124 @@ -import OAI.Analysis.FourierLLogL.Weak.Quantile - -/-! # Amplification by translations. -/ -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL.Weak -open Real -open scoped Classical -variable {f : Torus → ℂ} - -abbrev law : Measure Torus := circleMeasure - -def offsetS (S : Set Torus) (z : Torus) := (fun x => z+x) ⁻¹' S -lemma offsetMeas {S : Set Torus} (h : MeasurableSet S) (z : Torus) : - MeasurableSet (offsetS S z) := (measurable_const.add measurable_id) h - -lemma flow (z : Torus) : - MeasurePreserving (fun x => z+x) law law := by - unfold law circleMeasure - exact measurePreserving_add_left _ _ - -lemma volume_offset (S : Set Torus) (h : MeasurableSet S) (z : Torus) : - law.real (offsetS S z)=law.real S := by - unfold Measure.real offsetS - rw [(flow z).measure_preimage h.nullMeasurableSet] - -lemma offset_witness (S T : Set Torus) (hs : MeasurableSet S) (ht : MeasurableSet T) : - ∃ z : Torus, law.real (S∩ offsetS T z) ≤ law.real S * law.real T := by - let U : Set (Torus × Torus) := {p | p.2 ∈ S ∧ p.1+p.2∈T} - have hU : MeasurableSet U := (measurable_snd hs).inter ((measurable_fst.add measurable_snd) ht) - let f := U.indicator (fun _ => (1:ℝ)) - have hF : Integrable f (law.prod law) := (integrable_const _).indicator hU - have H (z : Torus) : - ∫ x : Torus, f (z,x) ∂law=law.real (S∩offsetS T z) := by - rw [show (fun x : Torus => f (z,x))=(S∩offsetS T z).indicator (fun x => (1:ℝ)) from rfl, - integral_indicator (hs.inter (offsetMeas ht z))] - simp - have he (x : Torus) : - ∫ z : Torus, f (z,x) ∂law=S.indicator (fun x => law.real T) x := by - by_cases hh : x∈S - · rw [show (fun z : Torus => f (z,x)) = (offsetS T x).indicator (fun _ => (1:ℝ)) from by - funext z; unfold offsetS f U; by_cases hi : z+x∈T <;> simp [hh,hi,add_comm x z], - indicator_of_mem hh,integral_indicator (offsetMeas ht x)] - simp [volume_offset T ht] - rw [show (fun z : Torus => f (z,x))=(fun _ => 0) from by funext z; simp [f,U,hh], - indicator_of_notMem hh]; simp - have HV : ∫ z, (∫ x, f (z,x) ∂law) ∂law = law.real S*law.real T := by - rw [integral_integral_swap (f:=fun i j => f (i,j)) hF, funext he, integral_indicator hs] - simp - obtain ⟨v,hv⟩ := exists_le_integral hF.integral_prod_left - exact ⟨v,(H v) ▸ HV ▸ hv⟩ - -def spread (S : Set Torus) {n : ℕ} (f : Fin n → Torus) : Set Torus := - {x | ∃ i : Fin n, x∈offsetS S (f i)} - -lemma spread_meas (S : Set Torus) {n : ℕ} (f : Fin n → Torus) (hS : MeasurableSet S) : - MeasurableSet (spread S f) := by - have h : spread S f= ⋃ i : Fin n,offsetS S (f i) := by ext x; simp [spread] - rw [h]; exact MeasurableSet.iUnion fun i => offsetMeas hS _ - -lemma multiply_cloud (S : Set Torus) (h : MeasurableSet S) (n : ℕ) : - ∃ z : Fin n → Torus, law.real (spread S z)ᶜ ≤ (law.real Sᶜ)^n := by - induction n with - | zero => - refine ⟨fun i => i.elim0,?_⟩ - rw [pow_zero]; exact measureReal_le_one - | succ n ih => - obtain ⟨z,hz⟩ := ih - obtain ⟨q,hq⟩ := offset_witness _ _ (spread_meas S z h).compl h.compl - refine ⟨Fin.cons q z,?_⟩ - have HH : (spread S (Fin.cons q z))ᶜ=(spread S z)ᶜ ∩ offsetS Sᶜ q := by - ext x - have HI : x ∈ spread S (Fin.cons q z) ↔ x∈offsetS S q ∨ x∈spread S z := by - constructor - · rintro ⟨i,hi⟩ - refine Fin.cases ?_ ?_ i hi - · intro H; exact Or.inl (by simpa using H) - intro j h; right; exact ⟨j,by simpa using h⟩ - rintro (hh|⟨i,hi⟩) - · exact ⟨0,by simpa using hh⟩ - exact ⟨i.succ,by simpa using hi⟩ - change x∉spread S (Fin.cons q z) ↔ x∉spread S z ∧ x∉offsetS S q - rw [HI]; tauto - rw [HH]; apply hq.trans - rw [pow_succ] - exact mul_le_mul_of_nonneg_right hz measureReal_nonneg - -lemma clouds_fill (S : Set Torus) (h : MeasurableSet S) (hp : 0 < law.real S) : - ∃ (n : ℕ), 0 < n ∧ ∃ f : Fin n → Torus, 1/2 ≤ law.real (spread S f) ∧ - (n:ℝ)*law.real S ≤ 4 := by - let δ := law.real S - let n : ℕ := Nat.floor (1/δ)+1 - have he : δ ≤ 1 := measureReal_le_one - have hq : law.real Sᶜ = 1-δ := by rw [measureReal_compl h,probReal_univ] - obtain ⟨z,hz⟩ := multiply_cloud S h n - have HA : 1 ≤ (n:ℝ)*δ := by - have he' := Nat.lt_floor_add_one (1/δ) - unfold n; push_cast - exact ((div_lt_iff₀ hp).mp he').le - refine ⟨n,by unfold n; omega,z,?_,?_⟩ - · have H (n : ℕ) : (1+(n:ℝ)*δ)*(1-δ)^n ≤ 1 := by - have hl : 0 ≤ 1-δ := by linarith - induction n with - | zero => norm_num - | succ n ih => - rw [pow_succ]; push_cast - have HE : (1+((n:ℝ)+1)*δ)*(1-δ) ≤ (1+(n:ℝ)*δ) := by - nlinarith [Nat.cast_nonneg (α:=ℝ) n,mul_nonneg (Nat.cast_nonneg (α:=ℝ) n) (sq_nonneg δ)] - nlinarith [mul_le_mul_of_nonneg_right HE (pow_nonneg hl n)] - rw [measureReal_compl (spread_meas S z h),probReal_univ, hq] at hz - nlinarith [H n, pow_nonneg (show 0 ≤ 1-δ by linarith) n] - · have he' := Nat.floor_le (show 0 ≤ 1/δ by unfold δ; positivity) - change (n:ℝ)*δ ≤ 4 - have hd : 0 < δ := hp - unfold n; push_cast - nlinarith [div_mul_cancel₀ 1 hd.ne',mul_le_mul_of_nonneg_right he' hd.le] - -end FourierLLogL.Weak - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Quantile + +/-! # Amplification by translations. -/ +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL.Weak +open Real +open scoped Classical +variable {f : Torus → ℂ} + +abbrev law : Measure Torus := circleMeasure + +def offsetS (S : Set Torus) (z : Torus) := (fun x => z+x) ⁻¹' S +lemma offsetMeas {S : Set Torus} (h : MeasurableSet S) (z : Torus) : + MeasurableSet (offsetS S z) := (measurable_const.add measurable_id) h + +lemma flow (z : Torus) : + MeasurePreserving (fun x => z+x) law law := by + unfold law circleMeasure + exact measurePreserving_add_left _ _ + +lemma volume_offset (S : Set Torus) (h : MeasurableSet S) (z : Torus) : + law.real (offsetS S z)=law.real S := by + unfold Measure.real offsetS + rw [(flow z).measure_preimage h.nullMeasurableSet] + +lemma offset_witness (S T : Set Torus) (hs : MeasurableSet S) (ht : MeasurableSet T) : + ∃ z : Torus, law.real (S∩ offsetS T z) ≤ law.real S * law.real T := by + let U : Set (Torus × Torus) := {p | p.2 ∈ S ∧ p.1+p.2∈T} + have hU : MeasurableSet U := (measurable_snd hs).inter ((measurable_fst.add measurable_snd) ht) + let f := U.indicator (fun _ => (1:ℝ)) + have hF : Integrable f (law.prod law) := (integrable_const _).indicator hU + have H (z : Torus) : + ∫ x : Torus, f (z,x) ∂law=law.real (S∩offsetS T z) := by + rw [show (fun x : Torus => f (z,x))=(S∩offsetS T z).indicator (fun x => (1:ℝ)) from rfl, + integral_indicator (hs.inter (offsetMeas ht z))] + simp + have he (x : Torus) : + ∫ z : Torus, f (z,x) ∂law=S.indicator (fun x => law.real T) x := by + by_cases hh : x∈S + · rw [show (fun z : Torus => f (z,x)) = (offsetS T x).indicator (fun _ => (1:ℝ)) from by + funext z; unfold offsetS f U; by_cases hi : z+x∈T <;> simp [hh,hi,add_comm x z], + indicator_of_mem hh,integral_indicator (offsetMeas ht x)] + simp [volume_offset T ht] + rw [show (fun z : Torus => f (z,x))=(fun _ => 0) from by funext z; simp [f,U,hh], + indicator_of_notMem hh]; simp + have HV : ∫ z, (∫ x, f (z,x) ∂law) ∂law = law.real S*law.real T := by + rw [integral_integral_swap (f:=fun i j => f (i,j)) hF, funext he, integral_indicator hs] + simp + obtain ⟨v,hv⟩ := exists_le_integral hF.integral_prod_left + exact ⟨v,(H v) ▸ HV ▸ hv⟩ + +def spread (S : Set Torus) {n : ℕ} (f : Fin n → Torus) : Set Torus := + {x | ∃ i : Fin n, x∈offsetS S (f i)} + +lemma spread_meas (S : Set Torus) {n : ℕ} (f : Fin n → Torus) (hS : MeasurableSet S) : + MeasurableSet (spread S f) := by + have h : spread S f= ⋃ i : Fin n,offsetS S (f i) := by ext x; simp [spread] + rw [h]; exact MeasurableSet.iUnion fun i => offsetMeas hS _ + +lemma multiply_cloud (S : Set Torus) (h : MeasurableSet S) (n : ℕ) : + ∃ z : Fin n → Torus, law.real (spread S z)ᶜ ≤ (law.real Sᶜ)^n := by + induction n with + | zero => + refine ⟨fun i => i.elim0,?_⟩ + rw [pow_zero]; exact measureReal_le_one + | succ n ih => + obtain ⟨z,hz⟩ := ih + obtain ⟨q,hq⟩ := offset_witness _ _ (spread_meas S z h).compl h.compl + refine ⟨Fin.cons q z,?_⟩ + have HH : (spread S (Fin.cons q z))ᶜ=(spread S z)ᶜ ∩ offsetS Sᶜ q := by + ext x + have HI : x ∈ spread S (Fin.cons q z) ↔ x∈offsetS S q ∨ x∈spread S z := by + constructor + · rintro ⟨i,hi⟩ + refine Fin.cases ?_ ?_ i hi + · intro H; exact Or.inl (by simpa using H) + intro j h; right; exact ⟨j,by simpa using h⟩ + rintro (hh|⟨i,hi⟩) + · exact ⟨0,by simpa using hh⟩ + exact ⟨i.succ,by simpa using hi⟩ + change x∉spread S (Fin.cons q z) ↔ x∉spread S z ∧ x∉offsetS S q + rw [HI]; tauto + rw [HH]; apply hq.trans + rw [pow_succ] + exact mul_le_mul_of_nonneg_right hz measureReal_nonneg + +lemma clouds_fill (S : Set Torus) (h : MeasurableSet S) (hp : 0 < law.real S) : + ∃ (n : ℕ), 0 < n ∧ ∃ f : Fin n → Torus, 1/2 ≤ law.real (spread S f) ∧ + (n:ℝ)*law.real S ≤ 4 := by + let δ := law.real S + let n : ℕ := Nat.floor (1/δ)+1 + have he : δ ≤ 1 := measureReal_le_one + have hq : law.real Sᶜ = 1-δ := by rw [measureReal_compl h,probReal_univ] + obtain ⟨z,hz⟩ := multiply_cloud S h n + have HA : 1 ≤ (n:ℝ)*δ := by + have he' := Nat.lt_floor_add_one (1/δ) + unfold n; push_cast + exact ((div_lt_iff₀ hp).mp he').le + refine ⟨n,by unfold n; omega,z,?_,?_⟩ + · have H (n : ℕ) : (1+(n:ℝ)*δ)*(1-δ)^n ≤ 1 := by + have hl : 0 ≤ 1-δ := by linarith + induction n with + | zero => norm_num + | succ n ih => + rw [pow_succ]; push_cast + have HE : (1+((n:ℝ)+1)*δ)*(1-δ) ≤ (1+(n:ℝ)*δ) := by + nlinarith [Nat.cast_nonneg (α:=ℝ) n,mul_nonneg (Nat.cast_nonneg (α:=ℝ) n) (sq_nonneg δ)] + nlinarith [mul_le_mul_of_nonneg_right HE (pow_nonneg hl n)] + rw [measureReal_compl (spread_meas S z h),probReal_univ, hq] at hz + nlinarith [H n, pow_nonneg (show 0 ≤ 1-δ by linarith) n] + · have he' := Nat.floor_le (show 0 ≤ 1/δ by unfold δ; positivity) + change (n:ℝ)*δ ≤ 4 + have hd : 0 < δ := hp + unfold n; push_cast + nlinarith [div_mul_cancel₀ 1 hd.ne',mul_le_mul_of_nonneg_right he' hd.le] + +end FourierLLogL.Weak + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Endpoint.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Endpoint.lean index f8a6527666813ef317b041d8de078192c0bfd1c2..507d08d24fe0fa761f7008a5ee6492568618c331 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Endpoint.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Endpoint.lean @@ -1,78 +1,78 @@ -import OAI.Analysis.FourierLLogL.Weak.Signed - -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL.Weak -open Real -open scoped Classical - -def renorm (f : Torus → ℂ) (b : ℝ) := fun x => ((b⁻¹:ℝ):ℂ)*f x - -lemma young_renorm (f : Torus → ℂ) (b : ℝ) (hh : 0 < b) : - Young (renorm f b)=fun x => priceΦ (‖f x‖/b) := by - funext x; unfold Young renorm - rw [norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_pos (inv_pos.2 hh),←div_eq_inv_mul] -lemma bill_renorm (f : Torus → ℂ) (b : ℝ) (hh : 0 < b) : - modBill (renorm f b) 1= modBill f b := by - rw [mod_unit,show (fun x => Young (renorm f b) x)=_ from young_renorm _ _ hh]; rfl -lemma lux_exists {f : Torus → ℂ} (h : MemLLogL f) : (luxSet f).Nonempty := by - let T := modBill f 1; let b := max T 1 - have hb : 1 ≤ b := le_max_right _ _ - have hh : 0 < b := by linarith - refine ⟨b, hh,?_⟩ - let g := renorm f b - have HG : Integrable (Young g) law := (mem_dilate _ h).2 - rw [← bill_renorm f b hh,mod_unit] - apply le_trans (b:=∫ x,b⁻¹*Young f x ∂law) - · apply integral_mono HG ((show Integrable (Young f) law from h.2).const_mul _) - rw [← show (fun x => Young (renorm f b) x)=Young g from rfl] - intro x - change Young (renorm f b) x ≤ _ - rw [young_renorm _ _ hh] - let t := ‖f x‖ - have HT : 0 ≤ t := norm_nonneg _ - have hv : t/b ≤ t := div_le_self HT hb - change (t/b)*log (2+t/b) ≤ b⁻¹*(t*log (2+t)) - rw [← mul_assoc,← div_eq_inv_mul] - apply mul_le_mul_of_nonneg_left _ (by positivity) - apply log_le_log (by positivity); linarith - · rw [integral_const_mul] - rw [show (∫ x, Young f x ∂law) = T from (mod_unit f).symm,←div_eq_inv_mul] - exact (div_le_one hh).mpr (le_max_left _ _) - -lemma consequence_maximal (h : GoodBound) : - ∃ C : ℝ, 0 < C ∧ ∀ (f : Torus → ℂ), MemLLogL f → - ∀ w : ℝ, 0 < w → - w*circleMeasure.real (cloudSet f w) ≤ C*luxNorm f := by - obtain ⟨C,hc,hF⟩ := circle_bill h - refine ⟨C,hc,?_⟩ - intro f hf w hw - let m := law.real (cloudSet f w) - have H (b : ℝ) (hb : b∈luxSet f) : - w*m ≤ C*b := by - obtain ⟨hh,ht⟩ := hb - let g := renorm f b - have HE : inside g := ⟨mem_dilate _ hf,(bill_renorm f b hh).le.trans ht⟩ - have HP : cloudSet g (w/b)=cloudSet f w := by - ext x - rw [cloudy _ (by positivity) x,cloudy _ hw.le x] - have Hz (i : ℕ) : ‖symmetricPartialSum g i x‖=‖symmetricPartialSum f i x‖/b := by - unfold g renorm - rw [symmetricPartialSum_const_mul,norm_mul, Complex.norm_real,Real.norm_eq_abs, - abs_of_pos (inv_pos.2 hh),← div_eq_inv_mul] - simp_rw [Hz,div_lt_div_iff_of_pos_right hh] - have HI := hF g HE (w/b) (div_pos hw hh) - rw [HP,div_mul_eq_mul_div] at HI - exact (div_le_iff₀ hh).1 HI - have HV : w*m/C ≤ sInf (luxSet f) := - le_csInf (lux_exists hf) fun b hb => (div_le_iff₀ hc).mpr ((H b hb).trans (le_of_eq (mul_comm C b))) - change w*m ≤ C*sInf (luxSet f) - rw [mul_comm C]; exact (div_le_iff₀ hc).1 HV - -end FourierLLogL.Weak - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Signed + +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL.Weak +open Real +open scoped Classical + +def renorm (f : Torus → ℂ) (b : ℝ) := fun x => ((b⁻¹:ℝ):ℂ)*f x + +lemma young_renorm (f : Torus → ℂ) (b : ℝ) (hh : 0 < b) : + Young (renorm f b)=fun x => priceΦ (‖f x‖/b) := by + funext x; unfold Young renorm + rw [norm_mul,Complex.norm_real,Real.norm_eq_abs,abs_of_pos (inv_pos.2 hh),←div_eq_inv_mul] +lemma bill_renorm (f : Torus → ℂ) (b : ℝ) (hh : 0 < b) : + modBill (renorm f b) 1= modBill f b := by + rw [mod_unit,show (fun x => Young (renorm f b) x)=_ from young_renorm _ _ hh]; rfl +lemma lux_exists {f : Torus → ℂ} (h : MemLLogL f) : (luxSet f).Nonempty := by + let T := modBill f 1; let b := max T 1 + have hb : 1 ≤ b := le_max_right _ _ + have hh : 0 < b := by linarith + refine ⟨b, hh,?_⟩ + let g := renorm f b + have HG : Integrable (Young g) law := (mem_dilate _ h).2 + rw [← bill_renorm f b hh,mod_unit] + apply le_trans (b:=∫ x,b⁻¹*Young f x ∂law) + · apply integral_mono HG ((show Integrable (Young f) law from h.2).const_mul _) + rw [← show (fun x => Young (renorm f b) x)=Young g from rfl] + intro x + change Young (renorm f b) x ≤ _ + rw [young_renorm _ _ hh] + let t := ‖f x‖ + have HT : 0 ≤ t := norm_nonneg _ + have hv : t/b ≤ t := div_le_self HT hb + change (t/b)*log (2+t/b) ≤ b⁻¹*(t*log (2+t)) + rw [← mul_assoc,← div_eq_inv_mul] + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply log_le_log (by positivity); linarith + · rw [integral_const_mul] + rw [show (∫ x, Young f x ∂law) = T from (mod_unit f).symm,←div_eq_inv_mul] + exact (div_le_one hh).mpr (le_max_left _ _) + +lemma consequence_maximal (h : GoodBound) : + ∃ C : ℝ, 0 < C ∧ ∀ (f : Torus → ℂ), MemLLogL f → + ∀ w : ℝ, 0 < w → + w*circleMeasure.real (cloudSet f w) ≤ C*luxNorm f := by + obtain ⟨C,hc,hF⟩ := circle_bill h + refine ⟨C,hc,?_⟩ + intro f hf w hw + let m := law.real (cloudSet f w) + have H (b : ℝ) (hb : b∈luxSet f) : + w*m ≤ C*b := by + obtain ⟨hh,ht⟩ := hb + let g := renorm f b + have HE : inside g := ⟨mem_dilate _ hf,(bill_renorm f b hh).le.trans ht⟩ + have HP : cloudSet g (w/b)=cloudSet f w := by + ext x + rw [cloudy _ (by positivity) x,cloudy _ hw.le x] + have Hz (i : ℕ) : ‖symmetricPartialSum g i x‖=‖symmetricPartialSum f i x‖/b := by + unfold g renorm + rw [symmetricPartialSum_const_mul,norm_mul, Complex.norm_real,Real.norm_eq_abs, + abs_of_pos (inv_pos.2 hh),← div_eq_inv_mul] + simp_rw [Hz,div_lt_div_iff_of_pos_right hh] + have HI := hF g HE (w/b) (div_pos hw hh) + rw [HP,div_mul_eq_mul_div] at HI + exact (div_le_iff₀ hh).1 HI + have HV : w*m/C ≤ sInf (luxSet f) := + le_csInf (lux_exists hf) fun b hb => (div_le_iff₀ hc).mpr ((H b hb).trans (le_of_eq (mul_comm C b))) + change w*m ≤ C*sInf (luxSet f) + rw [mul_comm C]; exact (div_le_iff₀ hc).1 HV + +end FourierLLogL.Weak + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Layers.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Layers.lean index df472b075c0217ba3a142a002cec2a9f3a5818e5..526d184a502899734b2a2812ded1808666e1c298 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Layers.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Layers.lean @@ -1,135 +1,135 @@ -import OAI.Analysis.FourierLLogL.Weak.Modular - -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL.Weak -open Real -open scoped Classical -variable {f : Torus → ℂ} - -/-- allocate an integral for the good-part majorant, uniformly on modular ball -/ -lemma feeSummed (h : inside f) : - (∑' j : ℕ,(2:ℝ)^(j+1)*heightLayerMass f j) ≤ 1 := by - let g := Young f - let A := heightLayerSet f - have hF : Integrable g circleMeasure := h.1.2 - have HD := hasSum_integral_iUnion_ae (heightLayerSet_nullMeasurable h.1) - ((heightLayerSet_pairwise_disjoint f).mono (fun _ _ h ↦ h.aedisjoint)) - (show IntegrableOn g _ _ from hF.integrableOn) - have hi (j : ℕ) : (2:ℝ)^(j+1)*heightLayerMass f j ≤ ∫ x in A j,g x ∂circleMeasure := by - rw [heightLayerMass, ← integral_const_mul] - apply integral_mono_ae (h.1.integrable.norm.integrableOn.const_mul _) hF.integrableOn - filter_upwards [ae_restrict_mem₀ (heightLayerSet_nullMeasurable h.1 j)] with x hx - have hlog : (2:ℝ)^(j+1) ≤ Real.log (2+‖f x‖) := le_trans - (Real.log_exp ((2:ℝ)^(j+1))).symm.le - (Real.log_le_log (Real.exp_pos _) (hx.1.trans (by linarith))) - unfold g Young priceΦ; nlinarith [norm_nonneg (f x)] - calc - _ ≤ ∑' j,∫ x in A j,g x ∂circleMeasure := h.1.summable_weighted_heightLayerMass.tsum_le_tsum hi HD.summable - _ = ∫ x in ⋃ i, A i,g x ∂circleMeasure := HD.tsum_eq - _ ≤ ∫ x,g x ∂circleMeasure := by - conv_rhs => rw [← setIntegral_univ] - apply setIntegral_mono_set hF.integrableOn (ae_of_all _ fun x => p_nonneg _ (norm_nonneg _)) - (subset_univ _).eventuallyLE - _ ≤ 1 := mod_unit f ▸ h.2 - -def piece (f : Torus→ℂ) : ℕ → Torus → ℂ -| 0 => boundedPart f -| j+1 => heightLayer f j -def grit (j : ℕ) : ℝ := (1/4:ℝ)^j -lemma grit_pos (j : ℕ) : 0 < grit j := by unfold grit; positivity -lemma grits : Summable (fun j => (2:ℝ)^(j+1) * grit j) := by - simp only [grit,pow_succ] - apply Summable.congr ( (summable_geometric_of_norm_lt_one (by norm_num : ‖(2:ℝ)*(1/4)‖ < 1)).mul_right 2) - intro n; rw [mul_pow]; ring - - -private lemma grit_height (j : ℕ) : - Real.exp ((2:ℝ)^(j+2)) ≤ grit j * Real.exp ((2:ℝ)^(j+4)) := by - have hj : (j:ℝ) ≤ (2:ℝ)^j := by exact_mod_cast (Nat.lt_two_pow_self (n:=j)).le - have hex : (4:ℝ)^j ≤ Real.exp ((3:ℝ) * j) := by - have h3 := Real.add_one_le_exp 3 - rw [mul_comm 3, Real.exp_nat_mul] - exact pow_le_pow_left₀ (by norm_num) (by norm_num at h3; linarith) _ - have Hmul : (4:ℝ)^j * Real.exp ((2:ℝ)^(j+2)) ≤ Real.exp ((2:ℝ)^(j+4)) := by - refine (mul_le_mul_of_nonneg_right hex (Real.exp_nonneg _)).trans ?_ - rw [← Real.exp_add] - apply Real.exp_monotone - simp only [pow_add] - linarith - simp only [grit, one_div, inv_pow, ← div_eq_inv_mul] - apply (le_div_iff₀ (by positivity : (0:ℝ)<4^j)).mpr - rw [mul_comm] - exact Hmul - -def allowance (f : Torus → ℂ) : ℕ → ℝ -| 0 => exp 2 -| j+1 => heightLayerMass f j + grit j -lemma taxpayer (h : inside f) (j : ℕ) : - let w := allowance f j; let F := piece f j - 0 - have hi := h.1.boundedPart_integrable - refine ⟨exp_pos _,hi,?_,fun x => (norm_boundedPart_le _ _).trans ?_⟩ - · apply le_trans (integral_mono hi.norm (integrable_const (μ:=circleMeasure) w) - (norm_boundedPart_le _)) (by simp) - · exact le_mul_of_one_le_right (exp_pos 2).le (one_le_exp (by positivity)) - | succ j => - have Hi := heightLayerMass_nonneg f j - have he : grit j ≤ w := le_add_of_nonneg_left Hi - refine ⟨lt_of_lt_of_le (grit_pos j) he,h.1.heightLayer_integrable _,?_,?_⟩ - · rw [show F=heightLayer f j from rfl] - rw [← heightLayerMass_eq_integral_norm h.1] - exact le_add_of_nonneg_right (grit_pos j).le - intro x - apply le_trans (le_trans (norm_heightLayer_le f j x) (grit_height j)) - apply mul_le_mul he (exp_monotone ?_) (exp_pos _).le (le_trans (grit_pos j).le he) - exact pow_le_pow_right₀ (by norm_num) (by omega) - -def tollMax : ℝ := exp 2 + 1 + ∑' j, (2:ℝ)^(j+1)*grit j -lemma toll_bound (h : inside f) : - let a (j : ℕ) := (2:ℝ)^j*allowance f j - Summable a ∧ ∑' j,a j ≤ tollMax := by - intro a - let F (j : ℕ) := (2:ℝ)^(j+1)*heightLayerMass f j; let w (j : ℕ) := (2:ℝ)^(j+1)*grit j - have hF : Summable F := h.1.summable_weighted_heightLayerMass - have HX : (fun j => a (j+1))=fun j => F j+w j := by funext j; unfold a w F; rw [allowance,mul_add] - have HT : Summable a := (summable_nat_add_iff 1).mp (HX ▸ (hF.add grits)) - refine ⟨HT,?_⟩ - rw [← HT.sum_add_tsum_nat_add 1,HX] - rw [hF.tsum_add grits] - have HE : ∑' i,F i ≤ 1 := feeSummed h - simp only [Finset.sum_range_one] - have hi : a 0=exp 2 := by simp [a,allowance] - rw [hi] - unfold tollMax; linarith - -lemma paid_transform (h : inside f) (x : Torus) (n : ℕ) : - ENNReal.ofReal ‖symmetricPartialSum f n x‖ ≤ ∑' i, symmetricMaximal (piece f i) x := by - let F (i : ℕ) := symmetricPartialSum (piece f i) n x - let t (i : ℕ) := ‖F i‖ - have HE (i) : t (i+1) ≤ (2*n+1:ℕ)*(∫ x, ‖heightLayer f i x‖ ∂circleMeasure) := - norm_symmetricPartialSum_le _ n x - have Hs : Summable t := (summable_nat_add_iff 1).mp - ((h.1.summable_heightLayer_norm.mul_left ((2*n+1:ℕ):ℝ)).of_nonneg_of_le - (fun n => norm_nonneg _) HE) - have HH := h.1.symmetricPartialSum_heightDecomposition n x - have H : symmetricPartialSum f n x=∑' j,F j := by - rw [← (Hs.of_norm).sum_add_tsum_nat_add 1,Finset.sum_range_one]; exact HH - rw [H] - calc - _ ≤ ENNReal.ofReal (∑' i,t i) := ENNReal.ofReal_le_ofReal (norm_tsum_le_tsum_norm Hs) - _ = _ := ENNReal.ofReal_tsum_of_nonneg (fun i => norm_nonneg (F i)) Hs - _ ≤ ∑' i,symmetricMaximal (piece f i) x := - ENNReal.tsum_le_tsum fun i => le_iSup (fun n=> ENNReal.ofReal ‖symmetricPartialSum (piece f i) n x‖) n - -end FourierLLogL.Weak - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Modular + +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL.Weak +open Real +open scoped Classical +variable {f : Torus → ℂ} + +/-- allocate an integral for the good-part majorant, uniformly on modular ball -/ +lemma feeSummed (h : inside f) : + (∑' j : ℕ,(2:ℝ)^(j+1)*heightLayerMass f j) ≤ 1 := by + let g := Young f + let A := heightLayerSet f + have hF : Integrable g circleMeasure := h.1.2 + have HD := hasSum_integral_iUnion_ae (heightLayerSet_nullMeasurable h.1) + ((heightLayerSet_pairwise_disjoint f).mono (fun _ _ h ↦ h.aedisjoint)) + (show IntegrableOn g _ _ from hF.integrableOn) + have hi (j : ℕ) : (2:ℝ)^(j+1)*heightLayerMass f j ≤ ∫ x in A j,g x ∂circleMeasure := by + rw [heightLayerMass, ← integral_const_mul] + apply integral_mono_ae (h.1.integrable.norm.integrableOn.const_mul _) hF.integrableOn + filter_upwards [ae_restrict_mem₀ (heightLayerSet_nullMeasurable h.1 j)] with x hx + have hlog : (2:ℝ)^(j+1) ≤ Real.log (2+‖f x‖) := le_trans + (Real.log_exp ((2:ℝ)^(j+1))).symm.le + (Real.log_le_log (Real.exp_pos _) (hx.1.trans (by linarith))) + unfold g Young priceΦ; nlinarith [norm_nonneg (f x)] + calc + _ ≤ ∑' j,∫ x in A j,g x ∂circleMeasure := h.1.summable_weighted_heightLayerMass.tsum_le_tsum hi HD.summable + _ = ∫ x in ⋃ i, A i,g x ∂circleMeasure := HD.tsum_eq + _ ≤ ∫ x,g x ∂circleMeasure := by + conv_rhs => rw [← setIntegral_univ] + apply setIntegral_mono_set hF.integrableOn (ae_of_all _ fun x => p_nonneg _ (norm_nonneg _)) + (subset_univ _).eventuallyLE + _ ≤ 1 := mod_unit f ▸ h.2 + +def piece (f : Torus→ℂ) : ℕ → Torus → ℂ +| 0 => boundedPart f +| j+1 => heightLayer f j +def grit (j : ℕ) : ℝ := (1/4:ℝ)^j +lemma grit_pos (j : ℕ) : 0 < grit j := by unfold grit; positivity +lemma grits : Summable (fun j => (2:ℝ)^(j+1) * grit j) := by + simp only [grit,pow_succ] + apply Summable.congr ( (summable_geometric_of_norm_lt_one (by norm_num : ‖(2:ℝ)*(1/4)‖ < 1)).mul_right 2) + intro n; rw [mul_pow]; ring + + +private lemma grit_height (j : ℕ) : + Real.exp ((2:ℝ)^(j+2)) ≤ grit j * Real.exp ((2:ℝ)^(j+4)) := by + have hj : (j:ℝ) ≤ (2:ℝ)^j := by exact_mod_cast (Nat.lt_two_pow_self (n:=j)).le + have hex : (4:ℝ)^j ≤ Real.exp ((3:ℝ) * j) := by + have h3 := Real.add_one_le_exp 3 + rw [mul_comm 3, Real.exp_nat_mul] + exact pow_le_pow_left₀ (by norm_num) (by norm_num at h3; linarith) _ + have Hmul : (4:ℝ)^j * Real.exp ((2:ℝ)^(j+2)) ≤ Real.exp ((2:ℝ)^(j+4)) := by + refine (mul_le_mul_of_nonneg_right hex (Real.exp_nonneg _)).trans ?_ + rw [← Real.exp_add] + apply Real.exp_monotone + simp only [pow_add] + linarith + simp only [grit, one_div, inv_pow, ← div_eq_inv_mul] + apply (le_div_iff₀ (by positivity : (0:ℝ)<4^j)).mpr + rw [mul_comm] + exact Hmul + +def allowance (f : Torus → ℂ) : ℕ → ℝ +| 0 => exp 2 +| j+1 => heightLayerMass f j + grit j +lemma taxpayer (h : inside f) (j : ℕ) : + let w := allowance f j; let F := piece f j + 0 + have hi := h.1.boundedPart_integrable + refine ⟨exp_pos _,hi,?_,fun x => (norm_boundedPart_le _ _).trans ?_⟩ + · apply le_trans (integral_mono hi.norm (integrable_const (μ:=circleMeasure) w) + (norm_boundedPart_le _)) (by simp) + · exact le_mul_of_one_le_right (exp_pos 2).le (one_le_exp (by positivity)) + | succ j => + have Hi := heightLayerMass_nonneg f j + have he : grit j ≤ w := le_add_of_nonneg_left Hi + refine ⟨lt_of_lt_of_le (grit_pos j) he,h.1.heightLayer_integrable _,?_,?_⟩ + · rw [show F=heightLayer f j from rfl] + rw [← heightLayerMass_eq_integral_norm h.1] + exact le_add_of_nonneg_right (grit_pos j).le + intro x + apply le_trans (le_trans (norm_heightLayer_le f j x) (grit_height j)) + apply mul_le_mul he (exp_monotone ?_) (exp_pos _).le (le_trans (grit_pos j).le he) + exact pow_le_pow_right₀ (by norm_num) (by omega) + +def tollMax : ℝ := exp 2 + 1 + ∑' j, (2:ℝ)^(j+1)*grit j +lemma toll_bound (h : inside f) : + let a (j : ℕ) := (2:ℝ)^j*allowance f j + Summable a ∧ ∑' j,a j ≤ tollMax := by + intro a + let F (j : ℕ) := (2:ℝ)^(j+1)*heightLayerMass f j; let w (j : ℕ) := (2:ℝ)^(j+1)*grit j + have hF : Summable F := h.1.summable_weighted_heightLayerMass + have HX : (fun j => a (j+1))=fun j => F j+w j := by funext j; unfold a w F; rw [allowance,mul_add] + have HT : Summable a := (summable_nat_add_iff 1).mp (HX ▸ (hF.add grits)) + refine ⟨HT,?_⟩ + rw [← HT.sum_add_tsum_nat_add 1,HX] + rw [hF.tsum_add grits] + have HE : ∑' i,F i ≤ 1 := feeSummed h + simp only [Finset.sum_range_one] + have hi : a 0=exp 2 := by simp [a,allowance] + rw [hi] + unfold tollMax; linarith + +lemma paid_transform (h : inside f) (x : Torus) (n : ℕ) : + ENNReal.ofReal ‖symmetricPartialSum f n x‖ ≤ ∑' i, symmetricMaximal (piece f i) x := by + let F (i : ℕ) := symmetricPartialSum (piece f i) n x + let t (i : ℕ) := ‖F i‖ + have HE (i) : t (i+1) ≤ (2*n+1:ℕ)*(∫ x, ‖heightLayer f i x‖ ∂circleMeasure) := + norm_symmetricPartialSum_le _ n x + have Hs : Summable t := (summable_nat_add_iff 1).mp + ((h.1.summable_heightLayer_norm.mul_left ((2*n+1:ℕ):ℝ)).of_nonneg_of_le + (fun n => norm_nonneg _) HE) + have HH := h.1.symmetricPartialSum_heightDecomposition n x + have H : symmetricPartialSum f n x=∑' j,F j := by + rw [← (Hs.of_norm).sum_add_tsum_nat_add 1,Finset.sum_range_one]; exact HH + rw [H] + calc + _ ≤ ENNReal.ofReal (∑' i,t i) := ENNReal.ofReal_le_ofReal (norm_tsum_le_tsum_norm Hs) + _ = _ := ENNReal.ofReal_tsum_of_nonneg (fun i => norm_nonneg (F i)) Hs + _ ≤ ∑' i,symmetricMaximal (piece f i) x := + ENNReal.tsum_le_tsum fun i => le_iSup (fun n=> ENNReal.ofReal ‖symmetricPartialSum (piece f i) n x‖) n + +end FourierLLogL.Weak + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Modular.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Modular.lean index 8a4bc2cd2c13f75c8d89f7e620418b212f693d70..300bca79ddb4dce0dc639ad938945fd3d4a02ef3 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Modular.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Modular.lean @@ -1,140 +1,140 @@ -import OAI.Analysis.FourierLLogL.Fourier.Endpoint -/-! # The Luxemburg gauge for the Fourier convergence class -/ -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL - -def priceΦ (x : ℝ) := x*Real.log (2+x) -def modBill (f : Torus → ℂ) (b : ℝ) := ∫ x, priceΦ (‖f x‖/b) ∂circleMeasure -def luxSet (f : Torus → ℂ) := {b | 0 < b ∧ modBill f b ≤ 1} -def luxNorm (f : Torus → ℂ) := sInf (luxSet f) - -namespace Weak -open Real -open scoped Classical - -lemma p_nonneg (x : ℝ) (hx : 0 ≤ x) : 0 ≤ priceΦ x := - mul_nonneg hx (log_nonneg (by linarith)) -lemma p_zero : priceΦ 0=0 := by simp [priceΦ] -lemma p_mono {x y : ℝ} (hx : 0 ≤ x) (he : x ≤ y) : - priceΦ x ≤ priceΦ y := mul_le_mul he (log_le_log (by linarith) (by linarith)) - (log_nonneg (by linarith)) (hx.trans he) - - -private def slopeΦ (x : ℝ) := log (2+x)+x/(2+x) -private lemma p_support (x y : ℝ) (hx : 0 ≤ x) (hy : 0 ≤ y) : - priceΦ y+slopeΦ y*(x-y) ≤ priceΦ x := by - let a := 2+x; let b := 2+y - have ha : 0 < a := by unfold a; linarith - have hb : 0 < b := by unfold b; linarith - have H : (a-b)/a ≤ log a-log b := by - have hi := log_le_sub_one_of_pos (div_pos hb ha) - rw [log_div hb.ne' ha.ne'] at hi - rw [sub_div,div_self ha.ne']; linarith - have HH : y/b*(x-y) ≤ x*((a-b)/a) := by - have HE : 0 ≤ 2*(x-y)^2/(a*b) := by positivity - have HV : x*((a-b)/a)-y/b*(x-y)=2*(x-y)^2/(a*b) := by - field_simp; unfold a b; ring - linarith - change y*log b + (log b + y/b)*(x-y) ≤ x*log a - nlinarith - -/-- Average of finitely many entries. -/ -lemma price_convex {ι : Type*} [Fintype ι] [Nonempty ι] (f : ι → ℂ) : - let a : ℝ := Fintype.card ι - priceΦ ‖(a⁻¹:ℝ) • (∑ i, f i)‖ ≤ a⁻¹ * ∑ i,priceΦ ‖f i‖ := by - intro a - have ha : 0 < a := by unfold a; positivity - let k := (∑ i, ‖f i‖)/a - have hk : 0 ≤ k := by unfold k; positivity - apply le_trans (b:=priceΦ k) (p_mono (norm_nonneg _) ?_) - · rw [←div_eq_inv_mul] - apply (le_div_iff₀ ha).mpr - have HH : ∑ i : ι,(priceΦ k+slopeΦ k*(‖f i‖-k)) ≤ _ := - Finset.sum_le_sum fun (i : ι) (_ : i∈Finset.univ) => p_support ‖f i‖ k (norm_nonneg _) hk - rw [Finset.sum_add_distrib,← Finset.mul_sum,Finset.sum_sub_distrib] at HH - simp only [Finset.sum_const,nsmul_eq_mul,Finset.card_univ] at HH - have H : ∑ i : ι,‖f i‖ = a*k := by unfold k; field_simp - change a*priceΦ k+_ * (_-a*k) ≤ _ at HH - rw [H] at HH; linarith - rw [norm_smul,Real.norm_eq_abs,abs_of_nonneg (inv_nonneg.mpr ha.le)] - apply (mul_le_mul_of_nonneg_left (norm_sum_le _ _) (inv_nonneg.mpr ha.le)).trans - exact (div_eq_inv_mul _ _).ge - -/-- Scalar comparison under every finite dilation. -/ -lemma scale_admit (a : ℝ) (ha : 0 ≤ a) : - ∃ c : ℝ, 0 ≤ c ∧ ∀ x, 0 ≤ x → priceΦ (a*x) ≤ c*priceΦ x := by - rcases le_total a 1 with h|h - · refine ⟨1,by norm_num,fun x hx => ?_⟩; rw [one_mul] - apply p_mono (mul_nonneg ha hx); nlinarith - · refine ⟨a*(1+2*log a),by have := log_nonneg h; positivity,fun x hx => ?_⟩ - have HH : log (2+a*x) ≤ log a+log (2+x) := by - rw [← log_mul (by positivity) (by positivity)] - apply log_le_log (by positivity); nlinarith - have hi : 1/2 ≤ log (2+x) := - le_trans (by linarith [Real.log_two_gt_d9]) (log_le_log (by norm_num : 0 < (2:ℝ)) (by linarith)) - have HR : log a ≤ 2*log a * log (2+x) := by nlinarith [log_nonneg h] - unfold priceΦ; nlinarith [mul_le_mul_of_nonneg_left (le_trans HH (add_le_add_left HR _)) - (mul_nonneg ha hx)] - -def Young (f : Torus → ℂ) (x : Torus) := priceΦ ‖f x‖ -def inside (f : Torus → ℂ) := MemLLogL f ∧ modBill f 1 ≤ 1 - -lemma young_meas {f : Torus → ℂ} (h : AEStronglyMeasurable f circleMeasure) : - AEStronglyMeasurable (Young f) circleMeasure := by - have hp := h.norm - unfold Young priceΦ - exact hp.mul ((aemeasurable_const.add hp.aemeasurable).log).aestronglyMeasurable - -lemma mem_dilate (c : ℂ) {f : Torus → ℂ} (hp : MemLLogL f) : - MemLLogL (fun x => c*f x) := by - let g (x : Torus) := c*f x - have hi : AEStronglyMeasurable g circleMeasure := hp.1.const_mul _ - obtain ⟨v,hv,ht⟩ := scale_admit ‖c‖ (norm_nonneg _) - refine ⟨hi,?_⟩ - change Integrable (Young g) circleMeasure - apply ((show Integrable (Young f) _ from hp.2).const_mul v).mono' (young_meas hi) - apply ae_of_all - intro x - change ‖Young g x‖ ≤ _ - rw [Real.norm_eq_abs,abs_of_nonneg (show 0 ≤ Young g x from p_nonneg _ (norm_nonneg _))] - unfold Young g; rw [norm_mul]; apply ht; exact norm_nonneg _ - -lemma mod_unit (f : Torus → ℂ) : - modBill f 1 = ∫ x, Young f x ∂circleMeasure := by simp only [modBill,div_one]; rfl - -lemma sum_inside {ι : Type*} [Fintype ι] [Nonempty ι] - (f : ι → Torus → ℂ) (h : ∀ i, inside (f i)) : - let a : ℝ := Fintype.card ι - inside (fun x => a⁻¹ • ∑ i, f i x) := by - intro a - let g (x : Torus) : ℂ := a⁻¹ • ∑ i, f i x - let F (x : Torus) := a⁻¹ * ∑ i, Young (f i) x - have ha : 0 < a := by unfold a; positivity - have he (i) : Integrable (Young (f i)) circleMeasure := (h i).1.2 - have HH : AEStronglyMeasurable g circleMeasure := by - unfold g - have HH := Finset.aestronglyMeasurable_fun_sum Finset.univ (fun (i:ι) _ => (h i).1.1) - exact HH.const_smul (a⁻¹) - have HI : Integrable F circleMeasure := (integrable_finsetSum _ fun i _ => he i).const_mul _ - have HX (x : Torus) : Young g x ≤ F x := price_convex fun i => f i x - have HV : Integrable (Young g) circleMeasure := - HI.mono' (young_meas HH) (ae_of_all _ fun x => by - rw [Real.norm_eq_abs,abs_of_nonneg (show 0 ≤ Young g x from p_nonneg _ (norm_nonneg _))] - exact HX x) - refine ⟨⟨HH,HV⟩,?_⟩ - rw [mod_unit] - apply (integral_mono HV HI HX).trans - unfold F; rw [integral_const_mul,integral_finsetSum _ (fun i _ => he i)] - rw [←div_eq_inv_mul] - apply (div_le_one ha).mpr - have Hz (i) : ∫ x,Young (f i) x ∂circleMeasure ≤ 1 := by rw [←mod_unit]; exact (h i).2 - exact le_trans (Finset.sum_le_sum (fun i _ => Hz i)) (by simp [a]) -end Weak -end FourierLLogL - -end OAI +import OAI.Analysis.FourierLLogL.Fourier.Endpoint +/-! # The Luxemburg gauge for the Fourier convergence class -/ +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL + +def priceΦ (x : ℝ) := x*Real.log (2+x) +def modBill (f : Torus → ℂ) (b : ℝ) := ∫ x, priceΦ (‖f x‖/b) ∂circleMeasure +def luxSet (f : Torus → ℂ) := {b | 0 < b ∧ modBill f b ≤ 1} +def luxNorm (f : Torus → ℂ) := sInf (luxSet f) + +namespace Weak +open Real +open scoped Classical + +lemma p_nonneg (x : ℝ) (hx : 0 ≤ x) : 0 ≤ priceΦ x := + mul_nonneg hx (log_nonneg (by linarith)) +lemma p_zero : priceΦ 0=0 := by simp [priceΦ] +lemma p_mono {x y : ℝ} (hx : 0 ≤ x) (he : x ≤ y) : + priceΦ x ≤ priceΦ y := mul_le_mul he (log_le_log (by linarith) (by linarith)) + (log_nonneg (by linarith)) (hx.trans he) + + +private def slopeΦ (x : ℝ) := log (2+x)+x/(2+x) +private lemma p_support (x y : ℝ) (hx : 0 ≤ x) (hy : 0 ≤ y) : + priceΦ y+slopeΦ y*(x-y) ≤ priceΦ x := by + let a := 2+x; let b := 2+y + have ha : 0 < a := by unfold a; linarith + have hb : 0 < b := by unfold b; linarith + have H : (a-b)/a ≤ log a-log b := by + have hi := log_le_sub_one_of_pos (div_pos hb ha) + rw [log_div hb.ne' ha.ne'] at hi + rw [sub_div,div_self ha.ne']; linarith + have HH : y/b*(x-y) ≤ x*((a-b)/a) := by + have HE : 0 ≤ 2*(x-y)^2/(a*b) := by positivity + have HV : x*((a-b)/a)-y/b*(x-y)=2*(x-y)^2/(a*b) := by + field_simp; unfold a b; ring + linarith + change y*log b + (log b + y/b)*(x-y) ≤ x*log a + nlinarith + +/-- Average of finitely many entries. -/ +lemma price_convex {ι : Type*} [Fintype ι] [Nonempty ι] (f : ι → ℂ) : + let a : ℝ := Fintype.card ι + priceΦ ‖(a⁻¹:ℝ) • (∑ i, f i)‖ ≤ a⁻¹ * ∑ i,priceΦ ‖f i‖ := by + intro a + have ha : 0 < a := by unfold a; positivity + let k := (∑ i, ‖f i‖)/a + have hk : 0 ≤ k := by unfold k; positivity + apply le_trans (b:=priceΦ k) (p_mono (norm_nonneg _) ?_) + · rw [←div_eq_inv_mul] + apply (le_div_iff₀ ha).mpr + have HH : ∑ i : ι,(priceΦ k+slopeΦ k*(‖f i‖-k)) ≤ _ := + Finset.sum_le_sum fun (i : ι) (_ : i∈Finset.univ) => p_support ‖f i‖ k (norm_nonneg _) hk + rw [Finset.sum_add_distrib,← Finset.mul_sum,Finset.sum_sub_distrib] at HH + simp only [Finset.sum_const,nsmul_eq_mul,Finset.card_univ] at HH + have H : ∑ i : ι,‖f i‖ = a*k := by unfold k; field_simp + change a*priceΦ k+_ * (_-a*k) ≤ _ at HH + rw [H] at HH; linarith + rw [norm_smul,Real.norm_eq_abs,abs_of_nonneg (inv_nonneg.mpr ha.le)] + apply (mul_le_mul_of_nonneg_left (norm_sum_le _ _) (inv_nonneg.mpr ha.le)).trans + exact (div_eq_inv_mul _ _).ge + +/-- Scalar comparison under every finite dilation. -/ +lemma scale_admit (a : ℝ) (ha : 0 ≤ a) : + ∃ c : ℝ, 0 ≤ c ∧ ∀ x, 0 ≤ x → priceΦ (a*x) ≤ c*priceΦ x := by + rcases le_total a 1 with h|h + · refine ⟨1,by norm_num,fun x hx => ?_⟩; rw [one_mul] + apply p_mono (mul_nonneg ha hx); nlinarith + · refine ⟨a*(1+2*log a),by have := log_nonneg h; positivity,fun x hx => ?_⟩ + have HH : log (2+a*x) ≤ log a+log (2+x) := by + rw [← log_mul (by positivity) (by positivity)] + apply log_le_log (by positivity); nlinarith + have hi : 1/2 ≤ log (2+x) := + le_trans (by linarith [Real.log_two_gt_d9]) (log_le_log (by norm_num : 0 < (2:ℝ)) (by linarith)) + have HR : log a ≤ 2*log a * log (2+x) := by nlinarith [log_nonneg h] + unfold priceΦ; nlinarith [mul_le_mul_of_nonneg_left (le_trans HH (add_le_add_left HR _)) + (mul_nonneg ha hx)] + +def Young (f : Torus → ℂ) (x : Torus) := priceΦ ‖f x‖ +def inside (f : Torus → ℂ) := MemLLogL f ∧ modBill f 1 ≤ 1 + +lemma young_meas {f : Torus → ℂ} (h : AEStronglyMeasurable f circleMeasure) : + AEStronglyMeasurable (Young f) circleMeasure := by + have hp := h.norm + unfold Young priceΦ + exact hp.mul ((aemeasurable_const.add hp.aemeasurable).log).aestronglyMeasurable + +lemma mem_dilate (c : ℂ) {f : Torus → ℂ} (hp : MemLLogL f) : + MemLLogL (fun x => c*f x) := by + let g (x : Torus) := c*f x + have hi : AEStronglyMeasurable g circleMeasure := hp.1.const_mul _ + obtain ⟨v,hv,ht⟩ := scale_admit ‖c‖ (norm_nonneg _) + refine ⟨hi,?_⟩ + change Integrable (Young g) circleMeasure + apply ((show Integrable (Young f) _ from hp.2).const_mul v).mono' (young_meas hi) + apply ae_of_all + intro x + change ‖Young g x‖ ≤ _ + rw [Real.norm_eq_abs,abs_of_nonneg (show 0 ≤ Young g x from p_nonneg _ (norm_nonneg _))] + unfold Young g; rw [norm_mul]; apply ht; exact norm_nonneg _ + +lemma mod_unit (f : Torus → ℂ) : + modBill f 1 = ∫ x, Young f x ∂circleMeasure := by simp only [modBill,div_one]; rfl + +lemma sum_inside {ι : Type*} [Fintype ι] [Nonempty ι] + (f : ι → Torus → ℂ) (h : ∀ i, inside (f i)) : + let a : ℝ := Fintype.card ι + inside (fun x => a⁻¹ • ∑ i, f i x) := by + intro a + let g (x : Torus) : ℂ := a⁻¹ • ∑ i, f i x + let F (x : Torus) := a⁻¹ * ∑ i, Young (f i) x + have ha : 0 < a := by unfold a; positivity + have he (i) : Integrable (Young (f i)) circleMeasure := (h i).1.2 + have HH : AEStronglyMeasurable g circleMeasure := by + unfold g + have HH := Finset.aestronglyMeasurable_fun_sum Finset.univ (fun (i:ι) _ => (h i).1.1) + exact HH.const_smul (a⁻¹) + have HI : Integrable F circleMeasure := (integrable_finsetSum _ fun i _ => he i).const_mul _ + have HX (x : Torus) : Young g x ≤ F x := price_convex fun i => f i x + have HV : Integrable (Young g) circleMeasure := + HI.mono' (young_meas HH) (ae_of_all _ fun x => by + rw [Real.norm_eq_abs,abs_of_nonneg (show 0 ≤ Young g x from p_nonneg _ (norm_nonneg _))] + exact HX x) + refine ⟨⟨HH,HV⟩,?_⟩ + rw [mod_unit] + apply (integral_mono HV HI HX).trans + unfold F; rw [integral_const_mul,integral_finsetSum _ (fun i _ => he i)] + rw [←div_eq_inv_mul] + apply (div_le_one ha).mpr + have Hz (i) : ∫ x,Young (f i) x ∂circleMeasure ≤ 1 := by rw [←mod_unit]; exact (h i).2 + exact le_trans (Finset.sum_le_sum (fun i _ => Hz i)) (by simp [a]) +end Weak +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Quantile.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Quantile.lean index cef79334e5c771a176c2e13f4c6101552d892830..77e8943203c6ac56c432ba9604d10a596a0bc6e8 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Quantile.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Quantile.lean @@ -1,139 +1,139 @@ -import OAI.Analysis.FourierLLogL.Weak.Layers - -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL -def cloudSet (f : Torus → ℂ) (th : ℝ) := - {x : Torus | ENNReal.ofReal th < symmetricMaximal f x} - -namespace Weak -open Real -open scoped Classical -variable {f : Torus → ℂ} -lemma cloud_meas (f : Torus → ℂ) (s : ℝ) : MeasurableSet (cloudSet f s) := - measurableSet_lt measurable_const (measurable_symmetricMaximal f) - -lemma cloudy (f : Torus → ℂ) {s : ℝ} (hs : 0 ≤ s) (x : Torus) : - x ∈ cloudSet f s ↔ ∃ N, s < ‖symmetricPartialSum f N x‖ := by - change _ < ⨆ N, ENNReal.ofReal ‖symmetricPartialSum f N x‖ ↔ _ - rw [lt_iSup_iff] - simp only [ENNReal.ofReal_lt_ofReal_iff_of_nonneg hs] - -private lemma good_law (h : GoodBound) : - ∃ (d : ℝ), 0 ≤ d ∧ ∃ (L : ℕ), 4 ≤ L ∧ - (∑' (n : ℕ), ENNReal.ofReal (d/((n+L:ℕ)+1:ℝ)^2)) ≤ ENNReal.ofReal (1/32:ℝ) ∧ - ∀ (f : Torus → ℂ), inside f → ∀ n : ℕ, ∃ G : Set Torus, MeasurableSet G ∧ - circleMeasure Gᶜ ≤ ENNReal.ofReal (d / ((n+L:ℕ)+1:ℝ)^2) ∧ - (∫⁻ x in G, symmetricMaximal (piece f n) x ∂circleMeasure) ≤ - ENNReal.ofReal (allowance f n * (d * (2:ℝ)^(n+L))) := by - classical - obtain ⟨d,hd,h⟩ := h - obtain ⟨N,hN⟩ := eventually_atTop.1 h - let p (i : ℕ) := d/ ((i+1:ℝ)^2) - have he : Summable p := by - have hh : Summable (fun i : ℕ => 1 / ((i+1:ℕ):ℝ)^2) := - (summable_nat_add_iff 1).mpr (Real.summable_one_div_nat_pow.mpr (by norm_num)) - simpa [p,div_eq_mul_inv] using hh.mul_left d - let P i := ENNReal.ofReal (p i) - have hP (i : ℕ) : 0 ≤ p i := by unfold p; positivity - have hp : (∑' i,P i) ≠ ∞ := by unfold P; rw [← ENNReal.ofReal_tsum_of_nonneg hP he]; exact ENNReal.ofReal_ne_top - have HR := (ENNReal.tendsto_sum_nat_add P hp).eventually - (gt_mem_nhds (show (0:ℝ≥0∞) < ENNReal.ofReal (1/32:ℝ) by positivity)) - obtain ⟨L,hH,hL⟩ := (HR.and (eventually_ge_atTop (N+4))).exists - refine ⟨d, hd,L,by omega,hH.le,?_⟩ - intro f hh n - let a := allowance f n; let q := piece f n; let k := n+L - obtain ⟨ha,hq,hc,hG⟩ := taxpayer hh n - change 0 by - rw [hv,←div_eq_inv_mul] - apply (div_le_iff₀ ha).mpr - rw [mul_comm] - apply (hG x).trans; unfold k; apply mul_le_mul_of_nonneg_left _ ha.le; apply exp_monotone; apply pow_le_pow_right₀ (by norm_num); omega - obtain ⟨G,hM,hz,hT⟩ := hN k (by unfold k; omega) g (hq.const_mul _) hs ht - refine ⟨G,hM,hz,?_⟩ - have H : piece f n = (fun x => (a:ℂ)*g x) := by - funext x; unfold g q; rw [←mul_assoc,←Complex.ofReal_mul,mul_inv_cancel₀ ha.ne', - Complex.ofReal_one,one_mul] - rw [H] - simp_rw [symmetricMaximal_mul,Complex.norm_real,Real.norm_eq_abs] - rw [abs_of_pos ha,lintegral_const_mul _ (measurable_symmetricMaximal g), - ENNReal.ofReal_mul ha.le] - exact mul_le_mul_right hT _ - -lemma median_budget (hp : GoodBound) : - ∃ L : ℝ, 0 < L ∧ ∀ (f : Torus → ℂ), inside f → circleMeasure.real (cloudSet f L) ≤ 1/16 := by - classical - obtain ⟨d,hd,n,hn,hh,h⟩ := good_law hp - let k := d*(2:ℝ)^n - let T := k*tollMax - have hK : 0 ≤ k := by unfold k; positivity - have hT : 0 ≤ T := mul_nonneg hK (by - unfold tollMax - have H : (0:ℝ) ≤ ∑' i, (2:ℝ)^(i+1)*grit i := - tsum_nonneg fun _ => mul_nonneg (by positivity) (grit_pos _).le - linarith [exp_pos 2]) - let L := 32*(T+1) - have hL : 0 < L := by unfold L; linarith - refine ⟨L,hL,?_⟩ - intro f hf - choose G hG hx hM using h f hf - let g (j : ℕ) := (G j).indicator (symmetricMaximal (piece f j)) - let E := ⋂ j, G j - let q (x) := ∑' j,g j x - let μ := circleMeasure - have hq j : Measurable (g j) := (measurable_symmetricMaximal _).indicator (hG _) - have hi : (∫⁻ x,q x ∂circleMeasure) ≤ ENNReal.ofReal T := by - obtain ⟨hS,hV⟩ := toll_bound hf - unfold q - rw [lintegral_tsum (fun j => (hq j).aemeasurable)] - have HC (j) : (∫⁻ x,g j x ∂circleMeasure) ≤ ENNReal.ofReal (k*((2:ℝ)^j*allowance f j)) := by - unfold g; rw [lintegral_indicator (hG j)] - apply le_trans (hM j) - unfold k; rw [pow_add]; exact ENNReal.ofReal_le_ofReal (le_of_eq (by ring)) - apply (ENNReal.tsum_le_tsum HC).trans - have HT (i : ℕ) : 0 ≤ k*((2:ℝ)^i*allowance f i) := by - have hi := (taxpayer hf i).1; change 0 < allowance f i at hi; positivity - rw [←ENNReal.ofReal_tsum_of_nonneg HT (hS.mul_left _), hS.tsum_mul_left] - exact ENNReal.ofReal_le_ofReal (mul_le_mul_of_nonneg_left hV hK) - have HG : μ Eᶜ ≤ ENNReal.ofReal (1/32:ℝ) := by - unfold E; rw [compl_iInter] - exact (measure_iUnion_le _).trans ((ENNReal.tsum_le_tsum hx).trans hh) - let X := {x : Torus | ENNReal.ofReal L ≤ q x} - have HA : μ X ≤ ENNReal.ofReal (1/32:ℝ) := by - have Hp : 0 < ENNReal.ofReal L := by positivity - have H := mul_meas_ge_le_lintegral₀ (μ:=circleMeasure) (show AEMeasurable (fun x => ∑' i,g i x) circleMeasure from AEMeasurable.tsum (fun i => (hq i).aemeasurable)) (ENNReal.ofReal L) - apply (ENNReal.mul_le_mul_iff_right Hp.ne' (show ENNReal.ofReal L ≠ ∞ from ENNReal.ofReal_ne_top)).mp - apply le_trans (H.trans hi) - rw [← ENNReal.ofReal_mul hL.le] - exact ENNReal.ofReal_le_ofReal (by unfold L; linarith) - have HV : cloudSet f L ⊆ Eᶜ∪X := by - intro x he - rcases (cloudy f hL.le x).1 he with ⟨i,hi⟩ - by_cases h : x∈E - · right - have hq (j) : g j x=symmetricMaximal (piece f j) x := Set.indicator_of_mem (mem_iInter.mp h j) _ - apply le_trans (ENNReal.ofReal_le_ofReal hi.le) (le_trans (paid_transform hf x i) _) - change _ ≤ ∑' j,g j x; simp_rw [hq] - exact le_rfl - left; exact h - have He : μ (cloudSet f L) ≤ ENNReal.ofReal (1/16:ℝ) := by - apply le_trans ((measure_mono HV).trans (measure_union_le _ _)) ((add_le_add HG HA).trans _) - rw [←ENNReal.ofReal_add (by positivity) (by positivity)] - exact ENNReal.ofReal_le_ofReal (by linarith) - have H := ENNReal.toReal_mono ENNReal.ofReal_ne_top He - rw [ENNReal.toReal_ofReal (by positivity)] at H; exact H - -end Weak -end FourierLLogL - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Layers + +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL +def cloudSet (f : Torus → ℂ) (th : ℝ) := + {x : Torus | ENNReal.ofReal th < symmetricMaximal f x} + +namespace Weak +open Real +open scoped Classical +variable {f : Torus → ℂ} +lemma cloud_meas (f : Torus → ℂ) (s : ℝ) : MeasurableSet (cloudSet f s) := + measurableSet_lt measurable_const (measurable_symmetricMaximal f) + +lemma cloudy (f : Torus → ℂ) {s : ℝ} (hs : 0 ≤ s) (x : Torus) : + x ∈ cloudSet f s ↔ ∃ N, s < ‖symmetricPartialSum f N x‖ := by + change _ < ⨆ N, ENNReal.ofReal ‖symmetricPartialSum f N x‖ ↔ _ + rw [lt_iSup_iff] + simp only [ENNReal.ofReal_lt_ofReal_iff_of_nonneg hs] + +private lemma good_law (h : GoodBound) : + ∃ (d : ℝ), 0 ≤ d ∧ ∃ (L : ℕ), 4 ≤ L ∧ + (∑' (n : ℕ), ENNReal.ofReal (d/((n+L:ℕ)+1:ℝ)^2)) ≤ ENNReal.ofReal (1/32:ℝ) ∧ + ∀ (f : Torus → ℂ), inside f → ∀ n : ℕ, ∃ G : Set Torus, MeasurableSet G ∧ + circleMeasure Gᶜ ≤ ENNReal.ofReal (d / ((n+L:ℕ)+1:ℝ)^2) ∧ + (∫⁻ x in G, symmetricMaximal (piece f n) x ∂circleMeasure) ≤ + ENNReal.ofReal (allowance f n * (d * (2:ℝ)^(n+L))) := by + classical + obtain ⟨d,hd,h⟩ := h + obtain ⟨N,hN⟩ := eventually_atTop.1 h + let p (i : ℕ) := d/ ((i+1:ℝ)^2) + have he : Summable p := by + have hh : Summable (fun i : ℕ => 1 / ((i+1:ℕ):ℝ)^2) := + (summable_nat_add_iff 1).mpr (Real.summable_one_div_nat_pow.mpr (by norm_num)) + simpa [p,div_eq_mul_inv] using hh.mul_left d + let P i := ENNReal.ofReal (p i) + have hP (i : ℕ) : 0 ≤ p i := by unfold p; positivity + have hp : (∑' i,P i) ≠ ∞ := by unfold P; rw [← ENNReal.ofReal_tsum_of_nonneg hP he]; exact ENNReal.ofReal_ne_top + have HR := (ENNReal.tendsto_sum_nat_add P hp).eventually + (gt_mem_nhds (show (0:ℝ≥0∞) < ENNReal.ofReal (1/32:ℝ) by positivity)) + obtain ⟨L,hH,hL⟩ := (HR.and (eventually_ge_atTop (N+4))).exists + refine ⟨d, hd,L,by omega,hH.le,?_⟩ + intro f hh n + let a := allowance f n; let q := piece f n; let k := n+L + obtain ⟨ha,hq,hc,hG⟩ := taxpayer hh n + change 0 by + rw [hv,←div_eq_inv_mul] + apply (div_le_iff₀ ha).mpr + rw [mul_comm] + apply (hG x).trans; unfold k; apply mul_le_mul_of_nonneg_left _ ha.le; apply exp_monotone; apply pow_le_pow_right₀ (by norm_num); omega + obtain ⟨G,hM,hz,hT⟩ := hN k (by unfold k; omega) g (hq.const_mul _) hs ht + refine ⟨G,hM,hz,?_⟩ + have H : piece f n = (fun x => (a:ℂ)*g x) := by + funext x; unfold g q; rw [←mul_assoc,←Complex.ofReal_mul,mul_inv_cancel₀ ha.ne', + Complex.ofReal_one,one_mul] + rw [H] + simp_rw [symmetricMaximal_mul,Complex.norm_real,Real.norm_eq_abs] + rw [abs_of_pos ha,lintegral_const_mul _ (measurable_symmetricMaximal g), + ENNReal.ofReal_mul ha.le] + exact mul_le_mul_right hT _ + +lemma median_budget (hp : GoodBound) : + ∃ L : ℝ, 0 < L ∧ ∀ (f : Torus → ℂ), inside f → circleMeasure.real (cloudSet f L) ≤ 1/16 := by + classical + obtain ⟨d,hd,n,hn,hh,h⟩ := good_law hp + let k := d*(2:ℝ)^n + let T := k*tollMax + have hK : 0 ≤ k := by unfold k; positivity + have hT : 0 ≤ T := mul_nonneg hK (by + unfold tollMax + have H : (0:ℝ) ≤ ∑' i, (2:ℝ)^(i+1)*grit i := + tsum_nonneg fun _ => mul_nonneg (by positivity) (grit_pos _).le + linarith [exp_pos 2]) + let L := 32*(T+1) + have hL : 0 < L := by unfold L; linarith + refine ⟨L,hL,?_⟩ + intro f hf + choose G hG hx hM using h f hf + let g (j : ℕ) := (G j).indicator (symmetricMaximal (piece f j)) + let E := ⋂ j, G j + let q (x) := ∑' j,g j x + let μ := circleMeasure + have hq j : Measurable (g j) := (measurable_symmetricMaximal _).indicator (hG _) + have hi : (∫⁻ x,q x ∂circleMeasure) ≤ ENNReal.ofReal T := by + obtain ⟨hS,hV⟩ := toll_bound hf + unfold q + rw [lintegral_tsum (fun j => (hq j).aemeasurable)] + have HC (j) : (∫⁻ x,g j x ∂circleMeasure) ≤ ENNReal.ofReal (k*((2:ℝ)^j*allowance f j)) := by + unfold g; rw [lintegral_indicator (hG j)] + apply le_trans (hM j) + unfold k; rw [pow_add]; exact ENNReal.ofReal_le_ofReal (le_of_eq (by ring)) + apply (ENNReal.tsum_le_tsum HC).trans + have HT (i : ℕ) : 0 ≤ k*((2:ℝ)^i*allowance f i) := by + have hi := (taxpayer hf i).1; change 0 < allowance f i at hi; positivity + rw [←ENNReal.ofReal_tsum_of_nonneg HT (hS.mul_left _), hS.tsum_mul_left] + exact ENNReal.ofReal_le_ofReal (mul_le_mul_of_nonneg_left hV hK) + have HG : μ Eᶜ ≤ ENNReal.ofReal (1/32:ℝ) := by + unfold E; rw [compl_iInter] + exact (measure_iUnion_le _).trans ((ENNReal.tsum_le_tsum hx).trans hh) + let X := {x : Torus | ENNReal.ofReal L ≤ q x} + have HA : μ X ≤ ENNReal.ofReal (1/32:ℝ) := by + have Hp : 0 < ENNReal.ofReal L := by positivity + have H := mul_meas_ge_le_lintegral₀ (μ:=circleMeasure) (show AEMeasurable (fun x => ∑' i,g i x) circleMeasure from AEMeasurable.tsum (fun i => (hq i).aemeasurable)) (ENNReal.ofReal L) + apply (ENNReal.mul_le_mul_iff_right Hp.ne' (show ENNReal.ofReal L ≠ ∞ from ENNReal.ofReal_ne_top)).mp + apply le_trans (H.trans hi) + rw [← ENNReal.ofReal_mul hL.le] + exact ENNReal.ofReal_le_ofReal (by unfold L; linarith) + have HV : cloudSet f L ⊆ Eᶜ∪X := by + intro x he + rcases (cloudy f hL.le x).1 he with ⟨i,hi⟩ + by_cases h : x∈E + · right + have hq (j) : g j x=symmetricMaximal (piece f j) x := Set.indicator_of_mem (mem_iInter.mp h j) _ + apply le_trans (ENNReal.ofReal_le_ofReal hi.le) (le_trans (paid_transform hf x i) _) + change _ ≤ ∑' j,g j x; simp_rw [hq] + exact le_rfl + left; exact h + have He : μ (cloudSet f L) ≤ ENNReal.ofReal (1/16:ℝ) := by + apply le_trans ((measure_mono HV).trans (measure_union_le _ _)) ((add_le_add HG HA).trans _) + rw [←ENNReal.ofReal_add (by positivity) (by positivity)] + exact ENNReal.ofReal_le_ofReal (by linarith) + have H := ENNReal.toReal_mono ENNReal.ofReal_ne_top He + rw [ENNReal.toReal_ofReal (by positivity)] at H; exact H + +end Weak +end FourierLLogL + +end OAI diff --git a/lean/OAI/Analysis/FourierLLogL/Weak/Signed.lean b/lean/OAI/Analysis/FourierLLogL/Weak/Signed.lean index 3a57c885aa8c6b2bfdf66730489b221a4aee0867..ae4ce891709e7365cf3d91aa07aa026eac25564f 100644 --- a/lean/OAI/Analysis/FourierLLogL/Weak/Signed.lean +++ b/lean/OAI/Analysis/FourierLLogL/Weak/Signed.lean @@ -1,209 +1,209 @@ -import OAI.Analysis.FourierLLogL.Weak.Cover - -noncomputable section -open MeasureTheory Filter Set Function -open scoped ENNReal BigOperators Topology - -namespace OAI - -namespace FourierLLogL.Weak -open Real -open scoped Classical -variable {f : Torus → ℂ} {n : ℕ} {w : ℝ} - -def drag (z : Torus) (f : Torus → ℂ) (x : Torus) := f (z+x) - -lemma translated_bill (f : Torus → ℂ) (z : Torus) : - modBill (drag z f) w = modBill f w := by - unfold modBill drag circleMeasure - exact integral_add_left_eq_self (fun x : Torus => priceΦ (‖f x‖/w)) z - -lemma translated_in (z : Torus) (h : inside f) : inside (drag z f) := by - have HE := flow z - have hi (g : Torus → ℂ) (h : Integrable g law) : - Integrable (fun i => g (z+i)) law := (HE.integrable_comp_of_integrable (g:=g) h) - refine ⟨⟨(hi f h.1.integrable).1,?_⟩,?_⟩ - exact HE.integrable_comp_of_integrable (g:=Young f) (show Integrable (Young f) law from h.1.2) - rw [translated_bill]; exact h.2 - -lemma fourier_drag (k : ℤ) (x y : Torus) : - fourier k (x+y)=fourier k x * fourier k y := by - simp_rw [fourier_apply,smul_add,AddCircle.toCircle_add,Circle.coe_mul] - -lemma translated_sum (z x : Torus) (f : Torus→ℂ) (N : ℕ) : - symmetricPartialSum (drag z f) N x=symmetricPartialSum f N (z+x) := by - have HE (k : ℤ) : fourierCoeff (drag z f) k = fourier k z * fourierCoeff f k := by - simp_rw [fourierCoeff_eq_integral] - let w := fun i => f i*fourier (-k) i - have H (i) : drag z f i*fourier (-k) i=fourier k z * w (z+i) := by - unfold drag w; rw [fourier_drag] - have HS : fourier k z*fourier (-k) z=1 := by - rw [← fourier_add]; simp - calc - _ = f (z+i)*fourier (-k) i * (fourier k z*fourier (-k) z) := by rw [HS]; ring - _ = _ := by ring - change (∫ i,_ ∂law) = fourier k z* ∫ i,w i ∂law - simp_rw [H]; rw [integral_const_mul] - unfold law circleMeasure; rw [integral_add_left_eq_self] - unfold symmetricPartialSum - exact Finset.sum_congr rfl fun i _ => by rw [HE, fourier_drag]; ring - -abbrev Toss (n : ℕ) := Fin n → Bool - -def mark (b : Bool) : ℂ := if b then -1 else 1 -lemma norm_mark (b) : ‖mark b‖=1 := by cases b <;> norm_num [mark] -def slate (f : Torus→ℂ) (z : Fin n → Torus) (c : Toss n) := - fun (x : Torus) => (n:ℝ)⁻¹ • ∑ i, mark (c i)*drag (z i) f x - -lemma family_in (f : Torus→ℂ) (z : Fin n→ Torus) (h : inside f) (hp : 0 < n) - (c : Toss n) : inside (slate f z c) := by - let : NeZero n := ⟨hp.ne'⟩ - let g (i : Fin n) x := mark (c i)*drag (z i) f x - have H (i : Fin n) : inside (g i) := by - have he := translated_in (z i) h - refine ⟨mem_dilate _ he.1,?_⟩ - have HE : modBill (g i) 1=modBill (drag (z i) f) 1 := by - unfold modBill g; simp only [norm_mul,norm_mark,one_mul] - rw [HE]; exact he.2 - have HZ := sum_inside g H - simp only [Fintype.card_fin] at HZ - exact HZ - -lemma symm_sum (s : Finset (Fin n)) (f : Fin n→Torus→ℂ) - (h : ∀ i, Integrable (f i) circleMeasure) (k : ℕ) (x : Torus) : - symmetricPartialSum (fun y => ∑ i∈s,f i y) k x= - ∑ i∈s,symmetricPartialSum (f i) k x := by - induction s using Finset.induction_on with - | empty => simp - | @insert i t hi ih => - simp only [Finset.sum_insert hi] - change symmetricPartialSum (f i+(fun y => ∑ j∈t,f j y)) _ _=_ - rw [symmetricPartialSum_add (h i) (integrable_finsetSum _ (fun i _ => h i)),ih] - -lemma slate_sum (z : Fin n→Torus) (x : Torus) (h : inside f) (k : ℕ) (c : Toss n) : - symmetricPartialSum (slate f z c) k x = - (((n:ℝ)⁻¹:ℝ):ℂ)*∑ i,mark (c i)*symmetricPartialSum f k (z i+x) := by - have HF (i : Fin n) : Integrable (drag (z i) f) circleMeasure := - (translated_in _ h).1.integrable - let g (i : Fin n) (x) := mark (c i)*drag (z i) f x - unfold slate - simp_rw [Complex.real_smul] - rw [symmetricPartialSum_const_mul,symm_sum _ g (fun i => (HF i).const_mul _)] - unfold g - simp_rw [symmetricPartialSum_const_mul,translated_sum] - -def switch (i : Fin n) : Toss n ≃ Toss n := - let w (f : Toss n) (x : Fin n) := if x=i then !f i else f x - let he : LeftInverse w w := fun c => by - funext p; by_cases h : p=i <;> simp [w,h] - ⟨w,w,he,he⟩ - -lemma flipsum_bill (g : Fin n → ℂ) (j : Fin n) (c : Toss n) : - let f (c : Toss n) := ∑ i,mark (c i)*g i - f c-f (switch j c)=2*mark (c j)*g j := by - intro f; unfold f - rw [← Finset.sum_sub_distrib, Finset.sum_eq_single j] - · cases h : c j <;> simp only [switch,Equiv.coe_fn_mk,ite_true,h] <;> - norm_num [mark] <;> ring - · intro b _ h - simp [switch, h] - simp - -def appears (s : Set Torus) (x : Torus) := if x∈s then (1:ℝ) else 0 -lemma signed_window (h : inside f) (p : ℝ) (hp : 0 < p) - (z : Fin n → Torus) (hn : 0 < n) (x : Torus) : - let q := fun c : Toss n => cloudSet (slate f z c) (p/n) - (Fintype.card (Toss n):ℝ)*appears (spread (cloudSet f p) z) x ≤ - 2 * ∑ c,appears (q c) x := by - intro q - have HH (c) : 0 ≤ appears (q c) x := by unfold appears; split <;> norm_num - by_cases hM : x∈spread (cloudSet f p) z - · rw [show appears _ x=1 from ite_eq_left hM,mul_one] - obtain ⟨j,hb⟩ := hM - obtain ⟨N,hX⟩ := (cloudy f hp.le (z j+x)).mp hb - let B := fun i : Fin n => symmetricPartialSum f N (z i+x) - let C (c : Toss n) := ∑ i,mark (c i)*B i - let a := (n:ℝ) - have ha : 0 < a := by unfold a; positivity - have HE (c : Toss n) (hx : p < ‖C c‖) : appears (q c) x=1 := by - refine ite_eq_left ?_ - refine (cloudy _ (show 0 ≤ p/a by positivity) x).2 ⟨N,?_⟩ - rw [slate_sum z x h, norm_mul,Complex.norm_real,Real.norm_eq_abs] - change p/a < |a⁻¹| * ‖C c‖ - rw [abs_of_pos (inv_pos.2 ha),←div_eq_inv_mul] - exact (div_lt_div_iff_of_pos_right ha).mpr hx - have HQ (c : Toss n) : 1 ≤ appears (q c) x + appears (q (switch j c)) x := by - by_cases hh : p < ‖C c‖ - · rw [HE c hh]; linarith [HH (switch j c)] - have Ht := norm_sub_le (C c) (C (switch j c)) - have hJ : ‖C c-C (switch j c)‖ = 2*‖B j‖ := by - rw [show C c-C (switch j c)=_ from flipsum_bill B j c] - simp only [norm_mul,norm_mark]; norm_num - rw [hJ] at Ht - change p < ‖B j‖ at hX - have hs : p < ‖C (switch j c)‖ := by linarith - rw [HE _ hs]; linarith [HH c] - let A (c : Toss n) := appears (q c) x - have H : ∑ _c : Toss n,(1:ℝ) ≤ ∑ c,(A c+A (switch j c)) := - Finset.sum_le_sum fun i _ => HQ i - have Hv : ∑ c,A (switch j c) = ∑ c,A c := - Fintype.sum_equiv (switch j) _ _ fun _ => rfl - rw [Finset.sum_add_distrib,Hv] at H; simp only [Finset.sum_const,nsmul_eq_mul,mul_one,Finset.card_univ] at H - change _ ≤ 2*∑ c,A c; linarith - · rw [show appears _ x=0 from ite_eq_right hM,mul_zero] - exact mul_nonneg (by norm_num) (Finset.sum_nonneg fun i _ => HH i) - -lemma occurs_meas (S : Set Torus) (h : MeasurableSet S) : - Integrable (appears S) law ∧ (∫ x,appears S x ∂law)=law.real S := by - rw [show appears S=S.indicator (fun _ => (1:ℝ)) by - funext x; by_cases h : x∈S <;> simp [appears,h]] - refine ⟨(integrable_const _).indicator h,?_⟩; rw [integral_indicator h]; simp - -lemma circle_bill (hp : GoodBound) : - ∃ v : ℝ, 0 < v ∧ ∀ f : Torus→ℂ, inside f → - ∀ p : ℝ, 0 < p → p*law.real (cloudSet f p) ≤ v := by - obtain ⟨L,hL,hF⟩ := median_budget hp - refine ⟨4*L,by positivity,?_⟩ - intro f h p hp - let S := cloudSet f p - have hS := cloud_meas f p - rcases eq_or_lt_of_le (show 0 ≤ law.real S from measureReal_nonneg) with hr|hr - · change _*law.real S ≤ _ - rw [←hr,mul_zero]; positivity - obtain ⟨n,hn,z,hz,hv⟩ := clouds_fill S hS hr - let m := (n:ℝ) - have he : 0 < m := by unfold m; positivity - suffices H : p/m ≤ L by - change p*law.real S ≤ _ - rw [div_le_iff₀ he] at H - change m*law.real S ≤ _ at hv - nlinarith - by_contra! H - let q (c : Toss n) := cloudSet (slate f z c) (p/m) - let k := (Fintype.card (Toss n) : ℝ) - have HH (c : Toss n) : law.real (q c) ≤ 1/16 := by - apply le_trans _ (hF _ (family_in f z h hn c)) - refine measureReal_mono ?_ (by finiteness) - intro x hx - exact lt_of_le_of_lt (ENNReal.ofReal_le_ofReal H.le) hx - let b := spread S z - have hB := occurs_meas b (spread_meas S z hS) - have hQ (c) := occurs_meas (q c) (cloud_meas _ _) - have HE : k*law.real b ≤ 2*∑ c,law.real (q c) := by - rw [← hB.2,←integral_const_mul] - simp_rw [← (hQ _).2] - rw [← integral_finsetSum _ (fun i _ => (hQ i).1), - ← integral_const_mul] - apply integral_mono (hB.1.const_mul _) ((integrable_finsetSum _ fun i _ => (hQ i).1).const_mul _) - exact signed_window h p hp z hn - have Hx : ∑ c,law.real (q c) ≤ k*(1/16) := by - calc - _ ≤ ∑ c : Toss n, (1/16:ℝ) := Finset.sum_le_sum fun i _ => HH i - _ = _ := by simp [k] - have HE' : 0 < k := by unfold k; positivity - change 1/2 ≤ law.real b at hz - nlinarith - -end FourierLLogL.Weak - -end OAI +import OAI.Analysis.FourierLLogL.Weak.Cover + +noncomputable section +open MeasureTheory Filter Set Function +open scoped ENNReal BigOperators Topology + +namespace OAI + +namespace FourierLLogL.Weak +open Real +open scoped Classical +variable {f : Torus → ℂ} {n : ℕ} {w : ℝ} + +def drag (z : Torus) (f : Torus → ℂ) (x : Torus) := f (z+x) + +lemma translated_bill (f : Torus → ℂ) (z : Torus) : + modBill (drag z f) w = modBill f w := by + unfold modBill drag circleMeasure + exact integral_add_left_eq_self (fun x : Torus => priceΦ (‖f x‖/w)) z + +lemma translated_in (z : Torus) (h : inside f) : inside (drag z f) := by + have HE := flow z + have hi (g : Torus → ℂ) (h : Integrable g law) : + Integrable (fun i => g (z+i)) law := (HE.integrable_comp_of_integrable (g:=g) h) + refine ⟨⟨(hi f h.1.integrable).1,?_⟩,?_⟩ + exact HE.integrable_comp_of_integrable (g:=Young f) (show Integrable (Young f) law from h.1.2) + rw [translated_bill]; exact h.2 + +lemma fourier_drag (k : ℤ) (x y : Torus) : + fourier k (x+y)=fourier k x * fourier k y := by + simp_rw [fourier_apply,smul_add,AddCircle.toCircle_add,Circle.coe_mul] + +lemma translated_sum (z x : Torus) (f : Torus→ℂ) (N : ℕ) : + symmetricPartialSum (drag z f) N x=symmetricPartialSum f N (z+x) := by + have HE (k : ℤ) : fourierCoeff (drag z f) k = fourier k z * fourierCoeff f k := by + simp_rw [fourierCoeff_eq_integral] + let w := fun i => f i*fourier (-k) i + have H (i) : drag z f i*fourier (-k) i=fourier k z * w (z+i) := by + unfold drag w; rw [fourier_drag] + have HS : fourier k z*fourier (-k) z=1 := by + rw [← fourier_add]; simp + calc + _ = f (z+i)*fourier (-k) i * (fourier k z*fourier (-k) z) := by rw [HS]; ring + _ = _ := by ring + change (∫ i,_ ∂law) = fourier k z* ∫ i,w i ∂law + simp_rw [H]; rw [integral_const_mul] + unfold law circleMeasure; rw [integral_add_left_eq_self] + unfold symmetricPartialSum + exact Finset.sum_congr rfl fun i _ => by rw [HE, fourier_drag]; ring + +abbrev Toss (n : ℕ) := Fin n → Bool + +def mark (b : Bool) : ℂ := if b then -1 else 1 +lemma norm_mark (b) : ‖mark b‖=1 := by cases b <;> norm_num [mark] +def slate (f : Torus→ℂ) (z : Fin n → Torus) (c : Toss n) := + fun (x : Torus) => (n:ℝ)⁻¹ • ∑ i, mark (c i)*drag (z i) f x + +lemma family_in (f : Torus→ℂ) (z : Fin n→ Torus) (h : inside f) (hp : 0 < n) + (c : Toss n) : inside (slate f z c) := by + let : NeZero n := ⟨hp.ne'⟩ + let g (i : Fin n) x := mark (c i)*drag (z i) f x + have H (i : Fin n) : inside (g i) := by + have he := translated_in (z i) h + refine ⟨mem_dilate _ he.1,?_⟩ + have HE : modBill (g i) 1=modBill (drag (z i) f) 1 := by + unfold modBill g; simp only [norm_mul,norm_mark,one_mul] + rw [HE]; exact he.2 + have HZ := sum_inside g H + simp only [Fintype.card_fin] at HZ + exact HZ + +lemma symm_sum (s : Finset (Fin n)) (f : Fin n→Torus→ℂ) + (h : ∀ i, Integrable (f i) circleMeasure) (k : ℕ) (x : Torus) : + symmetricPartialSum (fun y => ∑ i∈s,f i y) k x= + ∑ i∈s,symmetricPartialSum (f i) k x := by + induction s using Finset.induction_on with + | empty => simp + | @insert i t hi ih => + simp only [Finset.sum_insert hi] + change symmetricPartialSum (f i+(fun y => ∑ j∈t,f j y)) _ _=_ + rw [symmetricPartialSum_add (h i) (integrable_finsetSum _ (fun i _ => h i)),ih] + +lemma slate_sum (z : Fin n→Torus) (x : Torus) (h : inside f) (k : ℕ) (c : Toss n) : + symmetricPartialSum (slate f z c) k x = + (((n:ℝ)⁻¹:ℝ):ℂ)*∑ i,mark (c i)*symmetricPartialSum f k (z i+x) := by + have HF (i : Fin n) : Integrable (drag (z i) f) circleMeasure := + (translated_in _ h).1.integrable + let g (i : Fin n) (x) := mark (c i)*drag (z i) f x + unfold slate + simp_rw [Complex.real_smul] + rw [symmetricPartialSum_const_mul,symm_sum _ g (fun i => (HF i).const_mul _)] + unfold g + simp_rw [symmetricPartialSum_const_mul,translated_sum] + +def switch (i : Fin n) : Toss n ≃ Toss n := + let w (f : Toss n) (x : Fin n) := if x=i then !f i else f x + let he : LeftInverse w w := fun c => by + funext p; by_cases h : p=i <;> simp [w,h] + ⟨w,w,he,he⟩ + +lemma flipsum_bill (g : Fin n → ℂ) (j : Fin n) (c : Toss n) : + let f (c : Toss n) := ∑ i,mark (c i)*g i + f c-f (switch j c)=2*mark (c j)*g j := by + intro f; unfold f + rw [← Finset.sum_sub_distrib, Finset.sum_eq_single j] + · cases h : c j <;> simp only [switch,Equiv.coe_fn_mk,ite_true,h] <;> + norm_num [mark] <;> ring + · intro b _ h + simp [switch, h] + simp + +def appears (s : Set Torus) (x : Torus) := if x∈s then (1:ℝ) else 0 +lemma signed_window (h : inside f) (p : ℝ) (hp : 0 < p) + (z : Fin n → Torus) (hn : 0 < n) (x : Torus) : + let q := fun c : Toss n => cloudSet (slate f z c) (p/n) + (Fintype.card (Toss n):ℝ)*appears (spread (cloudSet f p) z) x ≤ + 2 * ∑ c,appears (q c) x := by + intro q + have HH (c) : 0 ≤ appears (q c) x := by unfold appears; split <;> norm_num + by_cases hM : x∈spread (cloudSet f p) z + · rw [show appears _ x=1 from ite_eq_left hM,mul_one] + obtain ⟨j,hb⟩ := hM + obtain ⟨N,hX⟩ := (cloudy f hp.le (z j+x)).mp hb + let B := fun i : Fin n => symmetricPartialSum f N (z i+x) + let C (c : Toss n) := ∑ i,mark (c i)*B i + let a := (n:ℝ) + have ha : 0 < a := by unfold a; positivity + have HE (c : Toss n) (hx : p < ‖C c‖) : appears (q c) x=1 := by + refine ite_eq_left ?_ + refine (cloudy _ (show 0 ≤ p/a by positivity) x).2 ⟨N,?_⟩ + rw [slate_sum z x h, norm_mul,Complex.norm_real,Real.norm_eq_abs] + change p/a < |a⁻¹| * ‖C c‖ + rw [abs_of_pos (inv_pos.2 ha),←div_eq_inv_mul] + exact (div_lt_div_iff_of_pos_right ha).mpr hx + have HQ (c : Toss n) : 1 ≤ appears (q c) x + appears (q (switch j c)) x := by + by_cases hh : p < ‖C c‖ + · rw [HE c hh]; linarith [HH (switch j c)] + have Ht := norm_sub_le (C c) (C (switch j c)) + have hJ : ‖C c-C (switch j c)‖ = 2*‖B j‖ := by + rw [show C c-C (switch j c)=_ from flipsum_bill B j c] + simp only [norm_mul,norm_mark]; norm_num + rw [hJ] at Ht + change p < ‖B j‖ at hX + have hs : p < ‖C (switch j c)‖ := by linarith + rw [HE _ hs]; linarith [HH c] + let A (c : Toss n) := appears (q c) x + have H : ∑ _c : Toss n,(1:ℝ) ≤ ∑ c,(A c+A (switch j c)) := + Finset.sum_le_sum fun i _ => HQ i + have Hv : ∑ c,A (switch j c) = ∑ c,A c := + Fintype.sum_equiv (switch j) _ _ fun _ => rfl + rw [Finset.sum_add_distrib,Hv] at H; simp only [Finset.sum_const,nsmul_eq_mul,mul_one,Finset.card_univ] at H + change _ ≤ 2*∑ c,A c; linarith + · rw [show appears _ x=0 from ite_eq_right hM,mul_zero] + exact mul_nonneg (by norm_num) (Finset.sum_nonneg fun i _ => HH i) + +lemma occurs_meas (S : Set Torus) (h : MeasurableSet S) : + Integrable (appears S) law ∧ (∫ x,appears S x ∂law)=law.real S := by + rw [show appears S=S.indicator (fun _ => (1:ℝ)) by + funext x; by_cases h : x∈S <;> simp [appears,h]] + refine ⟨(integrable_const _).indicator h,?_⟩; rw [integral_indicator h]; simp + +lemma circle_bill (hp : GoodBound) : + ∃ v : ℝ, 0 < v ∧ ∀ f : Torus→ℂ, inside f → + ∀ p : ℝ, 0 < p → p*law.real (cloudSet f p) ≤ v := by + obtain ⟨L,hL,hF⟩ := median_budget hp + refine ⟨4*L,by positivity,?_⟩ + intro f h p hp + let S := cloudSet f p + have hS := cloud_meas f p + rcases eq_or_lt_of_le (show 0 ≤ law.real S from measureReal_nonneg) with hr|hr + · change _*law.real S ≤ _ + rw [←hr,mul_zero]; positivity + obtain ⟨n,hn,z,hz,hv⟩ := clouds_fill S hS hr + let m := (n:ℝ) + have he : 0 < m := by unfold m; positivity + suffices H : p/m ≤ L by + change p*law.real S ≤ _ + rw [div_le_iff₀ he] at H + change m*law.real S ≤ _ at hv + nlinarith + by_contra! H + let q (c : Toss n) := cloudSet (slate f z c) (p/m) + let k := (Fintype.card (Toss n) : ℝ) + have HH (c : Toss n) : law.real (q c) ≤ 1/16 := by + apply le_trans _ (hF _ (family_in f z h hn c)) + refine measureReal_mono ?_ (by finiteness) + intro x hx + exact lt_of_le_of_lt (ENNReal.ofReal_le_ofReal H.le) hx + let b := spread S z + have hB := occurs_meas b (spread_meas S z hS) + have hQ (c) := occurs_meas (q c) (cloud_meas _ _) + have HE : k*law.real b ≤ 2*∑ c,law.real (q c) := by + rw [← hB.2,←integral_const_mul] + simp_rw [← (hQ _).2] + rw [← integral_finsetSum _ (fun i _ => (hQ i).1), + ← integral_const_mul] + apply integral_mono (hB.1.const_mul _) ((integrable_finsetSum _ fun i _ => (hQ i).1).const_mul _) + exact signed_window h p hp z hn + have Hx : ∑ c,law.real (q c) ≤ k*(1/16) := by + calc + _ ≤ ∑ c : Toss n, (1/16:ℝ) := Finset.sum_le_sum fun i _ => HH i + _ = _ := by simp [k] + have HE' : 0 < k := by unfold k; positivity + change 1/2 ≤ law.real b at hz + nlinarith + +end FourierLLogL.Weak + +end OAI