Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does numpy least squares result diverge from using the direct formula?

Tags:

python

numpy

I want to calculate the least squares estimate for given data.

There are a few ways to do this, one is to use numpy's least squares:

import numpy
np.linalg.lstsq(X,y)[0]

Where X is a matrix and y a vector of compatible dimension (type float64). Second way is to calculate the result directly using the formula:

import numpy
numpy.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)

My problem: there are cases where the different formulas give radically different results (although there may be no difference). Sometimes the coefficients grow to be extremely large, using one formula, while the other is much more well behaved. The formulas are the same so why can the results diverge so much? Is this some type of rounding error and how do I minimize it?

like image 703
Dole Avatar asked Jul 29 '16 00:07

Dole


People also ask

What does numpy Linalg Lstsq return?

linalg. lstsq. Return the least-squares solution to a linear matrix equation.

What two functions within the numpy library could you use to solve a system of linear equations?

The article explains how to solve a system of linear equations using Python's Numpy library. You can either use linalg. inv() and linalg. dot() methods in chain to solve a system of linear equations, or you can simply use the solve() method.

What does NP Linalg solve do?

solve. Solve a linear matrix equation, or system of linear scalar equations. Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.


1 Answers

While those two formulas are mathematically equivalent, they are not numerically equivalent! There are better ways to solve a system of linear equations Ax = b than by multiplying both sides by A^(-1), like Gaussian Elimination. numpy.linalg.lstsq uses this (and more sophisticated) methods to solve the underlying linear system, plus it can handle a lot of corner cases. So use it when you can.

Matrix inversion is very numerically unstable. Don't do it unless you have to.

like image 168
bpachev Avatar answered Oct 13 '22 19:10

bpachev