Loading...
Searching...
No Matches
ObservationLikelihood.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
6
7#include <cmath>
8#include <stdexcept>
9
10namespace gpr {
11
12Eigen::VectorXd findFMode(const EigenMatrix& K, const Eigen::VectorXd& y,
13 const ObservationLikelihood& ol, int max_iter,
14 double tol)
15{
16 const Eigen::Index n = K.rows();
17 if (K.cols() != n || y.size() != n) {
18 throw std::invalid_argument(
19 "findFMode: dimension mismatch between K and y");
20 }
21 Eigen::VectorXd f = Eigen::VectorXd::Zero(n); // R&W standard init
22
23 for (int iter = 0; iter < max_iter; ++iter) {
24 Eigen::VectorXd grad = ol.gradLogProb(y, f);
25 Eigen::VectorXd W_diag = ol.negHessDiag(y, f);
26 // Log-concavity guarantees W >= 0; clip negative rounding noise.
27 for (Eigen::Index i = 0; i < n; ++i) {
28 if (W_diag(i) < 0.0) W_diag(i) = 0.0;
29 }
30 const Eigen::VectorXd W_sqrt = W_diag.cwiseSqrt();
31
32 // B = I + W^{1/2} K W^{1/2}. Cholesky for numerical stability.
33 EigenMatrix B = EigenMatrix::Identity(n, n);
34 B += W_sqrt.asDiagonal() * K * W_sqrt.asDiagonal();
35 Eigen::LLT<EigenMatrix> llt(B);
36 if (llt.info() != Eigen::Success) {
37 throw std::runtime_error("findFMode: B Cholesky failed");
38 }
39
40 // R&W Algo 3.1: b = W f + grad; a = b - W^{1/2} B^{-1} W^{1/2} K b;
41 // f_new = K a.
42 const Eigen::VectorXd b_vec = W_diag.asDiagonal() * f + grad;
43 const Eigen::VectorXd Kb = K * b_vec;
44 const Eigen::VectorXd Binv_WKb = llt.solve(W_sqrt.asDiagonal() * Kb);
45 const Eigen::VectorXd a = b_vec - W_sqrt.asDiagonal() * Binv_WKb;
46 const Eigen::VectorXd f_new = K * a;
47
48 const double delta = (f_new - f).norm();
49 f = f_new;
50 if (delta < tol * std::max(1.0, f.norm())) return f;
51 }
52 throw std::runtime_error(
53 "findFMode: Newton did not converge within max_iter");
54}
55
56double laplaceMarginalLogLik(const EigenMatrix& K, const Eigen::VectorXd& y,
57 const Eigen::VectorXd& f_mode,
58 const ObservationLikelihood& ol)
59{
60 const Eigen::Index n = K.rows();
61 Eigen::VectorXd W_diag = ol.negHessDiag(y, f_mode);
62 for (Eigen::Index i = 0; i < n; ++i) {
63 if (W_diag(i) < 0.0) W_diag(i) = 0.0;
64 }
65 const Eigen::VectorXd W_sqrt = W_diag.cwiseSqrt();
66
67 EigenMatrix B = EigenMatrix::Identity(n, n);
68 B += W_sqrt.asDiagonal() * K * W_sqrt.asDiagonal();
69 Eigen::LLT<EigenMatrix> llt_B(B);
70 if (llt_B.info() != Eigen::Success) {
71 throw std::runtime_error("laplaceMarginalLogLik: B Cholesky failed");
72 }
73 // log|B| = 2 * sum log(diag(L_B)).
74 const EigenMatrix L_B = llt_B.matrixL();
75 double log_det_B = 0.0;
76 for (Eigen::Index i = 0; i < n; ++i)
77 log_det_B += std::log(L_B(i, i));
78 log_det_B *= 2.0;
79
80 // log N(f_mode | 0, K) = -(1/2) f^T K^{-1} f - (1/2) log|K|
81 // - (n/2) log(2 pi).
82 Eigen::LLT<EigenMatrix> llt_K(K);
83 if (llt_K.info() != Eigen::Success) {
84 throw std::runtime_error(
85 "laplaceMarginalLogLik: K Cholesky failed (check K is PSD)");
86 }
87 const EigenMatrix L_K = llt_K.matrixL();
88 double log_det_K = 0.0;
89 for (Eigen::Index i = 0; i < n; ++i)
90 log_det_K += std::log(L_K(i, i));
91 log_det_K *= 2.0;
92
93 const Eigen::VectorXd Kinv_f = llt_K.solve(f_mode);
94 constexpr double kLog2Pi = 1.8378770664093453; // log(2 pi)
95 const double log_p_f_prior = -0.5 * f_mode.dot(Kinv_f) - 0.5 * log_det_K -
96 0.5 * static_cast<double>(n) * kLog2Pi;
97
98 const double log_p_y_f = ol.logProb(y, f_mode);
99 return log_p_y_f + log_p_f_prior - 0.5 * log_det_B;
100}
101
102} // namespace gpr
@ y
Definition Enums.h:28
Non-Gaussian observation-likelihood scaffolding for INLA inner Laplace.
Contract for non-Gaussian observation models used by the INLA inner Laplace step (Rasmussen-Williams ...
virtual Eigen::VectorXd negHessDiag(const Eigen::VectorXd &y, const Eigen::VectorXd &f) const =0
virtual Eigen::VectorXd gradLogProb(const Eigen::VectorXd &y, const Eigen::VectorXd &f) const =0
d/df log p(y|f); same size as f.
virtual double logProb(const Eigen::VectorXd &y, const Eigen::VectorXd &f) const =0
log p(y | f) summed over observations.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
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
Definition Macros.h:20