Debian Codegraph @

src/dr/geo/BrownianBridge1D.java

raw symbols SCIP
1 /*
2 * BrownianBridge1D.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.MathUtils;
29
30 import java.awt.geom.Point2D;
31 import java.util.LinkedList;
32 import java.util.List;
33
34 /**
35 * @author Alexei Drummond
36 */
37
38 public class BrownianBridge1D {
39
40 /**
41 * Divide and conquer brownian bridge
42 *
43 * @param t0 starting time
44 * @param t1 end time
45 * @param y0 starting value
46 * @param y1 ending value
47 * @param depth depth of divide and conquer recursion resulting in 2^(depth-1) new points
48 * @return list of points, containing 2 + 2^(depth-1) points
49 */
50 public static List<Point2D> divideConquerBrownianBridge(double D, double t0, double y0, double t1, double y1, int depth, SpaceTimeRejector1D rejector) {
51
52 return divideConquerBrownianBridge(D, new Point2D.Double(t0, y0), new Point2D.Double(t1, y1), depth, rejector);
53 }
54
55
56 /**
57 * Divide and conquer brownian bridge
58 *
59 * @param t0y0 starting point in time and 1d space
60 * @param t1y1 end point in time and 1d space
61 * @param depth depth of divide and conquer recursion resulting in 2^(depth-1) new points
62 * @return list of points, containing 2 + 2^(depth-1) points
63 */
64 public static List<Point2D> divideConquerBrownianBridge(double D, Point2D t0y0, Point2D t1y1, int depth, SpaceTimeRejector1D rejector) {
65
66 List<Point2D> points = new LinkedList<Point2D>();
67 points.add(t0y0);
68 points.add(t1y1);
69
70 divideConquerBrownianBridge(D, 0, points, depth, rejector);
71 return points;
72 }
73
74 public static int divideConquerBrownianBridge(double D, int point0, List<Point2D> points, int depth, SpaceTimeRejector1D rejector) {
75
76 if (depth > 0) {
77
78 Point2D p0 = points.get(point0);
79 Point2D p1 = points.get(point0 + 1);
80
81 double t0 = p0.getX();
82 double y0 = p0.getY();
83
84 double t1 = p1.getX();
85 double y1 = p1.getY();
86
87 double tm = (t1 - t0) / 2.0 + t0;
88
89 double mean = y0 + 0.5 * (y1 - y0);
90 double stdev = Math.sqrt(D * (t1 - t0) / 4.0);
91
92 double xm = MathUtils.nextGaussian() * stdev + mean;
93 if (rejector != null) {
94 while (rejector.reject(tm, xm)) {
95 xm = MathUtils.nextGaussian() * stdev + mean;
96 }
97 }
98
99 points.add(point0 + 1, new Point2D.Double(tm, xm));
100
101 int endPoint = divideConquerBrownianBridge(D, point0, points, depth - 1, rejector);
102 return divideConquerBrownianBridge(D, endPoint, points, depth - 1, rejector);
103
104 } else return point0 + 1;
105 }
106
107 public static void main(String[] args) {
108
109 List<Point2D> points = divideConquerBrownianBridge(1, 0, 0, 4, 0, 5, null);
110
111 for (Point2D p : points) {
112 System.out.println(p.getX() + "\t" + p.getY());
113 }
114 }
115
116 }