Gradient descent
Gradient descent walks downhill: from a point it steps against the gradient, . gradientDescent runs the method and keeps its table. When is built from , , , and integer powers, every iterate, value, gradient and step is an exact fraction, so the table is what the method does with no rounding at all; otherwise it runs in floating point. The result is labelled simulated: it shows the method from one start and proves nothing about a minimum. For a certified minimum use extrema on one variable or lagrange with constraints.
const r = gradientDescent("x^2 + 4*y^2", ["1", "1"], { step: "1/10", steps: 2 });
r.iterations.map((row) => row.x); // [[1, 1], [4/5, 1/5], [16/25, 1/25]]
r.simulated; // true
Choosing the step
- Fixed (
method: "fixed",step= 1/10 by default). The same every time. For a step above makes the coordinate grow: the step must be below where is the largest curvature. - Backtracking (
"backtracking"). Start atstep(default 1) and halve until the Armijo condition holds, with =armijo(default ). Each step is then guaranteed to decrease by a fixed fraction of what the slope promised. - Exact line search (
"exact"), for a quadratic . Along the value is a parabola in , minimised at
Successive gradients are then perpendicular, which is why the path zigzags when the level sets are long ellipses. With exact fractions this is visible exactly: the dot product of consecutive gradients is 0, not a small number.
| call | returns |
|---|---|
| gradientDescent(f, start, { variables, method = "fixed", step, steps = 10, armijo = "1/2" }) | { simulated: true, method, variables, iterations: [{ n, x, fx, gradient, step }], final, value, exact } |
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.