Loading...
Searching...
No Matches
RobustRows.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4//
5// Robust training rows: a Student-t observation model per configuration,
6// fitted as a Gaussian scale mixture by EM (Kuss 2006; Tipping & Lawrence
7// 2005). Each configuration i contributes d_i channels (energy and
8// gradient). With y_i | f, lambda_i ~ N(f_i, Sigma / lambda_i) and
9// lambda_i ~ Gamma(nu/2, nu/2), the E-step gives
10//
11// w_i = E[lambda_i | y] = (nu + d_i) / (nu + z_i^2),
12//
13// where z_i^2 is the squared Mahalanobis norm of the configuration's
14// leave-one-geometry-out residual under its predictive covariance, and the
15// M-step is the Gaussian model with the configuration's noise scaled by
16// 1 / w_i, i.e. extra noise sigma2 (z_i^2 - d_i) / (nu + d_i) on every
17// channel, floored at zero. Derivation: scripts/derive_robust_row_weights.py;
18// proofs/lean/GPRidge/RobustRowWeight.lean. A row the model cannot
19// explain (an oracle wall or hole, a clashing interpolation seed) is
20// downweighted by its own misfit; no threshold is involved.
21//
22// For a Gaussian model with training covariance K and weights alpha =
23// K^-1 y, the leave-one-geometry-out residual of the rows B of geometry i
24// is r_B = (K^-1)_BB^-1 alpha_B with covariance (K^-1)_BB^-1, so
25// z_i^2 = alpha_B^T (K^-1)_BB^-1 alpha_B (Sundararajan & Keerthi 2001,
26// block form).
27#include <algorithm>
28#include <cmath>
29#include <limits>
30#include <vector>
31
33#include "../PhaseTimers.h"
34
35namespace gpr {
36
38 const Observation& full_observation)
39{
40 if (!(robust_nu_ > 0.0) || !lik_gaussian) return false;
41 const Eigen::Index n_geom = full_observation.R.rows();
42 const Eigen::Index dim = full_observation.R.cols();
43 if (n_geom == 0 || R_matrix.rows() == 0 || R_matrix.cols() != dim + 1 ||
44 energy_and_gradient.size() != R_matrix.rows())
45 return false;
46 const Eigen::Index n = R_matrix.rows();
47 // The training covariance in the assembled row order, with the
48 // likelihood noise (the current extra noise included), the constant
49 // block, and the jitter the conditioning uses.
52 /*serial_per_rank=*/true,
53 /*include_constant=*/true);
54 if (K.rows() != n || K.cols() != n || !K.allFinite()) return false;
55 if (jitter_sigma2 > 0.0) K.diagonal().array() += jitter_sigma2;
56 applyKernelNugget(K, n);
57 Eigen::LLT<EigenMatrix> llt(K);
58 if (llt.info() != Eigen::Success) return false;
59 const EigenMatrix& L_dense = llt.matrixLLT();
60 const Eigen::VectorXd alpha = llt.solve(energy_and_gradient);
61 if (!alpha.allFinite()) return false;
62
63 const double sigma2 = lik_gaussian->getSigma2();
64 Eigen::VectorXd z2 = Eigen::VectorXd::Zero(n_geom);
65 Eigen::VectorXd extra = Eigen::VectorXd::Zero(n_geom);
66 Eigen::VectorXd channels = Eigen::VectorXd::Zero(n_geom);
67 std::vector<Eigen::Index> rows;
68 for (Eigen::Index g = 0; g < n_geom; ++g) {
69 rows.clear();
70 for (Eigen::Index i = 0; i < n; ++i) {
71 if ((R_matrix.row(i).head(dim).array() ==
72 full_observation.R.row(g).array())
73 .all())
74 rows.push_back(i);
75 }
76 const Eigen::Index d = static_cast<Eigen::Index>(rows.size());
77 if (d == 0) continue;
78 channels(g) = static_cast<double>(d);
79 // Z = L^-1 E_B, so (K^-1)_BB = Z^T Z.
80 EigenMatrix E = EigenMatrix::Zero(n, d);
81 Eigen::VectorXd alpha_B(d);
82 for (Eigen::Index k = 0; k < d; ++k) {
83 E(rows[static_cast<std::size_t>(k)], k) = 1.0;
84 alpha_B(k) = alpha(rows[static_cast<std::size_t>(k)]);
85 }
86 const EigenMatrix Z =
87 L_dense.template triangularView<Eigen::Lower>().solve(E);
88 const EigenMatrix Kinv_BB = Z.transpose() * Z;
89 Eigen::LLT<EigenMatrix> block(Kinv_BB);
90 if (block.info() != Eigen::Success) continue;
91 const double norm = alpha_B.dot(block.solve(alpha_B));
92 if (!std::isfinite(norm) || norm < 0.0) continue;
93 z2(g) = norm;
94 extra(g) = std::max(0.0, sigma2 * (norm - static_cast<double>(d)) /
95 (robust_nu_ + static_cast<double>(d)));
96 }
97 const Eigen::VectorXd& previous = lik_gaussian->geometryExtraNoise();
98 const bool changed = previous.size() != n_geom ||
99 (previous - extra).cwiseAbs().maxCoeff() >
100 std::numeric_limits<double>::epsilon() *
101 (1.0 + extra.cwiseAbs().maxCoeff());
102 robust_z2_ = z2;
103 robust_extra_ = extra;
104 lik_gaussian->setGeometryExtraNoise(full_observation.R, extra);
105 if (report_level >= 1) {
106 Eigen::Index downweighted = 0;
107 for (Eigen::Index g = 0; g < n_geom; ++g)
108 if (extra(g) > 0.0) ++downweighted;
109 Eigen::Index worst = 0;
110 z2.maxCoeff(&worst);
111 log_man << "Robust rows: nu=" << robust_nu_ << ", " << downweighted
112 << "/" << n_geom
113 << " configurations above their expected "
114 "leave-one-geometry-out misfit; worst z2="
115 << z2(worst) << " over " << channels(worst)
116 << " channels (weight "
117 << (robust_nu_ + channels(worst)) / (robust_nu_ + z2(worst))
118 << ", extra noise " << extra(worst) << ")\n";
119 }
120 return changed;
121}
122
123} // namespace gpr
bool updateRobustRowWeights(const Observation &full_observation)
void evaluateTrainingCovarianceMatrix(const EigenMatrix &x, const Eigen::VectorXd &x_ind, EigenMatrix &cov_matrix, bool serial_per_rank=false, bool include_constant=true, bool include_noise=true)
Evaluate training covariance matrix (gp_cov + noise covariance).
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Observation data: energy, gradient and positions.