Skip to content

PR draft — ClosedFormStrategy: fail clearly on non-constant base measure (fixes issue-01) - #108

Open
docxology wants to merge 1 commit into
ReactiveBayes:mainfrom
docxology:fix/closedform-basemeasure
Open

docxology wants to merge 1 commit into
ReactiveBayes:mainfrom
docxology:fix/closedform-basemeasure

Conversation

@docxology

Copy link
Copy Markdown

Problem

ClosedFormStrategy.compute_gradient! only defines logbasemeasure_correction for ConstantBaseMeasure. For variational families with a non-constant base measure (Poisson, Chisq, Binomial, Rayleigh, Weibull, NegativeBinomial) the call throws an opaque MethodError deep inside the solver.

Change

Add an explicit, informative guard in the extension's compute_gradient! before the base-measure correction, and document the limitation.

Patch

File: ext/ClosedFormExpectationsExt/ClosedFormExpectationsExt.jl (in ExponentialFamilyProjection.compute_gradient!)

     # Compute ∇_η E[log p̃ * (T - μ)]
     grad_target = mean(ClosedWilliamsProduct(strategy.backend), target_fn, q_dist)
+    base_measure_type = ExponentialFamily.isbasemeasureconstant(q_dist)
+    if base_measure_type === ExponentialFamily.NonConstantBaseMeasure()
+        error(
+            "`ClosedFormStrategy` supports only variational families with a constant base measure, " *
+            "but got `$(base_measure_type)` for the variational family. The analytic " *
+            "-E_q[log h_q (T - μ)] base-measure correction is not implemented. " *
+            "Use `ControlVariateStrategy` instead.",
+        )
+    end
     grad_eta = logbasemeasure_correction(
         strategy,
-        ExponentialFamily.isbasemeasureconstant(q_dist),
+        base_measure_type,
         q_dist,
         grad_target,
     )

File: src/strategies/closed_form.jl — add to the docstring:

-This strategy provides a "Zero-Variance" gradient estimator, avoiding the noise associated
-with Monte Carlo sampling (like in `ControlVariateStrategy`).
+This strategy provides a "Zero-Variance" gradient estimator, avoiding the noise associated
+with Monte Carlo sampling (like in `ControlVariateStrategy`).
+
+!!! note
+    Currently only target–variational pairs where the variational distribution has a **constant
+    base measure** are supported (e.g. Normal, LogNormal, Beta, Gamma). For variational families
+    with a non-constant base measure (Poisson, Chisq, Binomial, Rayleigh, Weibull,
+    NegativeBinomial) the strategy errors; use `ControlVariateStrategy` for those.

Verification

  • Added regression test: ClosedFormStrategy on a non-constant-base-measure target errors with the new message (not a MethodError).
  • Full suite still green.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant