# Median

The 3 most popular average algorithms are:

In the previous articles the Arithmetic Mean and Mode have been covered. The last popular average algorithm the Median is covered in this article.

Median average is when the middle value within a range is found. For example, consider a class of 5 students whose height are 1.54m, 1.55cm, 1.56cm, 1.54cm and 1.53cm respectively. The Arithmetic Mean of the class height is 1.544cm and the Mode average is 1.54cm. However if the students are placed next to each other with their height ascending, the student in the middle will be the 1.55m student. The sorting of the class in ascending order and taking the student in the middle as the class’ height average is called Median average.

Formulating the Median algorithm into Mathematical Syntax

$$\text{Given a sample S}\\ \text{Let }O_S = (x | \forall x \in S)\\ Median(S) = \exists x \in O_S | x = O_{S\big\lceil\frac{|O_S|}{2}\big\rceil}$$

The expression $x = O_{S\big\lceil\frac{|O_S|}{2}\big\rceil}$ raises a concern. What happens when the length of the set is even. The equation states that the middle point will be one of the middle points, but in reality the middle point of the set is a fraction in between 2 values.

In the case of samples with even number of sample points, the Median express above is modified slightly such that the Median value will be the Arithmetic Mean of the 2 sample points around the set middle. In Mathemtical notation

$$Median(S) = \frac{x_{O_{S\frac{|O_S|}{2}}} + x_{O_{S\big\{\frac{|O_S|}{2} + 1\big\}}}}{2}$$

The last equation can be used even when the length of the sample is odd. This can be done as the ceiling and floor value of a whole number is the whole number itself, and then the same number is added to itself and divided by 2 will return the same number. Thus keeping the two Median formulæ above as an optimisation for odd and even length sets the final Median notation is

$$\text{Given a sample S}\\ \text{Let }O_S = (x | \forall x \in S)\\ Median(S) = \begin{cases}\exists x \in O_S | x = O_{S\big\lceil\frac{|O_S|}{2}\big\rceil} & \text{if } |O_S| \text{ is odd}\\ \frac{x_{O_{S\frac{|O_S|}{2}}} + x_{O_{S\big\{\frac{|O_S|}{2} + 1\big\}}}}{2} & \text{if } |O_S| \text{ is even}\end{cases}$$

The mathematical formula above with the sample examples can be found implemented in 5 different programming languages in our Github repository.

## Examples

### Example 1:

Consider the following simple set $S = \{1, 2, 3, 4, 5, 6, 7, 8, 9\}$. Using the formula above, we can find the Median for the sample $S$ as follow.

1. Sort the set in ascending order $$O_S = (1, 2, 3, 4, 5, 6, 7, 8, 9)$$
2. Find the length of the set S $$|O_S| = 9$$
3. Return the element at the middle of the set $$Median = x_{O_{S\big\lceil\frac{9}{2}\big\rceil}} = 5$$

### Example 2

$S = \left\{\begin{matrix}0.4963474212, 0.1976482578, 0.9688106204,\\ 0.6871861999,0.0291216746, 0.9002658844,\\0.5500229849, 0.7883546496, 0.9563570268,\\0.5221225236\end{matrix}\right\}$

Workings:

1. Sort the set in ascending order $$\left(\begin{matrix}0.0291216746, 0.197648278, 0.4963474212,\\0.5221225236, 0.5500229849, 0.6871861999,\\0.7883546496, 0.9002658844, 0.9563570268,\\0.9688106204\end{matrix}\right)$$
2. Find the length of the set S $$|O_S| = 10$$
3. Return the average between the 2 elements at the middle of the set $$Median = \frac{x_{O_{S\frac{10}{2}}} + x_{O_{S\big\{\frac{10}{2} + 1\big\}}}}{2} = 0.6186045924$$

### Example 3

$S = \left\{\begin{matrix}51,97,43,20,48,48,96,63,10,35,16,\\4,42,80,18,1,67,75,46,92,38,44,\\87,69,54,91,6,8,60,64,53,23,86\end{matrix}\right\}$

Workings:

1. Sort the set in ascending order $$\left(\begin{matrix}1,4,6,8,10,16,18,20,23,35,38,\\42,43,44,46,48,48,51,53,54,60,\\63,64,67,69,75,80,86,87,91,92,96,97\end{matrix}\right)$$
2. Find the length of the set S $$|O_S| = 33$$
3. Return the element at the middle of the set $$Median = x_{O_{S\big\lceil\frac{33}{2}\big\rceil}} = 48$$