Skip to main content

stats_claw/optimizers/
mod.rs

1//! Numerical optimizers minimizing an [`Objective`].
2//!
3//! Every optimizer in this module reduces a scalar objective `f: ℝⁿ → ℝ` and
4//! returns an [`OptimizeResult`] reporting the located point, its objective
5//! value, the iteration count, and a [`ConvergenceStatus`]. The families are
6//! grouped into subfolders: [`gradient`] (first-order learning-rate methods and
7//! conjugate gradient), [`second_order`] (Newton and L-BFGS), and [`stochastic`]
8//! (simulated annealing and genetic / differential evolution). The shared test
9//! objectives live in [`objectives`].
10//!
11//! ## `scipy.optimize` mapping
12//!
13//! Each optimizer is paired with the `scipy.optimize` method it is cross-checked
14//! against; methods with no faithful counterpart are documented as excluded so
15//! the comparison coverage is auditable.
16//!
17//! | stats-claw              | `scipy.optimize`                  | agreement |
18//! |-----------------------|-----------------------------------|-----------|
19//! | `gradient_descent`    | none (vanilla GD)                 | excluded  |
20//! | `sgd`                 | none                              | excluded  |
21//! | `adam`                | none                              | excluded  |
22//! | `rmsprop`             | none                              | excluded  |
23//! | `adagrad`             | none                              | excluded  |
24//! | `conjugate_gradient`  | `minimize(method="CG")`           | compared  |
25//! | `newton`              | `minimize(method="Newton-CG")`    | compared  |
26//! | `lbfgs`               | `minimize(method="L-BFGS-B")`     | compared  |
27//! | `simulated_annealing` | `dual_annealing`                  | optimum   |
28//! | `genetic`             | `differential_evolution`          | optimum   |
29//!
30//! Deterministic optimizers (exempt from the seed-variation check):
31//! `gradient_descent`, `adam`, `rmsprop`, `adagrad`, `conjugate_gradient`,
32//! `newton`, `lbfgs`. Stochastic optimizers (seed-variation required): `sgd`,
33//! `simulated_annealing`, `genetic`.
34
35pub mod gradient;
36pub mod objectives;
37pub mod second_order;
38pub mod stochastic;
39
40/// Outcome of an optimization run: whether the stopping criterion was satisfied.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum ConvergenceStatus {
43    /// The convergence criterion (e.g. gradient norm below tolerance) was met.
44    Converged,
45    /// The iteration budget was exhausted before the criterion was met.
46    MaxIterReached,
47}
48
49/// The result of minimizing an [`Objective`].
50///
51/// The fields together answer: *did it converge, how good is the
52/// result, and how much work did it take.*
53#[derive(Debug, Clone)]
54pub struct OptimizeResult {
55    /// The located minimizer (the point at which the run stopped).
56    pub x: Vec<f64>,
57    /// The objective value `f(x)` at the located point.
58    pub fx: f64,
59    /// The number of iterations actually performed (always `≥ 0`).
60    pub iterations: usize,
61    /// Whether the run converged or exhausted its iteration budget.
62    pub status: ConvergenceStatus,
63}
64
65/// A differentiable scalar objective `f: ℝⁿ → ℝ` to be minimized.
66///
67/// Implementors must supply [`value`](Objective::value) and
68/// [`grad`](Objective::grad). The Hessian defaults to a central finite-difference
69/// approximation built from `grad`, so second-order optimizers work for any
70/// objective without an analytic Hessian; objectives that have one may override
71/// [`hessian`](Objective::hessian) for accuracy.
72pub trait Objective {
73    /// Evaluates the objective at `x`.
74    ///
75    /// # Arguments
76    ///
77    /// * `x` — the point at which to evaluate; any finite coordinates.
78    ///
79    /// # Returns
80    ///
81    /// The scalar objective value `f(x)`.
82    fn value(&self, x: &[f64]) -> f64;
83
84    /// Evaluates the gradient `∇f(x)`.
85    ///
86    /// # Arguments
87    ///
88    /// * `x` — the point at which to evaluate the gradient.
89    ///
90    /// # Returns
91    ///
92    /// The gradient vector, the same length as `x`.
93    fn grad(&self, x: &[f64]) -> Vec<f64>;
94
95    /// Approximates the Hessian `∇²f(x)` by central differences of the gradient.
96    ///
97    /// The default uses a step of `√ε ≈ 1.49e-8` per coordinate and symmetrizes
98    /// the result so it is exactly symmetric (rounding can otherwise break
99    /// symmetry). Objectives with an analytic Hessian should override this.
100    ///
101    /// # Arguments
102    ///
103    /// * `x` — the point at which to approximate the Hessian.
104    ///
105    /// # Returns
106    ///
107    /// The `n × n` Hessian in row-major order (`n = x.len()`).
108    fn hessian(&self, x: &[f64]) -> Vec<Vec<f64>> {
109        let n = x.len();
110        let h = f64::EPSILON.sqrt();
111        let mut hess = vec![vec![0.0; n]; n];
112        let mut xp = x.to_vec();
113        for j in 0..n {
114            let xj = *xp.get(j).unwrap_or(&0.0);
115            set(&mut xp, j, xj + h);
116            let gp = self.grad(&xp);
117            set(&mut xp, j, xj - h);
118            let gm = self.grad(&xp);
119            set(&mut xp, j, xj);
120            for i in 0..n {
121                let dgi = gp.get(i).unwrap_or(&0.0) - gm.get(i).unwrap_or(&0.0);
122                set_mat(&mut hess, i, j, dgi / (2.0 * h));
123            }
124        }
125        symmetrize(&mut hess);
126        hess
127    }
128}
129
130/// Writes `value` into `v[i]`, ignoring an out-of-range index (cannot occur for
131/// the in-bounds indices used here, but keeps the code clear of
132/// `indexing_slicing`).
133fn set(v: &mut [f64], i: usize, value: f64) {
134    if let Some(slot) = v.get_mut(i) {
135        *slot = value;
136    }
137}
138
139/// Writes `value` into `m[i][j]`, ignoring an out-of-range index.
140fn set_mat(m: &mut [Vec<f64>], i: usize, j: usize, value: f64) {
141    if let Some(row) = m.get_mut(i)
142        && let Some(slot) = row.get_mut(j)
143    {
144        *slot = value;
145    }
146}
147
148/// Averages a square matrix with its transpose in place so it is symmetric.
149fn symmetrize(m: &mut [Vec<f64>]) {
150    let n = m.len();
151    for i in 0..n {
152        for j in (i + 1)..n {
153            let a = mat(m, i, j);
154            let b = mat(m, j, i);
155            let avg = 0.5 * (a + b);
156            set_mat(m, i, j, avg);
157            set_mat(m, j, i, avg);
158        }
159    }
160}
161
162/// Reads `m[i][j]`, returning `0.0` for an out-of-range index.
163fn mat(m: &[Vec<f64>], i: usize, j: usize) -> f64 {
164    *m.get(i).and_then(|row| row.get(j)).unwrap_or(&0.0)
165}
166
167/// Euclidean (L2) norm of a vector.
168///
169/// # Arguments
170///
171/// * `v` — the vector whose norm is taken.
172///
173/// # Returns
174///
175/// `√Σ vᵢ²`, used as the gradient-norm stopping criterion across optimizers.
176#[must_use]
177pub fn norm(v: &[f64]) -> f64 {
178    v.iter().map(|x| x * x).sum::<f64>().sqrt()
179}
180
181/// Dot product of two equal-length vectors (extra elements of the longer one are
182/// ignored, which never happens for the matched-length inputs used internally).
183///
184/// # Arguments
185///
186/// * `a`, `b` — the vectors to multiply elementwise and sum.
187///
188/// # Returns
189///
190/// `Σ aᵢ·bᵢ`.
191#[must_use]
192pub fn dot(a: &[f64], b: &[f64]) -> f64 {
193    a.iter().zip(b).map(|(x, y)| x * y).sum()
194}
195
196/// Multiplies a square matrix (row-major `Vec<Vec<f64>>`) by a vector.
197///
198/// # Arguments
199///
200/// * `m` — an `n × n` matrix.
201/// * `v` — an `n`-vector.
202///
203/// # Returns
204///
205/// The product `m·v` as an `n`-vector.
206#[must_use]
207pub fn matvec(m: &[Vec<f64>], v: &[f64]) -> Vec<f64> {
208    m.iter().map(|row| dot(row, v)).collect()
209}
210
211/// Backtracking line search satisfying the Armijo sufficient-decrease condition.
212///
213/// Starting from step `1.0`, halves the step until
214/// `f(x + α·d) ≤ f(x) + c·α·gᵀd` holds, used by the line-search optimizers
215/// (conjugate gradient, Newton, L-BFGS) to pick a stable step along `d`.
216///
217/// # Arguments
218///
219/// * `obj` — the objective being minimized.
220/// * `x` — the current point.
221/// * `dir` — the search direction (should be a descent direction).
222/// * `grad` — the gradient at `x` (so `gᵀd` need not be recomputed).
223///
224/// # Returns
225///
226/// The accepted step length `α` (at least `MIN_STEP`, so progress is bounded).
227pub(crate) fn line_search(obj: &impl Objective, x: &[f64], dir: &[f64], grad: &[f64]) -> f64 {
228    const C: f64 = 1e-4;
229    const SHRINK: f64 = 0.5;
230    /// Maximum halvings (`0.5^100 ≈ 1e-30`) before accepting the smallest step.
231    const MAX_HALVINGS: usize = 100;
232    let f0 = obj.value(x);
233    let slope = dot(grad, dir);
234    let mut alpha = 1.0_f64;
235    for _ in 0..MAX_HALVINGS {
236        let trial: Vec<f64> = x
237            .iter()
238            .zip(dir)
239            .map(|(xi, di)| alpha.mul_add(*di, *xi))
240            .collect();
241        if obj.value(&trial) <= (C * alpha).mul_add(slope, f0) {
242            return alpha;
243        }
244        alpha *= SHRINK;
245    }
246    alpha
247}
248
249/// Steps `x` to `x + α·dir`, returning the new point.
250///
251/// # Arguments
252///
253/// * `x` — the current point.
254/// * `alpha` — the step length.
255/// * `dir` — the step direction.
256///
257/// # Returns
258///
259/// The point `x + α·dir`.
260pub(crate) fn step(x: &[f64], alpha: f64, dir: &[f64]) -> Vec<f64> {
261    x.iter()
262        .zip(dir)
263        .map(|(xi, di)| alpha.mul_add(*di, *xi))
264        .collect()
265}
266
267/// Kani formal-verification harnesses for the optimizer step primitives.
268///
269/// Compiled only under `cargo kani` (behind `#[cfg(kani)]`); invisible to normal
270/// build/test/clippy. They prove that the arithmetic every optimizer step is built
271/// from — the vector primitives [`norm`], [`dot`], [`matvec`], and [`step`] —
272/// neither panics nor overflows for arbitrary *bounded finite* state.
273///
274/// ## Scope note (honest disclosure)
275///
276/// The optimizers take the learning rate, tolerance, and iteration budget as free
277/// parameters and do **not** validate them — there is no `Result`-returning
278/// parameter-validation surface to prove rejects bad input via `Err`. These proofs
279/// therefore target the property that *is* present: the per-step vector arithmetic
280/// is panic-/overflow-free over magnitude-bounded finite state.
281///
282/// A whole single [`gradient::gradient_descent`] iteration through a symbolic
283/// objective was attempted but **dropped**: the objective's `grad` returns a
284/// heap-allocated `Vec<f64>`, and modelling that allocation plus the update loop
285/// blew CBMC past its memory budget (≈200k SAT variables, out-of-memory). The step
286/// arithmetic it would have exercised is instead covered directly by
287/// [`optimizers_step_finite`], whose `α·mul_add(dir, x)` is the *exact* shape of the
288/// learning-rate update `xᵢ ← xᵢ − lr·gᵢ` (with `α = −lr`, `dir = g`); together with
289/// [`optimizers_norm_finite_non_negative`] (the gradient-norm stopping test) this
290/// covers every arithmetic operation a first-order step performs. The learning-rate
291/// optimizers (`sgd`, `adam`, `rmsprop`, `adagrad`) and the line-search / second-
292/// order methods (`newton`, `lbfgs`, `conjugate_gradient`) share this step shape;
293/// their extra per-optimizer accumulator state is not individually proved here.
294#[cfg(kani)]
295mod verification {
296    use super::{dot, matvec, norm, step};
297
298    /// Upper bound on `|xᵢ|` for every symbolic coordinate.
299    ///
300    /// The task scopes the step proofs to `|x| ≤ 1e6`. At this bound every product
301    /// `xᵢ·yⱼ ≤ 1e12` and every three-term sum `≤ 3e12` stays far below
302    /// `f64::MAX ≈ 1.8e308`, so no intermediate overflows to `±∞` and the sign /
303    /// finiteness arguments hold in `f64` rounding, not just exact arithmetic.
304    /// A fully unbounded symbolic `f64` would overflow these sums to `±∞` — a
305    /// genuine floating-point limitation, not a solver artifact — so the bound
306    /// isolates the panic-/overflow-freedom property from extreme-magnitude
307    /// arithmetic that no optimizer is expected to survive.
308    const MAX_ABS: f64 = 1e6;
309
310    /// Upper bound on `|α|` (a stand-in for `−lr`) in the [`step`] proof.
311    ///
312    /// Bounding the scale keeps the update `α·dirᵢ` at most `1e3·1e6 = 1e9`, so the
313    /// stepped coordinate `xᵢ + α·dirᵢ` stays finite; an unbounded scale could push
314    /// a finite direction past `f64::MAX`, again a real limitation rather than a
315    /// spurious failure.
316    const MAX_SCALE: f64 = 1e3;
317
318    /// Fixed problem dimension for the vector-primitive proofs.
319    ///
320    /// Three coordinates exercise the full iterator fold (more than the trivial
321    /// one- or two-element cases) while keeping the loop-free unrolling small.
322    const DIM: usize = 3;
323
324    /// Draws a symbolic `f64` constrained to be finite and bounded by [`MAX_ABS`].
325    ///
326    /// # Returns
327    ///
328    /// A finite `f64` with `|x| ≤ MAX_ABS`.
329    fn any_bounded() -> f64 {
330        let x: f64 = kani::any();
331        kani::assume(x.is_finite());
332        kani::assume(x.abs() <= MAX_ABS);
333        x
334    }
335
336    /// Builds a length-[`DIM`] array of independent bounded-finite coordinates.
337    ///
338    /// # Returns
339    ///
340    /// An array `[f64; DIM]` with every entry drawn by [`any_bounded`].
341    fn any_vec() -> [f64; DIM] {
342        [any_bounded(), any_bounded(), any_bounded()]
343    }
344
345    /// Proves the Euclidean norm of a bounded-finite vector is panic-/overflow-free
346    /// and yields a finite, non-negative result.
347    #[kani::proof]
348    fn optimizers_norm_finite_non_negative() {
349        let v = any_vec();
350        let n = norm(&v);
351        assert!(n.is_finite(), "norm produced a non-finite value: {n}");
352        assert!(n >= 0.0, "norm produced a negative value: {n}");
353    }
354
355    /// Proves the dot product of two bounded-finite vectors is panic-/overflow-free
356    /// and finite (the inner arithmetic shared by every gradient step).
357    #[kani::proof]
358    fn optimizers_dot_finite() {
359        let a = any_vec();
360        let b = any_vec();
361        let d = dot(&a, &b);
362        assert!(d.is_finite(), "dot produced a non-finite value: {d}");
363    }
364
365    /// Proves the matrix–vector product used by the Newton step is
366    /// panic-/overflow-free and finite for a bounded-finite matrix and vector.
367    #[kani::proof]
368    fn optimizers_matvec_finite() {
369        let m: Vec<Vec<f64>> = vec![any_vec().to_vec(), any_vec().to_vec(), any_vec().to_vec()];
370        let v = any_vec();
371        let y = matvec(&m, &v);
372        assert!(
373            y.iter().all(|c| c.is_finite()),
374            "matvec produced a non-finite component"
375        );
376        assert!(y.len() == DIM, "matvec changed the vector length");
377    }
378
379    /// Proves the `x ← x + α·dir` update is panic-/overflow-free and finite for a
380    /// bounded scale and bounded-finite point and direction. With `α = −lr` and
381    /// `dir = gradient` this is exactly the learning-rate update
382    /// `xᵢ ← xᵢ − lr·gᵢ`, so it stands in for the dropped whole-step proof.
383    #[kani::proof]
384    fn optimizers_step_finite() {
385        let x = any_vec();
386        let dir = any_vec();
387        let alpha = {
388            let a: f64 = kani::any();
389            kani::assume(a.is_finite());
390            kani::assume(a.abs() <= MAX_SCALE);
391            a
392        };
393        let next = step(&x, alpha, &dir);
394        assert!(
395            next.iter().all(|c| c.is_finite()),
396            "step produced a non-finite coordinate"
397        );
398    }
399}