| 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.substmodel; |
| 27 |
|
| 28 |
import dr.inference.markovjumps.MarkovJumpsType; |
| 29 |
import dr.inference.markovjumps.StateHistory; |
| 30 |
import dr.inference.markovjumps.SubordinatedProcess; |
| 31 |
import dr.inference.markovjumps.UniformizedStateHistory; |
| 32 |
import dr.inference.model.Model; |
| 33 |
|
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class UniformizedSubstitutionModel extends MarkovJumpsSubstitutionModel { |
| 56 |
|
| 57 |
public UniformizedSubstitutionModel(SubstitutionModel substModel) { |
| 58 |
this(substModel, MarkovJumpsType.COUNTS); |
| 59 |
} |
| 60 |
|
| 61 |
public UniformizedSubstitutionModel(SubstitutionModel substModel, MarkovJumpsType type) { |
| 62 |
this(substModel, type, 1); |
| 63 |
} |
| 64 |
|
| 65 |
public UniformizedSubstitutionModel(SubstitutionModel substModel, MarkovJumpsType type, int numSimulants) { |
| 66 |
super(substModel, type); |
| 67 |
this.numSimulants = numSimulants; |
| 68 |
updateSubordinator = true; |
| 69 |
} |
| 70 |
|
| 71 |
protected void setupStorage() { |
| 72 |
super.setupStorage(); |
| 73 |
tmp = new double[stateCount * stateCount]; |
| 74 |
} |
| 75 |
|
| 76 |
protected void storeState() { |
| 77 |
storedSubordinator = subordinator; |
| 78 |
} |
| 79 |
|
| 80 |
protected void restoreState() { |
| 81 |
subordinator = storedSubordinator; |
| 82 |
} |
| 83 |
|
| 84 |
private void constructSubordinator() { |
| 85 |
substModel.getInfinitesimalMatrix(tmp); |
| 86 |
subordinator = new SubordinatedProcess(tmp, stateCount); |
| 87 |
updateSubordinator = false; |
| 88 |
} |
| 89 |
|
| 90 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 91 |
if (model == substModel) { |
| 92 |
updateSubordinator = true; |
| 93 |
} |
| 94 |
super.handleModelChangedEvent(model, object, index); |
| 95 |
} |
| 96 |
|
| 97 |
public void setSaveCompleteHistory(boolean in) { |
| 98 |
saveCompleteHistory = in; |
| 99 |
} |
| 100 |
|
| 101 |
public void computeCondStatMarkovJumps(double time, |
| 102 |
double[] countMatrix) { |
| 103 |
|
| 104 |
throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel"); |
| 105 |
} |
| 106 |
|
| 107 |
public void computeCondStatMarkovJumps(double time, |
| 108 |
double[] transitionProbs, |
| 109 |
double[] countMatrix) { |
| 110 |
|
| 111 |
throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel"); |
| 112 |
} |
| 113 |
|
| 114 |
public void computeJointStatMarkovJumps(double time, |
| 115 |
double[] countMatrix) { |
| 116 |
|
| 117 |
throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel"); |
| 118 |
} |
| 119 |
|
| 120 |
public double computeCondStatMarkovJumps(int startingState, |
| 121 |
int endingState, |
| 122 |
double time) { |
| 123 |
|
| 124 |
substModel.getTransitionProbabilities(time, tmp); |
| 125 |
return computeCondStatMarkovJumps(startingState, endingState, time, |
| 126 |
tmp[startingState * stateCount + endingState]); |
| 127 |
} |
| 128 |
|
| 129 |
public String getCompleteHistory() { |
| 130 |
return getCompleteHistory(null, null); |
| 131 |
} |
| 132 |
|
| 133 |
public String getCompleteHistory(Double newStartTime, Double newEndTime) { |
| 134 |
return getCompleteHistory(-1, newStartTime, newEndTime); |
| 135 |
} |
| 136 |
|
| 137 |
public String getCompleteHistory(int site, Double newStartTime, Double newEndTime) { |
| 138 |
if (newStartTime != null && newEndTime != null) { |
| 139 |
|
| 140 |
completeHistory.rescaleTimesOfEvents(newStartTime, newEndTime); |
| 141 |
} |
| 142 |
return completeHistory.toStringChanges(site, dataType); |
| 143 |
} |
| 144 |
|
| 145 |
public int getNumberOfJumpsInCompleteHistory() { |
| 146 |
return completeHistory == null ? -1 : completeHistory.getNumberOfJumps(); |
| 147 |
} |
| 148 |
|
| 149 |
public double computeCondStatMarkovJumps(int startingState, |
| 150 |
int endingState, |
| 151 |
double time, |
| 152 |
double transitionProbability) { |
| 153 |
|
| 154 |
if (updateSubordinator) { |
| 155 |
constructSubordinator(); |
| 156 |
} |
| 157 |
|
| 158 |
double total = 0; |
| 159 |
for (int i = 0; i < numSimulants; i++) { |
| 160 |
StateHistory history = null; |
| 161 |
try { |
| 162 |
history = UniformizedStateHistory.simulateConditionalOnEndingState( |
| 163 |
0.0, |
| 164 |
startingState, |
| 165 |
time, |
| 166 |
endingState, |
| 167 |
transitionProbability, |
| 168 |
stateCount, |
| 169 |
subordinator |
| 170 |
); |
| 171 |
} catch (SubordinatedProcess.Exception e) { |
| 172 |
|
| 173 |
if (RETURN_NAN) { |
| 174 |
if (reportWarning) { |
| 175 |
Logger.getLogger("dr.app.beagle").info( |
| 176 |
"Unable to compute a robust count; this is most likely due to poor starting values." |
| 177 |
); |
| 178 |
} |
| 179 |
reportWarning = false; |
| 180 |
return Double.NaN; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
System.err.println("Attempting rejection sampling after uniformization failure"); |
| 185 |
|
| 186 |
substModel.getInfinitesimalMatrix(tmp); |
| 187 |
int attempts = 0; |
| 188 |
boolean success = false; |
| 189 |
|
| 190 |
while (!success) { |
| 191 |
if (attempts >= maxRejectionAttempts) { |
| 192 |
throw new RuntimeException("Rejection sampling failure, after uniformization failure"); |
| 193 |
} |
| 194 |
|
| 195 |
history = StateHistory.simulateUnconditionalOnEndingState(0.0, startingState, time, tmp, stateCount); |
| 196 |
if (history.getEndingState() == endingState) { |
| 197 |
success = true; |
| 198 |
} |
| 199 |
|
| 200 |
attempts++; |
| 201 |
} |
| 202 |
} |
| 203 |
total += getProcessForSimulant(history); |
| 204 |
if (saveCompleteHistory) { |
| 205 |
if (numSimulants == 1) { |
| 206 |
completeHistory = history; |
| 207 |
} else { |
| 208 |
throw new RuntimeException("Use single simulant when saving complete histories"); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return total / (double) numSimulants; |
| 213 |
} |
| 214 |
|
| 215 |
public StateHistory getStateHistory() { |
| 216 |
return completeHistory; |
| 217 |
} |
| 218 |
|
| 219 |
private final int numSimulants; |
| 220 |
private boolean updateSubordinator; |
| 221 |
private SubordinatedProcess subordinator; |
| 222 |
private SubordinatedProcess storedSubordinator; |
| 223 |
|
| 224 |
private boolean saveCompleteHistory = false; |
| 225 |
private StateHistory completeHistory = null; |
| 226 |
|
| 227 |
private double[] tmp; |
| 228 |
|
| 229 |
private static int maxRejectionAttempts = 100000; |
| 230 |
private static final boolean RETURN_NAN = true; |
| 231 |
private static boolean reportWarning = true; |
| 232 |
} |