Variational Inference using Pathfinder#

Stan supports the Pathfinder algorithm (Zhang, 2022). Pathfinder is a variational method for approximately sampling from differentiable log densities. 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 negative inverse Hessian estimates produced by the LBFGS optimizer. Pathfinder returns draws from the Gaussian approximation with the lowest estimated Kullback-Leibler (KL) divergence to the true posterior.

There are two Stan implementations of the Pathfinder algorithm: single-path Pathfinder and multi-path Pathfinder. Single-path Pathfinder generates a set of approximate draws from one run of the basic Pathfinder algorithm. Multi-path Pathfinder uses importance resampling over the draws from multiple runs of Pathfinder. This better matches non-normal target densities and also mitigates the problem of L-BFGS getting stuck at local optima or in saddle points on plateaus.

Example: variational inference with Pathfinder for model bernoulli.stan#

The CmdStanModel pathfinder method wraps the CmdStan pathfinder method.

By default, CmdStanPy runs multi-path Pathfinder which returns an importance-resampled set of draws over the outputs of 4 independent single-path Pathfinders.

[1]:
import os
from cmdstanpy import CmdStanModel, cmdstan_path
[2]:
bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')
stan_file = os.path.join(bernoulli_dir, 'bernoulli.stan')
data_file = os.path.join(bernoulli_dir, 'bernoulli.data.json')
# instantiate, compile bernoulli model
model = CmdStanModel(stan_file=stan_file)
# run CmdStan's pathfinder method, returns object `CmdStanPathfinder`
pathfinder = model.pathfinder(data=data_file)
[3]:
print(pathfinder)
print(pathfinder.metadata)
CmdStanPathfinder: model=bernoulli_model
 csv_file:
        /tmp/tmpcnjv8iba/bernoulli_74ez8mp/bernoulli-20261117171232.csv
 config_file:
        /tmp/tmpcnjv8iba/bernoulli_74ez8mp/bernoulli-20261117171232_config.json
 output_file:
        /tmp/tmpcnjv8iba/bernoulli_74ez8mp/bernoulli-20261117171232_stdout.txt
Metadata:
 column_names=('lp_approx__', 'lp__', 'path__', 'theta')

The pathfinder method returns a CmdStanPathfinder object, which provides access to the disparate information from the Stan CSV files.

  • The stan_variable and stan_variables methods return a Python numpy.ndarray containing all draws from the sample where the structure of each draw corresponds to the structure of the Stan variable.

  • The draws method returns the sample as a numpy.ndarray.

[4]:
pathfinder.stan_variable("theta").shape
[4]:
(1000,)
[5]:
pathfinder.column_names
[5]:
('lp_approx__', 'lp__', 'path__', 'theta')
[6]:
pathfinder.draws().shape
[6]:
(1000, 4)

Pathfinders as initialization for the MCMC sampler#

The method create_inits returns a Python Dict containing a set of per-chain initializations for the model parameters. Each set of initializations is a random draw from the Pathfinder sample. These initializations can be used as the initial parameter values for Stan’s NUTS-HMC sampler, which will reduce the number of warmup iterations needed.

[7]:
inits = pathfinder.create_inits()
print(inits)
[{'theta': array(0.18700108)}, {'theta': array(0.22947602)}, {'theta': array(0.22947602)}, {'theta': array(0.32703029)}]

The create_inits takes two arguments:

  • seed - used for random selection.

  • chains - the number of draws to return, default is 4. This should match the number of sampler chains to run.

[8]:
inits = pathfinder.create_inits(chains=3)
print(inits)
[{'theta': array(0.57669779)}, {'theta': array(0.16307887)}, {'theta': array(0.38512363)}]