Loading...
Searching...
No Matches
gpr::itergp::IterGP Class Reference

#include <gpr/ml/gp/IterGP.h>

Public Member Functions

void run (const Eigen::MatrixXd &K_hat, const Eigen::VectorXd &y, int iterations, Policy policy=Policy::ConjugateGradient)
int iterations () const noexcept
const Eigen::MatrixXd & actions () const noexcept
 The actions taken, n x i.
const Eigen::VectorXd & representerWeights () const noexcept
 v_i = C_i y, the representer weights the run justifies.
Eigen::MatrixXd inverseApproximation () const
double posteriorMean (const Eigen::VectorXd &kx) const
 Posterior mean at a test point with cross-covariance k(x) = K_xn.
double combinedVariance (double kxx, const Eigen::VectorXd &kx) const
double computationalVariance (const Eigen::LDLT< Eigen::MatrixXd > &exact, const Eigen::VectorXd &kx) const
double relativeResidual (const Eigen::MatrixXd &K_hat, const Eigen::VectorXd &y) const
 Relative residual ||K_hat v_i - y|| / ||y||, the usual convergence view.

Detailed Description

One IterGP run against a fixed regularized kernel.

Everything is expressed through the action matrix S, so the cost of a query is set by the number of iterations rather than by the matrix size, and the uncertainty split falls out of the same factor.

Definition at line 54 of file IterGP.h.

Member Function Documentation

◆ actions()

const Eigen::MatrixXd & gpr::itergp::IterGP::actions ( ) const
inlinenodiscardnoexcept

The actions taken, n x i.

Definition at line 68 of file IterGP.h.

69 {
70 return S_;
71 }

◆ combinedVariance()

double gpr::itergp::IterGP::combinedVariance ( double kxx,
const Eigen::VectorXd & kx ) const
nodiscard

Combined (mathematical + computational) posterior variance, k(x,x) - k(x)^T C_i k(x).

Definition at line 110 of file IterGP.cpp.

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}

◆ computationalVariance()

double gpr::itergp::IterGP::computationalVariance ( const Eigen::LDLT< Eigen::MatrixXd > & exact,
const Eigen::VectorXd & kx ) const
nodiscard

The computational part alone, k(x)^T (K_hat^{-1} - C_i) k(x). Requires the exact inverse action and so is for diagnostics and tests, not for the hot path; it is non-negative and decreases as iterations are added.

Definition at line 123 of file IterGP.cpp.

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}

◆ inverseApproximation()

Eigen::MatrixXd gpr::itergp::IterGP::inverseApproximation ( ) const
nodiscard

C_i = S (S^T K_hat S)^{-1} S^T, the solver's approximation to K_hat^{-1}. Dense, for validation; the query paths below never form it.

Definition at line 94 of file IterGP.cpp.

95{
96 if (S_.cols() == 0) {
97 return Eigen::MatrixXd::Zero(S_.rows(), S_.rows());
98 }
99 return S_ * Sinv_ * S_.transpose();
100}

◆ iterations()

int gpr::itergp::IterGP::iterations ( ) const
inlinenodiscardnoexcept

Definition at line 62 of file IterGP.h.

63 {
64 return static_cast<int>(S_.cols());
65 }

◆ posteriorMean()

double gpr::itergp::IterGP::posteriorMean ( const Eigen::VectorXd & kx) const
nodiscard

Posterior mean at a test point with cross-covariance k(x) = K_xn.

Definition at line 102 of file IterGP.cpp.

103{
104 if (kx.size() != v_.size()) {
105 throw std::invalid_argument("IterGP: cross-covariance size mismatch");
106 }
107 return kx.dot(v_);
108}

◆ relativeResidual()

double gpr::itergp::IterGP::relativeResidual ( const Eigen::MatrixXd & K_hat,
const Eigen::VectorXd & y ) const
nodiscard

Relative residual ||K_hat v_i - y|| / ||y||, the usual convergence view.

Definition at line 135 of file IterGP.cpp.

137{
138 const double denom = y.norm();
139 if (!(denom > 0.0)) return 0.0;
140 return (K_hat * v_ - y).norm() / denom;
141}
@ y
Definition Enums.h:28

◆ representerWeights()

const Eigen::VectorXd & gpr::itergp::IterGP::representerWeights ( ) const
inlinenodiscardnoexcept

v_i = C_i y, the representer weights the run justifies.

Definition at line 74 of file IterGP.h.

75 {
76 return v_;
77 }

◆ run()

void gpr::itergp::IterGP::run ( const Eigen::MatrixXd & K_hat,
const Eigen::VectorXd & y,
int iterations,
Policy policy = Policy::ConjugateGradient )

Take iterations actions against K_hat with right-hand side y. K_hat must be symmetric positive definite (add the noise before calling). Iterations are capped at the matrix size.

Definition at line 15 of file IterGP.cpp.

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}
int iterations() const noexcept
Definition IterGP.h:62
@ Unit
s_i = e_i, the i-th coordinate direction
Definition IterGP.h:46

The documentation for this class was generated from the following files: