GaryBAYLOR / R-code

A collection of algorithms written by myself for solving statistical problems
0 stars 0 forks source link

Random Walk #18

Open GaryBAYLOR opened 9 years ago

GaryBAYLOR commented 9 years ago

Random walk is a non-convergent Markov Chain. We can show that it is not convergent

random.walk <- function(t = 1e4) {
    res <- numeric(t + 1)
    res[0] <- rnorm(1)
    for(i in 1:t) {
        res[i + 1] <- res[i] + rnorm(1) 
    }
    plot(res, type = "l", lwd = 0.5)
}

The plot looks like the following graph. image Each time the random walk function is run, the plot is different. image

If we slightly change the code, the Markov Chain will be convergent.

Markov.chain <- function(t = 1e4, rho = 0.9) {
    res <- numeric(t + 1)
    res[0] <- rnorm(1)
    for(i in 1:t) {
        res[i + 1] <- rho * res[i] + rnorm(1) 
    }
    par(mfrow = c(1, 2))
    plot(res, type = "l", lwd = 0.5)
    plot(density(res), type = "l")
}

The plot is like this image Each time the function is run, the density will be almost the same image