Skip to content

Commit 67a9ec3

Browse files
committed
small namespace and proof cleanup
1 parent 64384f0 commit 67a9ec3

2 files changed

Lines changed: 44 additions & 97 deletions

File tree

Probability/Probability/Basic.lean

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import Mathlib.Data.Fintype.BigOperators
88
# Basic properties for probability spaces and expectations
99
1010
The main results:
11-
- Arithmetic manipulations of random variables
12-
- The law of total probabilities
11+
- LOTUS: The law of the unconscious statistician
1312
- The law of total expectations
13+
- The law of total probabilities
1414
-/
1515

1616
namespace Findist
1717

1818
variable {n : ℕ} {P : Findist n} {B : FinRV n Bool}
1919

2020
theorem ge_zero : 0 ≤ ℙ[B // P] :=
21-
by rw [Ex.prob_eq_exp_ind]
21+
by rw [prob_eq_exp_ind]
2222
calc 0 = 𝔼[0 // P] := exp_const.symm
2323
_ ≤ 𝔼[𝕀 ∘ B//P] := exp_monotone ind_nneg
2424

2525

2626
theorem le_one : ℙ[B // P] ≤ 1 :=
27-
by rw [Ex.prob_eq_exp_ind]
27+
by rw [prob_eq_exp_ind]
2828
calc 𝔼[𝕀 ∘ B//P] ≤ 𝔼[1 // P] := exp_monotone ind_le_one
2929
_ = 1 := exp_const
3030

@@ -34,33 +34,16 @@ end Findist
3434

3535
------------------------------ Probability ---------------------------
3636

37-
namespace Pr
38-
3937
variable {n : ℕ} {P : Findist n} {B C : FinRV n Bool}
4038

4139
theorem prob_compl_sums_to_one : ℙ[B // P] + ℙ[¬ᵣB // P] = 1 :=
42-
by rw [Ex.prob_eq_exp_ind, Ex.prob_eq_exp_ind, ←exp_dists_add, one_of_ind_bool_or_not]
40+
by rw [prob_eq_exp_ind, prob_eq_exp_ind, ←exp_dists_add, one_of_ind_bool_or_not]
4341
exact exp_one
4442

45-
4643
theorem prob_compl_one_minus : ℙ[¬ᵣB // P] = 1 - ℙ[B // P] :=
4744
by rw [←prob_compl_sums_to_one (P:=P) (B:=B)]; ring
4845

4946

50-
------------------------------ Expectation ---------------------------
51-
52-
namespace PMF
53-
54-
variable {n : ℕ} {k : ℕ} {L : FinRV n (Fin k)}
55-
variable {pmf : Fin k → ℚ} {P : Findist n}
56-
57-
theorem pmf_rv_k_ge_1 (h : PMF pmf P L) : 0 < k :=
58-
match k with
59-
| Nat.zero => Fin.pos <| L ⟨0,P.nonempty⟩
60-
| Nat.succ k₂ => Nat.zero_lt_succ k₂
61-
62-
end PMF
63-
6447
------------------------------ Expectation ---------------------------
6548

6649
namespace Ex
@@ -69,8 +52,8 @@ variable {n : ℕ} {P : Findist n}
6952
variable {k : ℕ} {X : FinRV n ℚ} {B : FinRV n Bool} {L : FinRV n (Fin k)}
7053
variable (g : Fin k → ℚ)
7154

72-
/-- LOTUS: the law of the unconscious statistician (or similar) -/
73-
theorem LOTUS (g : Fin k → ℚ) : 𝔼[g ∘ L // P ] = ∑ i, ℙ[L =ᵣ i // P] * (g i) :=
55+
/-- LOTUS: The law of the unconscious statistician (or similar) -/
56+
theorem LOTUS : 𝔼[g ∘ L // P ] = ∑ i, ℙ[L =ᵣ i // P] * (g i) :=
7457
by rewrite [exp_decompose (X := g ∘ L) (L := L) ]
7558
apply Fintype.sum_congr
7659
intro i
@@ -93,52 +76,16 @@ theorem law_total_exp : 𝔼[𝔼[X |ᵣ L // P] // P] = 𝔼[X // P] :=
9376

9477
end Ex
9578

96-
97-
98-
-- TODO: I think that we can show the following results from the law of total expectations
99-
100-
--TODO: theorem law_of_total_probs_bool : ℙ[B // P] = ℙ[B * C // P] + ℙ[B * (¬ᵣC) // P] :=
101-
/- by
102-
unfold probability
103-
have h : ∀ i : Fin n, (𝕀 (B i)) = (𝕀 (B i * C i)) + (𝕀 (B i * (¬ᵣ C) i)) :=
104-
by
105-
intro i
106-
by_cases hB : B i
107-
· by_cases hC : C i
108-
· simp [hB, hC, FinRV.not, indicator]
109-
· simp [hB, hC, FinRV.not, indicator]
110-
· by_cases hC : C i
111-
· simp [hB, hC, FinRV.not, indicator]
112-
· simp [hB, hC, FinRV.not, indicator]
113-
sorry ---I tried to do this proof but got stuck, feel free to delete my work
114-
-/
115-
116-
--TODO: theorem conditional_total (h : 0 < ℙ[C // P]) : ℙ[B * C // P] = ℙ[B | C // P] * ℙ[C // P] :=
117-
-- by simp [probability_cnd] at ⊢ h
118-
-- have : P.ℙ.iprodb C * (P.ℙ.iprodb C)⁻¹ = 1 :=
119-
-- Rat.mul_inv_cancel (P.ℙ.iprodb C) (Ne.symm (ne_of_lt h))
120-
-- calc
121-
-- P.ℙ.iprodb (B ∧ᵣC) = P.ℙ.iprodb (B ∧ᵣC) * 1 := by ring
122-
-- _ = P.ℙ.iprodb (B ∧ᵣC) * (P.ℙ.iprodb C * (P.ℙ.iprodb C)⁻¹) := by rw [←this]
123-
-- _ = P.ℙ.iprodb (B ∧ᵣ C) / P.ℙ.iprodb C * P.ℙ.iprodb C := by ring
124-
125-
126-
--TODO: theorem law_total_prbs_cnd (h1 : 0 < ℙ[C // P]) (h2 : ℙ[C // P] < 1)
127-
--: ℙ[B // P] = ℙ[B | C // P] * ℙ[ C // P] + ℙ[B | ¬ᵣC // P] * ℙ[¬ᵣC // P] :=
128-
-- by have h2' : 0 < ℙ[¬ᵣC // P] := by rw [prob_compl_one_minus]; linarith
129-
-- rw [←conditional_total P B C h1]
130-
-- rw [←conditional_total P B (¬ᵣC) h2']
131-
-- exact law_of_total_probs_bool P B C
79+
namespace Pr
13280

13381
variable {k : ℕ} {L : FinRV n (Fin k)}
13482

135-
13683
/-- The law of total probabilities -/
13784
theorem law_of_total_probs : ℙ[B // P] = ∑ i, ℙ[B * (L =ᵣ i) // P] :=
138-
by rewrite [Ex.prob_eq_exp_ind, rv_decompose (𝕀∘B) L, exp_additive]
85+
by rewrite [prob_eq_exp_ind, rv_decompose (𝕀∘B) L, exp_additive]
13986
apply Fintype.sum_congr
14087
intro i
141-
rewrite [Ex.prob_eq_exp_ind]
88+
rewrite [prob_eq_exp_ind]
14289
apply exp_congr
14390
ext ω
14491
by_cases h1 : L ω = i

Probability/Probability/Defs.lean

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ variable {k : ℕ} {L : FinRV n (Fin k)}
157157
theorem indi_eq_indr : ∀i : Fin k, (𝕀 ∘ (L =ᵣ i)) = (L =ᵢ i) := by
158158
intro i; unfold FinRV.eq FinRV.eqi 𝕀 indicator; ext ω; by_cases h: L ω = i; repeat simp [h]
159159

160-
161160
variable {B : FinRV n Bool}
162161
/-- Indicator is 0 or 1 -/
163-
theorem ind_zero_one : ∀ ω, (𝕀∘B) ω = 1 ∨ (𝕀∘B) ω = 0 := by
162+
theorem ind_zero_one : ∀ ω, (𝕀∘B) ω = 1 ∨ (𝕀∘B) ω = 0 := by
164163
intro ω
165164
by_cases h : B ω
166165
· left; simp only [Function.comp_apply, h, indicator]
@@ -190,14 +189,12 @@ theorem one_of_ind_bool_or_not : (𝕀∘B) + (𝕀∘(¬ᵣ B)) = (1 : FinRV n
190189
· simp [h]
191190
· simp [h]
192191

193-
variable {X Y: FinRV n ℚ}
192+
variable {X Y: FinRV n ℚ} {Xs : Fin k → FinRV n ℚ}
194193

195194
theorem rv_le_abs : X ≤ abs ∘ X := by intro i; simp [le_abs_self (X i)]
196195

197-
theorem rv_prod_sum_additive {Xs : Fin k → FinRV n ℚ} : ∑ i, Y * (Xs i) = Y * (∑ i, Xs i) :=
198-
by ext ω
199-
simp
200-
rw [Finset.mul_sum]
196+
theorem rv_prod_sum_additive : ∑ i, Y * (Xs i) = Y * (∑ i, Xs i) :=
197+
by ext ω; simp [Finset.mul_sum]
201198

202199
variable {g : Fin k → ℚ}
203200

@@ -221,9 +218,6 @@ notation "ℙ[" B "//" P "]" => probability P B
221218
-- helps to refold is when needed
222219
lemma probability_def : P.p ⬝ᵥ (𝕀 ∘ B) = ℙ[B // P] := rfl
223220

224-
-- TODO: the sorry in the definition has to do with the decidability of the membership
225-
--theorem prob_iprod_eq_def : ℙ[B // P] = P.measure (B.preimage true) sorry := sorry
226-
227221
/-- Conditional probability of B -/
228222
def probability_cnd : ℚ := ℙ[B * C // P] / ℙ[ C // P ]
229223

@@ -234,8 +228,7 @@ notation "ℙ[" B "|" C "//" P "]" => probability_cnd P B C
234228

235229
theorem prob_one_of_true : ℙ[1 // P] = 1 :=
236230
by unfold probability
237-
rw[one_of_true]
238-
rw [dotProduct_comm]
231+
rewrite [one_of_true, dotProduct_comm]
239232
exact P.prob
240233

241234
example {a b : ℚ} (h : 0 ≤ a) (h2 : 0 ≤ b) : 0 ≤ a * b := Rat.mul_nonneg h h2
@@ -245,7 +238,6 @@ variable {P : Findist n} {B : FinRV n Bool}
245238
theorem prod_zero_of_prob_zero : ℙ[B // P] = 0 → (P.p * (𝕀∘B) = 0) := by
246239
intro h; exact prod_eq_zero_of_nneg_dp_zero P.nneg ind_nneg h
247240

248-
249241
------------------------------ PMF ---------------------------
250242

251243
/-- Proof that p is a the PMF of X on probability space P -/
@@ -254,6 +246,15 @@ def PMF {K : ℕ} (pmf : Fin K → ℚ) (P : Findist n) (L : FinRV n (Fin K)) :=
254246

255247
namespace PMF
256248

249+
variable {n : ℕ} {k : ℕ} {L : FinRV n (Fin k)}
250+
variable {pmf : Fin k → ℚ} {P : Findist n}
251+
252+
theorem pmf_rv_k_ge_1 (h : PMF pmf P L) : 0 < k :=
253+
match k with
254+
| Nat.zero => Fin.pos <| L ⟨0,P.nonempty⟩
255+
| Nat.succ k₂ => Nat.zero_lt_succ k₂
256+
257+
257258

258259
end PMF
259260

@@ -270,7 +271,6 @@ Main results
270271
-/
271272

272273

273-
namespace Ex
274274

275275

276276
variable {n : ℕ} (P : Findist n) (X Y Z: FinRV n ℚ) (B : FinRV n Bool)
@@ -302,15 +302,15 @@ def expect_cnd_rv : Fin n → ℚ := fun i ↦ 𝔼[ X | L =ᵣ (L i) // P ]
302302

303303
notation "𝔼[" X "|ᵣ" L "//" P "]" => expect_cnd_rv P X L
304304

305-
end Ex
305+
306306
--- some basic properties
307307

308308
section Expectation_properties
309309
variable {P : Findist n} {X Y Z: FinRV n ℚ} {B : FinRV n Bool}
310310

311311
theorem exp_congr : (X = Y) → 𝔼[X // P] = 𝔼[Y // P] :=
312312
by intro h
313-
unfold Ex.expect dotProduct
313+
unfold expect dotProduct
314314
apply Fintype.sum_congr
315315
simp_all
316316

@@ -320,40 +320,40 @@ theorem exp_mul_comm : 𝔼[X * Y // P] = 𝔼[Y * X // P] := exp_congr (CommMon
320320
variable {c : ℚ} {p : Fin n → ℚ}
321321

322322
theorem exp_const : 𝔼[(fun _ ↦ c) // P] = c :=
323-
by unfold Ex.expect
324-
rw [rv_const_fun_to_one]
325-
simp only [dotProduct_smul, smul_eq_mul]
326-
rw [dotProduct_comm, P.prob]
327-
simp
323+
by unfold expect
324+
rw [rv_const_fun_to_one]
325+
simp only [dotProduct_smul, smul_eq_mul]
326+
rw [dotProduct_comm, P.prob]
327+
simp
328328

329329
theorem exp_one : 𝔼[ 1 // P] = 1 := exp_const
330330

331331
theorem exp_cond_eq_def : 𝔼[X | B // P] * ℙ[B // P] = 𝔼[X * (𝕀 ∘ B) // P] :=
332-
by unfold Ex.expect_cnd
333-
by_cases h: ℙ[B//P] = 0
334-
· rw [h, Rat.mul_zero]
335-
unfold Ex.expect
336-
rw [dotProd_hadProd_comm, dotProd_hadProd_rotate, prod_zero_of_prob_zero h]
337-
exact (dotProduct_zero X).symm
338-
· simp_all
332+
by unfold expect_cnd
333+
by_cases h: ℙ[B//P] = 0
334+
· rw [h, Rat.mul_zero]
335+
unfold expect
336+
rw [dotProd_hadProd_comm, dotProd_hadProd_rotate, prod_zero_of_prob_zero h]
337+
exact (dotProduct_zero X).symm
338+
· simp_all
339339

340340

341341
lemma constant_mul_eq_smul : (fun ω ↦ c * X ω) = c • X := rfl
342342

343343
theorem exp_prod_const_fun : 𝔼[(λ _ ↦ c) * X // P] = c * 𝔼[X // P] :=
344-
by simp only [Ex.expect, Pi.mul_def, constant_mul_eq_smul, dotProduct_smul, smul_eq_mul]
344+
by simp only [expect, Pi.mul_def, constant_mul_eq_smul, dotProduct_smul, smul_eq_mul]
345345

346346
theorem exp_indi_eq_exp_indr : ∀i : Fin k, 𝔼[L =ᵢ i // P] = 𝔼[𝕀 ∘ (L =ᵣ i) // P] := by
347347
intro i; rw [indi_eq_indr]
348348

349349
/-- Expectation is homogeneous under product -/
350-
theorem exp_homogenous : 𝔼[c • X // P] = c * 𝔼[X // P] := by simp only [Ex.expect, dotProduct_smul, smul_eq_mul]
350+
theorem exp_homogenous : 𝔼[c • X // P] = c * 𝔼[X // P] := by simp only [expect, dotProduct_smul, smul_eq_mul]
351351

352-
theorem exp_dists_add : 𝔼[X + Y // P] = 𝔼[X // P] + 𝔼[Y // P] := by simp [Ex.expect]
352+
theorem exp_dists_add : 𝔼[X + Y // P] = 𝔼[X // P] + 𝔼[Y // P] := by simp [expect]
353353

354354
/-- Additivity of expectation --/
355355
theorem exp_additive {m : ℕ} (Xs : Fin m → FinRV n ℚ) : 𝔼[∑ i : Fin m, Xs i // P] = ∑ i : Fin m, 𝔼[Xs i // P] :=
356-
by unfold Ex.expect; exact dotProduct_sum P.p Finset.univ Xs
356+
by unfold expect; exact dotProduct_sum P.p Finset.univ Xs
357357

358358
/-- Expectation is monotone -/
359359
theorem exp_monotone (h: X ≤ Y) : 𝔼[X // P] ≤ 𝔼[Y // P] := dotProduct_le_dotProduct_of_nonneg_left h P.nneg
@@ -371,9 +371,9 @@ theorem exp_decompose : 𝔼[X // P] = ∑ i, 𝔼[X * (L =ᵢ i) // P] :=
371371
/-- Expectation of a conditional constant. Only when probability is positive. -/
372372
theorem exp_cond_const : ∀ i, ℙ[L =ᵣ i // P] ≠ 0 → 𝔼[g ∘ L | L =ᵣ i // P] = g i :=
373373
by intro i h
374-
unfold Ex.expect_cnd
374+
unfold expect_cnd
375375
rw [indi_eq_indr, rv_prod_const i, exp_homogenous]
376-
rw [←indi_eq_indr, ←Ex.prob_eq_exp_ind]
376+
rw [←indi_eq_indr, ←prob_eq_exp_ind]
377377
simp only [h, ne_eq, isUnit_iff_ne_zero, not_false_eq_true, IsUnit.mul_div_cancel_right]
378378

379379
end Expectation_properties

0 commit comments

Comments
 (0)