1 44 45 package org.jfree.data.statistics; 46 47 import java.util.ArrayList ; 48 import java.util.Collections ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 52 57 public abstract class BoxAndWhiskerCalculator { 58 59 70 public static BoxAndWhiskerItem calculateBoxAndWhiskerStatistics( 71 List values) { 72 73 Collections.sort(values); 74 75 double mean = Statistics.calculateMean(values); 76 double median = Statistics.calculateMedian(values, false); 77 double q1 = calculateQ1(values); 78 double q3 = calculateQ3(values); 79 80 double interQuartileRange = q3 - q1; 81 82 double upperOutlierThreshold = q3 + (interQuartileRange * 1.5); 83 double lowerOutlierThreshold = q1 - (interQuartileRange * 1.5); 84 85 double upperFaroutThreshold = q3 + (interQuartileRange * 2.0); 86 double lowerFaroutThreshold = q1 - (interQuartileRange * 2.0); 87 88 double minRegularValue = Double.POSITIVE_INFINITY; 89 double maxRegularValue = Double.NEGATIVE_INFINITY; 90 double minOutlier = Double.POSITIVE_INFINITY; 91 double maxOutlier = Double.NEGATIVE_INFINITY; 92 List outliers = new ArrayList (); 93 94 Iterator iterator = values.iterator(); 95 while (iterator.hasNext()) { 96 Object object = iterator.next(); 97 if (object != null && object instanceof Number ) { 98 Number number = (Number ) object; 99 double value = number.doubleValue(); 100 if (value > upperOutlierThreshold) { 101 outliers.add(number); 102 if (value > maxOutlier && value <= upperFaroutThreshold) { 103 maxOutlier = value; 104 } 105 } 106 else if (value < lowerOutlierThreshold) { 107 outliers.add(number); 108 if (value < minOutlier && value >= lowerFaroutThreshold) { 109 minOutlier = value; 110 } 111 } 112 else { 113 if (minRegularValue == Double.NaN) { 114 minRegularValue = value; 115 } 116 else { 117 minRegularValue = Math.min(minRegularValue, value); 118 } 119 if (maxRegularValue == Double.NaN) { 120 maxRegularValue = value; 121 } 122 else { 123 maxRegularValue = Math.max(maxRegularValue, value); 124 } 125 } 126 127 } 128 } 129 minOutlier = Math.min(minOutlier, minRegularValue); 130 maxOutlier = Math.max(maxOutlier, maxRegularValue); 131 132 return new BoxAndWhiskerItem( 133 new Double (mean), 134 new Double (median), 135 new Double (q1), 136 new Double (q3), 137 new Double (minRegularValue), 138 new Double (maxRegularValue), 139 new Double (minOutlier), 140 new Double (maxOutlier), 141 outliers 142 ); 143 144 } 145 146 153 public static double calculateQ1(List values) { 154 double result = Double.NaN; 155 int count = values.size(); 156 if (count > 0) { 157 if (count % 2 == 1) { 158 if (count > 1) { 159 result = Statistics.calculateMedian(values, 0, count / 2); 160 } 161 else { 162 result = Statistics.calculateMedian(values, 0, 0); 163 } 164 } 165 else { 166 result = Statistics.calculateMedian(values, 0, count / 2 - 1); 167 } 168 169 } 170 return result; 171 } 172 173 180 public static double calculateQ3(List values) { 181 double result = Double.NaN; 182 int count = values.size(); 183 if (count > 0) { 184 if (count % 2 == 1) { 185 if (count > 1) { 186 result = Statistics.calculateMedian( 187 values, count / 2, count - 1 188 ); 189 } 190 else { 191 result = Statistics.calculateMedian(values, 0, 0); 192 } 193 } 194 else { 195 result = Statistics.calculateMedian( 196 values, count / 2, count - 1 197 ); 198 } 199 200 } 201 return result; 202 } 203 204 } 205 | Popular Tags |