-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinations.py
63 lines (42 loc) · 1.5 KB
/
combinations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
combinations.py
Demonstrate itertools using basic statistical concepts
In this simple card game each card has a value.
A player draws 5 cards without replacement and sums their values
to determine their final score.
Task:
Visualize the probability distribution for scores from this card game
'''
import itertools
suits = {'clubs', 'spades', 'hearts', 'diamonds'}
# Face cards have these values:
cards = {'jack': 13, 'queen': 17, 'king': 18, 'ace': 50}
# Numbered cards are worth twice their face value
nums = range(2, 11)
numbered_cards = zip(nums, (2 * x for x in nums))
cards.update(numbered_cards)
# The cartesian product of cards and suits produces the deck
deck = itertools.product(cards.keys(), suits)
# There are 52 choose 5 possible hands:
sample_space = itertools.combinations(deck, 5)
def random_variable(hand):
'''
Random variables are functions from the sample space to the real line.
This one sums the values of the cards in the hand by
looking up the value of cards in the module namespace.
>>> hand = ((5, 'C'), (5, 'S'), (5, 'H'), (5, 'D'), (2, 'C'))
>>> random_variable(hand)
44
'''
return sum(cards[x[0]] for x in hand)
pdist = map(random_variable, sample_space)
# We get a discrete probability distribution by applying the
# random variable to every element in the sample space.
#
# Analyze this result in Ipython notebook with:
#
# dist = pd.Series(list(pdist))
# dist.hist(bins=100)
if __name__ == '__main__':
import doctest
doctest.testmod()