Merged changes in MATH_1_1 branch to trunk. This includes revision 234481 through revision 240244.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@240245 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2005-08-26 14:05:45 +00:00
parent 154d4c999f
commit e3ab7379e2
8 changed files with 159 additions and 41 deletions

View File

@ -265,7 +265,9 @@ public abstract class UnivariateRealSolverImpl implements UnivariateRealSolver,
*/
protected boolean isBracketing(double lower, double upper,
UnivariateRealFunction f) throws FunctionEvaluationException {
return (f.value(lower) * f.value(upper) < 0);
double f1 = f.value(lower);
double f2 = f.value(upper);
return ((f1 > 0 && f2 < 0) || (f1 < 0 && f2 > 0));
}
/**

View File

@ -82,10 +82,8 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
ret = 0.0;
} else if(x >= domain[1]) {
ret = 1.0;
} else if (x - domain[0] < domain[1] - x) {
ret = lowerCumulativeProbability(domain[0], x, n, m, k);
} else {
ret = 1.0 - upperCumulativeProbability(x + 1, domain[1], n, m, k);
ret = innerCumulativeProbability(domain[0], x, 1, n, m, k);
}
return ret;
@ -178,28 +176,6 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
return Math.min(k, m);
}
/**
* For this disbution, X, this method returns P(x0 &le; X &le; x1). This
* probability is computed by summing the point probabilities for the values
* x0, x0 + 1, x0 + 2, ..., x1, in that order.
* @param x0 the inclusive, lower bound
* @param x1 the inclusive, upper bound
* @param n the population size.
* @param m number of successes in the population.
* @param k the sample size.
* @return P(x0 &le; X &le; x1).
*/
private double lowerCumulativeProbability(
int x0, int x1, int n, int m, int k)
{
double ret;
ret = 0.0;
for (int i = x0; i <= x1; ++i) {
ret += probability(n, m, k, i);
}
return ret;
}
/**
* For this disbution, X, this method returns P(X = x).
*
@ -281,7 +257,8 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
/**
* For this disbution, X, this method returns P(X &ge; x).
* @param x the value at which the CDF is evaluated.
* @return upper tail CDF for this distribution.
* @return upper tail CDF for this distribution.
* @since 1.1
*/
public double upperCumulativeProbability(int x) {
double ret;
@ -293,36 +270,36 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
int[] domain = getDomain(n, m, k);
if (x < domain[0]) {
ret = 1.0;
} else if(x >= domain[1]) {
} else if(x > domain[1]) {
ret = 0.0;
} else if (x - domain[0] < domain[1] - x) {
ret = 1.0 - lowerCumulativeProbability(domain[0], x - 1, n, m, k);
} else {
ret = upperCumulativeProbability(x, domain[1], n, m, k);
ret = innerCumulativeProbability(domain[1], x, -1, n, m, k);
}
return ret;
}
/**
* For this disbution, X, this method returns P(x0 &le; X &le; x1). This
* probability is computed by summing the point probabilities for the values
* x1, x1 - 1, x1 - 2, ..., x0, in that order.
* x0, x0 + 1, x0 + 2, ..., x1, in the order directed by dx.
* @param x0 the inclusive, lower bound
* @param x1 the inclusive, upper bound
* @param dx the direction of summation. 1 indicates summing from x0 to x1.
* 0 indicates summing from x1 to x0.
* @param n the population size.
* @param m number of successes in the population.
* @param k the sample size.
* @return P(x0 &le; X &le; x1).
*/
private double upperCumulativeProbability(
int x0, int x1, int n, int m, int k)
private double innerCumulativeProbability(
int x0, int x1, int dx, int n, int m, int k)
{
double ret = 0.0;
for (int i = x1; i >= x0; --i) {
ret += probability(n, m, k, i);
double ret = probability(n, m, k, x0);
while (x0 != x1) {
x0 += dx;
ret += probability(n, m, k, x0);
}
return ret;
}
}

View File

