Gradient descent step-size stability on a quadratic: eigenvalues, the 2/L bound and convergence regimes

Quick Overview

Implement NumPy functions that compute the Hessian spectrum of a quadratic objective, the gradient descent stability bound, and a table labelling each step size as converging, oscillating or diverging. It tests eigenvalue reasoning about the gradient descent error recursion and careful handling of shapes, dtypes and invalid inputs.

Gradient descent step-size stability on a quadratic: eigenvalues, the 2/L bound and convergence regimes

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Consider the quadratic objective $$f(x) = \frac{1}{2} x^\top A x - b^\top x$$ with Hessian $H = (A + A^\top)/2$ and gradient $Hx - b$. Implement the six functions below. Together they compute the spectrum of $H$, the stability bound `2 / L` (where $L$ is the largest eigenvalue of $H$), run gradient descent, and summarise the regime of each step size. ```python def hessian_eigenvalues(A): ... def max_stable_step(A): ... def spectral_radius(A, lr): ... def run_gradient_descent(A, b, x0, lr, n_steps): ... def classify_step(A, lr, tol=1e-9): ... def step_size_experiment(A, b, x0, lrs, n_steps): ... ``` Requirements: - `hessian_eigenvalues` returns the eigenvalues of $H$ in ascending order. A non-square `A` raises `ValueError`. - `max_stable_step` returns the float `2 / L`. If $L \le 0$, raise `ValueError`. - `spectral_radius(A, lr)` returns $\rho = \max_i |1 - \text{lr} \cdot \lambda_i|$ as a float. - `run_gradient_descent` iterates `x <- x - lr * (H @ x - b)` and returns an array of shape `(n_steps + 1, d)` whose row 0 is `x0`. A negative `n_steps` raises `ValueError`. - `classify_step` returns `"converges"` when $\rho < 1 - \text{tol}$, `"diverges"` when $\rho > 1 + \text{tol}$, and `"oscillates"` otherwise. A Hessian that is not positive definite, or `lr <= 0`, raises `ValueError`. - `step_size_experiment` returns a pandas DataFrame with one row per `lr` and the columns `lr, ratio_to_bound, spectral_radius, final_distance, regime`. `ratio_to_bound` is `lr` divided by `2 / L`. The first four columns are `float64` and `regime` is `object`. `final_distance` is the L2 norm of the last iterate minus $x^* = H^{-1} b$. Example: ```python A = np.diag([1.0, 4.0]); b = np.array([1.0, 2.0]) step_size_experiment(A, b, np.zeros(2), [0.25, 0.5, 0.55], n_steps=3) # lr ratio_to_bound spectral_radius final_distance regime # 0 0.25 0.5 0.75 0.421875 converges # 1 0.50 1.0 1.00 0.515388 oscillates # 2 0.55 1.1 1.20 0.868792 diverges ``` ```hint Track the error Rewrite the update in terms of the error $e_k = x_k - x^*$. The recursion you get is linear, and along each eigenvector of $H$ it multiplies the error by a single scalar. ``` ```hint Eigenvalue routine Pick an eigenvalue routine that matches the matrix you pass it. Check what a symmetric solver does with a non-symmetric input, and what a general solver returns for a matrix whose quadratic form is real. ``` ### Constraints & Assumptions - `A` is a `d x d` array-like of floats and need not be symmetric. `b` and `x0` are length-`d` vectors. - `lrs` is a sequence of step sizes and `n_steps` is a non-negative integer. - NumPy and pandas are available. ### What a Strong Answer Covers - Why the symmetric part of `A` is the Hessian, and how that choice drives the eigenvalue computation. - The error recursion of gradient descent and how $\rho$ decides between converging, oscillating and diverging, including why the boundary sits at `2 / L`. - Each function's contract: output shapes and dtypes, row 0 of the trajectory, and every `ValueError` case. - A hand check of at least one example row, including the boundary row. - The cost of `step_size_experiment` and how to avoid repeating the eigendecomposition per step size. ### Follow-up Questions - How does the condition number $\kappa = L/\mu$ limit the best achievable convergence rate? - Why does a non-symmetric `A` need symmetrising before taking eigenvalues? - How would you estimate $L$ without a full eigendecomposition for high-dimensional problems?

