73 double length,
double variance,
74 double energyNoiseVariance,
double gradientNoiseVariance)
77 length2_(length * length),
80 require(n_ > 0 && d_ > 0 && energies.size() == n_ &&
81 gradients.rows() == n_ && gradients.cols() == d_ &&
82 sites.allFinite() && energies.allFinite() &&
83 gradients.allFinite(),
84 "Matching finite Gaussian jet observations required");
85 require(length > 0 && length2_ > 0 && std::isfinite(length2_) &&
86 variance > 0 && std::isfinite(variance) &&
87 energyNoiseVariance >= 0 &&
88 std::isfinite(energyNoiseVariance) &&
89 gradientNoiseVariance >= 0 &&
90 std::isfinite(gradientNoiseVariance),
91 "Finite positive kernel scales and nonnegative noise required");
92 physicalSites_ = sites;
93 energyNoise_ = energyNoiseVariance;
94 gradientNoise_ = gradientNoiseVariance;
95 anchor_ = sites.row(0).transpose();
96 const M centered = sites.rowwise() - anchor_.transpose();
97 const Eigen::JacobiSVD<M> svd(centered.transpose(),
99 require(svd.info() == Eigen::Success,
100 "Gaussian training-span decomposition failed");
101 const double threshold =
102 64 * std::numeric_limits<double>::epsilon() *
103 std::min(
double(std::max(n_, d_)) * svd.singularValues()[0],
105 r_ = (svd.singularValues().array() > threshold).count();
106 basis_ = svd.matrixU().leftCols(r_);
107 normal_ = M::Identity(d_, d_) - basis_ * basis_.transpose();
108 sites_ = centered * basis_;
109 spanError_ = (centered - sites_ * basis_.transpose()).norm();
110 const auto count = n_ * (r_ + 1);
111 M tangent = M::Zero(count, count), normal = M::Zero(n_, n_);
113 const M tangentGradients = gradients * basis_;
114 for (Eigen::Index i = 0; i < n_; ++i) {
115 values[i * (r_ + 1)] = energies[i];
118 values.segment(i * (r_ + 1) + 1, r_) =
119 tangentGradients.row(i).transpose();
120 for (Eigen::Index j = 0; j < n_; ++j) {
121 const double tangentDistance2 =
123 : (sites_.row(i) - sites_.row(j)).squaredNorm();
125 variance_ * std::exp(-tangentDistance2 / (2 * length2_));
126 normal(i, j) = k / length2_;
127 tangent(i * (r_ + 1), j * (r_ + 1)) = k;
128 if (r_ == 0)
continue;
129 const V delta = (sites_.row(i) - sites_.row(j)).transpose();
130 tangent.block(i * (r_ + 1), j * (r_ + 1) + 1, 1, r_) =
131 k * delta.transpose() / length2_;
132 tangent.block(i * (r_ + 1) + 1, j * (r_ + 1), r_, 1) =
133 -k * delta / length2_;
134 tangent.block(i * (r_ + 1) + 1, j * (r_ + 1) + 1, r_, r_) =
135 k * (M::Identity(r_, r_) / length2_ -
136 delta * delta.transpose() / (length2_ * length2_));
138 tangent(i * (r_ + 1), i * (r_ + 1)) += energyNoiseVariance;
139 tangent.diagonal().segment(i * (r_ + 1) + 1, r_).array() +=
140 gradientNoiseVariance;
141 normal(i, i) += gradientNoiseVariance;
143 tangentFactor_.compute(tangent);
144 normalFactor_.compute(normal);
145 alpha_ = tangentFactor_.solve(values);
146 const M residualGradients = gradients * normal_;
147 beta_ = normalFactor_.solve(residualGradients);
148 beta_ = beta_ * normal_;
149 nlml_ = .5 * (values.dot(alpha_) +
150 (residualGradients.array() * beta_.array()).sum() +
151 tangentFactor_.logDet + (d_ - r_) * normalFactor_.logDet +
152 n_ * (d_ + 1) * std::log(2 * std::acos(-1.)));
153 require(std::isfinite(nlml_),
"Gaussian jet likelihood is nonfinite");
182 require(coordinate.size() == d_ && coordinate.allFinite(),
183 "Finite matching Gaussian query required");
184 const V displacement = coordinate - anchor_,
185 r = basis_.transpose() * displacement,
186 z = normal_ * displacement;
187 V gradient = V::Zero(r_), h = V::Zero(d_),
188 cross = V::Zero(n_ * (r_ + 1)), kernels(n_);
189 M hessian = M::Zero(r_, r_), hDerivative = M::Zero(r_, d_),
190 hContraction = M::Zero(r_, r_);
191 M crossDerivative = M::Zero(r_, cross.size()), kernelDerivative(n_, d_);
193 for (Eigen::Index i = 0; i < n_; ++i) {
195 const V delta = r_ == 0 ?
V() :
V(r - sites_.row(i).transpose());
197 variance_ * std::exp(-delta.squaredNorm() / (2 * length2_));
199 const V b = r_ == 0 ?
V() :
V(alpha_.segment(i * (r_ + 1) + 1, r_));
200 const double value = alpha_[i * (r_ + 1)] + delta.dot(b) / length2_;
201 const M second = delta * delta.transpose() / (length2_ * length2_) -
202 M::Identity(r_, r_) / length2_;
204 gradient += k * (b - delta * value) / length2_;
205 hessian += k * (second * value -
206 (delta * b.transpose() + b * delta.transpose()) /
207 (length2_ * length2_));
208 h += k * beta_.row(i).transpose() / length2_;
209 hDerivative -= k * delta * beta_.row(i) / (length2_ * length2_);
210 hContraction += k * second * beta_.row(i).dot(
z) / length2_;
211 cross[i * (r_ + 1)] = k;
213 cross.segment(i * (r_ + 1) + 1, r_) = k * delta / length2_;
214 crossDerivative.col(i * (r_ + 1)) = -k * delta / length2_;
215 crossDerivative.block(0, i * (r_ + 1) + 1, r_, r_) =
218 kernelDerivative.row(i) =
219 -k * (r_ == 0 ?
z :
V(basis_ * delta +
z)).transpose() /
222 const double value = mean +
z.dot(h),
223 attenuation = std::exp(-
z.squaredNorm() / (2 * length2_));
224 const V fullGradient = basis_ * (gradient + hDerivative *
z) + h,
225 logGradient = -
z / length2_;
226 const M fullHessian =
227 basis_ * (hessian + hContraction) * basis_.transpose() +
228 basis_ * hDerivative + hDerivative.transpose() * basis_.transpose();
230 result.
energy = attenuation * value;
231 result.
gradient = attenuation * (fullGradient + value * logGradient);
233 (fullHessian + logGradient * fullGradient.transpose() +
234 fullGradient * logGradient.transpose() +
235 value * (logGradient * logGradient.transpose() -
236 normal_ / length2_));
237 Eigen::Index owner = -1;
239 for (Eigen::Index i = 0; i < n_; ++i)
240 if ((coordinate.array() ==
241 physicalSites_.row(i).transpose().array())
246 if (uncertainty && owner >= 0 && energyNoise_ <= variance_ &&
247 gradientNoise_ <= variance_ / length2_) {
251 const auto offset = owner * (r_ + 1);
252 M selection = M::Zero(n_ * (r_ + 1), r_ + 1);
253 selection.block(offset, 0, r_ + 1, r_ + 1).setIdentity();
254 const M response = tangentFactor_.solve(selection);
255 V normalSelection = V::Zero(n_);
256 normalSelection[owner] = 1.;
257 const M normalResponse = normalFactor_.solve(normalSelection);
259 energyNoise_ * (1 - energyNoise_ * response(offset, 0));
261 for (Eigen::Index j = 0; j < d_; ++j) {
266 const M inverseGradients =
267 response.block(offset + 1, 1, r_, r_);
268 tangent = basis_.row(j).dot(
269 (inverseGradients * basis_.row(j).transpose()).eval());
271 const double inverse =
272 tangent + normal_(j, j) * normalResponse(owner, 0);
274 gradientNoise_ * (1 - gradientNoise_ * inverse);
276 }
else if (uncertainty) {
277 const V normalSolved = normalFactor_.solve(kernels);
278 const double normalQuadratic = kernels.dot(normalSolved),
279 l4 = length2_ * length2_;
280 const V tangentSolved = tangentFactor_.solve(cross);
282 variance_ - attenuation * attenuation *
283 (cross.dot(tangentSolved) +
284 z.squaredNorm() * normalQuadratic / l4);
285 const M fullCross = attenuation * (basis_ * crossDerivative +
286 logGradient * cross.transpose());
287 const M tangentResponse =
288 tangentFactor_.solve(fullCross.transpose());
289 const M normalResponse = normalFactor_.solve(kernelDerivative);
291 for (Eigen::Index j = 0; j < d_; ++j) {
292 const double reduction =
293 normal_(j, j) * normalQuadratic +
294 2 *
z[j] * kernels.dot(normalResponse.col(j)) +
296 kernelDerivative.col(j).dot(normalResponse.col(j));
298 variance_ / length2_ -
299 fullCross.row(j).dot(tangentResponse.col(j)) -
300 attenuation * attenuation * reduction / l4;
303 require(std::isfinite(result.
energy) && result.
gradient.allFinite() &&
305 "Gaussian jet prediction is nonfinite");
308 "Gaussian jet covariance is nonfinite");