Loading...
Searching...
No Matches
QuadraticMeanGPModel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <algorithm>
8#include <cmath>
9#include <functional>
10#include <stdexcept>
11#include <utility>
12#include <vector>
13
15
16namespace gpr::quantum {
17// The referenced model owns residual training targets and must start empty.
18// Native labels remain in this owner's exact mass-coordinate archive. The
19// deterministic mean changes neither posterior variances nor native ownership.
20template <class Model>
22 using V = Eigen::VectorXd;
23 using M = Eigen::MatrixXd;
24 Model& residual_;
25 V reference_, gradient_;
26 double energy_;
27 M hessian_;
28 struct NativeRecord {
29 V point;
31 };
32 std::vector<NativeRecord> native_;
33
34 void validatePoint(const V& point) const
35 {
36 if (point.size() != reference_.size() || !point.allFinite())
37 throw std::invalid_argument(
38 "Finite matching mass-coordinate mean sites required");
39 }
40 PotentialEvaluation mean(const V& point) const
41 {
42 validatePoint(point);
43 const V delta = point - reference_;
45 energy_ + gradient_.dot(delta) + .5 * delta.dot(hessian_ * delta),
46 V(gradient_ + hessian_ * delta)};
47 if (!std::isfinite(value.energy) || !value.gradient.allFinite())
48 throw std::invalid_argument(
49 "Finite quadratic mean values required");
50 return value;
51 }
52 std::vector<PotentialEvaluation> residualValues(
53 const std::vector<V>& points,
54 const std::vector<PotentialEvaluation>& values) const
55 {
56 if (points.size() != values.size())
57 throw std::invalid_argument(
58 "Matching native mean observations required");
59 std::vector<PotentialEvaluation> result;
60 result.reserve(values.size());
61 for (std::size_t j = 0; j < points.size(); ++j) {
62 const auto background = mean(points[j]);
63 if (!std::isfinite(values[j].energy) ||
64 values[j].gradient.size() != reference_.size() ||
65 !values[j].gradient.allFinite())
66 throw std::invalid_argument(
67 "Finite matching native energy and gradient required");
68 result.push_back({values[j].energy - background.energy,
69 V(values[j].gradient - background.gradient)});
70 if (!std::isfinite(result.back().energy) ||
71 !result.back().gradient.allFinite())
72 throw std::invalid_argument("Finite residual targets required");
73 }
74 return result;
75 }
76 const NativeRecord* findNative(const V& point) const
77 {
78 for (const auto& record: native_)
79 if ((record.point.array() == point.array()).all()) return &record;
80 return nullptr;
81 }
82 void retainNative(const std::vector<V>& points,
83 const std::vector<PotentialEvaluation>& values)
84 {
85 for (std::size_t j = 0; j < points.size(); ++j)
86 if (!findNative(points[j]))
87 native_.push_back({points[j], values[j]});
88 }
89
90public:
91 using HessianAction = std::function<bool(const V&, V&)>;
92
93 QuadraticMeanGPModel(Model& residual, V reference, double energy,
94 V gradient, M hessian)
95 : residual_(residual),
96 reference_(std::move(reference)),
97 gradient_(std::move(gradient)),
98 energy_(energy),
99 hessian_(std::move(hessian))
100 {
101 const auto d = reference_.size();
102 if (d == 0 || !reference_.allFinite() || !std::isfinite(energy_) ||
103 gradient_.size() != d || !gradient_.allFinite() ||
104 hessian_.rows() != d || hessian_.cols() != d ||
105 !hessian_.allFinite() ||
106 (hessian_ - hessian_.transpose()).norm() >
107 1e-12 * std::max(1., hessian_.norm()))
108 throw std::invalid_argument(
109 "Finite matching mean reference, gradient and symmetric "
110 "Hessian required");
111 if constexpr (requires { residual_.observations(); })
112 if (residual_.observations() != 0)
113 throw std::invalid_argument(
114 "Quadratic mean requires an empty residual model");
115 hessian_ = M(.5 * (hessian_ + hessian_.transpose()));
116 }
117 std::size_t addObservations(
118 const std::vector<V>& points,
119 const std::vector<PotentialEvaluation>& nativeValues)
120 {
121 const auto transformed = residualValues(points, nativeValues);
122 const auto added = residual_.addObservations(points, transformed);
123 retainNative(points, nativeValues);
124 return added;
125 }
126 void refitLocal(const std::vector<V>& points,
127 const std::vector<PotentialEvaluation>& nativeValues,
128 std::size_t maximumSites)
129 {
130 const auto transformed = residualValues(points, nativeValues);
131 addObservations(points, nativeValues);
132 residual_.refitLocal(points, transformed, maximumSites);
133 }
134 void refit(const std::vector<V>& points,
135 const std::vector<PotentialEvaluation>& nativeValues)
136 requires requires(Model& model,
137 const std::vector<PotentialEvaluation>& values) {
138 model.refit(points, values);
139 }
140 {
141 const auto transformed = residualValues(points, nativeValues);
142 addObservations(points, nativeValues);
143 residual_.refit(points, transformed);
144 }
145 void refitArchivedLocal(const std::vector<V>& targets,
146 const std::vector<V>& required,
147 std::size_t maximumSites)
148 requires requires(Model& model) {
149 model.refitArchivedLocal(targets, required, maximumSites);
150 }
151 {
152 for (const auto& point: targets)
153 validatePoint(point);
154 for (const auto& point: required)
155 validatePoint(point);
156 residual_.refitArchivedLocal(targets, required, maximumSites);
157 }
158 std::vector<PotentialEvaluation> evaluate(const std::vector<V>& points)
159 {
160 for (const auto& point: points)
161 validatePoint(point);
162 auto result = residual_.evaluate(points);
163 if (result.size() != points.size())
164 throw std::runtime_error("Residual prediction count differs");
165 for (std::size_t j = 0; j < points.size(); ++j) {
166 if (!std::isfinite(result[j].energy) ||
167 result[j].gradient.size() != reference_.size() ||
168 !result[j].gradient.allFinite())
169 throw std::runtime_error("Invalid residual mean prediction");
170 const auto background = mean(points[j]);
171 result[j].energy += background.energy;
172 result[j].gradient += background.gradient;
173 if (!std::isfinite(result[j].energy) ||
174 !result[j].gradient.allFinite())
175 throw std::runtime_error("Nonfinite restored mean prediction");
176 }
177 return result;
178 }
180 {
181 validatePoint(point);
182 auto action = residual_.prepareHessian(point);
183 if (!action) return {};
184 return [action = std::move(action), hessian = hessian_](
185 const V& direction, V& result) {
186 if (direction.size() != hessian.rows() || !direction.allFinite())
187 return false;
188 V residual;
189 if (!action(direction, residual) ||
190 residual.size() != hessian.rows() || !residual.allFinite())
191 return false;
192 result = residual + hessian * direction;
193 return result.allFinite();
194 };
195 }
196 auto posteriorPointVariance(const std::vector<V>& points)
197 requires requires(Model& model) {
198 model.posteriorPointVariance(points);
199 }
200 {
201 for (const auto& point: points)
202 validatePoint(point);
203 if constexpr (requires { residual_.fittedObservations(); })
204 if (!points.empty() && residual_.fittedObservations() == 0)
205 throw std::logic_error(
206 "Fit residual observations before variance prediction");
207 return residual_.posteriorPointVariance(points);
208 }
209 bool nativeObservation(const V& point, PotentialEvaluation& value) const
210 {
211 validatePoint(point);
212 const auto* record = findNative(point);
213 if (!record) return false;
214 value = record->value;
215 return true;
216 }
217 bool ownsNativeObservation(const V& point) const
218 {
219 validatePoint(point);
220 return findNative(point) != nullptr;
221 }
222 bool hasObservation(const V& point) const
223 {
224 return ownsNativeObservation(point);
225 }
226 auto observations() const
227 requires requires(const Model& model) { model.observations(); }
228 {
229 return residual_.observations();
230 }
232 requires requires(const Model& model) { model.fittedObservations(); }
233 {
234 return residual_.fittedObservations();
235 }
237 requires requires(const Model& model) {
238 model.observationIdentityComparisons();
239 }
240 {
241 return residual_.observationIdentityComparisons();
242 }
243 bool hasFittedObservation(const V& point) const
244 requires requires(const Model& model) {
245 model.hasFittedObservation(point);
246 }
247 {
248 validatePoint(point);
249 return residual_.hasFittedObservation(point);
250 }
251 double nearestNativeDistance(const V& point) const
252 requires requires(const Model& model) {
253 model.nearestNativeDistance(point);
254 }
255 {
256 validatePoint(point);
257 return residual_.nearestNativeDistance(point);
258 }
260 requires requires(const Model& model) {
261 model.hyperparametersConverged();
262 }
263 {
264 return residual_.hyperparametersConverged();
265 }
267 requires requires(const Model& model) { model.directionalPairTiles(); }
268 {
269 return residual_.directionalPairTiles();
270 }
272 requires requires(const Model& model) {
273 model.hasConditionedResidual();
274 }
275 {
276 return residual_.hasConditionedResidual();
277 }
278};
279} // namespace gpr::quantum
void refit(const std::vector< V > &points, const std::vector< PotentialEvaluation > &nativeValues)
QuadraticMeanGPModel(Model &residual, V reference, double energy, V gradient, M hessian)
bool hasObservation(const V &point) const
bool hasFittedObservation(const V &point) const
auto posteriorPointVariance(const std::vector< V > &points)
std::size_t addObservations(const std::vector< V > &points, const std::vector< PotentialEvaluation > &nativeValues)
std::function< bool(const V &, V &)> HessianAction
void refitArchivedLocal(const std::vector< V > &targets, const std::vector< V > &required, std::size_t maximumSites)
std::vector< PotentialEvaluation > evaluate(const std::vector< V > &points)
bool nativeObservation(const V &point, PotentialEvaluation &value) const
bool ownsNativeObservation(const V &point) const
void refitLocal(const std::vector< V > &points, const std::vector< PotentialEvaluation > &nativeValues, std::size_t maximumSites)
double nearestNativeDistance(const V &point) const
HessianAction prepareHessian(const V &point)
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
STL namespace.