Loading...
Searching...
No Matches
ArrowheadSpectrum.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5// Eigenpairs of an arrowhead, the one-row border of a diagonal matrix.
6//
7// A kernel that grows by one training row at fixed hyperparameters is
8// that border of its previous eigenbasis. The roots of the secular
9// equation are the eigenvalues of the bordered matrix, and the
10// eigenvectors are the corresponding rank-one formulas. A gap the
11// symmetric backward error cannot separate is left to a full solve:
12// clustering inside that error is where two drivers disagree.
13
14#ifndef GPR_ML_GP_ARROWHEAD_SPECTRUM_H_
15#define GPR_ML_GP_ARROWHEAD_SPECTRUM_H_
16
17#include <Eigen/Core>
18#include <algorithm>
19#include <cmath>
20#include <limits>
21#include <vector>
22
24
25namespace gpr {
26
31[[nodiscard]] inline bool arrowheadEigenpairs(
32 const Eigen::Ref<const Eigen::VectorXd>& diagonal,
33 const Eigen::Ref<const Eigen::VectorXd>& spike, double corner,
34 Eigen::VectorXd& evals, Eigen::MatrixXd& evecs)
35{
36 const Eigen::Index n = diagonal.size();
37 if (n <= 0 || spike.size() != n || !diagonal.allFinite() ||
38 !spike.allFinite() || !std::isfinite(corner))
39 return false;
40
41 double scale = std::abs(corner);
42 for (Eigen::Index i = 0; i < n; ++i)
43 scale = std::max(scale, std::abs(diagonal(i)));
44 scale = std::max(scale, spike.cwiseAbs().maxCoeff());
45 const double gap = linalg::symmetricBackwardError(n + 1, scale);
46
47 std::vector<Eigen::Index> order(static_cast<std::size_t>(n));
48 for (Eigen::Index i = 0; i < n; ++i)
49 order[static_cast<std::size_t>(i)] = i;
50 std::sort(order.begin(), order.end(), [&](Eigen::Index a, Eigen::Index b) {
51 return diagonal(a) < diagonal(b);
52 });
53 for (std::size_t i = 1; i < order.size(); ++i) {
54 if (diagonal(order[i]) - diagonal(order[i - 1]) <= gap) return false;
55 }
56
57 Eigen::VectorXd d(n);
58 Eigen::VectorXd u(n);
59 std::vector<Eigen::Index> active;
60 active.reserve(static_cast<std::size_t>(n));
61 const double spike_scale = std::max(spike.cwiseAbs().maxCoeff(), 1.0);
62 for (Eigen::Index i = 0; i < n; ++i) {
63 d(i) = diagonal(order[static_cast<std::size_t>(i)]);
64 u(i) = spike(order[static_cast<std::size_t>(i)]);
65 if (std::abs(u(i)) > gap * spike_scale) active.push_back(i);
66 }
67
68 const auto secular = [&](double lambda) {
69 long double sum = static_cast<long double>(corner - lambda);
70 for (const Eigen::Index i: active) {
71 const long double denom = static_cast<long double>(d(i)) -
72 static_cast<long double>(lambda);
73 sum -= static_cast<long double>(u(i)) *
74 static_cast<long double>(u(i)) / denom;
75 }
76 return static_cast<double>(sum);
77 };
78
79 const double reach = spike.norm() + std::abs(corner) + scale + 1.0;
80 std::vector<std::pair<double, double>> brackets;
81 brackets.reserve(static_cast<std::size_t>(active.size() + 2));
82 if (!active.empty()) {
83 brackets.emplace_back(d(active.front()) - reach, d(active.front()));
84 for (std::size_t i = 1; i < active.size(); ++i)
85 brackets.emplace_back(d(active[i - 1]), d(active[i]));
86 brackets.emplace_back(d(active.back()), d(active.back()) + reach);
87 } else {
88 brackets.emplace_back(corner - reach, corner + reach);
89 }
90
91 const auto isolate = [&](double left, double right, double& root) {
92 const double width = right - left;
93 if (!(width > gap)) return false;
94 const double pad =
95 std::max(gap, width * std::numeric_limits<double>::epsilon());
96 double lo = left + pad;
97 double hi = right - pad;
98 if (!(lo < hi)) return false;
99 double flo = secular(lo);
100 double fhi = secular(hi);
101 if (!std::isfinite(flo) || !std::isfinite(fhi) || flo * fhi > 0.0)
102 return false;
103 for (int iter = 0; iter < 80 && hi - lo > gap; ++iter) {
104 const double mid = 0.5 * (lo + hi);
105 const double fmid = secular(mid);
106 if (!std::isfinite(fmid)) return false;
107 if (flo * fmid <= 0.0) {
108 hi = mid;
109 fhi = fmid;
110 } else {
111 lo = mid;
112 flo = fmid;
113 }
114 }
115 root = 0.5 * (lo + hi);
116 return std::isfinite(root);
117 };
118
119 std::vector<double> roots;
120 roots.reserve(brackets.size());
121 for (const auto& bracket: brackets) {
122 double root = 0.0;
123 if (!isolate(bracket.first, bracket.second, root)) return false;
124 roots.push_back(root);
125 }
126 if (static_cast<Eigen::Index>(
127 roots.size() + (n - static_cast<Eigen::Index>(active.size()))) !=
128 n + 1)
129 return false;
130
131 evals.resize(n + 1);
132 evecs = Eigen::MatrixXd::Zero(n + 1, n + 1);
133 Eigen::Index col = 0;
134 for (Eigen::Index i = 0; i < n; ++i) {
135 if (std::abs(u(i)) > gap * spike_scale) continue;
136 evals(col) = d(i);
137 evecs(i, col) = 1.0;
138 ++col;
139 }
140 for (const double lambda: roots) {
141 evals(col) = lambda;
142 double norm_sq = 1.0;
143 for (const Eigen::Index i: active) {
144 // (D - λ I) v_head + spike * v_corner = 0 with v_corner = 1.
145 const double component = u(i) / (lambda - d(i));
146 evecs(i, col) = component;
147 norm_sq += component * component;
148 }
149 evecs(n, col) = 1.0;
150 evecs.col(col) /= std::sqrt(norm_sq);
151 ++col;
152 }
153 if (col != n + 1) return false;
154
155 // The secular vectors are in the sorted-diagonal order. Permute the
156 // leading rows back to the caller's diagonal order; the new row stays.
157 Eigen::MatrixXd restored = evecs;
158 for (Eigen::Index i = 0; i < n; ++i)
159 restored.row(order[static_cast<std::size_t>(i)]) = evecs.row(i);
160 evecs = restored;
161 linalg::canonicalizeEigenpairs(evals, evecs);
162 return true;
163}
164
169[[nodiscard]] inline bool extendSymmetricSpectrum(
170 const Eigen::Ref<const Eigen::MatrixXd>& grown,
171 const Eigen::Ref<const Eigen::VectorXd>& diagonal,
172 const Eigen::Ref<const Eigen::MatrixXd>& basis, Eigen::VectorXd& evals,
173 Eigen::MatrixXd& evecs)
174{
175 const Eigen::Index n0 = diagonal.size();
176 const Eigen::Index n = grown.rows();
177 if (n0 <= 0 || n <= n0 || grown.cols() != n || basis.rows() != n0 ||
178 basis.cols() != n0 || !grown.allFinite())
179 return false;
180
181 Eigen::VectorXd d = diagonal;
182 Eigen::MatrixXd q = basis;
183 for (Eigen::Index col = n0; col < n; ++col) {
184 const Eigen::Index m = q.rows();
185 const Eigen::VectorXd border = grown.col(col).head(m);
186 const Eigen::VectorXd spike = q.transpose() * border;
187 Eigen::VectorXd next_d;
188 Eigen::MatrixXd next_v;
189 if (!arrowheadEigenpairs(d, spike, grown(col, col), next_d, next_v))
190 return false;
191 Eigen::MatrixXd next_q(m + 1, m + 1);
192 next_q.topRows(m).noalias() = q * next_v.topRows(m);
193 next_q.row(m) = next_v.row(m);
194 q = std::move(next_q);
195 d = std::move(next_d);
196 }
197
198 const double scale = grown.cwiseAbs().maxCoeff();
199 // A dense matvec spreads the per-entry backward error across the
200 // vector. Roots inside this bound are the same eigenpairs a full
201 // symmetric solve is allowed to return.
202 const double residual_tol =
203 1024.0 * std::sqrt(static_cast<double>(n)) *
204 linalg::symmetricBackwardError(n, std::max(scale, 1.0));
205 for (Eigen::Index j = 0; j < d.size(); ++j) {
206 const double residual = (grown * q.col(j) - d(j) * q.col(j)).norm();
207 if (!std::isfinite(residual) || residual > residual_tol) return false;
208 }
209 evals = std::move(d);
210 evecs = std::move(q);
211 return true;
212}
213
214} // namespace gpr
215
216#endif // GPR_ML_GP_ARROWHEAD_SPECTRUM_H_
Implementation-independent symmetric eigenpairs.
void canonicalizeEigenpairs(Eigen::Ref< Eigen::VectorXd > evals, Eigen::Ref< Eigen::MatrixXd > evecs)
evals has one entry per leading column of evecs. Both are rewritten.
double symmetricBackwardError(Eigen::Index n, double scale) noexcept
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
bool extendSymmetricSpectrum(const Eigen::Ref< const Eigen::MatrixXd > &grown, const Eigen::Ref< const Eigen::VectorXd > &diagonal, const Eigen::Ref< const Eigen::MatrixXd > &basis, Eigen::VectorXd &evals, Eigen::MatrixXd &evecs)
bool arrowheadEigenpairs(const Eigen::Ref< const Eigen::VectorXd > &diagonal, const Eigen::Ref< const Eigen::VectorXd > &spike, double corner, Eigen::VectorXd &evals, Eigen::MatrixXd &evecs)