GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
45
namespace
gpr
{
46
namespace
laplace
{
47
52
struct
PsisLaplaceOptions
{
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;
87
bool
deterministic_axial_grid
=
false
;
88
};
89
93
struct
PsisLaplaceState
{
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
;
122
int
n_target_evals
= 0;
124
bool
valid
=
false
;
125
};
126
130
class
PsisLaplace
{
131
public
:
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,
151
PsisLaplaceState
&
out
);
152
153
private
:
154
PsisLaplaceOptions
opts_;
155
std::mt19937_64 rng_;
156
};
157
158
// ---------------------------------------------------------------------------
159
// Free helpers (exposed for unit tests).
160
// ---------------------------------------------------------------------------
161
165
std::vector<double>
selfNormalisedWeights
(
const
std::vector<double>& log_w);
166
175
double
estimateParetoKHat
(
const
std::vector<double>& weights_descending,
176
int
M_pareto);
177
184
double
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_
gpr::laplace::PsisLaplace::LogTarget
std::function< double(const Eigen::VectorXd &)> LogTarget
Definition
PsisLaplace.h:135
gpr::laplace::PsisLaplace::PsisLaplace
PsisLaplace(PsisLaplaceOptions opts={})
Definition
PsisLaplace.cpp:16
gpr::laplace::PsisLaplace::AlphaCacheFn
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> AlphaCacheFn
Definition
PsisLaplace.h:139
gpr::laplace::PsisLaplace::fit
bool fit(const Eigen::VectorXd &theta_map, const Eigen::MatrixXd &hessian, const LogTarget &log_target, const AlphaCacheFn &alpha_fn, PsisLaplaceState &out)
Definition
PsisLaplace.cpp:85
out
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Definition
eon_relax_engine.h:240
gpr::laplace
Definition
GaussianProcessRegression.h:86
gpr::laplace::estimateParetoKHat
double estimateParetoKHat(const std::vector< double > &weights_descending, int M_pareto)
Definition
PsisLaplace.cpp:58
gpr::laplace::logGaussianDensity
double logGaussianDensity(const Eigen::VectorXd &delta, const Eigen::MatrixXd &cov_chol)
Definition
PsisLaplace.cpp:21
gpr::laplace::selfNormalisedWeights
std::vector< double > selfNormalisedWeights(const std::vector< double > &log_w)
Definition
PsisLaplace.cpp:39
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::laplace::PsisLaplaceOptions
Definition
PsisLaplace.h:52
gpr::laplace::PsisLaplaceOptions::n_samples
int n_samples
Definition
PsisLaplace.h:63
gpr::laplace::PsisLaplaceOptions::seed
uint64_t seed
Definition
PsisLaplace.h:77
gpr::laplace::PsisLaplaceOptions::hessian_jitter_max
double hessian_jitter_max
Definition
PsisLaplace.h:74
gpr::laplace::PsisLaplaceOptions::deterministic_axial_grid
bool deterministic_axial_grid
Definition
PsisLaplace.h:87
gpr::laplace::PsisLaplaceOptions::k_hat_threshold
double k_hat_threshold
Definition
PsisLaplace.h:68
gpr::laplace::PsisLaplaceState
Definition
PsisLaplace.h:93
gpr::laplace::PsisLaplaceState::theta_map
Eigen::VectorXd theta_map
Definition
PsisLaplace.h:97
gpr::laplace::PsisLaplaceState::weights
std::vector< double > weights
Definition
PsisLaplace.h:109
gpr::laplace::PsisLaplaceState::log_weights_raw
std::vector< double > log_weights_raw
Definition
PsisLaplace.h:112
gpr::laplace::PsisLaplaceState::n_target_evals
int n_target_evals
Definition
PsisLaplace.h:122
gpr::laplace::PsisLaplaceState::proposal_cov_chol
Eigen::MatrixXd proposal_cov_chol
Definition
PsisLaplace.h:98
gpr::laplace::PsisLaplaceState::theta_samples
std::vector< Eigen::VectorXd > theta_samples
Definition
PsisLaplace.h:101
gpr::laplace::PsisLaplaceState::valid
bool valid
True iff the cache contains usable n_samples samples.
Definition
PsisLaplace.h:124
gpr::laplace::PsisLaplaceState::k_hat
double k_hat
Definition
PsisLaplace.h:116
gpr::laplace::PsisLaplaceState::alpha_samples
std::vector< Eigen::VectorXd > alpha_samples
Definition
PsisLaplace.h:104
gpr::laplace::PsisLaplaceState::reliable
bool reliable
Definition
PsisLaplace.h:119
gpr
ml
laplace
PsisLaplace.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf