Compute the hat matrix trace for linear regression and explain why it equals k+1
Quick Overview
Implement a NumPy function that builds the hat matrix of an intercept-augmented regression design and returns its trace. It tests projection matrices, the cyclic property of the trace, leverages, and the cost of materializing an n by n matrix.
Compute the hat matrix trace for linear regression and explain why it equals k+1
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a NumPy function that builds the hat matrix of an intercept-augmented regression design and returns its trace. For a full-rank design this equals `k+1` (the sum of the leverages).
```python
def hat_trace(X: np.ndarray) -> float:
...
```
`X` has shape `(n, k)` with no intercept column. The function should:
- Prepend a ones column to `X` to form `Xd` of shape `(n, k+1)`.
- Compute $H = X_d (X_d^\top X_d)^{-1} X_d^\top$ once (with `np.linalg.inv` or `np.linalg.solve`) and return `trace(H)`.
- Return a Python `float`, not an `int`: `k+1` up to floating-point error (for example `4.0000000000000004`).
Example:
```python
X = np.array([[1.0, 2.0],
[2.0, 1.0],
[3.0, 0.0],
[4.0, 5.0]]) # shape (4, 2), k = 2
hat_trace(X) # -> ~3.0 (k + 1 = 3)
```
Be ready to explain why the trace comes out to `k+1`.
```hint Why the trace is k+1
The trace of a product does not change when you move a factor from the front to the back. Try moving `Xd` around in the formula for `H`.
```
```hint Return type
`np.trace` returns a NumPy scalar. Convert it to a Python `float` before returning, and leave the rounding error in place.
```
### Constraints & Assumptions
- `X` has shape `(n, k)` and does not include an intercept column.
- The design `Xd` is full rank, so `XdᵀXd` is invertible.
- The result is returned as an unrounded Python `float`.
### What a Strong Answer Covers
- Building `Xd` with the intercept column and forming `H` once with a solve or an inverse.
- Returning `trace(H)` as an unrounded Python `float`.
- A short proof that the trace equals `k+1` for a full-rank design.
- How the trace relates to the leverages and to the number of fitted parameters.
- The cost of materializing `H`, and a cheaper way to get its trace.
### Follow-up Questions
- Why can you compute `trace(H)` without ever materializing `H`?
- How would you make this numerically stable for ill-conditioned `X`?
- In ridge regression the hat matrix becomes $X_d (X_d^\top X_d + \lambda I)^{-1} X_d^\top$. What does its trace measure, and how does it change as $\lambda$ grows?
Overview: Implement a NumPy function that builds the hat matrix of an intercept-augmented regression design and returns its trace. It tests projection matrices, the cyclic property of the trace, leverages, and the cost of materializing an n by n matrix.
Compute the hat matrix trace for linear regression and explain why it equals k+1
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement a NumPy function that builds the hat matrix of an intercept-augmented regression design and returns its trace. For a full-rank design this equals k+1 (the sum of the leverages).
def hat_trace(X: np.ndarray) -> float:
...
X has shape (n, k) with no intercept column. The function should:
Prepend a ones column to
X
to form
Xd
of shape
(n, k+1)
.
Compute
H=Xd(Xd⊤Xd)−1Xd⊤
once (with
np.linalg.inv
or
np.linalg.solve
) and return
trace(H)
.
Return a Python
float
, not an
int
:
k+1
up to floating-point error (for example
4.0000000000000004
).