BRAGA

02 Writing02-02 Notes02-02-445

Maximum Likelihood Reconstruction (Orthogonal Projection)

Published 16 Apr 202612 min read
Contents
  1. 01Deep Breakdown
  2. 02Scalar view first
  3. 03What the design matrix contains If there are
  4. 04Why sometimes XX and sometimes Φ\Phi
  5. 05Why this notation matters
  6. 06Intercept handling
  7. 07DA/DS relevance
  8. 08Flowchart
  9. 09Real-world use
  10. 10The formula, piece by piece
  11. 11The formula in words
  12. 12Critical Concepts
  13. 13Step-by-step calculation (tiny example)
  14. 14Code examples
  15. 15Self-check: mathematical verification
  16. 16Common Pitfalls and Edge Cases
  17. 17References and Standard Results
y=Xw+εy = Xw + \varepsilon
(1)

We are working on the feature matrix formulation of linear models, written as

y=Xw+ε,y = Xw + \varepsilon,
(2)

or equivalently with basis-function notation as

y=Φw+ε,y = \Phi w + \varepsilon,
(3)

where rows are observations, columns are features, ww is the coefficient vector, and ε\varepsilon is the error term. This is the standard matrix notation for linear regression and many related models. It is foundational for Normal Equations, Ridge regression, gradient-based fitting, and feature engineering workflows.

Deep Breakdown

The feature matrix formulation is the compact linear-algebra way to write a regression model. Instead of writing one separate equation for every observation, we stack all observations into one matrix system:

y=Xw+εy = Xw + \varepsilon
(4)

This is important because it turns regression from a list of scalar equations into a single object that can be manipulated with matrix algebra.

Scalar view first

For one observation ii, a linear regression model can be written as:

yi=w1xi1+w2xi2+⋯+wpxip+εiy_i = w_1 x_{i1} + w_2 x_{i2} + \cdots + w_p x_{ip} + \varepsilon_i
(5)

If there is an intercept, we often write:

yi=w0+w1xi1+⋯+wpxip+εiy_i = w_0 + w_1 x_{i1} + \cdots + w_p x_{ip} + \varepsilon_i
(6)

Now suppose we have nn observations. Writing those equations one by one is tedious.

So we collect them into:

  • one design matrix XX,
  • one coefficient vector ww,
  • one target vector yy,
  • one error vector ε\varepsilon.

That gives the matrix model:

y=Xw+εy = Xw + \varepsilon
(7)

What the design matrix contains If there are

  • nn observations,
  • pp features, then the design matrix has shape:
X∈Rn×pX \in \mathbb{R}^{n \times p}
(8)

if the intercept is handled separately, or:

X∈Rn×(p+1)X \in \mathbb{R}^{n \times (p+1)}
(9)

if a column of ones is included for the intercept.

The structure is always:

  • rows = observations,
  • columns = features.

So if observation ii has features (xi1,xi2,…,xip)(x_{i1},x_{i2},\dots,x_{ip}), then row ii of XX is:

Xi=[xi1xi2⋯xip]X_i= \begin{bmatrix} x_{i1} & x_{i2} & \cdots & x_{ip} \end{bmatrix}
(10)

or, with an intercept column:

Xi=[1xi1xi2⋯xip]X_i= \begin{bmatrix} 1 & x_{i1} & x_{i2} & \cdots & x_{ip} \end{bmatrix}
(11)

Why sometimes XX and sometimes Φ\Phi

In many statistics and machine-learning texts, the design matrix is written as XX. In other contexts, especially when the columns are not raw features but transformed features, the matrix is written as:

Φ\Phi
(12)

This is common when we use:

  • polynomial features,
  • splines,
  • radial basis functions,
  • interaction terms,
  • engineered transformations.

Then each column is a basis function value rather than just an original variable, and we write:

Φ=[ϕ1(x1)ϕ2(x1)⋯ϕ1(x2)ϕ2(x2)⋯⋮⋮⋱]\Phi= \begin{bmatrix} \phi_1(x_1) & \phi_2(x_1) & \cdots \\ \phi_1(x_2) & \phi_2(x_2) & \cdots \\ \vdots & \vdots & \ddots \end{bmatrix}
(13)

So:

  • XX emphasizes a standard design matrix,
  • Φ\Phi emphasizes a transformed or basis-expanded feature matrix.

But algebraically they play the same role.

Why this notation matters

This matrix form is the foundation for almost every major result in linear modeling.

Predictions become:

y^=Xw\hat{y}=Xw
(14)

Residuals become:

y−y^=y−Xwy-\hat{y}=y-Xw
(15)

Least-squares loss becomes:

∥y−Xw∥22\|y-Xw\|_2^2
(16)

Normal Equations become:

