Loading...
Searching...
No Matches
MapObjective.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_ML_MAP_OBJECTIVE_H_
6#define GPR_ML_MAP_OBJECTIVE_H_
7
8#include <Eigen/Dense>
9#include <algorithm>
10#include <cmath>
11#include <cstdint>
12#include <limits>
13#include <vector>
14
17#include "../prior/PriorBase.h"
19#include "../prior/PriorSqrtt.h"
20#include "../prior/PriorT.h"
21#include "MapPriorBarrier.h"
22
23namespace funcmin {
24
28 double signal_variance = 1.0;
30 double barrier_strength = 0.0;
31 bool use_magn_ceiling = false;
32 double max_log_magnSigma2 = 0.0;
34 double tail_lower_barrier = -std::numeric_limits<double>::infinity();
36
48
60 Eigen::VectorXd lengthscale_rho0;
64 Eigen::Index lengthscale_count = -1;
68 Eigen::VectorXd prior_weight;
69 std::vector<bool> active_parameters;
72 Eigen::VectorXd parameter_lower;
73 Eigen::VectorXd parameter_upper;
74};
75
78inline bool mapParametersInDomain(const Eigen::VectorXd& w,
79 const MapObjectiveConfig& cfg)
80{
81 if (!w.allFinite()) return false;
82 for (const auto* bounds: {&cfg.parameter_lower, &cfg.parameter_upper}) {
83 if (bounds->size() != 0 && bounds->size() != w.size()) return false;
84 if (bounds->array().isNaN().any()) return false;
85 }
86 return (cfg.parameter_lower.size() == 0 ||
87 (w.array() >= cfg.parameter_lower.array()).all()) &&
88 (cfg.parameter_upper.size() == 0 ||
89 (w.array() <= cfg.parameter_upper.array()).all());
90}
91
95 Eigen::VectorXd& gradient)
96{
97 if (cfg.active_parameters.empty()) return;
98 if (cfg.active_parameters.size() !=
99 static_cast<std::size_t>(gradient.size())) {
100 gradient.setConstant(std::numeric_limits<double>::quiet_NaN());
101 return;
102 }
103 for (Eigen::Index i = 0; i < gradient.size(); ++i) {
104 if (!cfg.active_parameters[static_cast<std::size_t>(i)])
105 gradient(i) = 0.0;
106 }
107}
108
110inline constexpr double kMapStudentTNu = 4.0;
111inline constexpr double kMapStudentTSigmaSq = 1.0;
112inline constexpr double kMapStudentTNorm = 0.5 * (kMapStudentTNu + 1.0);
113
114inline double mapHalfCauchyASq(double signal_variance) noexcept
115{
116 return 2.0 * signal_variance;
117}
118
119inline double mapMaxLogMagnSigma2(double signal_variance) noexcept
120{
121 // Pure data-scale ceiling: proportional to the target's own variance
122 // (c=100; Rasmussen-Williams sec. 5.4.1), no absolute unit-carrying
123 // floor. Callers gate whether the ceiling barrier is active at all
124 // on whether signal_variance is itself data-derived (see
125 // MapObjectiveConfig::use_magn_ceiling / the have_signal_variance
126 // guards in the MAP objective). A non-positive signal_variance here
127 // is never read by
128 // applyMapPenalty because use_magn_ceiling is false in that case.
129 return std::log(100.0 * signal_variance);
130}
131
135inline void restoreMapAmplitudeFeasibility(Eigen::VectorXd& w,
136 const MapObjectiveConfig& cfg)
137{
138 if (cfg.use_magn_ceiling && w.size() > 0 && w.allFinite() &&
139 !detail::isLogBarrierInterior(cfg.max_log_magnSigma2 - w(0)) &&
140 (cfg.active_parameters.empty() || cfg.active_parameters.front())) {
141 w(0) = std::log(mapHalfCauchyASq(cfg.signal_variance));
142 }
143}
144
153inline double mapLengthscaleRho0(const MapObjectiveConfig& cfg,
154 Eigen::Index i) noexcept
155{
156 const Eigen::Index k = i - 1;
157 if (k >= 0 && k < cfg.lengthscale_rho0.size()) {
158 const double rho0 = cfg.lengthscale_rho0(k);
159 if (std::isfinite(rho0) && rho0 > 0.0) return rho0;
160 }
161 return 1.0;
162}
163
166inline Eigen::Index mapLengthscaleEnd(const MapObjectiveConfig& cfg,
167 Eigen::Index size) noexcept
168{
169 if (cfg.lengthscale_count < 0) return size;
170 const Eigen::Index end = 1 + cfg.lengthscale_count;
171 if (end < 1) return 1;
172 return end < size ? end : size;
173}
174
179inline constexpr double kMadConsistencyConstant = 1.4826;
180
183inline double mapMedianInPlace(std::vector<double>& v) noexcept
184{
185 if (v.empty()) return 0.0;
186 std::sort(v.begin(), v.end());
187 const std::size_t m = v.size();
188 return (m % 2) ? v[m / 2] : 0.5 * (v[m / 2 - 1] + v[m / 2]);
189}
190
195inline double mapRobustMadVariance(const Eigen::VectorXd& block)
196{
197 const Eigen::Index n = block.size();
198 if (n == 0) return 0.0;
199
200 std::vector<double> sorted(block.data(), block.data() + n);
201 const double med = mapMedianInPlace(sorted);
202
203 std::vector<double> abs_dev(static_cast<std::size_t>(n));
204 for (Eigen::Index i = 0; i < n; ++i)
205 abs_dev[static_cast<std::size_t>(i)] = std::abs(block(i) - med);
206 const double mad = mapMedianInPlace(abs_dev);
207
208 const double sigma_robust = kMadConsistencyConstant * mad;
209 const double robust_var = sigma_robust * sigma_robust;
210 if (std::isfinite(robust_var) && robust_var > 0.0) return robust_var;
211
212 const double mean = block.mean();
213 return (block.array() - mean).square().mean();
214}
215
236 Eigen::Index n_e,
237 const Eigen::VectorXd& y,
238 const Eigen::VectorXd& w)
239{
240 if (w.size() < 2 || x.cols() < 2) return 0.0;
241 if (n_e <= 0 || n_e >= y.size() || n_e > x.rows()) return 0.0;
242
243 const Eigen::Index dof =
244 x.cols() - 1; // Cartesian dims; last col = block index
245 const Eigen::Index n_atoms = dof / 3;
246 if (n_atoms < 2 || dof % 3 != 0) return 0.0;
247
248 std::vector<double> inv_l2;
249 inv_l2.reserve(static_cast<std::size_t>(w.size() - 1));
250 for (Eigen::Index i = 1; i < w.size(); ++i) {
251 const double l = std::exp(w(i));
252 if (l > 0.0 && std::isfinite(l)) inv_l2.push_back(1.0 / (l * l));
253 }
254 if (inv_l2.empty()) return 0.0;
255
256 std::vector<double> inv_r2;
257 inv_r2.reserve(static_cast<std::size_t>(n_e) *
258 static_cast<std::size_t>(n_atoms) *
259 static_cast<std::size_t>(n_atoms - 1) / 2);
260 for (Eigen::Index row = 0; row < n_e; ++row) {
261 for (Eigen::Index i = 0; i < n_atoms - 1; ++i) {
262 for (Eigen::Index j = i + 1; j < n_atoms; ++j) {
263 double r2 = 0.0;
264 for (Eigen::Index c = 0; c < 3; ++c) {
265 const double delta = x(row, 3 * i + c) - x(row, 3 * j + c);
266 r2 += delta * delta;
267 }
268 if (r2 > 0.0 && std::isfinite(r2)) inv_r2.push_back(1.0 / r2);
269 }
270 }
271 }
272 if (inv_r2.empty()) return 0.0;
273
274 const double med_inv_l2 = mapMedianInPlace(inv_l2);
275 const double med_inv_r2 = mapMedianInPlace(inv_r2);
276 const double j_med = med_inv_l2 * med_inv_r2 * med_inv_r2;
277 if (!(j_med > 0.0) || !std::isfinite(j_med)) return 0.0;
278
279 const double grad_mad_var = mapRobustMadVariance(y.tail(y.size() - n_e));
280 if (!std::isfinite(grad_mad_var) || grad_mad_var <= 0.0) return 0.0;
281
282 return grad_mad_var / j_med;
283}
284
296 Eigen::Index n_e,
297 const Eigen::VectorXd& y,
298 const Eigen::VectorXd& w)
299{
300 const double energy_var = mapRobustMadVariance(y.head(n_e));
301 const double force_var = mapForceImpliedSignalVariance(x, n_e, y, w);
302 return std::max(std::max(energy_var, force_var), 1e-6);
303}
304
307inline double mapBayesPriorWeight(const MapObjectiveConfig& cfg) noexcept
308{
309 if (cfg.weight_mode == gpr::MapPriorWeightMode::Full) return 1.0;
310 return cfg.barrier_strength;
311}
312
313inline bool mapLogNormalMagnPriorActive(const MapObjectiveConfig& cfg) noexcept
314{
315 return cfg.use_lognormal_magn_prior && cfg.magn_prior.getS2() > 0.0 &&
316 std::isfinite(cfg.magn_prior.getS2()) &&
317 std::isfinite(cfg.magn_prior.getMu());
318}
319
320inline bool mapMagnPriorActive(const MapObjectiveConfig& cfg) noexcept
321{
322 return cfg.use_configured_magn_prior && cfg.magn_prior.getNu() > 0.0 &&
323 cfg.magn_prior.getS2() > 0.0;
324}
325
326inline double mapPriorWeight(const MapObjectiveConfig& cfg, Eigen::Index i,
327 Eigen::Index count) noexcept
328{
329 if (cfg.prior_weight.size() != count) return 1.0;
330 const double weight = cfg.prior_weight(i);
331 return std::isfinite(weight) && weight >= 0.0 ? weight : 1.0;
332}
333
334inline bool mapLengthscalePriorActive(const MapObjectiveConfig& cfg) noexcept
335{
336 return cfg.use_configured_lengthscale_prior &&
337 cfg.lengthscale_prior.getS2() > 0.0;
338}
339
364
368 const gpr::EigenMatrix& x, const Eigen::VectorXd& y,
369 const Eigen::VectorXd& w, double current_barrier_strength,
370 const Eigen::VectorXd& lengthscale_rho0,
371 const gpr::ScgOptimizationSettings& settings,
372 bool robust_signal_anchor = true)
373{
375 if (y.size() > 0) {
376 if (robust_signal_anchor) {
377 const Eigen::Index columns = x.cols();
378 Eigen::Index energies = columns > 0 ? y.size() / columns : 0;
379 if (!(energies > 0 && energies < y.size() &&
380 energies * columns == y.size()))
381 energies = y.size();
382 cfg.signal_variance =
383 mapRobustSignalVarianceAnchor(x, energies, y, w);
384 cfg.use_magn_ceiling = std::isfinite(cfg.signal_variance) &&
385 cfg.signal_variance > 1e-6;
386 } else {
387 const double variance = (y.array() - y.mean()).square().mean();
388 cfg.signal_variance = std::max(variance, 1e-6);
389 cfg.use_magn_ceiling = std::isfinite(variance) && variance > 0.0;
390 }
391 }
392 cfg.barrier_strength = std::min(current_barrier_strength, 0.5);
394 cfg.lengthscale_rho0 = lengthscale_rho0;
395 mapConfigFromSettings(settings, cfg);
396 return cfg;
397}
398
402inline void applyMapPenalty(const Eigen::VectorXd& w,
403 const MapObjectiveConfig& cfg, double& energy,
404 Eigen::VectorXd* gradient)
405{
406 if (!std::isfinite(energy)) {
407 if (gradient != nullptr) gradient->setZero();
408 return;
409 }
410 if (w.size() == 0) return;
411
412 const double bayes_prior_weight = mapBayesPriorWeight(cfg);
413 const double half_cauchy_A_sq = mapHalfCauchyASq(cfg.signal_variance);
414
415 // --- magnitude prior on w(0) = log(magnSigma2) ---
416 if (mapLogNormalMagnPriorActive(cfg) && std::isfinite(w(0))) {
417 // log p(x) from PriorLogNormal.h, x = exp(w), plus the log
418 // Jacobian +w. The exp is never formed, so a huge positive
419 // amplitude (w large) stays finite:
420 // -log p_log(w) = 0.5*log(2*pi*s2) + (w - mu)^2 / (2*s2).
421 const double mu = cfg.magn_prior.getMu();
422 const double s2 = cfg.magn_prior.getS2();
423 const double dw = w(0) - mu;
424 energy += bayes_prior_weight *
425 (0.5 * std::log(2.0 * M_PI * s2) + 0.5 * dw * dw / s2);
426 if (gradient != nullptr)
427 (*gradient)(0) += bayes_prior_weight * (dw / s2);
428 } else if (mapMagnPriorActive(cfg)) {
429 // PriorSqrtt on natural-space magnSigma2, reparameterised to log space
430 // with Jacobian log p_log(w) = log p_nat(exp(w)) + w.
431 gpr::PriorSqrtt prior_sqrtt;
432 prior_sqrtt.setParameters(cfg.magn_prior);
433 const double magn = std::exp(w(0));
434 if (magn > 0.0 && std::isfinite(magn)) {
435 gpr::FieldMatrixd x(1, 1);
436 x(0, 0) = magn;
437 const double lp = prior_sqrtt.calculateLogPrior(x);
438 energy += bayes_prior_weight * (-lp - w(0));
439 if (gradient != nullptr) {
440 const gpr::FieldMatrixd lpg =
441 prior_sqrtt.calculateLogPriorGradient(x);
442 (*gradient)(0) +=
443 bayes_prior_weight * (-lpg(0, 0) * magn - 1.0);
444 }
445 }
446 } else {
447 // Default: half-Cauchy C+(0, A) on sigma_m = sqrt(magnSigma2),
448 // log-space Jacobian -0.5 * w.
449 const double sigma_m_sq = std::exp(w(0));
450 const double denom = half_cauchy_A_sq + sigma_m_sq;
451 if (denom > 0.0) {
452 energy += bayes_prior_weight *
453 (std::log1p(sigma_m_sq / half_cauchy_A_sq) - 0.5 * w(0));
454 if (gradient != nullptr) {
455 (*gradient)(0) +=
456 bayes_prior_weight * (sigma_m_sq / denom - 0.5);
457 }
458 }
459 }
460
461 // --- lengthscale priors on w(i) = log(ell_i), i >= 1 ---
462 if (mapLengthscalePriorActive(cfg)) {
463 const bool use_t = cfg.lengthscale_prior.getNu() > 0.0;
464 gpr::PriorT prior_t;
465 gpr::PriorGaussian prior_g;
466 if (use_t)
467 prior_t.setParameters(cfg.lengthscale_prior);
468 else
469 prior_g.setParameters(cfg.lengthscale_prior);
470
471 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
472 const double ell = std::exp(w(i));
473 if (!(ell > 0.0) || !std::isfinite(ell)) continue;
474 const double weight =
475 bayes_prior_weight * mapPriorWeight(cfg, i, w.size());
476 gpr::FieldMatrixd x(1, 1);
477 x(0, 0) = ell;
478 const double lp = use_t ? prior_t.calculateLogPrior(x)
479 : prior_g.calculateLogPrior(x);
480 energy += weight * (-lp - w(i));
481 if (gradient != nullptr) {
482 const gpr::FieldMatrixd lpg =
483 use_t ? prior_t.calculateLogPriorGradient(x)
484 : prior_g.calculateLogPriorGradient(x);
485 (*gradient)(i) += weight * (-lpg(0, 0) * ell - 1.0);
486 }
487 }
488 } else {
489 // Default: half-Student-t(nu=4, sigma_t^2=1) on the DIMENSIONLESS
490 // ratio r = ell/rho0, r^2 = exp(2*w) / rho0^2. rho0
491 // (mapLengthscaleRho0) is the per-pairtype descriptor spread -- a
492 // chemistry scale, not an arbitrary unit -- so "sigma_t^2=1" is now a
493 // legitimate unit scale on a dimensionless quantity instead of an
494 // implicit 1-Angstrom-equivalent constant. Energy also carries the
495 // log|dr/dw| - log|d(ell)/dw| correction +log(rho0): treating the
496 // natural variable as r (not ell) while parameterizing in w=log(ell)
497 // shifts the reparameterization Jacobian by log(rho0), which is what
498 // keeps the whole prior contribution invariant when ell and rho0 are
499 // rescaled by the same constant (descriptor units are arbitrary; the
500 // ratio and this prior are not). rho0=1 (the no-descriptor-scale
501 // fallback) makes log(rho0)=0 and r=ell, reproducing the historical
502 // formula exactly.
503 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
504 const double rho0 = mapLengthscaleRho0(cfg, i);
505 const double r_sq = std::exp(2.0 * w(i)) / (rho0 * rho0);
506 const double denom_ls = kMapStudentTNu * kMapStudentTSigmaSq + r_sq;
507 const double weight =
508 bayes_prior_weight * mapPriorWeight(cfg, i, w.size());
509 if (denom_ls > 0.0) {
510 energy +=
511 weight * (kMapStudentTNorm *
512 std::log1p(r_sq / (kMapStudentTNu *
514 w(i) + std::log(rho0));
515 if (gradient != nullptr) {
516 (*gradient)(i) +=
517 weight *
518 ((2.0 * kMapStudentTNorm) * r_sq / denom_ls - 1.0);
519 }
520 }
521 }
522 }
523
524 if (cfg.use_magn_ceiling) {
525 const double boundary_gap = cfg.max_log_magnSigma2 - w(0);
526 double* grad0 = gradient != nullptr ? &(*gradient)(0) : nullptr;
527 if (!detail::addLogBarrierPenalty(
528 boundary_gap, -1.0, cfg.barrier_strength, energy, grad0)) {
529 energy = std::numeric_limits<double>::infinity();
530 }
531 }
532
533 if (!std::isfinite(energy)) return;
534
535 if (cfg.use_tail_lower_barrier && w.size() > 1) {
536 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
537 const double gap = w(i) - cfg.tail_lower_barrier;
538 double* gradi = gradient != nullptr ? &(*gradient)(i) : nullptr;
539 if (!detail::addLogBarrierPenalty(gap, 1.0, cfg.barrier_strength,
540 energy, gradi)) {
541 energy = std::numeric_limits<double>::infinity();
542 if (gradient != nullptr) gradient->setZero();
543 return;
544 }
545 }
546 }
547}
548
552inline bool applyMapPenaltyGradient(const Eigen::VectorXd& w,
553 const MapObjectiveConfig& cfg,
554 Eigen::VectorXd& gradient)
555{
556 if (gradient.size() != w.size() || w.size() == 0) return false;
557
558 const double bayes_prior_weight = mapBayesPriorWeight(cfg);
559 const double half_cauchy_A_sq = mapHalfCauchyASq(cfg.signal_variance);
560
561 if (mapLogNormalMagnPriorActive(cfg) && std::isfinite(w(0))) {
562 const double dw = w(0) - cfg.magn_prior.getMu();
563 gradient(0) += bayes_prior_weight * (dw / cfg.magn_prior.getS2());
564 } else if (mapMagnPriorActive(cfg)) {
565 gpr::PriorSqrtt prior_sqrtt;
566 prior_sqrtt.setParameters(cfg.magn_prior);
567 const double magn = std::exp(w(0));
568 if (magn > 0.0 && std::isfinite(magn)) {
569 gpr::FieldMatrixd x(1, 1);
570 x(0, 0) = magn;
571 const gpr::FieldMatrixd lpg =
572 prior_sqrtt.calculateLogPriorGradient(x);
573 gradient(0) += bayes_prior_weight * (-lpg(0, 0) * magn - 1.0);
574 }
575 } else {
576 const double sigma_m_sq = std::exp(w(0));
577 const double denom = half_cauchy_A_sq + sigma_m_sq;
578 if (denom > 0.0) {
579 gradient(0) += bayes_prior_weight * (sigma_m_sq / denom - 0.5);
580 }
581 }
582
583 if (mapLengthscalePriorActive(cfg)) {
584 const bool use_t = cfg.lengthscale_prior.getNu() > 0.0;
585 gpr::PriorT prior_t;
586 gpr::PriorGaussian prior_g;
587 if (use_t)
588 prior_t.setParameters(cfg.lengthscale_prior);
589 else
590 prior_g.setParameters(cfg.lengthscale_prior);
591 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
592 const double ell = std::exp(w(i));
593 if (!(ell > 0.0) || !std::isfinite(ell)) continue;
594 gpr::FieldMatrixd x(1, 1);
595 x(0, 0) = ell;
596 const gpr::FieldMatrixd lpg =
597 use_t ? prior_t.calculateLogPriorGradient(x)
598 : prior_g.calculateLogPriorGradient(x);
599 gradient(i) += bayes_prior_weight *
600 mapPriorWeight(cfg, i, w.size()) *
601 (-lpg(0, 0) * ell - 1.0);
602 }
603 } else {
604 // Mirrors the applyMapPenalty default branch: r = ell/rho0, the
605 // +log(rho0) energy term is a constant in w(i) so the gradient is
606 // unaffected by it (see applyMapPenalty for the full derivation).
607 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
608 const double rho0 = mapLengthscaleRho0(cfg, i);
609 const double r_sq = std::exp(2.0 * w(i)) / (rho0 * rho0);
610 const double denom_ls = kMapStudentTNu * kMapStudentTSigmaSq + r_sq;
611 if (denom_ls > 0.0) {
612 gradient(i) +=
613 bayes_prior_weight * mapPriorWeight(cfg, i, w.size()) *
614 ((2.0 * kMapStudentTNorm) * r_sq / denom_ls - 1.0);
615 }
616 }
617 }
618
619 if (cfg.use_magn_ceiling) {
620 const double boundary_gap = cfg.max_log_magnSigma2 - w(0);
621 double ignored_energy = 0.0;
622 double* grad0 = &gradient(0);
623 if (!detail::addLogBarrierPenalty(boundary_gap, -1.0,
624 cfg.barrier_strength, ignored_energy,
625 grad0)) {
626 gradient.setZero();
627 return false;
628 }
629 }
630
631 if (cfg.use_tail_lower_barrier && w.size() > 1) {
632 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
633 const double gap = w(i) - cfg.tail_lower_barrier;
634 double ignored_energy = 0.0;
635 double* gradi = &gradient(i);
636 if (!detail::addLogBarrierPenalty(gap, 1.0, cfg.barrier_strength,
637 ignored_energy, gradi)) {
638 gradient.setZero();
639 return false;
640 }
641 }
642 }
643
644 return gradient.allFinite();
645}
646
650inline double mapPenaltyDirectionalCurvature(const Eigen::VectorXd& w,
651 const Eigen::VectorXd& direction,
652 const MapObjectiveConfig& cfg)
653{
654 if (w.size() == 0 || direction.size() != w.size()) return 0.0;
655
656 const double bayes_prior_weight = mapBayesPriorWeight(cfg);
657 const double half_cauchy_A_sq = mapHalfCauchyASq(cfg.signal_variance);
658 double barrier_curvature = 0.0;
659
660 if (mapLogNormalMagnPriorActive(cfg) && std::isfinite(w(0))) {
661 barrier_curvature += bayes_prior_weight * direction(0) * direction(0) /
662 cfg.magn_prior.getS2();
663 } else if (mapMagnPriorActive(cfg)) {
664 const double root = std::exp(0.5 * w(0));
665 const double mu = cfg.magn_prior.getMu();
666 const double a = cfg.magn_prior.getNu() * cfg.magn_prior.getS2();
667 const double centered = root - mu;
668 const double denominator = a + centered * centered;
669 const double curvature =
670 0.25 * (cfg.magn_prior.getNu() + 1.0) * root *
671 ((2.0 * root - mu) * a - mu * centered * centered) /
672 (denominator * denominator);
673 barrier_curvature +=
674 bayes_prior_weight * curvature * direction(0) * direction(0);
675 } else {
676 const double sigma_m_sq = std::exp(w(0));
677 const double denom = half_cauchy_A_sq + sigma_m_sq;
678 const double half_cauchy_curv =
679 denom > 0.0 ? half_cauchy_A_sq * sigma_m_sq / (denom * denom) : 0.0;
680 barrier_curvature +=
681 bayes_prior_weight * half_cauchy_curv * direction(0) * direction(0);
682 }
683
684 if (mapLengthscalePriorActive(cfg)) {
685 const double mu = cfg.lengthscale_prior.getMu();
686 const double s2 = cfg.lengthscale_prior.getS2();
687 const double nu = cfg.lengthscale_prior.getNu();
688 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
689 const double ell = std::exp(w(i));
690 double curvature = ell * (2.0 * ell - mu) / s2;
691 if (nu > 0.0) {
692 const double centered = ell - mu;
693 const double a = nu * s2;
694 const double denominator = a + centered * centered;
695 curvature = (nu + 1.0) * ell *
696 ((2.0 * ell - mu) * a - mu * centered * centered) /
697 (denominator * denominator);
698 }
699 barrier_curvature += bayes_prior_weight *
700 mapPriorWeight(cfg, i, w.size()) * curvature *
701 direction(i) * direction(i);
702 }
703 } else {
704 // r = ell/rho0 in place of ell (see applyMapPenalty); the constant
705 // +log(rho0) energy term has zero second derivative so it drops out
706 // of the curvature entirely.
707 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
708 const double rho0 = mapLengthscaleRho0(cfg, i);
709 const double r_sq = std::exp(2.0 * w(i)) / (rho0 * rho0);
710 const double denom_ls = kMapStudentTNu * kMapStudentTSigmaSq + r_sq;
711 if (denom_ls > 0.0) {
712 const double ls_curv = 4.0 * kMapStudentTNu * kMapStudentTNorm *
713 kMapStudentTSigmaSq * r_sq /
714 (denom_ls * denom_ls);
715 barrier_curvature += bayes_prior_weight *
716 mapPriorWeight(cfg, i, w.size()) *
717 ls_curv * direction(i) * direction(i);
718 }
719 }
720 }
721
722 if (cfg.use_magn_ceiling) {
723 const double boundary_gap = cfg.max_log_magnSigma2 - w(0);
724 barrier_curvature += detail::logBarrierDirectionalCurvature(
725 boundary_gap, -direction(0), cfg.barrier_strength);
726 }
727 if (cfg.use_tail_lower_barrier && w.size() > 1) {
728 for (Eigen::Index i = 1; i < mapLengthscaleEnd(cfg, w.size()); ++i) {
729 const double gap = w(i) - cfg.tail_lower_barrier;
730 barrier_curvature += detail::logBarrierDirectionalCurvature(
731 gap, direction(i), cfg.barrier_strength);
732 }
733 }
734
735 return barrier_curvature;
736}
737
741inline void mapPenaltyHessianVector(const Eigen::VectorXd& w,
742 const Eigen::VectorXd& direction,
743 const MapObjectiveConfig& cfg,
744 Eigen::VectorXd& product)
745{
746 if (product.size() != w.size() || direction.size() != w.size()) {
747 product.setConstant(std::numeric_limits<double>::quiet_NaN());
748 return;
749 }
750 for (Eigen::Index i = 0; i < w.size(); ++i) {
751 Eigen::VectorXd axis = Eigen::VectorXd::Zero(w.size());
752 axis(i) = 1.0;
753 const double diagonal = mapPenaltyDirectionalCurvature(w, axis, cfg);
754 if (!std::isfinite(diagonal)) {
755 product(i) = diagonal;
756 continue;
757 }
758 product(i) += diagonal * direction(i);
759 }
760}
761
762} // namespace funcmin
763
764#endif // GPR_ML_MAP_OBJECTIVE_H_
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
Class of prior parameters.
Definition PriorBase.h:23
void setMu(const double value)
Definition PriorBase.h:63
void setS2(const double value)
Definition PriorBase.h:68
void setNu(const double value)
Definition PriorBase.h:73
void setParameters(const PriorBase &other)
Definition PriorBase.h:78
double getMu() const
Definition PriorBase.h:48
double getS2() const
Definition PriorBase.h:53
double getNu() const
Definition PriorBase.h:58
Gaussian prior.
double calculateLogPrior(const FieldMatrixd &x) override
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
Student-t prior for the square root of the parameter.
Definition PriorSqrtt.h:31
double calculateLogPrior(const FieldMatrixd &x) override
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
Student-t prior.
Definition PriorT.h:34
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
Definition PriorT.cpp:45
double calculateLogPrior(const FieldMatrixd &x) override
Definition PriorT.cpp:22
double mapMedianInPlace(std::vector< double > &v) noexcept
void restoreMapAmplitudeFeasibility(Eigen::VectorXd &w, const MapObjectiveConfig &cfg)
constexpr double kMapStudentTNorm
double mapForceImpliedSignalVariance(const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)
double mapHalfCauchyASq(double signal_variance) noexcept
MapObjectiveConfig makeMapObjectiveConfig(const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &w, double current_barrier_strength, const Eigen::VectorXd &lengthscale_rho0, const gpr::ScgOptimizationSettings &settings, bool robust_signal_anchor=true)
void applyMapPenalty(const Eigen::VectorXd &w, const MapObjectiveConfig &cfg, double &energy, Eigen::VectorXd *gradient)
double mapLengthscaleRho0(const MapObjectiveConfig &cfg, Eigen::Index i) noexcept
bool mapLogNormalMagnPriorActive(const MapObjectiveConfig &cfg) noexcept
bool applyMapPenaltyGradient(const Eigen::VectorXd &w, const MapObjectiveConfig &cfg, Eigen::VectorXd &gradient)
double mapMaxLogMagnSigma2(double signal_variance) noexcept
constexpr double kMapStudentTNu
Weakly-informative Student-t hyperparameters on lengthscales.
void mapPenaltyHessianVector(const Eigen::VectorXd &w, const Eigen::VectorXd &direction, const MapObjectiveConfig &cfg, Eigen::VectorXd &product)
constexpr double kMapStudentTSigmaSq
bool mapParametersInDomain(const Eigen::VectorXd &w, const MapObjectiveConfig &cfg)
Eigen::Index mapLengthscaleEnd(const MapObjectiveConfig &cfg, Eigen::Index size) noexcept
double mapRobustMadVariance(const Eigen::VectorXd &block)
void projectMapGradient(const MapObjectiveConfig &cfg, Eigen::VectorXd &gradient)
bool mapLengthscalePriorActive(const MapObjectiveConfig &cfg) noexcept
void mapConfigFromSettings(const gpr::ScgOptimizationSettings &settings, MapObjectiveConfig &cfg)
double mapBayesPriorWeight(const MapObjectiveConfig &cfg) noexcept
double mapPriorWeight(const MapObjectiveConfig &cfg, Eigen::Index i, Eigen::Index count) noexcept
double mapPenaltyDirectionalCurvature(const Eigen::VectorXd &w, const Eigen::VectorXd &direction, const MapObjectiveConfig &cfg)
bool mapMagnPriorActive(const MapObjectiveConfig &cfg) noexcept
double mapRobustSignalVarianceAnchor(const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)
constexpr double kMadConsistencyConstant
MapPriorWeightMode
Definition Structures.h:100
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
double barrier_strength
Already capped with min(current_barrier_strength, 0.5) by the caller.
Eigen::VectorXd prior_weight
Eigen::VectorXd lengthscale_rho0
Eigen::VectorXd parameter_lower
Eigen::VectorXd parameter_upper
gpr::PriorBase lengthscale_prior
std::vector< bool > active_parameters
gpr::MapPriorWeightMode weight_mode
SCG-specific settings (trust-region scale, log-magnSigma2 barriers, curvature mode).
Definition Structures.h:246
MapPriorWeightMode map_prior_weight_mode
Annealed (default) or full prior weight; see MapPriorWeightMode.
Definition Structures.h:255
std::vector< bool > active_parameters
Definition Structures.h:280