Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 331df5a

Browse files
committed
feat(probability/moments): moments and moment generating function of a real random variable (#14755)
This PR defines moments, central moments, moment generating function and cumulant generating function.
1 parent 3091b91 commit 331df5a

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

src/probability/moments.lean

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/-
2+
Copyright (c) 2022 Rémy Degenne. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Rémy Degenne
5+
-/
6+
7+
import probability.variance
8+
9+
/-!
10+
# Moments and moment generating function
11+
12+
## Main definitions
13+
14+
* `moment X p μ`: `p`th moment of a real random variable `X` with respect to measure `μ`, `μ[X^p]`
15+
* `central_moment X p μ`:`p`th central moment of `X` with respect to measure `μ`, `μ[(X - μ[X])^p]`
16+
* `mgf X μ t`: moment generating function of `X` with respect to measure `μ`, `μ[exp(t*X)]`
17+
* `cgf X μ t`: cumulant generating function, logarithm of the moment generating function
18+
19+
## Main results
20+
21+
* `indep_fun.mgf_add`: if two real random variables `X` and `Y` are independent and their mgf are
22+
defined at `t`, then `mgf (X + Y) μ t = mgf X μ t * mgf Y μ t`
23+
* `indep_fun.cgf_add`: if two real random variables `X` and `Y` are independent and their mgf are
24+
defined at `t`, then `cgf (X + Y) μ t = cgf X μ t + cgf Y μ t`
25+
26+
-/
27+
28+
open measure_theory filter finset
29+
30+
noncomputable theory
31+
32+
open_locale big_operators measure_theory probability_theory ennreal nnreal
33+
34+
namespace probability_theory
35+
36+
variables {Ω : Type*} {m : measurable_space Ω} {X : Ω → ℝ} {p : ℕ} {μ : measure Ω}
37+
38+
include m
39+
40+
/-- Moment of a real random variable, `μ[X ^ p]`. -/
41+
def moment (X : Ω → ℝ) (p : ℕ) (μ : measure Ω) : ℝ := μ[X ^ p]
42+
43+
/-- Central moment of a real random variable, `μ[(X - μ[X]) ^ p]`. -/
44+
def central_moment (X : Ω → ℝ) (p : ℕ) (μ : measure Ω) : ℝ := μ[(X - (λ x, μ[X])) ^ p]
45+
46+
@[simp] lemma moment_zero (hp : p ≠ 0) : moment 0 p μ = 0 :=
47+
by simp only [moment, hp, zero_pow', ne.def, not_false_iff, pi.zero_apply, integral_const,
48+
algebra.id.smul_eq_mul, mul_zero]
49+
50+
@[simp] lemma central_moment_zero (hp : p ≠ 0) : central_moment 0 p μ = 0 :=
51+
by simp only [central_moment, hp, pi.zero_apply, integral_const, algebra.id.smul_eq_mul,
52+
mul_zero, zero_sub, pi.pow_apply, pi.neg_apply, neg_zero', zero_pow', ne.def, not_false_iff]
53+
54+
lemma central_moment_one' [is_finite_measure μ] (h_int : integrable X μ) :
55+
central_moment X 1 μ = (1 - (μ set.univ).to_real) * μ[X] :=
56+
begin
57+
simp only [central_moment, pi.sub_apply, pow_one],
58+
rw integral_sub h_int (integrable_const _),
59+
simp only [sub_mul, integral_const, algebra.id.smul_eq_mul, one_mul],
60+
end
61+
62+
@[simp] lemma central_moment_one [is_probability_measure μ] : central_moment X 1 μ = 0 :=
63+
begin
64+
by_cases h_int : integrable X μ,
65+
{ rw central_moment_one' h_int,
66+
simp only [measure_univ, ennreal.one_to_real, sub_self, zero_mul], },
67+
{ simp only [central_moment, pi.sub_apply, pow_one],
68+
have : ¬ integrable (λ x, X x - integral μ X) μ,
69+
{ refine λ h_sub, h_int _,
70+
have h_add : X = (λ x, X x - integral μ X) + (λ x, integral μ X),
71+
{ ext1 x, simp, },
72+
rw h_add,
73+
exact h_sub.add (integrable_const _), },
74+
rw integral_undef this, },
75+
end
76+
77+
@[simp] lemma central_moment_two_eq_variance : central_moment X 2 μ = variance X μ := rfl
78+
79+
section moment_generating_function
80+
81+
variables {t : ℝ}
82+
83+
/-- Moment generating function of a real random variable `X`: `λ t, μ[exp(t*X)]`. -/
84+
def mgf (X : Ω → ℝ) (μ : measure Ω) (t : ℝ) : ℝ := μ[λ ω, real.exp (t * X ω)]
85+
86+
/-- Cumulant generating function of a real random variable `X`: `λ t, log μ[exp(t*X)]`. -/
87+
def cgf (X : Ω → ℝ) (μ : measure Ω) (t : ℝ) : ℝ := real.log (mgf X μ t)
88+
89+
@[simp] lemma mgf_zero_fun : mgf 0 μ t = (μ set.univ).to_real :=
90+
by simp only [mgf, pi.zero_apply, mul_zero, real.exp_zero, integral_const, algebra.id.smul_eq_mul,
91+
mul_one]
92+
93+
@[simp] lemma cgf_zero_fun : cgf 0 μ t = real.log (μ set.univ).to_real :=
94+
by simp only [cgf, mgf_zero_fun]
95+
96+
@[simp] lemma mgf_zero_measure : mgf X (0 : measure Ω) t = 0 :=
97+
by simp only [mgf, integral_zero_measure]
98+
99+
@[simp] lemma cgf_zero_measure : cgf X (0 : measure Ω) t = 0 :=
100+
by simp only [cgf, real.log_zero, mgf_zero_measure]
101+
102+
@[simp] lemma mgf_const' (c : ℝ) : mgf (λ _, c) μ t = (μ set.univ).to_real * real.exp (t * c) :=
103+
by simp only [mgf, integral_const, algebra.id.smul_eq_mul]
104+
105+
@[simp] lemma mgf_const (c : ℝ) [is_probability_measure μ] : mgf (λ _, c) μ t = real.exp (t * c) :=
106+
by simp only [mgf_const', measure_univ, ennreal.one_to_real, one_mul]
107+
108+
@[simp] lemma cgf_const' [is_finite_measure μ] (hμ : μ ≠ 0) (c : ℝ) :
109+
cgf (λ _, c) μ t = real.log (μ set.univ).to_real + t * c :=
110+
begin
111+
simp only [cgf, mgf_const'],
112+
rw real.log_mul _ (real.exp_pos _).ne',
113+
{ rw real.log_exp _, },
114+
{ rw [ne.def, ennreal.to_real_eq_zero_iff, measure.measure_univ_eq_zero],
115+
simp only [hμ, measure_ne_top μ set.univ, or_self, not_false_iff], },
116+
end
117+
118+
@[simp] lemma cgf_const [is_probability_measure μ] (c : ℝ) : cgf (λ _, c) μ t = t * c :=
119+
by simp only [cgf, mgf_const, real.log_exp]
120+
121+
@[simp] lemma mgf_zero' : mgf X μ 0 = (μ set.univ).to_real :=
122+
by simp only [mgf, zero_mul, real.exp_zero, integral_const, algebra.id.smul_eq_mul, mul_one]
123+
124+
@[simp] lemma mgf_zero [is_probability_measure μ] : mgf X μ 0 = 1 :=
125+
by simp only [mgf_zero', measure_univ, ennreal.one_to_real]
126+
127+
@[simp] lemma cgf_zero' : cgf X μ 0 = real.log (μ set.univ).to_real :=
128+
by simp only [cgf, mgf_zero']
129+
130+
@[simp] lemma cgf_zero [is_probability_measure μ] : cgf X μ 0 = 0 :=
131+
by simp only [cgf_zero', measure_univ, ennreal.one_to_real, real.log_one]
132+
133+
lemma mgf_undef (hX : ¬ integrable (λ ω, real.exp (t * X ω)) μ) : mgf X μ t = 0 :=
134+
by simp only [mgf, integral_undef hX]
135+
136+
lemma cgf_undef (hX : ¬ integrable (λ ω, real.exp (t * X ω)) μ) : cgf X μ t = 0 :=
137+
by simp only [cgf, mgf_undef hX, real.log_zero]
138+
139+
lemma mgf_nonneg : 0 ≤ mgf X μ t :=
140+
begin
141+
refine integral_nonneg _,
142+
intro ω,
143+
simp only [pi.zero_apply],
144+
exact (real.exp_pos _).le,
145+
end
146+
147+
lemma mgf_pos' (hμ : μ ≠ 0) (h_int_X : integrable (λ ω, real.exp (t * X ω)) μ) : 0 < mgf X μ t :=
148+
begin
149+
simp_rw mgf,
150+
have : ∫ (x : Ω), real.exp (t * X x) ∂μ = ∫ (x : Ω) in set.univ, real.exp (t * X x) ∂μ,
151+
{ simp only [measure.restrict_univ], },
152+
rw [this, set_integral_pos_iff_support_of_nonneg_ae _ _],
153+
{ have h_eq_univ : function.support (λ (x : Ω), real.exp (t * X x)) = set.univ,
154+
{ ext1 x,
155+
simp only [function.mem_support, set.mem_univ, iff_true],
156+
exact (real.exp_pos _).ne', },
157+
rw [h_eq_univ, set.inter_univ _],
158+
refine ne.bot_lt _,
159+
simp only [hμ, ennreal.bot_eq_zero, ne.def, measure.measure_univ_eq_zero, not_false_iff], },
160+
{ refine eventually_of_forall (λ x, _),
161+
rw pi.zero_apply,
162+
exact (real.exp_pos _).le, },
163+
{ rwa integrable_on_univ, },
164+
end
165+
166+
lemma mgf_pos [is_probability_measure μ] (h_int_X : integrable (λ ω, real.exp (t * X ω)) μ) :
167+
0 < mgf X μ t :=
168+
mgf_pos' (is_probability_measure.ne_zero μ) h_int_X
169+
170+
lemma indep_fun.mgf_add {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ)
171+
(h_int_X : integrable (λ ω, real.exp (t * X ω)) μ)
172+
(h_int_Y : integrable (λ ω, real.exp (t * Y ω)) μ) :
173+
mgf (X + Y) μ t = mgf X μ t * mgf Y μ t :=
174+
begin
175+
simp_rw [mgf, pi.add_apply, mul_add, real.exp_add],
176+
refine indep_fun.integral_mul_of_integrable' _ h_int_X h_int_Y,
177+
have h_meas : measurable (λ x, real.exp (t * x)) := (measurable_id'.const_mul t).exp,
178+
change indep_fun ((λ x, real.exp (t * x)) ∘ X) ((λ x, real.exp (t * x)) ∘ Y) μ,
179+
exact indep_fun.comp h_indep h_meas h_meas,
180+
end
181+
182+
lemma indep_fun.cgf_add {X Y : Ω → ℝ} (h_indep : indep_fun X Y μ)
183+
(h_int_X : integrable (λ ω, real.exp (t * X ω)) μ)
184+
(h_int_Y : integrable (λ ω, real.exp (t * Y ω)) μ) :
185+
cgf (X + Y) μ t = cgf X μ t + cgf Y μ t :=
186+
begin
187+
by_cases hμ : μ = 0,
188+
{ simp [hμ], },
189+
simp only [cgf, h_indep.mgf_add h_int_X h_int_Y],
190+
exact real.log_mul (mgf_pos' hμ h_int_X).ne' (mgf_pos' hμ h_int_Y).ne',
191+
end
192+
193+
end moment_generating_function
194+
195+
end probability_theory

0 commit comments

Comments
 (0)