4
$\begingroup$

For the linear model $y = X\beta$ for design matrix $X$, it's well known that the optimal solution is $\hat{\beta} = (X'X)^{-1}X'y$.

Some statistical libraries (such as Python's statsmodels) estimate parameters by first computing the pseudoinverse of the design matrix: $X^{+} = \text{pseudoinverse}(X) = (X'X)^{-1}X'$ and then computing the estimate as $\hat{\beta}=X^{+}y$.

I'm operating in a restricted memory environment with a "tall and skinny" design matrix with shapes up to (1e8,3). Computing the pseudoinverse requires allocating an additional matrix X^{+} of shape (3,1e8) before computing the final parameters and it is at this step that I often run out of memory.

We can contrast that with computing the parameters in 3 steps:

  1. $A = (X'X)^{-1}$. This allocates a 3x3 matrix then inverts it.
  2. $B = X'y$. This allocates a 3x1 vector.
  3. $\hat{\beta} = AB$.

My question is: are there any downsides (such as numerical issues) to computing the OLS estimates using the methodology above? I can guarantee the absence of perfect multicollinearity (from the structure of the design matrix) thus guaranteeing that the inverse of the gram matrix ($A$) exists.

$\endgroup$
3
  • 1
    $\begingroup$ Good question, the approach you have outlined is called "solving the normal equations"; typically it is avoided because it doubles the condition number of the problem (see e.g. cs.ubc.ca/~rbridson/courses/542g-fall-2008/notes-oct1.pdf). But computing a pseudoinverse and then multiplying the pseudoinverse against the response is also a bad idea. The best option is to use functions which are specifically designed for solving least squares problems. In R, solve(X, y) is an option. Since you seem to be using python, you would want to use np.linalg.lstsq(X, y)[0]. $\endgroup$ Commented May 31 at 15:55
  • 1
    $\begingroup$ That being said, if your problem is really $N=100,000,000; P = 3$, then you can totally get away with solving the normal equations. $\endgroup$ Commented May 31 at 15:58
  • 1
    $\begingroup$ Lots more discussion stats.stackexchange.com/q/1829 and stats.stackexchange.com/questions/160179 $\endgroup$
    – Sycorax
    Commented May 31 at 16:07

1 Answer 1

0
$\begingroup$

The libraries are making an assumption about what will make the most sense most of the time. The decision of the library is based on a memory-speed-numerical stability tradeoff that will make the most sense based on the shape and contents of $X$ the library expects to see. You have a very atypically shaped matrix $X$, so the library's assumption doesn't make sense for you. What you're proposing is perfectly valid and makes a lot of sense for your $X$ (especially if the columns aren't close to colinear).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.