All problems
Normal Equations 2x2
Problem
Solve the linear least squares problem for a design matrix A (n x 2) and response b (length n) via the normal equations:
(A^T A) x = A^T b
Return the coefficients [x0, x1], or None if A^T A is singular.
Hint
Build the 2x2 Gram matrix A^T A and the right-hand side A^T b, then solve.
Test cases
case 1
in: [[[1,1],[1,2],[1,3]],[1,2,3]]
expected: [0,1]
case 2
in: [[[1,1],[1,2],[1,3]],[1,3,4]]
expected: [-0.3333333333333333,1.5]
case 3
in: [[[1,0],[1,1]],[0,1]]
expected: [0,1]
case 4
in: [[[1,1],[1,1]],[1,2]]
expected: null
Starter codePython
def normal_equations(A, b):
# Your code here
pass4 test cases · run your code against them in the editor