Speed

Add Intercept Column

Prepend a column of ones at index 0 of X so a single matrix product X_aug.matmul(beta) evaluates both baseline offset and feature slopes on every row.

The problem

In Build y Vector, we aligned (n, 1) column predictions X.matmul(w) with an (n, 1) target vector. Yet a raw feature matrix X of shape (n, p) always multiplies w through the origin: whenever every predictor in a row equals 0, the product X @ w is forced to predict 0.

Real measurements carry baseline offsets such as callout fees, ambient temperatures, or resting heart rates that stay non-zero at zero input. Rather than carrying a separate scalar b0 alongside w in every formula, linear algebra folds the offset into X by prepending a column of 1s at index 0.

Work through the numbers

Four workshop jobs record [hours, setups] in X = [[0, 0], [2, 1], [4, 2], [6, 1]]. Job r0 = [0, 0] is a standby reservation with 0 hours and 0 setups, yet the shop charges a base fee b0 = 10 plus b1 = 3 per hour and b2 = 2 per setup. Without an intercept column, [0, 0] times [[3], [2]] equals 0, erasing the 10 fee on r0 and undercharging every row by 10.

Prepending ones = torch.ones(4, 1) at column 0 produces the (4, 3) augmented design matrix X_aug:

  • r0: [1, 0, 0]
  • r1: [1, 2, 1]
  • r2: [1, 4, 2]
  • r3: [1, 6, 1]

Stacking the intercept and slopes into beta = [[10], [3], [2]] turns the affine rule into one matrix product X_aug @ beta. Row r0 evaluates 1 * 10 + 0 * 3 + 0 * 2 = 10, row r1 gives 1 * 10 + 2 * 3 + 1 * 2 = 18, row r2 gives 1 * 10 + 4 * 3 + 2 * 2 = 26, and row r3 gives 1 * 10 + 6 * 3 + 1 * 2 = 30.

The live model

The model prepends ones at column 0 and multiplies X_aug by [[b0], [3], [2]]. At the default b0 = 10, row r0 = [1, 0, 0] scores 10 purely from its leading 1, and the four bars read [10, 18, 26, 30]. Slide b0 down to 0 to drop every bar by 10 ([0, 8, 16, 20]), or slide b0 up to 20 to lift every bar together to [20, 28, 36, 40] while X_aug stays unchanged.

Augmented design matrix X_aug with a leading column of ones [[1, 0, 0], [1, 2, 1], [1, 4, 2], [1, 6, 1]] and predictions [10, 18, 26, 30] at base fee b0 = 10.
let b0 = scene.parameter("b0", 10.0, 0.0, 20.0, 10.0);
let X = input;
let n = X.size(0);
let ones = torch.ones(n, 1).to(DType.F64);
let answer = torch.cat(torch.list().add(ones).add(X), 1);
let beta = torch.zeros(3, 1).to(DType.F64);
beta.set(0, b0);
beta.set(1, 3.0);
beta.set(2, 2.0);
let pred = answer.matmul(beta);
out.set("X_aug", answer).set("pred", pred);
{
  "X_aug": [
    [
      1,
      0,
      0
    ],
    [
      1,
      2,
      1
    ],
    [
      1,
      4,
      2
    ],
    [
      1,
      6,
      1
    ]
  ],
  "pred": [
    [
      10
    ],
    [
      18
    ],
    [
      26
    ],
    [
      30
    ]
  ]
}

Your turn

Right now the unit column sits at index 2 ([[0, 0, 1], [2, 1, 1], [4, 2, 1], [6, 1, 1]]), so multiplying by beta = [10, 3, 2] charges 10 per hour and only 2 for the base fee, yielding [2, 25, 48, 65]. Place the all-ones column at index 0 of answer so X_aug aligns with [b0, b1, ..., bp]. Check tests a 3 x 2 matrix and a 5 x 3 matrix.

The starter appends ones at column 2 instead of prepending ones at column 0, misaligning beta = [10, 3, 2].
let X = input;
let n = X.size(0);
let ones = torch.ones(n, 1).to(DType.F64);
let answer = torch.cat(torch.list().add(X).add(ones), 1);
out.set("X_aug", answer);
{
  "X_aug": [
    [
      0,
      0,
      1
    ],
    [
      2,
      1,
      1
    ],
    [
      4,
      2,
      1
    ],
    [
      6,
      1,
      1
    ]
  ]
}

Build the (n, p + 1) augmented design matrix answer from input X so beta = [b0, b1, ..., bp] multiplies the unit intercept at column 0.

Putting the unit column first turns c0 green at [1, 1, 1, 1] and restores the shop quotes [10, 18, 26, 30].

Read the result carefully

Read b0 directly off the zero-input row r0 = [1, 0, 0]: the intercept weight is the model's prediction when every feature equals 0. If a feature can never physically reach 0—such as apartment floor area—centering features around their training mean makes b0 the prediction at an average record instead.

Regularized estimators such as ridge and lasso usually leave column 0 unpenalized. Shrinking b0 toward 0 would force predictions toward the origin whenever outcomes carry a large baseline offset, penalizing the units of y rather than model complexity.

Recap

  • Raw matrix multiplication X @ w always passes through the origin, predicting 0 whenever a row's features are all 0.
  • Prepending an (n, 1) column of 1s at column 0 builds X_aug of shape (n, p + 1).
  • Packing beta = [b0, b1, ..., bp] lets one matrix product X_aug @ beta add b0 * 1 to every row automatically.
  • Swapping the concatenation order puts 1s in the last column and scrambles which weight multiplies the intercept versus the predictors.