Modifications to the hierarchy of distributions, according to MATH-692. Patch contributed by Christian Winter.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1200179 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2011-11-10 06:21:56 +00:00
parent 70667484eb
commit 87f0f14381
15 changed files with 134 additions and 153 deletions

View File

@ -60,18 +60,21 @@ public abstract class AbstractContinuousDistribution
/**
* {@inheritDoc}
*
* For continuous distributions {@code P(X = x)} always evaluates to 0.
*
* @return 0
*/
public abstract double density(double x);
@Override
public final double probability(double x) {
return 0.0;
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws OutOfRangeException if {@code p} is not a valid probability.
* {@inheritDoc}
*/
public double inverseCumulativeProbability(final double p) {
@Override
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
if (p < 0.0 || p > 1.0) {
throw new OutOfRangeException(p, 0, 1);
@ -81,6 +84,7 @@ public abstract class AbstractContinuousDistribution
// subclasses can override if there is a better method.
UnivariateRealFunction rootFindingFunction =
new UnivariateRealFunction() {
@Override
public double value(double x) {
return cumulativeProbability(x) - p;
}
@ -124,6 +128,7 @@ public abstract class AbstractContinuousDistribution
* @param seed New seed.
* @since 2.2
*/
@Override
public void reseedRandomGenerator(long seed) {
randomData.reSeed(seed);
}
@ -138,6 +143,7 @@ public abstract class AbstractContinuousDistribution
* @return a random value.
* @since 2.2
*/
@Override
public double sample() {
return randomData.nextInversionDeviate(this);
}
@ -151,6 +157,7 @@ public abstract class AbstractContinuousDistribution
* @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
* @since 2.2
*/
@Override
public double[] sample(int sampleSize) {
if (sampleSize <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,

View File

@ -52,21 +52,13 @@ public abstract class AbstractDistribution
}
/**
* For a random variable X whose values are distributed according
* to this distribution, this method returns P(x0 &le; X &le; x1).
* <p>
* The default implementation uses the identity</p>
* <p>
* P(x0 &le; X &le; x1) = P(X &le; x1) - P(X &le; x0) </p>
* {@inheritDoc}
*
* @param x0 the (inclusive) 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},
* including the endpoints.
* @throws NumberIsTooLargeException if {@code x0 > x1}
* The default implementation uses the identity
* <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
*/
public double cumulativeProbability(double x0, double x1) {
@Override
public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException {
if (x0 > x1) {
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x0, x1, true);
@ -89,6 +81,7 @@ public abstract class AbstractDistribution
*
* @return the mean or Double.NaN if it's not defined
*/
@Override
public double getNumericalMean() {
if (!numericalMeanIsCalculated) {
numericalMean = calculateNumericalMean();
@ -115,6 +108,7 @@ public abstract class AbstractDistribution
* for certain cases in {@link TDistributionImpl}) or
* Double.NaN if it's not defined
*/
@Override
public double getNumericalVariance() {
if (!numericalVarianceIsCalculated) {
numericalVariance = calculateNumericalVariance();
@ -130,6 +124,7 @@ public abstract class AbstractDistribution
*
* @return whether the lower bound of the support is inclusive or not
*/
@Override
public abstract boolean isSupportLowerBoundInclusive();
/**
@ -138,6 +133,7 @@ public abstract class AbstractDistribution
*
* @return whether the upper bound of the support is inclusive or not
*/
@Override
public abstract boolean isSupportUpperBoundInclusive();
/**
@ -159,6 +155,7 @@ public abstract class AbstractDistribution
*
* @return whether the support limits given by subclassed methods are connected or not
*/
@Override
public boolean isSupportConnected() {
return true;
}

View File

@ -166,12 +166,6 @@ public class BetaDistributionImpl
}
}
/** {@inheritDoc} */
@Override
public double cumulativeProbability(double x0, double x1) {
return cumulativeProbability(x1) - cumulativeProbability(x0);
}
/**
* Return the absolute accuracy setting of the solver used to estimate
* inverse cumulative probabilities.

View File

@ -87,11 +87,9 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
*
* @param x Value at which the CDF is evaluated.
* @return CDF evaluated at {@code x}.
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x) {
return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}
@ -99,6 +97,7 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getMedian() {
return median;
}
@ -106,6 +105,7 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getScale() {
return scale;
}
@ -120,17 +120,13 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
* It will return {@code Double.NEGATIVE_INFINITY} when p = 0 and
* {@code Double.POSITIVE_INFINITY} when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws OutOfRangeException if {@code p} is not a valid probability.
* It will return {@code Double.NEGATIVE_INFINITY} when {@code p = 0}
* and {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(double p) {
public double inverseCumulativeProbability(double p) throws OutOfRangeException {
double ret;
if (p < 0 || p > 1) {
throw new OutOfRangeException(p, 0, 1);

View File

@ -67,6 +67,7 @@ public class ChiSquaredDistributionImpl
/**
* {@inheritDoc}
*/
@Override
public double getDegreesOfFreedom() {
return gamma.getAlpha() * 2.0;
}
@ -80,25 +81,18 @@ public class ChiSquaredDistributionImpl
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
*
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x) {
return gamma.cumulativeProbability(x);
}
/**
* For this distribution, X, this method returns the critical point
* {@code x}, such that {@code P(X < x) = p}.
* It will return 0 when p = 0 and {@code Double.POSITIVE_INFINITY}
* when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
* It will return {@code 0} when {@code p = 0} and
* {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(final double p) {

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.exception.OutOfRangeException;
/**
* Base interface for continuous distributions.
*
@ -23,19 +25,27 @@ package org.apache.commons.math.distribution;
*/
public interface ContinuousDistribution extends Distribution {
/**
* For a distribution, {@code X}, compute {@code x} such that
* {@code P(X < x) = p}.
* 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}</code> for {@code 0 < p <= 1},</li>
* <li><code>inf{x in R | P(X<=x) > 0}</code> for {@code p = 0}.</li>
* </ul>
*
* @param p Cumulative probability.
* @return {@code x} such that {@code P(X < x) = p}.
* @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);
double inverseCumulativeProbability(double p) throws OutOfRangeException;
/**
* Probability density for a particular point.
* Returns the probability density function (PDF) of this distribution
* evaluated at the specified point.
*
* @param x Point at which the density should be computed.
* @return the pdf at point {@code x}.
* @param x the point at which the PDF should be evaluated
* @return the PDF at point {@code x}
*/
double density(double x);

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.exception.NumberIsTooLargeException;
/**
* Base interface for probability distributions.
*
@ -23,29 +25,40 @@ package org.apache.commons.math.distribution;
*/
public interface Distribution {
/**
* For a random variable X whose values are distributed according
* to this distribution, this method returns P(X &le; x). In other words,
* this method represents the (cumulative) distribution function, or
* CDF, for this distribution.
* 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 value at which the distribution function is evaluated.
* @param x the value at which the PMF is evaluated
* @return the value of the probability mass function at {@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(X <= x)}. In other
* words, this method represents the (cumulative) distribution function
* (CDF) for this distribution.
*
* @param x the value 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</code>
* distribution takes a value less than or equal to {@code x}
*/
double cumulativeProbability(double x);
/**
* For a random variable X whose values are distributed according
* to this distribution, this method returns P(x0 &le; X &le; x1).
* 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 (inclusive) lower bound
* @param x1 the (inclusive) upper bound
* @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</code> and <code>x1</code>,
* including the endpoints
* @throws IllegalArgumentException if <code>x0 > x1</code>
* takes a value between {@code x0} and {@code x1},
* excluding the lower and including the upper endpoint
* @throws NumberIsTooLargeException if {@code x0 > x1}
*/
double cumulativeProbability(double x0, double x1);
double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException;
/**
* Use this method to get the numerical value of the mean of this

View File

@ -71,6 +71,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getMean() {
return mean;
}
@ -87,7 +88,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, X, this method returns P(X &lt; x).
* {@inheritDoc}
*
* The implementation of this method is based on:
* <ul>
@ -95,10 +96,8 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
* <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
* Exponential Distribution</a>, equation (1).</li>
* </ul>
*
* @param x Value at which the CDF is evaluated.
* @return the CDF for this distribution.
*/
@Override
public double cumulativeProbability(double x) {
double ret;
if (x <= 0.0) {
@ -110,17 +109,13 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, X, this method returns the critical point x, such
* that {@code P(X < x) = p}.
* It will return 0 when p = 0 and {@code Double.POSITIVE_INFINITY}
* when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
* It will return {@code 0} when {@code p = 0} and
* {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(double p) {
public double inverseCumulativeProbability(double p) throws OutOfRangeException {
double ret;
if (p < 0.0 || p > 1.0) {

View File

@ -20,6 +20,7 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Beta;
import org.apache.commons.math.util.FastMath;
@ -89,10 +90,8 @@ public class FDistributionImpl
}
/**
* Returns the probability density for a particular point.
* {@inheritDoc}
*
* @param x The point at which the density should be computed.
* @return The pdf at point x.
* @since 2.1
*/
@Override
@ -110,7 +109,7 @@ public class FDistributionImpl
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
* {@inheritDoc}
*
* The implementation of this method is based on
* <ul>
@ -119,10 +118,8 @@ public class FDistributionImpl
* F-Distribution</a>, equation (4).
* </li>
* </ul>
*
* @param x Value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
@Override
public double cumulativeProbability(double x) {
double ret;
if (x <= 0) {
@ -139,17 +136,13 @@ public class FDistributionImpl
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
* Returns 0 when p = 0 and {@code Double.POSITIVE_INFINITY} when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws IllegalArgumentException if {@code p} is not a valid
* probability.
* It will return {@code 0} when {@code p = 0} and
* {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(final double p) {
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
if (p == 0) {
return 0;
}
@ -207,6 +200,7 @@ public class FDistributionImpl
/**
* {@inheritDoc}
*/
@Override
public double getNumeratorDegreesOfFreedom() {
return numeratorDegreesOfFreedom;
}
@ -214,6 +208,7 @@ public class FDistributionImpl
/**
* {@inheritDoc}
*/
@Override
public double getDenominatorDegreesOfFreedom() {
return denominatorDegreesOfFreedom;
}

View File

@ -79,7 +79,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
* {@inheritDoc}
*
* The implementation of this method is based on:
* <ul>
@ -91,10 +91,8 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
* Belmont, CA: Duxbury Press.
* </li>
* </ul>
*
* @param x Value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
@Override
public double cumulativeProbability(double x) {
double ret;
@ -108,15 +106,10 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
* It will return 0 when p = 0 and {@code Double.POSITIVE_INFINITY}
* when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
* It will return {@code 0} when {@cod p = 0} and
* {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(final double p) {
@ -132,6 +125,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getAlpha() {
return alpha;
}
@ -139,6 +133,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getBeta() {
return beta;
}

View File

@ -92,6 +92,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getMean() {
return mean;
}
@ -99,6 +100,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getStandardDeviation() {
return standardDeviation;
}
@ -114,13 +116,12 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
* If {@code x}is more than 40 standard deviations from the mean, 0 or 1 is returned,
* as in these cases the actual value is within {@code Double.MIN_VALUE} of 0 or 1.
* {@inheritDoc}
*
* @param x Value at which the CDF is evaluated.
* @return CDF evaluated at {@code x}.
* If {@code x} is more than 40 standard deviations from the mean, 0 or 1 is returned,
* as in these cases the actual value is within {@code Double.MIN_VALUE} of 0 or 1.
*/
@Override
public double cumulativeProbability(double x) {
final double dev = x - mean;
if (FastMath.abs(dev) > 40 * standardDeviation) {
@ -133,7 +134,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x0, double x1) {
public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException {
if (x0 > x1) {
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x0, x1, true);
@ -157,19 +158,13 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, X, this method returns the critical point
* {@code x}, such that {@code P(X < x) = p}.
* It will return {@code Double.NEGATIVE_INFINITY} when p = 0 and
* {@code Double.POSITIVE_INFINITY} for p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
* It will return {@code Double.NEGATIVE_INFINITY} when {@code p = 0}
* and {@code Double.POSITIVE_INFINITY} for {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(final double p)
{
public double inverseCumulativeProbability(final double p) {
if (p == 0) {
return Double.NEGATIVE_INFINITY;
}

View File

@ -79,6 +79,7 @@ public class TDistributionImpl
*
* @return the degrees of freedom.
*/
@Override
public double getDegreesOfFreedom() {
return degreesOfFreedom;
}
@ -96,11 +97,9 @@ public class TDistributionImpl
}
/**
* For this distribution, X, this method returns {@code P(X < x}).
*
* @param x Value at which the CDF is evaluated.
* @return CDF evaluated at {@code x}.
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x) {
double ret;
if (x == 0) {
@ -122,15 +121,10 @@ public class TDistributionImpl
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
* Returns {@code Double.NEGATIVE_INFINITY} when p = 0 and
* {@code Double.POSITIVE_INFINITY} when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
* It will return {@code Double.NEGATIVE_INFINITY} when {@cod p = 0}
* and {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(final double p) {

View File

@ -88,11 +88,9 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns {@code P(X < x)}.
*
* @param x Value at which the CDF is evaluated.
* @return the CDF evaluated at {@code x}.
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x) {
double ret;
if (x <= 0.0) {
@ -106,6 +104,7 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getShape() {
return shape;
}
@ -113,6 +112,7 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
/**
* {@inheritDoc}
*/
@Override
public double getScale() {
return scale;
}
@ -140,14 +140,10 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
}
/**
* For this distribution, {@code X}, this method returns the critical
* point {@code x}, such that {@code P(X < x) = p}.
* It will return {@code Double.NEGATIVE_INFINITY} when p = 0 and
* {@code Double.POSITIVE_INFINITY} when p = 1.
* {@inheritDoc}
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws OutOfRangeException if {@code p} is not a valid probability.
* It will return {@code 0} when {@code p = 0} and
* {@code Double.POSITIVE_INFINITY} when {@code p = 1}.
*/
@Override
public double inverseCumulativeProbability(double p) {

View File

@ -238,7 +238,7 @@ public abstract class ContinuousDistributionAbstractTest {
distribution.cumulativeProbability
(cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
// check that P(a < X < b) = P(X < b) - P(X < a)
// check that P(a < X <= b) = P(X <= b) - P(X <= a)
double upper = FastMath.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
double lower = FastMath.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
double diff = distribution.cumulativeProbability(upper) -

View File

@ -72,7 +72,7 @@ public class TDistributionTest extends ContinuousDistributionAbstractTest {
* Bug report that prompted this unit test.</a>
*/
@Test
public void testCumulativeProbabilityAgaintStackOverflow() throws Exception {
public void testCumulativeProbabilityAgainstStackOverflow() throws Exception {
TDistributionImpl td = new TDistributionImpl(5.);
td.cumulativeProbability(.1);
td.cumulativeProbability(.01);