353 - Topic 01 - Enumeration - Chapter 3

70 days ago by Professor353

John Travis

Mississippi College

MATH 353 - Introduction to Mathematical Probability and Statistics

Textbook:  Tanis and Hogg, A Brief Course in Mathematical Statistics

Methods of Enumeration

Now, let's consider a standard deck of cards.  We will consider each card as an ordered pair (value, suit).  Therefore, by the multiplication principle there are $13 \times 4 = 52$ cards in a standard deck with $13$ values and $4$ suits.

 

# %hideall var('A C D H J K Q S') suits = [S, D, C, H] values = [2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A] deck = [(value, suit) for suit in suits for value in values] full_deck = copy(deck) # to save a copy of the original deck for later use. print deck 
       
[(2, S), (3, S), (4, S), (5, S), (6, S), (7, S), (8, S), (9, S), (10,
S), (J, S), (Q, S), (K, S), (A, S), (2, D), (3, D), (4, D), (5, D), (6,
D), (7, D), (8, D), (9, D), (10, D), (J, D), (Q, D), (K, D), (A, D), (2,
C), (3, C), (4, C), (5, C), (6, C), (7, C), (8, C), (9, C), (10, C), (J,
C), (Q, C), (K, C), (A, C), (2, H), (3, H), (4, H), (5, H), (6, H), (7,
H), (8, H), (9, H), (10, H), (J, H), (Q, H), (K, H), (A, H)]
[(2, S), (3, S), (4, S), (5, S), (6, S), (7, S), (8, S), (9, S), (10, S), (J, S), (Q, S), (K, S), (A, S), (2, D), (3, D), (4, D), (5, D), (6, D), (7, D), (8, D), (9, D), (10, D), (J, D), (Q, D), (K, D), (A, D), (2, C), (3, C), (4, C), (5, C), (6, C), (7, C), (8, C), (9, C), (10, C), (J, C), (Q, C), (K, C), (A, C), (2, H), (3, H), (4, H), (5, H), (6, H), (7, H), (8, H), (9, H), (10, H), (J, H), (Q, H), (K, H), (A, H)]

Before we can pick some cards, we need to shuffle the deck of course.  In real life you might need to perform several shuffles in order to get a random arrangment of the cards.  We'll just shuffle once and hope for the best.  If you are leary of the shuffle, then just re-evaluate the cell and watch a new arrangment emerge.  Note that the cards are all there but now in a different order.

shuffle(deck) print(deck) print 'The number of cards in the deck is ',Set(deck).cardinality() 
       
[(6, H), (Q, H), (5, D), (7, S), (Q, C), (9, D), (3, D), (9, C), (10,
D), (10, H), (6, D), (7, D), (9, H), (2, S), (J, D), (10, C), (2, D),
(3, S), (2, H), (3, H), (4, H), (4, D), (4, C), (8, H), (A, D), (K, C),
(Q, D), (J, C), (4, S), (5, S), (A, C), (8, C), (7, H), (K, H), (10, S),
(Q, S), (9, S), (K, D), (2, C), (5, C), (6, S), (6, C), (J, H), (5, H),
(K, S), (J, S), (8, D), (7, C), (A, S), (A, H), (3, C), (8, S)]
The number of cards in the deck is  52
[(6, H), (Q, H), (5, D), (7, S), (Q, C), (9, D), (3, D), (9, C), (10, D), (10, H), (6, D), (7, D), (9, H), (2, S), (J, D), (10, C), (2, D), (3, S), (2, H), (3, H), (4, H), (4, D), (4, C), (8, H), (A, D), (K, C), (Q, D), (J, C), (4, S), (5, S), (A, C), (8, C), (7, H), (K, H), (10, S), (Q, S), (9, S), (K, D), (2, C), (5, C), (6, S), (6, C), (J, H), (5, H), (K, S), (J, S), (8, D), (7, C), (A, S), (A, H), (3, C), (8, S)]
The number of cards in the deck is  52

Often, one doles out the cards one at a time into "hands".  Let's get 5...

%auto print html('Click on "Update" to deal 5 more cards') deck1 = copy(full_deck) shuffle(deck1) @interact def _(auto_update=False): global deck1 shuffle(deck1) if (Set(deck1).cardinality()<5): print html('Deck is too small...getting a new deck') deck1 = copy(full_deck) else: hand = [deck1.pop() for card in range(5)] print html("The cards dealt:") show(hand) print html("The remaining cards in the deck:") print(deck1) print(html("\nThe number of remaining cards in the deck = %s"%str(Set(deck1).cardinality()))) 
       
Click on "Update" to deal 5 more cards Traceback (most recent call last): def _(auto_update=False): File "", line 1, in <module> File "/tmp/tmpUyuQaY/___code___.py", line 5, in <module> deck1 = copy(full_deck) NameError: name 'full_deck' is not defined
Click on "Update" to deal 5 more cards
Traceback (most recent call last):    def _(auto_update=False):
  File "", line 1, in <module>
    
  File "/tmp/tmpUyuQaY/___code___.py", line 5, in <module>
    deck1 = copy(full_deck)
NameError: name 'full_deck' is not defined

Notice, now that these cards have been dealt, the deck has fewer cards left.  If you go back up and reevaluate the previous cell for "hand", you will have 5 fewer cards again left below.  Don't try to deal yourself more than 10 hands!  Otherwise, you will need to go back up and create a new deck.

 

