Bayesian model, expected information gain and variational EIG bounds
DefinitionVariationalBOED_BasicObjects of Sections 2-4 and Appendices A-B of Foster et al. (2019). Everything is for one fixed design , which is not written.
- Densities. The parameter ranges over a measurable space and the outcome over a measurable space , carrying -finite reference measures and (for instance Lebesgue or counting measure). A probability density is a measurable with ; a conditional density is jointly measurable and a probability density in for every . denotes the measure with density , and is the (differential) entropy.
- Model (Section 2). A prior density and a likelihood . The marginal is , the posterior is , and the joint law of has density .
- Expected information gain (eq. 3). .
- Validity (Appendix A.1). An approximation is valid when it is a (conditional) density with the same support as the density it approximates: for every with ; ; . A valid VNMC proposal is a valid posterior approximation such that, for every with , the weight is bounded in (the assumption used in the proof of Lemma 1).
- Bounds and estimates (Section 3).
- (eq. 6);
- (eq. 9);
- with (eq. 10);
- (eq. 12).
- Lemma 1 (3). For fixed , the product law and the mixture on .
- Monte Carlo (Theorem 1). For samples and : and .
Formalization Note Densities are real-valued, and every expectation is a Bochner integral. Lean gives a non-integrable integrand the integral , so each statement assumes outright that the expectations it uses are finite. in Lean, but the support conditions make every logarithm that matters have a positive argument almost surely. The design is fixed and suppressed. The samples are indexed from .
import Mathlib
/-!
Foster, Jankowiak, Bingham, Horsfall, Teh, Rainforth and Goodman, *Variational Bayesian Optimal
Experimental Design*, arXiv:1903.05480v3 (NeurIPS 2019): the Bayesian model and the expected
information gain of Section 2 (eq. (3)), the variational bounds and estimates `L_post`, `U_marg`,
`U_VNMC`, `I_{m+ℓ}` of Section 3 (eqs. (6), (9), (10), (12)) with the validity conventions of
Appendix A, and the Monte Carlo estimator of Theorem 1 (Section 4).
Conventions. All quantities are for one fixed design `d`, which is suppressed from the notation.
The parameter `θ` lives in a measurable space `Th` and the outcome `y` in a measurable space `Y`;
all densities (`p(θ)`, `p(y|θ,d)`, and the variational densities) are real-valued densities with
respect to fixed reference measures `nuT` on `Th` and `nuY` on `Y` (for instance Lebesgue or
counting measure). A conditional density of `θ` given `y` is a function `Y → Th → ℝ`; a
conditional density of `y` given `θ` is a function `Th → Y → ℝ`.
-/
open MeasureTheory
noncomputable section
namespace VariationalBOED
section Densities
variable {α β : Type*} [MeasurableSpace α] [MeasurableSpace β]
/-- Section 2: `f` is a probability density with respect to the reference measure `ν`:
measurable, nonnegative, and of total integral `1`. -/
def IsProbDensity (ν : Measure α) (f : α → ℝ) : Prop :=
Measurable f ∧ (∀ x, 0 ≤ f x) ∧ ∫ x, f x ∂ν = 1
/-- Section 2: `f a ·` is a probability density with respect to `ν` for every `a`, and
`(a, b) ↦ f a b` is jointly measurable (a conditional density such as `p(y|θ,d)`). -/
def IsCondDensity (ν : Measure β) (f : α → β → ℝ) : Prop :=
Measurable (Function.uncurry f) ∧ (∀ a b, 0 ≤ f a b) ∧ ∀ a, ∫ b, f a b ∂ν = 1
/-- The measure with density `f` with respect to `ν`, i.e. the distribution written `f(x)` in the
paper (Sections 2-3). -/
def densMeasure (ν : Measure α) (f : α → ℝ) : Measure α :=
ν.withDensity (fun x => ENNReal.ofReal (f x))
/-- Lemma 2 and Appendix A.4: the (differential) entropy `H[f] = -E_f[log f]` of a density `f`
with respect to `ν`. -/
def diffEntropy (ν : Measure α) (f : α → ℝ) : ℝ :=
-∫ x, f x * Real.log (f x) ∂ν
end Densities
section Model
variable {Th Y : Type*} [MeasurableSpace Th] [MeasurableSpace Y]
/-- Section 2: a Bayesian model `p(θ) p(y|θ,d)`: a prior density `p` on `Th` and a likelihood
`lik θ y = p(y|θ,d)` that is a probability density in `y` for every `θ`. -/
def IsModel (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ) : Prop :=
IsProbDensity nuT p ∧ IsCondDensity nuY lik
/-- Section 2: the marginal density `p(y|d) = E_{p(θ)}[p(y|θ,d)]`. -/
def marginal (nuT : Measure Th) (p : Th → ℝ) (lik : Th → Y → ℝ) (y : Y) : ℝ :=
∫ θ, p θ * lik θ y ∂nuT
/-- Section 2, eq. (3): the posterior density `p(θ|y,d) = p(θ) p(y|θ,d) / p(y|d)` (meaningful
where `p(y|d) > 0`). -/
def posterior (nuT : Measure Th) (p : Th → ℝ) (lik : Th → Y → ℝ) (y : Y) (θ : Th) : ℝ :=
p θ * lik θ y / marginal nuT p lik y
/-- Section 2: the joint distribution `p(y,θ|d) = p(θ) p(y|θ,d)` of `(θ, y)`. -/
def jointLaw (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ) :
Measure (Th × Y) :=
densMeasure (nuT.prod nuY) (fun z => p z.1 * lik z.1 z.2)
/-- Section 2: the marginal distribution `p(y|d)` of the outcome. -/
def marginalLaw (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ) :
Measure Y :=
densMeasure nuY (marginal nuT p lik)
/-- Eq. (3): the integrand `log (p(y|θ,d) / p(y|d))` of the expected information gain. -/
def eigIntegrand (nuT : Measure Th) (p : Th → ℝ) (lik : Th → Y → ℝ) (z : Th × Y) : ℝ :=
Real.log (lik z.1 z.2 / marginal nuT p lik z.2)
/-- Eq. (3): the expected information gain
`EIG(d) = E_{p(y,θ|d)}[log (p(y|θ,d) / p(y|d))]`. -/
def eig (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ) : ℝ :=
∫ z, eigIntegrand nuT p lik z ∂jointLaw nuT nuY p lik
/-- Appendix A.1: a valid posterior approximation `q(θ|y,d)`: a conditional density of `θ` given
`y` with the same support as the posterior `p(θ|y,d)` at every `y` with `p(y|d) > 0`. -/
def IsValidPosteriorApprox (nuT : Measure Th) (p : Th → ℝ) (lik : Th → Y → ℝ)
(q : Y → Th → ℝ) : Prop :=
IsCondDensity nuT q ∧
∀ y, 0 < marginal nuT p lik y → ∀ θ, (0 < q y θ ↔ 0 < posterior nuT p lik y θ)
/-- Eq. (6): the integrand `log (q_p(θ|y,d) / p(θ))` of the variational posterior bound. -/
def postIntegrand (p : Th → ℝ) (q : Y → Th → ℝ) (z : Th × Y) : ℝ :=
Real.log (q z.2 z.1 / p z.1)
/-- Eq. (6): the variational posterior bound
`L_post(d) = E_{p(y,θ|d)}[log (q_p(θ|y,d) / p(θ))]`. -/
def postBound (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ)
(q : Y → Th → ℝ) : ℝ :=
∫ z, postIntegrand p q z ∂jointLaw nuT nuY p lik
/-- Appendix A.2 (with the validity convention of Appendix A.1): a valid marginal approximation
`q_m(y|d)`: a probability density on `Y` with the same support as `p(y|d)`. -/
def IsValidMarginalApprox (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ)
(lik : Th → Y → ℝ) (qm : Y → ℝ) : Prop :=
IsProbDensity nuY qm ∧ ∀ y, (0 < qm y ↔ 0 < marginal nuT p lik y)
/-- Eq. (9): the integrand `log (p(y|θ,d) / q_m(y|d))` of the variational marginal bound. -/
def margIntegrand (lik : Th → Y → ℝ) (qm : Y → ℝ) (z : Th × Y) : ℝ :=
Real.log (lik z.1 z.2 / qm z.2)
/-- Eq. (9): the variational marginal bound
`U_marg(d) = E_{p(y,θ|d)}[log (p(y|θ,d) / q_m(y|d))]`. -/
def margBound (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ)
(qm : Y → ℝ) : ℝ :=
∫ z, margIntegrand lik qm z ∂jointLaw nuT nuY p lik
/-- Lemma 1 with its proof in Appendix A.3: a valid VNMC proposal `q_v(θ|y,d)`: a valid posterior
approximation (Appendix A.1) such that, at every `y` with `p(y|d) > 0`, the importance weight
`p(y,θ|d) / q_v(θ|y,d)` is bounded in `θ` (the assumption invoked in the proof in Appendix A.3). -/
def IsValidVNMCProposal (nuT : Measure Th) (p : Th → ℝ) (lik : Th → Y → ℝ)
(qv : Y → Th → ℝ) : Prop :=
IsValidPosteriorApprox nuT p lik qv ∧
∀ y, 0 < marginal nuT p lik y → ∃ B : ℝ, ∀ θ, p θ * lik θ y / qv y θ ≤ B
/-- Eq. (10): the sampling distribution of `(θ₀, y, θ_{1:L})`, namely
`p(y, θ₀|d) ∏_{ℓ=1}^L q_v(θ_ℓ|y,d)`. -/
def vnmcLaw (nuT : Measure Th) [SigmaFinite nuT] (nuY : Measure Y) (p : Th → ℝ)
(lik : Th → Y → ℝ) (qv : Y → Th → ℝ) (L : ℕ) : Measure ((Th × Y) × (Fin L → Th)) :=
densMeasure ((nuT.prod nuY).prod (Measure.pi fun _ : Fin L => nuT))
(fun x => p x.1.1 * lik x.1.1 x.1.2 * ∏ i, qv x.1.2 (x.2 i))
/-- Eq. (10): the integrand
`log p(y|θ₀,d) - log ((1/L) ∑_{ℓ=1}^L p(y,θ_ℓ|d) / q_v(θ_ℓ|y,d))` of the VNMC bound. -/
def vnmcIntegrand (p : Th → ℝ) (lik : Th → Y → ℝ) (qv : Y → Th → ℝ) (L : ℕ)
(x : (Th × Y) × (Fin L → Th)) : ℝ :=
Real.log (lik x.1.1 x.1.2) -
Real.log ((1 / (L : ℝ)) * ∑ i, p (x.2 i) * lik (x.2 i) x.1.2 / qv x.1.2 (x.2 i))
/-- Eq. (10): the variational nested Monte Carlo bound `U_VNMC(d, L)`. -/
def vnmcBound (nuT : Measure Th) [SigmaFinite nuT] (nuY : Measure Y) (p : Th → ℝ)
(lik : Th → Y → ℝ) (qv : Y → Th → ℝ) (L : ℕ) : ℝ :=
∫ x, vnmcIntegrand p lik qv L x ∂vnmcLaw nuT nuY p lik qv L
/-- Lemma 1 (3): the distribution `∏_{ℓ=1}^L q_v(θ_ℓ|y,d)` of `L` independent proposal draws. -/
def vnmcProposalProd (nuT : Measure Th) [SigmaFinite nuT] (qv : Y → Th → ℝ) (L : ℕ) (y : Y) :
Measure (Fin L → Th) :=
densMeasure (Measure.pi fun _ : Fin L => nuT) (fun θs => ∏ i, qv y (θs i))
/-- Lemma 1 (3): the mixture
`(1/L) ∑_{ℓ=1}^L p(θ_ℓ|y,d) ∏_{k ≠ ℓ} q_v(θ_k|y,d)` on `L`-tuples of parameters. -/
def vnmcMixture (nuT : Measure Th) [SigmaFinite nuT] (p : Th → ℝ) (lik : Th → Y → ℝ)
(qv : Y → Th → ℝ) (L : ℕ) (y : Y) : Measure (Fin L → Th) :=
densMeasure (Measure.pi fun _ : Fin L => nuT)
(fun θs => (1 / (L : ℝ)) *
∑ l, posterior nuT p lik y (θs l) * ∏ k ∈ Finset.univ.erase l, qv y (θs k))
/-- Section 3 (implicit likelihood, eq. (12)) with the validity convention of Appendix A.1: a valid
likelihood approximation `q_ℓ(y|θ,d)`: a conditional density of `y` given `θ` with the same
support as `p(y|θ,d)`. -/
def IsValidLikelihoodApprox (nuY : Measure Y) (lik : Th → Y → ℝ) (ql : Th → Y → ℝ) : Prop :=
IsCondDensity nuY ql ∧ ∀ θ y, (0 < ql θ y ↔ 0 < lik θ y)
/-- Eq. (12): the integrand `log (q_ℓ(y|θ,d) / q_m(y|d))` of the marginal + likelihood
estimate. -/
def mlIntegrand (qm : Y → ℝ) (ql : Th → Y → ℝ) (z : Th × Y) : ℝ :=
Real.log (ql z.1 z.2 / qm z.2)
/-- Eq. (12): the marginal + likelihood approximation
`I_{m+ℓ}(d) = E_{p(y,θ|d)}[log (q_ℓ(y|θ,d) / q_m(y|d))]`. -/
def mlEstimate (nuT : Measure Th) (nuY : Measure Y) (p : Th → ℝ) (lik : Th → Y → ℝ)
(qm : Y → ℝ) (ql : Th → Y → ℝ) : ℝ :=
∫ z, mlIntegrand qm ql z ∂jointLaw nuT nuY p lik
end Model
section MonteCarlo
variable {Ω S E : Type*} [MeasurableSpace Ω]
/-- Theorem 1: the Monte Carlo estimator `μ̂_N(φ) = (1/N) ∑_{n=1}^N f(X_n, φ)` (the samples
`X_1, X_2, …` are indexed from `0` here). -/
def mcEstimate (f : S → E → ℝ) (X : ℕ → Ω → S) (N : ℕ) (φ : E) (ω : Ω) : ℝ :=
(1 / (N : ℝ)) * ∑ n ∈ Finset.range N, f (X n ω) φ
/-- Theorem 1: the target `μ(φ) = E[f(X_1, φ)]` of the Monte Carlo estimator. -/
def mcMean (P : Measure Ω) (f : S → E → ℝ) (X : ℕ → Ω → S) (φ : E) : ℝ :=
∫ ω, f (X 0 ω) φ ∂P
end MonteCarlo
end VariationalBOED
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Probability density (IsProbDensity). Let be a measurable space with a measure , and let . The predicate holds when all three of the following are true:
- is measurable;
- for every , everywhere and not just almost everywhere;
- .
The integral is the Bochner integral, which is defined to be when is not -integrable. So the last condition also forces to be integrable. No finiteness or -finiteness is assumed of .
Conditional density (IsCondDensity). Let be measurable spaces, a measure on , and , written . The predicate holds when:
- is measurable for the product -algebra on ;
- for all ;
- for every , , with the same Bochner convention, so each section is -integrable.
No measure on is involved.
Measure with density (densMeasure). For a measure on and , this is the measure
Negative values of are silently replaced by . The result is not required to be a probability measure, or even finite, unless happens to be a probability density.
Differential entropy (diffEntropy). For a measure on and ,
Here is the real logarithm with the conventions and for , so points where contribute . The integral is if the integrand is not integrable, in which case . No hypothesis on is imposed.
Bayesian model (IsModel). Let and be measurable spaces with measures and . Let and . The predicate holds when both of the following are true:
- is a probability density with respect to : measurable, everywhere , and .
- is a conditional density in with respect to :
- is jointly measurable;
- everywhere;
- for every .
No compatibility between and and no -finiteness are required.
Marginal (marginal). For , , as above and any ,
is defined to be whenever is not -integrable. No model assumptions are imposed, so may be negative or junk for arbitrary .
Posterior (posterior). For and ,
where is the marginal above. Division by zero is defined to give , so for all whenever . This includes the case where the marginal integral fails to exist.
Joint law (jointLaw). This is the measure on given by
Here is the library's product measure, with no -finiteness assumed. Negative values of the product are clipped to . is not asserted to be a probability measure.
Marginal law (marginalLaw). This is the measure on with density with respect to , where is the marginal above (with its "non-integrable gives " convention).
EIG integrand (eigIntegrand). For ,
The following conventions apply:
- , so the value is when .
- , so the value is (not ) when .
- for negative arguments.
Expected information gain (eig). For measures and functions (no model hypotheses assumed),
- is the joint law, with density with respect to .
- , taken as if not integrable.
- The logarithm conventions are , and .
- The whole quantity is if the integrand is not -integrable. It is a real number, never .
Valid posterior approximation (IsValidPosteriorApprox). For , , and , written , the predicate holds when two conditions are met.
First, is a conditional density in with respect to :
- is jointly measurable;
- everywhere;
- for every , including those with .
Second, for every with and every ,
Since , the right-hand side is equivalent to . This support condition is pointwise for every , not up to null sets. It says nothing about with . No measure on enters.
Posterior-bound integrand (postIntegrand). For ,
The conventions are and . So the value is when or , and for negative arguments.
Variational posterior bound (postBound). For with no hypotheses,
- is the joint law with density with respect to .
- The conventions are , and .
- The value is if the integrand is not -integrable.
Nothing in this definition asserts that it is a bound on anything.
Valid marginal approximation (IsValidMarginalApprox). For and , the predicate holds when two conditions are met:
- is a probability density with respect to : measurable, everywhere , and .
- For every , pointwise, , where (taken as if not integrable).
Marginal-bound integrand (margIntegrand). For ,
The conventions are , and . So the value is when or .
Variational marginal bound (margBound). With no hypotheses on the inputs,
- is the joint law with density with respect to .
- The conventions are , and .
- The value is if the integrand is not -integrable.
Valid VNMC proposal (IsValidVNMCProposal). For and , the predicate holds when two conditions are met.
First, is a valid posterior approximation in the sense above:
- it is a jointly measurable, everywhere nonnegative function;
- for every ;
- for every with and every , .
Second, for every with there is a real number (depending on ) such that for all ,
Because , the ratio equals wherever , so the bound only constrains points where . Only an upper bound is required, not a bound on the absolute value. Nothing is required at with .
VNMC sampling law (vnmcLaw). Assume is -finite and fix , with allowed. Write a point of as ; the code indexes the extra parameters by . The law is the measure with density
with respect to .
When , the product is empty (equal to ) and is a single point carrying unit mass. In that case the measure is just a copy of the joint law . No hypotheses on are imposed, and the measure is not asserted to be a probability measure.
VNMC integrand (vnmcIntegrand). For and a point ,
The conventions are:
- and ;
- , so any term with contributes ;
- is computed in , with .
When , the second logarithm is , so the integrand reduces to .
VNMC bound (vnmcBound). With -finite and (including ),
- is the measure on with density with respect to .
- The conventions are , , and .
- The value is if the integrand is not -integrable.
- For it becomes .
No hypotheses on are imposed.
Product of proposal draws (vnmcProposalProd). Assume is -finite and fix and . This is the measure on with density
with respect to . When , it is the unit point mass on the single point of . It is a probability measure only if is a probability density, which is not assumed here.
VNMC mixture (vnmcMixture). Assume is -finite and fix and . This is the measure on with density with respect to given by
Here , with when , and uses . When , the sum is empty, so the result is the zero measure. When , it is also the zero measure.
Valid likelihood approximation (IsValidLikelihoodApprox). For , and , the predicate holds when two conditions are met:
- is a conditional density in with respect to : jointly measurable, everywhere , and for every .
- For every and , pointwise, .
The prior plays no role.
Marginal-plus-likelihood integrand (mlIntegrand). For ,
The conventions are , and .
Marginal-plus-likelihood estimate (mlEstimate). With no hypotheses on the inputs,
- is the joint law with density with respect to .
- The conventions are , and .
- The value is if the integrand is not -integrable.
The true likelihood enters only through .
Monte Carlo estimate (mcEstimate). Let be a measurable space, and let and be arbitrary types. Take , a sequence of functions , , and . Then
Only the first functions are used, and , so . No measurability, independence or identical-distribution assumptions are made on the , and none on .
Monte Carlo target (mcMean). For a measure on (not required to be a probability measure), , the sequence and ,
Only is used. The value is if is not -integrable.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.