CSE-41XX
CS-4125 ML

Lecture 03: Linear Algebra Review

A comprehensive review of essential linear algebra concepts for machine learning, including matrices, vectors, matrix operations, inverses, transposes, eigenvalues, eigenvectors, and key properties for probabilistic models (GDA, GMM, EM).

Matrices - Overview

A matrix is a rectangular array of numbers written between square brackets.

  • 2D Array: Matrices are represented as two-dimensional arrays.
  • Naming: Named using capital letters (e.g., A,B,X,YA, B, X, Y).
  • Dimensions: The dimensions of a matrix are defined as [Rows×Columns][\text{Rows} \times \text{Columns}].
    • Count starting from top-left, down to bottom-left, and across to bottom-right.
    • Rr×c\mathbb{R}^{r \times c} denotes a matrix with rr rows and cc columns.

Matrix dimensions example

  • The example matrix above is a [4×2][4 \times 2] matrix.

Matrix Elements

  • Ai,j=entry in ith row and jth columnA_{i,j} = \text{entry in } i^{\text{th}} \text{ row and } j^{\text{th}} \text{ column}.

Matrix element indexing example

Matrices provide a powerful way to organize, index, and access large volumes of data.


Vectors - Overview

A vector is a matrix with nn rows and 1 column (n×1n \times 1 matrix).

  • Naming: Usually denoted by lower-case letters (e.g., v,x,yv, x, y).
  • Dimensions: Has nn rows and 1 column.

Vector example

  • The example above is a 4-dimensional vector, referred to as belonging to R4\mathbb{R}^4.

Vector Elements

  • vi=ith element of the vectorv_i = i^{\text{th}} \text{ element of the vector}.
  • Indexing:
    • Vectors can be 0-indexed (common in C/C++) or 1-indexed (common in MATLAB).
    • In standard mathematics, 1-indexing is most common.
    • In machine learning implementations, 0-indexing is frequently useful.
    • Normally assume 1-indexed vectors unless explicitly specified as 0-indexed.

Matrix Manipulation

Addition

  • Add corresponding elements one at a time.
  • Can only add matrices of the same dimensions.
  • Addition yields a new matrix of the exact same dimensions as the operands.

Matrix addition example

Multiplication by Scalar

  • Scalar: A real number.
  • Multiply every element of the matrix by the scalar.
  • Generates a matrix of the same dimensions as the original matrix.

Scalar multiplication example

Division by Scalar

  • Equivalent to multiplying the matrix by 1scalar\frac{1}{\text{scalar}} (e.g., multiplying by 14\frac{1}{4}).
  • Each element is divided by the scalar.

Combination of Operands

  • Standard order of operations applies: evaluate scalar multiplications first before additions.

Combination of scalar operations

Matrix-Vector Multiplication

Multiplying a [3×2][3 \times 2] matrix by a [2×1][2 \times 1] vector yields a [3×1][3 \times 1] vector.

  • General Rule: Multiplying an [a×b][a \times b] matrix by a [b×c][b \times c] matrix/vector yields an [a×c][a \times c] result.
  • Procedure:
    1. Take the elements of the vector and multiply them with the corresponding elements of the matrix's first row, then sum the results to get the first element of the new vector.
    2. Repeat the step for the second row of the matrix to get the second element.
    3. Repeat for the final row to complete the new vector.

Matrix-vector multiplication visual

Detailed Rules

For A×x=yA \times x = y:

  • AA is an m×nm \times n matrix.
  • xx is an n×1n \times 1 vector.
  • The inner dimensions (nn) must match between the matrix and vector.
  • The result yy is an mm-dimensional vector (m×1m \times 1).
  • To calculate yiy_i, multiply the elements of AA's ithi^{\text{th}} row by all the corresponding elements of vector xx and sum them up.

Vectorization Trick for Machine Learning

Suppose we have a dataset with four samples and a hypothesis function: hθ(x)=40+0.25xh_\theta(x) = -40 + 0.25x

