Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 346007D89 for ; Sat, 3 Dec 2011 06:17:59 +0000 (UTC) Received: (qmail 16593 invoked by uid 500); 3 Dec 2011 06:17:57 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 15656 invoked by uid 500); 3 Dec 2011 06:17:52 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 15620 invoked by uid 99); 3 Dec 2011 06:17:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Dec 2011 06:17:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Dec 2011 06:17:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E7B332388A32 for ; Sat, 3 Dec 2011 06:17:22 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1209836 [2/2] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/distribution/ main/java/org/apache/commons/math/random/ test/java/org/apache/commons/math/ test/java/org/apache/commons/math/distribution/ Date: Sat, 03 Dec 2011 06:17:21 -0000 To: commits@commons.apache.org From: celestin@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111203061722.E7B332388A32@eris.apache.org> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java Sat Dec 3 06:17:19 2011 @@ -16,8 +16,6 @@ */ package org.apache.commons.math.distribution; -import java.io.Serializable; - import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.NotPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -46,8 +44,7 @@ import org.apache.commons.math.util.Fast * @version $Id$ * @since 1.2 (changed to concrete class in 3.0) */ -public class PascalDistribution extends AbstractIntegerDistribution - implements Serializable { +public class PascalDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = 6751309484392813623L; /** The number of successes. */ @@ -98,41 +95,27 @@ public class PascalDistribution extends } /** {@inheritDoc} */ - @Override - protected int getDomainLowerBound(double p) { - return -1; - } - - /** {@inheritDoc} */ - @Override - protected int getDomainUpperBound(double p) { - // use MAX - 1 because MAX causes loop - return Integer.MAX_VALUE - 1; - } - - /** {@inheritDoc} */ - @Override - public double cumulativeProbability(int x) { + public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { - ret = Beta.regularizedBeta(probabilityOfSuccess, - numberOfSuccesses, x + 1); + ret = ArithmeticUtils.binomialCoefficientDouble(x + + numberOfSuccesses - 1, numberOfSuccesses - 1) * + FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * + FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; } /** {@inheritDoc} */ - public double probability(int x) { + public double cumulativeProbability(int x) { double ret; if (x < 0) { ret = 0.0; } else { - ret = ArithmeticUtils.binomialCoefficientDouble(x + - numberOfSuccesses - 1, numberOfSuccesses - 1) * - FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * - FastMath.pow(1.0 - probabilityOfSuccess, x); + ret = Beta.regularizedBeta(probabilityOfSuccess, + numberOfSuccesses, x + 1); } return ret; } @@ -159,32 +142,17 @@ public class PascalDistribution extends return ret; } - /** - * {@inheritDoc} - * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ + /** {@inheritDoc} */ @Override - public int getSupportLowerBound() { - return 0; + protected int getDomainLowerBound(double p) { + return -1; } - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity no matter the - * parameters. Positive infinity is symbolised by {@code Integer.MAX_VALUE} - * together with {@link #isSupportUpperBoundInclusive()} being - * {@code false}. - * - * @return upper bound of the support (always {@code Integer.MAX_VALUE} - * for positive infinity) - */ + /** {@inheritDoc} */ @Override - public int getSupportUpperBound() { - return Integer.MAX_VALUE; + protected int getDomainUpperBound(double p) { + // use MAX - 1 because MAX causes loop + return Integer.MAX_VALUE - 1; } /** @@ -193,8 +161,7 @@ public class PascalDistribution extends * For number of successes {@code r} and probability of success {@code p}, * the mean is {@code (r * p) / (1 - p)}. */ - @Override - protected double calculateNumericalMean() { + public double getNumericalMean() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return (r * p) / (1 - p); @@ -204,10 +171,9 @@ public class PascalDistribution extends * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, - * the mean is {@code (r * p) / (1 - p)^2}. + * the variance is {@code (r * p) / (1 - p)^2}. */ - @Override - protected double calculateNumericalVariance() { + public double getNumericalVariance() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); final double pInv = 1 - p; @@ -217,12 +183,35 @@ public class PascalDistribution extends /** * {@inheritDoc} * - * Always returns {@code false}. + * The lower bound of the support is always 0 no matter the parameters. * - * @see PascalDistribution#getSupportUpperBound() getSupportUpperBound() + * @return lower bound of the support (always 0) */ - @Override - public boolean isSupportUpperBoundInclusive() { - return false; + public int getSupportLowerBound() { + return 0; + } + + /** + * {@inheritDoc} + * + * The upper bound of the support is always positive infinity no matter the + * parameters. Positive infinity is symbolised by {@code Integer.MAX_VALUE}. + * + * @return upper bound of the support (always {@code Integer.MAX_VALUE} + * for positive infinity) + */ + public int getSupportUpperBound() { + return Integer.MAX_VALUE; + } + + /** + * {@inheritDoc} + * + * The support of this distribution is connected. + * + * @return {@code true} + */ + public boolean isSupportConnected() { + return true; } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java Sat Dec 3 06:17:19 2011 @@ -16,8 +16,6 @@ */ package org.apache.commons.math.distribution; -import java.io.Serializable; - import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.special.Gamma; @@ -31,8 +29,7 @@ import org.apache.commons.math.util.Fast * @see Poisson distribution (MathWorld) * @version $Id$ */ -public class PoissonDistribution extends AbstractIntegerDistribution - implements Serializable { +public class PoissonDistribution extends AbstractIntegerDistribution { /** * Default maximum number of iterations for cumulative probability calculations. * @since 2.1 @@ -72,7 +69,7 @@ public class PoissonDistribution extends * @param p the Poisson mean * @throws NotStrictlyPositiveException if {@code p <= 0}. */ - public PoissonDistribution(double p) { + public PoissonDistribution(double p) throws NotStrictlyPositiveException { this(p, DEFAULT_EPSILON, DEFAULT_MAX_ITERATIONS); } @@ -84,9 +81,11 @@ public class PoissonDistribution extends * @param epsilon Convergence criterion for cumulative probabilities. * @param maxIterations the maximum number of iterations for cumulative * probabilities. + * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 2.1 */ - public PoissonDistribution(double p, double epsilon, int maxIterations) { + public PoissonDistribution(double p, double epsilon, int maxIterations) + throws NotStrictlyPositiveException { if (p <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p); } @@ -102,9 +101,11 @@ public class PoissonDistribution extends * * @param p Poisson mean. * @param epsilon Convergence criterion for cumulative probabilities. + * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 2.1 */ - public PoissonDistribution(double p, double epsilon) { + public PoissonDistribution(double p, double epsilon) + throws NotStrictlyPositiveException { this(p, epsilon, DEFAULT_MAX_ITERATIONS); } @@ -146,7 +147,6 @@ public class PoissonDistribution extends } /** {@inheritDoc} */ - @Override public double cumulativeProbability(int x) { if (x < 0) { return 0; @@ -174,40 +174,14 @@ public class PoissonDistribution extends return normal.cumulativeProbability(x + 0.5); } - /** - * {@inheritDoc} - *

- * Algorithm Description: - *

    - *
  • For small means, uses simulation of a Poisson process - * using Uniform deviates, as described - * here. - * The Poisson process (and hence value returned) is bounded by 1000 * mean. - *
  • - *
  • For large means, uses the rejection algorithm described in - * - * Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables - * Computing vol. 26 pp. 197-207. - * - *
  • - *
- *

- * - * @return a random value. - * @since 2.2 - */ - @Override - public int sample() { - return (int) FastMath.min(randomData.nextPoisson(mean), Integer.MAX_VALUE); - } - /** {@inheritDoc} */ @Override protected int getDomainLowerBound(double p) { return 0; } - /** {@inheritDoc} */ @Override + /** {@inheritDoc} */ + @Override protected int getDomainUpperBound(double p) { return Integer.MAX_VALUE; } @@ -215,11 +189,28 @@ public class PoissonDistribution extends /** * {@inheritDoc} * + * For mean parameter {@code p}, the mean is {@code p}. + */ + public double getNumericalMean() { + return getMean(); + } + + /** + * {@inheritDoc} + * + * For mean parameter {@code p}, the variance is {@code p}. + */ + public double getNumericalVariance() { + return getMean(); + } + + /** + * {@inheritDoc} + * * The lower bound of the support is always 0 no matter the mean parameter. * * @return lower bound of the support (always 0) */ - @Override public int getSupportLowerBound() { return 0; } @@ -235,7 +226,6 @@ public class PoissonDistribution extends * @return upper bound of the support (always {@code Integer.MAX_VALUE} for * positive infinity) */ - @Override public int getSupportUpperBound() { return Integer.MAX_VALUE; } @@ -243,26 +233,38 @@ public class PoissonDistribution extends /** * {@inheritDoc} * - * For mean parameter {@code p}, the mean is {@code p}. + * The support of this distribution is connected. + * + * @return {@code true} */ - @Override - protected double calculateNumericalMean() { - return getMean(); + public boolean isSupportConnected() { + return true; } /** * {@inheritDoc} + *

+ * Algorithm Description: + *

    + *
  • For small means, uses simulation of a Poisson process + * using Uniform deviates, as described + * here. + * The Poisson process (and hence value returned) is bounded by 1000 * mean. + *
  • + *
  • For large means, uses the rejection algorithm described in + * + * Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables + * Computing vol. 26 pp. 197-207. + * + *
  • + *
+ *

* - * For mean parameter {@code p}, the variance is {@code p}. + * @return a random value. + * @since 2.2 */ @Override - protected double calculateNumericalVariance() { - return getMean(); - } - - /** {@inheritDoc} */ - @Override - public boolean isSupportUpperBoundInclusive() { - return true; + public int sample() { + return (int) FastMath.min(randomData.nextPoisson(mean), Integer.MAX_VALUE); } } Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java?rev=1209836&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java Sat Dec 3 06:17:19 2011 @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.distribution; + +import org.apache.commons.math.exception.NumberIsTooLargeException; +import org.apache.commons.math.exception.OutOfRangeException; + +/** + * Base interface for distributions on the reals. + * + * @version $Id$ + * @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); + + /** + * 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); + + /** + * 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); + + /** + * 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} + */ + 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 + * returned value is + *
    + *
  • inf{x in R | P(X<=x) >= p} for {@code 0 < p <= 1},
  • + *
  • inf{x in R | P(X<=x) > 0} for {@code p = 0}.
  • + *
+ * + * @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 TDistributionImpl}) or + * {@code Double.NaN} if it is not defined + */ + double getNumericalVariance(); + + /** + * Access the lower bound of the support. + * + * @return lower bound of the support (might be + * {@code Double.NEGATIVE_INFINITY}) + */ + double getSupportLowerBound(); + + /** + * Access the upper bound of the support. + * + * @return upper bound of the support (might be + * {@code Double.POSITIVE_INFINITY}) + */ + double getSupportUpperBound(); + + /** + * Use this method to get information about whether the lower bound + * of the support is inclusive or not. + * + * @return whether the lower bound of the support is inclusive or not + */ + boolean isSupportLowerBoundInclusive(); + + /** + * Use this method to get information about whether the upper bound + * of the support is inclusive or not. + * + * @return whether the upper bound of the support is inclusive or not + */ + boolean isSupportUpperBoundInclusive(); + + /** + * 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(); + + /** + * Reseed the random generator used to generate samples. + * + * @param seed the new seed + */ + void reseedRandomGenerator(long seed); + + /** + * Generate a random value sampled from this distribution. + * + * @return a random value. + */ + double sample(); + + /** + * Generate a random sample from the distribution. + * + * @param sampleSize the number of random values to generate + * @return an array representing the random sample + * @throws org.apache.commons.math.exception.NotStrictlyPositiveException + * if {@code sampleSize} is not positive + */ + double[] sample(int sampleSize); +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/SaddlePointExpansion.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/SaddlePointExpansion.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/SaddlePointExpansion.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/SaddlePointExpansion.java Sat Dec 3 06:17:19 2011 @@ -166,8 +166,8 @@ final class SaddlePointExpansion { } /** - * Compute the PMF for a binomial distribution using the saddle point - * expansion. + * Compute the logarithm of the PMF for a binomial distribution + * using the saddle point expansion. * * @param x the value at which the probability is evaluated. * @param n the number of trials. Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/TDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/TDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/TDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/TDistribution.java Sat Dec 3 06:17:19 2011 @@ -16,8 +16,6 @@ */ package org.apache.commons.math.distribution; -import java.io.Serializable; - import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.special.Beta; @@ -31,8 +29,7 @@ import org.apache.commons.math.util.Fast * @see "Student's t-distribution (MathWorld)" * @version $Id$ */ -public class TDistribution extends AbstractContinuousDistribution - implements Serializable { +public class TDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 @@ -86,6 +83,17 @@ public class TDistribution extends Abstr return degreesOfFreedom; } + /** + * {@inheritDoc} + * + * For this distribution {@code P(X = x)} always evaluates to 0. + * + * @return 0 + */ + public double probability(double x) { + return 0.0; + } + /** {@inheritDoc} */ public double density(double x) { final double n = degreesOfFreedom; @@ -162,42 +170,13 @@ public class TDistribution extends Abstr /** * {@inheritDoc} * - * The lower bound of the support is always negative infinity no matter the - * parameters. - * - * @return lower bound of the support (always - * {@code Double.NEGATIVE_INFINITY}) - */ - @Override - public double getSupportLowerBound() { - return Double.NEGATIVE_INFINITY; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity no matter the - * parameters. - * - * @return upper bound of the support (always - * {@code Double.POSITIVE_INFINITY}) - */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; - } - - /** - * {@inheritDoc} - * * For degrees of freedom parameter {@code df}, the mean is *
    *
  • if {@code df > 1} then {@code 0},
  • *
  • else undefined ({@code Double.NaN}).
  • *
*/ - @Override - protected double calculateNumericalMean() { + public double getNumericalMean() { final double df = getDegreesOfFreedom(); if (df > 1) { @@ -218,8 +197,7 @@ public class TDistribution extends Abstr *
  • else undefined ({@code Double.NaN}).
  • * */ - @Override - protected double calculateNumericalVariance() { + public double getNumericalVariance() { final double df = getDegreesOfFreedom(); if (df > 2) { @@ -233,15 +211,50 @@ public class TDistribution extends Abstr return Double.NaN; } + /** + * {@inheritDoc} + * + * The lower bound of the support is always negative infinity no matter the + * parameters. + * + * @return lower bound of the support (always + * {@code Double.NEGATIVE_INFINITY}) + */ + public double getSupportLowerBound() { + return Double.NEGATIVE_INFINITY; + } + + /** + * {@inheritDoc} + * + * The upper bound of the support is always positive infinity no matter the + * parameters. + * + * @return upper bound of the support (always + * {@code Double.POSITIVE_INFINITY}) + */ + public double getSupportUpperBound() { + return Double.POSITIVE_INFINITY; + } + /** {@inheritDoc} */ - @Override public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ - @Override public boolean isSupportUpperBoundInclusive() { return false; } + + /** + * {@inheritDoc} + * + * The support of this distribution is connected. + * + * @return {@code true} + */ + public boolean isSupportConnected() { + return true; + } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java Sat Dec 3 06:17:19 2011 @@ -17,8 +17,6 @@ package org.apache.commons.math.distribution; -import java.io.Serializable; - import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -36,22 +34,37 @@ import org.apache.commons.math.util.Fast * @since 1.1 (changed to concrete class in 3.0) * @version $Id$ */ -public class WeibullDistribution extends AbstractContinuousDistribution - implements Serializable { +public class WeibullDistribution extends AbstractRealDistribution { + /** Serializable version identifier. */ + private static final long serialVersionUID = 8589540077390120676L; + /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; - /** Serializable version identifier. */ - private static final long serialVersionUID = 8589540077390120676L; + /** The shape parameter. */ private final double shape; + /** The scale parameter. */ private final double scale; + /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; + /** Cached numerical mean */ + private double numericalMean = Double.NaN; + + /** Whether or not the numerical mean has been calculated */ + private boolean numericalMeanIsCalculated = false; + + /** Cached numerical variance */ + private double numericalVariance = Double.NaN; + + /** Whether or not the numerical variance has been calculated */ + private boolean numericalVarianceIsCalculated = false; + /** * Create a Weibull distribution with the given shape and scale and a * location equal to zero. @@ -95,17 +108,6 @@ public class WeibullDistribution extends solverAbsoluteAccuracy = inverseCumAccuracy; } - /** {@inheritDoc} */ - public double cumulativeProbability(double x) { - double ret; - if (x <= 0.0) { - ret = 0.0; - } else { - ret = 1.0 - FastMath.exp(-FastMath.pow(x / scale, shape)); - } - return ret; - } - /** * Access the shape parameter, {@code alpha}. * @@ -124,6 +126,17 @@ public class WeibullDistribution extends return scale; } + /** + * {@inheritDoc} + * + * For this distribution {@code P(X = x)} always evaluates to 0. + * + * @return 0 + */ + public double probability(double x) { + return 0.0; + } + /** {@inheritDoc} */ public double density(double x) { if (x < 0) { @@ -143,6 +156,17 @@ public class WeibullDistribution extends return (shape / scale) * xscalepow * FastMath.exp(-xscalepowshape); } + /** {@inheritDoc} */ + public double cumulativeProbability(double x) { + double ret; + if (x <= 0.0) { + ret = 0.0; + } else { + ret = 1.0 - FastMath.exp(-FastMath.pow(x / scale, shape)); + } + return ret; + } + /** * {@inheritDoc} * @@ -170,12 +194,14 @@ public class WeibullDistribution extends return 0; } - /** {@inheritDoc} */ @Override + /** {@inheritDoc} */ + @Override protected double getDomainUpperBound(double p) { return Double.MAX_VALUE; } - /** {@inheritDoc} */ @Override + /** {@inheritDoc} */ + @Override protected double getInitialDomain(double p) { // use median return FastMath.pow(scale * FastMath.log(2.0), 1.0 / shape); @@ -196,36 +222,22 @@ public class WeibullDistribution extends /** * {@inheritDoc} * - * The lower bound of the support is always 0 no matter the parameters. - * - * @return lower bound of the support (always 0) - */ - @Override - public double getSupportLowerBound() { - return 0; - } - - /** - * {@inheritDoc} - * - * The upper bound of the support is always positive infinity - * no matter the parameters. - * - * @return upper bound of the support (always - * {@code Double.POSITIVE_INFINITY}) + * The mean is {@code scale * Gamma(1 + (1 / shape))}, where {@code Gamma()} + * is the Gamma-function. */ - @Override - public double getSupportUpperBound() { - return Double.POSITIVE_INFINITY; + public double getNumericalMean() { + if (!numericalMeanIsCalculated) { + numericalMean = calculateNumericalMean(); + numericalMeanIsCalculated = true; + } + return numericalMean; } /** - * {@inheritDoc} - * - * The mean is {@code scale * Gamma(1 + (1 / shape))}, where {@code Gamma()} - * is the Gamma-function. + * used by {@link #getNumericalMean()} + * + * @return the mean of this distribution */ - @Override protected double calculateNumericalMean() { final double sh = getShape(); final double sc = getScale(); @@ -239,26 +251,70 @@ public class WeibullDistribution extends * The variance is {@code scale^2 * Gamma(1 + (2 / shape)) - mean^2} * where {@code Gamma()} is the Gamma-function. */ - @Override + public double getNumericalVariance() { + if (!numericalVarianceIsCalculated) { + numericalVariance = calculateNumericalVariance(); + numericalVarianceIsCalculated = true; + } + return numericalVariance; + } + + /** + * used by {@link #getNumericalVariance()} + * + * @return the variance of this distribution + */ protected double calculateNumericalVariance() { final double sh = getShape(); final double sc = getScale(); final double mn = getNumericalMean(); - return (sc * sc) * - FastMath.exp(Gamma.logGamma(1 + (2 / sh))) - - (mn * mn); + return (sc * sc) * FastMath.exp(Gamma.logGamma(1 + (2 / sh))) + - (mn * mn); + } + + /** + * {@inheritDoc} + * + * The lower bound of the support is always 0 no matter the parameters. + * + * @return lower bound of the support (always 0) + */ + public double getSupportLowerBound() { + return 0; + } + + /** + * {@inheritDoc} + * + * The upper bound of the support is always positive infinity + * no matter the parameters. + * + * @return upper bound of the support (always + * {@code Double.POSITIVE_INFINITY}) + */ + public double getSupportUpperBound() { + return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ - @Override public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ - @Override public boolean isSupportUpperBoundInclusive() { return false; } + + /** + * {@inheritDoc} + * + * The support of this distribution is connected. + * + * @return {@code true} + */ + public boolean isSupportConnected() { + return true; + } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java Sat Dec 3 06:17:19 2011 @@ -17,8 +17,6 @@ package org.apache.commons.math.distribution; -import java.io.Serializable; - import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.FastMath; @@ -29,15 +27,28 @@ import org.apache.commons.math.util.Fast * @see Zipf distribution (MathWorld) * @version $Id$ */ -public class ZipfDistribution extends AbstractIntegerDistribution - implements Serializable { +public class ZipfDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = -140627372283420404L; + /** Number of elements. */ private final int numberOfElements; + /** Exponent parameter of the distribution. */ private final double exponent; + /** Cached numerical mean */ + private double numericalMean = Double.NaN; + + /** Whether or not the numerical mean has been calculated */ + private boolean numericalMeanIsCalculated = false; + + /** Cached numerical variance */ + private double numericalVariance = Double.NaN; + + /** Whether or not the numerical variance has been calculated */ + private boolean numericalVarianceIsCalculated = false; + /** * Create a new Zipf distribution with the given number of elements and * exponent. @@ -90,7 +101,6 @@ public class ZipfDistribution extends Ab } /** {@inheritDoc} */ - @Override public double cumulativeProbability(final int x) { if (x <= 0) { return 0.0; @@ -114,6 +124,74 @@ public class ZipfDistribution extends Ab } /** + * {@inheritDoc} + * + * For number of elements {@code N} and exponent {@code s}, the mean is + * {@code Hs1 / Hs}, where + *
      + *
    • {@code Hs1 = generalizedHarmonic(N, s - 1)},
    • + *
    • {@code Hs = generalizedHarmonic(N, s)}.
    • + *
    + */ + public double getNumericalMean() { + if (!numericalMeanIsCalculated) { + numericalMean = calculateNumericalMean(); + numericalMeanIsCalculated = true; + } + return numericalMean; + } + + /** + * Used by {@link #getNumericalMean()}. + * + * @return the mean of this distribution + */ + protected double calculateNumericalMean() { + final int N = getNumberOfElements(); + final double s = getExponent(); + + final double Hs1 = generalizedHarmonic(N, s - 1); + final double Hs = generalizedHarmonic(N, s); + + return Hs1 / Hs; + } + + /** + * {@inheritDoc} + * + * For number of elements {@code N} and exponent {@code s}, the mean is + * {@code (Hs2 / Hs) - (Hs1^2 / Hs^2)}, where + *
      + *
    • {@code Hs2 = generalizedHarmonic(N, s - 2)},
    • + *
    • {@code Hs1 = generalizedHarmonic(N, s - 1)},
    • + *
    • {@code Hs = generalizedHarmonic(N, s)}.
    • + *
    + */ + public double getNumericalVariance() { + if (!numericalVarianceIsCalculated) { + numericalVariance = calculateNumericalVariance(); + numericalVarianceIsCalculated = true; + } + return numericalVariance; + } + + /** + * used by {@link #getNumericalVariance()} + * + * @return the variance of this distribution + */ + protected double calculateNumericalVariance() { + final int N = getNumberOfElements(); + final double s = getExponent(); + + final double Hs2 = generalizedHarmonic(N, s - 2); + final double Hs1 = generalizedHarmonic(N, s - 1); + final double Hs = generalizedHarmonic(N, s); + + return (Hs2 / Hs) - ((Hs1 * Hs1) / (Hs * Hs)); + } + + /** * Calculates the Nth generalized harmonic number. See * Harmonic * Series. @@ -137,7 +215,6 @@ public class ZipfDistribution extends Ab * * @return lower bound of the support (always 1) */ - @Override public int getSupportLowerBound() { return 1; } @@ -149,52 +226,18 @@ public class ZipfDistribution extends Ab * * @return upper bound of the support */ - @Override public int getSupportUpperBound() { return getNumberOfElements(); } /** * {@inheritDoc} - * - * For number of elements {@code N} and exponent {@code s}, the mean is - * {@code Hs1 / Hs}, where - *
      - *
    • {@code Hs1 = generalizedHarmonic(N, s - 1)},
    • - *
    • {@code Hs = generalizedHarmonic(N, s)}.
    • - *
    - */ - @Override - protected double calculateNumericalMean() { - final int N = getNumberOfElements(); - final double s = getExponent(); - - final double Hs1 = generalizedHarmonic(N, s - 1); - final double Hs = generalizedHarmonic(N, s); - - return Hs1 / Hs; - } - - /** - * {@inheritDoc} - * - * For number of elements {@code N} and exponent {@code s}, the mean is - * {@code (Hs2 / Hs) - (Hs1^2 / Hs^2)}, where - *
      - *
    • {@code Hs2 = generalizedHarmonic(N, s - 2)},
    • - *
    • {@code Hs1 = generalizedHarmonic(N, s - 1)},
    • - *
    • {@code Hs = generalizedHarmonic(N, s)}.
    • - *
    + * + * The support of this distribution is connected. + * + * @return {@code true} */ - @Override - protected double calculateNumericalVariance() { - final int N = getNumberOfElements(); - final double s = getExponent(); - - final double Hs2 = generalizedHarmonic(N, s - 2); - final double Hs1 = generalizedHarmonic(N, s - 1); - final double Hs = generalizedHarmonic(N, s); - - return (Hs2 / Hs) - ((Hs1 * Hs1) / (Hs * Hs)); + public boolean isSupportConnected() { + return true; } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java Sat Dec 3 06:17:19 2011 @@ -28,7 +28,7 @@ import org.apache.commons.math.distribut import org.apache.commons.math.distribution.BinomialDistribution; import org.apache.commons.math.distribution.CauchyDistribution; import org.apache.commons.math.distribution.ChiSquaredDistribution; -import org.apache.commons.math.distribution.ContinuousDistribution; +import org.apache.commons.math.distribution.RealDistribution; import org.apache.commons.math.distribution.FDistribution; import org.apache.commons.math.distribution.HypergeometricDistribution; import org.apache.commons.math.distribution.IntegerDistribution; @@ -600,7 +600,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link BetaDistribution Beta Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param alpha first distribution shape parameter @@ -614,7 +614,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link BinomialDistribution Binomial Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param numberOfTrials number of trials of the Binomial distribution @@ -628,7 +628,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link CauchyDistribution Cauchy Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param median the median of the Cauchy distribution @@ -642,7 +642,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link ChiSquaredDistribution ChiSquare Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the ChiSquare distribution @@ -655,7 +655,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link FDistribution F Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param numeratorDf the numerator degrees of freedom of the F distribution @@ -785,7 +785,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link TDistribution T Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the T distribution @@ -798,7 +798,7 @@ public class RandomDataImpl implements R /** * Generates a random value from the {@link WeibullDistribution Weibull Distribution}. - * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} + * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param shape the shape parameter of the Weibull distribution @@ -1035,7 +1035,7 @@ public class RandomDataImpl implements R * @return a random value sampled from the given distribution * @since 2.2 */ - public double nextInversionDeviate(ContinuousDistribution distribution) { + public double nextInversionDeviate(RealDistribution distribution) { return distribution.inverseCumulativeProbability(nextUniform(0, 1)); } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java Sat Dec 3 06:17:19 2011 @@ -27,7 +27,7 @@ import java.text.DecimalFormat; import org.apache.commons.math.complex.Complex; import org.apache.commons.math.complex.ComplexFormat; -import org.apache.commons.math.distribution.ContinuousDistribution; +import org.apache.commons.math.distribution.RealDistribution; import org.apache.commons.math.linear.FieldMatrix; import org.apache.commons.math.linear.RealMatrix; import org.apache.commons.math.stat.inference.ChiSquareTest; @@ -439,7 +439,7 @@ public class TestUtils { * Computes the 25th, 50th and 75th percentiles of the given distribution and returns * these values in an array. */ - public static double[] getDistributionQuartiles(ContinuousDistribution distribution) throws Exception { + public static double[] getDistributionQuartiles(RealDistribution distribution) throws Exception { double[] quantiles = new double[3]; quantiles[0] = distribution.inverseCumulativeProbability(0.25d); quantiles[1] = distribution.inverseCumulativeProbability(0.5d); Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/AbtractIntegerDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/AbtractIntegerDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/AbtractIntegerDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/AbtractIntegerDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.commons.math.distribution; import org.junit.Assert; @@ -45,25 +44,7 @@ public class AbtractIntegerDistributionT } @Test - public void testCumulativeProbabilitiesSingleDoubleArguments() throws Exception { - int lower = 1; - double arg = 0; - for (int i = 1; i < 7; i++) { - // Exact integer - arg = lower; - Assert.assertEquals(p * i, - diceDistribution.cumulativeProbability(arg), Double.MIN_VALUE); - - // Add a fraction - arg = lower + Math.random(); - Assert.assertEquals(p * i, - diceDistribution.cumulativeProbability(arg), Double.MIN_VALUE); - lower++; - } - } - - @Test - public void testCumulativeProbabilitiesRangeIntegerArguments() throws Exception { + public void testCumulativeProbabilitiesRangeArguments() throws Exception { int lower = 1; int upper = 6; for (int i = 0; i < 2; i++) { @@ -78,46 +59,14 @@ public class AbtractIntegerDistributionT } } - @Test - public void testCumulativeProbabilitiesRangeDoubleArguments() throws Exception { - int lower = 1; - int upper = 6; - double dlower = lower; - double dupper = upper; - for (int i = 0; i < 2; i++) { - // cum(1,6) = p(1 <= X <= 6) = 1, cum(2,5) = 4/6, cum(3,4) = 2/6 - // Exact integers - Assert.assertEquals(1 - p * 2 * i, - diceDistribution.cumulativeProbability(dlower, dupper), 1E-12); - // Subtract a fraction from lower, add to upper. Should be no change. - dlower -= Math.random(); - dupper += Math.random(); - Assert.assertEquals(1 - p * 2 * i, - diceDistribution.cumulativeProbability(dlower, dupper), 1E-12); - lower++; - upper--; - dlower = lower; - dupper = upper; - } - for (int i = 1; i < 7; i++) { - lower = i; - Assert.assertEquals(p, diceDistribution.cumulativeProbability( - lower, lower), 1E-12); - Assert.assertEquals(p, diceDistribution.cumulativeProbability( - lower, lower + Math.random()), 1E-12); - Assert.assertEquals(p, diceDistribution.cumulativeProbability( - lower - Math.random(), lower), 1E-12); - Assert.assertEquals(p, diceDistribution.cumulativeProbability( - lower - Math.random(), lower + Math.random()), 1E-12); - } - } - /** * Simple distribution modeling a 6-sided die */ class DiceDistribution extends AbstractIntegerDistribution { public static final long serialVersionUID = 23734213; + private final double p = 1d/6d; + public double probability(int x) { if (x < 1 || x > 6) { return 0; @@ -126,7 +75,6 @@ public class AbtractIntegerDistributionT } } - @Override public double cumulativeProbability(int x) { if (x < 1) { return 0; @@ -147,24 +95,24 @@ public class AbtractIntegerDistributionT return 6; } - @Override + public double getNumericalMean() { + return 3.5; + } + + public double getNumericalVariance() { + return 12.5 - 3.5 * 3.5; // E(X^2) - E(X)^2 + } + public int getSupportLowerBound() { return 1; } - @Override public int getSupportUpperBound() { return 6; } - @Override - protected double calculateNumericalMean() { - return 3.5; - } - - @Override - protected double calculateNumericalVariance() { - return 12.5 - 3.5 * 3.5; // E(X^2) - E(X)^2 + public final boolean isSupportConnected() { + return true; } } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -28,7 +28,7 @@ import org.junit.Test; * * @version $Id$ */ -public class CauchyDistributionTest extends ContinuousDistributionAbstractTest { +public class CauchyDistributionTest extends RealDistributionAbstractTest { // --------------------- Override tolerance -------------- protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ChiSquaredDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ChiSquaredDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ChiSquaredDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ChiSquaredDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -23,10 +23,10 @@ import org.junit.Test; /** * Test cases for {@link ChiSquaredDistribution}. * - * @see ContinuousDistributionAbstractTest + * @see RealDistributionAbstractTest * @version $Id$ */ -public class ChiSquaredDistributionTest extends ContinuousDistributionAbstractTest { +public class ChiSquaredDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- @@ -134,5 +134,4 @@ public class ChiSquaredDistributionTest Assert.assertEquals(dist.getNumericalMean(), 1.12, tol); Assert.assertEquals(dist.getNumericalVariance(), 2.24, tol); } - } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -29,7 +29,7 @@ import org.junit.Test; * * @version $Id$ */ -public class ExponentialDistributionTest extends ContinuousDistributionAbstractTest { +public class ExponentialDistributionTest extends RealDistributionAbstractTest { // --------------------- Override tolerance -------------- @Override Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -27,7 +27,7 @@ import org.junit.Test; * * @version $Id$ */ -public class FDistributionTest extends ContinuousDistributionAbstractTest { +public class FDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/GammaDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/GammaDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/GammaDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/GammaDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -28,7 +28,7 @@ import org.junit.Test; * * @version $Id$ */ -public class GammaDistributionTest extends ContinuousDistributionAbstractTest { +public class GammaDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java Sat Dec 3 06:17:19 2011 @@ -17,14 +17,12 @@ package org.apache.commons.math.distribution; import org.apache.commons.math.TestUtils; -import org.apache.commons.math.util.FastMath; import org.apache.commons.math.exception.MathIllegalArgumentException; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; - /** * Abstract base class for {@link IntegerDistribution} tests. *

    @@ -187,56 +185,6 @@ public abstract class IntegerDistributio } /** - * Verifies that floating point arguments are correctly handled by - * cumulativeProbablility(-,-) - * JIRA: MATH-184 - */ - @Test - public void testFloatingPointArguments() throws Exception { - for (int i = 0; i < cumulativeTestPoints.length; i++) { - double arg = cumulativeTestPoints[i]; - Assert.assertEquals( - "Incorrect cumulative probability value returned for " + - cumulativeTestPoints[i], - cumulativeTestValues[i], - distribution.cumulativeProbability(arg), tolerance); - if (i < cumulativeTestPoints.length - 1) { - double arg2 = cumulativeTestPoints[i + 1]; - Assert.assertEquals("Inconsistent probability for discrete range " + - "[ " + arg + "," + arg2 + " ]", - distribution.cumulativeProbability( - cumulativeTestPoints[i], - cumulativeTestPoints[i + 1]), - distribution.cumulativeProbability(arg, arg2), tolerance); - arg = arg - FastMath.random(); - arg2 = arg2 + FastMath.random(); - Assert.assertEquals("Inconsistent probability for discrete range " + - "[ " + arg + "," + arg2 + " ]", - distribution.cumulativeProbability( - cumulativeTestPoints[i], - cumulativeTestPoints[i + 1]), - distribution.cumulativeProbability(arg, arg2), tolerance); - } - } - int one = 1; - int ten = 10; - int two = 2; - double oned = one; - double twod = two; - double tend = ten; - Assert.assertEquals(distribution.cumulativeProbability(one, two), - distribution.cumulativeProbability(oned, twod), tolerance); - Assert.assertEquals(distribution.cumulativeProbability(one, two), - distribution.cumulativeProbability(oned - tolerance, - twod + 0.9), tolerance); - Assert.assertEquals(distribution.cumulativeProbability(two, ten), - distribution.cumulativeProbability(twod, tend), tolerance); - Assert.assertEquals(distribution.cumulativeProbability(two, ten), - distribution.cumulativeProbability(twod - tolerance, - tend + 0.9), tolerance); - } - - /** * Verifies that inverse cumulative probability density calculations match expected values * using default test instance data */ Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -28,7 +28,7 @@ import org.junit.Test; * * @version $Id$ */ -public class NormalDistributionTest extends ContinuousDistributionAbstractTest { +public class NormalDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -158,7 +158,7 @@ public class PoissonDistributionTest ext double sigma = FastMath.sqrt(mean); while (x >= 0) { try { - p = dist.cumulativeProbability(x); + p = dist.cumulativeProbability((int) x); Assert.assertFalse("NaN cumulative probability returned for mean = " + mean + " x = " + x,Double.isNaN(p)); if (x > mean - 2 * sigma) { @@ -190,7 +190,7 @@ public class PoissonDistributionTest ext checkProbability(dist, 6950); } - private void checkProbability(PoissonDistribution dist, double x) throws Exception { + private void checkProbability(PoissonDistribution dist, int x) throws Exception { double p = dist.cumulativeProbability(x); Assert.assertFalse("NaN cumulative probability returned for mean = " + dist.getMean() + " x = " + x, Double.isNaN(p)); @@ -206,9 +206,8 @@ public class PoissonDistributionTest ext double p = 0.1; double dp = p; while (p < .99) { - double ret = Double.NaN; try { - ret = dist.inverseCumulativeProbability(p); + int ret = dist.inverseCumulativeProbability(p); // Verify that returned value satisties definition Assert.assertTrue(p >= dist.cumulativeProbability(ret)); Assert.assertTrue(p < dist.cumulativeProbability(ret + 1)); Copied: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/RealDistributionAbstractTest.java (from r1209828, commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ContinuousDistributionAbstractTest.java) URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/RealDistributionAbstractTest.java?p2=commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/RealDistributionAbstractTest.java&p1=commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ContinuousDistributionAbstractTest.java&r1=1209828&r2=1209836&rev=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ContinuousDistributionAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/RealDistributionAbstractTest.java Sat Dec 3 06:17:19 2011 @@ -27,7 +27,7 @@ import org.junit.Before; import org.junit.Test; /** - * Abstract base class for {@link ContinuousDistribution} tests. + * Abstract base class for {@link RealDistribution} tests. *

    * To create a concrete test class for a continuous distribution * implementation, first implement makeDistribution() to return a distribution @@ -60,11 +60,11 @@ import org.junit.Test; * * @version $Id$ */ -public abstract class ContinuousDistributionAbstractTest { +public abstract class RealDistributionAbstractTest { //-------------------- Private test instance data ------------------------- /** Distribution instance used to perform tests */ - private ContinuousDistribution distribution; + private RealDistribution distribution; /** Tolerance used in comparing expected and returned values */ private double tolerance = 1E-4; @@ -87,7 +87,7 @@ public abstract class ContinuousDistribu //-------------------- Abstract methods ----------------------------------- /** Creates the default continuous distribution instance to use in tests. */ - public abstract ContinuousDistribution makeDistribution(); + public abstract RealDistribution makeDistribution(); /** Creates the default cumulative probability test input values */ public abstract double[] makeCumulativeTestPoints(); @@ -191,8 +191,7 @@ public abstract class ContinuousDistribu for (int i = 0; i < cumulativeTestPoints.length; i++) { TestUtils.assertEquals("Incorrect probability density value returned for " + cumulativeTestPoints[i], densityTestValues[i], - //TODO: remove cast when density(double) is added to ContinuousDistribution - ((AbstractContinuousDistribution) distribution).density(cumulativeTestPoints[i]), + distribution.density(cumulativeTestPoints[i]), getTolerance()); } } @@ -279,11 +278,10 @@ public abstract class ContinuousDistribu */ @Test public void testSampling() throws Exception { - AbstractContinuousDistribution dist = (AbstractContinuousDistribution) makeDistribution(); final int sampleSize = 1000; - dist.reseedRandomGenerator(1000); // Use fixed seed - double[] sample = dist.sample(sampleSize); - double[] quartiles = TestUtils.getDistributionQuartiles(dist); + distribution.reseedRandomGenerator(1000); // Use fixed seed + double[] sample = distribution.sample(sampleSize); + double[] quartiles = TestUtils.getDistributionQuartiles(distribution); double[] expected = {250, 250, 250, 250}; long[] counts = new long[4]; for (int i = 0; i < sampleSize; i++) { @@ -332,14 +330,14 @@ public abstract class ContinuousDistribu /** * @return Returns the distribution. */ - protected ContinuousDistribution getDistribution() { + protected RealDistribution getDistribution() { return distribution; } /** * @param distribution The distribution to set. */ - protected void setDistribution(AbstractContinuousDistribution distribution) { + protected void setDistribution(RealDistribution distribution) { this.distribution = distribution; } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -27,7 +27,7 @@ import org.apache.commons.math.TestUtils * * @version $Id$ */ -public class TDistributionTest extends ContinuousDistributionAbstractTest { +public class TDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java?rev=1209836&r1=1209835&r2=1209836&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java Sat Dec 3 06:17:19 2011 @@ -30,7 +30,7 @@ import org.junit.Test; * * @version $Id$ */ -public class WeibullDistributionTest extends ContinuousDistributionAbstractTest { +public class WeibullDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods -----------------------