Debian Codegraph @

src/test/dr/distibutions/InverseGaussianDistributionTest.java

raw symbols SCIP
1 package test.dr.distibutions;
2
3 import dr.math.distributions.InverseGaussianDistribution;
4 import dr.math.interfaces.OneVariableFunction;
5 import dr.math.iterations.BisectionZeroFinder;
6 import junit.framework.TestCase;
7
8 /**
9 * @author Wai Lok Sibon Li
10 */
11 public class InverseGaussianDistributionTest extends TestCase {
12
13 InverseGaussianDistribution invGaussian;
14
15 public void setUp() {
16
17 invGaussian = new InverseGaussianDistribution(1.0, 2.0);
18 }
19
20 public void testPdf() {
21
22 System.out.println("Testing 10000 random pdf calls");
23
24 for (int i = 0; i < 10000; i++) {
25 double M = Math.random() * 10.0 + 0.1;
26 double S = Math.random() * 5.0 + 0.01;
27
28 double x = Math.random() * 10;
29
30 invGaussian.setMean(M);
31 invGaussian.setShape(S);
32
33 //double pdf = 1.0 / (x * S * Math.sqrt(2 * Math.PI)) * Math.exp(-Math.pow(Math.log(x) - M, 2) / (2 * S * S));
34 double pdf = Math.sqrt(S/(2.0 * Math.PI * x * x * x)) * Math.exp((-1.0 * S * Math.pow((x - M), 2))/(2 * M * M * x));
35 assertEquals(pdf, invGaussian.pdf(x), 1e-10);
36 }
37
38 /* Test with an example using R */
39 invGaussian.setMean(2.835202292812448);
40 invGaussian.setShape(3.539139491639669);
41 assertEquals(0.1839934, invGaussian.pdf(2.540111), 1e-6);
42 }
43
44 public void testMean() {
45
46 for (int i = 0; i < 1000; i++) {
47 double M = Math.random() * 10.0 + 0.1;
48 double S = Math.random() * 5.0 + 0.01;
49
50 invGaussian.setMean(M);
51 invGaussian.setShape(S);
52
53 assertEquals(M, invGaussian.mean(), 1e-10);
54 }
55 }
56
57 public void testVariance() {
58
59 for (int i = 0; i < 1000; i++) {
60 double M = Math.random() * 10.0 + 0.1;
61 double S = Math.random() * 5.0 + 0.01;
62
63 invGaussian.setMean(M);
64 invGaussian.setShape(S);
65 double variance = (M * M * M) / S;
66 assertEquals(variance, invGaussian.variance(), 1e-8);
67 }
68 }
69
70 public void testShape() {
71
72 System.out.println("Testing 10000 random quantile(0.5) calls");
73
74 for (int i = 0; i < 10000; i++) {
75 double M = Math.random() * 10.0 + 0.1;
76 double S = Math.random() * 5.0 + 0.01;
77
78 invGaussian.setMean(M);
79 invGaussian.setShape(S);
80
81 assertEquals(S, invGaussian.getShape(), 1e-10);
82 }
83 }
84
85 public void testCDFAndQuantile() {
86 invGaussian.setMean(1.0);
87 invGaussian.setShape(351.7561121947152);
88 double q = invGaussian.quantile(0.20811009197062338);
89 assertEquals(0.20811009197062338, invGaussian.cdf(q), 3.0e-3);
90
91 for (int i = 0; i < 10000; i++) {
92 double M = 1.0;
93 double S = Math.random() * 1000.0 + 0.01;
94 invGaussian.setMean(M);
95 invGaussian.setShape(S);
96
97 double p = Math.random()*0.98 + 0.01;
98 double quantile = invGaussian.quantile(p);
99 //System.out.println(quantile + "\t" + p + "\t" + M + "\t" + S);
100
101 double cdf = invGaussian.cdf(quantile);
102 if(((int)S)==351) {
103 assertEquals(p, cdf, 1.0e-2);
104 }
105 else {
106 assertEquals(p, cdf, 1.0e-3);
107 }
108 }
109
110 /* Test with examples using R */
111 invGaussian.setMean(5);
112 invGaussian.setShape(0.5);
113 assertEquals(0.75, invGaussian.cdf(3.022232), 1e-5);
114
115 invGaussian.setMean(1.0);
116 invGaussian.setShape(17.418709855826197);
117 double q2 =invGaussian.quantile(0.27959422055126726);
118 double p_hat = invGaussian.cdf(q2);
119 assertEquals(0.27959422055126726, p_hat, 1.0e-3);
120
121 invGaussian.setMean(1.0);
122 invGaussian.setShape(0.4078303443934461);
123 assertEquals(0.05514379243099207, invGaussian.cdf(invGaussian.quantile(0.05514379243099207)), 1.0e-3);
124 }
125
126 public void testCDFAndQuantile2() {
127 final InverseGaussianDistribution f = new InverseGaussianDistribution(1, 1);
128 for (double i = 0.01; i < 0.95; i += 0.01) {
129 final double y = i;
130
131 BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new OneVariableFunction() {
132 public double value(double x) {
133 return f.cdf(x) - y;
134 }
135 }, 0.01, 100);
136 zeroFinder.setMaximumIterations(100);
137 zeroFinder.evaluate();
138 assertEquals(f.quantile(i), zeroFinder.getResult(), 1e-3);
139 }
140 }
141
142 public void testCDFAndQuantile3() {
143 double[] shapes = {0.010051836, 0.011108997, 0.01227734, 0.013568559, 0.014995577,
144 0.016572675, 0.018315639, 0.020241911, 0.022370772, 0.024723526, 0.027323722,
145 0.030197383, 0.03337327, 0.036883167, 0.040762204, 0.045049202, 0.049787068,
146 0.05502322, 0.060810063, 0.067205513, 0.074273578, 0.082084999, 0.090717953,
147 0.100258844, 0.110803158, 0.122456428, 0.135335283, 0.149568619, 0.165298888,
148 0.182683524, 0.201896518, 0.22313016, 0.246596964, 0.272531793, 0.301194212,
149 0.332871084, 0.367879441, 0.40656966, 0.449328964, 0.496585304, 0.548811636,
150 0.60653066, 0.670320046, 0.740818221, 0.818730753, 0.904837418, 1, 1.105170918,
151 1.221402758, 1.349858808, 1.491824698, 1.648721271, 1.8221188, 2.013752707,
152 2.225540928, 2.459603111, 2.718281828, 3.004166024, 3.320116923, 3.669296668,
153 4.055199967, 4.48168907, 4.953032424, 5.473947392, 6.049647464, 6.685894442,
154 7.389056099, 8.166169913, 9.025013499, 9.974182455, 11.02317638, 12.18249396,
155 13.46373804, 14.87973172, 16.44464677, 18.17414537, 20.08553692, 22.19795128,
156 24.5325302, 27.11263892, 29.96410005, 33.11545196, 36.59823444, 40.44730436,
157 44.70118449, 49.40244911, 54.59815003, 60.3402876, 66.68633104, 73.6997937,
158 81.45086866, 90.0171313, 99.48431564, 109.9471725, 121.5104175, 134.2897797,
159 148.4131591, 164.0219073, 181.2722419, 200.33681, 221.4064162, 244.6919323,
160 270.4264074, 298.867401, 330.2995599, 365.0374679, 403.4287935, 445.8577701,
161 492.7490411, 544.5719101, 601.8450379, 665.141633, 735.0951892, 812.4058252,
162 897.8472917, 992.2747156};
163 for (double shape : shapes) {
164 invGaussian.setShape(shape);
165 for (double p = 0.01; p < 0.99; p += 0.01) {
166 double q = invGaussian.quantile(p);
167 double p_hat = invGaussian.cdf(q);
168 assertEquals(p, p_hat, 1.0e-3);
169 }
170 }
171 }
172 }