Skip to content

Learning from dataLesson 4 of 6

Assume a shape, then fit it

Parametric models and least squares

The simplest shape: the output is a weighted sum of the inputs, plus a starting level.

fL(x;β)=β0+j=1pβjxj.f_L(x;\beta)=\beta_0+\sum_{j=1}^{p}\beta_j x_j.

symbolsay itfor the ad campaigns
x1,,xpx_1,\ldots,x_pthe p numbers in one input row — not p different rowsTV, radio, newspaper spends
β0\beta_0”beta zero”: where the line sits when every input is 0predicted sales when all advertising inputs are zero
βj\beta_jchange in the model’s prediction per unit of input j, others fixedpredicted sales difference per unit of TV spend
fL(x;β)f_L(x;\beta)the model’s output at row x, using coefficients βpredicted sales

This model has p+1p+1 parameters: one coefficient per feature, plus the intercept. For the advertising data, that is four parameters regardless of the number of rows. A parametric model uses a fixed-size list of parameters to describe its possible functions. After fitting, β^\hat\beta contains the estimated coefficients and f^L\hat f_L is the resulting prediction function.

methodwhich observations determine the estimate at xwith N = 1,000, p = 10, r = 0.1
average your neighborsonly those within r of x — about N·r^p0.0000001 expected observations
the lineall N rows, through the shared coefficients1,000 rows constrain 11 parameters

An observation far from the target input still helps estimate the slope, because a linear model uses the same slope throughout the input space. This lets all 1,000 rows inform the 11 parameters. The assumption also limits what the model can represent: if the conditional mean bends, no choice of coefficients can reproduce it exactly. The hidden rule in these examples has that curvature.

Choose the parameter values that minimize average squared error on the training data:

θ^=arg minθ1Ni=1N(yif(xi;θ))2.\hat\theta=\operatorname*{arg\,min}_{\theta}\frac1N\sum_{i=1}^{N}(y_i-f(x_i;\theta))^2.

θ\theta (“theta”) represents the model’s parameters. The sum runs over the N rows. The quantity being minimized is the training MSE — the mean squared error on the data used to fit the model — and θ^\hat\theta is the parameter estimate that minimizes it. The hat belongs to the fitted parameter: before fitting, f(x; θ) describes the whole family; afterward, f̂(x) = f(x; θ̂) is the chosen model.

For a tiny example, take rows (x,y) = (1,2) and (2,3). The candidate line 1+x predicts both exactly, so its training MSE is 0. The candidate 2x predicts 2 and 4, so its MSE is (0²+1²)/2 = 1/2. Argmin asks which parameter settings win this comparison across all allowed candidates. A numerical fitting algorithm carries out that search.

A polynomial in one input:

f(x;α)=α0+α1x++αdxd.f(x;\alpha)=\alpha_0+\alpha_1x+\cdots+\alpha_d x^d.

The degree d sets the parameter count, d + 1, and limits how much the curve can bend. A polynomial is nonlinear in x but still linear in its coefficients. d = 1 is the line. Increasing dd adds a coefficient and allows additional curvature.

Fit a polynomial to 40 observations. Change its degree to compare the fitted curve and training error, then reveal the rule that generated the data.

For these nested polynomial families, the minimum unpenalized training error cannot increase with degree. To evaluate predictions, use observations held out from fitting.

Definition

Read the full glossary entry →