Loading...
Searching...
No Matches
IterGP.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#include "gpr/ml/gp/IterGP.h"
5
6#include <Eigen/Eigenvalues>
7#include <algorithm>
8#include <cmath>
9#include <limits>
10#include <stdexcept>
11
12namespace gpr {
13namespace itergp {
14
15void IterGP::run(const Eigen::MatrixXd& K_hat, const Eigen::VectorXd& y,
17{
18 const Eigen::Index n = K_hat.rows();
19 if (K_hat.cols() != n || y.size() != n) {
20 throw std::invalid_argument("IterGP: shape mismatch");
21 }
22 const Eigen::Index iters =
23 std::max<Eigen::Index>(0, std::min<Eigen::Index>(iterations, n));
24 S_.resize(n, iters);
25 KS_.resize(n, iters);
26
27 Eigen::VectorXd v = Eigen::VectorXd::Zero(n);
28 Eigen::VectorXd residual = y;
29 Eigen::Index taken = 0;
30
31 for (Eigen::Index i = 0; i < iters; ++i) {
32 Eigen::VectorXd s;
33 if (policy == Policy::Unit) {
34 s = Eigen::VectorXd::Zero(n);
35 s(i) = 1.0;
36 } else {
37 // The residual is the direction in which the current iterate still
38 // fails to explain the data, which is what CG would explore next.
39 s = residual;
40 const double nrm = s.norm();
41 // A residual that has collapsed carries no new direction; stopping
42 // here keeps S full column rank, which the inner solve needs.
43 if (!(nrm > 0.0) || !std::isfinite(nrm)) break;
44 s /= nrm;
45 }
46 S_.col(taken) = s;
47 KS_.col(taken) = K_hat * s;
48 ++taken;
49
50 // Recompute the weights from the whole action set rather than updating
51 // them recursively: with an ill-conditioned K_hat the recursive form
52 // loses the conjugacy that would make the two equivalent.
53 const Eigen::MatrixXd Sblk = S_.leftCols(taken);
54 const Eigen::MatrixXd KSblk = KS_.leftCols(taken);
55 const Eigen::MatrixXd SKS = Sblk.transpose() * KSblk;
56 // (S^T K_hat S)^{-1} through a truncated pseudo-inverse rather than a
57 // plain factorization. On an ill-conditioned kernel the CG residuals
58 // stop being independent, SKS goes numerically singular, and inverting
59 // it yields a C_i that OVER-explains: the reported variance then falls
60 // below the exact posterior and even goes negative, losing the one
61 // guarantee the method exists to provide. Dropping the small
62 // directions instead shrinks C_i, and a smaller C_i can only raise the
63 // reported variance, so the bound survives the truncation.
64 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(SKS);
65 if (es.info() != Eigen::Success) {
66 --taken;
67 break;
68 }
69 const double lam_max = es.eigenvalues().cwiseAbs().maxCoeff();
70 const double floor =
71 std::sqrt(std::numeric_limits<double>::epsilon()) * lam_max;
72 Sinv_ = Eigen::MatrixXd::Zero(taken, taken);
73 for (Eigen::Index k = 0; k < taken; ++k) {
74 const double lam = es.eigenvalues()(k);
75 if (lam <= floor) continue;
76 Sinv_ += (es.eigenvectors().col(k) *
77 es.eigenvectors().col(k).transpose()) /
78 lam;
79 }
80 v = Sblk * (Sinv_ * (Sblk.transpose() * y));
81 residual = y - K_hat * v;
82 }
83
84 S_.conservativeResize(n, taken);
85 KS_.conservativeResize(n, taken);
86 if (taken == 0) {
87 Sinv_.resize(0, 0);
88 v_ = Eigen::VectorXd::Zero(n);
89 return;
90 }
91 v_ = v;
92}
93
94Eigen::MatrixXd IterGP::inverseApproximation() const
95{
96 if (S_.cols() == 0) {
97 return Eigen::MatrixXd::Zero(S_.rows(), S_.rows());
98 }
99 return S_ * Sinv_ * S_.transpose();
100}
101
102double IterGP::posteriorMean(const Eigen::VectorXd& kx) const
103{
104 if (kx.size() != v_.size()) {
105 throw std::invalid_argument("IterGP: cross-covariance size mismatch");
106 }
107 return kx.dot(v_);
108}
109
110double IterGP::combinedVariance(double kxx, const Eigen::VectorXd& kx) const
111{
112 if (S_.cols() == 0) return kxx;
113 if (kx.size() != S_.rows()) {
114 throw std::invalid_argument("IterGP: cross-covariance size mismatch");
115 }
116 const Eigen::VectorXd p = S_.transpose() * kx;
117 const double explained = p.dot(Sinv_ * p);
118 // C_i is PSD, so the explained part cannot be negative; clamping guards the
119 // rounding at convergence rather than hiding a modelling error.
120 return kxx - std::max(0.0, explained);
121}
122
123double IterGP::computationalVariance(const Eigen::LDLT<Eigen::MatrixXd>& exact,
124 const Eigen::VectorXd& kx) const
125{
126 const double exact_explained = kx.dot(exact.solve(kx));
127 double approx_explained = 0.0;
128 if (S_.cols() > 0) {
129 const Eigen::VectorXd p = S_.transpose() * kx;
130 approx_explained = p.dot(Sinv_ * p);
131 }
132 return std::max(0.0, exact_explained - approx_explained);
133}
134
135double IterGP::relativeResidual(const Eigen::MatrixXd& K_hat,
136 const Eigen::VectorXd& y) const
137{
138 const double denom = y.norm();
139 if (!(denom > 0.0)) return 0.0;
140 return (K_hat * v_ - y).norm() / denom;
141}
142
143} // namespace itergp
144} // namespace gpr
@ y
Definition Enums.h:28
double posteriorMean(const Eigen::VectorXd &kx) const
Posterior mean at a test point with cross-covariance k(x) = K_xn.
Definition IterGP.cpp:102
double relativeResidual(const Eigen::MatrixXd &K_hat, const Eigen::VectorXd &y) const
Relative residual ||K_hat v_i - y|| / ||y||, the usual convergence view.
Definition IterGP.cpp:135
Eigen::MatrixXd inverseApproximation() const
Definition IterGP.cpp:94
double computationalVariance(const Eigen::LDLT< Eigen::MatrixXd > &exact, const Eigen::VectorXd &kx) const
Definition IterGP.cpp:123
int iterations() const noexcept
Definition IterGP.h:62
double combinedVariance(double kxx, const Eigen::VectorXd &kx) const
Definition IterGP.cpp:110
void run(const Eigen::MatrixXd &K_hat, const Eigen::VectorXd &y, int iterations, Policy policy=Policy::ConjugateGradient)
Definition IterGP.cpp:15
@ Unit
s_i = e_i, the i-th coordinate direction
Definition IterGP.h:46
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16