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:

St = eSt-1

where e is drawn from a probability distribution. My initial implementation of Brownian motion in R and 2 dimensions is this:

brown1.png

brownian< -function(n=1000,plot=T,fun=rnorm) {
	x<-cumsum(fun(n))
	y<-cumsum(fun(n))
	plot(x,y,type="l")
	}
brownian(fun=rnorm)

The code above makes use of the R feature of taking functions as parameters. In this case the Brownian motion is generated using the normal distribution rnorm. Random numbers can be generated in R from the following built in functions:

runif, rpois, rmvnorm, rnbinom, rbinom, rbeta, rchisq, rexp, rgamma, rlogis, rstab, rt, rgeom, rhyper, rwilcox, rweibull.

However if I wanted to generate Brownian motion with another distribution such a ‘power tail’ I need to change the function to handle the parameters. For example, the t distribution with df = n degrees of freedom has the following code and plot:

brown2.png

brownian< -function(n=1000,plot=T,fun=rnorm,df=1) {
	x<-cumsum(fun(n,df))
	y<-cumsum(fun(n,df))
	plot(x,y,type="l")
	}
brownian(fun=rt)

The limitations are clearly seen on the plot, as the ‘longtail flights’ tend to be in an x or y direction. Anyone like to generalize this code to properly handle any distribution in 2 dimensions?

Advertisements

0 thoughts on “R Code for Brownian Motion

  1. Hello! your code was very useful for my own implementation of a brownian motion simulation in R, here’s mine <- http://tata-box-blog.blogspot.mx/2012/12/brownian-motion-simulation-in-r.html  and thank you for the code. Benjamin

  2. Pingback: link

  3. Pingback: livecooking

  4. Pingback: strona www

  5. Pingback: kliknij

  6. Pingback: witryna

  7. Pingback: strona firmy

  8. Pingback: strona firmy

  9. Pingback: oferta

  10. Pingback: link

  11. Pingback: uslugi informatyczne katowice

  12. Pingback: Clowns

  13. Pingback: tutaj

  14. Pingback: sufity armstrong

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s