Contrasts (linear combinations)

A contrast is any linear combination of a random vector’s components , e.g. N − S, E + W, or (E + W) − (N + S) on the cork-deposit data. Once you write the contrasts as for a constant matrix , their expectations and covariances drop out of the random-vector machinery: and . The prof flagged the cork worked example as “a good exercise to do in the exercise session.”

Definition (prof’s framing)

“[A contrast is] any linear combination of the variables you find interesting: e.g. N − S, E + W, (E + W) − (N + S). Once you’ve defined them as new variables, you can take their expectations and covariances using the same machinery.” - L04-statlearn-3

Formally, given a random vector and a constant matrix , the new random vector holds the contrasts.

Notation & setup

  • : original random vector with mean and covariance .
  • : contrast matrix. Each row is one linear combination’s coefficients.
  • : vector of contrasts.
  • “Contrast” in the strict statistical sense means the row-coefficients sum to zero (so the linear combination is invariant under shifts of the mean), but in this course “contrast” is used loosely for any linear combination of interest. The cork example “E + W” wouldn’t qualify under the strict definition; the prof’s usage is the looser one.

Formula(s) to know cold

The two formulas to know cold here are the same two from random-vector-and-covariance:

If is multivariate normal, then is also multivariate normal , that’s one of the four useful properties of the multivariate-normal from the slides (“linear combinations of components are multivariate normal”). So .

Insights & mental models

Contrasts as feature engineering for the multivariate setting. You define new variables that capture the comparison you actually care about , e.g. on the cork data, “is the cork deposit denser on the south side than the north?” maps to , and “is the east-west contrast different from the north-south contrast?” maps to . Once you have , the rest is matrix algebra.

The cork worked example (modules/2StatLearn/2StatLearn.2.md / L04-statlearn-3):

For three contrasts (N − S, E + W, (E + W) − (N + S)) on :

Then and , both computed by direct matrix multiplication. In R: C %*% mu and C %*% Sigma %*% t(C). The exercise asks you to write down , identify as , and find (row-1 of ) dotted with (row-3 of ).

Why contrasts matter beyond M2:

  • Hypothesis tests in regression (Q4 of the four “important questions” in M3): testing whether two coefficients are equal, or whether a sum of coefficients differs from a baseline, is a contrast on . The covariance machinery is the foundation for the standard errors of those tests.
  • Categorical predictors with K levels (categorical-encoding-and-interactions): the K − 1 dummies define K − 1 contrasts against the reference level. R’s contrasts() function is named for exactly this. The prof uses “contrast” in this regression sense in M3.
  • PCA (principal-component-analysis): each principal component is a contrast , a linear combination of the standardized predictors. The loadings are the contrast coefficients, the PC variances are on the diagonal.
  • LDA / QDA: the discriminant functions are linear contrasts in , the decision boundary is where two such contrasts are equal.

So while the M2 atom is light, the machinery (covariance of a linear transformation) is everywhere downstream.

Reading the result. is a matrix whose diagonal entries are (row- of ) (row- of ), and whose off-diagonal entries are the covariances between contrasts. From there you can pull correlations between contrasts the usual way.

Pitfalls

  • Order matters in , not . Easy transpose mistake.
  • The constant matrix has to be conformable: is , is , so is and is . Check dimensions first.
  • A “contrast” in this course is just any linear combination: don’t get hung up on the stricter definition that requires the coefficients to sum to zero.
  • If has linearly dependent rows, will be singular even if wasn’t , the contrasts you defined aren’t truly -dimensional.
  • Mean-centering vs not: since uses , the constant intercept term doesn’t appear in , but does carry the means.

Scope vs ISLP

  • In scope: writing down a contrast matrix , computing and by hand, the cork example.
  • Look up in ISLP: ISLP doesn’t have a dedicated “contrasts” section in chapter 2 , the closest treatment is the categorical-encoding discussion in §3.3.1 (and the implicit contrast-matrix view of dummy coding). For the matrix-algebra theory, ISLP is light; Johnson & Wichern or any multivariate-stats text covers it formally.
  • Skip in ISLP: none , this is a matrix-algebra fact, not a textbook topic.

Exercise instances

No recommended-exercise problem tagged “contrasts” specifically. The cork worked example in the slide deck (modules/2StatLearn/2StatLearn.2.md) and the prof’s “good exercise to do in the exercise session” remark in L04-statlearn-3 are the de facto exercise instance , write down for (N − S, E + W, (E + W) − (N + S)), compute and analytically and in R.

The downstream applications (regression coefficient testing in M3, PCA loadings in M10, discriminant functions in M4) are exercised heavily in their own atoms.

How it might appear on the exam

  • Direct hand-calc. Given and a (probably 2 × 3 or 2 × 4), compute . Pure plug-and-chug, well-suited to the 2026 short-answer format.
  • Identify a contrast in a regression context. Given a regression with three group dummies, write the contrast matrix that tests “group 1 vs the average of groups 2 and 3.”
  • Combined with multivariate normal. Show that if , then . (One-line application of the multivariate-normal property.)
  • Conceptual: why are PCA loadings called “contrasts”? Because each PC is a linear combination of the standardized variables, and the loadings give the coefficients of that combination.