@ -29,7 +29,7 @@ import junit.framework.TestCase;
*
* @version $Revision$ $Date$
*/
public class RetryTestCase extends TestCase {
public abstract class RetryTestCase extends TestCase {
public RetryTestCase() {
super();

View File

@ -127,4 +127,23 @@ public class TestUtils {
Assert.assertEquals("Equals check", object, object2);
Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode());
}
public static void assertRelativelyEquals(double expected, double actual, double relativeError) {
assertRelativelyEquals(null, expected, actual, relativeError);
}
public static void assertRelativelyEquals(String msg, double expected, double actual, double relativeError) {
if (Double.isNaN(expected)) {
Assert.assertTrue(msg, Double.isNaN(actual));
} else if (Double.isNaN(actual)) {
Assert.assertTrue(msg, Double.isNaN(expected));
} else if (Double.isInfinite(actual) || Double.isInfinite(expected)) {
Assert.assertEquals(expected, actual, relativeError);
} else if (expected == 0.0) {
Assert.assertEquals(msg, actual, expected, relativeError);
} else {
double x = Math.abs((expected - actual) / expected);
Assert.assertEquals(msg, 0.0, x, relativeError);
}
}
}

View File

@ -16,6 +16,8 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.TestUtils;
/**
* Test cases for HyperGeometriclDistribution.
* Extends IntegerDistributionAbstractTest. See class javadoc for
@ -128,4 +130,77 @@ public class HypergeometricDistributionTest extends IntegerDistributionAbstractT
dist.setPopulationSize(10);
assertEquals(10, dist.getPopulationSize());
}
public void testLargeValues() {
int populationSize = 3456;
int sampleSize = 789;
int numberOfSucceses = 101;
double[][] data = {
{0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0},
{1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244},
{2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673},
{3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792},
{4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375},
{5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203},
{20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781},
{21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701},
{22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381},
{23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199},
{24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718},
{25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418},
{96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57},
{97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59},
{98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61},
{99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63},
{100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65},
{101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68},
};
testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
}
private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) {
HypergeometricDistributionImpl dist = new HypergeometricDistributionImpl(populationSize, numberOfSucceses, sampleSize);
for (int i = 0; i < data.length; ++i) {
int x = (int)data[i][0];
double pdf = data[i][1];
double actualPdf = dist.probability(x);
TestUtils.assertRelativelyEquals(pdf, actualPdf, 1.0e-9);
double cdf = data[i][2];
double actualCdf = dist.cumulativeProbability(x);
TestUtils.assertRelativelyEquals(cdf, actualCdf, 1.0e-9);
double cdf1 = data[i][3];
double actualCdf1 = dist.upperCumulativeProbability(x);
TestUtils.assertRelativelyEquals(cdf1, actualCdf1, 1.0e-9);
}
}
public void testMoreLargeValues() {
int populationSize = 26896;
int sampleSize = 895;
int numberOfSucceses = 55;
double[][] data = {
{0.0, 0.155168304750504, 0.155168304750504, 1.0},
{1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496},
{2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036},
{3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033},
{4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247},
{5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237},
{20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16},
{21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17},
{22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18},
{23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20},
{24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21},
{25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23},
{50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69},
{51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71},
{52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74},
{53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76},
{54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79},
{55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82},
};
testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
}
}

View File

@ -174,6 +174,37 @@ public final class BigMatrixImplTest extends TestCase {
} catch (NumberFormatException ex) {
// expected
}
try {
BigMatrix m4 = new BigMatrixImpl(new String[][] {});
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
BigMatrix m4 = new BigMatrixImpl(new String[][] {{},{}});
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
BigMatrix m4 = new BigMatrixImpl(new String[][] {{"a", "b"},{"c"}});
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
BigMatrix m4 = new BigMatrixImpl(0, 1);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
BigMatrix m4 = new BigMatrixImpl(1, 0);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
}
/** test add */

View File

@ -668,6 +668,19 @@ public final class RealMatrixImplTest extends TestCase {
} catch (NullPointerException e) {
// expected
}
RealMatrixImpl m2 = new RealMatrixImpl();
try {
m2.setSubMatrix(testData,0,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
try {
m2.setSubMatrix(testData,1,0);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
// ragged
try {

View File

@ -113,7 +113,7 @@ public final class FrequencyTest extends TestCase {
assertEquals("one count", 3 , f.getCount("one"));
assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance);
assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance);
f = null;
f = new Frequency();
assertEquals(0L, f.getCount('a'));
@ -128,6 +128,7 @@ public final class FrequencyTest extends TestCase {
assertEquals(2L, f.getCumFreq('b'));
assertEquals(0.25, f.getPct('a'), 0.0);
assertEquals(0.5, f.getCumPct('b'), 0.0);
assertEquals(1.0, f.getCumPct('e'), 0.0);
}
/** test pcts */