Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate mean, median, mode and range from a set of numbers

Are there any functions (as part of a math library) which will calculate mean, median, mode and range from a set of numbers.

like image 387
user339108 Avatar asked Nov 16 '10 06:11

user339108


People also ask

How do you find the median of a set of numbers in statistics?

Count how many numbers are in the set. Find the pair of numbers in the middle of the figures. Find the pair's average by adding them together and dividing by two. The resulting number is the median.


1 Answers

Yes, there does seem to be 3rd libraries (none in Java Math). Two that have come up are:

http://opsresearch.com/app/

http://www.iro.umontreal.ca/~simardr/ssj/indexe.html

but, it is actually not that difficult to write your own methods to calculate mean, median, mode and range.

MEAN

public static double mean(double[] m) {     double sum = 0;     for (int i = 0; i < m.length; i++) {         sum += m[i];     }     return sum / m.length; } 

MEDIAN

// the array double[] m MUST BE SORTED public static double median(double[] m) {     int middle = m.length/2;     if (m.length%2 == 1) {         return m[middle];     } else {         return (m[middle-1] + m[middle]) / 2.0;     } } 

MODE

public static int mode(int a[]) {     int maxValue, maxCount;      for (int i = 0; i < a.length; ++i) {         int count = 0;         for (int j = 0; j < a.length; ++j) {             if (a[j] == a[i]) ++count;         }         if (count > maxCount) {             maxCount = count;             maxValue = a[i];         }     }      return maxValue; } 

UPDATE

As has been pointed out by Neelesh Salpe, the above does not cater for multi-modal collections. We can fix this quite easily:

public static List<Integer> mode(final int[] numbers) {     final List<Integer> modes = new ArrayList<Integer>();     final Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();      int max = -1;      for (final int n : numbers) {         int count = 0;          if (countMap.containsKey(n)) {             count = countMap.get(n) + 1;         } else {             count = 1;         }          countMap.put(n, count);          if (count > max) {             max = count;         }     }      for (final Map.Entry<Integer, Integer> tuple : countMap.entrySet()) {         if (tuple.getValue() == max) {             modes.add(tuple.getKey());         }     }      return modes; } 

ADDITION

If you are using Java 8 or higher, you can also determine the modes like this:

public static List<Integer> getModes(final List<Integer> numbers) {     final Map<Integer, Long> countFrequencies = numbers.stream()             .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));      final long maxFrequency = countFrequencies.values().stream()             .mapToLong(count -> count)             .max().orElse(-1);      return countFrequencies.entrySet().stream()             .filter(tuple -> tuple.getValue() == maxFrequency)             .map(Map.Entry::getKey)             .collect(Collectors.toList()); } 
like image 99
Nico Huysamen Avatar answered Sep 30 '22 12:09

Nico Huysamen