Loading...
Searching...
No Matches
funcmin Namespace Reference

Classes

class  ADAM
 Adaptive Moment Estimation (ADAM) optimizer. More...
class  Ceres
 Wrapper for the Ceres Solver for robust nonlinear optimization. More...
class  GprCostFunction
struct  MapObjectiveConfig
class  RgminCallbackFailure
class  RgminLbfgs
 L-BFGS waist through rgmin. The loop lives in Rust. More...
struct  RgminNlmlCtx
class  RgminOpt
class  RgminScg
struct  RgminTensorDeleter

Concepts

concept  NlmlHessianVectorHolder

Typedefs

using ParamOptimizer = std::variant<RgminScg, RgminOpt>
using RgminTensorHandle

Functions

bool mapParametersInDomain (const Eigen::VectorXd &w, const MapObjectiveConfig &cfg)
void projectMapGradient (const MapObjectiveConfig &cfg, Eigen::VectorXd &gradient)
double mapHalfCauchyASq (double signal_variance) noexcept
double mapMaxLogMagnSigma2 (double signal_variance) noexcept
void restoreMapAmplitudeFeasibility (Eigen::VectorXd &w, const MapObjectiveConfig &cfg)
double mapLengthscaleRho0 (const MapObjectiveConfig &cfg, Eigen::Index i) noexcept
Eigen::Index mapLengthscaleEnd (const MapObjectiveConfig &cfg, Eigen::Index size) noexcept
double mapMedianInPlace (std::vector< double > &v) noexcept
double mapRobustMadVariance (const Eigen::VectorXd &block)
double mapForceImpliedSignalVariance (const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)
double mapRobustSignalVarianceAnchor (const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)
double mapBayesPriorWeight (const MapObjectiveConfig &cfg) noexcept
bool mapLogNormalMagnPriorActive (const MapObjectiveConfig &cfg) noexcept
bool mapMagnPriorActive (const MapObjectiveConfig &cfg) noexcept
double mapPriorWeight (const MapObjectiveConfig &cfg, Eigen::Index i, Eigen::Index count) noexcept
bool mapLengthscalePriorActive (const MapObjectiveConfig &cfg) noexcept
void mapConfigFromSettings (const gpr::ScgOptimizationSettings &settings, MapObjectiveConfig &cfg)
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)
bool applyMapPenaltyGradient (const Eigen::VectorXd &w, const MapObjectiveConfig &cfg, Eigen::VectorXd &gradient)
double mapPenaltyDirectionalCurvature (const Eigen::VectorXd &w, const Eigen::VectorXd &direction, const MapObjectiveConfig &cfg)
void mapPenaltyHessianVector (const Eigen::VectorXd &w, const Eigen::VectorXd &direction, const MapObjectiveConfig &cfg, Eigen::VectorXd &product)
template<typename Holder>
bool holderHasNlmlHessianVector (Holder &holder)
template<typename Holder>
bool holderNlmlHessianVector (Holder &holder, const Eigen::VectorXd &x_ind, const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, Eigen::VectorXd &product)
template<typename Holder>
bool holderNlmlDirectionalCurvature (Holder &holder, const Eigen::VectorXd &x_ind, const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, double &curvature)
Eigen::Map< const Eigen::VectorXd > tensorToVec (const DLManagedTensorVersioned *tensor)
Eigen::Map< Eigen::VectorXd > tensorToVec (DLManagedTensorVersioned *tensor)
template<typename Ctx>
rgmin_status_t rgminEval (void *user, const DLManagedTensorVersioned *x, double *value_out) noexcept
template<typename Ctx>
rgmin_status_t rgminGrad (void *user, const DLManagedTensorVersioned *x, DLManagedTensorVersioned *grad_out) noexcept
template<typename Ctx>
rgmin_status_t rgminCurv (void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept
template<typename Ctx>
rgmin_status_t rgminNlmlHessianVectorCurv (void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept

Variables

constexpr double kMapStudentTNu = 4.0
 Weakly-informative Student-t hyperparameters on lengthscales.
constexpr double kMapStudentTSigmaSq = 1.0
constexpr double kMapStudentTNorm = 0.5 * (kMapStudentTNu + 1.0)
constexpr double kMadConsistencyConstant = 1.4826

Typedef Documentation

◆ ParamOptimizer

using funcmin::ParamOptimizer = std::variant<RgminScg, RgminOpt>

Definition at line 26 of file ParamOpt.h.

◆ RgminTensorHandle

Initial value:
std::unique_ptr<DLManagedTensorVersioned, RgminTensorDeleter>

Definition at line 36 of file RgminObjective.h.

Function Documentation

◆ applyMapPenalty()

void funcmin::applyMapPenalty ( const Eigen::VectorXd & w,
const MapObjectiveConfig & cfg,
double & energy,
Eigen::VectorXd * gradient )
inline

Apply MAP prior penalties (energy + optional gradient) and log-barriers. Energy is assumed to already hold the base NLML. On barrier violation sets energy to +inf; when gradient is non-null, zeros it on tail-barrier failure.

Definition at line 402 of file MapObjective.h.

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}
@ x
Definition Enums.h:28
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
double calculateLogPrior(const FieldMatrixd &x) override
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
double calculateLogPrior(const FieldMatrixd &x) override
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
FieldMatrixd calculateLogPriorGradient(const FieldMatrixd &x) override
Definition PriorT.cpp:45
double calculateLogPrior(const FieldMatrixd &x) override
Definition PriorT.cpp:22
constexpr double kMapStudentTNorm
double mapHalfCauchyASq(double signal_variance) noexcept
double mapLengthscaleRho0(const MapObjectiveConfig &cfg, Eigen::Index i) noexcept
bool mapLogNormalMagnPriorActive(const MapObjectiveConfig &cfg) noexcept
constexpr double kMapStudentTNu
Weakly-informative Student-t hyperparameters on lengthscales.
constexpr double kMapStudentTSigmaSq
Eigen::Index mapLengthscaleEnd(const MapObjectiveConfig &cfg, Eigen::Index size) noexcept
bool mapLengthscalePriorActive(const MapObjectiveConfig &cfg) noexcept
double mapBayesPriorWeight(const MapObjectiveConfig &cfg) noexcept
double mapPriorWeight(const MapObjectiveConfig &cfg, Eigen::Index i, Eigen::Index count) noexcept
bool mapMagnPriorActive(const MapObjectiveConfig &cfg) noexcept
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.
gpr::PriorBase lengthscale_prior

◆ applyMapPenaltyGradient()

bool funcmin::applyMapPenaltyGradient ( const Eigen::VectorXd & w,
const MapObjectiveConfig & cfg,
Eigen::VectorXd & gradient )
inline

Gradient-only MAP prior + barrier contributions (the path that reuses an accepted energy probe). Returns false and zeros gradient on barrier failure or non-finite result.

Definition at line 552 of file MapObjective.h.

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}
Student-t prior for the square root of the parameter.
Definition PriorSqrtt.h:31

