<jupyter_start><jupyter_text>Operations on EventsSometimes, complex event is defined by many simpler event. So we need operations to represent and calculate probiability of complex events.- SUM : C = A + B , means A or B or both occur- MULTIPLYING : D = A x B , means A occur then B occur<jupyter_code>import random<jupyter_output><empty_output><jupyter_text>SUM![Ven Diagram](../../draws/lesson2-ven-diagram.svg)As we can see :- Circle A is when A occurs, P(A) = area of circle A- Circle B is when B occurs, P(B) = area of circle B- Union between of A and B is event C occurs, P(C) = X1 + X3 + X2 = P(A) + P(B) - X2- Intersect A and B is event D occurs, P(D) = X2<jupyter_code># SUM

ROLL_DICE_EVENTS = [1, 2, 3, 4, 5, 6]

N = 100000
n_A = 0 # event A : dice < 4
n_B = 0 # event B : dice is even
n_C = 0 # event C = A + B : dice even or < 4 or both
n_D = 0 # event D = A * B : dice even and < 4

for i in range(N):
    exp_result = random.choice(ROLL_DICE_EVENTS)
    if exp_result < 4:
        n_A = n_A + 1
    if exp_result % 2 == 0:
        n_B = n_B + 1
    if exp_result < 4 or exp_result % 2 == 0:
        n_C = n_C + 1
    if exp_result < 4 and exp_result % 2 == 0:
        n_D = n_D + 1

p_A = n_A / N
p_B = n_B / N
p_C = n_C / N
p_D = n_D / N
print(f"P(A) = n_A / N = {n_A} / {N} = {p_A:.3f}")
print(f"P(B) = n_B / N = {n_B} / {N} = {p_B:.3f}")
print(f"P(C) = n_C / N = {n_C} / {N} = {p_C:.3f}")
print(f"P(D) = n_D / N = {n_D} / {N} = {p_D:.3f}")
print(f"P(C) = P(A+B) = P(A) + P(B) - P(D) = {p_A + p_B - p_D:.3f}")<jupyter_output>P(A) = n_A / N = 50050 / 100000 = 0.500
P(B) = n_B / N = 50010 / 100000 = 0.500
P(C) = n_C / N = 83542 / 100000 = 0.835
P(D) = n_D / N = 16518 / 100000 = 0.165
P(C) = P(A+B) = P(A) + P(B) - P(D) = 0.835<jupyter_text>MULTIPLYING<jupyter_code>ROLL_DICE_EVENTS = [1, 2, 3, 4, 5, 6]

N = 100000
n_A = 0 # event A : first dice = 1
n_B = 0 # event B : second dice = 6
n_C = 0 # event C : first dice = 1 then second = 6

for i in range(N):
    first = random.choice(ROLL_DICE_EVENTS)
    second = random.choice(ROLL_DICE_EVENTS)
    if first == 1:
        n_A = n_A + 1
    if second == 6:
        n_B = n_B + 1
    if first == 1 and second == 6:
        n_C = n_C + 1

p_A = n_A / N
p_B = n_B / N
p_C = n_C / N
print(f"P(A) = n_A / N = {n_A} / {N} = {p_A:.3f}")
print(f"P(B) = n_B / N = {n_B} / {N} = {p_B:.3f}")
print(f"P(C) = n_C / N = {n_C} / {N} = {p_C:.3f}")

# P(C) = P(A then B) = P(A) . P(B)
# We can see, second dice is independent to first dice
N = 100000
n_A = 0 # event A : first card = 1
n_B = 0 # event B : second card = 3
n_C = 0 # event C : first card = 1 then second = 3

for i in range(N):
    DECK_CARDS = [1, 2, 3]
    first = random.choice(DECK_CARDS)
    DECK_CARDS.remove(first)
    second = random.choice(DECK_CARDS)
    if first == 1 and second == 3:
        n_C = n_C + 1

p_C = n_C / N
print(f"P(C) = n_C / N = {n_C} / {N} = {p_C:.3f}")

# We can see, second card depends on first card, because deck of card when I take the second is removed the first card
# P(C) = P(A then B) = P(A) . P(B) = 1 / N * 1 / (N - 1) = 1/3 * 1/2 = 1/6 = 0.1667<jupyter_output>P(C) = n_C / N = 16731 / 100000 = 0.167
{'fraction_non_alphanumeric': 0.16592780240658644, 'fraction_numerical': 0.05858138062064598, 'mean_word_length': 2.5217391304347827, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '286638', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1461, 'n_words': 460, 'path': 'books/probability/lession2-operations-on-events.ipynb', 'repo_name': 'khanhicetea/learn-probability-statistics', 'size': '3158'}