Find the value of for which the quantity is minimal.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
For a set S with n elements such that s 1 < s 2 < ⋯ < s n , s ∈ S ∑ ∣ x − s ∣ is minimized when x is the median of S .
Let's show this intuitively before proving it rigorously.
Intuition
Imagine the given values as trees along a road. The quantity you are trying to minimize is the sum of distances between you and each of the trees.
Suppose that you are standing at the median, and you know the current value of the sum. How will it change if you move in either direction? Note that regardless of which direction you choose, in each moment you will be moving away from at least half of the trees, and towards at most half of the trees. Therefore, the sum of distances can never decrease -- and that means the sum of distances at the beginning had to be optimal.
Proof
Suppose that the set S has n elements, s 1 < s 2 < ⋯ < s n . If x < s 1 , then
f ( x ) = s ∈ S ∑ ∣ x − s ∣ = s ∈ S ∑ ∣ s − x ∣ = s ∈ S ∑ ( s − x ) = k = 1 ∑ n ( s k − x )
As x increases, each term of f ( x ) decreases until x reaches x 1 , so f ( s 1 ) < f ( x ) for all x < s 1 .
Now suppose that s k ≤ x ≤ x + d ≤ s k + 1 . Then
f ( x + d ) = i = 1 ∑ k ( x + d − s i ) + i = k + 1 ∑ n ( s i − ( x + d ) ) = d k + i = 1 ∑ k ( x − s i ) − d ( n − k ) + i = k + 1 ∑ n ( s i − x ) = d ( 2 k − n ) + i = 1 ∑ k ( x − s i ) + i = k + 1 ∑ n ( s i − x ) = d ( 2 k − n ) + f ( x )
so f ( x + d ) − f ( x ) = d ( 2 k − n ) . This is negative if 2 k < n , zero if 2 k = n , and positive if 2 k > n . Thus, on the interval [ s k , s k + 1 ] ,
f ( x ) is ⎩ ⎪ ⎨ ⎪ ⎧ decreasing, constant, increasing, if 2 k < n if 2 k = n if 2 k > n
In particular, we note that when 2 k = n , the sum stays constant. What this translates to for our tree analogy is that the sum of the distances does not "tend" toward one direction or another. Finally, we note that since 2 k = n , k is the median of our set S .
Thus, we have proven that for a set S with n elements such that s 1 < s 2 < ⋯ < s n , s ∈ S ∑ ∣ x − s ∣ is minimized when x is the median of S . Now let f ( x ) = n = 1 ∑ 2 0 1 7 ∣ x − n ∣ . f ( x ) is minimized when x is the median of the set S = { 1 , 2 , 3 , … 2 0 1 6 , 2 0 1 7 } . Since S is a uniform distribution, the median equals the mean of the set, so the median is x = 2 1 + 2 0 1 7 = 1 0 0 9 .