Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simulations and graph or rolling average for St. Petersburg paradox
PlayGame <- function(winnings=0, turn=1) {
if(sample(0:1, 1)) {
winnings <- winnings + (2^turn)
turn <- turn + 1
PlayGame(winnings, turn)
else {
RunSimulation <- function(timesToPlay=20000) {
results <- rep(NA, timesToPlay)
rollingAvg <- rep(NA, timesToPlay)
for(i in 1:timesToPlay) {
results[i] <- PlayGame()
rollingAvg[i] <- mean(results[1:i])
plot(rollingAvg, type="l", xlab="Number of Trials", ylab="Rolling Average", main="Rolling Average of St. Petersburg Simulations")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.