mdiggory 2003/11/18 07:07:12 Modified: math/src/test/org/apache/commons/math/stat BivariateRegressionTest.java math/src/java/org/apache/commons/math/special Beta.java math/src/java/org/apache/commons/math/stat BivariateRegression.java Log: fix for wrong results and stack overflow error from BivariateRegression PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24747 Obtained from: Submitted by: Sergei Skarupo, Brent Worden Reviewed by: Mark Diggory Revision Changes Path 1.9 +34 -3 jakarta-commons/math/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java Index: BivariateRegressionTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- BivariateRegressionTest.java 14 Nov 2003 22:22:18 -0000 1.8 +++ BivariateRegressionTest.java 18 Nov 2003 15:07:12 -0000 1.9 @@ -53,6 +53,8 @@ */ package org.apache.commons.math.stat; +import java.util.Random; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -258,6 +260,35 @@ ; } - } + } + + public void testPerfect() { + BivariateRegression regression = new BivariateRegression(); + int n = 100; + for (int i = 0; i < n; i++) { + regression.addData(((double) i) / (n - 1), i); + } + assertEquals(0.0, regression.getSignificance(), 1.0e-5); + assertTrue(regression.getSlope() > 0.0); + } + + public void testPerfectNegative() { + BivariateRegression regression = new BivariateRegression(); + int n = 100; + for (int i = 0; i < n; i++) { + regression.addData(-((double) i) / (n - 1), i); + } + assertEquals(0.0, regression.getSignificance(), 1.0e-5); + assertTrue(regression.getSlope() < 0.0); + } + + public void testRandom() { + BivariateRegression regression = new BivariateRegression(); + Random random = new Random(1); + int n = 100; + for (int i = 0; i < n; i++) { + regression.addData(((double) i) / (n - 1), random.nextDouble()); + } + assertTrue(0.0 < regression.getSignificance() && regression.getSignificance() < 1.0); + } } - 1.13 +5 -2 jakarta-commons/math/src/java/org/apache/commons/math/special/Beta.java Index: Beta.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/special/Beta.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- Beta.java 14 Nov 2003 22:22:17 -0000 1.12 +++ Beta.java 18 Nov 2003 15:07:12 -0000 1.13 @@ -151,8 +151,11 @@ double ret; if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) || - (x > 1) || (a <= 0.0) || (b <= 0.0)) { + (x > 1) || (a <= 0.0) || (b <= 0.0)) + { ret = Double.NaN; + } else if (x > (a + 1.0) / (a + b + 1.0)) { + ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations); } else { ContinuedFraction fraction = new ContinuedFraction() { protected double getB(int n, double x) { 1.10 +8 -6 jakarta-commons/math/src/java/org/apache/commons/math/stat/BivariateRegression.java Index: BivariateRegression.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/BivariateRegression.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- BivariateRegression.java 15 Nov 2003 16:01:38 -0000 1.9 +++ BivariateRegression.java 18 Nov 2003 15:07:12 -0000 1.10 @@ -133,11 +133,13 @@ xbar = x; ybar = y; } else { - sumXX += ((double) n / (double) (n + 1)) * (x - xbar) * (x - xbar); - sumYY += ((double) n / (double) (n + 1)) * (y - ybar) * (y - ybar); - sumXY += ((double) n / (double) (n + 1)) * (x - xbar) * (y - ybar); - xbar += (1d / (double) (n + 1)) * (x - xbar); - ybar += (1d / (double) (n + 1)) * (y - ybar); + double dx = x - xbar; + double dy = y - ybar; + sumXX += dx * dx * (double) n / (double) (n + 1.0); + sumYY += dy * dy * (double) n / (double) (n + 1.0); + sumXY += dx * dy * (double) n / (double) (n + 1.0); + xbar += dx / (double) (n + 1.0); + ybar += dy / (double) (n + 1.0); } sumX += x; sumY += y;
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]