Debian Codegraph @

src/test/dr/distibutions/GammaDistributionTest.java

raw symbols SCIP
1 package test.dr.distibutions;
2
3 import dr.math.distributions.GammaDistribution;
4 import dr.math.functionEval.GammaFunction;
5 import junit.framework.TestCase;
6 import org.apache.commons.math.ConvergenceException;
7 import org.apache.commons.math.FunctionEvaluationException;
8 import org.apache.commons.math.analysis.UnivariateRealFunction;
9 import org.apache.commons.math.analysis.integration.RombergIntegrator;
10 import org.apache.commons.math.analysis.integration.UnivariateRealIntegrator;
11
12 import java.util.Random;
13
14
15 public class GammaDistributionTest extends TestCase{
16
17 /**
18 * This test stochastically draws gamma
19 * variates and compares the coded pdf
20 * with the actual pdf.
21 * The tolerance is required to be at most 1e-10.
22 */
23
24 static double mypdf(double value, double shape, double scale) {
25 return Math.exp((shape-1) * Math.log(value) - value/scale - GammaFunction.logGamma(shape) - shape * Math.log(scale) );
26 }
27
28 public void testPdf() throws FunctionEvaluationException {
29
30 final int numberOfTests = 100;
31 double totErr = 0;
32 double ptotErr = 0; int np = 0;
33 double qtotErr = 0;
34
35 Random random = new Random(37);
36
37 for(int i = 0; i < numberOfTests; i++){
38 final double mean = .01 + (3-0.01) * random.nextDouble();
39 final double var = .01 + (3-0.01) * random.nextDouble();
40
41 final double scale = var / mean;
42 final double shape = mean / scale;
43
44 final GammaDistribution gamma = new GammaDistribution(shape,scale);
45
46 final double value = gamma.nextGamma();
47
48 final double mypdf = mypdf(value, shape, scale);
49 final double pdf = gamma.pdf(value);
50 if ( Double.isInfinite(mypdf) && Double.isInfinite(pdf)) {
51 continue;
52 }
53
54 assertFalse(Double.isNaN(mypdf));
55 assertFalse(Double.isNaN(pdf));
56
57 totErr += mypdf != 0 ? Math.abs((pdf - mypdf)/mypdf) : pdf;
58
59 assertFalse("nan", Double.isNaN(totErr));
60 //assertEquals("" + shape + "," + scale + "," + value, mypdf,gamma.pdf(value),1e-10);
61
62 final double cdf = gamma.cdf(value);
63 UnivariateRealFunction f = new UnivariateRealFunction() {
64 public double value(double v) throws FunctionEvaluationException {
65 return mypdf(v, shape, scale);
66 }
67 };
68 final UnivariateRealIntegrator integrator = new RombergIntegrator();
69 integrator.setAbsoluteAccuracy(1e-14);
70 integrator.setMaximalIterationCount(16); // fail if it takes too much time
71
72 double x;
73 try {
74 x = integrator.integrate(f, 0, value);
75 ptotErr += cdf != 0.0 ? Math.abs(x-cdf)/cdf : x;
76 np += 1;
77 //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6);
78
79 //System.out.println(shape + "," + scale + " " + value);
80 } catch( ConvergenceException e ) {
81 // can't integrate , skip test
82 // System.out.println(shape + "," + scale + " skipped");
83 }
84
85 final double q = gamma.quantile(cdf);
86 qtotErr += q != 0 ? Math.abs(q-value)/q : value;
87 // assertEquals("" + shape + "," + scale + "," + value + " " + Math.abs(q-value)/value, q, value, 1e-6);
88 }
89 //System.out.println( !Double.isNaN(totErr) );
90 // System.out.println(totErr);
91 // bad test, but I can't find a good threshold that works for all individual cases
92 assertTrue("failed " + totErr/numberOfTests, totErr/numberOfTests < 1e-7);
93 assertTrue("failed " + ptotErr/np, np > 0 ? (ptotErr/np < 1e-5) : true);
94 assertTrue("failed " + qtotErr/numberOfTests , qtotErr/numberOfTests < 1e-7);
95 }
96 }