KatInterview

Fit a Line by Full-Batch Gradient Descent on Squared Error

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinggradient-descentlinear-regressionnumerical

Last updated

Implement full-batch gradient descent for a one-feature linear model, using gradients from the old parameters at every step to update the slope and intercept together.

Fit a Line by Full-Batch Gradient Descent

You are given paired observations (x_i, y_i) and asked to fit a straight line prediction = slope * x + intercept by minimizing the mean squared error with plain (full-batch) gradient descent. Write the training loop yourself.

Task

Implement a function that takes:

  • xs: a list of n feature values,
  • ys: a list of n target values (same length as xs),
  • step_size: the learning rate,
  • num_steps: how many gradient descent iterations to run.

The function must return the learned parameters as a list [slope, intercept].

Procedure

  1. Start with slope = 0.0 and intercept = 0.0.

  2. The loss is the mean squared error over all samples:

    loss = (1/n) * sum((slope * x_i + intercept - y_i)^2)

  3. In each of the num_steps iterations:

    • Use all samples to compute the gradient of the loss with respect to both parameters, evaluated at the current slope and intercept.
    • Then update both parameters by subtracting step_size times their respective gradients.
  4. After the final iteration, return [slope, intercept].

Important details

  • The update must be simultaneous: both gradients are computed from the parameter values at the start of the iteration. Do not change the slope and then compute the intercept's gradient using the new slope.
  • Run exactly num_steps iterations. Do not stop early and do not add any convergence checks, clipping, or feature scaling.
  • Do not regularize either parameter.
  • Return a Python list of two floats, in the order [slope, intercept].
  • Do not guard against divergence: if the step size is too large, the values simply blow up as the arithmetic dictates.

Constraints

  • xs and ys have the same length n, with n >= 1.
  • step_size is a positive float.
  • num_steps is a positive integer.
  • Values are real numbers (floats or ints).

Examples

Example 1

Input: fit_line_gd([1, 2, 3], [2, 4, 6], 0.01, 1000)

Output: [1.97078250602932, 0.06641823135781669]

Example 2

Input: fit_line_gd([0, 1, 2, 3], [1, 3, 5, 7], 0.05, 500)

Output: [2.0000000067412476, 0.9999999856071559]

Example 3

Input: fit_line_gd([1, 2, 3, 4, 5], [5, 5, 5, 5, 5], 0.01, 1000)

Output: [0.04350115835152537, 4.8429470155081935]

Hints

Hint 1

Write the residual for each sample as slope * x_i + intercept - y_i. Both gradients can be built from these residuals.

Hint 2

The gradient of the mean squared error is (2/n) * sum(residual * x_i) for the slope and (2/n) * sum(residual) for the intercept.

Hint 3

Compute both gradients completely before changing either parameter, so that the update is simultaneous, then apply both updates.