![]() |
Stan Math Library
5.2.0
Automatic Differentiation
|
|
inline |
Curvature-aware Barzilai–Borwein (BB) step length with robust safeguards.
Given successive parameter displacements \(s = x_k - x_{k-1}\) and gradients \(g_k\), \(g_{k-1}\), this routine forms \(y = g_k - g_{k-1}\) and computes the two classical BB candidates
\begin{align*} \alpha_{\text{BB1}} &= \frac{\langle s,s\rangle}{\langle s,y\rangle},\\ \alpha_{\text{BB2}} &= \frac{\langle s,y\rangle}{\langle y,y\rangle}, \end{align*}
then chooses between them using the spectral cosine \(r = \cos^2\!\angle(s,y) = \dfrac{\langle s,y\rangle^2} {\langle s,s\rangle\,\langle y,y\rangle}\in[0,1]\):
All candidates are clamped into \([\text{min\_alpha},\,\text{max\_alpha}]\) and must be finite and positive. If the curvature scalars are ill-posed (non-finite or too small), \(\langle s,y\rangle \le \varepsilon\), or if last_backtracks == 99 (explicitly disabling BB for this iteration), the function falls back to a safe step: use prev_step when finite and positive, otherwise \(1.0\), then clamp to \([\text{min\_alpha},\,\text{max\_alpha}]\).
| s | Displacement between consecutive iterates ( \(s = x_k - x_{k-1}\)). |
| g_curr | Gradient at the current iterate \(g_k\). |
| g_prev | Gradient at the previous iterate \(g_{k-1}\). |
| prev_step | Previously accepted step length (used by the fallback). |
| last_backtracks | Number of backtracking contractions performed by the most recent line search; set to 99 to force the safe fallback. |
| min_alpha | Lower bound for the returned step length. |
| max_alpha | Upper bound for the returned step length. |
std::abs to BB ratios to avoid negative steps; descent is enforced by the line search. Definition at line 63 of file barzilai_borwein_step_size.hpp.