Class Bandwidth
Defined in File Bandwidth.hpp
Nested Relationships
Nested Types
Class Documentation
-
class Bandwidth
An inverse squared bandwidth matrix, and the operations that use it as a metric.
Holds an n x n inverse squared bandwidth matrix M (units of inverse length squared), which defines the squared distance between two points as d^T M d, d = x1 - x2. M acts as an inverse covariance matrix: for a diagonal bandwidth h_i in each dimension, its diagonal entries are 1 / h_i^2; in general it may be any symmetric positive semi-definite matrix, and need not be diagonal.
Some dimensions may be unspecified: a coordinate that is NaN in either point contributes no separation. With s the specified and f the unspecified dimensions, the distance minimized over the unspecified coordinates – equivalently, the metric of the Gaussian marginalized over them – is given not by the s-s block of M but by its Schur complement M_ss - M_sf M_ff^-1 M_fs (the inverse of the s-s block of the covariance). reduce() returns a Bandwidth holding that reduced matrix, and remembers which dimensions it treats as unspecified.
Every Bandwidth also precomputes what a kd tree search needs to bound the distance from a search point to a box (see BoxBounder), and whether its matrix is diagonal, in which case distances and bounds cost O(n) rather than O(n^2). It is immutable once built, so a single Bandwidth may be shared by any number of threads.
Public Functions
-
explicit Bandwidth(Eigen::MatrixXd h2Inv)
Construct from an inverse squared bandwidth matrix.
- Parameters:
h2Inv – The n x n matrix; must be symmetric and positive semi-definite. No dimension is unspecified.
- Throws:
std::invalid_argument – if h2Inv is not square, is empty, or is not symmetric
std::domain_error – if h2Inv is not positive semi-definite
-
inline auto nDim() const -> std::size_t
Number of dimensions.
- Returns:
n
-
inline auto h2Inv() const -> const Eigen::MatrixXd&
The inverse squared bandwidth matrix.
- Returns:
The n x n matrix; for a reduced Bandwidth, its rows and columns for unspecified dimensions are zero
-
inline auto unspecified() const -> const std::vector<bool>&
Which dimensions are unspecified.
-
inline auto isReduced() const -> bool
Whether any dimension is unspecified.
-
inline auto isDiagonal() const -> bool
Whether the matrix is diagonal (over the specified dimensions)
- Returns:
True if distances and bounds use the O(n) diagonal path
-
inline auto nSpecified() const -> std::size_t
Number of specified dimensions.
- Returns:
The number of dimensions not flagged in unspecified()
-
inline auto det() const -> double
Determinant of the matrix over the specified dimensions.
- Returns:
det M_s, with M_s the matrix’s block over the specified dimensions (for a reduced Bandwidth, the Schur complement; see reduce()); 1 if no dimension is specified, and 0 (to rounding) if M_s is singular. The integral of exp(-d^T M_s d) over the specified dimensions is pi^(nSpecified / 2) / sqrt(det()).
-
auto reduce(const std::vector<bool> &unspecified) const -> Bandwidth
Reduce the metric for points with some coordinates unspecified.
Costs O(n^3); for many searches with the same metric and the same unspecified dimensions, reduce once and reuse the result.
- Parameters:
unspecified – Which dimensions will be unspecified; must have n elements, and include every dimension this Bandwidth already treats as unspecified
- Throws:
std::invalid_argument – if unspecified has the wrong size or omits a dimension this Bandwidth leaves unspecified
std::domain_error – if the block of the matrix over the newly unspecified dimensions is not positive definite
- Returns:
A Bandwidth whose matrix, over the dimensions s that remain specified, is the Schur complement M_ss - M_sf M_ff^-1 M_fs of this one’s (computed by Cholesky factorization of M_ff), and is zero elsewhere
-
auto convolve(const Bandwidth &other) const -> Bandwidth
Convolve with another bandwidth.
Convolving two Gaussians adds their covariances, and the covariance is proportional to the inverse of the matrix, so this is, e.g., the bandwidth of a kernel density estimate smoothed by Gaussian measurement errors. It is computed as M1 (M1 + M2)^-1 M2, by Cholesky factorization of M1 + M2, which equals (M1^-1 + M2^-1)^-1 when both are invertible, but needs neither to be: a singular matrix (a kernel infinitely broad along some direction) gives a singular result. Costs O(n^3).
- Parameters:
other – The other Bandwidth; must have the same number of dimensions, and the same unspecified dimensions (see unspecified())
- Throws:
std::invalid_argument – if other has a different number of dimensions or different unspecified dimensions
std::domain_error – if M1 + M2 is not positive definite (the two kernels are both infinitely broad along some direction)
- Returns:
The Bandwidth of the convolution of the two kernels, whose matrix, over the specified dimensions, is (M1^-1 + M2^-1)^-1, with M1 and M2 the two matrices there
-
auto convolveCov(const Eigen::MatrixXd &cov) const -> Bandwidth
Convolve with a Gaussian given by its covariance matrix.
Like convolve(), with the other kernel given by its covariance C rather than its inverse squared bandwidth matrix, the two being related by M = (2 C)^-1 for the kernel exp(-d^T M d). Computed as (I + 2 M C)^-1 M, by LU factorization, so neither M nor C need be invertible – C typically is not, being zero in dimensions without measurement errors. Costs O(n^3).
- Parameters:
cov – The Gaussian’s covariance matrix C, n x n: symmetric and positive semi-definite (e.g. measurement errors, zero in dimensions without them); only its block over this Bandwidth’s specified dimensions is used
- Throws:
std::invalid_argument – if cov is not n x n, or is not finite and symmetric
std::domain_error – if cov is not positive semi-definite
- Returns:
The Bandwidth of the convolution of this kernel with that Gaussian, whose matrix, over the specified dimensions, is (M^-1 + 2 C)^-1
-
auto matches(std::span<const double> x) const -> bool
Check whether a point’s unspecified coordinates match this Bandwidth’s.
- Parameters:
x – The point
- Returns:
True if x has n elements, NaN in exactly the dimensions this Bandwidth treats as unspecified
-
auto dist2(std::span<const double> x1, std::span<const double> x2) const -> double
Squared distance between two points.
For a metric that is not diagonal, NaN coordinates give the distance minimized over the unspecified coordinates only if this Bandwidth has been reduced for them (see reduce()). Allocates a scratch vector; the overload taking one avoids that, for use in loops.
- Parameters:
x1 – Position of the first point; must have n elements, which may be NaN to leave a coordinate unspecified
x2 – Position of the second point, likewise
- Returns:
d^T M d, with d_i = x1[i] - x2[i], or 0 if either is NaN
-
auto dist2(std::span<const double> x1, std::span<const double> x2, std::vector<double> &scratch) const -> double
Squared distance between two points, using caller-provided scratch space.
- Parameters:
x1 – As for the other overload
x2 – As for the other overload
scratch – Scratch space; resized as needed, so may be reused across calls without further allocation
- Returns:
As for the other overload
-
auto boxBounder(std::span<const double> x) const -> BoxBounder
Make a box bounder for a search point.
- Parameters:
x – The search point; must match this Bandwidth (see matches())
- Returns:
A BoxBounder for x, which must not outlive this Bandwidth
Public Static Functions
-
static auto identity(std::size_t nDim) -> Bandwidth
The Euclidean metric.
- Parameters:
nDim – Number of dimensions
- Returns:
A Bandwidth holding the nDim x nDim identity matrix
-
static auto nanPattern(std::span<const double> x) -> std::vector<bool>
Which elements of a point are NaN.
- Parameters:
x – The point
- Returns:
One flag per element, true if it is NaN
-
class BoxBounder
Rigorous lower and upper bounds on the distance from one point to axis-aligned boxes.
Built once per search point (see Bandwidth::boxBounder()). With s the specified dimensions, the bound uses a factor W of the matrix’s s-s block, M_s = W^T W, from Eigen’s pivoted LDL^T factorization M_s = P^T L D L^T P, W = D^1/2 L^T P. For any y, (x - y)^T M_s (x - y) = sum_k (w_k . (x - y))^2, where w_k are the rows of W. Over a box with center c and half-widths h, w_k . y covers exactly the interval w_k . c +- |w_k| . h, so the minimum of each term over the box is the squared gap between w_k . x and that interval (zero if it lies inside); their sum is a rigorous lower bound on the minimum of the sum, and exact for a diagonal metric (which uses a direct O(n) path instead). Likewise the maximum of each term is the squared distance from w_k . x to the far end of the interval, (|w_k . x - w_k . c| + |w_k| . h)^2, and their sum is a rigorous upper bound on the maximum of the sum (the per-term maxima generally occur at different points of the box), again exact for a diagonal metric, where it is the distance to the box’s farthest corner. A triangular, Cholesky-type factor is used rather than the metric’s principal axes because it stays close to the coordinate axes as the metric approaches diagonal, keeping the bound tight; the principal axes can rotate arbitrarily when eigenvalues are nearly equal. For non-diagonal metrics the lower bound is shrunk, and the upper bound inflated, by a relative 1e-10, so that rounding in the factorization can never carry either past a true distance. Holds scratch space, so must not be shared between threads; must not outlive the Bandwidth it came from.
Public Functions
-
auto lower(std::span<const double> lo, std::span<const double> hi) const -> double
Lower bound on the squared distance from the point to a box.
- Parameters:
lo – Lower corner of the box (n elements)
hi – Upper corner of the box (n elements)
- Returns:
A value no larger than the squared distance (as computed by dist2()) from the point to any point in the box
-
inline auto nDim() const -> std::size_t
Number of dimensions of the boxes.
- Returns:
The Bandwidth’s number of dimensions, n
-
auto upper(std::span<const double> lo, std::span<const double> hi) const -> double
Upper bound on the squared distance from the point to a box.
- Parameters:
lo – Lower corner of the box (n elements)
hi – Upper corner of the box (n elements)
- Returns:
A value no smaller than the squared distance (as computed by dist2()) from the point to any point in the box
-
auto bounds(std::span<const double> lo, std::span<const double> hi) const -> std::pair<double, double>
Lower and upper bounds together.
- Parameters:
lo – Lower corner of the box (n elements)
hi – Upper corner of the box (n elements)
- Returns:
The pair (lower(lo, hi), upper(lo, hi)), computed with a single pass over the box, so cheaper than calling both separately
-
auto lower(std::span<const double> lo, std::span<const double> hi) const -> double
-
explicit Bandwidth(Eigen::MatrixXd h2Inv)