| 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.tree; |
| 27 |
|
| 28 |
import dr.evolution.tree.TreeUtils; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 32 |
import dr.inference.model.AbstractModelLikelihood; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
import dr.math.GammaFunction; |
| 37 |
import dr.util.Author; |
| 38 |
import dr.util.Citable; |
| 39 |
import dr.util.Citation; |
| 40 |
|
| 41 |
import java.util.Collections; |
| 42 |
import java.util.HashSet; |
| 43 |
import java.util.List; |
| 44 |
import java.util.Set; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class CTMCScalePrior extends AbstractModelLikelihood implements Citable { |
| 54 |
final private Parameter ctmcScale; |
| 55 |
final private TreeModel treeModel; |
| 56 |
private Set<Taxon> taxa = null; |
| 57 |
private double treeLength; |
| 58 |
private boolean treeLengthKnown; |
| 59 |
|
| 60 |
final private boolean reciprocal; |
| 61 |
final private SubstitutionModel substitutionModel; |
| 62 |
final private boolean trial; |
| 63 |
|
| 64 |
private static final double logGammaOneHalf = GammaFunction.lnGamma(0.5); |
| 65 |
|
| 66 |
public CTMCScalePrior(String name, Parameter ctmcScale, TreeModel treeModel) { |
| 67 |
this(name, ctmcScale, treeModel, false); |
| 68 |
} |
| 69 |
|
| 70 |
public CTMCScalePrior(String name, Parameter ctmcScale, TreeModel treeModel, boolean reciprocal) { |
| 71 |
this(name, ctmcScale, treeModel, reciprocal, null); |
| 72 |
} |
| 73 |
|
| 74 |
public CTMCScalePrior(String name, Parameter ctmcScale, TreeModel treeModel, boolean reciprocal, |
| 75 |
SubstitutionModel substitutionModel) { |
| 76 |
this(name, ctmcScale, treeModel, null, reciprocal, substitutionModel, false); |
| 77 |
} |
| 78 |
|
| 79 |
public CTMCScalePrior(String name, Parameter ctmcScale, TreeModel treeModel, TaxonList taxonList, boolean reciprocal, |
| 80 |
SubstitutionModel substitutionModel, boolean trial) { |
| 81 |
super(name); |
| 82 |
this.ctmcScale = ctmcScale; |
| 83 |
this.treeModel = treeModel; |
| 84 |
|
| 85 |
if (taxonList != null) { |
| 86 |
this.taxa = new HashSet<Taxon>(); |
| 87 |
for (Taxon taxon : taxonList) { |
| 88 |
this.taxa.add(taxon); |
| 89 |
} |
| 90 |
} |
| 91 |
addModel(treeModel); |
| 92 |
treeLengthKnown = false; |
| 93 |
this.reciprocal = reciprocal; |
| 94 |
this.substitutionModel = substitutionModel; |
| 95 |
this.trial = trial; |
| 96 |
} |
| 97 |
|
| 98 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 99 |
if (model == treeModel) { |
| 100 |
treeLengthKnown = false; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 105 |
} |
| 106 |
|
| 107 |
protected void storeState() { |
| 108 |
} |
| 109 |
|
| 110 |
protected void restoreState() { |
| 111 |
treeLengthKnown = false; |
| 112 |
} |
| 113 |
|
| 114 |
protected void acceptState() { |
| 115 |
} |
| 116 |
|
| 117 |
public Model getModel() { |
| 118 |
return this; |
| 119 |
} |
| 120 |
|
| 121 |
private double calculateTrialLikelihood() { |
| 122 |
double totalTreeTime = getTreeLength(); |
| 123 |
|
| 124 |
double[] eigenValues = substitutionModel.getEigenDecomposition().getEigenValues(); |
| 125 |
|
| 126 |
double lambda2 = Double.NEGATIVE_INFINITY; |
| 127 |
for (double l : eigenValues) { |
| 128 |
if (l > lambda2 && l < 0.0) { |
| 129 |
lambda2 = l; |
| 130 |
} |
| 131 |
} |
| 132 |
lambda2 = -lambda2; |
| 133 |
|
| 134 |
double logNormalization = 0.5 * Math.log(lambda2) - logGammaOneHalf; |
| 135 |
|
| 136 |
double logLike = 0; |
| 137 |
for (int i = 0; i < ctmcScale.getDimension(); ++i) { |
| 138 |
double ab = ctmcScale.getParameterValue(i) * totalTreeTime; |
| 139 |
logLike += logNormalization - 0.5 * Math.log(ab) - ab * lambda2; |
| 140 |
} |
| 141 |
return logLike; |
| 142 |
} |
| 143 |
|
| 144 |
public double getLogLikelihood() { |
| 145 |
|
| 146 |
if (trial) return calculateTrialLikelihood(); |
| 147 |
|
| 148 |
double totalTreeTime = getTreeLength(); |
| 149 |
if (reciprocal) { |
| 150 |
totalTreeTime = 1.0 / totalTreeTime; |
| 151 |
} |
| 152 |
if (substitutionModel != null) { |
| 153 |
double[] eigenValues = substitutionModel.getEigenDecomposition().getEigenValues(); |
| 154 |
|
| 155 |
double lambda2 = Double.NEGATIVE_INFINITY; |
| 156 |
for (double l : eigenValues) { |
| 157 |
if (l > lambda2 && l < 0.0) { |
| 158 |
lambda2 = l; |
| 159 |
} |
| 160 |
} |
| 161 |
totalTreeTime *= -lambda2; |
| 162 |
} |
| 163 |
double logNormalization = 0.5 * Math.log(totalTreeTime) - logGammaOneHalf; |
| 164 |
double logLike = 0; |
| 165 |
for (int i = 0; i < ctmcScale.getDimension(); ++i) { |
| 166 |
double ab = ctmcScale.getParameterValue(i); |
| 167 |
logLike += logNormalization - 0.5 * Math.log(ab) - ab * totalTreeTime; |
| 168 |
} |
| 169 |
return logLike; |
| 170 |
} |
| 171 |
|
| 172 |
private double getTreeLength() { |
| 173 |
|
| 174 |
if (taxa == null) { |
| 175 |
treeLength = TreeUtils.getTreeLength(treeModel, treeModel.getRoot()); |
| 176 |
} else { |
| 177 |
treeLength = TreeUtils.getSubTreeLength(treeModel, taxa); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
return treeLength; |
| 183 |
} |
| 184 |
|
| 185 |
public void makeDirty() { |
| 186 |
treeLengthKnown = false; |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public Citation.Category getCategory() { |
| 191 |
return Citation.Category.PRIOR_MODELS; |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public String getDescription() { |
| 196 |
return "CTMC Scale Reference Prior model"; |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public List<Citation> getCitations() { |
| 201 |
return Collections.singletonList(CITATION); |
| 202 |
} |
| 203 |
|
| 204 |
public static Citation CITATION = new Citation( |
| 205 |
new Author[]{ |
| 206 |
new Author("MAR", "Ferreira"), |
| 207 |
new Author("MA", "Suchard") |
| 208 |
}, |
| 209 |
"Bayesian analysis of elapsed times in continuous-time Markov chains", |
| 210 |
2008, |
| 211 |
"Canadian Journal of Statistics", |
| 212 |
36, |
| 213 |
355, 368, |
| 214 |
Citation.Status.PUBLISHED |
| 215 |
); |
| 216 |
} |