◆ holderHasNlmlHessianVector()

template<typename Holder>
bool funcmin::holderHasNlmlHessianVector ( Holder & holder)
nodiscard

True when the optimizer's holder offers the exact Hessian-vector product of the objective it currently evaluates.

Definition at line 32 of file NlmlCurvature.h.

33{
34 if constexpr (NlmlHessianVectorHolder<Holder>) {
35 return holder.exactNlmlHessianVectorAvailable();
36 } else {
37 return false;
38 }
39}

◆ holderNlmlDirectionalCurvature()

template<typename Holder>
bool funcmin::holderNlmlDirectionalCurvature ( Holder & holder,
const Eigen::VectorXd & x_ind,
const gpr::EigenMatrix & x,
const Eigen::VectorXd & y,
const Eigen::VectorXd & direction,
double & curvature )

d^T H d from the holder's exact NLML Hessian-vector product. False, with a NaN curvature, when the holder has no such product or it fails.

Definition at line 64 of file NlmlCurvature.h.

70{
71 curvature = std::numeric_limits<double>::quiet_NaN();
72 Eigen::VectorXd product;
73 if (!holderNlmlHessianVector(holder, x_ind, x, y, direction, product)) {
74 return false;
75 }
76 curvature = direction.dot(product);
77 return std::isfinite(curvature);
78}
@ y
Definition Enums.h:28
bool holderNlmlHessianVector(Holder &holder, const Eigen::VectorXd &x_ind, const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, Eigen::VectorXd &product)

