2D Random walk

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

2D Random walk

featherbox
I've wrote some code to simulate a random walk in 2 dimensions on a lattice. Basically I want to add something in to make it plot it point by point so you can see what is going on.
Heres my code for the random walk in 2d


RW2D<-function(N)
  {
    i<-0
    xdir<-0
    ydir<-0
    xpos<-vector()
    xpos[1]<-xdir
    ypos<-vector()
    ypos[1]<-ydir
    for (i in 1:N-1)
      {
        r<-runif(1)
        if(r<=0.25) {xdir<-xdir+1}
        if(r>0.25 && r<=0.5) {xdir<-xdir-1}
        if(r>0.5 && r<=0.75) {ydir<-ydir +1}
        if(r>0.75) {ydir<-ydir-1}
      xpos[i+1]<-xdir
      ypos[i+1]<-ydir
      }
    return(cbind(xpos,ypos))
  }
rw<-RW2D(10000)

xmin<-min(rw[,1])
xmax<-max(rw[,1])
ymin<-min(rw[,2])
ymax<-max(rw[,2])

plot(rw[,1],rw[,2],type="l",xlab="x",ylab="y",main="Random Walk Simulation In Two Dimensions",col="green4",xlim=range(xmin:xmax),ylim=range(ymin:ymax))

end<-cbind(rw[10000,1],rw[10000,2])
start<-cbind(0,0)

points(start,pch=4,col="red")
points(end,pch=4,col="red")
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

Patrick Burns
Hopefully someone has a good suggestion
for your question.  I'd just like to
point out that the generation of the
random walk is pretty much optimized for
worst performance.

A 'for' loop is not necessary and growing
the objects could be a very significant
drag.  See Circles 2 and 3 of 'The R Inferno'.

Hint: 'cumsum' will be useful.

On 02/12/2010 15:58, featherbox wrote:

>
> I've wrote some code to simulate a random walk in 2 dimensions on a lattice.
> Basically I want to add something in to make it plot it point by point so
> you can see what is going on.
> Heres my code for the random walk in 2d
>
>
> RW2D<-function(N)
>    {
>      i<-0
>      xdir<-0
>      ydir<-0
>      xpos<-vector()
>      xpos[1]<-xdir
>      ypos<-vector()
>      ypos[1]<-ydir
>      for (i in 1:N-1)
>        {
> r<-runif(1)
> if(r<=0.25) {xdir<-xdir+1}
> if(r>0.25&&  r<=0.5) {xdir<-xdir-1}
> if(r>0.5&&  r<=0.75) {ydir<-ydir +1}
> if(r>0.75) {ydir<-ydir-1}
>         xpos[i+1]<-xdir
>         ypos[i+1]<-ydir
>        }
>      return(cbind(xpos,ypos))
>    }
> rw<-RW2D(10000)
>
> xmin<-min(rw[,1])
> xmax<-max(rw[,1])
> ymin<-min(rw[,2])
> ymax<-max(rw[,2])
>
> plot(rw[,1],rw[,2],type="l",xlab="x",ylab="y",main="Random Walk Simulation
> In Two Dimensions",col="green4",xlim=range(xmin:xmax),ylim=range(ymin:ymax))
>
> end<-cbind(rw[10000,1],rw[10000,2])
> start<-cbind(0,0)
>
> points(start,pch=4,col="red")
> points(end,pch=4,col="red")
>

--
Patrick Burns
[hidden email]
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

jholtman
In reply to this post by featherbox
Here is a use of color to show what the path is and following up on
Patrick's post to make the code a little more efficient:

# compute path
n <- 10000
rw <- matrix(0, ncol = 2, nrow = n)
# generate the indices to set the deltas
indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))

# now set the values
rw[indx] <- sample(c(-1, 1), n, TRUE)
# cumsum the columns
rw[,1] <- cumsum(rw[, 1])
rw[, 2] <- cumsum(rw[, 2])

