Debian Codegraph @

src/dr/evomodel/operators/OldLatentLiabilityGibbsOperator.java

raw symbols SCIP
1 /*
2 * OldLatentLiabilityGibbsOperator.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.evomodel.operators;
27
28 import dr.evolution.tree.MutableTreeModel;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.continuous.AbstractMultivariateTraitLikelihood;
31 import dr.evomodel.continuous.BinaryLatentLiabilityLikelihood;
32 import dr.evomodel.continuous.IntegratedMultivariateTraitLikelihood;
33 import dr.inference.model.MatrixParameter;
34 import dr.inference.operators.GibbsOperator;
35 import dr.inference.operators.MCMCOperator;
36 import dr.inference.operators.SimpleMCMCOperator;
37 import dr.math.MathUtils;
38 import dr.util.Citable;
39 import dr.xml.*;
40
41 import java.util.logging.Logger;
42
43 /**
44 * @author Marc A. Suchard
45 * @author Joe Felsenstein
46 */
47 public class OldLatentLiabilityGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
48
49 public static final String GIBBS_OPERATOR = "oldLatentLiabilityGibbsOperator";
50 // public static final String INTERNAL_ONLY = "onlyInternalNodes";
51 // public static final String TIP_WITH_PRIORS_ONLY = "onlyTipsWithPriors";
52 // public static final String NODE_PRIOR = "nodePrior";
53 // public static final String NODE_LABEL = "taxon";
54 // public static final String ROOT_PRIOR = "rootPrior";
55
56 private final MutableTreeModel treeModel;
57 private final MatrixParameter precisionMatrixParameter;
58 private final IntegratedMultivariateTraitLikelihood traitModel;
59 private final BinaryLatentLiabilityLikelihood liabilityLikelihood;
60
61 private final int dim;
62 private final String traitName;
63
64 // private Map<Taxon, GeoSpatialDistribution> nodeGeoSpatialPrior;
65 // private Map<Taxon, MultivariateNormalDistribution> nodeMVNPrior;
66 // private GeoSpatialCollectionModel parameterPrior = null;
67
68 // private boolean onlyInternalNodes = true;
69 // private boolean onlyTipsWithPriors = true;
70 // private boolean sampleRoot = false;
71 // private double[] rootPriorMean;
72 // private double[][] rootPriorPrecision;
73
74 private final int maxTries = 10000;
75
76 public OldLatentLiabilityGibbsOperator(IntegratedMultivariateTraitLikelihood traitModel,
77 BinaryLatentLiabilityLikelihood liabilityLikelihood) {
78 super();
79 this.traitModel = traitModel;
80 this.liabilityLikelihood = liabilityLikelihood;
81
82 this.treeModel = traitModel.getTreeModel();
83 this.precisionMatrixParameter = (MatrixParameter) traitModel.getDiffusionModel().getPrecisionParameter();
84 this.traitName = traitModel.getTraitName();
85 this.dim = traitModel.getDimTrait();
86
87 StringBuilder sb = new StringBuilder();
88 sb.append("Using a latent trait Gibbs operator. Please cite:");
89 sb.append(Citable.Utils.getCitationString(liabilityLikelihood));
90 Logger.getLogger("dr.evomodel.continuous").info(sb.toString());
91 }
92
93 // public void setRootPrior(MultivariateNormalDistribution rootPrior) {
94 // rootPriorMean = rootPrior.getMean();
95 // rootPriorPrecision = rootPrior.getScaleMatrix();
96 // sampleRoot = true;
97 // }
98
99 // public void setTaxonPrior(Taxon taxon, MultivariateDistribution distribution) {
100 //
101 // if (distribution instanceof GeoSpatialDistribution) {
102 // if (nodeGeoSpatialPrior == null) {
103 // nodeGeoSpatialPrior = new HashMap<Taxon, GeoSpatialDistribution>();
104 // }
105 // nodeGeoSpatialPrior.put(taxon, (GeoSpatialDistribution)distribution);
106 //
107 // } else if (distribution instanceof MultivariateNormalDistribution) {
108 // if (nodeMVNPrior == null) {
109 // nodeMVNPrior = new HashMap<Taxon, MultivariateNormalDistribution>();
110 // }
111 // nodeMVNPrior.put(taxon, (MultivariateNormalDistribution)distribution);
112 // } else {
113 // throw new RuntimeException("Only flat/truncated geospatial and multivariate normal distributions allowed");
114 // }
115 // }
116 //
117 // public void setParameterPrior(GeoSpatialCollectionModel distribution) {
118 // parameterPrior = distribution;
119 // }
120
121 public int getStepCount() {
122 return 1;
123 }
124
125 // private boolean nodeGeoSpatialPriorExists(NodeRef node) {
126 // return nodeGeoSpatialPrior != null && nodeGeoSpatialPrior.containsKey(treeModel.getNodeTaxon(node));
127 // }
128 //
129 // private boolean nodeMVNPriorExists(NodeRef node) {
130 // return nodeMVNPrior != null && nodeMVNPrior.containsKey(treeModel.getNodeTaxon(node));
131 // }
132
133 public double doOperation() {
134
135 traitModel.redrawAncestralStates();
136
137 NodeRef node = treeModel.getNode(MathUtils.nextInt(treeModel.getExternalNodeCount()));
138 int tip = node.getNumber();
139
140 // Draw truncated MVN using rejection sampling
141 do {
142 // Nothing
143 } while (!liabilityLikelihood.validTraitForTip(tip));
144
145 // NodeRef node = null;
146 // final NodeRef root = treeModel.getRoot();
147
148 // while (node == null) {
149 // if (onlyInternalNodes)
150 // node = treeModel.getInternalNode(MathUtils.nextInt(
151 // treeModel.getInternalNodeCount()));
152 // else {
153 // node = treeModel.getNode(MathUtils.nextInt(
154 // treeModel.getNodeCount()));
155 // if (onlyTipsWithPriors &&
156 // (treeModel.getChildCount(node) == 0) && // Is a tip
157 // !nodeGeoSpatialPriorExists(node)) { // Does not have a prior
158 // node = null;
159 // }
160 // }
161 // if (!sampleRoot && node == root)
162 // node = null;
163 // } // select any internal (or internal/external) node
164 //
165 // final double[] initialValue = treeModel.getMultivariateNodeTrait(node,traitName);
166 //
167 // MeanPrecision mp;
168 //
169 // if (node != root)
170 // mp = operateNotRoot(node);
171 // else
172 // mp = operateRoot(node);
173 //
174 // final Taxon taxon = treeModel.getNodeTaxon(node);
175 //
176 // final boolean nodePriorExists = nodeGeoSpatialPriorExists(node);
177 //
178 // int count = 0;
179 //
180 // final boolean parameterPriorExists = parameterPrior != null;
181 //
182 // double[] draw;
183 //
184 // do {
185 // do {
186 // if (count > maxTries) {
187 // treeModel.setMultivariateTrait(node,traitName,initialValue);
188 // throw new OperatorFailedException("Truncated Gibbs is stuck!");
189 // }
190 //
191 // draw = MultivariateNormalDistribution.nextMultivariateNormalPrecision(
192 // mp.mean, mp.precision);
193 // count++;
194 //
195 // } while (nodePriorExists && // There is a prior for this node
196 // (nodeGeoSpatialPrior.get(taxon)).logPdf(draw) == Double.NEGATIVE_INFINITY); // And draw is invalid under prior
197 //
198 // treeModel.setMultivariateTrait(node, traitName, draw);
199 //
200 // } while (parameterPriorExists &&
201 // (parameterPrior.getLogLikelihood() == Double.NEGATIVE_INFINITY));
202
203 return 0;
204 }
205
206 // private MeanPrecision operateNotRoot(NodeRef node) {
207 //
208 // double[][] precision = precisionMatrixParameter.getParameterAsMatrix();
209 //
210 // NodeRef parent = treeModel.getParent(node);
211 //
212 // double[] mean = new double[dim];
213 //
214 // double weight = 1.0 / traitModel.getRescaledBranchLength(node);
215 //
216 // double[] trait = treeModel.getMultivariateNodeTrait(parent, traitName);
217 //
218 // for (int i = 0; i < dim; i++)
219 // mean[i] = trait[i] * weight;
220 //
221 // double weightTotal = weight;
222 // for (int j = 0; j < treeModel.getChildCount(node); j++) {
223 // NodeRef child = treeModel.getChild(node, j);
224 // trait = treeModel.getMultivariateNodeTrait(child, traitName);
225 // weight = 1.0 / traitModel.getRescaledBranchLength(child);
226 //
227 // for (int i = 0; i < dim; i++)
228 // mean[i] += trait[i] * weight;
229 //
230 // weightTotal += weight;
231 // }
232 //
233 // for (int i = 0; i < dim; i++) {
234 // mean[i] /= weightTotal;
235 // for (int j = i; j < dim; j++)
236 // precision[j][i] = precision[i][j] *= weightTotal;
237 // }
238 //
239 //// if (nodeMVNPriorExists(node)) {
240 //// throw new RuntimeException("Still trying to implement multivariate normal taxon priors");
241 //// }
242 //
243 // return new MeanPrecision(mean,precision);
244 // }
245
246 class MeanPrecision {
247 final double[] mean;
248 final double[][] precision;
249
250 MeanPrecision(double[] mean, double[][] precision) {
251 this.mean = mean;
252 this.precision = precision;
253 }
254 }
255
256 // private MeanPrecision operateRoot(NodeRef node) {
257 //
258 // double[] trait;
259 // double weightTotal = 0.0;
260 //
261 // double[] weightedAverage = new double[dim];
262 //
263 // double[][] precision = precisionMatrixParameter.getParameterAsMatrix();
264 //
265 // for (int k = 0; k < treeModel.getChildCount(node); k++) {
266 // NodeRef child = treeModel.getChild(node, k);
267 // trait = treeModel.getMultivariateNodeTrait(child, traitName);
268 // final double weight = 1.0 / traitModel.getRescaledBranchLength(child);
269 //
270 // for (int i = 0; i < dim; i++) {
271 // for (int j=0; j<dim; j++)
272 // weightedAverage[i] += precision[i][j] * weight * trait[j];
273 // }
274 //
275 // weightTotal += weight;
276 // }
277 //
278 // for (int i=0; i<dim; i++) {
279 // for (int j=0; j<dim; j++) {
280 // weightedAverage[i] += rootPriorPrecision[i][j] * rootPriorMean[j];
281 // precision[i][j] = precision[i][j] * weightTotal + rootPriorPrecision[i][j];
282 // }
283 // }
284 //
285 // double[][] variance = new SymmetricMatrix(precision).inverse().toComponents();
286 //
287 // trait = new double[dim];
288 // for (int i=0; i<dim; i++) {
289 // for (int j=0; j<dim; j++)
290 // trait[i] += variance[i][j] * weightedAverage[j];
291 // }
292 //
293 // return new MeanPrecision(trait,precision);
294 // }
295
296 public String getPerformanceSuggestion() {
297 return null;
298 }
299
300 public String getOperatorName() {
301 return GIBBS_OPERATOR;
302 }
303
304 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
305
306 public String getParserName() {
307 return GIBBS_OPERATOR;
308 }
309
310 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
311
312 double weight = xo.getDoubleAttribute(WEIGHT);
313 // boolean onlyInternalNodes = xo.getAttribute(INTERNAL_ONLY, true);
314 // boolean onlyTipsWithPriors = xo.getAttribute(TIP_WITH_PRIORS_ONLY, true);
315 // boolean onlyInternalNodes = true;
316 // boolean onlyTipsWithPriors = true;
317
318 IntegratedMultivariateTraitLikelihood traitModel = (IntegratedMultivariateTraitLikelihood)
319 xo.getChild(AbstractMultivariateTraitLikelihood.class);
320
321 BinaryLatentLiabilityLikelihood liabilityLikelihood = (BinaryLatentLiabilityLikelihood)
322 xo.getChild(BinaryLatentLiabilityLikelihood.class);
323
324 OldLatentLiabilityGibbsOperator operator = new OldLatentLiabilityGibbsOperator(traitModel, liabilityLikelihood);
325 operator.setWeight(weight);
326
327 return operator;
328 }
329
330 //************************************************************************
331 // AbstractXMLObjectParser implementation
332 //************************************************************************
333
334 public String getParserDescription() {
335 return "This element returns a multivariate Gibbs operator on traits for tip nodes under a latent liability model.";
336 }
337
338 public Class getReturnType() {
339 return MCMCOperator.class;
340 }
341
342 public XMLSyntaxRule[] getSyntaxRules() {
343 return rules;
344 }
345
346 private final XMLSyntaxRule[] rules = {
347 AttributeRule.newDoubleRule(WEIGHT),
348 new ElementRule(IntegratedMultivariateTraitLikelihood.class),
349 new ElementRule(BinaryLatentLiabilityLikelihood.class),
350 };
351
352 };
353
354 }