Debian Codegraph @

src/dr/geo/SpaceTimeSimulator.java

raw symbols SCIP
1 /*
2 * SpaceTimeSimulator.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.geo;
27
28 import dr.math.distributions.MultivariateNormalDistribution;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 /**
34 * @author Alexei Drummond
35 */
36 public class SpaceTimeSimulator {
37
38 // the diffusion kernel
39 MultivariateNormalDistribution D;
40
41 public SpaceTimeSimulator(MultivariateNormalDistribution D) {
42 this.D = D;
43 }
44
45 /**
46 * @param start the start time and location
47 * @param rejector
48 * @param dt the time steps
49 * @param steps the number of steps
50 * @return a path in space starting at start and continuing to time t = start.time + dt*steps
51 */
52 public List<SpaceTime> simulatePath(SpaceTime start, SpaceTimeRejector rejector, double dt, int steps) {
53
54 ArrayList<SpaceTime> list = new ArrayList<SpaceTime>();
55 list.add(start);
56 for (int i = 0; i < steps; i++) {
57
58 SpaceTime lastSpaceTime = list.get(list.size() - 1);
59 SpaceTime newST;
60 do {
61 double[] newPoint = new double[start.getX().length];
62 D.nextScaledMultivariateNormal(lastSpaceTime.getX(), dt, newPoint);
63 newST = new SpaceTime(lastSpaceTime.getTime() + dt, newPoint);
64 } while (rejector.reject(newST.time, newST.space));
65 list.add(newST);
66 }
67 return list;
68 }
69
70 /**
71 * @param spaceTime the start time and location
72 * @param rejector
73 * @param dt the time steps
74 * @param steps the number of steps
75 * @return a path in space starting at start and continuing to time t = start.time + dt*steps
76 */
77 public SpaceTime simulate(SpaceTime spaceTime, SpaceTimeRejector rejector, double dt, int steps) {
78
79 SpaceTime newST = new SpaceTime(spaceTime);
80 SpaceTime nextST = new SpaceTime(spaceTime);
81 for (int i = 0; i < steps; i++) {
82
83 do {
84 D.nextScaledMultivariateNormal(nextST.getX(), dt, newST.space);
85 newST.time = nextST.getTime() + dt;
86 } while (rejector.reject(newST.time, newST.space));
87 nextST.time = newST.time;
88 nextST.space = newST.space;
89 }
90 return nextST;
91 }
92
93 /**
94 * @param spaceTime the start time and location
95 * @param rejector
96 * @param dt the time steps
97 * @param steps the number of steps
98 * @return a path in space starting at start and continuing to time t = start.time + dt*steps,
99 * conditional on never encountering a rejection area
100 */
101 public SpaceTime simulateAbsorbing(SpaceTime spaceTime, SpaceTimeRejector rejector, double dt, int steps) {
102
103 int i = 0;
104 boolean found = false;
105 boolean reject = false;
106 SpaceTime nextST = null;
107 while (!found) {
108 SpaceTime newST = new SpaceTime(spaceTime);
109 nextST = new SpaceTime(spaceTime);
110 while (i < steps && !reject) {
111 D.nextScaledMultivariateNormal(nextST.getX(), dt, newST.space);
112 newST.time = nextST.getTime() + dt;
113 reject = rejector.reject(newST.time, newST.space);
114 nextST.time = newST.time;
115 nextST.space = newST.space;
116 i += 1;
117 }
118 if (!reject) found = true;
119 }
120 return nextST;
121 }
122 }