Skip to content

Instantly share code, notes, and snippets.

@SachaEpskamp
Created May 3, 2019 12:38
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 SachaEpskamp/8ce47537e33b8c7f3a9904ea8257eb1a to your computer and use it in GitHub Desktop.
Save SachaEpskamp/8ce47537e33b8c7f3a9904ea8257eb1a to your computer and use it in GitHub Desktop.
# Input:
payout <- 35
winSpaces <- 1
allSpaces <- 37
bet <- 10
# Probability:
p <- winSpaces / allSpaces
# Expected value:
ev <- (p * payout * bet - (1 - p) * bet)
round(ev,2)
# Variance:
var <- p * (bet*payout - ev)^2 + (1-p) * (-bet - ev)^2
var
# Standard deviation:
sd <- sqrt(var)
sd
## One person who bets every week of a year (52 weeks) 100 times 10 euro on the number 7 in French roulette:
set.seed(1)
profit <- replicate(52,{
sum(ifelse(sample(0:36,100,TRUE) == 7, 350, -10))
})
# Our better lost quite some money over the year:
sum(profit)
# But didn't consistently lose. In fact, exactly half of the time the better walked away with profit:
mean(profit > 0)
# But why? Well, with n bets, the EV(sum) = n * EV and SD(sum) = sqrt(n) * SD.
# We can also apply the central limit theorem and use the normal distribution.
# We obtain the following probability of making profit:
n <- exp(seq(log(100),log(100000),length=25))
Ps <- pnorm(0, n * ev, sqrt(n) * sd,lower.tail = FALSE)
plot(n, Ps, type = "l", ylim = c(0, 1), log = "x",xaxt="n",
xlab = "Number of bets on 7 in French roulete",
ylab = "Probability to end with profit",
lwd = 2)
axis(1,at = c(100,1000,10000,100000), labels = c(
"100","1,000","10,000","100,000"
))
# Let's simulate this to see if our prediction is accurate:
sims <- lapply(n,function(nn){
reps <- replicate(100,{
profit <- replicate(52,{
sum(ifelse(sample(0:36,nn,TRUE) == 7, 350, -10))
})
mean(profit > 0)
})
quantile(reps,c(0.025,0.5,0.975))
})
arrows(n,Ps,n,sapply(sims,"[[",1), angle = 90,length = 0.05)
arrows(n,Ps,n,sapply(sims,"[[",3), angle = 90,length = 0.05)
abline(h = 0.5, lty = 2)
# Seems pretty accurate!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment