16

I am trying to find the minimum of $(Ax-b)^T(Ax-b)$ but I am not sure whether I am taking the derivative of this expression properly.

What I did is the following: \begin{align*} \frac{\delta}{\delta x_i}\left(\sum_i \sum_j (A_{ij}x_i-b_i)(A_{ij}x_j-b_j)\right)&= \sum_j A_{ij}(A_{ij}x_j-b_j) + \sum_i A_{ij}(A_{ij}x_j-b_i) \end{align*}

but I'm not quite sure if this is correct and what the derivate would then be. Any help is appreciated.

Shuchang
  • 10,058
dreamer
  • 3,479

5 Answers5

18

Perhaps some help to compute the partial derivatives would be appreciated. It is always best to be explicit when one is a bit confused with heavy notation. Write $A = (a_{ij})$, $x = (x_1,\dots,x_n)^{\top}$ and $b = (b_1,\dots, b_m)^{\top}$, assuming $A$ is an $m \times n$ matrix. Then the $i^{\text{th}}$ component of $Ax-b$ is $$ (Ax-b)_i = \left( \sum_{j=1}^n a_{ij} x_j \right) - b_i $$ so that $$ (Ax-b)^{\top} (Ax-b) = \sum_{i=1}^m (Ax-b)_i^2 = \sum_{i=1}^m \left( \left( \sum_{j=1}^n a_{ij} x_j \right) - b_i \right)^2. $$ Suppose you want to compute the derivative with respect to $x_k$, $1 \le k \le n$ (I choose $k$ because choosing $i$ or $j$ would be confusing with the preceding subscripts used). Then $$ \frac{\partial}{\partial x_k} (Ax-b)^{\top} (Ax-b) = \sum_{i=1}^m \frac{\partial}{\partial x_k} \left( \left( \sum_{j=1}^n a_{ij} x_j \right) - b_i \right)^2 = \sum_{i=1}^m 2 \left( \left( \sum_{j=1}^n a_{ij}x_j \right) - b_i \right) (a_{ik}). $$ In particular, we can let $A_k = (a_{1k},a_{2k},\dots,a_{mk})$ so that $$ \frac{\partial}{\partial x_k} (Ax-b)^{\top} (Ax-b) = 2 \langle A_k, Ax-b \rangle $$ where $\langle - , - \rangle$ denotes inner product. You can use convexity arguments to show that any critical point is a minimizer in this case ; although you can see that the minimizer will not always be unique, even when $m=n$ ; it suffices for $A$ to be singular for this to happen.

Hope that helps,

  • 1
    Thank you so much, I really, really appreciate your help :)! This clarifies everything, many thanks :)! – dreamer Dec 17 '13 at 17:11
  • 2
    @901301 : Don't worry about it. :) – Patrick Da Silva Dec 18 '13 at 01:50
  • the gradient expressed in whole terms then becomes $2 A^T (Ax - b)$? Why is this the transpose of the answer given below, i.e, $2 (Ax - b)^T A$? Minimizing the gradient, then results in the same equation for x, i.e, $x = (A^T A)^(-1) A^T b$? I am not very familiar with vector calculus, but if someone could provide a explanation that would be great :) – Still a learner Sep 07 '23 at 13:18
  • 1
    I went back to calculus notation to explain what's going on with the matrix notation, but if you want, you can stay in matrix notation and compute derivatives using the fact that if $A,B$ are two matrix functions of $x$, then $$ \frac {\partial}{\partial x_k} AB = (\frac {\partial}{\partial x_k} A)B + A(\frac {\partial}{\partial x_k} B), \qquad \frac {\partial}{\partial x_k} x = e_k $$ where $e_k$ is the $k^{\text{th}}$ unit basis vector. This basically follows from the classical product rule, distributed over sums for each matrix coefficient. – Patrick Da Silva Oct 01 '23 at 17:32
  • @RitwickJha: Using the above logic, you get $$ \frac{\partial}{\partial x_k} (Ax-b) = A(x_k e_k) $$ and so $$ \frac{\partial}{\partial x_k} (Ax-b)^{\top}(Ax-b) = (A(e_k))^{\top} (Ax-b) + (Ax-b)^{\top}(A(e_k)) $$ Since these matrix terms are numbers, they are equal to their own transpose, so you can simplify a bit: $$ \frac{\partial}{\partial x_k} (Ax-b)^{\top}(Ax-b) = 2 (Ax-b)^{\top}(A(e_k)) $$ – Patrick Da Silva Oct 01 '23 at 17:34
  • Put together all those partial derivatives into a vector: $$ (\frac{\partial}{\partial x_1}, \cdots, \frac{\partial}{\partial x_n}) (Ax-b)^{\top}(Ax-b) = 2 (Ax-b)^{\top}A(e_1, \cdots, e_n) $$ and now those vectors on the right form the identity matrix, so you can basically write $$ \nabla ((Ax-b)^{\top} (Ax-b)) = 2(Ax-b)^{\top}A. $$ If you hadn't simplified a bit but kept going, the same logic would give you $$ \nabla ((Ax-b)^{\top} (Ax-b)) = (e_1,\cdots,e_n)^{\top}A^{\top}(Ax-b) + (Ax-b)^{\top}A(e_1,\cdots,e_n). $$ – Patrick Da Silva Oct 01 '23 at 17:41
