Debian Codegraph @

src/dr/evomodel/operators/TraitRateGibbsOperator.java

raw symbols SCIP
1 /*
2 * TraitRateGibbsOperator.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.branchratemodel.ArbitraryBranchRates;
31 import dr.evomodel.continuous.AbstractMultivariateTraitLikelihood;
32 import dr.evomodel.continuous.IntegratedMultivariateTraitLikelihood;
33 import dr.inference.distribution.DistributionLikelihood;
34 import dr.inference.distribution.GammaDistributionModel;
35 import dr.inference.model.MatrixParameter;
36 import dr.inference.operators.GibbsOperator;
37 import dr.inference.operators.MCMCOperator;
38 import dr.inference.operators.SimpleMCMCOperator;
39 import dr.math.distributions.GammaDistribution;
40 import dr.xml.*;
41
42 import java.util.logging.Logger;
43
44 /**
45 * Gibbs samples each of AbritraryBranchRates when their prior is a gamma distribution
46 *
47 * @author Marc A. Suchard
48 */
49 public class TraitRateGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
50
51 public static final String GIBBS_OPERATOR = "traitRateGibbsOperator";
52
53 private final MutableTreeModel treeModel;
54 private final MatrixParameter precisionMatrixParameter;
55 private final AbstractMultivariateTraitLikelihood traitModel;
56 private final GammaDistributionModel ratePriorModel;
57 private final GammaDistribution ratePrior;
58 private final ArbitraryBranchRates branchRateModel;
59 private final int dim;
60 private final String traitName;
61
62 public TraitRateGibbsOperator(AbstractMultivariateTraitLikelihood traitModel,
63 ArbitraryBranchRates branchRateModel,
64 GammaDistributionModel ratePriorModel,
65 GammaDistribution ratePrior) {
66 super();
67 this.traitModel = traitModel;
68 this.treeModel = traitModel.getTreeModel();
69 this.precisionMatrixParameter = (MatrixParameter) traitModel.getDiffusionModel().getPrecisionParameter();
70 this.traitName = traitModel.getTraitName();
71 this.branchRateModel = branchRateModel;
72 this.ratePriorModel = ratePriorModel;
73 this.ratePrior = ratePrior;
74 this.dim = treeModel.getMultivariateNodeTrait(treeModel.getRoot(), traitName).length;
75
76 boolean hasDistributionModel = ratePriorModel == null;
77 boolean hasDistribution = ratePrior == null;
78
79 if (traitModel instanceof IntegratedMultivariateTraitLikelihood) {
80 throw new RuntimeException("Only implemented for a SampledMultivariateTraitLikelihood");
81 }
82
83 if ((hasDistribution && hasDistributionModel) || (!hasDistribution && !hasDistributionModel)) {
84 throw new RuntimeException("Can only provide one prior density in TraitRateGibbsOperation");
85 }
86
87 if (!branchRateModel.usingReciprocal()) {
88 throw new RuntimeException("ArbitraryBranchRates in TraitRateGibbsOperatior must use reciprocal rates");
89 }
90
91 Logger.getLogger("dr.evomodel").info("Using Gibbs operator and trait rates");
92 }
93
94 public int getStepCount() {
95 return 1;
96 }
97
98 private void sampleRateForNode(NodeRef child, double[][] precision, double priorShape, double priorRate) {
99
100 NodeRef parent = treeModel.getParent(child);
101
102 final double[] trait = treeModel.getMultivariateNodeTrait(child, traitName);
103 final double[] parentTrait = treeModel.getMultivariateNodeTrait(parent, traitName);
104
105 final double precisionScalar = branchRateModel.getBranchRate(treeModel, child) /
106 traitModel.getRescaledBranchLengthForPrecision(child);
107
108 for (int i = 0; i < dim; i++) {
109 trait[i] -= parentTrait[i];
110 }
111
112 double SSE = 0;
113
114 for (int i = 0; i < dim; i++) {
115 for (int j = 0; j < dim; j++) {
116 SSE += trait[i] * precision[i][j] * trait[j];
117 }
118 }
119
120 final double gammaShape = priorShape + 0.5 * dim;
121 final double gammaRate = priorRate + 0.5 * SSE * precisionScalar;
122
123 final double newValue = GammaDistribution.nextGamma(gammaShape, 1.0 / gammaRate);
124
125 // Store the reciprocal value as the rate (\propto variance)
126 branchRateModel.setBranchRate(treeModel, child, 1.0 / newValue);
127 }
128
129 public double doOperation() {
130
131 double[][] precision = precisionMatrixParameter.getParameterAsMatrix();
132
133 double priorShape;
134 double priorRate;
135
136 if (ratePriorModel != null) {
137 priorShape = ratePriorModel.getShape();
138 priorRate = 1.0 / ratePriorModel.getScale();
139 } else {
140 priorShape = ratePrior.getShape();
141 priorRate = 1.0 / ratePrior.getScale();
142 }
143
144 for (int i = 0; i < treeModel.getNodeCount(); i++) {
145 NodeRef node = treeModel.getNode(i);
146 if (node != treeModel.getRoot()) {
147 sampleRateForNode(node, precision, priorShape, priorRate);
148 }
149 }
150 return 0;
151 }
152
153 public String getPerformanceSuggestion() {
154 return null;
155 }
156
157 public String getOperatorName() {
158 return GIBBS_OPERATOR;
159 }
160
161 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
162
163 public String getParserName() {
164 return GIBBS_OPERATOR;
165 }
166
167 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
168
169 double weight = xo.getDoubleAttribute(WEIGHT);
170 AbstractMultivariateTraitLikelihood traitLikelihood =
171 (AbstractMultivariateTraitLikelihood) xo.getChild(AbstractMultivariateTraitLikelihood.class);
172 ArbitraryBranchRates branchRates = (ArbitraryBranchRates) xo.getChild(ArbitraryBranchRates.class);
173
174 DistributionLikelihood priorLikelihood = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
175
176 GammaDistributionModel gammaPriorModel = null;
177 GammaDistribution gammaPrior = null;
178
179 if (priorLikelihood.getDistribution() instanceof GammaDistributionModel) {
180 gammaPriorModel = (GammaDistributionModel) priorLikelihood.getDistribution();
181 } else if (priorLikelihood.getDistribution() instanceof GammaDistribution) {
182 gammaPrior = (GammaDistribution) priorLikelihood.getDistribution();
183 } else {
184 throw new XMLParseException("Currently only works with a GammaDistributionModel or GammaDistribution");
185 }
186
187 if (!branchRates.usingReciprocal()) {
188 throw new XMLParseException(
189 "Gibbs sampling of rates only works with reciprocal rates under an ArbitraryBranchRates model");
190 }
191
192 TraitRateGibbsOperator operator = new TraitRateGibbsOperator(traitLikelihood, branchRates, gammaPriorModel, gammaPrior);
193 operator.setWeight(weight);
194
195 return operator;
196 }
197
198 //************************************************************************
199 // AbstractXMLObjectParser implementation
200 //************************************************************************
201
202 public String getParserDescription() {
203 return "This element returns a multivariate Gibbs operator on traits for possible all nodes.";
204 }
205
206 public Class getReturnType() {
207 return MCMCOperator.class;
208 }
209
210 public XMLSyntaxRule[] getSyntaxRules() {
211 return rules;
212 }
213
214 private final XMLSyntaxRule[] rules = {
215 AttributeRule.newDoubleRule(WEIGHT),
216 new ElementRule(AbstractMultivariateTraitLikelihood.class),
217 new ElementRule(ArbitraryBranchRates.class),
218 new ElementRule(DistributionLikelihood.class),
219 };
220
221 };
222 }