Loading...
Searching...
No Matches
ConsistentForceModel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#ifndef GPRIDGEIZATION_CONSISTENT_FORCE_MODEL_H_
5#define GPRIDGEIZATION_CONSISTENT_FORCE_MODEL_H_
6
7#include <Eigen/Core>
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <stdexcept>
12
13namespace gpr::optim {
14
18inline Eigen::RowVectorXd consistentGradientPrediction(
19 const Eigen::Ref<const Eigen::RowVectorXd>& measured_reference,
20 const Eigen::Ref<const Eigen::RowVectorXd>& predicted_reference,
21 const Eigen::Ref<const Eigen::RowVectorXd>& predicted_trial)
22{
23 if (measured_reference.size() == 0 ||
24 measured_reference.size() != predicted_reference.size() ||
25 measured_reference.size() != predicted_trial.size() ||
26 !measured_reference.allFinite() || !predicted_reference.allFinite() ||
27 !predicted_trial.allFinite()) {
28 throw std::invalid_argument(
29 "consistent gradient prediction requires matching finite "
30 "gradients");
31 }
32 // Subtract the predictions first so a zero step returns the measurement.
33 const Eigen::RowVectorXd predicted_change =
34 predicted_trial - predicted_reference;
35 return measured_reference + predicted_change;
36}
37
42 const Eigen::Ref<const Eigen::RowVectorXd>& measured_change,
43 const Eigen::Ref<const Eigen::RowVectorXd>& predicted_change)
44{
45 if (measured_change.size() == 0 ||
46 measured_change.size() != predicted_change.size() ||
47 !measured_change.allFinite() || !predicted_change.allFinite()) {
48 throw std::invalid_argument(
49 "gradient change agreement requires matching finite gradients");
50 }
51 const double scale = std::max(measured_change.cwiseAbs().maxCoeff(),
52 predicted_change.cwiseAbs().maxCoeff());
53 if (scale == 0.0) return 0.5;
54 // Scale before taking norms to avoid overflow or underflow in squares.
55 const Eigen::RowVectorXd measured = measured_change / scale;
56 const Eigen::RowVectorXd predicted = predicted_change / scale;
57 const double error = (measured - predicted).norm();
58 const double magnitude = std::max(measured.norm(), predicted.norm());
59 return std::clamp(1.0 - error / magnitude, -1.0, 1.0);
60}
61
66inline Eigen::RowVectorXd gradientChangeVariance(
67 const Eigen::MatrixXd& covariance, Eigen::Index dimension,
68 double likelihood_variance)
69{
70 if (dimension <= 0 || covariance.rows() != 2 * (dimension + 1) ||
71 covariance.cols() != covariance.rows() || !covariance.allFinite() ||
72 !std::isfinite(likelihood_variance) || likelihood_variance < 0.0)
73 throw std::invalid_argument("invalid gradient-change covariance");
74 Eigen::RowVectorXd variance(dimension);
75 const double eps = std::numeric_limits<double>::epsilon();
76 const double gamma3 = 3.0 * eps / (1.0 - 3.0 * eps);
77 for (Eigen::Index k = 0; k < dimension; ++k) {
78 const Eigen::Index a = 2 + 2 * k, b = a + 1;
79 const double diagonal = covariance(a, a) + covariance(b, b);
80 const double cross = covariance(a, b) + covariance(b, a);
81 const double latent = diagonal - cross;
82 const double magnitude =
83 std::abs(covariance(a, a)) + std::abs(covariance(b, b)) +
84 std::abs(covariance(a, b)) + std::abs(covariance(b, a));
85 if (latent < -gamma3 * magnitude)
86 throw std::domain_error("negative gradient-change variance");
87 variance(k) = std::max(0.0, latent) + 2.0 * likelihood_variance;
88 }
89 return variance;
90}
91
95inline bool measuredForceChangeCalibrated(double error, double variance,
96 double force_scale) noexcept
97{
98 return std::isfinite(error) && error >= 0.0 && std::isfinite(variance) &&
99 variance >= 0.0 && std::isfinite(force_scale) && force_scale > 0.0 &&
100 error <= std::min(std::sqrt(variance), force_scale);
101}
102
108inline double measuredTrialTrustAgreement(double rho, bool calibrated,
109 double displacement, double radius,
110 double rho_grow)
111{
112 if (!calibrated) return rho;
113 if (radius > 0.0 && displacement >= 0.9 * radius) return 1.0;
114 return std::max(rho, rho_grow);
115}
116} // namespace gpr::optim
117
118#endif // GPRIDGEIZATION_CONSISTENT_FORCE_MODEL_H_
bool measuredForceChangeCalibrated(double error, double variance, double force_scale) noexcept
double measuredTrialTrustAgreement(double rho, bool calibrated, double displacement, double radius, double rho_grow)
Eigen::RowVectorXd gradientChangeVariance(const Eigen::MatrixXd &covariance, Eigen::Index dimension, double likelihood_variance)
Eigen::RowVectorXd consistentGradientPrediction(const Eigen::Ref< const Eigen::RowVectorXd > &measured_reference, const Eigen::Ref< const Eigen::RowVectorXd > &predicted_reference, const Eigen::Ref< const Eigen::RowVectorXd > &predicted_trial)
double gradientChangeAgreement(const Eigen::Ref< const Eigen::RowVectorXd > &measured_change, const Eigen::Ref< const Eigen::RowVectorXd > &predicted_change)