Ma · Mathematics

Where Is the Sum of Squared Distances Minimized?

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the centroid

    Average the coordinates of all given points to get their centroid.

  2. 2. Use the identity

    Rewrite the total squared distance as a constant plus a nonnegative term measuring distance from the centroid.

  3. 3. Read off the minimum

    The extra term is smallest at zero, so the centroid is the unique minimizer.

  4. 4. Check constraints

    If the chosen point must lie in a restricted set, the unconstrained centroid may not be allowed.

If you can choose a point freely and want to minimize the sum of its squared distances to a fixed collection of points, the minimizing point is their centroid. This result holds in any finite-dimensional Euclidean space, not just in the plane.

The proof is short: express each point’s displacement from your chosen point as a displacement from the centroid plus a shared offset. When you square and sum, the cross terms cancel. The remaining expression shows exactly how much the total increases when you move away from the centroid.

The setup and answer

Let the fixed points be a1,a2,…,ana_1, a_2, \ldots, a_n in Rd\mathbb{R}^d, and let xx be the point you can choose. The objective is

S(x)=∑i=1n∥x−ai∥2.S(x)=\sum_{i=1}^n \|x-a_i\|^2.

Here, ∥x−ai∥\|x-a_i\| is the usual Euclidean distance. Define the centroid by averaging the points coordinate by coordinate:

aˉ=1n∑i=1nai.\bar a=\frac{1}{n}\sum_{i=1}^n a_i.

Then S(x)S(x) has a unique minimum at x=aˉx=\bar a. The points do not need to form a regular shape, and the centroid does not need to be one of the original points.

Why the centroid minimizes the sum

Write x−ai=(x−aˉ)+(aˉ−ai)x-a_i=(x-\bar a)+(\bar a-a_i) and expand the squared norm using the dot product:

S(x)=∑i=1n∥(x−aˉ)+(aˉ−ai)∥2=n∥x−aˉ∥2+2(x−aˉ)⋅∑i=1n(aˉ−ai)+∑i=1n∥aˉ−ai∥2.\begin{aligned} S(x) &=\sum_{i=1}^n \|(x-\bar a)+(\bar a-a_i)\|^2\\ &=n\|x-\bar a\|^2 +2(x-\bar a)\cdot\sum_{i=1}^n(\bar a-a_i) +\sum_{i=1}^n\|\bar a-a_i\|^2. \end{aligned}

The middle term is zero because

∑i=1n(aˉ−ai)=naˉ−∑i=1nai=0.\sum_{i=1}^n(\bar a-a_i)=n\bar a-\sum_{i=1}^n a_i=0.

Therefore,

S(x)=∑i=1n∥aˉ−ai∥2+n∥x−aˉ∥2.S(x)=\sum_{i=1}^n\|\bar a-a_i\|^2+n\|x-\bar a\|^2.

The first term is constant with respect to xx. The second is nonnegative and equals zero only when x=aˉx=\bar a. This proves both that the centroid is a minimizer and that it is the unique minimizer.

You can also see the result by differentiating. In coordinates, the gradient of SS is 2nx−2∑iai2n x-2\sum_i a_i, so setting it to zero gives x=aˉx=\bar a. The identity above is often more informative: it directly tells you the increase in the sum for any chosen point.

Worked example

Take the three points (0,0)(0,0), (2,0)(2,0), and (0,4)(0,4). Their centroid is

aˉ=(0+2+03,0+0+43)=(23,43).\bar a=\left(\frac{0+2+0}{3},\frac{0+0+4}{3}\right) =\left(\frac{2}{3},\frac{4}{3}\right).

The squared distances from the centroid to the three points are

∥(23,43)−(0,0)∥2=209,∥(23,43)−(2,0)∥2=329,∥(23,43)−(0,4)∥2=689.\begin{aligned} \left\|\left(\frac{2}{3},\frac{4}{3}\right)-(0,0)\right\|^2&=\frac{20}{9},\\ \left\|\left(\frac{2}{3},\frac{4}{3}\right)-(2,0)\right\|^2&=\frac{32}{9},\\ \left\|\left(\frac{2}{3},\frac{4}{3}\right)-(0,4)\right\|^2&=\frac{68}{9}. \end{aligned}

Their sum is 120/9=40/3120/9=40/3. Thus the minimum total is 40/340/3, attained at (2/3,4/3)(2/3,4/3). For any other point xx, the total is larger by 3∥x−aˉ∥23\|x-\bar a\|^2.

Common mistakes

  • Minimizing distances instead of squared distances. The result above is for ∑i∥x−ai∥2\sum_i\|x-a_i\|^2. If the objective is ∑i∥x−ai∥\sum_i\|x-a_i\|, the minimizer is generally a geometric median, not the centroid.
  • Using the wrong average. For equally weighted points, average each coordinate. For example, in two dimensions, average all the xx-coordinates and all the yy-coordinates separately.
  • Assuming the minimizing point must be one of the given points. The centroid can lie between them, as it does in the example.
  • Ignoring a constraint. The proof assumes xx can be anywhere in Rd\mathbb{R}^d. If xx must lie on a particular line or in a closed convex region, the constrained minimizer is the point in that set nearest to the centroid. For a line, this is the perpendicular projection of the centroid onto the line.

Weighted points

If point aia_i has a positive weight wiw_i, minimize ∑iwi∥x−ai∥2\sum_i w_i\|x-a_i\|^2. The same argument gives the weighted centroid

aˉw=∑iwiai∑iwi\bar a_w=\frac{\sum_i w_i a_i}{\sum_i w_i}

as the unique minimizer. A larger weight pulls the minimizing point more strongly toward its associated point.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.