7

The derivation becomes a lot simpler if we take the derivative with respect to the entire $x$ in one go:

$$\frac{\delta}{\delta x}(Ax-b)^T(Ax-b) \ \ = \ \ 2(Ax-b)^T\frac{\delta}{\delta x}(Ax-b) \ \ = \ \ 2(Ax-b)^TA$$

This follows from the product rule:

$$\frac{\delta}{\delta x}uv \ \ = \ \ \frac{\delta u}{\delta x}v+u\frac{\delta v}{\delta x}$$

And that we can swap the order of the dot product:

$$\frac{\delta}{\delta x}u^Tu \ \ = \ \ \frac{\delta u^T}{\delta x}u+u^T\frac{\delta u}{\delta x} \ \ = \ \ (\frac{\delta u}{\delta x})^Tu+u^T\frac{\delta u}{\delta x} \ \ = \ \ 2u^T\frac{\delta u}{x}$$

I just learned matrix calculus from the Matrix Cookbook yesterday, and I love it :)

Thomas Ahle
  • 5,925
  • Nice one! Thanks for sharing your newfound knowledge :)! – dreamer Mar 12 '14 at 12:02
  • @Thomas, you love Matrix Cookbook ; Petersen and Pedersen will be happy. But have you understand it ? Firstly an expression of type $\frac{\partial f}{\partial x}$ is not a derivative (as you wrote) but a gradient. Secondly, if you look at the formula (73) of your new bible, then you see that the gradient is a vector ; unfortunately your result $2(Ax-b)^TA$ is a row. Do you know how to correct your result? –  Mar 13 '14 at 01:34
  • @loupblanc: You got me ;-) I still have much to learn about this topic. Specifically I'd really really like a hint on this problem: http://math.stackexchange.com/questions/708696/differentiate-vector-norm-by-matrix because it's driving me nuts and I think I need an element-wise multiplication. – Thomas Ahle Mar 13 '14 at 07:35
  • 1
    that's not the chain's rule, instead it is the Leibniz' rule – janmarqz Sep 06 '22 at 00:25
5

I assume you are trying to minimize $\langle A x -b, A x - b\rangle.$ This is always nonnegative, so if $A$ is nonsingular, then the minimum is $0.$ Otherwise, the $i$-th component of the gradient is

$$\langle A_i, Ax +b\rangle + \langle A x + b, A_i\rangle$$

Where $A_i$ is the $i$-th column of $A.$ What does this tell you?

