Class 2 · Interactive companion

The Price of
Flexibility

Four ways of letting a model bend, and the bill that arrives with each one. Interactions, nearest neighbours, and the trade that decides how much bending is worth paying for.

Dr. Asaf Madar · lecturer

18 cohorts · one average fit
01

Interaction with a group

Last class every predictor was a number. Plenty are not: sex, diagnosis, scanner site, task condition.

A two-category variable becomes a dummy: one column that is 0 or 1.

di = 0subject i is a control 1subject i is a patient

With k categories we need k − 1 dummies, never k. Three sites take two columns, and the category left out becomes the baseline: the one β0 describes. Adding the third column would let the model write the same prediction in infinitely many ways, and there would be no unique answer to solve for.

Now put the dummy next to a continuous predictor. There are three models we could write, and the difference between them is the point of this tab:

1  y = β0 + β1x 2  y = β0 + β1x + β2d 3  y = β0 + β1x + β2d + β3xd

Read model 3 by substituting the two values of d:

  • Controls, d = 0:  y = β0 + β1x
  • Patients, d = 1:  y = (β0 + β2) + (β1 + β3)x

Two separate lines. β2 shifts one up or down, and β3 tilts it. That tilt is the interaction: the effect of age on memory is not the same in the two groups.

One line, two parallel lines, or two free lines
change how different the groups really are

Set the slope difference to zero and model 3 stops earning its extra parameter: the two lines come out parallel, and β3 is just noise.

Key idea

Without the interaction term, the model is forced to give both groups the same slope. It can move the lines apart but never let them converge or cross. If the groups really do differ in slope, that constraint shows up as a systematic pattern in the residuals.

A significant interaction changes how the other coefficients read. In model 3, β1 is no longer “the effect of age”. It is the effect of age in the baseline group only, and β2 is the group gap at x = 0. Centre your predictors and those two numbers become readable again.
02

Two continuous predictors

Nothing about an interaction needs a category. Multiply two continuous predictors together and the same thing happens, only now the moderator varies smoothly instead of switching between two values.

y = β0 + β1x1 + β2x2 + β3x1x2

The trick from the previous tab still works. Collect the x1 terms together:

y = (β0 + β2x2) + (β1 + β3x2) x1

Read it as a line in x1 whose slope is itself a straight-line function of x2.
Every value of x2 gives a different line, so instead of two lines we get a whole family of them. With β3 = 0 the family is parallel. Turn β3 up and it fans out.

Below, sleep predicts a memory score, and age is the moderator: an interaction means a night of sleep is worth more to some ages than to others.

A family of lines, one per age
turn the interaction on and watch them fan out

Each line is the model evaluated at one age, holding age fixed and sweeping sleep. The slope of each line is β1 + β3x2, printed beside it.

The same model as a surface. Without the interaction it is the flat plane from last class. With it, the plane acquires a twist: a saddle, high at two opposite corners and low at the other two.

The surface that fan describes
drag to turn it

Straight along either axis, curved along the diagonals. Every line you can draw on it parallel to an axis is still straight, which is why this is still a linear model.

“Linear” has always meant linear in the coefficients, not in the predictors. x1x2 is just another column in X, so least squares solves it with exactly the same normal equations.

Key idea

An interaction is not a new kind of model. It is one more column, built by multiplying two columns we already had. Everything from last class still applies: the same RSS, the same derivation, the same solution.

Keep the main effects. Fitting β3x1x2 without β1x1 and β2x2 forces the family of lines to pivot about a point the data never chose. The convention, called the hierarchy principle, is to include both main effects whenever their interaction is in the model, even if one of them looks unimpressive on its own.
03

K-nearest-neighbours regression

Every model so far assumed a shape and then estimated its coefficients. KNN assumes nothing at all.

To predict at a point x0: find the K training points closest to it, and average their y.

(x0) = 1K Σxi ∈ N0 yi

That is the whole method. There is no training step and nothing to solve: the model is the data. Methods like this are called non-parametric, because the number of things they remember grows with n instead of being fixed at p + 1.

K controls how much smoothing happens. K = 1 copies the nearest neighbour exactly, noise and all. Large K averages over a wide neighbourhood and flattens toward the overall mean.

KNN against a straight line
switch the truth between linear and curved

Key idea

When the truth really is a line, linear regression wins and no value of K catches it: the parametric model already knows the answer's shape, so it spends all its data estimating two numbers. When the truth curves, that assumption becomes a liability and KNN overtakes it.

Why this stops working in neuroscience