◆ holderNlmlHessianVector()

template<typename Holder>
bool funcmin::holderNlmlHessianVector ( Holder & holder,
const Eigen::VectorXd & x_ind,
const gpr::EigenMatrix & x,
const Eigen::VectorXd & y,
const Eigen::VectorXd & direction,
Eigen::VectorXd & product )

The holder's exact NLML Hessian-vector product H v. False when the holder has no such product, it is unavailable, or it fails or returns a vector of the wrong size or a non-finite one.

Definition at line 45 of file NlmlCurvature.h.

50{
51 if constexpr (NlmlHessianVectorHolder<Holder>) {
52 if (!holder.exactNlmlHessianVectorAvailable()) return false;
53 return holder.evaluateNlmlHessianVector(x_ind, x, y, direction,
54 product) &&
55 product.size() == direction.size() && product.allFinite();
56 } else {
57 return false;
58 }
59}

◆ makeMapObjectiveConfig()

MapObjectiveConfig funcmin::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 )
inline

Build the objective once from the observations and initial parameters. GP fits use the robust energy and force anchor with every minimizer.

Definition at line 367 of file MapObjective.h.

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}
double mapMaxLogMagnSigma2(double signal_variance) noexcept
void mapConfigFromSettings(const gpr::ScgOptimizationSettings &settings, MapObjectiveConfig &cfg)
double mapRobustSignalVarianceAnchor(const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)
Eigen::VectorXd lengthscale_rho0

◆ mapBayesPriorWeight()

double funcmin::mapBayesPriorWeight ( const MapObjectiveConfig & cfg)
inlinenoexcept

Prior weight for the bayesian prior terms. Barrier terms always use barrier_strength regardless of weight_mode.

Definition at line 307 of file MapObjective.h.

308{
309 if (cfg.weight_mode == gpr::MapPriorWeightMode::Full) return 1.0;
310 return cfg.barrier_strength;
311}
gpr::MapPriorWeightMode weight_mode

◆ mapConfigFromSettings()

void funcmin::mapConfigFromSettings ( const gpr::ScgOptimizationSettings & settings,
MapObjectiveConfig & cfg )
inline

Fill MapObjectiveConfig prior fields and weight mode from the rgmin optimizer settings (populated by Optimize.cpp / unit tests).

Definition at line 342 of file MapObjective.h.

344{
345 cfg.weight_mode = settings.map_prior_weight_mode;
352 cfg.magn_prior.setMu(settings.map_magn_prior_mu);
353 cfg.magn_prior.setS2(settings.map_magn_prior_s2);
354 cfg.magn_prior.setNu(settings.map_magn_prior_nu);
355 }
362 }
363}
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
std::vector< bool > active_parameters
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

◆ mapForceImpliedSignalVariance()

double funcmin::mapForceImpliedSignalVariance ( const gpr::EigenMatrix & x,
Eigen::Index n_e,
const Eigen::VectorXd & y,
const Eigen::VectorXd & w )
inline

Force-implied signal-variance floor from the SExpAt kernel Hessian identity Var[dE/dc] = sf2 * sum_p (dd_p/dc)^2 / l_p^2 for descriptors d_p = 1/r_p (verified symbolically in tests/symbolic/test_sexpat_force_variance.py). The exact chain rule gives dd_p/dc = -Delta_c / r_p^3 for a single Cartesian coordinate c of one of the pair's two atoms (Delta_c the corresponding coordinate difference), so abs(dd_p/dc) <= 1/r_p^2, tight when the pair lies along c.

