import OAI.Probability.FKMaps.ConditionedWalk namespace OAI noncomputable section open Set Filter MeasureTheory ProbabilityTheory open scoped Topology BigOperators ENNReal NNReal namespace FKCRT.TreeCoding lemma LadderEnumeration.prefixMaximum {S : ℕ → ℤ} {τ : ℕ → ℕ} (h : LadderEnumeration S τ) (n : ℕ) : prefixMaximum S n = S (τ (renewalCount τ n)) := prefixMaximum_eq (renewalCount_spec h.zero n) (fun _ hk => h.maximum hk) lemma prefix_record_remainder {x : ℕ → ℕ} (hx : HasAllLadders x) (ν ε A : ℝ) (h : ∀ r, |renewalLevel r x-ν*r| ≤ ε*renewalLevel r x+A) (n : ℕ) : |(prefixMaximum (integerSum x) n:ℝ)-ν*(weakRecords (integerSum x) n).card| ≤ ε*(prefixMaximum (integerSum x) n:ℝ)+A := by rw [(ladderTime_enumeration hx).prefixMaximum] exact record_count_remainder hx ν ε A h n lemma CriticalEvenOffspring.prefix_record_uniform_remainder (p : CriticalEvenOffspring) {ε : ℝ} (hε : 0<ε) : ∀ᵐ x ∂p.streamLaw, ∃ A : ℕ, ∀ n, |(prefixMaximum (integerSum x) n:ℝ)-(p.variance/2)*(weakRecords (integerSum x) n).card| ≤ ε*(prefixMaximum (integerSum x) n:ℝ)+A := by filter_upwards [p.ae_all_ladders,p.renewal_uniform_remainder hε] with x hx hrem obtain ⟨A,hA⟩ := hrem exact ⟨A,fun n => prefix_record_remainder hx _ _ _ hA n⟩ lemma CriticalEvenOffspring.prefix_record_remainder_probability (p : CriticalEvenOffspring) {ε : ℝ} (hε : 0<ε) : Tendsto (fun A : ℕ => p.streamLaw {x | ∃ n, ε*(prefixMaximum (integerSum x) n:ℝ)+A < |(prefixMaximum (integerSum x) n:ℝ)-(p.variance/2)*(weakRecords (integerSum x) n).card|}) atTop (𝓝 0) := by have hb (A : ℕ) : p.streamLaw {x | ∃ n, ε*(prefixMaximum (integerSum x) n:ℝ)+A < |(prefixMaximum (integerSum x) n:ℝ)-(p.variance/2)*(weakRecords (integerSum x) n).card|} ≤ p.streamLaw {x | ∃ r, ε*renewalLevel r x+A < |renewalLevel r x-(p.variance/2)*r|} := by apply measure_mono_ae filter_upwards [p.ae_all_ladders] with x hx rintro ⟨n,hn⟩ refine ⟨renewalCount (ladderTime x) n, ?_⟩ rwa [(ladderTime_enumeration hx).prefixMaximum, (ladderTime_enumeration hx).card_records, ladderTime_level_real hx] at hn exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds (p.renewal_remainder_probability hε) (fun _ => bot_le) hb lemma le_prefixMaximum (S : ℕ → ℤ) {i n : ℕ} (hi : i≤n) : S i ≤ prefixMaximum S n := Finset.le_sup' S (Finset.mem_range.mpr (by omega)) lemma prefixMaximum_le {S : ℕ → ℤ} {n : ℕ} {a : ℤ} (h : ∀ k, k≤n → S k≤a) : prefixMaximum S n≤a := Finset.sup'_le _ _ (fun k hk => h k (by simpa only [Finset.mem_range,Nat.lt_succ_iff] using hk)) lemma prefixMaximum_mono (S : ℕ → ℤ) {m n : ℕ} (h : m≤n) : prefixMaximum S m≤prefixMaximum S n := prefixMaximum_le fun _ hk => le_prefixMaximum S (hk.trans h) lemma prefixMaximum_attained (S : ℕ → ℤ) (n : ℕ) : ∃ k≤n, prefixMaximum S n = S k := by obtain ⟨k,hk,he⟩ := Finset.exists_mem_eq_sup' (s := Finset.range (n+1)) Finset.nonempty_range_add_one S exact ⟨k,by simpa only [Finset.mem_range,Nat.lt_succ_iff] using hk,he⟩ lemma CriticalEvenOffspring.integerSum_scaled (p : CriticalEvenOffspring) (x : ℕ → ℕ) (n : ℕ) : (integerSum x n:ℝ)=p.sigma*(∑ j∈Finset.range n,p.streamStep j x) := by simp only [integerSum,Int.cast_sum,Int.cast_sub,Int.cast_natCast,Int.cast_one, streamStep,standardStep,← Finset.sum_div] field_simp [p.sigma_pos.ne'] lemma CriticalEvenOffspring.prefix_maximum_bound (p : CriticalEvenOffspring) {N : ℕ} (hN : 0 by unfold streamStep; fun_prop) p.streamStep_memLp p.streamStep_indep p.streamStep_integral (fun k => (p.streamStep_integral_sq k).le) hN hR apply (measureReal_mono ?_).trans hb intro x hx obtain ⟨k,hk,he⟩ := prefixMaximum_attained (integerSum x) N refine ⟨k,hk,?_⟩ change p.sigma*R*Real.sqrt N ≤ (prefixMaximum (integerSum x) N:ℝ) at hx rw [he,p.integerSum_scaled] at hx have hh : R*Real.sqrt N ≤ ∑ j∈Finset.range k,p.streamStep j x := by apply (mul_le_mul_iff_right₀ p.sigma_pos).mp simpa only [mul_assoc] using hx exact hh.trans (le_abs_self _) lemma CriticalEvenOffspring.prefix_record_error_bound (p : CriticalEvenOffspring) {N : ℕ} (hN : 0 p.streamLaw.real {x | ∃ k≤N, δ*Real.sqrt N < |(prefixMaximum (integerSum x) k:ℝ)-(p.variance/2)*(weakRecords (integerSum x) k).card|}) atTop (𝓝 0) := by apply tendsto_order.2 constructor · intro a ha exact Eventually.of_forall fun _ => ha.trans_le measureReal_nonneg · intro η hη let R := Real.sqrt (4/η) have hR : 0 (p.streamLaw {x | ∃ r, ε*renewalLevel r x+A < |renewalLevel r x-(p.variance/2)*r|}).toReal) atTop (𝓝 0) := by simpa only [ENNReal.toReal_zero,Function.comp_def] using (ENNReal.tendsto_toReal (by simp : (0:ℝ≥0∞)≠∞)).comp (p.renewal_remainder_probability hε) obtain ⟨A,hA⟩ := (hlim.eventually (eventually_lt_nhds (show (0:ℝ)<η/2 from half_pos hη))).exists have hscale : Tendsto (fun N : ℕ => δ/2*Real.sqrt N) atTop atTop := (Real.tendsto_sqrt_atTop.comp tendsto_natCast_atTop_atTop).const_mul_atTop (half_pos hδ) filter_upwards [hscale.eventually_ge_atTop (A:ℝ),eventually_gt_atTop 0] with N hN hNpos have hbound := p.prefix_record_error_bound hNpos hR hε.le A have hs : ε*(p.sigma*R*Real.sqrt N)+A ≤ δ*Real.sqrt N := by rw [← mul_assoc,heq] linarith have hmono : p.streamLaw.real {x | ∃ k≤N, δ*Real.sqrt N < |(prefixMaximum (integerSum x) k:ℝ)-(p.variance/2)*(weakRecords (integerSum x) k).card|} ≤ p.streamLaw.real {x | ∃ k≤N, ε*(p.sigma*R*Real.sqrt N)+A < |(prefixMaximum (integerSum x) k:ℝ)-(p.variance/2)*(weakRecords (integerSum x) k).card|} := by refine measureReal_mono ?_ (measure_ne_top _ _) rintro x ⟨k,hk,hx⟩ exact ⟨k,hk,hs.trans_lt hx⟩ apply (hmono.trans hbound).trans_lt rw [htail] linarith end FKCRT.TreeCoding end noncomputable section open Set Filter MeasureTheory ProbabilityTheory open scoped Topology BigOperators ENNReal NNReal namespace FKCRT.TreeCoding lemma map_cond_preimage {Ω Ξ : Type*} [MeasurableSpace Ω] [MeasurableSpace Ξ] (μ : Measure Ω) {f : Ω → Ξ} (hf : Measurable f) {s : Set Ξ} (hs : MeasurableSet s) : (cond μ (f ⁻¹' s)).map f = cond (μ.map f) s := by rw [ProbabilityTheory.cond,ProbabilityTheory.cond,Measure.map_smul _ hf.aemeasurable, ← Measure.restrict_map hf hs,Measure.map_apply hf hs] def streamPrefix (N : ℕ) (x : ℕ → ℕ) : Fin N → ℕ := fun i => x i.val lemma measurable_streamPrefix (N : ℕ) : Measurable (streamPrefix N) := Measurable.of_eval (fun index => measurable_pi_apply index.val) lemma totalStep_streamPrefix (N : ℕ) (x : ℕ → ℕ) : totalStep (streamPrefix N x)=integerSum x N := by exact Fin.sum_univ_eq_sum_range (fun k => ((x k:ℤ)-1)) N lemma CriticalEvenOffspring.streamPrefix_hasLaw (p : CriticalEvenOffspring) (N : ℕ) : HasLaw (streamPrefix N) (p.iidLaw N) p.streamLaw := p.stream_restrict_law N def CriticalEvenOffspring.streamBridgeLaw (p : CriticalEvenOffspring) (n : ℕ) : Measure (ℕ → ℕ) := cond p.streamLaw {x | integerSum x (2*n+1)=-1} lemma CriticalEvenOffspring.streamBridge_restrict (p : CriticalEvenOffspring) (n : ℕ) : HasLaw (streamPrefix (2*n+1)) (p.iidBridgeLaw n) (p.streamBridgeLaw n) := by refine ⟨(measurable_streamPrefix _).aemeasurable,?_⟩ have hh := map_cond_preimage p.streamLaw (measurable_streamPrefix (2*n+1)) (show MeasurableSet {d : Fin (2*n+1) → ℕ | totalStep d=-1} from by measurability) simpa only [streamBridgeLaw,iidBridgeLaw,Set.preimage_ofPred_eq,totalStep_streamPrefix, (p.streamPrefix_hasLaw (2*n+1)).map_eq] using hh instance (p : CriticalEvenOffspring) (n : ℕ) : IsProbabilityMeasure (p.streamBridgeLaw n) := by have : IsProbabilityMeasure ((p.streamBridgeLaw n).map (streamPrefix (2*n+1))) := by rw [(p.streamBridge_restrict n).map_eq] infer_instance exact Measure.isProbabilityMeasure_of_map (measurable_streamPrefix (2*n+1)).aemeasurable def arrayStream {N : ℕ} (d : Fin N → ℕ) : ℕ → ℕ := fun k => if h:k integerSum_arrayStream_prefix N x (hj.trans hk) lemma iidSplit_streamPrefix (m r : ℕ) (x : ℕ → ℕ) : (iidSplit m r (streamPrefix (m+r) x)).1=streamPrefix m x := rfl lemma CriticalEvenOffspring.prefix_domination_stream (p : CriticalEvenOffspring) {n J : ℕ} {K : ℝ} (hdom : p.PrefixDominatedFraction (2*n+1) J K) {m r : ℕ} (hs : m+r=2*n+1) (hfrac : 2*n+1 ≤ J*r) (E : Set (Fin m → ℕ)) : (p.streamBridgeLaw n).real (streamPrefix m ⁻¹' E) ≤ K*p.streamLaw.real (streamPrefix m ⁻¹' E) := by have hmap := (p.streamPrefix_hasLaw m).measurePreserving (measurable_streamPrefix m) rw [hmap.measureReal_preimage (by measurability)] have hb := hdom m r hs hfrac E have heq : (cond (p.iidLaw (m+r)) {d | totalStep d=-1})= (p.streamBridgeLaw n).map (streamPrefix (m+r)) := by rw [hs] exact (p.streamBridge_restrict n).map_eq.symm rw [heq] at hb have hm : Measurable (streamPrefix (m+r)) := measurable_streamPrefix _ rw [map_measureReal_apply hm (by measurability)] at hb exact hb lemma CriticalEvenOffspring.prefix_record_domination (p : CriticalEvenOffspring) {n J : ℕ} {K : ℝ} (hdom : p.PrefixDominatedFraction (2*n+1) J K) {m r : ℕ} (hs : m+r=2*n+1) (hfrac : 2*n+1 ≤ J*r) (a : ℝ) : (p.streamBridgeLaw n).real {x | ∃ k ≤ m, a rintro ⟨k,hk,he⟩ <;> refine ⟨k,hk,?_⟩ · rwa [recordError_arrayStream_prefix m x hk] at he · rwa [recordError_arrayStream_prefix m x hk] simpa only [he] using p.prefix_domination_stream hdom hs hfrac E /-- The uniform record approximation survives conditioning on every fixed proper prefix. The remaining terminal segment is handled separately. -/ theorem CriticalEvenOffspring.bridge_prefix_record_error (p : CriticalEvenOffspring) {J : ℕ} (hJ : 0 (p.streamBridgeLaw n).real {x | ∃ k ≤ m n, δ*Real.sqrt ((2*n+1:ℕ):ℝ) 2*n+1) atTop atTop := by exact tendsto_atTop_mono (fun n => by dsimp; omega) tendsto_id have hi := (p.iid_prefix_record_error hδ).comp hN have hlim := hi.const_mul K simp only [mul_zero] at hlim apply squeeze_zero' (Eventually.of_forall fun _ => measureReal_nonneg) ?_ hlim filter_upwards [hdom,hfrac] with n hn hf have hb := p.prefix_record_domination hn (hs n) hf (δ*Real.sqrt ((2*n+1:ℕ):ℝ)) apply hb.trans apply mul_le_mul_of_nonneg_left _ hK.le refine measureReal_mono ?_ (measure_ne_top _ _) rintro x ⟨k,hk,hx⟩ exact ⟨k,by dsimp; have hh := hs n; omega,hx⟩ lemma CriticalEvenOffspring.streamBridge_reverse (p : CriticalEvenOffspring) (n : ℕ) : MeasurePreserving (reverseFirst (2*n+1)) (p.streamBridgeLaw n) (p.streamBridgeLaw n) := by apply measurePreserving_cond (reverseFirst_preserving p (2*n+1)) (measurableSet_eq_fun (measurable_integerSum _) measurable_const) ext x simp only [Set.mem_preimage,Set.mem_ofPred_eq,integerSum_reverseFirst x (2*n+1) (2*n+1) le_rfl, Nat.sub_self,integerSum_zero,sub_zero] lemma CriticalEvenOffspring.integer_maximal_bound (p : CriticalEvenOffspring) (r : ℕ) {a : ℝ} (ha : 0 by unfold streamStep; fun_prop) p.streamStep_memLp p.streamStep_indep p.streamStep_integral r (div_pos ha p.sigma_pos) simp only [p.streamStep_integral_sq,Finset.sum_const,Finset.card_range,nsmul_eq_mul,mul_one] at hb have hs : {x | ∃ k ≤ r, a ≤ |(integerSum x k:ℝ)|} ⊆ {x | ∃ k ≤ r, a/p.sigma ≤ |∑ j∈Finset.range k,p.streamStep j x|} := by rintro x ⟨k,hk,hx⟩ refine ⟨k,hk,?_⟩ rw [p.integerSum_scaled,abs_mul,abs_of_pos p.sigma_pos] at hx exact (div_le_iff₀ p.sigma_pos).mpr (by simpa [mul_comm] using hx) apply ((measureReal_mono hs).trans hb).trans_eq field_simp lemma integerSum_prefix_event (r : ℕ) (a : ℝ) : streamPrefix r ⁻¹' {d | ∃ k ≤ r, a ≤ |(integerSum (arrayStream d) k:ℝ)|}= {x | ∃ k ≤ r, a ≤ |(integerSum x k:ℝ)|} := by ext x change (∃ k ≤ r, a ≤ |(integerSum (arrayStream (streamPrefix r x)) k:ℝ)|) ↔ _ constructor <;> rintro ⟨k,hk,he⟩ <;> refine ⟨k,hk,?_⟩ · rwa [integerSum_arrayStream_prefix r x hk] at he · rwa [integerSum_arrayStream_prefix r x hk] lemma CriticalEvenOffspring.bridge_initial_maximal_bound (p : CriticalEvenOffspring) {n : ℕ} {K : ℝ} (hK : 0 ≤ K) (hdom : p.PrefixDominatedFraction (2*n+1) 2 K) {r : ℕ} (hr : 2*r ≤ 2*n+1) {a : ℝ} (ha : 0 S (m+k)-S m) (n-m)).card := by classical have hs : weakRecords S n ⊆ weakRecords S m ∪ (weakRecords (fun k => S (m+k)-S m) (n-m)).image (fun k => m+k) := by intro k hk obtain ⟨hk0,hkn,hrec⟩ := (mem_weakRecords S n k).mp hk by_cases hkm : k ≤ m · exact Finset.mem_union_left _ ((mem_weakRecords S m k).mpr ⟨hk0,hkm,hrec⟩) apply Finset.mem_union_right refine Finset.mem_image.mpr ⟨k-m,?_,by omega⟩ apply (mem_weakRecords ..).mpr refine ⟨by omega,by omega,?_⟩ intro j hj have hh := hrec (m+j) (by omega) have he : m+(k-m)=k := by omega rw [he] omega exact (Finset.card_le_card hs).trans ((Finset.card_union_le _ _).trans (add_le_add le_rfl (Finset.card_image_le))) lemma weakRecords_suffix_of_ladder_level (x : ℕ → ℕ) {m n h : ℕ} (hmn : m ≤ n) (hx : HasAllLadders (streamShift m x)) {a : ℝ} (hla : a ≤ renewalLevel h (streamShift m x)) (hosc : ∀ k, m ≤ k → k ≤ n → (integerSum x k:ℝ)-(integerSum x m:ℝ) integerSum x (m+k)-integerSum x m)=integerSum (streamShift m x) := by funext k exact (integerSum_shift x m k).symm rw [he,(ladderTime_enumeration hx).card_records] at hb have hcount : renewalCount (ladderTime (streamShift m x)) (n-m) ≤ h := by by_contra hn have htime : ladderTime (streamShift m x) h ≤ n-m := (le_renewalCount_iff (ladderTime_enumeration hx).strictMono (ladderTime_enumeration hx).zero h (n-m)).mp (by omega) have hosc' := hosc (m+ladderTime (streamShift m x) h) (by omega) (by omega) rw [← ladderTime_level_real hx] at hla rw [integerSum_shift] at hla push_cast at hla linarith omega lemma prefixMaximum_tail_le {S : ℕ → ℤ} {m n : ℕ} {a : ℝ} (ha : 0 ≤ a) (hosc : ∀ k, m ≤ k → k ≤ n → (S k:ℝ)-(S m:ℝ) ≤ a) : (prefixMaximum S n:ℝ) ≤ (prefixMaximum S m:ℝ)+a := by obtain ⟨k,hk,he⟩ := prefixMaximum_attained S n rw [he] by_cases hkm : k ≤ m · exact (Int.cast_le.mpr (le_prefixMaximum S hkm)).trans (le_add_of_nonneg_right ha) · have hh := hosc k (by omega) hk have hsup : (S m:ℝ) ≤ prefixMaximum S m := Int.cast_le.mpr (le_prefixMaximum S le_rfl) linarith /-- Uniform numerical estimate used to pass from a truncated bridge to its full record count. It needs only attained maxima and a one-sided suffix bound. -/ lemma prefix_record_error_tail {x : ℕ → ℕ} {m n h : ℕ} (hmn : m ≤ n) (hx : HasAllLadders (streamShift m x)) {ν a : ℝ} (hν : 0 ≤ ν) (ha : 0 ≤ a) (hla : a ≤ renewalLevel h (streamShift m x)) (hosc : ∀ k, m ≤ k → k ≤ n → (integerSum x k:ℝ)-(integerSum x m:ℝ) (hosc k hk hn).le) have hmaxlo : (prefixMaximum (integerSum x) m:ℝ) ≤ prefixMaximum (integerSum x) n := Int.cast_le.mpr (prefixMaximum_mono _ hmn) apply abs_le.mpr have hh := abs_le.mp (le_refl |(prefixMaximum (integerSum x) m:ℝ)-ν*(weakRecords (integerSum x) m).card|) constructor <;> nlinarith end FKCRT.TreeCoding end noncomputable section open Set Filter MeasureTheory ProbabilityTheory open scoped Topology BigOperators ENNReal NNReal namespace FKCRT.TreeCoding lemma cond_real_le {Ω : Type*} [MeasurableSpace Ω] (μ : Measure Ω) [IsFiniteMeasure μ] {E : Set Ω} (hE : MeasurableSet E) (B : Set Ω) : (cond μ E).real B ≤ μ.real B / μ.real E := by rw [measureReal_def,cond_apply hE,ENNReal.toReal_mul,ENNReal.toReal_inv] change (μ.real E)⁻¹ * μ.real (E ∩ B) ≤ _ rw [div_eq_mul_inv,mul_comm (μ.real B)] exact mul_le_mul_of_nonneg_left (measureReal_mono Set.inter_subset_right) (inv_nonneg.mpr measureReal_nonneg) lemma CriticalEvenOffspring.stream_endpoint_probability (p : CriticalEvenOffspring) (N : ℕ) : p.streamLaw.real {x | integerSum x N = -1} = (p.iidLaw N).real {d | totalStep d = -1} := by have hh := (p.streamPrefix_hasLaw N).measurePreserving (measurable_streamPrefix N) simpa only [Set.preimage_ofPred_eq,totalStep_streamPrefix] using hh.measureReal_preimage (show MeasurableSet {d : Fin N → ℕ | totalStep d = -1} from by measurability).nullMeasurableSet lemma CriticalEvenOffspring.stream_endpoint_limit (p : CriticalEvenOffspring) : Tendsto (fun n => Real.sqrt ((2*n+1:ℕ):ℝ)* p.streamLaw.real {x | integerSum x (2*n+1) = -1}) atTop (𝓝 p.bridgeConstant) := by simpa only [p.stream_endpoint_probability] using p.iid_endpoint_limit lemma CriticalEvenOffspring.streamBridge_shift_ladders (p : CriticalEvenOffspring) (n m : ℕ) : ∀ᵐ x ∂p.streamBridgeLaw n, HasAllLadders (streamShift m x) := cond_absolutelyContinuous.ae_le ((p.streamShift_preserving m).quasiMeasurePreserving.ae p.ae_all_ladders) lemma CriticalEvenOffspring.bridge_shift_ladder_bound (p : CriticalEvenOffspring) {c ρ : ℝ} (hρ : 0 ≤ ρ) (hb : ∀ h, p.streamLaw {x | renewalLevel h x ≤ c*h} ≤ ENNReal.ofReal (ρ^h)) (n m h : ℕ) : (p.streamBridgeLaw n).real {x | renewalLevel h (streamShift m x) ≤ c*h} ≤ ρ^h / p.streamLaw.real {x | integerSum x (2*n+1) = -1} := by have hr : MeasurableSet {x | renewalLevel h x ≤ c*h} := measurableSet_le (measurable_renewalLevel h) measurable_const have ht := (p.streamShift_preserving m).measureReal_preimage hr.nullMeasurableSet have hreal : p.streamLaw.real {x | renewalLevel h x ≤ c*h} ≤ ρ^h := by exact (ENNReal.toReal_mono (by finiteness) (hb h)).trans_eq (ENNReal.toReal_ofReal (pow_nonneg hρ _)) apply (cond_real_le p.streamLaw (measurableSet_eq_fun (measurable_integerSum _) measurable_const) _).trans exact div_le_div_of_nonneg_right (ht.le.trans hreal) measureReal_nonneg lemma CriticalEvenOffspring.bridge_terminal_maximal_bound (p : CriticalEvenOffspring) {n : ℕ} {K : ℝ} (hK : 0 ≤ K) (hdom : p.PrefixDominatedFraction (2*n+1) 2 K) {r : ℕ} (hr : 2*r ≤ 2*n+1) {a : ℝ} (ha : 0 (MeasurableSet.const (k ≤ r)).inter (measurableSet_le measurable_const ((measurable_of_countable (fun z : ℤ => (z:ℝ))).comp (measurable_integerSum k)).abs) have hh := (p.streamBridge_reverse n).measureReal_preimage hE.nullMeasurableSet exact (measureReal_mono hsub).trans (hh.le.trans (p.bridge_initial_maximal_bound hK hdom hr (half_pos ha))) end FKCRT.TreeCoding end noncomputable section open Set Filter MeasureTheory ProbabilityTheory open scoped Topology BigOperators ENNReal NNReal namespace FKCRT.TreeCoding lemma tendsto_natCeil_comp_atTop {s : ℕ → ℝ} (hs : Tendsto s atTop atTop) : Tendsto (fun n => ⌈s n⌉₊) atTop atTop := by refine tendsto_atTop.2 fun b => ?_ filter_upwards [hs.eventually (eventually_ge_atTop (b:ℝ))] with n hn exact_mod_cast hn.trans (Nat.le_ceil (s n)) lemma polynomial_geometric_ceil {s : ℕ → ℝ} (hs : Tendsto s atTop atTop) (hs0 : ∀ n, 0 ≤ s n) {ε ρ : ℝ} (hε : 0<ε) (hρ : 0 ≤ ρ) (hρ1 : ρ<1) (k : ℕ) : Tendsto (fun n => (s n)^k*ρ^⌈ε*s n⌉₊) atTop (𝓝 0) := by have hh : Tendsto (fun n => ⌈ε*s n⌉₊) atTop atTop := tendsto_natCeil_comp_atTop (hs.const_mul_atTop hε) have ht := ((tendsto_pow_const_mul_const_pow_of_lt_one k hρ hρ1).comp hh).div_const (ε^k) simp only [zero_div] at ht apply squeeze_zero (fun n => mul_nonneg (pow_nonneg (hs0 n) _) (pow_nonneg hρ _)) ?_ ht intro n have hr : s n ≤ (⌈ε*s n⌉₊:ℝ)/ε := (le_div_iff₀ hε).mpr (by simpa [mul_comm] using Nat.le_ceil (ε*s n)) calc (s n)^k*ρ^⌈ε*s n⌉₊ ≤ ((⌈ε*s n⌉₊:ℝ)/ε)^k*ρ^⌈ε*s n⌉₊ := by gcongr; exact hs0 n _ = _ := by dsimp; rw [div_pow]; ring lemma odd_size_tendsto : Tendsto (fun n : ℕ => 2*n+1) atTop atTop := tendsto_atTop_mono (fun n => by dsimp; omega) tendsto_id lemma odd_sqrt_tendsto : Tendsto (fun n : ℕ => Real.sqrt ((2*n+1:ℕ):ℝ)) atTop atTop := Real.tendsto_sqrt_atTop.comp (tendsto_natCast_atTop_atTop.comp odd_size_tendsto) lemma CriticalEvenOffspring.bridge_geometric_vanish (p : CriticalEvenOffspring) {ε ρ : ℝ} (hε : 0<ε) (hρ : 0 ≤ ρ) (hρ1 : ρ<1) : Tendsto (fun n => ρ^⌈ε*Real.sqrt ((2*n+1:ℕ):ℝ)⌉₊ / p.streamLaw.real {x | integerSum x (2*n+1) = -1}) atTop (𝓝 0) := by have ht := (polynomial_geometric_ceil odd_sqrt_tendsto (fun _ => Real.sqrt_nonneg _) hε hρ hρ1 1).div p.stream_endpoint_limit p.bridgeConstant_pos.ne' simp only [zero_div,pow_one] at ht convert ht using 1 funext n exact (mul_div_mul_left _ _ (Real.sqrt_pos.mpr (by positivity : (0:ℝ)<((2*n+1:ℕ):ℝ))).ne').symm end FKCRT.TreeCoding end noncomputable section open Set Filter MeasureTheory ProbabilityTheory open scoped Topology BigOperators ENNReal NNReal namespace FKCRT.TreeCoding lemma CriticalEvenOffspring.bridge_record_tail_bound (p : CriticalEvenOffspring) {n m r h : ℕ} (hs : m+r=2*n+1) {t a : ℝ} (ha : 0 ≤ a) : (p.streamBridgeLaw n).real {x | t+a+(p.variance/2)*h (p.streamBridgeLaw n).real {x | δ*Real.sqrt ((2*n+1:ℕ):ℝ) ha.trans_le measureReal_nonneg intro η hη have hjlim : Tendsto (fun J : ℕ => C/(J:ℝ)) atTop (𝓝 0) := tendsto_const_nhds.div_atTop tendsto_natCast_atTop_atTop obtain ⟨J,hJ,hCJ⟩ := ((eventually_ge_atTop 2).and (hjlim.eventually (eventually_lt_nhds (show (0:ℝ)<η/3 by positivity)))).exists have hJ0 : 0 (2*n+1)/J let m : ℕ → ℕ := fun n => 2*n+1-r n have hs : ∀ n, m n+r n=2*n+1 := fun n => Nat.sub_add_cancel (Nat.div_le_self (2*n+1) J) have hfrac : ∀ᶠ n in atTop, 2*n+1 ≤ (2*J)*r n := by filter_upwards [odd_size_tendsto.eventually_ge_atTop J] with n hn exact (division_prefix_properties hJ hn).2.2 have hpre := p.bridge_prefix_record_error (by omega : 0<2*J) m r hs hfrac (half_pos hδ) have hgeo := p.bridge_geometric_vanish hε hρ.le hρ1 have hlarge : ∀ᶠ n in atTop, c+ν ≤ δ/4*Real.sqrt ((2*n+1:ℕ):ℝ) := (odd_sqrt_tendsto.const_mul_atTop (by positivity : 0<δ/4)).eventually_ge_atTop (c+ν) filter_upwards [hdom,odd_size_tendsto.eventually_ge_atTop J,hlarge, hpre.eventually (eventually_lt_nhds (show (0:ℝ)<η/3 by positivity)), hgeo.eventually (eventually_lt_nhds (show (0:ℝ)<η/3 by positivity))] with n hn hJn hln hpn hgn let s := Real.sqrt ((2*n+1:ℕ):ℝ) let h := ⌈ε*s⌉₊ have hsp : 0 t*s) heq dsimp [s] at heq' ⊢ nlinarith have hthresh : δ/2*s+c*h+ν*h ≤ δ*s := by nlinarith have hmono : (p.streamBridgeLaw n).real {x | δ*s trivial⟩ def CriticalEvenOffspring.codeLaw (p : CriticalEvenOffspring) (n : ℕ) : Measure (Code (2*n+1)) := (p.iidBridgeLaw n).map (arrayTree n) instance (p : CriticalEvenOffspring) (n : ℕ) : IsProbabilityMeasure (p.codeLaw n) := inferInstanceAs (IsProbabilityMeasure ((p.iidBridgeLaw n).map (arrayTree n))) lemma CriticalEvenOffspring.code_integral (p : CriticalEvenOffspring) (n : ℕ) (F : Code (2*n+1) → ℝ) : (∫ d,F d ∂p.codeLaw n)=∑ d : Code (2*n+1), (codeWeight p d/codePartition p (2*n+1))*F d := by rw [codeLaw,integral_map (measurable_of_countable (arrayTree n)).aemeasurable (measurable_of_countable F).aestronglyMeasurable] exact p.arrayTree_expectation n F lemma integerSum_arrayStream_bridge {N : ℕ} (d : Bridge N) {k : ℕ} (hk : k ≤ N) : integerSum (arrayStream (bridgeEmbedding N d)) k=walk d.val k := by unfold integerSum walk rw [← Fin.sum_univ_eq_sum_range] symm rw [← Finset.sum_filter] apply Finset.sum_bij (fun i hi => (⟨i.val,by have hh := Finset.mem_filter.mp hi exact hh.2⟩ : Fin k)) · intro i hi exact Finset.mem_univ _ · intro i hi j hj he exact Fin.ext (congrArg (fun z : Fin k => z.val) he) · intro i _ exact ⟨⟨i.val,i.isLt.trans_le hk⟩,by simp [i.isLt],rfl⟩ · intro i hi have hi' : i.val recordError ν (integerSum x) N) := by have hh := (measurable_of_countable (fun d : Fin N → ℕ => recordError ν (integerSum (arrayStream d)) N)).comp (measurable_streamPrefix N) simpa only [Function.comp_def,recordError_arrayStream_prefix N _ le_rfl] using hh def badHeightFraction {N : ℕ} (ν a : ℝ) (d : Code N) : ℝ := (∑ v : Fin N, if a < |(walk d.val v.val:ℝ)-ν*height d.val v.val| then (1:ℝ) else 0)/(N:ℝ) lemma badHeightFraction_nonneg {N : ℕ} (ν a : ℝ) (d : Code N) : 0 ≤ badHeightFraction ν a d := by unfold badHeightFraction positivity lemma recordError_reverse_array_bridge {N : ℕ} (ν : ℝ) (b : Bridge N) : recordError ν (reversedPath (integerSum (arrayStream (bridgeEmbedding N b))) N) N = recordError ν (reversedPath (walk b.val) N) N := by apply recordError_congr intro k hk simp only [reversedPath,integerSum_arrayStream_bridge b le_rfl, integerSum_arrayStream_bridge b (Nat.sub_le N k)] lemma recordError_reverse_streamPrefix (ν : ℝ) (N : ℕ) (x : ℕ → ℕ) : recordError ν (reversedPath (integerSum (arrayStream (streamPrefix N x))) N) N = recordError ν (integerSum (reverseFirst N x)) N := by apply recordError_congr intro k hk rw [reversedPath,integerSum_arrayStream_prefix N x le_rfl, integerSum_arrayStream_prefix N x (Nat.sub_le N k),integerSum_reverseFirst x N k hk] lemma CriticalEvenOffspring.code_average_height_error (p : CriticalEvenOffspring) (n : ℕ) (ν a : ℝ) : (∫ d,badHeightFraction ν a d ∂p.codeLaw n) = (p.streamBridgeLaw n).real {x | a if a F (streamPrefix N x)) = fun x => if a ∫ d,badHeightFraction (p.variance/2) (δ*Real.sqrt ((2*n+1:ℕ):ℝ)) d ∂p.codeLaw n) atTop (𝓝 0) := by simpa only [p.code_average_height_error] using p.bridge_record_error hδ end FKCRT.TreeCoding end end OAI