| 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.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.ExponentialBSPGrowth; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.coalescent.BayesianSkylineLikelihoodParser; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.inference.model.Statistic; |
| 34 |
import dr.math.MathUtils; |
| 35 |
|
| 36 |
import java.util.Date; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class ExponentialSkythingLikelihood extends OldAbstractCoalescentLikelihood { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public ExponentialSkythingLikelihood(Tree tree, |
| 51 |
Parameter slopeParameter, |
| 52 |
Parameter startingPopSize, |
| 53 |
int type) { |
| 54 |
super(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD); |
| 55 |
|
| 56 |
groupSizeParameter = new Parameter.Default(slopeParameter.getDimension(), 1); |
| 57 |
popSizeParameter = new Parameter.Default(slopeParameter.getDimension()-1); |
| 58 |
|
| 59 |
this.slopeParameter = slopeParameter; |
| 60 |
this.startingPopSize = startingPopSize; |
| 61 |
int events = tree.getExternalNodeCount() - 1; |
| 62 |
int paramDim = slopeParameter.getDimension(); |
| 63 |
this.type = type; |
| 64 |
|
| 65 |
if (paramDim != events) { |
| 66 |
throw new IllegalArgumentException("There are more groups than coalescent nodes in the tree."); |
| 67 |
} |
| 68 |
|
| 69 |
this.tree = tree; |
| 70 |
if (tree instanceof TreeModel) { |
| 71 |
addModel((TreeModel)tree); |
| 72 |
} |
| 73 |
addVariable(slopeParameter); |
| 74 |
|
| 75 |
setupIntervals(); |
| 76 |
|
| 77 |
addStatistic(new GroupHeightStatistic()); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double getLogLikelihood() { |
| 89 |
|
| 90 |
setupIntervals(); |
| 91 |
|
| 92 |
double logL = 0.0; |
| 93 |
|
| 94 |
double currentTime = 0.0; |
| 95 |
|
| 96 |
int groupIndex=0; |
| 97 |
int[] groupSizes = getGroupSizes(); |
| 98 |
double[] groupEnds = getGroupHeights(); |
| 99 |
|
| 100 |
int subIndex = 0; |
| 101 |
|
| 102 |
|
| 103 |
ExponentialBSPGrowth eg = new ExponentialBSPGrowth(Type.YEARS); |
| 104 |
|
| 105 |
double startGroupPopSize = 0; |
| 106 |
double endGroupPopSize; |
| 107 |
|
| 108 |
for (int j = intervalCount - 1; j >=0; j--) { |
| 109 |
if(j==intervalCount - 1){ |
| 110 |
endGroupPopSize = startingPopSize.getParameterValue(0); |
| 111 |
} else { |
| 112 |
endGroupPopSize = startGroupPopSize; |
| 113 |
} |
| 114 |
double startTime = currentTime; |
| 115 |
double endTime = currentTime + intervals[j]; |
| 116 |
startGroupPopSize = endGroupPopSize*Math.exp(slopeParameter.getParameterValue(j)*(endTime - startTime)); |
| 117 |
|
| 118 |
|
| 119 |
eg.setupN1(endGroupPopSize, slopeParameter.getParameterValue(j), endTime - startTime); |
| 120 |
|
| 121 |
if (getIntervalType(j) == CoalescentEventType.COALESCENT) { |
| 122 |
subIndex += 1; |
| 123 |
if (subIndex >= groupSizes[groupIndex]) { |
| 124 |
groupIndex += 1; |
| 125 |
subIndex = 0; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
logL += calculateIntervalLikelihood(eg, intervals[j], currentTime, lineageCounts[j], getIntervalType(j)); |
| 130 |
|
| 131 |
|
| 132 |
int diff = getCoalescentEvents(j)-1; |
| 133 |
for (int k = 0; k < diff; k++) { |
| 134 |
eg.setup(startGroupPopSize, startGroupPopSize, endTime - startTime); |
| 135 |
logL += calculateIntervalLikelihood(eg, 0.0, currentTime, lineageCounts[j]-k-1, |
| 136 |
CoalescentEventType.COALESCENT); |
| 137 |
subIndex += 1; |
| 138 |
if (subIndex >= groupSizes[groupIndex]) { |
| 139 |
groupIndex += 1; |
| 140 |
subIndex = 0; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
currentTime += intervals[j]; |
| 145 |
} |
| 146 |
|
| 147 |
return logL; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public final double getPopSize(int groupIndex, double midTime, double[] groupHeights) { |
| 155 |
|
| 156 |
return popSizeParameter.getParameterValue(groupIndex); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
public final int[] getGroupSizes() { |
| 162 |
if (groupSizeParameter.getParameterValue(0) < 2.0) { |
| 163 |
throw new IllegalArgumentException("For linear model first group size must be >= 2."); |
| 164 |
} |
| 165 |
|
| 166 |
int[] groupSizes = new int[groupSizeParameter.getDimension()]; |
| 167 |
|
| 168 |
for (int i = 0; i < groupSizes.length; i++) { |
| 169 |
double g = groupSizeParameter.getParameterValue(i); |
| 170 |
if (g != Math.round(g)) { |
| 171 |
throw new RuntimeException("Group size " + i + " should be integer but found:" + g); |
| 172 |
} |
| 173 |
groupSizes[i] = (int)Math.round(g); |
| 174 |
} |
| 175 |
return groupSizes; |
| 176 |
} |
| 177 |
|
| 178 |
private int getGroupCount() { |
| 179 |
return groupSizeParameter.getDimension(); |
| 180 |
} |
| 181 |
|
| 182 |
private int getGroupSize(int groupIndex) { |
| 183 |
double g = groupSizeParameter.getParameterValue(groupIndex); |
| 184 |
if (g != Math.round(g)) { |
| 185 |
throw new RuntimeException("Group size " + groupIndex + " should be integer but found:" + g); |
| 186 |
} |
| 187 |
return (int)Math.round(g); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
public final double[] getGroupHeights() { |
| 192 |
double[] groupEnds = new double[getGroupCount()]; |
| 193 |
|
| 194 |
double timeEnd = 0.0; |
| 195 |
int groupIndex = 0; |
| 196 |
int subIndex = 0; |
| 197 |
for (int i = 0; i < intervalCount; i++) { |
| 198 |
|
| 199 |
timeEnd += intervals[i]; |
| 200 |
|
| 201 |
if (getIntervalType(i) == CoalescentEventType.COALESCENT) { |
| 202 |
subIndex += 1; |
| 203 |
if (subIndex >= getGroupSize(groupIndex)) { |
| 204 |
groupEnds[groupIndex] = timeEnd; |
| 205 |
groupIndex += 1; |
| 206 |
subIndex = 0; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
groupEnds[getGroupCount()-1] = timeEnd; |
| 211 |
|
| 212 |
return groupEnds; |
| 213 |
} |
| 214 |
|
| 215 |
private double getGroupHeight(int groupIndex) { |
| 216 |
return getGroupHeights()[groupIndex]; |
| 217 |
} |
| 218 |
|
| 219 |
final public int getType() { |
| 220 |
return type; |
| 221 |
} |
| 222 |
|
| 223 |
final public Parameter getPopSizeParameter() { |
| 224 |
return popSizeParameter; |
| 225 |
} |
| 226 |
|
| 227 |
final public Parameter getGroupSizeParameter() { |
| 228 |
return groupSizeParameter; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public String getTitle() { |
| 236 |
final String title = "Bayesian Skything (exponential)\n" + |
| 237 |
"Generated " + (new Date()).toString() + " [seed=" + MathUtils.getSeed() + "]"; |
| 238 |
return title; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public class GroupHeightStatistic extends Statistic.Abstract { |
| 246 |
|
| 247 |
public GroupHeightStatistic() { |
| 248 |
super("groupHeight"); |
| 249 |
} |
| 250 |
|
| 251 |
public int getDimension() { return getGroupCount(); } |
| 252 |
|
| 253 |
public double getStatisticValue(int i) { |
| 254 |
return getGroupHeight(i); |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private final Parameter slopeParameter; |
| 265 |
|
| 266 |
private final Parameter popSizeParameter; |
| 267 |
|
| 268 |
private final Parameter groupSizeParameter; |
| 269 |
|
| 270 |
private final Parameter startingPopSize; |
| 271 |
|
| 272 |
private final int type; |
| 273 |
|
| 274 |
} |