Conditioning report: verify that cond(XᵀX) = cond(X)² for a regression design matrix

Quick Overview

Build an intercept-augmented design matrix in NumPy and report its condition number, the condition number of its Gram matrix, and their ratio. It tests singular value reasoning and what conditioning implies for solving least squares through the normal equations compared with QR or SVD.

Conditioning report: verify that cond(XᵀX) = cond(X)² for a regression design matrix

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement the function below. `X` is float64 of shape `(n, p)` without a ones column. Prepend a ones column to form the design matrix `Xd` and return `(cond_X, cond_gram, ratio)`. ```python def conditioning_report(X: np.ndarray) -> tuple[float, float, float]: ... ``` The three values are defined as follows: - `cond_X` is `np.linalg.cond(Xd)` with the default `p=None`, which is the largest singular value divided by the smallest. - `cond_gram` is `np.linalg.cond(Xd.T @ Xd)`. - `ratio` is `cond_gram / cond_X**2`. - All three are Python floats (not NumPy scalars). Example: ```python X = np.array([[1.0], [2.0], [3.0]]) # shape (3, 1) cond_X, cond_gram, ratio = conditioning_report(X) # ratio ~= 1.0 since cond(Xd.T @ Xd) == cond(Xd)**2 ``` ```hint Singular values Write `Xd` as its SVD and substitute it into `Xd.T @ Xd`. Watch what happens to each singular value. ``` ```hint Return types `np.linalg.cond` returns a NumPy scalar. Check the exact type of each value you return. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n, p)` and has no ones column. The intercept column is added inside the function. - `Xd` has full column rank, so all condition numbers are finite. ### What a Strong Answer Covers - Building `Xd` with the ones column and computing both condition numbers with the 2-norm definition. - Returning three Python floats in the required order. - Why `cond(Xd.T @ Xd)` equals `cond(Xd)**2`, argued through the singular values. - What the squared condition number means for the accuracy of a normal-equations solve. - When the computed ratio stops being close to 1.0 in float64. ### Follow-up Questions - Given that squaring, why does a QR or SVD solve keep more accuracy? - How would centering or scaling the columns affect these condition numbers? - If `cond(Xd)` is around ${10^9}$, what would you expect the computed `ratio` to be, and why?

Overview: Build an intercept-augmented design matrix in NumPy and report its condition number, the condition number of its Gram matrix, and their ratio. It tests singular value reasoning and what conditioning implies for solving least squares through the normal equations compared with QR or SVD.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement the function below. X is float64 of shape (n, p) without a ones column. Prepend a ones column to form the design matrix Xd and return (cond_X, cond_gram, ratio).

def conditioning_report(X: np.ndarray) -> tuple[float, float, float]:
    ...

The three values are defined as follows:

  • cond_X is np.linalg.cond(Xd) with the default p=None , which is the largest singular value divided by the smallest.
  • cond_gram is np.linalg.cond(Xd.T @ Xd) .
  • ratio is cond_gram / cond_X**2 .
  • All three are Python floats (not NumPy scalars).

Example:

X = np.array([[1.0], [2.0], [3.0]])   # shape (3, 1)
cond_X, cond_gram, ratio = conditioning_report(X)
# ratio ~= 1.0 since cond(Xd.T @ Xd) == cond(Xd)**2

Constraints & Assumptions

  • X is float64 with shape (n, p) and has no ones column. The intercept column is added inside the function.
  • Xd has full column rank, so all condition numbers are finite.

What a Strong Answer Covers Guidance

  • Building Xd with the ones column and computing both condition numbers with the 2-norm definition.
  • Returning three Python floats in the required order.
  • Why cond(Xd.T @ Xd) equals cond(Xd)**2 , argued through the singular values.
  • What the squared condition number means for the accuracy of a normal-equations solve.
  • When the computed ratio stops being close to 1.0 in float64.

Follow-up Questions Guidance

  • Given that squaring, why does a QR or SVD solve keep more accuracy?
  • How would centering or scaling the columns affect these condition numbers?
  • If cond(Xd) is around 109{10^9} , what would you expect the computed ratio to be, and why?