| 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.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 |
|
| 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 |
|
| 98 |
double fx = 0, dx = 0, ddx = 0; |
| 99 |
for (int j = 0; j < mcSamples; ++j) { |
| 100 |
|
| 101 |
if (generator != null) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
integrateGenerator(x); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 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 |
|
| 209 |
|
| 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 |
} |