SCG anchor time has the training Cartesian coordinates (the energy block of x, rows [0, n_e), columns [0, x.cols()-1) once the trailing block-index column is dropped – see AuxiliaryFunctionality::assembleMatrixOfRepetitiveCoordinates) and the current lengthscales (w(i) = log(l_i), i = 1..P), but not the pairtype-to-atom-pair map (that lives in AtomsConfiguration, only available at the covariance-function layer), so the true l_p for each atom pair is unknown here. J_med decouples the sum into two robust medians instead: the median inverse-lengthscale-squared over the current pairtypes, times the squared median of the tight per-pair bound 1/r_p^2 over every atom pair and every training row in the energy block.

Definition at line 235 of file MapObjective.h.

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}
double mapMedianInPlace(std::vector< double > &v) noexcept
double mapRobustMadVariance(const Eigen::VectorXd &block)

◆ mapHalfCauchyASq()

double funcmin::mapHalfCauchyASq ( double signal_variance)
inlinenoexcept

Definition at line 114 of file MapObjective.h.

115{
116 return 2.0 * signal_variance;
117}

◆ mapLengthscaleEnd()

Eigen::Index funcmin::mapLengthscaleEnd ( const MapObjectiveConfig & cfg,
Eigen::Index size )
inlinenoexcept

One past the last length-scale coordinate. A negative count keeps the historical rule that every coordinate after the magnitude is a length scale.

Definition at line 166 of file MapObjective.h.

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}

◆ mapLengthscalePriorActive()

bool funcmin::mapLengthscalePriorActive ( const MapObjectiveConfig & cfg)
inlinenoexcept

Definition at line 334 of file MapObjective.h.

335{
337 cfg.lengthscale_prior.getS2() > 0.0;
338}

◆ mapLengthscaleRho0()

double funcmin::mapLengthscaleRho0 ( const MapObjectiveConfig & cfg,
Eigen::Index i )
inlinenoexcept

rho0_k lookup for w(i), i = 1..w.size()-1 (lengthscale block). Returns cfg.lengthscale_rho0(i - 1) when that entry is finite and positive; otherwise returns 1.0, exactly reproducing the pre-anchor Student-t prior (raw ell_i in implicit unit-Angstrom-equivalent descriptor units) for that index. No global on/off switch: an empty or short lengthscale_rho0 vector falls back index-by-index, so a partially-degenerate descriptor spread (one pairtype with too few observations) only loses scale-freedom for that one pairtype.

Definition at line 153 of file MapObjective.h.

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}

◆ mapLogNormalMagnPriorActive()

bool funcmin::mapLogNormalMagnPriorActive ( const MapObjectiveConfig & cfg)
inlinenoexcept

Definition at line 313 of file MapObjective.h.

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}

◆ mapMagnPriorActive()

bool funcmin::mapMagnPriorActive ( const MapObjectiveConfig & cfg)
inlinenoexcept

Definition at line 320 of file MapObjective.h.

321{
322 return cfg.use_configured_magn_prior && cfg.magn_prior.getNu() > 0.0 &&
323 cfg.magn_prior.getS2() > 0.0;
324}

◆ mapMaxLogMagnSigma2()

double funcmin::mapMaxLogMagnSigma2 ( double signal_variance)
inlinenoexcept

Definition at line 119 of file MapObjective.h.

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}

◆ mapMedianInPlace()

double funcmin::mapMedianInPlace ( std::vector< double > & v)
inlinenoexcept

Median of a vector, sorting in place (even-length: mean of the two middle elements).

Definition at line 183 of file MapObjective.h.

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}

◆ mapParametersInDomain()

bool funcmin::mapParametersInDomain ( const Eigen::VectorXd & w,
const MapObjectiveConfig & cfg )
inline

Reject invalid trials before invoking a covariance objective. Infinite endpoints leave a side unbounded; NaN or mismatched bounds are invalid.

Definition at line 78 of file MapObjective.h.

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}
Eigen::VectorXd parameter_lower
Eigen::VectorXd parameter_upper

◆ mapPenaltyDirectionalCurvature()