Empirical Card Probabilities - Poker

Finally, we want to experimentally develop a guess for the probability of the various poker hands.  If you do not know the various poker hands, you can check out a brief explanation in the textbook or browse check it out on the web .  It is easy to understand that the corresponding probabilities for determining a particular hand must use conditional probability.

Below, we deal out an increasingly large number of 5-card hands and count the resulting types of poker hands.   As then number of hands increases, the corresponding relative frequencies should approach the theoretical values.  Please note that for large numbers of hands, the results make take a while to arrive.

       
num_hands 

Click to the left again to hide and once more to show the dynamic interactive window

 
       

Now, some playing around with binomial coefficients...

# How about Pascal's triangle @interact def _(n = slider(1,30,1,5)): for row in range(n+1): binoms = sorted(binomial_coefficients(row).items()) given_n = [] for k in range(row+1): given_n.append(binoms[k][1]) pretty_print(html('<center>%s</center>'%given_n)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# or perhaps how about the number of unordered vs ordered arrangements for a given n n=20 binoms = sorted(binomial_coefficients(n).items()) for r in range(n+1): html('For (n,r)=%s'%str((n,r))+', unordered arrangements=%s'%str(binoms[r][1])+' and ordered arrangements=%s'%str(binoms[r][1]*factorial(r))) 
       
For (n,r)=(20, 0), unordered arrangements=1 and ordered arrangements=1

For (n,r)=(20, 1), unordered arrangements=20 and ordered arrangements=20

For (n,r)=(20, 2), unordered arrangements=190 and ordered arrangements=380

For (n,r)=(20, 3), unordered arrangements=1140 and ordered arrangements=6840

For (n,r)=(20, 4), unordered arrangements=4845 and ordered arrangements=116280

For (n,r)=(20, 5), unordered arrangements=15504 and ordered arrangements=1860480

For (n,r)=(20, 6), unordered arrangements=38760 and ordered arrangements=27907200

For (n,r)=(20, 7), unordered arrangements=77520 and ordered arrangements=390700800

For (n,r)=(20, 8), unordered arrangements=125970 and ordered arrangements=5079110400

For (n,r)=(20, 9), unordered arrangements=167960 and ordered arrangements=60949324800

For (n,r)=(20, 10), unordered arrangements=184756 and ordered arrangements=670442572800

For (n,r)=(20, 11), unordered arrangements=167960 and ordered arrangements=6704425728000

For (n,r)=(20, 12), unordered arrangements=125970 and ordered arrangements=60339831552000

For (n,r)=(20, 13), unordered arrangements=77520 and ordered arrangements=482718652416000

For (n,r)=(20, 14), unordered arrangements=38760 and ordered arrangements=3379030566912000

For (n,r)=(20, 15), unordered arrangements=15504 and ordered arrangements=20274183401472000

For (n,r)=(20, 16), unordered arrangements=4845 and ordered arrangements=101370917007360000

For (n,r)=(20, 17), unordered arrangements=1140 and ordered arrangements=405483668029440000

For (n,r)=(20, 18), unordered arrangements=190 and ordered arrangements=1216451004088320000

For (n,r)=(20, 19), unordered arrangements=20 and ordered arrangements=2432902008176640000

For (n,r)=(20, 20), unordered arrangements=1 and ordered arrangements=2432902008176640000
For (n,r)=(20, 0), unordered arrangements=1 and ordered arrangements=1

For (n,r)=(20, 1), unordered arrangements=20 and ordered arrangements=20

For (n,r)=(20, 2), unordered arrangements=190 and ordered arrangements=380

For (n,r)=(20, 3), unordered arrangements=1140 and ordered arrangements=6840

For (n,r)=(20, 4), unordered arrangements=4845 and ordered arrangements=116280

For (n,r)=(20, 5), unordered arrangements=15504 and ordered arrangements=1860480

For (n,r)=(20, 6), unordered arrangements=38760 and ordered arrangements=27907200

For (n,r)=(20, 7), unordered arrangements=77520 and ordered arrangements=390700800

For (n,r)=(20, 8), unordered arrangements=125970 and ordered arrangements=5079110400

For (n,r)=(20, 9), unordered arrangements=167960 and ordered arrangements=60949324800

For (n,r)=(20, 10), unordered arrangements=184756 and ordered arrangements=670442572800

For (n,r)=(20, 11), unordered arrangements=167960 and ordered arrangements=6704425728000

For (n,r)=(20, 12), unordered arrangements=125970 and ordered arrangements=60339831552000

For (n,r)=(20, 13), unordered arrangements=77520 and ordered arrangements=482718652416000

For (n,r)=(20, 14), unordered arrangements=38760 and ordered arrangements=3379030566912000

For (n,r)=(20, 15), unordered arrangements=15504 and ordered arrangements=20274183401472000

For (n,r)=(20, 16), unordered arrangements=4845 and ordered arrangements=101370917007360000

For (n,r)=(20, 17), unordered arrangements=1140 and ordered arrangements=405483668029440000

For (n,r)=(20, 18), unordered arrangements=190 and ordered arrangements=1216451004088320000

For (n,r)=(20, 19), unordered arrangements=20 and ordered arrangements=2432902008176640000

For (n,r)=(20, 20), unordered arrangements=1 and ordered arrangements=2432902008176640000