1 16 17 package org.apache.commons.math.stat.regression; 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.MathException; 21 import org.apache.commons.math.distribution.DistributionFactory; 22 import org.apache.commons.math.distribution.TDistribution; 23 24 54 public class SimpleRegression implements Serializable { 55 56 57 static final long serialVersionUID = -3004689053607543335L; 58 59 60 private double sumX = 0d; 61 62 63 private double sumXX = 0d; 64 65 66 private double sumY = 0d; 67 68 69 private double sumYY = 0d; 70 71 72 private double sumXY = 0d; 73 74 75 private long n = 0; 76 77 78 private double xbar = 0; 79 80 81 private double ybar = 0; 82 83 85 88 public SimpleRegression() { 89 super(); 90 } 91 92 105 public void addData(double x, double y) { 106 if (n == 0) { 107 xbar = x; 108 ybar = y; 109 } else { 110 double dx = x - xbar; 111 double dy = y - ybar; 112 sumXX += dx * dx * (double) n / (double) (n + 1.0); 113 sumYY += dy * dy * (double) n / (double) (n + 1.0); 114 sumXY += dx * dy * (double) n / (double) (n + 1.0); 115 xbar += dx / (double) (n + 1.0); 116 ybar += dy / (double) (n + 1.0); 117 } 118 sumX += x; 119 sumY += y; 120 n++; 121 } 122 123 139 public void addData(double[][] data) { 140 for (int i = 0; i < data.length; i++) { 141 addData(data[i][0], data[i][1]); 142 } 143 } 144 145 148 public void clear() { 149 sumX = 0d; 150 sumXX = 0d; 151 sumY = 0d; 152 sumYY = 0d; 153 sumXY = 0d; 154 n = 0; 155 } 156 157 162 public long getN() { 163 return n; 164 } 165 166 183 public double predict(double x) { 184 double b1 = getSlope(); 185 return getIntercept(b1) + b1 * x; 186 } 187 188 204 public double getIntercept() { 205 return getIntercept(getSlope()); 206 } 207 208 224 public double getSlope() { 225 if (n < 2) { 226 return Double.NaN; } 228 if (Math.abs(sumXX) < 10 * Double.MIN_VALUE) { 229 return Double.NaN; } 231 return sumXY / sumXX; 232 } 233 234 248 public double getSumSquaredErrors() { 249 return getSumSquaredErrors(getSlope()); 250 } 251 252 262 public double getTotalSumSquares() { 263 if (n < 2) { 264 return Double.NaN; 265 } 266 return sumYY; 267 } 268 269 285 public double getRegressionSumSquares() { 286 return getRegressionSumSquares(getSlope()); 287 } 288 289 299 public double getMeanSquareError() { 300 if (n < 3) { 301 return Double.NaN; 302 } 303 return getSumSquaredErrors() / (double) (n - 2); 304 } 305 306 320 public double getR() { 321 double b1 = getSlope(); 322 double result = Math.sqrt(getRSquare(b1)); 323 if (b1 < 0) { 324 result = -result; 325 } 326 return result; 327 } 328 329 343 public double getRSquare() { 344 return getRSquare(getSlope()); 345 } 346 347 358 public double getInterceptStdErr() { 359 return Math.sqrt( 360 getMeanSquareError() * ((1d / (double) n) + (xbar * xbar) / sumXX)); 361 } 362 363 373 public double getSlopeStdErr() { 374 return Math.sqrt(getMeanSquareError() / sumXX); 375 } 376 377 400 public double getSlopeConfidenceInterval() throws MathException { 401 return getSlopeConfidenceInterval(0.05d); 402 } 403 404 435 public double getSlopeConfidenceInterval(double alpha) 436 throws MathException { 437 if (alpha >= 1 || alpha <= 0) { 438 throw new IllegalArgumentException (); 439 } 440 return getSlopeStdErr() * 441 getTDistribution().inverseCumulativeProbability(1d - alpha / 2d); 442 } 443 444 465 public double getSignificance() throws MathException { 466 return 2d* (1.0 - getTDistribution().cumulativeProbability( 467 Math.abs(getSlope()) / getSlopeStdErr())); 468 } 469 470 472 480 private double getIntercept(double slope) { 481 return (sumY - slope * sumX) / ((double) n); 482 } 483 484 493 private double getSumSquaredErrors(double b1) { 494 return sumYY - sumXY * sumXY / sumXX; 495 } 496 497 505 private double getRSquare(double b1) { 506 double ssto = getTotalSumSquares(); 507 return (ssto - getSumSquaredErrors(b1)) / ssto; 508 } 509 510 516 private double getRegressionSumSquares(double slope) { 517 return slope * slope * sumXX; 518 } 519 520 526 private TDistribution getTDistribution() { 527 return DistributionFactory.newInstance().createTDistribution(n - 2); 528 } 529 } 530 | Popular Tags |