Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF018 · Algorithm

Stein variational gradient descent (SVGD)

Moves interacting particles along a kernel-defined variational direction combining attraction toward high posterior density with repulsion. It offers a particle representation without ordinary MCMC accept/reject steps;

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
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

Moves interacting particles along a kernel-defined variational direction combining attraction toward high posterior density with repulsion. It offers a particle representation without ordinary MCMC accept/reject steps; finite particles are not independent exact posterior draws.

Question / hypotheses

Which smooth transport direction most rapidly reduces a KL objective within a chosen kernel function space?

Architecture / computational structure

Evaluate target scores at all particles, build pairwise kernel interactions, compute the attractive and derivative-based repulsive terms, and update every particle from the same previous particle set. The kernel derivative is with respect to its first argument; a sign or axis error reverses repulsion.

Assumptions

The target must have an evaluable differentiable log density, and the kernel must satisfy the required smoothness/integrability conditions. A small finite particle set is an approximation; kernel choice and dimensional concentration can dominate behavior.

Formula / computational definition

θi+=θi+ϵϕ(θi),ϕ(θ)=1N∑j=1N[k(θj,θ)∇log⁡p(θj)+∇θjk(θj,θ)].\theta_i^+=\theta_i+\epsilon\phi(\theta_i),\quad \phi(\theta)=\frac1N\sum_{j=1}^N[k(\theta_j,\theta)\nabla\log p(\theta_j)+\nabla_{\theta_j}k(\theta_j,\theta)].

Parameters and notation

Particle locations are optimized inference variables. The target model’s parameters or random unknowns should be distinguished from kernel and optimizer choices. Hyperparameters include particle count, kernel bandwidth, step size, optimizer, and initialization. If particle coordinates are neural weights, parameterization and dimension strongly affect the kernel geometry.

Fitting, fine-tuning, and adaptation

Warm-start particles on a changed target but recompute all scores and interactions. For Bayesian neural adaptation, specify which weights vary and which are conditioned on. Step-size and bandwidth changes are optimization choices, not an MCMC adaptation theorem; reassess posterior approximation quality after any change.

How to use and calibrate

Plot particles against an analytic density in low dimensions and compare moments, predictive calibration, and discrepancy measures with reference inference. Test particle counts and bandwidths. Kernel Stein discrepancy can diagnose some failures under its own assumptions, but small discrepancy does not universally certify coverage.

Example application

Maintain a moderate particle approximation to uncertain parameters in a differentiable probabilistic model when parallel score evaluation is available and full MCMC is costly.

Interpretation / pitfalls

Watch out

Repulsion helps diversity but does not guarantee all modes are represented. One particle often behaves like gradient-based mode finding. Finite-particle covariance can badly underestimate uncertainty, and calling particles posterior samples without qualification hides the deterministic approximation.

Python and R code examples

Scope: One simultaneous SVGD update for a one-dimensional N(2,1) target with three particles and a fixed RBF bandwidth.

Implementation route: The pairwise update is native NumPy/base R. Full neural use requires autodiff target scores and careful kernel scaling; no generic optimizer is automatically SVGD merely because it operates on an ensemble.

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.

Python

Component demo, not a trained model — One simultaneous SVGD update for a one-dimensional N(2,1) target with three particles and a fixed RBF bandwidth. · needs numpy

import numpy as np
x=np.array([-1.,0.,1.]); h=1.; step=.1; score=2-x
d=x[:,None]-x[None,:]; K=np.exp(-d*d/(2*h*h))
grad_first=-d*K/(h*h)
phi=(K.T@score+grad_first.sum(0))/len(x)
updated=x+step*phi
print('transport',phi,'updated particles',updated)
assert updated.mean()>x.mean()
R

Component demo, not a trained model — One simultaneous SVGD update for a one-dimensional N(2,1) target with three particles and a fixed RBF bandwidth.

x <- c(-1,0,1); h <- 1; step <- .1; score <- 2-x
d <- outer(x,x,"-"); K <- exp(-d*d/(2*h*h)); grad.first <- -d*K/(h*h)
phi <- colMeans(sweep(K,1,score,"*")+grad.first)
updated <- x+step*phi; print(list(transport=phi,particles=updated))
stopifnot(mean(updated)>mean(x))

Sources

From the CodeSplash reference, entry INF018.