Unless the closed form solution is extremely expensive to compute, it generally is the way to go when it is available. However,
For most nonlinear regression problems there is no closed form solution.
Even in linear regression (one of the few cases where a closed form solution is available), it may be impractical to use the formula. The following example shows one way in which this can happen.
For linear regression on a model of the form $y=X\beta$, where $X$ is a matrix with full column rank, the least squares solution,
$\hat{\beta} = \arg \min \| X \beta -y \|_{2}$
is given by
$\hat{\beta}=(X^{T}X)^{-1}X^{T}y$
Now, imagine that $X$ is a very large but sparse matrix. e.g. $X$ might have 100,000 columns and 1,000,000 rows, but only 0.001% of the entries in $X$ are nonzero. There are specialized data structures for storing only the nonzero entries of such sparse matrices.
Also imagine that we're unlucky, and $X^{T}X$ is a fairly dense matrix with a much higher percentage of nonzero entries. Storing a dense 100,000 by 100,000 element $X^{T}X$ matrix would then require $1 \times 10^{10}$ floating point numbers (at 8 bytes per number, this comes to 80 gigabytes.) This would be impractical to store on anything but a supercomputer. Furthermore, the inverse of this matrix (or more commonly a Cholesky factor) would also tend to have mostly nonzero entries.
However, there are iterative methods for solving the least squares problem that require no more storage than $X$, $y$, and $\hat{\beta}$ and never explicitly form the matrix product $X^{T}X$.
In this situation, using an iterative method is much more computationally efficient than using the closed form solution to the least squares problem.
This example might seem absurdly large. However, large sparse least squares problems of this size are routinely solved by iterative methods on desktop computers in seismic tomography research.
Gradient descent is actually a pretty poor way of solving a linear regression problem. The lm()
function in R internally uses a form of QR decomposition, which is considerably more efficient. However, gradient descent is a generally useful technique, and worth introducing in this simple context, so that it's clearer how to apply it in more complex problems. If you want to implement your own version as a learning exercise, it's a worthwhile thing to do, but lm()
is a better choice if all you want is a tool to do linear regression.
Best Answer
Penalizing a Machine Leaning algorithm essentially means that you do not want your algorithm to be overfitted to your data. Have a look at this picture
The first plot shows an ML model that is under-fitted to the data and thus is not able to capture the pattern of the data.
The second plot shows that what your ML model will predict (dashed line) follows the trend of your data in some way.
The third picture on the right is very fitted to the data you train your algorithm on. This is bad for many reasons, but the main reason is that your training data does not contain all the data in the world.
The model in the second plot is better than the third because is more robust to predictions on new data (usually named test data).
Now, There exists a large number of algorithms that can fit the distribution of your data and you need to pick among these many.
A good way to do that is by "penalizing" the complexity of your model (e.g. assigning a negative cost (linear or quadratic cost are the most common) to the size of your weight parameter. This will result in a more robust model, i.e. similar to the one in the center.