MATH-1443: Depend on "Commons Statistics".

Simplify classes that remain in "Commons Math".
This commit is contained in:
Gilles 2018-01-25 19:34:16 +01:00
parent b1a8299ad2
commit 4e93138656
5 changed files with 18 additions and 158 deletions

View File

@ -93,8 +93,8 @@ public abstract class AbstractIntegerDistribution implements IntegerDistribution
// use the one-sided Chebyshev inequality to narrow the bracket // use the one-sided Chebyshev inequality to narrow the bracket
// cf. AbstractRealDistribution.inverseCumulativeProbability(double) // cf. AbstractRealDistribution.inverseCumulativeProbability(double)
final double mu = getNumericalMean(); final double mu = getMean();
final double sigma = FastMath.sqrt(getNumericalVariance()); final double sigma = FastMath.sqrt(getVariance());
final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) ||
Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0);
if (chebyshevApplies) { if (chebyshevApplies) {
@ -197,8 +197,8 @@ public abstract class AbstractIntegerDistribution implements IntegerDistribution
/**{@inheritDoc} */ /**{@inheritDoc} */
@Override @Override
public Sampler createSampler(final UniformRandomProvider rng) { public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new IntegerDistribution.Sampler() { return new DiscreteDistribution.Sampler() {
/** /**
* Inversion method distribution sampler. * Inversion method distribution sampler.
*/ */

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.statistics.distribution.DiscreteDistribution;
import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.NotANumberException; import org.apache.commons.math4.exception.NotANumberException;
@ -150,7 +151,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
* @return {@code sum(singletons[i] * probabilities[i])} * @return {@code sum(singletons[i] * probabilities[i])}
*/ */
@Override @Override
public double getNumericalMean() { public double getMean() {
double mean = 0; double mean = 0;
for (final Pair<Integer, Double> sample : innerDistribution.getPmf()) { for (final Pair<Integer, Double> sample : innerDistribution.getPmf()) {
@ -166,7 +167,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
* @return {@code sum((singletons[i] - mean) ^ 2 * probabilities[i])} * @return {@code sum((singletons[i] - mean) ^ 2 * probabilities[i])}
*/ */
@Override @Override
public double getNumericalVariance() { public double getVariance() {
double mean = 0; double mean = 0;
double meanOfSquares = 0; double meanOfSquares = 0;
@ -230,8 +231,8 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public IntegerDistribution.Sampler createSampler(final UniformRandomProvider rng) { public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new IntegerDistribution.Sampler() { return new DiscreteDistribution.Sampler() {
/** Delegate. */ /** Delegate. */
private final EnumeratedDistribution<Integer>.Sampler inner = private final EnumeratedDistribution<Integer>.Sampler inner =
innerDistribution.createSampler(rng); innerDistribution.createSampler(rng);

View File

@ -24,146 +24,4 @@ import org.apache.commons.rng.UniformRandomProvider;
/** /**
* Interface for distributions on the integers. * Interface for distributions on the integers.
*/ */
public interface IntegerDistribution { public interface IntegerDistribution extends DiscreteDistribution {}
/**
* For a random variable {@code X} whose values are distributed according to
* this distribution, this method returns {@code log(P(X = x))}, where
* {@code log} is the natural logarithm. In other words, this method
* represents the logarithm of the probability mass function (PMF) for the
* distribution. Note that due to the floating point precision and
* under/overflow issues, this method will for some distributions be more
* precise and faster than computing the logarithm of
* {@link #probability(int)}.
*
* @param x the point at which the PMF is evaluated
* @return the logarithm of the value of the probability mass function at {@code x}
* @since 4.0
*/
double logProbability(int x);
/**
* For a random variable {@code X} whose values are distributed according
* to this distribution, this method returns {@code P(X = x)}. In other
* words, this method represents the probability mass function (PMF)
* for the distribution.
*
* @param x the point at which the PMF is evaluated
* @return the value of the probability mass function at {@code x}
*/
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
* words, this method represents the (cumulative) distribution function
* (CDF) for this distribution.
*
* @param x the point at which the CDF is evaluated
* @return the probability that a random variable with this
* distribution takes a value less than or equal to {@code x}
*/
double cumulativeProbability(int x);
/**
* Computes the quantile function of this distribution.
* For a random variable {@code X} distributed according to this distribution,
* the returned value is
* <ul>
* <li>{@code inf{x in Z | P(X<=x) >= p}} for {@code 0 < p <= 1},</li>
* <li>{@code inf{x in Z | P(X<=x) > 0}} for {@code p = 0}.</li>
* </ul>
* If the result exceeds the range of the data type {@code int},
* then {@code Integer.MIN_VALUE} or {@code Integer.MAX_VALUE} is returned.
*
* @param p the cumulative probability
* @return the smallest {@code p}-quantile of this distribution
* (largest 0-quantile for {@code p = 0})
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}
*/
int inverseCumulativeProbability(double p) throws OutOfRangeException;
/**
* Use this method to get the numerical value of the mean of this
* distribution.
*
* @return the mean or {@code Double.NaN} if it is not defined
*/
double getNumericalMean();
/**
* Use this method to get the numerical value of the variance of this
* distribution.
*
* @return the variance (possibly {@code Double.POSITIVE_INFINITY} or
* {@code Double.NaN} if it is not defined)
*/
double getNumericalVariance();
/**
* Access the lower bound of the support. This method must return the same
* value as {@code inverseCumulativeProbability(0)}. In other words, this
* method must return
* <p>{@code inf {x in Z | P(X <= x) > 0}}.</p>
*
* @return lower bound of the support ({@code Integer.MIN_VALUE}
* for negative infinity)
*/
int getSupportLowerBound();
/**
* Access the upper bound of the support. This method must return the same
* value as {@code inverseCumulativeProbability(1)}. In other words, this
* method must return
* <p>{@code inf {x in R | P(X <= x) = 1}}.</p>
*
* @return upper bound of the support ({@code Integer.MAX_VALUE}
* for positive infinity)
*/
int getSupportUpperBound();
/**
* Use this method to get information about whether the support is
* connected, i.e. whether all integers between the lower and upper bound of
* the support are included in the support.
*
* @return whether the support is connected or not
*/
boolean isSupportConnected();
/**
* Creates a sampler.
*
* @param rng Generator of uniformly distributed numbers.
* @return a sampler that produces random numbers according this
* distribution.
*/
Sampler createSampler(UniformRandomProvider rng);
/**
* Sampling functionality.
*/
interface Sampler extends DiscreteDistribution.Sampler {
/**
* Generates a random value sampled from this distribution.
*
* @return a random value.
*/
int sample();
}
}

View File

@ -104,12 +104,12 @@ public class AbstractIntegerDistributionTest {
} }
@Override @Override
public double getNumericalMean() { public double getMean() {
return 3.5; return 3.5;
} }
@Override @Override
public double getNumericalVariance() { public double getVariance() {
return 70/24; // E(X^2) - E(X)^2 return 70/24; // E(X^2) - E(X)^2
} }

View File

@ -18,6 +18,7 @@ package org.apache.commons.math4.distribution;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.apache.commons.statistics.distribution.DiscreteDistribution;
import org.apache.commons.math4.distribution.EnumeratedIntegerDistribution; import org.apache.commons.math4.distribution.EnumeratedIntegerDistribution;
import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.MathArithmeticException;
@ -117,7 +118,7 @@ public class EnumeratedIntegerDistributionTest {
*/ */
@Test @Test
public void testGetNumericalMean() { public void testGetNumericalMean() {
Assert.assertEquals(3.4, testDistribution.getNumericalMean(), 1e-10); Assert.assertEquals(3.4, testDistribution.getMean(), 1e-10);
} }
/** /**
@ -125,7 +126,7 @@ public class EnumeratedIntegerDistributionTest {
*/ */
@Test @Test
public void testGetNumericalVariance() { public void testGetNumericalVariance() {
Assert.assertEquals(7.84, testDistribution.getNumericalVariance(), 1e-10); Assert.assertEquals(7.84, testDistribution.getVariance(), 1e-10);
} }
/** /**
@ -158,7 +159,7 @@ public class EnumeratedIntegerDistributionTest {
@Test @Test
public void testSample() { public void testSample() {
final int n = 1000000; final int n = 1000000;
final IntegerDistribution.Sampler sampler final DiscreteDistribution.Sampler sampler
= testDistribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, = testDistribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C,
-334759360)); // fixed seed -334759360)); // fixed seed
final int[] samples = AbstractIntegerDistribution.sample(n, sampler); final int[] samples = AbstractIntegerDistribution.sample(n, sampler);
@ -169,9 +170,9 @@ public class EnumeratedIntegerDistributionTest {
sum += samples[i]; sum += samples[i];
sumOfSquares += samples[i] * samples[i]; sumOfSquares += samples[i] * samples[i];
} }
Assert.assertEquals(testDistribution.getNumericalMean(), Assert.assertEquals(testDistribution.getMean(),
sum / n, 1e-2); sum / n, 1e-2);
Assert.assertEquals(testDistribution.getNumericalVariance(), Assert.assertEquals(testDistribution.getVariance(),
sumOfSquares / n - FastMath.pow(sum / n, 2), 1e-2); sumOfSquares / n - FastMath.pow(sum / n, 2), 1e-2);
} }