Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Implementering av Sannolikhetsgrunder i Python | Sannolikhet & Statistik
Matematik för Data Science

bookImplementering av Sannolikhetsgrunder i Python

Definiera utfallsrum och händelser

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

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

die_outcomes = 6

Här definierar vi:

  • A={1,2,3}A = \{1,2,3\} som representerar "små" utfall;
  • B={2,4,6}B = \{2,4,6\} som representerar "jämna" utfall.

Det totala antalet utfall för tärningen är 6.

Utföra mängdoperationer

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
  • Snittet AB={2}A \cap B = \{2\} → gemensamt element.
  • Unionen AB={1,2,3,4,6}A \cup B = \{1,2,3,4,6\} → alla element i A eller B.

Beräkning av sannolikheter

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

Vi använder formlerna:

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

Ytterligare mängddetaljer

12345
only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
copy
  • Element endast i A: {1, 3};
  • Element endast i B: {4, 6}.
question mark

Vad blir utdata från denna kod?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

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

How do you visualize these probabilities using a Venn diagram?

What happens if the events A and B are mutually exclusive?

Awesome!

Completion rate improved to 1.96

bookImplementering av Sannolikhetsgrunder i Python

Svep för att visa menyn

Definiera utfallsrum och händelser

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

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

die_outcomes = 6

Här definierar vi:

  • A={1,2,3}A = \{1,2,3\} som representerar "små" utfall;
  • B={2,4,6}B = \{2,4,6\} som representerar "jämna" utfall.

Det totala antalet utfall för tärningen är 6.

Utföra mängdoperationer

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
  • Snittet AB={2}A \cap B = \{2\} → gemensamt element.
  • Unionen AB={1,2,3,4,6}A \cup B = \{1,2,3,4,6\} → alla element i A eller B.

Beräkning av sannolikheter

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

Vi använder formlerna:

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

Ytterligare mängddetaljer

12345
only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
copy
  • Element endast i A: {1, 3};
  • Element endast i B: {4, 6}.
question mark

Vad blir utdata från denna kod?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 2
some-alt