Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Pathfinder

In this notebook we introduce the pathfinder Zhang et al., 2022 algorithm and we show how to use it as a variational inference method or as an initialization tool for MCMC kernels.

Notebook Cell

The Data

We create two clusters of points using scikit-learn’s make_bicluster function.

Source
<Figure size 600x600 with 1 Axes>

The Model

We use a simple logistic regression model to infer to which cluster each of the points belongs. We note yy a binary variable that indicates whether a point belongs to the first cluster:

y∼Bernoulli⁡(p)y \sim \operatorname{Bernoulli}(p)

The probability pp to belong to the first cluster commes from a logistic regression:

p=logistic⁡(Φ w)p = \operatorname{logistic}(\Phi\,\boldsymbol{w})

where ww is a vector of weights whose priors are a normal prior centered on 0:

w∼Normal⁡(0,σ)\boldsymbol{w} \sim \operatorname{Normal}(0, \sigma)

And Φ\Phi is the matrix that contains the data, so each row Φi,:\Phi_{i,:} is the vector [X0i,X1i]\left[X_0^i, X_1^i\right]

Pathfinder: Parallel Quasi-Newton Variational Inference

Starting from a random initialization, Pathfinder locates normal approximations to the target density along a quasi-Newton optimization path, with local covariance estimated using the inverse Hessian estimates produced by the optimizer. Pathfinder returns draws from the approximation with the lowest estimated Kullback-Leibler (KL) divergence to the true posterior. The optimizer is the limited memory BFGS algorithm.

To help understand the approximations that pathfinder evaluates during its run, here we plot for each step of the L-BFGS optimizer the approximation of the posterior distribution of the model derived by pathfinder and its ELBO:

Source
<Figure size 1500x2500 with 15 Axes>

Pathfinder as a Variational Inference Method

Pathfinder can be used as a variational inference method. We first create a pathfinder object pf which contains two functions init and sample:

We can now get samples from the approximation:

And display the trace:

Source
<Figure size 800x200 with 2 Axes>

Please note that pathfinder is implemented as follows:

Hence it makes sense to jit the init function and then use the sample helper function in the pathfinder object instead of implementing the inference loop:

CPU times: user 5.69 s, sys: 151 ms, total: 5.84 s
Wall time: 2.89 s

Quick comparison against the Rosenbluth-Metropolis-Hastings kernel rmh:

Source
<Figure size 1000x400 with 4 Axes>

Pathfinder as an Initialization Tool for MCMC Kernels

Pathfinder uses internally the inverse hessian estimation of the L-BFGS optimizer to evaluate the approximations to the target distribution along the quasi-Newton optimization path.

We can calculate explicitly this inverse hessian matrix for a step of the optimization path using the blackjax.optimizers.lbfgs.lbfgs_inverse_hessian_formula_1 function:

Array([[ 0.16326791, -0.0186634 ], [-0.01866339, 0.2359373 ]], dtype=float32)

This estimation of the inverse mass matrix, coupled with Nesterov’s dual averaging adaptation for estimating the step size, yields an alternative adaptation scheme for initializing MCMC kernels.

This scheme is implemented in blackjax.pathfinder_adaptation function:

Source
<Figure size 1000x400 with 2 Axes>

Some Caveats

References
  1. Zhang, L., Carpenter, B., Gelman, A., & Vehtari, A. (2022). Pathfinder: Parallel quasi-Newton variational inference.