Loading...
Searching...
No Matches
StationaryCurvature.h
Go to the documentation of this file.
1
7#ifndef GPRIDGEIZATION_STATIONARY_CURVATURE_H_
8#define GPRIDGEIZATION_STATIONARY_CURVATURE_H_
9
10#include <Eigen/Core>
11#include <algorithm>
12#include <cmath>
13#include <cstdint>
14#include <limits>
15#include <random>
16#include <vector>
17
19
20namespace gpr::optim {
21
29inline double measuredCubicColumnContribution(double overlap,
30 double mode_quadratic,
31 double axis_quadratic)
32{
33 return overlap * overlap *
34 (3.0 * mode_quadratic - 2.0 * overlap * axis_quadratic);
35}
36
43 const Eigen::MatrixXd& axes, const Eigen::MatrixXd& second_differences,
44 const Eigen::VectorXd& mode)
45{
46 double coefficient = 0.0;
47 for (Eigen::Index j = 0; j < axes.cols(); ++j) {
48 const Eigen::VectorXd action = second_differences.col(j);
50 axes.col(j).dot(mode), mode.dot(action), axes.col(j).dot(action));
51 }
52 return coefficient;
53}
54
74 bool valid = false;
75 double lower = -std::numeric_limits<double>::infinity();
76 double upper = std::numeric_limits<double>::infinity();
77 double undetermined = 1.0;
78 double force_target = std::numeric_limits<double>::infinity();
79 bool force_decidable = false;
80};
81
83 double c, double c_spread, double f, double f_spread, double b,
84 double b_spread, Eigen::Index samples = detail::kSpectrumSamples)
85{
87 if (!(std::isfinite(c) && std::isfinite(c_spread) && std::isfinite(f) &&
88 std::isfinite(f_spread) && std::isfinite(b) &&
89 std::isfinite(b_spread)) ||
90 samples < 4)
91 return out;
92 // A mode and its negative describe the same stationary point. Reflect
93 // both odd coefficients into one frame so a fixed finite sample also
94 // preserves that symmetry, not only its limiting distribution.
95 if (f < 0.0 || (f == 0.0 && b < 0.0)) {
96 f = -f;
97 b = -b;
98 }
99 std::mt19937_64 rng(0x5C0FFEEULL + static_cast<std::uint64_t>(samples));
100 std::normal_distribution<double> normal(0.0, 1.0);
101 std::vector<double> roots;
102 roots.reserve(static_cast<std::size_t>(samples));
103 Eigen::Index undetermined = 0;
104 for (Eigen::Index n = 0; n < samples; ++n) {
105 const double cs = c + c_spread * normal(rng);
106 const double fs = f + f_spread * normal(rng);
107 const double bs = b + b_spread * normal(rng);
108 const double discriminant = cs * cs - 2.0 * bs * fs;
109 if (discriminant < 0.0) {
110 ++undetermined;
111 continue;
112 }
113 roots.push_back(std::copysign(std::sqrt(discriminant), cs));
114 }
115 std::sort(roots.begin(), roots.end());
116 const auto total = static_cast<double>(samples);
117 out.undetermined = static_cast<double>(undetermined) / total;
118 // One-spread quantiles over every sample: an undetermined sample sits
119 // below every root for the lower bound and above every root for the
120 // upper bound.
121 const double q = 0.1587;
122 const auto rank_low = static_cast<Eigen::Index>(std::floor(q * total));
123 const auto rank_high =
124 static_cast<Eigen::Index>(std::floor((1.0 - q) * total));
125 const Eigen::Index n_roots = static_cast<Eigen::Index>(roots.size());
126 out.lower = rank_low < undetermined
127 ? -std::numeric_limits<double>::infinity()
128 : roots[static_cast<std::size_t>(
129 std::min(rank_low - undetermined, n_roots - 1))];
130 out.upper = rank_high >= n_roots
131 ? std::numeric_limits<double>::infinity()
132 : roots[static_cast<std::size_t>(rank_high)];
133 if (n_roots == 0) {
134 out.lower = -std::numeric_limits<double>::infinity();
135 out.upper = std::numeric_limits<double>::infinity();
136 }
137 // Solve c^2 - 2 abs(b) t = hypot(s_0, 2 s_b t) for t >= 0, where
138 // s_0 is the discriminant spread at zero force. The cubic error shrinks
139 // with the force being converged; using its value at the current force
140 // can incorrectly declare every positive target unresolvable.
141 const double curvature_squared = c * c;
142 const double zero_force_spread =
143 2.0 * std::hypot(c * c_spread, b * f_spread);
144 out.force_target = 0.0;
145 if (curvature_squared > zero_force_spread) {
146 const double ratio = zero_force_spread / curvature_squared;
147 const double slope = 2.0 * std::abs(b);
148 // Rationalizing the smaller quadratic root avoids subtracting
149 // nearly equal terms and also covers a zero mean cubic coefficient.
150 const double denominator =
151 slope + std::hypot(slope * ratio,
152 2.0 * b_spread *
153 std::sqrt((1.0 - ratio) * (1.0 + ratio)));
154 out.force_target = denominator > 0.0
155 ? (curvature_squared - zero_force_spread) *
156 (1.0 + ratio) / denominator
157 : std::numeric_limits<double>::infinity();
158 }
159 out.force_decidable =
160 std::isfinite(out.force_target) && out.force_target >= f_spread;
161 out.valid = true;
162 return out;
163}
164
165} // namespace gpr::optim
166
167#endif // GPRIDGEIZATION_STATIONARY_CURVATURE_H_
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double axisStencilDirectionalCubic(const Eigen::MatrixXd &axes, const Eigen::MatrixXd &second_differences, const Eigen::VectorXd &mode)
double measuredCubicColumnContribution(double overlap, double mode_quadratic, double axis_quadratic)
StationaryCurvaturePosterior stationaryCurvaturePosterior(double c, double c_spread, double f, double f_spread, double b, double b_spread, Eigen::Index samples=detail::kSpectrumSamples)
double undetermined
fraction of samples without a model root
bool force_decidable
force_target resolvable at f_spread