Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: add missing details to linalg.lstsq docstring #18062

Merged
merged 2 commits into from
Dec 31, 2020

Conversation

nilokr
Copy link
Contributor

@nilokr nilokr commented Dec 23, 2020

It turns out that linalg.lstsq also minimizes the 2-norm of x when a is rank-deficient. I found that by searching the documentation for the LAPACK library, which is the current implementation of lstsq.

This PR rewrites the docstring for linalg.lstsq such that it contains details for all cases.

``a @ x = b``, where `a` is an `m`-by-`n` matrix and `b` is a `m` element
vector. When the system is overdetermined (``m >= n`` and ``rank(a) = n``),
a solution `x` that minimizes the Euclidean 2-norm ``||b-ax||`` is found.
Else, when the system is underdetermined (``m < n`` and ``rank(A) = m``)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "If there are multiple minimizing solutions, the one with the smallest norm is returned". Note that the norm of the solution depends on column scaling (units) and choice of variables, so the smallest norm solution is seldom meaningful when the solution is not unique.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds better - I've updated my commit to reflect the suggestion, thanks!

The reason I stumbled on this smallest norm matter was that I was specifically looking for that solution in a rank-deficient problem. I was intrigued I got exactly what I wanted for free, so I did some digging and found why :)

It turns out that lstsq also minimizes the 2-norm of x when a is
rank-deficient. I found that by searching the documentation for the
LAPACK library, which is the current implementation of lstsq (as of Dec
2020). Ref: https://www.netlib.org/lapack/lug/node27.html
``a @ x = b``. The equation may be under-, well-, or over-determined
(i.e., the number of linearly independent rows of `a` can be less than,
equal to, or greater than its number of linearly independent columns).
If `a` is square and of full rank, then `x` (but for round-off error)
is the "exact" solution of the equation. Else, `x` minimizes the
Euclidean 2-norm :math:`|| b - a x ||`.
Euclidean 2-norm :math:`||b-ax||`. If there are multiple minimizing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep spaces around - for readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed that.

@charris charris merged commit 31647f1 into numpy:master Dec 31, 2020
@charris
Copy link
Member

charris commented Dec 31, 2020

Thanks @krnilo .

@matthew-brett
Copy link
Contributor

Worth pointing out that this is the behavior of the pseudoinverse?

@nilokr nilokr deleted the add-lstsq-details branch January 4, 2021 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants