Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Implementing Probability Basics in Python | Probability & Statistics
Mathematics for Data Science

bookImplementing Probability Basics in Python

Probability concepts are the foundation of analyzing uncertain events.
Here we learn how to compute union and intersection using a simple dice example.

Defining Sample Space and Events

# Small numbers on a die
A = {1, 2, 3}

# Even numbers on a die  
B = {2, 4, 6}  

die_outcomes = 6

Here we define:

  • A={1,2,3}A = \{1,2,3\} representing "small" outcomes;
  • B={2,4,6}B = \{2,4,6\} representing "even" outcomes.

The total number of die outcomes is 6.

Performing Set Operations

12345678
# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 print(f'A and B = {A & B}') # {2} print(f'A or B = {A | B}') # {1, 2, 3, 4, 6}
copy
  • The intersection AB={2}A \cap B = \{2\} → common element.
  • The union AB={1,2,3,4,6}A \cup B = \{1,2,3,4,6\} → all elements in A or B.

Calculating Probabilities

123456789101112131415161718
# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 A_and_B = A & B # {2} A_or_B = A | B # {1, 2, 3, 4, 6} P_A = len(A) / die_outcomes P_B = len(B) / die_outcomes P_A_and_B = len(A_and_B) / die_outcomes P_A_or_B = P_A + P_B - P_A_and_B print("P(A) =", P_A) print("P(B) =", P_B) print("P(A ∩ B) =", P_A_and_B) print("P(A ∪ B) =", P_A_or_B)
copy

We use the formulas:

  • P(A)=A6=36P(A) = \frac{|A|}{6} = \tfrac{3}{6};
  • P(B)=B6=36P(B) = \frac{|B|}{6} = \tfrac{3}{6};
  • P(AB)=AB6=16P(A \cap B) = \frac{|A \cap B|}{6} = \tfrac{1}{6};
  • P(AB)=P(A)+P(B)P(AB)=56P(A \cup B) = P(A) + P(B) - P(A \cap B) = \tfrac{5}{6}.

Additional Set Details

12345
only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
copy
  • Elements only in A: {1, 3};
  • Elements only in B: {4, 6}.

1. What is the output of this code?

2. What does this line compute?

3. What is the result of this code?

4. Which line correctly calculates the union probability using Python?

5. What does this code return?

question mark

What is the output of this code?

Select the correct answer

question mark

What does this line compute?

Select the correct answer

question mark

What is the result of this code?

Select the correct answer

question mark

Which line correctly calculates the union probability using Python?

Select the correct answer

question mark

What does this code return?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the inclusion-exclusion principle in more detail?

How do you visualize the union and intersection using a Venn diagram?

What happens if the two events are mutually exclusive?

Awesome!

Completion rate improved to 1.89

bookImplementing Probability Basics in Python

Свайпніть щоб показати меню

Probability concepts are the foundation of analyzing uncertain events.
Here we learn how to compute union and intersection using a simple dice example.

Defining Sample Space and Events

# Small numbers on a die
A = {1, 2, 3}

# Even numbers on a die  
B = {2, 4, 6}  

die_outcomes = 6

Here we define:

  • A={1,2,3}A = \{1,2,3\} representing "small" outcomes;
  • B={2,4,6}B = \{2,4,6\} representing "even" outcomes.

The total number of die outcomes is 6.

Performing Set Operations

12345678
# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 print(f'A and B = {A & B}') # {2} print(f'A or B = {A | B}') # {1, 2, 3, 4, 6}
copy
  • The intersection AB={2}A \cap B = \{2\} → common element.
  • The union AB={1,2,3,4,6}A \cup B = \{1,2,3,4,6\} → all elements in A or B.

Calculating Probabilities

123456789101112131415161718
# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 A_and_B = A & B # {2} A_or_B = A | B # {1, 2, 3, 4, 6} P_A = len(A) / die_outcomes P_B = len(B) / die_outcomes P_A_and_B = len(A_and_B) / die_outcomes P_A_or_B = P_A + P_B - P_A_and_B print("P(A) =", P_A) print("P(B) =", P_B) print("P(A ∩ B) =", P_A_and_B) print("P(A ∪ B) =", P_A_or_B)
copy

We use the formulas:

  • P(A)=A6=36P(A) = \frac{|A|}{6} = \tfrac{3}{6};
  • P(B)=B6=36P(B) = \frac{|B|}{6} = \tfrac{3}{6};
  • P(AB)=AB6=16P(A \cap B) = \frac{|A \cap B|}{6} = \tfrac{1}{6};
  • P(AB)=P(A)+P(B)P(AB)=56P(A \cup B) = P(A) + P(B) - P(A \cap B) = \tfrac{5}{6}.

Additional Set Details

12345
only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
copy
  • Elements only in A: {1, 3};
  • Elements only in B: {4, 6}.

1. What is the output of this code?

2. What does this line compute?

3. What is the result of this code?

4. Which line correctly calculates the union probability using Python?

5. What does this code return?

question mark

What is the output of this code?

Select the correct answer

question mark

What does this line compute?

Select the correct answer

question mark

What is the result of this code?

Select the correct answer

question mark

Which line correctly calculates the union probability using Python?

Select the correct answer

question mark

What does this code return?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2
some-alt