| 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.inference.distribution.ParametricDistributionModel; |
| 30 |
import dr.inference.model.CompoundModel; |
| 31 |
import dr.inference.model.Likelihood; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class SpeciesTreeBMPrior extends Likelihood.Abstract { |
| 39 |
|
| 40 |
private final SpeciesTreeModel sTree; |
| 41 |
|
| 42 |
private final ParametricDistributionModel tips; |
| 43 |
private final Parameter popSigma; |
| 44 |
private final Parameter stSigma; |
| 45 |
|
| 46 |
private static final double d1 = (1 - Math.exp(-1)); |
| 47 |
private static final double f2 = -0.5 * Math.log(2*Math.PI); |
| 48 |
private final boolean logRoot; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public SpeciesTreeBMPrior(SpeciesTreeModel sTree, Parameter popSigma, Parameter stSigma, |
| 63 |
ParametricDistributionModel tipsPrior, boolean logRoot) { |
| 64 |
super(new CompoundModel("STBMprior")); |
| 65 |
|
| 66 |
this.sTree = sTree; |
| 67 |
this.popSigma = popSigma; |
| 68 |
this.stSigma = stSigma; |
| 69 |
this.tips = tipsPrior; |
| 70 |
this.logRoot = logRoot; |
| 71 |
|
| 72 |
final CompoundModel cm = (CompoundModel)this.getModel(); |
| 73 |
|
| 74 |
cm.addModel(tipsPrior); |
| 75 |
cm.addModel(sTree); |
| 76 |
} |
| 77 |
|
| 78 |
protected double calculateLogLikelihood() { |
| 79 |
double logLike = 0; |
| 80 |
|
| 81 |
|
| 82 |
final NodeRef root = sTree.getRoot(); |
| 83 |
final double stRootHeight = sTree.getNodeHeight(root); |
| 84 |
|
| 85 |
final SpeciesTreeModel.RawPopulationHelper helper = sTree.getPopulationHelper(); |
| 86 |
final double lim = stRootHeight ; |
| 87 |
|
| 88 |
{ |
| 89 |
final double sigma = stSigma.getParameterValue(0); |
| 90 |
final double s2 = 2 * sigma * sigma; |
| 91 |
int count = 0; |
| 92 |
double[] pops = new double[2]; |
| 93 |
|
| 94 |
for(int nn = 0; nn < sTree.getNodeCount(); ++nn) { |
| 95 |
final NodeRef n = sTree.getNode(nn); |
| 96 |
|
| 97 |
if( sTree.isExternal(n) ) { |
| 98 |
logLike += tips.logPdf(helper.tipPopulation(n)); |
| 99 |
} else { |
| 100 |
for(int nc = 0; nc < 2; ++nc) { |
| 101 |
final NodeRef child = sTree.getChild(n, nc); |
| 102 |
helper.getPopulations(n, nc, pops); |
| 103 |
final double dt = sTree.getBranchLength(child) / lim; |
| 104 |
final double pDiff = (pops[1] - pops[0]) / lim; |
| 105 |
logLike -= pDiff * pDiff / (s2 * dt); |
| 106 |
|
| 107 |
logLike -= .5 * Math.log(dt); |
| 108 |
count += 1; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if( ! helper.perSpeciesPopulation() ) { |
| 114 |
helper.getRootPopulations(pops); |
| 115 |
final double dt = helper.geneTreesRootHeight()/lim - 1; |
| 116 |
|
| 117 |
|
| 118 |
final double pDiff = logRoot ? Math.log(pops[1]/pops[0]) : (pops[1] - pops[0])/lim; |
| 119 |
|
| 120 |
logLike -= pDiff * pDiff / (s2 * dt); |
| 121 |
|
| 122 |
logLike -= .5 * Math.log(dt); |
| 123 |
count += 1; |
| 124 |
} |
| 125 |
|
| 126 |
logLike += count * (f2 - Math.log(sigma)); |
| 127 |
} |
| 128 |
|
| 129 |
if( helper.perSpeciesPopulation() ) { |
| 130 |
final double sigma = (popSigma != null ? popSigma : stSigma).getParameterValue(0); |
| 131 |
final double s2 = 2 * sigma * sigma; |
| 132 |
|
| 133 |
for(int ns = 0; ns < helper.nSpecies(); ++ns) { |
| 134 |
double[] times = helper.getTimes(ns); |
| 135 |
double[] pops = helper.getPops(ns); |
| 136 |
final double tMax = times[times.length-1]; |
| 137 |
|
| 138 |
double ll = 0.0; |
| 139 |
double x = pops[0]/tMax; |
| 140 |
double t0 = 0.0; |
| 141 |
for(int k = 1; k < times.length; ++k) { |
| 142 |
final double y = pops[k]/tMax; |
| 143 |
final double dt = (times[k-1] - t0) / tMax; |
| 144 |
ll -= (y - x)*(y-x) / (s2*dt); |
| 145 |
|
| 146 |
ll -= .5 * Math.log(dt); |
| 147 |
x = y; |
| 148 |
t0 = times[k-1]; |
| 149 |
} |
| 150 |
ll += (times.length-1) * (f2 - Math.log(sigma)); |
| 151 |
|
| 152 |
logLike += ll; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return logLike; |
| 157 |
} |
| 158 |
|
| 159 |
protected boolean getLikelihoodKnown() { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
} |