How to detect outliers in an ArrayList

后端 未结 8 993
旧时难觅i
旧时难觅i 2021-01-14 02:54

I\'m trying to think of some code that will allow me to search through my ArrayList and detect any values outside the common range of \"good values.\"

Example: 100 1

8条回答
  •  耶瑟儿~
    2021-01-14 03:48

    I'm very glad and thanks to Valiyev. His solution helped me a lot. And I want to shere my little SRP on his works.

    Please note that I use List.of() to store Dixon's critical values, for this reason it is required to use Java higher than 8.

    public class DixonTest {
    protected List criticalValues = 
        List.of(0.941, 0.765, 0.642, 0.56, 0.507, 0.468, 0.437);
    private double scaleOfElimination;
    private double mean;
    private double stdDev;
    
    private double getMean(final List input) {
        double sum = input.stream()
                .mapToDouble(value -> value)
                .sum();
        return (sum / input.size());
    }
    
      private double getVariance(List input) {
        double mean = getMean(input);
        double temp = input.stream()
                .mapToDouble(a -> a)
                .map(a -> (a - mean) * (a - mean))
                .sum();
        return temp / (input.size() - 1);
    }
    
    private double getStdDev(List input) {
        return Math.sqrt(getVariance(input));
    }
    
    protected List eliminateOutliers(List input) {
        int N = input.size() - 3;
        scaleOfElimination = criticalValues.get(N).floatValue();
        mean = getMean(input);
        stdDev = getStdDev(input);
    
        return input.stream()
                .filter(this::isOutOfBounds)
                .collect(Collectors.toList());
    }
    
    private boolean isOutOfBounds(Double value) {
        return !(isLessThanLowerBound(value)
                || isGreaterThanUpperBound(value));
    }
    
    private boolean isGreaterThanUpperBound(Double value) {
        return value > mean + stdDev * scaleOfElimination;
    }
    
    private boolean isLessThanLowerBound(Double value) {
        return value < mean - stdDev * scaleOfElimination;
    }
    }
    

    I hope it will help someone else.

    Best regard

提交回复
热议问题