| 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.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 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 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 |
} |