| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.coalescent; |
| 27 |
|
| 28 |
import dr.inference.trace.LogFileTraces; |
| 29 |
import dr.inference.trace.TraceException; |
| 30 |
|
| 31 |
import java.io.File; |
| 32 |
import java.io.IOException; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Collections; |
| 35 |
|
| 36 |
public class GMRFBivariateCurveAnalysis { |
| 37 |
|
| 38 |
private LogFileTraces[] lft; |
| 39 |
private double endTime; |
| 40 |
private final int[] defaultIntegrationPartitionSizes = {2,5,10,15,25,50,100,1000,5000,25000}; |
| 41 |
private final int[] defaultDensityPartitionSizes = {10}; |
| 42 |
|
| 43 |
public GMRFBivariateCurveAnalysis(String[] inputFileNames, double endTime, int burnIn) |
| 44 |
throws TraceException, IOException{ |
| 45 |
this.endTime = endTime; |
| 46 |
|
| 47 |
lft = new LogFileTraces[2]; |
| 48 |
|
| 49 |
for(int i = 0; i < 2; i++){ |
| 50 |
File file = new File(inputFileNames[i]); |
| 51 |
lft[i] = new LogFileTraces(inputFileNames[i], file); |
| 52 |
lft[i].loadTraces(); |
| 53 |
lft[i].setBurnIn(burnIn); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public void densityReport(){ |
| 58 |
for(int partitionSize : defaultDensityPartitionSizes){ |
| 59 |
densityReport(partitionSize); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private void densityReport(int partitionSize){ |
| 64 |
double[][] values = new double[2][]; |
| 65 |
|
| 66 |
values[0] = new double[lft[0].getTraceCount()]; |
| 67 |
values[1] = new double[lft[1].getTraceCount()]; |
| 68 |
|
| 69 |
|
| 70 |
ArrayList<DensityResult> results = new ArrayList<DensityResult>(lft[0].getStateCount()); |
| 71 |
|
| 72 |
for(int index = 0; index < lft[0].getStateCount(); index++){ |
| 73 |
lft[0].getStateValues(index, values[0],0); |
| 74 |
lft[1].getStateValues(index, values[1],0); |
| 75 |
|
| 76 |
results.add(new DensityResult(getDensityReport(values, partitionSize))); |
| 77 |
} |
| 78 |
|
| 79 |
Collections.sort(results); |
| 80 |
|
| 81 |
double squareRootResultsSize = Math.sqrt(results.size()); |
| 82 |
int value = (int) Math.floor(squareRootResultsSize); |
| 83 |
|
| 84 |
double radius = results.get(value).radius; |
| 85 |
|
| 86 |
double logVolume = dr.math.MathUtils.logHyperSphereVolume(partitionSize,radius); |
| 87 |
|
| 88 |
System.out.println("Approximate Density at 0 with " + |
| 89 |
partitionSize + " partitions = " + |
| 90 |
1.0/(squareRootResultsSize*Math.exp(logVolume))); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
private class DensityResult implements Comparable<DensityResult>{ |
| 95 |
|
| 96 |
public double[] results; |
| 97 |
public double radius; |
| 98 |
|
| 99 |
public DensityResult(double[] results){ |
| 100 |
this.results = results; |
| 101 |
|
| 102 |
double a = 0; |
| 103 |
for(double result : results){ |
| 104 |
a += result*result; |
| 105 |
} |
| 106 |
|
| 107 |
radius = Math.pow(a, 1.0/results.length); |
| 108 |
} |
| 109 |
|
| 110 |
public int compareTo(DensityResult d){ |
| 111 |
if(this.radius < d.radius){ |
| 112 |
return -1; |
| 113 |
}else if(this.radius == d.radius){ |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
public String toString(){ |
| 121 |
String a = "("; |
| 122 |
for(double result : results){ |
| 123 |
a += Double.toString(result) + ", "; |
| 124 |
} |
| 125 |
a += " Radius = " + radius + " )\n"; |
| 126 |
return a; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
private double[] getDensityReport(double[][] values, int partitionSize){ |
| 133 |
double[] b = new double[partitionSize]; |
| 134 |
|
| 135 |
double timeInterval = endTime/partitionSize; |
| 136 |
double startTime = timeInterval/2.0; |
| 137 |
|
| 138 |
for(int i = 0; i < b.length; i++){ |
| 139 |
double[] a = { |
| 140 |
getPopSizeAtTime(values[0], startTime, values[0].length/2), |
| 141 |
getPopSizeAtTime(values[1], startTime, values[1].length/2) |
| 142 |
}; |
| 143 |
startTime += timeInterval; |
| 144 |
|
| 145 |
b[i] = a[0] - a[1]; |
| 146 |
} |
| 147 |
|
| 148 |
return b; |
| 149 |
} |
| 150 |
|
| 151 |
public void integrationReport(){ |
| 152 |
System.out.println("Analyzing Difference Between Curves\n"); |
| 153 |
for(int partitionSize : defaultIntegrationPartitionSizes){ |
| 154 |
integrationReport(partitionSize); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
private void integrationReport(int partitionSize){ |
| 159 |
|
| 160 |
double[] results = new double[lft[0].getStateCount()]; |
| 161 |
|
| 162 |
double[][] values = new double[2][]; |
| 163 |
|
| 164 |
values[0] = new double[lft[0].getTraceCount()]; |
| 165 |
values[1] = new double[lft[1].getTraceCount()]; |
| 166 |
|
| 167 |
double startTime = endTime/(partitionSize*2); |
| 168 |
double timeLength = endTime/partitionSize; |
| 169 |
|
| 170 |
for(int index = 0; index < lft[0].getStateCount(); index++){ |
| 171 |
lft[0].getStateValues(index, values[0],0); |
| 172 |
lft[1].getStateValues(index, values[1],0); |
| 173 |
|
| 174 |
results[index] = getPartitionReport(values, startTime, timeLength); |
| 175 |
} |
| 176 |
|
| 177 |
double sum = 0; |
| 178 |
|
| 179 |
for(double a : results) |
| 180 |
sum += a; |
| 181 |
|
| 182 |
|
| 183 |
System.out.println("Partition Size " + partitionSize + " = " + sum/results.length); |
| 184 |
|
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
private double getPartitionReport(double[][] values, double startTime, double timeLength){ |
| 189 |
double b = 0; |
| 190 |
|
| 191 |
while(startTime < endTime){ |
| 192 |
b += getPopSizeDifference(values, startTime); |
| 193 |
|
| 194 |
startTime += timeLength; |
| 195 |
} |
| 196 |
|
| 197 |
b *= timeLength; |
| 198 |
|
| 199 |
return b; |
| 200 |
} |
| 201 |
|
| 202 |
private double getPopSizeDifference(double[][] values, double time){ |
| 203 |
double[] a = new double[2]; |
| 204 |
|
| 205 |
for(int i = 0; i < 2; i++){ |
| 206 |
a[i] = getPopSizeAtTime(values[i],time,values[i].length/2); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
return Math.abs(a[0] - a[1]); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private double getPopSizeAtTime(double[] values, double time, int numberOfPopSizes){ |
| 216 |
for(int i = 0; i < numberOfPopSizes; i++){ |
| 217 |
if(time < values[i]){ |
| 218 |
return values[numberOfPopSizes + i]; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return -99; |
| 223 |
} |
| 224 |
|
| 225 |
} |