Skip to content

Commit b23758a

Browse files
committed
Merge branch 'lotus'
2 parents 715b08c + f1255fd commit b23758a

2 files changed

Lines changed: 141 additions & 57 deletions

File tree

Probability/MDP/Risk.lean

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Probability.Probability.Basic
2+
3+
namespace Risk
4+
5+
open Findist FinRV
6+
7+
variable {n : ℕ}
8+
9+
def cdf (P : Findist n) (X : FinRV n ℚ) (t : ℚ) : ℚ := ℙ[X ≤ᵣ t // P]
10+
11+
/-- Finite set of values taken by a random variable X : Fin n → ℚ. -/
12+
def rangeOfRV (X : FinRV n ℚ) : Finset ℚ := Finset.univ.image X
13+
14+
/-- Value-at-Risk of X at level α: VaR_α(X) = min { t ∈ X(Ω) | P[X ≤ t] ≥ α }.
15+
If we assume 0 ≤ α ∧ α ≤ 1, then the "else 0" branch is never used. -/
16+
17+
def VaR (P : Findist n) (X : FinRV n ℚ) (α : ℚ) : ℚ :=
18+
let S : Finset ℚ := (rangeOfRV X).filter (fun t => cdf P X t ≥ α)
19+
if h : S.Nonempty then
20+
S.min' h
21+
else
22+
0
23+
24+
notation "VaR[" α "," X "//" P "]" => VaR P X α
25+
26+
theorem VaR_monotone (P : Findist n) (X Y : FinRV n ℚ) (α : ℚ)
27+
(hXY : ∀ ω, X ω ≤ Y ω) : VaR P X α ≤ VaR P Y α := by
28+
have hcdf : ∀ t : ℚ, cdf P X t ≤ cdf P Y t := by
29+
intro t
30+
have h_ind : (𝕀 ∘ (Y ≤ᵣ t)) ≤ (𝕀 ∘ (X ≤ᵣ t)) := by
31+
intro ω
32+
have h1 : Y ω ≤ t → X ω ≤ t := by
33+
intro hY
34+
exact le_trans (hXY ω) hY
35+
by_cases hY : Y ω ≤ t
36+
· have hX : X ω ≤ t := by exact h1 hY
37+
simp [𝕀, indicator, FinRV.leq, hY, hX]
38+
· simp [𝕀, indicator, FinRV.leq, hY]
39+
simp [cdf]
40+
41+
sorry
42+
sorry
43+
44+
theorem VaR_translation_invariant (P : Findist n) (X : FinRV n ℚ) (α c : ℚ) :
45+
VaR P (fun ω => X ω + c) α = VaR P X α + c := sorry
46+
47+
theorem VaR_positive_homog (P : Findist n) (X : FinRV n ℚ) (α c : ℚ)
48+
(hc : c > 0) : VaR P (fun ω => c * X ω) α = c * VaR P X α := sorry
49+
50+
51+
/-- Tail indicator: 1 if X(ω) > t, else 0. -/
52+
def tailInd (X : FinRV n ℚ) (t : ℚ) : FinRV n ℚ :=
53+
fun ω => if X ω > t then 1 else 0
54+
55+
/-- Conditional Value-at-Risk (CVaR) of X at level α under P.
56+
CVaR = E[X * I[X > VaR] ] / P[X > VaR]
57+
If the tail probability is zero, CVaR is defined to be 0.
58+
-/
59+
def CVaR (P : Findist n) (X : FinRV n ℚ) (α : ℚ) : ℚ :=
60+
let v := VaR P X α
61+
let B : FinRV n ℚ := tailInd X v
62+
let num := 𝔼[X * B // P]
63+
let den := ℙ[X >ᵣ v // P]
64+
if _ : den = 0 then
65+
0
66+
else
67+
num / den
68+
69+
notation "CVaR[" α "," X "//" P "]" => CVaR P X α
70+
71+
--TODO: prove...
72+
-- monotonicity: (∀ ω, X ω ≤ Y ω) → CVaR[α, X // P] ≤ CVaR[α, Y // P]
73+
-- translation: CVaR[α, (fun ω => X ω + c) // P] = CVaR[α, X // P] + c
74+
-- positive homogeneity: c > 0 → CVaR[α, (fun ω => c * X ω) // P] = c * CVaR[α, X // P]
75+
-- convexity
76+
-- CVaR ≥ VaR: CVaR[α, X // P] ≥ VaR[α, X // P]
77+
78+
end Risk

Probability/Probability/Defs.lean

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Probability.Probability.Prelude
33
import Mathlib.Data.Matrix.Mul -- dot product definitions and results
44
import Mathlib.Algebra.Notation.Pi.Defs -- operations on functions
55
import Mathlib.Algebra.Module.PointwisePi -- for smul_pi
6-
import Mathlib.LinearAlgebra.Matrix.DotProduct -- for monotonicity
6+
import Mathlib.LinearAlgebra.Matrix.DotProduct -- for monotonicity
77

88
--------------------------- Findist ---------------------------------------------------------------
99

@@ -14,7 +14,7 @@ variable {n : ℕ}
1414
structure Findist (n : ℕ) : Type where
1515
p : Fin n → ℚ
1616
prob : 1 ⬝ᵥ p = 1
17-
nneg : 0 ≤ p
17+
nneg : 0 ≤ p
1818

1919
namespace Findist
2020

@@ -29,12 +29,12 @@ def singleton : Findist 1 :=
2929

3030

3131
@[simp]
32-
def length (_ : Findist n) := n
32+
def length (_ : Findist n) := n
3333

34-
variable {n : ℕ}
34+
variable {n : ℕ}
3535

36-
theorem nonempty (P : Findist n) : P.length > 0 :=
37-
by cases n
36+
theorem nonempty (P : Findist n) : P.length > 0 :=
37+
by cases n
3838
· have := P.prob; simp_all only [Matrix.dotProduct_of_isEmpty, zero_ne_one]
3939
· simp only [length, gt_iff_lt, lt_add_iff_pos_left, add_pos_iff, zero_lt_one, or_true]
4040

@@ -50,7 +50,7 @@ end Findist
5050
--------------------------- Random Variable -------------------------------------------------------------------
5151

5252
/-!
53-
Random variables are defined as function. The operations on random variables can be performed
53+
Random variables are defined as function. The operations on random variables can be performed
5454
using the standard notation:
5555
5656
- X + Y is elementwise addition
@@ -60,12 +60,12 @@ using the standard notation:
6060
6161
6262
- L =ᵣ i is a boolean indicator random variable
63-
- L =ᵢ i is a ℚ indicator random variable
64-
- L ≤ᵣ i is a bool indicator random variable
63+
- L =ᵢ i is a ℚ indicator random variable
64+
- L ≤ᵣ i is a bool indicator random variable
6565
66-
Main results
66+
Main results
6767
68-
- Hadamard product is linear: Y * (∑ i, Xs i) = ∑ i, Y * (Xs i)
68+
- Hadamard product is linear: Y * (∑ i, Xs i) = ∑ i, Y * (Xs i)
6969
-/
7070

7171

@@ -133,6 +133,12 @@ def leq [LE ρ] [DecidableLE ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n Bool :=
133133

134134
infix:50 "≤ᵣ" => FinRV.leq
135135

136+
@[simp]
137+
def gt [LT ρ] [DecidableLT ρ] (Y : FinRV n ρ) (y : ρ) : FinRV n Bool :=
138+
fun ω ↦ Y ω > y
139+
140+
infix:50 ">ᵣ" => FinRV.gt
141+
136142
example (m n : ℕ) : (m < n) ∨ (m = n) ∨ (m > n) := Nat.lt_trichotomy m n
137143

138144
/-- Shows equivalence when extending the random variable to another element. -/
@@ -158,16 +164,16 @@ abbrev 𝕀 [OfNat ρ 0] [OfNat ρ 1] : Bool → ρ := indicator
158164

159165
variable {k : ℕ} {L : FinRV n (Fin k)}
160166

161-
theorem indi_eq_indr : ∀i : Fin k, (𝕀 ∘ (L =ᵣ i)) = (L =ᵢ i) := by
162-
intro i
163-
unfold FinRV.eq FinRV.eqi 𝕀 indicator
164-
ext ω
165-
by_cases h: L ω = i
167+
theorem indi_eq_indr : ∀i : Fin k, (𝕀 ∘ (L =ᵣ i)) = (L =ᵢ i) := by
168+
intro i
169+
unfold FinRV.eq FinRV.eqi 𝕀 indicator
170+
ext ω
171+
by_cases h: L ω = i
166172
· simp [h]
167173
· simp [h]
168174

169175

170-
variable {B : FinRV n Bool}
176+
variable {B : FinRV n Bool}
171177
/-- Indicator is 0 or 1 -/
172178
theorem ind_zero_one : ∀ ω, (𝕀∘B) ω = 1 ∨ (𝕀∘B) ω = 0 := by
173179
intro ω
@@ -176,40 +182,40 @@ theorem ind_zero_one : ∀ ω, (𝕀∘B) ω = 1 ∨ (𝕀∘B) ω = 0 := by
176182
· right; simp only [Function.comp_apply, h, indicator]
177183

178184
/-- Indicator is 0 or 1 -/
179-
theorem ind_nneg : (0 : FinRV n ℚ) ≤ 𝕀∘B := by
185+
theorem ind_nneg : (0 : FinRV n ℚ) ≤ 𝕀∘B := by
180186
intro ω
181187
simp [𝕀, indicator]
182188
by_cases h : B ω
183-
· simp [h]
184-
· simp [h]
189+
· simp [h]
190+
· simp [h]
185191

186-
theorem ind_le_one : 𝕀∘B ≤ (1 : FinRV n ℚ) :=
192+
theorem ind_le_one : 𝕀∘B ≤ (1 : FinRV n ℚ) :=
187193
by unfold 𝕀 indicator
188194
intro ω
189195
by_cases h : B ω
190196
· simp [h]
191-
· simp [h]
197+
· simp [h]
192198

193199
theorem one_of_true : 𝕀 ∘ (1 : Fin n → Bool) = (1 : Fin n → ℚ) := by ext; simp [𝕀, indicator]
194200

195-
theorem one_of_bool_or_not : B + (¬ᵣ B) = (1 : FinRV n Bool) := by ext ω; unfold FinRV.not; simp
201+
theorem one_of_bool_or_not : B + (¬ᵣ B) = (1 : FinRV n Bool) := by ext ω; unfold FinRV.not; simp
196202

197-
theorem one_of_ind_bool_or_not : (𝕀∘B) + (𝕀∘(¬ᵣ B)) = (1 : FinRV n ℚ) :=
203+
theorem one_of_ind_bool_or_not : (𝕀∘B) + (𝕀∘(¬ᵣ B)) = (1 : FinRV n ℚ) :=
198204
by ext ω
199-
unfold FinRV.not 𝕀 indicator not
205+
unfold FinRV.not 𝕀 indicator not
200206
by_cases h : B ω
201207
· simp [h]
202-
· simp [h]
208+
· simp [h]
203209

204-
variable {X Y: FinRV n ℚ}
210+
variable {X Y: FinRV n ℚ}
205211

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

208-
theorem rv_prod_sum_linear {Xs : Fin k → FinRV n ℚ} : ∑ i, Y * (Xs i) = Y * (∑ i, Xs i) :=
209-
by ext ω
210-
simp
211-
rw [Finset.mul_sum]
212-
214+
theorem rv_prod_sum_linear {Xs : Fin k → FinRV n ℚ} : ∑ i, Y * (Xs i) = Y * (∑ i, Xs i) :=
215+
by ext ω
216+
simp
217+
rw [Finset.mul_sum]
218+
213219
end RandomVariable
214220

215221
------------------------------ Probability ---------------------------
@@ -222,7 +228,7 @@ def probability : ℚ := P.p ⬝ᵥ (𝕀 ∘ B)
222228

223229
notation "ℙ[" B "//" P "]" => probability P B
224230

225-
-- helps to refold is when needed
231+
-- helps to refold is when needed
226232
lemma probability_def : P.p ⬝ᵥ (𝕀 ∘ B) = ℙ[B // P] := rfl
227233

228234
-- TODO: the sorry in the definition has to do with the decidability of the membership
@@ -244,11 +250,11 @@ theorem prob_one_of_true : ℙ[1 // P] = 1 :=
244250

245251
example {a b : ℚ} (h : 0 ≤ a) (h2 : 0 ≤ b) : 0 ≤ a * b := Rat.mul_nonneg h h2
246252

247-
variable {P : Findist n} {B : FinRV n Bool}
253+
variable {P : Findist n} {B : FinRV n Bool}
254+
255+
theorem prod_zero_of_prob_zero : ℙ[B // P] = 0 → (P.p * (𝕀∘B) = 0) := by
256+
intro h; exact prod_eq_zero_of_nneg_dp_zero P.nneg ind_nneg h
248257

249-
theorem prod_zero_of_prob_zero : ℙ[B // P] = 0 → (P.p * (𝕀∘B) = 0) := by
250-
intro h; exact prod_eq_zero_of_nneg_dp_zero P.nneg ind_nneg h
251-
252258

253259
------------------------------ PMF ---------------------------
254260

@@ -278,7 +284,7 @@ notation "𝔼[" PX "]" => expect PX.1 PX.2
278284
--theorem exp_eq_correct : 𝔼[X // P] = ∑ v ∈ ((List.finRange P.length).map X).toFinset, v * ℙ[ X =ᵣ v // P]
279285

280286
@[simp]
281-
theorem prob_eq_exp_ind : ℙ[B // P] = 𝔼[𝕀 ∘ B // P] :=
287+
theorem prob_eq_exp_ind : ℙ[B // P] = 𝔼[𝕀 ∘ B // P] :=
282288
by simp only [expect, probability]
283289

284290

@@ -298,47 +304,47 @@ def expect_cnd_rv : Fin n → ℚ := fun i ↦ 𝔼[ X | L =ᵣ (L i) // P ]
298304
notation "𝔼[" X "|ᵣ" L "//" P "]" => expect_cnd_rv P X L
299305

300306
end Ex
301-
--- some basic properties
307+
--- some basic properties
302308

303-
section Expectation_properties
309+
section Expectation_properties
304310
variable {P : Findist n} {X Y Z: FinRV n ℚ} {B : FinRV n Bool}
305311

306-
theorem exp_congr : (X = Y) → 𝔼[X // P] = 𝔼[Y // P] :=
307-
by intro h
308-
unfold Ex.expect dotProduct
312+
theorem exp_congr : (X = Y) → 𝔼[X // P] = 𝔼[Y // P] :=
313+
by intro h
314+
unfold Ex.expect dotProduct
309315
apply Fintype.sum_congr
310-
simp_all
316+
simp_all
311317

312-
theorem exp_dists_add : 𝔼[X + Y // P] = 𝔼[X // P] + 𝔼[Y // P] := by simp [Ex.expect]
318+
theorem exp_dists_add : 𝔼[X + Y // P] = 𝔼[X // P] + 𝔼[Y // P] := by simp [Ex.expect]
313319

314320
theorem exp_mul_comm : 𝔼[X * Y // P] = 𝔼[Y * X // P] := by unfold Ex.expect; exact dotProd_hadProd_comm
315321

316322
variable {c : ℚ} {p : Fin n → ℚ}
317323

318-
theorem const_fun_to_one : (fun _ ↦ c : FinRV n ℚ) = c • 1 := by ext; simp;
324+
theorem const_fun_to_one : (fun _ ↦ c : FinRV n ℚ) = c • 1 := by ext; simp;
319325

320-
theorem exp_const : 𝔼[(fun _ ↦ c) // P] = c :=
326+
theorem exp_const : 𝔼[(fun _ ↦ c) // P] = c :=
321327
by unfold Ex.expect
322-
rw [const_fun_to_one]
328+
rw [const_fun_to_one]
323329
simp only [dotProduct_smul, smul_eq_mul]
324330
rw [dotProduct_comm, P.prob]
325-
simp
331+
simp
326332

327-
theorem exp_one : 𝔼[ 1 // P] = 1 :=
328-
by calc 𝔼[ 1 // P] = 𝔼[ (fun _ ↦ 1) // P] := rfl
329-
_ = 1 := exp_const
333+
theorem exp_one : 𝔼[ 1 // P] = 1 :=
334+
by calc 𝔼[ 1 // P] = 𝔼[ (fun _ ↦ 1) // P] := rfl
335+
_ = 1 := exp_const
330336

331337
theorem exp_prod_const : 𝔼[c • X // P] = c * 𝔼[X // P] := by simp only [Ex.expect, dotProduct_smul, smul_eq_mul]
332338

333-
lemma constant_mul_eq_smul : (fun ω ↦ c * X ω) = c • X := rfl
339+
lemma constant_mul_eq_smul : (fun ω ↦ c * X ω) = c • X := rfl
334340

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

338-
theorem exp_indi_eq_exp_indr : ∀i : Fin k, 𝔼[L =ᵢ i // P] = 𝔼[𝕀 ∘ (L =ᵣ i) // P] := by
339-
intro i
344+
theorem exp_indi_eq_exp_indr : ∀i : Fin k, 𝔼[L =ᵢ i // P] = 𝔼[𝕀 ∘ (L =ᵣ i) // P] := by
345+
intro i
340346
rw [indi_eq_indr]
341347

342348
theorem exp_monotone (h: X ≤ Y) : 𝔼[X // P] ≤ 𝔼[Y // P] := dotProduct_le_dotProduct_of_nonneg_left h P.nneg
343349

344-
end Expectation_properties
350+
end Expectation_properties

0 commit comments

Comments
 (0)