| 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.evolution.datatype.DataType; |
| 29 |
import dr.inference.markovjumps.MarkovJumpsCore; |
| 30 |
import dr.inference.markovjumps.MarkovJumpsType; |
| 31 |
import dr.inference.markovjumps.StateHistory; |
| 32 |
import dr.inference.model.AbstractModel; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class MarkovJumpsSubstitutionModel extends AbstractModel { |
| 49 |
|
| 50 |
public MarkovJumpsSubstitutionModel(SubstitutionModel substModel) { |
| 51 |
this(substModel, MarkovJumpsType.COUNTS); |
| 52 |
} |
| 53 |
|
| 54 |
public MarkovJumpsSubstitutionModel(SubstitutionModel substModel, MarkovJumpsType type) { |
| 55 |
super(substModel.getModelName()); |
| 56 |
this.substModel = substModel; |
| 57 |
this.eigenDecomposition = substModel.getEigenDecomposition(); |
| 58 |
stateCount = substModel.getDataType().getStateCount(); |
| 59 |
markovJumpsCore = new MarkovJumpsCore(stateCount); |
| 60 |
this.type = type; |
| 61 |
setupStorage(); |
| 62 |
addModel(substModel); |
| 63 |
dataType = substModel.getDataType(); |
| 64 |
} |
| 65 |
|
| 66 |
protected void setupStorage() { |
| 67 |
rateMatrix = new double[stateCount * stateCount]; |
| 68 |
transitionProbs = new double[stateCount * stateCount]; |
| 69 |
rateReg = new double[stateCount * stateCount]; |
| 70 |
if (PRECOMPUTE) { |
| 71 |
ievcRateRegEvec = new double[stateCount * stateCount]; |
| 72 |
tmp1 = new double[stateCount * stateCount]; |
| 73 |
} |
| 74 |
registration = new double[stateCount * stateCount]; |
| 75 |
reward = new double[stateCount]; |
| 76 |
} |
| 77 |
|
| 78 |
public MarkovJumpsType getType() { |
| 79 |
return type; |
| 80 |
} |
| 81 |
|
| 82 |
public void setRegistration(double[] inRegistration) { |
| 83 |
|
| 84 |
if (type == MarkovJumpsType.COUNTS || type == MarkovJumpsType.HISTORY) { |
| 85 |
|
| 86 |
System.arraycopy(inRegistration, 0, registration, 0, stateCount * stateCount); |
| 87 |
for (int i = 0; i < stateCount; i++) { |
| 88 |
registration[i * stateCount + i] = 0; |
| 89 |
} |
| 90 |
|
| 91 |
} else if (type == MarkovJumpsType.REWARDS) { |
| 92 |
|
| 93 |
int index = 0; |
| 94 |
for (int i = 0; i < stateCount; i++) { |
| 95 |
reward[i] = inRegistration[i]; |
| 96 |
for (int j = 0; j < stateCount; j++) { |
| 97 |
if (i == j) { |
| 98 |
registration[index] = inRegistration[i]; |
| 99 |
} else { |
| 100 |
registration[index] = 0; |
| 101 |
} |
| 102 |
index++; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
} else { |
| 107 |
throw new RuntimeException("Unknown expectation type in MarkovJumps"); |
| 108 |
} |
| 109 |
regRateChanged = true; |
| 110 |
} |
| 111 |
|
| 112 |
public double[] getRegistration() { |
| 113 |
return registration; |
| 114 |
} |
| 115 |
|
| 116 |
private void makeRateRegistrationMatrix(double[] registration, |
| 117 |
double[] rateReg, |
| 118 |
double[] ievcRateRegEvec) { |
| 119 |
|
| 120 |
if (type == MarkovJumpsType.COUNTS || type == MarkovJumpsType.HISTORY) { |
| 121 |
|
| 122 |
substModel.getInfinitesimalMatrix(rateMatrix); |
| 123 |
int index = 0; |
| 124 |
for (int i = 0; i < stateCount; i++) { |
| 125 |
for (int j = 0; j < stateCount; j++) { |
| 126 |
rateReg[index] = rateMatrix[index] * registration[index]; |
| 127 |
index++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
} else if (type == MarkovJumpsType.REWARDS) { |
| 132 |
|
| 133 |
System.arraycopy(registration, 0, rateReg, 0, stateCount * stateCount); |
| 134 |
|
| 135 |
} else { |
| 136 |
throw new RuntimeException("Unknown expectation type in MarkovJumps"); |
| 137 |
} |
| 138 |
|
| 139 |
if (PRECOMPUTE) { |
| 140 |
|
| 141 |
|
| 142 |
MarkovJumpsCore.matrixMultiply(rateReg, eigenDecomposition.getEigenVectors(), |
| 143 |
stateCount, tmp1); |
| 144 |
MarkovJumpsCore.matrixMultiply(eigenDecomposition.getInverseEigenVectors(), tmp1, |
| 145 |
stateCount, ievcRateRegEvec); |
| 146 |
} |
| 147 |
|
| 148 |
regRateChanged = false; |
| 149 |
} |
| 150 |
|
| 151 |
public double getMarginalRate() { |
| 152 |
|
| 153 |
if (regRateChanged) { |
| 154 |
makeRateRegistrationMatrix(registration, rateReg, ievcRateRegEvec); |
| 155 |
} |
| 156 |
|
| 157 |
FrequencyModel freqModel = substModel.getFrequencyModel(); |
| 158 |
double rate = 0; |
| 159 |
int index = 0; |
| 160 |
for (int i = 0; i < stateCount; i++) { |
| 161 |
double freq_i = freqModel.getFrequency(i); |
| 162 |
for (int j = 0; j < stateCount; j++) { |
| 163 |
rate += freq_i * rateReg[index++]; |
| 164 |
} |
| 165 |
} |
| 166 |
return rate; |
| 167 |
} |
| 168 |
|
| 169 |
public void computeCondStatMarkovJumps(double time, |
| 170 |
double[] countMatrix) { |
| 171 |
|
| 172 |
substModel.getTransitionProbabilities(time, transitionProbs); |
| 173 |
computeCondStatMarkovJumps(time, transitionProbs, countMatrix); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public double getProcessForSimulant(StateHistory history) { |
| 178 |
final double total; |
| 179 |
if (type == MarkovJumpsType.COUNTS || type == MarkovJumpsType.HISTORY) { |
| 180 |
total = history.getTotalRegisteredCounts(registration); |
| 181 |
} else { |
| 182 |
total = history.getTotalReward(reward); |
| 183 |
} |
| 184 |
return total; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
public void computeCondStatMarkovJumps(double time, |
| 189 |
double[] transitionProbs, |
| 190 |
double[] countMatrix) { |
| 191 |
|
| 192 |
if (regRateChanged) { |
| 193 |
makeRateRegistrationMatrix(registration, rateReg, ievcRateRegEvec); |
| 194 |
} |
| 195 |
|
| 196 |
double[] evec = eigenDecomposition.getEigenVectors(); |
| 197 |
double[] ievc = eigenDecomposition.getInverseEigenVectors(); |
| 198 |
double[] eval = eigenDecomposition.getEigenValues(); |
| 199 |
|
| 200 |
if (PRECOMPUTE) { |
| 201 |
markovJumpsCore.computeCondStatMarkovJumpsPrecompute( |
| 202 |
evec, ievc, eval, ievcRateRegEvec, time, transitionProbs, countMatrix); |
| 203 |
} else { |
| 204 |
markovJumpsCore.computeCondStatMarkovJumps(evec, ievc, eval, rateReg, time, transitionProbs, countMatrix); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public void computeJointStatMarkovJumps(double time, |
| 209 |
double[] countMatrix) { |
| 210 |
|
| 211 |
if (regRateChanged) { |
| 212 |
makeRateRegistrationMatrix(registration, rateReg, ievcRateRegEvec); |
| 213 |
} |
| 214 |
|
| 215 |
double[] evec = eigenDecomposition.getEigenVectors(); |
| 216 |
double[] ievc = eigenDecomposition.getInverseEigenVectors(); |
| 217 |
double[] eval = eigenDecomposition.getEigenValues(); |
| 218 |
|
| 219 |
if (PRECOMPUTE) { |
| 220 |
markovJumpsCore.computeJointStatMarkovJumpsPrecompute(evec, ievc, eval, ievcRateRegEvec, time, countMatrix); |
| 221 |
} else { |
| 222 |
markovJumpsCore.computeJointStatMarkovJumps(evec, ievc, eval, rateReg, time, countMatrix); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public SubstitutionModel getSubstitutionModel() { |
| 227 |
return substModel; |
| 228 |
} |
| 229 |
|
| 230 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 231 |
if (model == substModel) { |
| 232 |
regRateChanged = true; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
protected void storeState() { |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
protected void restoreState() { |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
protected void acceptState() { |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
public int stateCount; |
| 253 |
private double[] rateReg; |
| 254 |
private double[] ievcRateRegEvec; |
| 255 |
private double[] tmp1; |
| 256 |
private double[] transitionProbs; |
| 257 |
private double[] rateMatrix; |
| 258 |
protected double[] reward; |
| 259 |
protected double[] registration; |
| 260 |
|
| 261 |
protected SubstitutionModel substModel; |
| 262 |
private EigenDecomposition eigenDecomposition; |
| 263 |
private MarkovJumpsCore markovJumpsCore; |
| 264 |
|
| 265 |
private boolean regRateChanged = true; |
| 266 |
|
| 267 |
protected MarkovJumpsType type; |
| 268 |
protected DataType dataType; |
| 269 |
|
| 270 |
private static final boolean PRECOMPUTE = true; |
| 271 |
} |
| 272 |
|