32 using V = Eigen::VectorXd;
33 using M = Eigen::MatrixXd;
37 std::vector<V> points_;
38 std::vector<PotentialEvaluation> values_;
39 std::vector<std::size_t> fittedOwners_;
40 std::unique_ptr<AffineGaussianJet> posterior_;
41 std::size_t fittedSites_{}, fitEvaluations_{};
42 Eigen::Index fittedSpan_{}, fittedTangent_{}, fittedNormal_{};
43 double fittedLength_{};
45 static void require(
bool yes,
const char* message)
47 if (!yes)
throw std::invalid_argument(message);
49 Eigen::Index find(
const V& q)
const
51 const V physical = metric_.physical(q);
52 for (std::size_t i = 0; i < points_.size(); ++i) {
53 const V candidate = metric_.physical(points_[i]);
54 const double scale = std::max({1., physical.cwiseAbs().maxCoeff(),
55 candidate.cwiseAbs().maxCoeff()});
56 if ((physical - candidate).cwiseAbs().maxCoeff() <=
57 128 * std::numeric_limits<double>::epsilon() * scale)
62 void fit(
const std::vector<std::size_t>& selected)
64 require(!selected.empty(),
65 "At least one native Gaussian observation required");
67 fittedOwners_.clear();
69 fittedSpan_ = fittedTangent_ = fittedNormal_ = 0;
70 const auto n = selected.size();
71 const auto d = metric_.dimension();
74 for (std::size_t i = 0; i < n; ++i) {
75 const auto owner = selected[i];
76 const V background = well_ * points_[owner];
77 x.row(i) = metric_.physical(points_[owner]).transpose();
78 e[i] = values_[owner].energy - .5 * points_[owner].dot(background);
79 g.row(i) = ((values_[owner].gradient - background).array() *
80 metric_.roots().array())
84 std::vector<double> distances;
85 for (std::size_t i = 0; i < n; ++i)
86 for (std::size_t j = 0; j < i; ++j) {
87 const double distance = (
x.row(i) -
x.row(j)).norm();
88 if (distance > 0) distances.push_back(distance);
90 double pivot = options_.initialLength;
92 if (distances.empty())
95 const auto mid = distances.begin() + distances.size() / 2;
96 std::nth_element(distances.begin(), mid, distances.end());
101 options_.minimumLength > 0 ? options_.minimumLength : pivot / 16;
103 options_.maximumLength > 0 ? options_.maximumLength : pivot * 16;
104 require(lower > 0 && upper >= lower && std::isfinite(upper),
105 "Finite ordered Gaussian length bounds required");
106 const double lo = std::log(lower), hi = std::log(upper);
109 std::unique_ptr<AffineGaussianJet> best;
110 double score = std::numeric_limits<double>::infinity(), bestLog = 0;
111 std::vector<std::pair<double, double>> evaluated;
112 auto available = [&] {
113 return !options_.maximumFitEvaluations ||
114 fitEvaluations_ < options_.maximumFitEvaluations;
118 double value = std::numeric_limits<double>::infinity();
120 auto candidate = std::make_unique<AffineGaussianJet>(
121 x, e, g, std::exp(t), options_.signalVariance,
122 options_.energyNoiseVariance,
123 options_.gradientNoiseVariance);
124 value = candidate->negativeLogLikelihood();
128 best = std::move(candidate);
130 }
catch (
const std::runtime_error&) {
132 evaluated.emplace_back(t, value);
136 fittedLength_ > 0 ? std::log(fittedLength_) : std::log(pivot), lo,
139 for (
unsigned j = 0; j < 9 && available(); ++j) {
140 const double t = lo + (hi - lo) * j / 8.;
141 if (std::none_of(evaluated.begin(), evaluated.end(),
142 [&](
const auto& v) { return v.first == t; }))
145 if (best && available()) {
146 std::sort(evaluated.begin(), evaluated.end());
147 double left = lo, right = hi;
148 for (
const auto& sample: evaluated) {
149 if (sample.first < bestLog) left = sample.first;
150 if (sample.first > bestLog) {
151 right = sample.first;
155 constexpr double fraction = .3819660112501051518;
156 while (available() &&
157 right - left > options_.logLengthTolerance) {
159 bestLog - left > right - bestLog
160 ? bestLog - fraction * (bestLog - left)
161 : bestLog + fraction * (right - bestLog);
162 const double previous = bestLog;
164 if (bestLog != previous) {
169 }
else if (t < previous)
173 if (!std::isfinite(value) && left == right)
break;
175 converged_ = right - left <= options_.logLengthTolerance;
181 "No positive Gaussian covariance exists within the length bounds");
182 fittedSpan_ = best->spanDimension();
183 fittedTangent_ = best->tangentFactorDimension();
184 fittedNormal_ = best->normalFactorDimension();
185 posterior_ = std::move(best);
187 fittedOwners_ = selected;
188 fittedLength_ = std::exp(bestLog);
197 : metric_(
std::move(metric)), well_(
std::move(well)), options_(options)
199 require(well_.rows() == metric_.
dimension() &&
200 well_.cols() == metric_.
dimension() && well_.allFinite() &&
201 (well_ - well_.transpose()).norm() <=
202 1e-12 * std::max(1., well_.norm()),
203 "Finite symmetric Gaussian background curvature required");
218 "Positive Gaussian prior and finite observation controls required");
221 const std::vector<PotentialEvaluation>& values)
223 require(q.size() == values.size(),
224 "Matching native energy-gradient observations required");
225 std::size_t added = 0;
226 for (std::size_t i = 0; i < q.size(); ++i) {
227 require(q[i].size() == metric_.dimension() && q[i].allFinite() &&
228 values[i].gradient.size() == metric_.dimension() &&
229 values[i].gradient.allFinite() &&
230 std::isfinite(values[i].energy),
231 "Finite native observations in the supplied mass metric "
233 if (find(q[i]) >= 0)
continue;
234 points_.push_back(q[i]);
235 values_.push_back(values[i]);
238 if (added) posterior_.reset();
242 const std::vector<PotentialEvaluation>& values,
243 std::size_t maximumSites)
245 require(!q.empty() && maximumSites > 0,
246 "Positive native Gaussian training budget required");
248 std::vector<std::size_t> selected;
249 std::vector<bool> required(points_.size(),
false);
250 for (
const auto& point: q) {
251 const auto index = find(point);
252 require(index >= 0,
"Requested native observation is absent");
253 if (!required[index]) {
254 required[index] =
true;
255 selected.push_back(index);
258 require(selected.size() <= maximumSites,
259 "Training budget cannot exclude a requested native site");
260 if (options_.archiveSelection ==
262 std::vector<std::pair<double, std::size_t>> candidates;
263 for (std::size_t i = 0; i < points_.size(); ++i)
265 double distance = std::numeric_limits<double>::infinity();
266 for (
const auto& point: q)
267 distance = std::min(distance,
268 (point - points_[i]).squaredNorm());
269 candidates.emplace_back(distance, i);
271 std::stable_sort(candidates.begin(), candidates.end());
272 for (
const auto& candidate: candidates) {
273 if (selected.size() == maximumSites)
break;
274 selected.push_back(candidate.second);
281 using Candidate = std::pair<double, std::size_t>;
282 std::vector<std::vector<Candidate>> neighbours(selected.size());
283 for (std::size_t anchor = 0; anchor < selected.size(); ++anchor) {
284 auto& candidates = neighbours[anchor];
285 for (std::size_t i = 0; i < points_.size(); ++i)
287 candidates.emplace_back(
288 (points_[selected[anchor]] - points_[i]).squaredNorm(),
290 std::stable_sort(candidates.begin(), candidates.end());
292 std::vector<std::size_t> cursor(neighbours.size(), 0);
293 while (selected.size() < maximumSites &&
294 selected.size() < points_.size()) {
295 const auto previous = selected.size();
296 for (std::size_t anchor = 0; anchor < neighbours.size(); ++anchor) {
297 auto& next = cursor[anchor];
298 const auto& candidates = neighbours[anchor];
299 while (next < candidates.size() &&
300 required[candidates[next].second])
302 if (next == candidates.size())
continue;
303 const auto owner = candidates[next++].second;
304 required[owner] =
true;
305 selected.push_back(owner);
306 if (selected.size() == maximumSites)
break;
308 if (selected.size() == previous)
break;
313 const std::vector<PotentialEvaluation>& values)
316 std::vector<std::size_t> all(points_.size());
317 std::iota(all.begin(), all.end(), 0);
320 std::vector<PotentialEvaluation>
evaluate(
const std::vector<V>& q)
const
324 "Native Gaussian observations require fitting before prediction");
325 std::vector<PotentialEvaluation> result;
326 for (
const auto& point: q) {
327 const auto predicted =
328 posterior_->predict(metric_.physical(point),
false);
330 {predicted.energy + .5 * point.dot(well_ * point),
331 metric_.gradient(predicted.gradient) + well_ * point});
337 require(
bool(posterior_),
338 "A fitted Gaussian posterior is required for search curvature");
339 const auto predicted =
340 posterior_->predict(metric_.physical(point),
false);
341 const M curvature = well_ + metric_.hessian(predicted.hessian);
342 return [curvature](
const V& direction, V& result) {
343 if (direction.size() != curvature.cols())
return false;
344 result = curvature * direction;
345 return result.allFinite();
349 const std::vector<V>& q)
const
351 require(
bool(posterior_),
352 "A fitted Gaussian posterior is required for uncertainty");
353 std::vector<PointVariance> result;
354 for (
const auto& point: q) {
355 const auto predicted = posterior_->predict(metric_.physical(point));
357 options_.signalVariance / (fittedLength_ * fittedLength_);
358 require(predicted.energyVariance >=
359 -1e-12 * std::max(1., options_.signalVariance) &&
360 predicted.gradientVariance.minCoeff() >=
361 -1e-12 * std::max(1., scale),
362 "Gaussian posterior variance is negative");
363 const V diagonal = predicted.gradientVariance.cwiseMax(0.);
365 {std::max(0., predicted.energyVariance),
366 (diagonal.array() / metric_.roots().array().square()).sum(),
373 return points_.size();
381 return fittedOwners_;
385 return fitEvaluations_;
393 return fittedLength_;
401 return fittedTangent_;
405 return fittedNormal_;