In [1]:
# importing
import numpy as np
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 ))
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 ) )
In [ ]: