Loading...
Searching...
No Matches
RigidNullDeflation.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#ifndef GPR_ML_GP_RIGIDNULLDEFLATION_H_
5#define GPR_ML_GP_RIGIDNULLDEFLATION_H_
6
7#include <Eigen/Dense>
8#include <cstddef>
9#include <string>
10#include <vector>
11
14
15namespace gpr {
16
20 bool requested = false;
21 bool applied = false;
22 std::string reason = "not_evaluated";
23 Eigen::Index full_rows = 0;
24 Eigen::Index deflated_rows = 0;
25 Eigen::Index rigid_modes = 0;
26 Eigen::Index geometries = 0;
27 double kernel_null_norm = 0.0;
31};
32
33namespace detail {
34
35struct RigidDeflationGeometryBlock {
36 Eigen::Index full_row_offset = 0;
37 Eigen::Index deflated_row_offset = 0;
38 Eigen::Index full_grad_dim = 0;
39 Eigen::Index rigid_modes = 0;
40 Eigen::Index deflated_grad_dim = 0;
41 double grad_noise_variance = 0.0;
42 EigenMatrix R; // D x r_i orthonormal rigid-motion basis
43 EigenMatrix B; // D x d_i orthonormal complement ([R, B] orthogonal)
44};
45
50struct RigidDeflationState {
51 bool active = false;
52 bool factor_order = true;
53 Eigen::Index full_rows = 0;
54 Eigen::Index deflated_rows = 0;
55 Eigen::Index num_geometries = 0;
56 Eigen::Index grad_dim = 0;
57 std::vector<RigidDeflationGeometryBlock> blocks;
58
59 // Closed-form rigid-subspace terms for NLML, gradient, and LOO.
60 double rigid_nlml_logdet_term = 0.0; // sum_i 0.5*r_i*log(2*pi*sigma_i^2)
61 double rigid_logdet_only = 0.0; // sum_i r_i*log(sigma_i^2)
62 double rigid_quad_term = 0.0; // sum_i ||R_i^T g_i||^2 / sigma_i^2
63 double rigid_alpha_sq_norm = 0.0; // sum_i ||R_i^T g_i||^2 / sigma_i^4
64 double rigid_inv_trace = 0.0; // sum_i r_i / sigma_i^2
65 double rigid_inv_sq_trace = 0.0; // sum_i r_i / sigma_i^4
66 double rigid_alpha_weighted_sq_norm = 0.0; // sum_i ||R_i^T g_i||^2 /
67 // sigma_i^6
68 Eigen::VectorXd a_rigid; // U * Sigma_R^{-1} * U^T * y
69
70 void reset() noexcept;
71
72 [[nodiscard]] bool isActiveForFullOrder(
73 Eigen::Index expected_full_rows) const noexcept
74 {
75 return active && full_rows == expected_full_rows;
76 }
77
78 [[nodiscard]] Eigen::Index activeOrderFor(
79 Eigen::Index expected_full_rows) const noexcept
80 {
81 return isActiveForFullOrder(expected_full_rows) ? deflated_rows
82 : expected_full_rows;
83 }
84
85 // Project N x K matrix on the left: T^T * M -> N_r x K.
86 [[nodiscard]] EigenMatrix projectRows(const EigenMatrix& full_mat) const;
87 // Project N-vector: T^T * v -> N_r.
88 [[nodiscard]] Eigen::VectorXd projectVector(
89 const Eigen::VectorXd& full_vec) const;
90 // Project symmetric N x N matrix: T^T * M * T -> N_r x N_r.
91 [[nodiscard]] EigenMatrix projectSymmetric(
92 const EigenMatrix& full_sym) const;
93
94 // Lift N_r x K matrix on the left with zero rigid component: T * M_r -> N x
95 // K.
96 [[nodiscard]] EigenMatrix liftRowsZeroRigid(
97 const EigenMatrix& deflated_mat) const;
98 // Lift N_r-vector with zero rigid component: T * v_r -> N.
99 [[nodiscard]] Eigen::VectorXd liftVectorZeroRigid(
100 const Eigen::VectorXd& deflated_vec) const;
101
102 // Lift full solve result: T * solved_r + U * Sigma_R^{-1} * U^T * rhs_full.
103 [[nodiscard]] Eigen::VectorXd liftSolvedVector(
104 const Eigen::VectorXd& rhs_full, const Eigen::VectorXd& solved_r) const;
105 [[nodiscard]] EigenMatrix liftSolvedColumns(
106 const EigenMatrix& rhs_full, const EigenMatrix& solved_r) const;
107
108 // Lift full inverse: T * inv_r * T^T + U * Sigma_R^{-1} * U^T.
109 [[nodiscard]] EigenMatrix liftInverse(const EigenMatrix& inv_r) const;
110
111 // Rigid-block part of the NLML curvature bilinear form
112 // -1/2 tr(A D_a A D_b) + 1/2 tr(A E_ab)
113 // + (D_a alpha)^T A (D_b alpha) - 1/2 alpha^T E_ab alpha
114 // with A = Sigma_R^{-1}, alpha = a_rigid, and the noise-only derivatives
115 // D_a = first_a * I, D_b = first_b * I, E_ab = second_ab * I on U.
116 // Closed form, so U Sigma_R^{-1} U^T never meets a full-space vector.
117 [[nodiscard]] double rigidCurvature(double first_a, double first_b,
118 double second_ab) const noexcept
119 {
120 return -0.5 * first_a * first_b * rigid_inv_sq_trace +
121 0.5 * second_ab * rigid_inv_trace +
122 first_a * first_b * rigid_alpha_weighted_sq_norm -
123 0.5 * second_ab * rigid_alpha_sq_norm;
124 }
125
126 // Full N-element diagonal of C^{-1} given L_r (factor_order = true).
127 [[nodiscard]] Eigen::VectorXd fullInverseDiagonalFromFactor(
128 const EigenMatrix& lower_r) const;
129
130 // Compute and cache a_rigid and rigid scalar terms from y (in layout
131 // order).
132 void updateTargetStats(const Eigen::VectorXd& y_layout);
133
134 [[nodiscard]] double nlmlPenalty() const noexcept
135 {
136 return rigid_nlml_logdet_term + 0.5 * rigid_quad_term;
137 }
138};
139
143bool buildRigidMotionBasisForGeometry(const Eigen::RowVectorXd& coords,
144 const double cell[9], EigenMatrix& R_out,
145 EigenMatrix& B_out);
146
150bool prepareRigidDeflationState(
151 bool enabled, bool factor_order, const AtomsConfiguration& conf_info,
152 const EigenMatrix& x_layout, const Eigen::VectorXd& x_ind_layout,
153 const Eigen::VectorXd& diag_noise_layout, double uniform_extra_noise,
154 bool unsupported_kernel_or_likelihood, RigidDeflationState& state,
155 RigidDeflationDiagnostic& diag);
156
162bool verifySignalKernelRigidNullspace(const EigenMatrix& signal_kernel,
163 const RigidDeflationState& state,
164 RigidDeflationDiagnostic& diag,
165 double rel_tol = 1e-10);
166
170bool verifyCrossBlockRigidNullspace(const EigenMatrix& cross_kernel,
171 const RigidDeflationState& tail_state,
172 const RigidDeflationState& old_state,
173 RigidDeflationDiagnostic& diag,
174 double rel_tol = 1e-10);
175
176inline constexpr bool kCompiledWithScalapack =
177#ifdef GPR_LINALG_SCALAPACK
178 true;
179#else
180 false;
181#endif
182
187void validateRigidNullDeflationConfiguration(
188 bool use_rigid_null_deflation, bool principled_nugget,
189 bool scalapack_backend = kCompiledWithScalapack);
190
191} // namespace detail
192} // namespace gpr
193
194#endif /* GPR_ML_GP_RIGIDNULLDEFLATION_H_ */
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