1 42 43 package org.jfree.data; 44 45 52 public class Statistics { 53 54 61 public static double getAverage(Number [] data) { 62 double sum = 0.0; 63 int counter = 0; 64 for (; counter < data.length; counter++) { 65 sum = sum + data[counter].doubleValue(); 66 } 67 return (sum / counter); 68 } 69 70 77 public static double getStdDev(Number [] data) { 78 double avg = getAverage(data); 79 double sum = 0.0; 80 int counter = 0; 81 double diff = 0.0; 82 83 for (; counter < data.length; counter++) { 84 diff = data[counter].doubleValue() - avg; 85 sum = sum + diff * diff; 86 } 87 return Math.sqrt(sum / (counter - 1)); 88 } 89 90 99 public static double[] getLinearFit(Number [] xData, Number [] yData) { 100 101 if (xData.length != yData.length) { 103 throw new IllegalArgumentException ( 104 "Statistics.getLinearFit(...): array lengths must be equal."); 105 } 106 107 double[] result = new double[2]; 108 result[1] = getSlope(xData, yData); 110 result[0] = getAverage(yData) - result[1] * getAverage(xData); 112 113 return result; 114 115 } 116 117 125 public static double getSlope(Number [] xData, Number [] yData) { 126 127 if (xData.length != yData.length) { 129 throw new IllegalArgumentException ( 130 "Statistics.getSlope(...): array lengths must be equal."); 131 } 132 133 141 double sx = 0.0, sxx = 0.0, sxy = 0.0, sy = 0.0; 143 int counter; 144 for (counter = 0; counter < xData.length; counter++) { 145 sx = sx + xData[counter].doubleValue(); 146 sxx = sxx + Math.pow(xData[counter].doubleValue(), 2); 147 sxy = sxy + yData[counter].doubleValue() * xData[counter].doubleValue(); 148 sy = sy + yData[counter].doubleValue(); 149 } 150 return (sxy - (sx * sy) / counter) / (sxx - (sx * sx) / counter); 151 152 } 153 154 162 public static double getCorrelation(Number [] data1, Number [] data2) { 163 164 if (data1.length != data2.length) { 166 throw new IllegalArgumentException ( 167 "Statistics.getCorrelation(...): array lengths must be equal."); 168 } 169 170 double xavg = 0, yavg = 0; 171 double xstd = 0, ystd = 0; 172 int counter = 0; 173 174 Number [] xData = new Double [data1.length]; 176 Number [] yData = new Double [data2.length]; 177 for (int i = 0; i < data1.length; i++) { 178 xData[i] = new Double (data1[i].doubleValue()); 179 } 180 for (int i = 0; i < data2.length; i++) { 181 yData[i] = new Double (data2[i].doubleValue()); 182 } 183 184 xavg = getAverage(xData); 186 yavg = getAverage(yData); 187 xstd = getStdDev(xData); 188 ystd = getStdDev(yData); 189 190 for (; counter < xData.length; counter++) { 192 xData[counter] = new Double ((xData[counter].doubleValue() - xavg) / xstd); 193 yData[counter] = new Double ((yData[counter].doubleValue() - yavg) / ystd); 194 } 195 196 for (counter = 0; counter < xData.length; counter++) { 198 xData[counter] = new Double (xData[counter].doubleValue() 199 * yData[counter].doubleValue()); 200 } 201 202 return getAverage(xData); 203 204 } 205 206 216 public static double[][] getMovingAverage(Number [] xData, Number [] yData, int period) { 217 218 if (xData.length != yData.length) { 220 throw new IllegalArgumentException ( 221 "Statistics.getMovingAverage(...): array lengths must be equal."); 222 } 223 224 if (period > xData.length) { 225 throw new IllegalArgumentException ( 226 "Statistics.getMovingAverage(...): period can't be longer than dataset."); 227 } 228 229 double[][] result = new double[xData.length - period][2]; 230 for (int i = 0; i < result.length; i++) { 231 result[i][0] = xData[i + period].doubleValue(); 232 double sum = 0.0; 234 for (int j = 0; j < period; j++) { 235 sum += yData[i + j].doubleValue(); 236 } 237 sum = sum / period; 238 result[i][1] = sum; 239 } 240 return result; 241 242 } 243 244 } 245 | Popular Tags |