Summary of linear regression

In [1]:
versioninfo()
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.6.0)
  CPU: Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4

Methods for solving linear regression $\widehat \beta = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}$:

Method Flops Remarks Software Stability
Sweep $np^2 + p^3$ $(X^TX)^{-1}$ available SAS less stable
Cholesky $np^2 + p^3/3$ less stable
QR by Householder $2np^2 - (2/3)p^3$ R stable
QR by MGS $2np^2$ $Q_1$ available stable
QR by SVD $4n^2p + 8np^2 + 9p^3$ $X = UDV^T$ most stable

Remarks:

  1. When $n \gg p$, sweep and Cholesky are twice faster than QR and need less space.
  2. Sweep and Cholesky are based on the Gram matrix $\mathbf{X}^T \mathbf{X}$, which can be dynamically updated with incoming data. They can handle huge $n$, moderate $p$ data sets that cannot fit into memory.
  3. QR methods are more stable and produce numerically more accurate solution.
  4. Although sweep is slower than Cholesky, it yields standard errors and so on.
  5. MGS appears slower than Householder, but it yields $\mathbf{Q}_1$.

There is simply no such thing as a universal 'gold standard' when it comes to algorithms.

In [2]:
using SweepOperator, BenchmarkTools, LinearAlgebra

linreg_cholesky(y::Vector, X::Matrix) = cholesky!(X'X) \ (X'y)

linreg_qr(y::Vector, X::Matrix) = X \ y

function linreg_sweep(y::Vector, X::Matrix)
    p = size(X, 2)
    xy = [X y]
    tableau = xy'xy
    sweep!(tableau, 1:p)
    return tableau[1:p, end]
end

function linreg_svd(y::Vector, X::Matrix)
    xsvd = svd(X)
    return xsvd.V * ((xsvd.U'y) ./ xsvd.S)
end
Out[2]:
linreg_svd (generic function with 1 method)
In [3]:
using Random

Random.seed!(123) # seed

n, p = 10, 3
X = randn(n, p)
y = randn(n)

# check these methods give same answer
@show linreg_cholesky(y, X)
@show linreg_qr(y, X)
@show linreg_sweep(y, X)
@show linreg_svd(y, X);
linreg_cholesky(y, X) = [0.18663443776194222, 0.3425458137678085, 0.29576444004323654]
linreg_qr(y, X) = [0.1866344377619422, 0.3425458137678086, 0.29576444004323627]
linreg_sweep(y, X) = [0.18663443776194222, 0.3425458137678085, 0.2957644400432365]
linreg_svd(y, X) = [0.18663443776194236, 0.3425458137678091, 0.2957644400432366]
In [4]:
n, p = 1000, 300
X = randn(n, p)
y = randn(n)

@benchmark linreg_cholesky(y, X)
Out[4]:
BenchmarkTools.Trial: 
  memory estimate:  708.25 KiB
  allocs estimate:  6
  --------------
  minimum time:     1.745 ms (0.00% GC)
  median time:      2.110 ms (0.00% GC)
  mean time:        2.362 ms (1.85% GC)
  maximum time:     13.229 ms (0.00% GC)
  --------------
  samples:          2114
  evals/sample:     1
In [5]:
@benchmark linreg_sweep(y, X)
Out[5]:
BenchmarkTools.Trial: 
  memory estimate:  2.99 MiB
  allocs estimate:  6
  --------------
  minimum time:     8.724 ms (0.00% GC)
  median time:      11.045 ms (0.00% GC)
  mean time:        11.182 ms (1.63% GC)
  maximum time:     16.231 ms (16.84% GC)
  --------------
  samples:          447
  evals/sample:     1
In [6]:
@benchmark linreg_qr(y, X)
Out[6]:
BenchmarkTools.Trial: 
  memory estimate:  4.01 MiB
  allocs estimate:  1836
  --------------
  minimum time:     8.699 ms (0.00% GC)
  median time:      10.068 ms (0.00% GC)
  mean time:        10.455 ms (3.58% GC)
  maximum time:     16.324 ms (25.08% GC)
  --------------
  samples:          479
  evals/sample:     1
In [7]:
@benchmark linreg_svd(y, X)
Out[7]:
BenchmarkTools.Trial: 
  memory estimate:  8.06 MiB
  allocs estimate:  14
  --------------
  minimum time:     25.919 ms (0.00% GC)
  median time:      29.107 ms (0.00% GC)
  mean time:        29.735 ms (1.72% GC)
  maximum time:     36.948 ms (9.59% GC)
  --------------
  samples:          169
  evals/sample:     1