The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Probability and Statistics

Lady EriLady Eri Registered User regular
edited November 2010 in Debate and/or Discourse
I am trying to find the probability of that any three cards I draw out of a deck will add up to 21 or more (this is not for a school project or gambling theory or anything like that, real world application I promise), but I am having trouble coming up with the equation. I don't fancy typing out each possible combination out in excel either, and am starting to suspect it would take a very long time regardless.

Anyone know of a site where I can put the probability of a number being selected (like 4/13 for the number ten, 1/13 for the number six, ect) then set a threshold where it will give me a probability. Or alternatively, if someone knows an easier equation then writing out each possible combination of cards? I did well in statistics, but it was a looong time ago.

Lady Eri on

Posts

  • DrezDrez Registered User regular
    edited November 2010
    Yes, and that site might be the Penny Arcade Help and Advice subforum. The link is here: http://forums.penny-arcade.com/forumdisplay.php?f=22

    But in all seriousness...first of all, what are the value of face cards in this hypothetical? And what is an Ace worth? 1 or 11 or something else? Or are face cards omitted from this deck? Or what?

    edit: The first problem (for us, since you probably already know exactly what you mean) is defining "deck," especially when you are talking about point values. Face cards can be worth anything (including zero) depending on the game. And some cards, depending on the game, can be worth different amounts depending on the context. So, yeah, we need some more info first.

    Drez on
    Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
  • FlarnaFlarna Registered User regular
    edited November 2010
    Well, you've piqued the math problem portion of my brain. For this one, I'd just write out the 3 number combinations that total 21, find the number of ways to make each of those combinations with the cards in a standard deck, then divide that by the number of possible 3 card combinations.

    SO (assuming you're counting all face cards as 10s and aces are only 1):

    1 + 10 + 10
    2 + 9 + 10
    3 + 8 + 10
    3 + 9 + 9
    4 + 7 + 10
    4 + 8 + 9
    5 + 6 + 10
    5 + 7 + 9
    5 + 8 + 8
    6 + 6 + 9
    6 + 7 + 8
    7 + 7 + 7

    Now, there are 16 cards that count 10 and 4 cards each for 1-9. so the number of ways to make each of the aforementioned sets are:

    1 + 10 + 10: 4 * 16C2 = 4 * 120 = 480 +
    2 + 9 + 10: 4 * 4 * 16 = 256 +
    3 + 8 + 10: 4 * 4 * 16 = 256 +
    3 + 9 + 9: 4 * 4C2 = 4 * 6 = 24 +
    4 + 7 + 10: 4 * 4 * 16 = 256 +
    4 + 8 + 9: 4 * 4 * 4 = 64 +
    5 + 6 + 10: 4 * 4 * 16 = 256 +
    5 + 7 + 9: 4 * 4 * 4 = 64 +
    5 + 8 + 8: 4 * 4C2 = 24 +
    6 + 6 + 9: = 24 +
    6 + 7 + 8: = 64 +
    7 + 7 + 7: 4C3 = 4 +
    = 1772

    then the probability of getting 21 would just be the sum of all that divided by 52C3 or 1772 / 22100 or ~0.0802 or just over 8%.

    Yay maths!

    Flarna on
  • Lady EriLady Eri Registered User regular
    edited November 2010
    aces are 1, not 11, otherwise standard blackjack rules.

    Lady Eri on
  • Lady EriLady Eri Registered User regular
    edited November 2010
    Flarna wrote: »
    Well, you've piqued the math problem portion of my brain. For this one, I'd just write out the 3 number combinations that total 21, find the number of ways to make each of those combinations with the cards in a standard deck, then divide that by the number of possible 3 card combinations.

    SO (assuming you're counting all face cards as 10s and aces are only 1):

    1 + 10 + 10
    2 + 9 + 10
    3 + 8 + 10
    3 + 9 + 9
    4 + 7 + 10
    4 + 8 + 9
    5 + 6 + 10
    5 + 7 + 9
    5 + 8 + 8
    6 + 6 + 9
    6 + 7 + 8
    7 + 7 + 7

    Now, there are 16 cards that count 10 and 4 cards each for 1-9. so the number of ways to make each of the aforementioned sets are:

    1 + 10 + 10: 4 * 16C2 = 4 * 120 = 480 +
    2 + 9 + 10: 4 * 4 * 16 = 256 +
    3 + 8 + 10: 4 * 4 * 16 = 256 +
    3 + 9 + 9: 4 * 4C2 = 4 * 6 = 24 +
    4 + 7 + 10: 4 * 4 * 16 = 256 +
    4 + 8 + 9: 4 * 4 * 4 = 64 +
    5 + 6 + 10: 4 * 4 * 16 = 256 +
    5 + 7 + 9: 4 * 4 * 4 = 64 +
    5 + 8 + 8: 4 * 4C2 = 24 +
    6 + 6 + 9: = 24 +
    6 + 7 + 8: = 64 +
    7 + 7 + 7: 4C3 = 4 +
    = 1772

    then the probability of getting 21 would just be the sum of all that divided by 52C3 or 1772 / 22100 or ~0.0802 or just over 8%.

    Yay maths!

    Thats awesome, is there a way for me to run with that and get all outcomes over 21 included in the result?

    Lady Eri on
  • DrezDrez Registered User regular
    edited November 2010
    Lady Eri wrote: »
    Flarna wrote: »
    Well, you've piqued the math problem portion of my brain. For this one, I'd just write out the 3 number combinations that total 21, find the number of ways to make each of those combinations with the cards in a standard deck, then divide that by the number of possible 3 card combinations.

    SO (assuming you're counting all face cards as 10s and aces are only 1):

    1 + 10 + 10
    2 + 9 + 10
    3 + 8 + 10
    3 + 9 + 9
    4 + 7 + 10
    4 + 8 + 9
    5 + 6 + 10
    5 + 7 + 9
    5 + 8 + 8
    6 + 6 + 9
    6 + 7 + 8
    7 + 7 + 7

    Now, there are 16 cards that count 10 and 4 cards each for 1-9. so the number of ways to make each of the aforementioned sets are:

    1 + 10 + 10: 4 * 16C2 = 4 * 120 = 480 +
    2 + 9 + 10: 4 * 4 * 16 = 256 +
    3 + 8 + 10: 4 * 4 * 16 = 256 +
    3 + 9 + 9: 4 * 4C2 = 4 * 6 = 24 +
    4 + 7 + 10: 4 * 4 * 16 = 256 +
    4 + 8 + 9: 4 * 4 * 4 = 64 +
    5 + 6 + 10: 4 * 4 * 16 = 256 +
    5 + 7 + 9: 4 * 4 * 4 = 64 +
    5 + 8 + 8: 4 * 4C2 = 24 +
    6 + 6 + 9: = 24 +
    6 + 7 + 8: = 64 +
    7 + 7 + 7: 4C3 = 4 +
    = 1772

    then the probability of getting 21 would just be the sum of all that divided by 52C3 or 1772 / 22100 or ~0.0802 or just over 8%.

    Yay maths!

    Thats awesome, is there a way for me to run with that and get all outcomes over 21 included in the result?

    I think you'd need to calculate the total probability for each possible score and add them to each other.

    So you'd have to work out the possibility of getting a 21, a 22, a 23, a 24, a 25, a 26, a 27, a 28, a 29, and a 30 and then smoosh them all together into an uber-probability.

    Drez on
    Switch: SW-7690-2320-9238Steam/PSN/Xbox: Drezdar
  • Lady EriLady Eri Registered User regular
    edited November 2010
    Also I know why you are doing the C#, I am just unfamiliar on how to calculate that.

    Lady Eri on
  • FlarnaFlarna Registered User regular
    edited November 2010
    Oh. You did say "or more" didn't you. Well, the way I did it, you'd just need to add those combinations of 3 cards that total more and work that into the sum.

    Flarna on
  • FlarnaFlarna Registered User regular
    edited November 2010
    nCm = n! / m!(n - m)! if memory serves

    Flarna on
  • Lady EriLady Eri Registered User regular
    edited November 2010
    Thanks, well this does help a lot and I appreciate it. Though I do hope to scale the calculations to multiple cards. My ultimate goal is to get an idea of the probability of getting a certain number of cards before going over 21, where that certain number is anything from 3 to 8. I am sure there is a statistics calculator out there somewhere that can make this easy.

    Lady Eri on
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    So I wrote a simple brute force program to figure it out. Even though the runtime is exponential in the number of cards selected, it's still fast enough to easily run for 3 cards (and probably more).

    I got a ~45.85% probability of getting 21 or greater without replacement, and a ~8.02% probability of getting exactly 21 without replacement (which matches the other guy's result!).

    Here's the program (it's in Python):
    threshold = 21
    cards = []
    for suit in range(0, 4):
      for value in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]:
        cards.append((suit, value))
    total_equal = 0
    total_over = 0
    total_cards = 0
    for card1 in cards:
      for card2 in cards:
        if card2[0] == card1[0] and card2[1] == card1[1]:
          continue
        for card3 in cards:
          if card3[0] == card1[0] and card3[1] == card1[1]:
            continue
          if card2[0] == card3[0] and card2[1] == card3[1]:
            continue
          total_cards += 1
          sum = min(10, card1[1]) + min(10, card2[1]) + min(10, card3[1])
          if (sum == threshold):
            total_equal += 1
          if (sum >= threshold):
            total_over += 1
    
    print "total over %d: %d, percent: %f" % (threshold, total_over, 100 * total_over / float(total_cards))
    print "total equal to %d: %d, percent: %f" % (threshold, total_equal, 100 * total_equal / float(total_cards))
    

    Smug Duckling on
    smugduckling,pc,days.png
  • FlarnaFlarna Registered User regular
    edited November 2010
    In case you're curious, I did the calculation and got 45.85% for at least 21 in 3 cards.

    Edit: I did it by hand, so I fully expect to have made an error somewhere.

    Flarna on
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    You're right. I had an error in my program. I fixed it and got 45.85% as well. (The source above is fixed.)

    Smug Duckling on
    smugduckling,pc,days.png
  • FlarnaFlarna Registered User regular
    edited November 2010
    Ah, yes. You were evaluating face cards of the same suit as the same card, huh. Tricksy Hobbitses.

    Your solution happens to be simpler and more flexible than doing it by hand as I was. Assuming one has access to a microprocessor, that is. It's also probably more in line with what the OP was looking for.

    Flarna on
  • Kipling217Kipling217 Registered User regular
    edited November 2010
    Considering that the point spread is between 3( 3 aces) and 33(3 face cards) with the deck stacked at the high end(Kings, Queens and Jacks all count 11) that sounds reasonable.

    Kipling217 on
    The sky was full of stars, every star an exploding ship. One of ours.
  • redxredx I(x)=2(x)+1 whole numbersRegistered User regular
    edited November 2010
    The other option, if you are going to write a program, would be to just do a simulation. Set up an array as a virtual deck. Randomize it, pretend to deal out cards and keep track of the results. Run it a few hundred thousand time, and your results will be pretty accurate. It makes it a hell of a lot easier if you are going to look at things like various shoe sizes and number of hands dealt between shuffles.

    like you could. If you wanted and kinda sucked at math. It's real easy and obvious. I'm fucking lazy of course so I'm not doing it.

    redx on
    They moistly come out at night, moistly.
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    redx wrote: »
    The other option, if you are going to write a program, would be to just do a simulation. Set up an array as a virtual deck. Randomize it, pretend to deal out cards and keep track of the results. Run it a few hundred thousand time, and your results will be pretty accurate. It makes it a hell of a lot easier if you are going to look at things like various shoe sizes and number of hands dealt between shuffles.

    like you could. If you wanted and kinda sucked at math. It's real easy and obvious. I'm fucking lazy of course so I'm not doing it.

    Yeah - for a math class this would obviously not work, but in the real world, it would give close enough approximations of the probabilities for basically any purpose.

    In this case, of course, there's likely a (relatively) quick analytical solution. It would just take longer to figure out and program.

    Smug Duckling on
    smugduckling,pc,days.png
  • GarthorGarthor Registered User regular
    edited November 2010
    redx wrote: »
    The other option, if you are going to write a program, would be to just do a simulation. Set up an array as a virtual deck. Randomize it, pretend to deal out cards and keep track of the results. Run it a few hundred thousand time, and your results will be pretty accurate. It makes it a hell of a lot easier if you are going to look at things like various shoe sizes and number of hands dealt between shuffles.

    like you could. If you wanted and kinda sucked at math. It's real easy and obvious. I'm fucking lazy of course so I'm not doing it.

    There's no point in doing a random simulation when you can just deal out every single possible combination of cards and tally up the scores.

    Garthor on
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    That's hard when you want to do it for more than 3 cards though, since you'll get an exponential explosion of combinations.

    Smug Duckling on
    smugduckling,pc,days.png
  • MrBlarneyMrBlarney Registered User regular
    edited November 2010
    Well, when it only takes a few minutes to write up a program and run a million trials, it's not a bad option either. In regards to Eri's question about how many cards are needed to draw at least 21, I've got the following approximations over 10,000,000 trials:

    p(X = 1) = p(X = 2) = 0
    p(X = 3) = .4589
    p(X = 4) = .3582
    p(X = 5) = .1435
    p(X = 6) = .0338
    p(X = 7) = .0052
    p(X = 8) = .0005
    p(X = 9) < .0001
    p(X = 10) < .00001
    p(X = 11) = negligible

    Anyone want to check these numbers?

    MrBlarney on
    4463rwiq7r47.png
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    I get
    number of cards required to sum to 21 or over, as a percentage of cases
    cards: 3, percent: 45.8386&#37;
    cards: 4, percent: 35.7946%
    cards: 5, percent: 14.3888%
    cards: 6, percent: 3.4066%
    cards: 7, percent: 0.5170%
    cards: 8, percent: 0.0511%
    cards: 9, percent: 0.0033%
    cards: 10, percent: 0.0001%
    

    with 10,000,000 trials using
    import random
    
    # we want to know how often we get a sum of at least 21
    threshold = 21
    trials = 10000000
    
    deck = []
    for suit in range(0, 4):
      for value in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]:
        deck.append((suit, value))
    
    results = {}
    for trial in range(0, trials):
      # a progress counter thing if uncommented
      #if (trial % (trials / 100) == 0):
      #  print 100 * trial / float(trials)
      indices = {}
      sum = 0
      while (sum < threshold):
        # select a random card
        index = random.randint(0, 51)
        # if we've already seen this index, try a new one
        while (index in indices):
          index = random.randint(0, 51)
        indices[index] = 1
    
        sum += min(10, deck[index][1])
      
      # the number of cards required is the number of indices we've
      # recorded
      cards = len(indices)
      if not cards in results:
        results[cards] = 0
      results[cards] += 1
    
    print "number of cards required to sum to 21 or over, as a percentage of cases"
    for (cards, number) in results.items():
      print "cards: %d, percent: %.04f%%" % (cards, 100 * number / float(trials))
    

    which is pretty damn close to what you get.

    Smug Duckling on
    smugduckling,pc,days.png
  • Lady EriLady Eri Registered User regular
    edited November 2010
    You guys are so awesome, there aren't even words.

    Lady Eri on
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    Okay, so I did the calculation analytically, using Flarna's method, and got the following results:
    cards: 3, percent: 45.846154&#37;, total probability: 45.846154%
    cards: 4, percent: 35.783544%, total probability: 81.629698%
    cards: 5, percent: 14.397020%, total probability: 96.026718%
    cards: 6, percent: 3.401541%, total probability: 99.428259%
    cards: 7, percent: 0.516643%, total probability: 99.944902%
    cards: 8, percent: 0.051764%, total probability: 99.996667%
    cards: 9, percent: 0.003228%, total probability: 99.999894%
    cards: 10, percent: 0.000105%, total probability: 99.999999%
    cards: 11, percent: 0.000001%, total probability: 100.000000%
    cards: 12, percent: 0.000000%, total probability: 100.000000%
    
    

    using the following code:
    import math
    
    # returns a list of all possible combinations of
    # num_cards cards, with no duplicates, where each
    # combination is listed in ascending order.
    def find_combos(num_cards):
      all_combos = {1: [[i] for i in range(1, 11)]}
      for i in range(2, num_cards + 1):
        prev_combos = all_combos[i - 1]
        combos = []
        for combo in prev_combos:
          for j in range(1, 11):
            # ensure that we don't use a number more than
            # it is allowed (16 times for 10 value cards
            # and 4 times for all other values).
            times = (4 if j != 10 else 16)
            if j >= combo[-1] and combo.count(j) < times:
              combos.append(combo + [j])
        all_combos[i] = combos
      return all_combos
    
    # returns total CHOOSE number
    def choose(total, number):
      return math.factorial(total) / (math.factorial(total - number) * math.factorial(number))
    
    if __name__ == "__main__":
      total_cards = 12
      all_combos = find_combos(total_cards)
      prev_prob = 0
      for cards in range(3, total_cards + 1):
        total_over = 0
        total_not_over = 0
        for combo in all_combos[cards]:
          seen = {}
          sum = 0
          for card in combo:
            if not card in seen:
              seen[card] = 0
            seen[card] += 1
            sum += card
          
          ways = 1
          for (card, num) in seen.items():
            # there are 4 cards of each value, except
            # there are 16 cards valued at 10 each
            choices = (4 if card != 10 else 16)
            ways *= choose(choices, num)
    
          if sum >= 21:
            total_over += ways
          else:
            total_not_over += ways
    
        prob = total_over / float(total_over + total_not_over) - prev_prob
        prev_prob += prob
        print "cards: %d, percent: %.06f%%, total probability: %.06f%%" % (cards, 100 * prob, 100 * prev_prob)
    

    It produces the right answer for 3, so I'm assuming it's correct. ;)

    EDIT: whoops, found my error. Had variable declarations outside the appropriate loop. *facepalm*

    OLD : Although it's weird that the 4 card case is about 2% under the simulated result. I wonder why that is?

    Smug Duckling on
    smugduckling,pc,days.png
  • Smug DucklingSmug Duckling Registered User regular
    edited November 2010
    Whoops, had a dumb error. The result now very closely matches the approximations so I'm actually confident it's correct. Notably this solution actually runs significantly faster than my simulation approach. :P

    Smug Duckling on
    smugduckling,pc,days.png
Sign In or Register to comment.