This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/stats/linmod

Least squares for linear models and GLMs

The least-squares solver slot of vp.stats. Every solver takes the design X (n by p) and y and returns the coefficients, the rank, the columns used, and (X^T X)^-1 over those columns (NaN rows and columns for the others):

  • lstsq_qr: Householder QR with column pivoting (the column of largest remaining norm first, as LAPACK's dgeqp3) on X with its columns scaled to unit norm; a column whose pivot is below tol times the first is dropped. With refine 1, two steps of Bjorck's iterative refinement of the augmented system [I X; X^T 0] [r; b] = [y; 0] follow: its residuals y - r - X b and -X^T r summed exactly (each product split by fma, the terms added by fsum), the corrections of b and r solved with the same factors (A. Bjorck, BIT 7 (1967) 257-278). Refining r as well as b is what keeps the digits of a fit with large residuals, whose coefficients are sensitive to cond(X)^2.
  • lstsq_chol: the normal equations X^T X b = X^T y by Cholesky, on the scaled columns (no rank detection).
  • lstsq_svd: the pseudo-inverse from the SVD (Viv's one-sided Jacobi), the singular values below tol times the largest dropped (statsmodels' pinv).

glm_irls (lib/stats/glm.viv) calls lstsq_qr for each weighted step.

Used with from stats.linmod import ....

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 29–350