Igor Rivin
  • 26,467
  • Thanks for your help. Is the expression you are minimizing the same as $||Ax-b||^2=(Ax-b)^T(Ax-b)$ (if so, I'm slightly confused by your notation)? And I'm not quite sure what this tells me. – dreamer Dec 14 '13 at 16:15
  • It's not clear that $A$ is a square matrix, much less non-singular. This is a common minimization problem when $A$ is non-square... – Thomas Andrews Dec 14 '13 at 16:42
  • @ThomasAndrews Indeed, but the approach I am describing (with no claims to any sort of originality) works for any $A.$ – Igor Rivin Dec 14 '13 at 16:50
2

Using the identities: $\frac{\partial x^{T} Bx}{\partial x} = (B+B^{T} )x $

$\frac{\partial x ^{T} a}{\partial x} = \frac{\partial x a ^{T}}{\partial x} = a$

$\frac{\partial a ^{T} x b}{\partial x} = a b ^{T}$

$\frac{\partial a ^{T} x ^{T} b}{\partial x} = ba ^{T}$

We have

$f(x) = (Ax-b)^{T}(Ax-b) $

$= x^{T} A^{T} Ax - x^{T} A^{T} b -b ^{T} Ax -b^{T} b$

Using above identities, we have

${f}'(x) = (A^{T} A+(A^{T} A)^{T})x - A^{T} b -(b^{T} A)^{T} $

$ = 2A^{T} Ax - 2A^{T} b$

  • I think there is a typo: $f(x)$ should be $f(x) = x^{T} A^{T} Ax - x^{T} A^{T} b -b ^{T} Ax +b^{T} b$ – s114 Jun 17 '20 at 14:26
0

I will include an answer using Einstein notation, because I work with high order tensors and this notation makes this kind of derivative very easy to obtain.

$$f(\mathbf{x}) = \left[A_{ij}\,x_j-b_{i}\right] \left[A_{ik}\,x_k-b_i\right] = A_{ij}\,A_{ik}\,x_j\,x_k - b_i\,A_{ij}\,x_j - b_i\,A_{ik}\,x_k + b_i\,b_i$$

$$f(\mathbf{x}) = A_{ij}\,A_{ik}\,x_j\,x_k - 2\,b_i\,A_{ij}\,x_j + b_i\,b_i$$


We have that $\frac{\partial x_i}{\partial x_j} = \delta_{ij}$, thus:

$$\frac{\partial f}{\partial x_p} = A_{ij}\,A_{ik}\,\delta_{jp}\,x_k + A_{ij}\,A_{ik}\,x_j\,\delta_{kp} - 2\,b_i\,A_{ij}\,\delta_{jp} = A_{ip}\,A_{ik}\,x_k + A_{ij}\,A_{ip}\,x_j - 2\,b_i\,A_{ip}$$

We can then transpose some terms to get:

$$\frac{\partial f}{\partial x_p} = A^T_{pi}\,A_{ik}\,x_k + A^T_{pi}\,A_{ij}\,x_j - 2\,A^T_{pi}\,b_i = 2\left[A^T_{pi}\,A_{ij}\,x_j - A^T_{pi}\,b_i\right]$$

In matrix notation we get:

$$\boxed{\frac{\partial f}{\partial \mathbf{x}} = 2\,\mathbf{A}^T\left[\mathbf{A}\,\mathbf{x}-\mathbf{b}\right]}$$


If $\mathbf{A}$ is invertible, we get:

$$\frac{\partial f}{\partial \mathbf{x}} = \mathbf{0} \Leftrightarrow \mathbf{x} = \mathbf{A}^{-1}\,\mathbf{b}$$

If $\mathbf{A}$ is rectangular and $\mathbf{H} = \mathbf{A}^T\mathbf{A}$ is invertible, considering $\mathbf{q} = \mathbf{A}^T\,\mathbf{b}$, we get:

$$\frac{\partial f}{\partial \mathbf{x}} = \mathbf{0} \Leftrightarrow \mathbf{x} = \mathbf{H}^{-1}\,\mathbf{q}$$

For singular matrices, there should be infinite solutions for the minimization problem.

Daniel Cunha
  • 1,842