diff --git a/src/java/org/apache/commons/math/special/Beta.java b/src/java/org/apache/commons/math/special/Beta.java index ebbc3678f..cbeb0c4ca 100644 --- a/src/java/org/apache/commons/math/special/Beta.java +++ b/src/java/org/apache/commons/math/special/Beta.java @@ -60,7 +60,7 @@ import org.apache.commons.math.util.ContinuedFraction; * This is a utility class that provides computation methods related to the * Beta family of functions. * - * @version $Revision: 1.12 $ $Date: 2003/11/14 22:22:17 $ + * @version $Revision: 1.13 $ $Date: 2003/11/18 15:07:12 $ */ public class Beta { /** Maximum allowed numerical error. */ @@ -151,8 +151,11 @@ public class Beta { 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) { diff --git a/src/java/org/apache/commons/math/stat/BivariateRegression.java b/src/java/org/apache/commons/math/stat/BivariateRegression.java index 871c8dd1b..70015e522 100644 --- a/src/java/org/apache/commons/math/stat/BivariateRegression.java +++ b/src/java/org/apache/commons/math/stat/BivariateRegression.java @@ -84,7 +84,7 @@ import org.apache.commons.math.distribution.TDistribution; * the necessary computations to return the requested statistic. * * - * @version $Revision: 1.9 $ $Date: 2003/11/15 16:01:38 $ + * @version $Revision: 1.10 $ $Date: 2003/11/18 15:07:12 $ */ public class BivariateRegression { @@ -133,11 +133,13 @@ public class BivariateRegression { 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; diff --git a/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java b/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java index 41fbdb599..6c35ce4c8 100644 --- a/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java +++ b/src/test/org/apache/commons/math/stat/BivariateRegressionTest.java @@ -53,13 +53,15 @@ */ package org.apache.commons.math.stat; +import java.util.Random; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Test cases for the TestStatistic class. * - * @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:18 $ + * @version $Revision: 1.9 $ $Date: 2003/11/18 15:07:12 $ */ public final class BivariateRegressionTest extends TestCase { @@ -258,6 +260,35 @@ public final class BivariateRegressionTest extends TestCase { ; } - } + } + + 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); + } } -