Eliminated MathException from distribution interfaces and impls.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1178295 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2011-10-03 04:36:27 +00:00
parent 0cf21a482b
commit 2568420bf6
20 changed files with 73 additions and 223 deletions

View File

@ -18,9 +18,9 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils;
import org.apache.commons.math.exception.MathInternalError;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -69,13 +69,9 @@ public abstract class AbstractContinuousDistribution
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws OutOfRangeException if {@code p} is not a valid probability.
*/
public double inverseCumulativeProbability(final double p)
throws MathException {
try {
public double inverseCumulativeProbability(final double p) {
if (p < 0.0 || p > 1.0) {
throw new OutOfRangeException(p, 0, 1);
@ -86,16 +82,7 @@ public abstract class AbstractContinuousDistribution
UnivariateRealFunction rootFindingFunction =
new UnivariateRealFunction() {
public double value(double x) {
double ret = Double.NaN;
try {
ret = cumulativeProbability(x) - p;
} catch (MathException ex) {
throw new WrappingException(ex);
}
if (Double.isNaN(ret)) {
throw new WrappingException(new MathException(LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p));
}
return ret;
return cumulativeProbability(x) - p;
}
};
@ -120,7 +107,7 @@ public abstract class AbstractContinuousDistribution
return upperBound;
}
// Failed bracket convergence was not because of corner solution
throw new MathException(ex);
throw new MathInternalError(ex);
}
// find root
@ -129,35 +116,6 @@ public abstract class AbstractContinuousDistribution
// absolute accuracy different from the default.
bracket[0],bracket[1], getSolverAbsoluteAccuracy());
return root;
} catch (WrappingException we) {
throw we.getWrapped();
}
}
/** Local exception wrapping a MathException. */
private static class WrappingException extends RuntimeException {
/** Serializable UID. */
private static final long serialVersionUID = -2102700399222815344L;
/** Wrapped exception. */
private final MathException wrapped;
/** simple constructor.
* @param wrapped exception to wrap
*/
public WrappingException(final MathException wrapped) {
this.wrapped = wrapped;
}
/** Get the wrapped exception.
* @return wrapped exception
*/
public MathException getWrapped() {
return wrapped;
}
}
/**
@ -178,10 +136,9 @@ public abstract class AbstractContinuousDistribution
* </a>
*
* @return a random value.
* @throws MathException if an error occurs generating the random value.
* @since 2.2
*/
public double sample() throws MathException {
public double sample() {
return randomData.nextInversionDeviate(this);
}
@ -191,11 +148,10 @@ public abstract class AbstractContinuousDistribution
*
* @param sampleSize Number of random values to generate.
* @return an array representing the random sample.
* @throws MathException if an error occurs generating the sample.
* @throws NotStrictlyPositiveException if {@code sampleSize} is not positive.
* @since 2.2
*/
public double[] sample(int sampleSize) throws MathException {
public double[] sample(int sampleSize) {
if (sampleSize <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
sampleSize);

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -65,12 +64,9 @@ public abstract class AbstractDistribution
* @return the probability that a random variable with this distribution
* will take a value between {@code x0} and {@code x1},
* including the endpoints.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws NumberIsTooLargeException if {@code x0 > x1}
*/
public double cumulativeProbability(double x0, double x1)
throws MathException {
public double cumulativeProbability(double x0, double x1) {
if (x0 > x1) {
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x0, x1, true);

View File

@ -18,7 +18,7 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.MathInternalError;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException;
@ -60,10 +60,8 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
* @param x Value at which the distribution function is evaluated.
* @return the cumulative probability that a random variable with this
* distribution takes a value less than or equal to {@code x}.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
return cumulativeProbability((int) FastMath.floor(x));
}
@ -77,13 +75,10 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
* @return the probability that a random variable with this distribution
* will take a value between {@code x0} and {@code x1},
* including the endpoints.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws NumberIsTooSmallException if {@code x1 > x0}.
*/
@Override
public double cumulativeProbability(double x0, double x1)
throws MathException {
public double cumulativeProbability(double x0, double x1) {
if (x1 < x0) {
throw new NumberIsTooSmallException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x1, x0, true);
@ -105,10 +100,8 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*
* @param x Value at which the PDF is evaluated.
* @return PDF for this distribution.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public abstract double cumulativeProbability(int x) throws MathException;
public abstract double cumulativeProbability(int x);
/**
* For a random variable {@code X} whose values are distributed according
@ -136,11 +129,9 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
* @param x0 Inclusive lower bound.
* @param x1 Inclusive upper bound.
* @return the cumulative probability.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws NumberIsTooSmallException {@code if x0 > x1}.
*/
public double cumulativeProbability(int x0, int x1) throws MathException {
public double cumulativeProbability(int x0, int x1) {
if (x1 < x0) {
throw new NumberIsTooSmallException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x1, x0, true);
@ -155,11 +146,9 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*
* @param p Desired probability.
* @return the largest {@code x} such that {@code P(X < x) <= p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
*/
public int inverseCumulativeProbability(final double p) throws MathException{
public int inverseCumulativeProbability(final double p) {
if (p < 0 || p > 1) {
throw new OutOfRangeException(p, 0, 1);
}
@ -221,9 +210,8 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*
* @return a random value.
* @since 2.2
* @throws MathException if an error occurs generating the random value.
*/
public int sample() throws MathException {
public int sample() {
return randomData.nextInversionDeviate(this);
}
@ -235,10 +223,9 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
* @param sampleSize number of random values to generate.
* @since 2.2
* @return an array representing the random sample.
* @throws MathException if an error occurs generating the sample.
* @throws NotStrictlyPositiveException if {@code sampleSize <= 0}.
*/
public int[] sample(int sampleSize) throws MathException {
public int[] sample(int sampleSize) {
if (sampleSize <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
sampleSize);
@ -253,20 +240,21 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
/**
* Computes the cumulative probability function and checks for NaN
* values returned.
* Throws MathException if the value is NaN. Rethrows any MathException encountered
* Throws MathInternalError if the value is NaN. Rethrows any Exception encountered
* evaluating the cumulative probability function. Throws
* MathException if the cumulative probability function returns NaN.
* MathInternalError if the cumulative probability function returns NaN.
*
* @param argument Input value.
* @return the cumulative probability.
* @throws MathException if the cumulative probability is NaN
* @throws MathInternalError if the cumulative probability is NaN
*/
private double checkedCumulativeProbability(int argument)
throws MathException {
throws MathInternalError {
double result = Double.NaN;
result = cumulativeProbability(argument);
if (Double.isNaN(result)) {
throw new MathException(LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
throw new MathInternalError(
LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
}
return result;
}

View File

@ -16,7 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Gamma;
@ -128,7 +127,7 @@ public class BetaDistributionImpl
/** {@inheritDoc} */
@Override
public double inverseCumulativeProbability(double p) throws MathException {
public double inverseCumulativeProbability(double p) {
if (p == 0) {
return 0;
} else if (p == 1) {
@ -157,7 +156,7 @@ public class BetaDistributionImpl
}
/** {@inheritDoc} */
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
if (x <= 0) {
return 0;
} else if (x >= 1) {
@ -169,7 +168,7 @@ public class BetaDistributionImpl
/** {@inheritDoc} */
@Override
public double cumulativeProbability(double x0, double x1) throws MathException {
public double cumulativeProbability(double x0, double x1) {
return cumulativeProbability(x1) - cumulativeProbability(x0);
}

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -104,11 +103,10 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution
*
* @param x Value at which the PDF is evaluated.
* @return PDF for this distribution.
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
public double cumulativeProbability(int x) {
double ret;
if (x < 0) {
ret = 0.0;
@ -146,13 +144,10 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution
*
* @param p Desired probability.
* @return the largest {@code x} such that {@code P(X < x) <= p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
*/
@Override
public int inverseCumulativeProbability(final double p)
throws MathException {
public int inverseCumulativeProbability(final double p) {
// handle extreme values explicitly
if (p == 0) {
return -1;

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
/**
* The default implementation of {@link ChiSquaredDistribution}
@ -85,10 +84,8 @@ public class ChiSquaredDistributionImpl
*
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
* @throws MathException if the cumulative probability cannot be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
return gamma.cumulativeProbability(x);
}
@ -100,14 +97,11 @@ public class ChiSquaredDistributionImpl
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
*/
@Override
public double inverseCumulativeProbability(final double p)
throws MathException {
public double inverseCumulativeProbability(final double p) {
if (p == 0) {
return 0d;
}

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Base interface for continuous distributions.
*
@ -30,10 +28,8 @@ public interface ContinuousDistribution extends Distribution {
*
* @param p Cumulative probability.
* @return {@code x} such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
*/
double inverseCumulativeProbability(double p) throws MathException;
double inverseCumulativeProbability(double p);
/**
* Probability density for a particular point.
@ -55,20 +51,18 @@ public interface ContinuousDistribution extends Distribution {
* Generate a random value sampled from this distribution.
*
* @return a random value.
* @throws MathException if an error occurs generating the random value.
* @since 3.0
*/
double sample() throws MathException;
double sample();
/**
* Generate a random sample from the distribution.
*
* @param sampleSize number of random values to generate.
* @return an array representing the random sample.
* @throws MathException if an error occurs generating the sample.
* @throws org.apache.commons.math.exception.NotStrictlyPositiveException
* if {@code sampleSize} is not positive.
* @since 3.0
*/
double[] sample(int sampleSize) throws MathException;
double[] sample(int sampleSize);
}

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Base interface for probability distributions.
*
@ -33,10 +31,8 @@ public interface Distribution {
* @param x the value at which the distribution function is evaluated.
* @return the probability that a random variable with this
* distribution takes a value less than or equal to <code>x</code>
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
double cumulativeProbability(double x) throws MathException;
double cumulativeProbability(double x);
/**
* For a random variable X whose values are distributed according
@ -47,11 +43,9 @@ public interface Distribution {
* @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 MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if <code>x0 > x1</code>
*/
double cumulativeProbability(double x0, double x1) throws MathException;
double cumulativeProbability(double x0, double x1);
/**
* Use this method to get the numerical value of the mean of this

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -99,10 +98,8 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
*
* @param x Value at which the CDF is evaluated.
* @return the CDF for this distribution.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
double ret;
if (x <= 0.0) {
ret = 0.0;
@ -120,12 +117,10 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
*/
@Override
public double inverseCumulativeProbability(double p) throws MathException {
public double inverseCumulativeProbability(double p) {
double ret;
if (p < 0.0 || p > 1.0) {
@ -148,11 +143,10 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
* uniform deviates.</p>
*
* @return a random value.
* @throws MathException if an error occurs generating the random value.
* @since 2.2
*/
@Override
public double sample() throws MathException {
public double sample() {
return randomData.nextExponential(mean);
}

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Beta;
@ -123,10 +122,8 @@ public class FDistributionImpl
*
* @param x Value at which the CDF is evaluated.
* @return CDF for this distribution.
* @throws MathException if the cumulative probability cannot be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
double ret;
if (x <= 0) {
ret = 0;
@ -148,14 +145,11 @@ public class FDistributionImpl
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if {@code p} is not a valid
* probability.
*/
@Override
public double inverseCumulativeProbability(final double p)
throws MathException {
public double inverseCumulativeProbability(final double p) {
if (p == 0) {
return 0;
}

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Gamma;
@ -95,10 +94,8 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
*
* @param x Value at which the CDF is evaluated.
* @return CDF for this distribution.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException{
public double cumulativeProbability(double x) {
double ret;
if (x <= 0) {
@ -118,14 +115,11 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
*/
@Override
public double inverseCumulativeProbability(final double p)
throws MathException {
public double inverseCumulativeProbability(final double p) {
if (p == 0) {
return 0;
}

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Interface for discrete distributions of integer-valued random variables.
*
@ -43,10 +41,8 @@ public interface IntegerDistribution extends DiscreteDistribution {
*
* @param x Value at which the PDF is evaluated.
* @return PDF for this distribution.
* @throws MathException if the cumulative probability cannot be
* computed due to convergence or other numerical errors.
*/
double cumulativeProbability(int x) throws MathException;
double cumulativeProbability(int x);
/**
* For this distribution, {@code X}, this method returns
@ -55,11 +51,9 @@ public interface IntegerDistribution extends DiscreteDistribution {
* @param x0 the inclusive, lower bound
* @param x1 the inclusive, upper bound
* @return the cumulative probability.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if {@code x0 > x1}.
*/
double cumulativeProbability(int x0, int x1) throws MathException;
double cumulativeProbability(int x0, int x1);
/**
* For this distribution, {@code X}, this method returns the largest
@ -82,12 +76,10 @@ public interface IntegerDistribution extends DiscreteDistribution {
*
* @param p Cumulative probability.
* @return the largest {@code x} such that {@code P(X < x) <= p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if {@code p} is not between 0 and 1
* (inclusive).
*/
int inverseCumulativeProbability(double p) throws MathException;
int inverseCumulativeProbability(double p);
/**
* Reseed the random generator used to generate samples.
@ -101,20 +93,18 @@ public interface IntegerDistribution extends DiscreteDistribution {
* Generate a random value sampled from this distribution.
*
* @return a random value.
* @throws MathException if an error occurs generating the random value.
* @since 3.0
*/
int sample() throws MathException;
int sample();
/**
* Generate a random sample from the distribution.
*
* @param sampleSize number of random values to generate.
* @return an array representing the random sample.
* @throws MathException if an error occurs generating the sample.
* @throws org.apache.commons.math.exception.NotStrictlyPositiveException
* if {@code sampleSize} is not positive.
* @since 3.0
*/
int[] sample(int sampleSize) throws MathException;
int[] sample(int sampleSize);
}

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -121,9 +120,8 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
*
* @param x Value at which the CDF is evaluated.
* @return CDF evaluated at {@code x}.
* @throws MathException if the algorithm fails to converge
*/
public double cumulativeProbability(double x) throws MathException {
public double cumulativeProbability(double x) {
final double dev = x - mean;
if (FastMath.abs(dev) > 40 * standardDeviation) {
return dev < 0 ? 0.0d : 1.0d;
@ -135,7 +133,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
* {@inheritDoc}
*/
@Override
public double cumulativeProbability(double x0, double x1) throws MathException {
public double cumulativeProbability(double x0, double x1) {
if (x0 > x1) {
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
x0, x1, true);
@ -166,14 +164,12 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
*/
@Override
public double inverseCumulativeProbability(final double p)
throws MathException {
{
if (p == 0) {
return Double.NEGATIVE_INFINITY;
}
@ -188,10 +184,9 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
*
* @return a random value.
* @since 2.2
* @throws MathException if an error occurs generating the random value.
*/
@Override
public double sample() throws MathException {
public double sample() {
return randomData.nextGaussian(mean, standardDeviation);
}

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -104,11 +103,10 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution
*
* @param x Value at which the PDF is evaluated.
* @return PDF for this distribution.
* @throws MathException if the cumulative probability can not be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
public double cumulativeProbability(int x) {
double ret;
if (x < 0) {
ret = 0.0;
@ -145,13 +143,10 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution
*
* @param p Desired probability.
* @return the largest {@code x} such that {@code P(X <= x) <= p}.
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
*/
@Override
public int inverseCumulativeProbability(final double p)
throws MathException {
public int inverseCumulativeProbability(final double p) {
int ret;
// handle extreme values explicitly

View File

@ -16,7 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
/**
* Interface representing the Poisson Distribution.
@ -44,7 +43,6 @@ public interface PoissonDistribution extends IntegerDistribution {
*
* @param x the upper bound, inclusive
* @return the distribution function value calculated using a normal approximation
* @throws MathException if an error occurs computing the normal approximation
*/
double normalApproximateProbability(int x) throws MathException;
double normalApproximateProbability(int x) ;
}

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Gamma;
@ -146,11 +145,10 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
*
* @param x Value at which the PDF is evaluated.
* @return the Poisson distribution function evaluated at {@code x}.
* @throws MathException if the cumulative probability cannot be computed
* due to convergence or other numerical errors.
*/
@Override
public double cumulativeProbability(int x) throws MathException {
public double cumulativeProbability(int x) {
if (x < 0) {
return 0;
}
@ -170,10 +168,9 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
* @param x Upper bound, inclusive.
* @return the distribution function value calculated using a normal
* approximation.
* @throws MathException if an error occurs computing the normal
* approximation.
*/
public double normalApproximateProbability(int x) throws MathException {
public double normalApproximateProbability(int x) {
// calculate the probability using half-correction
return normal.cumulativeProbability(x + 0.5);
}
@ -198,10 +195,9 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
*
* @return a random value.
* @since 2.2
* @throws MathException if an error occurs generating the random value.
*/
@Override
public int sample() throws MathException {
public int sample() {
return (int) FastMath.min(randomData.nextPoisson(mean), Integer.MAX_VALUE);
}

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.special.Beta;
@ -101,10 +100,8 @@ public class TDistributionImpl
*
* @param x Value at which the CDF is evaluated.
* @return CDF evaluated at {@code x}.
* @throws MathException if the cumulative probability can not be
* computed due to convergence or other numerical errors.
*/
public double cumulativeProbability(double x) throws MathException{
public double cumulativeProbability(double x) {
double ret;
if (x == 0) {
ret = 0.5;
@ -132,14 +129,11 @@ public class TDistributionImpl
*
* @param p Desired probability.
* @return {@code x}, such that {@code P(X < x) = p}.
* @throws MathException if the inverse cumulative probability cannot be
* computed due to convergence or other numerical errors.
* @throws org.apache.commons.math.exception.OutOfRangeException if
* {@code p} is not a valid probability.
*/
@Override
public double inverseCumulativeProbability(final double p)
throws MathException {
public double inverseCumulativeProbability(final double p) {
if (p == 0) {
return Double.NEGATIVE_INFINITY;
}

View File

@ -24,7 +24,6 @@ import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Collection;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.BetaDistributionImpl;
import org.apache.commons.math.distribution.BinomialDistributionImpl;
import org.apache.commons.math.distribution.CauchyDistributionImpl;
@ -607,10 +606,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param alpha first distribution shape parameter
* @param beta second distribution shape parameter
* @return random value sampled from the beta(alpha, beta) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextBeta(double alpha, double beta) throws MathException {
public double nextBeta(double alpha, double beta) {
return nextInversionDeviate(new BetaDistributionImpl(alpha, beta));
}
@ -622,10 +620,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param numberOfTrials number of trials of the Binomial distribution
* @param probabilityOfSuccess probability of success of the Binomial distribution
* @return random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) throws MathException {
public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) {
return nextInversionDeviate(new BinomialDistributionImpl(numberOfTrials, probabilityOfSuccess));
}
@ -637,10 +634,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param median the median of the Cauchy distribution
* @param scale the scale parameter of the Cauchy distribution
* @return random value sampled from the Cauchy(median, scale) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextCauchy(double median, double scale) throws MathException {
public double nextCauchy(double median, double scale) {
return nextInversionDeviate(new CauchyDistributionImpl(median, scale));
}
@ -651,10 +647,9 @@ public class RandomDataImpl implements RandomData, Serializable {
*
* @param df the degrees of freedom of the ChiSquare distribution
* @return random value sampled from the ChiSquare(df) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextChiSquare(double df) throws MathException {
public double nextChiSquare(double df) {
return nextInversionDeviate(new ChiSquaredDistributionImpl(df));
}
@ -666,10 +661,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param numeratorDf the numerator degrees of freedom of the F distribution
* @param denominatorDf the denominator degrees of freedom of the F distribution
* @return random value sampled from the F(numeratorDf, denominatorDf) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextF(double numeratorDf, double denominatorDf) throws MathException {
public double nextF(double numeratorDf, double denominatorDf) {
return nextInversionDeviate(new FDistributionImpl(numeratorDf, denominatorDf));
}
@ -692,10 +686,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param shape the median of the Gamma distribution
* @param scale the scale parameter of the Gamma distribution
* @return random value sampled from the Gamma(shape, scale) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextGamma(double shape, double scale) throws MathException {
public double nextGamma(double shape, double scale) {
if (shape < 1) {
// [1]: p. 228, Algorithm GS
@ -770,10 +763,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
* @param sampleSize the sample size of the Hypergeometric distribution
* @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws MathException {
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) {
return nextInversionDeviate(new HypergeometricDistributionImpl(populationSize, numberOfSuccesses, sampleSize));
}
@ -785,10 +777,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param r the number of successes of the Pascal distribution
* @param p the probability of success of the Pascal distribution
* @return random value sampled from the Pascal(r, p) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public int nextPascal(int r, double p) throws MathException {
public int nextPascal(int r, double p) {
return nextInversionDeviate(new PascalDistributionImpl(r, p));
}
@ -799,10 +790,9 @@ public class RandomDataImpl implements RandomData, Serializable {
*
* @param df the degrees of freedom of the T distribution
* @return random value from the T(df) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextT(double df) throws MathException {
public double nextT(double df) {
return nextInversionDeviate(new TDistributionImpl(df));
}
@ -814,10 +804,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public double nextWeibull(double shape, double scale) throws MathException {
public double nextWeibull(double shape, double scale) {
return nextInversionDeviate(new WeibullDistributionImpl(shape, scale));
}
@ -829,10 +818,9 @@ public class RandomDataImpl implements RandomData, Serializable {
* @param numberOfElements the number of elements of the ZipfDistribution
* @param exponent the exponent of the ZipfDistribution
* @return random value sampled from the Zipf(numberOfElements, exponent) distribution
* @throws MathException if an error occurs generating the random value
* @since 2.2
*/
public int nextZipf(int numberOfElements, double exponent) throws MathException {
public int nextZipf(int numberOfElements, double exponent) {
return nextInversionDeviate(new ZipfDistributionImpl(numberOfElements, exponent));
}
@ -1044,10 +1032,9 @@ public class RandomDataImpl implements RandomData, Serializable {
*
* @param distribution Continuous distribution to generate a random value from
* @return a random value sampled from the given distribution
* @throws MathException if an error occurs computing the inverse cumulative distribution function
* @since 2.2
*/
public double nextInversionDeviate(ContinuousDistribution distribution) throws MathException {
public double nextInversionDeviate(ContinuousDistribution distribution) {
return distribution.inverseCumulativeProbability(nextUniform(0, 1));
}
@ -1058,10 +1045,9 @@ public class RandomDataImpl implements RandomData, Serializable {
*
* @param distribution Integer distribution to generate a random value from
* @return a random value sampled from the given distribution
* @throws MathException if an error occurs computing the inverse cumulative distribution function
* @since 2.2
*/
public int nextInversionDeviate(IntegerDistribution distribution) throws MathException {
public int nextInversionDeviate(IntegerDistribution distribution) {
final double target = nextUniform(0, 1);
final int glb = distribution.inverseCumulativeProbability(target);
if (distribution.cumulativeProbability(glb) == 1.0d) { // No mass above

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.junit.Assert;
import org.junit.Test;
@ -128,7 +127,7 @@ public class AbtractIntegerDistributionTest {
}
@Override
public double cumulativeProbability(int x) throws MathException {
public double cumulativeProbability(int x) {
if (x < 1) {
return 0;
} else if (x >= 6) {

View File

@ -16,7 +16,6 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.junit.Assert;
@ -169,7 +168,7 @@ public class PoissonDistributionTest extends IntegerDistributionAbstractTest {
Assert.assertTrue("Zero cum probaility returned for mean = " +
mean + " x = " + x, p > 0);
}
} catch (MathException ex) {
} catch (Exception ex) {
Assert.fail("mean of " + mean + " and x of " + x + " caused " + ex.getMessage());
}
x -= dx;
@ -216,7 +215,7 @@ public class PoissonDistributionTest extends IntegerDistributionAbstractTest {
// Verify that returned value satisties definition
Assert.assertTrue(p >= dist.cumulativeProbability(ret));
Assert.assertTrue(p < dist.cumulativeProbability(ret + 1));
} catch (MathException ex) {
} catch (Exception ex) {
Assert.fail("mean of " + mean + " and p of " + p + " caused " + ex.getMessage());
}
p += dp;