Skip to content

Commit 76a6b0b

Browse files
authored
Merge pull request #33 from formalproofs/lotus
provisional proof of lotus
2 parents 9031777 + 5fd37ce commit 76a6b0b

2 files changed

Lines changed: 103 additions & 27 deletions

File tree

Probability/Probability/Basic.lean

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ lemma refold_probability : P.p ⬝ᵥ (𝕀 ∘ B) = ℙ[B // P] := rfl
4444

4545
theorem law_of_total_probs_bool : ℙ[B // P] = ℙ[B * C // P] + ℙ[B * (¬ᵣC) // P] :=
4646
by
47-
unfold Pr.probability
47+
unfold probability
4848
have h : ∀ i : Fin n, (𝕀 (B i)) = (𝕀 (B i * C i)) + (𝕀 (B i * (¬ᵣ C) i)) :=
4949
by
5050
intro i
@@ -78,6 +78,7 @@ theorem law_total_prbs_cnd (h1 : 0 < ℙ[C // P]) (h2 : ℙ[C // P] < 1)
7878

7979
variable {k : ℕ} {L : FinRV n (Fin k)}
8080

81+
-- TODO: we will prove this from the law for expectations
8182
theorem law_of_total_probs : ∑ i : Fin k, ℙ[B * (L =ᵣ i) // P] = ℙ[B // P] := sorry
8283

8384
end Pr
@@ -87,8 +88,7 @@ end Pr
8788
namespace PMF
8889

8990
variable {n : ℕ} {k : ℕ} {L : FinRV n (Fin k)}
90-
variable {pmf : Fin k → ℚ}
91-
variable {P : Findist n}
91+
variable {pmf : Fin k → ℚ} {P : Findist n}
9292

9393
theorem pmf_rv_k_ge_1 (h : PMF pmf P L) : 0 < k :=
9494
match k with
@@ -113,14 +113,21 @@ example (f g : Fin k → ℚ) (h : f = g) : ∑ i, f i = ∑ i, g i := by
113113
rw [←h]
114114

115115

116-
theorem prob_eq_exp_ind : ℙ[B // P] = 𝔼[𝕀 ∘ B // P] := sorry
117-
118116
-- TODO: The following derivations should be our focus
119117

120118
---- STEP 1:
121-
variable (g : Fin k → ℚ)
122119

123-
--abbrev 𝕀ᵣ (B : FinRV n Bool) : FinRV n ℚ := fun ω => 𝕀 (B ω)
120+
/-- Pi.single is an indicator for the random variable -/
121+
theorem indicator_eq_single : ∀ ω : Fin n, (fun i ↦ (L =ᵢ i) ω) = Pi.single (L ω) (1:ℚ) :=
122+
by intro ω
123+
simp [Pi.single]
124+
ext i
125+
simp [Function.update]
126+
by_cases h : L ω = i
127+
· simp [h]
128+
· simp [h]; exact fun a ↦ h a.symm
129+
130+
variable (g : Fin k → ℚ)
124131

125132
theorem fin_sum_g: ∀ ω, ∑ i, (g i) * (𝕀 ∘ (L =ᵣ i)) ω = g (L ω) := by
126133
intro ω
@@ -138,8 +145,30 @@ theorem fin_sum_g: ∀ ω, ∑ i, (g i) * (𝕀 ∘ (L =ᵣ i)) ω = g (L ω) :=
138145
· intro b _ hneq
139146
exact h1 b hneq.symm
140147

148+
variable {ρ : Type} [AddCommMonoid ρ]
149+
150+
/-- Linearity of expectation --/
151+
theorem expect_linear {m : ℕ} (Xs : Fin m → FinRV n ℚ) : 𝔼[∑ i : Fin m, Xs i // P] = ∑ i : Fin m, 𝔼[Xs i // P] :=
152+
by unfold expect
153+
exact dotProduct_sum P.p Finset.univ Xs
154+
155+
/-- Decompose a random variable to a sum of constant variables with indicators -/
156+
theorem fin_sum_simple : (g ∘ L) = ∑ i, (fun _ ↦ g i) * (L =ᵢ i) :=
157+
by ext ω
158+
simp
159+
141160
theorem idktheorem (P : Findist n) (L : FinRV n (Fin k)) (g : Fin k → ℚ) :
142-
𝔼[g ∘ L // P] = ∑ i : Fin k, g i * ℙ[L =ᵣ i // P] := sorry
161+
𝔼[g ∘ L // P] = ∑ i : Fin k, g i * ℙ[L =ᵣ i // P] := by
162+
rw [fin_sum_simple]
163+
rw [expect_linear]
164+
apply Fintype.sum_congr
165+
intro a
166+
rw [exp_prod_const_fun]
167+
rw [prob_eq_exp_ind]
168+
rw [exp_indi_eq_exp_indr]
169+
170+
171+
-- TODO: just need the expectation of a constant function and then we are done!!!!
143172

144173
-- LOTUS: the law of the unconscious statistician (or similar)
145174
theorem LOTUS {g : Fin k → ℚ} (h : PMF pmf P L):
@@ -194,9 +223,6 @@ theorem fin_sum : ∀ ω : Fin n, ∑ i : Fin k, (𝕀 ∘ (L =ᵣ i)) ω = (1:
194223
theorem exp_eq_exp_cond_true : 𝔼[X // P] = 𝔼[X * (fun ω ↦ 1 ) // P] := sorry
195224

196225

197-
-- TODO: need to sum all probabilities
198-
199-
200226
example {f g : ℕ → ℚ} {m : ℕ} (h : ∀ n : ℕ, f n = g n) :
201227
∑ i : Fin m, f i = ∑ i : Fin m, g i :=
202228
by apply Finset.sum_congr

Probability/Probability/Defs.lean

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Probability.Probability.Prelude
22

33
import Mathlib.Data.Matrix.Mul -- dot product definitions and results
44
import 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

5668
section 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]
8395
theorem one_eq_true : (1:Bool) = true := rfl
@@ -104,6 +116,13 @@ def eq [DecidableEq ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n Bool :=
104116

105117
infix: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]
108127
def 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) :=
127146
end 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

146178
variable {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 -/
157189
def probability_cnd : ℚ := ℙ[B * C // P] / ℙ[ C // P ]
158190

191+
namespace Pr
159192

160193
theorem 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-
169197
theorem true_one : ℙ[ 1 // P] = 1 :=
170198
by unfold probability
171199
rw[one_of_true]
@@ -203,7 +231,10 @@ notation "𝔼[" X "//" P "]" => expect P X
203231
notation "𝔼[" 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
215246
notation "𝔼[" 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
220251
def expect_cnd_rv : Fin n → ℚ := fun i ↦ 𝔼[ X | L =ᵣ (L i) // P ]
221252

222253
notation "𝔼[" 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+
224274
end Ex

0 commit comments

Comments
 (0)