openAI-math-dataset / lean /OAI /LinearAlgebra /MatrixMultiplication /Entropy /ComplexFiniteEntropy.lean
Download lean/OAI/LinearAlgebra/MatrixMultiplication/Entropy/ComplexFiniteEntropy.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 6.33 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/LinearAlgebra/MatrixMultiplication/Entropy/ComplexFiniteEntropy.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/LinearAlgebra/MatrixMultiplication/Entropy/ComplexFiniteEntropy.lean
-
curl -L -o ComplexFiniteEntropy.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/LinearAlgebra/MatrixMultiplication/Entropy/ComplexFiniteEntropy.lean
6.33 kB
| import Mathlib.Analysis.SpecialFunctions.Log.Basic | |
| import Mathlib.Algebra.BigOperators.Fin | |
| import Mathlib.Algebra.BigOperators.Field | |
| import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise | |
| import Mathlib.Algebra.Order.BigOperators.Group.Finset | |
| import Mathlib.Tactic.FieldSimp | |
| import Mathlib.Tactic.Ring | |
| namespace OAI | |
| /-! Finite entropy, rate estimates and ordered asymptotic limits. -/ | |
| noncomputable section | |
| namespace MatrixMultiplication.Foundation | |
| open scoped BigOperators | |
| def entropyTerm (p : ℝ) : ℝ := -p * Real.log p | |
| def finiteEntropy {A : Type*} [Fintype A] (p : A → ℝ) : ℝ := ∑ a, entropyTerm (p a) | |
| theorem finiteEntropy_equiv {A B : Type*} [Fintype A] [Fintype B] | |
| (e : A ≃ B) (p : B → ℝ) : finiteEntropy (fun a => p (e a)) = finiteEntropy p := | |
| e.sum_comp (fun b => entropyTerm (p b)) | |
| @[simp] theorem entropyTerm_zero : entropyTerm 0 = 0 := by simp [entropyTerm] | |
| theorem entropyTerm_mul (p q : ℝ) : | |
| entropyTerm (p * q) = q * entropyTerm p + p * entropyTerm q := by | |
| by_cases hp : p = 0 | |
| · simp [hp] | |
| by_cases hq : q = 0 | |
| · simp [hq] | |
| rw [entropyTerm, Real.log_mul hp hq] | |
| unfold entropyTerm | |
| ring | |
| theorem finiteEntropy_chain {A B : Type*} [Fintype A] [Fintype B] | |
| (p : A → ℝ) (q : A → B → ℝ) (hq : ∀ a, ∑ b, q a b = 1) : | |
| finiteEntropy (fun ab : A × B => p ab.1 * q ab.1 ab.2) = | |
| finiteEntropy p + ∑ a, p a * finiteEntropy (q a) := by | |
| simp only [finiteEntropy, Fintype.sum_prod_type, entropyTerm_mul, | |
| Finset.sum_add_distrib, ← Finset.sum_mul, ← Finset.mul_sum, hq, one_mul] | |
| structure FiniteLaw (A : Type*) [Fintype A] where | |
| mass : A → ℝ | |
| nonneg : ∀ a, 0 ≤ mass a | |
| total : ∑ a, mass a = 1 | |
| namespace FiniteLaw | |
| variable {A B : Type*} [Fintype A] [Fintype B] | |
| def map (p : FiniteLaw A) (f : A → B) : FiniteLaw B := by | |
| classical | |
| exact | |
| { mass := fun b => ∑ a, if f a = b then p.mass a else 0 | |
| nonneg := fun b => Finset.sum_nonneg fun a _ => by | |
| split_ifs | |
| · exact p.nonneg a | |
| · exact le_rfl | |
| total := by | |
| rw [Finset.sum_comm] | |
| simpa using p.total } | |
| @[simp] theorem map_mass [DecidableEq B] (p : FiniteLaw A) (f : A → B) (b : B) : | |
| (p.map f).mass b = ∑ a, if f a = b then p.mass a else 0 := by | |
| classical | |
| dsimp only [map] | |
| apply Finset.sum_congr rfl | |
| intro a _ | |
| by_cases h : f a = b <;> simp only [h, ite_true, ite_false] | |
| theorem map_mass_apply (p : FiniteLaw A) (f : A → B) | |
| (hf : Function.Injective f) (a : A) : (p.map f).mass (f a) = p.mass a := by | |
| classical | |
| simp [map_mass, hf.eq_iff] | |
| theorem map_entropy_of_injective (p : FiniteLaw A) (f : A → B) | |
| (hf : Function.Injective f) : finiteEntropy (p.map f).mass = finiteEntropy p.mass := by | |
| classical | |
| have hterm (b : B) : entropyTerm ((p.map f).mass b) = | |
| ∑ a, if f a = b then entropyTerm (p.mass a) else 0 := by | |
| by_cases hb : ∃ a, f a = b | |
| · obtain ⟨a, rfl⟩ := hb | |
| rw [map_mass_apply p f hf] | |
| simp [hf.eq_iff] | |
| · simp [map_mass, not_exists.mp hb] | |
| unfold finiteEntropy | |
| simp_rw [hterm] | |
| rw [Finset.sum_comm] | |
| simp | |
| def joint (p : FiniteLaw A) (q : A → FiniteLaw B) : FiniteLaw (A × B) where | |
| mass ab := p.mass ab.1 * (q ab.1).mass ab.2 | |
| nonneg ab := mul_nonneg (p.nonneg ab.1) ((q ab.1).nonneg ab.2) | |
| total := by simp [Fintype.sum_prod_type, ← Finset.mul_sum, (q _).total, p.total] | |
| theorem joint_entropy (p : FiniteLaw A) (q : A → FiniteLaw B) : | |
| finiteEntropy (p.joint q).mass = | |
| finiteEntropy p.mass + ∑ a, p.mass a * finiteEntropy (q a).mass := | |
| finiteEntropy_chain p.mass (fun a => (q a).mass) (fun a => (q a).total) | |
| theorem mass_le_map_mass (p : FiniteLaw A) (f : A → B) (a : A) : | |
| p.mass a ≤ (p.map f).mass (f a) := by | |
| classical | |
| rw [map_mass] | |
| have h := Finset.single_le_sum | |
| (s := (Finset.univ : Finset A)) | |
| (f := fun a' => if f a' = f a then p.mass a' else 0) | |
| (fun a' _ => by | |
| split_ifs | |
| · exact p.nonneg a' | |
| · exact le_rfl) | |
| (Finset.mem_univ a) | |
| simpa using h | |
| def conditional (p : FiniteLaw A) (f : A → B) (b : B) : FiniteLaw A := by | |
| classical | |
| exact if hzero : (p.map f).mass b = 0 then p else | |
| { mass := fun a => (if f a = b then p.mass a else 0) / (p.map f).mass b | |
| nonneg := fun a => div_nonneg | |
| (by split_ifs; exact p.nonneg a; exact le_rfl) ((p.map f).nonneg b) | |
| total := by | |
| rw [← Finset.sum_div, ← map_mass p f b, div_self hzero] } | |
| theorem map_mass_mul_conditional [DecidableEq B] | |
| (p : FiniteLaw A) (f : A → B) (b : B) (a : A) : | |
| (p.map f).mass b * (p.conditional f b).mass a = | |
| if f a = b then p.mass a else 0 := by | |
| classical | |
| by_cases hzero : (p.map f).mass b = 0 | |
| · have ha : f a = b → p.mass a = 0 := by | |
| intro hab | |
| have hle := p.mass_le_map_mass f a | |
| rw [hab, hzero] at hle | |
| exact le_antisymm hle (p.nonneg a) | |
| rw [hzero, zero_mul] | |
| by_cases hab : f a = b | |
| · rw [ite_eq_left hab, ha hab] | |
| · rw [ite_eq_right hab] | |
| · simp only [conditional, hzero, ↓reduceDIte] | |
| by_cases hab : f a = b | |
| · simpa only [hab, ite_true] using (mul_div_cancel₀ (p.mass a) hzero) | |
| · simp only [hab, ite_false, zero_div, mul_zero] | |
| theorem entropy_eq_map_add_conditional (p : FiniteLaw A) (f : A → B) : | |
| finiteEntropy p.mass = finiteEntropy (p.map f).mass + | |
| ∑ b, (p.map f).mass b * finiteEntropy (p.conditional f b).mass := by | |
| classical | |
| let graph : A → B × A := fun a => (f a, a) | |
| have hinj : Function.Injective graph := fun _ _ h => congrArg Prod.snd h | |
| have hmass : ((p.map f).joint (p.conditional f)).mass = (p.map graph).mass := by | |
| funext ba | |
| rcases ba with ⟨b, a⟩ | |
| change (p.map f).mass b * (p.conditional f b).mass a = (p.map graph).mass (b, a) | |
| rw [map_mass_mul_conditional, map_mass, Finset.sum_eq_single a] | |
| · simp only [graph, Prod.mk.injEq, and_true] | |
| · intro x _ hxa | |
| simp only [graph, Prod.mk.injEq, hxa, and_false, ite_false] | |
| · intro ha | |
| exact (ha (Finset.mem_univ a)).elim | |
| calc | |
| finiteEntropy p.mass = finiteEntropy (p.map graph).mass := | |
| (p.map_entropy_of_injective graph hinj).symm | |
| _ = finiteEntropy ((p.map f).joint (p.conditional f)).mass := by rw [hmass] | |
| _ = _ := (p.map f).joint_entropy (p.conditional f) | |
| end FiniteLaw | |
| end MatrixMultiplication.Foundation | |
| end | |
| end OAI | |