(This article was first published on

**stotastic » R**, and kindly contributed to R-bloggers)In 1827, the botanist Robert Brown was studying pollen particles as they floated in water. When viewed through a microscope, he observed that the particles seemed to move around as if the were alive. Although he couldn’t have known at the time, the seemingly random motion was caused by the collision of water molecules against the pollen particle. Later on, the random motion he observed would be given the name ‘Brownian Motion’.

# Simulating Brownian Motion

We can model what Brown may have seen by simulating a two dimensional Brownian Motion. Executing the following code in R will produce a chart as if we had recorded the location of the pollen particle every minute (or some other arbitrary time interval) and connected the points in sequence.

n <- 100 y <- rep(0,n) x <- rep(0,n) for(i in 2:n){ y[i] <- y[i-1] + rnorm(1,0,1) x[i] <- x[i-1] + rnorm(1,0,1) } plot(x,y, type="l", col="blue", ylim=c(-10,10), xlim=c(-10,10))

To

**leave a comment**for the author, please follow the link and comment on their blog:**stotastic » R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...