Loading...
Searching...
No Matches
gpr::ski::DerivativeSKI Class Reference

#include <gpr/ml/gp/DerivativeSKI.h>

Public Member Functions

 DerivativeSKI (const Eigen::VectorXd &x, double lengthscale, Eigen::Index m)
Eigen::Index numPoints () const noexcept
Eigen::Index gridSize () const noexcept
Eigen::Index outOfGrid () const noexcept
const Eigen::VectorXd & gridKernelRow () const noexcept
 First row of the symmetric Toeplitz grid kernel.
Eigen::VectorXd applyGridKernel (const Eigen::VectorXd &v) const
 y = K_UU v, using the Toeplitz structure.
Eigen::MatrixXd denseJoint () const
double approximationError () const
 Relative Frobenius error ||A_ski - A_exact|| / ||A_exact||.

Static Public Member Functions

static Eigen::MatrixXd exactJoint (const Eigen::VectorXd &x, double lengthscale)

Detailed Description

D-SKI for one dimension of a squared-exponential factor.

k(x,x') = exp(-(x-x')^2 / (2 l^2))

Holds W, dW and the grid kernel K_UU. The grid kernel is symmetric Toeplitz (a uniform grid and a stationary kernel), which is what makes the multiply cheap; it is stored by its first row.

Definition at line 92 of file DerivativeSKI.h.

Constructor & Destructor Documentation

◆ DerivativeSKI()

gpr::ski::DerivativeSKI::DerivativeSKI ( const Eigen::VectorXd & x,
double lengthscale,
Eigen::Index m )

Build the interpolant for inputs x on a grid of m points. lengthscale must be positive; m must be at least 4 for the cubic stencil to fit. A positive input range needs one interior step, which five points lack.

Definition at line 109 of file DerivativeSKI.cpp.

111 : grid_(x.size() > 0 ? x.minCoeff() : 0.0,
112 x.size() > 0 ? x.maxCoeff() : 0.0, m),
113 x_(x),
114 lengthscale_(lengthscale),
115 n_(x.size())
116{
117 if (!(lengthscale > 0.0)) {
118 throw std::invalid_argument("SKI lengthscale must be positive");
119 }
120 const InterpolationWeights w = interpolate(grid_, x);
121 W_ = w.W;
122 dW_ = w.dW;
123 out_of_grid_ = w.out_of_grid;
124
125 // A stationary kernel on a uniform grid gives a symmetric Toeplitz K_UU,
126 // so its first row determines it.
127 kuu_row_.resize(m);
128 for (Eigen::Index j = 0; j < m; ++j) {
129 kuu_row_(j) = seKernel(grid_.at(j) - grid_.at(0), lengthscale_);
130 }
131}
@ x
Definition Enums.h:28
InterpolationWeights interpolate(const UniformGrid &grid, const Eigen::VectorXd &x)

Member Function Documentation

◆ applyGridKernel()

Eigen::VectorXd gpr::ski::DerivativeSKI::applyGridKernel ( const Eigen::VectorXd & v) const
nodiscard

y = K_UU v, using the Toeplitz structure.

Definition at line 133 of file DerivativeSKI.cpp.

134{
135 const Eigen::Index m = grid_.size();
136 if (v.size() != m) {
137 throw std::invalid_argument("grid kernel multiply: size mismatch");
138 }
139 // Direct Toeplitz application. An FFT would make this m log m; at the sizes
140 // where D-SKI is even a candidate the quadratic form is not the bottleneck,
141 // and this keeps the reference implementation exact.
142 Eigen::VectorXd y(m);
143 for (Eigen::Index i = 0; i < m; ++i) {
144 double acc = 0.0;
145 for (Eigen::Index j = 0; j < m; ++j) {
146 acc += kuu_row_(std::abs(i - j)) * v(j);
147 }
148 y(i) = acc;
149 }
150 return y;
151}
@ y
Definition Enums.h:28

◆ approximationError()

double gpr::ski::DerivativeSKI::approximationError ( ) const
nodiscard

Relative Frobenius error ||A_ski - A_exact|| / ||A_exact||.

Definition at line 189 of file DerivativeSKI.cpp.

190{
191 const Eigen::MatrixXd exact = exactJoint(x_, lengthscale_);
192 const Eigen::MatrixXd approx = denseJoint();
193 const double denom = exact.norm();
194 if (!(denom > 0.0)) return 0.0;
195 return (approx - exact).norm() / denom;
196}
static Eigen::MatrixXd exactJoint(const Eigen::VectorXd &x, double lengthscale)
Eigen::MatrixXd denseJoint() const

◆ denseJoint()

Eigen::MatrixXd gpr::ski::DerivativeSKI::denseJoint ( ) const
nodiscard

The 2n x 2n joint value/derivative matrix this interpolant represents. Dense, and only for validation against the exact kernel – the point of D-SKI is never to form it.

Definition at line 153 of file DerivativeSKI.cpp.

154{
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));
160 }
161 }
162 Eigen::MatrixXd stacked(2 * n_, m);
163 stacked.topRows(n_) = W_;
164 stacked.bottomRows(n_) = dW_;
165 return stacked * kuu * stacked.transpose();
166}

◆ exactJoint()

Eigen::MatrixXd gpr::ski::DerivativeSKI::exactJoint ( const Eigen::VectorXd & x,
double lengthscale )
staticnodiscard

Exact joint value/derivative SE matrix for the same inputs, in the same block layout, so the two can be compared entry for entry.

Definition at line 168 of file DerivativeSKI.cpp.

170{
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);
178 A(i, j) = k;
179 // d/dx_i k = -(x_i - x_j)/l^2 k
180 A(n + i, j) = -d / l2 * k;
181 A(i, n + j) = d / l2 * k;
182 // d2/dx_i dx_j k = (1/l^2 - d^2/l^4) k
183 A(n + i, n + j) = (1.0 / l2 - d * d / (l2 * l2)) * k;
184 }
185 }
186 return A;
187}

◆ gridKernelRow()

const Eigen::VectorXd & gpr::ski::DerivativeSKI::gridKernelRow ( ) const
inlinenodiscardnoexcept

First row of the symmetric Toeplitz grid kernel.

Definition at line 113 of file DerivativeSKI.h.

114 {
115 return kuu_row_;
116 }

◆ gridSize()

Eigen::Index gpr::ski::DerivativeSKI::gridSize ( ) const
inlinenodiscardnoexcept

Definition at line 103 of file DerivativeSKI.h.

104 {
105 return grid_.size();
106 }

◆ numPoints()

Eigen::Index gpr::ski::DerivativeSKI::numPoints ( ) const
inlinenodiscardnoexcept

Definition at line 99 of file DerivativeSKI.h.

100 {
101 return n_;
102 }

◆ outOfGrid()

Eigen::Index gpr::ski::DerivativeSKI::outOfGrid ( ) const
inlinenodiscardnoexcept

Definition at line 107 of file DerivativeSKI.h.

108 {
109 return out_of_grid_;
110 }

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