Debian Codegraph @

src/test/dr/distibutions/NormalDistributionTest.java

raw symbols SCIP
1 package test.dr.distibutions;
2
3 import dr.math.distributions.NormalDistribution;
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 */
12 public class NormalDistributionTest extends TestCase {
13 NormalDistribution norm;
14
15 public void setUp() {
16
17 norm = new NormalDistribution(0.0, 1.0);
18 }
19
20
21 public void testPdf() {
22
23 System.out.println("Testing 10000 random pdf calls");
24
25 for (int i = 0; i < 10000; i++) {
26 double M = Math.random() * 10.0 - 5.0;
27 double S = Math.random() * 10;
28
29 double x = Math.random() * 10;
30
31 norm.setMean(M);
32 norm.setSD(S);
33
34 double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * S);
35 double b = -(x - M) * (x - M) / (2.0 * S * S);
36 double pdf = a * Math.exp(b);
37
38 assertEquals(pdf, norm.pdf(x), 1e-10);
39 }
40
41 /* Test with an example using R */
42 norm.setMean(2.835202292812448);
43 norm.setSD(3.539139491639669);
44 assertEquals(0.1123318, norm.pdf(2.540111), 1e-6);
45 }
46
47 public void testMean() {
48
49 for (int i = 0; i < 1000; i++) {
50 double M = Math.random() * 10.0 - 5.0;
51
52 norm.setMean(M);
53
54 assertEquals(M, norm.mean(), 1e-10);
55 }
56 }
57
58 public void testVariance() {
59
60 for (int i = 0; i < 1000; i++) {
61 double S = Math.random() * 10;
62 norm.setSD(S);
63
64 double variance = S * S;
65
66 assertEquals(variance, norm.variance(), 1e-10);
67 }
68 }
69
70
71 public void testMedian() {
72
73 System.out.println("Testing 10000 random quantile(0.5) calls");
74
75 for (int i = 0; i < 10000; i++) {
76 double M = Math.random() * 10.0 - 5.0;
77 double S = Math.random() * 10;
78
79 norm.setMean(M);
80 norm.setSD(S);
81
82 double median = M;
83
84 assertEquals(median, norm.quantile(0.5), 1e6);
85 }
86 }
87
88 public void testCDFAndQuantile() {
89
90 System.out.println("Testing 10000 random quantile/cdf pairs");
91
92 for (int i = 0; i < 10000; i++) {
93
94 double M = Math.random() * 10.0 - 5.0;
95 double S = Math.random() * 10;
96
97 norm.setMean(M);
98 norm.setSD(S);
99
100 double p = Math.random();
101 double quantile = norm.quantile(p);
102 double cdf = norm.cdf(quantile);
103
104 assertEquals(p, cdf, 1e-8);
105 }
106
107 }
108
109 public void testCDFAndQuantile2() {
110 for(int i=0; i<10000; i++) {
111 double x =Math.random();
112 double m = Math.random() * 10;
113 double s = Math.random() * 10;
114
115 double a = NormalDistribution.cdf(x, m, s, false);
116 double b =NormalDistribution.cdf(x, m, s);
117
118 assertEquals(a, b, 1.0e-8);
119 }
120 }
121
122
123 }