Table of Contents

Brownian Motion

The function brownian.motion() has illustrated the phenomenon of Random Walk on the 2D plane. For a point (x, y), its next location is (x + rnorm(1), y + rnorm(1)).

R code

library(animation)
saveHTML({
    ani.options(interval = 0.05, nmax = 100)
    par(mar = c(3, 3, 1, 0.5), mgp = c(2, 0.5, 0), tcl = -0.3, 
        cex.axis = 0.8, cex.lab = 0.8, cex.main = 1)
    brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow")
}, img.name = "brownian_motion", htmlfile = "brownian_motion.html", 
    ani.width = 500, ani.height = 500, title = "Demonstration of Brownian Motion", 
    description = c("Random walk on the 2D plane: for each point (x, y), x = x + rnorm(1) and y = y + rnorm(1)."))
	

Animation

The animation is like this:

Random walk on the 2D plane: for each point (x, y), x = x + rnorm(1) and y = y + rnorm(1). </ani>

Other resources

Reminder to lattice users

If you use the lattice package, don't forget to print() your plots, otherwise graphics devices like png() will not be able to record your images. Here is an example:

library(animation)
library(lattice)
saveHTML({(bm.mat = data.frame(x = rnorm(10), y = rnorm(10)))
    ani.options(interval = 0.05, nmax = 100,)
	## Brownian Motion in lattice
	for (i in 1:ani.options("nmax")) {
    print(xyplot(y ~ x, bm.mat, xlim = c(-10, 10), ylim = c(-10, 10), pch = 19))
    bm.mat = bm.mat + data.frame(x = rnorm(10), y = rnorm(10))
	}},img.name="Brownian_Motion", hltmfile="Brownian_Motion.html",
	title = "Demonstration of Brownian Motion",
    description = c("Random walk on the 2D plane: for each point (x, y), 
    x = x + rnorm(1) and y = y + rnorm(1)."))