double funcmin::mapPenaltyDirectionalCurvature ( const Eigen::VectorXd & w,
const Eigen::VectorXd & direction,
const MapObjectiveConfig & cfg )
inline

Directional curvature of the selected MAP prior and barrier terms. Natural-space priors include their log-coordinate Jacobian, and tied parameters carry the same prior weights as applyMapPenalty.

Definition at line 650 of file MapObjective.h.

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}

◆ mapPenaltyHessianVector()

void funcmin::mapPenaltyHessianVector ( const Eigen::VectorXd & w,
const Eigen::VectorXd & direction,
const MapObjectiveConfig & cfg,
Eigen::VectorXd & product )
inline

Hessian-vector product of the MAP penalty. The penalty Hessian is diagonal in the log coordinates, so column i is mapPenaltyDirectionalCurvature(e_i) times the i-th component of the direction.

Definition at line 741 of file MapObjective.h.

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}
double mapPenaltyDirectionalCurvature(const Eigen::VectorXd &w, const Eigen::VectorXd &direction, const MapObjectiveConfig &cfg)

◆ mapPriorWeight()

double funcmin::mapPriorWeight ( const MapObjectiveConfig & cfg,
Eigen::Index i,
Eigen::Index count )
inlinenoexcept

Definition at line 326 of file MapObjective.h.

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}
Eigen::VectorXd prior_weight

◆ mapRobustMadVariance()

double funcmin::mapRobustMadVariance ( const Eigen::VectorXd & block)
inline

Robust (MAD-based) variance of a data block: sigma_robust = kMadConsistencyConstant * median(abs(v - median(v))), var = sigma_robust^2. Falls back to the plain sample variance when the MAD is degenerate (block constant or empty), matching the un-robust estimator in that limit.

Definition at line 195 of file MapObjective.h.

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}
constexpr double kMadConsistencyConstant

◆ mapRobustSignalVarianceAnchor()

double funcmin::mapRobustSignalVarianceAnchor ( const gpr::EigenMatrix & x,
Eigen::Index n_e,
const Eigen::VectorXd & y,
const Eigen::VectorXd & w )
inline

Force-aware signal-variance anchor for the magnSigma2 prior and ceiling. The GP trains on energies AND gradients under one shared amplitude; an energy-channel robust variance is blind to the force scale the kernel must also represent, so the half-Cauchy prior scale and the ceiling built from it (mapMaxLogMagnSigma2) can pin magnSigma2 below what the observed force residuals need to be representable as posterior uncertainty. The anchor takes the larger of the energy-block MAD variance and the force-implied floor (mapForceImpliedSignalVariance), then floors at 1e-6 so a degenerate (all-equal) target block never yields a zero or negative ceiling.

Definition at line 295 of file MapObjective.h.

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}
double mapForceImpliedSignalVariance(const gpr::EigenMatrix &x, Eigen::Index n_e, const Eigen::VectorXd &y, const Eigen::VectorXd &w)

◆ projectMapGradient()

void funcmin::projectMapGradient ( const MapObjectiveConfig & cfg,
Eigen::VectorXd & gradient )
inline

Restrict the optimizer gradient while retaining the full objective and covariance. Empty masks leave the complete parameter space available.

Definition at line 94 of file MapObjective.h.

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}

◆ restoreMapAmplitudeFeasibility()

void funcmin::restoreMapAmplitudeFeasibility ( Eigen::VectorXd & w,
const MapObjectiveConfig & cfg )
inline

Preserve a feasible warm start. An amplitude outside the fitted MAP domain returns to the half-Cauchy prior scale, with every other parameter unchanged. The gap to the ceiling is log(100*s)-log(2*s)=log(50)>0.

Definition at line 135 of file MapObjective.h.

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}

◆ rgminCurv()

template<typename Ctx>
rgmin_status_t funcmin::rgminCurv ( void * user,
const DLManagedTensorVersioned * x,
const DLManagedTensorVersioned * d,
double * curv_out )
noexcept

Definition at line 391 of file RgminObjective.h.

