Created
August 5, 2022 20:07
-
-
Save necronet/0c379b3ac2ebe7035fbcd36a29961949 to your computer and use it in GitHub Desktop.
Statistical Rethinking example for MCMC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num_weeks <- 1e5 | |
position <- rep(0, num_weeks) | |
current <- 10 | |
for (i in 1:num_weeks) { | |
position[i] <- current | |
proposal <- current + sample( c(-1, 1), size = 1) | |
if (proposal < 1) proposal <- 10 | |
if (proposal > 10) proposal <- 1 | |
prob_move <- proposal/current | |
current <- ifelse(runif(1) < prob_move, proposal, current) | |
} | |
plot(table(position)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment