Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF004 · Algorithm
Black-box variational inference and pathwise gradients
Optimizes a variational objective using Monte Carlo gradient estimates instead of deriving every analytic expectation.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.
- Bayesian regression model (Stan) Experimental
- Variational autoencoder (latent map, anomalies and generated rows) Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Runs in CodeSplash (Experimental)
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through discover.vae. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Optimizes a variational objective using Monte Carlo gradient estimates instead of deriving every analytic expectation. Score-function BBVI and reparameterized pathwise gradients are grouped here as related estimator choices, not counted as two new model architectures.
Question / hypotheses
How can we estimate the derivative of an expectation with respect to the parameters of the distribution generating its samples?
Architecture / computational structure
Sample from q, evaluate the log joint and variational density, and differentiate an ELBO estimator. A score estimator multiplies a log-weight signal by the score of q; a pathwise estimator differentiates through the sampled transformation. Baselines must preserve the intended gradient expectation.
Assumptions
Score-function gradients require differentiable log q and justified differentiation under the integral. Pathwise gradients require a suitable transformation z=g_lambda(epsilon), differentiable downstream computation, and appropriate treatment of support boundaries. Discrete latents generally need another estimator, enumeration, or an explicitly biased relaxation.
Formula / computational definition
Parameters and notation
Learned lambda controls variational locations, scales, flows, or an amortized encoder. Model parameters may be learned jointly but are distinct. Hyperparameters include Monte Carlo samples, optimizer, learning rate, baseline/control variate, family flexibility, and minibatch scale. Positive scales should use a stable constrained parameterization.
Fitting, fine-tuning, and adaptation
Warm-start variational parameters or a neural guide, then optimize the updated ELBO. If a generative network is frozen, update only inference parameters; if jointly adapting it, use distinct parameter groups and check prior/likelihood scaling. A smaller encoder step can preserve a useful amortized initialization, but validate the approximation after adaptation.
How to use and calibrate
Compare gradient variance and accuracy against an analytic small model, use independent random seeds, and monitor posterior predictive checks. Plot ELBO estimates with uncertainty. Validate intervals against simulation-based calibration or trusted reference inference rather than treating optimized loss as a coverage guarantee.
Example application
Infer latent continuous variables in a nonlinear generative model whose likelihood can be differentiated but whose posterior expectations have no closed form.
Interpretation / pitfalls
Watch out
Unbiased gradients do not make the optimized variational family unbiased. Finite-sample baselines estimated from the same samples need care. Reparameterization is not possible for every distribution, and a low-variance biased relaxation must be disclosed.
Python and R code examples
Scope: Score-function and pathwise mean-gradient calculations for q=N(mu,1) against a standard-normal target, using supplied innovations; no autodiff training engine.
Implementation route: Native NumPy/base R calculations show both estimators. For full differentiable models, Python Pyro supports ELBO estimators; an R implementation needs analytic/custom gradients, an R autodiff stack, or an explicitly configured Python bridge.
Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Score-function and pathwise mean-gradient calculations for q=N(mu,1) against a standard-normal target, using supplied innovations; no autodiff training engine. · needs numpy
import numpy as np
mu=.5; eps=np.array([-1.5,-.5,0.,.5,1.5]); z=mu+eps
log_ratio=-.5*z*z+.5*eps*eps # common normal constants cancel
score_est=np.mean(eps*log_ratio)
pathwise_est=np.mean(-z)
print('score estimate',score_est,'pathwise estimate',pathwise_est,'analytic',-mu)
Component demo, not a trained model — Score-function and pathwise mean-gradient calculations for q=N(mu,1) against a standard-normal target, using supplied innovations; no autodiff training engine.
mu <- .5; eps <- c(-1.5,-.5,0,.5,1.5); z <- mu+eps
log.ratio <- -.5*z*z+.5*eps*eps
score.est <- mean(eps*log.ratio); pathwise.est <- mean(-z)
print(c(score=score.est,pathwise=pathwise.est,analytic=-mu))
Sources
- Primary/official source 1proceedings.mlr.press
- Primary/official source 2proceedings.mlr.press
- Primary/official source 3docs.pyro.ai
From the CodeSplash reference, entry INF004.