Skip to content

Commit 59c05f4

Browse files
committed
perf(MeasureTheory,Probability): expand slow measurabilty calls (#13770)
Manually expand slow `measurability` calls in various files: see individual commit messages for details. From the benchmarking run, this seems to net -160*10⁹ instructions. The `RadonNikodym` and `SignedMeasure` files each speed up by more than half (33 -> 18 billion resp. 100 -> 40 billion instructions). `Group/Convolution` is galvanised, 55 -> 8 billion instructions. The independence files become 10 billion instructions faster.
1 parent 46b526e commit 59c05f4

File tree

7 files changed

+68
-14
lines changed

7 files changed

+68
-14
lines changed

Mathlib/Analysis/SpecialFunctions/JapaneseBracket.lean

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ theorem integrable_rpow_neg_one_add_norm_sq {r : ℝ} (hnr : (finrank ℝ E :
156156
refine ((integrable_one_add_norm hnr).const_mul <| (2 : ℝ) ^ (r / 2)).mono'
157157
?_ (eventually_of_forall fun x => ?_)
158158
· -- Note: `measurability` proves this, but very slowly.
159+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
159160
exact measurable_norm.pow_const 2 |>.const_add 1 |>.pow_const (-r / 2) |>.aestronglyMeasurable
160161
refine (abs_of_pos ?_).trans_le (rpow_neg_one_add_norm_sq_le x hr)
161162
positivity

Mathlib/MeasureTheory/Decomposition/RadonNikodym.lean

+3-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ theorem withDensityᵥ_rnDeriv_eq (s : SignedMeasure α) (μ : Measure α) [Sigm
428428
refine IntegrableOn.restrict ?_ MeasurableSet.univ
429429
refine ⟨?_, hasFiniteIntegral_toReal_of_lintegral_ne_top ?_⟩
430430
· apply Measurable.aestronglyMeasurable
431-
measurability
431+
-- NB. `measurability` proves this, but is quite slow
432+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
433+
apply (Measure.measurable_rnDeriv _ μ).ennreal_toNNReal.coe_nnreal_real
432434
· rw [set_lintegral_univ]
433435
exact (lintegral_rnDeriv_lt_top _ _).ne
434436
· exact equivMeasure.right_inv μ

Mathlib/MeasureTheory/Decomposition/SignedLebesgue.lean

+17-4
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,21 @@ variable {s t : SignedMeasure α}
189189
@[measurability]
190190
theorem measurable_rnDeriv (s : SignedMeasure α) (μ : Measure α) : Measurable (rnDeriv s μ) := by
191191
rw [rnDeriv_def]
192-
measurability
192+
-- Note. `measurabilty` proves this, but is very slow
193+
apply Measurable.add
194+
· exact ((Measure.measurable_rnDeriv _ μ).ennreal_toNNReal).coe_nnreal_real
195+
· rw [measurable_neg_iff]
196+
exact (Measure.measurable_rnDeriv _ μ).ennreal_toNNReal.coe_nnreal_real
197+
193198
#align measure_theory.signed_measure.measurable_rn_deriv MeasureTheory.SignedMeasure.measurable_rnDeriv
194199

195200
theorem integrable_rnDeriv (s : SignedMeasure α) (μ : Measure α) : Integrable (rnDeriv s μ) μ := by
196201
refine Integrable.sub ?_ ?_ <;>
197202
· constructor
198-
· apply Measurable.aestronglyMeasurable; measurability
203+
· -- NB: `measurability` proves this, but is very slow
204+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
205+
exact (((Measure.measurable_rnDeriv _ μ
206+
).ennreal_toNNReal).coe_nnreal_real).aestronglyMeasurable
199207
exact hasFiniteIntegral_toReal_of_lintegral_ne_top (lintegral_rnDeriv_lt_top _ μ).ne
200208
#align measure_theory.signed_measure.integrable_rn_deriv MeasureTheory.SignedMeasure.integrable_rnDeriv
201209

@@ -306,10 +314,15 @@ private theorem eq_singularPart' (t : SignedMeasure α) {f : α → ℝ} (hf : M
306314
rw [singularPart, ← t.toSignedMeasure_toJordanDecomposition,
307315
JordanDecomposition.toSignedMeasure]
308316
congr
309-
· have hfpos : Measurable fun x => ENNReal.ofReal (f x) := by measurability
317+
-- NB: `measurability` proves this `have`, but is slow.
318+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
319+
· have hfpos : Measurable fun x => ENNReal.ofReal (f x) := hf.real_toNNReal.coe_nnreal_ennreal
310320
refine eq_singularPart hfpos htμ.1 ?_
311321
rw [toJordanDecomposition_eq_of_eq_add_withDensity hf hfi htμ' hadd]
312-
· have hfneg : Measurable fun x => ENNReal.ofReal (-f x) := by measurability
322+
· have hfneg : Measurable fun x => ENNReal.ofReal (-f x) :=
323+
-- NB: `measurability` proves this, but is slow.
324+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
325+
(measurable_neg_iff.mpr hf).real_toNNReal.coe_nnreal_ennreal
313326
refine eq_singularPart hfneg htμ.2 ?_
314327
rw [toJordanDecomposition_eq_of_eq_add_withDensity hf hfi htμ' hadd]
315328

Mathlib/MeasureTheory/Group/Convolution.lean

+26-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ theorem dirac_one_mconv [MeasurableMul₂ M] (μ : Measure M) [SFinite μ] :
4343
unfold mconv
4444
rw [MeasureTheory.Measure.dirac_prod, map_map]
4545
· simp only [Function.comp_def, one_mul, map_id']
46-
all_goals { measurability }
46+
· -- NB. `measurability` proves this, but is slow
47+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
48+
exact Measurable.mul measurable_fst measurable_snd
49+
measurability
4750

4851
/-- Convolution of a measure μ with the dirac measure at 1 returns μ. -/
4952
@[to_additive (attr := simp)]
@@ -52,7 +55,14 @@ theorem mconv_dirac_one [MeasurableMul₂ M]
5255
unfold mconv
5356
rw [MeasureTheory.Measure.prod_dirac, map_map]
5457
· simp only [Function.comp_def, mul_one, map_id']
55-
all_goals { measurability }
58+
· -- NB. `measurability` proves this, but is slow
59+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
60+
exact Measurable.mul measurable_fst measurable_snd
61+
-- NB. `measurability` proves this, but is slow
62+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
63+
apply Measurable.prod
64+
· apply measurable_id'
65+
· exact measurable_const
5666

5767
/-- Convolution of the zero measure with a measure μ returns the zero measure. -/
5868
@[to_additive (attr := simp) conv_zero]
@@ -71,14 +81,18 @@ theorem mconv_add [MeasurableMul₂ M] (μ : Measure M) (ν : Measure M) (ρ : M
7181
[SFinite ν] [SFinite ρ] : μ ∗ (ν + ρ) = μ ∗ ν + μ ∗ ρ := by
7282
unfold mconv
7383
rw [prod_add, map_add]
74-
measurability
84+
-- NB. `measurability` proves this, but is pretty slow
85+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
86+
exact Measurable.mul measurable_fst measurable_snd
7587

7688
@[to_additive add_conv]
7789
theorem add_mconv [MeasurableMul₂ M] (μ : Measure M) (ν : Measure M) (ρ : Measure M) [SFinite μ]
7890
[SFinite ν] [SFinite ρ] : (μ + ν) ∗ ρ = μ ∗ ρ + ν ∗ ρ := by
7991
unfold mconv
8092
rw [add_prod, map_add]
81-
measurability
93+
-- NB. `measurability` proves this, but is pretty slow
94+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
95+
exact Measurable.mul measurable_fst measurable_snd
8296

8397
/-- To get commutativity, we need the underlying multiplication to be commutative. -/
8498
@[to_additive conv_comm]
@@ -87,7 +101,10 @@ theorem mconv_comm {M : Type*} [CommMonoid M] [MeasurableSpace M] [MeasurableMul
87101
unfold mconv
88102
rw [← prod_swap, map_map]
89103
· simp [Function.comp_def, mul_comm]
90-
all_goals { measurability }
104+
· -- NB. `measurability` proves this, but is pretty slow
105+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
106+
exact Measurable.mul measurable_fst measurable_snd
107+
measurability
91108

92109
/-- Convolution of SFinite maps is SFinite. -/
93110
@[to_additive sfinite_conv_of_sfinite]
@@ -107,7 +124,10 @@ instance probabilitymeasure_of_probabilitymeasures_mconv (μ : Measure M) (ν :
107124
[MeasurableMul₂ M] [IsProbabilityMeasure μ] [IsProbabilityMeasure ν] :
108125
IsProbabilityMeasure (μ ∗ ν) := by
109126
apply MeasureTheory.isProbabilityMeasure_map
110-
measurability
127+
-- NB. `measurability` proves this, but is really slow
128+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
129+
exact AEMeasurable.mul (measurable_fst.comp_aemeasurable' aemeasurable_id')
130+
(measurable_snd.comp_aemeasurable' aemeasurable_id')
111131

112132
end Measure
113133

Mathlib/Probability/Independence/Basic.lean

+7-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,13 @@ lemma iIndepFun.indepFun_prod_mk_prod_mk (h_indep : iIndepFun m f μ) (hf : ∀
710710
classical
711711
let g (i j : ι) (v : Π x : ({i, j} : Finset ι), β x) : β i × β j :=
712712
⟨v ⟨i, mem_insert_self _ _⟩, v ⟨j, mem_insert_of_mem <| mem_singleton_self _⟩⟩
713-
have hg (i j : ι) : Measurable (g i j) := by measurability
713+
have hg (i j : ι) : Measurable (g i j) := by
714+
-- NB: `measurability proves this, but is slow
715+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
716+
simp only [ne_eq, g]
717+
apply Measurable.prod
718+
· exact measurable_pi_apply _
719+
· exact measurable_pi_apply _
714720
exact (h_indep.indepFun_finset {i, j} {k, l} (by aesop) hf).comp (hg i j) (hg k l)
715721

716722
end iIndepFun

Mathlib/Probability/Independence/Conditional.lean

+7-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,13 @@ lemma iCondIndepFun.condIndepFun_prod_mk_prod_mk (h_indep : iCondIndepFun m' hm'
731731
classical
732732
let g (i j : ι) (v : Π x : ({i, j} : Finset ι), β x) : β i × β j :=
733733
⟨v ⟨i, mem_insert_self _ _⟩, v ⟨j, mem_insert_of_mem <| mem_singleton_self _⟩⟩
734-
have hg (i j : ι) : Measurable (g i j) := by measurability
734+
have hg (i j : ι) : Measurable (g i j) := by
735+
-- NB: `measurability proves this, but is slow
736+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
737+
simp only [ne_eq, g]
738+
apply Measurable.prod
739+
· exact measurable_pi_apply _
740+
· exact measurable_pi_apply _
735741
exact (h_indep.indepFun_finset {i, j} {k, l} (by aesop) hf).comp (hg i j) (hg k l)
736742

737743
end iCondIndepFun

Mathlib/Probability/Independence/Kernel.lean

+7-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,13 @@ lemma iIndepFun.indepFun_prod_mk_prod_mk [IsMarkovKernel κ] (hf_indep : iIndepF
963963
classical
964964
let g (i j : ι) (v : Π x : ({i, j} : Finset ι), β x) : β i × β j :=
965965
⟨v ⟨i, mem_insert_self _ _⟩, v ⟨j, mem_insert_of_mem <| mem_singleton_self _⟩⟩
966-
have hg (i j : ι) : Measurable (g i j) := by measurability
966+
have hg (i j : ι) : Measurable (g i j) := by
967+
-- NB: `measurability proves this, but is slow
968+
-- TODO(#13864): reinstate faster automation, e.g. by making `fun_prop` work here
969+
simp only [ne_eq, g]
970+
apply Measurable.prod
971+
· exact measurable_pi_apply _
972+
· exact measurable_pi_apply _
967973
exact (hf_indep.indepFun_finset {i, j} {k, l} (by aesop) hf_meas).comp (hg i j) (hg k l)
968974

969975
end iIndepFun

0 commit comments

Comments
 (0)