Restored backward compatibility in distributions classes.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1054524 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2011-01-03 04:59:18 +00:00
parent 7218e827a4
commit 79c2fc7a52
32 changed files with 239 additions and 596 deletions

View File

@ -228,17 +228,4 @@ public abstract class AbstractContinuousDistribution
return solverAbsoluteAccuracy;
}
/**
* Access the lower bound of the support.
*
* @return lower bound of the support (might be Double.NEGATIVE_INFINITY)
*/
public abstract double getSupportLowerBound();
/**
* Access the upper bound of the support.
*
* @return upper bound of the support (might be Double.POSITIVE_INFINITY)
*/
public abstract double getSupportUpperBound();
}

View File

@ -33,18 +33,6 @@ public abstract class AbstractDistribution
/** Serializable version identifier */
private static final long serialVersionUID = -38038050983108802L;
/** Cached numerical mean */
private double numericalMean = Double.NaN;
/** Whether or not the numerical mean has been calculated */
private boolean numericalMeanIsCalculated = false;
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
/**
* Default constructor.
*/
@ -78,106 +66,4 @@ public abstract class AbstractDistribution
}
return cumulativeProbability(x1) - cumulativeProbability(x0);
}
/**
* This method invalidates cached moments when parameters change.
* Usually it is called from a sub-class when the distribution
* gets its parameters updated.
*
* @deprecated as of 2.2 (sub-classes will become immutable in 3.0)
*/
@Deprecated
protected void invalidateParameterDependentMoments() {
numericalMeanIsCalculated = false;
numericalVarianceIsCalculated = false;
}
/**
* Use this method to actually calculate the mean for the
* specific distribution. Use {@link #getNumericalMean()}
* (which implements caching) to actually get the mean.
*
* @return the mean or Double.NaN if it's not defined
*/
protected abstract double calculateNumericalMean();
/**
* Use this method to get the numerical value of the mean of this
* distribution.
*
* @return the mean or Double.NaN if it's not defined
*/
public double getNumericalMean() {
if (!numericalMeanIsCalculated) {
numericalMean = calculateNumericalMean();
numericalMeanIsCalculated = true;
}
return numericalMean;
}
/**
* Use this method to actually calculate the variance for the
* specific distribution. Use {@link #getNumericalVariance()}
* (which implements caching) to actually get the variance.
*
* @return the variance or Double.NaN if it's not defined
*/
protected abstract double calculateNumericalVariance();
/**
* Use this method to get the numerical value of the variance of this
* distribution.
*
* @return the variance (possibly Double.POSITIVE_INFINITY as
* for certain cases in {@link TDistributionImpl}) or
* Double.NaN if it's not defined
*/
public double getNumericalVariance() {
if (!numericalVarianceIsCalculated) {
numericalVariance = calculateNumericalVariance();
numericalVarianceIsCalculated = true;
}
return numericalVariance;
}
/**
* Use this method to get information about whether the lower bound
* of the support is inclusive or not.
*
* @return whether the lower bound of the support is inclusive or not
*/
public abstract boolean isSupportLowerBoundInclusive();
/**
* Use this method to get information about whether the upper bound
* of the support is inclusive or not.
*
* @return whether the upper bound of the support is inclusive or not
*/
public abstract boolean isSupportUpperBoundInclusive();
/**
* Use this method to get information about whether the support is connected,
* i.e. whether all values between the lower and upper bound of the support
* is included in the support.
*
* For {@link AbstractIntegerDistribution} the support is discrete, so
* if this is true, then the support is
* {lower bound, lower bound + 1, ..., upper bound}.
*
* For {@link AbstractContinuousDistribution} the support is continuous, so
* if this is true, then the support is the interval
* [lower bound, upper bound]
* where the limits are inclusive or not according to
* {@link #isSupportLowerBoundInclusive()} and {@link #isSupportUpperBoundInclusive()}
* (in the example both are true). If both are false, then the support is the interval
* (lower bound, upper bound)
*
* @return whether the support limits given by subclassed methods are connected or not
*/
public boolean isSupportConnected() {
return true;
}
}