plot(0,type="n",xlab="x",ylab="y",main="Random Walk Simulation
In Two Dimensions",col=1:10,xlim=range(rw[,1]),ylim=range(rw[,2]))

# use 'segments' to color each path
segments(head(rw[, 1], -1)
       , head(rw[, 2], -1)
       , tail(rw[, 1], -1)
       , tail(rw[, 2], -1)
       , col = rainbow(nrow(rw) -1)  # a range of colors
       )

end<-cbind(rw[10000,1],rw[10000,2])
start<-cbind(0,0)

points(start,pch=16,col="green", cex = 3)
points(end,pch=16,col="red", cex = 3)



On Thu, Dec 2, 2010 at 10:58 AM, featherbox
<[hidden email]> wrote:

>
> I've wrote some code to simulate a random walk in 2 dimensions on a lattice.
> Basically I want to add something in to make it plot it point by point so
> you can see what is going on.
> Heres my code for the random walk in 2d
>
>
> RW2D<-function(N)
>  {
>    i<-0
>    xdir<-0
>    ydir<-0
>    xpos<-vector()
>    xpos[1]<-xdir
>    ypos<-vector()
>    ypos[1]<-ydir
>    for (i in 1:N-1)
>      {
>        r<-runif(1)
>        if(r<=0.25) {xdir<-xdir+1}
>        if(r>0.25 && r<=0.5) {xdir<-xdir-1}
>        if(r>0.5 && r<=0.75) {ydir<-ydir +1}
>        if(r>0.75) {ydir<-ydir-1}
>        xpos[i+1]<-xdir
>        ypos[i+1]<-ydir
>      }
>    return(cbind(xpos,ypos))
>  }
> rw<-RW2D(10000)
>
> xmin<-min(rw[,1])
> xmax<-max(rw[,1])
> ymin<-min(rw[,2])
> ymax<-max(rw[,2])
>
> plot(rw[,1],rw[,2],type="l",xlab="x",ylab="y",main="Random Walk Simulation
> In Two Dimensions",col="green4",xlim=range(xmin:xmax),ylim=range(ymin:ymax))
>
> end<-cbind(rw[10000,1],rw[10000,2])
> start<-cbind(0,0)
>
> points(start,pch=4,col="red")
> points(end,pch=4,col="red")
>
> --
> View this message in context: http://r.789695.n4.nabble.com/2D-Random-walk-tp3069557p3069557.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

Komal
Hi,

I saw your code and try running it, it works!! Can you please write this code in a user defined function.
I tried making it with making a function and cant run it.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

Komal
Please change the code to a user defined function.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

Komal
In reply to this post by jholtman
HI Jholtman,

walk.2d<-function(n)
{
rw <- matrix(0, ncol = 2, nrow = n)

# generate the indices to set the deltas
indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))
 
# now set the values
rw[indx] <- sample(c(-1, 1), n, TRUE)

# cumsum the columns
rw[,1] <- cumsum(rw[, 1])
rw[,2] <- cumsum(rw[, 2])

return(rw[,1],rw[,2])
}
n<-1000

plot(walk.2d(n), type="n",xlab="x",ylab="y",main="Random Walk Simulation In Two Dimensions",xlim=range(rw[,1]),ylim=range(rw[,2]))

        # use 'segments' to color each path
segments(head(rw[, 1], -1), head(rw[, 2], -1), tail(rw[, 1], -1), tail(rw[, 2], -1), col ="blue")

I tried to make it in a function.. its not working I dont know why... please help me correcting this code.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Random walk

jholtman
This should work.  You have to return an object from a function; you
can not try to reference an object within a function.  So the value is
returned and saved in an object called 'rw' since that is how you are
referening it.


walk.2d<-function(n)
{
rw <- matrix(0, ncol = 2, nrow = n)

# generate the indices to set the deltas
indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))

# now set the values
rw[indx] <- sample(c(-1, 1), n, TRUE)

# cumsum the columns
rw[,1] <- cumsum(rw[, 1])
rw[,2] <- cumsum(rw[, 2])

rw  # return value
}
n<-1000

rw <- walk.2d(n)
plot(0, type="n",xlab="x",ylab="y",main="Random Walk Simulation In
Two Dimensions",xlim=range(rw[,1]),ylim=range(rw[,2]))

        # use 'segments' to color each path
segments(head(rw[, 1], -1), head(rw[, 2], -1), tail(rw[, 1], -1), tail(rw[,
2], -1), col ="blue")


On Wed, Jun 29, 2011 at 11:30 AM, Komal <[hidden email]> wrote:

> HI Jholtman,
>
> walk.2d<-function(n)
> {
> rw <- matrix(0, ncol = 2, nrow = n)
>
> # generate the indices to set the deltas
> indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))
>
> # now set the values
> rw[indx] <- sample(c(-1, 1), n, TRUE)
>
> # cumsum the columns
> rw[,1] <- cumsum(rw[, 1])
> rw[,2] <- cumsum(rw[, 2])
>
> return(rw[,1],rw[,2])
> }
> n<-1000
>
> plot(walk.2d(n), type="n",xlab="x",ylab="y",main="Random Walk Simulation In
> Two Dimensions",xlim=range(rw[,1]),ylim=range(rw[,2]))
>
>        # use 'segments' to color each path
> segments(head(rw[, 1], -1), head(rw[, 2], -1), tail(rw[, 1], -1), tail(rw[,
> 2], -1), col ="blue")
>
> I tried to make it in a function.. its not working I dont know why... please
> help me correcting this code.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/2D-Random-walk-tp3069557p3633205.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.