Loading...
Searching...
No Matches
gpr::quantum::AffineGaussianJet Class Reference

#include <gpr/quantum/AffineGaussianJet.h>

Classes

struct  Prediction

Public Types

using M = Eigen::MatrixXd
using V = Eigen::VectorXd

Public Member Functions

 AffineGaussianJet (const M &sites, const V &energies, const M &gradients, double length, double variance, double energyNoiseVariance, double gradientNoiseVariance)
Eigen::Index dimension () const
Eigen::Index spanDimension () const
Eigen::Index tangentFactorDimension () const
Eigen::Index normalFactorDimension () const
double spanReconstructionError () const
double negativeLogLikelihood () const
Prediction predict (const V &coordinate, bool uncertainty=true) const

Detailed Description

Definition at line 19 of file AffineGaussianJet.h.

Member Typedef Documentation

◆ M

using gpr::quantum::AffineGaussianJet::M = Eigen::MatrixXd

Definition at line 21 of file AffineGaussianJet.h.

◆ V

using gpr::quantum::AffineGaussianJet::V = Eigen::VectorXd

Definition at line 22 of file AffineGaussianJet.h.

Constructor & Destructor Documentation

◆ AffineGaussianJet()

gpr::quantum::AffineGaussianJet::AffineGaussianJet ( const M & sites,
const V & energies,
const M & gradients,
double length,
double variance,
double energyNoiseVariance,
double gradientNoiseVariance )
inline

Definition at line 72 of file AffineGaussianJet.h.

75 : n_(sites.rows()),
76 d_(sites.cols()),
77 length2_(length * length),
78 variance_(variance)
79 {
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(),
98 Eigen::ComputeThinU);
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],
104 length);
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_);
112 V values(count);
113 const M tangentGradients = gradients * basis_;
114 for (Eigen::Index i = 0; i < n_; ++i) {
115 values[i * (r_ + 1)] = energies[i];
116 // No tangent span: sites_ has no columns and no storage.
117 if (r_ > 0)
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 =
122 r_ == 0 ? 0.0
123 : (sites_.row(i) - sites_.row(j)).squaredNorm();
124 const double k =
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_));
137 }
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;
142 }
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");
154 }

Member Function Documentation

◆ dimension()

Eigen::Index gpr::quantum::AffineGaussianJet::dimension ( ) const
inline

Definition at line 155 of file AffineGaussianJet.h.

156 {
157 return d_;
158 }

◆ negativeLogLikelihood()

double gpr::quantum::AffineGaussianJet::negativeLogLikelihood ( ) const
inline

Definition at line 175 of file AffineGaussianJet.h.

176 {
177 return nlml_;
178 }

◆ normalFactorDimension()

Eigen::Index gpr::quantum::AffineGaussianJet::normalFactorDimension ( ) const
inline

Definition at line 167 of file AffineGaussianJet.h.

168 {
169 return n_;
170 }

◆ predict()

Prediction gpr::quantum::AffineGaussianJet::predict ( const V & coordinate,
bool uncertainty = true ) const
inline

Definition at line 180 of file AffineGaussianJet.h.

181 {
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_);
192 double mean = 0;
193 for (Eigen::Index i = 0; i < n_; ++i) {
194 // sites_ has no storage when the training span is empty.
195 const V delta = r_ == 0 ? V() : V(r - sites_.row(i).transpose());
196 const double k =
197 variance_ * std::exp(-delta.squaredNorm() / (2 * length2_));
198 kernels[i] = k;
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_;
203 mean += k * value;
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;
212 if (r_ > 0) {
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_) =
216 -k * second;
217 }
218 kernelDerivative.row(i) =
219 -k * (r_ == 0 ? z : V(basis_ * delta + z)).transpose() /
220 length2_;
221 }
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();
229 Prediction result;
230 result.energy = attenuation * value;
231 result.gradient = attenuation * (fullGradient + value * logGradient);
232 result.hessian = attenuation *
233 (fullHessian + logGradient * fullGradient.transpose() +
234 fullGradient * logGradient.transpose() +
235 value * (logGradient * logGradient.transpose() -
236 normal_ / length2_));
237 Eigen::Index owner = -1;
238 if (uncertainty)
239 for (Eigen::Index i = 0; i < n_; ++i)
240 if ((coordinate.array() ==
241 physicalSites_.row(i).transpose().array())
242 .all()) {
243 owner = i;
244 break;
245 }
246 if (uncertainty && owner >= 0 && energyNoise_ <= variance_ &&
247 gradientNoise_ <= variance_ / length2_) {
248 // Conditioning an owned observation gives D-D(K+D)^-1D. This
249 // keeps exact noiseless constraints and avoids subtracting two
250 // prior-scale covariances to resolve an observation-scale result.
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);
258 result.energyVariance =
259 energyNoise_ * (1 - energyNoise_ * response(offset, 0));
260 result.gradientVariance.resize(d_);
261 for (Eigen::Index j = 0; j < d_; ++j) {
262 // No tangent span: basis_ has no columns and no storage, so
263 // row(j) at j > 0 would offset a null pointer.
264 double tangent = 0;
265 if (r_ > 0) {
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());
270 }
271 const double inverse =
272 tangent + normal_(j, j) * normalResponse(owner, 0);
273 result.gradientVariance[j] =
274 gradientNoise_ * (1 - gradientNoise_ * inverse);
275 }
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);
281 result.energyVariance =
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);
290 result.gradientVariance.resize(d_);
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)) +
295 z.squaredNorm() *
296 kernelDerivative.col(j).dot(normalResponse.col(j));
297 result.gradientVariance[j] =
298 variance_ / length2_ -
299 fullCross.row(j).dot(tangentResponse.col(j)) -
300 attenuation * attenuation * reduction / l4;
301 }
302 }
303 require(std::isfinite(result.energy) && result.gradient.allFinite() &&
304 result.hessian.allFinite(),
305 "Gaussian jet prediction is nonfinite");
306 require(!uncertainty || (std::isfinite(result.energyVariance) &&
307 result.gradientVariance.allFinite()),
308 "Gaussian jet covariance is nonfinite");
309 return result;
310 }
@ z
Definition Enums.h:28

◆ spanDimension()

Eigen::Index gpr::quantum::AffineGaussianJet::spanDimension ( ) const
inline

Definition at line 159 of file AffineGaussianJet.h.

160 {
161 return r_;
162 }

◆ spanReconstructionError()

double gpr::quantum::AffineGaussianJet::spanReconstructionError ( ) const
inline

Definition at line 171 of file AffineGaussianJet.h.

172 {
173 return spanError_;
174 }

◆ tangentFactorDimension()

Eigen::Index gpr::quantum::AffineGaussianJet::tangentFactorDimension ( ) const
inline

Definition at line 163 of file AffineGaussianJet.h.

164 {
165 return n_ * (r_ + 1);
166 }

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