libitofin 0.5.0

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Levenberg-Marquardt optimization method.
//!
//! Port of `ql/math/optimization/levenbergmarquardt.{hpp,cpp}`, wrapping the
//! MINPACK [`lmdif`] routine. It has a built-in forward-difference scheme to
//! compute the Jacobian, used by default; with
//! `use_cost_functions_jacobian` the cost function's own `jacobian` method
//! (a central difference by default, order 2 but costlier) is used instead.
//!
//! Several deviations guard against silent false convergence, all stemming
//! from the penalty fallbacks that `ProblemAdapter` returns for a bad
//! evaluation (which yield a zero/flat Jacobian and let lmdif report
//! convergence). The starting point is validated against the constraint -
//! QuantLib accepts an infeasible start and converges there - and the
//! initial residuals, the initial analytic Jacobian (when
//! `use_cost_functions_jacobian` is set), and the final scalar cost are all
//! checked for finiteness, so a feasible point whose evaluation is NaN/inf
//! fails loudly instead of returning a non-finite optimum. The final point
//! is also re-validated against the constraint: the flat penalty is not
//! always worse than a huge valid residual, so lmdif can accept an
//! infeasible step and stop there.

use crate::errors::QlResult;
use crate::math::array::Array;
use crate::math::matrix::Matrix;
use crate::math::optimization::endcriteria::{EndCriteria, EndCriteriaType};
use crate::math::optimization::lmdif::{LmdifCostFunction, lmdif};
use crate::math::optimization::method::OptimizationMethod;
use crate::math::optimization::problem::Problem;
use crate::require;
use crate::types::Real;

/// Levenberg-Marquardt method for least-squares problems.
pub struct LevenbergMarquardt {
    epsfcn: Real,
    xtol: Real,
    gtol: Real,
    use_cost_functions_jacobian: bool,
}

impl LevenbergMarquardt {
    /// A Levenberg-Marquardt method with the given finite-difference step
    /// seed and x/gradient tolerances, optionally using the cost function's
    /// own Jacobian.
    pub fn new(epsfcn: Real, xtol: Real, gtol: Real, use_cost_functions_jacobian: bool) -> Self {
        LevenbergMarquardt {
            epsfcn,
            xtol,
            gtol,
            use_cost_functions_jacobian,
        }
    }
}

impl Default for LevenbergMarquardt {
    /// The QuantLib defaults: `epsfcn = xtol = gtol = 1e-8`, built-in
    /// forward-difference Jacobian.
    fn default() -> Self {
        LevenbergMarquardt::new(1e-8, 1e-8, 1e-8, false)
    }
}

/// Adapts a [`Problem`] to the [`lmdif`] callbacks, steering the optimizer
/// away from infeasible or non-finite regions.
struct ProblemAdapter<'a, 'b> {
    problem: &'a mut Problem<'b>,
    init_jacobian: Option<Matrix>,
    m: usize,
    n: usize,
}

impl LmdifCostFunction for ProblemAdapter<'_, '_> {
    fn fcn(&mut self, x: &[Real], fvec: &mut [Real]) {
        let xt: Array = x.iter().copied().collect();
        if self.problem.constraint().test(&xt) {
            let tmp = self.problem.values(&xt);
            if tmp.size() == fvec.len() && tmp.iter().all(|value| value.is_finite()) {
                fvec.copy_from_slice(&tmp);
                return;
            }
        }
        // Constraint violated, wrong residual count, or non-finite values:
        // return a large, uniform penalty so the optimizer steers away. A
        // fixed constant is used instead of the initial cost values because
        // the latter can be very small (even zero) when the starting point
        // is near-optimal, which would fail to deter the optimizer from
        // exploring infeasible regions.
        fvec.fill(1.0e10);
    }

    fn has_jacobian(&self) -> bool {
        self.init_jacobian.is_some()
    }