View File

@ -293,20 +293,6 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*/
protected abstract int getDomainUpperBound(double p);
/**
* Access the lower bound of the support.
*
* @return lower bound of the support (Integer.MIN_VALUE for negative infinity)
*/
public abstract int getSupportLowerBound();
/**
* Access the upper bound of the support.
*
* @return upper bound of the support (Integer.MAX_VALUE for positive infinity)
*/
public abstract int getSupportUpperBound();
/**
* Use this method to get information about whether the lower bound
* of the support is inclusive or not. For discrete support,
@ -314,7 +300,6 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*
* @return true (always but at Integer.MIN_VALUE because of the nature of discrete support)
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
}
@ -326,7 +311,6 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution
*
* @return true (always but at Integer.MAX_VALUE because of the nature of discrete support)
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return true;
}

View File

@ -39,7 +39,7 @@ public class BetaDistributionImpl
extends AbstractContinuousDistribution implements BetaDistribution {
/**
* Default inverse cumulative probability accurac
* Default inverse cumulative probability accuracy
* @since 2.1
*/
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
@ -92,7 +92,6 @@ public class BetaDistributionImpl
public void setAlpha(double alpha) {
this.alpha = alpha;
z = Double.NaN;
invalidateParameterDependentMoments();
}
/** {@inheritDoc} */
@ -107,7 +106,6 @@ public class BetaDistributionImpl
public void setBeta(double beta) {
this.beta = beta;
z = Double.NaN;
invalidateParameterDependentMoments();
}
/** {@inheritDoc} */
@ -227,75 +225,60 @@ public class BetaDistributionImpl
}
/**
* {@inheritDoc}
*
* The lower bound of the support is always 0 no matter the parameters.
* Returns the lower bound of the support for this distribution.
* The support of the Beta distribution is always [0, 1], regardless
* of the parameters, so this method always returns 0.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for this distribution.
* The support of the Beta distribution is always [0, 1], regardless
* of the parameters, so this method always returns 1.
*
* The upper bound of the support is always 1 no matter the parameters.
*
* @return upper bound of the support (always 1)
* @return lower bound of the support (always 1)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return 1;
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For first shape parameter <code>s1</code> and
* second shape parameter <code>s2</code>, the mean is
* <code>s1 / (s1 + s2)</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
final double a = getAlpha();
return a / (a + getBeta());
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For first shape parameter <code>s1</code> and
* second shape parameter <code>s2</code>,
* the variance is
* <code>[ s1 * s2 ] / [ (s1 + s2)^2 * (s1 + s2 + 1) ]</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double a = getAlpha();
final double b = getBeta();
final double alphabetasum = a + b;
return (a * b) / ((alphabetasum * alphabetasum) * (alphabetasum + 1));
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -83,7 +83,6 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setNumberOfTrials(int trials) {
setNumberOfTrialsInternal(trials);
invalidateParameterDependentMoments();
}
/**
@ -112,7 +111,6 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setProbabilityOfSuccess(double p) {
setProbabilityOfSuccessInternal(p);
invalidateParameterDependentMoments();
}
/**
@ -226,55 +224,55 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the number of trials
* and probability parameter.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public int getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is the number of trials.
*
* @return upper bound of the support (equal to number of trials)
* @since 2.2
*/
@Override
public int getSupportUpperBound() {
return getNumberOfTrials();
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For <code>n</code> number of trials and
* probability parameter <code>p</code>, the mean is
* <code>n * p</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
return (double)getNumberOfTrials() * getProbabilityOfSuccess();
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For <code>n</code> number of trials and
* probability parameter <code>p</code>, the variance is
* <code>n * p * (1 - p)</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double p = getProbabilityOfSuccess();
return (double)getNumberOfTrials() * p * (1 - p);
}

View File

