Loading...
Searching...
No Matches
HyperparameterPosterior.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#include <algorithm>
6#include <iterator>
7#include <memory>
8#include <utility>
9
12
13namespace gpr {
14
15namespace {
16
17// Moving coordinates belong to the conditioning rows. Atom roles, frozen
18// geometry and the periodic descriptor domain define the random function.
19bool sameDescriptorDomain(const AtomsConfiguration& lhs,
20 const AtomsConfiguration& rhs)
21{
22 using detail::matrixExactlyEqual;
23 return lhs.n_pt == rhs.n_pt && lhs.pair_cutoff == rhs.pair_cutoff &&
24 lhs.pair_cutoff_width == rhs.pair_cutoff_width &&
25 std::equal(std::begin(lhs.cell), std::end(lhs.cell),
26 std::begin(rhs.cell)) &&
27 matrixExactlyEqual(lhs.atomicNrs, rhs.atomicNrs) &&
28 matrixExactlyEqual(lhs.is_frozen, rhs.is_frozen) &&
29 matrixExactlyEqual(lhs.type, rhs.type) &&
30 matrixExactlyEqual(lhs.pairtype, rhs.pairtype) &&
31 matrixExactlyEqual(lhs.atoms_mov.type, rhs.atoms_mov.type) &&
32 matrixExactlyEqual(lhs.atoms_froz_active.type,
33 rhs.atoms_froz_active.type) &&
34 matrixExactlyEqual(lhs.atoms_froz_active.positions,
35 rhs.atoms_froz_active.positions) &&
36 matrixExactlyEqual(lhs.atoms_froz_inactive.type,
37 rhs.atoms_froz_inactive.type) &&
38 matrixExactlyEqual(lhs.atoms_froz_inactive.positions,
39 rhs.atoms_froz_inactive.positions);
40}
41
42} // namespace
43
46 Eigen::VectorXd mode;
47 std::vector<Eigen::VectorXd> points;
48 Eigen::VectorXd weights;
54 double mean = 0.0;
55 double scale = 1.0;
56 double likelihood_variance = 0.0;
57 double constant_variance = 0.0;
58 double jitter_variance = 0.0;
59 bool zbl_mean = false;
60 bool ff_mean = false;
62 std::shared_ptr<const geometry::InternalCoordinateChart> chart;
63 std::shared_ptr<const GeodesicHeatEnergyForceKernel> heat;
64};
65
66std::span<const Eigen::VectorXd>
68{
69 return state_ ? std::span<const Eigen::VectorXd>(state_->points)
70 : std::span<const Eigen::VectorXd>();
71}
72
73std::span<const double>
75{
76 return state_ ? std::span<const double>(
77 state_->weights.data(),
78 static_cast<std::size_t>(state_->weights.size()))
79 : std::span<const double>();
80}
81
84{
85 if ((uncertainty_mode_ != UncertaintyMode::EB &&
86 uncertainty_mode_ != UncertaintyMode::LAPLACE) ||
87 !ensureLaplacePosterior())
88 return {};
90}
91
94{
96 if (uncertainty_mode_ != UncertaintyMode::EB &&
97 uncertainty_mode_ != UncertaintyMode::LAPLACE)
98 return result;
99 if (!pending_hyperparameter_posterior_.empty() &&
100 hyperparameterPosteriorCompatible(pending_hyperparameter_posterior_))
101 return pending_hyperparameter_posterior_;
102 if (!laplace_posterior_ || !laplace_posterior_->valid ||
103 !laplace_posterior_->signature.stamp.matches(
104 currentLaplacePosteriorStamp()))
105 return result;
106 auto state = std::make_shared<HyperparameterPosterior::State>();
107 state->source = this;
108 state->mode = packInlaHyperparameters();
109 state->laplace = laplace_posterior_->laplace;
110 state->length_scales = lengthScaleFitState();
111 state->atoms = atoms_config;
112 state->kernel_atoms = sexpat_cov_func->getConfInfo();
113 state->profile = model_profile_;
114 state->mean = y_mean_;
115 state->scale = y_scale_;
116 state->likelihood_variance = likelihoodNoiseVariance();
117 state->constant_variance = const_cov_fun->getConstSigma2();
118 state->jitter_variance = jitter_sigma2;
119 state->zbl_mean = use_zbl_prior_mean_;
120 state->ff_mean = use_ff_prior_mean_;
121 state->learns_likelihood_noise = theta_includes_likelihood_noise_;
122 state->chart = sexpat_cov_func->manifoldChart();
123 state->heat = sexpat_cov_func->geodesicHeatKernel();
124 for (const auto& factor: laplace_posterior_->factors) {
125 if (factor.valid && factor.weight > 0.0)
126 state->points.push_back(factor.theta);
127 }
128 state->weights.resize(static_cast<Eigen::Index>(state->points.size()));
129 Eigen::Index index = 0;
130 for (const auto& factor: laplace_posterior_->factors) {
131 if (factor.valid && factor.weight > 0.0)
132 state->weights(index++) = factor.weight;
133 }
134 result.state_ = std::move(state);
135 return result;
136}
137
138bool GaussianProcessRegression::hyperparameterPosteriorCompatible(
139 const HyperparameterPosterior& posterior) const
140{
141 using detail::matrixExactlyEqual;
142 if (!posterior.state_ || !sexpat_cov_func || !const_cov_fun) return false;
143 const auto& source = *posterior.state_;
144 if (source.source != this &&
145 (source.chart || sexpat_cov_func->usesManifoldChart()))
146 return false;
147 if (!matrixExactlyEqual(source.mode, packInlaHyperparameters()) ||
148 source.length_scales != lengthScaleFitState() ||
149 !sameDescriptorDomain(source.atoms, atoms_config) ||
150 !sameDescriptorDomain(source.kernel_atoms,
151 sexpat_cov_func->getConfInfo()) ||
152 source.profile != model_profile_ ||
153 source.zbl_mean != use_zbl_prior_mean_ ||
154 source.ff_mean != use_ff_prior_mean_ ||
155 source.learns_likelihood_noise != theta_includes_likelihood_noise_ ||
156 source.chart != sexpat_cov_func->manifoldChart() ||
157 source.heat != sexpat_cov_func->geodesicHeatKernel() ||
158 source.mean != y_mean_ || source.scale != y_scale_ ||
159 source.likelihood_variance != likelihoodNoiseVariance() ||
160 source.constant_variance != const_cov_fun->getConstSigma2() ||
161 source.jitter_variance != jitter_sigma2)
162 return false;
163 return true;
164}
165
168{
169 if (!hyperparameterPosteriorCompatible(posterior)) return false;
170 pending_hyperparameter_posterior_ = posterior;
171 laplace_posterior_.reset();
172 ++laplace_model_revision_;
173 return true;
174}
175
176bool GaussianProcessRegression::buildHyperparameterPosterior(
177 const HyperparameterPosterior& posterior,
178 const LaplacePosteriorSignature& signature,
179 LaplacePosteriorState& candidate)
180{
181 if (!hyperparameterPosteriorCompatible(posterior) ||
182 !signature.stamp.validForPosterior())
183 return false;
184 const auto& source = *posterior.state_;
185 if (!buildUniformParticlePosteriorState(source.points, signature,
186 candidate))
187 return false;
188 for (std::size_t k = 0; k < candidate.factors.size(); ++k) {
189 if (!candidate.factors[k].valid) return false;
190 candidate.factors[k].weight =
191 source.weights(static_cast<Eigen::Index>(k));
192 }
193 candidate.laplace = source.laplace;
194 return true;
195}
196
197} // namespace gpr
std::span< const Eigen::VectorXd > points() const noexcept
LengthScaleFitState lengthScaleFitState() const
bool conditionOnHyperparameterPosterior(const HyperparameterPosterior &posterior)
HyperparameterPosterior currentHyperparameterPosterior() const
Retain an existing, current quadrature without estimating a new one.
std::unique_ptr< SexpatCF > sexpat_cov_func
HyperparameterPosterior retainHyperparameterPosterior()
Laplace / INLA uncertainty quantifier over GP/TP hyperparameters.
Definition LaplaceINLA.h:73
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
GPModelProfile
Definition Structures.h:201
Stores configurations of all atoms in the system.
Definition Structures.h:832
std::shared_ptr< const geometry::InternalCoordinateChart > chart
std::shared_ptr< const GeodesicHeatEnergyForceKernel > heat