| 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.evolution.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evolution.util.Units; |
| 30 |
import dr.math.Binomial; |
| 31 |
import dr.math.MultivariateFunction; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class Coalescent implements MultivariateFunction, Units { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public Coalescent(Tree tree, DemographicFunction demographicFunction) { |
| 48 |
this(new TreeIntervals(tree), demographicFunction); |
| 49 |
} |
| 50 |
|
| 51 |
public Coalescent(IntervalList intervals, DemographicFunction demographicFunction) { |
| 52 |
|
| 53 |
this.intervals = intervals; |
| 54 |
this.demographicFunction = demographicFunction; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public double calculateLogLikelihood() { |
| 63 |
|
| 64 |
return calculateLogLikelihood(intervals, demographicFunction); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static double calculateLogLikelihood(IntervalList intervals, DemographicFunction demographicFunction) { |
| 72 |
return calculateLogLikelihood(intervals, demographicFunction, 0.0); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static double calculateLogLikelihood(IntervalList intervals, DemographicFunction demographicFunction, double threshold) { |
| 80 |
|
| 81 |
double logL = 0.0; |
| 82 |
|
| 83 |
double startTime = 0.0; |
| 84 |
final int n = intervals.getIntervalCount(); |
| 85 |
for (int i = 0; i < n; i++) { |
| 86 |
|
| 87 |
final double duration = intervals.getInterval(i); |
| 88 |
final double finishTime = startTime + duration; |
| 89 |
|
| 90 |
final double intervalArea = demographicFunction.getIntegral(startTime, finishTime); |
| 91 |
if( intervalArea == 0 && duration != 0 ) { |
| 92 |
return Double.NEGATIVE_INFINITY; |
| 93 |
} |
| 94 |
final int lineageCount = intervals.getLineageCount(i); |
| 95 |
|
| 96 |
final double kChoose2 = Binomial.choose2(lineageCount); |
| 97 |
|
| 98 |
logL += -kChoose2 * intervalArea; |
| 99 |
|
| 100 |
if (intervals.getIntervalType(i) == IntervalType.COALESCENT) { |
| 101 |
|
| 102 |
final double demographicAtCoalPoint = demographicFunction.getDemographic(finishTime); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if( duration == 0.0 || demographicAtCoalPoint * (intervalArea/duration) >= threshold ) { |
| 109 |
|
| 110 |
logL -= Math.log(demographicAtCoalPoint); |
| 111 |
} else { |
| 112 |
|
| 113 |
|
| 114 |
return Double.NEGATIVE_INFINITY; |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
startTime = finishTime; |
| 120 |
} |
| 121 |
|
| 122 |
return logL; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static double calculateAnalyticalLogLikelihood(IntervalList intervals) { |
| 130 |
|
| 131 |
if (!intervals.isCoalescentOnly()) { |
| 132 |
throw new IllegalArgumentException("Can only calculate analytical likelihood for pure coalescent intervals"); |
| 133 |
} |
| 134 |
|
| 135 |
final double lambda = getLambda(intervals); |
| 136 |
final int n = intervals.getSampleCount(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
return (1-n) * Math.log(lambda); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private static double getLambda(IntervalList intervals) { |
| 151 |
double lambda = 0.0; |
| 152 |
for (int i= 0; i < intervals.getIntervalCount(); i++) { |
| 153 |
lambda += (intervals.getInterval(i) * intervals.getLineageCount(i)); |
| 154 |
} |
| 155 |
lambda /= 2; |
| 156 |
|
| 157 |
return lambda; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public double evaluate(double[] argument) { |
| 165 |
for (int i = 0; i < argument.length; i++) { |
| 166 |
demographicFunction.setArgument(i, argument[i]); |
| 167 |
} |
| 168 |
|
| 169 |
return calculateLogLikelihood(); |
| 170 |
} |
| 171 |
|
| 172 |
public int getNumArguments() { |
| 173 |
return demographicFunction.getNumArguments(); |
| 174 |
} |
| 175 |
|
| 176 |
public double getLowerBound(int n) { |
| 177 |
return demographicFunction.getLowerBound(n); |
| 178 |
} |
| 179 |
|
| 180 |
public double getUpperBound(int n) { |
| 181 |
return demographicFunction.getUpperBound(n); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public final void setUnits(Type u) |
| 193 |
{ |
| 194 |
demographicFunction.setUnits(u); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public final Type getUnits() |
| 202 |
{ |
| 203 |
return demographicFunction.getUnits(); |
| 204 |
} |
| 205 |
|
| 206 |
public DemographicFunction getDemographicFunction(){ |
| 207 |
return demographicFunction; |
| 208 |
} |
| 209 |
|
| 210 |
public IntervalList getIntervals(){ |
| 211 |
return intervals; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
DemographicFunction demographicFunction = null; |
| 216 |
|
| 217 |
|
| 218 |
IntervalList intervals = null; |
| 219 |
} |