Section mini-project · about 10 to 14 hours
Least squares and PCA with rank and conditioning failures
Uses U1, U2, U3, U4, U5, U6, U7, U8
Start the shared numlab/ repository with a linear-algebra package: a QR least-squares solver, power iteration, a cutoff-aware pseudoinverse and PCA. Each routine is checked against an independent NumPy oracle and shown failing on a case you construct, and the solver with its conditioning evidence carries into the capstone.
Steps
- Before coding, write
notes/linalg.md: derive the normal equations, show the least-squares projector is symmetric and idempotent, and present PCA as both maximum variance and best rank approximation. - Implement thin QR by modified Gram-Schmidt or Householder reflections and solve least squares by back substitution on . Check a full-rank tall case against
numpy.linalg.lstsqwith projector identities, keeping the matrix and tolerances your choice. - Make one column a combination of the others, show the small diagonal entry of
Rand the exploding solution, then detect it with a relative threshold onRentries that you justify, either raising an error or falling back to the minimum-norm solution. - Add power iteration on a symmetric matrix reporting the Rayleigh quotient and the eigen-residual each step. Match
numpy.linalg.eighon a separated case, then construct a tied-magnitude case where the quotient settles at a non-eigenvalue and the residual rule reports failure. - Build a pseudoinverse from
numpy.linalg.svdwith a cutoff relative to the largest singular value, checking it againstnumpy.linalg.pinvand the Penrose conditions, and show the no-cutoff behavior. Implement PCA on centered data and compare components through projectors or reconstructions so sign flips cannot fail. - Assert shapes in every routine, including a batched case, and compare flop counts for the normal equations against QR. Then solve a rising-condition family from a known , tabulating conditioning, residual and error per solver and explaining the digit loss from the residual bound.
Deliverables
numlab/linalg/: QR least squares with rank detection, power iteration, the cutoff pseudoinverse and PCA.- Tests against NumPy oracles covering projector and Penrose checks, both failure cases, sign-invariant comparison and shapes.
notes/linalg.mdwith the derivations andnotes/linalg-defense.mdwith the conditioning table and solver choice.
How it is checked
- Rubric Self-grade with the rubric below
Rubric
| Criterion | Pass | Strong |
|---|---|---|
| Written derivations | The notes derive the normal equations, projector properties and both PCA views without copying. | Each derivation names the assumption it needs at the step that uses it. |
| Least squares | QR matches numpy.linalg.lstsq on a full-rank case, and the rank-deficient case is detected, not returned. | The rank threshold is justified from the scale of R and rounding, with a minimum-norm fallback checked against the pseudoinverse. |
| Spectral methods | Power iteration matches numpy.linalg.eigh on the separated case and reports failure on the tied case. | The note explains the settled quotient from the tied components, and the stopping rule uses the eigen-residual. |
| SVD and PCA | The pseudoinverse passes the Penrose conditions, and PCA reconstruction error matches the discarded singular values. | Every comparison is sign-invariant, and the no-cutoff behavior is shown. |
| Conditioning evidence | The table reports conditioning, residual and error for both solvers across the family. | The observed digit loss is compared with the residual bound and the squared conditioning of the normal equations. |
Feeds into: Capstone least squares and PCA: the QR solver with its rank failure, sign-invariant PCA checks and the conditioning table behind the solver choice.