Vistat

A reproducible gallery of statistical graphics

Contact

Brownian Motion with R

We can use the animation package to produce animationsin R. This short article shows you how to create a Brownian motion with the brownian.motion() function.

Simply speaking, a Brownian motion shows the trace of the coordinates

\[x_{i+1}=x_{i}+\epsilon_{i+1}\]

where \(\epsilon_i\) is i.i.d from a standard Normal distribution. That is fairly easy to program in R -- it is nothing but cumsum(rnorm(n)), and that is what brownian.motion() does internally.

opts_knit$set(animation.fun = hook_scianimator)
library(animation)
ani.options(nmax = 50)  # create 50 image frames
set.seed(20121106)
brownian.motion(n = 20, pch = 21, cex = 4, col = "red", bg = "yellow", xlim = c(-10, 
    10), ylim = c(-15, 15))

Done.