Estimate Chance of Hitting it Big in the Lottery
In this project we will code for an app that allows lottery addicts to estimate their chances of winning the 6/49 lottery. This information will be used as a resource by the medical institute to help lottery players make more informed choices before they buy tickets.
def factorial(n):
final_product = 1
for i in range(n, 0, -1):
final_product *= i
return final_product
def combinations(n, k):
result = factorial(n) / (factorial(k)*(factorial(n - k)))
return result
#Using the factorial and combinations functions, we will calculate
#the probability of winning the lottery.
def one_ticket_probability(six_uniq_numbs):
poss_outcomes = combinations(49, 6)
prob_one_tckt = (1 / poss_outcomes) * 100
print ('Your probability of winning this lottery is {0:1%}. These odds are 1 in {1:,} submissions.'
.format(prob_one_tckt, int(poss_outcomes)))
#Test the probability function
numbers = [1,2,4,6,7,3]
guess = one_ticket_probability(numbers)
numbers = [45, 3, 7, 5, 23, 21]
guess = one_ticket_probability(numbers)
Your probability of winning this lottery is 0.000715%. These odds are 1 in 13,983,816 submissions. Your probability of winning this lottery is 0.000715%. These odds are 1 in 13,983,816 submissions.
We've written a function that takes the six lottery numbers and returns the probability of winning. This will give the buyer some numbers and odds to consider before making the purchase.
import pandas as pd
info = pd.read_csv('649.csv')
info.shape
(3665, 11)
The dataframe has 3665 rows and 11 columns. Let's look at the first and last three rows to become familiar with the database.
print(info.head(3))
print(info.tail(3))
PRODUCT DRAW NUMBER SEQUENCE NUMBER DRAW DATE NUMBER DRAWN 1 \ 0 649 1 0 6/12/1982 3 1 649 2 0 6/19/1982 8 2 649 3 0 6/26/1982 1 NUMBER DRAWN 2 NUMBER DRAWN 3 NUMBER DRAWN 4 NUMBER DRAWN 5 \ 0 11 12 14 41 1 33 36 37 39 2 6 23 24 27 NUMBER DRAWN 6 BONUS NUMBER 0 43 13 1 41 9 2 39 34 PRODUCT DRAW NUMBER SEQUENCE NUMBER DRAW DATE NUMBER DRAWN 1 \ 3662 649 3589 0 6/13/2018 6 3663 649 3590 0 6/16/2018 2 3664 649 3591 0 6/20/2018 14 NUMBER DRAWN 2 NUMBER DRAWN 3 NUMBER DRAWN 4 NUMBER DRAWN 5 \ 3662 22 24 31 32 3663 15 21 31 38 3664 24 31 35 37 NUMBER DRAWN 6 BONUS NUMBER 3662 34 16 3663 49 8 3664 48 17
In order to consider if it's worthwhile to play the lottery, we will look at historical lottery drawings to help the user determine if they could have won by now.The function below does so using data from the 6/49 lottery drawing from 1982 to 2018.
#Extract the six winning numbers from the historical 6/49 dataframe
def extract_numbers(row):
row = row[4:10] #select columns with the six drawn numbers
row = set(row.values) #convert the numbers to a set
return row
winning_lottery = info.apply(extract_numbers, axis=1)
winning_lottery.head(3)
0 {3, 41, 11, 12, 43, 14} 1 {33, 36, 37, 39, 8, 41} 2 {1, 6, 39, 23, 24, 27} dtype: object
def check_historical_occurrence(player_numbers, winning_lottery):
numbers_guessed = set(player_numbers)
compare = numbers_guessed == winning_lottery
comparison = compare.sum()
print('''Your lottery numbers {0} matched past ones {1} times.The
odds of winning the big prize in the next drawing with
that combination is 1 in 13,983,816 submissions.'''.format(numbers_guessed, comparison))
Now we will test our historical occurrences function to see how well it works.
guess = [6, 34, 43, 2, 12, 7]
check_historical_occurrence(guess, winning_lottery)
Your lottery numbers {34, 2, 6, 7, 43, 12} matched past ones 0 times.The odds of winning the big prize in the next drawing with that combination is 1 in 13,983,816 submissions.
guess = [1,2,3,4,5,6]
check_historical_occurrence(guess, winning_lottery)
Your lottery numbers {1, 2, 3, 4, 5, 6} matched past ones 0 times.The odds of winning the big prize in the next drawing with that combination is 1 in 13,983,816 submissions.
Do the odds of winning change with more tickets purchased? Let's see.
def multi_ticket_probability(no_tckts):
poss_outcomes = combinations(49, 6)
multi_tix_prob = (no_tckts / poss_outcomes) * 100
print('''The likelihood of your winning the lottery from purchasing {:,} ticket(s) is {:.6f}%. This is equivalent to {:,} out of {:,} different tickets.'''.format(no_tckts, multi_tix_prob, no_tckts, int(poss_outcomes)))
test_no_tix = [1, 10, 100, 10000, 1000000, 6991908, 13983816]
for test in test_no_tix:
print(multi_ticket_probability(test))
The likelihood of your winning the lottery from purchasing 1 ticket(s) is 0.000007%. This is equivalent to 1 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 10 ticket(s) is 0.000072%. This is equivalent to 10 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 100 ticket(s) is 0.000715%. This is equivalent to 100 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 10,000 ticket(s) is 0.071511%. This is equivalent to 10,000 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 1,000,000 ticket(s) is 7.151124%. This is equivalent to 1,000,000 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 6,991,908 ticket(s) is 50.000000%. This is equivalent to 6,991,908 out of 13,983,816 different tickets. None The likelihood of your winning the lottery from purchasing 13,983,816 ticket(s) is 100.000000%. This is equivalent to 13,983,816 out of 13,983,816 different tickets. None
We see above that the more tickets the player purchases, the greater their likelihood of winning the lottery. If they purchased all of the possible combinations (13,983,816) then the player is guaranteed to win. So our code makes sense.
def probability_less_6(numbr):
numbr_combn = combinations(6, numbr)
succ_outcme = 49 - numbr_combn
total_no_succ_outcme = numbr_combn * succ_outcme
prob = (total_no_succ_outcme / combinations(49, numbr_combn)) * 100
print('''The likelihood of your matching {:} numbers in your lottery ticket is {:.2f}%.'''.format(numbr, prob))
for nmbr in [2, 3, 4, 5]:
probability_less_6(nmbr)
TypeErrorTraceback (most recent call last) <ipython-input-15-c4782d8657f2> in <module>() 1 for nmbr in [2, 3, 4, 5]: ----> 2 probability_less_6(nmbr) <ipython-input-14-23117095dc9b> in probability_less_6(numbr) 3 succ_outcme = 49 - numbr_combn 4 total_no_succ_outcme = numbr_combn * succ_outcme ----> 5 prob = (total_no_succ_outcme / combinations(49, numbr_combn)) * 100 6 print('''The likelihood of your matching {:} numbers in your lottery ticket is {:.2f}%.'''.format(numbr, prob)) <ipython-input-1-2a3077e7367d> in combinations(n, k) 6 7 def combinations(n, k): ----> 8 result = factorial(n) / (factorial(k)*(factorial(n - k))) 9 return result 10 #Using the factorial and combinations functions, we will calculate <ipython-input-1-2a3077e7367d> in factorial(n) 1 def factorial(n): 2 final_product = 1 ----> 3 for i in range(n, 0, -1): 4 final_product *= i 5 return final_product TypeError: 'float' object cannot be interpreted as an integer