Loading...
Searching...
No Matches
LaplacePosteriorTypes.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6// TU-private definitions of the four Laplace cache types nested under
7// GaussianProcessRegression. Forward-declared in the public header; the
8// out-of-class definitions live here so the .cpp split (Predict.cpp,
9// Variance.cpp, Laplace.cpp) can share them without re-pasting the
10// 200-line struct bodies.
11
12#include <Eigen/Core>
13#include <cstdint>
14#include <vector>
15
19#include "../LaplaceINLA.h"
20#include "../MapObjective.h"
21
22namespace gpr {
23
25 std::uint64_t model_revision = 0;
26 int mpi_rank = 0;
27 int mpi_size = 1;
30 bool zbl_mean = false;
31 bool ff_mean = false;
32 std::shared_ptr<const geometry::InternalCoordinateChart> chart;
33 std::shared_ptr<const GeodesicHeatEnergyForceKernel> heat;
34 double sigma2_value = 0.0;
35 double likelihood_sigma2 = 0.0;
36 double jitter_sigma2_value = 0.0;
37 double sexpat_pair_cutoff = 0.0;
40 Eigen::Index R_rows = 0;
41 Eigen::Index R_cols = 0;
42 Eigen::Index R_indices_size = 0;
43 Eigen::Index y_train_size = 0;
44 Eigen::VectorXd theta_star;
45 Eigen::VectorXd const_parameters;
46 Eigen::VectorXd lik_parameters;
47 Eigen::Vector3d sexpat_gaussian_prior = Eigen::Vector3d::Zero();
48 Eigen::Vector3d sexpat_sqrtt_prior = Eigen::Vector3d::Zero();
49
50 [[nodiscard]] bool validForPosterior() const noexcept
51 {
52 return theta_star.size() > 0 && R_rows > 0 && R_cols > 0 &&
54 }
55
56 [[nodiscard]] bool matches(const LaplacePosteriorStamp& other) const
57 {
58 using detail::matrixExactlyEqual;
59 return model_revision == other.model_revision &&
60 mpi_rank == other.mpi_rank && mpi_size == other.mpi_size &&
64 zbl_mean == other.zbl_mean && ff_mean == other.ff_mean &&
65 chart == other.chart && heat == other.heat &&
66 sigma2_value == other.sigma2_value &&
71 atom_n_pt == other.atom_n_pt && R_rows == other.R_rows &&
72 R_cols == other.R_cols &&
74 y_train_size == other.y_train_size &&
75 matrixExactlyEqual(theta_star, other.theta_star) &&
76 matrixExactlyEqual(const_parameters, other.const_parameters) &&
77 matrixExactlyEqual(lik_parameters, other.lik_parameters) &&
78 matrixExactlyEqual(sexpat_gaussian_prior,
79 other.sexpat_gaussian_prior) &&
80 matrixExactlyEqual(sexpat_sqrtt_prior, other.sexpat_sqrtt_prior);
81 }
82};
83
86 int mpi_rank = 0;
87 int mpi_size = 1;
89 double sigma2_value = 0.0;
90 double likelihood_sigma2 = 0.0;
91 double jitter_sigma2_value = 0.0;
93 Eigen::VectorXd theta_star;
94 Eigen::VectorXd const_parameters;
95 Eigen::VectorXd lik_parameters;
96 Eigen::Vector3d sexpat_gaussian_prior = Eigen::Vector3d::Zero();
97 Eigen::Vector3d sexpat_sqrtt_prior = Eigen::Vector3d::Zero();
99 Eigen::VectorXd R_indices_snapshot;
100 Eigen::VectorXd y_train;
113
114 [[nodiscard]] bool matches(const LaplacePosteriorSignature& other) const
115 {
116 using detail::matrixExactlyEqual;
117 return stamp.matches(other.stamp) && mpi_rank == other.mpi_rank &&
118 mpi_size == other.mpi_size &&
121 sigma2_value == other.sigma2_value &&
124 atom_n_pt == other.atom_n_pt &&
125 matrixExactlyEqual(theta_star, other.theta_star) &&
126 matrixExactlyEqual(const_parameters, other.const_parameters) &&
127 matrixExactlyEqual(lik_parameters, other.lik_parameters) &&
128 matrixExactlyEqual(sexpat_gaussian_prior,
129 other.sexpat_gaussian_prior) &&
130 matrixExactlyEqual(sexpat_sqrtt_prior,
131 other.sexpat_sqrtt_prior) &&
132 matrixExactlyEqual(R_matrix_snapshot, other.R_matrix_snapshot) &&
133 matrixExactlyEqual(R_indices_snapshot,
134 other.R_indices_snapshot) &&
135 matrixExactlyEqual(y_train, other.y_train) &&
136 matrixExactlyEqual(atom_positions, other.atom_positions) &&
137 matrixExactlyEqual(atom_is_frozen, other.atom_is_frozen) &&
138 matrixExactlyEqual(atom_id, other.atom_id) &&
139 matrixExactlyEqual(atom_atomic_nrs, other.atom_atomic_nrs) &&
140 matrixExactlyEqual(atom_type, other.atom_type) &&
141 matrixExactlyEqual(atom_pairtype, other.atom_pairtype) &&
142 matrixExactlyEqual(atoms_froz_active_positions,
144 matrixExactlyEqual(atoms_froz_active_type,
145 other.atoms_froz_active_type) &&
146 matrixExactlyEqual(atoms_froz_inactive_positions,
148 matrixExactlyEqual(atoms_froz_inactive_type,
150 matrixExactlyEqual(atoms_mov_positions,
151 other.atoms_mov_positions) &&
152 matrixExactlyEqual(atoms_mov_type, other.atoms_mov_type);
153 }
154};
155
162
185
188 std::vector<Eigen::VectorXd> theta_samples;
189 std::vector<Eigen::VectorXd> alpha_samples;
190 // Lower-triangular Cholesky factor of K(theta_k) + jitter, cached
191 // per accepted sample. Used by the proper law-of-total-variance
192 // estimator in calculateVarianceLaplaceSlice.
193 std::vector<EigenMatrix> L_samples;
194 // Per-sample posterior weights, summing to 1. Empty for the uniform
195 // slice arm; populated by the PSIS arm with self-normalised
196 // importance weights.
197 std::vector<double> sample_weights;
198 // Pareto k_hat diagnostic from the PSIS arm (Vehtari 2024).
199 double k_hat = 0.0;
200 bool psis_reliable = true;
201 // Delta-method INLA cache (Rue 2009 sec 3.2). Replaces the LOTV
202 // sampling loop with the analytical formula
203 // Var[mu(x*) | y] = Var_MAP(x*) + g(x*)^T H^{-1} g(x*)
204 // when delta_method is set.
205 bool delta_method = false;
207 std::vector<Eigen::VectorXd> dalpha_dtheta;
209 int n_accepted = 0;
210 int n_stuck = 0;
211 int n_steps = 0;
212 bool valid = false;
213};
214
215} // namespace gpr
INLA-style Laplace approximation of the GP/TP hyperparameter posterior.
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
Eigen::Matrix< int, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixi
Definition Macros.h:27
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Definition Macros.h:29
FieldMatrixd Coord
Definition Macros.h:35
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< uint8_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixU8
Definition Macros.h:31
GPModelProfile
Definition Structures.h:201
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Stores configurations of all atoms in the system.
Definition Structures.h:832
bool matches(const LaplacePosteriorSignature &other) const
std::shared_ptr< const geometry::InternalCoordinateChart > chart
std::shared_ptr< const GeodesicHeatEnergyForceKernel > heat
bool matches(const LaplacePosteriorStamp &other) const