File size: 25,130 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import OAI.Dynamics.StandardMap.EntropyEndpoint
import OAI.Dynamics.StandardMap.Coding.PartitionLimit
import OAI.Dynamics.StandardMap.Coding.SanitizedTowerLaw

namespace OAI

section
namespace HyperbolicCoding
open MeasureTheory Set Filter StandardMapEntropy.Entropy
open scoped BigOperators ENNReal Topology
variable {X A : Type*} [MeasurableSpace X] [StandardBorelSpace X]
  [TopologicalSpace X] [SecondCountableTopology X] [OpensMeasurableSpace X]
  [MeasurableSpace A] [Fintype A] [DecidableEq A] [MeasurableSingletonClass A] [Nonempty A]

def FiniteRateSupremum (μ : Measure X) (f : X → X) (h : ℝ) : Prop :=
  (∀ r : ℕ,∀ p : X → Fin (r+1),Measurable p → rate μ f p≤h) ∧
  (∀ ε : ℝ,0 < ε → ∃ r : ℕ,∃ p : X → Fin (r+1),Measurable p ∧ h-ε < rate μ f p)

omit [StandardBorelSpace X] [TopologicalSpace X] [SecondCountableTopology X]
  [OpensMeasurableSpace X] in
lemma FiniteRateSupremum.upper {μ : Measure X} [IsProbabilityMeasure μ] {f : X → X} {h : ℝ}
    (H : FiniteRateSupremum μ f h)
    {C : Type*} [Fintype C] [Nonempty C] [MeasurableSpace C] [MeasurableSingletonClass C]
    (p : X → C) (hp : Measurable p) : rate μ f p≤h := by
  have hc : Fintype.card C-1+1=Fintype.card C := Nat.sub_add_cancel Fintype.card_pos
  let e : C ≃ Fin (Fintype.card C-1+1) := (Fintype.equivFin C).trans (finCongr hc.symm)
  have he := H.1 (Fintype.card C-1) (e ∘ p) ((measurable_of_finite e).comp hp)
  rwa [rate_equiv] at he

omit [StandardBorelSpace X] [TopologicalSpace X] [SecondCountableTopology X]
  [OpensMeasurableSpace X] [DecidableEq A] [Nonempty A] in
lemma iid_lawClose_prefix (μ : Measure X) (e : X ≃ᵐ X) (p : X → A) (hp : Measurable p)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    {m n : ℕ} (hmn : m≤n) {δ : ℝ}
    (h : LawClose (μ.map (word e p n))
      (Measure.pi (fun _ : Fin n => finiteWeightLaw β)) δ) :
    LawClose (μ.map (word e p m))
      (Measure.pi (fun _ : Fin m => finiteWeightLaw β)) δ := by
  let : IsProbabilityMeasure (finiteWeightLaw β) := finiteWeightLaw_probability β hβ hβsum
  let ν := Measure.infinitePi (fun _ : ℤ => finiteWeightLaw β)
  have h' : LawClose (μ.map (word e p n))
      (ν.map (word iidShift (fun w : ℤ → A => w 0) n)) δ := by
    simpa only [ν,iid_word_law] using h
  have hh := lawClose_prefix μ ν e iidShift e.measurable iidShift.measurable p
    (fun w : ℤ → A => w 0) hp (measurable_pi_apply 0) hmn h'
  simpa only [ν,iid_word_law] using hh

def GoodIIDStage (μ : Measure X) (e : X ≃ᵐ X) (β : A → ℝ)
    {η : ℝ} (T : IIDCopyTest μ e β η) (n : ℕ) (q : X → A) : Prop :=
  Measurable q ∧
  LawClose (μ.map (word e q (max T.length n)))
    (Measure.pi (fun _ : Fin (max T.length n) => finiteWeightLaw β))
    (min T.tolerance (1/(n+1 : ℝ))) ∧
  weightEntropy β-T.tolerance≤rate μ e q

