24 report.
message = std::move(message);
28std::string negativeMessage(
const char* what,
double value,
int point)
30 return std::string(what) +
" " + std::to_string(value) +
" at point " +
31 std::to_string(point) +
32 " is negative beyond round-off of its prior scale";
39 const Eigen::MatrixXd& positions)
41 const Eigen::Index n = positions.rows();
42 const Eigen::Index channels = positions.cols() + 1;
43 Eigen::VectorXd
out(n * channels);
45 const double scale2 = scale * scale;
47 for (Eigen::Index p = 0; p < n; ++p) {
48 const Coord row = positions.row(p);
50 Eigen::VectorXd indices;
55 for (Eigen::Index c = 0; c < channels; ++c)
56 out(c * n + p) = prior(c, c) * scale2;
63 const int energy =
static_cast<int>(options.
energy_unit);
64 const int length =
static_cast<int>(options.
length_unit);
65 return energy >= 0 && energy <= 4 && length >= 0 && length <= 3;
77 const Eigen::MatrixXd& points,
80 if (!knownUnits(options))
85 "non-finite option value");
89 if (points.rows() == 0 || points.cols() != dof || !points.allFinite())
91 "points must be finite rows of the training dimension");
94 "rigid projection needs 3N coordinates");
98 report.
n_points =
static_cast<int>(points.rows());
100 for (Eigen::Index b = 0; b < points.rows(); ++b) {
102 query.
R = points.row(b);
104 if (query.
E.size() < 1 || !std::isfinite(query.
E(0, 0)))
106 "posterior energy variance is not finite");
107 const double raw = query.
E(0, 0);
108 const auto variance =
111 : std::optional<double>(raw);
114 negativeMessage(
"posterior energy variance", raw,
115 static_cast<int>(b)));
116 const double std_dev = std::sqrt(*variance);
126 report.
message =
"posterior energy standard deviation " +
129 " exceeds max_energy_std " +
137 const Eigen::MatrixXd& directions, Eigen::MatrixXd&
out)
139 out.resize(directions.rows(), directions.cols());
140 Eigen::VectorXd product;
141 for (Eigen::Index j = 0; j < directions.cols(); ++j) {
142 if (!action(directions.col(j), product) ||
143 product.size() != directions.rows() || !product.allFinite())
145 out.col(j) = product;
154 Coord R = points.row(b);
158constexpr Eigen::Index kMaxStencilJointDim = 32768;
162double representable(
long double value,
const char* what)
164 const double out =
static_cast<double>(value);
165 if (!std::isfinite(
out))
166 throw std::range_error(std::string(what) +
167 " is not finite in double precision");
168 if (value != 0.0L && std::abs(
out) < std::numeric_limits<double>::min())
169 throw std::range_error(std::string(what) +
170 " underflows the normal double range");
174using LongMatrix = Eigen::Matrix<long double, Eigen::Dynamic, Eigen::Dynamic>;
184Stencil stencilFunctionals(
const Eigen::VectorXd&
x,
185 const Eigen::MatrixXd& left,
186 const Eigen::MatrixXd& right,
double step)
188 const Eigen::Index dof =
x.size();
189 const Eigen::Index kl = left.rows();
190 const Eigen::Index kr = right.rows();
191 const Eigen::Index n = 2 * kr;
192 if (n > kMaxStencilJointDim / (dof + 1))
193 throw std::length_error(
194 "Hessian stencil covariance dimension exceeds memory guard");
196 Eigen::VectorXd norms(kr);
197 Eigen::VectorXd signs = Eigen::VectorXd::Ones(kr);
198 for (Eigen::Index j = 0; j < kr; ++j) {
199 const Eigen::Matrix<long double, 1, Eigen::Dynamic> row =
200 right.row(j).cast<
long double>();
201 const long double norm = row.norm();
202 norms(j) =
static_cast<double>(norm);
203 Eigen::RowVectorXd unit = Eigen::RowVectorXd::Zero(dof);
205 Eigen::Index pivot = 0;
206 row.cwiseAbs().maxCoeff(&pivot);
207 if (row(pivot) < 0.0L) signs(j) = -1.0;
208 unit = (
static_cast<long double>(signs(j)) * (row / norm))
211 positions.row(2 * j) =
x.transpose() + step * unit;
212 positions.row(2 * j + 1) =
x.transpose() - step * unit;
217 LongMatrix w = LongMatrix::Zero(kl * kr, n * (dof + 1));
218 for (Eigen::Index i = 0; i < kl; ++i)
219 for (Eigen::Index j = 0; j < kr; ++j) {
220 const long double scale =
static_cast<long double>(signs(j)) *
221 static_cast<long double>(norms(j)) /
223 for (Eigen::Index c = 0; c < dof; ++c) {
224 const Eigen::Index base = n + c * n;
225 const long double term =
226 scale *
static_cast<long double>(left(i, c));
227 w(i * kr + j, base + 2 * j) += term;
228 w(i * kr + j, base + 2 * j + 1) -= term;
231 return {std::move(positions), std::move(w)};
236Eigen::Matrix<long double, Eigen::Dynamic, 1> stencilPriorRoots(
239 const Eigen::VectorXd marginals = priorMarginals(gp, stencil.positions);
240 Eigen::Matrix<long double, Eigen::Dynamic, 1> roots(marginals.size());
241 for (Eigen::Index c = 0; c < marginals.size(); ++c)
243 std::sqrt(
static_cast<long double>(std::max(marginals(c), 0.0)));
244 return stencil.weights.cwiseAbs() * roots;
250 const long double root = stencilPriorRoots(gp, stencil)(r);
251 return static_cast<double>(root * root);
255 const Stencil& stencil)
257 const Eigen::Index joint_dim = stencil.weights.cols();
260 if (joint.rows() != joint_dim || joint.cols() != joint_dim)
261 throw std::runtime_error(
"posterior covariance has the wrong shape");
262 const LongMatrix& w = stencil.weights;
269 const LongMatrix joint_ld = joint.cast<
long double>();
270 const Eigen::Index rows = w.rows();
271 LongMatrix product(rows, rows);
272 for (Eigen::Index col = 0; col < rows; ++col) {
273 const Eigen::Matrix<long double, Eigen::Dynamic, 1> weighted =
274 joint_ld * w.row(col).transpose();
275 for (Eigen::Index row = 0; row < rows; ++row)
276 product(row, col) = w.row(row).dot(weighted);
278 Eigen::MatrixXd covariance(product.rows(), product.cols());
279 for (Eigen::Index i = 0; i < product.rows(); ++i)
280 for (Eigen::Index j = 0; j < product.cols(); ++j)
282 representable(product(i, j),
"Hessian stencil covariance");
283 return 0.5 * (covariance + covariance.transpose());
288class TrainingRollback {
291 : gp_(gp), training_(training)
294 TrainingRollback(
const TrainingRollback&) =
delete;
295 TrainingRollback& operator=(
const TrainingRollback&) =
delete;
300 gp_.updateModelWithFullData(training_);
304 void release()
noexcept
318 double prior_scale)
noexcept
320 if (!std::isfinite(variance))
return std::nullopt;
321 if (variance >= 0.0)
return variance;
322 if (std::isfinite(prior_scale) && prior_scale > 0.0 &&
330 const Eigen::Index dof = positions.size();
331 if (dof == 0 || dof % 3 != 0)
return Eigen::MatrixXd::Zero(dof, 6);
332 const Eigen::Index atoms = dof / 3;
334 positions, Eigen::VectorXd::Ones(atoms));
335 generators.rightCols<3>() = -generators.rightCols<3>();
336 const double tiny = 64.0 * std::numeric_limits<double>::epsilon() *
337 std::max(generators.rightCols<3>().norm(), 1.0) *
338 std::sqrt(
static_cast<double>(atoms));
339 for (Eigen::Index k = 0; k < 6; ++k) {
340 const double norm = generators.col(k).norm();
342 generators.col(k) /= norm;
344 generators.col(k).setZero();
352 const Eigen::Index dof = positions.size();
353 if (dof == 0 || dof % 3 != 0)
return Eigen::MatrixXd::Identity(dof, dof);
355 coord.row(0) = positions.transpose();
356 const Eigen::MatrixXd basis =
359 if (basis.cols() == 0)
return Eigen::MatrixXd::Zero(dof, dof);
360 if (basis.cols() == dof)
return Eigen::MatrixXd::Identity(dof, dof);
361 return basis * basis.transpose();
370 const Eigen::MatrixXd& points,
372 std::vector<Eigen::MatrixXd>& hessians,
373 Eigen::MatrixXd* residuals)
376 if (!report.
ok())
return report;
377 if (residuals !=
nullptr && points.cols() % 3 != 0)
379 "rigid residuals need 3N coordinates");
380 const Eigen::Index dof = points.cols();
381 std::vector<Eigen::MatrixXd>
out(
static_cast<std::size_t
>(points.rows()));
382 Eigen::MatrixXd residual_rows;
383 if (residuals !=
nullptr) residual_rows.resize(points.rows(), 6);
384 const Eigen::MatrixXd identity = Eigen::MatrixXd::Identity(dof, dof);
385 for (Eigen::Index b = 0; b < points.rows(); ++b) {
386 auto action = prepare(gp, points, b);
387 Eigen::MatrixXd columns;
388 if (!action || !applyAt(action, identity, columns))
390 "no analytic mean Hessian for this model");
391 Eigen::MatrixXd hessian = 0.5 * (columns + columns.transpose());
392 if (residuals !=
nullptr) {
393 const Eigen::MatrixXd generators =
395 for (Eigen::Index k = 0; k < 6; ++k) {
396 const double value = generators.col(k).squaredNorm() > 0.0
397 ? (hessian * generators.col(k)).norm()
399 residual_rows(b, k) = value;
402 peak = std::max(peak, value);
406 const Eigen::MatrixXd P =
408 hessian = P * hessian * P;
409 hessian = 0.5 * (hessian + hessian.transpose()).eval();
411 out[
static_cast<std::size_t
>(b)] = std::move(hessian);
413 hessians = std::move(
out);
414 if (residuals !=
nullptr) *residuals = std::move(residual_rows);
420 const std::vector<Eigen::MatrixXd>& directions,
423 if (directions.size() !=
static_cast<std::size_t
>(points.rows()))
425 "one direction block per point");
426 for (
const auto& block: directions)
427 if (block.rows() == 0 || block.cols() != points.cols() ||
430 "direction blocks must be finite k x d");
432 if (!report.
ok())
return report;
433 std::vector<Eigen::MatrixXd>
out(directions.size());
434 for (Eigen::Index b = 0; b < points.rows(); ++b) {
435 auto action = prepare(gp, points, b);
436 Eigen::MatrixXd vectors =
437 directions[
static_cast<std::size_t
>(b)].transpose();
441 vectors = (P * vectors).eval();
443 Eigen::MatrixXd products;
444 if (!action || !applyAt(action, vectors, products))
446 "no analytic mean Hessian for this model");
448 out[
static_cast<std::size_t
>(b)] = products.transpose();
450 actions = std::move(
out);
456 const std::vector<Eigen::MatrixXd>& left,
458 std::vector<Eigen::MatrixXd>& covariance)
460 const std::size_t count =
static_cast<std::size_t
>(points.rows());
461 if (left.size() != count || right.size() != count)
463 "one left and one right block per point");
464 for (std::size_t b = 0; b < count; ++b)
465 if (left[b].rows() == 0 || right[b].rows() == 0 ||
466 left[b].cols() != points.cols() ||
467 right[b].cols() != points.cols() || !left[b].allFinite() ||
468 !right[b].allFinite())
470 "vector blocks must be finite k x d");
472 if (!report.
ok())
return report;
473 const double step = varianceStep(options);
474 std::vector<Eigen::MatrixXd>
out(count);
475 for (std::size_t b = 0; b < count; ++b) {
476 const Eigen::VectorXd
x = points.row(
static_cast<Eigen::Index
>(b));
477 Eigen::MatrixXd v = left[b];
478 Eigen::MatrixXd w = right[b];
485 const Stencil stencil = stencilFunctionals(
x, v, w, step);
486 Eigen::MatrixXd covariance = stencilCovariance(gp, stencil);
487 for (Eigen::Index r = 0; r < covariance.rows(); ++r) {
488 const double raw = covariance(r, r);
489 if (!(raw < 0.0))
continue;
490 const auto variance =
494 negativeMessage(
"Hessian bilinear variance",
495 raw,
static_cast<int>(b)));
496 covariance(r, r) = *variance;
498 out[b] = std::move(covariance);
499 }
catch (
const std::length_error& e) {
501 }
catch (
const std::range_error& e) {
505 covariance = std::move(
out);
511 const std::vector<Eigen::MatrixXd>& directions,
514 const std::size_t count =
static_cast<std::size_t
>(points.rows());
515 if (directions.size() != count)
517 "one direction block per point");
518 for (
const auto& block: directions)
519 if (block.rows() == 0 || block.cols() != points.cols() ||
522 "direction blocks must be finite k x d");
524 if (!report.
ok())
return report;
525 const double step = varianceStep(options);
526 std::vector<Eigen::VectorXd>
out(count);
527 for (std::size_t b = 0; b < count; ++b) {
528 const Eigen::VectorXd
x = points.row(
static_cast<Eigen::Index
>(b));
529 Eigen::MatrixXd vectors = directions[b];
532 vectors = (vectors * P).eval();
534 out[b].resize(vectors.rows());
535 for (Eigen::Index k = 0; k < vectors.rows(); ++k) {
537 const Stencil stencil =
538 stencilFunctionals(
x, vectors.row(k), vectors.row(k), step);
539 const double raw = stencilCovariance(gp, stencil)(0, 0);
540 const auto variance =
542 raw, stencilPriorScale(gp, stencil, 0))
543 : std::optional<double>(raw);
546 negativeMessage(
"Hessian bilinear variance",
547 raw,
static_cast<int>(b)));
548 out[b](k) = *variance;
549 }
catch (
const std::length_error& e) {
551 }
catch (
const std::range_error& e) {
556 variances = std::move(
out);
562 const std::vector<Eigen::MatrixXd>& weights,
565 const std::size_t count =
static_cast<std::size_t
>(points.rows());
566 if (weights.size() != count)
568 "one weight matrix per point");
569 for (
const auto& A: weights)
570 if (A.rows() != points.cols() || A.cols() != points.cols() ||
572 (A - A.transpose()).cwiseAbs().maxCoeff() >
573 64.0 * std::numeric_limits<double>::epsilon() *
574 std::max(1.0, A.cwiseAbs().maxCoeff()))
576 "weight matrices must be finite, symmetric d x d");
578 if (!report.
ok())
return report;
579 const double step = varianceStep(options);
580 Eigen::VectorXd
out(points.rows());
581 for (std::size_t b = 0; b < count; ++b) {
582 const Eigen::VectorXd
x = points.row(
static_cast<Eigen::Index
>(b));
583 Eigen::MatrixXd A = 0.5 * (weights[b] + weights[b].transpose());
586 A = (P * A * P).eval();
588 const LongMatrix Al = A.cast<
long double>();
589 const Eigen::SelfAdjointEigenSolver<LongMatrix> spectrum(Al);
590 if (spectrum.info() != Eigen::Success)
592 "weight matrix has no eigendecomposition");
593 const long double cut =
595 static_cast<long double>(std::numeric_limits<double>::epsilon()) *
596 spectrum.eigenvalues().cwiseAbs().maxCoeff();
597 std::vector<Eigen::Index> kept;
598 for (Eigen::Index k = 0; k < A.rows(); ++k)
599 if (std::abs(spectrum.eigenvalues()(k)) > cut) kept.push_back(k);
601 out(
static_cast<Eigen::Index
>(b)) = 0.0;
604 const Eigen::Index r =
static_cast<Eigen::Index
>(kept.size());
605 Eigen::MatrixXd modes(r, A.rows());
606 Eigen::Matrix<long double, Eigen::Dynamic, 1> lambda(r);
607 for (Eigen::Index k = 0; k < r; ++k) {
608 const Eigen::Index idx = kept[
static_cast<std::size_t
>(k)];
609 Eigen::Matrix<long double, Eigen::Dynamic, 1> mode =
610 spectrum.eigenvectors().col(idx);
611 Eigen::Index pivot = 0;
612 mode.cwiseAbs().maxCoeff(&pivot);
613 if (mode(pivot) < 0.0L) mode = -mode;
614 modes.row(k) = mode.transpose().cast<
double>();
615 lambda(k) = spectrum.eigenvalues()(idx);
618 const Stencil stencil = stencilFunctionals(
x, modes, modes, step);
619 const Eigen::MatrixXd pairs = stencilCovariance(gp, stencil);
622 long double total = 0.0L;
623 for (Eigen::Index k = 0; k < r; ++k)
624 for (Eigen::Index l = 0; l < r; ++l)
626 lambda(k) * lambda(l) *
627 static_cast<long double>(pairs(k * r + k, l * r + l));
628 const double raw = representable(total,
"Hessian trace variance");
629 std::optional<double> variance(raw);
633 const auto roots = stencilPriorRoots(gp, stencil);
634 long double bound = 0.0L;
635 for (Eigen::Index k = 0; k < r; ++k)
636 bound += std::abs(lambda(k)) * roots(k * r + k);
642 negativeMessage(
"Hessian trace variance", raw,
643 static_cast<int>(b)));
644 out(
static_cast<Eigen::Index
>(b)) = *variance;
645 }
catch (
const std::length_error& e) {
647 }
catch (
const std::range_error& e) {
651 variances = std::move(
out);
656 const Eigen::MatrixXd& points,
658 Eigen::VectorXd& energy_variance,
659 Eigen::MatrixXd& gradient_variance)
662 if (!report.
ok())
return report;
663 Eigen::VectorXd e(points.rows());
664 Eigen::MatrixXd g(points.rows(), points.cols());
665 for (Eigen::Index b = 0; b < points.rows(); ++b) {
667 query.
R = points.row(b);
669 if (query.
E.size() < 1 || query.
G.size() != points.cols() ||
670 !query.
G.allFinite() || !std::isfinite(query.
E(0, 0)))
672 "posterior variance is not finite");
673 e(b) = query.
E(0, 0);
674 g.row(b) = query.
G.row(0);
676 energy_variance = std::move(e);
677 gradient_variance = std::move(g);
682 const Eigen::VectorXd& point,
683 const Eigen::MatrixXd& hessian)
690 gp.clearConditionedHessians();
694 const Eigen::VectorXd& point,
695 Eigen::MatrixXd& hessian_out)
702 const Observation& added,
const Eigen::MatrixXd* fixed_hessian,
705 Eigen::MatrixXd& gradient, Eigen::VectorXd& energy_variance,
706 Eigen::MatrixXd& gradient_variance)
710 const Eigen::Index dof = training.
R.cols();
711 if (added.
R.rows() == 0 || added.
R.cols() != dof ||
712 added.
G.rows() != added.
R.rows() || added.
G.cols() != dof ||
713 added.
E.size() != added.
R.rows() || !added.
R.allFinite() ||
714 !added.
G.allFinite() || !added.
E.allFinite())
716 "added rows need finite positions, energies and "
717 "gradients of the training dimension");
718 if (queries.rows() == 0 || queries.cols() != dof || !queries.allFinite())
720 "queries must be finite rows of the training dimension");
721 if (!knownUnits(options))
726 "non-finite option value");
728 const bool has_hvp = hvp !=
nullptr &&
static_cast<bool>(*hvp);
729 if (hvp !=
nullptr && !has_hvp)
731 "fixed-point HVP callback is empty");
732 if (fixed_hessian !=
nullptr && has_hvp)
734 "specify at most one of fixed_hessian and hvp");
736 Eigen::MatrixXd validated_hessian;
737 const bool has_fixed_curvature = (fixed_hessian !=
nullptr) || has_hvp;
738 if (has_fixed_curvature) {
739 if (added.
R.rows() != 1)
741 "fixed-point Hessian requires a single added point "
743 if (fixed_hessian !=
nullptr) {
744 if (fixed_hessian->rows() != dof || fixed_hessian->cols() != dof)
746 "fixed-point Hessian must have shape d x d");
747 if (!fixed_hessian->allFinite())
749 "fixed-point Hessian must be finite");
750 validated_hessian = *fixed_hessian;
752 validated_hessian.resize(dof, dof);
754 for (Eigen::Index j = 0; j < dof; ++j) {
756 if (!(*hvp)(Eigen::VectorXd::Unit(dof, j), col) ||
757 col.size() != dof || !col.allFinite())
759 "fixed-point HVP callback returned a "
760 "non-finite or mis-sized vector");
761 validated_hessian.col(j) = col;
765 std::max(1.0, validated_hessian.cwiseAbs().maxCoeff());
766 if ((validated_hessian - validated_hessian.transpose())
768 .maxCoeff() > 1e-10 * scale)
771 "fixed-point Hessian or HVP operator is not symmetric");
773 0.5 * (validated_hessian + validated_hessian.transpose()).eval();
778 TrainingRollback rollback(gp, training);
784 "posterior cannot be refactored with the added rows");
787 Eigen::VectorXd e(queries.rows());
788 Eigen::MatrixXd g(queries.rows(), dof);
791 for (Eigen::Index b = 0; b < queries.rows(); ++b) {
793 query.
R = queries.row(b);
795 if (query.
E.size() < 1 || query.
G.size() != dof) {
797 "posterior mean is unavailable");
799 e(b) = query.
E(0, 0);
800 g.row(b) = query.
G.row(0);
807 if (!report.
ok())
return report;
809 training = std::move(combined);
810 if (has_fixed_curvature)
818 "a query is outside the conditioned posterior's "
819 "support; the rows were appended";
822 energy = std::move(e);
823 gradient = std::move(g);
824 energy_variance = std::move(ve);
825 gradient_variance = std::move(vg);
831 const Eigen::MatrixXd& queries,
833 Eigen::VectorXd& energy,
834 Eigen::MatrixXd& gradient,
835 Eigen::VectorXd& energy_variance,
836 Eigen::MatrixXd& gradient_variance)
839 gp, training, added,
static_cast<const Eigen::MatrixXd*
>(
nullptr),
840 static_cast<const HessianAction*
>(
nullptr), queries, options, energy,
841 gradient, energy_variance, gradient_variance);
846 const Observation& added,
const Eigen::MatrixXd& fixed_hessian,
848 Eigen::VectorXd& energy, Eigen::MatrixXd& gradient,
849 Eigen::VectorXd& energy_variance, Eigen::MatrixXd& gradient_variance)
853 options, energy, gradient, energy_variance,
861 Eigen::VectorXd& energy, Eigen::MatrixXd& gradient,
862 Eigen::VectorXd& energy_variance, Eigen::MatrixXd& gradient_variance)
865 gp, training, added,
static_cast<const Eigen::MatrixXd*
>(
nullptr), &hvp,
866 queries, options, energy, gradient, energy_variance, gradient_variance);
Some auxiliary functions.
void assembleMatrixOfRepetitiveCoordinates(const gpr::Coord &coord, gpr::EigenMatrix &matrix, Eigen::VectorXd &ind)
Assemble Eigen matrix with repetitive rows based on provided set of coordinates R.
const Observation & fittedObservations() const noexcept
The observations the current posterior is fitted on.
void evaluateCovarianceMatrix(const EigenMatrix &x1, const EigenMatrix &x2, const Eigen::VectorXd &x1_ind, const Eigen::VectorXd &x2_ind, EigenMatrix &C, bool serial_per_rank=false) const
Evaluate covariance matrix between two input vectors.
void calculateVarianceDispatched(Observation &image1)
std::function< bool(const Eigen::VectorXd &, Eigen::VectorXd &)> MeanHessianAction
bool isDecomposedSuccessfully() const noexcept
void setConditionedHessian(const Eigen::VectorXd &point, const Eigen::MatrixXd &hessian)
void calculatePotentialDispatched(Observation &image1)
void updateModelWithFullData(const Observation &full_observation)
void calculateObservationPosteriorCovariance(const EigenMatrix &positions, EigenMatrix &K_post, bool allow_hssvd_local=true)
MeanHessianAction prepareAnalyticMeanHessian(const Coord &R)
bool appendFixedHyperparameters(const Observation &full_observation)
double outputScale() const noexcept
bool findConditionedHessian(const Eigen::VectorXd &point, Eigen::MatrixXd &hessian_out) const
const AtomsConfiguration & getAtomsConfig() const
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Eigen::MatrixXd physicalCurvatureBasis(const gpr::Coord &positions, const gpr::AtomsConfiguration &config, bool exclude_rotations=true, RigidMotionMetric metric=RigidMotionMetric::MassWeighted)
Eigen::MatrixXd massWeightedRigidMotions(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses)
DerivativeReport posteriorVariances(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const DerivativeOptions &options, Eigen::VectorXd &energy_variance, Eigen::MatrixXd &gradient_variance)
Posterior energy and gradient variance at the rows of points.
DerivativeReport conditionFixed(GaussianProcessRegression &gp, Observation &training, const Observation &added, const Eigen::MatrixXd *fixed_hessian, const HessianAction *hvp, const Eigen::MatrixXd &queries, const DerivativeOptions &options, Eigen::VectorXd &energy, Eigen::MatrixXd &gradient, Eigen::VectorXd &energy_variance, Eigen::MatrixXd &gradient_variance)
Condition on added and optionally record an exact Hessian or Hessian-vector-product callback at the f...
void setConditionedHessian(GaussianProcessRegression &gp, const Eigen::VectorXd &point, const Eigen::MatrixXd &hessian)
Record a user-supplied exact Hessian conditioned at point.
DerivativeReport hessianBilinearCovariance(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const std::vector< Eigen::MatrixXd > &left, const std::vector< Eigen::MatrixXd > &right, const DerivativeOptions &options, std::vector< Eigen::MatrixXd > &covariance)
Posterior covariance of M_ij = v_i^T H w_j at each point. left[b] holds the v_i as rows and right[b] ...
DerivativeReport hessianTraceVariance(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const std::vector< Eigen::MatrixXd > &weights, const DerivativeOptions &options, Eigen::VectorXd &variances)
Posterior variance of tr(A dH) at each point for a symmetric A per point, the same estimate as hessia...
DerivativeReport meanHessianActions(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const std::vector< Eigen::MatrixXd > &directions, const DerivativeOptions &options, std::vector< Eigen::MatrixXd > &actions)
Posterior-mean Hessian actions. directions[b] holds k rows, each one vector at point b; actions[b] re...
void clearConditionedHessians(GaussianProcessRegression &gp) noexcept
Clear all conditioned exact Hessians stored on gp.
constexpr double kNegativeVarianceTolerance
Relative tolerance under which a negative posterior variance is binary64 round-off.
DerivativeReport hessianBilinearVariances(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const std::vector< Eigen::MatrixXd > &directions, const DerivativeOptions &options, std::vector< Eigen::VectorXd > &variances)
Diagonal posterior variances of v_i^T H v_i at each point. Computes one stencil functional at a time,...
Eigen::MatrixXd rigidGenerators(const Eigen::VectorXd &positions)
Unit rigid generators of 3N coordinates as columns: three translations, then rotations about the x,...
constexpr double defaultVarianceStep() noexcept
Default finite-difference step for posterior curvature variance.
Eigen::MatrixXd rigidProjector(const Eigen::VectorXd &positions, const AtomsConfiguration &config)
Orthogonal projector onto the physical vibrational complement of the rigid motions of positions under...
std::function< bool(const Eigen::VectorXd &direction, Eigen::VectorXd &hv)> HessianAction
Exact Hessian-vector-product callback at a fixed point: writes H(x) * v into hv and returns true on s...
bool findConditionedHessian(const GaussianProcessRegression &gp, const Eigen::VectorXd &point, Eigen::MatrixXd &hessian_out)
Look up a conditioned exact Hessian at point.
DerivativeReport meanHessians(GaussianProcessRegression &gp, const Eigen::MatrixXd &points, const DerivativeOptions &options, std::vector< Eigen::MatrixXd > &hessians, Eigen::MatrixXd *residuals)
Full posterior-mean Hessians at the rows of points, symmetrized. residuals, when given,...
DerivativeStatus
Outcome of a derivative primitive. The C API maps each value to one status code; nothing here returns...
@ Unsupported
The fitted mean has no analytic Hessian contraction.
std::optional< double > roundoffVariance(double variance, double prior_scale) noexcept
variance when it is finite and nonnegative, zero when it is negative by at most kNegativeVarianceTole...
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Stores configurations of all atoms in the system.
Observation data: energy, gradient and positions.
void append(const Observation &other)
Options for batched posterior derivative primitives.
Report returned by a batched posterior derivative primitive.
double max_translation_residual
bool ok() const noexcept
True when the call succeeded.
double max_rotation_residual