conv3d
conv3d

Reputation: 2896

Random walk in R

As per request, pseudocode (I am not a coder, so not good pseudocode here), but,

  1. Choose random coordinate in 1x1 square.
  2. for (i in 1:iterations)
  3. Check to see if coordinates inside 1x1 square.
  4. If it is outside the square repeat with previous values of coordinates.
  5. Check to see if coordinates fall in the circle inscribed in 1x1 square.
  6. If it is, +1 to inside counter.
  7. If it isn't, +1 to outside counter.
  8. Add value of epsilon in any direction, and repeat.

Finally, pi_est <- (inside/(inside + outside))*4

Upvotes: 0

Views: 359

Answers (1)

Glen_b
Glen_b

Reputation: 8252

As I suggested might be the issue in comments -- with the random walk scheme you're using, I believe you're not going to visit each square equally often... and that is going to make your estimate inaccurate (biased).

I suggest you set up a small grid (say 5x5) and run it a long time, then I'd suggest accumulating the results for the six different cell types:

C E M E C   (Corner,   Edge,     Midedge, 
E D O D E            Diagonal,  Orthogonal, 
M O X O M                        Xenter)
E D O D E
C E M E C

(Since - by symmetry - any differences within those types should be noise-only).

Then (scaling for the fact that the respective counts of each type of cell are 4,8,4,4,4,1) you should be able to see whether they're visited in the right proportions (16%,32% and 4% for 4-, 8- or 1- counts respectively). I believe what you'll find is that the boundary cells are visited more often (relative to their fair proportion) than the middle.

One suggestion would be to "wrap" the edges to that the top and bottom edges and the left and right edges communicate. This avoids the "bounceback" at the edge and instead makes every cell like every other cell; by symmetry you should get equal probability for each cell.

Upvotes: 2

Related Questions