X⊤Xw=X⊤yX^\top Xw=X^\top y
(17)

Ridge regression becomes:

(X⊤X+λI)w=X⊤y(X^\top X+\lambda I)w=X^\top y
(18)

So the design matrix is the bridge between raw tabular data and the full linear-algebra machinery of regression.

Intercept handling

One very common convention is to include the intercept as a column of ones in the feature matrix.

For example:

X=[1x11x121x21x22⋮⋮⋮]X= \begin{bmatrix} 1 & x_{11} & x_{12}\\ 1 & x_{21} & x_{22}\\ \vdots & \vdots & \vdots \end{bmatrix}
(19)

Then the first element of ww is the intercept coefficient. This is convenient because the whole model stays inside one matrix multiplication. If the intercept is handled separately, then XX excludes the ones column and the scalar model is written with an extra constant term. Both conventions are valid, but the analyst must stay consistent.

DA/DS relevance

In DA/DS practice, everything from raw CSV data to a regression model passes through this matrix viewpoint.

It is central to:

  • feature engineering,
  • one-hot encoding,
  • interaction construction,
  • lag features,
  • polynomial regression,
  • linear-model APIs in Python and R,
  • large-scale optimization pipelines. A “features table” in production is, mathematically, a design matrix.

Flowchart

Start with rawobservationsand targetvaluesCreate one rowper observationCreate onecolumn perfeature or basisfunctionAssembledesign matrix Xor feature matrixPhiAssemblecoefficient vectorwAssemble targetvector yWrite linearmodel y = Xw +epsilonUse matrix formfor prediction,least squares,NormalEquations, andRidge

Real-world use

Where it is used

The feature matrix formulation is used whenever a linear model is fit to structured data, including regression, regularized regression, generalized linear models, and basis-expansion models.

Problem it solves

It solves the problem of organizing many observations and many features into one coherent mathematical object. Without matrix notation, linear modeling becomes cumbersome and harder to scale.

How it solves the problem

It stacks observations into rows and features into columns, then expresses the full model as one matrix equation. This enables compact notation, efficient computation, and clean derivations.

Example in a business

Suppose a demand model uses price, promotion flag, holiday indicator, region dummies, and lagged sales as predictors. Each product-date observation becomes one row. Each engineered predictor becomes one column. The resulting feature matrix is passed into the regression routine, which estimates coefficients, generates forecasts, and supports business interpretation.

The formula, piece by piece

Symbol Meaning
XX Design matrix, rows = observations, columns = features
Φ\Phi Feature matrix or basis-function matrix
yy Target vector
ww Coefficient vector
ε\varepsilon Error vector
XiX_i Row of the design matrix for observation ii
xijx_{ij} Value of feature jj for observation ii
y^\hat{y} Predicted target vector

Canonical matrix model:

y=Xw+εy = Xw + \varepsilon
(20)

Equivalent basis-function form:

y=Φw+εy = \Phi w + \varepsilon
(21)

Prediction equation:

y^=Xw\hat{y}=Xw
(22)

Residual equation:

y−y^=y−Xwy-\hat{y}=y-Xw
(23)

Observation-level row form:

yi=Xiw+εiy_i = X_i w + \varepsilon_i
(24)

Expanded vector form:

[y1y2⋮yn]=[−X1−−X2−⋮−Xn−]w+[ε1ε2⋮εn]\begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix} = \begin{bmatrix} - & X_1 & - \\ - & X_2 & - \\ & \vdots & \\ - & X_n & - \end{bmatrix} w + \begin{bmatrix} \varepsilon_1\\ \varepsilon_2\\ \vdots\\ \varepsilon_n \end{bmatrix}
(25)

The formula in words

Take the full dataset and put it into a matrix where each row is one observation and each column is one feature. Multiply that matrix by the coefficient vector ww. This gives the model’s predicted values for all observations at once. Then add the error vector ε\varepsilon, which represents the unexplained part of the target. So the model says: > the target vector equals a linear combination of the feature columns, plus error.

Critical Concepts

  • Rows are observations. One row corresponds to one case, record, or sample.
  • Columns are features. One column corresponds to one predictor or engineered transformation.
  • XX and Φ\Phi play the same algebraic role. Φ\Phi often emphasizes basis functions or transformed features.
  • The matrix form enables efficient computation. It is what makes least squares, gradients, and Normal Equations compact.
  • Intercept treatment must be explicit. Either add a ones column or keep the intercept separate.
  • Feature engineering changes columns, not the core structure. Raw variables, dummies, lags, and interactions all become matrix columns.
  • Dimension consistency is essential. Matrix multiplication only works when shapes align properly.

Step-by-step calculation (tiny example)

Suppose we have 3 observations and 2 features, plus an intercept column.