@ -86,7 +86,7 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
/**
* For this distribution, X, this method returns P(X &lt; <code>x</code>).
* @param x the value at which the CDF is evaluated.
* @return CDF evaluted at <code>x</code>.
* @return CDF evaluated at <code>x</code>.
*/
public double cumulativeProbability(double x) {
return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
@ -157,7 +157,6 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setMedian(double median) {
setMedianInternal(median);
invalidateParameterDependentMoments();
}
/**
@ -177,7 +176,6 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setScale(double s) {
setScaleInternal(s);
invalidateParameterDependentMoments();
}
/**
@ -273,68 +271,50 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
*
* The lower bound of the support is always negative infinity no matter
* the parameters.
* Returns the lower bound of the support for this distribution.
* The lower bound of the support of the Cauchy distribution is always
* negative infinity, regardless of the parameters.
*
* @return lower bound of the support (always Double.NEGATIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return Double.NEGATIVE_INFINITY;
}
/**
* {@inheritDoc}
*
* The upper bound of the support is always positive infinity no matter
* the parameters.
* Returns the upper bound of the support for this distribution.
* The upper bound of the support of the Cauchy distribution is always
* positive infinity, regardless of the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean.
*
* The mean is always undefined no matter the parameters.
* The mean is always undefined, regardless of the parameters.
*
* @return mean (always Double.NaN)
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
return Double.NaN;
}
/**
* {@inheritDoc}
* Returns the variance.
*
* The variance is always undefined no matter the parameters.
* The variance is always undefined, regardless of the parameters.
*
* @return variance (always Double.NaN)
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
return Double.NaN;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -91,7 +91,6 @@ public class ChiSquaredDistributionImpl
@Deprecated
public void setDegreesOfFreedom(double degreesOfFreedom) {
setDegreesOfFreedomInternal(degreesOfFreedom);
invalidateParameterDependentMoments();
}
/**
* Modify the degrees of freedom.
@ -272,70 +271,54 @@ public class ChiSquaredDistributionImpl
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the
* degrees of freedom.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound for the support for the distribution.
*
* The upper bound of the support is always positive infinity no matter the
* degrees of freedom.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean of the distribution.
*
* For <code>k</code> degrees of freedom, the mean is
* <code>k</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
return getDegreesOfFreedom();
}
/**
* {@inheritDoc}
* Returns the variance of the distribution.
*
* For <code>k</code> degrees of freedom, the variance is
* <code>2 * k</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
return 2*getDegreesOfFreedom();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -53,58 +53,4 @@ public interface Distribution {
*/
double cumulativeProbability(double x0, double x1) throws MathException;
/**
* Use this method to get the numerical value of the mean of this
* distribution.
*
* @return the mean or Double.NaN if it's not defined
*/
double getNumericalMean();
/**
* Use this method to get the numerical value of the variance of this
* distribution.
*
* @return the variance (possibly Double.POSITIVE_INFINITY as
* for certain cases in {@link TDistributionImpl}) or
* Double.NaN if it's not defined
*/
double getNumericalVariance();
/**
* Use this method to get information about whether the lower bound
* of the support is inclusive or not.
*
* @return whether the lower bound of the support is inclusive or not
*/
boolean isSupportLowerBoundInclusive();
/**
* Use this method to get information about whether the upper bound
* of the support is inclusive or not.
*
* @return whether the upper bound of the support is inclusive or not
*/
boolean isSupportUpperBoundInclusive();
/**
* Use this method to get information about whether the support is connected,
* i.e. whether all values between the lower and upper bound of the support
* is included in the support.
*
* For {@link AbstractIntegerDistribution} the support is discrete, so
* if this is true, then the support is
* {lower bound, lower bound + 1, ..., upper bound}.
*
* For {@link AbstractContinuousDistribution} the support is continuous, so
* if this is true, then the support is the interval
* [lower bound, upper bound]
* where the limits are inclusive or not according to
* {@link #isSupportLowerBoundInclusive()} and {@link #isSupportUpperBoundInclusive()}
* (in the example both are true). If both are false, then the support is the interval
* (lower bound, upper bound)
*
* @return whether the support limits given by subclassed methods are connected or not
*/
boolean isSupportConnected();
}

