namespace funcmin

Overview

namespace funcmin {
 
// typedefs
 
typedef std::variant<RgminScg, RgminOpt> ParamOptimizer;
typedef std::unique_ptr<DLManagedTensorVersioned, RgminTensorDeleter> RgminTensorHandle;
 
// structs
 
struct MapObjectiveConfig;
 
template <typename ClassName, typename FuncName>
struct RgminNlmlCtx;
 
struct RgminTensorDeleter;
 
// classes
 
class ADAM;
class Ceres;
 
template <typename ClassName, typename FuncName>
class GprCostFunction;
 
class RgminCallbackFailure;
class RgminLbfgs;
class RgminOpt;
class RgminScg;
 
// global variables
 
double kMapStudentTNu = 4.0;
double kMapStudentTSigmaSq = 1.0;
double kMapStudentTNorm = 0.5*(kMapStudentTNu + 1.0);
double kMadConsistencyConstant = 1.4826;
 
// global functions
 
bool mapParametersInDomain(const Eigen::VectorXd& w, const MapObjectiveConfig& cfg);
void projectMapGradient(const MapObjectiveConfig& cfg, Eigen::VectorXd& gradient);
double mapHalfCauchyASq(double signal_variance);
double mapMaxLogMagnSigma2(double signal_variance);
void restoreMapAmplitudeFeasibility(Eigen::VectorXd& w, const MapObjectiveConfig& cfg);
double mapLengthscaleRho0(const MapObjectiveConfig& cfg, Eigen::Index i);
Eigen::Index mapLengthscaleEnd(const MapObjectiveConfig& cfg, Eigen::Index size);
double mapMedianInPlace(std::vector<double>& v);
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);
bool mapLogNormalMagnPriorActive(const MapObjectiveConfig& cfg);
bool mapMagnPriorActive(const MapObjectiveConfig& cfg);
double mapPriorWeight(const MapObjectiveConfig& cfg, Eigen::Index i, Eigen::Index count);
bool mapLengthscalePriorActive(const MapObjectiveConfig& cfg);
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);
 
template <typename Ctx>
rgmin_status_t rgminGrad(void* user, const DLManagedTensorVersioned* x, DLManagedTensorVersioned* grad_out);
 
template <typename Ctx>
rgmin_status_t rgminCurv(void* user, const DLManagedTensorVersioned* x, const DLManagedTensorVersioned* d, double* curv_out);
 
template <typename Ctx>
rgmin_status_t rgminNlmlHessianVectorCurv(void* user, const DLManagedTensorVersioned* x, const DLManagedTensorVersioned* d, double* curv_out);
 
} // namespace funcmin

Detailed Documentation

Global Variables

double kMapStudentTNu = 4.0

Weakly-informative Student-t hyperparameters on lengthscales.

double kMadConsistencyConstant = 1.4826

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).

Global Functions

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

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

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

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

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

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.

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

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.

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

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

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

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

double mapRobustMadVariance(const Eigen::VectorXd& block)

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.

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

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.

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

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.

double mapBayesPriorWeight(const MapObjectiveConfig& cfg)

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

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

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

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)

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

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

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.

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

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.

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

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.

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

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.

template <typename Holder>
bool holderHasNlmlHessianVector(Holder& holder)

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

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)

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.

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)

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.

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

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.