1 16 17 package org.apache.commons.math.distribution; 18 19 26 public class GammaDistributionTest extends ContinuousDistributionAbstractTest { 27 28 32 public GammaDistributionTest(String name) { 33 super(name); 34 } 35 36 38 39 public ContinuousDistribution makeDistribution() { 40 return DistributionFactory.newInstance().createGammaDistribution(4d, 2d); 41 } 42 43 44 public double[] makeCumulativeTestPoints() { 45 return new double[] {0.8571048, 1.646497, 2.179731, 2.732637, 47 3.489539, 26.12448, 20.09024, 17.53455, 48 15.50731, 13.36157}; 49 } 50 51 52 public double[] makeCumulativeTestValues() { 53 return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 54 0.990d, 0.975d, 0.950d, 0.900d}; 55 } 56 57 protected void setup() throws Exception { 59 super.setUp(); 60 setTolerance(1E-6); 61 } 62 63 public void testParameterAccessors() { 65 GammaDistribution distribution = (GammaDistribution) getDistribution(); 66 assertEquals(4d, distribution.getAlpha(), 0); 67 distribution.setAlpha(3d); 68 assertEquals(3d, distribution.getAlpha(), 0); 69 assertEquals(2d, distribution.getBeta(), 0); 70 distribution.setBeta(4d); 71 assertEquals(4d, distribution.getBeta(), 0); 72 try { 73 distribution.setAlpha(0d); 74 fail("Expecting IllegalArgumentException for alpha = 0"); 75 } catch (IllegalArgumentException ex) { 76 } 78 try { 79 distribution.setBeta(0d); 80 fail("Expecting IllegalArgumentException for beta = 0"); 81 } catch (IllegalArgumentException ex) { 82 } 84 } 85 86 public void testProbabilities() throws Exception { 87 testProbability(-1.000, 4.0, 2.0, .0000); 88 testProbability(15.501, 4.0, 2.0, .9499); 89 testProbability(0.504, 4.0, 1.0, .0018); 90 testProbability(10.011, 1.0, 2.0, .9933); 91 testProbability(5.000, 2.0, 2.0, .7127); 92 } 93 94 public void testValues() throws Exception { 95 testValue(15.501, 4.0, 2.0, .9499); 96 testValue(0.504, 4.0, 1.0, .0018); 97 testValue(10.011, 1.0, 2.0, .9933); 98 testValue(5.000, 2.0, 2.0, .7127); 99 } 100 101 private void testProbability(double x, double a, double b, double expected) throws Exception { 102 DistributionFactory factory = DistributionFactory.newInstance(); 103 GammaDistribution distribution = factory.createGammaDistribution( a, b ); 104 double actual = distribution.cumulativeProbability(x); 105 assertEquals("probability for " + x, expected, actual, 10e-4); 106 } 107 108 private void testValue(double expected, double a, double b, double p) throws Exception { 109 DistributionFactory factory = DistributionFactory.newInstance(); 110 GammaDistribution distribution = factory.createGammaDistribution( a, b ); 111 double actual = distribution.inverseCumulativeProbability(p); 112 assertEquals("critical value for " + p, expected, actual, 10e-4); 113 } 114 115 public void testInverseCumulativeProbabilityExtremes() throws Exception { 116 setInverseCumulativeTestPoints(new double[] {0, 1}); 117 setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); 118 verifyInverseCumulativeProbabilities(); 119 } 120 } 121 | Popular Tags |