Speed

What the Matrix Does and Does Not Say

Distinguish what a design matrix guarantees—exact linear combinations of its columns—from what it cannot express without explicit feature engineering or causal assumptions.

The problem

Across this book we stripped row identifiers, chose ordered columns, shaped X as (n, p), shaped y as (n, 1), and prepended a unit column in Add Intercept Column. That clean (n, p + 1) design matrix looks authoritative, so it is tempting to assume X_aug @ beta can capture any pattern in the data or tell us what will happen under an external intervention.

Knowing the exact algebra of X_aug @ beta reveals both its power and its hard boundaries. A matrix product can form every linear combination of the columns stored inside X_aug, and nothing outside their column span.

Work through the numbers

Four calibration runs record [x, y] in table = [[0, 5], [1, 8], [2, 13], [3, 20]]. Look at consecutive target gaps as x steps by +1: 8 - 5 = 3, 13 - 8 = 5, and 20 - 13 = 7. Any two-column linear matrix [1, x] multiplied by [[b0], [b1]] changes by the constant slope b1 on every unit step, so no [b0, b1] can match gaps 3, 5, and 7 simultaneously.

To fit the curve y = 5 + 2x + x^2 with linear algebra, we must engineer x_sq = x * x as an explicit third column, forming X_poly = [[1, 0, 0], [1, 1, 1], [1, 2, 4], [1, 3, 9]]. Multiplying X_poly by beta = [[5], [2], [1]] stays linear in the parameters beta while evaluating a parabola in x:

  • x = 0: 1*5 + 0*2 + 0*1 = 5, |5 - 5| = 0
  • x = 1: 1*5 + 1*2 + 1*1 = 8, |8 - 8| = 0
  • x = 2: 1*5 + 2*2 + 4*1 = 13, |13 - 13| = 0
  • x = 3: 1*5 + 3*2 + 9*1 = 20, |20 - 20| = 0

Even with zero residual on every row, X_poly records observed co-variation rather than physical mechanism. If a hidden confounder drove both x and y, intervening on x directly could break 5 + 2x + x^2 completely.

The live model

The model builds X_poly = [1, x, x^2], multiplies by [[5], [2], [quad]], and pairs [pred, abs_err] in answer. At the default quad = 1, predictions hit [5, 8, 13, 20] with errors [0, 0, 0, 0]. Drag quad down to 0 to turn off the x^2 column: predictions drop to the straight line [5, 7, 9, 11] with errors [0, 1, 4, 9]. Drag quad up to 2 to over-curve predictions to [5, 9, 17, 29], producing symmetric errors [0, 1, 4, 9].

Predictions and absolute errors for y = 5 + 2x + x^2: zero error across [5, 8, 13, 20] when the engineered x^2 column is active at quad = 1.
let quad = scene.parameter("quad", 1.0, 0.0, 2.0, 1.0);
let table = input;
let n = table.size(0);
let ones = torch.ones(n, 1).to(DType.F64);
let x = table.select(1, 0).unsqueeze(1);
let x_sq = x * x;
let X_poly = torch.cat(torch.list().add(ones).add(x).add(x_sq), 1);
let beta = torch.zeros(3, 1).to(DType.F64);
beta.set(0, 5.0);
beta.set(1, 2.0);
beta.set(2, quad);
let pred = X_poly.matmul(beta);
let y_col = table.select(1, 1).unsqueeze(1);
let err = (pred - y_col).abs();
let answer = torch.cat(torch.list().add(pred).add(err), 1);
out.set("X_poly", X_poly).set("answer", answer);
{
  "X_poly": [
    [
      1,
      0,
      0
    ],
    [
      1,
      1,
      1
    ],
    [
      1,
      2,
      4
    ],
    [
      1,
      3,
      9
    ]
  ],
  "answer": [
    [
      5,
      0
    ],
    [
      8,
      0
    ],
    [
      13,
      0
    ],
    [
      20,
      0
    ]
  ]
}

Your turn

Because column 2 of X_poly currently repeats x, X_poly @ [5, 2, 1] can only trace the straight line 5 + 3x = [5, 8, 11, 14], leaving curved residuals [0, 0, 2, 6] on rows 2 and 3. Supply the quadratic feature in x_sq so X_poly spans [1, x, x^2] and answer pairs [pred, abs_err]. Check evaluates a three-row table and a five-row table.

Duplicating x instead of x^2 leaves curved errors [0, 0, 2, 6] because [1, x, x] spans only straight lines.
let table = input;
let n = table.size(0);
let ones = torch.ones(n, 1).to(DType.F64);
let x = table.select(1, 0).unsqueeze(1);
let x_sq = x;
let X_poly = torch.cat(torch.list().add(ones).add(x).add(x_sq), 1);
let beta = torch.zeros(3, 1).to(DType.F64);
beta.set(0, 5.0);
beta.set(1, 2.0);
beta.set(2, 1.0);
let pred = X_poly.matmul(beta);
let y_col = table.select(1, 1).unsqueeze(1);
let err = (pred - y_col).abs();
let answer = torch.cat(torch.list().add(pred).add(err), 1);
out.set("X_poly", X_poly).set("answer", answer);
{
  "X_poly": [
    [
      1,
      0,
      0
    ],
    [
      1,
      1,
      1
    ],
    [
      1,
      2,
      2
    ],
    [
      1,
      3,
      3
    ]
  ],
  "answer": [
    [
      5,
      0
    ],
    [
      8,
      0
    ],
    [
      11,
      2
    ],
    [
      14,
      6
    ]
  ]
}

Complete the three-column polynomial matrix X_poly so answer pairs [pred, abs_err] for the quadratic curve 5 + 2x + x^2.

Once column 2 carries [0, 1, 4, 9], predictions match [5, 8, 13, 20] and all four error cells drop to 0.

Read the result carefully

Notice that X_poly @ beta is still ordinary linear algebra in beta: curves enter by expanding the columns of X_poly, not by changing matrix multiplication. On unseen probes where noisy targets deviate from 5 + 2x + x^2, (pred - y_col).abs() still measures exact non-negative distance from that quadratic column combination.

Recap

  • X @ beta expresses every linear combination of the columns placed inside X, and nothing outside that column span.
  • Curves and interactions such as x^2 or x0 * x1 enter a linear model only when engineered as explicit columns of X.
  • Duplicating an existing column ([1, x, x]) adds a parameter without expanding the set of functions X @ beta can represent.
  • Zero residual inside X @ beta confirms numerical fit on observed rows, never causal direction under outside intervention.