Let the design matrix be:

X=[125134146]X= \begin{bmatrix} 1 & 2 & 5\\ 1 & 3 & 4\\ 1 & 4 & 6 \end{bmatrix}
(26)

Let the coefficient vector be:

w=[102−1]w= \begin{bmatrix} 10\\ 2\\ -1 \end{bmatrix}
(27)

Step 1, compute prediction for observation 1 Row 1 is

X1=[125]X_1= \begin{bmatrix} 1 & 2 & 5 \end{bmatrix}
(28)

So:

y^1=X1w=1⋅10+2⋅2+5⋅(−1)=10+4−5=9\hat{y}_1 = X_1 w = 1\cdot 10 + 2\cdot 2 + 5\cdot (-1)=10+4-5=9
(29)

Step 2, compute prediction for observation 2 Row 2 is

X2=[134]X_2= \begin{bmatrix} 1 & 3 & 4 \end{bmatrix}
(30)

So:

y^2=1⋅10+3⋅2+4⋅(−1)=10+6−4=12\hat{y}_2 = 1\cdot 10 + 3\cdot 2 + 4\cdot (-1)=10+6-4=12
(31)

Step 3, compute prediction for observation 3 Row 3 is

X3=[146]X_3= \begin{bmatrix} 1 & 4 & 6 \end{bmatrix}
(32)

So:

y^3=1⋅10+4⋅2+6⋅(−1)=10+8−6=12\hat{y}_3 = 1\cdot 10 + 4\cdot 2 + 6\cdot (-1)=10+8-6=12
(33)

Step 4, write the full prediction vector

y^=Xw=[91212]\hat{y}=Xw= \begin{bmatrix} 9\\ 12\\ 12 \end{bmatrix}
(34)

Interpretation The matrix multiplication computes all observation-level linear predictions in one object.

Tiny plain-language reading

The feature matrix stores the full dataset in a structure that lets one coefficient vector generate all predictions at once.

Code examples

Beginner friendly Example [Python])

markdown
import numpy as np # Design matrix with intercept + 2 features
X = np.array([ [1, 2, 5], [1, 3, 4], [1, 4, 6]
], dtype=float) w = np.array([10, 2, -1], dtype=float) y_hat = X @ w print("Design matrix X:")
print(X)
print("Coefficient vector w:")
print(w)
print("Predictions Xw:")
print(y_hat)

Beginner friendly Example [R])

r
# Design matrix with intercept + 2 features
X <- matrix( c( 1, 2, 5, 1, 3, 4, 1, 4, 6 ), nrow = 3, byrow = TRUE
) w <- c(10, 2, -1) y_hat <- X %*% w cat("Design matrix X:\n")
print(X)
cat("Coefficient vector w:\n")
print(w)
cat("Predictions Xw:\n")
print(y_hat)

Final Professional Example [Python])

markdown
from __future__ import annotations from dataclasses import dataclass
from typing import Sequence import numpy as np RANDOM_SEED = 42 @dataclass(frozen=True)
class LinearModelDesign: feature_names: Sequence[str] design_matrix: Sequence[Sequence[float]] target_vector: Sequence[float] coefficients: Sequence[float] def validate(self) -> None: if len(self.design_matrix) == 0: raise ValueError("At least one observation is required.") if len(self.design_matrix) != len(self.target_vector): raise ValueError("design_matrix and target_vector must have the same number of rows.") if len(self.feature_names) != len(self.coefficients): raise ValueError("feature_names and coefficients must have the same length.") feature_count = len(self.coefficients) for row in self.design_matrix: if len(row) != feature_count: raise ValueError("Each design-matrix row must match the number of coefficients.") def predictions(design_matrix: np.ndarray, coefficients: np.ndarray) -> np.ndarray: return design_matrix @ coefficients def residuals(target_vector: np.ndarray, predicted_vector: np.ndarray) -> np.ndarray: return target_vector - predicted_vector def summarize_design(example: LinearModelDesign) -> None: example.validate() X = np.array(example.design_matrix, dtype=float) y = np.array(example.target_vector, dtype=float) w = np.array(example.coefficients, dtype=float) y_hat = predictions(X, w) residual_vector = residuals(y, y_hat) print("Feature matrix formulation summary") print("-" * 72) print("Feature names:") print(list(example.feature_names)) print("-" * 72) print("Design matrix X:") print(X) print("-" * 72) print("Target vector y:") print(y) print("Coefficient vector w:") print(w) print("-" * 72) print("Predictions Xw:") print(y_hat) print("Residuals y - Xw:") print(residual_vector) def main() -> None: # Demand-model style example: # Features: intercept, price, promotion_flag example = LinearModelDesign( feature_names=["intercept", "price", "promotion_flag"], design_matrix=[ [1.0, 10.0, 0.0], [1.0, 9.5, 1.0], [1.0, 11.0, 0.0], [1.0, 10.5, 1.0], ], target_vector=[120.0, 145.0, 112.0, 138.0], coefficients=[200.0, -8.0, 18.0], ) summarize_design(example) if __name__ == "__main__": main()

