**Computational Biology Blog in fasta format**, and kindly contributed to R-bloggers)

According to Wikipedia the mathematical model for Brownian motion (also known as random walks) can also be used to describe many phenomena as well as the random movements of minute particles,

such as stock market fluctuations and the evolution of physical characteristics in the fossil record. The simple form of the mathematical model for Brownian motion has the form:

S_t = eS_t-1

where e is drawn from a probability distribution.

The source code is here

After loading the source code, there are two functions:

The first one, brownian will plot in an R graphics window the resulting simulation in an animated way.

The second function, export.brownian will export each step of the simulation in independent PNG files.

Example of running:

> source(“brownian.motion.R”)

> brownian(500)

The second function will produce this output

> export.brownian(500)

CODE:

`# *******************************`

# BROWNIAN MOTION SIMULATION

# December 2012 | Benjamin Tovar

# *******************************

#

# REFERENCES

# http://landshape.org/enm/r-code-for-brownian-motion/

#

# According to Wikipedia the mathematical model for Brownian motion

# (also known as random walks) can also be used to describe many

# phenomena as well as the random movements of minute particles,

# such as stock market fluctuations and the evolution of physical

# characteristics in the fossil record. The simple form of the

# mathematical model for Brownian motion has the form:

#

# S_t = eS_t-1

#

# where e is drawn from a probability distribution.

#

#######################################################################

brownian <- function(n.times){

x <- y <- x.new <- y.new <- x.new.p <- y.new.p <- vector()

for(i in 1:n.times){

# Initialize variables

x <- rnorm(1)

y <- rnorm(1)

# concatenate variables

# to increase the vector size

x.new <- c(x.new,x)

y.new <- c(y.new,y)

# sum the vector numbers

x.new.p <- cumsum(x.new)

y.new.p <- cumsum(y.new)

# plot the model

plot(x.new.p,y.new.p,type="b",

main=paste("Brownian motion simulation in R\nTime =",i,sep=" "),

xlab="x coordinates",ylab="y coordinates",

col=c(rep("gray",i-1),"red"),

pch=c(rep(20,i-1),1))

}

}

# Test the function

# brownian(500)

# ****************************************

# EXPORT BROWNIAN MOTION SIMULATION IMAGES

# ****************************************

export.brownian <- function(n.times){

x <- y <- x.new <- y.new <- x.new.p <- y.new.p <- vector()

for(i in 1:n.times){

# Initialize variables

x <- rnorm(1)

y <- rnorm(1)

# concatenate variables to increase the

# vector size

x.new <- c(x.new,x)

y.new <- c(y.new,y)

# sum the vector numbers

x.new.p <- cumsum(x.new)

y.new.p <- cumsum(y.new)

# plot the model

png(paste("image",i,"png",sep="."),width=600,height=600)

plot(x.new.p,y.new.p,type="b",

main=paste("Brownian motion simulation in R\nTime =",

i,sep=" "),

xlab="x coordinates",ylab="y coordinates",

col=c(rep("gray",i-1),"red"),

pch=c(rep(20,i-1),1))

cat("image",i,"DONE",date(),"\n")

dev.off()

}

}

# Test the function

# export.brownian(500)

**leave a comment**for the author, please follow the link and comment on their blog:

**Computational Biology Blog in fasta format**.

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...