Content and Objective

  • Confirm results derived in the lecture when analyzing probability of sum of two dice being greater than 9, conditioned on the result of first dice being even and odd
  • Dice are sampled and occurences of according events are being counted

Import


In [1]:
# importing
import numpy as np

Simulation


In [2]:
# number of trials
N_trials = int( 1e4 )

# counter of events "first dice even, first dice odd, sum of dice >= 9"
# counter of conditional events

# NOTE: Naming of events as on lecture slides

N_A = 0 
N_B = 0
N_C = 0

N_CA = 0
N_CB = 0


# loop for realizations
for _n in range( N_trials ):
    
    # sample 2 dice
    # NOTE: Upper limit is not included randint
    sample = np.random.randint( 1, 7, size=2 )
    
    # check for events; result: boolean
    first_even = ( sample[0] % 2 == 0 )
    sum_greater_9 = ( sum( sample ) >= 9 )
    
    # increase according counters
    
    ###
    # two methods for counting
    ###    

    # if you like "ifs" activate this one
    if 0:

        if first_even:
            N_A += 1
        else:
            N_B += 1

        if sum_greater_9:
            N_C += 1

            if first_even:
                N_CA += 1
            else:
                N_CB += 1
                
    # alternative solution    
    # as you can see N_CA, N_CB realize an "and" of events, so division by P(condition) has to be applied later on
    else:
        N_A += int( first_even )
        N_B += 1 - int( first_even )

        N_C += int( sum_greater_9 )

        N_CA += int( first_even ) * int( sum_greater_9 )
        N_CB += ( 1 - int( first_even ) ) * int( sum_greater_9 )
        

           
            
# print results         
print('Estimation of P(A):\t\t{}'.format( N_A / N_trials ))
print('Estimation of P(B):\t\t{}\n'.format( N_B / N_trials ))

print('Estimation of P(C|A):\t\t{}'.format( N_CA / N_A ))
print('Estimation of P(C|B):\t\t{}'.format( N_CB / N_B ))


Estimation of P(A):		0.5055
Estimation of P(B):		0.4945

Estimation of P(C|A):		0.333135509396637
Estimation of P(C|B):		0.21435793731041455

In [3]:
print('Estimation of P(C):\t\t{}'.format( N_C / N_trials ) )
print('P(C) by total probability:\t{}'.format( N_A / N_trials * N_CA / N_A + N_B / N_trials * N_CB / N_B ) )


Estimation of P(C):		0.2744
P(C) by total probability:	0.2744

Note:

Since we are dealing with Laplace experiments, we have $P(A)=|A|/|\Omega|$. So, conditional probabilities become $P(C|A)=\frac{\frac{|CA|}{|\Omega|}}{\frac{|A|}{|\Omega|}}=\frac{|CA|}{|A|}$.


In [ ]: