<jupyter_start><jupyter_text>A Monte Carlo approach to Question 10 (Worksheet 2)> A certain lottery has the following rules: you buy a ticket, choose 3 diﬀerent numbers from 1 to 100, and write them on the ticket. The lottery has a box with 100 balls numbered 1 to 100. Three (diﬀerent) balls are chosen. If **any** of the balls has one of the numbers you have chosen, you win. What is the probability of winning?<jupyter_code>maxNum = 100   # Lottery numbers are 1 to 100 without replacement
maxPick = 3    # We can pick 3 numbers without replacement (so it's a group of 3 different numbers)<jupyter_output><empty_output><jupyter_text>Here's the Monte CarloI'll pick 3 numbers from 1 to 100. Computer does the same. If any of my numbers equals the computer's then I win. Repeat this game 1 million times. It should approach the theoretical value for the probability of the average game played.<jupyter_code>import random  # Get Python's random number generator
list_of_random_items = random.sample(range(1,maxNum+1), maxPick)
list_of_random_items  # Just showing it is selecting a random 3 numbers from 1-100
list_of_random_items = random.sample(range(1,maxNum+1), maxPick)
list_of_random_items   # Each call gives a different number. Order doesn't matter (combination)
winners = 0.0  # Start with 0 wins
trials = 0.0   # Start with 0 attempts at playing the lottery
for i in range(1,1000001):  # 1 to 1 million
    
    computer = set(random.sample(range(1,maxNum+1), maxPick)) # computer picks a random set of 3 numbers between 1 and 100
    me = set(random.sample(range(1,maxNum+1), maxPick))       # I pick a random set of 3 numbers between 1 and 100
    
    if len(me.intersection(computer)) > 0: # Check if any of my # intersects (equals) the computer's
        winners += 1.   # At least one of our numbers matches computer's pick. So we win.  
        
    trials += 1.   # Add 1 to the number of times we've played this lottery
    
winners   # How many times we picked at least one of the same numbers as the computer
trials    # How many times we played this lottery
print('Here\'s the average probability of winning: {:.10f}'.format(winners/trials))<jupyter_output>Here's the average probability of winning: 0.0885090000<jupyter_text>Probability of winning is the probability of hitting **any** of the 3 numbers *N.B.* It's the "any" part that you have to focus on. That means it is a set union (an "or"-- addition of probabilities), not a set intersection (an "and" -- multiplication of probabilities).So this is a union of 3 sets: win by 1st pick (set A) *OR* win by 2nd pick (set B) *OR* win by 3rd pick (set C). That it, I can win just **any** of the following cases: one of my picks is correct, two of my picks are correct, or all 3 of my picks are correct. All of these outcomes have the same payout. In set theory, this would be written: $$P(A \cup B\cup C)$$![Union of 3 diagram](union3.png)Note that the probabilities are slightly different for A, B, C because of the two conditions: 1. The number can only be picked once. So we can't get (15, 15, 100) or (93, 10, 93). All 3 numbers must be different. So with each pick, our possible set of numbers reduces by 1 (100 choices on pick 1, 99 on pick 2, and 98 on pick 3).2. If our number on any pick matches any of the 3 numbers the computer picked, then we win. So the probability for each pick (A, B, C) increases 3 times.The first pick is a $\frac{1}{100}$ chance of getting the number-- I pick 1 of a set of 100 numbers. However, I get 3 chances of being correct since the lottery says that if I match any of the computer's 3 numbers then I win. So $P(A) = 3 \times \frac{1}{100} = \frac{3}{100}$. The second is a $\frac{1}{99}$ chance (since I've removed one number from the set of 100 in the first pick). Again, I get 3 chances to win this (assuming I didn't win on the first pick). So $P(B) = 3 \times \frac{1}{99} = \frac{3}{99}$. The third is a $\frac{1}{98}$ chance (since I've removed two numbers from the set of 100 in the first two picks). Yet again, I have 3 chances to win in this set of 98 (assuming that I didn't win in the first two picks). So $P(C) = 3 \times \frac{1}{98} = \frac{3}{98}$.<jupyter_code>pA = (3./100)   # There are 3 ways to win in the 100 numbers for pick #1
pB = (3./99)    # There are 3 ways to win in the 99 numbers left for pick #2
pC = (3./98)    # There are 3 ways to win in the 98 numbers left for pick #3

pAB = pA*pB
pAC = pA*pC
pBC = pB*pC

pABC = pA*pB*pC
# From the Venn diagram, we see that  the probability of the union is
# just the addition of all 3 probabilities minus the intersections.
# The intersections occur twice (AB, AC, BC). However, when we subtract those intersections, we lose the ABC intersection
# because we've done 3 subtractions with that section.Therefore, we need to add that intersection back in.
prob = pA + pB + pC - pAB - pAC - pBC + pABC
print('Theoretical average probability of winning = {:.18f}'.format(prob))
prob<jupyter_output>Theoretical average probability of winning = 0.088188002473716762<jupyter_text>Easier theory Sometimes it is easier to solve the opposite case (what's the chance of losing) and work backward.The only way we lose is if **ALL** 3 numbers are wrong. So what's the probability of losing? Then subtract that from 1.0 to get the probability of winning.$$Pr(\text{winning}) = 1.0 - \left ( \frac{(100-3) \ \text{losers}}{100 \ \text{picks}} \times \frac{(99-3) \ \text{losers}}{99 \ \text{picks}} \times \frac{(98-3) \ \text{losers}}{98 \ \text{picks}} \right ) = 1.0 - \left ( \frac{97 \ \text{losers}}{100 \ \text{picks}} \times \frac{96 \ \text{losers}}{99 \ \text{picks}} \times \frac{95 \ \text{losers}}{98 \ \text{picks}} \right )$$So 99 ways to be wrong on the first pick (out of 100 choices). 98 ways to be wrong (out of 99 numbers left after the first pick) on the second. And, 97 ways to be wrong (out of 98 left) on the third pick.<jupyter_code>print('{:.18f}'.format(1-((100-3)/100 * ((99-3)/99) * ((98-3)/98))))<jupyter_output>0.088188002473716831
{'fraction_non_alphanumeric': 0.09910507126284389, 'fraction_numerical': 0.05104408352668213, 'mean_word_length': 3.8669354838709675, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '254881', 'license': 'Unlicense', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1801, 'n_words': 922, 'path': 'Probability Monte Carlo.ipynb', 'repo_name': 'mas-dse-greina/dse210', 'size': '6034'}