Loading...
Searching...
No Matches
SteinVariational.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4//
5// Stein Variational Gradient Descent (SVGD) for GP hyperparameter
6// posterior approximation.
7//
8// Reference: Liu & Wang 2016 ``Stein Variational Gradient Descent:
9// A General Purpose Bayesian Inference Algorithm'' (NeurIPS 2016).
10//
11// Replaces the deterministic CCD grid in LaplaceINLA with K particles
12// {theta_k} that iteratively evolve toward the marginal-likelihood
13// posterior via the SVGD update rule:
14//
15// theta_k^{t+1} = theta_k^t + eps * phi*(theta_k^t)
16//
17// where
18//
19// phi*(theta) = (1/K) sum_j [k(theta_j, theta) * grad_log_p(theta_j)
20// + grad_{theta_j} k(theta_j, theta)]
21//
22// First term: attraction toward high-posterior-density regions.
23// Second term: repulsion to maintain particle diversity (the
24// "Stein discrepancy" that gives SVGD its name).
25//
26// For the GP hyperparameter posterior:
27// log p(theta | D) = -NLML(theta) + log p(theta) (+ const)
28// grad_log_p(theta_j) = -grad_NLML(theta_j) + grad_log_prior(theta_j)
29//
30// We use the RBF kernel k(theta, theta') = exp(-||theta - theta'||^2 / (2*h^2))
31// with h chosen via median heuristic (Liu & Wang 2016, Eq. 8):
32// h = median(||theta_i - theta_j||) / sqrt(log K)
33//
34// After T iterations the particles approximate samples from p(theta|D).
35// Downstream LaplaceINLA-style mixing uses uniform weights (1/K each)
36// since the particle distribution itself encodes the posterior density.
37//
38// Why SVGD over CCD:
39// - CCD has fixed K=2d+1 deterministic points; passenger directions
40// get integrated symmetrically and contribute noise.
41// - SVGD adapts particle locations to the actual posterior shape;
42// particles concentrate in informative directions.
43// - For small-data regimes where the posterior is sharply peaked
44// in only some directions (CuH2 with passenger Cu-Cu lengthscale),
45// SVGD particles cluster near theta* in passenger dirs and spread
46// in informative dirs -- correctly reflecting the true posterior.
47//
48// Particle count K and iteration count T tradeoff:
49// - K=10-30 is typical (vs CCD's K=2d+1=11 for d=5)
50// - T=20-50 SVGD iterations suffice for smooth posteriors
51// - Each iteration costs K NLML gradient evaluations
52// - Total fit cost: K*T NLML evals (~ K*T factor over MAP)
53//
54// Non-copyable, value-semantic.
55
56#ifndef GPR_ML_LAPLACE_STEIN_VARIATIONAL_H_
57#define GPR_ML_LAPLACE_STEIN_VARIATIONAL_H_
58
59#include <Eigen/Core>
60#include <cstddef>
61#include <functional>
62#include <utility>
63#include <vector>
64
66
67namespace gpr {
68namespace laplace {
69
85 int n_iterations = 10;
86 double step_size = 0.05;
88 double conv_tol = 1e-3;
89 double init_f0 = 0.0;
90};
91
93public:
97 using GradOracle =
98 std::function<Eigen::VectorXd(const Eigen::VectorXd& theta)>;
99
100 SteinVariational() = default;
101
110 void initFromCCD(const Eigen::VectorXd& theta_star,
111 const Eigen::MatrixXd& L, double f0 = 0.0);
112
119 [[nodiscard]] int fit(GradOracle grad_oracle, int T = 30, double eps = 0.05,
120 double conv_tol = 1e-3);
121 [[nodiscard]] int fit(GradOracle grad_oracle, int T, double eps,
122 double conv_tol,
123 const linalg::MpiContext& process_group);
124
130 [[nodiscard]] int fit(GradOracle grad_oracle,
131 const SteinVariationalConfig& cfg)
132 {
133 return fit(std::move(grad_oracle), cfg.n_iterations, cfg.step_size,
134 cfg.conv_tol);
135 }
136
137 [[nodiscard]] int fit(GradOracle grad_oracle,
138 const SteinVariationalConfig& cfg,
139 const linalg::MpiContext& process_group)
140 {
141 return fit(std::move(grad_oracle), cfg.n_iterations, cfg.step_size,
142 cfg.conv_tol, process_group);
143 }
144
146 [[nodiscard]] const std::vector<Eigen::VectorXd>& particles() const noexcept
147 {
148 return particles_;
149 }
150 [[nodiscard]] std::size_t numParticles() const noexcept
151 {
152 return particles_.size();
153 }
154
158 [[nodiscard]] Eigen::VectorXd uniformWeights() const;
159
160 [[nodiscard]] bool fitted() const noexcept
161 {
162 return fitted_;
163 }
164
165 void invalidate() noexcept
166 {
167 fitted_ = false;
168 particles_.clear();
169 parameter_basis_.resize(0, 0);
170 }
171
172private:
176 [[nodiscard]] Eigen::MatrixXd rbfKernel(double* h_out) const;
177
178 bool fitted_{false};
179 std::vector<Eigen::VectorXd> particles_;
180 Eigen::MatrixXd parameter_basis_;
181};
182
183} // namespace laplace
184} // namespace gpr
185
186#endif // GPR_ML_LAPLACE_STEIN_VARIATIONAL_H_
Tiny value type describing the MPI dispatch context for a probe loop.
int fit(GradOracle grad_oracle, int T=30, double eps=0.05, double conv_tol=1e-3)
void initFromCCD(const Eigen::VectorXd &theta_star, const Eigen::MatrixXd &L, double f0=0.0)
const std::vector< Eigen::VectorXd > & particles() const noexcept
Particle access (post-fit).
Eigen::VectorXd uniformWeights() const
int fit(GradOracle grad_oracle, const SteinVariationalConfig &cfg)
int fit(GradOracle grad_oracle, const SteinVariationalConfig &cfg, const linalg::MpiContext &process_group)
std::size_t numParticles() const noexcept
std::function< Eigen::VectorXd(const Eigen::VectorXd &theta)> GradOracle
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
double conv_tol
early-stop on max particle move
double init_f0
CCD init radius; 0 = sqrt(p + 2).