lemma goodIIDStage_initial (μ : Measure X) [IsProbabilityMeasure μ]
    [NullSingletonClass μ] [μ.OuterRegular] (e : X ≃ᵐ X) (he : Ergodic e μ)
    (htotal : ∀ m : ℕ,0 < m → Ergodic (e^[m]) μ)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    (hβentropy : 0 < weightEntropy β) (H : FiniteRateSupremum μ e (weightEntropy β))
    (zero one : A) (h01 : zero≠one) {η : ℝ} (T : IIDCopyTest μ e β η) (n : ℕ) :
    ∃ q : X → A,GoodIIDStage μ e β T n q := by
  obtain ⟨r,p,hp,hpnear⟩ := H.2 (T.tolerance/2) (half_pos T.tolerance_pos)
  have hε : 0 < min T.tolerance (1/(n+1 : ℝ)) := lt_min T.tolerance_pos (by positivity)
  obtain ⟨q,hq,hl,hr⟩ := initial_rate_controlled_copy μ e he htotal p hp β hβ hβsum
    hβentropy (H.1 r p hp) zero one h01 (max T.length n) hε (half_pos T.tolerance_pos)
  exact ⟨q,hq,hl,by linarith⟩

lemma goodIIDStage_next (μ : Measure X) [IsProbabilityMeasure μ]
    [NullSingletonClass μ] [μ.OuterRegular] (e : X ≃ᵐ X) (he : Ergodic e μ)
    (htotal : ∀ m : ℕ,0 < m → Ergodic (e^[m]) μ)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    (hβentropy : 0 < weightEntropy β) (H : FiniteRateSupremum μ e (weightEntropy β))
    (zero one : A) (h01 : zero≠one) {η ξ : ℝ} (hη : 0 < η)
    (T : IIDCopyTest μ e β (η/2)) (U : IIDCopyTest μ e β ξ)
    {n : ℕ} {q : X → A} (hq : GoodIIDStage μ e β T n q) :
    ∃ q' : X → A,GoodIIDStage μ e β U (n+1) q' ∧ μ.real {x | q x≠q' x}<η := by
  obtain ⟨r,p,hp,hpnear⟩ := H.2 (U.tolerance/2) (half_pos U.tolerance_pos)
  let P : X → A × Fin (r+1) := fun x => (q x,p x)
  have hP : Measurable P := hq.1.prodMk hp
  have hrateP : rate μ e p≤rate μ e P := rate_factor μ e he.toMeasurePreserving P hP Prod.snd
  have htest : LawClose (μ.map (word e (Prod.fst ∘ P) T.length))
      (Measure.pi (fun _ : Fin T.length => finiteWeightLaw β)) T.tolerance :=
    (iid_lawClose_prefix μ e q hq.1 β hβ hβsum (le_max_left _ _) hq.2.1).mono (min_le_left _ _)
  have hε : 0 < min U.tolerance (1/((n+1)+1 : ℝ)) := lt_min U.tolerance_pos (by positivity)
  obtain ⟨q',s,M,G,hq',hc,hl,_hd,hr⟩ := T.copy μ e he htotal β hβ hβsum hβentropy
    zero one h01 hη P hP Prod.fst htest hq.2.2 (H.upper P hP)
    (max U.length (n+1)) hε (half_pos U.tolerance_pos)
  refine ⟨q',⟨hq',?_,by linarith⟩,hc⟩
  simpa only [Nat.cast_add,Nat.cast_one] using hl

theorem full_entropy_copy_sequence (μ : Measure X) [IsProbabilityMeasure μ]
    [NullSingletonClass μ] [μ.OuterRegular] (e : X ≃ᵐ X) (he : Ergodic e μ)
    (htotal : ∀ m : ℕ,0 < m → Ergodic (e^[m]) μ)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    (hβentropy : 0 < weightEntropy β) (H : FiniteRateSupremum μ e (weightEntropy β))
    (zero one : A) (h01 : zero≠one) (η : ℕ → ℝ) (hη : ∀ n,0 < η n) :
    ∃ q : ℕ → X → A,(∀ n,Measurable (q n)) ∧
      (∀ n,μ.real {x | q (n+1) x≠q n x}<η n) ∧
      (∀ n,LawClose (μ.map (word e (q n) n))
        (Measure.pi (fun _ : Fin n => finiteWeightLaw β)) (1/(n+1 : ℝ))) := by
  have hT (n : ℕ) : Nonempty (IIDCopyTest μ e β (η n/2)) :=
    exists_iidCopyTest μ e he.toMeasurePreserving β hβ hβsum hβentropy (half_pos (hη n))
  let T (n : ℕ) : IIDCopyTest μ e β (η n/2) := Classical.choice (hT n)
  obtain ⟨q₀,hq₀⟩ := goodIIDStage_initial μ e he htotal β hβ hβsum hβentropy H zero one h01 (T 0) 0
  have hnext (n : ℕ) (q : {q : X → A // GoodIIDStage μ e β (T n) n q}) :
      ∃ q' : X → A,GoodIIDStage μ e β (T (n+1)) (n+1) q' ∧
        μ.real {x | q.val x≠q' x}<η n :=
    goodIIDStage_next μ e he htotal β hβ hβsum hβentropy H zero one h01 (hη n)
      (T n) (T (n+1)) q.property
  let next (n : ℕ) (q : {q : X → A // GoodIIDStage μ e β (T n) n q}) :
      {q' : X → A // GoodIIDStage μ e β (T (n+1)) (n+1) q'} :=
    ⟨Classical.choose (hnext n q),(Classical.choose_spec (hnext n q)).1⟩
  let z : (n : ℕ) → {q : X → A // GoodIIDStage μ e β (T n) n q} :=
    fun n => Nat.rec (motive:=fun j => {q : X → A // GoodIIDStage μ e β (T j) j q})
      ⟨q₀,hq₀⟩ next n
  refine ⟨fun n => (z n).val,fun n => (z n).property.1,?_,?_⟩
  · intro n
    have h := (Classical.choose_spec (hnext n (z n))).2
    have h' : μ.real {x | (z n).val x≠(z (n+1)).val x}<η n := h
    have hs : {x | (z (n+1)).val x≠(z n).val x}=
        {x | (z n).val x≠(z (n+1)).val x} := Set.ext (fun _ => ne_comm)
    rw [hs]
    exact h'
  · intro n
    exact (iid_lawClose_prefix μ e (z n).val (z n).property.1 β hβ hβsum
      (le_max_right _ _) (z n).property.2.1).mono (min_le_right _ _)

end HyperbolicCoding

end
section
namespace HyperbolicCoding
open MeasureTheory Set Filter StandardMapEntropy.Entropy
open scoped BigOperators ENNReal Topology
variable {X Y A : Type*} [MeasurableSpace X] [MeasurableSpace Y]
  [MeasurableSpace A] [Fintype A] [MeasurableSingletonClass A]

omit [Fintype A] [MeasurableSingletonClass A] in
lemma iidShift_integerIterate (n : ℤ) (w : ℤ → A) (i : ℤ) :
    integerIterate iidShift n w i=w (i+n) := by
  cases n with
  | ofNat m => rw [Int.ofNat_eq_natCast,integerIterate_nat,iidShift_iterate]
  | negSucc m =>
    have hn : Int.negSucc m= -((m+1 : ℕ) : ℤ) := by omega
    rw [hn,integerIterate_neg_nat,iidShift_symm_iterate,sub_eq_add_neg]

omit [Fintype A] [MeasurableSingletonClass A] in
lemma iid_orbitName (w : ℤ → A) : orbitName iidShift (fun z : ℤ → A => z 0) w=w := by
  funext i
  exact (iidShift_integerIterate i w 0).trans (by rw [zero_add])

lemma finite_integer_window (s : Finset ℤ) :
    ∃ a : ℤ,∃ N : ℕ,∀ i∈s,0≤ i-a ∧ i-a < (N : ℤ) := by
  let b := s.sup Int.natAbs
  refine ⟨-(b : ℤ),2*b+1,?_⟩
  intro i hi
  have hb : i.natAbs≤b := Finset.le_sup (f:=Int.natAbs) hi
  have hb' : (i.natAbs : ℤ)≤b := by exact_mod_cast hb
  have h1 : -i≤(i.natAbs : ℤ) := by simpa only [Int.natAbs_neg] using (Int.le_natAbs (a:=-i))
  have h2 := (Int.le_natAbs (a:=i))
  constructor <;> omega

lemma orbit_restrict_map_eq_window (μ : Measure X) (e : X ≃ᵐ X)
    (he : MeasurePreserving e μ μ) (p : X → A) (hp : Measurable p)
    (s : Finset ℤ) (a : ℤ) (N : ℕ) (hb : ∀ i∈s,0≤ i-a ∧ i-a < (N : ℤ)) :
    μ.map (s.restrict ∘ orbitName e p)=
      (μ.map (word e p N)).map (fun w : Fin N → A => fun i : s =>
        w ⟨(i.val-a).toNat,by have h := hb i.val i.property; omega⟩) := by
  let W : (Fin N → A) → (s → A) := fun w i =>
    w ⟨(i.val-a).toNat,by have h := hb i.val i.property; omega⟩
  have hW : Measurable W := measurable_of_finite W
  have hpN := word_measurable e e.measurable p hp N
  have hfun : s.restrict ∘ orbitName e p=(W ∘ word e p N) ∘ integerIterate e a := by
    funext x i
    change p (integerIterate e i.val x) = p (e^[((i.val-a).toNat)] (integerIterate e a x))
    rw [←integerIterate_nat,←integerIterate_add]
    have h := hb i.val i.property
    congr 2
    omega
  change μ.map (s.restrict ∘ orbitName e p)=(μ.map (word e p N)).map W
  rw [hfun,←Measure.map_map (hW.comp hpN) (integerIterate_measurable e a),
    (integerIterate_measurePreserving e he a).map_eq,Measure.map_map hW hpN]

lemma lawClose_orbit_restrict_of_word
    (μ : Measure X) (ν : Measure Y) (e : X ≃ᵐ X) (d : Y ≃ᵐ Y)
    (he : MeasurePreserving e μ μ) (hd : MeasurePreserving d ν ν)
    (p : X → A) (q : Y → A) (hp : Measurable p) (hq : Measurable q)
    (s : Finset ℤ) (a : ℤ) (N : ℕ) (hb : ∀ i∈s,0≤ i-a ∧ i-a < (N : ℤ))
    {δ : ℝ} (hl : LawClose (μ.map (word e p N)) (ν.map (word d q N)) δ) :
    LawClose (μ.map (s.restrict ∘ orbitName e p))
      (ν.map (s.restrict ∘ orbitName d q)) δ := by
  rw [orbit_restrict_map_eq_window μ e he p hp s a N hb,
    orbit_restrict_map_eq_window ν d hd q hq s a N hb]
  exact hl.map (measurable_of_finite _)

omit [Fintype A] [MeasurableSingletonClass A] in
lemma restricted_box_lawClose (μ : Measure X) (ν : Measure Y)
    (e : X ≃ᵐ X) (d : Y ≃ᵐ Y) (p : X → A) (q : Y → A)
    (hp : Measurable p) (hq : Measurable q) (s : Finset ℤ)
    (t : ℤ → Set A) (ht : ∀ i,MeasurableSet (t i)) {δ : ℝ}
    (h : LawClose (μ.map (s.restrict ∘ orbitName e p))
      (ν.map (s.restrict ∘ orbitName d q)) δ) :
    |(μ.map (orbitName e p)).real (Set.pi s t)-
      (ν.map (orbitName d q)).real (Set.pi s t)|≤δ := by
  let D : Set (s → A) := Set.pi Set.univ (fun i => t i.val)
  have hD : MeasurableSet D := MeasurableSet.pi Set.countable_univ (fun i _ => ht i.val)
  have hpre : (s.restrict : (ℤ → A) → (s → A)) ⁻¹' D=Set.pi s t := by
    ext w
    simp only [D,mem_preimage,Set.mem_pi,mem_univ,true_implies,Finset.mem_coe,Finset.restrict]
    exact ⟨fun h i hi => h ⟨i,hi⟩,fun h i => h i i.property⟩
  have hr : Measurable (s.restrict : (ℤ → A) → (s → A)) := measurable_restrict (s : Set ℤ)
  have h1 : (μ.map (s.restrict ∘ orbitName e p)).real D=
      (μ.map (orbitName e p)).real (Set.pi s t) := by
    simp only [Measure.real]
    rw [←Measure.map_map hr (measurable_orbitName e hp),Measure.map_apply hr hD,hpre]
  have h2 : (ν.map (s.restrict ∘ orbitName d q)).real D=
      (ν.map (orbitName d q)).real (Set.pi s t) := by
    simp only [Measure.real]
    rw [←Measure.map_map hr (measurable_orbitName d hq),Measure.map_apply hr hD,hpre]
  simpa only [h1,h2] using h D hD

theorem iid_law_of_growing_forward_words [Nonempty A]
    (μ : Measure X) [IsProbabilityMeasure μ] (e : X ≃ᵐ X)
    (he : MeasurePreserving e μ μ) (q : ℕ → X → A) (hq : ∀ n,Measurable (q n))
    (p : X → A) (hp : Measurable p)
    (hlim : ∀ᵐ x ∂μ,∀ᶠ n : ℕ in atTop,q n x=p x)
    (β : Measure A) [IsProbabilityMeasure β] (ε : ℕ → ℝ)
    (hε : Tendsto ε atTop (𝓝 0))
    (hlaw : ∀ n,LawClose (μ.map (word e (q n) n))
      (Measure.pi (fun _ : Fin n => β)) (ε n)) :
    MeasurePreserving (orbitName e p) μ (Measure.infinitePi (fun _ : ℤ => β)) := by
  apply iid_law_of_partition_limit e he hq hp hlim β
  intro s t ht
  let ν := Measure.infinitePi (fun _ : ℤ => β)
  obtain ⟨a,N,hN⟩ := finite_integer_window s
  have htarget : (ν.map (orbitName iidShift (fun w : ℤ → A => w 0))).real (Set.pi s t)=
      (∏ i∈s,β (t i)).toReal := by
    rw [show orbitName iidShift (fun w : ℤ → A => w 0)=id from funext iid_orbitName,
      Measure.map_id]
    exact congrArg ENNReal.toReal (Measure.infinitePi_pi (fun _ : ℤ => β) (fun i _ => ht i))
  have hbound : ∀ᶠ n : ℕ in atTop,
      |(μ.map (orbitName e (q n))).real (Set.pi s t)-(∏ i∈s,β (t i)).toReal|≤ε n := by
    filter_upwards [eventually_ge_atTop N] with n hn
    have hb : ∀ i∈s,0≤ i-a ∧ i-a < (n : ℤ) := by
      intro i hi
      have h := hN i hi
      exact ⟨h.1,lt_of_lt_of_le h.2 (by exact_mod_cast hn)⟩
    have hl : LawClose (μ.map (word e (q n) n))
        (ν.map (word iidShift (fun w : ℤ → A => w 0) n)) (ε n) := by
      simpa only [ν,iid_word_law] using hlaw n
    have hr := lawClose_orbit_restrict_of_word μ ν e iidShift he (iidShift_preserving β)
      (q n) (fun w : ℤ → A => w 0) (hq n) (measurable_pi_apply 0) s a n hb hl
    have hh := restricted_box_lawClose μ ν e iidShift (q n) (fun w : ℤ → A => w 0)
      (hq n) (measurable_pi_apply 0) s t ht hr
    rwa [htarget] at hh
  apply tendsto_iff_dist_tendsto_zero.mpr
  simp only [Real.dist_eq]
  exact squeeze_zero' (Eventually.of_forall (fun _ => abs_nonneg _)) hbound hε

end HyperbolicCoding

end
section
namespace HyperbolicCoding
open MeasureTheory Set Filter StandardMapEntropy.Entropy
open scoped BigOperators ENNReal NNReal Topology
variable {X A : Type*} [MeasurableSpace X]
  [MeasurableSpace A] [Fintype A] [MeasurableSingletonClass A]

omit [Fintype A] [MeasurableSingletonClass A] in
lemma word_law_of_iid_factor (μ : Measure X) (e : X ≃ᵐ X) (p : X → A)
    (_hp : Measurable p) (β : Measure A) [IsProbabilityMeasure β]
    (hcode : MeasurePreserving (orbitName e p) μ (Measure.infinitePi (fun _ : ℤ => β)))
    (n : ℕ) : μ.map (word e p n)=Measure.pi (fun _ : Fin n => β) := by
  have heq : word iidShift (fun w : ℤ → A => w 0) n ∘ orbitName e p=word e p n := by
    funext x i
    rw [Function.comp_apply,iid_word_eq]
    exact congrArg p (integerIterate_nat e i.val x)
  have hm := congrArg (fun ν => ν.map (word iidShift (fun w : ℤ → A => w 0) n)) hcode.map_eq
  rw [Measure.map_map (word_measurable iidShift iidShift.measurable _ (measurable_pi_apply 0) n)
    hcode.measurable,heq,iid_word_law] at hm
  exact hm

lemma rate_of_iid_factor [DecidableEq A] (μ : Measure X) [IsProbabilityMeasure μ]
    (e : X ≃ᵐ X) (he : MeasurePreserving e μ μ) (p : X → A) (hp : Measurable p)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    (hcode : MeasurePreserving (orbitName e p) μ
      (Measure.infinitePi (fun _ : ℤ => finiteWeightLaw β))) :
    rate μ e p=weightEntropy β := by
  let : IsProbabilityMeasure (finiteWeightLaw β) := finiteWeightLaw_probability β hβ hβsum
  have hword (n : ℕ) : obs μ (word e p n)=(n : ℝ)*weightEntropy β := by
    have hmass : mass μ (word e p n)=productWordWeight β n := by
      funext w
      change μ.real ((word e p n) ⁻¹' {w})=_
      rw [←map_measureReal_apply (word_measurable e e.measurable p hp n) (measurableSet_singleton w),
        word_law_of_iid_factor μ e p hp (finiteWeightLaw β) hcode,
        Measure.real,Measure.pi_singleton]
      simp only [finiteWeightLaw_singleton,ENNReal.toReal_prod,ENNReal.toReal_ofReal (hβ _),
        productWordWeight]
    rw [obs,hmass]
    exact shannon_productWordWeight β hβsum n
  apply tendsto_nhds_unique (rate_tendsto μ e he p hp)
  apply tendsto_const_nhds.congr'
  filter_upwards [eventually_gt_atTop 0] with n hn
  rw [hword,mul_div_cancel_left₀ _ (Nat.cast_ne_zero.mpr hn.ne')]

omit [MeasurableSpace A] [Fintype A] [MeasurableSingletonClass A] in
lemma geometric_repainting_summable (μ : Measure X) [IsFiniteMeasure μ]
    (q : ℕ → X → A)
    (h : ∀ n,μ.real {x | q (n+1) x≠q n x}<((1/2 : ℝ≥0)^n : ℝ)) :
    (∑' n : ℕ,μ {x | q (n+1) x≠q n x})≠⊤ := by
  have hs : (∑' n : ℕ,(((1/2 : ℝ≥0)^n) : ℝ≥0∞))≠⊤ := by
    apply ENNReal.tsum_coe_ne_top_iff_summable_coe.mpr
    simpa only [NNReal.coe_pow,NNReal.coe_div,NNReal.coe_one,NNReal.coe_ofNat] using
      (summable_geometric_of_lt_one (show (0 : ℝ)≤1/2 by norm_num) (by norm_num : (1/2 : ℝ) < 1))
  apply ne_top_of_le_ne_top hs
  apply ENNReal.tsum_le_tsum
  intro n
  apply (ENNReal.toReal_le_toReal (measure_ne_top μ _) (by simp)).mp
  simpa only [Measure.real,ENNReal.toReal_pow,ENNReal.coe_toReal,NNReal.coe_pow] using (h n).le

variable [StandardBorelSpace X] [TopologicalSpace X] [SecondCountableTopology X]
  [OpensMeasurableSpace X] [DecidableEq A] [Nonempty A]

theorem full_entropy_iid_factor (μ : Measure X) [IsProbabilityMeasure μ]
    [NullSingletonClass μ] [μ.OuterRegular] (e : X ≃ᵐ X) (he : Ergodic e μ)
    (htotal : ∀ m : ℕ,0 < m → Ergodic (e^[m]) μ)
    (β : A → ℝ) (hβ : ∀ a,0≤β a) (hβsum : ∑ a,β a=1)
    (hβentropy : 0 < weightEntropy β) (H : FiniteRateSupremum μ e (weightEntropy β))
    (zero one : A) (h01 : zero≠one) :
    ∃ p : X → A,Measurable p ∧
      MeasurePreserving (orbitName e p) μ (Measure.infinitePi (fun _ : ℤ => finiteWeightLaw β)) ∧
      rate μ e p=weightEntropy β ∧
      (∀ x,orbitName e p (e x)=fun i => orbitName e p x (i+1)) ∧
      (∀ r : ℕ,∀ α : X → Fin (r+1),Measurable α →
        rate μ e (fun x => (p x,α x))=weightEntropy β) := by
  let : IsProbabilityMeasure (finiteWeightLaw β) := finiteWeightLaw_probability β hβ hβsum
  obtain ⟨q,hq,hpaint,hlaw⟩ := full_entropy_copy_sequence μ e he htotal β hβ hβsum hβentropy
    H zero one h01 (fun n => ((1/2 : ℝ≥0)^n : ℝ)) (fun _ => by positivity)
  obtain ⟨p,hp,hlim⟩ := exists_partition_limit μ q hq (geometric_repainting_summable μ q hpaint)
  have hcode := iid_law_of_growing_forward_words μ e he.toMeasurePreserving q hq p hp hlim
    (finiteWeightLaw β) (fun n => 1/(n+1 : ℝ)) tendsto_one_div_add_atTop_nhds_zero_nat hlaw
  have hrate := rate_of_iid_factor μ e he.toMeasurePreserving p hp β hβ hβsum hcode
  refine ⟨p,hp,hcode,hrate,fun x => orbitName_shift e p x,?_⟩
  intro r α hα
  apply le_antisymm (H.upper (fun x => (p x,α x)) (hp.prodMk hα))
  rw [←hrate]
  exact rate_pair_ge_left μ e he.toMeasurePreserving p α hp hα

end HyperbolicCoding

end
section
namespace StandardMapEntropy.Entropy
open MeasureTheory Set Filter
open scoped BigOperators ENNReal Topology

lemma shannon_weighted_le {A : Type*} [Fintype A]
    (p q : A → ℝ) (hp : ∀ a,0 ≤ p a) (hq : ∀ a,0 ≤ q a)
    (hps : ∑ a,p a=1) (hqs : ∑ a,q a=1)
    {c : ℝ} (hc : 0 < c) (hdom : ∀ a,c*p a ≤ q a) :
    c*shannon p ≤ shannon q := by
  have hs (a : A) (ha : p a≠0) : q a≠0 := by
    have hp' : 0 < p a := lt_of_le_of_ne (hp a) (Ne.symm ha)
    exact ne_of_gt ((mul_pos hc hp').trans_le (hdom a))
  have hh := mul_le_mul_of_nonneg_left
    (shannon_le_cross p q hp hq hs (by rw [hps,hqs])) hc.le
  apply hh.trans
  rw [shannon,←Finset.sum_neg_distrib,Finset.mul_sum]
  apply Finset.sum_le_sum
  intro a _
  have hq1 : q a ≤ 1 := (Finset.single_le_sum (fun b _ => hq b) (Finset.mem_univ a)).trans_eq hqs
  have hl : Real.log (q a) ≤ 0 := Real.log_nonpos (hq a) hq1
  have hh := mul_le_mul_of_nonpos_right (hdom a) hl
  rw [Real.negMulLog]
  nlinarith

variable {X A : Type*} [MeasurableSpace X] [MeasurableSpace A]
  [Fintype A] [MeasurableSingletonClass A]
lemma obs_weighted_le (μ ν : Measure X) [IsProbabilityMeasure μ] [IsProbabilityMeasure ν]
    {c : ℝ} (hc : 0 < c) (hdom : ∀ E : Set X,c*ν.real E ≤ μ.real E)
    (p : X → A) (hp : Measurable p) : c*obs ν p ≤ obs μ p :=
  shannon_weighted_le (mass ν p) (mass μ p) (mass_nonneg _ _) (mass_nonneg _ _)
    (by simpa only [measure_univ,ENNReal.toReal_one] using mass_sum ν p hp)
    (by simpa only [measure_univ,ENNReal.toReal_one] using mass_sum μ p hp) hc (fun a => hdom _)

lemma rate_weighted_le (μ ν : Measure X) [IsProbabilityMeasure μ] [IsProbabilityMeasure ν]
    {c : ℝ} (hc : 0 < c) (hdom : ∀ E : Set X,c*ν.real E ≤ μ.real E)
    (f : X → X) (hμ : MeasurePreserving f μ μ) (hν : MeasurePreserving f ν ν)
    (p : X → A) (hp : Measurable p) : c*rate ν f p ≤ rate μ f p := by
  apply le_of_tendsto_of_tendsto
    ((rate_tendsto ν f hν p hp).const_mul c) (rate_tendsto μ f hμ p hp)
  apply Filter.Eventually.of_forall
  intro n
  have hh := div_le_div_of_nonneg_right
    (obs_weighted_le μ ν hc hdom (word f p n) (word_measurable f hμ.measurable p hp n))
    (Nat.cast_nonneg n)
  simpa only [mul_div_assoc] using hh
end StandardMapEntropy.Entropy

namespace StandardMapEntropy
open MeasureTheory Set Filter HyperbolicCoding
open scoped BigOperators ENNReal Topology
lemma area_weight_normalized (E : Set Torus) (hE : 0 < area E) (B : Set Torus) :
    area.real E*(normalizedArea E).real B ≤ area.real B := by
  have hc : area.real E≠0 := (ENNReal.toReal_pos hE.ne' (measure_ne_top area E)).ne'
  have he : area.real E*(normalizedArea E).real B=(area.restrict E).real B := by
    simp only [normalizedArea,Measure.real,Measure.smul_apply,smul_eq_mul,
      ENNReal.toReal_mul,ENNReal.toReal_inv]
    change area.real E*((area.real E)⁻¹*(area.restrict E).real B)=_
    rw [←mul_assoc,mul_inv_cancel₀ hc,one_mul]
    rfl
  rw [he]
  exact ENNReal.toReal_mono (measure_ne_top area B) (Measure.restrict_le_self B)

lemma restricted_finite_rate_upper {A : Type*} [MeasurableSpace A]
    [Fintype A] [MeasurableSingletonClass A]
    (k : ℝ) (hk : 0 ≤ k) (n : ℕ) (E : Set Torus) (hE : 0 < area E)
    (hinv : MeasurePreserving ((standardMap k)^[n]) (normalizedArea E) (normalizedArea E))
    (p : Torus → A) (hp : Measurable p) :
    Entropy.rate (normalizedArea E) ((standardMap k)^[n]) p ≤
      (Real.log 1000+(∫ z,Real.log ‖standardDerivativeProduct k z n‖ ∂area))/(area.real E) := by
  let := normalizedArea_probability hE
  have hc : 0 < area.real E := ENNReal.toReal_pos hE.ne' (measure_ne_top area E)
  have hweighted := Entropy.rate_weighted_le area (normalizedArea E) hc
    (area_weight_normalized E hE) _ ((measurePreserving_standardMap k).iterate n) hinv p hp
  have hu := (finite_rate_le_metricEntropy area _ ((measurePreserving_standardMap k).iterate n) p hp).trans
    (metricEntropy_iterate_derivative_upper k hk n)
  have hnonneg : 0 ≤ Real.log 1000+(∫ z,Real.log ‖standardDerivativeProduct k z n‖ ∂area) := by
    apply add_nonneg (Real.log_nonneg (by norm_num))
    apply integral_nonneg
    intro z
    exact Real.log_nonneg (standardDerivativeProduct_norm_ge_one k z n)
  have hh := (ENNReal.ofReal_le_ofReal_iff hnonneg).mp hu
  apply (le_div_iff₀ hc).mpr
  simpa only [mul_comm] using hweighted.trans hh
end StandardMapEntropy

end
section
namespace HyperbolicCoding
open MeasureTheory Set Filter StandardMapEntropy.Entropy
open scoped BigOperators ENNReal Topology
variable {X : Type*} [MeasurableSpace X]

theorem exists_finiteRateSupremum (μ : Measure X) (f : X → X)
    (b : ℝ) (hb : ∀ r : ℕ,∀ p : X → Fin (r+1),Measurable p → rate μ f p ≤ b) :
    ∃ h : ℝ,FiniteRateSupremum μ f h ∧ h ≤ b := by
  let S : Set ℝ := {v | ∃ r : ℕ,∃ p : X → Fin (r+1),Measurable p ∧ v=rate μ f p}
  have hne : S.Nonempty := ⟨rate μ f (fun _ => (0 : Fin 1)),0,fun _ => 0,measurable_const,rfl⟩
  have hbd : BddAbove S := ⟨b,by rintro v ⟨r,p,hp,rfl⟩; exact hb r p hp⟩
  refine ⟨sSup S,⟨?_,?_⟩,csSup_le hne (by rintro v ⟨r,p,hp,rfl⟩; exact hb r p hp)⟩
  · intro r p hp
    exact le_csSup hbd ⟨r,p,hp,rfl⟩
  · intro ε hε
    obtain ⟨v,hv,hlt⟩ := exists_lt_of_lt_csSup hne (show sSup S-ε < sSup S by linarith)
    obtain ⟨r,p,hp,rfl⟩ := hv
    exact ⟨r,p,hp,hlt⟩
end HyperbolicCoding
end

end OAI