Debian Codegraph @

src/dr/inference/distribution/EmpiricalDistributionLikelihood.java

raw symbols SCIP
1 /*
2 * EmpiricalDistributionLikelihood.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * A class that returns the log likelihood of a set of data (statistics)
42 * being distributed according to a distribution generated empirically from some data.
43 *
44 * @author Andrew Rambaut
45 * @author Marc Suchard
46 * @version $Id:$
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 // Prune off begining and ending zeros
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 // Find min density
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 // Set zeros in the middle to 1/100th of minDensity
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 // Likelihood IMPLEMENTATION
205 // **************************************************************
206
207 /**
208 * Calculate the log likelihood of the current state.
209 *
210 * @return the log likelihood.
211 */
212 public double calculateLogLikelihood() {
213
214 double logL = 0.0;
215
216 for (Attribute<double[]> data : dataList) {
217 // see comment in DistributionLikelihood
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