Loading...
Searching...
No Matches
PsisLaplace.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// Pareto-Smoothed Importance-Sampled Laplace marginalisation over the GP
6// hyperparameter posterior. Implements the workflow of Vehtari, Gelman,
7// Gabry, Yao, Paananen 2024 (PSIS, arXiv:1507.02646 v9, JMLR 25)
8// composed with the standard Laplace approximation (Carlin & Louis 2008
9// Section 6) as the importance proposal:
10//
11// q(theta) = N(theta_MAP, H^{-1}), H = - d^2 log p(theta | y) / dtheta^2
12// |_{theta_MAP}
13//
14// Drop-in replacement for the doubling slice sampler in SliceSampler.h
15// that targets the same posterior. Trades the slice's M acccepted
16// samples (built from M * burst Cholesky factorisations, burst ~ 5) for K
17// importance-weighted samples (K Cholesky factorisations) plus the
18// Pareto k-hat reliability diagnostic.
19//
20// Cost decomposition at PET-MAD np = 16, n_obs ~ 72, p = 4:
21// slice: 16 samples * ~5 NLML/sample = ~80 Cholesky per fit
22// PSIS: 16 samples * 1 NLML/sample = 16 Cholesky per fit
23// 5x reduction in cache-build cost when k_hat <= 0.7. When k_hat > 0.7
24// the proposal misses the posterior; the caller is expected to fall back
25// to the slice path or the MAP point estimate.
26//
27// SymPy proof of the weighted law-of-total-variance identity used by the
28// downstream variance estimator: python/tests/test_sympy_psis_laplace.py
29// (pinned identities P1-P6).
30//
31// MPI: sample generation runs identically on every rank (same seed, same
32// theta_MAP, same H produces the same samples). The caller distributes
33// per-sample NLML evaluation across ranks via the same round-robin
34// pattern as the slice sampler.
35
36#ifndef GPR_ML_LAPLACE_PSIS_LAPLACE_H_
37#define GPR_ML_LAPLACE_PSIS_LAPLACE_H_
38
39#include <Eigen/Core>
40#include <cstdint>
41#include <functional>
42#include <random>
43#include <vector>
44
45namespace gpr {
46namespace laplace {
47
63 int n_samples = 16;
68 double k_hat_threshold = 0.7;
74 double hessian_jitter_max = 1.0e-3;
77 uint64_t seed = 42;
88};
89
97 Eigen::VectorXd theta_map;
98 Eigen::MatrixXd proposal_cov_chol; // L such that L L^T = H^{-1}
101 std::vector<Eigen::VectorXd> theta_samples;
104 std::vector<Eigen::VectorXd> alpha_samples;
109 std::vector<double> weights;
112 std::vector<double> log_weights_raw;
116 double k_hat = 0.0;
119 bool reliable = false;
124 bool valid = false;
125};
126
131public:
135 using LogTarget = std::function<double(const Eigen::VectorXd&)>;
136
139 using AlphaCacheFn = std::function<Eigen::VectorXd(const Eigen::VectorXd&)>;
140
141 explicit PsisLaplace(PsisLaplaceOptions opts = {});
142
149 bool fit(const Eigen::VectorXd& theta_map, const Eigen::MatrixXd& hessian,
150 const LogTarget& log_target, const AlphaCacheFn& alpha_fn,
152
153private:
154 PsisLaplaceOptions opts_;
155 std::mt19937_64 rng_;
156};
157
158// ---------------------------------------------------------------------------
159// Free helpers (exposed for unit tests).
160// ---------------------------------------------------------------------------
161
165std::vector<double> selfNormalisedWeights(const std::vector<double>& log_w);
166
175double estimateParetoKHat(const std::vector<double>& weights_descending,
176 int M_pareto);
177
184double logGaussianDensity(const Eigen::VectorXd& delta,
185 const Eigen::MatrixXd& cov_chol);
186
187} // namespace laplace
188} // namespace gpr
189
190#endif // GPR_ML_LAPLACE_PSIS_LAPLACE_H_
std::function< double(const Eigen::VectorXd &)> LogTarget
PsisLaplace(PsisLaplaceOptions opts={})
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> AlphaCacheFn
bool fit(const Eigen::VectorXd &theta_map, const Eigen::MatrixXd &hessian, const LogTarget &log_target, const AlphaCacheFn &alpha_fn, PsisLaplaceState &out)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double estimateParetoKHat(const std::vector< double > &weights_descending, int M_pareto)
double logGaussianDensity(const Eigen::VectorXd &delta, const Eigen::MatrixXd &cov_chol)
std::vector< double > selfNormalisedWeights(const std::vector< double > &log_w)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
std::vector< double > weights
std::vector< double > log_weights_raw
Eigen::MatrixXd proposal_cov_chol
Definition PsisLaplace.h:98
std::vector< Eigen::VectorXd > theta_samples
bool valid
True iff the cache contains usable n_samples samples.
std::vector< Eigen::VectorXd > alpha_samples