[MATH-839] Renamed cumulativeProbability(double, double) to probability(double, double) in IntegerDistribution and RealDistribution. Thanks to Gilles.

This commit is contained in:
Thomas Neidhart 2015-02-25 21:43:34 +01:00
parent 0351963e6b
commit 3fd9cf1753
15 changed files with 62 additions and 112 deletions

View File

@ -54,6 +54,10 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>
<release version="4.0" date="XXXX-XX-XX" description="">
<action dev="tn" type="update" issue="MATH-839" due-to="Gilles Sadowski">
Renamed "cumulativeProbability(double, double)" to "probability(double, double)"
in "IntegerDistribution" and "RealDistribution".
</action>
<action dev="tn" type="add" issue="MATH-1039" due-to="Aleksei Dievskii">
Added logDensity(double) to RealDistribution and logProbability(int)
to IntegerDistribution interface. The implementations have already been

View File

@ -56,8 +56,10 @@ public abstract class AbstractIntegerDistribution implements IntegerDistribution
*
* The default implementation uses the identity
* <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
*
* @since 4.0, was previously named cumulativeProbability
*/
public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException {
public double probability(int x0, int x1) throws NumberIsTooLargeException {
if (x1 < x0) {
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x0, x1, true);

View File

@ -58,20 +58,6 @@ implements RealDistribution, Serializable {
random = rng;
}
/**
* {@inheritDoc}
*
* The default implementation uses the identity
* <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
*
* @deprecated As of 3.1 (to be removed in 4.0). Please use
* {@link #probability(double,double)} instead.
*/
@Deprecated
public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException {
return probability(x0, x1);
}
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(x0 < X <= x1)}.

View File

@ -51,6 +51,21 @@ public interface IntegerDistribution {
*/
double probability(int x);
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(x0 < X <= x1)}.
*
* @param x0 the exclusive lower bound
* @param x1 the inclusive upper bound
* @return the probability that a random variable with this distribution
* will take a value between {@code x0} and {@code x1},
* excluding the lower and including the upper endpoint
* @throws NumberIsTooLargeException if {@code x0 > x1}
*
* @since 4.0, was previously named cumulativeProbability
*/
double probability(int x0, int x1) throws NumberIsTooLargeException;
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(X <= x)}. In other
@ -63,19 +78,6 @@ public interface IntegerDistribution {
*/
double cumulativeProbability(int x);
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(x0 < X <= x1)}.
*
* @param x0 the exclusive lower bound
* @param x1 the inclusive upper bound
* @return the probability that a random variable with this distribution
* will take a value between {@code x0} and {@code x1},
* excluding the lower and including the upper endpoint
* @throws NumberIsTooLargeException if {@code x0 > x1}
*/
double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException;
/**
* Computes the quantile function of this distribution.
* For a random variable {@code X} distributed according to this distribution,

View File

@ -254,17 +254,6 @@ public class LogNormalDistribution extends AbstractRealDistribution {
return 0.5 + 0.5 * Erf.erf(dev / (shape * SQRT2));
}
/**
* {@inheritDoc}
*
* @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
*/
@Override@Deprecated
public double cumulativeProbability(double x0, double x1)
throws NumberIsTooLargeException {
return probability(x0, x1);
}
/** {@inheritDoc} */
@Override
public double probability(double x0,

View File

@ -205,17 +205,6 @@ public class NormalDistribution extends AbstractRealDistribution {
return mean + standardDeviation * SQRT2 * Erf.erfInv(2 * p - 1);
}
/**
* {@inheritDoc}
*
* @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
*/
@Override@Deprecated
public double cumulativeProbability(double x0, double x1)
throws NumberIsTooLargeException {
return probability(x0, x1);
}
/** {@inheritDoc} */
@Override
public double probability(double x0,

View File

@ -215,18 +215,6 @@ public class ParetoDistribution extends AbstractRealDistribution {
return 1 - FastMath.pow(scale / x, shape);
}
/**
* {@inheritDoc}
*
* @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
*/
@Override
@Deprecated
public double cumulativeProbability(double x0, double x1)
throws NumberIsTooLargeException {
return probability(x0, x1);
}
/** {@inheritDoc} */
@Override
protected double getSolverAbsoluteAccuracy() {

View File

@ -36,6 +36,21 @@ public interface RealDistribution {
*/
double probability(double x);
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(x0 < X <= x1)}.
*
* @param x0 the exclusive lower bound
* @param x1 the inclusive upper bound
* @return the probability that a random variable with this distribution
* takes a value between {@code x0} and {@code x1},
* excluding the lower and including the upper endpoint
* @throws NumberIsTooLargeException if {@code x0 > x1}
*
* @since 4.0, was previously named cumulativeProbability
*/
double probability(double x0, double x1) throws NumberIsTooLargeException;
/**
* Returns the probability density function (PDF) of this distribution
* evaluated at the specified point {@code x}. In general, the PDF is
@ -79,23 +94,6 @@ public interface RealDistribution {
*/
double cumulativeProbability(double x);
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(x0 < X <= x1)}.
*
* @param x0 the exclusive lower bound
* @param x1 the inclusive upper bound
* @return the probability that a random variable with this distribution
* takes a value between {@code x0} and {@code x1},
* excluding the lower and including the upper endpoint
* @throws NumberIsTooLargeException if {@code x0 > x1}
*
* @deprecated As of 3.1. In 4.0, this method will be renamed
* {@code probability(double x0, double x1)}.
*/
@Deprecated
double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException;
/**
* Computes the quantile function of this distribution. For a random
* variable {@code X} distributed according to this distribution, the

View File

@ -765,8 +765,8 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
private double kB(int i) {
final double[] binBounds = getUpperBounds();
final RealDistribution kernel = getKernel(binStats.get(i));
return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
return i == 0 ? kernel.probability(min, binBounds[0]) :
kernel.probability(binBounds[i - 1], binBounds[i]);
}
/**

View File

@ -60,18 +60,18 @@ public class AbstractIntegerDistributionTest {
}
@Test
public void testCumulativeProbabilitiesRangeArguments() {
public void testProbabilitiesRangeArguments() {
int lower = 0;
int upper = 6;
for (int i = 0; i < 2; i++) {
// cum(0,6) = p(0 < X <= 6) = 1, cum(1,5) = 4/6, cum(2,4) = 2/6
Assert.assertEquals(1 - p * 2 * i,
diceDistribution.cumulativeProbability(lower, upper), 1E-12);
diceDistribution.probability(lower, upper), 1E-12);
lower++;
upper--;
}
for (int i = 0; i < 6; i++) {
Assert.assertEquals(p, diceDistribution.cumulativeProbability(i, i+1), 1E-12);
Assert.assertEquals(p, diceDistribution.probability(i, i+1), 1E-12);
}
}

View File

@ -86,7 +86,7 @@ public class ExponentialDistributionTest extends RealDistributionAbstractTest {
@Test
public void testCumulativeProbability2() {
double actual = getDistribution().cumulativeProbability(0.25, 0.75);
double actual = getDistribution().probability(0.25, 0.75);
Assert.assertEquals(0.0905214, actual, 10e-4);
}

View File

@ -262,7 +262,7 @@ public abstract class IntegerDistributionAbstractTest {
@Test
public void testIllegalArguments() {
try {
distribution.cumulativeProbability(1, 0);
distribution.probability(1, 0);
Assert.fail("Expecting MathIllegalArgumentException for bad cumulativeProbability interval");
} catch (MathIllegalArgumentException ex) {
// expected

View File

@ -29,7 +29,6 @@ import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.analysis.integration.BaseAbstractUnivariateIntegrator;
import org.apache.commons.math4.analysis.integration.IterativeLegendreGaussIntegrator;
import org.apache.commons.math4.distribution.AbstractRealDistribution;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
@ -52,7 +51,7 @@ import org.junit.Test;
* defined by the arrays returned by the makeCumulativeXxx methods.
* <p>
* makeCumulativeTestPoints() -- arguments used to test cumulative probabilities
* makeCumulativeTestValues() -- expected cumulative probabilites
* makeCumulativeTestValues() -- expected cumulative probabilities
* makeDensityTestValues() -- expected density values at cumulativeTestPoints
* makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf
* makeInverseCumulativeTestValues() -- expected inverse cdf values
@ -61,7 +60,7 @@ import org.junit.Test;
* test data, use the setXxx methods for the instance data in test cases and
* call the verifyXxx methods to verify results.
* <p>
* Error tolerance can be overriden by implementing getTolerance().
* Error tolerance can be overridden by implementing getTolerance().
* <p>
* Test data should be validated against reference tables or other packages
* where possible, and the source of the reference data and/or validation
@ -181,21 +180,20 @@ public abstract class RealDistributionAbstractTest {
distribution.cumulativeProbability(cumulativeTestPoints[i]),
getTolerance());
}
// verify cumulativeProbability(double, double)
// XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839).
// verify probability(double, double)
for (int i = 0; i < cumulativeTestPoints.length; i++) {
for (int j = 0; j < cumulativeTestPoints.length; j++) {
if (cumulativeTestPoints[i] <= cumulativeTestPoints[j]) {
TestUtils.assertEquals(cumulativeTestValues[j] - cumulativeTestValues[i],
distribution.cumulativeProbability(cumulativeTestPoints[i], cumulativeTestPoints[j]),
distribution.probability(cumulativeTestPoints[i], cumulativeTestPoints[j]),
getTolerance());
} else {
try {
distribution.cumulativeProbability(cumulativeTestPoints[i], cumulativeTestPoints[j]);
distribution.probability(cumulativeTestPoints[i], cumulativeTestPoints[j]);
} catch (NumberIsTooLargeException e) {
continue;
}
Assert.fail("distribution.cumulativeProbability(double, double) should have thrown an exception that second argument is too large");
Assert.fail("distribution.probability(double, double) should have thrown an exception that second argument is too large");
}
}
}
@ -230,11 +228,10 @@ public abstract class RealDistributionAbstractTest {
* Verifies that logarithmic density calculations match expected values
*/
protected void verifyLogDensities() {
// FIXME: when logProbability methods are added to RealDistribution in 4.0, remove cast below
for (int i = 0; i < cumulativeTestPoints.length; i++) {
TestUtils.assertEquals("Incorrect probability density value returned for "
+ cumulativeTestPoints[i], logDensityTestValues[i],
((AbstractRealDistribution) distribution).logDensity(cumulativeTestPoints[i]),
distribution.logDensity(cumulativeTestPoints[i]),
getTolerance());
}
}
@ -285,9 +282,8 @@ public abstract class RealDistributionAbstractTest {
for (int i=1; i < cumulativeTestPoints.length; i++) {
// check that cdf(x, x) = 0
// XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839).
TestUtils.assertEquals(0d,
distribution.cumulativeProbability
distribution.probability
(cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
// check that P(a < X <= b) = P(X <= b) - P(X <= a)
@ -295,9 +291,8 @@ public abstract class RealDistributionAbstractTest {
double lower = FastMath.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
double diff = distribution.cumulativeProbability(upper) -
distribution.cumulativeProbability(lower);
// XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839).
double direct = distribution.cumulativeProbability(lower, upper);
TestUtils.assertEquals("Inconsistent cumulative probabilities for ("
double direct = distribution.probability(lower, upper);
TestUtils.assertEquals("Inconsistent probability for ("
+ lower + "," + upper + ")", diff, direct, tolerance);
}
}
@ -308,8 +303,7 @@ public abstract class RealDistributionAbstractTest {
@Test
public void testIllegalArguments() {
try {
// XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839).
distribution.cumulativeProbability(1, 0);
distribution.probability(1, 0);
Assert.fail("Expecting MathIllegalArgumentException for bad cumulativeProbability interval");
} catch (MathIllegalArgumentException ex) {
// expected
@ -375,7 +369,7 @@ public abstract class RealDistributionAbstractTest {
Collections.sort(integrationTestPoints);
for (int i = 1; i < integrationTestPoints.size(); i++) {
Assert.assertEquals(
distribution.cumulativeProbability( // FIXME @4.0 when rename happens
distribution.probability(
integrationTestPoints.get(0), integrationTestPoints.get(i)),
integrator.integrate(
1000000, // Triangle integrals are very slow to converge

View File

@ -59,12 +59,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
public void setUp() {
super.setUp();
empiricalDistribution = new EmpiricalDistribution(100);
// empiricalDistribution = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
url = getClass().getResource("testData.txt");
final ArrayList<Double> list = new ArrayList<Double>();
try {
empiricalDistribution2 = new EmpiricalDistribution(100);
// empiricalDistribution2 = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API
BufferedReader in =
new BufferedReader(new InputStreamReader(
url.openStream()));
@ -351,8 +349,8 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
// First bin has mass 11 / 10000, the rest have mass 10 / 10000.
final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
final RealDistribution kernel = findKernel(lower, upper);
final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
final double withinBinKernelMass = kernel.probability(lower, upper);
final double kernelCum = kernel.probability(lower, testPoints[i]);
cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
}
return cumValues;
@ -370,7 +368,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
binBounds[bin - 1];
final double upper = binBounds[bin];
final RealDistribution kernel = findKernel(lower, upper);
final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
final double withinBinKernelMass = kernel.probability(lower, upper);
final double density = kernel.density(testPoints[i]);
densityValues[i] = density * (bin == 0 ? firstBinMass : binMass) / withinBinKernelMass;
}
@ -399,7 +397,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
final double[] upper = {5, 12, 1030, 5010, 10000};
for (int i = 1; i < 5; i++) {
Assert.assertEquals(
distribution.cumulativeProbability(
distribution.probability(
lower[i], upper[i]),
integrator.integrate(
1000000, // Triangle integrals are very slow to converge

View File

@ -474,8 +474,8 @@ public class RandomDataGeneratorTest {
double upperBinMass = 0;
while (!widthSufficient) {
binWidth++;
lowerBinMass = poissonDistribution.cumulativeProbability(lower - 1, lower + binWidth - 1);
upperBinMass = poissonDistribution.cumulativeProbability(upper - binWidth - 1, upper - 1);
lowerBinMass = poissonDistribution.probability(lower - 1, lower + binWidth - 1);
upperBinMass = poissonDistribution.probability(upper - binWidth - 1, upper - 1);
widthSufficient = FastMath.min(lowerBinMass, upperBinMass) * sampleSize >= minExpectedCount;
}