TITLE: How does the SVD solve the least squares problem?
QUESTION [22 upvotes]: How do I prove that the least-squares solution for $$\text{minimize} \quad \|Ax-b\|_2$$ is $A^{+} b$, where $A^{+}$ is the pseudoinverse of $A$?

REPLY [2 votes]: The first thing to realise is that the possible values of $Ax$ cover all and only the range of $A$: $\{Ax: x\in V\}=\mathrm{Range}(A)$.
This means that the problem $Ax=b$ is solvable if and only if $b\in\mathrm{Range}(A)$.
Now consider the more general case in which $b\in \mathrm{Range}(A)$ is not necessarily true, and thus the problem cannot be solved exactly. Because $A$ allows us to generate everything in $\mathrm{Range}(A)$ by an appropriate choice of $x$, it is then only natural that the $x$ that gives the best solution (in $\|\cdot\|_2$ norm) is the one such that $Ax$ equals the projection of $b$ onto $\mathrm{Range}(A)$. In other words, denoting with $\mathbb P_R$ the projector onto $\mathrm{Range}(A)$, we want $x$ such that
$$Ax=\mathbb P_R b.\tag A$$
With this choice of $x$, the distance between $Ax$ and $b$ is then
$$\|b-\mathbb P_R b\|_2=\|(I-\mathbb P_R)b\|_2.$$
Now, why does the SVD enters the discussion? Well, mostly because it provides an easy way to find the $x$ satisfying (A).
To see this, write the SVD in dyadic notation as
$$A=\sum_k s_k u_k v_k^*,\tag B$$
where $s_k>0$ are the singular values, and $\{u_k\}$ and $\{v_k\}$ are orthonormal bases for $\mathrm{Range}(A)$ and $A^{-1}(\mathrm{Range}(A)\setminus \{0\})=\mathrm{Ker}(A)_\perp$, respectively.
The expression (B) is equivalent to the maybe more common way to see the SVD, $A=UDV^\dagger$, with $v_k$ being the columns of $V$ and $u_k$ the columns of $U$.
The pseudo-inverse $A^+$ has a nice expression in terms of its SVD:
$$A^+=\sum_k s_k^{-1} v_k u_k^*. \tag C$$
With these expressions, you might notice how
$$A(A^+ y)=\sum_k u_k u_k^* y=\sum_k u_k\langle u_k,y\rangle=\mathbb P_R y.$$
Now, remember we want to get as close to $b$ as possible, and therefore are looking for some $y$ such that
$$A(A^+ y)=\mathbb P_R y=\mathbb P_R b.$$
This tells us that we want $y=b$, and thus $x=A^+ b$.