Median
Jakob Jenkov |
The median of a data set is found by sorting the values in the data set and taking the value that is exactly in the middle of the data set. I will illustrate the median using the following example data set:
Item | Amount | Order Id | Customer Id |
---|---|---|---|
Hard disk | 99.95 | 790 | 23 |
Monitor | 195.95 | 791 | 45 |
Mouse | 19.95 | 792 | 23 |
First we need to sort this data set according to the values of the property we want to find the median of.
We will sort the example data set according to th Amount
property. Here is the sorted data set:
Item | Amount | Order Id | Customer Id |
---|---|---|---|
Mouse | 19.95 | 792 | 23 |
Hard disk | 99.95 | 790 | 23 |
Monitor | 195.95 | 791 | 45 |
The median Amount
value is the value found in the record which is exactly in the middle of the
data set. In the above example it would be the second record. The median Amount
value would
thus be 99.95 .
In case a data set has an equal number of records there is no record that is exactly in the middle of the data set. In that case the median is found by taking the average value of the two records that are closest to the middle of the data set. As an example, look at this data set:
Item | Amount | Order Id | Customer Id |
---|---|---|---|
Mouse | 17.95 | 795 | 34 |
Mouse | 19.95 | 792 | 23 |
Hard disk | 99.95 | 790 | 23 |
Monitor | 195.95 | 791 | 45 |
The data set is already sorted according to the Amount
. The data set has an equal number of records (4)
meaning the median has to be calculated as the average of the Amount
values of two middle records.
The Amount
values of the two middle records are 19.95 and 99.95. The median is thus calculated as:
median = ratio(19.95 + 99.95, 2)
which is 59.95 .
Tweet | |
Jakob Jenkov |