Skip to content

Instantly share code, notes, and snippets.

@necronet
Created August 5, 2022 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necronet/0c379b3ac2ebe7035fbcd36a29961949 to your computer and use it in GitHub Desktop.
Save necronet/0c379b3ac2ebe7035fbcd36a29961949 to your computer and use it in GitHub Desktop.
Statistical Rethinking example for MCMC
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