Debian Codegraph @

src/test/dr/distibutions/InvGammaTest.java

raw symbols SCIP
1 package test.dr.distibutions;
2
3 import junit.framework.TestCase;
4 import dr.math.distributions.InverseGammaDistribution;
5
6 /**
7 * Simple test for inverse gamma distribution.
8 *
9 * @author Joseph Heled
10 * Date: 24/04/2009
11 */
12 public class InvGammaTest extends TestCase {
13 interface TestData {
14 double getShape();
15
16 double getScale();
17
18 double[] getPDF();
19
20 double[] getCDF();
21 }
22
23 // test data generated from this python code:
24 // import scipy.stats
25 //
26 //print """TestData[] tests = {"""
27 //for shape,scale in ((3,2), (3,1)) :
28 // d = scipy.stats.invgamma(shape, scale=scale)
29 // print """
30 // new TestData() {
31 // public double getShape() {
32 // return %d;
33 // }
34 //
35 // public double getScale() {
36 // return %d;
37 // }""" % (shape, scale)
38 // x = (0.5, 1, 2)
39 // print """
40 // public double[] getPDF() {
41 // return new double[]{%s};
42 // }""" % " , ".join(["%g,%.14lf" % (z,d.pdf(z)) for z in x])
43 //
44 // print """
45 // public double[] getCDF() {
46 // return new double[]{%s};
47 // }
48 // } ,""" % " , ".join(["%g,%.14lf" % (z,d.cdf(z)) for z in x])
49 //
50
51 TestData[] tests = {
52
53 new TestData() {
54 public double getShape() {
55 return 3;
56 }
57
58 public double getScale() {
59 return 2;
60 }
61
62 public double[] getPDF() {
63 return new double[]{0.5, 1.17220088887899, 1, 0.54134113294645, 2, 0.09196986029286};
64 }
65
66 public double[] getCDF() {
67 return new double[]{0.5, 0.23810330555354, 1, 0.67667641618306, 2, 0.91969860292861};
68 }
69 },
70
71 new TestData() {
72 public double getShape() {
73 return 3;
74 }
75
76 public double getScale() {
77 return 1;
78 }
79
80 public double[] getPDF() {
81 return new double[]{0.5, 1.08268226589290, 1, 0.18393972058572, 2, 0.01895408311602};
82 }
83
84 public double[] getCDF() {
85 return new double[]{0.5, 0.67667641618306, 1, 0.91969860292861, 2, 0.98561232203303};
86 }
87 },
88 };
89
90
91 public void testInvGamma() {
92 for( TestData td : tests ) {
93 InverseGammaDistribution d = new InverseGammaDistribution(td.getShape(), td.getScale());
94
95 {
96 double[] p = td.getPDF();
97 for(int k = 0; k < p.length; k += 2) {
98 assertEquals(d.pdf(p[k]), p[k + 1], 1e-10);
99
100 assertEquals(d.logPdf(p[k]), Math.log(p[k + 1]), 1e-10);
101 }
102 }
103
104 double[] cdf = td.getCDF();
105 for(int k = 0; k < cdf.length; k += 2) {
106 assertEquals(d.cdf(cdf[k]), cdf[k + 1], 1e-10);
107 }
108
109 // int count[] = new int[cdf.length];
110 // final int N = 100000;
111 // for(int k = 0; k < N; ++k) {
112 // double x = d.nextInverseGamma();
113 // for(int l = 0; l < cdf.length; l += 2) {
114 // if( x < cdf[l] ) {
115 // count[l / 2] += 1;
116 // }
117 // }
118 // }
119 // for(int l = 0; l < cdf.length; l += 2) {
120 // assertEquals(count[l / 2] / (double) N, cdf[l + 1], 5e-3);
121 // }
122 }
123 }
124 }