1 16 17 package org.apache.commons.math.distribution; 18 19 26 public class NormalDistributionTest extends ContinuousDistributionAbstractTest { 27 28 32 public NormalDistributionTest(String arg0) { 33 super(arg0); 34 } 35 36 public static void main(String [] args) { 37 junit.swingui.TestRunner.run(NormalDistributionTest.class); 38 } 39 40 42 43 public ContinuousDistribution makeDistribution() { 44 return DistributionFactory.newInstance().createNormalDistribution(2.1, 1.4); 45 } 46 47 48 public double[] makeCumulativeTestPoints() { 49 return new double[] {-2.226325d, -1.156887d, -0.6439496d, -0.2027951d, 0.3058278d, 51 6.426325d, 5.356887d, 4.84395d, 4.402795d, 3.894172d}; 52 } 53 54 55 public double[] makeCumulativeTestValues() { 56 return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 57 0.990d, 0.975d, 0.950d, 0.900d}; 58 } 59 60 protected void setup() throws Exception { 62 super.setUp(); 63 setTolerance(1E-6); 64 } 65 66 68 private void verifyQuantiles() throws Exception { 69 NormalDistribution distribution = (NormalDistribution) getDistribution(); 70 double mu = distribution.getMean(); 71 double sigma = distribution.getStandardDeviation(); 72 setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma, 73 mu, mu + sigma, mu +2 * sigma, mu +3 * sigma, mu + 4 * sigma, 74 mu + 5 * sigma}); 75 setCumulativeTestValues(new double[] {0.02275013, 0.1586553, 0.5, 0.8413447, 77 0.9772499, 0.9986501, 0.9999683, 0.9999997}); 78 verifyCumulativeProbabilities(); 79 } 80 81 public void testQuantiles() throws Exception { 82 verifyQuantiles(); 83 setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 1)); 84 verifyQuantiles(); 85 setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 0.1)); 86 verifyQuantiles(); 87 } 88 89 public void testInverseCumulativeProbabilityExtremes() throws Exception { 90 setInverseCumulativeTestPoints(new double[] {0, 1}); 91 setInverseCumulativeTestValues( 92 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}); 93 verifyInverseCumulativeProbabilities(); 94 } 95 96 public void testGetMean() { 97 NormalDistribution distribution = (NormalDistribution) getDistribution(); 98 assertEquals(2.1, distribution.getMean(), 0); 99 } 100 101 public void testSetMean() throws Exception { 102 double mu = Math.random(); 103 NormalDistribution distribution = (NormalDistribution) getDistribution(); 104 distribution.setMean(mu); 105 verifyQuantiles(); 106 } 107 108 public void testGetStandardDeviation() { 109 NormalDistribution distribution = (NormalDistribution) getDistribution(); 110 assertEquals(1.4, distribution.getStandardDeviation(), 0); 111 } 112 113 public void testSetStandardDeviation() throws Exception { 114 double sigma = 0.1d + Math.random(); 115 NormalDistribution distribution = (NormalDistribution) getDistribution(); 116 distribution.setStandardDeviation(sigma); 117 assertEquals(sigma, distribution.getStandardDeviation(), 0); 118 verifyQuantiles(); 119 try { 120 distribution.setStandardDeviation(0); 121 fail("Expecting IllegalArgumentException for sd = 0"); 122 } catch (IllegalArgumentException ex) { 123 } 125 } 126 } 127 | Popular Tags |