Loading...
Searching...
No Matches
RFF.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// Random Fourier Feature predict-mean path. Three small methods on
6// GaussianProcessRegression that delegate to the underlying RFFModel.
7
8#include <algorithm>
9#include <cmath>
10
12#include "../PhaseTimers.h"
13#include "../RFFModel.h"
14
15namespace gpr {
16
18 int D_rff)
19{
20 // Layered RFF predict: build the random-feature model on the
21 // SCG-trained kernel hyperparameters. Per-fit cost O(N D D_rff +
22 // D_rff^3), per-predict cost O(D_rff D) -- constant in N. The
23 // LAPLACE_SLICE arm's INLA-Delta variance is unchanged; only the
24 // predict-mean cost path is replaced. Caller is expected to call
25 // this after optimize() so magnSigma2 / lengthScale are set, and
26 // before the first calculatePotentialRFF.
27 // ChemGP-tuned BLR noise: noise_e = 1e-6, noise_g = 1e-4 (per
28 // crates/chemgp-core/src/predict.rs build_pred_model defaults).
29 // The C++ GPR's sigma2 / jitter_sigma2 are tuned for the exact-GP
30 // Cholesky (typically sigma2 ~ 1e-7, jitter ~ 1e-6) which is too
31 // tight for the RFF BLR -- the Bayesian posterior mean overfits
32 // the random features and produces noisy gradient predictions
33 // that the dimer's curvature finite-difference step turns into
34 // NaN. Floor the noise at the ChemGP-validated values; if the
35 // GP's own noise estimate is larger (rare), use that.
36 // Defensive: if SCG drifted sigma2 / jitter_sigma2 to non-finite or
37 // negative values, std::max(NaN, x) returns NaN under libstdc++ and
38 // bakes the NaN into the BLR posterior. Substitute the floor before
39 // taking max so the floor is the worst case. The floors live in
40 // rffObservationNoise so the automatic selector uses the same ones.
41 double rff_noise_e = 0.0;
42 double rff_noise_g = 0.0;
43 rffObservationNoise(rff_noise_e, rff_noise_g);
44 // The random-feature regression fits the residual about the prior
45 // mean (ZBL when active), exactly as the exact posterior does; the mean
46 // is added back in calculatePotentialRFF. Without this the RFF model
47 // could not be used under the ZBL prior mean at all. A fixed count
48 // trains on physical residuals. Automatic selection uses the
49 // standardized builder and sets rff_standardized_targets_.
50 const Observation residual = rffResidualObservation(training_data);
51 rff_standardized_targets_ = false;
52 rff_model_.build(*sexpat_cov_func, atoms_config, residual, D_rff,
53 rff_noise_e, rff_noise_g);
54 PhaseTimers::notePeak("rff_build");
55 rff_features_ = D_rff;
56}
57
59{
60 // Pass-through: RFFModel writes the predict mean (energy +
61 // gradient) into image1.E / image1.G via Bayesian linear
62 // regression in the random-feature space. Bumps the GP's call
63 // counter to keep diagnostic parity with the exact-GP predict.
64 rff_model_.calculatePotential(image1);
65 if (rff_standardized_targets_) rffUnstandardizePrediction(image1);
66 addPriorMeanToObservation(image1);
67 ++num_of_potential_calls;
68}
69
71{
72 // Pass-through: RFFModel writes the BLR predictive variance into
73 // image1.E / image1.G. For the LAPLACE_SLICE + INLA-Delta arm the
74 // dispatch routes variance through calculateVarianceLaplaceDelta
75 // instead (calibrated hyperparameter posterior) and only uses the
76 // RFF variance on the bare-GP arm. Kept for completeness so the
77 // RFF path is self-contained.
78 rff_model_.calculateVariance(image1);
79 if (rff_standardized_targets_) {
80 const double scale =
81 (std::isfinite(y_scale_) && y_scale_ > 0.0) ? y_scale_ : 1.0;
82 const double scale_sq = scale * scale;
83 if (scale_sq != 1.0) {
84 if (image1.E.size() > 0) image1.E *= scale_sq;
85 if (image1.G.size() > 0) image1.G *= scale_sq;
86 }
87 }
88}
89
90} // namespace gpr
void calculateVarianceRFF(Observation &image)
Definition RFF.cpp:70
void calculatePotentialRFF(Observation &image)
Definition RFF.cpp:58
void buildRFFModel(const Observation &training_data, int D_rff)
Definition RFF.cpp:17
std::unique_ptr< SexpatCF > sexpat_cov_func
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Observation data: energy, gradient and positions.
FieldMatrixd E
static void notePeak(const char *phase) noexcept