Loading...
Searching...
No Matches
TCovSecondOrder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_ML_LAPLACE_TCOV_SECOND_ORDER_H_
6#define GPR_ML_LAPLACE_TCOV_SECOND_ORDER_H_
7
8#include <Eigen/Core>
9#include <cstddef>
10#include <vector>
11
12namespace gpr {
13class GaussianProcessRegression; // fwd
14struct Observation; // fwd (from structures/Structures.h)
15} // namespace gpr
16
17namespace gpr {
18namespace laplace {
19
64public:
72 static constexpr double kTrustTau = 0.5;
73
74 TCovSecondOrder() = default;
75
83 [[nodiscard]] bool compute(const GaussianProcessRegression& gpr,
84 const Eigen::MatrixXd& H_inv);
85
89 Observation& image1);
90
92 [[nodiscard]] bool valid() const noexcept
93 {
94 return valid_;
95 }
96
98 [[nodiscard]] std::size_t nLs() const noexcept
99 {
100 return n_ls_;
101 }
102
104 void invalidate() noexcept
105 {
106 valid_ = false;
107 z_.clear();
108 beta_.clear();
109 beta_full_.clear();
110 K_d_.clear();
111 K_ij_full_.clear();
112 alpha_star_.resize(0);
113 H_inv_.resize(0, 0);
114 n_ls_ = 0;
115 }
116
117private:
121 [[nodiscard]] std::size_t ijFlatIndex(std::size_t i,
122 std::size_t j) const noexcept
123 {
124 // Row-major upper-triangular flattening: index = i*p_eff - i*(i-1)/2 +
125 // (j - i)
126 const std::size_t p_eff = n_ls_ + 1;
127 return i * p_eff - (i * (i - 1)) / 2 + (j - i);
128 }
129
137 static void assembleCrossCovDirectional(
138 const GaussianProcessRegression& gpr, const Eigen::MatrixXd& x_q,
139 const Eigen::VectorXd& q_ind, const Eigen::VectorXd& dir,
140 Eigen::MatrixXd* K_first_out, Eigen::MatrixXd* K_second_out);
141
142 bool valid_{false};
143 std::size_t n_ls_{0}; // number of lengthscale dims
144 Eigen::VectorXd alpha_star_; // MAP alpha = K_star^-1 y
145 Eigen::MatrixXd H_inv_; // p x p Hessian inverse
146 // K_d for d in {0 (magn), 1..n_ls (ls)}; size (n_ls_ + 1) matrices
147 // of shape (m_tr x m_tr) where m_tr = gpr.R_matrix.rows().
148 std::vector<Eigen::MatrixXd> K_d_;
149 // K_{ij} = d^2 K / d theta_i d theta_j for i, j in {0..n_ls},
150 // stored upper-triangular (i <= j). Size p_eff * (p_eff + 1) / 2.
151 std::vector<Eigen::MatrixXd> K_ij_full_;
152 // z_d = K^{-1} K_d alpha_star for d in {0..n_ls}; size (n_ls_ + 1)
153 std::vector<Eigen::VectorXd> z_;
154 // beta_{0,d} (legacy partial-scope): size (n_ls_ + 1). Kept for
155 // API stability; applyCorrection uses beta_full_.
156 std::vector<Eigen::VectorXd> beta_;
157 // beta_{ij} = K^{-1} (K_i z_j + K_j z_i - K_{ij} alpha_star)
158 // for all i <= j in {0..n_ls}. Upper-triangular, size
159 // p_eff * (p_eff + 1) / 2.
160 std::vector<Eigen::VectorXd> beta_full_;
161};
162
163} // namespace laplace
164} // namespace gpr
165
166#endif // GPR_ML_LAPLACE_TCOV_SECOND_ORDER_H_
std::size_t nLs() const noexcept
Number of cached lengthscale dimensions (0..n_ls).
void invalidate() noexcept
Invalidate (drops all cached vectors).
bool compute(const GaussianProcessRegression &gpr, const Eigen::MatrixXd &H_inv)
static constexpr double kTrustTau
bool valid() const noexcept
Is the cache populated and usable?
void applyCorrection(const GaussianProcessRegression &gpr, Observation &image1)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Observation data: energy, gradient and positions.