Multidimensional scaling and matrix factorisation
How Multidimensional Scaling (MDS) recovers low-dimensional point coordinates from pairwise distances and how matrix factorisation (SVD, PCA, NMF) gives optimal low-rank approximations used in visualization, dimensionality reduction and recommender systems.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Multidimensional scaling (MDS) and matrix factorisation are used by engineers to convert pairwise similarities or a high‑dimensional dataset into a low‑dimensional representation. This is essential for visualization (showing structure on 2D/3D), for reducing storage and compute by low‑rank approximation, and for building recommender systems via latent factors.
Key ideas
Start from first principles and basic definitions.
- Point configuration: a set of n points x1, x2, ..., xn in R^p. Each xi is a column vector. (Defined here so you know what we try to recover.)
- Euclidean distance: the distance between xi and xj is d_ij = ||xi - xj|| = sqrt(sum_k (x_{ik} - x_{jk})^2). This is in SI units of the coordinates (e.g., metres) if coordinates are in metres.
- Distance matrix: the matrix D with entries D_ij = d_ij. D is symmetric, with zeros on the diagonal.
- Classical (metric) MDS goal: given D (pairwise distances), find a set of points y1,...,yn in R^m (m«p, often m=2) whose pairwise Euclidean distances approximate D. The solution is unique up to rigid motions (translation, rotation, reflection).
Derivation (classical MDS) — how distances give inner products and coordinates:
- Let X be the n×p data matrix with row i equal to xi^T. Let the centroid be x_mean = (1/n) sum_i xi. Define centered coordinates Xc = X - 1 x_mean^T (so each row centered), where 1 is the n×1 vector of ones.
- The Gram (inner‑product) matrix is B = Xc Xc^T. B_ij = <xi - x_mean, xj - x_mean>.
- Relation between squared distances and B: for any i,j, d_{ij}^2 = ||xi - xj||^2 = ||xi||^2 + ||xj||^2 - 2 <xi, xj>. Using centering and linear algebra, double‑centering the squared distances recovers B exactly: B = -1/2 J (D .^ 2) J, where J = I_n - (1/n) 11^T, and (D .^ 2) is the entrywise square of D. (Derive by writing squared distances in terms of norms and inner products, then apply J on both sides to remove dependence on the unknown norms.)
- Compute eigen‑decomposition of B: B = V Λ V^T, where Λ = diag(λ1, ..., λn) with λ1 ≥ λ2 ≥ ... ≥ λn. If we want m dimensions, take the top m positive eigenvalues and eigenvectors. The coordinates (rows of Y) are given by Y = V_m Λ_m^{1/2}. This yields Y Y^T = V_m Λ_m V_m^T = B_m, the rank‑m approximation to B.
- If some of the top eigenvalues are negative because the original D does not come from Euclidean distances in any p-space, classical MDS gives the best Euclidean approximation in the least squares sense; negative eigenvalues indicate non‑Euclidean structure and need special handling (e.g., ignore negatives or use non‑metric MDS).
Other MDS variants:
- Non‑metric MDS: preserves rank order of distances; solves a nonlinear optimisation that minimises a stress function (see Formulas). Useful when only ordinal information is reliable.
- Sammon mapping: a weighted stress emphasising small distances.
Connection to matrix factorisation:
- Classical MDS is a matrix factorisation of the centered Gram matrix: B ≈ Y Y^T (low‑rank). This is equivalent to PCA on X: the principal components of centered data give the same embedding (up to scaling) as classical MDS when input is Euclidean distances computed from X.
- Singular Value Decomposition (SVD): any real matrix A (n×p) can be factorised as A = U Σ V^T. Truncating Σ to top r gives the best rank‑r approximation in Frobenius norm (Eckart–Young theorem). This is the standard low‑rank factorisation used in recommender systems and compression.
- Non‑negative matrix factorisation (NMF): decomposes A ≈ W H with W,H ≥ 0; used when data are non‑negative and interpretability of parts is desired.
Important theorems (exact statements):
- Eckart–Young theorem: Let A be any n×p matrix with SVD A = U Σ V^T. The best rank‑r approximation A_r (minimising ||A - B||_F over matrices B of rank ≤ r) is A_r = U_r Σ_r V_r^T, where Σ_r contains the top r singular values. Assumption: singular values sorted nonincreasingly.
- Classical MDS correctness: If D is a Euclidean distance matrix coming from X in R^p, then B = -1/2 J (D .^ 2) J equals Xc Xc^T, and the eigen‑decomposition recovers Xc up to orthogonal transformation. Assumption: D must be exact Euclidean distances.
Diagrams to draw for exam answers:
- For derivation: draw n points in 2D, label x1,x2,x3; show D entries between pairs. Draw vector from each xi to centroid x_mean and show centered vectors. Label B as matrix of dot products between centered vectors.
- For SVD: draw rectangle A, arrows to U, Σ, V^T showing decomposition into rotations (U,V^T) and scaling (Σ). For rank‑1 approx, show one principal direction capturing most of variance.
Formulas
d_{ij} = ||x_i - x_j|| — d_{ij} is Euclidean distance between points i and j. Units = units of coordinates (e.g., m). Applies for Euclidean space.
D = [d_{ij}] — distance matrix, symmetric, zeros on diagonal.
J = I_n - (1/n) 11^T — centering matrix. Unitless. Used to center row/column means. J is symmetric and idempotent (J = J^T = J^2) and J 1 = 0.
B = -1/2 · J · (D .^ 2) · J — Gram (inner‑product) matrix recovered by double centering the squared distances. B_ij = <x_i - x_mean, x_j - x_mean>. Unit = (coordinate units)^2.
B = V Λ V^T — eigenvalue decomposition of B. V columns are orthonormal eigenvectors, Λ diagonal eigenvalues. Applies when B is symmetric.
Y = V_m · Λ_m^{1/2} — coordinates in m dimensions; rows of Y are the m‑dimensional embeddings. Units = same as original coordinates.
stress = sqrt( sum_{i<j} (d_{ij} - δ_{ij})^2 / sum_{i<j} δ_{ij}^2 ) — Kruskal's Stress-1; δ_{ij} are distances in the embedding (δ_{ij} = ||y_i - y_j||), d_{ij} are the original dissimilarities (or their monotone disparities in non-metric MDS).
E_Sammon = (1 / sum_{i<j} d_{ij}) · sum_{i<j} ((d_{ij} - δ_{ij})^2 / d_{ij}) — Sammon mapping objective; weights small original distances more strongly.
A = U Σ V^T — SVD of A (n×p). U (n×n) and V (p×p) orthogonal, Σ diagonal with nonnegative singular values. Units same as A entries.
A_r = U_r Σ_r V_r^T — best rank‑r approximation to A (Eckart–Young theorem). Error measured in Frobenius norm.
Worked examples
Example 1 — Classical MDS for three points (numerical, simple, units metres): Problem: Distances between three points A, B, C are AB = 3 m, AC = 4 m, BC = 5 m. Find coordinates in 2D consistent with these distances. Steps:
- Place A at (0,0) (choose origin). Place B on x‑axis at (3,0) because AB = 3 m.
- Let C = (x,y). From AC = 4 m: x^2 + y^2 = 16. From BC = 5 m: (x - 3)^2 + y^2 = 25.
- Subtract the first equation from the second: (x-3)^2 - x^2 = 25 - 16 = 9. Expand: x^2 -6x +9 - x^2 = -6x +9 = 9 ⇒ x = 0.
- Then y^2 = 16 ⇒ y = ±4. Choose y = 4 m for orientation. Answer: Coordinates A = (0.000 m, 0.000 m), B = (3.000 m, 0.000 m), C = (0.000 m, 4.000 m). Final: A(0,0), B(3,0), C(0,4) (units metres). Note: classical MDS would recover these points up to rotation and translation.
Example 2 — Matrix factorisation (SVD) and rank‑1 approximation (no units): Problem: A = [[3,1],[1,3]]. Find SVD and best rank‑1 approximation. Steps:
- A is symmetric. Eigenvalues λ satisfy det(A - λI) = 0. Compute trace = 6, det = 8 ⇒ λ^2 -6λ +8 = 0 ⇒ λ = 4, 2.
- Normalised eigenvectors: for λ=4, v1 = (1/√2)[1,1]^T. For λ=2, v2 = (1/√2)[1,-1]^T.
- Since A is SPD, SVD = eigen‑decomp: U = V = [v1 v2], Σ = diag(4,2). So A = U Σ V^T.
- Best rank‑1 approximation keeping top singular value 4 is A1 = 4 · v1 v1^T = 4 · (1/2) [[1,1],[1,1]] = [[2,2],[2,2]].
- Approximation error (Frobenius norm): ||A - A1||_F^2 = (3-2)^2 + (1-2)^2 + (1-2)^2 + (3-2)^2 = 4 ⇒ ||A - A1||_F = 2. Answer: A1 = [[2,2],[2,2]], error = 2 (Frobenius norm).
Example 3 — Short non‑metric MDS illustration (theory model answer): Question: Explain how you would use non‑metric MDS if you only trust the order of dissimilarities (noisy scales). Answer model: Set up an embedding Y in R^m, compute fitted distances δ_{ij}=||y_i - y_j||, choose a monotone nondecreasing transformation f of original dissimilarities d_{ij} (isotonic regression) to produce fitted dissimilarities f(d_{ij}). Minimise stress = sum_{i<j} w_{ij} (f(d_{ij}) - δ_{ij})^2 over Y and the monotone f using iterative optimisation (majorisation or SMACOF). Report final stress and provide 2D scatter of Y. (State convergence criteria and initialisation using classical MDS.)
Common mistakes
- Assuming the embedding is unique; forgetting that solutions are determined only up to orthogonal transforms and translation.
- Ignoring negative eigenvalues in B and attempting to take square roots of negative numbers without explanation.
- For SVD: keeping singular vectors but not scaling by singular values Σ when reconstructing A_r (mixing U and Σ incorrectly). For classical MDS: forgetting to use Λ^{1/2} when forming coordinates Y = V_m Λ_m^{1/2}.
For your exam
Types of questions you will get:
- Definitions and short notes: define MDS, Gram matrix, centering matrix J, stress.
- Derivations: show B = -1/2 J D^2 J from squared distances; derive coordinates Y = V_m Λ_m^{1/2}.
- Numericals: given small distance matrix, compute centred Gram matrix and give 2D embedding (for n=3 the hand computation shown above is enough). Show units.
- Programs: implement classical MDS in Python (use eigen decomposition or SVD of B) or apply truncated SVD for low‑rank approximation.
- Diagrams: draw points, centroid, centred vectors, and show J operation as subtracting row/column means. What a full‑marks answer includes: clear definitions, correct derivation steps with linear algebra justifying each operation, correct handling of sign of eigenvalues, numeric computation with units and final embeddings stated up to rigid transform, and mention of alternatives (non‑metric MDS, Sammon mapping) if asked.
Quick check
- In classical MDS the matrix you diagonalise is the distance matrix D, the squared distance matrix D.^2, the centered Gram matrix B, or the centering matrix J? 2) What does a negative eigenvalue of B indicate? 3) State the Eckart–Young theorem in one line. Answers: B; that D is not exactly Euclidean (non‑Euclidean structure) or embedding requires imaginary coordinates; the best rank‑r approximation to A in Frobenius norm is given by truncating its SVD to the top r singular values (A_r = U_r Σ_r V_r^T).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?