Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java Fri Oct 1 12:46:16 2010 @@ -19,7 +19,7 @@ package org.apache.commons.math.distribu import java.io.Serializable; -import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.FastMath; @@ -30,115 +30,56 @@ import org.apache.commons.math.util.Fast */ public class ZipfDistributionImpl extends AbstractIntegerDistribution implements ZipfDistribution, Serializable { - /** Serializable version identifier. */ private static final long serialVersionUID = -140627372283420404L; - /** Number of elements. */ - private int numberOfElements; - + private final int numberOfElements; /** Exponent parameter of the distribution. */ - private double exponent; + private final double exponent; /** * Create a new Zipf distribution with the given number of elements and - * exponent. Both values must be positive; otherwise an - * <code>IllegalArgumentException</code> is thrown. + * exponent. * - * @param numberOfElements the number of elements - * @param exponent the exponent - * @exception IllegalArgumentException if n ≤ 0 or s ≤ 0.0 - */ - public ZipfDistributionImpl(final int numberOfElements, final double exponent) - throws IllegalArgumentException { - setNumberOfElementsInternal(numberOfElements); - setExponentInternal(exponent); + * @param numberOfElements Number of elements. + * @param exponent Exponent. + * @exception NotStrictlyPositiveException if {...@code numberOfElements <= 0} + * or {...@code exponent <= 0}. + */ + public ZipfDistributionImpl(final int numberOfElements, + final double exponent) { + if (numberOfElements <= 0) { + throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION, + numberOfElements); + } + if (exponent <= 0) { + throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT, + exponent); + } + + this.numberOfElements = numberOfElements; + this.exponent = exponent; } /** - * Get the number of elements (e.g. corpus size) for the distribution. - * - * @return the number of elements + * {...@inheritdoc} */ public int getNumberOfElements() { return numberOfElements; } /** - * Set the number of elements (e.g. corpus size) for the distribution. - * The parameter value must be positive; otherwise an - * <code>IllegalArgumentException</code> is thrown. - * - * @param n the number of elements - * @exception IllegalArgumentException if n ≤ 0 - * @deprecated as of 2.1 (class will become immutable in 3.0) - */ - @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 - * <code>IllegalArgumentException</code> 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( - LocalizedFormats.INSUFFICIENT_DIMENSION, n, 0); - } - this.numberOfElements = n; - } - - /** - * Get the exponent characterising the distribution. - * - * @return the exponent + * {...@inheritdoc} */ public double getExponent() { return exponent; } /** - * Set the exponent characterising the distribution. - * The parameter value must be positive; otherwise an - * <code>IllegalArgumentException</code> is thrown. - * - * @param s the exponent - * @exception IllegalArgumentException if s ≤ 0.0 - * @deprecated as of 2.1 (class will become immutable in 3.0) - */ - @Deprecated - public void setExponent(final double s) { - setExponentInternal(s); - } - /** - * Set the exponent characterising the distribution. - * The parameter value must be positive; otherwise an - * <code>IllegalArgumentException</code> 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( - LocalizedFormats.NOT_POSITIVE_EXPONENT, - s); - } - this.exponent = s; - } - - /** - * The probability mass function P(X = x) for a Zipf distribution. + * The probability mass function {...@code P(X = x)} for a Zipf distribution. * - * @param x the value at which the probability density function is evaluated. - * @return the value of the probability mass function at x + * @param x Value at which the probability density function is evaluated. + * @return the value of the probability mass function at {...@code x}. */ public double probability(final int x) { if (x <= 0 || x > numberOfElements) { @@ -146,14 +87,14 @@ public class ZipfDistributionImpl extend } return (1.0 / FastMath.pow(x, exponent)) / generalizedHarmonic(numberOfElements, exponent); - } /** - * The probability distribution function P(X <= x) for a Zipf distribution. + * The probability distribution function {...@code P(X <= x)} for a + * Zipf distribution. * - * @param x the value at which the PDF is evaluated. - * @return Zipf distribution function evaluated at x + * @param x Value at which the PDF is evaluated. + * @return Zipf distribution function evaluated at {...@code x}. */ @Override public double cumulativeProbability(final int x) { @@ -164,16 +105,14 @@ public class ZipfDistributionImpl extend } return generalizedHarmonic(x, exponent) / generalizedHarmonic(numberOfElements, exponent); - } /** - * Access the domain value lower bound, based on <code>p</code>, used to + * Access the domain value lower bound, based on {...@code p}, used to * bracket a PDF root. * - * @param p the desired probability for the critical value - * @return domain value lower bound, i.e. - * P(X < <i>lower bound</i>) < <code>p</code> + * @param p Desired probability for the critical value. + * @return the domain value lower bound, i.e. {...@code P(X < 'lower bound') < p}. */ @Override protected int getDomainLowerBound(final double p) { @@ -181,27 +120,25 @@ public class ZipfDistributionImpl extend } /** - * Access the domain value upper bound, based on <code>p</code>, used to + * Access the domain value upper bound, based on {...@code p}, used to * bracket a PDF root. * - * @param p the desired probability for the critical value - * @return domain value upper bound, i.e. - * P(X < <i>upper bound</i>) > <code>p</code> + * @param p Desired probability for the critical value + * @return the domain value upper bound, i.e. {...@code P(X < 'upper bound') > p}. */ @Override protected int getDomainUpperBound(final double p) { return numberOfElements; } - /** * Calculates the Nth generalized harmonic number. See * <a href="http://mathworld.wolfram.com/HarmonicSeries.html">Harmonic * Series</a>. * - * @param n the term in the series to calculate (must be ≥ 1) - * @param m the exponent; special case m == 1.0 is the harmonic series - * @return the nth generalized harmonic number + * @param n Term in the series to calculate (must be larger than 1) + * @param m Exponent (special case {...@code m = 1} is the harmonic series). + * @return the n<sup>th</sup> generalized harmonic number. */ private double generalizedHarmonic(final int n, final double m) { double value = 0; @@ -210,5 +147,4 @@ public class ZipfDistributionImpl extend } return value; } - }
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java Fri Oct 1 12:46:16 2010 @@ -119,6 +119,7 @@ public enum LocalizedFormats implements INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES("instance of class {0} not comparable to existing values"), INSUFFICIENT_DATA_FOR_T_STATISTIC("insufficient data for t statistic, needs at least 2, got {0}"), INSUFFICIENT_DIMENSION("insufficient dimension {0}, must be at least {1}"), + DIMENSION("dimension ({0})"), /* keep */ INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE("sample contains {0} observed points, at least {1} are required"), INSUFFICIENT_ROWS_AND_COLUMNS("insufficient data: only {0} rows and {1} columns."), INTEGRATION_METHOD_NEEDS_AT_LEAST_ONE_PREVIOUS_POINT("{0} method needs at least one previous point"), @@ -150,7 +151,9 @@ public enum LocalizedFormats implements NEGATIVE_ELEMENT_AT_2D_INDEX("element ({0}, {1}) is negative: {2}"), NEGATIVE_ELEMENT_AT_INDEX("element {0} is negative: {1}"), NEGATIVE_NUMBER_OF_SUCCESSES("number of successes must be non-negative ({0})"), + NUMBER_OF_SUCCESSES("number of successes ({0})"), /* keep */ NEGATIVE_NUMBER_OF_TRIALS("number of trials must be non-negative ({0})"), + NUMBER_OF_TRIALS("number of trials ({0})"), NEGATIVE_ROBUSTNESS_ITERATIONS("the number of robustness iterations must be non-negative, but got {0}"), START_POSITION("start position ({0})"), /* keep */ NON_CONVERGENT_CONTINUED_FRACTION("Continued fraction convergents failed to converge for value {0}"), @@ -178,6 +181,7 @@ public enum LocalizedFormats implements NOT_POSITIVE_DEGREES_OF_FREEDOM("degrees of freedom must be positive ({0})"), NOT_POSITIVE_ELEMENT_AT_INDEX("element {0} is not positive: {1}"), NOT_POSITIVE_EXPONENT("invalid exponent {0} (must be positive)"), + EXPONENT("exponent ({0})"), /* keep */ NOT_POSITIVE_LENGTH("length must be positive ({0})"), LENGTH("length ({0})"), /* keep */ NOT_POSITIVE_MEAN("mean must be positive ({0})"), @@ -188,6 +192,7 @@ public enum LocalizedFormats implements PERMUTATION_SIZE("permutation size ({0}"), /* keep */ NOT_POSITIVE_POISSON_MEAN("the Poisson mean must be positive ({0})"), NOT_POSITIVE_POPULATION_SIZE("population size must be positive ({0})"), + POPULATION_SIZE("population size ({0})"), /* keep */ NOT_POSITIVE_ROW_DIMENSION("invalid row dimension: {0} (must be positive)"), NOT_POSITIVE_SAMPLE_SIZE("sample size must be positive ({0})"), NOT_POSITIVE_SCALE("scale must be positive ({0})"), Modified: commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties (original) +++ commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties Fri Oct 1 12:46:16 2010 @@ -91,6 +91,7 @@ INPUT_DATA_FROM_UNSUPPORTED_DATASOURCE = INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES = l''instance de la classe {0} n''est pas comparable aux valeurs existantes INSUFFICIENT_DATA_FOR_T_STATISTIC = deux valeurs ou plus sont n\u00e9cessaires pour la statistique t, il y en a {0} INSUFFICIENT_DIMENSION = dimension {0} insuffisante, elle devrait \u00eatre au moins {1} +DIMENSION = dimension ({0}) INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE = l''\u00e9chantillon ne contient que {0} points alors qu''au moins {1} sont n\u00e9cessaires INSUFFICIENT_ROWS_AND_COLUMNS = donn\u00e9es insuffisantes : seulement {0} lignes et {1} colonnes. INTEGRATION_METHOD_NEEDS_AT_LEAST_ONE_PREVIOUS_POINT = la m\u00e9thode {0} n\u00e9cessite au moins un point pr\u00e9c\u00e9dent @@ -122,7 +123,9 @@ NEGATIVE_COMPLEX_MODULE = module n\u00e9 NEGATIVE_ELEMENT_AT_2D_INDEX = l''\u00e9l\u00e9ment ({0}, {1}) est n\u00e9gatif : {2} NEGATIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} est n\u00e9gatif : {1} NEGATIVE_NUMBER_OF_SUCCESSES = le nombre de succ\u00e8s ne doit pas \u00eatre n\u00e9gatif ({0}) +NUMBER_OF_SUCCESSES = nombre de succ\u00e8s ({0}) NEGATIVE_NUMBER_OF_TRIALS = le nombre d''essais ne doit pas \u00eatre n\u00e9gatif ({0}) +NUMBER_OF_TRIALS = nombre d''essais ({0}) NEGATIVE_ROBUSTNESS_ITERATIONS = le nombre d''it\u00e9rations robuste ne peut \u00eatre n\u00e9gatif, alors qu''il est de {0} START_POSITION = position de d\u00e9part NON_CONVERGENT_CONTINUED_FRACTION = \u00c9chec de convergence de fraction continue pour la valeur {0} @@ -150,6 +153,7 @@ NOT_POSITIVE_DEGREES_OF_FREEDOM = les de DEGREES_OF_FREEDOM = degr\u00e9s de libert\u00e9 ({0}) NOT_POSITIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} n''est pas positif : {1} NOT_POSITIVE_EXPONENT = exposant {0} invalide (doit \u00eatre positif) +EXPONENT = exposant ({0}) NOT_POSITIVE_LENGTH = la longueur doit \u00eatre positive ({0}) LENGTH = longueur ({0}) NOT_POSITIVE_MEAN = la moyenne doit \u00eatre positive ({0}) @@ -160,6 +164,7 @@ NOT_POSITIVE_PERMUTATION = la permutatio PERMUTATION_SIZE = taille de la permutation NOT_POSITIVE_POISSON_MEAN = la moyenne de Poisson doit \u00eatre positive ({0}) NOT_POSITIVE_POPULATION_SIZE = la taille de la population doit \u00eatre positive ({0}) +POPULATION_SIZE = taille de la population ({0}) NOT_POSITIVE_ROW_DIMENSION = nombre de lignes invalide : {0} (doit \u00eatre positif) NOT_POSITIVE_SAMPLE_SIZE = la taille de l''\u00e9chantillon doit \u00eatre positive ({0}) NOT_POSITIVE_SCALE = l''\u00e9chelle doit \u00eatre positive ({0}) Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Oct 1 12:46:16 2010 @@ -52,6 +52,10 @@ The <action> type attribute can be add,u If the output is not quite correct, check for invisible trailing spaces! --> <release version="3.0" date="TBD" description="TBD"> + <action dev="erans" type="update" issue="MATH-310"> + Made "sample" methods part of the "IntegerDistribution" and + "ContinuousDistribution" interfaces. + </action> <action dev="erans" type="fix" issue="MATH-349"> All distribution classes (in package "distribution") are immutable. </action> Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java Fri Oct 1 12:46:16 2010 @@ -18,6 +18,9 @@ package org.apache.commons.math.distribution; import org.apache.commons.math.TestUtils; +import org.apache.commons.math.exception.NotPositiveException; +import org.apache.commons.math.exception.NotStrictlyPositiveException; +import org.apache.commons.math.exception.NumberIsTooLargeException; /** * Test cases for HyperGeometriclDistribution. @@ -127,16 +130,45 @@ public class HypergeometricDistributionT verifyInverseCumulativeProbabilities(); } - public void testPopulationSize() { - HypergeometricDistribution dist = new HypergeometricDistributionImpl(5,3,5); + public void testPreconditions() { + HypergeometricDistribution dist; try { - dist.setPopulationSize(-1); - fail("negative population size. IllegalArgumentException expected"); - } catch(IllegalArgumentException ex) { + dist = new HypergeometricDistributionImpl(0, 3, 5); + fail("negative population size. NotStrictlyPositiveException expected"); + } catch(NotStrictlyPositiveException ex) { + // Expected. } + try { + dist = new HypergeometricDistributionImpl(5, -1, 5); + fail("negative number of successes. NotPositiveException expected"); + } catch(NotPositiveException ex) { + // Expected. + } + try { + dist = new HypergeometricDistributionImpl(5, 3, -1); + fail("negative sample size. NotPositiveException expected"); + } catch(NotPositiveException ex) { + // Expected. + } + try { + dist = new HypergeometricDistributionImpl(5, 6, 5); + fail("numberOfSuccesses > populationSize. NumberIsTooLargeException expected"); + } catch(NumberIsTooLargeException ex) { + // Expected. + } + try { + dist = new HypergeometricDistributionImpl(5, 3, 6); + fail("sampleSize > populationSize. NumberIsTooLargeException expected"); + } catch(NumberIsTooLargeException ex) { + // Expected. + } + } - dist.setPopulationSize(10); - assertEquals(10, dist.getPopulationSize()); + public void testAccessors() { + HypergeometricDistribution dist = new HypergeometricDistributionImpl(5, 3, 4); + assertEquals(5, dist.getPopulationSize()); + assertEquals(3, dist.getNumberOfSuccesses()); + assertEquals(4, dist.getSampleSize()); } public void testLargeValues() { Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java?rev=1003512&r1=1003511&r2=1003512&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java Fri Oct 1 12:46:16 2010 @@ -17,6 +17,8 @@ package org.apache.commons.math.distribution; +import org.apache.commons.math.exception.NotStrictlyPositiveException; + /** * Test cases for {...@link ZipfDistribution}. * Extends IntegerDistributionAbstractTest. See class javadoc for @@ -29,6 +31,22 @@ public class ZipfDistributionTest extend super(name); } + public void testPreconditions() { + ZipfDistribution dist; + try { + dist = new ZipfDistributionImpl(0, 1); + fail("NotStrictlyPositiveException expected"); + } catch (NotStrictlyPositiveException e) { + // Expected. + } + try { + dist = new ZipfDistributionImpl(1, 0); + fail("NotStrictlyPositiveException expected"); + } catch (NotStrictlyPositiveException e) { + // Expected. + } + } + //-------------- Implementations for abstract methods ----------------------- /** Creates the default discrete distribution instance to use in tests. */
