Loading...
Searching...
No Matches
StudentTProcessRegression.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
6
7#include <cfloat>
8#include <cmath>
9#include <limits>
10
11#include "gpr/linalg/Backend.h"
12#ifdef GPR_LINALG_KOKKOS
14#endif
15
16namespace gpr {
17
22
24{
25 hp_a_ = a;
26 hp_b_ = b;
27}
28
30 const Eigen::VectorXd& x_ind,
31 const Eigen::VectorXd& y)
32{
33 Index_t n = static_cast<Index_t>(x.rows());
34 double energy = 0.0;
35
36 bool decomposed = decomposeCovarianceMatrix(x, x_ind);
37
38 if (!decomposed) {
39 return std::numeric_limits<double>::quiet_NaN();
40 }
41
43
44 // TP log marginal likelihood (Cholesky-based):
45 // L = log_det + 0.5*(2a + n)*log(1 + S/(2b))
46 double log_det = 0.0;
47 double S = 0.0; // = y^T C^{-1} y
48#ifdef GPR_LINALG_KOKKOS
49 if (hasDevicePosterior()) {
50 const auto parts = device_posterior_->objectiveParts();
51 log_det = parts[0];
52 S = parts[1];
53 } else
54#endif
55 {
56 log_det = backend().log_det_cholesky(L);
57 S = b.dot(b);
58 }
59
60 double edata =
61 log_det + 0.5 * (2.0 * hp_a_ + n) * std::log(1.0 + S / (2.0 * hp_b_));
62
63 // The gradient at this theta weighs its data term with these.
64 data_rows_ = static_cast<double>(n);
65 data_quadratic_ = S;
66
67 // Cache the TP prefactor for variance scaling
68 double n_adj = (n > 2) ? static_cast<double>(n - 2) : 0.0;
69 tp_prefactor_ = (2.0 * hp_b_ + S) / (2.0 * hp_a_ + n_adj);
70
71 // Prior contribution (same as GP)
72 double eprior = 0.0;
73 eprior -= sexpat_cov_func->calculateLogPrior();
74
75 energy = edata + eprior;
76 return energy;
77}
78
80{
81 // d/dtheta of 0.5 (2a + n) log(1 + S / 2b) is -(a + n/2) / (b + S/2) times
82 // the Gaussian data term 0.5 alpha^T dK alpha, with S = y^T C^{-1} y. n and
83 // S are those recorded with the posterior weights this gradient scales.
84 if (data_rows_ <= 0.0) return 1.0;
85 return (hp_a_ + 0.5 * data_rows_) / (hp_b_ + 0.5 * data_quadratic_);
86}
87
89 double quadratic)
90{
91 data_rows_ = rows;
92 data_quadratic_ = quadratic;
93}
94
96{
97 // Base GP variance
99
100 // Scale by TP prefactor
101 image1.E.array() *= tp_prefactor_;
102 image1.G.array() *= tp_prefactor_;
103}
104
106 const Eigen::VectorXd& y_train)
107{
108 // calculateMeanPrediction has just been called upstream, so b = L^{-1} y
109 // is current for theta_k. Recompute S_k and the TP prefactor so the
110 // subsequent calculateVariance (virtual-dispatched back into this class)
111 // uses a prefactor consistent with the current grid point.
112 //
113 // TP prefactor (Shah/Wilson/Ghahramani, eq. 7):
114 // tp_prefactor = (2 hp_b + S) / (2 hp_a + n - 2)
115 // where S = y^T K_y^{-1} y = b.dot(b) with b = L^{-1} y.
116 double S_k = 0.0;
117#ifdef GPR_LINALG_KOKKOS
118 if (hasDevicePosterior()) {
119 S_k = device_posterior_->objectiveParts()[1];
120 } else
121#endif
122 {
123 if (b.size() == 0) {
124 return;
125 }
126 S_k = b.dot(b);
127 }
128 const double n = static_cast<double>(y_train.size());
129 const double n_adj = (n > 2.0) ? (n - 2.0) : 0.0;
130 const double denom = 2.0 * hp_a_ + n_adj;
131 if (!(denom > 0.0)) {
132 return;
133 }
134 tp_prefactor_ = (2.0 * hp_b_ + S_k) / denom;
135}
136
138 const Eigen::VectorXd& y_train, const Eigen::VectorXd& b_local) const
139{
140 if (b_local.size() == 0) return 1.0;
141 return localVariancePrefactorFromQuadratic(y_train, b_local.dot(b_local));
142}
143
145 const Eigen::VectorXd& y_train, const double y_dot_alpha) const
146{
147 if (!std::isfinite(y_dot_alpha)) return 1.0;
148 const double n = static_cast<double>(y_train.size());
149 const double n_adj = (n > 2.0) ? (n - 2.0) : 0.0;
150 const double denom = 2.0 * hp_a_ + n_adj;
151 if (!(denom > 0.0)) return 1.0;
152 return (2.0 * hp_b_ + y_dot_alpha) / denom;
153}
154
155} // namespace gpr
Linear algebra abstraction layer for GPR hot-path operations.
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
Student-t process regression, extending GP with heavy-tailed priors.
linalg::BackendContext & backend() const
virtual void calculateVariance(Observation &image1)
Compute predictive variance for energy and gradient. Corresponds to MATLAB get_variance....
Definition Variance.cpp:353
bool decomposeCovarianceMatrix(const EigenMatrix &x, const Eigen::VectorXd &x_ind)
Evaluate and decompose the covariance matrix using Cholesky decomposition.
void calculateMeanPrediction(const Eigen::VectorXd &y)
Posterior mean prediction w.r.t energy. This function mimics the following step from the MATLAB code:...
Definition Predict.cpp:688
std::shared_ptr< linalg::KokkosMolecularPosterior > device_posterior_
bool hasDevicePosterior() const noexcept
std::unique_ptr< SexpatCF > sexpat_cov_func
double hyperparameterDataTermScale() const override
Weight of the data term in the Student-t gradient.
void recordHyperparameterDataTerm(double rows, double quadratic) override
void refreshVariancePrefactor(const Eigen::VectorXd &y_train) override
Hook called inside calculateVarianceLaplace at each CCD grid point, right after calculateMeanPredicti...
void setHyperpriorParameters(double a, double b)
Set the inverse-gamma hyperprior parameters.
double localVariancePrefactor(const Eigen::VectorXd &y_train, const Eigen::VectorXd &b_local) const override
double evaluateEnergy(const EigenMatrix &x, const Eigen::VectorXd &x_ind, const Eigen::VectorXd &y) override
Evaluate the Student-t negative log marginal posterior.
void calculateVariance(Observation &image1) override
Compute predictive variance for energy and gradient. Corresponds to MATLAB get_variance....
double localVariancePrefactorFromQuadratic(const Eigen::VectorXd &y_train, double y_dot_alpha) const override
double log_det_cholesky(const EigenMatrix &L)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Observation data: energy, gradient and positions.
FieldMatrixd E