Loading...
Searching...
No Matches
DerivativeVecchia.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
5
6#include <Eigen/Cholesky>
7#include <algorithm>
8#include <cmath>
9#include <numeric>
10#include <stdexcept>
11
12namespace gpr {
13namespace vecchia {
14
15Eigen::MatrixXd jointBlock(const Eigen::VectorXd& a, const Eigen::VectorXd& b,
16 double lengthscale)
17{
18 const Eigen::Index d = a.size();
19 const double l2 = lengthscale * lengthscale;
20 const Eigen::VectorXd diff = a - b;
21 const double k = std::exp(-0.5 * diff.squaredNorm() / l2);
22
23 Eigen::MatrixXd block(1 + d, 1 + d);
24 block(0, 0) = k;
25 for (Eigen::Index p = 0; p < d; ++p) {
26 // d/da_p and d/db_p carry opposite signs.
27 block(1 + p, 0) = -diff(p) / l2 * k;
28 block(0, 1 + p) = diff(p) / l2 * k;
29 }
30 for (Eigen::Index p = 0; p < d; ++p) {
31 for (Eigen::Index q = 0; q < d; ++q) {
32 const double kron = (p == q) ? 1.0 / l2 : 0.0;
33 block(1 + p, 1 + q) = (kron - diff(p) * diff(q) / (l2 * l2)) * k;
34 }
35 }
36 return block;
37}
38
39namespace {
40
42Eigen::MatrixXd assemble(const Eigen::MatrixXd& points,
43 const std::vector<int>& idx, double lengthscale,
44 double noise)
45{
46 const Eigen::Index d = points.cols();
47 const Eigen::Index bs = 1 + d;
48 const Eigen::Index n = static_cast<Eigen::Index>(idx.size());
49 Eigen::MatrixXd K(n * bs, n * bs);
50 for (Eigen::Index i = 0; i < n; ++i) {
51 for (Eigen::Index j = 0; j < n; ++j) {
52 K.block(i * bs, j * bs, bs, bs) = jointBlock(
53 points.row(idx[static_cast<std::size_t>(i)]).transpose(),
54 points.row(idx[static_cast<std::size_t>(j)]).transpose(),
55 lengthscale);
56 }
57 }
58 K.diagonal().array() += noise;
59 return K;
60}
61
63Eigen::VectorXd gather(const Eigen::VectorXd& y, const std::vector<int>& idx,
64 Eigen::Index block_size)
65{
66 Eigen::VectorXd out(static_cast<Eigen::Index>(idx.size()) * block_size);
67 for (std::size_t i = 0; i < idx.size(); ++i) {
68 out.segment(static_cast<Eigen::Index>(i) * block_size, block_size) =
69 y.segment(idx[i] * block_size, block_size);
70 }
71 return out;
72}
73
74} // namespace
75
76std::vector<std::vector<int>> nearestPredecessors(const Eigen::MatrixXd& points,
77 int max_neighbours)
78{
79 const int n = static_cast<int>(points.rows());
80 std::vector<std::vector<int>> out(static_cast<std::size_t>(std::max(0, n)));
81 if (n <= 0) return out;
82 const int cap = std::max(0, max_neighbours);
83 for (int i = 0; i < n; ++i) {
84 std::vector<std::pair<double, int>> candidates;
85 candidates.reserve(static_cast<std::size_t>(i));
86 for (int j = 0; j < i; ++j) {
87 candidates.emplace_back(
88 (points.row(i) - points.row(j)).squaredNorm(), j);
89 }
90 const std::size_t keep =
91 std::min(static_cast<std::size_t>(cap), candidates.size());
92 std::partial_sort(
93 candidates.begin(),
94 candidates.begin() + static_cast<std::ptrdiff_t>(keep),
95 candidates.end());
96 std::vector<int> nb;
97 nb.reserve(keep);
98 for (std::size_t k = 0; k < keep; ++k)
99 nb.push_back(candidates[k].second);
100 out[static_cast<std::size_t>(i)] = std::move(nb);
101 }
102 return out;
103}
104
105VecchiaResult evaluate(const Eigen::MatrixXd& points, const Eigen::VectorXd& y,
106 double lengthscale, double noise, int max_neighbours)
107{
108 VecchiaResult result;
109 const Eigen::Index n = points.rows();
110 const Eigen::Index d = points.cols();
111 const Eigen::Index bs = 1 + d;
112 if (n == 0 || d == 0) return result;
113 if (y.size() != n * bs) {
114 throw std::invalid_argument(
115 "Vecchia: observation vector size mismatch");
116 }
117 if (!(lengthscale > 0.0)) {
118 throw std::invalid_argument("Vecchia: lengthscale must be positive");
119 }
120
121 const std::vector<std::vector<int>> neighbours =
122 nearestPredecessors(points, max_neighbours);
123
124 for (Eigen::Index i = 0; i < n; ++i) {
125 const std::vector<int>& nb = neighbours[static_cast<std::size_t>(i)];
126 const Eigen::VectorXd xi = points.row(i).transpose();
127 const Eigen::VectorXd yi = y.segment(i * bs, bs);
128 Eigen::MatrixXd cond = jointBlock(xi, xi, lengthscale);
129 cond.diagonal().array() += noise;
130 Eigen::VectorXd mean = Eigen::VectorXd::Zero(bs);
131
132 if (!nb.empty()) {
133 const Eigen::MatrixXd Knn =
134 assemble(points, nb, lengthscale, noise);
135 const Eigen::Index m = static_cast<Eigen::Index>(nb.size()) * bs;
136 Eigen::MatrixXd Kin(bs, m);
137 for (std::size_t j = 0; j < nb.size(); ++j) {
138 Kin.block(0, static_cast<Eigen::Index>(j) * bs, bs, bs) =
139 jointBlock(xi, points.row(nb[j]).transpose(), lengthscale);
140 }
141 const Eigen::LLT<Eigen::MatrixXd> llt(Knn);
142 if (llt.info() != Eigen::Success) return result;
143 const Eigen::MatrixXd solved = llt.solve(Kin.transpose());
144 cond.noalias() -= Kin * solved;
145 mean.noalias() = Kin * llt.solve(gather(y, nb, bs));
146 result.largest_local_solve = std::max<std::size_t>(
147 result.largest_local_solve, static_cast<std::size_t>(m + bs));
148 result.total_local_rows += static_cast<std::size_t>(m + bs);
149 } else {
150 result.largest_local_solve = std::max<std::size_t>(
151 result.largest_local_solve, static_cast<std::size_t>(bs));
152 result.total_local_rows += static_cast<std::size_t>(bs);
153 }
154
155 // Symmetrize before factoring: the Schur complement above is symmetric
156 // in exact arithmetic and only nearly so in floating point.
157 cond = 0.5 * (cond + cond.transpose()).eval();
158 const Eigen::LLT<Eigen::MatrixXd> cllt(cond);
159 if (cllt.info() != Eigen::Success) return result;
160 const Eigen::VectorXd residual = yi - mean;
161 const Eigen::VectorXd w = cllt.solve(residual);
162 result.quadratic_form += residual.dot(w);
163 // log det from the Cholesky diagonal, doubled.
164 result.log_determinant +=
165 2.0 *
166 cllt.matrixL().nestedExpression().diagonal().array().log().sum();
167 }
168 result.valid = true;
169 return result;
170}
171
172VecchiaResult evaluateExact(const Eigen::MatrixXd& points,
173 const Eigen::VectorXd& y, double lengthscale,
174 double noise)
175{
176 VecchiaResult result;
177 const Eigen::Index n = points.rows();
178 const Eigen::Index d = points.cols();
179 if (n == 0 || d == 0) return result;
180 std::vector<int> all(static_cast<std::size_t>(n));
181 std::iota(all.begin(), all.end(), 0);
182 const Eigen::MatrixXd K = assemble(points, all, lengthscale, noise);
183 const Eigen::LLT<Eigen::MatrixXd> llt(K);
184 if (llt.info() != Eigen::Success) return result;
185 result.quadratic_form = y.dot(llt.solve(y));
186 result.log_determinant =
187 2.0 * llt.matrixL().nestedExpression().diagonal().array().log().sum();
188 result.largest_local_solve = static_cast<std::size_t>(K.rows());
189 result.total_local_rows = static_cast<std::size_t>(K.rows());
190 result.valid = true;
191 return result;
192}
193
194} // namespace vecchia
195} // namespace gpr
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Eigen::MatrixXd jointBlock(const Eigen::VectorXd &a, const Eigen::VectorXd &b, double lengthscale)
The joint value/gradient kernel block between two points, (1+d) x (1+d).
VecchiaResult evaluate(const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise, int max_neighbours)
std::vector< std::vector< int > > nearestPredecessors(const Eigen::MatrixXd &points, int max_neighbours)
VecchiaResult evaluateExact(const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
What the approximation produced, and what it cost.
double log_determinant
sum of local conditional log-dets
std::size_t total_local_rows
summed local work
bool valid
false when a local block was not factorizable
std::size_t largest_local_solve
rows in the biggest local system
double quadratic_form
y^T K^{-1} y, summed over conditionals