Algorithmadvanced
Powell's dog leg method
Also called dog leg trust region.
A trust region method for least squares whose step follows the path from the steepest descent point to the Gauss-Newton point, cut at the region boundary.
- Solves
- Nonlinear least squares
- Time
- Θ(m n^2 + n^3) per_iteration
- Space
- Θ(m n) extra
- Counts
- evaluations 1 per_iteration
- Where
- k = the number of steps tried, accepted or rejected; m = the number of residuals; n = the number of parameters
- Model
- Real RAM
- Properties
- deterministiciterativelocalmonotoneiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- M. J. D. Powell, 1970
Powell's dog leg method minimizes a sum of squares by a trust region strategy. At each step it trusts the linear model only inside a ball of radius . It computes two steps: the Gauss-Newton step , which minimizes the model, and the steepest descent step , which minimizes it along the negative gradient. If fits in the ball it takes . If even does not fit, it takes the steepest descent direction cut at the boundary. Otherwise it follows the bent path from to to , the "dog leg", and stops where the path leaves the ball. The radius grows after good steps and shrinks after bad ones, and a step is kept only if it lowers . The nonlinear least squares page states the problem. Compared with the close methods: Gauss-Newton always takes , with no safeguard. Levenberg-Marquardt also moves between the two steps, but through a damping parameter, and it must solve a new linear system after each rejected step. The dog leg method keeps and when a step is rejected and only shrinks the radius, so a rejection costs no new factorization. This is why Lourakis and Argyros proposed it for bundle adjustment.
In plain words
You are walking downhill in fog and can only trust your map for a short distance around you. The map shows two useful targets: the very bottom of the valley as the map draws it, and the best point if you walk straight downhill. If the bottom is within your trusted distance, you go there. If even the straight-downhill point is too far, you walk straight downhill until you reach the edge of the trusted area. Otherwise you walk to the downhill point and then turn toward the bottom, and stop at the edge. After the step you compare the real height with the map. If they agree well, you trust the map farther next time; if they disagree, you trust it less. The bent path looks like a dog's hind leg, which is where the name comes from.
The problem
Input. A residual function , its Jacobian , a start , a start radius , tolerances and , and an iteration limit . The method as written needs of full column rank at every iterate.
Output. A point with at which is small, or the last iterate when a limit is reached.
The algorithm
The version below is Algorithm 3.21 of Madsen, Nielsen and Tingleff (2004).
DOG-LEG(r, J, x_0, Δ_0, ε_1, ε_2, k_max)- 1
x <- x_0 ; Δ <- Δ_0 ; r <- r(x) ; J <- J(x) - 2
for k <- 1 to k_max - 3
g <- J^T r - 4
if ||g||_∞ <= ε_1 then return x - 5
α <- ||g||^2 / ||J g||^2 - 6
a <- -α g ; solve (J^T J) b = -g - 7
if ||b|| <= Δ then h <- b - 8
else if ||a|| >= Δ then h <- -(Δ / ||g||) g - 9
else h <- a + β (b - a), with β in (0, 1) such that ||h|| = Δ - 10
if ||h|| <= ε_2 (||x|| + ε_2) then return x - 11
ρ <- (F(x) - F(x + h)) / (L(0) - L(h)) - 12
if ρ > 0 then x <- x + h ; r <- r(x) ; J <- J(x) - 13
if ρ > 0.75 then Δ <- max(Δ, 3 ||h||) - 14
else if ρ < 0.25 then Δ <- Δ / 2 - 15
return x
Line L5 is the exact minimizer of the model along , so in line L6 is the steepest descent point of the model; is the Gauss-Newton step. Lines L7 to L9 choose the step: inside the region, the cut steepest descent step if is outside, and otherwise the point where the segment from to crosses the boundary. Line L9 solves a scalar quadratic for ; there is exactly one root in . Line L11 compares the actual decrease with the decrease of the model, . Line L12 keeps the step if went down. Lines L13 and L14 update the radius. When a step is rejected, , and do not change, so the next try needs only a new cut of the same path; the reference code recomputes them for simplicity.
Example
Take the Rosenbrock residuals at . Then , , and . Line L5 gives , so with , and with . The step for four radii:
| line | next | |||||
|---|---|---|---|---|---|---|
| L8 | , accepted | |||||
| L9 | , accepted | |||||
| L9 | , rejected | |||||
| L7 | , rejected |
The Gauss-Newton step zeroes the model but raises almost a hundredfold: the linear model is good only near . The radius keeps the step where the model holds. From the reference code reaches to machine precision after 21 tries, 12 of them accepted. On the one-parameter example , where plain Gauss-Newton does not converge, it converges to , slowly: about 90 tries to reach , since the radius keeps cutting the Gauss-Newton step that overshoots. These counts are measurements on these examples only.
Correctness
The step of lines L7 to L9 lies in the region, and on its boundary unless it is the Gauss-Newton step (theorem). Along the dog leg path the model decreases and the distance from increases (theorem), so the step is at least as good for the model as the Cauchy point, the best point along inside the region. This gives the Cauchy decrease (theorem), so the model decrease is positive and every accepted step lowers (theorem). The proof file below proves these facts. From the Cauchy decrease, the general trust region theory gives when is bounded below and has a Lipschitz gradient, for a radius rule with an upper bound (Nocedal and Wright, Theorem 4.5; theorem, not proved here). The rule of lines L13 and L14 has no upper bound; add one if you need the theorem. Near a minimizer with zero residual and full rank , the radius stops binding and the method takes Gauss-Newton steps, with their quadratic rate.
Proof: The dog leg step stays in the trust region, decreases the model at least as much as the Cauchy point, and accepted steps lower the costfull
We use the notation of the page. At the current point, is the residual, the Jacobian with full column rank, (positive definite), , and is the model. The steepest descent step is with (line L5), the Gauss-Newton step is (line L6), and is the radius. The Cauchy point is the minimizer of on the set .
Claim. (a) The step of lines L7 to L9 satisfies , with equality unless . (b) . (c) . (d) Every accepted step of line L12 lowers strictly.
Lemma 1 (the first leg). For , decreases in , and .
Proof. is a convex quadratic with minimum at , so it decreases on . For , , so .
So with : it is if and otherwise.
Lemma 2 (the second leg). Let for . Then is nondecreasing and is nonincreasing in .
Proof. is a convex quadratic in . Its derivative at is , because . The derivative of a convex function is nondecreasing, so it is on .
For the norm, . Now and , so . By the Cauchy-Schwarz inequality for and , , so the bracket is .
Proof of (a). There are three cases. Line L7: and . Line L8: and , of length . Line L9: . Then , and by Lemma 2 and continuity there is exactly one with (the norm is nondecreasing and its square is a strictly convex quadratic in , so it cannot stay constant on an interval). With , and , the equation is . Its roots have product , so exactly one is positive, namely . The code uses the equal form when , to avoid cancellation; this is the formula (3.20b) of Madsen, Nielsen and Tingleff. So .
Proof of (b). In the case of line L7, minimizes on all of , so . In the case of line L8, , so and . In the case of line L9, , so , and by Lemma 2.
Proof of (c). By (b) and Lemma 1 with , . Since , . So the decrease is at least , which is positive because .
Proof of (d). Line L12 accepts the step when . The denominator is positive by (c), so . The reference code computes the denominator as and stops if rounding makes it nonpositive, which only happens when no decrease is left to find.
Global convergence. Claim (c) is the Cauchy decrease condition of trust region theory (Nocedal and Wright, Lemma 4.3). With it, a trust region method that accepts every step with and shrinks the radius after poor steps has , if is bounded below on the level set of , has a Lipschitz gradient near it, and the model Hessians are bounded (Nocedal and Wright, Theorem 4.5). With an acceptance threshold , the whole sequence tends to zero (Theorem 4.6). Those theorems use a radius rule with a fixed upper bound and fixed shrink and grow factors. The rule of lines L13 and L14 is the one of Madsen, Nielsen and Tingleff (Algorithm 3.21), which has no upper bound on the radius, so the theorems apply to it only after adding such a bound. We do not claim more for the code as written.
Complexity
In the real RAM model, one try costs one evaluation of , for and , to form and for its dense Cholesky factorization, so plus the evaluation. An implementation that caches and pays the part only once per accepted step; a rejected try then costs plus one evaluation of . The extra space is . The number of tries has no useful general bound. In bundle adjustment, the Gauss-Newton step is computed with the reduced camera system, and the caching matters because that solve is the most expensive part of a step.
Properties
- Monotone (theorem). Accepted costs decrease strictly.
- Cheap rejections. After a rejected step only the radius changes, and the same and give the next step. In Levenberg-Marquardt a rejection needs a new factorization.
- Needs full rank. The Gauss-Newton step of line L6 needs of full column rank. With a gauge freedom, fix the gauge or add a tiny damping.
- Scale dependence. The region is a ball, so the step depends on the units of the parameters. A scaled norm removes this.
- Steps on a piecewise linear path. The dog leg only approximates the exact trust region step, which lies on a curved path; the approximation is good when is well conditioned.
Variants and improvements
- Double dog leg. Dennis and Mei move the turning point from toward , so the path bends less and the steps are closer to the Gauss-Newton step.
- Exact trust region step. Solve subject to exactly by finding the for which the Levenberg-Marquardt step has length (Moré 1978). This is more accurate per step and more expensive.
- Two-dimensional subspace minimization. Minimize the model exactly over the span of and inside the region.
- Steihaug's truncated CG. For large problems, run the conjugate gradient method on the normal equations from and stop at the boundary. The first CG iterate is the Cauchy point, so the Cauchy decrease still holds.
- Sparse dog leg for bundle adjustment. Lourakis and Argyros (2005) compute with the sparse block structure and report that, on their tests, the dog leg method was faster than sparse Levenberg-Marquardt for the same accuracy (a benchmark result).
When to use it
Use the dog leg method when one solve of the Gauss-Newton system is expensive compared with an evaluation of the residuals, so that cheap rejections pay off, as in bundle adjustment. Use it also when you want an explicit trust radius, for example to limit the step in a robot or a tracker. When can be rank deficient, Levenberg-Marquardt is simpler, since its damping makes every system solvable. When the start is good and the residuals are small, plain Gauss-Newton is enough.
Pitfalls
- A wrong model decrease. Line L11 must use for the step actually taken. Using the decrease of the Gauss-Newton step for a cut step gives a wrong .
- Rounding in line L9. The textbook root cancels badly when ; use the second form. The reference code does both cases.
- Zero model decrease near convergence. Near the solution, can round to , and line L11 divides by zero. The reference code stops when it is not positive.
- Rank deficient . Line L6 fails without a gauge fix. Do not silently replace by a pseudoinverse step without checking the length.
- A start radius far off. A that is much too small wastes many accepted but tiny steps, because line L13 at most triples the radius per step. Scale to the size of .
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/powell-dogleg/. The authors are listed in prov/GQ5JQ6.jsonl.
"""Powell's dog leg method for nonlinear least squares.
The problem is to minimize F(x) = 1/2 ||r(x)||^2 for r: R^n -> R^m. Each
step works in a trust region of radius Delta around x. It takes the
Gauss-Newton step b if ||b|| <= Delta; else the steepest descent step cut at
the boundary if the Cauchy step a = -alpha g is already outside; else the
point where the segment from a to b meets the boundary. The radius update is
the one of Madsen, Nielsen and Tingleff (2004, Algorithm 3.21).
"""
import math
from typing import Callable, List, Sequence, Tuple
Vector = List[float]
Matrix = List[List[float]]
def cost(r: Sequence[float]) -> float:
"""F = 1/2 ||r||^2."""
return 0.5 * sum(v * v for v in r)
def _norm(v: Sequence[float]) -> float:
return math.sqrt(sum(x * x for x in v))
def _matvec(a: Sequence[Sequence[float]], x: Sequence[float]) -> Vector:
return [sum(ai * xi for ai, xi in zip(row, x)) for row in a]
def cholesky_solve(a: Sequence[Sequence[float]], b: Sequence[float]) -> Vector:
"""Solve a x = b for a symmetric positive definite a by a = L L^T."""
n = len(a)
low = [[0.0] * n for _ in range(n)]
for j in range(n):
d = a[j][j] - sum(low[j][k] ** 2 for k in range(j))
if d <= 0.0:
raise ValueError("J^T J is not positive definite: J is rank deficient")
low[j][j] = math.sqrt(d)
for i in range(j + 1, n):
low[i][j] = (a[i][j] - sum(low[i][k] * low[j][k] for k in range(j))) / low[j][j]
y = [0.0] * n
for i in range(n):
y[i] = (b[i] - sum(low[i][k] * y[k] for k in range(i))) / low[i][i]
x = [0.0] * n
for i in range(n - 1, -1, -1):
x[i] = (y[i] - sum(low[k][i] * x[k] for k in range(i + 1, n))) / low[i][i]
return x
def dogleg_step(jac: Sequence[Sequence[float]], r: Sequence[float], delta: float) -> Tuple[Vector, str]:
"""The dog leg step for the model L(h) = 1/2 ||r + J h||^2 and radius delta.
Returns (h, kind), kind is "gauss-newton", "steepest" or "dogleg"."""
m, n = len(jac), len(jac[0])
g = [sum(jac[k][i] * r[k] for k in range(m)) for i in range(n)]
jg = _matvec(jac, g)
alpha = sum(v * v for v in g) / sum(v * v for v in jg) # @line L5
a = [-alpha * v for v in g] # @line L6
jtj = [[sum(jac[k][i] * jac[k][j] for k in range(m)) for j in range(n)] for i in range(n)]
b = cholesky_solve(jtj, [-v for v in g])
if _norm(b) <= delta: # @line L7
return b, "gauss-newton"
if _norm(a) >= delta: # @line L8
return [-(delta / _norm(g)) * v for v in g], "steepest"
d = [bi - ai for ai, bi in zip(a, b)] # @line L9
c = sum(ai * di for ai, di in zip(a, d))
dd = sum(v * v for v in d)
rad = delta * delta - sum(v * v for v in a) # positive, since ||a|| < delta
if c <= 0:
beta = (-c + math.sqrt(c * c + dd * rad)) / dd
else:
beta = rad / (c + math.sqrt(c * c + dd * rad))
return [ai + beta * di for ai, di in zip(a, d)], "dogleg"
def powell_dogleg(residual: Callable[[Vector], Vector],
jacobian: Callable[[Vector], Matrix],
x0: Sequence[float], delta0: float = 1.0,
eps1: float = 1e-12, eps2: float = 1e-14,
max_iter: int = 200):
"""Run the dog leg method from x0 with initial radius delta0.
Returns (x, info). info["costs"] holds F at x0 and after each accepted
step; info["steps"] holds (kind, ||h||, delta) for each step tried."""
x, delta = [float(v) for v in x0], float(delta0) # @line L1
r, jac = residual(x), jacobian(x)
f = cost(r)
info = {"costs": [f], "steps": []}
for _ in range(max_iter): # @line L2
g = [sum(jac[k][i] * r[k] for k in range(len(r))) for i in range(len(x))] # @line L3
if max(abs(v) for v in g) <= eps1: # @line L4
break
h, kind = dogleg_step(jac, r, delta)
info["steps"].append((kind, _norm(h), delta))
if _norm(h) <= eps2 * (_norm(x) + eps2): # @line L10
break
x_new = [xi + hi for xi, hi in zip(x, h)]
r_new = residual(x_new)
f_new = cost(r_new)
jh = _matvec(jac, h)
predicted = -sum(gi * hi for gi, hi in zip(g, h)) - 0.5 * sum(v * v for v in jh) # L(0) - L(h)
if predicted <= 0.0: # only rounding can give this: no decrease is left
break
rho = (f - f_new) / predicted # @line L11
if rho > 0: # @line L12
x, r, f = x_new, r_new, f_new
jac = jacobian(x)
info["costs"].append(f)
if rho > 0.75: # @line L13
delta = max(delta, 3.0 * _norm(h))
elif rho < 0.25: # @line L14
delta /= 2.0
return x, info # @line L15
test_powell_dogleg.py 102 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/powell-dogleg/. The authors are listed in prov/GQ5JQ6.jsonl.
import math
import random
from powell_dogleg import cost, dogleg_step, powell_dogleg
T = [0.0, 1.0, 2.0, 3.0]
Y = [2.0 * math.exp(-0.5 * t) for t in T]
def exp_r(x):
return [x[0] * math.exp(x[1] * t) - y for t, y in zip(T, Y)]
def exp_j(x):
return [[math.exp(x[1] * t), x[0] * t * math.exp(x[1] * t)] for t in T]
def rosen_r(x):
return [10.0 * (x[1] - x[0] ** 2), 1.0 - x[0]]
def rosen_j(x):
return [[-20.0 * x[0], 10.0], [-1.0, 0.0]]
def model(jac, r, h):
jh = [sum(a * b for a, b in zip(row, h)) for row in jac]
return cost([ri + v for ri, v in zip(r, jh)])
def test_worked_example_steps():
jac, r = rosen_j([-1.2, 1.0]), rosen_r([-1.2, 1.0])
h, kind = dogleg_step(jac, r, 6.0)
assert kind == "gauss-newton" and abs(h[0] - 2.2) < 1e-12 and abs(h[1] + 4.84) < 1e-12
h, kind = dogleg_step(jac, r, 1.0)
assert kind == "dogleg" and abs(math.hypot(*h) - 1.0) < 1e-12
assert abs(h[0] - 0.5372) < 1e-4 and abs(h[1] + 0.8434) < 1e-4
h, kind = dogleg_step(jac, r, 0.1)
assert kind == "steepest" and abs(math.hypot(*h) - 0.1) < 1e-12
def test_step_properties_on_random_models():
rng = random.Random(23)
for _ in range(200):
m, n = rng.randint(3, 6), rng.randint(1, 3)
jac = [[rng.gauss(0, 1) for _ in range(n)] for _ in range(m)]
r = [rng.gauss(0, 1) for _ in range(m)]
delta = 10 ** rng.uniform(-2, 1)
h, kind = dogleg_step(jac, r, delta)
nh = math.sqrt(sum(v * v for v in h))
assert nh <= delta * (1 + 1e-12)
g = [sum(jac[k][i] * r[k] for k in range(m)) for i in range(n)]
ng = math.sqrt(sum(v * v for v in g))
jg = [sum(a * b for a, b in zip(row, g)) for row in jac]
# the Cauchy point: the model minimizer along -g inside the region
t = min(delta / ng, ng * ng / sum(v * v for v in jg))
cauchy = model(jac, r, [-t * v for v in g])
assert model(jac, r, h) <= cauchy + 1e-10
assert model(jac, r, h) <= cost(r) + 1e-12
if kind != "gauss-newton":
assert abs(nh - delta) < 1e-9 * max(1.0, delta)
def test_model_decreases_along_the_dog_leg_path():
rng = random.Random(29)
for _ in range(50):
jac = [[rng.gauss(0, 1) for _ in range(2)] for _ in range(4)]
r = [rng.gauss(0, 1) for _ in range(4)]
last_len, last_val = -1.0, float("inf")
for delta in [0.01 * 1.3 ** i for i in range(30)]:
h, _ = dogleg_step(jac, r, delta)
nh = math.sqrt(sum(v * v for v in h))
val = model(jac, r, h)
assert nh >= last_len - 1e-12 and val <= last_val + 1e-12
last_len, last_val = nh, val
def test_rosenbrock_converges_with_decreasing_cost():
x, info = powell_dogleg(rosen_r, rosen_j, [-1.2, 1.0], delta0=1.0)
assert abs(x[0] - 1.0) < 1e-8 and abs(x[1] - 1.0) < 1e-8
costs = info["costs"]
assert all(c1 < c0 for c0, c1 in zip(costs, costs[1:]))
for kind, nh, delta in info["steps"]:
assert nh <= delta * (1 + 1e-12)
def test_zero_residual_exact_recovery():
x, _ = powell_dogleg(exp_r, exp_j, [1.0, 0.0], delta0=0.5)
assert abs(x[0] - 2.0) < 1e-8 and abs(x[1] + 0.5) < 1e-8
def test_converges_where_gauss_newton_fails():
lam = -2.0
r = lambda x: [x[0] + 1.0, lam * x[0] ** 2 + x[0] - 1.0]
j = lambda x: [[1.0], [2.0 * lam * x[0] + 1.0]]
x, info = powell_dogleg(r, j, [0.1], delta0=1.0, max_iter=400)
assert abs(x[0]) < 1e-6
costs = info["costs"]
assert all(c1 < c0 for c0, c1 in zip(costs, costs[1:]))
History
M. J. D. Powell introduced the hybrid step in 1970 for systems of nonlinear equations: inside a region of trusted radius, combine the steepest descent step and the Newton step, and change the radius by comparing actual and predicted reductions. This is one of the first trust region methods. The same idea applies to least squares with the Gauss-Newton step in place of the Newton step, as in the lecture notes of Madsen, Nielsen and Tingleff (2004), who remark that the name comes from golf, a game Powell played. Dennis and Mei proposed the double dog leg. In computer vision, Lourakis and Argyros (2005) asked whether Levenberg-Marquardt was the most efficient method for bundle adjustment and showed that a sparse dog leg method was faster on their tests.
References
- M. J. D. Powell, A hybrid method for nonlinear equations, in P. Rabinowitz (ed.), Numerical Methods for Nonlinear Algebraic Equations, Gordon and Breach, London, 1970, the whole chapter (the step that combines the steepest descent and the Newton step inside a region of fixed radius)
- Kaj Madsen, Hans Bruun Nielsen and Ole Tingleff, Methods for Non-Linear Least Squares Problems, second edition, Informatics and Mathematical Modelling, Technical University of Denmark, 2004, Section 3.3, Powell's dog leg method (the formula (3.20b) for the step on the second leg and Algorithm 3.21 with its radius update)
- Jorge Nocedal and Stephen J. Wright, Numerical Optimization, second edition, Springer, 2006, Chapter 4, Trust-region methods (Section 4.1, the Cauchy point and the dogleg method; Lemma 4.3, the Cauchy decrease; Section 4.2, Theorems 4.5 and 4.6 on global convergence)
- Manolis I. A. Lourakis and Antonis A. Argyros, Is Levenberg-Marquardt the most efficient optimization algorithm for implementing bundle adjustment?, Tenth IEEE International Conference on Computer Vision (ICCV 2005), vol. 2, 1526-1531, the whole paper (a sparse dog leg for bundle adjustment and its comparison with sparse Levenberg-Marquardt)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag GQ5JQ6 Edit this page History Report an error Markdown