Instead of computing predictions individually:

  1. Represent your dataset as a data matrix.
  2. Put parameters (θ0,θ1\theta_0, \theta_1) into a column vector.
  3. Compute predictions in a single matrix-vector multiplication: Prediction=Data Matrix×Parameters\text{Prediction} = \text{Data Matrix} \times \text{Parameters}

Vectorization trick for hypothesis predictions

  • Key Step: Add an extra column of 1s to the data matrix (representing x0=1x_0 = 1) so that the intercept parameter θ0\theta_0 is properly calculated.
  • Efficiency: This matrix formulation is computationally far more efficient than explicit for loops and produces cleaner, more concise code when using optimized linear algebra libraries.

Matrix-Matrix Multiplication

General Concept

  1. Step through the second matrix one column at a time.
  2. Multiply each column vector of the second matrix by the entire first matrix, generating a column vector for each step.
  3. Combine (concatenate) the resulting column vectors side-by-side to form the final matrix.

Details

For A×B=CA \times B = C:

  • AA is [m×n][m \times n].
  • BB is [n×o][n \times o].
  • CC is [m×o][m \times o]. (Note: matrix-vector multiplication is just the special case where o=1o = 1).
  • Multiplication is only valid if the number of columns in AA matches the number of rows in BB.

Summary

The ithi^{\text{th}} column of matrix CC is obtained by multiplying matrix AA with the ithi^{\text{th}} column of matrix BB.

Step-by-Step Example

Given A×BA \times B:

Matrix-matrix multiplication setup

  1. Multiply matrix AA by the 1st column vector of BB to get the 1st column of the result.
  2. Multiply matrix AA by the 2nd column vector of BB to get the 2nd column of the result.

Matrix-matrix multiplication step-by-step

  • Multiplying a [2×3][2 \times 3] matrix by a [3×2][3 \times 2] matrix produces a [2×2][2 \times 2] matrix.

Implementation and Applications

Vectorizing Multiple Hypotheses

Consider a housing price dataset where we want to evaluate three competing hypotheses on the same data:

  • We have 4 house sizes (samples) and want to compute predictions under 3 different hypotheses simultaneously.
  • Create a [4×2][4 \times 2] data matrix (house sizes with an extra column of 1s).
  • Create a [2×3][2 \times 3] parameter matrix where each column represents the parameters (θ0,θ1)(\theta_0, \theta_1) of one hypothesis.

Vectorized multiple hypothesis predictions

Benefits:

  • Simultaneously evaluates 3 hypotheses across 4 houses, producing all 12 predictions in a single matrix multiplication.
  • Leverages optimized parallel linear algebra routines for maximum computational efficiency.

Matrix Multiplication Properties

While matrix multiplication is a powerful operation, it behaves differently from scalar multiplication in key ways.

Commutativity

  • For scalars: 3×5=5×33 \times 5 = 5 \times 3 (commutative).
  • For matrices: A×BB×AA \times B \neq B \times A in general.
  • Matrix multiplication is NOT commutative.

Associativity

  • For scalars: (3×5)×2=3×(5×2)=30(3 \times 5) \times 2 = 3 \times (5 \times 2) = 30.
  • For matrices: Matrix multiplication IS associative. A×(B×C)=(A×B)×CA \times (B \times C) = (A \times B) \times C

Identity Matrix

  • In scalar arithmetic, 11 is the multiplicative identity (1z=z1 \cdot z = z).
  • In matrix algebra, the Identity Matrix is denoted by II (or In×nI_{n \times n}).

Identity matrix examples

  • Properties of II:
    • Square matrix (n×nn \times n).
    • Contains 11s along the main diagonal and 00s everywhere else.
    • A [1×1][1 \times 1] identity matrix is simply [1][1].
  • Multiplying any matrix AA by an appropriate identity matrix returns AA:
    • AIn×n=AA \cdot I_{n \times n} = A (where AA is [m×n][m \times n])
    • Im×mA=AI_{m \times m} \cdot A = A (where AA is [m×n][m \times n])
  • Exception to Commutativity: Matrix multiplication is commutative only when one of the matrices is the identity matrix (AI=IA=AA \cdot I = I \cdot A = A).

