| 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.speciation; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodelxml.speciation.BirthDeathModelParser; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.util.Author; |
| 33 |
import dr.util.Citable; |
| 34 |
import dr.util.Citation; |
| 35 |
import dr.util.CommonCitations; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Collections; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
import static org.apache.commons.math.special.Gamma.logGamma; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class BirthDeathGernhard08Model extends UltrametricSpeciationModel implements Citable { |
| 61 |
|
| 62 |
public enum TreeType { |
| 63 |
UNSCALED, |
| 64 |
TIMESONLY, |
| 65 |
ORIENTED, |
| 66 |
LABELED, |
| 67 |
} |
| 68 |
|
| 69 |
public static final String BIRTH_DEATH_MODEL = BirthDeathModelParser.BIRTH_DEATH_MODEL; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private Parameter relativeDeathRateParameter; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private Parameter birthDiffRateParameter; |
| 82 |
|
| 83 |
private Parameter sampleProbability; |
| 84 |
|
| 85 |
private TreeType type; |
| 86 |
|
| 87 |
private boolean conditionalOnRoot; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public BirthDeathGernhard08Model(Parameter birthDiffRateParameter, |
| 94 |
Parameter relativeDeathRateParameter, |
| 95 |
Parameter sampleProbability, |
| 96 |
TreeType type, |
| 97 |
Type units) { |
| 98 |
|
| 99 |
this(BIRTH_DEATH_MODEL, birthDiffRateParameter, relativeDeathRateParameter, sampleProbability, type, units, false); |
| 100 |
} |
| 101 |
|
| 102 |
public BirthDeathGernhard08Model(String modelName, |
| 103 |
Parameter birthDiffRateParameter, |
| 104 |
Parameter relativeDeathRateParameter, |
| 105 |
Parameter sampleProbability, |
| 106 |
TreeType type, |
| 107 |
Type units, boolean conditionalOnRoot) { |
| 108 |
|
| 109 |
super(modelName, units); |
| 110 |
|
| 111 |
this.birthDiffRateParameter = birthDiffRateParameter; |
| 112 |
addVariable(birthDiffRateParameter); |
| 113 |
birthDiffRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 114 |
|
| 115 |
this.relativeDeathRateParameter = relativeDeathRateParameter; |
| 116 |
if( relativeDeathRateParameter != null ) { |
| 117 |
addVariable(relativeDeathRateParameter); |
| 118 |
relativeDeathRateParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1)); |
| 119 |
} |
| 120 |
|
| 121 |
this.sampleProbability = sampleProbability; |
| 122 |
if (sampleProbability != null) { |
| 123 |
addVariable(sampleProbability); |
| 124 |
sampleProbability.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1)); |
| 125 |
} |
| 126 |
|
| 127 |
this.conditionalOnRoot = conditionalOnRoot; |
| 128 |
if ( conditionalOnRoot && sampleProbability != null) { |
| 129 |
throw new IllegalArgumentException("Not supported: birth death prior conditional on root with sampling probability."); |
| 130 |
} |
| 131 |
|
| 132 |
this.type = type; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public boolean isYule() { |
| 137 |
|
| 138 |
return (relativeDeathRateParameter == null && sampleProbability == null && !conditionalOnRoot); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public double getMarginal(Tree tree, CalibrationPoints calibration) { |
| 143 |
|
| 144 |
return calibration.getCorrection(tree, getR()); |
| 145 |
} |
| 146 |
|
| 147 |
public double getR() { |
| 148 |
return birthDiffRateParameter.getParameterValue(0); |
| 149 |
} |
| 150 |
|
| 151 |
public double getA() { |
| 152 |
return relativeDeathRateParameter != null ? relativeDeathRateParameter.getParameterValue(0) : 0; |
| 153 |
} |
| 154 |
|
| 155 |
public double getRho() { |
| 156 |
return sampleProbability != null ? sampleProbability.getParameterValue(0) : 1.0; |
| 157 |
} |
| 158 |
|
| 159 |
private double logCoeff(int taxonCount) { |
| 160 |
switch( type ) { |
| 161 |
case UNSCALED: break; |
| 162 |
case TIMESONLY: return logGamma(taxonCount + 1); |
| 163 |
case ORIENTED: return Math.log(taxonCount); |
| 164 |
case LABELED: { |
| 165 |
final double two2nm1 = (taxonCount - 1) * Math.log(2.0); |
| 166 |
if( ! conditionalOnRoot ) { |
| 167 |
return two2nm1 - logGamma(taxonCount); |
| 168 |
} else { |
| 169 |
return two2nm1 - Math.log(taxonCount-1) - logGamma(taxonCount+1); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
return 0.0; |
| 174 |
} |
| 175 |
|
| 176 |
public double logTreeProbability(int taxonCount) { |
| 177 |
double c1 = logCoeff(taxonCount); |
| 178 |
if( ! conditionalOnRoot ) { |
| 179 |
c1 += (taxonCount - 1) * Math.log(getR() * getRho()) + taxonCount * Math.log(1 - getA()); |
| 180 |
} |
| 181 |
return c1; |
| 182 |
} |
| 183 |
|
| 184 |
public double logNodeProbability(Tree tree, NodeRef node) { |
| 185 |
final double height = tree.getNodeHeight(node); |
| 186 |
final double r = getR(); |
| 187 |
final double mrh = -r * height; |
| 188 |
final double a = getA(); |
| 189 |
|
| 190 |
if( ! conditionalOnRoot ) { |
| 191 |
final double rho = getRho(); |
| 192 |
final double z = Math.log(rho + ((1 - rho) - a) * Math.exp(mrh)); |
| 193 |
double l = -2 * z + mrh; |
| 194 |
|
| 195 |
if( tree.getRoot() == node ) { |
| 196 |
l += mrh - z; |
| 197 |
} |
| 198 |
return l; |
| 199 |
} else { |
| 200 |
double l; |
| 201 |
if( tree.getRoot() != node ) { |
| 202 |
final double z = Math.log(1 - a * Math.exp(mrh)); |
| 203 |
l = -2 * z + mrh; |
| 204 |
} else { |
| 205 |
|
| 206 |
final double ca = 1 - a; |
| 207 |
final double emrh = Math.exp(-mrh); |
| 208 |
if( emrh != 1.0 ) { |
| 209 |
l = (tree.getTaxonCount() - 2) * Math.log(r * ca * (1 + ca /(emrh - 1))); |
| 210 |
} else { |
| 211 |
l = (tree.getTaxonCount() - 2) * Math.log(ca * (r + ca/height)); |
| 212 |
} |
| 213 |
} |
| 214 |
return l; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
public boolean includeExternalNodesInLikelihoodCalculation() { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public Citation.Category getCategory() { |
| 224 |
return Citation.Category.TREE_PRIORS; |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public String getDescription() { |
| 229 |
return "Gernhard 2008 Birth Death Tree Model"; |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public List<Citation> getCitations() { |
| 234 |
return Collections.singletonList(new Citation( |
| 235 |
new Author[]{ |
| 236 |
new Author("T", "Gernhard"), |
| 237 |
}, |
| 238 |
"The conditioned reconstructed process", |
| 239 |
2008, |
| 240 |
"Journal of Theoretical Biology", |
| 241 |
253, |
| 242 |
769, 778, |
| 243 |
"10.1016/j.jtbi.2008.04.005" |
| 244 |
)); |
| 245 |
} |
| 246 |
} |
| 247 |
|