Skip to content

Instantly share code, notes, and snippets.

@glesica
Created August 26, 2012 03:37
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 glesica/3473645 to your computer and use it in GitHub Desktop.
Save glesica/3473645 to your computer and use it in GitHub Desktop.
R functions to simulate the Petersburg Paradox.
pburg <- function() {
p <- 1
while (sample(1:2, 1)[1] == 1) {
p <- p * 2
}
return(p)
}
ipburg <- function(n) {
r <- sapply(1:n, function(x) { return(pburg()) })
return(r)
}
cumpburg <- function(n, v=1) {
f <- seq(1,n,v)
r <- cumsum(ipburg(n))[f]/f
return(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment