| 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.inference.markovjumps; |
| 27 |
|
| 28 |
import dr.math.MathUtils; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class UniformizedStateHistory extends StateHistory { |
| 49 |
|
| 50 |
public UniformizedStateHistory(double startingTime, int startingState, int stateCount, double[] lambda) { |
| 51 |
this(startingTime, startingState, stateCount, new SubordinatedProcess(lambda, stateCount)); |
| 52 |
} |
| 53 |
|
| 54 |
protected UniformizedStateHistory(double startingTime, int startingState, int stateCount, |
| 55 |
SubordinatedProcess subordinator) { |
| 56 |
super(startingTime, startingState, stateCount); |
| 57 |
this.subordinator = subordinator; |
| 58 |
} |
| 59 |
|
| 60 |
public SubordinatedProcess getSubordinatedProcess() { |
| 61 |
return subordinator; |
| 62 |
} |
| 63 |
|
| 64 |
public static StateHistory simulateUnconditionalOnEndingState(double startingTime, |
| 65 |
int startingState, |
| 66 |
double endingTime, |
| 67 |
double[] lambda, |
| 68 |
int stateCount) { |
| 69 |
throw new RuntimeException("Impossible to simulate an unconditioned CTMC using Uniformization"); |
| 70 |
} |
| 71 |
|
| 72 |
public static StateHistory simulateConditionalOnEndingState(double startingTime, |
| 73 |
int startingState, |
| 74 |
double endingTime, |
| 75 |
int endingState, |
| 76 |
double transitionProbability, |
| 77 |
double[] lambda, |
| 78 |
int stateCount) throws SubordinatedProcess.Exception { |
| 79 |
|
| 80 |
return simulateConditionalOnEndingState(startingTime, startingState, endingTime, endingState, |
| 81 |
transitionProbability, stateCount, new SubordinatedProcess(lambda, stateCount)); |
| 82 |
} |
| 83 |
|
| 84 |
public static StateHistory simulateConditionalOnEndingState(double startingTime, |
| 85 |
int startingState, |
| 86 |
double endingTime, |
| 87 |
int endingState, |
| 88 |
double transitionProbability, |
| 89 |
int stateCount, |
| 90 |
SubordinatedProcess subordinator) throws SubordinatedProcess.Exception { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
StateHistory history = new UniformizedStateHistory(startingTime, startingState, stateCount, subordinator); |
| 95 |
|
| 96 |
double timeDuration = endingTime - startingTime; |
| 97 |
|
| 98 |
int stateChanges = subordinator.drawNumberOfChanges(startingState, endingState, timeDuration, |
| 99 |
transitionProbability); |
| 100 |
|
| 101 |
if (stateChanges == 0) { |
| 102 |
|
| 103 |
} else if (stateChanges == 1) { |
| 104 |
|
| 105 |
if (startingState == endingState) { |
| 106 |
|
| 107 |
} else { |
| 108 |
double transitionTime = (timeDuration) * MathUtils.nextDouble(); |
| 109 |
history.addChange(new StateChange(startingTime + transitionTime, endingState)); |
| 110 |
} |
| 111 |
} else { |
| 112 |
|
| 113 |
double[] transitionTimes = subordinator.drawTransitionTimes(timeDuration, stateChanges); |
| 114 |
int currentState = startingState; |
| 115 |
for (int i = 1; i < stateChanges; i++) { |
| 116 |
int nextState = subordinator.drawNextChainState(currentState, endingState, stateChanges, i); |
| 117 |
if (nextState != currentState) { |
| 118 |
history.addChange(new StateChange(startingTime + transitionTimes[i-1], nextState)); |
| 119 |
currentState = nextState; |
| 120 |
} |
| 121 |
} |
| 122 |
if (currentState != endingState) { |
| 123 |
history.addChange(new StateChange(startingTime + transitionTimes[stateChanges-1], endingState)); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
history.addEndingState(new StateChange(endingTime, endingState)); |
| 128 |
return history; |
| 129 |
} |
| 130 |
|
| 131 |
final private SubordinatedProcess subordinator; |
| 132 |
} |