Loading...
Searching...
No Matches
gpr::vecchia Namespace Reference

Classes

struct  VecchiaResult
 What the approximation produced, and what it cost. More...

Functions

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).
std::vector< std::vector< int > > nearestPredecessors (const Eigen::MatrixXd &points, int max_neighbours)
VecchiaResult evaluate (const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise, int max_neighbours)
VecchiaResult evaluateExact (const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise)

Function Documentation

◆ evaluate()

VecchiaResult gpr::vecchia::evaluate ( const Eigen::MatrixXd & points,
const Eigen::VectorXd & y,
double lengthscale,
double noise,
int max_neighbours )
nodiscard

Evaluate the Vecchia log-likelihood pieces for a gradient-enhanced SE-ARD GP.

points is n x d. y is the stacked observation vector of length n(d+1) laid out point-major: for point i, one value followed by its d gradient components. noise is added to the diagonal.

With max_neighbours >= n-1 this is exact.

Definition at line 105 of file DerivativeVecchia.cpp.

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}
@ y
Definition Enums.h:28
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).
std::vector< std::vector< int > > nearestPredecessors(const Eigen::MatrixXd &points, int max_neighbours)
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

◆ evaluateExact()

VecchiaResult gpr::vecchia::evaluateExact ( const Eigen::MatrixXd & points,
const Eigen::VectorXd & y,
double lengthscale,
double noise )
nodiscard

The exact joint quantities, for comparison. Builds the full n(d+1) matrix, so it exists to validate evaluate, not to be used beside it.

Definition at line 172 of file DerivativeVecchia.cpp.

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}

◆ jointBlock()

Eigen::MatrixXd gpr::vecchia::jointBlock ( const Eigen::VectorXd & a,
const Eigen::VectorXd & b,
double lengthscale )
nodiscard

The joint value/gradient kernel block between two points, (1+d) x (1+d).

Definition at line 15 of file DerivativeVecchia.cpp.

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}

◆ nearestPredecessors()

std::vector< std::vector< int > > gpr::vecchia::nearestPredecessors ( const Eigen::MatrixXd & points,
int max_neighbours )
nodiscard

Neighbour sets over POINTS (not rows): entry i lists the predecessors of point i, nearest first, at most max_neighbours of them.

Definition at line 76 of file DerivativeVecchia.cpp.

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}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out