1 49 50 package org.jfree.data.statistics; 51 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.Collections ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 58 61 public abstract class Statistics { 62 63 71 public static double calculateMean(Number [] values) { 72 double result = Double.NaN; 73 if (values != null && values.length > 0) { 74 double sum = 0.0; 75 int counter = 0; 76 for (; counter < values.length; counter++) { 77 sum = sum + values[counter].doubleValue(); 78 } 79 result = (sum / counter); 80 } 81 return result; 82 } 83 84 92 public static double calculateMean(Collection values) { 93 94 double result = Double.NaN; 95 int count = 0; 96 double total = 0.0; 97 Iterator iterator = values.iterator(); 98 while (iterator.hasNext()) { 99 Object object = iterator.next(); 100 if (object != null && object instanceof Number ) { 101 Number number = (Number ) object; 102 total = total + number.doubleValue(); 103 count = count + 1; 104 } 105 } 106 if (count > 0) { 107 result = total / count; 108 } 109 return result; 110 111 } 112 113 121 public static double calculateMedian(List values) { 122 return calculateMedian(values, true); 123 } 124 125 135 public static double calculateMedian(List values, boolean copyAndSort) { 136 137 double result = Double.NaN; 138 if (values != null) { 139 if (copyAndSort) { 140 int itemCount = values.size(); 141 List copy = new ArrayList (itemCount); 142 for (int i = 0; i < itemCount; i++) { 143 copy.add(i, values.get(i)); 144 } 145 Collections.sort(copy); 146 values = copy; 147 } 148 int count = values.size(); 149 if (count > 0) { 150 if (count % 2 == 1) { 151 if (count > 1) { 152 Number value = (Number ) values.get((count - 1) / 2); 153 result = value.doubleValue(); 154 } 155 else { 156 Number value = (Number ) values.get(0); 157 result = value.doubleValue(); 158 } 159 } 160 else { 161 Number value1 = (Number ) values.get(count / 2 - 1); 162 Number value2 = (Number ) values.get(count / 2); 163 result = (value1.doubleValue() + value2.doubleValue()) 164 / 2.0; 165 } 166 } 167 } 168 return result; 169 } 170 171 181 public static double calculateMedian(List values, int start, int end) { 182 return calculateMedian(values, start, end, true); 183 } 184 185 198 public static double calculateMedian(List values, int start, int end, 199 boolean copyAndSort) { 200 201 double result = Double.NaN; 202 if (copyAndSort) { 203 List working = new ArrayList (end - start + 1); 204 for (int i = start; i <= end; i++) { 205 working.add(values.get(i)); 206 } 207 Collections.sort(working); 208 result = calculateMedian(working, false); 209 } 210 else { 211 int count = end - start + 1; 212 if (count > 0) { 213 if (count % 2 == 1) { 214 if (count > 1) { 215 Number value 216 = (Number ) values.get(start + (count - 1) / 2); 217 result = value.doubleValue(); 218 } 219 else { 220 Number value = (Number ) values.get(start); 221 result = value.doubleValue(); 222 } 223 } 224 else { 225 Number value1 = (Number ) values.get(start + count / 2 - 1); 226 Number value2 = (Number ) values.get(start + count / 2); 227 result 228 = (value1.doubleValue() + value2.doubleValue()) / 2.0; 229 } 230 } 231 } 232 return result; 233 234 } 235 236 243 public static double getStdDev(Number [] data) { 244 double avg = calculateMean(data); 245 double sum = 0.0; 246 247 for (int counter = 0; counter < data.length; counter++) { 248 double diff = data[counter].doubleValue() - avg; 249 sum = sum + diff * diff; 250 } 251 return Math.sqrt(sum / (data.length - 1)); 252 } 253 254 263 public static double[] getLinearFit(Number [] xData, Number [] yData) { 264 265 if (xData.length != yData.length) { 267 throw new IllegalArgumentException ( 268 "Statistics.getLinearFit(): array lengths must be equal."); 269 } 270 271 double[] result = new double[2]; 272 result[1] = getSlope(xData, yData); 274 result[0] = calculateMean(yData) - result[1] * calculateMean(xData); 276 277 return result; 278 279 } 280 281 289 public static double getSlope(Number [] xData, Number [] yData) { 290 291 if (xData.length != yData.length) { 293 throw new IllegalArgumentException ("Array lengths must be equal."); 294 } 295 296 304 double sx = 0.0, sxx = 0.0, sxy = 0.0, sy = 0.0; 306 int counter; 307 for (counter = 0; counter < xData.length; counter++) { 308 sx = sx + xData[counter].doubleValue(); 309 sxx = sxx + Math.pow(xData[counter].doubleValue(), 2); 310 sxy = sxy + yData[counter].doubleValue() 311 * xData[counter].doubleValue(); 312 sy = sy + yData[counter].doubleValue(); 313 } 314 return (sxy - (sx * sy) / counter) / (sxx - (sx * sx) / counter); 315 316 } 317 318 331 public static double getCorrelation(Number [] data1, Number [] data2) { 332 if (data1 == null) { 333 throw new IllegalArgumentException ("Null 'data1' argument."); 334 } 335 if (data2 == null) { 336 throw new IllegalArgumentException ("Null 'data2' argument."); 337 } 338 if (data1.length != data2.length) { 339 throw new IllegalArgumentException ( 340 "'data1' and 'data2' arrays must have same length." 341 ); 342 } 343 int n = data1.length; 344 double sumX = 0.0; 345 double sumY = 0.0; 346 double sumX2 = 0.0; 347 double sumY2 = 0.0; 348 double sumXY = 0.0; 349 for (int i = 0; i < n; i++) { 350 double x = 0.0; 351 if (data1[i] != null) { 352 x = data1[i].doubleValue(); 353 } 354 double y = 0.0; 355 if (data2[i] != null) { 356 y = data2[i].doubleValue(); 357 } 358 sumX = sumX + x; 359 sumY = sumY + y; 360 sumXY = sumXY + (x * y); 361 sumX2 = sumX2 + (x * x); 362 sumY2 = sumY2 + (y * y); 363 } 364 return (n * sumXY - sumX * sumY) / Math.pow((n * sumX2 - sumX * sumX) 365 * (n * sumY2 - sumY * sumY), 0.5); 366 } 367 368 378 public static double[][] getMovingAverage(Number [] xData, 379 Number [] yData, 380 int period) { 381 382 if (xData.length != yData.length) { 384 throw new IllegalArgumentException ("Array lengths must be equal."); 385 } 386 387 if (period > xData.length) { 388 throw new IllegalArgumentException ( 389 "Period can't be longer than dataset." 390 ); 391 } 392 393 double[][] result = new double[xData.length - period][2]; 394 for (int i = 0; i < result.length; i++) { 395 result[i][0] = xData[i + period].doubleValue(); 396 double sum = 0.0; 398 for (int j = 0; j < period; j++) { 399 sum += yData[i + j].doubleValue(); 400 } 401 sum = sum / period; 402 result[i][1] = sum; 403 } 404 return result; 405 406 } 407 408 } 409 | Popular Tags |