Loading...
Searching...
No Matches
gpr::ConditionedKernel Class Referencefinal

#include <gpr/covariance_functions/ConditionedKernel.h>

Inheritance diagram for gpr::ConditionedKernel:

Classes

struct  State

Public Member Functions

 ConditionedKernel (std::shared_ptr< const KernelOperator > kernel, Coord anchors, Eigen::VectorXd noise)
 ~ConditionedKernel () override
CrossCovarianceFunction prepareCrossCovariance (const Eigen::MatrixXd &training) const override
MeanHessianFunction prepareMeanHessianVector (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
MeanHessianPreparation prepareMeanHessian (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
MeanFunction prepareMean (const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override
Eigen::MatrixXd covarianceBlock (const Eigen::MatrixXd &left, const Eigen::MatrixXd &right, Eigen::Index left_channel, Eigen::Index right_channel) const override
 Covariance between one channel at each set of geometry rows.
Eigen::MatrixXd jointEnergyGradientCrossCovariance (const Eigen::MatrixXd &left, const Eigen::MatrixXd &right) const override
 Cross covariance in Observation order on each operand.
Eigen::MatrixXd jointEnergyGradientCovariance (const Eigen::MatrixXd &positions) const override
 Joint covariance in Observation order [E; G_col0; ...; G_colD-1].
Eigen::VectorXd jointEnergyGradientDiagonal (const Eigen::MatrixXd &positions) const override
 Marginal prior variances in the same channel order as the joint matrix.
Eigen::VectorXd applyJointCrossCovariance (const Eigen::MatrixXd &left, const Eigen::MatrixXd &right, const Eigen::VectorXd &weights) const override
bool meanHessianVector (const Coord &training, const Eigen::VectorXd &weights, const Eigen::VectorXd &position, const Eigen::VectorXd &direction, Eigen::VectorXd &action) const override
MeanCurvature meanCurvature () const noexcept override
Public Member Functions inherited from gpr::KernelOperator
virtual ~KernelOperator ()=default
virtual JointCovarianceTileFunction prepareJointCovarianceTiles (const Eigen::MatrixXd &) const
virtual Eigen::VectorXd energyDiagonal (const Eigen::MatrixXd &positions) const
 Energy self covariance at each geometry, without an all-pairs matrix.
Eigen::MatrixXd energyCorrelationDistances (const Eigen::MatrixXd &left_positions, const Eigen::MatrixXd &right_positions) const

Friends

class ConditionalKernelProjection
RefusalAwareHessianPreparation makeRefusalAwareKernelMeanHessian (std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training, const Eigen::VectorXd &weights, bool *all_exact=nullptr, bool *has_exact_or_refusal=nullptr)
 Prepare a fitted kernel Hessian action that propagates exact-child refusals through composite operators.

Additional Inherited Members

Public Types inherited from gpr::KernelOperator
enum class  MeanHessianStatus { Computed , Refused , Unavailable }
enum class  MeanCurvature { Unknown , Exact , Refused }
using MeanFunction = std::function<Eigen::VectorXd(const Eigen::MatrixXd&)>
using CrossCovarianceFunction
using JointCovarianceTileFunction
using MeanHessianFunction
using HessianAction
using MeanHessianPreparation
using MeanHessianMatrixFunction

Detailed Description

Condition an additive kernel on zero energy/gradient observations at fixed anchors. Anchor variances express the permitted residual at measured rows. The factor belongs to this fitted covariance operator, not to a query.

Definition at line 95 of file ConditionedKernel.h.

Constructor & Destructor Documentation

◆ ConditionedKernel()

gpr::ConditionedKernel::ConditionedKernel ( std::shared_ptr< const KernelOperator > kernel,
Coord anchors,
Eigen::VectorXd noise )

Definition at line 117 of file ConditionedKernel.cpp.

120 : state_(std::make_unique<State>())
121{
122 if (!kernel || anchors.rows() == 0 || anchors.cols() == 0 ||
123 !anchors.allFinite() ||
124 noise.size() != anchors.rows() * (anchors.cols() + 1) ||
125 !noise.allFinite() || (noise.array() < 0.0).any())
126 throw std::invalid_argument("invalid conditioned kernel anchors");
127 Eigen::MatrixXd lower = kernel->jointEnergyGradientCovariance(anchors);
128 lower.diagonal() += noise;
129 const bool factored =
132 : Eigen::LLT<Eigen::Ref<Eigen::MatrixXd>>(lower).info() ==
133 Eigen::Success;
134 if (!factored)
135 throw std::invalid_argument("conditioned kernel anchors are singular");
136 state_->lower = linalg::CholeskyPanels::fromLower(lower);
137 state_->kernel = std::move(kernel);
138 state_->anchors = std::move(anchors);
139 state_->anchor_covariance =
140 makeKernelCrossCovariance(state_->kernel, state_->anchors);
141}
static CholeskyPanels fromLower(const Eigen::MatrixXd &lower)
bool trainingCholeskyLower(ProductMatrix lower)
bool trainingCholeskySelected(Eigen::Index order)
KernelOperator::CrossCovarianceFunction makeKernelCrossCovariance(std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training)

◆ ~ConditionedKernel()

gpr::ConditionedKernel::~ConditionedKernel ( )
overridedefault

Member Function Documentation

◆ applyJointCrossCovariance()

Eigen::VectorXd gpr::ConditionedKernel::applyJointCrossCovariance ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Contract the joint cross covariance with coefficients in [E; G] order. Operators can apply their feature contractions without a dense tensor.

Reimplemented from gpr::KernelOperator.

Definition at line 216 of file ConditionedKernel.cpp.

219{
220 return prepareMean(right, weights)(left);
221}
MeanFunction prepareMean(const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override

◆ covarianceBlock()

Eigen::MatrixXd gpr::ConditionedKernel::covarianceBlock ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions,
Eigen::Index left_channel,
Eigen::Index right_channel ) const
nodiscardoverridevirtual

Covariance between one channel at each set of geometry rows.

Implements gpr::KernelOperator.

Definition at line 202 of file ConditionedKernel.cpp.

205{
206 const auto& state = *state_;
207 const Eigen::MatrixXd la =
208 channelRows(state.anchor_covariance(left), left.rows(), left_channel);
209 const Eigen::MatrixXd ra = channelRows(state.anchor_covariance(right),
210 right.rows(), right_channel);
211 return state.kernel->covarianceBlock(left, right, left_channel,
212 right_channel) -
213 la * state.solve(ra.transpose());
214}

◆ jointEnergyGradientCovariance()

Eigen::MatrixXd gpr::ConditionedKernel::jointEnergyGradientCovariance ( const Eigen::MatrixXd & positions) const
nodiscardoverridevirtual

Joint covariance in Observation order [E; G_col0; ...; G_colD-1].

Reimplemented from gpr::KernelOperator.

Definition at line 175 of file ConditionedKernel.cpp.

177{
178 const auto& state = *state_;
179 const Eigen::MatrixXd anchor_query =
180 state.anchor_covariance(positions).transpose();
181 const Eigen::MatrixXd weights = state.solve(anchor_query);
182 return conditionalCovariance(
183 state.kernel->jointEnergyGradientCovariance(positions), anchor_query,
184 weights);
185}

◆ jointEnergyGradientCrossCovariance()

Eigen::MatrixXd gpr::ConditionedKernel::jointEnergyGradientCrossCovariance ( const Eigen::MatrixXd & left_positions,
const Eigen::MatrixXd & right_positions ) const
nodiscardoverridevirtual

Cross covariance in Observation order on each operand.

Reimplemented from gpr::KernelOperator.

Definition at line 163 of file ConditionedKernel.cpp.

165{
166 if (detail::matrixExactlyEqual(left, right))
168 const auto& state = *state_;
169 const Eigen::MatrixXd la = state.anchor_covariance(left);
170 const Eigen::MatrixXd ar = state.anchor_covariance(right).transpose();
171 return state.kernel->jointEnergyGradientCrossCovariance(left, right) -
172 la * state.solve(ar);
173}
Eigen::MatrixXd jointEnergyGradientCovariance(const Eigen::MatrixXd &positions) const override
Joint covariance in Observation order [E; G_col0; ...; G_colD-1].

◆ jointEnergyGradientDiagonal()

Eigen::VectorXd gpr::ConditionedKernel::jointEnergyGradientDiagonal ( const Eigen::MatrixXd & positions) const
nodiscardoverridevirtual

Marginal prior variances in the same channel order as the joint matrix.

Reimplemented from gpr::KernelOperator.

Definition at line 187 of file ConditionedKernel.cpp.

189{
190 const auto& state = *state_;
191 const Eigen::MatrixXd anchor_query =
192 state.anchor_covariance(positions).transpose();
193 const Eigen::MatrixXd weights = state.solve(anchor_query);
194 return state.kernel->jointEnergyGradientDiagonal(positions) -
195 (anchor_query.array() * weights.array())
196 .colwise()
197 .sum()
198 .transpose()
199 .matrix();
200}

◆ meanCurvature()

KernelOperator::MeanCurvature gpr::ConditionedKernel::meanCurvature ( ) const
nodiscardoverridevirtualnoexcept

Reimplemented from gpr::KernelOperator.

Definition at line 304 of file ConditionedKernel.cpp.

305{
306 return state_->kernel->meanCurvature();
307}

◆ meanHessianVector()

bool gpr::ConditionedKernel::meanHessianVector ( const Coord & training,
const Eigen::VectorXd & weights,
const Eigen::VectorXd & position,
const Eigen::VectorXd & direction,
Eigen::VectorXd & action ) const
nodiscardoverridevirtual

Hessian action of the scalar kernel expansion with joint [E; G] training coefficients. False denotes an unsupported derivative.

Reimplemented from gpr::KernelOperator.

Definition at line 280 of file ConditionedKernel.cpp.

285{
286 if (auto prepare = prepareMeanHessian(training, weights)) {
287 const auto apply = prepare(position);
288 return apply && apply(direction, action);
289 }
290 const auto& state = *state_;
291 const Eigen::VectorXd anchor_weights =
292 state.solve(state.kernel->applyJointCrossCovariance(state.anchors,
293 training, weights));
294 Eigen::VectorXd correction;
295 if (!state.kernel->meanHessianVector(training, weights, position, direction,
296 action) ||
297 !state.kernel->meanHessianVector(state.anchors, anchor_weights,
298 position, direction, correction))
299 return false;
300 action -= correction;
301 return action.allFinite();
302}
MeanHessianPreparation prepareMeanHessian(const Eigen::MatrixXd &training, const Eigen::VectorXd &weights) const override

◆ prepareCrossCovariance()

KernelOperator::CrossCovarianceFunction gpr::ConditionedKernel::prepareCrossCovariance ( const Eigen::MatrixXd & training) const
nodiscardoverridevirtual

Immutable observation functionals for a fixed right operand. Query geometries are evaluated afresh; an empty function requests fallback.

Reimplemented from gpr::KernelOperator.

Definition at line 151 of file ConditionedKernel.cpp.

152{
153 const auto& state = *state_;
154 const Eigen::MatrixXd weights =
155 state.solve(state.anchor_covariance(training).transpose());
156 return [cross = makeKernelCrossCovariance(state.kernel, training),
157 anchors = state.anchor_covariance,
158 weights](const Eigen::MatrixXd& query) -> Eigen::MatrixXd {
159 return cross(query) - anchors(query) * weights;
160 };
161}

◆ prepareMean()

KernelOperator::MeanFunction gpr::ConditionedKernel::prepareMean ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Immutable fitted coefficients with fresh evaluation at every query. An empty function requests the owning generic covariance product.

Reimplemented from gpr::KernelOperator.

Definition at line 223 of file ConditionedKernel.cpp.

225{
226 const auto [positions, combined] = meanExpansion(training, weights);
227 return makeKernelMean(state_->kernel, positions, combined);
228}
KernelOperator::MeanFunction makeKernelMean(std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training, const Eigen::VectorXd &weights)
Owning fitted mean, using the kernel's representation or its exact product.

◆ prepareMeanHessian()

KernelOperator::MeanHessianPreparation gpr::ConditionedKernel::prepareMeanHessian ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Own the fitted mean coefficients and construct a scoped linear Hessian operator at each requested geometry. No query is retained by the fitted model. An empty preparation requests the directional path.

Reimplemented from gpr::KernelOperator.

Definition at line 238 of file ConditionedKernel.cpp.

240{
241 // Exact only when the child prepares its own Hessian. The generic
242 // wrapper is not an analytic contraction.
243 const auto [positions, combined] = meanExpansion(training, weights);
244 return state_->kernel->prepareMeanHessian(positions, combined);
245}

◆ prepareMeanHessianVector()

KernelOperator::MeanHessianFunction gpr::ConditionedKernel::prepareMeanHessianVector ( const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights ) const
nodiscardoverridevirtual

Owning fitted derivative coefficients; query geometry is evaluated afresh. An empty function requests the generic scalar-kernel action.

Reimplemented from gpr::KernelOperator.

Definition at line 230 of file ConditionedKernel.cpp.

232{
233 const auto [positions, combined] = meanExpansion(training, weights);
234 return makeAnalyticKernelMeanHessianVector(state_->kernel, positions,
235 combined);
236}
KernelOperator::MeanHessianFunction makeAnalyticKernelMeanHessianVector(const std::shared_ptr< const KernelOperator > &kernel, const Eigen::MatrixXd &training, const Eigen::VectorXd &weights)

◆ ConditionalKernelProjection

friend class ConditionalKernelProjection
friend

Definition at line 136 of file ConditionedKernel.h.

◆ makeRefusalAwareKernelMeanHessian

RefusalAwareHessianPreparation makeRefusalAwareKernelMeanHessian ( std::shared_ptr< const KernelOperator > kernel,
const Eigen::MatrixXd & training,
const Eigen::VectorXd & weights,
bool * all_exact = nullptr,
bool * has_exact_or_refusal = nullptr )
friend

Prepare a fitted kernel Hessian action that propagates exact-child refusals through composite operators.

Definition at line 167 of file KernelOperator.cpp.

171{
172 if (!kernel || training.cols() <= 0 || !training.allFinite() ||
173 weights.size() != training.rows() * (training.cols() + 1) ||
174 !weights.allFinite())
175 throw std::invalid_argument("invalid fitted kernel Hessian");
176 if (const auto* conditioned =
177 dynamic_cast<const ConditionedKernel*>(kernel.get())) {
178 const auto [positions, combined] =
179 conditioned->meanExpansion(training, weights);
180 return makeRefusalAwareKernelMeanHessian(conditioned->childKernel(),
181 positions, combined, all_exact,
182 has_exact_or_refusal);
183 }
184 const auto& current = *kernel;
185 if (const auto* sum = dynamic_cast<const Sum*>(kernel.get());
186 sum != nullptr && typeid(current) == typeid(Sum)) {
187 struct SummandTerm {
189 bool all_exact = false;
190 bool has_exact_or_refusal = false;
191 };
192 std::vector<SummandTerm> terms;
193 terms.reserve(sum->summands().size());
194 bool every_exact = !sum->summands().empty();
195 bool any_exact_or_refusal = false;
196 for (const auto& child: sum->summands()) {
197 if (!child)
198 throw std::invalid_argument("invalid fitted kernel Hessian");
199 SummandTerm term;
201 child, training, weights, &term.all_exact,
202 &term.has_exact_or_refusal);
203 every_exact = every_exact && term.all_exact;
204 any_exact_or_refusal =
205 any_exact_or_refusal || term.has_exact_or_refusal;
206 terms.push_back(std::move(term));
207 }
208 if (all_exact != nullptr) *all_exact = every_exact;
209 if (has_exact_or_refusal != nullptr)
210 *has_exact_or_refusal = any_exact_or_refusal;
211 const Eigen::Index dof = training.cols();
212 return [terms = std::move(terms), dof, any_exact_or_refusal](
213 const Eigen::VectorXd& position,
214 bool& exact_refusal) -> RefusalAwareHessianAction {
215 if (position.size() != dof || !position.allFinite()) {
216 if (any_exact_or_refusal) exact_refusal = true;
217 return {};
218 }
219 struct PreparedAction {
221 bool has_exact_or_refusal = false;
222 };
223 std::vector<PreparedAction> actions;
224 actions.reserve(terms.size());
225 bool query_failed = false;
226 for (const auto& term: terms) {
227 bool child_refused = false;
228 auto action = term.prepare(position, child_refused);
229 if (child_refused) exact_refusal = true;
230 if (!action) {
231 query_failed = true;
232 if (term.has_exact_or_refusal) exact_refusal = true;
233 } else {
234 actions.push_back(
235 {std::move(action), term.has_exact_or_refusal});
236 }
237 }
238 if (query_failed) return {};
239 return [actions = std::move(actions), dof, any_exact_or_refusal](
240 const Eigen::VectorXd& direction,
241 Eigen::VectorXd& action, bool& exact_refusal) {
242 if (direction.size() != dof || !direction.allFinite()) {
243 if (any_exact_or_refusal) exact_refusal = true;
244 return false;
245 }
246 ExtendedVector result = ExtendedVector::Zero(dof);
247 bool failed = false;
248 for (const auto& prepared: actions) {
249 Eigen::VectorXd term;
250 bool child_refused = false;
251 if (!prepared.apply(direction, term, child_refused) ||
252 term.size() != dof || !term.allFinite()) {
253 failed = true;
254 if (child_refused || prepared.has_exact_or_refusal)
255 exact_refusal = true;
256 } else if (!failed) {
257 result += term.cast<long double>();
258 }
259 }
260 if (failed) return false;
261 action = result.cast<double>();
262 if (!action.allFinite()) {
263 if (any_exact_or_refusal) exact_refusal = true;
264 return false;
265 }
266 return true;
267 };
268 };
269 }
270 // Refused is the kernel's answer. A subclass that still prepares a
271 // Hessian does not turn that answer into a product.
272 if (kernel->meanCurvature() == KernelOperator::MeanCurvature::Refused) {
273 if (all_exact != nullptr) *all_exact = false;
274 if (has_exact_or_refusal != nullptr) *has_exact_or_refusal = true;
275 const Eigen::Index dof = training.cols();
276 return [dof](const Eigen::VectorXd& position,
277 bool& exact_refusal) -> RefusalAwareHessianAction {
278 (void)position;
279 (void)dof;
280 exact_refusal = true;
281 return {};
282 };
283 }
284 if (auto prepare = kernel->prepareMeanHessian(training, weights)) {
285 if (all_exact != nullptr) *all_exact = true;
286 if (has_exact_or_refusal != nullptr) *has_exact_or_refusal = true;
287 const Eigen::Index dof = training.cols();
288 return [prepare = std::move(prepare), dof](
289 const Eigen::VectorXd& position,
290 bool& exact_refusal) -> RefusalAwareHessianAction {
291 if (position.size() != dof || !position.allFinite()) {
292 exact_refusal = true;
293 return {};
294 }
295 auto action = prepare(position);
296 if (!action) {
297 exact_refusal = true;
298 return {};
299 }
300 return [action = std::move(action), dof](
301 const Eigen::VectorXd& direction,
302 Eigen::VectorXd& result, bool& exact_refusal) {
303 if (direction.size() != dof || !direction.allFinite() ||
304 !action(direction, result) || result.size() != dof ||
305 !result.allFinite()) {
306 exact_refusal = true;
307 return false;
308 }
309 return true;
310 };
311 };
312 }
313 const bool leaf_refuses =
314 kernel->meanCurvature() != KernelOperator::MeanCurvature::Unknown;
315 if (all_exact != nullptr) *all_exact = false;
316 if (has_exact_or_refusal != nullptr) *has_exact_or_refusal = leaf_refuses;
317 const auto fitted = makeKernelMeanHessianVector(kernel, training, weights);
318 const Eigen::Index dof = training.cols();
319 return [fitted, dof, leaf_refuses](
320 const Eigen::VectorXd& position,
321 bool& exact_refusal) -> RefusalAwareHessianAction {
322 if (position.size() != dof || !position.allFinite()) {
323 if (leaf_refuses) exact_refusal = true;
324 return {};
325 }
326 return [fitted, position, dof, leaf_refuses](
327 const Eigen::VectorXd& direction, Eigen::VectorXd& result,
328 bool& exact_refusal) {
329 if (direction.size() != dof || !direction.allFinite()) {
330 if (leaf_refuses) exact_refusal = true;
331 return false;
332 }
333 bool applied = false;
334 try {
335 applied = fitted(position, direction, result);
336 } catch (const geometry::UndefinedInternalCoordinate&) {
337 exact_refusal = true;
338 return false;
339 }
340 if (!applied || result.size() != dof || !result.allFinite()) {
341 if (leaf_refuses) exact_refusal = true;
342 return false;
343 }
344 return true;
345 };
346 };
347}
ConditionedKernel(std::shared_ptr< const KernelOperator > kernel, Coord anchors, Eigen::VectorXd noise)
friend RefusalAwareHessianPreparation makeRefusalAwareKernelMeanHessian(std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training, const Eigen::VectorXd &weights, bool *all_exact, bool *has_exact_or_refusal)
Prepare a fitted kernel Hessian action that propagates exact-child refusals through composite operato...
KernelOperator::MeanHessianFunction makeKernelMeanHessianVector(std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training, const Eigen::VectorXd &weights)
std::function< bool( const Eigen::VectorXd &, Eigen::VectorXd &, bool &exact_refusal)> RefusalAwareHessianAction
Hessian action that reports whether an exact child refused.
std::function< RefusalAwareHessianAction( const Eigen::VectorXd &, bool &exact_refusal)> RefusalAwareHessianPreparation
Query preparation that reports whether an exact child refused.

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