Random Walks (draft)

library("gganimate")
library("ggplot2")

p = 1/2

# parameters
p <- 0.5
Tmax = 60

# start at the origin
x = 0
y = 0
t = 0
df <- data.frame(x,y,t)

# random walk
for(i in 1:Tmax){
  t <- t + 1
  if(runif(1) < p){
    x <- x - 1
  } else {
    x <- x + 1
  }
  
  # reset process if dot leaves view
  if(abs(x) > 10){
    x <- 0
  }
  
  this_step <- data.frame(x,y,t)
  df <- rbind(df, this_step)
}

p <- ggplot(df, aes(x,y)) +
  coord_cartesian(xlim = c(-10, 10)) +
  geom_point(color = "blue", size = 10) +
  labs(title = "Random Walk (p = 1/2)",
       subtitle = "t = {closest_state}",
       caption = "Math 181",
       x = "",
       y = "") +
  scale_x_continuous(breaks = -10:10) +
  scale_y_continuous(breaks = NULL) +
  theme_minimal() +
  transition_states(t, transition_length = 1, state_length = 1) +
  shadow_wake(colour = "gray", wake_length = 0.2, alpha = 0.4)

animate(p, fps = 2)

histogram

p <- 1/2
Tmax = 60
N <- 100 # number of starting "dots"

# start at the origin
x <- rep(0,N)
t <- rep(0,N)
df <- data.frame(x,t)

# random walk
for(i in 1:Tmax){
  for(j in 1:N){
    if(runif(1) < p){
      x[j] <- x[j] - 1
    } else {
      x[j] <- x[j] + 1
    }
  }
  t <- rep(i,N)
  
  this_step <- data.frame(x,t)
  df <- rbind(df, this_step)
}

p <- ggplot(df, aes(x)) +
  coord_cartesian(xlim = c(-10, 10)) +
  geom_bar(fill = "blue", stat = "count") +
  labs(title = "Random Walk (p = 1/2)",
       subtitle = "t = {closest_state}",
       caption = "Math 181",
       x = "",
       y = "") +
  scale_x_continuous(breaks = -10:10) +
  scale_y_continuous(breaks = NULL) +
  theme_minimal() +
  transition_states(t, transition_length = 1, state_length = 1)

animate(p, fps = 2)

Multiple Random Walks

pL <- 3/4
pM <- 1/2
pR <- 7/16
Tmax = 60
N <- 300 # number of starting "dots" (use a number divisible by 3)

# start at the origin
x <- rep(0,N)
t <- rep(0,N)
group <- c(rep("transient left", N/3),
           rep("recurrent", N/3), 
           rep("transient right", N/3))
df <- data.frame(x,t, group)

# random walk
for(i in 1:Tmax){
  for(j in 1:(N/3)){
    if(runif(1) < pL){
      x[j] <- x[j] - 1
    } else {
      x[j] <- x[j] + 1
    }
  }
  for(j in (N/3+1):(2*N/3)){
    if(runif(1) < pM){
      x[j] <- x[j] - 1
    } else {
      x[j] <- x[j] + 1
    }
  }
  for(j in (2*N/3+1):N){
    if(runif(1) < pR){
      x[j] <- x[j] - 1
    } else {
      x[j] <- x[j] + 1
    }
  }
  
  t <- rep(i,N)
  this_step <- data.frame(x,t,group)
  df <- rbind(df, this_step)
}

p <- ggplot(df, aes(x)) +
  coord_cartesian(xlim = c(-10, 10)) +
  geom_bar(aes(fill = group),
           stat = "count", position=position_dodge()) +
  labs(title = "Random Walks",
       subtitle = "t = {closest_state}",
       caption = "Math 181",
       x = "",
       y = "") +
  scale_x_continuous(breaks = -10:10) +
  scale_y_continuous(trans='log2', breaks = NULL) +
  theme_minimal() +
  transition_states(t, transition_length = 1, state_length = 1)

animate(p, fps = 2)
## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

## Warning: position_dodge requires non-overlapping x intervals

Drunkard’s Walk

# parameters
Tmax = 60

# start at the origin
x = 0
y = 0
t = 0
df <- data.frame(x,y,t)

# random walk
for(i in 1:Tmax){
  t <- t + 1
  rand_num <- runif(1)
  if(rand_num < 0.25){
    x <- x - 1
  } else if(rand_num < 0.5){
    y <- y - 1
  } else if(rand_num < 0.75){
    x <- x + 1
  } else {
    y <- y + 1
  }
  
  # reset process if dot leaves view
  if(abs(x) > 10){
    x <- 0
    y <- 0
  }
  if(abs(y) > 10){
    x <- 0
    y <- 0
  }
  
  this_step <- data.frame(x,y,t)
  df <- rbind(df, this_step)
}

p <- ggplot(df, aes(x,y)) +
  coord_cartesian(xlim = c(-10, 10)) +
  geom_point(color = "blue", size = 10) +
  labs(title = "Drunkard's Walk",
       subtitle = "t = {closest_state}",
       caption = "Math 181",
       x = "",
       y = "") +
  scale_x_continuous(breaks = -10:10) +
  scale_y_continuous(breaks = -10:10) +
  theme_minimal() +
  transition_states(t, transition_length = 1, state_length = 1) +
  shadow_wake(colour = "gray", wake_length = 0.2, alpha = 0.4)

animate(p, fps = 2)

Related