You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2010/09/30 14:55:03 UTC
svn commit: r1003048 [2/2] - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/distribution/
main/java/org/apache/commons/math/exception/
main/java/org/apache/commons/math/exception/util/
main/java/org/apache/commons/math/stat/infere...
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java Thu Sep 30 12:55:02 2010
@@ -17,6 +17,7 @@
package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.exception.util.Localizable;
/**
* Exception to be thrown when some argument is out of range.
@@ -44,10 +45,28 @@ public class OutOfRangeException extends
public OutOfRangeException(Number wrong,
Number lo,
Number hi) {
- super(LocalizedFormats.OUT_OF_RANGE_SIMPLE, wrong, lo, hi);
+ this(null, wrong, lo, hi);
+ }
+
+ /**
+ * Construct an exception from the mismatched dimensions with a
+ * specific context information.
+ *
+ * @param specific Context information.
+ * @param wrong Requested value.
+ * @param lo Lower bound.
+ * @param hi Higher bound.
+ */
+ public OutOfRangeException(Localizable specific,
+ Number wrong,
+ Number lo,
+ Number hi) {
+ super(specific, LocalizedFormats.OUT_OF_RANGE_SIMPLE,
+ wrong, lo, hi);
this.lo = lo;
this.hi = hi;
}
+
/**
* @return the lower bound.
*/
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=1003048&r1=1003047&r2=1003048&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 Thu Sep 30 12:55:02 2010
@@ -174,6 +174,7 @@ public enum LocalizedFormats implements
BETA("beta"), /* keep */
NOT_POSITIVE_COLUMNDIMENSION("invalid column dimension: {0} (must be positive)"),
NOT_POSITIVE_DEFINITE_MATRIX("not positive definite matrix"),
+ DEGREES_OF_FREEDOM("degrees of freedom ({0})"), /* keep */
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)"),
@@ -190,7 +191,9 @@ public enum LocalizedFormats implements
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})"),
+ SCALE("scale ({0})"), /* keep */
NOT_POSITIVE_SHAPE("shape must be positive ({0})"),
+ SHAPE("shape ({0})"), /* keep */
NOT_POSITIVE_STANDARD_DEVIATION("standard deviation must be positive ({0})"),
STANDARD_DEVIATION("standard deviation ({0})"), /* keep */
NOT_POSITIVE_UPPER_BOUND("upper bound must be positive ({0})"),
@@ -238,6 +241,7 @@ public enum LocalizedFormats implements
OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY("observed counts are both zero for entry {0}"),
OUT_OF_BOUNDS_QUANTILE_VALUE("out of bounds quantile value: {0}, must be in (0, 100]"),
OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"),
+ SIGNIFICANCE_LEVEL("significance level ({0})"), /* keep */
OUT_OF_ORDER_ABSCISSA_ARRAY("the abscissae array must be sorted in a strictly increasing order, but the {0}-th element is {1} whereas {2}-th is {3}"),
OUT_OF_RANGE_ROOT_OF_UNITY_INDEX("out of range root of unity index {0} (must be in [{1};{2}])"),
OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TTestImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TTestImpl.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TTestImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TTestImpl.java Thu Sep 30 12:55:02 2010
@@ -17,7 +17,9 @@
package org.apache.commons.math.stat.inference;
import org.apache.commons.math.MathException;
-import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.TDistributionImpl;
import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -34,28 +36,6 @@ import org.apache.commons.math.util.Fast
* @version $Revision$ $Date$
*/
public class TTestImpl implements TTest {
-
- /** Distribution used to compute inference statistics. */
- private TDistribution distribution;
-
- /**
- * Default constructor.
- */
- public TTestImpl() {
- this(new TDistributionImpl(1.0));
- }
-
- /**
- * Create a test instance using the given distribution for computing
- * inference statistics.
- * @param t distribution used to compute inference statistics.
- * @since 1.2
- */
- public TTestImpl(TDistribution t) {
- super();
- setDistribution(t);
- }
-
/**
* Computes a paired, 2-sample t-statistic based on the data in the input
* arrays. The t-statistic returned is equivalent to what would be returned by
@@ -82,8 +62,8 @@ public class TTestImpl implements TTest
checkSampleData(sample2);
double meanDifference = StatUtils.meanDifference(sample1, sample2);
return t(meanDifference, 0,
- StatUtils.varianceDifference(sample1, sample2, meanDifference),
- sample1.length);
+ StatUtils.varianceDifference(sample1, sample2, meanDifference),
+ sample1.length);
}
/**
@@ -873,8 +853,9 @@ public class TTestImpl implements TTest
* @throws MathException if an error occurs performing the test
*/
public boolean tTest(StatisticalSummary sampleStats1,
- StatisticalSummary sampleStats2, double alpha)
- throws IllegalArgumentException, MathException {
+ StatisticalSummary sampleStats2,
+ double alpha)
+ throws MathException {
checkSignificanceLevel(alpha);
return tTest(sampleStats1, sampleStats2) < alpha;
}
@@ -956,9 +937,9 @@ public class TTestImpl implements TTest
* @throws MathException if an error occurs computing the p-value
*/
protected double tTest(double m, double mu, double v, double n)
- throws MathException {
+ throws MathException {
double t = FastMath.abs(t(m, mu, v, n));
- distribution.setDegreesOfFreedom(n - 1);
+ TDistribution distribution = new TDistributionImpl(n - 1);
return 2.0 * distribution.cumulativeProbability(-t);
}
@@ -977,13 +958,14 @@ public class TTestImpl implements TTest
* @return p-value
* @throws MathException if an error occurs computing the p-value
*/
- protected double tTest(double m1, double m2, double v1, double v2,
- double n1, double n2)
- throws MathException {
+ protected double tTest(double m1, double m2,
+ double v1, double v2,
+ double n1, double n2)
+ throws MathException {
double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2));
double degreesOfFreedom = 0;
degreesOfFreedom = df(v1, v2, n1, n2);
- distribution.setDegreesOfFreedom(degreesOfFreedom);
+ TDistribution distribution = new TDistributionImpl(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t);
}
@@ -1002,61 +984,60 @@ public class TTestImpl implements TTest
* @return p-value
* @throws MathException if an error occurs computing the p-value
*/
- protected double homoscedasticTTest(double m1, double m2, double v1,
- double v2, double n1, double n2)
- throws MathException {
+ protected double homoscedasticTTest(double m1, double m2,
+ double v1, double v2,
+ double n1, double n2)
+ throws MathException {
double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
double degreesOfFreedom = n1 + n2 - 2;
- distribution.setDegreesOfFreedom(degreesOfFreedom);
+ TDistribution distribution = new TDistributionImpl(degreesOfFreedom);
return 2.0 * distribution.cumulativeProbability(-t);
}
/**
- * Modify the distribution used to compute inference statistics.
- * @param value the new distribution
- * @since 1.2
- */
- public void setDistribution(TDistribution value) {
- distribution = value;
- }
-
- /** Check significance level.
+ * Check significance level.
+ *
* @param alpha significance level
- * @exception IllegalArgumentException if significance level is out of bounds
+ * @throws OutOfRangeException if the significance level is out of bounds.
*/
- private void checkSignificanceLevel(final double alpha)
- throws IllegalArgumentException {
- if ((alpha <= 0) || (alpha > 0.5)) {
- throw MathRuntimeException.createIllegalArgumentException(
- LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
- alpha, 0.0, 0.5);
+ private void checkSignificanceLevel(final double alpha) {
+ if (alpha <= 0 || alpha > 0.5) {
+ throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
+ alpha, 0.0, 0.5);
}
}
- /** Check sample data.
- * @param data sample data
- * @exception IllegalArgumentException if there is not enough sample data
- */
- private void checkSampleData(final double[] data)
- throws IllegalArgumentException {
- if ((data == null) || (data.length < 2)) {
- throw MathRuntimeException.createIllegalArgumentException(
- LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
- (data == null) ? 0 : data.length);
+ /**
+ * Check sample data.
+ *
+ * @param data Sample data.
+ * @throws NullArgumentException if {@code data} is {@code null}.
+ * @throws NumberIsTooSmallException if there is not enough sample data.
+ */
+ private void checkSampleData(final double[] data) {
+ if (data == null) {
+ throw new NullArgumentException();
+ }
+ if (data.length < 2) {
+ throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
+ data.length, 2, true);
}
}
- /** Check sample data.
- * @param stat statistical summary
- * @exception IllegalArgumentException if there is not enough sample data
- */
- private void checkSampleData(final StatisticalSummary stat)
- throws IllegalArgumentException {
- if ((stat == null) || (stat.getN() < 2)) {
- throw MathRuntimeException.createIllegalArgumentException(
- LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
- (stat == null) ? 0 : stat.getN());
+ /**
+ * Check sample data.
+ *
+ * @param stat Statistical summary.
+ * @throws NullArgumentException if {@code data} is {@code null}.
+ * @throws NumberIsTooSmallException if there is not enough sample data.
+ */
+ private void checkSampleData(final StatisticalSummary stat) {
+ if (stat == null) {
+ throw new NullArgumentException();
+ }
+ if (stat.getN() < 2) {
+ throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC,
+ stat.getN(), 2, true);
}
}
-
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/SimpleRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/SimpleRegression.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/SimpleRegression.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/SimpleRegression.java Thu Sep 30 12:55:02 2010
@@ -19,7 +19,7 @@ package org.apache.commons.math.stat.reg
import java.io.Serializable;
import org.apache.commons.math.MathException;
-import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.TDistributionImpl;
import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -93,18 +93,19 @@ public class SimpleRegression implements
* Create an empty SimpleRegression instance
*/
public SimpleRegression() {
- this(new TDistributionImpl(1.0));
+ this(1);
}
/**
* Create an empty SimpleRegression using the given distribution object to
* compute inference statistics.
- * @param t the distribution used to compute inference statistics.
- * @since 1.2
+ *
+ * @param degrees Number of degrees of freedom of the distribution used
+ * to compute inference statistics.
+ * @since 2.2
*/
- public SimpleRegression(TDistribution t) {
- super();
- setDistribution(t);
+ public SimpleRegression(int degrees) {
+ distribution = new TDistributionImpl(degrees);
}
/**
@@ -138,7 +139,7 @@ public class SimpleRegression implements
n++;
if (n > 2) {
- distribution.setDegreesOfFreedom(n - 2);
+ distribution = new TDistributionImpl(n - 2);
}
}
@@ -170,7 +171,7 @@ public class SimpleRegression implements
n--;
if (n > 2) {
- distribution.setDegreesOfFreedom(n - 2);
+ distribution = new TDistributionImpl(n - 2);
}
}
}
@@ -549,9 +550,8 @@ public class SimpleRegression implements
public double getSlopeConfidenceInterval(double alpha)
throws MathException {
if (alpha >= 1 || alpha <= 0) {
- throw MathRuntimeException.createIllegalArgumentException(
- LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
- alpha, 0.0, 1.0);
+ throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
+ alpha, 0, 1);
}
return getSlopeStdErr() *
distribution.inverseCumulativeProbability(1d - alpha / 2d);
@@ -606,18 +606,4 @@ public class SimpleRegression implements
private double getRegressionSumSquares(double slope) {
return slope * slope * sumXX;
}
-
- /**
- * Modify the distribution used to compute inference statistics.
- * @param value the new distribution
- * @since 1.2
- */
- public void setDistribution(TDistribution value) {
- distribution = value;
-
- // modify degrees of freedom
- if (n > 2) {
- distribution.setDegreesOfFreedom(n - 2);
- }
- }
}
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=1003048&r1=1003047&r2=1003048&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 Thu Sep 30 12:55:02 2010
@@ -147,6 +147,7 @@ BETA = beta
NOT_POSITIVE_COLUMNDIMENSION = nombre de colonnes invalide : {0} (doit \u00eatre positif)
NOT_POSITIVE_DEFINITE_MATRIX = matrice non d\u00e9finie positive
NOT_POSITIVE_DEGREES_OF_FREEDOM = les degr\u00e9s de libert\u00e9 doivent \u00eatre positifs ({0})
+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)
NOT_POSITIVE_LENGTH = la longueur doit \u00eatre positive ({0})
@@ -162,7 +163,9 @@ NOT_POSITIVE_POPULATION_SIZE = la taille
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})
+SCALE = facteur d''\u00e9chelle ({0})
NOT_POSITIVE_SHAPE = le facteur de forme doit \u00eatre positif ({0})
+SHAPE = facteur de forme ({0})
NOT_POSITIVE_STANDARD_DEVIATION = l''\u00e9cart type doit \u00eatre positif ({0})
STANDARD_DEVIATION = \u00e9cart type
NOT_POSITIVE_UPPER_BOUND = la borne sup\u00e9rieure doit \u00eatre positive ({0})
@@ -210,6 +213,7 @@ OBSERVED_COUNTS_ALL_ZERO = aucune occurr
OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY = les occurrences observ\u00e9es sont toutes deux nulles pour l''entr\u00e9e {0}
OUT_OF_BOUNDS_QUANTILE_VALUE = valeur de quantile {0} hors bornes, doit \u00eatre dans l''intervalle ]0, 100]
OUT_OF_BOUND_SIGNIFICANCE_LEVEL = niveau de signification {0} hors domaine, doit \u00eatre entre {1} et {2}
+SIGNIFICANCE_LEVEL = niveau de signification ({0})
OUT_OF_ORDER_ABSCISSA_ARRAY = les abscisses doivent \u00eatre en ordre strictement croissant, mais l''\u00e9l\u00e9ment {0} vaut {1} alors que l''\u00e9l\u00e9ment {2} vaut {3}
OUT_OF_RANGE_ROOT_OF_UNITY_INDEX = l''indice de racine de l''unit\u00e9 {0} est hors du domaine autoris\u00e9 [{1};{2}]
OUT_OF_RANGE_SIMPLE = {0} hors du domaine [{1}, {2}]
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=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Sep 30 12:55:02 2010
@@ -53,7 +53,7 @@ The <action> type attribute can be add,u
-->
<release version="3.0" date="TBD" description="TBD">
<action dev="erans" type="fix" issue="MATH-349">
- "ChiSquaredDistributionImpl" and "PoissonDistributionImpl" are immutable.
+ All distribution classes (in package "distribution") are immutable.
</action>
<action dev="erans" type="update" issue="MATH-195">
Created "MatrixDimensionMismatch" to replace "InvalidMatrixException"
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/CauchyDistributionTest.java Thu Sep 30 12:55:02 2010
@@ -18,6 +18,7 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
/**
* Test cases for CauchyDistribution.
@@ -85,32 +86,27 @@ public class CauchyDistributionTest exte
public void testMedian() {
CauchyDistribution distribution = (CauchyDistribution) getDistribution();
- double expected = FastMath.random();
- distribution.setMedian(expected);
- assertEquals(expected, distribution.getMedian(), 0.0);
+ assertEquals(1.2, distribution.getMedian(), 0.0);
}
public void testScale() {
CauchyDistribution distribution = (CauchyDistribution) getDistribution();
- double expected = FastMath.random();
- distribution.setScale(expected);
- assertEquals(expected, distribution.getScale(), 0.0);
+ assertEquals(2.1, distribution.getScale(), 0.0);
}
- public void testSetScale() {
- CauchyDistribution distribution = (CauchyDistribution) getDistribution();
+ public void testPreconditions() {
+ CauchyDistribution dist;
try {
- distribution.setScale(0.0);
- fail("Can not have 0.0 scale.");
- } catch (IllegalArgumentException ex) {
- // success
+ dist = new CauchyDistributionImpl(0, 0);
+ fail("Cannot have zero scale");
+ } catch (NotStrictlyPositiveException ex) {
+ // Expected.
}
-
try {
- distribution.setScale(-1.0);
- fail("Can not have negative scale.");
- } catch (IllegalArgumentException ex) {
- // success
+ dist = new CauchyDistributionImpl(0, -1);
+ fail("Cannot have negative scale");
+ } catch (NotStrictlyPositiveException ex) {
+ // Expected.
}
}
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/FDistributionTest.java Thu Sep 30 12:55:02 2010
@@ -16,6 +16,7 @@
*/
package org.apache.commons.math.distribution;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
/**
* Test cases for FDistribution.
@@ -85,48 +86,43 @@ public class FDistributionTest extends C
}
public void testDfAccessors() {
- FDistribution distribution = (FDistribution) getDistribution();
- assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
- distribution.setNumeratorDegreesOfFreedom(4d);
- assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
- assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
- distribution.setDenominatorDegreesOfFreedom(4d);
- assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
+ FDistribution dist = (FDistribution) getDistribution();
+ assertEquals(5d, dist.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE);
+ assertEquals(6d, dist.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE);
+ }
+
+ public void testPreconditions() {
+ FDistribution dist;
try {
- distribution.setNumeratorDegreesOfFreedom(0d);
- fail("Expecting IllegalArgumentException for df = 0");
- } catch (IllegalArgumentException ex) {
- // expected
+ dist = new FDistributionImpl(0, 1);
+ fail("Expecting NotStrictlyPositiveException for df = 0");
+ } catch (NotStrictlyPositiveException ex) {
+ // Expected.
}
try {
- distribution.setDenominatorDegreesOfFreedom(0d);
- fail("Expecting IllegalArgumentException for df = 0");
- } catch (IllegalArgumentException ex) {
- // expected
+ dist = new FDistributionImpl(1, 0);
+ fail("Expecting NotStrictlyPositiveException for df = 0");
+ } catch (NotStrictlyPositiveException ex) {
+ // Expected.
}
}
public void testLargeDegreesOfFreedom() throws Exception {
- org.apache.commons.math.distribution.FDistributionImpl fd =
- new org.apache.commons.math.distribution.FDistributionImpl(
- 100000., 100000.);
+ FDistributionImpl fd = new FDistributionImpl(100000, 100000);
double p = fd.cumulativeProbability(.999);
double x = fd.inverseCumulativeProbability(p);
assertEquals(.999, x, 1.0e-5);
}
public void testSmallDegreesOfFreedom() throws Exception {
- org.apache.commons.math.distribution.FDistributionImpl fd =
- new org.apache.commons.math.distribution.FDistributionImpl(
- 1.0, 1.0);
+ FDistributionImpl fd = new FDistributionImpl(1, 1);
double p = fd.cumulativeProbability(0.975);
double x = fd.inverseCumulativeProbability(p);
assertEquals(0.975, x, 1.0e-5);
- fd.setDenominatorDegreesOfFreedom(2.0);
+ fd = new FDistributionImpl(1, 2);
p = fd.cumulativeProbability(0.975);
x = fd.inverseCumulativeProbability(p);
assertEquals(0.975, x, 1.0e-5);
}
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java Thu Sep 30 12:55:02 2010
@@ -16,6 +16,8 @@
*/
package org.apache.commons.math.distribution;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+
/**
* Test cases for TDistribution.
* Extends ContinuousDistributionAbstractTest. See class javadoc for
@@ -104,16 +106,16 @@ public class TDistributionTest extends C
}
public void testDfAccessors() {
- TDistribution distribution = (TDistribution) getDistribution();
- assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
- distribution.setDegreesOfFreedom(4d);
- assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
+ TDistribution dist = (TDistribution) getDistribution();
+ assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE);
+ }
+
+ public void testPreconditions() {
try {
- distribution.setDegreesOfFreedom(0d);
- fail("Expecting IllegalArgumentException for df = 0");
- } catch (IllegalArgumentException ex) {
+ TDistribution dist = new TDistributionImpl(0);
+ fail("Expecting NotStrictlyPositiveException for df = 0");
+ } catch (NotStrictlyPositiveException ex) {
// expected
}
}
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java?rev=1003048&r1=1003047&r2=1003048&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/WeibullDistributionTest.java Thu Sep 30 12:55:02 2010
@@ -18,6 +18,7 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
/**
* Test cases for WeibullDistribution.
@@ -75,50 +76,24 @@ public class WeibullDistributionTest ext
}
public void testAlpha() {
- WeibullDistribution distribution = (WeibullDistribution) getDistribution();
- double expected = FastMath.random();
- distribution.setShape(expected);
- assertEquals(expected, distribution.getShape(), 0.0);
- }
-
- public void testBeta() {
- WeibullDistribution distribution = (WeibullDistribution) getDistribution();
- double expected = FastMath.random();
- distribution.setScale(expected);
- assertEquals(expected, distribution.getScale(), 0.0);
- }
-
- public void testSetAlpha() {
- WeibullDistribution distribution = (WeibullDistribution) getDistribution();
- try {
- distribution.setShape(0.0);
- fail("Can not have 0.0 alpha.");
- } catch (IllegalArgumentException ex) {
- // success
- }
-
+ WeibullDistribution dist = new WeibullDistributionImpl(1, 2);
+ assertEquals(1, dist.getShape(), 0);
try {
- distribution.setShape(-1.0);
- fail("Can not have negative alpha.");
- } catch (IllegalArgumentException ex) {
- // success
+ dist = new WeibullDistributionImpl(0, 2);
+ fail("NotStrictlyPositiveException expected");
+ } catch (NotStrictlyPositiveException e) {
+ // Expected.
}
}
- public void testSetBeta() {
- WeibullDistribution distribution = (WeibullDistribution) getDistribution();
- try {
- distribution.setScale(0.0);
- fail("Can not have 0.0 beta.");
- } catch (IllegalArgumentException ex) {
- // success
- }
-
+ public void testBeta() {
+ WeibullDistribution dist = new WeibullDistributionImpl(1, 2);
+ assertEquals(2, dist.getScale(), 0);
try {
- distribution.setScale(-1.0);
- fail("Can not have negative beta.");
- } catch (IllegalArgumentException ex) {
- // success
+ dist = new WeibullDistributionImpl(1, 0);
+ fail("NotStrictlyPositiveException expected");
+ } catch (NotStrictlyPositiveException e) {
+ // Expected.
}
}
}