Conditional Probability#
Suppose you draw a card from a deck of 52 cards and it’s red. What is the probability that it is also a diamond ?
Suppose you draw a card from a deck of 52 cards and it’s red. What is the probability that it is a queen ?
Suppose you draw a card from a deck of 52 cards and it’s red. What is the probability that it is a queen of diamonds ?
import random
suits = ["diamonds", "clubs", "spades", "hearts"]
reds = ["diamonds", "hearts"]
blacks = ["clubs", "spades"]
cards = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
print ("Number of cards in the deck:", len(suits)*len(cards))
Number of cards in the deck: 52
# initialize couters
c_draws = 0
c_reds = 0
c_reds_and_diamonds = 0
c_reds_and_queens = 0
c_queens_and_diamonds_and_red = 0
# draw the cards at random
n_draws = 1000000
for i in range(1, n_draws):
c_draws += 1
s = random.choice(suits)
c = random.choice(cards)
# print (s, c)
# select the cards
if s in reds:
c_reds += 1
if s == "diamonds":
c_reds_and_diamonds += 1
if c == "Q":
c_reds_and_queens += 1
if s == "diamonds" and c == "Q":
c_queens_and_diamonds_and_red += 1
print ("Total number of draws = ", c_draws)
print ("Total number of diamonds = ", c_reds)
print ("Total number of diamons queens = ", c_reds_and_diamonds)
print ("Total number of red queens = ", c_reds_and_queens)
print ("Total number of red queens of diamons = ", c_queens_and_diamonds_and_red)
Total number of draws = 999999
Total number of diamonds = 500808
Total number of diamons queens = 250313
Total number of red queens = 38687
Total number of red queens of diamons = 19274
print ("""Suppose you draw a card from a deck of 52 cards and it’s red.\nWhat is the probability that it is also a diamond ?""")
print ("Frequency = ", round(c_reds_and_diamonds/float(c_reds),3))
Suppose you draw a card from a deck of 52 cards and it’s red.
What is the probability that it is also a diamond ?
Frequency = 0.5
print ("Suppose you draw a card from a deck of 52 cards and it’s red.\nWhat is the probability that it is a queen ?")
print ("Frequency = ", round(c_reds_and_queens/float(c_reds),3))
Suppose you draw a card from a deck of 52 cards and it’s red.
What is the probability that it is a queen ?
Frequency = 0.077
print("Suppose you draw a card from a deck of 52 cards and it’s red.\nWhat is the probability that it is a queen of diamonds ?")
print ("Frequency = ", round(c_queens_and_diamonds_and_red/float(c_reds),3))
Suppose you draw a card from a deck of 52 cards and it’s red.
What is the probability that it is a queen of diamonds ?
Frequency = 0.038