| 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.inference.distribution; |
| 27 |
|
| 28 |
import dr.util.Attribute; |
| 29 |
|
| 30 |
import java.awt.geom.Point2D; |
| 31 |
import java.io.BufferedReader; |
| 32 |
import java.io.FileNotFoundException; |
| 33 |
import java.io.FileReader; |
| 34 |
import java.io.IOException; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Collections; |
| 37 |
import java.util.List; |
| 38 |
import java.util.StringTokenizer; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public abstract class EmpiricalDistributionLikelihood extends AbstractDistributionLikelihood { |
| 50 |
|
| 51 |
public static final String EMPIRICAL_DISTRIBUTION_LIKELIHOOD = "empiricalDistributionLikelihood"; |
| 52 |
|
| 53 |
private static final double MIN_DENSITY_PROPORTION = 1E-2; |
| 54 |
private static final boolean DEBUG = false; |
| 55 |
|
| 56 |
private int from = -1; |
| 57 |
private int to = Integer.MAX_VALUE; |
| 58 |
private double offset = 0; |
| 59 |
private double lower = Double.NEGATIVE_INFINITY; |
| 60 |
private double upper = Double.POSITIVE_INFINITY; |
| 61 |
|
| 62 |
public EmpiricalDistributionLikelihood(String fileName, boolean inverse, boolean byColumn) { |
| 63 |
super(null); |
| 64 |
this.fileName = fileName; |
| 65 |
|
| 66 |
if (byColumn) |
| 67 |
readFileByColumn(fileName); |
| 68 |
else |
| 69 |
readFileByRow(fileName); |
| 70 |
|
| 71 |
this.inverse = inverse; |
| 72 |
} |
| 73 |
|
| 74 |
public void setBounds(double lower, double upper) { |
| 75 |
this.lower = lower; |
| 76 |
this.upper = upper; |
| 77 |
} |
| 78 |
|
| 79 |
class ComparablePoint2D extends Point2D.Double implements Comparable<ComparablePoint2D> { |
| 80 |
|
| 81 |
ComparablePoint2D(double x, double y) { |
| 82 |
super(x,y); |
| 83 |
} |
| 84 |
|
| 85 |
public int compareTo(ComparablePoint2D pt0) { |
| 86 |
if (getX() > pt0.getX()) |
| 87 |
return 1; |
| 88 |
if (getX() == pt0.getX()) |
| 89 |
return 0; |
| 90 |
return -1; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
protected void readFileByRow(String fileName) { |
| 95 |
try { |
| 96 |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); |
| 97 |
|
| 98 |
List<ComparablePoint2D> ptList = new ArrayList<ComparablePoint2D>(); |
| 99 |
|
| 100 |
String line; |
| 101 |
while ((line = reader.readLine()) != null) { |
| 102 |
if (line.charAt(0) != '#') { |
| 103 |
StringTokenizer st = new StringTokenizer(line); |
| 104 |
try { |
| 105 |
double value = Double.valueOf(st.nextToken()); |
| 106 |
double density = Double.valueOf(st.nextToken()); |
| 107 |
ptList.add(new ComparablePoint2D(value,density)); |
| 108 |
} catch (Exception e) { |
| 109 |
System.err.println("Error parsing line: '"+line+"' in "+fileName); |
| 110 |
System.exit(-1); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
Collections.sort(ptList); |
| 115 |
|
| 116 |
|
| 117 |
while( (ptList.get(0).getY() == 0) && |
| 118 |
(ptList.get(1).getY() == 0) ) |
| 119 |
ptList.remove(0); |
| 120 |
|
| 121 |
while( (ptList.get(ptList.size()-1).getY() == 0) && |
| 122 |
(ptList.get(ptList.size()-2).getY() == 0) ) |
| 123 |
ptList.remove(ptList.size()-1); |
| 124 |
|
| 125 |
|
| 126 |
double minDensity = Double.POSITIVE_INFINITY; |
| 127 |
for(ComparablePoint2D pt : ptList) { |
| 128 |
if (pt.getY() > 0.0 && pt.getY() < minDensity) |
| 129 |
minDensity = pt.getY(); |
| 130 |
} |
| 131 |
|
| 132 |
for(ComparablePoint2D pt : ptList) { |
| 133 |
if (pt.getY() == 0) |
| 134 |
pt.y = minDensity * MIN_DENSITY_PROPORTION; |
| 135 |
} |
| 136 |
values = new double[ptList.size()]; |
| 137 |
density = new double[ptList.size()]; |
| 138 |
double total = 0.0; |
| 139 |
for(int i=0; i<ptList.size(); i++) { |
| 140 |
ComparablePoint2D pt = ptList.get(i); |
| 141 |
values[i] = pt.getX(); |
| 142 |
density[i] = pt.getY(); |
| 143 |
total += pt.getY(); |
| 144 |
} |
| 145 |
for (int i = 0; i < density.length; ++i) { |
| 146 |
density[i] /= total; |
| 147 |
} |
| 148 |
reader.close(); |
| 149 |
|
| 150 |
if (DEBUG) { |
| 151 |
System.err.println("EDL File : "+fileName); |
| 152 |
System.err.println("Min value: "+values[0]); |
| 153 |
System.err.println("Max value: "+values[values.length-1]); |
| 154 |
} |
| 155 |
|
| 156 |
} catch (FileNotFoundException e) { |
| 157 |
System.err.println("File not found: "+fileName); |
| 158 |
System.exit(-1); |
| 159 |
} catch (IOException e) { |
| 160 |
System.err.println("IO exception reading: "+fileName); |
| 161 |
System.exit(-1); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
protected void readFileByColumn(String fileName) { |
| 166 |
|
| 167 |
try { |
| 168 |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); |
| 169 |
|
| 170 |
String line1 = reader.readLine(); |
| 171 |
StringTokenizer st = new StringTokenizer(line1," "); |
| 172 |
values = new double[st.countTokens()]; |
| 173 |
for(int i=0; i<values.length; i++) |
| 174 |
values[i] = Double.valueOf(st.nextToken()); |
| 175 |
String line2 = reader.readLine(); |
| 176 |
st = new StringTokenizer(line2," "); |
| 177 |
density = new double[st.countTokens()]; |
| 178 |
for(int i=0; i<density.length; i++) |
| 179 |
density[i] = Double.valueOf(st.nextToken()); |
| 180 |
|
| 181 |
reader.close(); |
| 182 |
|
| 183 |
} catch (FileNotFoundException e) { |
| 184 |
System.err.println("File not found: "+fileName); |
| 185 |
System.exit(-1); |
| 186 |
} catch (IOException e) { |
| 187 |
System.err.println("IO exception reading: "+fileName); |
| 188 |
System.exit(-1); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
public void setOffset(double offset) { |
| 194 |
this.offset = offset; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
public void setRange(int from, int to) { |
| 199 |
this.from = from; |
| 200 |
this.to = to; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public double calculateLogLikelihood() { |
| 213 |
|
| 214 |
double logL = 0.0; |
| 215 |
|
| 216 |
for (Attribute<double[]> data : dataList) { |
| 217 |
|
| 218 |
final double[] attributeValue = data.getAttributeValue(); |
| 219 |
|
| 220 |
for (int j = Math.max(0, from); j < Math.min(attributeValue.length, to); j++) { |
| 221 |
|
| 222 |
double value = attributeValue[j] + offset; |
| 223 |
if (value > lower && value < upper) { |
| 224 |
logL += logPDF(value); |
| 225 |
} else { |
| 226 |
return Double.NEGATIVE_INFINITY; |
| 227 |
} |
| 228 |
|
| 229 |
if (DEBUG) { |
| 230 |
if (Double.isInfinite(logL)) { |
| 231 |
System.err.println("Infinite log density in "+ |
| 232 |
(getId() != null ? getId() : fileName) |
| 233 |
); |
| 234 |
System.err.println("Evaluated at "+value); |
| 235 |
System.err.println("Min: " + values[0] + " Max: " + values[values.length - 1]); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return logL; |
| 241 |
} |
| 242 |
|
| 243 |
abstract protected double logPDF(double value); |
| 244 |
|
| 245 |
protected double[] values; |
| 246 |
protected double[] density; |
| 247 |
|
| 248 |
protected boolean inverse; |
| 249 |
|
| 250 |
protected String fileName; |
| 251 |
} |
| 252 |
|