Merged NormalDistribution and NormalDistributionImpl (MATH-711).
Merged PascalDistribution and PascalDistributionImpl (MATH-711). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1206421 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ca7a127a14
commit
3f9a933d4c
|
@ -17,31 +17,266 @@
|
|||
|
||||
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;
|
||||
import org.apache.commons.math.special.Erf;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/**
|
||||
* Normal (Gauss) Distribution.
|
||||
*
|
||||
* <p>
|
||||
* References:</p><p>
|
||||
* <ul>
|
||||
* <li><a href="http://mathworld.wolfram.com/NormalDistribution.html">
|
||||
* Normal Distribution</a></li>
|
||||
* </ul>
|
||||
* </p>
|
||||
* Implementation of the normal (gaussian) distribution.
|
||||
*
|
||||
* @see <a href="http://en.wikipedia.org/wiki/Normal_distribution">Normal distribution (Wikipedia)</a>
|
||||
* @see <a href="http://mathworld.wolfram.com/NormalDistribution.html">Normal distribution (MathWorld)</a>
|
||||
* @version $Id$
|
||||
*/
|
||||
public interface NormalDistribution extends ContinuousDistribution {
|
||||
public class NormalDistribution 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 = 8589540077390120676L;
|
||||
/** √(2 π) */
|
||||
private static final double SQRT2PI = FastMath.sqrt(2 * FastMath.PI);
|
||||
/** √(2) */
|
||||
private static final double SQRT2 = FastMath.sqrt(2.0);
|
||||
/** Mean of this distribution. */
|
||||
private final double mean;
|
||||
/** Standard deviation of this distribution. */
|
||||
private final double standardDeviation;
|
||||
/** Inverse cumulative probability accuracy. */
|
||||
private final double solverAbsoluteAccuracy;
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean and standard deviation.
|
||||
*
|
||||
* @param mean Mean for this distribution.
|
||||
* @param sd Standard deviation for this distribution.
|
||||
* @throws NotStrictlyPositiveException if {@code sd <= 0}.
|
||||
*/
|
||||
public NormalDistribution(double mean, double sd)
|
||||
throws NotStrictlyPositiveException {
|
||||
this(mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean, standard deviation and
|
||||
* inverse cumulative distribution accuracy.
|
||||
*
|
||||
* @param mean Mean for this distribution.
|
||||
* @param sd Standard deviation for this distribution.
|
||||
* @param inverseCumAccuracy Inverse cumulative probability accuracy.
|
||||
* @throws NotStrictlyPositiveException if {@code sd <= 0}.
|
||||
* @since 2.1
|
||||
*/
|
||||
public NormalDistribution(double mean, double sd, double inverseCumAccuracy) {
|
||||
if (sd <= 0) {
|
||||
throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
|
||||
}
|
||||
|
||||
this.mean = mean;
|
||||
standardDeviation = sd;
|
||||
solverAbsoluteAccuracy = inverseCumAccuracy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a normal distribution with mean equal to zero and standard
|
||||
* deviation equal to one.
|
||||
*/
|
||||
public NormalDistribution(){
|
||||
this(0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the mean.
|
||||
*
|
||||
* @return the mean for this distribution.
|
||||
*/
|
||||
double getMean();
|
||||
public double getMean() {
|
||||
return mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the standard deviation.
|
||||
*
|
||||
* @return the standard deviation for this distribution.
|
||||
*/
|
||||
double getStandardDeviation();
|
||||
public double getStandardDeviation() {
|
||||
return standardDeviation;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double density(double x) {
|
||||
final double x0 = x - mean;
|
||||
final double x1 = x0 / standardDeviation;
|
||||
return FastMath.exp(-0.5 * x1 * x1) / (standardDeviation * SQRT2PI);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* If {@code x} is more than 40 standard deviations from the mean, 0 or 1
|
||||
* is returned, as in these cases the actual value is within
|
||||
* {@code Double.MIN_VALUE} of 0 or 1.
|
||||
*/
|
||||
public double cumulativeProbability(double x) {
|
||||
final double dev = x - mean;
|
||||
if (FastMath.abs(dev) > 40 * standardDeviation) {
|
||||
return dev < 0 ? 0.0d : 1.0d;
|
||||
}
|
||||
return 0.5 * (1 + Erf.erf(dev / (standardDeviation * SQRT2)));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double cumulativeProbability(double x0, double x1)
|
||||
throws NumberIsTooLargeException {
|
||||
if (x0 > x1) {
|
||||
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
|
||||
x0, x1, true);
|
||||
}
|
||||
final double denom = standardDeviation * SQRT2;
|
||||
final double v0 = (x0 - mean) / denom;
|
||||
final double v1 = (x1 - mean) / denom;
|
||||
return 0.5 * Erf.erf(v0, v1);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected double getSolverAbsoluteAccuracy() {
|
||||
return solverAbsoluteAccuracy;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0}
|
||||
* and {@code Double.POSITIVE_INFINITY} for {@code p == 1}.
|
||||
*/
|
||||
@Override
|
||||
public double inverseCumulativeProbability(final double p) {
|
||||
if (p == 0) {
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
}
|
||||
if (p == 1) {
|
||||
return Double.POSITIVE_INFINITY;
|
||||
}
|
||||
return super.inverseCumulativeProbability(p);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double sample() {
|
||||
return randomData.nextGaussian(mean, standardDeviation);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected double getDomainLowerBound(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = -Double.MAX_VALUE;
|
||||
} else {
|
||||
ret = mean;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected double getDomainUpperBound(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = mean;
|
||||
} else {
|
||||
ret = Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected double getInitialDomain(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = mean - standardDeviation;
|
||||
} else if (p > 0.5) {
|
||||
ret = mean + standardDeviation;
|
||||
} else {
|
||||
ret = mean;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The lower bound of the support is always negative infinity
|
||||
* no matter the parameters.
|
||||
*
|
||||
* @return lower bound of the support (always
|
||||
* {@code Double.NEGATIVE_INFINITY})
|
||||
*/
|
||||
@Override
|
||||
public double getSupportLowerBound() {
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The upper bound of the support is always positive infinity
|
||||
* no matter the parameters.
|
||||
*
|
||||
* @return upper bound of the support (always
|
||||
* {@code Double.POSITIVE_INFINITY})
|
||||
*/
|
||||
@Override
|
||||
public double getSupportUpperBound() {
|
||||
return Double.POSITIVE_INFINITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For mean parameter {@code mu}, the mean is {@code mu}.
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalMean() {
|
||||
return getMean();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For standard deviation parameter {@code s}, the variance is {@code s^2}.
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalVariance() {
|
||||
final double s = getStandardDeviation();
|
||||
return s * s;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean isSupportLowerBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean isSupportUpperBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,316 +0,0 @@
|
|||
/*
|
||||
* 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.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.special.Erf;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/**
|
||||
* Default implementation of
|
||||
* {@link org.apache.commons.math.distribution.NormalDistribution}.
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class NormalDistributionImpl extends AbstractContinuousDistribution
|
||||
implements NormalDistribution, 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 = 8589540077390120676L;
|
||||
/** √(2 π) */
|
||||
private static final double SQRT2PI = FastMath.sqrt(2 * FastMath.PI);
|
||||
/** √(2) */
|
||||
private static final double SQRT2 = FastMath.sqrt(2.0);
|
||||
/** Mean of this distribution. */
|
||||
private final double mean;
|
||||
/** Standard deviation of this distribution. */
|
||||
private final double standardDeviation;
|
||||
/** Inverse cumulative probability accuracy. */
|
||||
private final double solverAbsoluteAccuracy;
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean and standard deviation.
|
||||
*
|
||||
* @param mean Mean for this distribution.
|
||||
* @param sd Standard deviation for this distribution.
|
||||
*/
|
||||
public NormalDistributionImpl(double mean, double sd){
|
||||
this(mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean, standard deviation and
|
||||
* inverse cumulative distribution accuracy.
|
||||
*
|
||||
* @param mean Mean for this distribution.
|
||||
* @param sd Standard deviation for this distribution.
|
||||
* @param inverseCumAccuracy Inverse cumulative probability accuracy.
|
||||
* @throws NotStrictlyPositiveException if {@code sd <= 0}.
|
||||
* @since 2.1
|
||||
*/
|
||||
public NormalDistributionImpl(double mean, double sd, double inverseCumAccuracy) {
|
||||
if (sd <= 0) {
|
||||
throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
|
||||
}
|
||||
|
||||
this.mean = mean;
|
||||
standardDeviation = sd;
|
||||
solverAbsoluteAccuracy = inverseCumAccuracy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a normal distribution with mean equal to zero and standard
|
||||
* deviation equal to one.
|
||||
*/
|
||||
public NormalDistributionImpl(){
|
||||
this(0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double getMean() {
|
||||
return mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double getStandardDeviation() {
|
||||
return standardDeviation;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double density(double x) {
|
||||
final double x0 = x - mean;
|
||||
final double x1 = x0 / standardDeviation;
|
||||
return FastMath.exp(-0.5 * x1 * x1) / (standardDeviation * SQRT2PI);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* If {@code x} is more than 40 standard deviations from the mean, 0 or 1 is returned,
|
||||
* as in these cases the actual value is within {@code Double.MIN_VALUE} of 0 or 1.
|
||||
*/
|
||||
public double cumulativeProbability(double x) {
|
||||
final double dev = x - mean;
|
||||
if (FastMath.abs(dev) > 40 * standardDeviation) {
|
||||
return dev < 0 ? 0.0d : 1.0d;
|
||||
}
|
||||
return 0.5 * (1 + Erf.erf(dev / (standardDeviation * SQRT2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException {
|
||||
if (x0 > x1) {
|
||||
throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
|
||||
x0, x1, true);
|
||||
}
|
||||
final double denom = standardDeviation * SQRT2;
|
||||
final double v0 = (x0 - mean) / denom;
|
||||
final double v1 = (x1 - mean) / denom;
|
||||
return 0.5 * Erf.erf(v0, v1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the absolute accuracy setting of the solver used to estimate
|
||||
* inverse cumulative probabilities.
|
||||
*
|
||||
* @return the solver absolute accuracy.
|
||||
* @since 2.1
|
||||
*/
|
||||
@Override
|
||||
protected double getSolverAbsoluteAccuracy() {
|
||||
return solverAbsoluteAccuracy;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* It will return {@code Double.NEGATIVE_INFINITY} when {@code p = 0}
|
||||
* and {@code Double.POSITIVE_INFINITY} for {@code p = 1}.
|
||||
*/
|
||||
@Override
|
||||
public double inverseCumulativeProbability(final double p) {
|
||||
if (p == 0) {
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
}
|
||||
if (p == 1) {
|
||||
return Double.POSITIVE_INFINITY;
|
||||
}
|
||||
return super.inverseCumulativeProbability(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random value sampled from this distribution.
|
||||
*
|
||||
* @return a random value.
|
||||
* @since 2.2
|
||||
*/
|
||||
@Override
|
||||
public double sample() {
|
||||
return randomData.nextGaussian(mean, standardDeviation);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*/
|
||||
@Override
|
||||
protected double getDomainLowerBound(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = -Double.MAX_VALUE;
|
||||
} else {
|
||||
ret = mean;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*/
|
||||
@Override
|
||||
protected double getDomainUpperBound(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = mean;
|
||||
} else {
|
||||
ret = Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@Override
|
||||
protected double getInitialDomain(double p) {
|
||||
double ret;
|
||||
|
||||
if (p < 0.5) {
|
||||
ret = mean - standardDeviation;
|
||||
} else if (p > 0.5) {
|
||||
ret = mean + standardDeviation;
|
||||
} else {
|
||||
ret = mean;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The lower bound of the support is always negative infinity
|
||||
* no matter the parameters.
|
||||
*
|
||||
* @return lower bound of the support (always Double.NEGATIVE_INFINITY)
|
||||
*/
|
||||
@Override
|
||||
public double getSupportLowerBound() {
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The upper bound of the support is always positive infinity
|
||||
* no matter the parameters.
|
||||
*
|
||||
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
|
||||
*/
|
||||
@Override
|
||||
public double getSupportUpperBound() {
|
||||
return Double.POSITIVE_INFINITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For mean parameter <code>mu</code>, the mean is <code>mu</code>
|
||||
*
|
||||
* @return {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalMean() {
|
||||
return getMean();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For standard deviation parameter <code>s</code>,
|
||||
* the variance is <code>s^2</code>
|
||||
*
|
||||
* @return {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalVariance() {
|
||||
final double s = getStandardDeviation();
|
||||
return s * s;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportLowerBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportUpperBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -16,40 +16,213 @@
|
|||
*/
|
||||
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;
|
||||
import org.apache.commons.math.special.Beta;
|
||||
import org.apache.commons.math.util.ArithmeticUtils;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/**
|
||||
* The Pascal distribution. The Pascal distribution is a special case of the
|
||||
* Negative Binomial distribution where the number of successes parameter is an
|
||||
* integer.
|
||||
*
|
||||
* <p>
|
||||
* Implementation of the Pascal distribution. The Pascal distribution is a
|
||||
* special case of the Negative Binomial distribution where the number of
|
||||
* successes parameter is an integer.
|
||||
* </p>
|
||||
* <p>
|
||||
* There are various ways to express the probability mass and distribution
|
||||
* functions for the Pascal distribution. The convention employed by the
|
||||
* library is to express these functions in terms of the number of failures in
|
||||
* a Bernoulli experiment [2].
|
||||
*
|
||||
* <p>
|
||||
* References:
|
||||
* <ol>
|
||||
* <li><a href="http://mathworld.wolfram.com/NegativeBinomialDistribution.html">
|
||||
* Negative Binomial Distribution</a></li>
|
||||
* <oi><a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution#Waiting_time_in_a_Bernoulli_process">Waiting Time in a Bernoulli Process</a></li>
|
||||
* </ul>
|
||||
* a Bernoulli experiment
|
||||
* (see <a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution#Waiting_time_in_a_Bernoulli_process">Waiting Time in a Bernoulli Process</a>).
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution">
|
||||
* Negative binomial distribution (Wikipedia)</a>
|
||||
* @see <a href="http://mathworld.wolfram.com/NegativeBinomialDistribution.html">
|
||||
* Negative binomial distribution (MathWorld)</a>
|
||||
* @version $Id$
|
||||
* @since 1.2
|
||||
* @since 1.2 (changed to concrete class in 3.0)
|
||||
*/
|
||||
public interface PascalDistribution extends IntegerDistribution {
|
||||
public class PascalDistribution extends AbstractIntegerDistribution
|
||||
implements Serializable {
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = 6751309484392813623L;
|
||||
/** The number of successes. */
|
||||
private final int numberOfSuccesses;
|
||||
/** The probability of success. */
|
||||
private final double probabilityOfSuccess;
|
||||
|
||||
/**
|
||||
* Create a Pascal distribution with the given number of trials and
|
||||
* probability of success.
|
||||
*
|
||||
* @param r Number of successes.
|
||||
* @param p Probability of success.
|
||||
* @throws NotPositiveException if the number of successes is not positive
|
||||
* @throws OutOfRangeException if the probability of success is not in the
|
||||
* range [0, 1]
|
||||
*/
|
||||
public PascalDistribution(int r, double p)
|
||||
throws NotPositiveException, OutOfRangeException {
|
||||
if (r < 0) {
|
||||
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
|
||||
r);
|
||||
}
|
||||
if (p < 0 || p > 1) {
|
||||
throw new OutOfRangeException(p, 0, 1);
|
||||
}
|
||||
|
||||
numberOfSuccesses = r;
|
||||
probabilityOfSuccess = p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the number of successes for this distribution.
|
||||
*
|
||||
* @return the number of successes.
|
||||
*/
|
||||
int getNumberOfSuccesses();
|
||||
public int getNumberOfSuccesses() {
|
||||
return numberOfSuccesses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the probability of success for this distribution.
|
||||
*
|
||||
* @return the probability of success.
|
||||
*/
|
||||
double getProbabilityOfSuccess();
|
||||
public double getProbabilityOfSuccess() {
|
||||
return probabilityOfSuccess;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected int getDomainLowerBound(double p) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected int getDomainUpperBound(double p) {
|
||||
// use MAX - 1 because MAX causes loop
|
||||
return Integer.MAX_VALUE - 1;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double cumulativeProbability(int x) {
|
||||
double ret;
|
||||
if (x < 0) {
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = Beta.regularizedBeta(probabilityOfSuccess,
|
||||
numberOfSuccesses, x + 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double probability(int x) {
|
||||
double ret;
|
||||
if (x < 0) {
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = ArithmeticUtils.binomialCoefficientDouble(x +
|
||||
numberOfSuccesses - 1, numberOfSuccesses - 1) *
|
||||
FastMath.pow(probabilityOfSuccess, numberOfSuccesses) *
|
||||
FastMath.pow(1.0 - probabilityOfSuccess, x);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Returns {@code -1} when {@code p == 0} and
|
||||
* {@code Integer.MAX_VALUE} when {@code p == 1}.
|
||||
*/
|
||||
@Override
|
||||
public int inverseCumulativeProbability(final double p) {
|
||||
int ret;
|
||||
|
||||
// handle extreme values explicitly
|
||||
if (p == 0) {
|
||||
ret = -1;
|
||||
} else if (p == 1) {
|
||||
ret = Integer.MAX_VALUE;
|
||||
} else {
|
||||
ret = super.inverseCumulativeProbability(p);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The lower bound of the support is always 0 no matter the parameters.
|
||||
*
|
||||
* @return lower bound of the support (always 0)
|
||||
*/
|
||||
@Override
|
||||
public int getSupportLowerBound() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The upper bound of the support is always positive infinity no matter the
|
||||
* parameters. Positive infinity is symbolised by {@code Integer.MAX_VALUE}
|
||||
* together with {@link #isSupportUpperBoundInclusive()} being
|
||||
* {@code false}.
|
||||
*
|
||||
* @return upper bound of the support (always {@code Integer.MAX_VALUE}
|
||||
* for positive infinity)
|
||||
*/
|
||||
@Override
|
||||
public int getSupportUpperBound() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For number of successes {@code r} and probability of success {@code p},
|
||||
* the mean is {@code (r * p) / (1 - p)}.
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalMean() {
|
||||
final double p = getProbabilityOfSuccess();
|
||||
final double r = getNumberOfSuccesses();
|
||||
return (r * p) / (1 - p);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For number of successes {@code r} and probability of success {@code p},
|
||||
* the mean is {@code (r * p) / (1 - p)^2}.
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalVariance() {
|
||||
final double p = getProbabilityOfSuccess();
|
||||
final double r = getNumberOfSuccesses();
|
||||
final double pInv = 1 - p;
|
||||
return (r * p) / (pInv * pInv);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Always returns {@code false}.
|
||||
*
|
||||
* @see {@link PascalDistribution#getSupportUpperBound() getSupportUpperBound()}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportUpperBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,231 +0,0 @@
|
|||
/*
|
||||
* 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.exception.OutOfRangeException;
|
||||
import org.apache.commons.math.exception.NotPositiveException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.special.Beta;
|
||||
import org.apache.commons.math.util.ArithmeticUtils;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link PascalDistribution}.
|
||||
* @version $Id$
|
||||
* @since 1.2
|
||||
*/
|
||||
public class PascalDistributionImpl extends AbstractIntegerDistribution
|
||||
implements PascalDistribution, Serializable {
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = 6751309484392813623L;
|
||||
/** The number of successes. */
|
||||
private final int numberOfSuccesses;
|
||||
/** The probability of success. */
|
||||
private final double probabilityOfSuccess;
|
||||
|
||||
/**
|
||||
* Create a Pascal distribution with the given number of trials and
|
||||
* probability of success.
|
||||
*
|
||||
* @param r Number of successes.
|
||||
* @param p Probability of success.
|
||||
*/
|
||||
public PascalDistributionImpl(int r, double p) {
|
||||
if (r < 0) {
|
||||
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
|
||||
r);
|
||||
}
|
||||
if (p < 0 || p > 1) {
|
||||
throw new OutOfRangeException(p, 0, 1);
|
||||
}
|
||||
|
||||
numberOfSuccesses = r;
|
||||
probabilityOfSuccess = p;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int getNumberOfSuccesses() {
|
||||
return numberOfSuccesses;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double getProbabilityOfSuccess() {
|
||||
return probabilityOfSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value lower bound, based on {@code p}, used to
|
||||
* bracket a PDF root.
|
||||
*
|
||||
* @param p Desired probability for the critical value.
|
||||
* @return the domain value lower bound, i.e. {@code P(X < 'lower bound') < p}.
|
||||
*/
|
||||
@Override
|
||||
protected int getDomainLowerBound(double p) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value upper bound, based on {@code p}, used to
|
||||
* bracket a PDF root.
|
||||
*
|
||||
* @param p Desired probability for the critical value
|
||||
* @return the domain value upper bound, i.e. {@code P(X < 'upper bound') > p}.
|
||||
*/
|
||||
@Override
|
||||
protected int getDomainUpperBound(double p) {
|
||||
// use MAX - 1 because MAX causes loop
|
||||
return Integer.MAX_VALUE - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this distribution, {@code X}, this method returns {@code P(X <= x)}.
|
||||
*
|
||||
* @param x Value at which the PDF is evaluated.
|
||||
* @return PDF for this distribution.
|
||||
* due to convergence or other numerical errors.
|
||||
*/
|
||||
@Override
|
||||
public double cumulativeProbability(int x) {
|
||||
double ret;
|
||||
if (x < 0) {
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = Beta.regularizedBeta(probabilityOfSuccess,
|
||||
numberOfSuccesses, x + 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this distribution, {@code X}, this method returns {@code P(X = x)}.
|
||||
*
|
||||
* @param x Value at which the PMF is evaluated.
|
||||
* @return PMF for this distribution.
|
||||
*/
|
||||
public double probability(int x) {
|
||||
double ret;
|
||||
if (x < 0) {
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = ArithmeticUtils.binomialCoefficientDouble(x +
|
||||
numberOfSuccesses - 1, numberOfSuccesses - 1) *
|
||||
FastMath.pow(probabilityOfSuccess, numberOfSuccesses) *
|
||||
FastMath.pow(1.0 - probabilityOfSuccess, x);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this distribution, {@code X}, this method returns the largest
|
||||
* {@code x}, such that {@code P(X <= x) <= p}.
|
||||
* It will return -1 when p = 0 and {@code Integer.MAX_VALUE} when p = 1.
|
||||
*
|
||||
* @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}.
|
||||
*/
|
||||
@Override
|
||||
public int inverseCumulativeProbability(final double p) {
|
||||
int ret;
|
||||
|
||||
// handle extreme values explicitly
|
||||
if (p == 0) {
|
||||
ret = -1;
|
||||
} else if (p == 1) {
|
||||
ret = Integer.MAX_VALUE;
|
||||
} else {
|
||||
ret = super.inverseCumulativeProbability(p);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The lower bound of the support is always 0 no matter the parameters.
|
||||
*
|
||||
* @return lower bound of the support (always 0)
|
||||
*/
|
||||
@Override
|
||||
public int getSupportLowerBound() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* The upper bound of the support is always positive infinity
|
||||
* no matter the parameters. Positive infinity is symbolised
|
||||
* by <code>Integer.MAX_VALUE</code> together with
|
||||
* {@link #isSupportUpperBoundInclusive()} being <code>false</code>
|
||||
*
|
||||
* @return upper bound of the support (always <code>Integer.MAX_VALUE</code> for positive infinity)
|
||||
*/
|
||||
@Override
|
||||
public int getSupportUpperBound() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For number of successes <code>r</code> and
|
||||
* probability of success <code>p</code>, the mean is
|
||||
* <code>( r * p ) / ( 1 - p )</code>
|
||||
*
|
||||
* @return {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalMean() {
|
||||
final double p = getProbabilityOfSuccess();
|
||||
final double r = getNumberOfSuccesses();
|
||||
return ( r * p ) / ( 1 - p );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* For number of successes <code>r</code> and
|
||||
* probability of success <code>p</code>, the mean is
|
||||
* <code>( r * p ) / ( 1 - p )^2</code>
|
||||
*
|
||||
* @return {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected double calculateNumericalVariance() {
|
||||
final double p = getProbabilityOfSuccess();
|
||||
final double r = getNumberOfSuccesses();
|
||||
final double pInv = 1 - p;
|
||||
return ( r * p ) / (pInv * pInv);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isSupportUpperBoundInclusive() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -85,7 +85,7 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
|
|||
throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
|
||||
}
|
||||
mean = p;
|
||||
normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
|
||||
normal = new NormalDistribution(p, FastMath.sqrt(p));
|
||||
this.epsilon = epsilon;
|
||||
this.maxIterations = maxIterations;
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.apache.commons.math.distribution.ContinuousDistribution;
|
|||
import org.apache.commons.math.distribution.FDistribution;
|
||||
import org.apache.commons.math.distribution.HypergeometricDistribution;
|
||||
import org.apache.commons.math.distribution.IntegerDistribution;
|
||||
import org.apache.commons.math.distribution.PascalDistributionImpl;
|
||||
import org.apache.commons.math.distribution.PascalDistribution;
|
||||
import org.apache.commons.math.distribution.TDistributionImpl;
|
||||
import org.apache.commons.math.distribution.WeibullDistributionImpl;
|
||||
import org.apache.commons.math.distribution.ZipfDistributionImpl;
|
||||
|
@ -770,7 +770,7 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Generates a random value from the {@link PascalDistributionImpl Pascal Distribution}.
|
||||
* Generates a random value from the {@link PascalDistribution Pascal Distribution}.
|
||||
* This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion}
|
||||
* to generate random values.
|
||||
*
|
||||
|
@ -780,7 +780,7 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
* @since 2.2
|
||||
*/
|
||||
public int nextPascal(int r, double p) {
|
||||
return nextInversionDeviate(new PascalDistributionImpl(r, p));
|
||||
return nextInversionDeviate(new PascalDistribution(r, p));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
package org.apache.commons.math.stat.inference;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.distribution.NormalDistributionImpl;
|
||||
import org.apache.commons.math.distribution.NormalDistribution;
|
||||
import org.apache.commons.math.stat.ranking.NaNStrategy;
|
||||
import org.apache.commons.math.stat.ranking.NaturalRanking;
|
||||
import org.apache.commons.math.stat.ranking.TiesStrategy;
|
||||
|
@ -159,7 +159,7 @@ public class MannWhitneyUTestImpl implements MannWhitneyUTest {
|
|||
|
||||
final double z = (Umin - EU) / FastMath.sqrt(VarU);
|
||||
|
||||
final NormalDistributionImpl standardNormal = new NormalDistributionImpl(
|
||||
final NormalDistribution standardNormal = new NormalDistribution(
|
||||
0, 1);
|
||||
|
||||
return 2 * standardNormal.cumulativeProbability(z);
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
package org.apache.commons.math.stat.inference;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.distribution.NormalDistributionImpl;
|
||||
import org.apache.commons.math.distribution.NormalDistribution;
|
||||
import org.apache.commons.math.stat.ranking.NaNStrategy;
|
||||
import org.apache.commons.math.stat.ranking.NaturalRanking;
|
||||
import org.apache.commons.math.stat.ranking.TiesStrategy;
|
||||
|
@ -228,7 +228,7 @@ public class WilcoxonSignedRankTestImpl implements WilcoxonSignedRankTest {
|
|||
// - 0.5 is a continuity correction
|
||||
final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);
|
||||
|
||||
final NormalDistributionImpl standardNormal = new NormalDistributionImpl(0, 1);
|
||||
final NormalDistribution standardNormal = new NormalDistribution(0, 1);
|
||||
|
||||
return 2*standardNormal.cumulativeProbability(z);
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.junit.Test;
|
|||
public class CauchyDistributionTest extends ContinuousDistributionAbstractTest {
|
||||
|
||||
// --------------------- Override tolerance --------------
|
||||
protected double defaultTolerance = NormalDistributionImpl.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
|
|
@ -35,7 +35,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
/** Creates the default continuous distribution instance to use in tests. */
|
||||
@Override
|
||||
public NormalDistribution makeDistribution() {
|
||||
return new NormalDistributionImpl(2.1, 1.4);
|
||||
return new NormalDistribution(2.1, 1.4);
|
||||
}
|
||||
|
||||
/** Creates the default cumulative probability distribution test input values */
|
||||
|
@ -61,7 +61,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
}
|
||||
|
||||
// --------------------- Override tolerance --------------
|
||||
protected double defaultTolerance = NormalDistributionImpl.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
@ -90,13 +90,13 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
verifyQuantiles();
|
||||
verifyDensities();
|
||||
|
||||
setDistribution(new NormalDistributionImpl(0, 1));
|
||||
setDistribution(new NormalDistribution(0, 1));
|
||||
setDensityTestValues(new double[] {0.0539909665132, 0.241970724519, 0.398942280401, 0.241970724519, 0.0539909665132,
|
||||
0.00443184841194, 0.000133830225765, 1.48671951473e-06});
|
||||
verifyQuantiles();
|
||||
verifyDensities();
|
||||
|
||||
setDistribution(new NormalDistributionImpl(0, 0.1));
|
||||
setDistribution(new NormalDistribution(0, 0.1));
|
||||
setDensityTestValues(new double[] {0.539909665132, 2.41970724519, 3.98942280401, 2.41970724519,
|
||||
0.539909665132, 0.0443184841194, 0.00133830225765, 1.48671951473e-05});
|
||||
verifyQuantiles();
|
||||
|
@ -125,7 +125,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
|
||||
@Test(expected=NotStrictlyPositiveException.class)
|
||||
public void testPreconditions() {
|
||||
new NormalDistributionImpl(1, 0);
|
||||
new NormalDistribution(1, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -138,7 +138,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
}
|
||||
|
||||
private void checkDensity(double mean, double sd, double[] x, double[] expected) {
|
||||
NormalDistribution d = new NormalDistributionImpl(mean, sd);
|
||||
NormalDistribution d = new NormalDistribution(mean, sd);
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
Assert.assertEquals(expected[i], d.density(x[i]), 1e-9);
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
*/
|
||||
@Test
|
||||
public void testExtremeValues() throws Exception {
|
||||
NormalDistribution distribution = new NormalDistributionImpl(0, 1);
|
||||
NormalDistribution distribution = new NormalDistribution(0, 1);
|
||||
for (int i = 0; i < 100; i++) { // make sure no convergence exception
|
||||
double lowerTail = distribution.cumulativeProbability(-i);
|
||||
double upperTail = distribution.cumulativeProbability(i);
|
||||
|
@ -175,7 +175,7 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
|
||||
@Test
|
||||
public void testMath280() {
|
||||
NormalDistribution normal = new NormalDistributionImpl(0,1);
|
||||
NormalDistribution normal = new NormalDistribution(0,1);
|
||||
double result = normal.inverseCumulativeProbability(0.9986501019683698);
|
||||
Assert.assertEquals(3.0, result, defaultTolerance);
|
||||
result = normal.inverseCumulativeProbability(0.841344746068543);
|
||||
|
@ -191,15 +191,15 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
|
|||
final double tol = 1e-9;
|
||||
NormalDistribution dist;
|
||||
|
||||
dist = new NormalDistributionImpl(0, 1);
|
||||
dist = new NormalDistribution(0, 1);
|
||||
Assert.assertEquals(dist.getNumericalMean(), 0, tol);
|
||||
Assert.assertEquals(dist.getNumericalVariance(), 1, tol);
|
||||
|
||||
dist = new NormalDistributionImpl(2.2, 1.4);
|
||||
dist = new NormalDistribution(2.2, 1.4);
|
||||
Assert.assertEquals(dist.getNumericalMean(), 2.2, tol);
|
||||
Assert.assertEquals(dist.getNumericalVariance(), 1.4 * 1.4, tol);
|
||||
|
||||
dist = new NormalDistributionImpl(-2000.9, 10.4);
|
||||
dist = new NormalDistribution(-2000.9, 10.4);
|
||||
Assert.assertEquals(dist.getNumericalMean(), -2000.9, tol);
|
||||
Assert.assertEquals(dist.getNumericalVariance(), 10.4 * 10.4, tol);
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
|||
public class PascalDistributionTest extends IntegerDistributionAbstractTest {
|
||||
|
||||
// --------------------- Override tolerance --------------
|
||||
protected double defaultTolerance = NormalDistributionImpl.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
|
@ -41,7 +41,7 @@ public class PascalDistributionTest extends IntegerDistributionAbstractTest {
|
|||
/** Creates the default discrete distribution instance to use in tests. */
|
||||
@Override
|
||||
public IntegerDistribution makeDistribution() {
|
||||
return new PascalDistributionImpl(10,0.70);
|
||||
return new PascalDistribution(10,0.70);
|
||||
}
|
||||
|
||||
/** Creates the default probability density test input values */
|
||||
|
@ -90,7 +90,7 @@ public class PascalDistributionTest extends IntegerDistributionAbstractTest {
|
|||
/** Test degenerate case p = 0 */
|
||||
@Test
|
||||
public void testDegenerate0() throws Exception {
|
||||
setDistribution(new PascalDistributionImpl(5,0.0d));
|
||||
setDistribution(new PascalDistribution(5,0.0d));
|
||||
setCumulativeTestPoints(new int[] {-1, 0, 1, 5, 10 });
|
||||
setCumulativeTestValues(new double[] {0d, 0d, 0d, 0d, 0d});
|
||||
setDensityTestPoints(new int[] {-1, 0, 1, 10, 11});
|
||||
|
@ -105,7 +105,7 @@ public class PascalDistributionTest extends IntegerDistributionAbstractTest {
|
|||
/** Test degenerate case p = 1 */
|
||||
@Test
|
||||
public void testDegenerate1() throws Exception {
|
||||
setDistribution(new PascalDistributionImpl(5,1.0d));
|
||||
setDistribution(new PascalDistribution(5,1.0d));
|
||||
setCumulativeTestPoints(new int[] {-1, 0, 1, 2, 5, 10 });
|
||||
setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d, 1d});
|
||||
setDensityTestPoints(new int[] {-1, 0, 1, 2, 5, 10});
|
||||
|
@ -122,11 +122,11 @@ public class PascalDistributionTest extends IntegerDistributionAbstractTest {
|
|||
final double tol = 1e-9;
|
||||
PascalDistribution dist;
|
||||
|
||||
dist = new PascalDistributionImpl(10, 0.5);
|
||||
dist = new PascalDistribution(10, 0.5);
|
||||
Assert.assertEquals(dist.getNumericalMean(), ( 10d * 0.5d ) / 0.5d, tol);
|
||||
Assert.assertEquals(dist.getNumericalVariance(), ( 10d * 0.5d ) / (0.5d * 0.5d), tol);
|
||||
|
||||
dist = new PascalDistributionImpl(25, 0.3);
|
||||
dist = new PascalDistribution(25, 0.3);
|
||||
Assert.assertEquals(dist.getNumericalMean(), ( 25d * 0.3d ) / 0.7d, tol);
|
||||
Assert.assertEquals(dist.getNumericalVariance(), ( 25d * 0.3d ) / (0.7d * 0.7d), tol);
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.apache.commons.math.distribution.FDistribution;
|
|||
import org.apache.commons.math.distribution.GammaDistribution;
|
||||
import org.apache.commons.math.distribution.HypergeometricDistribution;
|
||||
import org.apache.commons.math.distribution.HypergeometricDistributionTest;
|
||||
import org.apache.commons.math.distribution.PascalDistributionImpl;
|
||||
import org.apache.commons.math.distribution.PascalDistribution;
|
||||
import org.apache.commons.math.distribution.PascalDistributionTest;
|
||||
import org.apache.commons.math.distribution.PoissonDistribution;
|
||||
import org.apache.commons.math.distribution.PoissonDistributionImpl;
|
||||
|
@ -1015,7 +1015,7 @@ public class RandomDataTest {
|
|||
double[] densityValues = testInstance.makeDensityTestValues();
|
||||
int sampleSize = 1000;
|
||||
int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
|
||||
PascalDistributionImpl distribution = (PascalDistributionImpl) testInstance.makeDistribution();
|
||||
PascalDistribution distribution = (PascalDistribution) testInstance.makeDistribution();
|
||||
double[] expectedCounts = new double[length];
|
||||
long[] observedCounts = new long[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
|
Loading…
Reference in New Issue