Loading...
Searching...
No Matches
DecisionBand.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_NUMERICS_DECISION_BAND_H_
6#define GPR_NUMERICS_DECISION_BAND_H_
7
8#include <Eigen/Core>
9#include <cmath>
10#include <cstdlib>
11#include <limits>
12
13namespace gpr::numerics {
14
22inline double posteriorIndeterminateBand(double scale) noexcept
23{
24 const double magnitude = std::abs(scale);
25 if (!std::isfinite(magnitude)) return 0.0;
26 return std::sqrt(std::numeric_limits<double>::epsilon()) * magnitude;
27}
28
32inline bool exceedsThreshold(double value, double threshold) noexcept
33{
34 if (!std::isfinite(value) || !std::isfinite(threshold)) return false;
35 return value > threshold + posteriorIndeterminateBand(threshold);
36}
37
40inline bool withinThreshold(double value, double threshold) noexcept
41{
42 if (!std::isfinite(value) || !std::isfinite(threshold)) return false;
43 return !exceedsThreshold(value, threshold);
44}
45
49inline bool clearlyGreater(double candidate, double incumbent) noexcept
50{
51 if (!std::isfinite(candidate)) {
52 return std::isinf(candidate) && candidate > 0.0 &&
53 !(std::isinf(incumbent) && incumbent > 0.0);
54 }
55 if (!std::isfinite(incumbent)) return !(incumbent > 0.0);
56 const double scale = std::max(std::abs(candidate), std::abs(incumbent));
57 return candidate > incumbent + posteriorIndeterminateBand(scale);
58}
59
65inline double quantizeToPosteriorBand(double value) noexcept
66{
67 if (!std::isfinite(value) || value == 0.0) return value;
68 // Bin at the coordinate resolution positionsCoincide already treats
69 // as one geometry (a squared norm of 1e-12). The step is that
70 // fraction of the value's binary magnitude, so a 1e-12 relative
71 // perturbation stays in the bin. A step proportional to the value
72 // itself would map every number onto itself.
73 int exponent = 0;
74 std::frexp(value, &exponent);
75 const double step = 1e-6 * std::ldexp(1.0, exponent);
76 if (!(step > 0.0) || !std::isfinite(step)) return value;
77 return std::nearbyint(value / step) * step;
78}
79
87inline void perturbPosterior(double& energy, Eigen::VectorXd& gradient)
88{
89 const char* amplitude_text = std::getenv("GPR_POSTERIOR_RELATIVE_NOISE");
90 if (amplitude_text == nullptr || amplitude_text[0] == '\0') return;
91 const double amplitude = std::atof(amplitude_text);
92 if (!(amplitude > 0.0) || !std::isfinite(amplitude)) return;
93 const char* seed_text = std::getenv("GPR_POSTERIOR_NOISE_SEED");
94 const unsigned long long seed =
95 seed_text == nullptr ? 0ULL : std::strtoull(seed_text, nullptr, 10);
96 static unsigned long long draw = 0;
97 auto sign = [&]() {
98 unsigned long long mixed =
99 (seed << 32) ^ (++draw * 0x9E3779B97F4A7C15ULL);
100 mixed = (mixed ^ (mixed >> 30)) * 0xBF58476D1CE4E5B9ULL;
101 mixed = (mixed ^ (mixed >> 27)) * 0x94D049BB133111EBULL;
102 mixed ^= mixed >> 31;
103 return (mixed & 1ULL) != 0ULL ? 1.0 : -1.0;
104 };
105 if (std::isfinite(energy)) energy *= 1.0 + amplitude * sign();
106 for (Eigen::Index i = 0; i < gradient.size(); ++i) {
107 if (std::isfinite(gradient(i))) gradient(i) *= 1.0 + amplitude * sign();
108 }
109 energy = quantizeToPosteriorBand(energy);
110 for (Eigen::Index i = 0; i < gradient.size(); ++i)
111 gradient(i) = quantizeToPosteriorBand(gradient(i));
112}
113
114} // namespace gpr::numerics
115
116#endif // GPR_NUMERICS_DECISION_BAND_H_
bool exceedsThreshold(double value, double threshold) noexcept
void perturbPosterior(double &energy, Eigen::VectorXd &gradient)
double quantizeToPosteriorBand(double value) noexcept
bool clearlyGreater(double candidate, double incumbent) noexcept
double posteriorIndeterminateBand(double scale) noexcept
bool withinThreshold(double value, double threshold) noexcept