#296Linear regression — one gradient descent stepEasy

Linear regression — one gradient descent step

Background

Gradient descent improves the line by repeatedly stepping the parameters downhill — opposite the gradient of the SSR — scaled by a learning rate η\eta:

mmηJm,ccηJcm \leftarrow m - \eta\,\frac{\partial J}{\partial m}, \qquad c \leftarrow c - \eta\,\frac{\partial J}{\partial c}

The lesson derives the two gradients of J(m,c)=i(yi(mxi+c))2J(m,c)=\sum_i (y_i-(m x_i+c))^2 with the chain rule:

Jm=2ixi(yi(mxi+c)),Jc=2i(yi(mxi+c))\frac{\partial J}{\partial m} = -2 \sum_i x_i\,(y_i-(m x_i+c)), \qquad \frac{\partial J}{\partial c} = -2 \sum_i (y_i-(m x_i+c))

The gradient points toward higher loss, so subtracting it moves toward lower loss. The learning rate sets the step size: too large overshoots and diverges, too small crawls. This problem is one single step — the body of the training loop.

Problem statement

Implement gd_step(x, y, m, c, lr) that performs one gradient-descent update on the SSR and returns the new (m, c).

Input

  • x — array-like of inputs.
  • y — array-like of true targets, the same length.
  • mfloat, the current slope.
  • cfloat, the current intercept.
  • lrfloat, the learning rate η\eta.

Output

Returns a tuple of two Python floats: the updated (m, c).

Examples

Example 1 — one step off a zero start

Input:  x = [1, 2], y = [1, 2], m = 0, c = 0, lr = 0.01
Output: (0.1, 0.06)

Explanation: residuals are [1,2][1, 2], so J/m=2(11+22)=10\partial J/\partial m=-2(1\cdot1+2\cdot2)=-10 and J/c=2(1+2)=6\partial J/\partial c=-2(1+2)=-6; then m=00.01(10)=0.1m = 0 - 0.01(-10) = 0.1 and c=00.01(6)=0.06c = 0 - 0.01(-6) = 0.06.

Example 2 — already optimal, so it barely moves

Input:  x = [1, 2, 3], y = [2, 4, 6], m = 2, c = 0, lr = 0.1
Output: (2.0, 0.0)

Explanation: the fit is perfect, both gradients are 0, so the parameters stay put.

Constraints

  • Compute the gradients with the exact formulas above (raw sum, leading 2-2).
  • Subtract lr * gradient from each parameter (descent, not ascent).
  • Compute both gradients from the same old (m, c) before updating either.
  • Return plain floats (m_new, c_new).

Notes

  • Call this repeatedly — feeding (m, c) back in — and the line converges, exactly the animation in the lesson. A correct single step is the whole algorithm; the loop is just repetition.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example: one step off a zero start
  • Reference step from zero on y = 2x + 1
  • Sample step from zero on a shifted line