Created
February 7, 2025 03:21
-
-
Save chelseaparlett/01593326f41108dfa55cb8a4294dd7ff to your computer and use it in GitHub Desktop.
Simulate King Metropolis' Travels
This file contains hidden or 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
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