Loading...
Searching...
No Matches
gpr::laplace::ScopedKernelState Class Reference

#include <gpr/ml/laplace/ScopedKernelState.h>

Public Member Functions

 ScopedKernelState (SexpatCF &kernel, double &sigma2, LikGaussian *likelihood=nullptr) noexcept
 ~ScopedKernelState () noexcept
void restore () noexcept
 ScopedKernelState (const ScopedKernelState &)=delete
 ScopedKernelState (ScopedKernelState &&)=delete
ScopedKernelState & operator= (const ScopedKernelState &)=delete
ScopedKernelState & operator= (ScopedKernelState &&)=delete
void applyTheta (const Eigen::VectorXd &theta)

Detailed Description

RAII guard: snapshots (magnSigma2, lengthScales, sigma2) at construction and restores them at destruction.

Used by Laplace and SVGD grid/particle loops to mutate hyperparameters without leaking rank-local state across early returns.

Non-copyable, non-movable: the guard owns a fixed scope.

Definition at line 22 of file ScopedKernelState.h.

Constructor & Destructor Documentation

◆ ScopedKernelState() [1/3]

gpr::laplace::ScopedKernelState::ScopedKernelState ( SexpatCF & kernel,
double & sigma2,
LikGaussian * likelihood = nullptr )
inlinenoexcept
Parameters
kernelSexpAt covariance function whose magn + ls to snapshot
sigma2reference to the noise variance member in the GPR
likelihoodoptional Gaussian likelihood whose noise is theta-owned

Definition at line 28 of file ScopedKernelState.h.

30 : kernel_(kernel),
31 sigma2_ref_(sigma2),
32 likelihood_(likelihood),
33 saved_magn_(kernel.getMagnSigma2()),
34 saved_ls_(kernel.getLengthScaleRef()),
35 saved_sigma2_(sigma2),
36 saved_likelihood_sigma2_(
37 likelihood != nullptr ? likelihood->getSigma2() : 0.0)
38 {
39 }

◆ ~ScopedKernelState()

gpr::laplace::ScopedKernelState::~ScopedKernelState ( )
inlinenoexcept

Definition at line 41 of file ScopedKernelState.h.

42 {
43 restore();
44 }

◆ ScopedKernelState() [2/3]

gpr::laplace::ScopedKernelState::ScopedKernelState ( const ScopedKernelState & )
delete

◆ ScopedKernelState() [3/3]

gpr::laplace::ScopedKernelState::ScopedKernelState ( ScopedKernelState && )
delete

Member Function Documentation

◆ applyTheta()

void gpr::laplace::ScopedKernelState::applyTheta ( const Eigen::VectorXd & theta)
inline

Apply a theta vector to the kernel state. theta = (log magn, log ls_1, ..., log ls_nls, log noise).

Definition at line 68 of file ScopedKernelState.h.

69 {
70 if (theta.size() < 2) return;
71 kernel_.setMagnSigma2(std::exp(theta(0)));
72 auto& ls = kernel_.getLengthScaleRef();
73 const int n_ls = static_cast<int>(ls.size());
74 const int n_cov = 1 + n_ls;
75 for (int i = 0; i < n_ls && (1 + i) < theta.size(); ++i) {
76 ls(0, i) = std::exp(theta(1 + i));
77 }
78 if (theta.size() > n_cov) {
79 const double noise = std::exp(theta(theta.size() - 1));
80 sigma2_ref_ = noise;
81 if (likelihood_ != nullptr) {
82 likelihood_->setSigma2(noise);
83 }
84 }
85 }

◆ operator=() [1/2]

ScopedKernelState & gpr::laplace::ScopedKernelState::operator= ( const ScopedKernelState & )
delete

◆ operator=() [2/2]

ScopedKernelState & gpr::laplace::ScopedKernelState::operator= ( ScopedKernelState && )
delete

◆ restore()

void gpr::laplace::ScopedKernelState::restore ( )
inlinenoexcept

Restore the snapshot mid-scope. Called automatically by the dtor; exposed publicly so callers that need the snapshot's state side-effects (e.g. re-decomposing the covariance matrix at theta_star while still owning the guard) don't have to drop the guard. Idempotent.

Definition at line 51 of file ScopedKernelState.h.

52 {
53 kernel_.setMagnSigma2(saved_magn_);
54 kernel_.getLengthScaleRef() = saved_ls_;
55 sigma2_ref_ = saved_sigma2_;
56 if (likelihood_ != nullptr) {
57 likelihood_->setSigma2(saved_likelihood_sigma2_);
58 }
59 }

The documentation for this class was generated from the following file: