Debian Codegraph @

src/dr/inference/model/LikelihoodProfile.java

raw symbols SCIP
1 /*
2 * LikelihoodProfile.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.model;
27
28 import dr.math.NumericalDerivative;
29 import dr.math.UnivariateFunction;
30 import dr.math.distributions.RandomGenerator;
31 import dr.xml.*;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * @author Marc A. Suchard
38 */
39
40 public class LikelihoodProfile implements Reportable {
41
42 public static final String SEP = ",";
43 public static final String ENDL = "\n";
44 public static final String LIKELIHOOD_PROFILE = "likelihoodProfile";
45
46 private RandomGenerator generator;
47 private int mcSamples = 1;
48 private Parameter integrated;
49
50 public LikelihoodProfile(Likelihood likelihood_, Parameter parameter_, final int dim_,
51 double lowerBound_, double upperBound_, int numPoints_) {
52 this.likelihood = likelihood_;
53 this.parameter = parameter_;
54 this.dim = dim_;
55 this.lowerBound = lowerBound_;
56 this.upperBound = upperBound_;
57 this.numPoints = numPoints_;
58
59 function = new UnivariateFunction() {
60
61 public double evaluate(double argument) {
62 parameter.setParameterValue(dim, argument);
63 return likelihood.getLogLikelihood();
64 }
65
66 public double getLowerBound() {
67 return lowerBound;
68 }
69
70 public double getUpperBound() {
71 return upperBound;
72 }
73 };
74 }
75
76 public String getReport() {
77 if (profilePoints.size() == 0) {
78 generateProfile();
79 }
80 StringBuilder sb = new StringBuilder();
81 sb.append("x").append(SEP)
82 .append("f").append(SEP)
83 .append("df").append(SEP)
84 .append("ddf").append(ENDL);
85 for (ProfilePoint pt : profilePoints) {
86 sb.append(pt.toString());
87 }
88 return sb.toString();
89 }
90
91 private int count = 0;
92
93 private void generateProfile() {
94 double delta = (upperBound - lowerBound) / (numPoints - 1);
95 double x = lowerBound;
96 for (int i = 0; i < numPoints; ++i) {
97 // System.err.println("i = " + i);
98 double fx = 0, dx = 0, ddx = 0;
99 for (int j = 0; j < mcSamples; ++j) {
100 // System.err.println("j = " + j);
101 if (generator != null) {
102
103 // System.err.println("Original: ");
104 // System.err.println(new Vector(integrated.getParameterValues()));
105
106 integrateGenerator(x);
107 // System.err.println(new Vector(integrated.getParameterValues()));
108 // System.err.println("");
109 //
110 // if (count > 0) {
111 // System.exit(-1);
112 // }
113 // count++;
114
115 }
116 fx += function.evaluate(x);
117 dx += NumericalDerivative.firstDerivative(function, x);
118 ddx += NumericalDerivative.secondDerivative(function, x);
119 }
120 fx /= mcSamples;
121 dx /= mcSamples;
122 ddx /= mcSamples;
123 profilePoints.add(new ProfilePoint(x, fx, dx, ddx));
124 x += delta;
125 }
126 }
127
128 private void integrateGenerator(double argument) {
129 parameter.setParameterValue(dim, argument);
130 double[] draw = (double[]) generator.nextRandom();
131 int dim = draw.length;
132
133 if (dim != integrated.getDimension()) {
134 throw new RuntimeException("Invalid integrated parameter and generator");
135 }
136 for (int i = 0; i < dim; ++i) {
137 draw[i] = 0;
138 integrated.setParameterValue(i, draw[i]);
139 }
140 }
141
142 private class ProfilePoint {
143 public double x;
144 public double f;
145 public double df;
146 public double ddf;
147
148 ProfilePoint(double x, double f, double df, double ddf) {
149 this.x = x;
150 this.f = f;
151 this.df = df;
152 this.ddf = ddf;
153 }
154
155 public String toString() {
156 return new StringBuilder()
157 .append(x).append(SEP)
158 .append(f).append(SEP)
159 .append(df).append(SEP)
160 .append(ddf).append(ENDL)
161 .toString();
162 }
163 }
164
165 private final Likelihood likelihood;
166 private final Parameter parameter;
167 private final UnivariateFunction function;
168 private int dim;
169 private double lowerBound;
170 private double upperBound;
171 private int numPoints;
172 private List<ProfilePoint> profilePoints = new ArrayList<ProfilePoint>();
173
174 public static final String DIM = "dim";
175 public static final String LOWER_BOUND = "lower";
176 public static final String UPPER_BOUND = "upper";
177 public static final String GRID_POINTS = "points";
178 public static final String EXPECTATION = "expectation";
179 public static final String MC_SAMPLES = "samples";
180
181 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
182
183 public String getParserName() {
184 return LIKELIHOOD_PROFILE;
185 }
186
187 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
188
189 Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
190 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
191 int dim = xo.getAttribute(DIM, 0);
192 double lowerBound = xo.getDoubleAttribute(LOWER_BOUND);
193 double upperBound = xo.getDoubleAttribute(UPPER_BOUND);
194 int numPoints = xo.getAttribute(GRID_POINTS, 100);
195
196 LikelihoodProfile profile = new LikelihoodProfile(likelihood, parameter, dim, lowerBound, upperBound, numPoints);
197
198
199 if (xo.hasChildNamed(EXPECTATION)) {
200 // System.err.println("Here");
201 XMLObject cxo = xo.getChild(EXPECTATION);
202 int mcSamples = cxo.getAttribute(MC_SAMPLES, 1);
203 RandomGenerator trait =
204 (RandomGenerator) cxo.getChild(RandomGenerator.class);
205
206 Parameter integrated = (Parameter) cxo.getChild(Parameter.class);
207 profile.addGeneratorForExpectation(trait, integrated, mcSamples);
208 // System.err.println("trait null ? " + (trait == null ? "yes" : "no"));
209 // System.exit(-1);
210 }
211
212 return profile;
213 }
214
215 public String getParserDescription() {
216 return "This element represents a tool to profile a likelihood surface";
217 }
218
219 public Class getReturnType() {
220 return LikelihoodProfile.class;
221 }
222
223 public XMLSyntaxRule[] getSyntaxRules() {
224 return rules;
225 }
226
227 private final XMLSyntaxRule[] rules = {
228 AttributeRule.newDoubleArrayRule(LOWER_BOUND),
229 AttributeRule.newDoubleArrayRule(UPPER_BOUND),
230 AttributeRule.newIntegerRule(DIM, true),
231 AttributeRule.newIntegerRule(GRID_POINTS, true),
232 new ElementRule(Likelihood.class),
233 new ElementRule(Parameter.class),
234 new ElementRule(EXPECTATION,
235 new XMLSyntaxRule[]{
236 AttributeRule.newIntegerRule(MC_SAMPLES),
237 new ElementRule(RandomGenerator.class),
238 new ElementRule(Parameter.class),
239 }, true),
240 };
241 };
242
243 private void addGeneratorForExpectation(RandomGenerator generator, Parameter integrated, int mcSamples) {
244 this.generator = generator;
245 this.integrated = integrated;
246 this.mcSamples = mcSamples;
247 }
248 }