Loading...
Searching...
No Matches
FactorizedLikelihood.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
9namespace gpr {
10
12 const Eigen::VectorXd& eigenvalues, const EigenMatrix& eigenvectors,
13 const Eigen::VectorXd& targets)
14{
15 D_ = eigenvalues;
16 n_ = eigenvalues.size();
17
18 // Project targets: UTY = U^T y
19 UTY_ = eigenvectors.transpose() * targets;
20 UTY2_ = UTY_.array().square();
21}
22
23double FactorizedLikelihood::computeS(double noise_variance) const
24{
25 // S = sum((U^T y)^2 / (D + noise))
26 double S = 0.0;
27 for (Eigen::Index i = 0; i < n_; ++i) {
28 S += UTY2_(i) / (D_(i) + noise_variance);
29 }
30 return S;
31}
32
33double FactorizedLikelihood::evaluateGP(double noise_variance) const
34{
35 // GP NLML with analytical prefactor:
36 // L = 0.5 * n * (1 + log(2*pi))
37 // + n * prefactor_log
38 // + 0.5 * sum(log(D_n))
39 //
40 // where prefactor_log = 0.5 * log(mean(UTY^2 / D_n))
41
42 double log_det = 0.0;
43 double S = 0.0;
44 for (Eigen::Index i = 0; i < n_; ++i) {
45 double D_n = D_(i) + noise_variance;
46 log_det += std::log(D_n);
47 S += UTY2_(i) / D_n;
48 }
49
50 double prefactor2 = S / n_; // mean(UTY^2 / D_n)
51 double prefactor_log = 0.5 * std::log(prefactor2);
52
53 return 0.5 * n_ * (1.0 + std::log(2.0 * M_PI)) + n_ * prefactor_log +
54 0.5 * log_det;
55}
56
57double FactorizedLikelihood::evaluateTP(double noise_variance, double a,
58 double b) const
59{
60 // TP NLML:
61 // L = 0.5 * sum(log(D_n)) + 0.5 * (2a + n) * log(1 + S/(2b))
62
63 double log_det = 0.0;
64 double S = 0.0;
65 for (Eigen::Index i = 0; i < n_; ++i) {
66 double D_n = D_(i) + noise_variance;
67 log_det += std::log(D_n);
68 S += UTY2_(i) / D_n;
69 }
70
71 return 0.5 * log_det + 0.5 * (2.0 * a + n_) * std::log(1.0 + S / (2.0 * b));
72}
73
74double FactorizedLikelihood::analyticalPrefactor(double noise_variance) const
75{
76 double S = computeS(noise_variance);
77 return S / n_; // mean(UTY^2 / D_n)
78}
79
80double FactorizedLikelihood::tpPrefactor(double noise_variance, double a,
81 double b) const
82{
83 double S = computeS(noise_variance);
84 double n_adjusted = (n_ > 2) ? static_cast<double>(n_ - 2) : 0.0;
85 return (2.0 * b + S) / (2.0 * a + n_adjusted);
86}
87
88} // namespace gpr
Eigendecomposition-based log marginal likelihood evaluation.
double evaluateGP(double noise_variance) const
GP negative log marginal likelihood at given noise level.
double evaluateTP(double noise_variance, double a, double b) const
TP negative log marginal likelihood at given noise level.
void setEigendecomposition(const Eigen::VectorXd &eigenvalues, const EigenMatrix &eigenvectors, const Eigen::VectorXd &targets)
Cache the eigendecomposition of K_XX for repeated noise sweeps.
double analyticalPrefactor(double noise_variance) const
Analytically optimal GP prefactor (signal variance).
double tpPrefactor(double noise_variance, double a, double b) const
TP prefactor from inverse-gamma hyperprior.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20