Skip to content

Instantly share code, notes, and snippets.

@chelseaparlett
Created February 7, 2025 03:21
Show Gist options
  • Save chelseaparlett/01593326f41108dfa55cb8a4294dd7ff to your computer and use it in GitHub Desktop.
Save chelseaparlett/01593326f41108dfa55cb8a4294dd7ff to your computer and use it in GitHub Desktop.
Simulate King Metropolis' Travels
markov_step <- function(island){
islands <- LETTERS[1:5]
islands_pop <- list(A = 100,
B = 200,
C = 300,
D = 400,
E = 500)
proposed_i <- sample(islands[islands != island],
size = 1)
accept_prob <- min(1,
islands_pop[[proposed_i]]/islands_pop[[island]])
u <- runif(1)
accepted <- u <= accept_prob
value <- ifelse(accepted,
proposed_i,
island)
value
}
n_sim <- 10000
isl = "A"
track <- c(isl)
for (i in 1:n_sim){
isl <- markov_step(isl)
track <- c(track, isl)
}
ggplot(data = data.frame(track),
aes(x = track, fill = track)) +
geom_bar(color = "black") +
theme_minimal() +
theme(panel.grid = element_blank(),
legend.position = "none") +
labs(x = "Island",
y = "Nights Spent")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment