| 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.distributions.MultivariateNormalDistribution; |
| 29 |
|
| 30 |
import java.awt.geom.Point2D; |
| 31 |
import java.awt.geom.Rectangle2D; |
| 32 |
import java.io.FileNotFoundException; |
| 33 |
import java.io.PrintWriter; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NumericalSpaceTimeProbs2D { |
| 39 |
|
| 40 |
final int latticeWidth; |
| 41 |
final int latticeHeight; |
| 42 |
final int tsteps; |
| 43 |
final int subtsteps; |
| 44 |
final double minx, miny, dx, dy, dt; |
| 45 |
final MultivariateNormalDistribution D; |
| 46 |
final SpaceTimeRejector rejector; |
| 47 |
|
| 48 |
int[][][][][] counts; |
| 49 |
int[][][] normalization; |
| 50 |
int[][][] maxCount; |
| 51 |
|
| 52 |
public NumericalSpaceTimeProbs2D( |
| 53 |
int latticeWidth, |
| 54 |
int latticeHeight, |
| 55 |
int tsteps, |
| 56 |
int subtsteps, |
| 57 |
double dt, |
| 58 |
Rectangle2D bounds, |
| 59 |
MultivariateNormalDistribution distribution, |
| 60 |
SpaceTimeRejector rejector) { |
| 61 |
|
| 62 |
this.latticeWidth = latticeWidth; |
| 63 |
this.latticeHeight = latticeHeight; |
| 64 |
this.tsteps = tsteps; |
| 65 |
this.subtsteps = subtsteps; |
| 66 |
this.D = distribution; |
| 67 |
this.rejector = rejector; |
| 68 |
this.dt = dt; |
| 69 |
|
| 70 |
minx = bounds.getMinX(); |
| 71 |
miny = bounds.getMinY(); |
| 72 |
|
| 73 |
dx = (bounds.getMaxX() - minx) / latticeWidth; |
| 74 |
dy = (bounds.getMaxY() - miny) / latticeHeight; |
| 75 |
|
| 76 |
counts = new int[latticeWidth][latticeHeight][latticeWidth][latticeHeight][tsteps]; |
| 77 |
normalization = new int[latticeWidth][latticeHeight][tsteps]; |
| 78 |
maxCount = new int[latticeWidth][latticeHeight][tsteps]; |
| 79 |
} |
| 80 |
|
| 81 |
public void populate(Point2D start, int paths, boolean includeSubpaths) { |
| 82 |
|
| 83 |
populate(x(start.getX()), y(start.getY()), paths, includeSubpaths); |
| 84 |
} |
| 85 |
|
| 86 |
public int populateAbsorbing(Point2D start, int paths) { |
| 87 |
return populateAbsorbing(x(start.getX()), y(start.getY()), paths); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public int populateAbsorbing(int i, int j, int paths) { |
| 97 |
|
| 98 |
double subdt = dt / (double) subtsteps; |
| 99 |
|
| 100 |
double[] next = new double[2]; |
| 101 |
double[] start = new double[2]; |
| 102 |
int[] pathx = new int[tsteps]; |
| 103 |
int[] pathy = new int[tsteps]; |
| 104 |
int successes = 0; |
| 105 |
|
| 106 |
for (int reps = 0; reps < paths; reps += 1) { |
| 107 |
|
| 108 |
double time = 0.0; |
| 109 |
start[0] = (i + Math.random()) * dx + minx; |
| 110 |
start[1] = (j + Math.random()) * dy + miny; |
| 111 |
while (rejector.reject(0, start)) { |
| 112 |
start[0] = (i + Math.random()) * dx + minx; |
| 113 |
start[1] = (j + Math.random()) * dy + miny; |
| 114 |
} |
| 115 |
|
| 116 |
boolean reject = false; |
| 117 |
for (int t = 0; t < tsteps && !reject; t++) { |
| 118 |
|
| 119 |
for (int s = 0; s < subtsteps && !reject; s++) { |
| 120 |
|
| 121 |
D.nextScaledMultivariateNormal(start, subdt, next); |
| 122 |
time += subdt; |
| 123 |
reject = rejector.reject(time, next); |
| 124 |
|
| 125 |
if (!reject) { |
| 126 |
start[0] = next[0]; |
| 127 |
start[1] = next[1]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (!reject) { |
| 132 |
pathx[t] = x(next[0]); |
| 133 |
pathy[t] = y(next[1]); |
| 134 |
|
| 135 |
increment(i, j, pathx[t], pathy[t], t); |
| 136 |
} |
| 137 |
} |
| 138 |
if (!reject) successes += 1; |
| 139 |
|
| 140 |
if (reps % 10000 == 0) { |
| 141 |
System.out.print("."); |
| 142 |
System.out.flush(); |
| 143 |
} |
| 144 |
} |
| 145 |
System.out.println(); |
| 146 |
return successes; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public void populate(int i, int j, int paths, boolean includeSubpaths) { |
| 151 |
|
| 152 |
double subdt = dt / (double) subtsteps; |
| 153 |
|
| 154 |
double[] next = new double[2]; |
| 155 |
double[] start = new double[2]; |
| 156 |
int[] pathx = new int[tsteps]; |
| 157 |
int[] pathy = new int[tsteps]; |
| 158 |
|
| 159 |
for (int reps = 0; reps < paths; reps += 1) { |
| 160 |
|
| 161 |
double time = 0.0; |
| 162 |
start[0] = (i + Math.random()) * dx + minx; |
| 163 |
start[1] = (j + Math.random()) * dy + miny; |
| 164 |
while (rejector.reject(0, start)) { |
| 165 |
start[0] = (i + Math.random()) * dx + minx; |
| 166 |
start[1] = (j + Math.random()) * dy + miny; |
| 167 |
} |
| 168 |
|
| 169 |
for (int t = 0; t < tsteps; t++) { |
| 170 |
|
| 171 |
for (int s = 0; s < subtsteps; s++) { |
| 172 |
|
| 173 |
do { |
| 174 |
D.nextScaledMultivariateNormal(start, subdt, next); |
| 175 |
time += subdt; |
| 176 |
} while (rejector.reject(time, next)); |
| 177 |
|
| 178 |
start[0] = next[0]; |
| 179 |
start[1] = next[1]; |
| 180 |
} |
| 181 |
|
| 182 |
pathx[t] = x(next[0]); |
| 183 |
pathy[t] = y(next[1]); |
| 184 |
|
| 185 |
increment(i, j, pathx[t], pathy[t], t); |
| 186 |
} |
| 187 |
|
| 188 |
if (includeSubpaths) { |
| 189 |
for (int t = 0; t < tsteps; t++) { |
| 190 |
for (int s = t + 1; s < tsteps; s++) { |
| 191 |
increment(pathx[t], pathy[t], pathx[s], pathy[s], s - t - 1); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (reps % 1000 == 0) { |
| 197 |
System.out.print("."); |
| 198 |
System.out.flush(); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
private void increment(int i, int j, int k, int l, int t) { |
| 204 |
counts[i][j][k][l][t] += 1; |
| 205 |
normalization[i][j][t] += 1; |
| 206 |
|
| 207 |
if (counts[i][j][k][l][t] > maxCount[i][j][t]) { |
| 208 |
maxCount[i][j][t] = counts[i][j][k][l][t]; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
public void populate(int paths) { |
| 213 |
|
| 214 |
System.out.println("Populating numerical transition probabilities"); |
| 215 |
|
| 216 |
for (int i = 0; i < latticeWidth; i++) { |
| 217 |
|
| 218 |
for (int j = 0; j < latticeHeight; j++) { |
| 219 |
|
| 220 |
populate(i, j, paths, true); |
| 221 |
} |
| 222 |
System.out.print("."); |
| 223 |
System.out.flush(); |
| 224 |
} |
| 225 |
|
| 226 |
System.out.println(latticeWidth * latticeHeight * paths + " new paths computed."); |
| 227 |
} |
| 228 |
|
| 229 |
public final int x(double x) { |
| 230 |
return (int) ((x - minx) / dx); |
| 231 |
} |
| 232 |
|
| 233 |
public final int y(double y) { |
| 234 |
return (int) ((y - miny) / dy); |
| 235 |
} |
| 236 |
|
| 237 |
public final int t(double time) { |
| 238 |
return (int) (time / dt); |
| 239 |
} |
| 240 |
|
| 241 |
public double getProb(Point2D start, Point2D end, double time) { |
| 242 |
|
| 243 |
int i = x(start.getX()); |
| 244 |
int j = x(start.getY()); |
| 245 |
int k = x(end.getX()); |
| 246 |
int l = x(end.getY()); |
| 247 |
|
| 248 |
if (time > tsteps * dt) { |
| 249 |
System.err.println("Time = " + time + ", max time estimated is " + tsteps * dt); |
| 250 |
return (double) counts[i][j][k][l][tsteps - 1] / normalization[i][j][tsteps - 1]; |
| 251 |
} else { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
int t = t(time); |
| 256 |
double tlow = t * dt; |
| 257 |
double thigh = tlow + dt; |
| 258 |
|
| 259 |
double weightlow = (thigh - time) / dt; |
| 260 |
|
| 261 |
return weightlow * p(i, j, k, l, t) + (1.0 - weightlow) * p(i, j, k, l, t + 1); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public double p(int i, int j, int k, int l, int t) { |
| 266 |
return (double) counts[i][j][k][l][t] / (double) normalization[i][j][t]; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public double r(int i, int j, int k, int l, int t) { |
| 271 |
return (double) counts[i][j][k][l][t] / (double) maxCount[i][j][t]; |
| 272 |
} |
| 273 |
|
| 274 |
public void writeToFile(String s) throws FileNotFoundException { |
| 275 |
|
| 276 |
PrintWriter writer = new PrintWriter(s); |
| 277 |
|
| 278 |
writer.write("xsteps=" + latticeWidth + "\n"); |
| 279 |
writer.write("ysteps=" + latticeHeight + "\n"); |
| 280 |
writer.write("tsteps=" + tsteps + "\n"); |
| 281 |
writer.write("dx=" + dx + "\n"); |
| 282 |
writer.write("dy=" + dy + "\n"); |
| 283 |
writer.write("dt=" + dt + "\n"); |
| 284 |
writer.write("minx=" + minx + "\n"); |
| 285 |
writer.write("miny=" + miny + "\n"); |
| 286 |
writer.write("D=" + matrixString()); |
| 287 |
|
| 288 |
for (int i = 0; i < latticeWidth; i++) { |
| 289 |
for (int j = 0; j < latticeHeight; j++) { |
| 290 |
for (int k = 0; k < latticeWidth; k++) { |
| 291 |
for (int l = 0; l < latticeHeight; l++) { |
| 292 |
for (int t = 0; t < tsteps; t++) { |
| 293 |
|
| 294 |
writer.write(i + "\t" + j + "\t" + k + "\t" + l + "\t" + t + "\t" + counts[i][j][k][l][t] + "\n"); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
writer.close(); |
| 301 |
} |
| 302 |
|
| 303 |
private String matrixString() { |
| 304 |
|
| 305 |
double[][] m = D.getScaleMatrix(); |
| 306 |
StringBuilder builder = new StringBuilder(); |
| 307 |
builder.append("["); |
| 308 |
for (int i = 0; i < m.length; i++) { |
| 309 |
builder.append("["); |
| 310 |
builder.append(m[i][0]); |
| 311 |
for (int j = 1; j < m[i].length; j++) { |
| 312 |
builder.append("," + m[i][0]); |
| 313 |
} |
| 314 |
builder.append("]"); |
| 315 |
|
| 316 |
} |
| 317 |
builder.append("]"); |
| 318 |
return builder.toString(); |
| 319 |
} |
| 320 |
|
| 321 |
public static void main(String[] args) throws FileNotFoundException { |
| 322 |
|
| 323 |
Rectangle2D bounds = new Rectangle2D.Double(0, 0, 1, 1); |
| 324 |
|
| 325 |
MultivariateNormalDistribution D = |
| 326 |
new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{1, 0}, {0, 1}}); |
| 327 |
|
| 328 |
NumericalSpaceTimeProbs2D nstp2D = |
| 329 |
new NumericalSpaceTimeProbs2D(50, 50, 50, 1, 0.02, bounds, D, SpaceTimeRejector.Utils.createSimpleBounds2D(bounds)); |
| 330 |
|
| 331 |
|
| 332 |
long startTime = System.currentTimeMillis(); |
| 333 |
nstp2D.populate(0, 0, 1000, true); |
| 334 |
long stopTime = System.currentTimeMillis(); |
| 335 |
System.out.println("Time taken = " + (stopTime - startTime) / 1000 + " seconds"); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
for (int i = 0; i < 10; i++) { |
| 342 |
|
| 343 |
Point2D start = new Point2D.Double(Math.random(), Math.random()); |
| 344 |
Point2D end = new Point2D.Double(Math.random(), Math.random()); |
| 345 |
double time = Math.random(); |
| 346 |
|
| 347 |
double p = nstp2D.getProb(start, end, time); |
| 348 |
|
| 349 |
System.out.println("Pr(" + end.getX() + ", " + end.getY() + " | " + start.getX() + ", " + start.getY() + ", t=" + time + ") = " + p); |
| 350 |
} |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
} |