Repository navigation
Expand file tree
/
Copy pathExpectation.lean
More file actions
402 lines (343 loc) · 17.9 KB
/
Copy pathExpectation.lean
File metadata and controls
402 lines (343 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
module
public import Probability.Distribution
public import Mathlib.Analysis.Real.Sqrt
public import Mathlib.Analysis.Convex.Jensen
/-!
# Expectation
The expectation `𝔼_X[f] = ∑ₐ X(a) · f(a)` over a finite distribution.
## Main definitions
* `expect X f`: the expectation of `f` under `X`
* `variance X f`: the variance
## Main results
* linearity in the distribution and in the function
* `expect_indicator`: the expectation of an indicator is a mass
* `expect_nonneg`, `expect_mono`, `mass_ge_le_expect_div` (Markov),
`expect_mul_sq_le_sq_mul_sq` (Cauchy–Schwarz)
* `expect_sq_le_mass_ne_zero_mul_second`, `expect_sq_le_mass_pos_mul_second`: hit-mass bounds
* `variance_nonneg`, `variance_eq_expect_sq_sub_sq_expect`
* Jensen: `ConcaveOn.le_map_expect`, `ConvexOn.map_expect_le`
* `expect_fTransform`, `expect_prod_eq`, `abs_expect_sub_expect_le_expect`
* `expect_comp_eq_sum_mass`: `𝔼_X[ε ∘ classOf] = ∑_c ε(c) · X(classOf = c)`
-/
@[expose] public section
noncomputable section
open scoped BigOperators NNReal
namespace Probability
namespace Distribution
variable {A : Type*}
/-- Expectation of `f : A → ℝ` under a finite signed distribution:
`𝔼_X[f] = ∑_a X(a)·f(a)`, summed over the finite support of `X`.
For arbitrary signed `X` this is the bilinear pairing of `X` and `f`; it is an
honest expectation when `X.isProbDist`. Weakest-layer facts about it are
collected in this file; the transport identifying it with the Bochner integral
against `Distribution.toPMF` is `integral_toPMF_eq_expect` in
`Probability.DistributionMeasure`. -/
def expect (X : Distribution A) (f : A → ℝ) : ℝ :=
X.sum fun a w => w * f a
/-- On a finite carrier, `expect` is the ordinary sum over all points — the
`Finset.univ` unfolding used by proofs that reason over the whole carrier
(cf. `weight_eq_sum`, `mass_eq_sum`). -/
lemma expect_eq_sum [Fintype A] (X : Distribution A) (f : A → ℝ) :
X.expect f = ∑ a : A, X a * f a :=
Finsupp.sum_fintype X (fun a w => w * f a) fun a => zero_mul (f a)
/-- `expect` may be computed over any `Finset` containing the support: the
off-support summands vanish. -/
lemma expect_eq_sum_of_support_subset (X : Distribution A) (f : A → ℝ) {s : Finset A}
(hs : X.support ⊆ s) :
X.expect f = ∑ a ∈ s, X a * f a :=
Finset.sum_subset hs fun a _ ha => by
simp [Finsupp.notMem_support_iff.mp ha]
/-! ### Linearity in the distribution — signed layer -/
/-- Additivity of expectation in the distribution. Signed layer: no
hypothesis. -/
lemma expect_add_left (X Y : Distribution A) (f : A → ℝ) :
(X + Y).expect f = X.expect f + Y.expect f :=
Finsupp.sum_add_index' (fun a => zero_mul (f a)) fun a w₁ w₂ => add_mul w₁ w₂ (f a)
/-- Scalar homogeneity of expectation in the distribution. Signed layer: no
hypothesis. -/
lemma expect_smul_left (r : ℝ) (X : Distribution A) (f : A → ℝ) :
(r • X).expect f = r * X.expect f := by
unfold expect
rw [Finsupp.sum_smul_index fun a => zero_mul (f a)]
unfold Finsupp.sum
rw [Finset.mul_sum]
exact Finset.sum_congr rfl fun a _ => mul_assoc r (X a) (f a)
/-- Expectation under a difference of distributions is the difference of
expectations. Signed layer: no hypothesis — this is the form consumed by
`statDist`-style arguments on the signed carrier. -/
lemma expect_sub_left (X Y : Distribution A) (f : A → ℝ) :
(X - Y).expect f = X.expect f - Y.expect f :=
Finsupp.sum_sub_index fun a w₁ w₂ => sub_mul w₁ w₂ (f a)
/-- Expectation under the zero distribution vanishes. -/
@[simp]
lemma expect_zero_left (f : A → ℝ) : (0 : Distribution A).expect f = 0 :=
Finsupp.sum_zero_index
/-! ### Linearity in the function — signed layer -/
/-- Additivity of expectation in the function. Signed layer: no
hypothesis. -/
lemma expect_add_right (X : Distribution A) (f g : A → ℝ) :
X.expect (f + g) = X.expect f + X.expect g := by
unfold expect
simp only [Pi.add_apply, mul_add]
exact Finsupp.sum_add
/-- Expectation pulls out a constant factor of the function. Signed layer: no
hypothesis. (`expect_smul_right` is the `•` spelling.) -/
lemma expect_const_mul (X : Distribution A) (r : ℝ) (f : A → ℝ) :
X.expect (fun a => r * f a) = r * X.expect f := by
unfold expect Finsupp.sum
rw [Finset.mul_sum]
exact Finset.sum_congr rfl fun a _ => mul_left_comm (X a) r (f a)
/-- Scalar homogeneity of expectation in the function. Signed layer: no
hypothesis. -/
lemma expect_smul_right (X : Distribution A) (r : ℝ) (f : A → ℝ) :
X.expect (r • f) = r * X.expect f :=
expect_const_mul X r f
/-- Expectation of a difference of functions is the difference of expectations
(the function-side mirror of `expect_sub_left`). Signed layer: no
hypothesis. -/
lemma expect_sub_right (X : Distribution A) (f g : A → ℝ) :
X.expect (fun a => f a - g a) = X.expect f - X.expect g := by
unfold expect Finsupp.sum
rw [← Finset.sum_sub_distrib]
exact Finset.sum_congr rfl fun a _ => by ring
/-- Expectation of the zero function vanishes. -/
@[simp]
lemma expect_zero_right (X : Distribution A) : X.expect 0 = 0 := by
unfold expect Finsupp.sum
simp
/-- Expectation of a constant is the constant times the total weight; at
`weight = 1` this is `𝔼[c] = c`. Signed layer: no hypothesis. -/
lemma expect_const (X : Distribution A) (c : ℝ) :
X.expect (fun _ => c) = c * X.weight := by
unfold expect weight Finsupp.sum
rw [Finset.mul_sum]
exact Finset.sum_congr rfl fun a _ => mul_comm (X a) c
/-- Expectation of a finite sum of functions is the sum of expectations
(the finite-sum form of `expect_add_right`, mirroring mathlib's
`MeasureTheory.integral_finset_sum`). Signed layer: no hypothesis. -/
lemma expect_finset_sum {ι : Type*} (X : Distribution A) (s : Finset ι)
(f : ι → A → ℝ) :
X.expect (fun a => ∑ i ∈ s, f i a) = ∑ i ∈ s, X.expect (f i) := by
unfold expect Finsupp.sum
simp_rw [Finset.mul_sum]
exact Finset.sum_comm
/-! ### The indicator/mass identity — signed layer -/
/-- Expectation of a scaled indicator is the scalar times the event mass:
`𝔼_X[c·𝟙[P]] = c·X(P)`. Signed layer: no hypothesis. Statement-level
`[DecidablePred P]` per the decidability policy of `Probability.Distribution`. -/
lemma expect_indicator_const (X : Distribution A) (P : A → Prop) [DecidablePred P]
(c : ℝ) :
X.expect (fun a => if P a then c else 0) = c * X.mass P := by
classical
unfold expect mass Finsupp.sum
rw [Finset.mul_sum]
exact Finset.sum_congr rfl fun a _ => by
by_cases h : P a <;> simp [h, mul_comm]
/-- Expectation of an indicator is the event mass: `𝔼_X[𝟙[P]] = X(P)`. This
is the relation of `expect` to `Distribution.mass`. Signed layer: no hypothesis. -/
lemma expect_indicator (X : Distribution A) (P : A → Prop) [DecidablePred P] :
X.expect (fun a => if P a then (1 : ℝ) else 0) = X.mass P := by
rw [expect_indicator_const, one_mul]
/-! ### Order facts — `NonNeg` layer -/
/-- Expectation of a nonnegative function under a nonnegative distribution is
nonnegative. `NonNeg` layer. -/
lemma expect_nonneg {X : Distribution A} (hX : X.NonNeg) {f : A → ℝ}
(hf : ∀ a, 0 ≤ f a) :
0 ≤ X.expect f :=
Finset.sum_nonneg fun a _ => mul_nonneg (hX a) (hf a)
/-- Monotonicity of expectation. `NonNeg` layer: fails signed (one point of
mass `-1`). -/
lemma expect_mono {X : Distribution A} (hX : X.NonNeg) {f g : A → ℝ}
(h : ∀ a, f a ≤ g a) :
X.expect f ≤ X.expect g :=
Finset.sum_le_sum fun a _ => mul_le_mul_of_nonneg_left (h a) (hX a)
/-- Pointwise gaps bounded by `ε a` give an average gap bounded by `𝔼_X[ε]`. -/
lemma abs_expect_sub_expect_le_expect
-- `X` is a nonnegative finite law; its total weight need not be one.
{X : Distribution A} (hX : X.NonNeg)
-- `f`, `g` are the compared observations; `ε a` bounds their gap at outcome `a`.
(f g ε : A → ℝ) (h : ∀ a, |f a - g a| ≤ ε a) :
|X.expect f - X.expect g| ≤ X.expect ε := by
apply abs_sub_le_iff.mpr
constructor
· rw [← expect_sub_right]
exact expect_mono hX (fun a => (abs_sub_le_iff.mp (h a)).1)
· rw [← expect_sub_right]
exact expect_mono hX (fun a => (abs_sub_le_iff.mp (h a)).2)
/-- A uniform bound on `f` over the support bounds the expectation by
`c · |X|`. `NonNeg` layer; the bound is only required where `X` has mass. -/
lemma expect_le_mul_weight {X : Distribution A} (hX : X.NonNeg) {f : A → ℝ} {c : ℝ}
(h : ∀ a ∈ X.support, f a ≤ c) :
X.expect f ≤ c * X.weight := by
unfold expect weight Finsupp.sum
rw [Finset.mul_sum]
exact Finset.sum_le_sum fun a ha =>
(mul_le_mul_of_nonneg_left (h a ha) (hX a)).trans_eq (mul_comm _ _)
/-- **Markov's inequality**: `X{a | c ≤ f a} ≤ 𝔼_X[f]/c` for nonnegative `f`
and `c > 0`. `NonNeg` layer — total weight plays no role, so this holds for
arbitrary nonnegative (sub- or super-normalized) distributions; fails signed
(negative mass can pull the expectation below the event mass). -/
theorem mass_ge_le_expect_div {X : Distribution A} (hX : X.NonNeg) {f : A → ℝ}
(hf : ∀ a, 0 ≤ f a) {c : ℝ} (hc : 0 < c) :
X.mass (fun a => c ≤ f a) ≤ X.expect f / c := by
classical
rw [le_div_iff₀ hc]
unfold mass expect Finsupp.sum
rw [Finset.sum_mul]
refine Finset.sum_le_sum fun a _ => ?_
by_cases ha : c ≤ f a
· simpa [ha] using mul_le_mul_of_nonneg_left ha (hX a)
· simpa [ha] using mul_nonneg (hX a) (hf a)
/-- **Discrete Cauchy–Schwarz**: `𝔼_X[uv]² ≤ 𝔼_X[u²]·𝔼_X[v²]`. `NonNeg`
layer — this is positive semidefiniteness of the pairing, not normalization,
so any nonnegative weight works; fails signed (the pairing becomes
indefinite). Name mirrors mathlib's `Finset.sum_mul_sq_le_sq_mul_sq`. -/
theorem expect_mul_sq_le_sq_mul_sq {X : Distribution A} (hX : X.NonNeg) (u v : A → ℝ) :
(X.expect fun a => u a * v a) ^ 2
≤ (X.expect fun a => u a ^ 2) * X.expect fun a => v a ^ 2 := by
have h := Finset.sum_mul_sq_le_sq_mul_sq X.support
(fun a => Real.sqrt (X a) * u a) fun a => Real.sqrt (X a) * v a
have h1 : ∀ a, Real.sqrt (X a) * u a * (Real.sqrt (X a) * v a)
= X a * (u a * v a) := fun a => by
rw [mul_mul_mul_comm, Real.mul_self_sqrt (hX a)]
have h2 : ∀ (w : A → ℝ) (a : A), (Real.sqrt (X a) * w a) ^ 2
= X a * w a ^ 2 := fun w a => by
rw [mul_pow, Real.sq_sqrt (hX a)]
simpa only [expect, Finsupp.sum, h1, h2 u, h2 v] using h
/-! ### Variance -/
/-- Variance of `f` under `X`, in the `𝔼[(f - 𝔼f)²]` form. This form is the
definition because its nonnegativity already holds at the `NonNeg` layer
(`variance_nonneg`); the subtracted form `𝔼[f²] - 𝔼[f]²` agrees with it only
at `weight = 1` (`variance_eq_expect_sq_sub_sq_expect`), and goes negative at
weight `2`: on a one-point law of mass `2` with `f = 1`, the subtracted form is
`2 - 2² = -2`. -/
def variance (X : Distribution A) (f : A → ℝ) : ℝ :=
X.expect fun a => (f a - X.expect f) ^ 2
/-- Nonnegativity of the `𝔼[(f-c)²]` form for any center `c`. `NonNeg`
layer. -/
lemma expect_sub_sq_nonneg {X : Distribution A} (hX : X.NonNeg) (f : A → ℝ) (c : ℝ) :
0 ≤ X.expect fun a => (f a - c) ^ 2 :=
Finset.sum_nonneg fun a _ => mul_nonneg (hX a) (sq_nonneg _)
/-- Variance is nonnegative. `NonNeg` layer (the `𝔼[(f-c)²]` form at
`c = 𝔼f`; no normalization needed). -/
lemma variance_nonneg {X : Distribution A} (hX : X.NonNeg) (f : A → ℝ) :
0 ≤ X.variance f :=
expect_sub_sq_nonneg hX f _
/-- Expansion of the square about an arbitrary center:
`𝔼[(f-c)²] = 𝔼[f²] - 2c·𝔼[f] + c²·|X|`. Signed layer: pure bilinearity. -/
lemma expect_sub_sq (X : Distribution A) (f : A → ℝ) (c : ℝ) :
(X.expect fun a => (f a - c) ^ 2)
= (X.expect fun a => f a ^ 2) - 2 * c * X.expect f + c ^ 2 * X.weight := by
unfold expect weight Finsupp.sum
rw [Finset.mul_sum, Finset.mul_sum, ← Finset.sum_sub_distrib,
← Finset.sum_add_distrib]
exact Finset.sum_congr rfl fun a _ => by ring
/-- At total weight one, variance takes the subtracted form
`Var_X f = 𝔼[f²] - 𝔼[f]²` (mathlib: `ProbabilityTheory.variance_def'`).
Weakest layer: `weight = 1` alone — nonnegativity of `X` plays no role in the
*identity*, only in the sign of either side. -/
lemma variance_eq_expect_sq_sub_sq_expect {X : Distribution A} (hw : X.weight = 1)
(f : A → ℝ) :
X.variance f = (X.expect fun a => f a ^ 2) - X.expect f ^ 2 := by
rw [variance, expect_sub_sq, hw]
ring
/-- Nonnegativity of the subtracted form: `𝔼[f]² ≤ 𝔼[f²]`. `isProbDist`
layer — this genuinely needs both conjuncts: `NonNeg` for the sign of the
`𝔼[(f-c)²]` form and `weight = 1` to equate the two forms
(the one-point law of mass `2` with `f = 1` gives `2 < 2²`). -/
lemma expect_sq_sub_sq_expect_nonneg {X : Distribution A} (hX : X.isProbDist)
(f : A → ℝ) :
0 ≤ (X.expect fun a => f a ^ 2) - X.expect f ^ 2 := by
rw [← variance_eq_expect_sq_sub_sq_expect hX.2]
exact variance_nonneg hX.1 f
/-! ### Jensen — `isProbDist` layer -/
/-- **Jensen's inequality**, concave case: `𝔼_X[φ∘f] ≤ φ(𝔼_X[f])` for a
probability distribution `X`. `isProbDist` layer. Name mirrors mathlib's
`ConcaveOn.le_map_sum`. -/
theorem _root_.ConcaveOn.le_map_expect {φ : ℝ → ℝ}
(hφ : ConcaveOn ℝ Set.univ φ) {X : Distribution A} (hX : X.isProbDist)
(f : A → ℝ) :
(X.expect fun a => φ (f a)) ≤ φ (X.expect f) := by
have h := hφ.le_map_sum (t := X.support) (w := fun a => X a) (p := f)
(fun a _ => hX.1 a) hX.2 (fun a _ => trivial)
simpa only [expect, Finsupp.sum, smul_eq_mul] using h
/-- **Jensen's inequality**, convex case: `φ(𝔼_X[f]) ≤ 𝔼_X[φ∘f]` for a
probability distribution `X`. `isProbDist` layer. Name mirrors mathlib's
`ConvexOn.map_sum_le`. -/
theorem _root_.ConvexOn.map_expect_le {φ : ℝ → ℝ}
(hφ : ConvexOn ℝ Set.univ φ) {X : Distribution A} (hX : X.isProbDist)
(f : A → ℝ) :
φ (X.expect f) ≤ X.expect fun a => φ (f a) := by
have h := hφ.map_sum_le (t := X.support) (w := fun a => X a) (p := f)
(fun a _ => hX.1 a) hX.2 (fun a _ => trivial)
simpa only [expect, Finsupp.sum, smul_eq_mul] using h
/-- Expectation transports along a pushforward: `𝔼_{f_*X}[g] = 𝔼_X[g ∘ f]`.
This identity holds for finite signed distributions. -/
lemma expect_fTransform {B : Type*} (X : Distribution A) (f : A → B) (g : B → ℝ) :
(fTransform f X).expect g = X.expect (g ∘ f) := by
classical
unfold expect fTransform
rw [Finsupp.sum_sum_index (fun _ => zero_mul (g _))
(fun _ m₁ m₂ => add_mul m₁ m₂ (g _))]
exact Finsupp.sum_congr fun a _ => by simp
/-- Integrands agreeing on the finite support have the same signed expectation. -/
lemma expect_congr_of_support {X : Distribution A} {f g : A → ℝ}
(h : ∀ a ∈ X.support, f a = g a) :
X.expect f = X.expect g :=
Finsupp.sum_congr fun a ha => by rw [h a ha]
/-- Eta-expanded additivity of expectation in its integrand. -/
lemma expect_add_right' (X : Distribution A) (f g : A → ℝ) :
(X.expect fun a => f a + g a) = X.expect f + X.expect g :=
expect_add_right X f g
/-- CR18, Appendix A, Definition A.4: “independently selected”. Integrating
against a finite independent product equals iterated signed expectation. -/
lemma expect_prod_eq {A B : Type*} (first : Distribution A)
(second : Distribution B) (value : A × B → ℝ) :
(Distribution.prod first second).expect value =
first.expect (fun a => second.expect (fun b => value (a, b))) := by
rw [Distribution.expect_eq_sum_of_support_subset _ _
(Distribution.support_prod_subset first second), Finset.sum_product]
simp only [Distribution.prod_apply, Distribution.expect, Finsupp.sum,
Finset.mul_sum, mul_assoc]
/-- **The expectation of an error constant on classes**, for a finite type of classes `C`:
`𝔼_X[ε ∘ classOf] = ∑_c ε(c) · X(classOf = c)`. Signed layer: no hypothesis. -/
lemma expect_comp_eq_sum_mass {A C : Type*} [Fintype C]
(X : Distribution A) (classOf : A → C) (ε : C → ℝ) :
X.expect (fun a => ε (classOf a)) = ∑ c, ε c * X.mass (fun a => classOf a = c) := by
classical
-- Exchange the sums; each atom `a` contributes `X(a) · ε(classOf a)` to its own class only.
unfold Distribution.expect Distribution.mass Finsupp.sum
simp only [Finset.mul_sum]
rw [Finset.sum_comm]
refine Finset.sum_congr rfl fun a _ => ?_
rw [Finset.sum_eq_single (classOf a) (fun c _ hc => by simp only [Ne.symm hc, ↓reduceIte,
mul_zero])
(fun h => absurd (Finset.mem_univ _) h)]
simp only [mul_comm, ↓reduceIte]
/-- The squared expectation is at most the nonzero mass times the second moment.
Only the distribution weights must be nonnegative; `X` may take either sign. -/
theorem expect_sq_le_mass_ne_zero_mul_second {A : Type*} (P : Distribution A) (hp : P.NonNeg)
(X : A → ℝ) :
P.expect X ^ 2 ≤ P.mass (fun a => X a ≠ 0) * P.expect (fun a => X a ^ 2) := by
classical
have h := Distribution.expect_mul_sq_le_sq_mul_sq hp
(fun a => if X a ≠ 0 then (1 : ℝ) else 0) X
have hm (a : A) : (if X a ≠ 0 then (1 : ℝ) else 0) * X a = X a := by
by_cases ha : X a = 0
· rw [ha, mul_zero]
· rw [ite_eq_left ha, one_mul]
have hs (a : A) : (if X a ≠ 0 then (1 : ℝ) else 0) ^ 2 =
if X a ≠ 0 then 1 else 0 := by
split_ifs <;> ring
simpa only [hm, hs, Distribution.expect_indicator] using h
/-- The first and second moments of a nonnegative count `f` lower-bound the mass of a hit.
The weights in `P` need only be nonnegative. -/
theorem expect_sq_le_mass_pos_mul_second {P : Distribution A} (weights : P.NonNeg)
(f : A → ℝ) (nonnegative : ∀ x, 0 ≤ f x) :
P.expect f ^ 2 ≤ P.mass (fun x => 0 < f x) * P.expect (fun x => f x ^ 2) := by
have events : P.mass (fun x => f x ≠ 0) = P.mass (fun x => 0 < f x) :=
P.mass_congr fun x => ⟨fun h => lt_of_le_of_ne (nonnegative x) h.symm, ne_of_gt⟩
rw [← events]
exact expect_sq_le_mass_ne_zero_mul_second P weights f
end Distribution
end Probability