Loading...
Searching...
No Matches
gpr::bo::ScaledEuclideanDistance Class Reference

Squared Euclidean distance with one length scale, or one per coordinate. More...

#include <gpr/bo/StationaryKernel.h>

Public Member Functions

 ScaledEuclideanDistance (Eigen::VectorXd length_scales)
 ScaledEuclideanDistance (Eigen::VectorXd length_scales, std::vector< bool > categorical)
 Distance with unordered coordinates.
template<typename Left, typename Right>
double squared (const Left &left, const Right &right) const
const std::vector< bool > & categorical () const noexcept
 Unordered-coordinate flags. Empty: every coordinate is ordered.
const Eigen::VectorXd & lengthScales () const noexcept
 The stored length scales: one, or one per coordinate.

Detailed Description

Squared Euclidean distance with one length scale, or one per coordinate.

Definition at line 32 of file StationaryKernel.h.

Constructor & Destructor Documentation

◆ ScaledEuclideanDistance() [1/2]

gpr::bo::ScaledEuclideanDistance::ScaledEuclideanDistance ( Eigen::VectorXd length_scales)
inlineexplicit

Definition at line 34 of file StationaryKernel.h.

35 : length_scales_(std::move(length_scales))
36 {
37 if (length_scales_.size() == 0) {
38 throw std::invalid_argument("length scales are empty");
39 }
40 for (Eigen::Index i = 0; i < length_scales_.size(); ++i) {
41 if (!(length_scales_[i] > 0.0) ||
42 !std::isfinite(length_scales_[i])) {
43 throw std::invalid_argument("length scale must be positive");
44 }
45 }
46 }

◆ ScaledEuclideanDistance() [2/2]

gpr::bo::ScaledEuclideanDistance::ScaledEuclideanDistance ( Eigen::VectorXd length_scales,
std::vector< bool > categorical )
inline

Distance with unordered coordinates.

A coordinate whose categorical entry is true holds a label. Two rows are then zero apart on that coordinate when the labels are equal and one length-scale unit apart otherwise, so the term is (a != b) / l^2. That is the squared Euclidean distance of a one-hot encoding up to a factor two in l^2, so a radial kernel of it stays positive definite. categorical has one entry per length scale.

Definition at line 58 of file StationaryKernel.h.

60 : ScaledEuclideanDistance(std::move(length_scales))
61 {
62 categorical_ = std::move(categorical);
63 if (!categorical_.empty() &&
64 static_cast<Eigen::Index>(categorical_.size()) !=
65 length_scales_.size()) {
66 throw std::invalid_argument(
67 "categorical flags do not match the length scales");
68 }
69 }
const std::vector< bool > & categorical() const noexcept
Unordered-coordinate flags. Empty: every coordinate is ordered.
ScaledEuclideanDistance(Eigen::VectorXd length_scales)

Member Function Documentation

◆ categorical()

const std::vector< bool > & gpr::bo::ScaledEuclideanDistance::categorical ( ) const
inlinenodiscardnoexcept

Unordered-coordinate flags. Empty: every coordinate is ordered.

Definition at line 102 of file StationaryKernel.h.

103 {
104 return categorical_;
105 }

◆ lengthScales()

const Eigen::VectorXd & gpr::bo::ScaledEuclideanDistance::lengthScales ( ) const
inlinenodiscardnoexcept

The stored length scales: one, or one per coordinate.

Definition at line 108 of file StationaryKernel.h.

109 {
110 return length_scales_;
111 }

◆ squared()

template<typename Left, typename Right>
double gpr::bo::ScaledEuclideanDistance::squared ( const Left & left,
const Right & right ) const
inline

One stored scale applies to every coordinate. Otherwise the count matches the vector.

Definition at line 74 of file StationaryKernel.h.

75 {
76 const Eigen::Index count = left.size();
77 if (right.size() != count) {
78 throw std::invalid_argument("distance operands differ");
79 }
80 if (length_scales_.size() != 1 && length_scales_.size() != count) {
81 throw std::invalid_argument(
82 "length scales do not match the vector");
83 }
84 double radius = 0.0;
85 for (Eigen::Index dim = 0; dim < count; ++dim) {
86 const double scale = length_scales_.size() == 1
87 ? length_scales_[0]
88 : length_scales_[dim];
89 const double delta =
90 !categorical_.empty() &&
91 categorical_[static_cast<std::size_t>(dim)]
92 ? (left.coeff(dim) != right.coeff(dim) ? 1.0 : 0.0)
93 : left.coeff(dim) - right.coeff(dim);
94 const double scaled = delta / scale;
95 radius += scaled * scaled;
96 }
97 return radius;
98 }

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