Debian Codegraph @

src/dr/inference/markovjumps/UniformizedStateHistory.java

raw symbols SCIP
1 /*
2 * UniformizedStateHistory.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.markovjumps;
27
28 import dr.math.MathUtils;
29
30 /**
31 * A class to represent a complete state history of a continuous-time Markov chain in the
32 * interval [0,T] simulated using the Uniformization Method
33 * <p/>
34 * This work is supported by NSF grant 0856099
35 * <p/>
36 * Minin VN and Suchard MA (2008) Counting labeled transitions in continous-time Markov models of evolution.
37 * Journal of Mathematical Biology, 56, 391-412.
38 * <p/>
39 * Rodrigue N, Philippe H and Lartillot N (2006) Uniformization for sampling realizations of Markov processes:
40 * applications to Bayesian implementations of codon substitution models. Bioinformatics, 24, 56-62.
41 * <p/>
42 * Hobolth A and Stone E (2009) Simulation from endpoint-conditioned, continuous-time Markov chains on a finite
43 * state space, with applications to molecular evolution. Annals of Applied Statistics, 3, 1204-1231.
44 *
45 * @author Marc A. Suchard
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 * Algorithm 5
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 // Do nothing
103 } else if (stateChanges == 1) {
104
105 if (startingState == endingState) {
106 // Do nothing, just a single pseudo-transition
107 } else {
108 double transitionTime = (timeDuration) * MathUtils.nextDouble();
109 history.addChange(new StateChange(startingTime + transitionTime, endingState));
110 }
111 } else { // More than one transition; real work to do
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 }