From 2e3e0829846239f4f1378a81e307ed28b11f9ecc Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Tue, 9 Mar 2010 12:53:44 +0000 Subject: [PATCH] Issues https://issues.apache.org/jira/browse/MATH-348 https://issues.apache.org/jira/browse/MATH-349 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@920852 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 8 +- .../math/distribution/BetaDistribution.java | 12 ++- .../distribution/BetaDistributionImpl.java | 10 ++- .../distribution/BinomialDistribution.java | 4 + .../BinomialDistributionImpl.java | 36 +++++++-- .../math/distribution/CauchyDistribution.java | 4 + .../distribution/CauchyDistributionImpl.java | 33 ++++++-- .../distribution/ChiSquaredDistribution.java | 2 + .../ChiSquaredDistributionImpl.java | 35 +++++++-- .../distribution/ExponentialDistribution.java | 2 + .../ExponentialDistributionImpl.java | 24 ++++-- .../math/distribution/FDistribution.java | 4 + .../math/distribution/FDistributionImpl.java | 36 +++++++-- .../math/distribution/GammaDistribution.java | 4 + .../distribution/GammaDistributionImpl.java | 36 +++++++-- .../HypergeometricDistribution.java | 7 +- .../HypergeometricDistributionImpl.java | 78 ++++++++++++------- .../math/distribution/NormalDistribution.java | 4 + .../distribution/NormalDistributionImpl.java | 37 ++++++--- .../math/distribution/PascalDistribution.java | 4 + .../distribution/PascalDistributionImpl.java | 36 +++++++-- .../distribution/PoissonDistribution.java | 2 + .../distribution/PoissonDistributionImpl.java | 30 +++++-- .../math/distribution/TDistribution.java | 2 + .../math/distribution/TDistributionImpl.java | 15 +++- .../distribution/WeibullDistribution.java | 4 + .../distribution/WeibullDistributionImpl.java | 28 +++++-- .../math/distribution/ZipfDistribution.java | 5 +- .../distribution/ZipfDistributionImpl.java | 38 +++++++-- 29 files changed, 426 insertions(+), 114 deletions(-) diff --git a/pom.xml b/pom.xml index 4b709b03d..0e0d4e94e 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,11 @@ brentworden brentworden at apache dot org + + Gilles Sadowski + erans + erans at apache dot org + @@ -191,9 +196,6 @@ Matthew Rowles - - Gilles Sadowski - Joni Salonen diff --git a/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java b/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java index caac986df..8c6cf229b 100644 --- a/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/BetaDistribution.java @@ -27,10 +27,12 @@ import org.apache.commons.math.MathException; */ public interface BetaDistribution extends ContinuousDistribution, HasDensity { /** - * Modify the shape parameter, alpha. - * @param alpha the new shape parameter. - */ - void setAlpha(double alpha); + * Modify the shape parameter, alpha. + * @param alpha the new shape parameter. + * @deprecated as of 2.1 + */ + @Deprecated + void setAlpha(double alpha); /** * Access the shape parameter, alpha @@ -41,7 +43,9 @@ public interface BetaDistribution extends ContinuousDistribution, HasDensitytrials is not a valid * number of trials. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setNumberOfTrials(int trials) { + setNumberOfTrialsInternal(trials); + } + /** + * Change the number of trials for this distribution. + * + * @param trials the new number of trials. + * @throws IllegalArgumentException if trials is not a valid + * number of trials. + */ + private void setNumberOfTrialsInternal(int trials) { if (trials < 0) { throw MathRuntimeException.createIllegalArgumentException( "number of trials must be non-negative ({0})", trials); @@ -91,8 +103,20 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution * @param p the new probability of success. * @throws IllegalArgumentException if p is not a valid * probability. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setProbabilityOfSuccess(double p) { + setProbabilityOfSuccessInternal(p); + } + /** + * Change the probability of success for this distribution. + * + * @param p the new probability of success. + * @throws IllegalArgumentException if p is not a valid + * probability. + */ + private void setProbabilityOfSuccessInternal(double p) { if (p < 0.0 || p > 1.0) { throw MathRuntimeException.createIllegalArgumentException( "{0} out of [{1}, {2}] range", p, 0.0, 1.0); @@ -123,7 +147,7 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution */ @Override protected int getDomainUpperBound(double p) { - return getNumberOfTrials(); + return numberOfTrials; } /** @@ -139,11 +163,11 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution double ret; if (x < 0) { ret = 0.0; - } else if (x >= getNumberOfTrials()) { + } else if (x >= numberOfTrials) { ret = 1.0; } else { ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(), - x + 1.0, getNumberOfTrials() - x); + x + 1.0, numberOfTrials - x); } return ret; } @@ -156,7 +180,7 @@ public class BinomialDistributionImpl extends AbstractIntegerDistribution */ public double probability(int x) { double ret; - if (x < 0 || x > getNumberOfTrials()) { + if (x < 0 || x > numberOfTrials) { ret = 0.0; } else { ret = Math.exp(SaddlePointExpansion.logBinomialProbability(x, diff --git a/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java b/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java index 1743bc342..1b78d52e6 100644 --- a/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/CauchyDistribution.java @@ -48,12 +48,16 @@ public interface CauchyDistribution extends ContinuousDistribution { /** * Modify the median. * @param median for this distribution + * @deprecated as of v2.1 */ + @Deprecated void setMedian(double median); /** * Modify the scale parameter. * @param s scale parameter for this distribution + * @deprecated as of v2.1 */ + @Deprecated void setScale(double s); } diff --git a/src/main/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java index a7584a2c6..b10db0ddf 100644 --- a/src/main/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java @@ -55,8 +55,8 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution */ public CauchyDistributionImpl(double median, double s){ super(); - setMedian(median); - setScale(s); + setMedianInternal(median); + setScaleInternal(s); } /** @@ -115,8 +115,17 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution /** * Modify the median. * @param median for this distribution + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setMedian(double median) { + setMedianInternal(median); + } + /** + * Modify the median. + * @param median for this distribution + */ + private void setMedianInternal(double median) { this.median = median; } @@ -124,8 +133,18 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution * Modify the scale parameter. * @param s scale parameter for this distribution * @throws IllegalArgumentException if sd is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setScale(double s) { + setScaleInternal(s); + } + /** + * Modify the scale parameter. + * @param s scale parameter for this distribution + * @throws IllegalArgumentException if sd is not positive. + */ + private void setScaleInternal(double s) { if (s <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "scale must be positive ({0})", s); @@ -149,7 +168,7 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution if (p < .5) { ret = -Double.MAX_VALUE; } else { - ret = getMedian(); + ret = median; } return ret; @@ -169,7 +188,7 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution double ret; if (p < .5) { - ret = getMedian(); + ret = median; } else { ret = Double.MAX_VALUE; } @@ -190,11 +209,11 @@ public class CauchyDistributionImpl extends AbstractContinuousDistribution double ret; if (p < .5) { - ret = getMedian() - getScale(); + ret = median - scale; } else if (p > .5) { - ret = getMedian() + getScale(); + ret = median + scale; } else { - ret = getMedian(); + ret = median; } return ret; diff --git a/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java b/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java index eef22458c..764628291 100644 --- a/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java @@ -33,7 +33,9 @@ public interface ChiSquaredDistribution extends ContinuousDistribution, HasDensi /** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. + * @deprecated as of v2.1 */ + @Deprecated void setDegreesOfFreedom(double degreesOfFreedom); /** diff --git a/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java index 4066b33e7..398804747 100644 --- a/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java @@ -48,19 +48,31 @@ public class ChiSquaredDistributionImpl * @param df degrees of freedom. * @param g the underlying gamma distribution used to compute probabilities. * @since 1.2 + * @deprecated as of 2.1 (to avoid possibly inconsistent state, the + * "GammaDistribution" will be instantiated internally) */ + @Deprecated public ChiSquaredDistributionImpl(double df, GammaDistribution g) { super(); - setGamma(g); - setDegreesOfFreedom(df); + setGammaInternal(g); + setDegreesOfFreedomInternal(df); } /** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setDegreesOfFreedom(double degreesOfFreedom) { - getGamma().setAlpha(degreesOfFreedom / 2.0); + setDegreesOfFreedomInternal(degreesOfFreedom); + } + /** + * Modify the degrees of freedom. + * @param degreesOfFreedom the new degrees of freedom. + */ + private void setDegreesOfFreedomInternal(double degreesOfFreedom) { + gamma.setAlpha(degreesOfFreedom / 2.0); } /** @@ -68,7 +80,7 @@ public class ChiSquaredDistributionImpl * @return the degrees of freedom. */ public double getDegreesOfFreedom() { - return getGamma().getAlpha() * 2.0; + return gamma.getAlpha() * 2.0; } /** @@ -89,7 +101,7 @@ public class ChiSquaredDistributionImpl * computed due to convergence or other numerical errors. */ public double cumulativeProbability(double x) throws MathException { - return getGamma().cumulativeProbability(x); + return gamma.cumulativeProbability(x); } /** @@ -128,7 +140,7 @@ public class ChiSquaredDistributionImpl */ @Override protected double getDomainLowerBound(double p) { - return Double.MIN_VALUE * getGamma().getBeta(); + return Double.MIN_VALUE * gamma.getBeta(); } /** @@ -189,8 +201,19 @@ public class ChiSquaredDistributionImpl * insuring the gamma distribution has the proper parameter settings. * @param g the new distribution. * @since 1.2 made public + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setGamma(GammaDistribution g) { + setGammaInternal(g); + } + /** + * Modify the underlying gamma distribution. The caller is responsible for + * insuring the gamma distribution has the proper parameter settings. + * @param g the new distribution. + * @since 1.2 made public + */ + private void setGammaInternal(GammaDistribution g) { this.gamma = g; } diff --git a/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java b/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java index affc97627..6baa3cfc2 100644 --- a/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/ExponentialDistribution.java @@ -33,7 +33,9 @@ public interface ExponentialDistribution extends ContinuousDistribution, HasDens /** * Modify the mean. * @param mean the new mean. + * @deprecated as of v2.1 */ + @Deprecated void setMean(double mean); /** diff --git a/src/main/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java index 3fda341d5..27291716b 100644 --- a/src/main/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java @@ -41,15 +41,25 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution */ public ExponentialDistributionImpl(double mean) { super(); - setMean(mean); + setMeanInternal(mean); } /** * Modify the mean. * @param mean the new mean. * @throws IllegalArgumentException if mean is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setMean(double mean) { + setMeanInternal(mean); + } + /** + * Modify the mean. + * @param mean the new mean. + * @throws IllegalArgumentException if mean is not positive. + */ + private void setMeanInternal(double mean) { if (mean <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "mean must be positive ({0})", mean); @@ -75,7 +85,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution if (x < 0) { return 0; } - return Math.exp(-x / getMean()) / getMean(); + return Math.exp(-x / mean) / mean; } /** @@ -98,7 +108,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution if (x <= 0.0) { ret = 0.0; } else { - ret = 1.0 - Math.exp(-x / getMean()); + ret = 1.0 - Math.exp(-x / mean); } return ret; } @@ -125,7 +135,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution } else if (p == 1.0) { ret = Double.POSITIVE_INFINITY; } else { - ret = -getMean() * Math.log(1.0 - p); + ret = -mean * Math.log(1.0 - p); } return ret; @@ -159,7 +169,7 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution if (p < .5) { // use mean - return getMean(); + return mean; } else { // use max return Double.MAX_VALUE; @@ -181,10 +191,10 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution // Exponential is skewed to the left, therefore, P(X < μ) > .5 if (p < .5) { // use 1/2 mean - return getMean() * .5; + return mean * .5; } else { // use mean - return getMean(); + return mean; } } } diff --git a/src/main/java/org/apache/commons/math/distribution/FDistribution.java b/src/main/java/org/apache/commons/math/distribution/FDistribution.java index 17d85d012..1a2afe3a4 100644 --- a/src/main/java/org/apache/commons/math/distribution/FDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/FDistribution.java @@ -33,7 +33,9 @@ public interface FDistribution extends ContinuousDistribution { /** * Modify the numerator degrees of freedom. * @param degreesOfFreedom the new numerator degrees of freedom. + * @deprecated as of v2.1 */ + @Deprecated void setNumeratorDegreesOfFreedom(double degreesOfFreedom); /** @@ -45,7 +47,9 @@ public interface FDistribution extends ContinuousDistribution { /** * Modify the denominator degrees of freedom. * @param degreesOfFreedom the new denominator degrees of freedom. + * @deprecated as of v2.1 */ + @Deprecated void setDenominatorDegreesOfFreedom(double degreesOfFreedom); /** diff --git a/src/main/java/org/apache/commons/math/distribution/FDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/FDistributionImpl.java index 0b95fd8aa..b1200571e 100644 --- a/src/main/java/org/apache/commons/math/distribution/FDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/FDistributionImpl.java @@ -51,10 +51,10 @@ public class FDistributionImpl * @param denominatorDegreesOfFreedom the denominator degrees of freedom. */ public FDistributionImpl(double numeratorDegreesOfFreedom, - double denominatorDegreesOfFreedom) { + double denominatorDegreesOfFreedom) { super(); - setNumeratorDegreesOfFreedom(numeratorDegreesOfFreedom); - setDenominatorDegreesOfFreedom(denominatorDegreesOfFreedom); + setNumeratorDegreesOfFreedomInternal(numeratorDegreesOfFreedom); + setDenominatorDegreesOfFreedomInternal(denominatorDegreesOfFreedom); } /** @@ -77,8 +77,8 @@ public class FDistributionImpl if (x <= 0.0) { ret = 0.0; } else { - double n = getNumeratorDegreesOfFreedom(); - double m = getDenominatorDegreesOfFreedom(); + double n = numeratorDegreesOfFreedom; + double m = denominatorDegreesOfFreedom; ret = Beta.regularizedBeta((n * x) / (m + n * x), 0.5 * n, @@ -151,7 +151,7 @@ public class FDistributionImpl @Override protected double getInitialDomain(double p) { double ret = 1.0; - double d = getDenominatorDegreesOfFreedom(); + double d = denominatorDegreesOfFreedom; if (d > 2.0) { // use mean ret = d / (d - 2.0); @@ -164,8 +164,20 @@ public class FDistributionImpl * @param degreesOfFreedom the new numerator degrees of freedom. * @throws IllegalArgumentException if degreesOfFreedom is not * positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setNumeratorDegreesOfFreedom(double degreesOfFreedom) { + setNumeratorDegreesOfFreedomInternal(degreesOfFreedom); + } + + /** + * Modify the numerator degrees of freedom. + * @param degreesOfFreedom the new numerator degrees of freedom. + * @throws IllegalArgumentException if degreesOfFreedom is not + * positive. + */ + private void setNumeratorDegreesOfFreedomInternal(double degreesOfFreedom) { if (degreesOfFreedom <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( NON_POSITIVE_DEGREES_OF_FREEDOM_MESSAGE, degreesOfFreedom); @@ -186,8 +198,20 @@ public class FDistributionImpl * @param degreesOfFreedom the new denominator degrees of freedom. * @throws IllegalArgumentException if degreesOfFreedom is not * positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setDenominatorDegreesOfFreedom(double degreesOfFreedom) { + setDenominatorDegreesOfFreedomInternal(degreesOfFreedom); + } + + /** + * Modify the denominator degrees of freedom. + * @param degreesOfFreedom the new denominator degrees of freedom. + * @throws IllegalArgumentException if degreesOfFreedom is not + * positive. + */ + private void setDenominatorDegreesOfFreedomInternal(double degreesOfFreedom) { if (degreesOfFreedom <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( NON_POSITIVE_DEGREES_OF_FREEDOM_MESSAGE, degreesOfFreedom); diff --git a/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java b/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java index 5db179d1d..795ed32f4 100644 --- a/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/GammaDistribution.java @@ -33,7 +33,9 @@ public interface GammaDistribution extends ContinuousDistribution, HasDensityalpha is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setAlpha(double alpha) { + setAlphaInternal(alpha); + } + + /** + * Modify the shape parameter, alpha. + * @param alpha the new shape parameter. + * @throws IllegalArgumentException if alpha is not positive. + */ + private void setAlphaInternal(double alpha) { if (alpha <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "alpha must be positive ({0})", @@ -130,8 +141,19 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution * Modify the scale parameter, beta. * @param beta the new scale parameter. * @throws IllegalArgumentException if beta is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setBeta(double beta) { + setBetaInternal(beta); + } + + /** + * Modify the scale parameter, beta. + * @param beta the new scale parameter. + * @throws IllegalArgumentException if beta is not positive. + */ + private void setBetaInternal(double beta) { if (beta <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "beta must be positive ({0})", @@ -156,7 +178,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution */ public double density(Double x) { if (x < 0) return 0; - return Math.pow(x / getBeta(), getAlpha() - 1) / getBeta() * Math.exp(-x / getBeta()) / Math.exp(Gamma.logGamma(getAlpha())); + return Math.pow(x / beta, alpha - 1) / beta * Math.exp(-x / beta) / Math.exp(Gamma.logGamma(alpha)); } /** @@ -193,7 +215,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution if (p < .5) { // use mean - ret = getAlpha() * getBeta(); + ret = alpha * beta; } else { // use max value ret = Double.MAX_VALUE; @@ -219,10 +241,10 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution if (p < .5) { // use 1/2 mean - ret = getAlpha() * getBeta() * .5; + ret = alpha * beta * .5; } else { // use mean - ret = getAlpha() * getBeta(); + ret = alpha * beta; } return ret; diff --git a/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java b/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java index 25843e47e..e49977819 100644 --- a/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java @@ -53,19 +53,24 @@ public interface HypergeometricDistribution extends IntegerDistribution { /** * Modify the number of successes. * @param num the new number of successes. + * @deprecated as of v2.1 */ + @Deprecated void setNumberOfSuccesses(int num); /** * Modify the population size. * @param size the new population size. + * @deprecated as of v2.1 */ + @Deprecated void setPopulationSize(int size); /** * Modify the sample size. * @param size the new sample size. + * @deprecated as of v2.1 */ + @Deprecated void setSampleSize(int size); - } diff --git a/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java index 43e6bf692..56c84cb3e 100644 --- a/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java @@ -65,9 +65,10 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution "sample size ({0}) must be less than or equal to population size ({1})", sampleSize, populationSize); } - setPopulationSize(populationSize); - setSampleSize(sampleSize); - setNumberOfSuccesses(numberOfSuccesses); + + setPopulationSizeInternal(populationSize); + setSampleSizeInternal(sampleSize); + setNumberOfSuccessesInternal(numberOfSuccesses); } /** @@ -80,17 +81,14 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution public double cumulativeProbability(int x) { double ret; - int n = getPopulationSize(); - int m = getNumberOfSuccesses(); - int k = getSampleSize(); - - int[] domain = getDomain(n, m, k); + int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x < domain[0]) { ret = 0.0; } else if (x >= domain[1]) { ret = 1.0; } else { - ret = innerCumulativeProbability(domain[0], x, 1, n, m, k); + ret = innerCumulativeProbability(domain[0], x, 1, populationSize, + numberOfSuccesses, sampleSize); } return ret; @@ -119,8 +117,7 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution */ @Override protected int getDomainLowerBound(double p) { - return getLowerDomain(getPopulationSize(), getNumberOfSuccesses(), - getSampleSize()); + return getLowerDomain(populationSize, numberOfSuccesses, sampleSize); } /** @@ -133,7 +130,7 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution */ @Override protected int getDomainUpperBound(double p) { - return getUpperDomain(getSampleSize(), getNumberOfSuccesses()); + return getUpperDomain(sampleSize, numberOfSuccesses); } /** @@ -197,23 +194,19 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution public double probability(int x) { double ret; - int m = getPopulationSize(); - int s = getNumberOfSuccesses(); - int f = m - s; - int k = getSampleSize(); - - int[] domain = getDomain(m, s, k); + int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x < domain[0] || x > domain[1]) { ret = 0.0; } else { - double p = (double) sampleSize / (double) m; - double q = (double) (m - sampleSize) / (double) m; + double p = (double) sampleSize / (double) populationSize; + double q = (double) (populationSize - sampleSize) / (double) populationSize; double p1 = SaddlePointExpansion.logBinomialProbability(x, numberOfSuccesses, p, q); double p2 = - SaddlePointExpansion.logBinomialProbability(sampleSize - x, f, p, q); + SaddlePointExpansion.logBinomialProbability(sampleSize - x, + populationSize - numberOfSuccesses, p, q); double p3 = - SaddlePointExpansion.logBinomialProbability(sampleSize, m, p, q); + SaddlePointExpansion.logBinomialProbability(sampleSize, populationSize, p, q); ret = Math.exp(p1 + p2 - p3); } @@ -241,8 +234,19 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution * * @param num the new number of successes. * @throws IllegalArgumentException if num is negative. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setNumberOfSuccesses(int num) { + setNumberOfSuccessesInternal(num); + } + /** + * Modify the number of successes. + * + * @param num the new number of successes. + * @throws IllegalArgumentException if num is negative. + */ + private void setNumberOfSuccessesInternal(int num) { if (num < 0) { throw MathRuntimeException.createIllegalArgumentException( "number of successes must be non-negative ({0})", num); @@ -255,8 +259,19 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution * * @param size the new population size. * @throws IllegalArgumentException if size is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setPopulationSize(int size) { + setPopulationSizeInternal(size); + } + /** + * Modify the population size. + * + * @param size the new population size. + * @throws IllegalArgumentException if size is not positive. + */ + private void setPopulationSizeInternal(int size) { if (size <= 0) { throw MathRuntimeException.createIllegalArgumentException( "population size must be positive ({0})", size); @@ -269,8 +284,19 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution * * @param size the new sample size. * @throws IllegalArgumentException if size is negative. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setSampleSize(int size) { + setSampleSizeInternal(size); + } + /** + * Modify the sample size. + * + * @param size the new sample size. + * @throws IllegalArgumentException if size is negative. + */ + private void setSampleSizeInternal(int size) { if (size < 0) { throw MathRuntimeException.createIllegalArgumentException( "sample size must be positive ({0})", size); @@ -288,17 +314,13 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution public double upperCumulativeProbability(int x) { double ret; - int n = getPopulationSize(); - int m = getNumberOfSuccesses(); - int k = getSampleSize(); - - int[] domain = getDomain(n, m, k); + final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x < domain[0]) { ret = 1.0; } else if (x > domain[1]) { ret = 0.0; } else { - ret = innerCumulativeProbability(domain[1], x, -1, n, m, k); + ret = innerCumulativeProbability(domain[1], x, -1, populationSize, numberOfSuccesses, sampleSize); } return ret; diff --git a/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java b/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java index 2cc611c01..f6d6335b9 100644 --- a/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/NormalDistribution.java @@ -39,7 +39,9 @@ public interface NormalDistribution extends ContinuousDistribution, HasDensitysd is not positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setStandardDeviation(double sd) { + setStandardDeviationInternal(sd); + } + /** + * Modify the standard deviation. + * @param sd standard deviation for this distribution + * @throws IllegalArgumentException if sd is not positive. + */ + private void setStandardDeviationInternal(double sd) { if (sd <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "standard deviation must be positive ({0})", @@ -128,8 +147,8 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution * @return The pdf at point x. */ public double density(Double x) { - double x0 = x - getMean(); - return Math.exp(-x0 * x0 / (2 * getStandardDeviation() * getStandardDeviation())) / (getStandardDeviation() * SQRT2PI); + double x0 = x - mean; + return Math.exp(-x0 * x0 / (2 * standardDeviation * standardDeviation)) / (standardDeviation * SQRT2PI); } /** @@ -208,7 +227,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution if (p < .5) { ret = -Double.MAX_VALUE; } else { - ret = getMean(); + ret = mean; } return ret; @@ -228,7 +247,7 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution double ret; if (p < .5) { - ret = getMean(); + ret = mean; } else { ret = Double.MAX_VALUE; } @@ -249,11 +268,11 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution double ret; if (p < .5) { - ret = getMean() - getStandardDeviation(); + ret = mean - standardDeviation; } else if (p > .5) { - ret = getMean() + getStandardDeviation(); + ret = mean + standardDeviation; } else { - ret = getMean(); + ret = mean; } return ret; diff --git a/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java b/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java index b2d0496e1..ac8a67822 100644 --- a/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/PascalDistribution.java @@ -57,13 +57,17 @@ public interface PascalDistribution extends IntegerDistribution { * Change the number of successes for this distribution. * * @param successes the new number of successes + * @deprecated as of v2.1 */ + @Deprecated void setNumberOfSuccesses(int successes); /** * Change the probability of success for this distribution. * * @param p the new probability of success + * @deprecated as of v2.1 */ + @Deprecated void setProbabilityOfSuccess(double p); } diff --git a/src/main/java/org/apache/commons/math/distribution/PascalDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/PascalDistributionImpl.java index 5a632b0db..0e9c3a8e0 100644 --- a/src/main/java/org/apache/commons/math/distribution/PascalDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/PascalDistributionImpl.java @@ -48,8 +48,8 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution */ public PascalDistributionImpl(int r, double p) { super(); - setNumberOfSuccesses(r); - setProbabilityOfSuccess(p); + setNumberOfSuccessesInternal(r); + setProbabilityOfSuccessInternal(p); } /** @@ -73,8 +73,19 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution * @param successes the new number of successes * @throws IllegalArgumentException if successes is not * positive. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setNumberOfSuccesses(int successes) { + setNumberOfSuccessesInternal(successes); + } + /** + * Change the number of successes for this distribution. + * @param successes the new number of successes + * @throws IllegalArgumentException if successes is not + * positive. + */ + private void setNumberOfSuccessesInternal(int successes) { if (successes < 0) { throw MathRuntimeException.createIllegalArgumentException( "number of successes must be non-negative ({0})", @@ -88,8 +99,19 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution * @param p the new probability of success * @throws IllegalArgumentException if p is not a valid * probability. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setProbabilityOfSuccess(double p) { + setProbabilityOfSuccessInternal(p); + } + /** + * Change the probability of success for this distribution. + * @param p the new probability of success + * @throws IllegalArgumentException if p is not a valid + * probability. + */ + private void setProbabilityOfSuccessInternal(double p) { if (p < 0.0 || p > 1.0) { throw MathRuntimeException.createIllegalArgumentException( "{0} out of [{1}, {2}] range", p, 0.0, 1.0); @@ -135,8 +157,8 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution if (x < 0) { ret = 0.0; } else { - ret = Beta.regularizedBeta(getProbabilityOfSuccess(), - getNumberOfSuccesses(), x + 1); + ret = Beta.regularizedBeta(probabilityOfSuccess, + numberOfSuccesses, x + 1); } return ret; } @@ -152,9 +174,9 @@ public class PascalDistributionImpl extends AbstractIntegerDistribution ret = 0.0; } else { ret = MathUtils.binomialCoefficientDouble(x + - getNumberOfSuccesses() - 1, getNumberOfSuccesses() - 1) * - Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses()) * - Math.pow(1.0 - getProbabilityOfSuccess(), x); + numberOfSuccesses - 1, numberOfSuccesses - 1) * + Math.pow(probabilityOfSuccess, numberOfSuccesses) * + Math.pow(1.0 - probabilityOfSuccess, x); } return ret; } diff --git a/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java b/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java index 3b6305018..6aca50999 100644 --- a/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/PoissonDistribution.java @@ -47,7 +47,9 @@ public interface PoissonDistribution extends IntegerDistribution { * * @param p the mean * @throws IllegalArgumentException if p ≤ 0 + * @deprecated as of v2.1 */ + @Deprecated void setMean(double p); /** diff --git a/src/main/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java index 9e2509978..9c8dc12a6 100644 --- a/src/main/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java @@ -121,11 +121,13 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * @param z a normal distribution used to compute normal approximations. * @throws IllegalArgumentException if p ≤ 0 * @since 1.2 + * @deprecated as of 2.1 (to avoid possibly inconsistent state, the + * "NormalDistribution" will be instantiated internally) */ + @Deprecated public PoissonDistributionImpl(double p, NormalDistribution z) { super(); - setNormal(z); - setMean(p); + setNormalAndMeanInternal(z, p); } /** @@ -134,7 +136,7 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * @return the Poisson mean for the distribution. */ public double getMean() { - return this.mean; + return mean; } /** @@ -143,13 +145,28 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * * @param p the Poisson mean value * @throws IllegalArgumentException if p ≤ 0 + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setMean(double p) { + setNormalAndMeanInternal(normal, p); + } + /** + * Set the Poisson mean for the distribution. The mean value must be + * positive; otherwise an IllegalArgument is thrown. + * + * @param z the new distribution + * @param p the Poisson mean value + * @throws IllegalArgumentException if p ≤ 0 + */ + private void setNormalAndMeanInternal(NormalDistribution z, + double p) { if (p <= 0) { throw MathRuntimeException.createIllegalArgumentException( "the Poisson mean must be positive ({0})", p); } - this.mean = p; + mean = p; + normal = z; normal.setMean(p); normal.setStandardDeviation(Math.sqrt(p)); } @@ -248,9 +265,10 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * * @param value the new distribution * @since 1.2 + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setNormal(NormalDistribution value) { - normal = value; + setNormalAndMeanInternal(value, mean); } - } diff --git a/src/main/java/org/apache/commons/math/distribution/TDistribution.java b/src/main/java/org/apache/commons/math/distribution/TDistribution.java index d22200c49..233fe6d95 100644 --- a/src/main/java/org/apache/commons/math/distribution/TDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/TDistribution.java @@ -33,7 +33,9 @@ public interface TDistribution extends ContinuousDistribution { /** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. + * @deprecated as of v2.1 */ + @Deprecated void setDegreesOfFreedom(double degreesOfFreedom); /** diff --git a/src/main/java/org/apache/commons/math/distribution/TDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/TDistributionImpl.java index 29ef29d5f..f29445db2 100644 --- a/src/main/java/org/apache/commons/math/distribution/TDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/TDistributionImpl.java @@ -44,14 +44,23 @@ public class TDistributionImpl */ public TDistributionImpl(double degreesOfFreedom) { super(); - setDegreesOfFreedom(degreesOfFreedom); + setDegreesOfFreedomInternal(degreesOfFreedom); } /** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setDegreesOfFreedom(double degreesOfFreedom) { + setDegreesOfFreedomInternal(degreesOfFreedom); + } + /** + * Modify the degrees of freedom. + * @param degreesOfFreedom the new degrees of freedom. + */ + private void setDegreesOfFreedomInternal(double degreesOfFreedom) { if (degreesOfFreedom <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "degrees of freedom must be positive ({0})", @@ -82,8 +91,8 @@ public class TDistributionImpl } else { double t = Beta.regularizedBeta( - getDegreesOfFreedom() / (getDegreesOfFreedom() + (x * x)), - 0.5 * getDegreesOfFreedom(), + degreesOfFreedom / (degreesOfFreedom + (x * x)), + 0.5 * degreesOfFreedom, 0.5); if (x < 0.0) { ret = 0.5 * t; diff --git a/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java b/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java index e58bc603e..834ffb234 100644 --- a/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/WeibullDistribution.java @@ -51,12 +51,16 @@ public interface WeibullDistribution extends ContinuousDistribution { /** * Modify the shape parameter. * @param alpha The new shape parameter value. + * @deprecated as of v2.1 */ + @Deprecated void setShape(double alpha); /** * Modify the scale parameter. * @param beta The new scale parameter value. + * @deprecated as of v2.1 */ + @Deprecated void setScale(double beta); } diff --git a/src/main/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java index 4f4985af3..7347a5808 100644 --- a/src/main/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java @@ -48,8 +48,8 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution */ public WeibullDistributionImpl(double alpha, double beta){ super(); - setShape(alpha); - setScale(beta); + setShapeInternal(alpha); + setScaleInternal(beta); } /** @@ -62,7 +62,7 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution if (x <= 0.0) { ret = 0.0; } else { - ret = 1.0 - Math.exp(-Math.pow(x / getScale(), getShape())); + ret = 1.0 - Math.exp(-Math.pow(x / scale, shape)); } return ret; } @@ -106,7 +106,7 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution } else if (p == 1) { ret = Double.POSITIVE_INFINITY; } else { - ret = getScale() * Math.pow(-Math.log(1.0 - p), 1.0 / getShape()); + ret = scale * Math.pow(-Math.log(1.0 - p), 1.0 / shape); } return ret; } @@ -114,8 +114,17 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution /** * Modify the shape parameter. * @param alpha the new shape parameter value. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setShape(double alpha) { + setShapeInternal(alpha); + } + /** + * Modify the shape parameter. + * @param alpha the new shape parameter value. + */ + private void setShapeInternal(double alpha) { if (alpha <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "shape must be positive ({0})", @@ -127,8 +136,17 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution /** * Modify the scale parameter. * @param beta the new scale parameter value. + * @deprecated as of 2.1 (class will become immutable in 3.0) */ + @Deprecated public void setScale(double beta) { + setScaleInternal(beta); + } + /** + * Modify the scale parameter. + * @param beta the new scale parameter value. + */ + private void setScaleInternal(double beta) { if (beta <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( "scale must be positive ({0})", @@ -176,6 +194,6 @@ public class WeibullDistributionImpl extends AbstractContinuousDistribution @Override protected double getInitialDomain(double p) { // use median - return Math.pow(getScale() * Math.log(2.0), 1.0 / getShape()); + return Math.pow(scale * Math.log(2.0), 1.0 / shape); } } diff --git a/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java b/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java index 4d9f72a8c..7d1026f90 100644 --- a/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/ZipfDistribution.java @@ -45,7 +45,9 @@ public interface ZipfDistribution extends IntegerDistribution { * * @param n the number of elements * @throws IllegalArgumentException if n ≤ 0 + * @deprecated as of v2.1 */ + @Deprecated void setNumberOfElements(int n); /** @@ -62,7 +64,8 @@ public interface ZipfDistribution extends IntegerDistribution { * * @param s the exponent * @throws IllegalArgumentException if s ≤ 0.0 + * @deprecated as of v2.1 */ + @Deprecated void setExponent(double s); - } diff --git a/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java b/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java index df45f8d6e..7728e5a62 100644 --- a/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java +++ b/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java @@ -49,8 +49,8 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution */ public ZipfDistributionImpl(final int numberOfElements, final double exponent) throws IllegalArgumentException { - setNumberOfElements(numberOfElements); - setExponent(exponent); + setNumberOfElementsInternal(numberOfElements); + setExponentInternal(exponent); } /** @@ -69,8 +69,21 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution * * @param n the number of elements * @exception IllegalArgumentException if n ≤ 0 + * @deprecated as of 2.1 (class will become immutable in 3.0) */ - public void setNumberOfElements(final int n) + @Deprecated + public void setNumberOfElements(final int n) { + setNumberOfElementsInternal(n); + } + /** + * Set the number of elements (e.g. corpus size) for the distribution. + * The parameter value must be positive; otherwise an + * IllegalArgumentException is thrown. + * + * @param n the number of elements + * @exception IllegalArgumentException if n ≤ 0 + */ + private void setNumberOfElementsInternal(final int n) throws IllegalArgumentException { if (n <= 0) { throw MathRuntimeException.createIllegalArgumentException( @@ -96,8 +109,21 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution * * @param s the exponent * @exception IllegalArgumentException if s ≤ 0.0 + * @deprecated as of 2.1 (class will become immutable in 3.0) */ - public void setExponent(final double s) + @Deprecated + public void setExponent(final double s) { + setExponentInternal(s); + } + /** + * Set the exponent characterising the distribution. + * The parameter value must be positive; otherwise an + * IllegalArgumentException is thrown. + * + * @param s the exponent + * @exception IllegalArgumentException if s ≤ 0.0 + */ + private void setExponentInternal(final double s) throws IllegalArgumentException { if (s <= 0.0) { throw MathRuntimeException.createIllegalArgumentException( @@ -114,7 +140,7 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution * @return the value of the probability mass function at x */ public double probability(final int x) { - if (x <= 0 || x > getNumberOfElements()) { + if (x <= 0 || x > numberOfElements) { return 0.0; } @@ -132,7 +158,7 @@ public class ZipfDistributionImpl extends AbstractIntegerDistribution public double cumulativeProbability(final int x) { if (x <= 0) { return 0.0; - } else if (x >= getNumberOfElements()) { + } else if (x >= numberOfElements) { return 1.0; }