Modified to extend ContinuousDistributionAbstractTest, improved coverage.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141251 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-05-30 05:54:43 +00:00
parent df23d31d6f
commit 229c782087
1 changed files with 97 additions and 142 deletions

View File

@ -16,149 +16,104 @@
package org.apache.commons.math.distribution; package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.apache.commons.math.TestUtils;
import junit.framework.TestCase;
/** /**
* Tests for NormalDistribution implementation * Test cases for NormalDistribution.
* * Extends ContinuousDistributionAbstractTest. See class javadoc for
* "True" results are taken from R - the same as in Mathematica * ContinuousDistributionAbstractTest for details.
* *
* @version $Revision: 1.7 $ $Date: 2004/05/30 05:54:43 $
*/ */
public class NormalDistributionTest extends TestCase { public class NormalDistributionTest extends ContinuousDistributionAbstractTest {
private NormalDistribution z; /**
private static final double PRECISION = 10e-6; * Constructor for NormalDistributionTest.
private static final double M = 2.1; * @param arg0
private static final double SD = 1.4; */
public NormalDistributionTest(String arg0) {
super(arg0);
}
/** public static void main(String[] args) {
* Constructor for NormalDistributionTest. junit.swingui.TestRunner.run(NormalDistributionTest.class);
* @param arg0 }
*/
public NormalDistributionTest(String arg0) {
super(arg0);
}
public static void main(String[] args) { //-------------- Implementations for abstract methods -----------------------
junit.swingui.TestRunner.run(NormalDistributionTest.class);
}
protected void setUp() throws Exception { /** Creates the default continuous distribution instance to use in tests. */
super.setUp(); public ContinuousDistribution makeDistribution() {
z = DistributionFactory.newInstance().createNormalDistribution(M, SD); return DistributionFactory.newInstance().createNormalDistribution(2.1, 1.4);
} }
protected void tearDown() throws Exception { /** Creates the default cumulative probability distribution test input values */
super.tearDown(); public double[] makeCumulativeTestPoints() {
z = null; // quantiles computed using R
} return new double[] {-2.226325d, -1.156887d, -0.6439496d, -0.2027951d, 0.3058278d,
6.426325d, 5.356887d, 4.84395d, 4.402795d, 3.894172d};
}
public void testCumulativeProbabilitydoubleM_MINUS_2SD() throws MathException { /** Creates the default cumulative probability density test expected values */
testProbability(M - 2*SD, 0.02275013); public double[] makeCumulativeTestValues() {
} return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
0.990d, 0.975d, 0.950d, 0.900d};
}
public void testCumulativeProbabilitydoubleM_MINUS_SD() throws MathException { // --------------------- Override tolerance --------------
testProbability(M - SD, 0.1586553); protected void setup() throws Exception {
} super.setUp();
setTolerance(1E-6);
}
public void testCumulativeProbabilitydoubleM() throws MathException { //---------------------------- Additional test cases -------------------------
testProbability(M, 0.5);
}
public void testCumulativeProbabilitydoubleM_PLUS_SD() throws MathException { private void verifyQuantiles() throws Exception {
testProbability(M + SD, 0.8413447); NormalDistribution distribution = (NormalDistribution) getDistribution();
} double mu = distribution.getMean();
double sigma = distribution.getStandardDeviation();
setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
mu, mu + sigma, mu +2 * sigma, mu +3 * sigma, mu + 4 * sigma,
mu + 5 * sigma});
// Quantiles computed using R (same as Mathematica)
setCumulativeTestValues(new double[] {0.02275013, 0.1586553, 0.5, 0.8413447,
0.9772499, 0.9986501, 0.9999683, 0.9999997});
verifyCumulativeProbabilities();
}
public void testCumulativeProbabilitydoubleM_PLUS_2SD() throws MathException { public void testQuantiles() throws Exception {
testProbability(M + 2*SD, 0.9772499); verifyQuantiles();
} setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 1));
verifyQuantiles();
setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 0.1));
verifyQuantiles();
}
public void testCumulativeProbabilitydoubleM_PLUS_3SD() throws MathException { public void testGetMean() {
testProbability(M + 3*SD, 0.9986501); NormalDistribution distribution = (NormalDistribution) getDistribution();
} assertEquals(2.1, distribution.getMean(), 0);
}
public void testCumulativeProbabilitydoubleM_PLUS_4SD() throws MathException { public void testSetMean() throws Exception {
testProbability(M + 4*SD, 0.9999683); double mu = Math.random();
} NormalDistribution distribution = (NormalDistribution) getDistribution();
distribution.setMean(mu);
verifyQuantiles();
}
public void testCumulativeProbabilitydoubleM_PLUS_5SD() throws MathException { public void testGetStandardDeviation() {
testProbability(M + 5*SD, 0.9999997); NormalDistribution distribution = (NormalDistribution) getDistribution();
} assertEquals(1.4, distribution.getStandardDeviation(), 0);
}
public void testInverseCumulativeProbability001() throws MathException {
testValue(-2.226325, .001);
}
public void testInverseCumulativeProbability010() throws MathException{
testValue(-1.156887, .010);
}
public void testInverseCumulativeProbability025() throws MathException{
testValue(-0.6439496, .025);
}
public void testInverseCumulativeProbability050() throws MathException{
testValue(-0.2027951, .050);
}
public void testInverseCumulativeProbability100() throws MathException{
testValue(0.3058278, .100);
}
public void testInverseCumulativeProbability900() throws MathException{
testValue(3.894172, .900);
}
public void testInverseCumulativeProbability950() throws MathException{
testValue(4.402795, .950);
}
public void testInverseCumulativeProbability975() throws MathException{
testValue(4.84395, .975);
}
public void testInverseCumulativeProbability990() throws MathException{
testValue(5.356887, .990);
}
public void testInverseCumulativeProbability999() throws MathException{
testValue(6.426325, .999);
}
public void testGetMean() {
assertEquals(M, z.getMean(), 0);
}
public void testSetMean() throws MathException {
double mu = Math.random();
z.setMean(mu);
assertEquals(mu, z.getMean(), 0);
assertEquals(0.5d, z.cumulativeProbability(mu), PRECISION);
}
public void testGetStandardDeviation() {
assertEquals(SD, z.getStandardDeviation(), 0);
}
public void testSetStandardDeviation() throws MathException{
double sigma = 0.1d + Math.random();
z.setStandardDeviation(sigma);
assertEquals(sigma, z.getStandardDeviation(), 0);
assertEquals(0.84134475, z.cumulativeProbability(z.getMean() + z.getStandardDeviation()), PRECISION );
}
private void testProbability(double x, double expected) throws MathException {
double actual = Double.NaN;
actual = z.cumulativeProbability(x);
assertEquals(expected, actual, PRECISION);
}
private void testValue(double expected, double p) throws MathException {
double actual = z.inverseCumulativeProbability(p);
TestUtils.assertEquals(expected, actual, PRECISION);
}
public void testSetStandardDeviation() throws Exception {
double sigma = 0.1d + Math.random();
NormalDistribution distribution = (NormalDistribution) getDistribution();
distribution.setStandardDeviation(sigma);
assertEquals(sigma, distribution.getStandardDeviation(), 0);
verifyQuantiles();
try {
distribution.setStandardDeviation(0);
fail("Expecting IllegalArgumentException for sd = 0");
} catch (IllegalArgumentException ex) {
// Expected
}
}
} }