394{
395 auto* ctx = static_cast<Ctx*>(user);
396 *curv_out = std::numeric_limits<double>::quiet_NaN();
397 if (ctx->failure.failed()) return RGMIN_INTERNAL_ERROR;
398 if (!ctx->exact_curvature) return RGMIN_INVALID_PARAMETER;
399 try {
400 const Eigen::VectorXd w = tensorToVec(x);
401 const Eigen::VectorXd dir = tensorToVec(d);
402 double exact = std::numeric_limits<double>::quiet_NaN();
403 if (!ctx->holder.evaluateDirectionalCurvature(ctx->x_ind, ctx->x,
404 ctx->y, dir, exact) ||
405 !std::isfinite(exact)) {
406 return RGMIN_INVALID_PARAMETER;
407 }
408 *curv_out =
409 exact + mapPenaltyDirectionalCurvature(w, dir, ctx->map_cfg);
410 return RGMIN_SUCCESS;
411 } catch (...) {
412 return ctx->captureCallbackError("curvature");
413 }
414}
eon_relax_band_t eon_relax_surface_fn void * user
Eigen::Map< const Eigen::VectorXd > tensorToVec(const DLManagedTensorVersioned *tensor)

◆ rgminEval()

template<typename Ctx>
rgmin_status_t funcmin::rgminEval ( void * user,
const DLManagedTensorVersioned * x,
double * value_out )
noexcept

Definition at line 342 of file RgminObjective.h.

344{
345 auto* ctx = static_cast<Ctx*>(user);
346 *value_out = std::numeric_limits<double>::infinity();
347 if (ctx->failure.failed()) return RGMIN_INTERNAL_ERROR;
348 try {
349 const Eigen::VectorXd w = tensorToVec(x);
350 const auto& sample = ctx->evalAt(w);
351 double energy = sample.energy;
352 if (sample.gradient.size() != w.size() || !std::isfinite(energy))
353 return RGMIN_SUCCESS;
354 applyMapPenalty(w, ctx->map_cfg, energy, nullptr);
355 *value_out = energy;
356 return RGMIN_SUCCESS;
357 } catch (...) {
358 return ctx->captureCallbackError("objective");
359 }
360}
void applyMapPenalty(const Eigen::VectorXd &w, const MapObjectiveConfig &cfg, double &energy, Eigen::VectorXd *gradient)

◆ rgminGrad()

template<typename Ctx>
rgmin_status_t funcmin::rgminGrad ( void * user,
const DLManagedTensorVersioned * x,
DLManagedTensorVersioned * grad_out )
noexcept

Definition at line 363 of file RgminObjective.h.

365{
366 auto* ctx = static_cast<Ctx*>(user);
367 auto* gp = reinterpret_cast<double*>(
368 static_cast<uint8_t*>(grad_out->dl_tensor.data) +
369 static_cast<std::size_t>(grad_out->dl_tensor.byte_offset));
370 Eigen::Map<Eigen::VectorXd> gout(gp, grad_out->dl_tensor.shape[0]);
371 gout.setConstant(std::numeric_limits<double>::quiet_NaN());
372 if (ctx->failure.failed()) return RGMIN_INTERNAL_ERROR;
373 try {
374 const Eigen::VectorXd w = tensorToVec(x);
375 const auto& sample = ctx->evalAt(w);
376 double energy = sample.energy;
377 Eigen::VectorXd g = sample.gradient;
378 if (g.size() != w.size() || g.size() != gout.size() ||
379 !std::isfinite(energy))
380 return RGMIN_SUCCESS;
381 applyMapPenalty(w, ctx->map_cfg, energy, &g);
382 projectMapGradient(ctx->map_cfg, g);
383 gout = g;
384 return RGMIN_SUCCESS;
385 } catch (...) {
386 return ctx->captureCallbackError("gradient");
387 }
388}
void projectMapGradient(const MapObjectiveConfig &cfg, Eigen::VectorXd &gradient)

◆ rgminNlmlHessianVectorCurv()

