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:
parent
df23d31d6f
commit
229c782087
|
@ -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
|
||||||
|
* ContinuousDistributionAbstractTest for details.
|
||||||
*
|
*
|
||||||
* "True" results are taken from R - the same as in Mathematica
|
* @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);
|
||||||
* Constructor for NormalDistributionTest.
|
}
|
||||||
* @param arg0
|
|
||||||
*/
|
public static void main(String[] args) {
|
||||||
public NormalDistributionTest(String arg0) {
|
junit.swingui.TestRunner.run(NormalDistributionTest.class);
|
||||||
super(arg0);
|
}
|
||||||
}
|
|
||||||
|
//-------------- Implementations for abstract methods -----------------------
|
||||||
public static void main(String[] args) {
|
|
||||||
junit.swingui.TestRunner.run(NormalDistributionTest.class);
|
/** Creates the default continuous distribution instance to use in tests. */
|
||||||
}
|
public ContinuousDistribution makeDistribution() {
|
||||||
|
return DistributionFactory.newInstance().createNormalDistribution(2.1, 1.4);
|
||||||
protected void setUp() throws Exception {
|
}
|
||||||
super.setUp();
|
|
||||||
z = DistributionFactory.newInstance().createNormalDistribution(M, SD);
|
/** Creates the default cumulative probability distribution test input values */
|
||||||
}
|
public double[] makeCumulativeTestPoints() {
|
||||||
|
// quantiles computed using R
|
||||||
protected void tearDown() throws Exception {
|
return new double[] {-2.226325d, -1.156887d, -0.6439496d, -0.2027951d, 0.3058278d,
|
||||||
super.tearDown();
|
6.426325d, 5.356887d, 4.84395d, 4.402795d, 3.894172d};
|
||||||
z = null;
|
}
|
||||||
}
|
|
||||||
|
/** Creates the default cumulative probability density test expected values */
|
||||||
public void testCumulativeProbabilitydoubleM_MINUS_2SD() throws MathException {
|
public double[] makeCumulativeTestValues() {
|
||||||
testProbability(M - 2*SD, 0.02275013);
|
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 {
|
|
||||||
testProbability(M - SD, 0.1586553);
|
// --------------------- Override tolerance --------------
|
||||||
}
|
protected void setup() throws Exception {
|
||||||
|
super.setUp();
|
||||||
public void testCumulativeProbabilitydoubleM() throws MathException {
|
setTolerance(1E-6);
|
||||||
testProbability(M, 0.5);
|
}
|
||||||
}
|
|
||||||
|
//---------------------------- Additional test cases -------------------------
|
||||||
public void testCumulativeProbabilitydoubleM_PLUS_SD() throws MathException {
|
|
||||||
testProbability(M + SD, 0.8413447);
|
private void verifyQuantiles() throws Exception {
|
||||||
}
|
NormalDistribution distribution = (NormalDistribution) getDistribution();
|
||||||
|
double mu = distribution.getMean();
|
||||||
public void testCumulativeProbabilitydoubleM_PLUS_2SD() throws MathException {
|
double sigma = distribution.getStandardDeviation();
|
||||||
testProbability(M + 2*SD, 0.9772499);
|
setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
|
||||||
}
|
mu, mu + sigma, mu +2 * sigma, mu +3 * sigma, mu + 4 * sigma,
|
||||||
|
mu + 5 * sigma});
|
||||||
public void testCumulativeProbabilitydoubleM_PLUS_3SD() throws MathException {
|
// Quantiles computed using R (same as Mathematica)
|
||||||
testProbability(M + 3*SD, 0.9986501);
|
setCumulativeTestValues(new double[] {0.02275013, 0.1586553, 0.5, 0.8413447,
|
||||||
}
|
0.9772499, 0.9986501, 0.9999683, 0.9999997});
|
||||||
|
verifyCumulativeProbabilities();
|
||||||
public void testCumulativeProbabilitydoubleM_PLUS_4SD() throws MathException {
|
}
|
||||||
testProbability(M + 4*SD, 0.9999683);
|
|
||||||
}
|
public void testQuantiles() throws Exception {
|
||||||
|
verifyQuantiles();
|
||||||
public void testCumulativeProbabilitydoubleM_PLUS_5SD() throws MathException {
|
setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 1));
|
||||||
testProbability(M + 5*SD, 0.9999997);
|
verifyQuantiles();
|
||||||
}
|
setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 0.1));
|
||||||
|
verifyQuantiles();
|
||||||
public void testInverseCumulativeProbability001() throws MathException {
|
}
|
||||||
testValue(-2.226325, .001);
|
|
||||||
}
|
public void testGetMean() {
|
||||||
|
NormalDistribution distribution = (NormalDistribution) getDistribution();
|
||||||
public void testInverseCumulativeProbability010() throws MathException{
|
assertEquals(2.1, distribution.getMean(), 0);
|
||||||
testValue(-1.156887, .010);
|
}
|
||||||
}
|
|
||||||
|
public void testSetMean() throws Exception {
|
||||||
public void testInverseCumulativeProbability025() throws MathException{
|
double mu = Math.random();
|
||||||
testValue(-0.6439496, .025);
|
NormalDistribution distribution = (NormalDistribution) getDistribution();
|
||||||
}
|
distribution.setMean(mu);
|
||||||
|
verifyQuantiles();
|
||||||
public void testInverseCumulativeProbability050() throws MathException{
|
}
|
||||||
testValue(-0.2027951, .050);
|
|
||||||
}
|
public void testGetStandardDeviation() {
|
||||||
|
NormalDistribution distribution = (NormalDistribution) getDistribution();
|
||||||
public void testInverseCumulativeProbability100() throws MathException{
|
assertEquals(1.4, distribution.getStandardDeviation(), 0);
|
||||||
testValue(0.3058278, .100);
|
}
|
||||||
}
|
|
||||||
|
public void testSetStandardDeviation() throws Exception {
|
||||||
public void testInverseCumulativeProbability900() throws MathException{
|
double sigma = 0.1d + Math.random();
|
||||||
testValue(3.894172, .900);
|
NormalDistribution distribution = (NormalDistribution) getDistribution();
|
||||||
}
|
distribution.setStandardDeviation(sigma);
|
||||||
|
assertEquals(sigma, distribution.getStandardDeviation(), 0);
|
||||||
public void testInverseCumulativeProbability950() throws MathException{
|
verifyQuantiles();
|
||||||
testValue(4.402795, .950);
|
try {
|
||||||
}
|
distribution.setStandardDeviation(0);
|
||||||
|
fail("Expecting IllegalArgumentException for sd = 0");
|
||||||
public void testInverseCumulativeProbability975() throws MathException{
|
} catch (IllegalArgumentException ex) {
|
||||||
testValue(4.84395, .975);
|
// Expected
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue