.. index:: pair: namespace; funcmin .. _doxid-namespacefuncmin: namespace funcmin ================= .. toctree:: :hidden: struct_funcmin_MapObjectiveConfig.rst struct_funcmin_RgminNlmlCtx.rst struct_funcmin_RgminTensorDeleter.rst class_funcmin_ADAM.rst class_funcmin_Ceres.rst class_funcmin_GprCostFunction.rst class_funcmin_RgminCallbackFailure.rst class_funcmin_RgminLbfgs.rst class_funcmin_RgminOpt.rst class_funcmin_RgminScg.rst Overview ~~~~~~~~ .. ref-code-block:: cpp :class: doxyrest-overview-code-block namespace funcmin { // typedefs typedef std::variant<:ref:`RgminScg`, :ref:`RgminOpt`> :target:`ParamOptimizer`; typedef std::unique_ptr`> :target:`RgminTensorHandle`; // structs struct :ref:`MapObjectiveConfig`; template struct :ref:`RgminNlmlCtx`; struct :ref:`RgminTensorDeleter`; // classes class :ref:`ADAM`; class :ref:`Ceres`; template class :ref:`GprCostFunction`; class :ref:`RgminCallbackFailure`; class :ref:`RgminLbfgs`; class :ref:`RgminOpt`; class :ref:`RgminScg`; // global variables double :ref:`kMapStudentTNu` = 4.0; double :target:`kMapStudentTSigmaSq` = 1.0; double :target:`kMapStudentTNorm` = 0.5*(:ref:`kMapStudentTNu` + 1.0); double :ref:`kMadConsistencyConstant` = 1.4826; // global functions bool :ref:`mapParametersInDomain`(const Eigen::VectorXd& w, const :ref:`MapObjectiveConfig`& cfg); void :ref:`projectMapGradient`(const :ref:`MapObjectiveConfig`& cfg, Eigen::VectorXd& gradient); double :target:`mapHalfCauchyASq`(double signal_variance); double :target:`mapMaxLogMagnSigma2`(double signal_variance); void :ref:`restoreMapAmplitudeFeasibility`(Eigen::VectorXd& w, const :ref:`MapObjectiveConfig`& cfg); double :ref:`mapLengthscaleRho0`(const :ref:`MapObjectiveConfig`& cfg, Eigen::Index i); Eigen::Index :ref:`mapLengthscaleEnd`(const :ref:`MapObjectiveConfig`& cfg, Eigen::Index size); double :ref:`mapMedianInPlace`(std::vector& v); double :ref:`mapRobustMadVariance`(const Eigen::VectorXd& block); double :ref:`mapForceImpliedSignalVariance`(const :ref:`gpr::EigenMatrix`& x, Eigen::Index n_e, const Eigen::VectorXd& y, const Eigen::VectorXd& w); double :ref:`mapRobustSignalVarianceAnchor`(const :ref:`gpr::EigenMatrix`& x, Eigen::Index n_e, const Eigen::VectorXd& y, const Eigen::VectorXd& w); double :ref:`mapBayesPriorWeight`(const :ref:`MapObjectiveConfig`& cfg); bool :target:`mapLogNormalMagnPriorActive`(const :ref:`MapObjectiveConfig`& cfg); bool :target:`mapMagnPriorActive`(const :ref:`MapObjectiveConfig`& cfg); double :target:`mapPriorWeight`(const :ref:`MapObjectiveConfig`& cfg, Eigen::Index i, Eigen::Index count); bool :target:`mapLengthscalePriorActive`(const :ref:`MapObjectiveConfig`& cfg); void :ref:`mapConfigFromSettings`(const :ref:`gpr::ScgOptimizationSettings`& settings, :ref:`MapObjectiveConfig`& cfg); :ref:`MapObjectiveConfig` :ref:`makeMapObjectiveConfig`(const :ref:`gpr::EigenMatrix`& x, const Eigen::VectorXd& y, const Eigen::VectorXd& w, double current_barrier_strength, const Eigen::VectorXd& lengthscale_rho0, const :ref:`gpr::ScgOptimizationSettings`& settings, bool robust_signal_anchor = true); void :ref:`applyMapPenalty`(const Eigen::VectorXd& w, const :ref:`MapObjectiveConfig`& cfg, double& energy, Eigen::VectorXd* gradient); bool :ref:`applyMapPenaltyGradient`(const Eigen::VectorXd& w, const :ref:`MapObjectiveConfig`& cfg, Eigen::VectorXd& gradient); double :ref:`mapPenaltyDirectionalCurvature`(const Eigen::VectorXd& w, const Eigen::VectorXd& direction, const :ref:`MapObjectiveConfig`& cfg); void :ref:`mapPenaltyHessianVector`(const Eigen::VectorXd& w, const Eigen::VectorXd& direction, const :ref:`MapObjectiveConfig`& cfg, Eigen::VectorXd& product); template bool :ref:`holderHasNlmlHessianVector`(Holder& holder); template bool :ref:`holderNlmlHessianVector`(Holder& holder, const Eigen::VectorXd& x_ind, const :ref:`gpr::EigenMatrix`& x, const Eigen::VectorXd& y, const Eigen::VectorXd& direction, Eigen::VectorXd& product); template bool :ref:`holderNlmlDirectionalCurvature`(Holder& holder, const Eigen::VectorXd& x_ind, const :ref:`gpr::EigenMatrix`& x, const Eigen::VectorXd& y, const Eigen::VectorXd& direction, double& curvature); Eigen::Map :target:`tensorToVec`(const DLManagedTensorVersioned* tensor); Eigen::Map :target:`tensorToVec`(DLManagedTensorVersioned* tensor); template rgmin_status_t :target:`rgminEval`(void* user, const DLManagedTensorVersioned* x, double* value_out); template rgmin_status_t :target:`rgminGrad`(void* user, const DLManagedTensorVersioned* x, DLManagedTensorVersioned* grad_out); template rgmin_status_t :target:`rgminCurv`(void* user, const DLManagedTensorVersioned* x, const DLManagedTensorVersioned* d, double* curv_out); template rgmin_status_t :ref:`rgminNlmlHessianVectorCurv`(void* user, const DLManagedTensorVersioned* x, const DLManagedTensorVersioned* d, double* curv_out); } // namespace funcmin .. _details-namespacefuncmin: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Global Variables ---------------- .. index:: pair: variable; kMapStudentTNu .. _doxid-namespacefuncmin_1a8ee1b6e4b8ed759136394e1db5d5bf4a: .. ref-code-block:: cpp :class: doxyrest-title-code-block double kMapStudentTNu = 4.0 Weakly-informative Student-t hyperparameters on lengthscales. .. index:: pair: variable; kMadConsistencyConstant .. _doxid-namespacefuncmin_1aee489343819ab38a601efbbce432d65f: .. ref-code-block:: cpp :class: doxyrest-title-code-block 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 ---------------- .. index:: pair: function; mapParametersInDomain .. _doxid-namespacefuncmin_1a9e4c217de2a0ea65a56120fc2ff29c4a: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool mapParametersInDomain(const Eigen::VectorXd& w, const :ref:`MapObjectiveConfig`& cfg) Reject invalid trials before invoking a covariance objective. Infinite endpoints leave a side unbounded; NaN or mismatched bounds are invalid. .. index:: pair: function; projectMapGradient .. _doxid-namespacefuncmin_1ab2c4b6e7fa930081ba637749800b2e63: .. ref-code-block:: cpp :class: doxyrest-title-code-block void projectMapGradient(const :ref:`MapObjectiveConfig`& cfg, Eigen::VectorXd& gradient) Restrict the optimizer gradient while retaining the full objective and covariance. Empty masks leave the complete parameter space available. .. index:: pair: function; restoreMapAmplitudeFeasibility .. _doxid-namespacefuncmin_1a0f198c6d81410cc789e189514f47e51e: .. ref-code-block:: cpp :class: doxyrest-title-code-block void restoreMapAmplitudeFeasibility(Eigen::VectorXd& w, const :ref:`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. .. index:: pair: function; mapLengthscaleRho0 .. _doxid-namespacefuncmin_1a65ff4e8f0ab54ff2f45037b910a338fd: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapLengthscaleRho0(const :ref:`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. .. index:: pair: function; mapLengthscaleEnd .. _doxid-namespacefuncmin_1a9f832c21d7b05a56ae6a3ae26fd4bd2b: .. ref-code-block:: cpp :class: doxyrest-title-code-block Eigen::Index mapLengthscaleEnd(const :ref:`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. .. index:: pair: function; mapMedianInPlace .. _doxid-namespacefuncmin_1a00dadd1045d65e1969560ee1a7d51a98: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapMedianInPlace(std::vector& v) Median of a vector, sorting in place (even-length: mean of the two middle elements). .. index:: pair: function; mapRobustMadVariance .. _doxid-namespacefuncmin_1aa1693d7d4dcd54094f5e41d082cf306f: .. ref-code-block:: cpp :class: doxyrest-title-code-block 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. .. index:: pair: function; mapForceImpliedSignalVariance .. _doxid-namespacefuncmin_1a1b89d0da9b61d9d223b2441085e1167a: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapForceImpliedSignalVariance(const :ref:`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. .. index:: pair: function; mapRobustSignalVarianceAnchor .. _doxid-namespacefuncmin_1aed6406ed6d476248544d0d032424288e: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapRobustSignalVarianceAnchor(const :ref:`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. .. index:: pair: function; mapBayesPriorWeight .. _doxid-namespacefuncmin_1acc1a4f165cc3a1b3f022051ae59519b5: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapBayesPriorWeight(const :ref:`MapObjectiveConfig`& cfg) Prior weight for the bayesian prior terms. Barrier terms always use barrier_strength regardless of weight_mode. .. index:: pair: function; mapConfigFromSettings .. _doxid-namespacefuncmin_1ac60776307d674f042b67d408770c0a7d: .. ref-code-block:: cpp :class: doxyrest-title-code-block void mapConfigFromSettings(const :ref:`gpr::ScgOptimizationSettings`& settings, :ref:`MapObjectiveConfig`& cfg) Fill :ref:`MapObjectiveConfig ` prior fields and weight mode from the rgmin optimizer settings (populated by ``Optimize.cpp`` / unit tests). .. index:: pair: function; makeMapObjectiveConfig .. _doxid-namespacefuncmin_1a2b8218371ab2f9d3a0dbe26346341ee8: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`MapObjectiveConfig` makeMapObjectiveConfig(const :ref:`gpr::EigenMatrix`& x, const Eigen::VectorXd& y, const Eigen::VectorXd& w, double current_barrier_strength, const Eigen::VectorXd& lengthscale_rho0, const :ref:`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. .. index:: pair: function; applyMapPenalty .. _doxid-namespacefuncmin_1a4d0c0b82c4b79624e5fee11603b5a109: .. ref-code-block:: cpp :class: doxyrest-title-code-block void applyMapPenalty(const Eigen::VectorXd& w, const :ref:`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. .. index:: pair: function; applyMapPenaltyGradient .. _doxid-namespacefuncmin_1a6c523a5b1da2212d9d71b0d65745b94d: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool applyMapPenaltyGradient(const Eigen::VectorXd& w, const :ref:`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. .. index:: pair: function; mapPenaltyDirectionalCurvature .. _doxid-namespacefuncmin_1ad98f66328ab2183d8aa6dce6db6743c1: .. ref-code-block:: cpp :class: doxyrest-title-code-block double mapPenaltyDirectionalCurvature(const Eigen::VectorXd& w, const Eigen::VectorXd& direction, const :ref:`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. .. index:: pair: function; mapPenaltyHessianVector .. _doxid-namespacefuncmin_1a937199ff1dd53b17f213cabf8e7b3d42: .. ref-code-block:: cpp :class: doxyrest-title-code-block void mapPenaltyHessianVector(const Eigen::VectorXd& w, const Eigen::VectorXd& direction, const :ref:`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. .. index:: pair: function; holderHasNlmlHessianVector .. _doxid-namespacefuncmin_1a2d277faabab4aa291a2f784f1609059b: .. ref-code-block:: cpp :class: doxyrest-title-code-block template bool holderHasNlmlHessianVector(Holder& holder) True when the optimizer's holder offers the exact Hessian-vector product of the objective it currently evaluates. .. index:: pair: function; holderNlmlHessianVector .. _doxid-namespacefuncmin_1acbd85991290893b29dda83d6e16e41d9: .. ref-code-block:: cpp :class: doxyrest-title-code-block template bool holderNlmlHessianVector(Holder& holder, const Eigen::VectorXd& x_ind, const :ref:`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. .. index:: pair: function; holderNlmlDirectionalCurvature .. _doxid-namespacefuncmin_1a6f656d5a3bc7b0d54d3967e75af6ed3b: .. ref-code-block:: cpp :class: doxyrest-title-code-block template bool holderNlmlDirectionalCurvature(Holder& holder, const Eigen::VectorXd& x_ind, const :ref:`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. .. index:: pair: function; rgminNlmlHessianVectorCurv .. _doxid-namespacefuncmin_1a54eeba63225e91642db2bd0539ac1d05: .. ref-code-block:: cpp :class: doxyrest-title-code-block template 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.