View File

@ -76,7 +76,6 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setMean(double mean) {
setMeanInternal(mean);
invalidateParameterDependentMoments();
}
/**
* Modify the mean.
@ -204,7 +203,6 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
* @return domain value lower bound, i.e.
* P(X &lt; <i>lower bound</i>) &lt; <code>p</code>
*/
@Override
protected double getDomainLowerBound(double p) {
return 0;
}
@ -217,7 +215,6 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
* @return domain value upper bound, i.e.
* P(X &lt; <i>upper bound</i>) &gt; <code>p</code>
*/
@Override
protected double getDomainUpperBound(double p) {
// NOTE: exponential is skewed to the left
// NOTE: therefore, P(X < &mu;) > .5
@ -266,70 +263,55 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the mean parameter.
* The lower bound of the support is always 0, regardless of the mean.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the mean parameter.
* The upper bound of the support is always positive infinity,
* regardless of the mean.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean of the distribution.
*
* For mean parameter <code>k</code>, the mean is
* <code>k</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
return getMean();
}
/**
* {@inheritDoc}
* Returns the variance of the distribution.
*
* For mean parameter <code>k</code>, the variance is
* <code>k^2</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double m = getMean();
return m * m;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -209,7 +209,6 @@ public class FDistributionImpl
@Deprecated
public void setNumeratorDegreesOfFreedom(double degreesOfFreedom) {
setNumeratorDegreesOfFreedomInternal(degreesOfFreedom);
invalidateParameterDependentMoments();
}
/**
@ -244,7 +243,6 @@ public class FDistributionImpl
@Deprecated
public void setDenominatorDegreesOfFreedom(double degreesOfFreedom) {
setDenominatorDegreesOfFreedomInternal(degreesOfFreedom);
invalidateParameterDependentMoments();
}
/**
@ -282,32 +280,32 @@ public class FDistributionImpl
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the parameters.
* The lower bound of the support is always 0, regardless of the parameters.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters.
* The upper bound of the support is always positive infinity,
* regardless of the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean of the distribution.
*
* For denominator degrees of freedom parameter <code>b</code>,
* the mean is
@ -316,10 +314,10 @@ public class FDistributionImpl
* <li>else <code>undefined</code>
* </ul>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
final double denominatorDF = getDenominatorDegreesOfFreedom();
if (denominatorDF > 2) {
@ -330,7 +328,7 @@ public class FDistributionImpl
}
/**
* {@inheritDoc}
* Returns the variance of the distribution.
*
* For numerator degrees of freedom parameter <code>a</code>
* and denominator degrees of freedom parameter <code>b</code>,
@ -343,10 +341,10 @@ public class FDistributionImpl
* <li>else <code>undefined</code>
* </ul>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double denominatorDF = getDenominatorDegreesOfFreedom();
if (denominatorDF > 4) {
@ -359,20 +357,4 @@ public class FDistributionImpl
return Double.NaN;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -137,7 +137,6 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setAlpha(double alpha) {
setAlphaInternal(alpha);
invalidateParameterDependentMoments();
}
/**
@ -171,7 +170,6 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setBeta(double newBeta) {
setBetaInternal(newBeta);
invalidateParameterDependentMoments();
}
/**
@ -302,72 +300,56 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the parameters.
* The lower bound of the support is always 0, regardless of the parameters.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters.
* The upper bound of the support is always positive infinity,
* regardless of the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For shape parameter <code>alpha</code> and scale
* parameter <code>beta</code>, the mean is
* <code>alpha * beta</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
return getAlpha() * getBeta();
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For shape parameter <code>alpha</code> and scale
* parameter <code>beta</code>, the variance is
* <code>alpha * beta^2</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double b = getBeta();
return getAlpha() * b * b;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -241,7 +241,6 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setNumberOfSuccesses(int num) {
setNumberOfSuccessesInternal(num);
invalidateParameterDependentMoments();
}
/**
@ -268,7 +267,6 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setPopulationSize(int size) {
setPopulationSizeInternal(size);
invalidateParameterDependentMoments();
}
/**
@ -295,7 +293,6 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setSampleSize(int size) {
setSampleSizeInternal(size);
invalidateParameterDependentMoments();
}
/**
* Modify the sample size.
@ -358,7 +355,7 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound for the support for the distribution.
*
* For population size <code>N</code>,
* number of successes <code>m</code>, and
@ -367,15 +364,15 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
* <code>max(0, n + m - N)</code>
*
* @return lower bound of the support
* @since 2.2
*/
@Override
public int getSupportLowerBound() {
return FastMath.max(0,
getSampleSize() + getNumberOfSuccesses() - getPopulationSize());
}
/**
* {@inheritDoc}
* Returns the upper bound for the support of the distribution.
*
* For number of successes <code>m</code> and
* sample size <code>n</code>,
@ -383,39 +380,39 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
* <code>min(m, n)</code>
*
* @return upper bound of the support
* @since 2.2
*/
@Override
public int getSupportUpperBound() {
return FastMath.min(getNumberOfSuccesses(), getSampleSize());
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For population size <code>N</code>,
* number of successes <code>m</code>, and
* sample size <code>n</code>, the mean is
* <code>n * m / N</code>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
protected double getNumericalMean() {
return (double)(getSampleSize() * getNumberOfSuccesses()) / (double)getPopulationSize();
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For population size <code>N</code>,
* number of successes <code>m</code>, and
* sample size <code>n</code>, the variance is
* <code>[ n * m * (N - n) * (N - m) ] / [ N^2 * (N - 1) ]</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double N = getPopulationSize();
final double m = getNumberOfSuccesses();
final double n = getSampleSize();

View File

@ -104,7 +104,6 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setMean(double mean) {
setMeanInternal(mean);
invalidateParameterDependentMoments();
}
/**
@ -132,7 +131,6 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
@Deprecated
public void setStandardDeviation(double sd) {
setStandardDeviationInternal(sd);
invalidateParameterDependentMoments();
}
/**
@ -310,70 +308,42 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always negative infinity
* no matter the parameters.
*
* @return lower bound of the support (always Double.NEGATIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return Double.NEGATIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@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}
* Returns the variance.
*
* For standard deviation parameter <code>s</code>,
* the variance is <code>s^2</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double s = getStandardDeviation();
return s * s;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -80,7 +80,6 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setNumberOfSuccesses(int successes) {
setNumberOfSuccessesInternal(successes);
invalidateParameterDependentMoments();
}
/**
@ -108,7 +107,6 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setProbabilityOfSuccess(double p) {
setProbabilityOfSuccessInternal(p);
invalidateParameterDependentMoments();
}
/**
@ -217,70 +215,62 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the parameters.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public int getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters. Positive infinity is symbolised
* no matter the parameters. Positive infinity is represented
* 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)
* @since 2.2
*/
@Override
public int getSupportUpperBound() {
return Integer.MAX_VALUE;
}
/**
* {@inheritDoc}
* Returns the mean.
*
* 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}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
final double p = getProbabilityOfSuccess();
final double r = getNumberOfSuccesses();
return ( r * p ) / ( 1 - p );
}
/**
* {@inheritDoc}
* Returns the variance.
*
* 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}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
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;
}
}

View File

@ -157,7 +157,6 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setMean(double p) {
setNormalAndMeanInternal(normal, p);
invalidateParameterDependentMoments();
}
/**
* Set the Poisson mean for the distribution. The mean value must be
@ -303,19 +302,19 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the mean parameter.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public int getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is positive infinity,
* regardless of the parameter values. There is no integer infinity,
@ -323,41 +322,22 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution
* {@link #isSupportUpperBoundInclusive()} returns <code>true</code>.
*
* @return upper bound of the support (always <code>Integer.MAX_VALUE</code> for positive infinity)
* @since 2.2
*/
@Override
public int getSupportUpperBound() {
return Integer.MAX_VALUE;
}
/**
* {@inheritDoc}
*
* For mean parameter <code>p</code>, the mean is <code>p</code>
*
* @return {@inheritDoc}
*/
@Override
protected double calculateNumericalMean() {
return getMean();
}
/**
* {@inheritDoc}
* Returns the variance of the distribution.
*
* For mean parameter <code>p</code>, the variance is <code>p</code>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
return getMean();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return true;
}
}

View File

@ -81,7 +81,6 @@ public class TDistributionImpl
@Deprecated
public void setDegreesOfFreedom(double degreesOfFreedom) {
setDegreesOfFreedomInternal(degreesOfFreedom);
invalidateParameterDependentMoments();
}
/**
@ -227,33 +226,33 @@ public class TDistributionImpl
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always negative infinity
* no matter the parameters.
*
* @return lower bound of the support (always Double.NEGATIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return Double.NEGATIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For degrees of freedom parameter df, the mean is
* <ul>
@ -261,10 +260,10 @@ public class TDistributionImpl
* <li>else <code>undefined</code></li>
* </ul>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
public double getNumericalMean() {
final double df = getDegreesOfFreedom();
if (df > 1) {
@ -275,7 +274,7 @@ public class TDistributionImpl
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For degrees of freedom parameter df, the variance is
* <ul>
@ -284,10 +283,10 @@ public class TDistributionImpl
* <li>else <code>undefined</code></li>
* </ul>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
public double getNumericalVariance() {
final double df = getDegreesOfFreedom();
if (df > 2) {
@ -301,19 +300,4 @@ public class TDistributionImpl
return Double.NaN;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
}
}

View File

@ -52,6 +52,18 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
/** Inverse cumulative probability accuracy */
private final double solverAbsoluteAccuracy;
/** Cached numerical mean */
private double numericalMean = Double.NaN;
/** Whether or not the numerical mean has been calculated */
private boolean numericalMeanIsCalculated = false;
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
/**
* Creates weibull distribution with the given shape and scale and a
* location equal to zero.
@ -81,7 +93,7 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
/**
* For this distribution, X, this method returns P(X &lt; <code>x</code>).
* @param x the value at which the CDF is evaluated.
* @return CDF evaluted at <code>x</code>.
* @return CDF evaluated at <code>x</code>.
*/
public double cumulativeProbability(double x) {
double ret;
@ -264,39 +276,39 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 0 no matter the parameters.
*
* @return lower bound of the support (always 0)
* @since 2.2
*/
@Override
public double getSupportLowerBound() {
return 0;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is always positive infinity
* no matter the parameters.
*
* @return upper bound of the support (always Double.POSITIVE_INFINITY)
* @since 2.2
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
* Calculates the mean.
*
* The mean is <code>scale * Gamma(1 + (1 / shape))</code>
* where <code>Gamma(...)</code> is the Gamma-function
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
final double sh = getShape();
final double sc = getScale();
@ -305,16 +317,16 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Calculates the variance.
*
* The variance is
* <code>scale^2 * Gamma(1 + (2 / shape)) - mean^2</code>
* where <code>Gamma(...)</code> is the Gamma-function
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
private double calculateNumericalVariance() {
final double sh = getShape();
final double sc = getScale();
final double mn = getNumericalMean();
@ -325,18 +337,42 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution
}
/**
* {@inheritDoc}
* Returns the mean of the distribution.
*
* @return the mean or Double.NaN if it's not defined
* @since 2.2
*/
@Override
public boolean isSupportLowerBoundInclusive() {
return true;
public double getNumericalMean() {
if (!numericalMeanIsCalculated) {
numericalMean = calculateNumericalMean();
numericalMeanIsCalculated = true;
}
return numericalMean;
}
/**
* {@inheritDoc}
* Returns the variance of the distribution.
*
* @return the variance (possibly Double.POSITIVE_INFINITY as
* for certain cases in {@link TDistributionImpl}) or
* Double.NaN if it's not defined
* @since 2.2
*/
@Override
public boolean isSupportUpperBoundInclusive() {
return false;
public double getNumericalVariance() {
if (!numericalVarianceIsCalculated) {
numericalVariance = calculateNumericalVariance();
numericalVarianceIsCalculated = true;
}
return numericalVariance;
}
/**
* Invalidates the cached mean and variance.
*/
private void invalidateParameterDependentMoments() {
numericalMeanIsCalculated = false;
numericalVarianceIsCalculated = false;
}
}

View File

@ -76,7 +76,6 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setNumberOfElements(final int n) {
setNumberOfElementsInternal(n);
invalidateParameterDependentMoments();
}
/**
* Set the number of elements (e.g. corpus size) for the distribution.
@ -116,7 +115,6 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
@Deprecated
public void setExponent(final double s) {
setExponentInternal(s);
invalidateParameterDependentMoments();
}
/**
@ -215,31 +213,31 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the lower bound of the support for the distribution.
*
* The lower bound of the support is always 1 no matter the parameters.
*
* @return lower bound of the support (always 1)
* @since 2.2
*/
@Override
public int getSupportLowerBound() {
return 1;
}
/**
* {@inheritDoc}
* Returns the upper bound of the support for the distribution.
*
* The upper bound of the support is the number of elements
*
* @return upper bound of the support
* @since 2.2
*/
@Override
public int getSupportUpperBound() {
return getNumberOfElements();
}
/**
* {@inheritDoc}
* Returns the mean.
*
* For number of elements N and exponent s, the mean is
* <code>Hs1 / Hs</code> where
@ -248,10 +246,10 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
* <li><code>Hs = generalizedHarmonic(N, s)</code></li>
* </ul>
*
* @return {@inheritDoc}
* @return the mean
* @since 2.2
*/
@Override
protected double calculateNumericalMean() {
protected double getNumericalMean() {
final int N = getNumberOfElements();
final double s = getExponent();
@ -262,7 +260,7 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
}
/**
* {@inheritDoc}
* Returns the variance.
*
* For number of elements N and exponent s, the mean is
* <code>(Hs2 / Hs) - (Hs1^2 / Hs^2)</code> where
@ -272,10 +270,10 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution
* <li><code>Hs = generalizedHarmonic(N, s)</code></li>
* </ul>
*
* @return {@inheritDoc}
* @return the variance
* @since 2.2
*/
@Override
protected double calculateNumericalVariance() {
protected double getNumericalVariance() {
final int N = getNumberOfElements();
final double s = getExponent();

View File

@ -290,7 +290,7 @@ public class BetaDistributionTest extends TestCase {
public void testMomonts() {
final double tol = 1e-9;
BetaDistribution dist;
BetaDistributionImpl dist;
dist = new BetaDistributionImpl(1, 1);
assertEquals(dist.getNumericalMean(), 0.5, tol);

View File

@ -115,7 +115,7 @@ public class BinomialDistributionTest extends IntegerDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
BinomialDistribution dist;
BinomialDistributionImpl dist;
dist = new BinomialDistributionImpl(10, 0.5);
assertEquals(dist.getNumericalMean(), 10d * 0.5d, tol);

View File

@ -115,7 +115,7 @@ public class CauchyDistributionTest extends ContinuousDistributionAbstractTest
}
public void testMomonts() {
CauchyDistribution dist;
CauchyDistributionImpl dist;
dist = new CauchyDistributionImpl(10.2, 0.15);
assertTrue(Double.isNaN(dist.getNumericalMean()));

View File

@ -134,7 +134,7 @@ public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTes
public void testMomonts() {
final double tol = 1e-9;
ChiSquaredDistribution dist;
ChiSquaredDistributionImpl dist;
dist = new ChiSquaredDistributionImpl(1500);
assertEquals(dist.getNumericalMean(), 1500, tol);

View File

@ -124,7 +124,7 @@ public class ExponentialDistributionTest extends ContinuousDistributionAbstractT
public void testMomonts() {
final double tol = 1e-9;
ExponentialDistribution dist;
ExponentialDistributionImpl dist;
dist = new ExponentialDistributionImpl(11d);
assertEquals(dist.getNumericalMean(), 11d, tol);

View File

@ -131,7 +131,7 @@ public class FDistributionTest extends ContinuousDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
FDistribution dist;
FDistributionImpl dist;
dist = new FDistributionImpl(1, 2);
assertTrue(Double.isNaN(dist.getNumericalMean()));

View File

@ -155,7 +155,7 @@ public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
GammaDistribution dist;
GammaDistributionImpl dist;
dist = new GammaDistributionImpl(1, 2);
assertEquals(dist.getNumericalMean(), 2, tol);

View File

@ -214,7 +214,7 @@ public class HypergeometricDistributionTest extends IntegerDistributionAbstractT
public void testMomonts() {
final double tol = 1e-9;
HypergeometricDistribution dist;
HypergeometricDistributionImpl dist;
dist = new HypergeometricDistributionImpl(1500, 40, 100);
assertEquals(dist.getNumericalMean(), 40d * 100d / 1500d, tol);

View File

@ -206,20 +206,17 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
public void testMomonts() {
final double tol = 1e-9;
NormalDistribution dist;
NormalDistributionImpl dist;
dist = new NormalDistributionImpl(0, 1);
assertEquals(dist.getNumericalMean(), 0, tol);
assertEquals(dist.getNumericalVariance(), 1, tol);
dist.setMean(2.2);
dist.setStandardDeviation(1.4);
assertEquals(dist.getNumericalMean(), 2.2, tol);
assertEquals(dist.getNumericalVariance(), 1.4 * 1.4, tol);
dist.setMean(-2000.9);
dist.setStandardDeviation(10.4);
assertEquals(dist.getNumericalMean(), -2000.9, tol);
assertEquals(dist.getNumericalVariance(), 10.4 * 10.4, tol);
}
}

View File

@ -122,7 +122,7 @@ public class PascalDistributionTest extends IntegerDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
PascalDistribution dist;
PascalDistributionImpl dist;
dist = new PascalDistributionImpl(10, 0.5);
assertEquals(dist.getNumericalMean(), ( 10d * 0.5d ) / 0.5d, tol);

View File

@ -220,14 +220,12 @@ public class PoissonDistributionTest extends IntegerDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
PoissonDistribution dist;
PoissonDistributionImpl dist;
dist = new PoissonDistributionImpl(1);
assertEquals(dist.getNumericalMean(), 1, tol);
assertEquals(dist.getNumericalVariance(), 1, tol);
dist.setMean(11.23);
assertEquals(dist.getNumericalMean(), 11.23, tol);
assertEquals(dist.getNumericalVariance(), 11.23, tol);
}
}

View File

@ -118,7 +118,7 @@ public class TDistributionTest extends ContinuousDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
TDistribution dist;
TDistributionImpl dist;
dist = new TDistributionImpl(1);
assertTrue(Double.isNaN(dist.getNumericalMean()));

View File

@ -125,7 +125,7 @@ public class WeibullDistributionTest extends ContinuousDistributionAbstractTest
public void testMomonts() {
final double tol = 1e-9;
WeibullDistribution dist;
WeibullDistributionImpl dist;
dist = new WeibullDistributionImpl(2.5, 3.5);
// In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5)))

View File

@ -80,7 +80,7 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest {
public void testMomonts() {
final double tol = 1e-9;
ZipfDistribution dist;
ZipfDistributionImpl dist;
dist = new ZipfDistributionImpl(2, 0.5);
assertEquals(dist.getNumericalMean(), FastMath.sqrt(2), tol);