# Solving for regression parameters in closed-form vs gradient descent

In Andrew Ng's machine learning course, he introduces linear regression and logistic regression, and shows how to fit the model parameters using gradient descent and Newton's method.

I know gradient descent can be useful in some applications of machine learning (e.g., backpropogation), but in the more general case is there any reason why you wouldn't solve for the parameters in closed form-- i.e., by taking the derivative of the cost function and solving via Calculus?

What is the advantage of using an iterative algorithm like gradient descent over a closed-form solution in general, when one is available?

• I don't think there is a closed form solution for the MLE of the regression parameters in most glms (e.g. logistic regression). Linear regression with normal errors is one exception. Feb 20, 2012 at 2:35
• Interesting... Does this mean different stats packages might give different answers for logistic regression depending on, e.g., initial parameter settings, number of iterations, multiple local minima, etc.-- or is there a conventional procedure that all good stats packages will follow? (Though I'm sure any differences, if they do exist, are minute in most cases)
– Jeff
Feb 20, 2012 at 4:45
• (+1) To your question and your comment, Jeff. GLMs using the canonical link (like logistic regression) benefit from the nice properties of convexity. There can be more than one algorithm to solve such problems, but the basic upshot of this is that (modulo some fairly minor details), well-implemented numerical algorithms will give consistent results between them. Feb 20, 2012 at 10:58
• I personally dislike Andrew Ng's course because it has led people into believing that Linear Regression is "machine learning". May 19, 2016 at 14:28
• Jan 15, 2017 at 23:46

Unless the closed form solution is extremely expensive to compute, it generally is the way to go when it is available. However,

1. For most nonlinear regression problems there is no closed form solution.

2. 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.

• I should mention that there are also numerical accuracy issues that can make the use of the closed form solution to the least squares problem unadvisable. However, this would require a discussion of ill-conditioning that seems likely to be beyond the current understanding of the original poster. Feb 20, 2012 at 3:28
• please don't hesitate to post an answer because you don't think i will understand it. first-- it won't hurt to provide more information, even if takes me some research in order to grasp it. second-- the stackexchange model assumes that this question and answer will benefit others in the future. in other words, don't dumb down your answer based on how much you think the OP knows, or you'll be doing others a disservice.
– Jeff
Feb 20, 2012 at 4:20
• @Brian, my feeling is your comment hits closer to the heart of the issue and is a bit at odds with the first sentence in the answer. I don't think any least-squares software (in its right mind) employs the closed-form solution. :) Feb 20, 2012 at 10:53
• Cardinal- in practice, it's best to use the QR factorization or SVD to solve small scale least squares problems. I'd argue that a solution using one of these orthogonal factorizations is also a "closed form solution" in comparison to using an iterative technique like LSQR. I didn't delve into this in my answer because it needlessly draws attention away from my main point. Feb 22, 2012 at 17:31
• Ill-conditioning? Textbook closed form solution? I love the smell of squared condition numbers in the morning. Have a big condition number? Why not square it and make it even bigger? Have a not so big condition number? Why not square it and make it big. May 15, 2016 at 17:32

UPDATE

For linear regression, it's a one step procedure, so iteration of any kind is not needed.

For logistic regression, the Newton-Raphson iterative approach uses the second partial derivatives of the objective function w.r.t. each coefficient, as well as the first partial derivatives, so it converges much faster than gradient descent, which only uses the first partial derivatives.

OP

There have been several posts on machine learning (ML) and regression. ML is not needed for solving ordinary least squares (OLS), since it involves a one-step matrix sandwiching operation for solving a system of linear equations -- i.e., $$\boldsymbol{\beta}=(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}$$ . The fact that everything is linear means that only a one-step operation is needed to solve for the coefficients. Logistic regression is based on maximizing the likelihood function $$L=\prod_i{p_i}$$, which can be solved using Newton-Raphson, or other ML gradient ascent methods, metaheuristics (hill climbing, genetic algorithms, swarm intelligence, ant colony optimization, etc).

Regarding parsimony, use of ML for OLS would be wasteful because iterative learning is inefficient for solving OLS.

Now, back to your real question on derivatives vs. ML approaches to solving gradient-based problems. Specifically, for logistic regression, Newton-Raphson's gradient descent (derivative-based) approach is commonly used. Newton-Raphson requires that you know the objective function and its partial derivatives w.r.t. each parameter (continuous in the limit and differentiable). ML is mostly used when the objective function is too complex ("narly") and you don't know the derivatives. For example, an artificial neural network (ANN) can be used to solve either a function approximation problem or supervised classification problem when the function is not known. In this case, the ANN is the function.

Don't make the mistake of using ML methods to solve a logistic regression problem, just because you can. For logistic, Newton-Raphson is extremely fast and is the appropriate technique for solving the problem. ML is commonly used when you don't know what the function is. (by the way, ANNs are from the field of computational intelligence, and not ML).

• Could you mark a difference between topics covered by computational intelligence and machine learning ? Jul 28, 2020 at 12:29
• This answer seems is a bit misleading : dont you agree that we know the derivatives (partial derivatives) of the cost function (not the func that the ANN is approximating of course...since it is obviously unkown) of an artificial neural network (ANN), since otherwise the backprop algo simply could not be computed! From reading this answer it feels like the author is saying the derivatives of cost func are not known... this is very problematic if wrong answers are allowed?! Nov 14, 2020 at 17:56
• Also the fact that the user does not anymore have a profile is dubious...? For me, spreading wring information, and even worse, wrong info packed in true info is very bad. Of course it might not be on purpose.But I would be happy for someone else to give his opinions. Nov 14, 2020 at 18:01
• Or maybe i m missing/forgetting something but in any case I really think this point should be explicitely detailed Nov 14, 2020 at 18:38