In terms of numerical analysis, you never want to write code that
- Explicitly inverts the matrix or
- Generates a matrix of normal equations (
A^TA ) for regression
Both of them work more and are less accurate (and probably less stable) than alternatives that solve the same problem directly.
Whenever you see any math showing the inverse of a matrix, it should be considered to mean "solve a system of linear equations" or multiply a matrix and use factorization to solve the system. Both BLAS and Lapack have routines for this.
Similarly, for regression, call the library function that calculates the regression, or read how to do it yourself. The normal equation method is not the right way for a textbook.
source share