Inverse and Transpose Operations

Matrix Inverse

Concept in Real Numbers

  • The number 11 is the multiplicative identity.
  • The inverse of a number xx is x1=1xx^{-1} = \frac{1}{x}, such that xx1=1x \cdot x^{-1} = 1 (e.g., 331=13 \cdot 3^{-1} = 1).
  • Not all numbers have an inverse: 00 does not have a multiplicative inverse.

Matrix Inverse Definition

  • If AA is an m×mm \times m matrix, its inverse is A1A^{-1} such that: AA1=IA \cdot A^{-1} = I
  • Square Matrices Only: Only square matrices (m×mm \times m) can have an inverse.

Matrix inverse example

Singular and Degenerate Matrices

  • Matrices filled with all zeros do not have an inverse.
  • Matrices that do not have an inverse are called singular or degenerate matrices (intuitively, matrices that are "too close to 0" or lack full rank).

Matrix Transpose

Concept

Given an [m×n][m \times n] matrix AA, the transpose ATA^T is an [n×m][n \times m] matrix formed by swapping rows and columns.

Procedure

  • 1st row of AA becomes the 1st column of ATA^T.
  • 2nd row of AA becomes the 2nd column of ATA^T.
  • In general, for B=ATB = A^T: Ai,j=Bj,iA_{i,j} = B_{j,i}

Matrix transpose example


Eigenvalues and Eigenvectors

Definition and Geometric Intuition

For an n×nn \times n square matrix AA, a non-zero vector vRnv \in \mathbb{R}^n is an eigenvector if multiplying it by AA yields a scaled version of itself:

Av=λvAv = \lambda v

  • AA: An n×nn \times n transformation matrix.
  • vv: The eigenvector (v0v \neq \mathbf{0}).
  • λ\lambda: The eigenvalue (a scalar scaling factor).

Geometric Meaning

Most linear transformations both rotate and stretch vectors. Eigenvectors are special invariant directions that do not rotate under the transformation—they only stretch, shrink, or flip along the same line:

  • λ>1\lambda > 1: The vector stretches.
  • 0<λ<10 < \lambda < 1: The vector shrinks.
  • λ<0\lambda < 0: The vector flips in the opposite direction.
  • λ=0\lambda = 0: The vector collapses to zero (vv lies in the null space of AA).

Characteristic Equation

To compute eigenvalues, solve the characteristic equation: (AλI)v=0    det(AλI)=0(A - \lambda I)v = \mathbf{0} \implies \det(A - \lambda I) = 0


Special Eigenvalue Properties in Theoretical Machine Learning

In probabilistic machine learning algorithms such as Gaussian Discriminant Analysis (GDA), Gaussian Mixture Models (GMM), and the Expectation-Maximization (EM) algorithm, eigenvalues play a foundational role in analyzing covariance matrices (Σ\Sigma) and Hessian matrices (HH).

1. Covariance Matrices & Positive Definiteness

Any empirical covariance matrix Σ\Sigma is symmetric (ΣT=Σ\Sigma^T = \Sigma) and Positive Semi-Definite (PSD) (Σ0\Sigma \succeq 0):

  • Real Eigenvalues: All eigenvalues of a symmetric matrix are strictly real (λiR\lambda_i \in \mathbb{R}).
  • Non-Negative Eigenvalues: For PSD matrices, λi0\lambda_i \ge 0.
  • Strictly Positive Definite (PD): If Σ0\Sigma \succ 0, then every eigenvalue is strictly positive (λi>0\lambda_i > 0), guaranteeing that variance along any arbitrary projection is strictly positive.

2. Determinant as Volume (det(Σ)=i=1dλi\det(\Sigma) = \prod_{i=1}^d \lambda_i) & The EM Singularity

