mirror of
https://github.com/apache/commons-math.git
synced 2025-03-06 08:29:06 +00:00
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 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141034 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1b45cc8196
commit
0810e249ac
@ -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) {
|
||||
|
@ -84,7 +84,7 @@ import org.apache.commons.math.distribution.TDistribution;
|
||||
* the necessary computations to return the requested statistic.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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;
|
||||
|
@ -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 {
|
||||
@ -259,5 +261,34 @@ 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user