template<typename Ctx>
rgmin_status_t funcmin::rgminNlmlHessianVectorCurv ( void * user,
const DLManagedTensorVersioned * x,
const DLManagedTensorVersioned * d,
double * curv_out )
noexcept

Curvature of the nlml_hessian_vector mode: d^T H d from the holder's exact NLML Hessian-vector product plus the MAP penalty. A product that fails or is not finite sets hessian_vector_fallback and answers RGMIN_UNAVAILABLE, for this and every later direction; rgmin then takes the curvature from its finite-difference probe, the finite_difference mode's path. An exception from the holder fails the context as in rgminCurv.

Definition at line 423 of file RgminObjective.h.

427{
428 auto* ctx = static_cast<Ctx*>(user);
429 *curv_out = std::numeric_limits<double>::quiet_NaN();
430 if (ctx->failure.failed()) return RGMIN_INTERNAL_ERROR;
431 if (ctx->hessian_vector_fallback) return RGMIN_UNAVAILABLE;
432 try {
433 const Eigen::VectorXd w = tensorToVec(x);
434 const Eigen::VectorXd dir = tensorToVec(d);
435 double exact = std::numeric_limits<double>::quiet_NaN();
436 const bool product = holderNlmlDirectionalCurvature(
437 ctx->holder, ctx->x_ind, ctx->x, ctx->y, dir, exact);
438 const double curvature =
439 exact + mapPenaltyDirectionalCurvature(w, dir, ctx->map_cfg);
440 if (!product || !std::isfinite(curvature)) {
441 ctx->hessian_vector_fallback = true;
442 return RGMIN_UNAVAILABLE;
443 }
444 *curv_out = curvature;
445 return RGMIN_SUCCESS;
446 } catch (...) {
447 return ctx->captureCallbackError("curvature");
448 }
449}
bool holderNlmlDirectionalCurvature(Holder &holder, const Eigen::VectorXd &x_ind, const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, double &curvature)

◆ tensorToVec() [1/2]

Eigen::Map< const Eigen::VectorXd > funcmin::tensorToVec ( const DLManagedTensorVersioned * tensor)

Definition at line 40 of file RgminObjective.cpp.

42{
43 const auto& view = tensor->dl_tensor;
44 const auto* data = reinterpret_cast<const double*>(
45 static_cast<const uint8_t*>(view.data) +
46 static_cast<std::size_t>(view.byte_offset));
47 return Eigen::Map<const Eigen::VectorXd>(data, view.shape[0]);
48}

◆ tensorToVec() [2/2]

Eigen::Map< Eigen::VectorXd > funcmin::tensorToVec ( DLManagedTensorVersioned * tensor)

Definition at line 50 of file RgminObjective.cpp.

52{
53 auto& view = tensor->dl_tensor;
54 auto* data =
55 reinterpret_cast<double*>(static_cast<uint8_t*>(view.data) +
56 static_cast<std::size_t>(view.byte_offset));
57 return Eigen::Map<Eigen::VectorXd>(data, view.shape[0]);
58}

Variable Documentation

◆ kMadConsistencyConstant

double funcmin::kMadConsistencyConstant = 1.4826
inlineconstexpr

Standard-normal consistency constant for the median absolute deviation: sigma_hat = kMadConsistencyConstant * median(abs(v - median(v))) is a consistent estimator of the standard deviation of an underlying normal (Ruppert, "Statistics and Finance", 2004, sec 3.2).

Definition at line 179 of file MapObjective.h.

◆ kMapStudentTNorm

double funcmin::kMapStudentTNorm = 0.5 * (kMapStudentTNu + 1.0)
inlineconstexpr

Definition at line 112 of file MapObjective.h.

◆ kMapStudentTNu

double funcmin::kMapStudentTNu = 4.0
inlineconstexpr

Weakly-informative Student-t hyperparameters on lengthscales.

Definition at line 110 of file MapObjective.h.

◆ kMapStudentTSigmaSq

double funcmin::kMapStudentTSigmaSq = 1.0
inlineconstexpr

Definition at line 111 of file MapObjective.h.