-2

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.

Robb1
  • 3,075
  • 5
  • 21
  • 49
  • So what problem do you have taking the C# and R implementations and converting them to Java? – Joe C Jul 29 '17 at 07:28
  • I don't know both C# and R. I don't really get why am I getting so many downvotes. Please, right after downvoting explain it in comments section. – Robb1 Jul 29 '17 at 07:29
  • If you know Java, you'll find it easy to learn C#, it is pretty similar. You might want to try to learn enough C# to work with the code you found -- it is useful in at least 2 ways: (1) you get a working solution for the problem at hand, (2) you have a new skill. – Robert Dodier Jul 29 '17 at 16:44
  • Thanks for your advice @RobertDodier but atm I'm really short on time! Later on I will consider giving C# a try. For the moment Krzysztof's answer did what I needed :) – Robb1 Jul 29 '17 at 16:49

1 Answers1

2

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];
    }
}
Krzysztof Cichocki
  • 5,658
  • 1
  • 13
  • 31
  • Thanks for your comment. How to proceed having a list (`List list`) instead of an array? (I just edited my question) – Robb1 Jul 29 '17 at 07:36
  • 1
    Look at my answer again, I believe you can learn something form it :) – Krzysztof Cichocki Jul 29 '17 at 07:48
  • Thanks again for your help! Why consider the `massArray.length == 1` as a different case? Can't be included already in the "odd number of elements" case? – Robb1 Jul 29 '17 at 08:57
  • You're right, it is already included in the "odd number of elements" case. I believe you can also accept my answer as the correct one. – Krzysztof Cichocki Jul 29 '17 at 12:44