Projection and Least Squares Lab
Model: y ≈ c0 + c1x
Model: y ≈ c1f1(x) + c2f2(x) + …
Use x, numbers, + − * / ^, parentheses, pi and e, and sin, cos, tan, exp, log (natural), sqrt, abs. Example: 1, x, sin(2x).
Each challenge checks itself. Work on this keeps it in view above the controls.
Project a vector onto a line and watch the error stay perpendicular; fit a line to data with the normal equations; then fit any basis of functions, including your own, and compare the errors.
How to use
- Switch labs with the two buttons at the top of the Controls tab (arrow keys move between them).
- Projection lab. Slide t to move the point t a along the line through a. The dashed segment from t a to b is the error e = b − t a. It is shortest exactly when e is perpendicular to a, at t = aTb / aTa.
- Data lab. Move the intercept and slope sliders to fit a line by eye. Each dashed vertical segment is a residual r = y − (c0 + c1x). The sum of squared errors SSE = r1² + … + rn² measures the fit: smaller is better.
- Press Snap to the least-squares line to jump to the line with the smallest possible SSE. It solves the normal equations ATA c = ATy exactly (fractions, not rounded) and shows every number.
- Change the data by typing in the table or by dragging points in the picture (mouse or touch).
- Other bases. Least squares is not only for lines: any model y ≈ c1f1(x) + … + ckfk(x) that is a linear combination of fixed functions works the same way. The design matrix A has one column per function (its values at the data x's); the best coefficients solve ATA c = ATy. Choose a basis or type your own, and compare its sum of squared errors with the best straight line. With as many functions as points the curve passes through every point; if the columns of A are dependent there is no unique answer.
- Keyboard: every control is a standard form field. The status boxes are read aloud by screen readers. Shapes and dash patterns, not only colours, tell the parts apart.
Accessibility: every control is a standard form control you can reach with Tab and change with the arrow keys; the status box is announced by screen readers; the picture has a text description (press Describe) that updates as you work; animations are skipped when your system asks for reduced motion.
Projection onto a line through the origin
Numbers
Data and candidate line
Data and the normal equations
| Point | x | y | Prediction c₀ + c₁x | Residual r | r² |
|---|
Type new coordinates (arrow keys change them by 0.5), or drag a point in the picture.