-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards-example.py
executable file
·63 lines (53 loc) · 2.24 KB
/
cards-example.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
#!/usr/bin/env python3
import nbayes
class Card(nbayes.Instance):
def __init__(self, card_string):
labels = set()
if card_string[0] in 'JQK':
labels.add('face')
if card_string[0] == 'K':
labels.add('king')
if card_string[0] == 'J':
labels.add('jack')
if card_string[0] == 'Q':
labels.add('queen')
if card_string[0] == '2':
labels.add('deuce')
if card_string[0] == 'A':
labels.add('ace')
if card_string[1] in 'CS':
labels.add('black')
if card_string[1] == 'C':
labels.add('clubs')
if card_string[1] == 'S':
labels.add('spades')
if card_string[1] in 'DH':
labels.add('red')
if card_string[1] == 'D':
labels.add('diamonds')
if card_string[1] == 'H':
labels.add('hearts')
super(Card,self).__init__(card_string, *labels)
c = nbayes.Classifier()
c.add_instances(*[ Card(v+s) for s in 'CDHS' for v in '23456789TJQKA' ])
# prob_lattr is prob label or attr
p_king = c.prob_lattr('king')
p_face = c.prob_lattr('face')
p_face_given_king = c.prob_lattr_given_lattr('face','king')
assert p_king == 4.0/52
assert p_face == 12.0/52
assert p_face_given_king == 1.0 # all kings are faces
p_king_given_face = c.posterior('king','face') # P(H|E) = P(E|H)/P(E) * P(H)
assert p_king_given_face == 1.0/3
print('''
Have someone draw a card from a deck. They tell you it's a face card, what's
the probability it's a king?
(You could totally do this without invoking Bayes… Here's how you use Bayes.)
''')
print("P(King) = {:0.4f} aka the prior probability".format(p_king))
print("P(Face) = {:0.4f} aka the evidence".format(p_face))
print("P(Face|King) = {:0.4f}".format(p_face_given_king))
print("P(Face|King)/P(Face) = {:0.4f} aka the likelyhood ratio".format(c.likelyhood_ratio('face','king')))
print("P(King|Face) = P(Face|King)/P(Face) * P(King) aka the posterior probability")
print(" = {:0.4f}/{:0.4f} * {:0.4f}".format(p_face_given_king, p_face, p_king))
print(" = {:0.4f}".format( p_king_given_face ))