Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ barzilai_borwein_step_size()

double stan::math::internal::barzilai_borwein_step_size ( const Eigen::VectorXd &  s,
const Eigen::VectorXd &  g_curr,
const Eigen::VectorXd &  g_prev,
double  prev_step,
int  last_backtracks,
double  min_alpha,
double  max_alpha 
)
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]\):

  • if \(r > 0.9\) (well-aligned curvature) and the previous line search did ≤ 1 backtrack, prefer the “long” step \(\alpha_{\text{BB1}}\);
  • if \(0.1 \le r \le 0.9\), take the neutral geometric mean \(\sqrt{\alpha_{\text{BB1}}\alpha_{\text{BB2}}}\);
  • otherwise default to the “short” step \(\alpha_{\text{BB2}}\).

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}]\).

Parameters
sDisplacement between consecutive iterates ( \(s = x_k - x_{k-1}\)).
g_currGradient at the current iterate \(g_k\).
g_prevGradient at the previous iterate \(g_{k-1}\).
prev_stepPreviously accepted step length (used by the fallback).
last_backtracksNumber of backtracking contractions performed by the most recent line search; set to 99 to force the safe fallback.
min_alphaLower bound for the returned step length.
max_alphaUpper bound for the returned step length.
Returns
A finite, positive BB-style step length \(\alpha \in [\text{min\_alpha},\,\text{max\_alpha}]\) suitable for seeding a line search or as a spectral preconditioner scale.
Note
Uses \(\varepsilon=10^{-16}\) to guard against division by very small curvature terms, and applies std::abs to BB ratios to avoid negative steps; descent is enforced by the line search.
Warning
The vectors must have identical size. Non-finite inputs yield the safe fallback.

Definition at line 63 of file barzilai_borwein_step_size.hpp.