Log-derivative trick:
ProvedScoreFunction.likelihood_ratio_gradientThis is the log-derivative trick (also called the score-function or likelihood-ratio gradient identity) in its measure-theoretic form: the continuous analogue of the finite-sum identity likelihood_ratio_gradient already on the platform.
Let be a measure space and let be a family of densities with respect to , indexed by a real parameter . For an observable , write
Then, under the interchange conditions listed below,
The right-hand side is again an expectation under itself, which is what makes the derivative estimable by Monte Carlo from samples of ; the intermediate quantity is in general not a density, so it cannot be sampled directly. This is the identity behind the REINFORCE and policy-gradient estimators in reinforcement learning, the score-function gradient estimator for variational objectives, and likelihood-ratio sensitivity analysis in stochastic simulation.
The hypotheses are the standard dominated-derivative conditions for differentiating under the integral sign, stated on a neighbourhood of the base point :
- -almost everywhere, so that is defined a.e.;
- is almost-everywhere strongly measurable for every near , and integrable at , so that exists;
- for almost every , the map is differentiable at every with derivative ;
- the family of derivatives is dominated: for almost every and all , with integrable.
Normalization of is deliberately not assumed: the identity is a statement about differentiating a weighted integral and holds for any positive weight family. The normalized case, in which the score has mean zero, is a separate statement.
Formalization Note The derivative family is supplied explicitly as p' via HasDerivAt (fun u => p u x) (p' t x) t rather than through deriv, which avoids the junk value deriv takes at points of non-differentiability. The conclusion is nevertheless phrased with deriv (fun t => Real.log (p t x)) θ, so the score appears literally as a logarithmic derivative. bound and the neighbourhood s are explicit parameters; taking s ∈ 𝓝 θ rather than a ball keeps the statement independent of the metric. The hypotheses line up one-for-one with Mathlib's hasDerivAt_integral_of_dominated_loc_of_deriv_le.
import Mathlib.Analysis.Calculus.ParametricIntegral import Mathlib.Analysis.SpecialFunctions.Log.Deriv open MeasureTheory Filter open scoped Topology
namespace ScoreFunction
theorem likelihood_ratio_gradient
{X : Type*} [MeasurableSpace X] {μ : Measure X}
{p p' : ℝ → X → ℝ} {f bound : X → ℝ} {θ : ℝ} {s : Set ℝ}
(hs : s ∈ 𝓝 θ)
(hp_pos : ∀ᵐ x ∂μ, 0 < p θ x)
(hF_meas : ∀ᶠ t in 𝓝 θ, AEStronglyMeasurable (fun x => f x * p t x) μ)
(hF_int : Integrable (fun x => f x * p θ x) μ)
(hF'_meas : AEStronglyMeasurable (fun x => f x * p' θ x) μ)
(h_bound : ∀ᵐ x ∂μ, ∀ t ∈ s, |f x * p' t x| ≤ bound x)
(h_bound_int : Integrable bound μ)
(h_diff : ∀ᵐ x ∂μ, ∀ t ∈ s, HasDerivAt (fun u => p u x) (p' t x) t) :
deriv (fun t => ∫ x, f x * p t x ∂μ) θ
= ∫ x, f x * p θ x * deriv (fun t => Real.log (p t x)) θ ∂μ := by sorry
end ScoreFunction