MATH-1443: Depend on "Commons Statistics".
Simplify classes that remain in "Commons Math".
This commit is contained in:
parent
d2359cedd7
commit
b1a8299ad2
|
@ -46,7 +46,8 @@ import org.apache.commons.math4.util.FastMath;
|
|||
* @since 3.0
|
||||
*/
|
||||
public abstract class AbstractRealDistribution
|
||||
implements RealDistribution, Serializable {
|
||||
implements RealDistribution,
|
||||
Serializable {
|
||||
/** Default absolute accuracy for inverse cumulative computation. */
|
||||
public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
|
||||
/** Serializable version identifier */
|
||||
|
@ -131,8 +132,8 @@ public abstract class AbstractRealDistribution
|
|||
return upperBound;
|
||||
}
|
||||
|
||||
final double mu = getNumericalMean();
|
||||
final double sig = FastMath.sqrt(getNumericalVariance());
|
||||
final double mu = getMean();
|
||||
final double sig = FastMath.sqrt(getVariance());
|
||||
final boolean chebyshevApplies;
|
||||
chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) ||
|
||||
Double.isInfinite(sig) || Double.isNaN(sig));
|
||||
|
@ -245,8 +246,8 @@ public abstract class AbstractRealDistribution
|
|||
|
||||
/**{@inheritDoc} */
|
||||
@Override
|
||||
public Sampler createSampler(final UniformRandomProvider rng) {
|
||||
return new RealDistribution.Sampler() {
|
||||
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
|
||||
return new ContinuousDistribution.Sampler() {
|
||||
/**
|
||||
* Inversion method distribution sampler.
|
||||
*/
|
||||
|
|
|
@ -100,7 +100,8 @@ import org.apache.commons.math4.util.MathUtils;
|
|||
* </ul>
|
||||
*
|
||||
*/
|
||||
public class EmpiricalDistribution extends AbstractRealDistribution {
|
||||
public class EmpiricalDistribution extends AbstractRealDistribution
|
||||
implements ContinuousDistribution {
|
||||
|
||||
/** Default bin count */
|
||||
public static final int DEFAULT_BIN_COUNT = 1000;
|
||||
|
@ -623,7 +624,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
|
|||
* @since 3.1
|
||||
*/
|
||||
@Override
|
||||
public double getNumericalMean() {
|
||||
public double getMean() {
|
||||
return sampleStats.getMean();
|
||||
}
|
||||
|
||||
|
@ -632,7 +633,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
|
|||
* @since 3.1
|
||||
*/
|
||||
@Override
|
||||
public double getNumericalVariance() {
|
||||
public double getVariance() {
|
||||
return sampleStats.getVariance();
|
||||
}
|
||||
|
||||
|
@ -665,7 +666,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
|
|||
|
||||
/**{@inheritDoc} */
|
||||
@Override
|
||||
public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) {
|
||||
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
|
||||
if (!loaded) {
|
||||
throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED);
|
||||
}
|
||||
|
|
|
@ -26,159 +26,4 @@ import org.apache.commons.rng.UniformRandomProvider;
|
|||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface RealDistribution {
|
||||
/**
|
||||
* 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 point {@code x}
|
||||
*/
|
||||
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
|
||||
* the derivative of the {@link #cumulativeProbability(double) CDF}.
|
||||
* If the derivative does not exist at {@code x}, then an appropriate
|
||||
* replacement should be returned, e.g. {@code Double.POSITIVE_INFINITY},
|
||||
* {@code Double.NaN}, or the limit inferior or limit superior of the
|
||||
* difference quotient.
|
||||
*
|
||||
* @param x the point at which the PDF is evaluated
|
||||
* @return the value of the probability density function at point {@code x}
|
||||
*/
|
||||
double density(double x);
|
||||
|
||||
/**
|
||||
* Returns the natural logarithm of the probability density function
|
||||
* (PDF) of this distribution evaluated at the specified point {@code x}.
|
||||
* In general, the PDF is the derivative of the {@link #cumulativeProbability(double) CDF}.
|
||||
* If the derivative does not exist at {@code x}, then an appropriate replacement
|
||||
* should be returned, e.g. {@code Double.POSITIVE_INFINITY}, {@code Double.NaN},
|
||||
* or the limit inferior or limit superior of the difference quotient. 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 #density(double)}.
|
||||
*
|
||||
* @param x the point at which the PDF is evaluated
|
||||
* @return the logarithm of the value of the probability density function at point {@code x}
|
||||
* @since 4.0
|
||||
*/
|
||||
double logDensity(double 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 (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(double 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 R | P(X<=x) >= p}} for {@code 0 < p <= 1},</li>
|
||||
* <li>{@code inf{x in R | P(X<=x) > 0}} for {@code p = 0}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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}
|
||||
*/
|
||||
double 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} as
|
||||
* for certain cases in {@link TDistribution}) 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 R | P(X <= x) > 0}}.</p>
|
||||
*
|
||||
* @return lower bound of the support (might be
|
||||
* {@code Double.NEGATIVE_INFINITY})
|
||||
*/
|
||||
double 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 (might be
|
||||
* {@code Double.POSITIVE_INFINITY})
|
||||
*/
|
||||
double getSupportUpperBound();
|
||||
|
||||
/**
|
||||
* Use this method to get information about whether the support is connected,
|
||||
* i.e. whether all values 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 ContinuousDistribution.Sampler {
|
||||
/**
|
||||
* Generates a random value sampled from this distribution.
|
||||
*
|
||||
* @return a random value.
|
||||
*/
|
||||
double sample();
|
||||
}
|
||||
}
|
||||
public interface RealDistribution extends ContinuousDistribution {}
|
||||
|
|
|
@ -69,13 +69,13 @@ public class AbstractRealDistributionTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public double getNumericalMean() {
|
||||
public double getMean() {
|
||||
return ((x0 + x1) * p12 + (x2 + x3) * (1.0 - p12)) / 2.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getNumericalVariance() {
|
||||
final double meanX = getNumericalMean();
|
||||
public double getVariance() {
|
||||
final double meanX = getMean();
|
||||
final double meanX2;
|
||||
meanX2 = ((x0 * x0 + x0 * x1 + x1 * x1) * p12 + (x2 * x2 + x2
|
||||
* x3 + x3 * x3)
|
||||
|
@ -155,7 +155,7 @@ public class AbstractRealDistributionTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public double getNumericalMean() {
|
||||
public double getMean() {
|
||||
final UnivariateFunction f = new UnivariateFunction() {
|
||||
|
||||
@Override
|
||||
|
@ -168,8 +168,8 @@ public class AbstractRealDistributionTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public double getNumericalVariance() {
|
||||
final double meanX = getNumericalMean();
|
||||
public double getVariance() {
|
||||
final double meanX = getMean();
|
||||
final UnivariateFunction f = new UnivariateFunction() {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -270,7 +270,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
|
||||
private void tstGen(double tolerance)throws Exception {
|
||||
empiricalDistribution.load(url);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= empiricalDistribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
SummaryStatistics stats = new SummaryStatistics();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
|
@ -282,7 +282,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
|
||||
private void tstDoubleGen(double tolerance)throws Exception {
|
||||
empiricalDistribution2.load(dataArray);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= empiricalDistribution2.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
SummaryStatistics stats = new SummaryStatistics();
|
||||
for (int i = 1; i < 1000; i++) {
|
||||
|
@ -413,7 +413,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
}
|
||||
EmpiricalDistribution dist = new EmpiricalDistribution(10);
|
||||
dist.load(data);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final double dev = sampler.sample();
|
||||
|
@ -430,7 +430,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
final double[] data = {0, 0, 1, 1};
|
||||
EmpiricalDistribution dist = new EmpiricalDistribution(2);
|
||||
dist.load(data);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final double dev = sampler.sample();
|
||||
|
@ -473,7 +473,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
final EmpiricalDistribution dist = new ConstantKernelEmpiricalDistribution(5);
|
||||
final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d};
|
||||
dist.load(data);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
// Bin masses concentrated on 2, 5, 8, 11, 14 <- effectively discrete uniform distribution over these
|
||||
double[] values = {2d, 5d, 8d, 11d, 14d};
|
||||
|
@ -501,7 +501,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
|
|||
final EmpiricalDistribution dist = new UniformKernelEmpiricalDistribution(5);
|
||||
final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d};
|
||||
dist.load(data);
|
||||
RealDistribution.Sampler sampler
|
||||
ContinuousDistribution.Sampler sampler
|
||||
= dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000));
|
||||
// Kernels are uniform distributions on [1,3], [4,6], [7,9], [10,12], [13,15]
|
||||
final double bounds[] = {3d, 6d, 9d, 12d};
|
||||
|
|
|
@ -391,11 +391,11 @@ public abstract class RealDistributionAbstractTest {
|
|||
// generator, using a fixed seed for deterministic results.
|
||||
final long seed = 123;
|
||||
RandomSource source = RandomSource.WELL_512_A;
|
||||
RealDistribution.Sampler origSampler = distribution.createSampler(RandomSource.create(source, seed));
|
||||
ContinuousDistribution.Sampler origSampler = distribution.createSampler(RandomSource.create(source, seed));
|
||||
|
||||
// Clone the distribution.
|
||||
final RealDistribution cloned = deepClone();
|
||||
RealDistribution.Sampler clonedSampler = cloned.createSampler(RandomSource.create(source, seed));
|
||||
ContinuousDistribution.Sampler clonedSampler = cloned.createSampler(RandomSource.create(source, seed));
|
||||
|
||||
// Make sure they still produce the same samples.
|
||||
Assert.assertEquals(origSampler.sample(),
|
||||
|
|
Loading…
Reference in New Issue