Debian Codegraph @

src/dr/geo/MultivariateBrownianBridge.java

raw symbols SCIP
1 /*
2 * MultivariateBrownianBridge.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.LinkedList;
31 import java.util.List;
32
33 /**
34 * @author Alexei Drummond
35 * @author Marc Suchard
36 */
37 public class MultivariateBrownianBridge {
38
39 private static boolean stop;
40
41 /**
42 * Divide and conquer brownian bridge; NOT THREAD-SAFE!!!!!
43 *
44 * @param normal multivariate normal distribution characterizing Brownian bridge
45 * @param start starting spacetime
46 * @param end ending spacetime
47 * @param depth depth of divide and conquer recursion resulting in 2^(depth-1) new points
48 * @param maxTries maximum number of rejections for each level - MUST BE 1 for correct sampling under absorptive boundaries (says AlexeiMarc)
49 * @param rejector invalid space/time rejector
50 * @return list of points, containing 2 + 2^(depth-1) points
51 */
52 public static List<SpaceTime> divideConquerBrownianBridge(MultivariateNormalDistribution normal,
53 SpaceTime start, SpaceTime end,
54 int depth, int maxTries, SpaceTimeRejector rejector) {
55
56 List<SpaceTime> points = new LinkedList<SpaceTime>();
57 points.add(start);
58 points.add(end);
59
60 stop = false;
61 if (divideConquerBrownianBridge(normal, 0, points, depth, maxTries, rejector) == (2 << (depth - 1))) {
62 return points;
63 }
64 stop = false;
65 return null;
66 }
67
68 public static int divideConquerBrownianBridge(MultivariateNormalDistribution normal,
69 int point0, List<SpaceTime> points,
70 int depth, int maxTries, SpaceTimeRejector rejector) {
71
72 if (depth > 0 && !stop) {
73
74 SpaceTime pt0 = points.get(point0);
75 SpaceTime pt1 = points.get(point0 + 1);
76
77 double t0 = pt0.getTime();
78 double[] x0 = pt0.getX();
79
80 double t1 = pt1.getTime();
81 double[] x1 = pt1.getX();
82
83 double tm = (t1 + t0) / 2.0;
84
85 double t0m = tm - t0;
86 double p0m = 1.0 / t0m;
87
88 double tm1 = t1 - tm;
89 double pm1 = 1.0 / tm1;
90
91 // p0m + pm1 = precision of pt at tm,
92 double v01 = 1.0 / (p0m + pm1);
93
94
95 // System.err.println("p0m = " + p0m);
96 // System.err.println("pm1 = " + pm1);
97 // System.err.println("v01 = " + v01);
98
99 final int dim = x0.length;
100 double[] xm = new double[dim];
101 for (int i = 0; i < dim; i++) {
102 xm[i] = (p0m * x0[i] + pm1 * x1[i]) * v01;
103 }
104
105 // System.err.println("Mean: " + new Vector(xm));
106
107
108 int tries = 0;
109
110 SpaceTime s;
111 do {
112 s = new SpaceTime(tm, normal.nextScaledMultivariateNormal(xm, v01));
113 tries += 1;
114 if (tries > maxTries) {
115 stop = true;
116 return 0;
117 }
118 } while (rejector != null && rejector.reject(s.time, s.space));
119
120 points.add(point0 + 1, s);
121
122 int endPoint = divideConquerBrownianBridge(normal, point0, points, depth - 1, maxTries, rejector);
123 return divideConquerBrownianBridge(normal, endPoint, points, depth - 1, maxTries, rejector);
124
125 } else return point0 + 1;
126 }
127 }