The determinant of a covariance matrix is the product of its eigenvalues: det(Σ)=i=1dλi\det(\Sigma) = \prod_{i=1}^d \lambda_i

  • Gaussian Normalization: Appears in the denominator of the multivariate Gaussian probability density function: p(x;μ,Σ)=1(2π)d/2Σ1/2exp(12(xμ)TΣ1(xμ))p(x; \mu, \Sigma) = \frac{1}{(2\pi)^{d/2}|\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x-\mu)\right)
  • EM Singularity Problem in GMMs: If a Gaussian component collapses onto a single data point during EM optimization, the variance along one dimension shrinks to zero (λmin0\lambda_{\min} \to 0). Consequently, det(Σ)0\det(\Sigma) \to 0, causing Σ1/2|\Sigma|^{-1/2} \to \infty and the likelihood objective to diverge to ++\infty.

3. Precision Matrix (Ω=Σ1\Omega = \Sigma^{-1}) & Conditional Independence

If Σ\Sigma has eigenvectors u1,,udu_1, \dots, u_d with eigenvalues λ1,,λd\lambda_1, \dots, \lambda_d, then the precision matrix Ω=Σ1\Omega = \Sigma^{-1} shares the exact same eigenvectors uiu_i, but with inverted eigenvalues: λ(Σ1)i=1λi\lambda(\Sigma^{-1})_i = \frac{1}{\lambda_i}

  • Checking Conditional Independence: In a multivariate Gaussian distribution XN(μ,Σ)X \sim \mathcal{N}(\mu, \Sigma), two variables XiX_i and XjX_j are conditionally independent given all other variables if and only if their corresponding off-diagonal entry in the precision matrix is zero: XiXjXrest    (Σ1)ij=0X_i \perp X_j \mid X_{\text{rest}} \iff (\Sigma^{-1})_{ij} = 0
    • Contrast this with marginal independence, which holds if and only if the covariance entry is zero: XiXj    Σij=0X_i \perp X_j \iff \Sigma_{ij} = 0.
  • Mahalanobis Distance: In the quadratic form (xμ)TΣ1(xμ)=i=1d1λi(uiT(xμ))2(x-\mu)^T \Sigma^{-1} (x-\mu) = \sum_{i=1}^d \frac{1}{\lambda_i} (u_i^T (x-\mu))^2, directions with large variance (λi0\lambda_i \gg 0) receive smaller weight penalties, naturally normalizing distances across features with varying scales.

4. Spectral Decomposition & Geometric Contours of Gaussians

By the Spectral Theorem, any symmetric matrix can be decomposed into: Σ=UΛUT=i=1dλiuiuiT\Sigma = U \Lambda U^T = \sum_{i=1}^d \lambda_i u_i u_i^T where U=[u1,,ud]U = [u_1, \dots, u_d] is an orthogonal matrix of eigenvectors (UTU=IU^T U = I) and Λ=diag(λ1,,λd)\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_d).

  • Iso-Density Contours: The level curves of equal probability {x:(xμ)TΣ1(xμ)=c2}\{x : (x-\mu)^T \Sigma^{-1} (x-\mu) = c^2\} form an ellipsoid.
  • Principal Axes: The axes of the ellipsoid point along the eigenvectors uiu_i.
  • Axis Radii: The half-lengths of the semi-axes are proportional to λi\sqrt{\lambda_i} (the standard deviation along that principal direction).

5. Trace as Total Variance (Tr(Σ)=i=1dλi\operatorname{Tr}(\Sigma) = \sum_{i=1}^d \lambda_i)

The trace of a square matrix equals the sum of its diagonal entries, which also equals the sum of its eigenvalues: Tr(Σ)=i=1dΣii=i=1dλi\operatorname{Tr}(\Sigma) = \sum_{i=1}^d \Sigma_{ii} = \sum_{i=1}^d \lambda_i

  • In feature analysis and PCA, Tr(Σ)\operatorname{Tr}(\Sigma) measures the total variance across all features in the dataset.