    fn jacobian(&mut self, x: &[Real], fjac: &mut [Real]) {
        let xt: Array = x.iter().copied().collect();
        if self.problem.constraint().test(&xt) {
            let mut tmp = Matrix::with_size(self.m, self.n);
            self.problem.cost_function().jacobian(&mut tmp, &xt);
            if (0..self.m).all(|i| tmp.row(i).iter().all(|value| value.is_finite())) {
                for j in 0..self.n {
                    for i in 0..self.m {
                        fjac[i + self.m * j] = tmp[(i, j)];
                    }
                }
                return;
            }
        }
        // Constraint violated or Jacobian produced non-finite values:
        // return the initial Jacobian so the optimizer doesn't diverge
        let init = self
            .init_jacobian
            .as_ref()
            .expect("jacobian callback requires an initial jacobian");
        for j in 0..self.n {
            for i in 0..self.m {
                fjac[i + self.m * j] = init[(i, j)];
            }
        }
    }
}

impl OptimizationMethod for LevenbergMarquardt {
    fn minimize(
        &mut self,
        problem: &mut Problem<'_>,
        end_criteria: &EndCriteria,
    ) -> QlResult<EndCriteriaType> {
        problem.reset();
        let init_x = problem.current_value().clone();
        if !problem.constraint().test(&init_x) {
            crate::fail!("initial guess {init_x:?} is not in the feasible region");
        }
        let init_cost_values = problem.cost_function().values(&init_x);
        if !init_cost_values.iter().all(|value| value.is_finite()) {
            crate::fail!("initial cost values {init_cost_values:?} are not all finite");
        }
        let m = init_cost_values.size();
        let n = init_x.size();
        let init_jacobian = if self.use_cost_functions_jacobian {
            let mut jacobian = Matrix::with_size(m, n);
            problem.cost_function().jacobian(&mut jacobian, &init_x);
            // ProblemAdapter::jacobian falls back to this matrix on any bad
            // later evaluation, so a non-finite initial jacobian would poison
            // MINPACK instead of failing loudly.
            if !(0..m).all(|i| jacobian.row(i).iter().all(|value| value.is_finite())) {
                crate::fail!("initial jacobian is not all finite");
            }
            Some(jacobian)
        } else {
            None
        };
        // magic number recommended by the MINPACK documentation
        let factor = 100.0;
        // lmdif evaluates the cost function n+1 times per iteration
        // (technically 2n+1 with use_cost_functions_jacobian, which lmdif
        // doesn't account for)
        let Some(maxfev) = end_criteria.max_iterations().checked_mul(n + 1) else {
            crate::fail!(
                "maxIterations ({}) times {} evaluations per iteration overflows the evaluation budget",
                end_criteria.max_iterations(),
                n + 1
            );
        };

        // requirements; checked here to get more detailed error messages
        require!(n > 0, "no variables given");
        require!(
            m >= n,
            "less functions ({m}) than available variables ({n})"
        );
        if end_criteria.function_epsilon() < 0.0 {
            crate::fail!("negative f tolerance");
        }
        if self.xtol < 0.0 {
            crate::fail!("negative x tolerance");
        }
        if self.gtol < 0.0 {
            crate::fail!("negative g tolerance");
        }
        require!(maxfev > 0, "null number of evaluations");

        let mut xx = init_x.to_vec();
        let mut fvec = vec![0.0; m];
        let mut adapter = ProblemAdapter {
            problem,
            init_jacobian,
            m,
            n,
        };
        let info = lmdif(
            m,
            n,
            &mut xx,
            &mut fvec,
            end_criteria.function_epsilon(),
            self.xtol,
            self.gtol,
            maxfev,
            self.epsfcn,
            factor,
            &mut adapter,
        );

        require!(info != 0, "MINPACK: improper input parameters");
        require!(
            info != 7,
            "MINPACK: xtol is too small. no further improvement in the approximate solution x is possible."
        );
        require!(
            info != 8,
            "MINPACK: gtol is too small. fvec is orthogonal to the columns of the jacobian to machine precision."
        );
        let ec_type = match info {
            // 2 and 3 should be StationaryPoint, 4 a new gradient-related
            // value, but QuantLib keeps StationaryFunctionValue for
            // backwards compatibility
            1..=4 => EndCriteriaType::StationaryFunctionValue,
            5 => EndCriteriaType::MaxIterations,
            6 => EndCriteriaType::FunctionEpsilonTooSmall,
            _ => crate::fail!("unknown MINPACK result: {info}"),
        };

        let x: Array = xx.into_iter().collect();
        // the flat penalty is not always worse than a huge valid residual,
        // so lmdif can accept and stop at an infeasible point
        if !problem.constraint().test(&x) {
            crate::fail!("final point {x:?} is not in the feasible region");
        }
        problem.set_current_value(x);
        let function_value = problem.cost_function().value(problem.current_value());
        if !function_value.is_finite() {
            crate::fail!("final cost value ({function_value}) is not finite");
        }
        problem.set_function_value(function_value);
        Ok(ec_type)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::math::optimization::constraint::{NoConstraint, PositiveConstraint};
    use crate::math::optimization::costfunction::CostFunction;
    use crate::math::optimization::testsupport::{
        OneDimensionalPolynomialDegreeN, check_parabola_minimization,
    };

    #[test]
    fn minimizes_one_dimensional_parabola() {
        check_parabola_minimization(
            &mut LevenbergMarquardt::new(1e-8, 1e-8, 1e-8, false),
            "Levenberg Marquardt",
        );
    }

    #[test]
    fn minimizes_one_dimensional_parabola_with_cost_functions_jacobian() {
        check_parabola_minimization(
            &mut LevenbergMarquardt::new(1e-8, 1e-8, 1e-8, true),
            "Levenberg Marquardt (cost function's jacobian)",
        );
    }

    #[test]
    fn rejects_an_infeasible_starting_point() {
        let cost = OneDimensionalPolynomialDegreeN::new(Array::from([1.0, 1.0, 1.0]));
        let constraint = PositiveConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([-1.0]));
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-8, 1e-8, None).unwrap();
        let err = LevenbergMarquardt::default()
            .minimize(&mut problem, &end_criteria)
            .unwrap_err();
        assert!(
            err.message().contains("feasible region"),
            "unexpected message: {}",
            err.message()
        );
    }

    #[test]
    fn rejects_a_feasible_start_with_non_finite_residuals() {
        // Feasible everywhere, but the residuals are NaN: without the finite
        // check lmdif sees a flat penalty, a zero jacobian, and reports
        // convergence at a non-finite cost.
        struct NanCost;
        impl CostFunction for NanCost {
            fn values(&self, x: &Array) -> Array {
                Array::filled(1, x[0] - Real::NAN)
            }
        }
        let cost = NanCost;
        let constraint = NoConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([1.0]));
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-8, 1e-8, None).unwrap();
        let err = LevenbergMarquardt::default()
            .minimize(&mut problem, &end_criteria)
            .unwrap_err();
        assert!(
            err.message().contains("finite"),
            "unexpected message: {}",
            err.message()
        );
    }

    #[test]
    fn tolerates_a_cost_function_with_a_varying_residual_count() {
        // The residual count depends on x, so trial points can return a
        // different length than the initial evaluation; those evaluations
        // must take the penalty path instead of panicking.
        struct VaryingLength;
        impl CostFunction for VaryingLength {
            fn values(&self, x: &Array) -> Array {
                if x[0] > 0.0 {
                    Array::from([x[0], x[0]])
                } else {
                    Array::from([x[0]])
                }
            }
        }
        let cost = VaryingLength;
        let constraint = NoConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([1.0]));
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-8, 1e-8, None).unwrap();
        let _ = LevenbergMarquardt::default().minimize(&mut problem, &end_criteria);
    }

    #[test]
    fn rejects_a_non_finite_cost_function_jacobian() {
        // Residuals are finite, but the analytic jacobian is NaN: with
        // use_cost_functions_jacobian set, that matrix becomes the adapter's
        // fallback, so it must be rejected up front instead of poisoning
        // MINPACK.
        struct NanJacobian;
        impl CostFunction for NanJacobian {
            fn values(&self, x: &Array) -> Array {
                Array::filled(1, x[0])
            }
            fn jacobian(&self, jac: &mut Matrix, _x: &Array) {
                jac[(0, 0)] = Real::NAN;
            }
        }
        let cost = NanJacobian;
        let constraint = NoConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([1.0]));
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-8, 1e-8, None).unwrap();
        let err = LevenbergMarquardt::new(1e-8, 1e-8, 1e-8, true)
            .minimize(&mut problem, &end_criteria)
            .unwrap_err();
        assert!(
            err.message().contains("jacobian is not all finite"),
            "unexpected message: {}",
            err.message()
        );
    }

    #[test]
    fn rejects_an_infeasible_final_point() {
        // The unconstrained minimum is far below zero and the residuals
        // there dwarf the adapter's 1.0e10 penalty, so lmdif accepts an
        // infeasible step and "converges" outside the feasible region.
        struct HugeResidual;
        impl CostFunction for HugeResidual {
            fn values(&self, x: &Array) -> Array {
                Array::filled(1, x[0] + 1.0e11)
            }
        }
        let cost = HugeResidual;
        let constraint = PositiveConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([1.0]));
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-8, 1e-8, None).unwrap();
        let err = LevenbergMarquardt::default()
            .minimize(&mut problem, &end_criteria)
            .unwrap_err();
        assert!(
            err.message().contains("feasible region"),
            "unexpected message: {}",
            err.message()
        );
    }

    #[test]
    fn rejects_a_max_evaluation_count_that_overflows() {
        let cost = OneDimensionalPolynomialDegreeN::new(Array::from([1.0, 1.0, 1.0]));
        let constraint = NoConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::from([-100.0]));
        let end_criteria = EndCriteria::new(usize::MAX, Some(100), 1e-8, 1e-8, None).unwrap();
        let err = LevenbergMarquardt::default()
            .minimize(&mut problem, &end_criteria)
            .unwrap_err();
        assert!(
            err.message().contains("overflow"),
            "unexpected message: {}",
            err.message()
        );
    }

    // The goal of this cost function is simply to call another optimization
    // inside, in order to test nested optimizations
    struct OptimizationBasedCostFunction;

    impl CostFunction for OptimizationBasedCostFunction {
        fn value(&self, _x: &Array) -> Real {
            1.0
        }

        fn values(&self, _x: &Array) -> Array {
            // dummy nested optimization
            let inner_cost = OneDimensionalPolynomialDegreeN::new(Array::filled(3, 1.0));
            let constraint = NoConstraint;
            let mut problem = Problem::new(&inner_cost, &constraint, Array::filled(1, 100.0));
            let mut method = LevenbergMarquardt::default();
            let end_criteria = EndCriteria::new(1000, Some(100), 1e-5, 1e-5, Some(1e-5)).unwrap();
            let _ = method.minimize(&mut problem, &end_criteria);
            // return dummy result
            Array::filled(1, 0.0)
        }
    }

    #[test]
    fn supports_nested_optimizations() {
        let cost = OptimizationBasedCostFunction;
        let constraint = NoConstraint;
        let mut problem = Problem::new(&cost, &constraint, Array::filled(1, 0.0));
        let mut method = LevenbergMarquardt::default();
        let end_criteria = EndCriteria::new(1000, Some(100), 1e-5, 1e-5, Some(1e-5)).unwrap();
        method.minimize(&mut problem, &end_criteria).unwrap();
    }
}