How to calculate the Median Absolute Deviation (MAD) in Java from a list (e.g. List<Double> list
)?
I found some implementations on stackoverflow in programming languages I don't know (e.g. in C# and in R) but none of them is in Java.
How to calculate the Median Absolute Deviation (MAD) in Java from a list (e.g. List<Double> list
)?
I found some implementations on stackoverflow in programming languages I don't know (e.g. in C# and in R) but none of them is in Java.
To calculate median:
You can use Arrays.sort()
to sort the input
array, then if arrayLength%2==0
then median
value is (array[array.length/2-1] + array[array.length/2])/2
otherwise the median is (array[array.length/2])
.
To calculate MAD:
Create Double[] intermediate - new Double[array.length]
and calculate the intermediate
array using intermediate[i] = Math.abs(input[i]-median)
for each element, then calculate the median for the intermediate
array same way as in the first step and you're ready.
Example I've made for you:
import java.util.Arrays;
import java.util.List;
public class MAD {
public static void main(String[] args) {
List<Double> input = Arrays.asList(new Double[] {1d, 1d, 2d, 2d, 4d, 6d, 9d});
System.out.println(mad(input));
}
private static Double mad(List<Double> inputList) {
Double[] input = inputList.toArray(new Double[inputList.size()]);
Double median = median(input);
arrayAbsDistance(input, median);
return median(input);
}
private static void arrayAbsDistance(Double[] array, Double value) {
for (int i=0; i<array.length;i++) {
array[i] = Math.abs(array[i] - value);
}
}
private static Double median(Double[] input) {
if (input.length==0) {
throw new IllegalArgumentException("to calculate median we need at least 1 element");
}
Arrays.sort(input);
if (input.length%2==0) {
return (input[input.length/2-1] + input[input.length/2])/2;
}
return input[input.length/2];
}
}