RW <- function(n) { x <- sample(c(-1,1),size=n,replace=T) rw <- cumsum(x) plot(1:n,rw,xlab="N",ylim=c(-3.1*sqrt(n),3.1*sqrt(n)),type="l") abline(h=0,lty=2) } RWadd <- function(n,color="blue") { x <- sample(c(-1,1),size=n,replace=T) rw <- cumsum(x) lines(1:n,rw,col=color) }
RWpos <- function(n,reps,...) { Pos <- numeric(reps) for(i in 1:reps) { x <- sample(c(-1,1),size=n,replace=T) rw <- cumsum(x) Pos[i] <- sum(rw > 0)/n } hist(Pos,col="yellow", main="Proportion of Time spent in Positive Domain",...) Pos }