Debian Codegraph @

src/test/dr/math/KDEDistributionTest.java

raw symbols SCIP
1 package test.dr.math;
2
3 import dr.inference.distribution.DistributionLikelihood;
4 import dr.inference.model.Parameter;
5 import dr.inference.trace.LogFileTraces;
6 import dr.inference.trace.TraceException;
7 import dr.math.MathUtils;
8 import dr.math.distributions.GammaKDEDistribution;
9 import dr.math.distributions.LogTransformedNormalKDEDistribution;
10 import dr.math.distributions.NormalKDEDistribution;
11 import dr.math.matrixAlgebra.Vector;
12
13 import java.io.File;
14 import java.io.IOException;
15
16 /**
17 * @author Marc A. Suchard
18 */
19 public class KDEDistributionTest extends MathTestCase {
20
21 public void testNormalCDF() {
22
23 MathUtils.setSeed(666);
24 int length = 100000;
25
26 Double[] values = new Double[length];
27 for (int i = 0; i < length; ++i) {
28 values[i] = MathUtils.nextGaussian();
29 }
30
31 NormalKDEDistribution distribution = new NormalKDEDistribution(values);
32
33 double cdf = distribution.cdf(0);
34 System.out.println("cdf(0.00) = " + cdf);
35 assertEquals(cdf, 0.5, cdfTolerance);
36
37 cdf = distribution.cdf(1.96);
38 System.out.println("cdf(1.96) = " + cdf);
39 assertEquals(cdf, 0.975, cdfTolerance);
40
41 double quantile = distribution.quantile(0.68);
42 System.out.println("quantile(0.68) = " + quantile);
43 assertEquals(quantile, 0.468, cdfTolerance);
44
45 quantile = distribution.quantile(0.25);
46 System.out.println("quantile(0.25) = " + quantile);
47 assertEquals(quantile, -0.674, cdfTolerance);
48 }
49
50 private final static double cdfTolerance = 1E-2;
51
52 public void testNormalKDE() {
53
54 int length = 100;
55 Double[] sample = new Double[length];
56 for (int i = 0; i < length; i++) {
57 sample[i] = (double) (i + 1);
58 }
59
60 // final int gridSize = 256;
61
62 NormalKDEDistribution kde = new NormalKDEDistribution(sample, null, null, null);
63 System.out.println("bw = " + kde.getBandWidth());
64 double tolerance = 1E-4;
65 assertEquals(rBandWidth[0], kde.getBandWidth(), tolerance);
66
67
68 final int gridSize = NormalKDEDistribution.MINIMUM_GRID_SIZE;
69 double[] testPoints = new double[gridSize];
70 double x = kde.getFromPoint();
71 double delta = (kde.getToPoint() - kde.getFromPoint()) / (gridSize - 1);
72 for (int i = 0; i < gridSize; ++i) {
73 testPoints[i] = x;
74 x += delta;
75 }
76 System.out.println("Eval @ " + new Vector(testPoints));
77
78 double[] testDensity = new double[gridSize];
79 for (int i = 0; i < gridSize; ++i) {
80 testDensity[i] = kde.pdf(testPoints[i]);
81 }
82 System.out.println("Den " + new Vector(testDensity));
83
84 System.out.println("den[0] = " + testDensity[0]);
85 System.out.println("den[N] = " + testDensity[NormalKDEDistribution.MINIMUM_GRID_SIZE - 1]);
86
87 // System.exit(-1);
88 }
89
90 public void testGammaKDE() {
91
92 // int length = 10000;
93 // double shape = 4;
94 // double scale = 5;
95
96 // double[] values = new double[length];
97 // for (int i = 0; i < length; i++) {
98 // values[i] = MathUtils.nextGamma(shape, scale);
99 // }
100
101
102 // GammaKDEDistribution kde = new GammaKDEDistribution(values);
103 // System.out.println("prediction: at 2.02: "+kde.pdf(2.02177)+" at 0.405: "+kde.pdf(0.4046729)+" at 0.15: "+kde.pdf(0.1502078));
104 // System.out.println("sm: "+kde.sampleMean());
105 // TODO Need test values
106
107 }
108
109 private static double[] rBandWidth = { 12.24266 };
110
111 /**
112 * Test KDE construction from a BEAST log file
113 * @param args 0: BEAST log file; 1: trace to construct KDE; 2: burn-in
114 */
115 public static void main(String[] args) {
116
117 File file = new File(args[0]);
118 String parent = file.getParent();
119 file = new File(parent, args[0]);
120 String fileName = file.getAbsolutePath();
121
122 try {
123 LogFileTraces traces = new LogFileTraces(fileName, file);
124 traces.loadTraces();
125 long maxState = traces.getMaxState();
126
127 long burnin = Long.parseLong(args[2]);
128 traces.setBurnIn(burnin);
129
130 int traceIndexParameter = -1;
131 for (int i = 0; i < traces.getTraceCount(); i++) {
132 String traceName = traces.getTraceName(i);
133 if (traceName.trim().equals(args[1])) {
134 traceIndexParameter = i;
135 }
136 }
137
138 Double[] parameterSamples = new Double[traces.getStateCount()];
139 DistributionLikelihood likelihood = new DistributionLikelihood(new LogTransformedNormalKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));
140 //DistributionLikelihood likelihood = new DistributionLikelihood(new NormalKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));
141 //DistributionLikelihood likelihood = new DistributionLikelihood(new GammaKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples)));
142
143 Parameter parameter = new Parameter.Default(args[1]);
144 parameter.setDimension(1);
145
146 likelihood.addData(parameter);
147
148 for (Double d : parameterSamples) {
149 parameter.setParameterValue(0, d);
150 System.out.println(d + " > " + likelihood.calculateLogLikelihood());
151 }
152
153 } catch(IOException ioe) {
154 System.out.println(ioe);
155 } catch(TraceException te) {
156 System.out.println(te);
157 }
158
159 }
160
161 }
162
163 /*
164
165 # R test
166
167 sample = seq(1:100)
168 d = density(sample, bw="nrd")
169
170
171 */