@@ -2,6 +2,7 @@ import Probability.Probability.Prelude
22
33import Mathlib.Data.Matrix.Mul -- dot product definitions and results
44import Mathlib.Algebra.Notation.Pi.Defs -- operations on functions
5+ import Mathlib.Algebra.Module.PointwisePi -- for smul_pi
56
67--------------------------- Findist ---------------------------------------------------------------
78
@@ -47,11 +48,22 @@ end Findist
4748
4849--------------------------- Random Variable -------------------------------------------------------------------
4950
50- -- Here we define random variables as finitely supported vectors
51+ /-!
52+ Random variables are defined as function. The operations on random variables can be performed
53+ using the standard notation:
54+
55+ X + Y is elementwise addition
56+ X * Y is elementwise (Hadamard product)
57+ f ∘ X is composition
58+ c • X is scalar multiplication
59+
60+
61+ L =ᵣ i is a boolean indicator random variable
62+ L =ᵢ i is a ℚ indicator random variable
63+ L ≤ᵣ i is a bool indicator random variable
64+
65+ -/
5166
52- -- TODO: Or, better, define random variables as a Vector Space, or a Module.
53- -- see, for example: https://leanprover-community.github.io/mathlib4_docs/Mathlib/RingTheory/Finiteness/Defs.html#Module.Finite
54- -- see also: https://github.com/leanprover-community/mathlib4/blob/8666bd82efec40b8b3a5abca02dc9b24bbdf2652/Mathlib/Data/Fin/VecNotation.lean
5567
5668section RandomVariable
5769
@@ -77,7 +89,7 @@ instance instBoolOne : One Bool where one := true
7789@[simp] lemma bool_mul_ff : (false * false : Bool) = false := rfl
7890
7991
80- variable {A B : Bool}
92+ variable {A B : Bool}
8193
8294@[simp]
8395theorem one_eq_true : (1 :Bool) = true := rfl
@@ -104,6 +116,13 @@ def eq [DecidableEq ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n Bool :=
104116
105117infix :50 "=ᵣ" => FinRV.eq
106118
119+ /-- indicator version of equality -/
120+ @[simp]
121+ def eqi [DecidableEq ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n ℚ :=
122+ (fun ω ↦ if Y ω = y then 1 else 0 )
123+
124+ infix :50 "=ᵢ" => FinRV.eqi
125+
107126@[simp]
108127def leq [LE ρ] [DecidableLE ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n Bool :=
109128 (fun ω ↦ Y ω ≤ y)
@@ -127,12 +146,26 @@ def preimage (f : FinRV n ρ) : ρ → Set (Fin n) :=
127146end FinRV
128147
129148/-- Boolean indicator function -/
130- def indicator {τ : Type } [OfNat τ 0 ] [OfNat τ 1 ] (cond : Bool) : τ := cond.rec 0 1
149+ def indicator [OfNat ρ 0 ] [OfNat ρ 1 ] (cond : Bool) : ρ := cond.rec 0 1
150+
151+ abbrev 𝕀 [OfNat ρ 0 ] [OfNat ρ 1 ] : Bool → ρ := indicator
152+
153+ -- TODO: add the equivalence between 𝕀 ∘ (L =ᵣ i) and L =ᵢ i
154+
155+ variable {k : ℕ} {L : FinRV n (Fin k)}
156+
157+ theorem indi_eq_indr : ∀i : Fin k, (𝕀 ∘ (L =ᵣ i)) = (L =ᵢ i) := by
158+ intro i
159+ unfold FinRV.eq FinRV.eqi 𝕀 indicator
160+ ext ω
161+ by_cases h: L ω = i
162+ · simp [h]
163+ · simp [h]
131164
132- abbrev 𝕀 [OfNat τ 0 ] [OfNat τ 1 ] : Bool → τ := indicator
133165
134166/-- Indicator is 0 or 1 -/
135- theorem ind_zero_one (cond : τ → Bool) : ( (𝕀∘cond) ω = 1 ) ∨ ((𝕀∘cond) ω = 0 ) := by
167+ theorem ind_zero_one (cond : ρ → Bool) : ∀ ω, (𝕀∘cond) ω = 1 ∨ (𝕀∘cond) ω = 0 := by
168+ intro ω
136169 by_cases h : cond ω
137170 · left; simp only [Function.comp_apply, h, indicator]
138171 · right; simp only [Function.comp_apply, h, indicator]
@@ -141,7 +174,6 @@ end RandomVariable
141174
142175------------------------------ Probability ---------------------------
143176
144- namespace Pr
145177
146178variable {n : ℕ} (P : Findist n) (B C : FinRV n Bool)
147179
@@ -156,16 +188,12 @@ notation "ℙ[" B "//" P "]" => probability P B
156188/-- Conditional probability of B -/
157189def probability_cnd : ℚ := ℙ[B * C // P] / ℙ[ C // P ]
158190
191+ namespace Pr
159192
160193theorem one_of_true : 𝕀 ∘ (1 : Fin n → Bool) = (1 : Fin n → ℚ) :=
161194 by ext
162195 simp [𝕀, indicator]
163196
164-
165- --#synth (OfNat Bool 1)
166- --#check One.toOfNat1
167-
168-
169197theorem true_one : ℙ[ 1 // P] = 1 :=
170198 by unfold probability
171199 rw[one_of_true]
@@ -203,7 +231,10 @@ notation "𝔼[" X "//" P "]" => expect P X
203231notation "𝔼[" PX "]" => expect PX.1 PX.2
204232
205233--theorem exp_eq_correct : 𝔼[X // P] = ∑ v ∈ ((List.finRange P.length).map X).toFinset, v * ℙ[ X =ᵣ v // P]
206- --:= sorry
234+
235+ @[simp]
236+ theorem prob_eq_exp_ind : ℙ[B // P] = 𝔼[𝕀 ∘ B // P] :=
237+ by simp only [expect, probability]
207238
208239
209240/-- Conditional expectation -/
@@ -214,11 +245,30 @@ notation "𝔼[" X "|" B "//" P "]" => expect_cnd P X B
214245-- expectation for a joint probability space and random variable
215246notation "𝔼[" PX "|" B "]" => expect_cnd PX.1 PX.2 B
216247
217- variable {K : ℕ} (L : FinRV n (Fin K ))
248+ variable {k : ℕ} (L : FinRV n (Fin k ))
218249
219250-- creates a random variable
220251def expect_cnd_rv : Fin n → ℚ := fun i ↦ 𝔼[ X | L =ᵣ (L i) // P ]
221252
222253notation "𝔼[" X "|ᵣ" L "//" P "]" => expect_cnd_rv P X L
223254
255+ --- some basic properties
256+
257+ theorem exp_dists_add : 𝔼[X + Y // P] = 𝔼[X // P] + 𝔼[Y // P] := by simp [expect]
258+
259+ variable {c : ℚ}
260+
261+ theorem exp_prod_const : 𝔼[c • X // P] = c * 𝔼[X // P] := by simp only [expect, dotProduct_smul, smul_eq_mul]
262+
263+ lemma constant_mul_eq_smul : (fun ω ↦ c * X ω) = c • X := rfl
264+
265+ theorem exp_prod_const_fun : 𝔼[(λ _ ↦ c) * X // P] = c * 𝔼[X // P] :=
266+ by simp only [expect, Pi.mul_def, constant_mul_eq_smul, dotProduct_smul, smul_eq_mul]
267+
268+
269+ theorem exp_indi_eq_exp_indr : ∀i : Fin k, 𝔼[L =ᵢ i // P] = 𝔼[𝕀 ∘ (L =ᵣ i) // P] := by
270+ intro i
271+ rw [indi_eq_indr]
272+
273+
224274end Ex
0 commit comments