25double ruesDefaultF0(
int p)
27 return std::sqrt(
static_cast<double>(p) + 2.0);
34double ruesDelta(
int p,
double f0,
int n_corners)
36 return 1.0 / (f0 * f0 * (2.0 +
static_cast<double>(n_corners) / p));
43 theta_star_ = theta_star;
53 const Eigen::MatrixXd& parameter_directions)
55 if (H.rows() <= 0 || H.rows() != H.cols()) {
56 throw std::invalid_argument(
"LaplaceINLA::setHessian: H not square");
58 const bool constrained = parameter_directions.size() > 0;
59 const Eigen::Index ambient_dimension =
60 constrained ? parameter_directions.rows() : H.rows();
61 if (theta_star_.size() != 0 && ambient_dimension != theta_star_.size()) {
62 throw std::invalid_argument(
63 "LaplaceINLA::setHessian: H dimension does not match theta*");
65 Eigen::MatrixXd dual_directions;
67 if (parameter_directions.cols() != H.rows() ||
68 parameter_directions.rows() < parameter_directions.cols() ||
69 !parameter_directions.allFinite()) {
70 throw std::invalid_argument(
71 "LaplaceINLA::setHessian: invalid parameter directions");
73 const Eigen::MatrixXd gram =
74 parameter_directions.transpose() * parameter_directions;
75 Eigen::LLT<Eigen::MatrixXd> factor(gram);
76 if (factor.info() != Eigen::Success) {
77 throw std::invalid_argument(
78 "LaplaceINLA::setHessian: dependent parameter directions");
81 factor.solve(parameter_directions.transpose()).transpose();
84 throw std::runtime_error(
"LaplaceINLA::setHessian: H is not finite");
92 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> solver(H_);
93 if (solver.info() != Eigen::Success) {
94 throw std::runtime_error(
"LaplaceINLA::setHessian: eigensolver failed");
96 lambda_ = solver.eigenvalues();
97 U_ = solver.eigenvectors();
99 const double lambda_max = lambda_.maxCoeff();
100 if (!lambda_.allFinite() || !(lambda_max > 0.0)) {
101 throw std::runtime_error(
102 "LaplaceINLA::setHessian: all eigenvalues of H are non-positive");
105 const double spectral_floor =
106 std::max(std::numeric_limits<double>::epsilon() * lambda_max,
107 std::numeric_limits<double>::min());
108 for (Eigen::Index i = 0; i < lambda_.size(); ++i) {
109 if (!(lambda_(i) > spectral_floor)) {
110 lambda_(i) = spectral_floor;
137 constexpr double kPrior = 1e-2;
138 const double prior_precision = kPrior * lambda_max;
139 const double lambda_min = lambda_.minCoeff();
140 const bool needs_regularization = (lambda_min < prior_precision);
141 if (needs_regularization) {
142 for (Eigen::Index i = 0; i < lambda_.size(); ++i) {
143 lambda_(i) += prior_precision;
152 for (Eigen::Index i = 0; i < lambda_.size(); ++i) {
153 L_scale_.col(i) *= 1.0 / std::sqrt(lambda_(i));
156 L_scale_ = (parameter_directions * L_scale_).eval();
159 U_ = (dual_directions * U_).eval();
167 if (theta_star_.size() == 0) {
168 throw std::runtime_error(
"LaplaceINLA::buildGrid: setTheta0 first");
170 if (H_.size() == 0) {
171 throw std::runtime_error(
"LaplaceINLA::buildGrid: setHessian first");
173 const int p =
static_cast<int>(L_scale_.cols());
174 if (f0 <= 0.0) f0 = ruesDefaultF0(
p);
181 z_k_.emplace_back(Eigen::VectorXd::Zero(
p));
184 for (
int i = 0; i <
p; ++i) {
185 Eigen::VectorXd z_plus = Eigen::VectorXd::Zero(
p);
186 Eigen::VectorXd z_minus = Eigen::VectorXd::Zero(
p);
189 z_k_.emplace_back(std::move(z_plus));
190 z_k_.emplace_back(std::move(z_minus));
200 if (include_corners &&
p >= 2) {
201 const int n_corner = 1 <<
p;
202 const double corner_axis = f0 / std::sqrt(
static_cast<double>(
p));
203 for (
int bits = 0; bits < n_corner; ++bits) {
204 Eigen::VectorXd
z(
p);
205 for (
int i = 0; i <
p; ++i) {
206 const int sign = ((bits >> i) & 1) ? +1 : -1;
207 z(i) = sign * corner_axis;
209 z_k_.emplace_back(std::move(
z));
214 theta_k_.reserve(z_k_.size());
215 for (
const auto&
z: z_k_) {
216 theta_k_.emplace_back(theta_star_ + L_scale_ *
z);
220 const int n =
static_cast<int>(z_k_.size());
221 const int n_corners = n - 1 - 2 *
p;
222 const double delta = ruesDelta(
p, f0, n_corners);
223 w_grid_ = Eigen::VectorXd::Constant(n, delta);
224 w_grid_(0) = 1.0 -
static_cast<double>(n - 1) * delta;
229double LaplaceINLA::evaluateNLMLAtTheta(
const Eigen::VectorXd&
y,
231 const Eigen::VectorXd& theta)
const
238 if (theta.size() < 1) {
239 throw std::runtime_error(
240 "LaplaceINLA::evaluateNLMLAtTheta: theta must have at least one "
241 "component (log-noise)");
243 auto [K_XX, K_cross, k_diag] = kb(theta);
252 throw std::runtime_error(
253 "LaplaceINLA: NON_GAUSSIAN requested but no "
254 "ObservationLikelihood attached (setObservationLikelihood)");
256 const Eigen::VectorXd f_mode =
findFMode(K_XX,
y, *ol_);
260 Eigen::SelfAdjointEigenSolver<EigenMatrix> solver(K_XX);
261 const Eigen::VectorXd D = solver.eigenvalues();
264 FactorizedLikelihood fl;
265 fl.setEigendecomposition(D, U,
y);
267 const double noise = std::exp(theta(theta.size() - 1));
272 return -fl.evaluateTP(noise, tp_a_, tp_b_);
274 return -fl.evaluateGP(noise);
280 if (theta_k_.empty()) {
281 throw std::runtime_error(
"LaplaceINLA::fit: buildGrid first");
283 const int n =
static_cast<int>(theta_k_.size());
284 const double phi_star =
285 evaluateNLMLAtTheta(
y, kb, theta_star_) - log_prior(theta_star_);
289 std::vector<double> delta_phi(n);
291#pragma omp parallel for schedule(dynamic)
293 for (
int k = 0; k < n; ++k) {
294 const Eigen::VectorXd& theta_k = theta_k_[k];
295 const Eigen::VectorXd d = theta_k - theta_star_;
298 evaluateNLMLAtTheta(
y, kb, theta_k) - log_prior(theta_k);
299 delta_phi[k] = phi_k - phi_star - quad;
304 const double dmin = *std::min_element(delta_phi.begin(), delta_phi.end());
305 Eigen::VectorXd rho(n);
306 for (
int k = 0; k < n; ++k) {
307 rho(k) = std::exp(-(delta_phi[k] - dmin));
313 for (
int k = 0; k < n; ++k) {
314 w_final_(k) = w_grid_(k) * rho(k);
318 throw std::runtime_error(
319 "LaplaceINLA::fit: all grid weights collapsed to 0");
329 return predict(
y, std::move(kb), workspace);
336 throw std::runtime_error(
"LaplaceINLA::predict: fit first");
340 out.n_grid_points =
static_cast<int>(theta_k_.size());
342 Eigen::VectorXd weighted_mean;
343 Eigen::VectorXd weighted_sq;
347 double retained_weight = 0.0;
349 for (
size_t k = 0; k < theta_k_.size(); ++k) {
350 if (w_final_(k) < 1e-15)
continue;
351 retained_weight += w_final_(k);
352 const Eigen::VectorXd& theta_k = theta_k_[k];
353 auto [K_XX, K_cross, k_diag] = kb(theta_k);
355 const double noise = std::exp(theta_k(theta_k.size() - 1));
357 K_XX.diagonal().array() += noise;
360 throw std::runtime_error(
361 "LaplaceINLA::predict: covariance is not positive definite");
363 Eigen::VectorXd alpha =
365 Eigen::VectorXd mu = K_cross * alpha;
368 Eigen::VectorXd var =
370 (K_cross.array() * V.transpose().array()).rowwise().sum().matrix();
371 var = var.cwiseMax(0.0);
374 weighted_mean = w_final_(k) * mu;
375 weighted_sq = w_final_(k) * (var + mu.array().square().matrix());
378 weighted_mean += w_final_(k) * mu;
379 weighted_sq += w_final_(k) * (var + mu.array().square().matrix());
383 if (first || !(retained_weight > 0.0)) {
387 throw std::runtime_error(
388 "LaplaceINLA::predict: every mixture component has negligible "
392 weighted_mean /= retained_weight;
393 weighted_sq /= retained_weight;
395 out.mean = weighted_mean;
397 out.variance = weighted_sq - weighted_mean.array().square().matrix();
398 out.variance =
out.variance.cwiseMax(0.0);
Eigendecomposition-based log marginal likelihood evaluation.
INLA-style Laplace approximation of the GP/TP hyperparameter posterior.
Non-Gaussian observation-likelihood scaffolding for INLA inner Laplace.
@ NON_GAUSSIAN
Inner Laplace via ObservationLikelihood (future).
@ T_CLOSED_FORM
FactorizedLikelihood::evaluateTP (MVT marginal).
INLAPrediction predict(const Eigen::VectorXd &y, KernelBuilder kernel_builder) const
Predictive moments at the test points embedded in the kernel builder's K_test_X / k_test_test outputs...
void fit(const Eigen::VectorXd &y, KernelBuilder kernel_builder, LogPrior log_prior)
Evaluate the per-grid-point correction rho_k = exp(-Delta phi_k) and build the final normalised weigh...
void setHessian(const Eigen::MatrixXd &H)
Eigen::MatrixXd precisionMatrix() const
std::function< double(const Eigen::VectorXd &theta)> LogPrior
void setTheta0(const Eigen::VectorXd &theta_star)
MAP hyperparameters (log space if using the project's conventions).
std::function< std::tuple< EigenMatrix, EigenMatrix, Eigen::VectorXd >( const Eigen::VectorXd &theta)> KernelBuilder
void buildGrid(double f0=-1.0, bool include_corners=true)
Build the CCD grid in theta-space from (theta*, H).
Eigen::VectorXd forward_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
EigenMatrix back_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
EigenMatrix forward_solve_mat(const EigenMatrix &L, const EigenMatrix &B)
bool cholesky(const EigenMatrix &C, EigenMatrix &L)
Eigen::VectorXd back_solve(const EigenMatrix &L, const Eigen::VectorXd &b)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Eigen::VectorXd findFMode(const EigenMatrix &K, const Eigen::VectorXd &y, const ObservationLikelihood &ol, int max_iter, double tol)
Posterior mode f_mode = argmax_f [log p(y|f) + log N(f | 0, K)] via the Rasmussen-Williams safe Newto...
double laplaceMarginalLogLik(const EigenMatrix &K, const Eigen::VectorXd &y, const Eigen::VectorXd &f_mode, const ObservationLikelihood &ol)
Laplace approximation to log p(y | theta) given prior K and the converged posterior mode f_mode (R&W ...
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix