| 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.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 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BrownianBridge1D { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
} |