[R] simple loop problemo (Geo brownian motion)
I would like to plot multiple random walks onto the same graph. My p variable dictates how may random walks there will be. par(mfrow=c(1,1)) p - 100 N - 1000 S0 - 10 mu - 0.03 sigma - 0.2 nu - mu-sigma^2/2 x - matrix(rep(0,(N+1)*p),nrow=(N+1)) y - matrix(rep(0,(N+1)*p),nrow=(N+1)) t- (c(0:N))/N for (j in 1:p) { z - rnorm(N,0,1) x[1,j] - 0 y[1,j] - S0 for (i in 1:N) { x[i+1,j] - (1/sqrt(N))*sum(z[1:i]) y[i+1,j] - y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j]) } plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion) } Any help would be appreciated, thanks. -- View this message in context: http://r.789695.n4.nabble.com/simple-loop-problemo-Geo-brownian-motion-tp3050762p3050762.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simple loop problemo (Geo brownian motion)
On 19/11/2010 1:09 PM, newbster wrote: I would like to plot multiple random walks onto the same graph. My p variable dictates how may random walks there will be. par(mfrow=c(1,1)) p- 100 N- 1000 S0- 10 mu- 0.03 sigma- 0.2 nu- mu-sigma^2/2 x- matrix(rep(0,(N+1)*p),nrow=(N+1)) y- matrix(rep(0,(N+1)*p),nrow=(N+1)) t- (c(0:N))/N for (j in 1:p) { z- rnorm(N,0,1) x[1,j]- 0 y[1,j]- S0 for (i in 1:N) { x[i+1,j]- (1/sqrt(N))*sum(z[1:i]) y[i+1,j]- y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j]) } plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion) } Any help would be appreciated, thanks. You can use the matplot function to plot multiple columns of a matrix at once. So with your code as above, leave out the call to plot(), then matplot(t, y) would give you approximately what you're looking for (though the colour and symbol choices need some work). You can get rid of the inner loop by using cumsum() and vectorized calculations; then it will be reasonably fast. Another way to do this is to create the plot before the loop, then use lines() to add lines to it within the loop. Then you need to guess the scale ahead of time, or do a little experimentation. Duncan Murdoch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simple loop problemo (Geo brownian motion)
Maybe you can consider the brownian.motion() function in the animation package, e.g. library(animation) ani.options(interval = 0.05, nmax = 150) brownian.motion(pch = 21, cex = 5, col = red, bg = yellow, main = Demonstration of Brownian Motion) Or here is another (awkward) example of several parallel groups of random walks: https://github.com/hadley/ggplot2/wiki/Using-ggplot2-animations-to-demonstrate-several-parallel-numerical-experiments-in-a-single-layout Regards, Yihui -- Yihui Xie xieyi...@gmail.com Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Fri, Nov 19, 2010 at 12:09 PM, newbster gpm8...@yahoo.com wrote: I would like to plot multiple random walks onto the same graph. My p variable dictates how may random walks there will be. par(mfrow=c(1,1)) p - 100 N - 1000 S0 - 10 mu - 0.03 sigma - 0.2 nu - mu-sigma^2/2 x - matrix(rep(0,(N+1)*p),nrow=(N+1)) y - matrix(rep(0,(N+1)*p),nrow=(N+1)) t- (c(0:N))/N for (j in 1:p) { z - rnorm(N,0,1) x[1,j] - 0 y[1,j] - S0 for (i in 1:N) { x[i+1,j] - (1/sqrt(N))*sum(z[1:i]) y[i+1,j] - y[1,j]*exp(nu*t[i+1]+sigma*x[i+1,j]) } plot(t,y,type=l,xlab=time, ylab=Geometric Brownian motion) } Any help would be appreciated, thanks. -- View this message in context: http://r.789695.n4.nabble.com/simple-loop-problemo-Geo-brownian-motion-tp3050762p3050762.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.