Python: Finding probability from already specified probability

Viewed 39

I am trying to write a python program to find the probability of a scenario to occur given the probability of other scenarios. Below is the situation:

A deck of cards has white and black cards. I draw 2 cards randomly without replacement.

Given Probability 1: Probability of selecting a white and then a black card is x. Given Probability 2: Probability of selecting a white card in the first draw is y.

I need to find the probability of drawing a black card, given that the first card drawn was white.

Mathematically, this is what I could figure out:

#Probability of selecting a white and then a black card is x (W is white, B is black) x = (W/ ( W + B) ) * (B /( W + B - 1)

#The probability of selecting a white card in the first draw is y y = W/ ( W + B)

x = y* B/(W+B - 1)

B/(W+B - 1) = x/y

So ultimately, x/y would be the solution to the above problem.

My Question: When trying to code this solution in python, do I need to do the initial calculation mathematically and write the below code or can I do the above calculation which i did mathematically in python to avoid doing the manual work?

x=float(input())
y=float(input())
result=float(x/y)
print(result)
1 Answers

If I was to do this thing with a python program and do not want to pass over formulas I would do some simulation. We could use a montecarlo simulation. A basic understanding is repeat an experiment n-times but starting from a different point to check how many times the result of it is the same.To do so the more times we repeat our experiment the best

Considering for example that you have a card deck with 26 cards white and 26 cards black

import itertools
import random

# make a deck of cards
cards_per_color = 26
deck = list(itertools.product([i for i in range(1, cards_per_color+1)],
                              ['white','black'])) 
                             
number_experiments = 1000000 # define number of experiments
white_black = 0
for hands in range(number_experiments):
    # shuffle the cards
    random.shuffle(deck) # generate new condition
    cards = deck[0:2]  # get top two cards
    if cards[0][1] == 'white' and cards[1][1] == 'black': # check if first is white and second black
        white_black+=1
prob = white_black/number_experiments
print(prob)
0.25468

altenatively we coud do something more numerically such us considering all posible combinations of two cards and count how many of this combinations the first is white and the second is black

from itertools import combinations
import itertools

# make a deck of cards
cards_per_color = 26
deck = list(itertools.product([i for i in range(1, cards_per_color+1)],
                              ['white','black']))

# all combinations of 2 cards
all_possiblecombinations = list(combinations(deck, 2))

white_black = 0
for cards in all_possiblecombinations:
    if cards[0][1] == 'white' and cards[1][1] == 'black':
        white_black += 1

prob = white_black/len(all_possiblecombinations)
print(prob)
0.2647
Related