commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1209836 [1/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 GMT
Author: celestin
Date: Sat Dec  3 06:17:19 2011
New Revision: 1209836

URL: http://svn.apache.org/viewvc?rev=1209836&view=rev
Log:
Patch contributed by Christian Winter (MATH-703).

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/RealDistribution.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/RealDistributionAbstractTest.java
      - copied, changed from r1209828, commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ContinuousDistributionAbstractTest.java
Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ContinuousDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/DiscreteDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/Distribution.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ContinuousDistributionAbstractTest.java
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BinomialDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/FDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/IntegerDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/SaddlePointExpansion.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/TDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/AbtractIntegerDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ChiSquaredDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/GammaDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/IntegerDistributionAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java Sat Dec  3 06:17:19 2011
@@ -24,8 +24,6 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.OutOfRangeException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.random.RandomDataImpl;
-import org.apache.commons.math.util.FastMath;
-
 
 /**
  * Base class for integer-valued discrete distributions.  Default
@@ -34,120 +32,35 @@ import org.apache.commons.math.util.Fast
  *
  * @version $Id$
  */
-public abstract class AbstractIntegerDistribution extends AbstractDistribution
-    implements IntegerDistribution, Serializable {
-   /** Serializable version identifier */
+public abstract class AbstractIntegerDistribution
+implements IntegerDistribution, Serializable {
+    /** Serializable version identifier */
     private static final long serialVersionUID = -1146319659338487221L;
+
     /**
      * RandomData instance used to generate samples from the distribution.
-     * @since 2.2
      */
     protected final RandomDataImpl randomData = new RandomDataImpl();
 
-    /**
-     * Default constructor.
-     */
+    /** Default constructor. */
     protected AbstractIntegerDistribution() {}
 
     /**
-     * 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,
-     * or CDF, for this distribution.
-     * If {@code x} does not represent an integer value, the CDF is
-     * evaluated at the greatest integer less than {@code x}.
-     *
-     * @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}.
-     */
-    public double cumulativeProbability(double x) {
-        return cumulativeProbability((int) FastMath.floor(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 Inclusive lower bound.
-     * @param x1 Inclusive upper bound.
-     * @return the probability that a random variable with this distribution
-     * will take a value between {@code x0} and {@code x1},
-     * including the endpoints.
-     * @throws NumberIsTooSmallException if {@code x1 > x0}.
-     */
-    @Override
-    public double cumulativeProbability(double x0, double x1) {
-        if (x1 < x0) {
-            throw new NumberIsTooSmallException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
-                                                x1, x0, true);
-        }
-        if (FastMath.floor(x0) < x0) {
-            return cumulativeProbability(((int) FastMath.floor(x0)) + 1,
-               (int) FastMath.floor(x1)); // don't want to count mass below x0
-        } else { // x0 is mathematical integer, so use as is
-            return cumulativeProbability((int) FastMath.floor(x0),
-                (int) FastMath.floor(x1));
-        }
-    }
-
-    /**
-     * 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 distribution function,
-     * or PDF, for this distribution.
-     *
-     * @param x Value at which the PDF is evaluated.
-     * @return PDF for this distribution.
-     */
-    public abstract double cumulativeProbability(int x);
-
-    /**
-     * For a random variable {@code X} whose values are distributed according
-     * to this distribution, this method returns {@code P(X = x)}. In other
-     * words, this method represents the probability mass function, or PMF,
-     * for the distribution.
-     * If {@code x} does not represent an integer value, 0 is returned.
-     *
-     * @param x Value at which the probability density function is evaluated.
-     * @return the value of the probability density function at {@code x}.
-     */
-    public double probability(double x) {
-        double fl = FastMath.floor(x);
-        if (fl == x) {
-            return this.probability((int) x);
-        } else {
-            return 0;
-        }
-    }
-
-    /**
-     * For a random variable {@code X} whose values are distributed according
-     * to this distribution, this method returns {@code P(x0 < X < x1)}.
-     *
-     * @param x0 Inclusive lower bound.
-     * @param x1 Inclusive upper bound.
-     * @return the cumulative probability.
-     * @throws NumberIsTooSmallException {@code if x0 > x1}.
+     * {@inheritDoc}
+     * 
+     * The default implementation uses the identity
+     * <p>{@code P(x0 <= X <= x1) = P(X <= x1) - P(X <= x0 - 1)}</p>
      */
     public double cumulativeProbability(int x0, int x1) {
         if (x1 < x0) {
-            throw new NumberIsTooSmallException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
-                                                x1, x0, true);
+            throw new NumberIsTooSmallException(
+                    LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
+                    x1, x0, true);
         }
         return cumulativeProbability(x1) - cumulativeProbability(x0 - 1);
     }
 
-    /**
-     * For a random variable {@code X} whose values are distributed according
-     * to this distribution, this method returns the largest {@code x}, such
-     * that {@code P(X <= x) <= p}.
-     *
-     * @param p Desired probability.
-     * @return the largest {@code x} such that {@code P(X < x) <= p}.
-     * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
-     */
+    /** {@inheritDoc} */
     public int inverseCumulativeProbability(final double p) {
         if (p < 0 || p > 1) {
             throw new OutOfRangeException(p, 0, 1);
@@ -195,40 +108,53 @@ public abstract class AbstractIntegerDis
     }
 
     /**
-     * {@inheritDoc}
+     * Access the domain value lower bound, based on {@code p}, used to
+     * bracket a CDF root. This method is used by
+     * {@link #inverseCumulativeProbability(double)} to find critical values.
+     *
+     * @param p the desired probability for the critical value ({@code 0 < p < 1})
+     * @return a domain value lower bound, i.e. a value {@code x} such that
+     * {@code P(X <= x) < p}
+     */
+    protected abstract int getDomainLowerBound(double p);
+
+    /**
+     * Access the domain value upper bound, based on {@code p}, used to
+     * bracket a CDF root. This method is used by
+     * {@link #inverseCumulativeProbability(double)} to find critical values.
+     *
+     * @param p the desired probability for the critical value ({@code 0 < p < 1})
+     * @return a domain value upper bound, i.e. a value {@code x} such that
+     * {@code P(X <= x) >= p}
      */
+    protected abstract int getDomainUpperBound(double p);
+
+    /** {@inheritDoc} */
     public void reseedRandomGenerator(long seed) {
         randomData.reSeed(seed);
     }
 
     /**
-     * Generates a random value sampled from this distribution. The default
-     * implementation uses the
+     * {@inheritDoc}
+     * 
+     * The default implementation uses the
      * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">
-     *  inversion method.
-     * </a>
-     *
-     * @return a random value.
-     * @since 2.2
+     * inversion method</a>.
      */
     public int sample() {
         return randomData.nextInversionDeviate(this);
     }
 
     /**
-     * Generates a random sample from the distribution.  The default
-     * implementation generates the sample by calling {@link #sample()}
-     * in a loop.
-     *
-     * @param sampleSize number of random values to generate.
-     * @since 2.2
-     * @return an array representing the random sample.
-     * @throws NotStrictlyPositiveException if {@code sampleSize <= 0}.
+     * {@inheritDoc}
+     * 
+     * The default implementation generates the sample by calling
+     * {@link #sample()} in a loop.
      */
     public int[] sample(int sampleSize) {
         if (sampleSize <= 0) {
-            throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
-                                                   sampleSize);
+            throw new NotStrictlyPositiveException(
+                    LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize);
         }
         int[] out = new int[sampleSize];
         for (int i = 0; i < sampleSize; i++) {
@@ -238,82 +164,24 @@ public abstract class AbstractIntegerDis
     }
 
     /**
-     * Computes the cumulative probability function and checks for NaN
-     * values returned.
-     * Throws MathInternalError if the value is NaN. Rethrows any Exception encountered
-     * evaluating the cumulative probability function. Throws
-     * MathInternalError if the cumulative probability function returns NaN.
-     *
-     * @param argument Input value.
-     * @return the cumulative probability.
-     * @throws MathInternalError if the cumulative probability is NaN
+     * Computes the cumulative probability function and checks for {@code NaN}
+     * values returned. Throws {@code MathInternalError} if the value is
+     * {@code NaN}. Rethrows any exception encountered evaluating the cumulative
+     * probability function. Throws {@code MathInternalError} if the cumulative
+     * probability function returns {@code NaN}.
+     *
+     * @param argument input value
+     * @return the cumulative probability
+     * @throws MathInternalError if the cumulative probability is {@code NaN}
      */
     private double checkedCumulativeProbability(int argument)
         throws MathInternalError {
         double result = Double.NaN;
-            result = cumulativeProbability(argument);
+        result = cumulativeProbability(argument);
         if (Double.isNaN(result)) {
-            throw new MathInternalError(
-                    LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
+            throw new MathInternalError(LocalizedFormats
+                    .DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument);
         }
         return result;
     }
-
-    /**
-     * Access the domain value lower bound, based on {@code p}, used to
-     * bracket a PDF root.  This method is used by
-     * {@link #inverseCumulativeProbability(double)} to find critical values.
-     *
-     * @param p Desired probability for the critical value
-     * @return the domain value lower bound, i.e. {@code P(X < 'lower bound') < p}.
-     */
-    protected abstract int getDomainLowerBound(double p);
-
-    /**
-     * Access the domain value upper bound, based on {@code p}, used to
-     * bracket a PDF root.  This method is used by
-     * {@link #inverseCumulativeProbability(double)} to find critical values.
-     *
-     * @param p Desired probability for the critical value.
-     * @return the domain value upper bound, i.e. {@code P(X < 'upper bound') > p}.
-     */
-    protected abstract int getDomainUpperBound(double p);
-
-    /**
-     * Access the lower bound of the support.
-     *
-     * @return lower bound of the support (Integer.MIN_VALUE for negative infinity)
-     */
-    public abstract int getSupportLowerBound();
-
-    /**
-     * Access the upper bound of the support.
-     *
-     * @return upper bound of the support (Integer.MAX_VALUE for positive infinity)
-     */
-    public abstract int getSupportUpperBound();
-
-    /**
-     * Use this method to get information about whether the lower bound
-     * of the support is inclusive or not. For discrete support,
-     * only true here is meaningful.
-     *
-     * @return true (always but at Integer.MIN_VALUE because of the nature of discrete support)
-     */
-    @Override
-    public boolean isSupportLowerBoundInclusive() {
-        return true;
-    }
-
-    /**
-     * Use this method to get information about whether the upper bound
-     * of the support is inclusive or not. For discrete support,
-     * only true here is meaningful.
-     *
-     * @return true (always but at Integer.MAX_VALUE because of the nature of discrete support)
-     */
-    @Override
-    public boolean isSupportUpperBoundInclusive() {
-        return true;
-    }
 }

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java?rev=1209836&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java Sat Dec  3 06:17:19 2011
@@ -0,0 +1,196 @@
+/*
+ * 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 java.io.Serializable;
+
+import org.apache.commons.math.analysis.UnivariateFunction;
+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.NumberIsTooLargeException;
+import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.random.RandomDataImpl;
+import org.apache.commons.math.util.FastMath;
+
+/**
+ * Base class for probability distributions on the reals.
+ * Default implementations are provided for some of the methods
+ * that do not vary from distribution to distribution.
+ *
+ * @version $Id$
+ * @since 3.0
+ */
+public abstract class AbstractRealDistribution
+implements RealDistribution, Serializable {
+    /** Serializable version identifier */
+    private static final long serialVersionUID = -38038050983108802L;
+
+    /** Default accuracy. */
+    public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
+
+    /** Solver absolute accuracy for inverse cumulative computation */
+    private double solverAbsoluteAccuracy = SOLVER_DEFAULT_ABSOLUTE_ACCURACY;
+
+    /** RandomData instance used to generate samples from the distribution. */
+    protected final RandomDataImpl randomData = new RandomDataImpl();
+
+    /** Default constructor. */
+    protected AbstractRealDistribution() {}
+
+    /**
+     * {@inheritDoc}
+     *
+     * The default implementation uses the identity
+     * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p>
+     */
+    public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException {
+        if (x0 > x1) {
+            throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
+                    x0, x1, true);
+        }
+        return cumulativeProbability(x1) - cumulativeProbability(x0);
+    }
+
+    /** {@inheritDoc} */
+    public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
+
+        if (p < 0.0 || p > 1.0) {
+            throw new OutOfRangeException(p, 0, 1);
+        }
+
+        // by default, do simple root finding using bracketing and default solver.
+        // subclasses can override if there is a better method.
+        UnivariateFunction rootFindingFunction =
+            new UnivariateFunction() {
+            public double value(double x) {
+                return cumulativeProbability(x) - p;
+            }
+        };
+
+        // Try to bracket root, test domain endpoints if this fails
+        double lowerBound = getDomainLowerBound(p);
+        double upperBound = getDomainUpperBound(p);
+        double[] bracket = null;
+        try {
+            bracket = UnivariateRealSolverUtils.bracket(
+                    rootFindingFunction, getInitialDomain(p),
+                    lowerBound, upperBound);
+        } catch (NumberIsTooLargeException ex) {
+            /*
+             * Check domain endpoints to see if one gives value that is within
+             * the default solver's defaultAbsoluteAccuracy of 0 (will be the
+             * case if density has bounded support and p is 0 or 1).
+             */
+            if (FastMath.abs(rootFindingFunction.value(lowerBound)) < getSolverAbsoluteAccuracy()) {
+                return lowerBound;
+            }
+            if (FastMath.abs(rootFindingFunction.value(upperBound)) < getSolverAbsoluteAccuracy()) {
+                return upperBound;
+            }
+            // Failed bracket convergence was not because of corner solution
+            throw new MathInternalError(ex);
+        }
+
+        // find root
+        double root = UnivariateRealSolverUtils.solve(rootFindingFunction,
+                // override getSolverAbsoluteAccuracy() to use a Brent solver with
+                // absolute accuracy different from the default.
+                bracket[0],bracket[1], getSolverAbsoluteAccuracy());
+        return root;
+    }
+
+    /**
+     * Access the initial domain value, based on {@code p}, used to
+     * bracket a CDF root.  This method is used by
+     * {@link #inverseCumulativeProbability(double)} to find critical values.
+     *
+     * @param p Desired probability for the critical value.
+     * @return the initial domain value.
+     * TODO to be deleted when applying MATH-699
+     */
+    protected abstract double getInitialDomain(double p);
+
+    /**
+     * Access the domain value lower bound, based on {@code p}, used to
+     * bracket a CDF root.  This method is used by
+     * {@link #inverseCumulativeProbability(double)} to find critical values.
+     *
+     * @param p Desired probability for the critical value.
+     * @return the domain value lower bound, i.e. {@code P(X < 'lower bound') < p}.
+     * TODO to be deleted when applying MATH-699
+     */
+    protected abstract double getDomainLowerBound(double p);
+
+    /**
+     * Access the domain value upper bound, based on {@code p}, used to
+     * bracket a CDF root.  This method is used by
+     * {@link #inverseCumulativeProbability(double)} to find critical values.
+     *
+     * @param p Desired probability for the critical value.
+     * @return the domain value upper bound, i.e. {@code P(X < 'upper bound') > p}.
+     * TODO to be deleted when applying MATH-699
+     */
+    protected abstract double getDomainUpperBound(double p);
+
+    /**
+     * Returns the solver absolute accuracy for inverse cumulative computation.
+     * You can override this method in order to use a Brent solver with an
+     * absolute accuracy different from the default.
+     *
+     * @return the maximum absolute error in inverse cumulative probability estimates
+     */
+    protected double getSolverAbsoluteAccuracy() {
+        return solverAbsoluteAccuracy;
+    }
+
+    /** {@inheritDoc} */
+    public void reseedRandomGenerator(long seed) {
+        randomData.reSeed(seed);
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * The default implementation uses the
+     * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">
+     * inversion method.
+     * </a>
+     */
+    public double sample() {
+        return randomData.nextInversionDeviate(this);
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * The default implementation generates the sample by calling
+     * {@link #sample()} in a loop.
+     */
+    public double[] sample(int sampleSize) {
+        if (sampleSize <= 0) {
+            throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
+                    sampleSize);
+        }
+        double[] out = new double[sampleSize];
+        for (int i = 0; i < sampleSize; i++) {
+            out[i] = sample();
+        }
+        return out;
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/AbstractRealDistribution.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java Sat Dec  3 06:17:19 2011
@@ -29,7 +29,7 @@ import org.apache.commons.math.util.Fast
  * @version $Id$
  * @since 2.0 (changed to concrete class in 3.0)
  */
-public class BetaDistribution extends AbstractContinuousDistribution {
+public class BetaDistribution extends AbstractRealDistribution {
     /**
      * Default inverse cumulative probability accuracy.
      * @since 2.1
@@ -100,6 +100,17 @@ public class BetaDistribution extends Ab
         }
     }
 
+    /**
+     * {@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) {
         recomputeZ();
@@ -178,63 +189,68 @@ public class BetaDistribution extends Ab
     /**
      * {@inheritDoc}
      *
-     * The lower bound of the support is always 0 no matter the parameters.
-     *
-     * @return lower bound of the support (always 0)
+     * For first shape parameter {@code alpha} and second shape parameter
+     * {@code beta}, the mean is {@code alpha / (alpha + beta)}.
      */
-    @Override
-    public double getSupportLowerBound() {
-        return 0;
+    public double getNumericalMean() {
+        final double a = getAlpha();
+        return a / (a + getBeta());
     }
 
     /**
      * {@inheritDoc}
      *
-     * The upper bound of the support is always 1 no matter the parameters.
-     *
-     * @return upper bound of the support (always 1)
+     * For first shape parameter {@code alpha} and second shape parameter
+     * {@code beta}, the variance is
+     * {@code (alpha * beta) / [(alpha + beta)^2 * (alpha + beta + 1)]}.
      */
-    @Override
-    public double getSupportUpperBound() {
-        return 1;
+    public double getNumericalVariance() {
+        final double a = getAlpha();
+        final double b = getBeta();
+        final double alphabetasum = a + b;
+        return (a * b) / ((alphabetasum * alphabetasum) * (alphabetasum + 1));
     }
 
     /**
      * {@inheritDoc}
      *
-     * For first shape parameter {@code alpha} and second shape parameter {@code beta}, the
-     * mean is {@code alpha / (alpha + beta)}.
+     * The lower bound of the support is always 0 no matter the parameters.
+     *
+     * @return lower bound of the support (always 0)
      */
-    @Override
-    protected double calculateNumericalMean() {
-        final double a = getAlpha();
-        return a / (a + getBeta());
+    public double getSupportLowerBound() {
+        return 0;
     }
 
     /**
      * {@inheritDoc}
      *
-     * For first shape parameter {@code alpha} and second shape parameter
-     * {@code beta}, the variance is
-     * {@code (alpha * beta) / [(alpha + beta)^2 * (alpha + beta + 1)]}.
+     * The upper bound of the support is always 1 no matter the parameters.
+     *
+     * @return upper bound of the support (always 1)
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double a = getAlpha();
-        final double b = getBeta();
-        final double alphabetasum = a + b;
-        return (a * b) / ((alphabetasum * alphabetasum) * (alphabetasum + 1));
+    public double getSupportUpperBound() {
+        return 1;
     }
 
     /** {@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/BinomialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BinomialDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BinomialDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/BinomialDistribution.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;
@@ -31,8 +29,7 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/BinomialDistribution.html">Binomial Distribution (MathWorld)</a>
  * @version $Id$
  */
-public class BinomialDistribution extends AbstractIntegerDistribution
-        implements Serializable {
+public class BinomialDistribution extends AbstractIntegerDistribution {
     /** Serializable version identifier. */
     private static final long serialVersionUID = 6751309484392813623L;
     /** The number of trials. */
@@ -81,19 +78,19 @@ public class BinomialDistribution extend
     }
 
     /** {@inheritDoc} */
-    @Override
-    protected int getDomainLowerBound(double p) {
-        return -1;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    protected int getDomainUpperBound(double p) {
-        return numberOfTrials;
+    public double probability(int x) {
+        double ret;
+        if (x < 0 || x > numberOfTrials) {
+            ret = 0.0;
+        } else {
+            ret = FastMath.exp(SaddlePointExpansion.logBinomialProbability(x,
+                    numberOfTrials, probabilityOfSuccess,
+                    1.0 - probabilityOfSuccess));
+        }
+        return ret;
     }
 
     /** {@inheritDoc} */
-    @Override
     public double cumulativeProbability(int x) {
         double ret;
         if (x < 0) {
@@ -107,19 +104,6 @@ public class BinomialDistribution extend
         return ret;
     }
 
-    /** {@inheritDoc} */
-    public double probability(int x) {
-        double ret;
-        if (x < 0 || x > numberOfTrials) {
-            ret = 0.0;
-        } else {
-            ret = FastMath.exp(SaddlePointExpansion.logBinomialProbability(x,
-                    numberOfTrials, probabilityOfSuccess,
-                    1.0 - probabilityOfSuccess));
-        }
-        return ret;
-    }
-
     /**
      * {@inheritDoc}
      *
@@ -140,6 +124,39 @@ public class BinomialDistribution extend
         return super.inverseCumulativeProbability(p);
     }
 
+    /** {@inheritDoc} */
+    @Override
+    protected int getDomainLowerBound(double p) {
+        return -1;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected int getDomainUpperBound(double p) {
+        return numberOfTrials;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For {@code n} trials and probability parameter {@code p}, the mean is
+     * {@code n * p}.
+     */
+    public double getNumericalMean() {
+        return getNumberOfTrials() * getProbabilityOfSuccess();
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For {@code n} trials and probability parameter {@code p}, the variance is
+     * {@code n * p * (1 - p)}.
+     */
+    public double getNumericalVariance() {
+        final double p = getProbabilityOfSuccess();
+        return getNumberOfTrials() * p * (1 - p);
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -148,7 +165,6 @@ public class BinomialDistribution extend
      *
      * @return lower bound of the support (always 0)
      */
-    @Override
     public int getSupportLowerBound() {
         return 0;
     }
@@ -160,31 +176,18 @@ public class BinomialDistribution extend
      *
      * @return upper bound of the support (equal to number of trials)
      */
-    @Override
     public int getSupportUpperBound() {
         return getNumberOfTrials();
     }
 
     /**
      * {@inheritDoc}
-     *
-     * For {@code n} trials and probability parameter {@code p}, the mean is
-     * {@code n * p}.
-     */
-    @Override
-    protected double calculateNumericalMean() {
-        return getNumberOfTrials() * getProbabilityOfSuccess();
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * For {@code n} trials and probability parameter {@code p}, the variance is
-     * {@code n * p * (1 - p)}.
+     * 
+     * The support of this distribution is connected.
+     * 
+     * @return {@code true}
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double p = getProbabilityOfSuccess();
-        return getNumberOfTrials() * p * (1 - p);
+    public boolean isSupportConnected() {
+        return true;
     }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java Sat Dec  3 06:17:19 2011
@@ -14,11 +14,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.commons.math.distribution;
 
-import java.io.Serializable;
-
 import org.apache.commons.math.exception.NotStrictlyPositiveException;
 import org.apache.commons.math.exception.OutOfRangeException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -32,8 +29,7 @@ import org.apache.commons.math.util.Fast
  * @since 1.1 (changed to concrete class in 3.0)
  * @version $Id$
  */
-public class CauchyDistribution extends AbstractContinuousDistribution
-    implements Serializable {
+public class CauchyDistribution extends AbstractRealDistribution {
     /**
      * Default inverse cumulative probability accuracy.
      * @since 2.1
@@ -110,6 +106,17 @@ public class CauchyDistribution 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) {
         final double dev = x - median;
@@ -190,62 +197,67 @@ public class CauchyDistribution extends 
     /**
      * {@inheritDoc}
      *
-     * The lower bound of the support is always negative infinity no matter
-     * the parameters.
+     * The mean is always undefined no matter the parameters.
      *
-     * @return lower bound of the support (always Double.NEGATIVE_INFINITY)
+     * @return mean (always Double.NaN)
      */
-    @Override
-    public double getSupportLowerBound() {
-        return Double.NEGATIVE_INFINITY;
+    public double getNumericalMean() {
+        return Double.NaN;
     }
 
     /**
      * {@inheritDoc}
      *
-     * The upper bound of the support is always positive infinity no matter
-     * the parameters.
+     * The variance is always undefined no matter the parameters.
      *
-     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
+     * @return variance (always Double.NaN)
      */
-    @Override
-    public double getSupportUpperBound() {
-        return Double.POSITIVE_INFINITY;
+    public double getNumericalVariance() {
+        return Double.NaN;
     }
 
     /**
      * {@inheritDoc}
      *
-     * The mean is always undefined no matter the parameters.
+     * The lower bound of the support is always negative infinity no matter
+     * the parameters.
      *
-     * @return mean (always Double.NaN)
+     * @return lower bound of the support (always Double.NEGATIVE_INFINITY)
      */
-    @Override
-    protected double calculateNumericalMean() {
-        return Double.NaN;
+    public double getSupportLowerBound() {
+        return Double.NEGATIVE_INFINITY;
     }
 
     /**
      * {@inheritDoc}
      *
-     * The variance is always undefined no matter the parameters.
+     * The upper bound of the support is always positive infinity no matter
+     * the parameters.
      *
-     * @return variance (always Double.NaN)
+     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        return Double.NaN;
+    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/ChiSquaredDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java Sat Dec  3 06:17:19 2011
@@ -1,214 +1,225 @@
-/*
- * 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 java.io.Serializable;
-
-
-/**
- * Implementation of the chi-squared distribution.
- *
- * @see <a href="http://en.wikipedia.org/wiki/Chi-squared_distribution">Chi-squared distribution (Wikipedia)</a>
- * @see <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">Chi-squared Distribution (MathWorld)</a>
- * @version $Id: ChiSquaredDistribution.java 1206060 2011-11-25 05:16:56Z celestin $
- */
-public class ChiSquaredDistribution
-    extends AbstractContinuousDistribution
-    implements Serializable {
-    /**
-     * 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 = -8352658048349159782L;
-    /** Internal Gamma distribution. */
-    private final GammaDistribution gamma;
-    /** Inverse cumulative probability accuracy */
-    private final double solverAbsoluteAccuracy;
-
-    /**
-     * Create a Chi-Squared distribution with the given degrees of freedom.
-     *
-     * @param degreesOfFreedom Degrees of freedom.
-     */
-    public ChiSquaredDistribution(double degreesOfFreedom) {
-        this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
-    }
-
-    /**
-     * Create a Chi-Squared distribution with the given degrees of freedom and
-     * inverse cumulative probability accuracy.
-     *
-     * @param degreesOfFreedom Degrees of freedom.
-     * @param inverseCumAccuracy the maximum absolute error in inverse
-     * cumulative probability estimates (defaults to
-     * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
-     * @since 2.1
-     */
-    public ChiSquaredDistribution(double degreesOfFreedom,
-                                      double inverseCumAccuracy) {
-        gamma = new GammaDistribution(degreesOfFreedom / 2, 2);
-        solverAbsoluteAccuracy = inverseCumAccuracy;
-    }
-
-    /**
-     * Access the number of degrees of freedom.
-     *
-     * @return the degrees of freedom.
-     */
-    public double getDegreesOfFreedom() {
-        return gamma.getAlpha() * 2.0;
-    }
-
-    /** {@inheritDoc} */
-    public double density(double x) {
-        return gamma.density(x);
-    }
-
-    /** {@inheritDoc} */
-    public double cumulativeProbability(double x)  {
-        return gamma.cumulativeProbability(x);
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * Returns {@code 0} when {@code p == 0} and
-     * {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
-     */
-    @Override
-    public double inverseCumulativeProbability(final double p) {
-        if (p == 0) {
-            return 0d;
-        }
-        if (p == 1) {
-            return Double.POSITIVE_INFINITY;
-        }
-        return super.inverseCumulativeProbability(p);
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    protected double getDomainLowerBound(double p) {
-        return Double.MIN_VALUE * gamma.getBeta();
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    protected double getDomainUpperBound(double p) {
-        // NOTE: chi squared is skewed to the left
-        // NOTE: therefore, P(X < &mu;) > .5
-
-        double ret;
-
-        if (p < .5) {
-            // use mean
-            ret = getDegreesOfFreedom();
-        } else {
-            // use max
-            ret = Double.MAX_VALUE;
-        }
-
-        return ret;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    protected double getInitialDomain(double p) {
-        // NOTE: chi squared is skewed to the left
-        // NOTE: therefore, P(X < &mu;) > 0.5
-
-        double ret;
-
-        if (p < 0.5) {
-            // use 1/2 mean
-            ret = getDegreesOfFreedom() * 0.5;
-        } else {
-            // use mean
-            ret = getDegreesOfFreedom();
-        }
-
-        return ret;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    protected double getSolverAbsoluteAccuracy() {
-        return solverAbsoluteAccuracy;
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * The lower bound of the support is always 0 no matter the
-     * degrees of freedom.
-     *
-     * @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
-     * degrees of freedom.
-     *
-     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
-     */
-    @Override
-    public double getSupportUpperBound() {
-        return Double.POSITIVE_INFINITY;
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * For {@code k} degrees of freedom, the mean is {@code k}.
-     */
-    @Override
-    protected double calculateNumericalMean() {
-        return getDegreesOfFreedom();
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * For {@code k} degrees of freedom, the variance is {@code 2 * k}.
-     *
-     * @return {@inheritDoc}
-     */
-    @Override
-    protected double calculateNumericalVariance() {
-        return 2*getDegreesOfFreedom();
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public boolean isSupportLowerBoundInclusive() {
-        return true;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public boolean isSupportUpperBoundInclusive() {
-        return false;
-    }
-}
+/*
+ * 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;
+
+/**
+ * Implementation of the chi-squared distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Chi-squared_distribution">Chi-squared distribution (Wikipedia)</a>
+ * @see <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">Chi-squared Distribution (MathWorld)</a>
+ * @version $Id$
+ */
+public class ChiSquaredDistribution extends AbstractRealDistribution {
+    /**
+     * 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 = -8352658048349159782L;
+    /** Internal Gamma distribution. */
+    private final GammaDistribution gamma;
+    /** Inverse cumulative probability accuracy */
+    private final double solverAbsoluteAccuracy;
+
+    /**
+     * Create a Chi-Squared distribution with the given degrees of freedom.
+     *
+     * @param degreesOfFreedom Degrees of freedom.
+     */
+    public ChiSquaredDistribution(double degreesOfFreedom) {
+        this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+    }
+
+    /**
+     * Create a Chi-Squared distribution with the given degrees of freedom and
+     * inverse cumulative probability accuracy.
+     *
+     * @param degreesOfFreedom Degrees of freedom.
+     * @param inverseCumAccuracy the maximum absolute error in inverse
+     * cumulative probability estimates (defaults to
+     * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
+     * @since 2.1
+     */
+    public ChiSquaredDistribution(double degreesOfFreedom,
+                                      double inverseCumAccuracy) {
+        gamma = new GammaDistribution(degreesOfFreedom / 2, 2);
+        solverAbsoluteAccuracy = inverseCumAccuracy;
+    }
+
+    /**
+     * Access the number of degrees of freedom.
+     *
+     * @return the degrees of freedom.
+     */
+    public double getDegreesOfFreedom() {
+        return gamma.getAlpha() * 2.0;
+    }
+
+    /**
+     * {@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) {
+        return gamma.density(x);
+    }
+
+    /** {@inheritDoc} */
+    public double cumulativeProbability(double x)  {
+        return gamma.cumulativeProbability(x);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * Returns {@code 0} when {@code p == 0} and
+     * {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
+     */
+    @Override
+    public double inverseCumulativeProbability(final double p) {
+        if (p == 0) {
+            return 0d;
+        }
+        if (p == 1) {
+            return Double.POSITIVE_INFINITY;
+        }
+        return super.inverseCumulativeProbability(p);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected double getDomainLowerBound(double p) {
+        return Double.MIN_VALUE * gamma.getBeta();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected double getDomainUpperBound(double p) {
+        // NOTE: chi squared is skewed to the left
+        // NOTE: therefore, P(X < &mu;) > .5
+
+        double ret;
+
+        if (p < .5) {
+            // use mean
+            ret = getDegreesOfFreedom();
+        } else {
+            // use max
+            ret = Double.MAX_VALUE;
+        }
+
+        return ret;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected double getInitialDomain(double p) {
+        // NOTE: chi squared is skewed to the left
+        // NOTE: therefore, P(X < &mu;) > 0.5
+
+        double ret;
+
+        if (p < 0.5) {
+            // use 1/2 mean
+            ret = getDegreesOfFreedom() * 0.5;
+        } else {
+            // use mean
+            ret = getDegreesOfFreedom();
+        }
+
+        return ret;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected double getSolverAbsoluteAccuracy() {
+        return solverAbsoluteAccuracy;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For {@code k} degrees of freedom, the mean is {@code k}.
+     */
+    public double getNumericalMean() {
+        return getDegreesOfFreedom();
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For {@code k} degrees of freedom, the variance is {@code 2 * k}.
+     *
+     * @return {@inheritDoc}
+     */
+    public double getNumericalVariance() {
+        return 2*getDegreesOfFreedom();
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * The lower bound of the support is always 0 no matter the
+     * degrees of freedom.
+     *
+     * @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
+     * degrees of freedom.
+     *
+     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
+     */
+    public double getSupportUpperBound() {
+        return Double.POSITIVE_INFINITY;
+    }
+
+    /** {@inheritDoc} */
+    public boolean isSupportLowerBoundInclusive() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    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/ExponentialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.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.OutOfRangeException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -30,8 +28,7 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">Exponential distribution (MathWorld)</a>
  * @version $Id$
  */
-public class ExponentialDistribution extends AbstractContinuousDistribution
-    implements Serializable {
+public class ExponentialDistribution extends AbstractRealDistribution {
     /**
      * Default inverse cumulative probability accuracy.
      * @since 2.1
@@ -80,6 +77,17 @@ public class ExponentialDistribution ext
         return mean;
     }
 
+    /**
+     * {@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) {
@@ -192,58 +200,63 @@ public class ExponentialDistribution ext
     /**
      * {@inheritDoc}
      *
-     * The lower bound of the support is always 0 no matter the mean parameter.
-     *
-     * @return lower bound of the support (always 0)
+     * For mean parameter {@code k}, the mean is {@code k}.
      */
-    @Override
-    public double getSupportLowerBound() {
-        return 0;
+    public double getNumericalMean() {
+        return getMean();
     }
 
     /**
      * {@inheritDoc}
      *
-     * The upper bound of the support is always positive infinity
-     * no matter the mean parameter.
-     *
-     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
+     * For mean parameter {@code k}, the variance is {@code k^2}.
      */
-    @Override
-    public double getSupportUpperBound() {
-        return Double.POSITIVE_INFINITY;
+    public double getNumericalVariance() {
+        final double m = getMean();
+        return m * m;
     }
 
     /**
      * {@inheritDoc}
      *
-     * For mean parameter {@code k}, the mean is {@code k}.
+     * The lower bound of the support is always 0 no matter the mean parameter.
+     *
+     * @return lower bound of the support (always 0)
      */
-    @Override
-    protected double calculateNumericalMean() {
-        return getMean();
+    public double getSupportLowerBound() {
+        return 0;
     }
 
     /**
      * {@inheritDoc}
      *
-     * For mean parameter {@code k}, the variance is {@code k^2}.
+     * The upper bound of the support is always positive infinity
+     * no matter the mean parameter.
+     *
+     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double m = getMean();
-        return m * m;
+    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/FDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/FDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/FDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/FDistribution.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.OutOfRangeException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -32,23 +30,31 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/F-Distribution.html">F-distribution (MathWorld)</a>
  * @version $Id$
  */
-public class FDistribution
-    extends AbstractContinuousDistribution
-    implements Serializable  {
+public class FDistribution extends AbstractRealDistribution {
+    /** Serializable version identifier. */
+    private static final long serialVersionUID = -8516354193418641566L;
+
     /**
      * 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 = -8516354193418641566L;
+
     /** The numerator degrees of freedom. */
     private final double numeratorDegreesOfFreedom;
+
     /** The numerator degrees of freedom. */
     private final double denominatorDegreesOfFreedom;
+
     /** Inverse cumulative probability accuracy. */
     private final double solverAbsoluteAccuracy;
 
+    /** Cached numerical variance */
+    private double numericalVariance = Double.NaN;
+
+    /** Whether or not the numerical variance has been calculated */
+    private boolean numericalVarianceIsCalculated = false;
+
     /**
      * Create a F distribution using the given degrees of freedom.
      * @param numeratorDegreesOfFreedom Numerator degrees of freedom.
@@ -97,6 +103,17 @@ public class FDistribution
     /**
      * {@inheritDoc}
      *
+     * For this distribution {@code P(X = x)} always evaluates to 0.
+     *
+     * @return 0
+     */
+    public double probability(double x) {
+        return 0.0;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
      * @since 2.1
      */
     public double density(double x) {
@@ -206,39 +223,13 @@ public class FDistribution
     /**
      * {@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 Double.POSITIVE_INFINITY)
-     */
-    @Override
-    public double getSupportUpperBound() {
-        return Double.POSITIVE_INFINITY;
-    }
-
-    /**
-     * {@inheritDoc}
-     *
      * For denominator degrees of freedom parameter {@code b}, the mean is
      * <ul>
      *  <li>if {@code b > 2} then {@code b / (b - 2)},</li>
      *  <li>else undefined ({@code Double.NaN}).
      * </ul>
      */
-    @Override
-    protected double calculateNumericalMean() {
+    public double getNumericalMean() {
         final double denominatorDF = getDenominatorDegreesOfFreedom();
 
         if (denominatorDF > 2) {
@@ -261,7 +252,19 @@ public class FDistribution
      *  <li>else undefined ({@code Double.NaN}).
      * </ul>
      */
-    @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 denominatorDF = getDenominatorDegreesOfFreedom();
 
@@ -276,15 +279,47 @@ public class FDistribution
         return Double.NaN;
     }
 
+    /**
+     * {@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 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/GammaDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/GammaDistribution.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;
@@ -30,8 +28,7 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/GammaDistribution.html">Gamma distribution (MathWorld)</a>
  * @version $Id$
  */
-public class GammaDistribution extends AbstractContinuousDistribution
-    implements Serializable  {
+public class GammaDistribution extends AbstractRealDistribution {
     /**
      * Default inverse cumulative probability accuracy.
      * @since 2.1
@@ -84,6 +81,44 @@ public class GammaDistribution extends A
     }
 
     /**
+     * Access the {@code alpha} shape parameter.
+     *
+     * @return {@code alpha}.
+     */
+    public double getAlpha() {
+        return alpha;
+    }
+
+    /**
+     * Access the {@code beta} scale parameter.
+     *
+     * @return {@code beta}.
+     */
+    public double getBeta() {
+        return beta;
+    }
+
+    /**
+     * {@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) {
+            return 0;
+        }
+        return FastMath.pow(x / beta, alpha - 1) / beta *
+               FastMath.exp(-x / beta) / FastMath.exp(Gamma.logGamma(alpha));
+    }
+
+    /**
      * {@inheritDoc}
      *
      * The implementation of this method is based on:
@@ -126,33 +161,6 @@ public class GammaDistribution extends A
         return super.inverseCumulativeProbability(p);
     }
 
-    /**
-     * Access the {@code alpha} shape parameter.
-     *
-     * @return {@code alpha}.
-     */
-    public double getAlpha() {
-        return alpha;
-    }
-
-    /**
-     * Access the {@code beta} scale parameter.
-     *
-     * @return {@code beta}.
-     */
-    public double getBeta() {
-        return beta;
-    }
-
-    /** {@inheritDoc} */
-    public double density(double x) {
-        if (x < 0) {
-            return 0;
-        }
-        return FastMath.pow(x / beta, alpha - 1) / beta *
-               FastMath.exp(-x / beta) / FastMath.exp(Gamma.logGamma(alpha));
-    }
-
     /** {@inheritDoc} */
     @Override
     protected double getDomainLowerBound(double p) {
@@ -208,62 +216,67 @@ public class GammaDistribution extends A
     /**
      * {@inheritDoc}
      *
-     * The lower bound of the support is always 0 no matter the parameters.
-     *
-     * @return lower bound of the support (always 0)
+     * For shape parameter {@code alpha} and scale parameter {@code beta}, the
+     * mean is {@code alpha * beta}.
      */
-    @Override
-    public double getSupportLowerBound() {
-        return 0;
+    public double getNumericalMean() {
+        return getAlpha() * getBeta();
     }
 
     /**
      * {@inheritDoc}
      *
-     * The upper bound of the support is always positive infinity
-     * no matter the parameters.
+     * For shape parameter {@code alpha} and scale parameter {@code beta}, the
+     * variance is {@code alpha * beta^2}.
      *
-     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
+     * @return {@inheritDoc}
      */
-    @Override
-    public double getSupportUpperBound() {
-        return Double.POSITIVE_INFINITY;
+    public double getNumericalVariance() {
+        final double b = getBeta();
+        return getAlpha() * b * b;
     }
 
     /**
      * {@inheritDoc}
      *
-     * For shape parameter {@code alpha} and scale parameter {@code beta}, the
-     * mean is {@code alpha * beta}.
+     * The lower bound of the support is always 0 no matter the parameters.
+     *
+     * @return lower bound of the support (always 0)
      */
-    @Override
-    protected double calculateNumericalMean() {
-        return getAlpha() * getBeta();
+    public double getSupportLowerBound() {
+        return 0;
     }
 
     /**
      * {@inheritDoc}
      *
-     * For shape parameter {@code alpha} and scale parameter {@code beta}, the
-     * variance is {@code alpha * beta^2}.
+     * The upper bound of the support is always positive infinity
+     * no matter the parameters.
      *
-     * @return {@inheritDoc}
+     * @return upper bound of the support (always Double.POSITIVE_INFINITY)
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double b = getBeta();
-        return getAlpha() * b * b;
+    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/HypergeometricDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.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.NotPositiveException;
 import org.apache.commons.math.exception.NotStrictlyPositiveException;
 import org.apache.commons.math.exception.NumberIsTooLargeException;
@@ -33,17 +31,25 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/HypergeometricDistribution.html">Hypergeometric distribution (MathWorld)</a>
  * @version $Id$
  */
-public class HypergeometricDistribution extends AbstractIntegerDistribution
-    implements Serializable {
+public class HypergeometricDistribution extends AbstractIntegerDistribution {
     /** Serializable version identifier. */
     private static final long serialVersionUID = -436928820673516179L;
+
     /** The number of successes in the population. */
     private final int numberOfSuccesses;
+
     /** The population size. */
     private final int populationSize;
+
     /** The sample size. */
     private final int sampleSize;
 
+    /** Cached numerical variance */
+    private double numericalVariance = Double.NaN;
+
+    /** Whether or not the numerical variance has been calculated */
+    private boolean numericalVarianceIsCalculated = false;
+
     /**
      * Construct a new hypergeometric distribution with the specified population
      * size, number of successes in the population, and sample size.
@@ -51,18 +57,13 @@ public class HypergeometricDistribution 
      * @param populationSize Population size.
      * @param numberOfSuccesses Number of successes in the population.
      * @param sampleSize Sample size.
-     * @throws NotPositiveException if {@code numberOfSuccesses < 0},
-     * or {@code populationSize < 0}.
+     * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
      * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
      * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize},
      * or {@code sampleSize > populationSize}.
      */
-    public HypergeometricDistribution(int populationSize,
-                                          int numberOfSuccesses,
-                                          int sampleSize)
-        throws NotPositiveException,
-        NotStrictlyPositiveException,
-        NumberIsTooLargeException {
+    public HypergeometricDistribution(int populationSize, int numberOfSuccesses, int sampleSize)
+    throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
         if (populationSize <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
                                                    populationSize);
@@ -91,7 +92,6 @@ public class HypergeometricDistribution 
     }
 
     /** {@inheritDoc} */
-    @Override
     public double cumulativeProbability(int x) {
         double ret;
 
@@ -277,12 +277,48 @@ public class HypergeometricDistribution 
      * {@inheritDoc}
      *
      * For population size {@code N}, number of successes {@code m}, and sample
+     * size {@code n}, the mean is {@code n * m / N}.
+     */
+    public double getNumericalMean() {
+        return (double)(getSampleSize() * getNumberOfSuccesses()) / (double)getPopulationSize();
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For population size {@code N}, number of successes {@code m}, and sample
+     * size {@code n}, the variance is
+     * {@code [n * m * (N - n) * (N - m)] / [N^2 * (N - 1)]}.
+     */
+    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 N = getPopulationSize();
+        final double m = getNumberOfSuccesses();
+        final double n = getSampleSize();
+        return ( n * m * (N - n) * (N - m) ) / ( (N*N * (N - 1)) );
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For population size {@code N}, number of successes {@code m}, and sample
      * size {@code n}, the lower bound of the support is
      * {@code max(0, n + m - N)}.
      *
      * @return lower bound of the support
      */
-    @Override
     public int getSupportLowerBound() {
         return FastMath.max(0,
                 getSampleSize() + getNumberOfSuccesses() - getPopulationSize());
@@ -296,34 +332,18 @@ public class HypergeometricDistribution 
      *
      * @return upper bound of the support
      */
-    @Override
     public int getSupportUpperBound() {
         return FastMath.min(getNumberOfSuccesses(), getSampleSize());
     }
 
     /**
      * {@inheritDoc}
-     *
-     * For population size {@code N}, number of successes {@code m}, and sample
-     * size {@code n}, the mean is {@code n * m / N}.
-     */
-    @Override
-    protected double calculateNumericalMean() {
-        return (double)(getSampleSize() * getNumberOfSuccesses()) / (double)getPopulationSize();
-    }
-
-    /**
-     * {@inheritDoc}
-     *
-     * For population size {@code N}, number of successes {@code m}, and sample
-     * size {@code n}, the variance is
-     * {@code [n * m * (N - n) * (N - m)] / [N^2 * (N - 1)]}.
+     * 
+     * The support of this distribution is connected.
+     * 
+     * @return {@code true}
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double N = getPopulationSize();
-        final double m = getNumberOfSuccesses();
-        final double n = getSampleSize();
-        return ( n * m * (N - n) * (N - m) ) / ( (N*N * (N - 1)) );
+    public boolean isSupportConnected() {
+        return true;
     }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/IntegerDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/IntegerDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/IntegerDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/IntegerDistribution.java Sat Dec  3 06:17:19 2011
@@ -17,19 +17,19 @@
 package org.apache.commons.math.distribution;
 
 /**
- * Interface for discrete distributions of integer-valued random variables.
+ * Interface for distributions on the integers.
  *
  * @version $Id$
  */
-public interface IntegerDistribution extends DiscreteDistribution {
+public interface IntegerDistribution {
     /**
      * 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 for the
      * distribution.
      *
-     * @param x Value at which the probability density function is evaluated.
-     * @return the value of the probability density function at {@code x}.
+     * @param x the point at which the probability density function is evaluated.
+     * @return the value of the probability density function at {@code x}
      */
     double probability(int x);
 
@@ -39,19 +39,19 @@ public interface IntegerDistribution ext
      * words, this method represents the probability distribution function, or
      * PDF for the distribution.
      *
-     * @param x Value at which the PDF is evaluated.
+     * @param x the point at which the PDF is evaluated
      * @return PDF for this distribution.
      */
     double cumulativeProbability(int x);
 
     /**
-     * For this distribution, {@code X}, this method returns
-     * {@code P(x0 <= X <= x1)}.
+     * For a random variable {@code X} whose values are distributed according
+     * to this distribution, this method returns {@code P(x0 <= X <= x1)}.
      *
-     * @param x0 the inclusive, lower bound
-     * @param x1 the inclusive, upper bound
-     * @return the cumulative probability.
-     * @throws IllegalArgumentException if {@code x0 > x1}.
+     * @param x0 the inclusive lower bound
+     * @param x1 the inclusive upper bound
+     * @return the cumulative probability
+     * @throws IllegalArgumentException if {@code x0 > x1}
      */
     double cumulativeProbability(int x0, int x1);
 
@@ -74,17 +74,60 @@ public interface IntegerDistribution ext
      *  </li>
      * </ul>
      *
-     * @param p Cumulative probability.
-     * @return the largest {@code x} such that {@code P(X < x) <= p}.
+     * @param p the cumulative probability
+     * @return the largest {@code x} such that {@code P(X < x) <= p}
      * @throws IllegalArgumentException if {@code p} is not between 0 and 1
-     * (inclusive).
+     * (inclusive)
      */
     int inverseCumulativeProbability(double p);
 
     /**
+     * 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 ({@code Integer.MIN_VALUE}
+     * for negative infinity)
+     */
+    int getSupportLowerBound();
+
+    /**
+     * Access the upper bound of the support.
+     *
+     * @return upper bound of the support ({@code Integer.MAX_VALUE}
+     * for positive infinity)
+     */
+    int getSupportUpperBound();
+
+    /**
+     * Use this method to get information about whether the support is
+     * connected, i.e. whether all integers between the lower and upper bound of
+     * the support are included in the support.
+     *
+     * @return whether the support is connected or not
+     */
+    boolean isSupportConnected();
+
+    /**
      * Reseed the random generator used to generate samples.
      *
-     * @param seed New seed.
+     * @param seed the new seed
      * @since 3.0
      */
     void reseedRandomGenerator(long seed);
@@ -92,7 +135,7 @@ public interface IntegerDistribution ext
     /**
      * Generate a random value sampled from this distribution.
      *
-     * @return a random value.
+     * @return a random value
      * @since 3.0
      */
     int sample();
@@ -100,10 +143,10 @@ public interface IntegerDistribution ext
     /**
      * Generate a random sample from the distribution.
      *
-     * @param sampleSize number of random values to generate.
-     * @return an array representing the random sample.
+     * @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.
+     * if {@code sampleSize} is not positive
      * @since 3.0
      */
     int[] sample(int sampleSize);

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java?rev=1209836&r1=1209835&r2=1209836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/NormalDistribution.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.NumberIsTooLargeException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -32,8 +30,7 @@ import org.apache.commons.math.util.Fast
  * @see <a href="http://mathworld.wolfram.com/NormalDistribution.html">Normal distribution (MathWorld)</a>
  * @version $Id$
  */
-public class NormalDistribution extends AbstractContinuousDistribution
-        implements Serializable {
+public class NormalDistribution extends AbstractRealDistribution {
     /**
      * Default inverse cumulative probability accuracy.
      * @since 2.1
@@ -74,7 +71,8 @@ public class NormalDistribution extends 
      * @throws NotStrictlyPositiveException if {@code sd <= 0}.
      * @since 2.1
      */
-    public NormalDistribution(double mean, double sd, double inverseCumAccuracy) {
+    public NormalDistribution(double mean, double sd, double inverseCumAccuracy)
+        throws NotStrictlyPositiveException {
         if (sd <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
         }
@@ -88,7 +86,7 @@ public class NormalDistribution extends 
      * Create a normal distribution with mean equal to zero and standard
      * deviation equal to one.
      */
-    public NormalDistribution(){
+    public NormalDistribution() {
         this(0, 1);
     }
 
@@ -110,6 +108,17 @@ public class NormalDistribution extends 
         return standardDeviation;
     }
 
+    /**
+     * {@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 x0 = x - mean;
@@ -146,12 +155,6 @@ public class NormalDistribution extends 
         return 0.5 * Erf.erf(v0, v1);
     }
 
-    /** {@inheritDoc} */
-    @Override
-    protected double getSolverAbsoluteAccuracy() {
-        return solverAbsoluteAccuracy;
-    }
-
     /**
      * {@inheritDoc}
      *
@@ -171,12 +174,6 @@ public class NormalDistribution extends 
 
     /** {@inheritDoc} */
     @Override
-    public double sample()  {
-        return randomData.nextGaussian(mean, standardDeviation);
-    }
-
-    /** {@inheritDoc} */
-    @Override
     protected double getDomainLowerBound(double p) {
         double ret;
 
@@ -219,6 +216,31 @@ public class NormalDistribution extends 
         return ret;
     }
 
+    /** {@inheritDoc} */
+    @Override
+    protected double getSolverAbsoluteAccuracy() {
+        return solverAbsoluteAccuracy;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For mean parameter {@code mu}, the mean is {@code mu}.
+     */
+    public double getNumericalMean() {
+        return getMean();
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * For standard deviation parameter {@code s}, the variance is {@code s^2}.
+     */
+    public double getNumericalVariance() {
+        final double s = getStandardDeviation();
+        return s * s;
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -228,7 +250,6 @@ public class NormalDistribution extends 
      * @return lower bound of the support (always
      * {@code Double.NEGATIVE_INFINITY})
      */
-    @Override
     public double getSupportLowerBound() {
         return Double.NEGATIVE_INFINITY;
     }
@@ -242,41 +263,34 @@ public class NormalDistribution extends 
      * @return upper bound of the support (always
      * {@code Double.POSITIVE_INFINITY})
      */
-    @Override
     public double getSupportUpperBound() {
         return Double.POSITIVE_INFINITY;
     }
 
-    /**
-     * {@inheritDoc}
-     *
-     * For mean parameter {@code mu}, the mean is {@code mu}.
-     */
-    @Override
-    protected double calculateNumericalMean() {
-        return getMean();
+    /** {@inheritDoc} */
+    public boolean isSupportLowerBoundInclusive() {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    public boolean isSupportUpperBoundInclusive() {
+        return false;
     }
 
     /**
      * {@inheritDoc}
-     *
-     * For standard deviation parameter {@code s}, the variance is {@code s^2}.
+     * 
+     * The support of this distribution is connected.
+     * 
+     * @return {@code true}
      */
-    @Override
-    protected double calculateNumericalVariance() {
-        final double s = getStandardDeviation();
-        return s * s;
+    public boolean isSupportConnected() {
+        return true;
     }
 
     /** {@inheritDoc} */
     @Override
-    public boolean isSupportLowerBoundInclusive() {
-        return false;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public boolean isSupportUpperBoundInclusive() {
-        return false;
+    public double sample()  {
+        return randomData.nextGaussian(mean, standardDeviation);
     }
 }



Mime
View raw message