Overview: Implement NumPy functions that compute the Hessian spectrum of a quadratic objective, the gradient descent stability bound, and a table labelling each step size as converging, oscillating or diverging. It tests eigenvalue reasoning about the gradient descent error recursion and careful handling of shapes, dtypes and invalid inputs.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Consider the quadratic objective

f(x)=12x⊤Ax−b⊤xf(x) = \frac{1}{2} x^\top A x - b^\top x

with Hessian H=(A+A⊤)/2H = (A + A^\top)/2 and gradient Hx−bHx - b. Implement the six functions below. Together they compute the spectrum of HH, the stability bound 2 / L (where LL is the largest eigenvalue of HH), run gradient descent, and summarise the regime of each step size.

def hessian_eigenvalues(A): ...
def max_stable_step(A): ...
def spectral_radius(A, lr): ...
def run_gradient_descent(A, b, x0, lr, n_steps): ...
def classify_step(A, lr, tol=1e-9): ...
def step_size_experiment(A, b, x0, lrs, n_steps): ...

Requirements:

  • hessian_eigenvalues returns the eigenvalues of HH in ascending order. A non-square A raises ValueError .
  • max_stable_step returns the float 2 / L . If L≤0L \le 0 , raise ValueError .
  • spectral_radius(A, lr) returns ρ=max⁡i∣1−lr⋅λi∣\rho = \max_i |1 - \text{lr} \cdot \lambda_i| as a float.
  • run_gradient_descent iterates x <- x - lr * (H @ x - b) and returns an array of shape (n_steps + 1, d) whose row 0 is x0 . A negative n_steps raises ValueError .
  • classify_step returns "converges" when ρ<1−tol\rho < 1 - \text{tol} , "diverges" when ρ>1+tol\rho > 1 + \text{tol} , and "oscillates" otherwise. A Hessian that is not positive definite, or lr <= 0 , raises ValueError .
  • step_size_experiment returns a pandas DataFrame with one row per lr and the columns lr, ratio_to_bound, spectral_radius, final_distance, regime . ratio_to_bound is lr divided by 2 / L . The first four columns are float64 and regime is object . final_distance is the L2 norm of the last iterate minus x∗=H−1bx^* = H^{-1} b .

Example:

A = np.diag([1.0, 4.0]); b = np.array([1.0, 2.0])
step_size_experiment(A, b, np.zeros(2), [0.25, 0.5, 0.55], n_steps=3)
#      lr  ratio_to_bound  spectral_radius  final_distance      regime
# 0  0.25             0.5             0.75        0.421875   converges
# 1  0.50             1.0             1.00        0.515388  oscillates
# 2  0.55             1.1             1.20        0.868792    diverges

Constraints & Assumptions

  • A is a d x d array-like of floats and need not be symmetric. b and x0 are length- d vectors.
  • lrs is a sequence of step sizes and n_steps is a non-negative integer.
  • NumPy and pandas are available.

What a Strong Answer Covers Guidance

  • Why the symmetric part of A is the Hessian, and how that choice drives the eigenvalue computation.
  • The error recursion of gradient descent and how ρ\rho decides between converging, oscillating and diverging, including why the boundary sits at 2 / L .
  • Each function's contract: output shapes and dtypes, row 0 of the trajectory, and every ValueError case.
  • A hand check of at least one example row, including the boundary row.
  • The cost of step_size_experiment and how to avoid repeating the eigendecomposition per step size.

Follow-up Questions Guidance

  • How does the condition number κ=L/μ\kappa = L/\mu limit the best achievable convergence rate?
  • Why does a non-symmetric A need symmetrising before taking eigenvalues?
  • How would you estimate LL without a full eigendecomposition for high-dimensional problems?