Understanding the Least-Squares Method From the Perspectives of Calculus and Linear Algebra
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
I previously used the least-squares method when trying to implement some computer graphics papers, but at that time I understood it from the perspective of calculus. This year, after taking a linear algebra course, I found that it can also be explained using concepts from linear algebra and that this explanation is more intuitive (it provides a geometric understanding). Therefore, I plan to use this article to record two ways of understanding the least-squares method.
Before beginning, I first want to complain about the Chinese name of this method, “minimum two multiplication.” It just feels very strange: why is minimizing the square of the error called “two multiplication”? Well… after looking it up, I found that it was translated from Japanese, so I can only say that the quality of this translation is rather impressive… Although I really do not want to use this confusing name, there is nothing I can do because everyone uses it.
Definition of the Problem
Before learning the least-squares method, we first need to understand what problem this algorithm is trying to solve. The most common use of the least-squares method is fitting data with a function and then using that function to predict the trend of the data. To fit data, we need to use a mathematical method to define what constitutes a good fit, and then try to make a function fit the data better.
Here, suppose there are data points . There is also a function used to fit these data points. We define the error of an individual point as:
Note that the meaning of the function here is using to predict . You can understand these two values as scalars, but the least-squares method can actually also be used to fit vectors, because the formulas are exactly the same in both cases. For simplicity, we assume here that they are scalars.
Then the sum of squared errors is:
We want to adjust the parameters of this function to minimize this value.
Specifically, the function can be expressed in the following form:
Here, are the parameters that we need to adjust, while are some linearly independent functions.
Calculus Perspective
The content of this section mainly comes from this video. Its content is quite complete, but some parts move quickly and contain writing mistakes. I also watched it several times before understanding it.
We can express the error in the following form and then minimize this error from the perspective of calculus.
Writing it in this form expresses our objective more clearly: minimizing the error by adjusting the parameters .
Note that, when summing the squared error of each data point, an additional weight appears here. This weight makes it more convenient to adjust the importance of each data point.
From calculus, we know that the derivative of a function must be 0 when the function reaches an extremum. We can use this property to adjust the parameters from the perspective of calculus so that the error is minimized. However, this extremum may be either a minimum or a maximum. When calculating the error , only one minimum can exist (you can imagine the situation in which all parameters are set to ).
Because is a multivariable function, we need to use partial derivatives to find the minimum with the method above:
In the partial derivative above, everything other than can be treated as a constant. This is because the definition of a partial derivative is the effect on the error caused by changing the parameter .
When , we can say that, when only the parameter is adjusted, the error has already reached its minimum. However, because every parameter can be adjusted, we want for every .
In this way, we obtain a system of linear equations. When seeing a system of linear equations, the first thought is certainly to represent it using linear algebra, which can greatly increase the solution speed.
After some transformations, we obtain the following formula:
Because we want to move toward a linear-algebra representation, we can express the following summation as a dot product:
Here, , , and .
Similarly, the other part of the expression can also be represented as a dot product:
Then, if:
the partial derivative of the error is 0; that is, .
Of course, our final goal is to write this expression as a matrix-multiplication equation to increase the solution speed. Looking carefully at the right-hand side of the equation, we can actually see that this summation is essentially also a dot product.
To make this easier to understand, denote the part marked in red as . It is an -dimensional vector, so this summation is , where .
Similarly, we can denote as (a scalar).
Thus, solving the following system of equations solves the least-squares problem:
This form is very familiar and can be represented directly in matrix form.
Written more simply, this is .
Linear Algebra Perspective
To be completed.



![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)