Purpose
If a random walk starts from y = 2 with p as 0.3 and q as 0.7, whats the prob that the walk hits 0 ?

> p <- 0.2
> q <- 1 - p
> n <- 100
> a <- 20
> z <- cumsum(sample(c(1, -1), n, prob = c(p, q), replace = T)) +
+     a
> plot(z, type = "l")
> abline(h = a, col = "blue", lty = "dashed")
> abline(h = 0, col = "red", lty = "solid")

Chapter_4_4-001.jpg

> p <- 0.54
> q <- 1 - p
> n <- 1000
> a <- 1
> set.seed(1977)
> counter <- 0
> i <- 1
> N <- 10000
> for (i in 1:N) {
+     z <- cumsum(sample(c(1, -1), n, prob = c(p, q), replace = T)) +
+         a
+     z.ind <- which(z == 0)
+     if (length(z.ind) > 0) {
+         counter <<- counter + 1
+     }
+ }
> print(counter/N)
[1] 0.8529

Chapter_4_4-002.jpg

The above simulation matches with 0.851851851851852 The key lesson to be learnt is that the zero hitting time depends on the probabilities p and q.