Author: tn
Date: Wed Jan 22 22:00:37 2014
New Revision: 1560531
URL: http://svn.apache.org/r1560531
Log:
[MATH-1038] Refactor code as agreed on the mailinglist.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java
- copied, changed from r1559500,
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/BinomialConfidenceInterval.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java
- copied, changed from r1559500,
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ConfidenceInterval.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
(with props)
Removed:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/BinomialConfidenceInterval.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ConfidenceInterval.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/BinomialConfidenceIntervalTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/package-info.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/package-info.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/package-info.java?rev=1560531&r1=1560530&r2=1560531&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/package-info.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/package-info.java
Wed Jan 22 22:00:37 2014
@@ -16,8 +16,7 @@
*/
/**
*
- * Classes providing hypothesis testing and confidence interval
- * construction.
+ * Classes providing hypothesis testing.
*
*/
package org.apache.commons.math3.stat.inference;
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,34 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the Agresti-Coull method for creating a binomial proportion
confidence interval.
+ *
+ * @see <a
+ *
href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Agresti-Coull_Interval">
+ * Agresti-Coull interval (Wikipedia)</a>
+ * @version $Id$
+ * @since 3.3
+ */
+public class AgrestiCoullInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ @Override
+ public ConfidenceInterval createInterval(int numberOfTrials, int
numberOfSuccesses, double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ final double alpha = (1.0 - confidenceLevel) / 2;
+ final NormalDistribution normalDistribution = new NormalDistribution();
+ final double z = normalDistribution.inverseCumulativeProbability(1 -
alpha);
+ final double zSquared = FastMath.pow(z, 2);
+ final double modifiedNumberOfTrials = numberOfTrials + zSquared;
+ final double modifiedSuccessesRatio = (1.0 / modifiedNumberOfTrials) *
(numberOfSuccesses + 0.5 * zSquared);
+ final double difference = z *
+ FastMath.sqrt(1.0 / modifiedNumberOfTrials *
modifiedSuccessesRatio *
+ (1 - modifiedSuccessesRatio));
+ return new ConfidenceInterval(modifiedSuccessesRatio - difference,
modifiedSuccessesRatio + difference,
+ confidenceLevel);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java
(from r1559500,
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/BinomialConfidenceInterval.java)
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/BinomialConfidenceInterval.java&r1=1559500&r2=1560531&rev=1560531&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/BinomialConfidenceInterval.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java
Wed Jan 22 22:00:37 2014
@@ -14,26 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.math3.stat.inference;
+package org.apache.commons.math3.stat.interval;
-import org.apache.commons.math3.distribution.FDistribution;
-import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
-import org.apache.commons.math3.exception.util.LocalizedFormats;
-import org.apache.commons.math3.util.FastMath;
/**
- * Factory methods to generate confidence intervals for a binomial proportion.
- * The supported methods are:
- * <ul>
- * <li>Clopper-Pearson method (exact method)</li>
- * <li>Normal approximation (based on central limit theorem)</li>
- * <li>Agresti-Coull interval</li>
- * <li>Wilson score interval</li>
- * </ul>
+ * Abstract base class to generate confidence intervals for a binomial
proportion.
*
* @see <a
*
href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval">Binomial
@@ -41,12 +30,12 @@ import org.apache.commons.math3.util.Fas
* @version $Id$
* @since 3.3
*/
-public class BinomialConfidenceInterval {
+public interface BinomialConfidenceInterval {
/**
- * Create a Clopper-Pearson binomial confidence interval for the true
- * probability of success of an unknown binomial distribution with the
given
- * observed number of trials, successes and confidence level.
+ * Create a confidence interval for the true probability of success
+ * of an unknown binomial distribution with the given observed number
+ * of trials, successes and confidence level.
* <p>
* Preconditions:
* <ul>
@@ -67,147 +56,8 @@ public class BinomialConfidenceInterval
* @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
* @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
*/
- public ConfidenceInterval getClopperPearsonInterval(int numberOfTrials,
int numberOfSuccesses,
- double
confidenceLevel) {
- checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
- double lowerBound = 0;
- double upperBound = 0;
- final double alpha = (1.0 - confidenceLevel) / 2.0;
+ ConfidenceInterval createInterval(int numberOfTrials, int
numberOfSuccesses, double confidenceLevel)
+ throws NotStrictlyPositiveException, NotPositiveException,
+ NumberIsTooLargeException, OutOfRangeException;
- final FDistribution distributionLowerBound = new FDistribution(2 *
(numberOfTrials - numberOfSuccesses + 1),
- 2 *
numberOfSuccesses);
- final double fValueLowerBound =
distributionLowerBound.inverseCumulativeProbability(1 - alpha);
- if (numberOfSuccesses > 0) {
- lowerBound = numberOfSuccesses /
- (numberOfSuccesses + (numberOfTrials -
numberOfSuccesses + 1) * fValueLowerBound);
- }
-
- final FDistribution distributionUpperBound = new FDistribution(2 *
(numberOfSuccesses + 1),
- 2 *
(numberOfTrials - numberOfSuccesses));
- final double fValueUpperBound =
distributionUpperBound.inverseCumulativeProbability(1 - alpha);
- if (numberOfSuccesses > 0) {
- upperBound = (numberOfSuccesses + 1) * fValueUpperBound /
- (numberOfTrials - numberOfSuccesses +
(numberOfSuccesses + 1) * fValueUpperBound);
- }
-
- return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
- }
-
- /**
- * Create a binomial confidence interval for the true probability of
success
- * of an unknown binomial distribution with the given observed number of
- * trials, successes and confidence level using the Normal approximation to
- * the binomial distribution.
- *
- * @param numberOfTrials number of trials
- * @param numberOfSuccesses number of successes
- * @param confidenceLevel desired probability that the true probability of
- * success falls within the interval
- * @return Confidence interval containing the probability of success with
- * probability {@code confidenceLevel}
- */
- public ConfidenceInterval getNormalApproximationInterval(int
numberOfTrials, int numberOfSuccesses,
- double
confidenceLevel) {
- checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
- final double mean = (double) numberOfSuccesses / (double)
numberOfTrials;
- final double alpha = (1.0 - confidenceLevel) / 2;
- final NormalDistribution normalDistribution = new NormalDistribution();
- final double difference =
normalDistribution.inverseCumulativeProbability(1 - alpha) *
- FastMath.sqrt(1.0 / numberOfTrials * mean *
(1 - mean));
- return new ConfidenceInterval(mean - difference, mean + difference,
confidenceLevel);
- }
-
- /**
- * Create an Agresti-Coull binomial confidence interval for the true
- * probability of success of an unknown binomial distribution with the
given
- * observed number of trials, successes and confidence level.
- *
- * @param numberOfTrials number of trials
- * @param numberOfSuccesses number of successes
- * @param confidenceLevel desired probability that the true probability of
- * success falls within the returned interval
- * @return Confidence interval containing the probability of success with
- * probability {@code confidenceLevel}
- * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
- * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
- * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
- * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
- */
- public ConfidenceInterval getAgrestiCoullInterval(int numberOfTrials, int
numberOfSuccesses, double confidenceLevel) {
- checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
- final double alpha = (1.0 - confidenceLevel) / 2;
- final NormalDistribution normalDistribution = new NormalDistribution();
- final double z = normalDistribution.inverseCumulativeProbability(1 -
alpha);
- final double zSquared = FastMath.pow(z, 2);
- final double modifiedNumberOfTrials = numberOfTrials + zSquared;
- final double modifiedSuccessesRatio = (1.0 / modifiedNumberOfTrials) *
(numberOfSuccesses + 0.5 * zSquared);
- final double difference = z *
- FastMath.sqrt(1.0 / modifiedNumberOfTrials *
modifiedSuccessesRatio *
- (1 - modifiedSuccessesRatio));
- return new ConfidenceInterval(modifiedSuccessesRatio - difference,
modifiedSuccessesRatio + difference,
- confidenceLevel);
- }
-
- /**
- * Create a Wilson score binomial confidence interval for the true
- * probability of success of an unknown binomial distribution with the
given
- * observed number of trials, successes and confidence level.
- *
- * @param numberOfTrials number of trials
- * @param numberOfSuccesses number of successes
- * @param confidenceLevel desired probability that the true probability of
- * success falls within the returned interval
- * @return Confidence interval containing the probability of success with
- * probability {@code confidenceLevel}
- * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
- * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
- * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
- * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
- */
- public ConfidenceInterval getWilsonScoreInterval(int numberOfTrials, int
numberOfSuccesses, double confidenceLevel) {
- checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
- final double alpha = (1.0 - confidenceLevel) / 2;
- final NormalDistribution normalDistribution = new NormalDistribution();
- final double z = normalDistribution.inverseCumulativeProbability(1 -
alpha);
- final double zSquared = FastMath.pow(z, 2);
- final double mean = (double) numberOfSuccesses / (double)
numberOfTrials;
-
- final double factor = 1.0 / (1 + (1.0 / numberOfTrials) * zSquared);
- final double modifiedSuccessRatio = mean + (1.0 / (2 *
numberOfTrials)) * zSquared;
- final double difference = z *
- FastMath.sqrt(1.0 / numberOfTrials * mean *
(1 - mean) +
- (1.0 / (4 *
FastMath.pow(numberOfTrials, 2)) * zSquared));
-
- final double lowerBound = factor * (modifiedSuccessRatio - difference);
- final double upperBound = factor * (modifiedSuccessRatio + difference);
- return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
- }
-
- /**
- * Verifies that parameters satisfy preconditions.
- *
- * @param numberOfTrials number of trials (must be positive)
- * @param numberOfSuccesses number of successes (must not exceed
numberOfTrials)
- * @param confidenceLevel confidence level (must be strictly between 0 and
1)
- * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
- * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
- * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
- * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
- */
- private void checkParameters(int numberOfTrials, int numberOfSuccesses,
double confidenceLevel) {
- if (numberOfTrials <= 0) {
- throw new
NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, numberOfTrials);
- }
- if (numberOfSuccesses < 0) {
- throw new
NotPositiveException(LocalizedFormats.NEGATIVE_NUMBER_OF_SUCCESSES,
numberOfSuccesses);
- }
- if (numberOfSuccesses > numberOfTrials) {
- throw new
NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
- numberOfSuccesses,
numberOfTrials, true);
- }
- if (confidenceLevel <= 0 || confidenceLevel >= 1) {
- throw new
OutOfRangeException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL,
- confidenceLevel, 0, 1);
- }
- }
}
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,44 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.FDistribution;
+
+/**
+ * Implements the Clopper-Pearson method for creating a binomial proportion
confidence interval.
+ *
+ * @see <a
+ *
href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Clopper-Pearson_interval">
+ * Clopper-Pearson interval (Wikipedia)</a>
+ * @version $Id$
+ * @since 3.3
+ */
+public class ClopperPearsonInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ @Override
+ public ConfidenceInterval createInterval(int numberOfTrials, int
numberOfSuccesses,
+ double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ double lowerBound = 0;
+ double upperBound = 0;
+ final double alpha = (1.0 - confidenceLevel) / 2.0;
+
+ final FDistribution distributionLowerBound = new FDistribution(2 *
(numberOfTrials - numberOfSuccesses + 1),
+ 2 *
numberOfSuccesses);
+ final double fValueLowerBound =
distributionLowerBound.inverseCumulativeProbability(1 - alpha);
+ if (numberOfSuccesses > 0) {
+ lowerBound = numberOfSuccesses /
+ (numberOfSuccesses + (numberOfTrials -
numberOfSuccesses + 1) * fValueLowerBound);
+ }
+
+ final FDistribution distributionUpperBound = new FDistribution(2 *
(numberOfSuccesses + 1),
+ 2 *
(numberOfTrials - numberOfSuccesses));
+ final double fValueUpperBound =
distributionUpperBound.inverseCumulativeProbability(1 - alpha);
+ if (numberOfSuccesses > 0) {
+ upperBound = (numberOfSuccesses + 1) * fValueUpperBound /
+ (numberOfTrials - numberOfSuccesses +
(numberOfSuccesses + 1) * fValueUpperBound);
+ }
+
+ return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java
(from r1559500,
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ConfidenceInterval.java)
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ConfidenceInterval.java&r1=1559500&r2=1560531&rev=1560531&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/ConfidenceInterval.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java
Wed Jan 22 22:00:37 2014
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.math3.stat.inference;
+package org.apache.commons.math3.stat.interval;
import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,159 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
+
+/**
+ * Factory methods to generate confidence intervals for a binomial proportion.
+ * The supported methods are:
+ * <ul>
+ * <li>Agresti-Coull interval</li>
+ * <li>Clopper-Pearson method (exact method)</li>
+ * <li>Normal approximation (based on central limit theorem)</li>
+ * <li>Wilson score interval</li>
+ * </ul>
+ *
+ * @version $Id$
+ * @since 3.3
+ */
+public final class IntervalUtils {
+
+ /** Singleton Agresti-Coull instance. */
+ private static final BinomialConfidenceInterval AGRESTI_COULL = new
AgrestiCoullInterval();
+
+ /** Singleton Clopper-Pearson instance. */
+ private static final BinomialConfidenceInterval CLOPPER_PEARSON = new
ClopperPearsonInterval();
+
+ /** Singleton NormalApproximation instance. */
+ private static final BinomialConfidenceInterval NORMAL_APPROXIMATION = new
NormalApproximationInterval();
+
+ /** Singleton Wilson score instance. */
+ private static final BinomialConfidenceInterval WILSON_SCORE = new
WilsonScoreInterval();
+
+ /**
+ * Prevent instantiation.
+ */
+ private IntervalUtils() {
+ }
+
+ /**
+ * Create an Agresti-Coull binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the
given
+ * observed number of trials, successes and confidence level.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getAgrestiCoullInterval(int
numberOfTrials, int numberOfSuccesses,
+ double
confidenceLevel) {
+ return AGRESTI_COULL.createInterval(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ }
+
+ /**
+ * Create a Clopper-Pearson binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the
given
+ * observed number of trials, successes and confidence level.
+ * <p>
+ * Preconditions:
+ * <ul>
+ * <li>{@code numberOfTrials} must be positive</li>
+ * <li>{@code numberOfSuccesses} may not exceed {@code numberOfTrials}</li>
+ * <li>{@code confidenceLevel} must be strictly between 0 and 1
(exclusive)</li>
+ * </ul>
+ * </p>
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getClopperPearsonInterval(int
numberOfTrials, int numberOfSuccesses,
+ double
confidenceLevel) {
+ return CLOPPER_PEARSON.createInterval(numberOfTrials,
numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Create a binomial confidence interval for the true probability of
success
+ * of an unknown binomial distribution with the given observed number of
+ * trials, successes and confidence level using the Normal approximation to
+ * the binomial distribution.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ */
+ public static ConfidenceInterval getNormalApproximationInterval(int
numberOfTrials, int numberOfSuccesses,
+ double
confidenceLevel) {
+ return NORMAL_APPROXIMATION.createInterval(numberOfTrials,
numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Create a Wilson score binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the
given
+ * observed number of trials, successes and confidence level.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getWilsonScoreInterval(int
numberOfTrials, int numberOfSuccesses,
+ double
confidenceLevel) {
+ return WILSON_SCORE.createInterval(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ }
+
+ /**
+ * Verifies that parameters satisfy preconditions.
+ *
+ * @param numberOfTrials number of trials (must be positive)
+ * @param numberOfSuccesses number of successes (must not exceed
numberOfTrials)
+ * @param confidenceLevel confidence level (must be strictly between 0 and
1)
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses >
numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the
interval {@code (0, 1)}.
+ */
+ static void checkParameters(int numberOfTrials, int numberOfSuccesses,
double confidenceLevel) {
+ if (numberOfTrials <= 0) {
+ throw new
NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, numberOfTrials);
+ }
+ if (numberOfSuccesses < 0) {
+ throw new
NotPositiveException(LocalizedFormats.NEGATIVE_NUMBER_OF_SUCCESSES,
numberOfSuccesses);
+ }
+ if (numberOfSuccesses > numberOfTrials) {
+ throw new
NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
+ numberOfSuccesses,
numberOfTrials, true);
+ }
+ if (confidenceLevel <= 0 || confidenceLevel >= 1) {
+ throw new
OutOfRangeException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL,
+ confidenceLevel, 0, 1);
+ }
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,30 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the normal approximation method for creating a binomial
proportion confidence interval.
+ *
+ * @see <a
+ *
href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Normal_approximation_interval">
+ * Normal approximation interval (Wikipedia)</a>
+ * @version $Id$
+ * @since 3.3
+ */
+public class NormalApproximationInterval implements BinomialConfidenceInterval
{
+
+ /** {@inheritDoc} */
+ @Override
+ public ConfidenceInterval createInterval(int numberOfTrials, int
numberOfSuccesses,
+ double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ final double mean = (double) numberOfSuccesses / (double)
numberOfTrials;
+ final double alpha = (1.0 - confidenceLevel) / 2;
+ final NormalDistribution normalDistribution = new NormalDistribution();
+ final double difference =
normalDistribution.inverseCumulativeProbability(1 - alpha) *
+ FastMath.sqrt(1.0 / numberOfTrials * mean *
(1 - mean));
+ return new ConfidenceInterval(mean - difference, mean + difference,
confidenceLevel);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,38 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the Wilson score method for creating a binomial proportion
confidence interval.
+ *
+ * @see <a
+ *
href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval">
+ * Wilson score interval (Wikipedia)</a>
+ * @version $Id$
+ * @since 3.3
+ */
+public class WilsonScoreInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ @Override
+ public ConfidenceInterval createInterval(int numberOfTrials, int
numberOfSuccesses, double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses,
confidenceLevel);
+ final double alpha = (1.0 - confidenceLevel) / 2;
+ final NormalDistribution normalDistribution = new NormalDistribution();
+ final double z = normalDistribution.inverseCumulativeProbability(1 -
alpha);
+ final double zSquared = FastMath.pow(z, 2);
+ final double mean = (double) numberOfSuccesses / (double)
numberOfTrials;
+
+ final double factor = 1.0 / (1 + (1.0 / numberOfTrials) * zSquared);
+ final double modifiedSuccessRatio = mean + (1.0 / (2 *
numberOfTrials)) * zSquared;
+ final double difference = z *
+ FastMath.sqrt(1.0 / numberOfTrials * mean *
(1 - mean) +
+ (1.0 / (4 *
FastMath.pow(numberOfTrials, 2)) * zSquared));
+
+ final double lowerBound = factor * (modifiedSuccessRatio - difference);
+ final double upperBound = factor * (modifiedSuccessRatio + difference);
+ return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+/**
+ *
+ * Classes providing binomial proportion confidence interval construction.
+ *
+ */
+package org.apache.commons.math3.stat.interval;
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,28 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.stat.interval.AgrestiCoullInterval;
+import org.apache.commons.math3.stat.interval.BinomialConfidenceInterval;
+import org.apache.commons.math3.stat.interval.ConfidenceInterval;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the AgrestiCoullInterval class.
+ *
+ * @version $Id$
+ */
+public class AgrestiCoullIntervalTest extends
BinomialConfidenceIntervalAbstractTest {
+
+ @Override
+ protected BinomialConfidenceInterval createBinomialConfidenceInterval() {
+ return new AgrestiCoullInterval();
+ }
+
+ @Test
+ public void testStandardInterval() {
+ ConfidenceInterval confidenceInterval = createStandardTestInterval();
+ Assert.assertEquals(0.07993521, confidenceInterval.getLowerBound(),
1E-5);
+ Assert.assertEquals(0.1243704, confidenceInterval.getUpperBound(),
1E-5);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/AgrestiCoullIntervalTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,83 @@
+/*
+ * 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.math3.stat.interval;
+
+import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.apache.commons.math3.stat.interval.BinomialConfidenceInterval;
+import org.apache.commons.math3.stat.interval.ConfidenceInterval;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for the BinomialConfidenceInterval implementations.
+ *
+ * @version $Id$
+ */
+public abstract class BinomialConfidenceIntervalAbstractTest {
+
+ protected BinomialConfidenceInterval testStatistic;
+
+ private final int successes = 50;
+ private final int trials = 500;
+ private final double confidenceLevel = 0.9;
+
+ protected abstract BinomialConfidenceInterval
createBinomialConfidenceInterval();
+
+ /**
+ * Returns the confidence interval for the given statistic with the
following values:
+ *
+ * <ul>
+ * <li>trials: 500</li>
+ * <li>successes: 50</li>
+ * <li>confidenceLevel: 0.9</li>
+ * </ul>
+ * @return the Confidence Interval for the given values
+ */
+ protected ConfidenceInterval createStandardTestInterval() {
+ return testStatistic.createInterval(trials, successes,
confidenceLevel);
+ }
+
+ @Before
+ public void setUp() {
+ testStatistic = createBinomialConfidenceInterval();
+ }
+
+ @Test(expected = MathIllegalArgumentException.class)
+ public void testZeroConfidencelevel() {
+ testStatistic.createInterval(trials, successes, 0d);
+ }
+
+ @Test(expected = MathIllegalArgumentException.class)
+ public void testOneConfidencelevel() {
+ testStatistic.createInterval(trials, successes, 1d);
+ }
+
+ @Test(expected = MathIllegalArgumentException.class)
+ public void testZeroTrials() {
+ testStatistic.createInterval(0, 0, confidenceLevel);
+ }
+
+ @Test(expected = MathIllegalArgumentException.class)
+ public void testNegativeSuccesses() {
+ testStatistic.createInterval(trials, -1, confidenceLevel);
+ }
+
+ @Test(expected = MathIllegalArgumentException.class)
+ public void testSuccessesExceedingTrials() {
+ testStatistic.createInterval(trials, trials + 1, confidenceLevel);
+ }
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/BinomialConfidenceIntervalAbstractTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,28 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.stat.interval.BinomialConfidenceInterval;
+import org.apache.commons.math3.stat.interval.ClopperPearsonInterval;
+import org.apache.commons.math3.stat.interval.ConfidenceInterval;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the ClopperPearsonInterval class.
+ *
+ * @version $Id$
+ */
+public class ClopperPearsonIntervalTest extends
BinomialConfidenceIntervalAbstractTest {
+
+ @Override
+ protected BinomialConfidenceInterval createBinomialConfidenceInterval() {
+ return new ClopperPearsonInterval();
+ }
+
+ @Test
+ public void testStandardInterval() {
+ ConfidenceInterval confidenceInterval = createStandardTestInterval();
+ Assert.assertEquals(0.07873857, confidenceInterval.getLowerBound(),
1E-5);
+ Assert.assertEquals(0.1248658, confidenceInterval.getUpperBound(),
1E-5);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/ClopperPearsonIntervalTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,49 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the IntervalUtils class.
+ *
+ * @version $Id$
+ */
+public class IntervalUtilsTest {
+
+ private final int successes = 50;
+ private final int trials = 500;
+ private final double confidenceLevel = 0.9;
+
+ // values to test must be exactly the same
+ private final double eps = 0.0;
+
+ @Test
+ public void testAgrestiCoull() {
+ checkConfidenceIntervals(new
AgrestiCoullInterval().createInterval(trials, successes, confidenceLevel),
+ IntervalUtils.getAgrestiCoullInterval(trials,
successes, confidenceLevel));
+ }
+
+ @Test
+ public void testClopperPearson() {
+ checkConfidenceIntervals(new
ClopperPearsonInterval().createInterval(trials, successes, confidenceLevel),
+
IntervalUtils.getClopperPearsonInterval(trials, successes, confidenceLevel));
+ }
+
+ @Test
+ public void testNormalApproximation() {
+ checkConfidenceIntervals(new
NormalApproximationInterval().createInterval(trials, successes,
confidenceLevel),
+
IntervalUtils.getNormalApproximationInterval(trials, successes,
confidenceLevel));
+ }
+
+ @Test
+ public void testWilsonScore() {
+ checkConfidenceIntervals(new
WilsonScoreInterval().createInterval(trials, successes, confidenceLevel),
+ IntervalUtils.getWilsonScoreInterval(trials,
successes, confidenceLevel));
+ }
+
+ private void checkConfidenceIntervals(ConfidenceInterval expected,
ConfidenceInterval actual) {
+ Assert.assertEquals(expected.getLowerBound(), actual.getLowerBound(),
eps);
+ Assert.assertEquals(expected.getUpperBound(), actual.getUpperBound(),
eps);
+ Assert.assertEquals(expected.getConfidenceLevel(),
actual.getConfidenceLevel(), eps);
+ }
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/IntervalUtilsTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,28 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.stat.interval.BinomialConfidenceInterval;
+import org.apache.commons.math3.stat.interval.ConfidenceInterval;
+import org.apache.commons.math3.stat.interval.NormalApproximationInterval;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the NormalApproximationInterval class.
+ *
+ * @version $Id$
+ */
+public class NormalApproximationIntervalTest extends
BinomialConfidenceIntervalAbstractTest {
+
+ @Override
+ protected BinomialConfidenceInterval createBinomialConfidenceInterval() {
+ return new NormalApproximationInterval();
+ }
+
+ @Test
+ public void testStandardInterval() {
+ ConfidenceInterval confidenceInterval = createStandardTestInterval();
+ Assert.assertEquals(0.07793197, confidenceInterval.getLowerBound(),
1E-5);
+ Assert.assertEquals(0.1220680, confidenceInterval.getUpperBound(),
1E-5);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/NormalApproximationIntervalTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java?rev=1560531&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
Wed Jan 22 22:00:37 2014
@@ -0,0 +1,28 @@
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.stat.interval.BinomialConfidenceInterval;
+import org.apache.commons.math3.stat.interval.ConfidenceInterval;
+import org.apache.commons.math3.stat.interval.WilsonScoreInterval;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the WilsonScoreInterval class.
+ *
+ * @version $Id$
+ */
+public class WilsonScoreIntervalTest extends
BinomialConfidenceIntervalAbstractTest {
+
+ @Override
+ protected BinomialConfidenceInterval createBinomialConfidenceInterval() {
+ return new WilsonScoreInterval();
+ }
+
+ @Test
+ public void testStandardInterval() {
+ ConfidenceInterval confidenceInterval = createStandardTestInterval();
+ Assert.assertEquals(0.08003919, confidenceInterval.getLowerBound(),
1E-5);
+ Assert.assertEquals(0.1242664, confidenceInterval.getUpperBound(),
1E-5);
+ }
+
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/interval/WilsonScoreIntervalTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain