Skip to content

Instantly share code, notes, and snippets.

@grano
Created January 13, 2015 17:27
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 grano/fc854a2ffb2d6af8793b to your computer and use it in GitHub Desktop.
Save grano/fc854a2ffb2d6af8793b to your computer and use it in GitHub Desktop.
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 {
winnings
}
}
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")
}
RunSimulation()
@amulekii
Copy link

amulekii commented Feb 7, 2023

Is the y-axis the total winnings from however many times you played?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment