Last active
November 18, 2015 13:12
-
-
Save MarcusWalz/ff262c04c849e30c2fa1 to your computer and use it in GitHub Desktop.
Western States 100 Lottery Simulator
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
entrants tickets expected_winners prob | |
1 1981 1981 64.98780 0.03280555 | |
2 890 1780 57.54135 0.06465320 | |
3 390 1560 48.80195 0.12513321 | |
4 189 1512 44.29210 0.23434974 | |
5 79 1264 32.71075 0.41406013 | |
6 33 1056 21.66605 0.65654697 | |
sums 3562 9153 270.00000 1.52754879 |
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
# Goal: Compute Probability of Winning Western States Lottery | |
# See http://www.wser.org/lottery/ for lottery details | |
# We group runners into "entry pools" determined by the number of consecutive western states entries. | |
# Number of lotterys | |
simulations = 20000 | |
# entry pools by year | |
year = 1:6 | |
# Number of winners | |
slots = 270 | |
# Number of winners by entry pool | |
entrants = c(1981, 890, 390, 189, 79,33) | |
# Number of tickets awarded to each entrant in entry pool | |
tickets_per_entrant = 2^(year - 1) | |
# Simulate a lottery. Returns the number of winners in each pool. | |
ws_lottery = function() { | |
# track winners by pool | |
winners = rep(0,length(year)) | |
while(sum(winners) < slots) { | |
# number of tickets belonging to each pool | |
tickets = (entrants - winners) * tickets_per_entrant | |
winner = sample(year, 1, prob=tickets/sum(tickets)) | |
winners[winner] = winners[winner]+1 | |
} | |
return(winners) | |
} | |
# Simulate 1000 drawings. | |
lottery_results = t(sapply(1:simulations, | |
function(x) { ws_lottery() }) | |
) | |
# Sum the columns of lottery results | |
column_sums = apply(lottery_results, 2, sum) | |
# Divide by number of simulations for the expected number of winners in each pool | |
expected_winners = column_sums / simulations | |
# Probability of a member of a pool being chosen | |
prob = expected_winners / entrants | |
tbl=cbind(entrants | |
,tickets = entrants*tickets_per_entrant | |
,expected_winners | |
,prob | |
) | |
rownames(tbl) <- year | |
tbl = rbind(tbl, sums=colSums(tbl)) | |
print(tbl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment