All problems
Total Least Squares Step
Problem
Compute the total least squares solution for A*x = b (with A of shape n x 2) from the smallest right singular vector v of the augmented matrix [A | b]:
x = -v[0:2] / v[2]
Return None if v[2] is 0 within 1e-12, since the problem is then not solvable by TLS.
Hint
TLS minimizes the orthogonal distance to the data and reduces to the last singular vector's null direction.
Test cases
case 1
in: [[[1,0],[0,1],[1,1]],[1,2,3],[-0.4082482904638631,-0.816496580927726,0.4082482904638631]]
expected: [1,2]
case 2
in: [[[1,0],[0,1]],[2,3],[-0.5345224838248488,-0.8017837257372732,0.2672612419124244]]
expected: [2,3]
case 3
in: [[[1,0],[0,1]],[1,1],[1,0,0]]
expected: null
case 4
in: [[[1,1],[1,2],[1,3]],[1,2,3],[0,-0.7071067811865475,0.7071067811865475]]
expected: [0,1]
Starter codePython
def tls_step(A, b, v_smallest):
# Your code here
pass4 test cases · run your code against them in the editor