6#include <Eigen/Eigenvalues>
17constexpr double kKeysA = -0.5;
19double seKernel(
double r,
double lengthscale)
noexcept
21 const double s = r / lengthscale;
22 return std::exp(-0.5 * s * s);
29 const double a = std::fabs(s);
31 return ((kKeysA + 2.0) * a - (kKeysA + 3.0)) * a * a + 1.0;
34 return ((kKeysA * a - 5.0 * kKeysA) * a + 8.0 * kKeysA) * a -
42 const double a = std::fabs(s);
44 const double sign = (s > 0.0) ? 1.0 : ((s < 0.0) ? -1.0 : 0.0);
46 return sign * (3.0 * (kKeysA + 2.0) * a * a - 2.0 * (kKeysA + 3.0) * a);
49 return sign * (3.0 * kKeysA * a * a - 10.0 * kKeysA * a + 8.0 * kKeysA);
56 if (m < 4)
throw std::invalid_argument(
"SKI grid needs at least 4 points");
57 if (!(hi >= lo))
throw std::invalid_argument(
"SKI grid bounds inverted");
63 const double span = hi - lo;
64 const Eigen::Index steps = m - 5;
65 if (span > 0.0 && steps == 0) {
66 throw std::invalid_argument(
67 "SKI grid needs an interior interval to span a positive range");
69 h_ = (span > 0.0) ? span /
static_cast<double>(steps) : 1.0;
70 origin_ = lo - 2.0 * h_;
74 const Eigen::VectorXd&
x)
77 const Eigen::Index n =
x.size();
78 const Eigen::Index m = grid.
size();
79 out.W = Eigen::MatrixXd::Zero(n, m);
80 out.dW = Eigen::MatrixXd::Zero(n, m);
82 const double inv_h = 1.0 / grid.
spacing();
84 for (Eigen::Index i = 0; i < n; ++i) {
86 const Eigen::Index base =
static_cast<Eigen::Index
>(std::floor(t));
87 bool supported =
false;
88 for (Eigen::Index k = -1; k <= 2; ++k) {
89 const Eigen::Index j = base + k;
90 if (j < 0 || j >= m)
continue;
91 const double s = t -
static_cast<double>(j);
98 if (w == 0.0 && dw == 0.0)
continue;
101 out.dW(i, j) = dw * inv_h;
104 if (!supported) ++
out.out_of_grid;
111 : grid_(
x.size() > 0 ?
x.minCoeff() : 0.0,
112 x.size() > 0 ?
x.maxCoeff() : 0.0, m),
114 lengthscale_(lengthscale),
117 if (!(lengthscale > 0.0)) {
118 throw std::invalid_argument(
"SKI lengthscale must be positive");
128 for (Eigen::Index j = 0; j < m; ++j) {
129 kuu_row_(j) = seKernel(grid_.at(j) - grid_.at(0), lengthscale_);
135 const Eigen::Index m = grid_.size();
137 throw std::invalid_argument(
"grid kernel multiply: size mismatch");
142 Eigen::VectorXd
y(m);
143 for (Eigen::Index i = 0; i < m; ++i) {
145 for (Eigen::Index j = 0; j < m; ++j) {
146 acc += kuu_row_(std::abs(i - j)) * v(j);
155 const Eigen::Index m = grid_.size();
156 Eigen::MatrixXd kuu(m, m);
157 for (Eigen::Index i = 0; i < m; ++i) {
158 for (Eigen::Index j = 0; j < m; ++j) {
159 kuu(i, j) = kuu_row_(std::abs(i - j));
162 Eigen::MatrixXd stacked(2 * n_, m);
163 stacked.topRows(n_) = W_;
164 stacked.bottomRows(n_) = dW_;
165 return stacked * kuu * stacked.transpose();
171 const Eigen::Index n =
x.size();
172 const double l2 = lengthscale * lengthscale;
173 Eigen::MatrixXd A(2 * n, 2 * n);
174 for (Eigen::Index i = 0; i < n; ++i) {
175 for (Eigen::Index j = 0; j < n; ++j) {
176 const double d =
x(i) -
x(j);
177 const double k = seKernel(d, lengthscale);
180 A(n + i, j) = -d / l2 * k;
181 A(i, n + j) = d / l2 * k;
183 A(n + i, n + j) = (1.0 / l2 - d * d / (l2 * l2)) * k;
191 const Eigen::MatrixXd exact =
exactJoint(x_, lengthscale_);
193 const double denom = exact.norm();
194 if (!(denom > 0.0))
return 0.0;
195 return (approx - exact).norm() / denom;
199 Eigen::Index max_rank)
201 const Eigen::Index n = A.rows();
202 if (n == 0)
return Eigen::MatrixXd();
206 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(A);
207 if (es.info() != Eigen::Success)
return Eigen::MatrixXd();
208 Eigen::VectorXd ev = es.eigenvalues();
209 const double total = ev.cwiseMax(0.0).sum();
210 if (!(total > 0.0))
return Eigen::MatrixXd::Zero(n, 0);
213 Eigen::Index keep = 0;
215 const Eigen::Index cap = (max_rank > 0) ? std::min(max_rank, n) : n;
216 for (Eigen::Index i = 0; i < cap; ++i) {
217 const double lam = std::max(0.0, ev(n - 1 - i));
220 if (acc >= (1.0 - tol) * total)
break;
222 Eigen::MatrixXd F(n, keep);
223 for (Eigen::Index i = 0; i < keep; ++i) {
224 const double lam = std::max(0.0, ev(n - 1 - i));
225 F.col(i) = es.eigenvectors().col(n - 1 - i) * std::sqrt(lam);
231 const Eigen::VectorXd& lengthscales,
double tol,
232 Eigen::Index max_rank)
235 const Eigen::Index n =
x.rows();
236 const Eigen::Index d =
x.cols();
237 if (n == 0 || d == 0 || lengthscales.size() != d)
return report;
241 Eigen::MatrixXd running = Eigen::MatrixXd::Ones(n, n);
242 Eigen::MatrixXd exact = Eigen::MatrixXd::Ones(n, n);
243 for (Eigen::Index dim = 0; dim < d; ++dim) {
244 Eigen::MatrixXd factor(n, n);
245 for (Eigen::Index i = 0; i < n; ++i) {
246 for (Eigen::Index j = 0; j < n; ++j) {
248 seKernel(
x(i, dim) -
x(j, dim), lengthscales(dim));
251 exact = exact.cwiseProduct(factor);
252 running = running.cwiseProduct(factor);
257 running = F * F.transpose();
260 const double denom = exact.norm();
262 (denom > 0.0) ? (running - exact).norm() / denom : 0.0;
Eigen::VectorXd applyGridKernel(const Eigen::VectorXd &v) const
y = K_UU v, using the Toeplitz structure.
static Eigen::MatrixXd exactJoint(const Eigen::VectorXd &x, double lengthscale)
double approximationError() const
Relative Frobenius error ||A_ski - A_exact|| / ||A_exact||.
DerivativeSKI(const Eigen::VectorXd &x, double lengthscale, Eigen::Index m)
Eigen::MatrixXd denseJoint() const
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double cubicConvolutionWeight(double s) noexcept
DSkipReport dskipProduct(const Eigen::MatrixXd &x, const Eigen::VectorXd &lengthscales, double tol, Eigen::Index max_rank)
Eigen::MatrixXd lanczosLowRank(const Eigen::MatrixXd &A, double tol, Eigen::Index max_rank)
InterpolationWeights interpolate(const UniformGrid &grid, const Eigen::VectorXd &x)
double cubicConvolutionWeightDerivative(double s) noexcept
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
double relative_error
against the exact product
Eigen::Index exact_rank
numerical rank of the exact matrix
bool rank_bounded
did the rank stay below the matrix size
std::vector< Eigen::Index > rank_after_product
one entry per dimension
Eigen::MatrixXd W
n x m, rows sum to 1 on interior points
Eigen::Index out_of_grid
inputs with no support
Eigen::MatrixXd dW
n x m, d/dx of each row