Self-check: mathematical verification

Re-derivation from observation-level equations

Suppose each observation satisfies:

yi=Xiw+εiy_i = X_i w + \varepsilon_i
(35)

for i=1,…,ni=1,\dots,n. Stack all nn equations vertically.

Then the target values become the vector:

y=[y1y2⋮yn]y= \begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix}
(36)

the row feature vectors become the matrix:

X=[−X1−−X2−⋮−Xn−]X= \begin{bmatrix} - & X_1 & - \\ - & X_2 & - \\ & \vdots & \\ - & X_n & - \end{bmatrix}
(37)

and the errors become:

ε=[ε1ε2⋮εn]\varepsilon= \begin{bmatrix} \varepsilon_1\\ \varepsilon_2\\ \vdots\\ \varepsilon_n \end{bmatrix}
(38)

So the full system is:

y=Xw+εy=Xw+\varepsilon
(39)

This matches the canonical formula exactly.

Prediction-form check

If the model excludes the error term and keeps only the fitted linear part, then predicted values are:

y^=Xw\hat{y}=Xw
(40)

This is a direct consequence of the model equation, so the prediction formula is correct.

Residual-form check Residuals are actual values minus predictions

y−y^=y−Xwy-\hat{y}=y-Xw
(41)

This is algebraically consistent.

Dimension check If

  • XX has shape n×pn\times p,
  • ww has shape p×1p\times 1, then:
XwXw
(42)

has shape n×1n\times 1, which matches the shape of yy.

Therefore:

y=Xw+εy=Xw+\varepsilon
(43)

is dimensionally valid.

Intercept-column check

If an intercept is included as the first coefficient, then adding a column of ones to XX makes the scalar model:

yi=w0+w1xi1+⋯+wpxip+εiy_i = w_0 + w_1x_{i1}+\cdots+w_px_{ip}+\varepsilon_i
(44)

equivalent to the row-matrix product Xiw+εiX_i w + \varepsilon_i. So the intercept-column interpretation is correct.

XX versus Φ\Phi notation check

If raw inputs are transformed through basis functions ϕj(x)\phi_j(x), then the resulting matrix can be written as Φ\Phi rather than XX.

The same linear form still applies:

y=Φw+εy=\Phi w+\varepsilon
(45)

So the equivalence in structural role is correct.

Verbal-to-algebra consistency

The verbal explanation says rows represent observations, columns represent features, and matrix multiplication gives all predictions together. The formulas and worked example confirm exactly that.

Structural verification

  • Opening restatement paragraph is present.
  • All required sections are present and non-empty.
  • Block equations use `
......
(46)

`.

  • Symbol-table entries use math mode such as $X$, $\Phi$, $y$, $w$, and $\varepsilon$.
  • Mermaid uses flowchart TD and node text in ["..."].
  • Three code blocks are present: Python beginner, R beginner, Python professional.
  • Links to Normal Equations, Ridge regression, regression, and feature engineering are explicitly included.
  • No placeholder text remains.

Common Pitfalls and Edge Cases

  • Mixing up rows and columns: Rows are observations, columns are features.
  • Forgetting the intercept convention: If the intercept is in the model, either include a ones column or handle it separately.
  • Confusing raw features with engineered features: Both appear as columns once the matrix is built.
  • Using inconsistent matrix shapes: XX, ww, and yy must align dimensionally.
  • Switching between XX and Φ\Phi without explanation: The notation is fine, but the meaning should stay explicit.
  • Assuming matrix notation is only cosmetic: It is what enables closed-form solutions, gradients, and scalable optimization.
  • Ignoring feature preprocessing: A formally correct matrix can still be practically poor if the features are badly constructed.
  • Thinking the matrix form is only for linear regression: It also underlies many generalized and regularized linear models.

References and Standard Results

  • Linear model matrix form, y=Xw+εy=Xw+\varepsilon
  • Equivalent basis-function form, y=Φw+εy=\Phi w+\varepsilon
  • Prediction equation, y^=Xw\hat{y}=Xw
  • Residual equation, y−Xwy-Xw
  • Row-wise observation interpretation of the design matrix
  • Column-wise feature interpretation of the design matrix
  • Intercept-column convention with leading ones
  • Role of the design matrix in Normal Equations and Ridge regression

#machine-learning#machine-learning-algorithms#linear-regression-and-maximum-likelihood-estimation-mle

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close