Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-162

Quadratic Fit via Sums

Problem

Fit y = a + b*x + c*x^2 by least squares using the 3x3 normal equations (built from power sums and solved by Gaussian elimination).

Return [a, b, c]; fewer than three points or a singular system returns [0.0, 0.0, 0.0].

Hint

The normal equations are [[n, S1, S2], [S1, S2, S3], [S2, S3, S4]] times [a, b, c] equals [Sy, Sxy, Sx2y].

Test cases

case 1
in: [[0,1,2],[1,6,17]]
expected: [1.0000000000000022,1.9999999999999671,3.000000000000017]
case 2
in: [[-1,0,1,2],[2,1,6,17]]
expected: [1,2.000000000000001,2.9999999999999996]
case 3
in: [[0,1,2],[2,5,10]]
expected: [2,1.9999999999999991,1.0000000000000004]
case 4
in: [[0],[1]]
expected: [0,0,0]
Starter codePython
def quadratic_fit(X, y):
    # Returns [a, b, c]
    # Your code here
    pass

4 test cases · run your code against them in the editor