| 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 java.util.List; |
| 29 |
|
| 30 |
import dr.evolution.coalescent.IntervalType; |
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.evomodel.coalescent.OldAbstractCoalescentLikelihood.CoalescentEventType; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.inference.model.Likelihood; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class ExponentialProductSufficientStatisticsLikelihood extends OldAbstractCoalescentLikelihood { |
| 43 |
|
| 44 |
|
| 45 |
public final static boolean FIXED_TREE = false; |
| 46 |
|
| 47 |
private TreeModel treeModel; |
| 48 |
private double[] posteriorMeans; |
| 49 |
|
| 50 |
protected int fieldLength; |
| 51 |
protected double[] coalescentIntervals; |
| 52 |
protected double[] sufficientStatistics; |
| 53 |
|
| 54 |
|
| 55 |
public ExponentialProductSufficientStatisticsLikelihood(TreeModel treeModel, double[] posteriorMeans) { |
| 56 |
super("ExponentialProductSufficientStatisticsLikelihood"); |
| 57 |
this.treeModel = treeModel; |
| 58 |
this.posteriorMeans = posteriorMeans; |
| 59 |
|
| 60 |
tree = treeModel; |
| 61 |
addModel((TreeModel) tree); |
| 62 |
|
| 63 |
this.fieldLength = posteriorMeans.length; |
| 64 |
wrapSetupIntervals(); |
| 65 |
coalescentIntervals = new double[fieldLength]; |
| 66 |
sufficientStatistics = new double[fieldLength]; |
| 67 |
} |
| 68 |
|
| 69 |
protected void wrapSetupIntervals() { |
| 70 |
setupIntervals(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected void setupSufficientStatistics() { |
| 85 |
int index = 0; |
| 86 |
|
| 87 |
double length = 0; |
| 88 |
double weight = 0; |
| 89 |
for (int i = 0; i < getIntervalCount(); i++) { |
| 90 |
|
| 91 |
length += getInterval(i); |
| 92 |
weight += getInterval(i) * getLineageCount(i) * (getLineageCount(i) - 1); |
| 93 |
if (getIntervalType(i) == CoalescentEventType.COALESCENT) { |
| 94 |
coalescentIntervals[index] = length; |
| 95 |
sufficientStatistics[index] = weight / 2.0; |
| 96 |
index++; |
| 97 |
length = 0; |
| 98 |
weight = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private void makeIntervalsKnown() { |
| 104 |
if (!intervalsKnown) { |
| 105 |
wrapSetupIntervals(); |
| 106 |
intervalsKnown = true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public double calculateLogLikelihood() { |
| 111 |
tree = treeModel; |
| 112 |
makeIntervalsKnown(); |
| 113 |
setupSufficientStatistics(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
double currentLike = 0; |
| 118 |
double[] currentGamma = posteriorMeans; |
| 119 |
|
| 120 |
for (int i = 0; i < fieldLength; i++) { |
| 121 |
|
| 122 |
currentLike += -currentGamma[i] - sufficientStatistics[i] * Math.exp(-currentGamma[i]); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
return currentLike; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected boolean getLikelihoodKnown() { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
} |