Probability Foundations
On this page
Probability theory studies situations that are subject to chance, such as rolling a die, drawing a card, or a network packet getting lost. Every probability problem is solved the same way: first capture the essence of the story in a probability model, then apply a small set of rules to that model. This note covers the model and the rules for combining events. Counting techniques for large sample spaces follow in "Combinatorics".
Building A Probability Model
Probability problems usually arrive as a story, and the first job is to strip the story down to a precise experiment.
The drinking problem. Frans and Fons cannot agree on who pays for the beer. The café owner rolls a die: on a , Frans pays, otherwise Fons pays. What is the probability that each man pays?
The experiment, described briefly but completely and unambiguously, is: "roll a fair die and count the pips". From there, every probability model has the same three ingredients.
Sample Space
When you perform an experiment, the outcome is unpredictable, but you can usually list every possible outcome. That set is the sample space or universe, written (omega). For the die:
This course restricts itself to finite sample spaces.
Events
An event is a subset of the sample space: a collection of one or more outcomes you are interested in. Events get capital letters , , .
An event with only one outcome, like "rolling a 1" , is an elementary event. Two events deserve special names:
| Event | Set | Example |
|---|---|---|
| Certain event | itself | Rolling a number less than |
| Impossible event | The empty set | Rolling a |
Thinking of events as sets is the key move of the whole subject: "A or B", "A and B" and "not A" become union, intersection and complement, and all of set theory becomes available.
Probabilities
The last ingredient assigns a number to every event : the probability that occurs. There are two standard ways to get these numbers.
Assigning Probabilities
The method depends on whether you can argue that all outcomes are equally likely.
Laplace's Rule
If is finite and all outcomes are equally likely, then every outcome has probability , and for any event
where means the number of elements in . For the die, each face has probability and . In problem texts, the word "randomly" is the signal that all outcomes are equally likely, so Laplace applies.
Both conditions matter. A weighted die has a finite but unequal outcomes, so Laplace fails there.
Relative Frequencies
When outcomes are not equally likely, or the sample space is not finite, you can estimate probabilities by repeating the experiment many times:
The more repetitions, the better the estimate (this is the law of large numbers). Since running a physical experiment millions of times is impractical, computers simulate it instead. This is the idea behind Monte Carlo methods.
import random
def estimate(trials):
hits = sum(1 for _ in range(trials) if random.randint(1, 6) >= 2)
return hits / trials
for n in (100, 10_000, 1_000_000):
print(n, estimate(n)) # drifts towards 5/6 = 0.8333...Properties Every Model Obeys
Whatever method you use, probabilities always satisfy these laws:
- for every event .
- The probabilities of all elementary outcomes add up to : .
- The probability of an event is the sum of the probabilities of its outcomes.
- The certain event has .
- The impossible event has .
A probability outside is always a sign of an error, which makes property 1 a free sanity check. For the drinking problem: and .
Combining Events
Events are sets, so combining them uses set operations, and Venn diagrams make the rules visible. The universe is drawn as a rectangle, events as shapes inside it that overlap when they share outcomes.
| In words | Set notation | Meaning |
|---|---|---|
| or | (union) | At least one of them happens |
| and | (intersection) | Both happen at the same time |
| not | (complement) | does not happen |
The Addition Rule
Roll a die, with = "odd" and = "at least 4" . Then and . But , which is too much. The outcome is in both events, so adding counts it twice. Subtract the overlap once to fix it:
Mutually Exclusive Events
When , the events cannot happen at the same time and are called mutually exclusive (or disjoint). There is no overlap to correct, so the rule simplifies to . Only for mutually exclusive events may you simply add probabilities.
The Complement Rule
The complement contains every outcome not in . An event and its complement are mutually exclusive and together fill , so :
This is one of the most useful tricks in the subject. Whenever is hard to compute directly, try instead. "At least one" problems are the classic case: the complement of "at least one success" is "no successes at all", which is usually a single easy product.
Conditional Probability
Probabilities change when you learn something. The chance of rain goes up when you see clouds. The chance of a on a die is , but if someone tells you the result was even, only remain and the chance becomes .
Notation And Intuition
, read "the probability of given ", is the probability that occurs when you already know has occurred. The key idea: knowing shrinks the universe to . Outcomes outside are no longer possible, so you recompute Laplace inside only.
The Sandwich Example
A bag holds 3 white sandwiches (ham, cheese, salami) and 2 brown ones (ham, cheese). You pick one at random. Let = "cheese" and = "white".
- Without information: .
- After glimpsing that it is white, only the 3 white sandwiches remain, and one is cheese: .
Rewriting the counting version in terms of probabilities gives the general definition:
Definition
For :
and symmetrically . Note that and are generally different: the probability that a spam email contains the word "free" is not the probability that an email containing "free" is spam. Confusing the two is a famous reasoning error.
Independence
Sometimes extra information changes nothing. Tossing a coin first does not change the probability of rolling a afterwards. In the sandwich example, on the other hand, knowing "white" changed the probability of "cheese" from to , so those events are dependent.
Definition
For events with non-zero probability, is independent of when
It can be shown that independence is symmetric: if is independent of , then also . In words, the occurrence or non-occurrence of one event does not affect the probability of the other.
Do not confuse independent with mutually exclusive. Mutually exclusive events (with non-zero probability) are actually strongly dependent: if one happens, you know for certain the other did not.
The Multiplication Rule
Rearranging the definition of conditional probability gives a formula for "A and B":
This is the multiplication rule. Read it as a story in time: first must happen, then, given that, must happen.
Independent Case
If and are independent, , and the rule simplifies to
Only use this simple product when you know the events are independent. Toss a coin and roll a die: .
Dependent Case
Draw two cards without replacement. The probability that the first is the King of Spades and the second the Queen of Clubs is
The second factor is , not , because one card is already gone. The draws are dependent.
Three Or More Events
The rule chains naturally. For independent events , and in general
Among 480 students, are male, of those smoke, and of those male smokers live in student housing. The probability that a random student is a male smoker in student housing is . Each percentage is conditional on the previous group, which is exactly the chain.
Bayes' Theorem
Bayes' theorem answers the reverse question. You know how likely an effect is for each possible cause, you observe the effect, and you want to know how likely each cause is.
The Machine Example
Two machines make and of all items. Of their output, and respectively is defective. An item is chosen at random. Let and be "made by machine 1 / 2" and "defective".
Question 1: how likely is a defect? Every defective item comes from exactly one machine, so splits into the two mutually exclusive pieces and . Add them, each computed with the multiplication rule:
This is the law of total probability.
Question 2: given a defect, how likely is machine 1? Use the definition of conditional probability, with the result of question 1 in the denominator:
Machine 1 makes most items, so even with the lower defect rate it is responsible for about of the defects.
General Form
If and form a partition of ( and ), then
The same formulas extend to partitions with three or more parts: the denominator just gets one term per part. In the tree picture, Bayes is simply "the path you care about, divided by all paths that lead to the observed effect".
Why Bayes Matters In CS
Spam filters, medical tests and classifiers all face the same question: given what I observed, how likely is the hypothesis? Suppose of email is spam, a filter catches of spam and wrongly flags of legitimate mail:
The rarer the thing you are detecting, the more the false positives dominate, which is why rare-disease screening tests produce so many false alarms even when they are very accurate.
Worked Exercises
These come from the probability exercise sets in the course book.
Unequal Probabilities
Jan, Piet and Tom are the only competitors. Jan's chance is twice Tom's, and Jan and Piet are equally likely. Let , then , so .
A die is weighted so the probability of each face is proportional to its number: with , so .
Addition And Complement
own a TV, a radio, both. Given a radio, the chance of also owning a TV is . Given a TV, the chance of not owning a radio is .
Multiplication With Independence
Two independent routers work with probabilities and , and a message needs both: .
Three independent servers are each up with probability , and the app needs at least one. Use the complement: .
Two intrusion detection systems catch an attack with probability and . The attack is missed only if both miss it: .
Bayes
of athletes ate spoiled fish. Of those, are sick the next day, versus of the others. , and .
A classifier sees cats, detects a cat with probability , and calls a dog a cat with probability . .
Quick Recap
- A probability model = sample space + events (subsets) + probabilities.
- Laplace: , only for finite with equally likely outcomes ("randomly").
- Relative frequency estimates by repetition or simulation.
- , , .
- Addition rule: . Plain addition only for mutually exclusive events.
- Complement: . Use it for "at least one".
- Conditional: . The condition becomes the new universe. in general.
- Independent: . Not the same as mutually exclusive.
- Multiplication rule: , which becomes only under independence.
- Bayes: total probability in the denominator, one path in the numerator. Answers "which cause, given the effect?".