-
Notifications
You must be signed in to change notification settings - Fork 5
/
derivatives.rs
496 lines (426 loc) · 14.2 KB
/
derivatives.rs
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Tools for derivative-based methods.
use std::ops::Deref;
use nalgebra::{
convert,
storage::{Storage, StorageMut},
DimName, Dyn, IsContiguous, OMatrix, OVector, Vector, U1,
};
use crate::core::{Function, Problem, RealField, System};
/// Jacobian matrix of a system of equations.
#[derive(Debug)]
pub struct Jacobian<R: Problem> {
jac: OMatrix<R::Field, Dyn, Dyn>,
}
impl<R: Problem> Jacobian<R> {
/// Initializes the Jacobian matrix with zeros.
pub fn zeros(r: &R) -> Self {
let dim = Dyn(r.domain().dim());
Self {
jac: OMatrix::zeros_generic(dim, dim),
}
}
}
impl<R: System> Jacobian<R> {
/// Computes the Jacobian matrix of the system of equations in given point
/// with given scale of variables. See [`compute`](Jacobian::compute) for
/// more details.
pub fn new<Sx, Sscale, Srx>(
r: &R,
x: &mut Vector<R::Field, Dyn, Sx>,
scale: &Vector<R::Field, Dyn, Sscale>,
rx: &Vector<R::Field, Dyn, Srx>,
eps: R::Field,
step_rule: StepRule,
) -> Self
where
Sx: StorageMut<R::Field, Dyn> + IsContiguous,
Sscale: Storage<R::Field, Dyn>,
Srx: Storage<R::Field, Dyn>,
{
let mut jac = Self::zeros(r);
jac.compute(r, x, scale, rx, eps, step_rule);
jac
}
/// Computes the Jacobian matrix of the system of equations in given point
/// with given scale of variables.
///
/// The parameter `x` is mutable to allow temporary mutations avoiding
/// unnecessary allocations, but after this method ends, the content of the
/// vector is exactly the same as before.
///
/// Information about variable scale is useful for problematic cases of
/// finite differentiation (e.g., when the value is near zero).
pub fn compute<Sx, Sscale, Srx>(
&mut self,
r: &R,
x: &mut Vector<R::Field, Dyn, Sx>,
scale: &Vector<R::Field, Dyn, Sscale>,
rx: &Vector<R::Field, Dyn, Srx>,
eps_rel: R::Field,
step_rule: StepRule,
) -> &mut Self
where
Sx: StorageMut<R::Field, Dyn> + IsContiguous,
Sscale: Storage<R::Field, Dyn>,
Srx: Storage<R::Field, Dyn>,
{
let one: R::Field = convert(1.0);
for (j, mut col) in self.jac.column_iter_mut().enumerate() {
let xj = x[j];
let step = step_rule.apply(xj, one / scale[j], eps_rel);
// Update the point.
x[j] = xj + step;
r.eval(x, &mut col);
// Compute the derivative approximation: J[i, j] = (r(x + e_j * step_j) - r(x)) / step_j.
col -= rx;
col /= step;
// Restore the original value.
x[j] = xj;
}
self
}
}
impl<R: Problem> Deref for Jacobian<R> {
type Target = OMatrix<R::Field, Dyn, Dyn>;
fn deref(&self) -> &Self::Target {
&self.jac
}
}
/// Gradient vector of a function.
#[derive(Debug)]
pub struct Gradient<F: Problem> {
grad: OVector<F::Field, Dyn>,
}
impl<F: Problem> Gradient<F> {
/// Initializes the gradient vector with zeros.
pub fn zeros(f: &F) -> Self {
let dim = Dyn(f.domain().dim());
Self {
grad: OVector::zeros_generic(dim, U1::name()),
}
}
}
impl<F: Function> Gradient<F> {
/// Computes the gradient vector of the function in given point with given
/// scale of variables. See [`compute`](Gradient::compute) for more details.
pub fn new<Sx, Sscale>(
f: &F,
x: &mut Vector<F::Field, Dyn, Sx>,
scale: &Vector<F::Field, Dyn, Sscale>,
fx: F::Field,
eps_rel: F::Field,
step_rule: StepRule,
) -> Self
where
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sscale: Storage<F::Field, Dyn>,
{
let mut grad = Self::zeros(f);
grad.compute(f, x, scale, fx, eps_rel, step_rule);
grad
}
/// Computes the gradient vector of the function in given point with given
/// scale of variables.
///
/// The parameter `x` is mutable to allow temporary mutations avoiding
/// unnecessary allocations, but after this method ends, the content of the
/// vector is exactly the same as before.
///
/// Information about variable scale is useful for problematic cases of
/// finite differentiation (e.g., when the value is near zero).
pub fn compute<Sx, Sscale>(
&mut self,
f: &F,
x: &mut Vector<F::Field, Dyn, Sx>,
scale: &Vector<F::Field, Dyn, Sscale>,
fx: F::Field,
eps_rel: F::Field,
step_rule: StepRule,
) -> &mut Self
where
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sscale: Storage<F::Field, Dyn>,
{
let one: F::Field = convert(1.0);
for i in 0..x.nrows() {
let xi = x[i];
let step = step_rule.apply(xi, one / scale[i], eps_rel);
// Update the point.
x[i] = xi + step;
let fxi = f.apply(x);
// Compute the derivative approximation: grad[i] = (f(x + e_i * step_i) - f(x)) / step_i.
self.grad[i] = (fxi - fx) / step;
// Restore the original value.
x[i] = xi;
}
self
}
}
impl<F: Problem> Deref for Gradient<F> {
type Target = OVector<F::Field, Dyn>;
fn deref(&self) -> &Self::Target {
&self.grad
}
}
/// Hessian matrix of a function.
#[derive(Debug)]
pub struct Hessian<F: Problem> {
hes: OMatrix<F::Field, Dyn, Dyn>,
steps: OVector<F::Field, Dyn>,
neighbors: OVector<F::Field, Dyn>,
}
impl<F: Problem> Hessian<F> {
/// Initializes the Hessian matrix with zeros.
pub fn zeros(f: &F) -> Self {
let dim = Dyn(f.domain().dim());
Self {
hes: OMatrix::zeros_generic(dim, dim),
steps: OVector::zeros_generic(dim, U1::name()),
neighbors: OVector::zeros_generic(dim, U1::name()),
}
}
}
impl<F: Function> Hessian<F> {
/// Computes the Hessian matrix of the function in given point with given
/// scale of variables. See [`compute`](Hessian::compute) for more details.
pub fn new<Sx, Sscale>(
f: &F,
x: &mut Vector<F::Field, Dyn, Sx>,
scale: &Vector<F::Field, Dyn, Sscale>,
fx: F::Field,
eps_rel: F::Field,
step_rule: StepRule,
) -> Self
where
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sscale: Storage<F::Field, Dyn>,
{
let mut hes = Self::zeros(f);
hes.compute(f, x, scale, fx, eps_rel, step_rule);
hes
}
/// Computes the Hessian matrix of the function in given point with given
/// scale of variables.
///
/// The parameter `x` is mutable to allow temporary mutations avoiding
/// unnecessary allocations, but after this method ends, the content of the
/// vector is exactly the same as before.
///
/// Information about variable scale is useful for problematic cases of
/// finite differentiation (e.g., when the value is near zero).
pub fn compute<Sx, Sscale>(
&mut self,
f: &F,
x: &mut Vector<F::Field, Dyn, Sx>,
scale: &Vector<F::Field, Dyn, Sscale>,
fx: F::Field,
eps_rel: F::Field,
step_rule: StepRule,
) -> &mut Self
where
Sx: StorageMut<F::Field, Dyn> + IsContiguous,
Sscale: Storage<F::Field, Dyn>,
{
let one: F::Field = convert(1.0);
for i in 0..x.nrows() {
let xi = x[i];
let step = step_rule.apply(xi, one / scale[i], eps_rel);
// Store the step for Hessian calculation.
self.steps[i] = step;
// Update the point and store the function output.
x[i] = xi + step;
let fxi = f.apply(x);
self.neighbors[i] = fxi;
// Restore the original value.
x[i] = xi;
}
for i in 0..x.nrows() {
let xi = x[i];
let stepi = self.steps[i];
// Prepare x_i + 2 * e_i.
x[i] = xi + stepi + stepi;
let fxi = f.apply(x);
let fni = self.neighbors[i];
x[i] = xi + stepi;
self.hes[(i, i)] = ((fx - fni) + (fxi - fni)) / (stepi * stepi);
for j in (i + 1)..x.nrows() {
let xj = x[j];
let stepj = self.steps[j];
x[j] = xj + stepj;
let fxj = f.apply(x);
let fnj = self.neighbors[j];
let hij = ((fx - fni) + (fxj - fnj)) / (stepi * stepj);
self.hes[(i, j)] = hij;
self.hes[(j, i)] = hij;
x[j] = xj;
}
x[i] = xi;
}
self
}
}
impl<F: Problem> Deref for Hessian<F> {
type Target = OMatrix<F::Field, Dyn, Dyn>;
fn deref(&self) -> &Self::Target {
&self.hes
}
}
/// Rule for computing the step in finite difference method.
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub enum StepRule {
/// _eps<sub>rel</sub>_
Fixed,
/// _eps<sub>rel</sub> * x_
ValueScaled,
/// _eps<sub>rel</sub> * |x|_
#[default]
AbsValueScaled,
/// _eps<sub>rel</sub> * max(|x|, mag) * sign(x)_
ValueOrMagScaled,
/// _eps<sub>rel</sub> * max(|x|, mag)_
AbsValueOrMagScaled,
/// _eps<sub>rel</sub> * max(|x|, mag) * x_
ValueOrMagTimesValueScaled,
/// _eps<sub>rel</sub> * max(|x|, mag) * |x|_
AbsValueOrMagTimesAbsValueScaled,
}
impl StepRule {
fn apply<T: RealField + Copy>(&self, x: T, mag: T, eps_rel: T) -> T {
// Compute the step size. We would like to have the step as small as
// possible (to be as close to the zero -- i.e., real derivative -- as
// possible). But at the same time, very small step could cause r(x +
// e_j * step_j) ~= r(x) with very small number of good digits.
//
// We implement multiple rules. One is just using fixed relative epsilon
// value. The other use some form of scaling by the variable value,
// which balances the competing needs mentioned in the first paragraph.
// Some of them also utilize the information of estimated typical
// magnitude of the variable, mostly to avoid problems when the variable
// value is close to zero. Some methods inherit the sign of the variable
// value for the step size, while the others always use a positive
// value.
let one = convert(1.0);
let scale = match self {
StepRule::Fixed => one,
StepRule::ValueScaled => x,
StepRule::AbsValueScaled => x.abs(),
StepRule::ValueOrMagScaled => x.abs().max(mag) * one.copysign(x),
StepRule::AbsValueOrMagScaled => x.abs().max(mag),
StepRule::ValueOrMagTimesValueScaled => x.abs().max(mag) * x,
StepRule::AbsValueOrMagTimesAbsValueScaled => x.abs().max(mag) * x.abs(),
};
// Since some of the rules use the value of x directly, we need to check
// for cases when the value is zero, in which case we need to fallback
// to some non-zero step size.
if scale != convert(0.0) {
eps_rel * scale
} else {
eps_rel
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
core::Domain,
testing::{ExtendedPowell, ExtendedRosenbrock},
};
use approx::assert_abs_diff_eq;
use nalgebra::{dmatrix, dvector, Dyn};
struct MixedVars;
impl Problem for MixedVars {
type Field = f64;
fn domain(&self) -> Domain<Self::Field> {
Domain::unconstrained(2)
}
}
impl Function for MixedVars {
fn apply<Sx>(&self, x: &Vector<Self::Field, Dyn, Sx>) -> Self::Field
where
Sx: Storage<Self::Field, Dyn> + IsContiguous,
{
// A simple, arbitrary function that produces Hessian matrix with
// non-zero corners.
let x1 = x[0];
let x2 = x[1];
x1.powi(2) + x1 * x2 + x2.powi(3)
}
}
#[test]
fn rosenbrock_jacobian() {
let mut x = dvector![2.0, 2.0];
let scale = dvector![1.0, 1.0];
let mut fx = dvector![0.0, 0.0];
let func = ExtendedRosenbrock::new(2);
func.eval(&x, &mut fx);
let jac = Jacobian::new(
&func,
&mut x,
&scale,
&fx,
f64::EPSILON_SQRT,
StepRule::default(),
);
let expected = dmatrix![-40.0, 10.0; -1.0, 0.0];
assert_abs_diff_eq!(&*jac, &expected, epsilon = 10e-6);
}
#[test]
fn powell_jacobian_in_root() {
let mut x = dvector![0.0, 0.0, 0.0, 0.0];
let scale = dvector![1.0, 1.0, 1.0, 1.0];
let mut fx = dvector![0.0, 0.0, 0.0, 0.0];
let func = ExtendedPowell::new(4);
func.eval(&x, &mut fx);
let jac = Jacobian::new(
&func,
&mut x,
&scale,
&fx,
f64::EPSILON_SQRT,
StepRule::default(),
);
let expected = dmatrix![
1.0, 10.0, 0.0, 0.0;
0.0, 0.0, 5f64.sqrt(), -(5f64.sqrt());
0.0, 0.0, 0.0, 0.0;
0.0, 0.0, 0.0, 0.0
];
assert_abs_diff_eq!(&*jac, &expected, epsilon = 10e-6);
}
#[test]
fn mixed_vars_gradient() {
let mut x = dvector![3.0, -3.0];
let scale = dvector![1.0, 1.0];
let func = MixedVars;
let fx = func.apply(&x);
let grad = Gradient::new(
&func,
&mut x,
&scale,
fx,
f64::EPSILON_SQRT,
StepRule::default(),
);
let expected = dvector![3.0, 30.0];
assert_abs_diff_eq!(&*grad, &expected, epsilon = 10e-6);
}
#[test]
fn mixed_vars_hessian() {
let mut x = dvector![3.0, -3.0];
let scale = dvector![1.0, 1.0];
let func = MixedVars;
let fx = func.apply(&x);
let hes = Hessian::new(
&func,
&mut x,
&scale,
fx,
f64::EPSILON_CBRT,
StepRule::default(),
);
let expected = dmatrix![2.0, 1.0; 1.0, -18.0];
assert_abs_diff_eq!(&*hes, &expected, epsilon = 10e-3);
}
}