Dimensionality reduction can change a geometry deliberately or preserve a geometry approximately. Metric learning uses information about similarity to decide which directions should matter. Random projection uses an independently sampled map to preserve the distances of a fixed finite collection of points. Both approaches apply linear maps, but their assumptions and success criteria are different. Writing each map as a matrix makes those differences precise.
Motivation
Euclidean distance adds squared coordinate differences with equal weights. That choice has consequences when measurements use different units, or when some features are informative for a classification task and others are not. A learned geometry can rescale directions, combine correlated features, and identify variations that should be ignored for the task.
Random projection addresses another question. Suppose the existing Euclidean geometry is useful, but each point has thousands of coordinates. We want a shorter representation whose distances remain close on the objects under consideration. The target is simultaneous accuracy on finitely many pairwise differences, rather than an invertible transformation of the entire original space. This distinction permits a substantial reduction in dimension.
Definitions
Definition
Mahalanobis geometry and squared distances
For a symmetric positive semidefinite matrix , define
If with , then
The squared form is convenient for optimization. The distance , including its square root, is the object whose triangle inequality we will establish.
A spectral decomposition supplies such a factor : take square roots of the positive eigenvalues and retain their eigenvectors. The smallest number of rows needed is . A low-rank learned matrix thus describes both a geometry and a representation with fewer coordinates.
Proposition
Positive semidefinite gives a pseudometric; positive definite gives a metric
For , the function is a pseudometric: it is nonnegative, symmetric, zero on identical points, and satisfies the triangle inequality. It is a metric on all of exactly when .
If is singular, distinct points have zero distance precisely when their difference belongs to .
The proof follows the linear representation. Euclidean norms give
Also vanishes exactly when . A positive definite matrix has no nonzero null direction; a singular positive semidefinite matrix does. On a particular finite set, a singular matrix can still separate every distinct pair if none of their differences lies in that nullspace. Global invertibility and separation of the observed objects are therefore different requirements.
Learning from labeled neighbors
Choose a fixed set of same-class target neighbors, excluding the anchor itself, for each anchor . Let contain indices with a different class label, and take . One large-margin nearest-neighbor objective is
where . The first term pulls each target neighbor closer. The hinge term penalizes an impostor whose squared distance falls below the target's squared distance plus a margin of one. The margin is expressed in squared distances; it is not a difference of ordinary Euclidean lengths.
For , define . Then
This expression is linear in . With the target sets fixed, every hinge is the maximum of zero and an affine function of , so it is convex. Adding these terms preserves convexity, and the positive semidefinite constraint defines a convex feasible set. Thus this version of the learning problem is convex in the matrix variable.
The same claim can be seen using one slack variable per triplet. Require and , then minimize the pull term plus . All these inequalities are affine in and the slacks. Keeping the neighbor sets fixed is essential to this argument; changing them changes which terms enter the objective.
Rank and the choice of optimization variable
Positive semidefiniteness alone does not force a low-rank answer. Imposing selects a generally nonconvex set. Alternatively, optimizing and setting enforces that rank limit automatically, but the objective as a function of is generally nonconvex. An equivalent representation of distances need not give an equivalent convex optimization problem. The example below makes this failure visible even with a single scalar factor.
Interpreting a learned geometry
For a diagonal matrix, the squared distance weights each coordinate separately. A full matrix also contains cross terms:
Those terms allow the learned map to identify combinations of coordinates. For example, the form measures change in a difference and ignores equal changes in both coordinates. Its negative off-diagonal entries are compatible with a positive semidefinite matrix. The eigenvectors specify directions being measured; eigenvalues specify their squared-distance weights. Looking at both makes a fitted matrix easier to interpret than looking only at individual diagonal entries.
Units can be handled explicitly through a change of coordinates. If for an invertible matrix , the same geometry has matrix
Substitution shows that the quadratic difference is unchanged. This congruence also preserves positive semidefiniteness and rank. Rescaling the input table while keeping the matrix fixed, in contrast, changes the geometry being fitted. The feature representation and the learned matrix should therefore be interpreted together.
The pull and push terms also determine scale. At all points collapse, the pull term is zero, and every included hinge equals one. With that collapsed solution is optimal. A positive push weight introduces the margin cost and trades it against the target distances. The constant one in the margin fixes a squared-distance scale; multiplying by a scalar does not generally preserve the objective. This differs from simply rotating the rows of a factor, which preserves and all learned distances.
Gaussian random projection
Now fix the points independently of the random map. Sample all entries of independently with
The variance is , so the standard deviation used to generate entries is . The rows need not be orthogonal. For any fixed nonzero difference , independent rows give
Consequently . Equality in expectation is a statement about repeated draws of the map for that fixed difference. It does not say that a particular draw preserves even one distance exactly.
The normalized squared-length ratio has variance , since a chi-square variable with degrees of freedom has variance . This gives a first indication of concentration as the projected dimension grows. The exponential bound below is stronger: it makes simultaneous control over a growing collection of differences possible with only logarithmic dependence on the number of points.
Theorem
A simultaneous finite-set projection bound
Let , , and let fixed points be independent of . Define
The probability that any pair violates
is at most . In particular, the sufficient choice
makes all pairwise bounds hold with probability at least . Here denotes the natural logarithm.
The guarantee uses squared lengths. The corresponding ordinary lengths lie between and times their original values. Zero differences remain zero under every linear map and require no probabilistic estimate.
From one difference to all pairs
Proof
Chi-square concentration and the union bound
For , its moment generating function is for . Applying the exponential Markov inequality and optimizing gives
For , integration of and on yields
Both tails are therefore bounded by , giving a two-sided bound of for one nonzero difference. Apply this result to the at most unordered pairs and add their failure probabilities. The pairwise events need not be independent for this union bound. Solving gives the displayed dimension choice.
Since in this interval, a sufficient dimension has order , with no original dimension in that expression. Fixing a failure probability below one gives a positive probability of success, which proves that a suitable map exists. This is the Johnson–Lindenstrauss existence conclusion. The explicit probability bound additionally specifies how likely a fresh Gaussian draw is to succeed.
If this sufficient dimension is larger than , the estimate does not certify dimensional reduction for those parameters. It does not prove that every smaller-dimensional representation must fail. Its role is to certify a chosen construction uniformly over the specified finite set.
Worked examples
Worked example
Example 1: A learned null direction collapses distinct points
Take
Its eigenvalues are and , so the learned representation has one coordinate. Points and both map to zero and have learned distance zero. The point maps to one and is distance one from either of those points. These values satisfy the pseudometric properties while identifying variations along the direction .
Adding the identity gives
whose eigenvalues are and . It defines a metric on the whole plane; the formerly collapsed pair now has distance . The additional positive direction restores separation in that direction and makes the matrix full rank. This change alters the chosen geometry rather than merely changing a coordinate notation.
Worked example
Example 2: Convex matrix learning, nonconvex factor learning
Use one anchor at , one same-class target at , and an impostor at . In one dimension let , with , and set . The anchored loss is
The slope changes from to , so the function is convex. Its minimum is at . At that point the impostor's squared distance exceeds the target's by , exactly the required margin.
Factor . The objective becomes . At its value is , but at their midpoint it is . This violates the defining midpoint inequality for a convex function. Factoring enforces positivity, yet loses convexity in the factor variable. The distinction persists in higher-dimensional low-rank parameterizations.
Worked example
Example 3: Choose a dimension with a stated failure probability
Take , original dimension , squared-distance tolerance , and desired failure probability . Then
The bound on failure is , which is below . Thus a fresh Gaussian map into coordinates succeeds on every pair with probability at least . This is a simultaneous guarantee for the given twenty points; it is not a claim that every direction in has its length preserved.
Indeed a matrix has a nontrivial kernel. Its induced matrix is singular and defines only a pseudometric on the full space. The finite collection can nevertheless avoid all collapsed pairwise differences. There is no contradiction between a low-rank map and accurate distances on the prescribed finite set.
A reproducible projection experiment
This example first creates a dataset, then fixes it before drawing the projection. Rows store points, so their transformed table is . For the stated seed, the smallest and largest squared-distance ratios are approximately and , inside the required interval .
import numpy as np
n, d, epsilon, delta = 20, 2000, 0.5, 0.1
pairs = n * (n - 1) // 2
c = epsilon**2 / 4 - epsilon**3 / 6
k = int(np.ceil(np.log(2 * pairs / delta) / c))
rng = np.random.default_rng(2026)
X = rng.normal(size=(n, d))
R = rng.normal(size=(k, d)) / np.sqrt(k)
Y = X @ R.T
i, j = np.triu_indices(n, k=1)
original = np.sum((X[i] - X[j])**2, axis=1)
projected = np.sum((Y[i] - Y[j])**2, axis=1)
ratios = projected / original
print(k)
print(np.round([ratios.min(), ratios.max()], 6))
The experiment checks all observed pairs for one sampled map. The probability theorem describes repeated random draws conditional on the fixed dataset, while this output records one realization. Both forms of evidence answer useful but different questions about the construction.
Common mistakes
Common mistake
Changing the mathematical object or the probability claim
A positive semidefinite matrix can collapse a nonzero difference; use positive definiteness when a metric on the entire space is required. Squared distances are linear in but generally do not satisfy a triangle inequality: on a line, the squared distance from to is , larger than through the point .
For random projection, a correct expected squared length does not establish a simultaneous high-probability bound. Concentration controls one fixed difference, and the union bound controls the finite collection. Choosing a new difference from the kernel after observing does not meet the fixed-set assumption.
Summary
Metric learning fits a positive semidefinite quadratic form to task constraints. Fixed-neighbor LMNN is convex in that matrix, while rank limits and factor optimization can change the problem's convexity. Gaussian random projection instead preserves fixed finite-set distances with a quantified probability, using the variance normalization , chi-square concentration, and a union bound over pairs.
Exercises
Checkpoint
Exercise 1: For , give a one-row factor , a distinct pair with zero learned distance, and the representation rank.
Identify the ignored coordinate direction.
Checkpoint
Exercise 2: An anchor-target difference is and an anchor-impostor difference is . With one triplet and , compute the loss at and at .
Calculate the two squared distances before applying the hinge.
Checkpoint
Exercise 3: A fixed difference has norm , and the Gaussian map has . What is the variance of one coordinate of , its expected squared norm, and a chi-square normalization?
Keep variance and standard deviation distinct.
Checkpoint
Exercise 4: Keep and , but require . What sufficient integer dimension follows from the finite-set bound?
Use the same pairs and .
Solutions
Solution · Exercise 1 solution
Take . The points and both map to zero. The rank is one, so is a pseudometric on the plane, with a one-dimensional representation.
Solution · Exercise 2 solution
The target squared distance is in both cases. The impostor squared distances are respectively and . Thus the losses are and . The second matrix collapses the impostor direction, increasing the margin penalty.
Solution · Exercise 3 solution
Each coordinate has variance , and . The normalized variable has the distribution. The coordinate standard deviation is .
Solution · Exercise 4 solution
The required dimension from this bound is
It guarantees success probability at least . Requiring greater confidence adds a logarithmic dependence on ; the original dimension still does not enter this sufficient estimate.
Related notes
Fisher discriminants and class scatter uses labels to select projection directions. SVD and principal components explains spectral rank reduction, while classical and non-metric MDS builds coordinates from pairwise relations. The course reading list collects the course books.