6. Regularization (Jitter / Ridge) Shifts Eigenvalues

When applying shrinkage or diagonal jittering to stabilize covariance estimation: Σreg=Σ+ϵI\Sigma_{\text{reg}} = \Sigma + \epsilon I

  • The eigenvectors uiu_i remain completely unchanged.
  • Every eigenvalue is shifted by a constant: λiλi+ϵ\lambda_i \to \lambda_i + \epsilon.
  • Practical Benefit: Even if Σ\Sigma is singular or rank-deficient (λmin=0\lambda_{\min} = 0), Σreg\Sigma_{\text{reg}} is guaranteed to be strictly positive definite with minimum eigenvalue ϵ\epsilon, ensuring numerical stability when taking matrix inverses in GDA and EM.

7. Generalized Eigenvalue Problems in LDA / GDA

In Linear Discriminant Analysis (and Gaussian Discriminant Analysis with shared covariance), finding the optimal linear projection ww that maximizes the ratio of between-class variance SBS_B to within-class variance SWS_W involves maximizing the Rayleigh Quotient: maxwwTSBwwTSWw\max_w \frac{w^T S_B w}{w^T S_W w} Taking the gradient and setting it to zero yields the generalized eigenvalue problem: SBw=λSWw    SW1SBw=λwS_B w = \lambda S_W w \implies S_W^{-1} S_B w = \lambda w The optimal projection direction ww is the eigenvector corresponding to the largest eigenvalue λmax\lambda_{\max} of SW1SBS_W^{-1} S_B.

8. Hessians, Curvature, and Convexity in Maximum Likelihood

In optimization and Maximum Likelihood Estimation (MLE):

  • The curvature of an objective function (θ)\ell(\theta) is governed by its Hessian matrix H=2(θ)H = \nabla^2 \ell(\theta).
  • If all eigenvalues of HH are strictly negative (λi<0    H0\lambda_i < 0 \iff H \prec 0), the objective function is strictly concave. This guarantees that any stationary point found by gradient ascent or Newton's method is the unique global maximum (e.g., Logistic Regression, GLMs).

Eigenvalues in Machine Learning: Quick Reference

PropertyMathematical FormML Context & Application
Positivityλi>0    Σ0\lambda_i > 0 \iff \Sigma \succ 0Guarantees non-zero variance and invertible covariance in GDA/GMMs
Determinantdet(Σ)=i=1dλi\det(\Sigma) = \prod_{i=1}^d \lambda_iNormalization factor in Gaussian PDF; λi0\lambda_i \to 0 causes EM divergence
Precisionλ(Σ1)i=1λi\lambda(\Sigma^{-1})_i = \frac{1}{\lambda_i}Scales Mahalanobis distance along principal axes
Conditional Independence(Σ1)ij=0    XiXjXrest(\Sigma^{-1})_{ij} = 0 \iff X_i \perp X_j \mid X_{\text{rest}}Zero entries in precision matrix indicate conditional independence
Spectral FormΣ=UΛUT\Sigma = U \Lambda U^TDefines orientation (uiu_i) and semi-axis lengths (λi\sqrt{\lambda_i}) of Gaussian density ellipsoids
TraceTr(Σ)=i=1dλi\operatorname{Tr}(\Sigma) = \sum_{i=1}^d \lambda_iTotal variance in the dataset
RegularizationΣ+ϵI    λi+ϵ\Sigma + \epsilon I \implies \lambda_i + \epsilonJittering / Ridge shrinkage to ensure numerical invertibility
Rayleigh RatioSW1SBw=λwS_W^{-1} S_B w = \lambda wLDA / GDA optimal discriminant projection vector
Hessian Curvatureλi(H)<0    H0\lambda_i(H) < 0 \iff H \prec 0Strict concavity in MLE, guaranteeing unique global optima

On this page