AI Hints
Statistics & Math for ML · Lesson 28 of 138

And, Or, Not

Combining events.

⏱ 2 min read▶ Video at 31:36🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

Real questions combine events. What's the chance of drawing a heart, or a king? What's the chance of not getting spam? A few simple rules handle all of them.

And, Or, Not
From the video: And, Or, Not
  • Heart or king?
  • Not spam?
  • A few simple rules

A deck of cards

We build a deck of fifty two cards, from thirteen ranks and four suits. Then we count. Thirteen hearts, and four kings. But heart or king isn't thirteen plus four, because the king of hearts would be counted twice. Run it. Sixteen cards, so four out of thirteen.

cards.pypython
from fractions import Fraction
from itertools import product

ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
deck = list(product(ranks, ["hearts", "spades", "clubs", "diamonds"]))

def P(test):
    return Fraction(sum(test(c) for c in deck), len(deck))

print("hearts:", P(lambda c: c[1] == "hearts"))
print("kings: ", P(lambda c: c[0] == "K"))
print("either:", P(lambda c: c[1] == "hearts" or c[0] == "K"))
Output
hearts: 1/4
kings:  1/13
either: 4/13

The three rules

Not A is one minus the probability of A. A or B is the probability of A, plus B, minus the overlap, so we don't count it twice. And if A and B can't both happen, the overlap is zero, so you just add. We'll meet the rule for and in the next lesson.

RuleFormula
not A1 − P(A)
A or BP(A) + P(B) − P(A and B)
can't both happenP(A) + P(B)

Summary

Not is one minus, and or is add, then subtract the overlap. Next, the and rule, for independent events.

Key takeaways
  • P(not A) = 1 − P(A)
  • Or: add, minus overlap
  • Don't double count
Next up: Independent Events — multiply them