What are ∇x (x⊤Ax) for a square matrix A and ∇x ∥Ax−b∥2, and how do you derive them?

Linear algebra and optimisation, Matrix calculus
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can differentiate vector expressions reliably, including when A is not symmetric.

Key idea

Write the scalar out with indices, x⊤Ax=∑i,jxiAijxj, and differentiate one coordinate at a time. You get (A+A⊤)x, which equals 2Ax only when A is symmetric.

Steps

  1. Write x⊤Ax=∑i,jxiAijxj and differentiate with respect to one coordinate xk. The terms with i=k give ∑jAkjxj=(Ax)k, and the terms with j=k give ∑iAikxi=(A⊤x)k.
  2. Collect the coordinates into a vector: ∇x(x⊤Ax)=(A+A⊤)x. Check with one dimension: ax2 has derivative 2ax.
  3. Expand the second expression: ∥Ax−b∥2=(Ax−b)⊤(Ax−b)=x⊤A⊤Ax−2b⊤Ax+b⊤b.
  4. Differentiate term by term. A⊤A is symmetric, so the first term gives 2A⊤Ax; and ∇x(c⊤x)=c gives −2A⊤b. So ∇x∥Ax−b∥2=2A⊤(Ax−b).
  5. Mention the use. Setting this to zero gives the normal equations A⊤Ax=A⊤b of least squares. The Hessian (matrix of second derivatives) is 2A⊤A, which is PSD (positive semidefinite), so least squares is convex.

Common mistakes

How long your answer should be

5–6 lines of derivation; 2–3 minutes.

Read this

Previous in Linear algebra and optimisation: How do you show that the negative log-likelihood of logistic regression is convex in the weights w?
Next in Linear algebra and optimisation: Which of these functions are convex, and why: (a) ∥x∥2; (b) log⁡(1+ex); (c) x3 on R; (d) f(x,y)=xy; (e) the hinge loss max⁡(0, 1−y w⊤x) as a function of w?