|
| 1 | +/- |
| 2 | +Copyright (c) 2025 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 | +import Mathlib.Analysis.LocallyConvex.ContinuousOfBounded |
| 7 | +import Mathlib.MeasureTheory.Constructions.BorelSpace.ContinuousLinearMap |
| 8 | +import Mathlib.Probability.Variance |
| 9 | + |
| 10 | +/-! |
| 11 | +# Covariance in Banach spaces |
| 12 | +
|
| 13 | +We define the covariance of a finite measure in a Banach space `E`, |
| 14 | +as a continous bilinear form on `Dual ℝ E`. |
| 15 | +
|
| 16 | +## Main definitions |
| 17 | +
|
| 18 | +Let `μ` be a finite measure on a normed space `E` with the Borel σ-algebra. We then define |
| 19 | +
|
| 20 | +* `Dual.toLp`: the function `MemLp.toLp` as a continuous linear map from `Dual 𝕜 E` (for `RCLike 𝕜`) |
| 21 | + into the space `Lp 𝕜 p μ` for `p ≥ 1`. This needs a hypothesis `MemLp id p μ` (we set it to the |
| 22 | + junk value 0 if that's not the case). |
| 23 | +* `covarianceBilin` : covariance of a measure `μ` with `∫ x, ‖x‖^2 ∂μ < ∞` on a Banach space, |
| 24 | + as a continuous bilinear form `Dual ℝ E →L[ℝ] Dual ℝ E →L[ℝ] ℝ`. |
| 25 | + If the second moment of `μ` is not finite, we set `covarianceBilin μ = 0`. |
| 26 | +
|
| 27 | +## Main statements |
| 28 | +
|
| 29 | +* `covarianceBilin_apply` : the covariance of `μ` on `L₁, L₂ : Dual ℝ E` is equal to |
| 30 | + `∫ x, (L₁ x - μ[L₁]) * (L₂ x - μ[L₂]) ∂μ`. |
| 31 | +* `covarianceBilin_same_eq_variance`: `covarianceBilin μ L L = Var[L; μ]`. |
| 32 | +
|
| 33 | +## Implementation notes |
| 34 | +
|
| 35 | +The hypothesis that `μ` has a second moment is written as `MemLp id 2 μ` in the code. |
| 36 | +
|
| 37 | +-/ |
| 38 | + |
| 39 | + |
| 40 | +open MeasureTheory ProbabilityTheory Complex NormedSpace |
| 41 | +open scoped ENNReal NNReal Real Topology |
| 42 | + |
| 43 | +variable {E : Type*} [NormedAddCommGroup E] {mE : MeasurableSpace E} {μ : Measure E} {p : ℝ≥0∞} |
| 44 | + |
| 45 | +namespace NormedSpace.Dual |
| 46 | + |
| 47 | +section LinearMap |
| 48 | + |
| 49 | +variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] |
| 50 | + |
| 51 | +open Classical in |
| 52 | +/-- Linear map from the dual to `Lp` equal to `MemLp.toLp` if `MemLp id p μ` and to 0 otherwise. -/ |
| 53 | +noncomputable |
| 54 | +def toLpₗ (μ : Measure E) (p : ℝ≥0∞) : |
| 55 | + Dual 𝕜 E →ₗ[𝕜] Lp 𝕜 p μ := |
| 56 | + if h_Lp : MemLp id p μ then |
| 57 | + { toFun := fun L ↦ MemLp.toLp L (h_Lp.continuousLinearMap_comp L) |
| 58 | + map_add' u v := by push_cast; rw [MemLp.toLp_add] |
| 59 | + map_smul' c L := by push_cast; rw [MemLp.toLp_const_smul]; rfl } |
| 60 | + else 0 |
| 61 | + |
| 62 | +@[simp] |
| 63 | +lemma toLpₗ_apply (h_Lp : MemLp id p μ) (L : Dual 𝕜 E) : |
| 64 | + L.toLpₗ μ p = MemLp.toLp L (h_Lp.continuousLinearMap_comp L) := by |
| 65 | + simp [toLpₗ, dif_pos h_Lp] |
| 66 | + |
| 67 | +@[simp] |
| 68 | +lemma toLpₗ_of_not_memLp (h_Lp : ¬ MemLp id p μ) (L : Dual 𝕜 E) : |
| 69 | + L.toLpₗ μ p = 0 := by |
| 70 | + simp [toLpₗ, dif_neg h_Lp] |
| 71 | + |
| 72 | +lemma norm_toLpₗ_le [OpensMeasurableSpace E] (L : Dual 𝕜 E) : |
| 73 | + ‖L.toLpₗ μ p‖ ≤ ‖L‖ * (eLpNorm id p μ).toReal := by |
| 74 | + by_cases h_Lp : MemLp id p μ |
| 75 | + swap |
| 76 | + · simp only [h_Lp, not_false_eq_true, toLpₗ_of_not_memLp, Lp.norm_zero] |
| 77 | + positivity |
| 78 | + by_cases hp : p = 0 |
| 79 | + · simp only [h_Lp, toLpₗ_apply, Lp.norm_toLp] |
| 80 | + simp [hp] |
| 81 | + by_cases hp_top : p = ∞ |
| 82 | + · simp only [hp_top, Dual.toLpₗ_apply h_Lp, Lp.norm_toLp, eLpNorm_exponent_top] at h_Lp ⊢ |
| 83 | + simp only [eLpNormEssSup, id_eq] |
| 84 | + suffices (essSup (fun x ↦ ‖L x‖ₑ) μ).toReal ≤ (essSup (fun x ↦ ‖L‖ₑ *‖x‖ₑ) μ).toReal by |
| 85 | + rwa [ENNReal.essSup_const_mul, ENNReal.toReal_mul, toReal_enorm] at this |
| 86 | + gcongr |
| 87 | + · rw [ENNReal.essSup_const_mul] |
| 88 | + exact ENNReal.mul_ne_top (by simp) h_Lp.eLpNorm_ne_top |
| 89 | + · exact essSup_mono_ae <| ae_of_all _ L.le_opNorm_enorm |
| 90 | + have h0 : 0 < p.toReal := by simp [ENNReal.toReal_pos_iff, pos_iff_ne_zero, hp, Ne.lt_top hp_top] |
| 91 | + suffices ‖L.toLpₗ μ p‖ |
| 92 | + ≤ (‖L‖ₑ ^ p.toReal * ∫⁻ x, ‖x‖ₑ ^ p.toReal ∂μ).toReal ^ p.toReal⁻¹ by |
| 93 | + refine this.trans_eq ?_ |
| 94 | + simp only [ENNReal.toReal_mul] |
| 95 | + rw [← ENNReal.toReal_rpow, Real.mul_rpow (by positivity) (by positivity), |
| 96 | + ← Real.rpow_mul (by positivity), mul_inv_cancel₀ h0.ne', Real.rpow_one, toReal_enorm] |
| 97 | + rw [eLpNorm_eq_lintegral_rpow_enorm (by simp [hp]) hp_top, ENNReal.toReal_rpow] |
| 98 | + simp |
| 99 | + rw [Dual.toLpₗ_apply h_Lp, Lp.norm_toLp, eLpNorm_eq_lintegral_rpow_enorm (by simp [hp]) hp_top] |
| 100 | + simp only [ENNReal.toReal_ofNat, ENNReal.rpow_ofNat, one_div] |
| 101 | + refine ENNReal.toReal_le_of_le_ofReal (by positivity) ?_ |
| 102 | + suffices ∫⁻ x, ‖L x‖ₑ ^ p.toReal ∂μ ≤ ‖L‖ₑ ^ p.toReal * ∫⁻ x, ‖x‖ₑ ^ p.toReal ∂μ by |
| 103 | + rw [← ENNReal.ofReal_rpow_of_nonneg (by positivity) (by positivity)] |
| 104 | + gcongr |
| 105 | + rwa [ENNReal.ofReal_toReal] |
| 106 | + refine ENNReal.mul_ne_top (by simp) ?_ |
| 107 | + have h := h_Lp.eLpNorm_ne_top |
| 108 | + rw [eLpNorm_eq_lintegral_rpow_enorm (by simp [hp]) hp_top] at h |
| 109 | + simpa [h0] using h |
| 110 | + calc ∫⁻ x, ‖L x‖ₑ ^ p.toReal ∂μ |
| 111 | + _ ≤ ∫⁻ x, ‖L‖ₑ ^ p.toReal * ‖x‖ₑ ^ p.toReal ∂μ := by |
| 112 | + refine lintegral_mono fun x ↦ ?_ |
| 113 | + rw [← ENNReal.mul_rpow_of_nonneg] |
| 114 | + swap; · positivity |
| 115 | + gcongr |
| 116 | + exact L.le_opNorm_enorm x |
| 117 | + _ = ‖L‖ₑ ^ p.toReal * ∫⁻ x, ‖x‖ₑ ^ p.toReal ∂μ := by rw [lintegral_const_mul]; fun_prop |
| 118 | + |
| 119 | +end LinearMap |
| 120 | + |
| 121 | +section ContinuousLinearMap |
| 122 | + |
| 123 | +variable {𝕜 : Type*} [RCLike 𝕜] [NormedSpace 𝕜 E] [OpensMeasurableSpace E] |
| 124 | + |
| 125 | +/-- Continuous linear map from the dual to `Lp` equal to `MemLp.toLp` if `MemLp id p μ` |
| 126 | +and to 0 otherwise. -/ |
| 127 | +noncomputable |
| 128 | +def toLp (μ : Measure E) (p : ℝ≥0∞) [Fact (1 ≤ p)] : |
| 129 | + Dual 𝕜 E →L[𝕜] Lp 𝕜 p μ where |
| 130 | + toLinearMap := Dual.toLpₗ μ p |
| 131 | + cont := by |
| 132 | + refine LinearMap.continuous_of_locally_bounded _ fun s hs ↦ ?_ |
| 133 | + rw [image_isVonNBounded_iff] |
| 134 | + simp_rw [isVonNBounded_iff'] at hs |
| 135 | + obtain ⟨r, hxr⟩ := hs |
| 136 | + refine ⟨r * (eLpNorm id p μ).toReal, fun L hLs ↦ ?_⟩ |
| 137 | + specialize hxr L hLs |
| 138 | + refine (Dual.norm_toLpₗ_le L).trans ?_ |
| 139 | + gcongr |
| 140 | + |
| 141 | +@[simp] |
| 142 | +lemma toLp_apply [Fact (1 ≤ p)] (h_Lp : MemLp id p μ) (L : Dual 𝕜 E) : |
| 143 | + L.toLp μ p = MemLp.toLp L (h_Lp.continuousLinearMap_comp L) := by |
| 144 | + simp [toLp, h_Lp] |
| 145 | + |
| 146 | +@[simp] |
| 147 | +lemma toLp_of_not_memLp [Fact (1 ≤ p)] (h_Lp : ¬ MemLp id p μ) (L : Dual 𝕜 E) : |
| 148 | + L.toLp μ p = 0 := by |
| 149 | + simp [toLp, h_Lp] |
| 150 | + |
| 151 | +end ContinuousLinearMap |
| 152 | + |
| 153 | +end NormedSpace.Dual |
| 154 | + |
| 155 | +namespace ProbabilityTheory |
| 156 | + |
| 157 | +section Centered |
| 158 | + |
| 159 | +variable [NormedSpace ℝ E] [OpensMeasurableSpace E] |
| 160 | + |
| 161 | +/-- Continuous bilinear form with value `∫ x, L₁ x * L₂ x ∂μ` on `(L₁, L₂)`. |
| 162 | +This is equal to the covariance only if `μ` is centered. -/ |
| 163 | +noncomputable |
| 164 | +def uncenteredCovarianceBilin (μ : Measure E) : Dual ℝ E →L[ℝ] Dual ℝ E →L[ℝ] ℝ := |
| 165 | + ContinuousLinearMap.bilinearComp (isBoundedBilinearMap_inner (𝕜 := ℝ)).toContinuousLinearMap |
| 166 | + (Dual.toLp μ 2) (Dual.toLp μ 2) |
| 167 | + |
| 168 | +lemma uncenteredCovarianceBilin_apply (h : MemLp id 2 μ) (L₁ L₂ : Dual ℝ E) : |
| 169 | + uncenteredCovarianceBilin μ L₁ L₂ = ∫ x, L₁ x * L₂ x ∂μ := by |
| 170 | + simp only [uncenteredCovarianceBilin, ContinuousLinearMap.bilinearComp_apply, |
| 171 | + Dual.toLp_apply h, L2.inner_def, RCLike.inner_apply, conj_trivial] |
| 172 | + refine integral_congr_ae ?_ |
| 173 | + filter_upwards [MemLp.coeFn_toLp (h.continuousLinearMap_comp L₁), |
| 174 | + MemLp.coeFn_toLp (h.continuousLinearMap_comp L₂)] with x hxL₁ hxL₂ |
| 175 | + simp only [id_eq] at hxL₁ hxL₂ |
| 176 | + rw [hxL₁, hxL₂, mul_comm] |
| 177 | + |
| 178 | +lemma uncenteredCovarianceBilin_of_not_memLp (h : ¬ MemLp id 2 μ) (L₁ L₂ : Dual ℝ E) : |
| 179 | + uncenteredCovarianceBilin μ L₁ L₂ = 0 := by |
| 180 | + simp [uncenteredCovarianceBilin, Dual.toLp_of_not_memLp h] |
| 181 | + |
| 182 | +lemma norm_uncenteredCovarianceBilin_le (L₁ L₂ : Dual ℝ E) : |
| 183 | + ‖uncenteredCovarianceBilin μ L₁ L₂‖ ≤ ‖L₁‖ * ‖L₂‖ * ∫ x, ‖x‖ ^ 2 ∂μ := by |
| 184 | + by_cases h : MemLp id 2 μ |
| 185 | + swap; · simp only [uncenteredCovarianceBilin_of_not_memLp h, norm_zero]; positivity |
| 186 | + calc ‖uncenteredCovarianceBilin μ L₁ L₂‖ |
| 187 | + _ = ‖∫ x, L₁ x * L₂ x ∂μ‖ := by rw [uncenteredCovarianceBilin_apply h] |
| 188 | + _ ≤ ∫ x, ‖L₁ x‖ * ‖L₂ x‖ ∂μ := (norm_integral_le_integral_norm _).trans (by simp) |
| 189 | + _ ≤ ∫ x, ‖L₁‖ * ‖x‖ * ‖L₂‖ * ‖x‖ ∂μ := by |
| 190 | + refine integral_mono_ae ?_ ?_ (ae_of_all _ fun x ↦ ?_) |
| 191 | + · simp_rw [← norm_mul] |
| 192 | + exact (MemLp.integrable_mul (h.continuousLinearMap_comp L₁) |
| 193 | + (h.continuousLinearMap_comp L₂)).norm |
| 194 | + · simp_rw [mul_assoc] |
| 195 | + refine Integrable.const_mul ?_ _ |
| 196 | + simp_rw [← mul_assoc, mul_comm _ (‖L₂‖), mul_assoc, ← pow_two] |
| 197 | + refine Integrable.const_mul ?_ _ |
| 198 | + exact h.integrable_norm_pow (by simp) |
| 199 | + · simp only |
| 200 | + rw [mul_assoc] |
| 201 | + gcongr |
| 202 | + · exact ContinuousLinearMap.le_opNorm L₁ x |
| 203 | + · exact ContinuousLinearMap.le_opNorm L₂ x |
| 204 | + _ = ‖L₁‖ * ‖L₂‖ * ∫ x, ‖x‖ ^ 2 ∂μ := by |
| 205 | + rw [← integral_const_mul] |
| 206 | + congr with x |
| 207 | + ring |
| 208 | + |
| 209 | +end Centered |
| 210 | + |
| 211 | +section Covariance |
| 212 | + |
| 213 | +variable [NormedSpace ℝ E] [BorelSpace E] [IsFiniteMeasure μ] |
| 214 | + |
| 215 | +open Classical in |
| 216 | +/-- Continuous bilinear form with value `∫ x, (L₁ x - μ[L₁]) * (L₂ x - μ[L₂]) ∂μ` on `(L₁, L₂)` |
| 217 | +if `MemLp id 2 μ`. If not, we set it to zero. -/ |
| 218 | +noncomputable |
| 219 | +def covarianceBilin (μ : Measure E) : Dual ℝ E →L[ℝ] Dual ℝ E →L[ℝ] ℝ := |
| 220 | + uncenteredCovarianceBilin (μ.map (fun x ↦ x - ∫ x, x ∂μ)) |
| 221 | + |
| 222 | +@[simp] |
| 223 | +lemma covarianceBilin_of_not_memLp (h : ¬ MemLp id 2 μ) (L₁ L₂ : Dual ℝ E) : |
| 224 | + covarianceBilin μ L₁ L₂ = 0 := by |
| 225 | + rw [covarianceBilin, uncenteredCovarianceBilin_of_not_memLp] |
| 226 | + rw [(measurableEmbedding_subRight _).memLp_map_measure_iff] |
| 227 | + refine fun h_Lp ↦ h ?_ |
| 228 | + have : (id : E → E) = fun x ↦ x - ∫ x, x ∂μ + ∫ x, x ∂μ := by ext; simp |
| 229 | + rw [this] |
| 230 | + exact h_Lp.add (memLp_const _) |
| 231 | + |
| 232 | +variable [CompleteSpace E] |
| 233 | + |
| 234 | +lemma covarianceBilin_apply (h : MemLp id 2 μ) (L₁ L₂ : Dual ℝ E) : |
| 235 | + covarianceBilin μ L₁ L₂ = ∫ x, (L₁ x - μ[L₁]) * (L₂ x - μ[L₂]) ∂μ := by |
| 236 | + rw [covarianceBilin, uncenteredCovarianceBilin_apply, |
| 237 | + integral_map (by fun_prop) (by fun_prop)] |
| 238 | + · have hL (L : Dual ℝ E) : μ[L] = L (∫ x, x ∂μ) := L.integral_comp_comm (h.integrable (by simp)) |
| 239 | + simp [← hL] |
| 240 | + · exact (measurableEmbedding_subRight _).memLp_map_measure_iff.mpr <| h.sub (memLp_const _) |
| 241 | + |
| 242 | +lemma covarianceBilin_same_eq_variance (h : MemLp id 2 μ) (L : Dual ℝ E) : |
| 243 | + covarianceBilin μ L L = Var[L; μ] := by |
| 244 | + rw [covarianceBilin_apply h, variance_eq_integral (by fun_prop)] |
| 245 | + simp_rw [pow_two] |
| 246 | + |
| 247 | +end Covariance |
| 248 | + |
| 249 | +end ProbabilityTheory |
0 commit comments