<s>[INST] Derivative of quadratic expression involving outer product of estimate I was trying to calculate the derivative with respect to $x$ of the following expression:
$\frac{1}{2}\left|\left|\left(\frac{Axx^TA^T}{x^TA^TAx}-I\right)b\right|\right|^2_2$
Here, $x$, $b$ are vectors and $A$ is a matrix and $I$ the identity matrix.
However, I have difficulties because I can't wrap my head around the emerging 3D matrix as a result from the $xx^T$. It all becomes very messy, because I get confused with the notation. Could anyone help me on the derivation of this derivative? Thank you very much for your efforts.
 [/INST] We can rewrite the function as
\begin{align*}
f(x) ={}& \frac{1}{2}b^\top\left( \frac{Axx^\top A^\top}{x^\top A^\top Ax} - I \right)^\top\left( \frac{Axx^\top A^\top}{x^\top A^\top Ax} - I \right) b \\
={}& \frac{1}{2}b^\top \left( \frac{Axx^\top A^\top}{x^\top A^\top Ax} - I \right)^2 b \\
={}& \frac{1}{2}b^\top \left( \frac{Axx^\top A^\top Axx^\top A^\top}{(x^\top A^\top Ax)^2} - 2\frac{Axx^\top A^\top}{x^\top A^\top Ax} + I \right) b \\
={}& \frac{1}{2}b^\top \left( \frac{Axx^\top A^\top}{x^\top A^\top Ax} - 2\frac{Axx^\top A^\top}{x^\top A^\top Ax} + I \right) b \\
={}& \frac{1}{2}b^\top \left( I - \frac{Axx^\top A^\top}{x^\top A^\top Ax} \right) b \\
={}& \frac{1}{2}\|b\|_2^2 - \frac{1}{2}\frac{(b^\top A x)^2}{x^\top A^\top Ax}.
\end{align*}
Therefore
\begin{align*}
\nabla f(x) ={}& -\frac{1}{2}\frac{2b^\top Ax}{x^\top A^\top Ax}\nabla(b^\top Ax) + \frac{1}{2} \frac{(b^\top Ax)^2}{(x^\top A^\top Ax)^2} \nabla(x^\top A^\top Ax) \\
={}& -\frac{1}{2}\frac{2b^\top Ax}{x^\top A^\top Ax} A^\top b + \frac{1}{2}\left(\frac{b^\top Ax}{x^\top A^\top Ax}\right)^2 2A^\top Ax \\
={}& \frac{b^\top Ax}{x^\top A^\top Ax} \left( \frac{b^\top Ax}{x^\top A^\top Ax} A^\top Ax - A^\top b \right).
\end{align*}
</s>