This page explains what the “curve fitted to the points,” the “two bars of misses,” the “bundle of overlaid curves,” and the “band where the curve probably passes” that you saw in the simulator correspond to in the language of machine learning, and by what equations they are computed, in the order words → equations → calculation procedure. The procedure for the detailed view, the conditions of the calculation, and questions for checking are also collected here.
▶ Open the simulator (simple view)▶ Open the detailed view
The Regression and Overfitting Simulator is a tool for seeing how, when a curve is fitted to measured points, the complexity of the curve, the number of points, and regularization change how well it does on new data. It covers polynomial regression, the bias–variance decomposition, and regularization in Chapter 6 (generalization and overfitting), and ridge regression and Bayesian linear regression in Chapter 8 (linear regression models). In every scene the data are Data A, “firing temperature and relative density” (synthetic data for teaching purposes), the same as in the text.
The simulator has two views.
The three scenes of the simple view are computed with the same functions as ①, ②, and ③ of the detailed view. We recommend this order: try the simple view, check what it really shows in Section 2, read the equations in Section 3, and operate the detailed view with the procedure in Section 4.
The simple view compares fitting a curve to “bending a wire along the points.” The softness of the wire (how easily it bends) corresponds to the degree d of the polynomial, and the “brake” that keeps it from bending too much corresponds to regularization (the penalty of ridge regression). In every scene, the horizontal axis is the firing temperature T (°C) and the vertical axis is the relative density y (%; how tightly packed the fired material is), and, as in the text, the calculation uses the normalized s = (T − 1300 °C)/(200 °C) (s = −1 is 1100 °C and s = 1 is 1500 °C). The “true relation” shown by the gray dashed line is the true function g(s) that generated the data.
The curve f(s; 𝒘) is a polynomial of degree d (its coefficients are w0, …, wd, M = d + 1 in all), and 88 % is a value fixed as the baseline of the target value (Section 6.2.1 of the text).
A curve is fitted by least squares to the points at hand (the training data), and we compare how well that curve fits data newly produced by the same mechanism (200 points of validation data). The lengths of the two bars below are both the RMSE (root mean square error: the vertical gaps between the points and the curve, squared, averaged, and then square-rooted). The readout on the screen rephrases it as “The size you get by squaring the vertical gaps between the points and the curve, averaging them, and taking the square root” (because of the squaring, larger gaps count more strongly). The first bar is the training RMSE and the second is the validation RMSE (an estimate of the generalization error).
| Words on the screen | Term | Symbol or formula |
|---|---|---|
| Points at hand (●) | training data | 𝒟 = {(sn, yn)}, N = 10, 20, 50 |
| New data (+) | validation data | 200 points drawn separately from the same distribution |
| Firing temperature (horizontal axis) | input (firing temperature) | T = 1300 + 200s (°C) |
| How tightly packed after firing (vertical axis) | target value (relative density) | y (%) |
| True relation (made up) | true function | g(s) |
| Fitted curve | fitted polynomial | f(s; 𝒘) |
| How easily the curve bends: Straight / Moderate / Wiggly | degree of the polynomial | d = 1, 3, 9 (M = 2, 4, 10 coefficients) |
| Brake (regularization): off / on | strength of the ridge regression penalty | λ = 0 (least squares) / λ = e−5 ≈ 0.0067 |
| Red vertical lines | residual | yn − f(sn; 𝒘) |
| Miss on the points at hand (first bar) | training RMSE | RMSE on the training data |
| Miss on new data (second bar) | validation RMSE | RMSE on the 200 points of validation data |
| 1 % = measurement scatter (purple dotted line) | noise standard deviation | σ = 1.0 % |
Numbers for “Try it” (the points at hand are the same points as the default of the detailed view ①, seed 20261112):
Note that, for any curve, the miss on new data rarely becomes smaller than the noise standard deviation of 1 %. This is because no curve can predict the scatter contained in a new measurement (Section 6.2.3 of the text). On the other hand, the miss on the points at hand can become smaller than 1 %, because the curve fits itself even to the scatter of the points at hand.
Where it fits: just as a softer wire can be made to follow more points, raising the degree d makes the curve pass closer to the points at hand, and the training RMSE keeps falling (the set of polynomials of degree d contains the set of degree d − 1, so the minimum training error never increases).
Where it does not fit: for a real wire, its stiffness decides the shape between the points, but polynomial fitting only minimizes the sum of squared errors at the points, and the shape between the points is decided by the form of a polynomial. That is why the curve swings widely between the points at d = 9. Also, the “brake” is a penalty on the sum of squares of the coefficients ‖𝒘‖2, not a penalty on the amount of bending itself. This simulator also penalizes the constant term w0, so a stronger brake brings the curve closer to the horizontal line at 88 %. The order and motion in which the new data fall are only for the picture and are not used in the calculation.
▶ Simulator (simple view) A: Does a curve that fits the points exactly also fit new data?
Under the same conditions (the same temperature range, 15 points, the same noise), the points are measured again many times, and each time a curve of the same degree is fitted and overlaid. This is a direct picture of “considering many data sets under the same conditions” in Section 6.4 of the text. If the fit to the l-th data set is f(l)(s) and its mean is f̄(s), the spread among the curves is the variance, and the offset of the mean line from the true function is the bias.
| Words on the screen | Term | Symbol or formula |
|---|---|---|
| Measure again (15 points each time) | drawing a data set under the same conditions again | the l-th data set, N = 15 |
| Thin lines (curve per measurement) | fit to each data set | f(l)(s) |
| Curves overlaid | number of data sets | L (up to 30) |
| Thick green line (Mean line) | mean of the fits | f̄(s) = (1/L) Σl f(l)(s) |
| Spread among the curves (variance) | square root of the variance | (variance)1/2 (%) |
| Offset of the mean line from the true relation (bias) | magnitude of the bias | ((bias)2)1/2 (%) |
| Brake (regularization): on | penalty of ridge regression | λ = e−3 ≈ 0.050 (the default of the detailed view ②; stronger than the brake in A) |
| Curves that swung outside the frame | (a number for the screen only) | number of curves that went outside 66–100 % |
“Brake (regularization): on” in A and in B does not have the same strength. A uses ln λ = −5 (step 2 of the procedure for the detailed view ①) and B uses ln λ = −3 (the default of the detailed view ②), so the λ of B is e2 ≈ 7.4 times that of A. Both are set to the same values as in the detailed view, so that the numbers can be compared directly with the detailed view and the text. The screen does not show the difference in strength, so be careful when comparing the numbers of A and B.
The mean line (thick green line) is drawn from the 2nd curve on, so the number for the offset (bias) is also shown from the 2nd curve on.
The two numbers on the screen are the (bias)2 and the variance of the equation in the text (Section 6.4.3), each turned into its square root so that the unit is %. Both are sizes averaged (smoothed) over the temperature s.
Numbers for “Try it” (all for 10 measurements):
Where it fits: as in the detailed view ②, 15 points are drawn again each time from the same true function g(s) and noise σ = 1.0 %, and fitted with the same equations. The points up to the L-th set are the same as the first L sets of the default of the detailed view ② (seed 20261112).
Where it does not fit: in a real experiment the true relation is unknown, so the offset of the mean line from the true relation (the bias) cannot be measured directly. It can be measured in this scene only because the data are made from a made-up formula. Also, the numbers on the screen are estimated from 10–30 sets, and (bias)2 contains an estimation error of about variance/L (Section 5.3). Note also that the spread of the straight line is not necessarily smaller than that of the moderately bending curve (0.78 % and 0.71 % in the numbers above).
▶ Simulator (simple view) B: Does the shape of the curve change each time you measure again?
Instead of fixing the coefficients 𝒘 of the cubic to a single set, Bayesian linear regression represents them by a probability distribution (the posterior distribution) saying “coefficients that fit the data are around here” (Section 8.7 of the text). Once the distribution of the coefficients is known, the range through which the curve probably passes at each temperature (the predictive distribution) is determined (Section 8.8 of the text). Each time a point is measured, the posterior distribution is recomputed and the band is redrawn.
| Words on the screen | Term | Symbol or formula |
|---|---|---|
| Measure 1 point | adding one observation | (sn, yn), yn = g(sn) + εn |
| Temperatures to measure: Whole range / Low temperatures only | distribution of the input | s uniform on [−1, 1] / [−1, 0] |
| Middle line (most likely curve) | predictive mean (the curve given by the posterior mean 𝒎N) | 𝒎NT𝝓(s) + 88 |
| Blue band (likely range of the curve) | lack-of-knowledge band | ± 2(𝝓T𝑺N𝝓)1/2 |
| Dotted line (likely range of the next measurement) | observation band | ± 2(σ2 + 𝝓T𝑺N𝝓)1/2 |
| Purple dots (possible curves) | function samples from the posterior | 𝒘 = 𝒎N + 𝑹−1𝒛 |
| 1200 °C / 1500 °C: likely range of the curve | half-width of the lack-of-knowledge band | at s = −0.5 / 1: 2(𝝓T𝑺N𝝓)1/2 |
| Not measured (gray background) | outside the range from which observations are drawn | region of extrapolation |
Numbers for “Try it” (the sequence of points measured from the whole range is the same as “1 point from Data A” in the detailed view ③):
When 8 points are measured from the whole range, they are the same 8 points as right after opening the detailed view ③. The half-width of the blue band at 1300 °C (s = 0) is then 1.43 %, the same value as in step 2 of the procedure of Simulator 8.2 in the text.
Where it fits: the calculation uses the same equations as the default of the detailed view ③ (polynomial of degree 3, σ = 1.0 %, σw = 10). The purple dotted lines are three “wire shapes” that fit the data, drawn from the posterior distribution, and they use the same random numbers 𝒛 as the first three samples of ③.
Where it does not fit: the blue band is the width when the model assumptions “the curve is a cubic, the prior of the coefficients is 𝒩(𝟎, σw2𝑰), and the noise is σ = 1.0 %” are correct. The offset due to the part of the true function that a cubic cannot represent (part of 2 sin 3s) is not included in the band. If the basis functions are switched to the Gaussian basis, the band can even become narrow far from the data (step 6 of the procedure for the detailed view ③).
How far “the band gets narrower as more points are measured” holds: each added observation only adds to 𝑺N−1 the term σ−2𝝓𝝓T, so with the settings of this scene (σ and σw fixed) there is no temperature at which adding a point makes the band wider. However, at temperatures far from the measured range the narrowing is much slower: with “Low temperatures only,” the half-width at 1500 °C is ±40 % before measuring, ±31 % with 20 points, and still ±29 % with 40 points. Moreover, under the assumption of a cubic, if you keep adding points even only at low temperatures, the band at high temperatures also narrows slowly (this goes beyond the limit of the screen, but with the same functions and 1000 points it is ±10 %). In that case the predictive mean at 1500 °C is 106 %, which is off from the true value of 91.3 % by more than the band. The band in the unmeasured range is a width under the condition “if the assumption of a cubic is correct,” and it does not guarantee that the extrapolation is right.
The polynomial f(s; 𝒘) is linear in the coefficients 𝒘, so “the sum of squared errors at the points” is a quadratic function of 𝒘, and the 𝒘 that minimizes it is determined by simultaneous linear equations (Section 8.2 of the text). This is the least-squares method. Raising the degree increases the number of coefficients and lets you make curves that pass closer to the points, but when the number of coefficients approaches the number of points, the coefficients become huge positive and negative values that cancel each other, and the curve swings widely between the points (Section 6.2.4 of the text). Regularization (ridge regression) adds to the loss the penalty on the sum of squares of the coefficients, (λ/2)‖𝒘‖2, so that the coefficients do not become too large (Sections 6.6 and 8.6 of the text).
The input is the standardized firing temperature T (°C), s = (T − 1300)/200 ∈ [−1, 1]. The basis of the degree-d polynomial is φj(s) = sj (j = 0, …, d), M = d + 1 functions in all, and the design matrix is Φ ∈ ℝN×M, Φnj = snj. The target value is the relative density minus the fixed baseline 88 %, yn − 88 %, and the curve on the screen is displayed with 88 % added back (the baseline is not computed from the data).
The ridge regression in ① minimizes the following loss (λ = 0 is the least-squares method).
The normal equations are not formed. For λ = 0 the Householder method is used to QR-decompose Φ, and for λ > 0 the vertically stacked matrix [Φ; √λ I], and w is obtained by back substitution with the triangular matrix (the least-squares solution of [Φ; √λ I] coincides with the ridge solution). When the number of coefficients M = d + 1 exceeds the number of data points N and λ = 0, there are infinitely many solutions with zero training error, so the minimum-norm solution from the singular value decomposition Φ = U diag(si) VT is shown, and the screen says so. The RMSE is
where the training RMSE is computed on the training data and the validation RMSE on 200 points drawn separately from the same distribution.
When the data are taken again under the same conditions, the fitted curve also changes. The expectation of the squared error of the prediction for a new observation splits into the sum of three parts: the amount by which the mean prediction is off from the true function (the square of the bias), the fluctuation of the prediction due to retaking the data (the variance), and the fluctuation of a new observation that no model can reduce (the noise σ2) (Theorem 6.2 in Section 6.4 of the text). Making the model more complex (raising the degree, weakening the regularization) decreases the bias and increases the variance, and making it simpler does the opposite.
Bias–variance decomposition in ②: denote the fit to the l-th data set (l = 1, …, L) by f(l)(s) and its mean by f̄(s) = (1/L) Σl=1L f(l)(s), and take the average over s with the uniform distribution on [−1, 1], ⟨·⟩s (midpoint rule, 200 points).
The cross term 2⟨(f̄ − g)(f(l) − f̄)⟩ vanishes when averaged over l, and since the noise ε is independent of the fit, its cross terms also have zero expectation. When sweeping λ, the singular value decomposition of Φ is done only once for each data set, and w(λ) = Σi vi si/(si2 + λ) uiTy gives the solutions for all λ (they agree with the QR solutions within rounding error). The mean and variance are updated one set at a time by Welford’s method, and the computation is divided into chunks of at most 12 ms per frame.
In Bayesian linear regression, a prior distribution is placed on the coefficients, and the distribution of the coefficients after seeing the data (the posterior distribution) is obtained. Since both the prior and the noise are Gaussian, the posterior is also Gaussian, and its mean 𝒎N and covariance 𝑺N can be written as formulas (Section 8.7 of the text). The variance of the prediction at a new temperature splits into a part due to not knowing the coefficients (lack of knowledge) and a part due to the noise of the next measurement (Section 8.8 of the text). With each observation, 𝑺N−1 gains the term σ−2𝝓𝝓T, so the lack-of-knowledge part becomes small where there are data, but the noise part σ2 remains.
Bayesian linear regression in ③: the prior is p(w) = 𝒩(w | 0, σw2I) and the noise variance is σ2. The posterior is 𝒩(w | mN, SN), and the predictive distribution is as follows.
The lack-of-knowledge band is the predictive mean ± 2(φ*TSNφ*)1/2, and the observation band is ± 2(σ2 + φ*TSNφ*)1/2. SN is not formed; the QR decomposition of the vertically stacked matrix [Φ/σ; I/σw] gives SN−1 = RTR, and φTSNφ = ‖R−Tφ‖2 is computed by triangular substitution. A sample from the posterior is w = mN + R−1z (z ~ 𝒩(0, I)). The MAP estimate mN coincides with the ridge regression solution for λ = σ2/σw2 (the screen shows the difference).
The Gaussian basis is φj(s) = exp[−(s − cj)2/(2r2)], with nine centers cj = −1, −0.75, …, 1 and width r = 0.25 (equal to the spacing of the centers). No constant basis function is included.
The calculation in the simple view calls the same functions as the detailed view (in regression.model.js: fitPoly, rmse, makeDatasets, bvSweepLambda, bayesPosterior, bayesPredict, bayesSamples, and others). The only calculations added for the simple view are the way the “Low temperatures only” points are drawn in Scene C, the count of curves outside the frame, and the rounding of the numbers on the screen (regression.easy.js).
The detailed view has three tabs: ① (Polynomial Regression and Regularization), ② (Bias and Variance), and ③ (Bayesian Linear Regression). Each has the figure and the “Putting the current numbers into the formula” box on the left, and the controls and readouts on the right.
▶ Simulator (detailed view) ①: Polynomial Regression and Regularization▶ Simulator (detailed view) ②: Bias and Variance▶ Simulator (detailed view) ③: Bayesian Linear Regression
With the keyboard, Tab moves to buttons and sliders, Enter (or Space) presses a button, and the arrow keys move a slider. The tabs can be switched with ← and →.
Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.
No. A polynomial of degree 9 has 10 coefficients and passes through all 10 points at hand, so its miss on the points at hand is almost 0. But it swings widely between the points, and its miss on new data is 14.8 %, more than an order of magnitude worse than the 1.23 % of “Moderate” (a cubic). The quality of a curve is measured by its miss on new data. Note that the miss on new data rarely becomes smaller than the measurement scatter of 1 %, so 1.23 % is a fairly good value.
The blue band is ±1.1 % and the dotted line is ±2.3 %. As more points are added, the blue band approaches 0, but the dotted line does not become narrower than ±2 % (twice the measurement scatter σ = 1 %). Adding points reduces only the part due to not knowing the curve (the coefficients); the scatter of the next measurement remains.
There are d + 1 = 10 coefficients and 10 training points, so Φ is a nonsingular 10×10 matrix and there is exactly one polynomial satisfying Φw = y (interpolation through all points). The validation RMSE is 14.8 %, more than an order of magnitude worse than the 1.23 % at d = 3. The curve swings widely between the training points and has memorized even the noise.
The validation RMSE goes 2.40 → 1.31 → 2.43 %, and the largest absolute value of the coefficients goes 75.0 → 12.8 → 5.89. The penalty (λ/2)‖w‖2 keeps the coefficients small and so suppresses the swings, but if it is too large the curve can no longer express even the curvature of the true function (the bias increases), and the training RMSE also rises to 1.68 %.
Around ln λ ≈ −3 (bias2 ≈ 0.16, variance ≈ 0.73, noise 1.00, total ≈ 1.89 %2). To the left (small λ) the variance is larger, and to the right (large λ) bias2 is larger.
Of the predictive variance of the observed value, σ2 + φTSNφ, the term σ2 is the measurement noise and does not decrease with more data. Since SN−1 gains σ−2φφT with each observation, φTSNφ becomes small where there are data, and the lack-of-knowledge band approaches 0.
References (from the end of Chapter 6):
References (from the end of Chapter 8):
Back to the Regression and Overfitting Simulator | Machine Learning — From Probability to GPT (Contents) | Equations are typeset with the browser’s built-in MathML (no external library).