Least-square fitting

We want to find the best xx that minimizes the error Axb2||Ax-b||^2.

The brute-force strategy is to find the solution for the equation ATAx^=ATbA^TA \hat x = A^Tb. Because ATAA^TA as a projection operator, keeps only the part of xx that is representable by the basis vectors from AA, effectively removing the error ee:

Ax^b2=Ax^(be)2+e2. ||A \hat x - b||^2 = ||A \hat x - (b-e)||^2 + ||e||^2.

In Python, least square fitting is done with numpy.linalg.lstsq, or scipy.linalg.lstsq.

Polynomial fitting (with numpy.polyfit) is a special case for least-square fitting, where AA is a Vandermonde matrix (numpy.vander).