Created
August 26, 2012 03:37
-
-
Save glesica/3473645 to your computer and use it in GitHub Desktop.
R functions to simulate the Petersburg Paradox.
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
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