KNN needs neighbours that are genuinely near. In one dimension that is easy. As p grows, the space empties out: to capture 10% of the data in a cube, you need to reach 10% of the range along one axis, but 63% along each of ten axes. The “nearest” neighbours are no longer near, and the average is taken over points that have little to do with x0.

The curse of dimensionality
how far you must reach to find 10% of the data

The side length of the cube you need in order to enclose a fixed fraction of a uniformly filled space, as dimensions are added.

A 500-voxel pattern is 500 dimensions. This is why a flexible method that looks unbeatable on one predictor can be worse than a straight line on real imaging data.

04

Bias and variance

Last class we watched test error fall, bottom out, then climb as a model got more flexible. This tab is the reason it does that.

Imagine repeating the whole study many times: same population, same design, a fresh cohort each time. Every cohort gives a slightly different fitted model. Two separate things can go wrong.

  • Bias is how far the average of those fits sits from the truth. It is the error the model makes by being the wrong shape, and more data will not remove it.
  • Variance is how much the fits differ from each other. It is the error that comes from having fitted this particular cohort rather than another.

Below, the study is repeated thirty times at whatever flexibility you choose.

Thirty cohorts, thirty fits
slide flexibility from too little to far too much

Squared bias and variance are averaged across the range of x. Their sum, plus the noise that no model can remove, is the expected test error at a new point.

E[(y(x))2]  =  Bias()2  +  Var()  +  Var(ε)

Three terms, and only the first two are ours to trade. The third is the irreducible noise from class 1: the part of y that no model, however good, can recover.

The two we control move in opposite directions: flexibility buys lower bias at the price of higher variance. The best model is not the one with the least bias, nor the one with the least variance, but the one whose sum is smallest.

The trade, drawn out
the dashed line marks your current degree

Squared bias falls, variance rises, and their sum is U-shaped. Where that U bottoms out is the flexibility worth choosing. The vertical axis is logarithmic, because the two curves span two orders of magnitude across this range.

Drag the subject count in the panel above and watch the curve shift. More data lowers variance at every degree, which nudges the sweet spot rightward: bigger samples can afford more flexibility.
Past that point the measured bias drifts upward too. With only thirty cohorts the average fit is itself an estimate, and once variance is large that estimate gets noisy. Real bias does not rise; our measurement of it does.

Key idea

A model that is too simple is wrong the same way every time. A model that is too flexible is wrong a different way every time. Both cost you, and the cost is measured on data the model has not seen.

You cannot compute this on real data. Bias needs the truth and variance needs many cohorts, and in a real study you have neither. That is what a test set is for: it estimates their sum directly, without ever separating them.
05

Glossary

Everything introduced today, on top of the class 1 glossary.

dummy
A 0/1 column standing for one category. k categories need k − 1 of them.
baseline
The category left without a dummy. β0 describes it, and every other coefficient is a difference from it.
main effect
A predictor’s own term, β1x1, as opposed to its interaction.
interaction
A product column x1x2. It lets the effect of one predictor depend on the value of another.
hierarchy principle
Keep both main effects whenever their interaction is in the model.
moderator
The predictor doing the changing. In β3x1x2 either one can be read as the moderator.
saddle
The surface an interaction makes: straight along each axis, twisted along the diagonals.
parametric
A model with a fixed number of coefficients, chosen in advance. Linear regression is parametric.
non-parametric
A model that keeps the data instead of summarising it. KNN is non-parametric.
KNN
Predict at a point by averaging the y of its K nearest training points.
K
How many neighbours to average. Small K is flexible and noisy, large K is smooth and blunt.
curse of dimensionality
In many dimensions, the nearest neighbours are not near. Flexible local methods lose their advantage.
flexibility
How many shapes a model can take. Polynomial degree and 1/K are both measures of it.
bias
How far the average fit sits from the truth. The error of being the wrong shape.
variance
How much the fit changes from one cohort to the next.
irreducible error
Var(ε). The floor no model can go below.
bias-variance
tradeoff
Flexibility lowers one and raises the other. Test error is smallest where their sum is.

Next time

What to do when a model has more flexibility than the data can pay for: shrinking coefficients toward zero, and choosing how much to shrink without touching the test set.

Machine Learning for Neuroscience · 1501.1027 · Semester A, 5787
Course text: James, Witten, Hastie & Tibshirani, An Introduction to Statistical Learning with Applications in Python (2023). Chapter 3.3 covers interactions, 3.5 covers KNN regression, and 2.2 covers the bias-variance tradeoff.
All data on this page is simulated.