| 1 |
package test.dr.distibutions; |
| 2 |
|
| 3 |
import dr.math.distributions.LogNormalDistribution; |
| 4 |
import dr.math.interfaces.OneVariableFunction; |
| 5 |
import dr.math.iterations.BisectionZeroFinder; |
| 6 |
import junit.framework.TestCase; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
public class LogNormalDistributionTest extends TestCase { |
| 12 |
|
| 13 |
LogNormalDistribution logNormal; |
| 14 |
|
| 15 |
public void setUp() { |
| 16 |
|
| 17 |
logNormal = new LogNormalDistribution(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 - 5.0; |
| 26 |
double S = Math.random() * 10; |
| 27 |
|
| 28 |
double x = Math.log(Math.random() * 10); |
| 29 |
|
| 30 |
logNormal.setM(M); |
| 31 |
logNormal.setS(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 |
if (x <= 0) pdf = 0; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
assertEquals(pdf, logNormal.pdf(x), 1e-10); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public void testMean() { |
| 43 |
|
| 44 |
for (int i = 0; i < 1000; i++) { |
| 45 |
double M = Math.random() * 10.0 - 5.0; |
| 46 |
double S = Math.random() * 10; |
| 47 |
|
| 48 |
logNormal.setM(M); |
| 49 |
logNormal.setS(S); |
| 50 |
|
| 51 |
double mean = Math.exp(M + S * S / 2); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
assertEquals(mean, logNormal.mean(), 1e-10); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public void testVariance() { |
| 60 |
|
| 61 |
for (int i = 0; i < 1000; i++) { |
| 62 |
double M = Math.random() * 10.0 - 5.0; |
| 63 |
double S = Math.random() * 10; |
| 64 |
|
| 65 |
logNormal.setM(M); |
| 66 |
logNormal.setS(S); |
| 67 |
|
| 68 |
double variance = (Math.exp(S * S) - 1) * Math.exp(2 * M + S * S); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
assertEquals(variance, logNormal.variance(), 1e-10); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
public void testMedian() { |
| 78 |
|
| 79 |
System.out.println("Testing 10000 random quantile(0.5) calls"); |
| 80 |
|
| 81 |
for (int i = 0; i < 10000; i++) { |
| 82 |
double M = Math.random() * 10.0 - 5.0; |
| 83 |
double S = Math.random() * 10; |
| 84 |
|
| 85 |
logNormal.setM(M); |
| 86 |
logNormal.setS(S); |
| 87 |
|
| 88 |
double median = Math.exp(M); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
assertEquals(median, logNormal.quantile(0.5), median / 1e6); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public void testCDFAndQuantile() { |
| 97 |
|
| 98 |
System.out.println("Testing 10000 random quantile/cdf pairs"); |
| 99 |
|
| 100 |
for (int i = 0; i < 10000; i++) { |
| 101 |
|
| 102 |
double M = Math.random() * 10.0 - 5.0; |
| 103 |
double S = Math.random() * 10; |
| 104 |
|
| 105 |
logNormal.setM(M); |
| 106 |
logNormal.setS(S); |
| 107 |
|
| 108 |
double p = Math.random(); |
| 109 |
double quantile = logNormal.quantile(p); |
| 110 |
|
| 111 |
double cdf = logNormal.cdf(quantile); |
| 112 |
|
| 113 |
assertEquals(p, cdf, 1e-8); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public void testCDFAndQuantile2() { |
| 118 |
|
| 119 |
final LogNormalDistribution f = new LogNormalDistribution(1, 1); |
| 120 |
for (double i = 0.01; i < 0.95; i += 0.01) { |
| 121 |
final double y = i; |
| 122 |
|
| 123 |
BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new OneVariableFunction() { |
| 124 |
public double value(double x) { |
| 125 |
return f.cdf(x) - y; |
| 126 |
} |
| 127 |
}, 0.01, 100); |
| 128 |
zeroFinder.evaluate(); |
| 129 |
|
| 130 |
assertEquals(f.quantile(i), zeroFinder.getResult(), 1e-6); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} |