Detect Outlier using Boxplot in Java
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
public boolean isOutlier(String itemId, String value) { if (monitoringItem.getProperties().getBoxPlotSize() > 0) { BoxPlot boxPlot = Statics.boxPlotRepository.findByItemId(itemId); DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(); double dValue = Double.parseDouble(value); for (ItemBoxPlot bItem : boxPlot.getBoxPlotList()) { double d = Double.parseDouble(bItem.getValue()); descriptiveStatistics.addValue(d); } double Q1 = descriptiveStatistics.getPercentile(25); double Q3 = descriptiveStatistics.getPercentile(75); double IQR = Q3 - Q1; double highRange = Q3 + 3 * IQR; double lowRange = Q1 - 3 * IQR; if (dValue > highRange || dValue < lowRange) { return true; } } return false; }
References
https://pupli.net/2019/05/31/detect-outlier-using-boxplot/