Module 2, topic 16, week 8, Thursday
Probability basics: sample space, events, conditional probability
Why probability matters in this course, then probability from the beginning: outcomes and events, the rules for "not", "or" and "and", counting, conditional probability, independence and Bayes' theorem. Worked by hand.
Introduction: the first lesson on probability
The fourth lesson of Module 2, and the first one on probability. Linear algebra described data and models with exact numbers. But data is never exact: a measurement has noise, a sample is only a part of the whole, and a model is never sure. Probability is the mathematics of uncertainty: it gives a number to "how likely", and rules for calculating with such numbers. This guide first explains why probability matters in this course and where you will meet it again. Then it teaches probability from the very beginning: experiments and their outcomes, events as sets, what a probability is, the rules for "not", "or" and "and", how to count outcomes, and the most important idea of the lesson, conditional probability: how a probability changes when we learn something. From it come independence, the law of total probability and Bayes' theorem. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.
You need only school arithmetic: fractions, decimals and percentages. A calculator helps with the larger numbers. As in the whole module, the work is done by hand; Python appears only in one short part near the end.
What you will learn
- 1.Why probability is the language of uncertainty in data science and machine learning
- 2.Experiments, outcomes, the sample space and events
- 3.Events as sets: "or", "and", "not", mutually exclusive events, Venn diagrams
- 4.What a probability is: equally likely outcomes, relative frequency, and the three basic rules
- 5.The complement rule, the addition rule and the "at least one" trick
- 6.Counting: the multiplication principle, factorials, arrangements and combinations
- 7.Conditional probability, the multiplication rule and tree diagrams
- 8.Independent events, and why "independent" is not "mutually exclusive"
- 9.The law of total probability and Bayes' theorem
- 10.Tables of two features: joint, marginal and conditional probabilities, and the confusion matrix of a classifier
- 11.Probability in machine learning (a Naive Bayes spam filter), and a first look at random variables and the expected value
| Written | Meaning |
|---|---|
| the sample space: the set of all outcomes | |
| , | events |
| the probability of the event | |
| or (at least one of them happens) | |
| and (both happen) | |
| not (the complement of ) | |
| the probability of given that has happened | |
| factorial: | |
| the number of ways to choose things out of ("n choose k") |
A probability is written as a fraction (), as a decimal () or as a percentage (16.7 percent). All three mean the same.
1Why probability?
1.1A model is never sure
Ask a weather service about tomorrow and it does not say "rain". It says "70 percent chance of rain". A good machine learning model speaks in the same way. A spam filter does not know that an email is spam; it calculates that it is spam with the probability 0.97. A model that reads X-ray images gives a probability for every disease. A language model chooses its next word from a list of probabilities.
There are three reasons why uncertainty cannot be avoided:
- The data is a sample. We see 10,000 customers, not all customers that will ever come.
- The data has noise. Two flats with the same area, rooms and age are sold for different prices.
- The future is open. Nobody knows whether this customer will pay the loan back. We only know how often similar customers did.
Probability is the tool that makes it possible to reason exactly about things that are not exact.
1.2Where you will meet it again in this course
| Later in the course | The probability idea behind it |
|---|---|
| Probability distributions (Topic 17) | the probabilities of all values of a quantity at once |
| Hypothesis testing (Topic 19) | "how likely is this result if nothing special is going on?" |
| Logistic regression (Topic 27) | the output of the model is a probability between 0 and 1 |
| Classification metrics (Topic 28) | precision and recall are conditional probabilities |
| Train/test split, cross-validation (Topic 23) | the rows are chosen at random |
| Random forests (Topic 30) | every tree sees a random sample of the data |
| Naive Bayes (Topic 34) | Bayes' theorem as a classifier |
| Neural networks and language models (Module 5) | the last layer gives a probability for every class or every next word |
1.3Why by hand?
A library can simulate a million dice in a second. But only your own calculations with small examples build the feeling for which rule a problem needs: is it "or" or "and"? Does the first event change the second? Which way round is the condition? These questions decide everything, and no library answers them for you.
2Experiments, outcomes and events
2.1The words
- An experiment is any process whose result is not known in advance: tossing a coin, rolling a die, choosing a customer at random, measuring tomorrow's temperature.
- An outcome is one possible result of the experiment.
- The sample space is the set of all outcomes.
- An event is a set of outcomes: a part of the sample space. The event happens when the outcome of the experiment is one of its outcomes.
| Experiment | Sample space | Number of outcomes |
|---|---|---|
| Tossing a coin | 2 | |
| Rolling a die | 6 | |
| Tossing two coins | 4 | |
| Rolling two dice | all pairs from to | 36 |
| Drawing a card | the 52 cards of a deck | 52 |
Example. A die is rolled. Some events:
If the die shows 6, all three events happen. If it shows 4, only happens.
2.2Two special events
- The whole sample space is the certain event: it always happens.
- The empty set, written , is the impossible event: it never happens ("the die shows 7").
2.3Listing outcomes carefully: two dice
With two dice, and are different outcomes: think of a red die and a blue die. There are outcomes, and they are all equally likely. The table shows the sum of the two dice for every outcome:
The event "the sum is 7" has the six outcomes on the marked diagonal: , , , , , . This table will be used many times.
Choose an event of two dice. Its outcomes are filled: count them.
The event has 6 of the 36 outcomes: P = 6/36 = 1/6 ≈ 0.167
The rows are the first die, the columns the second. (2, 5) and (5, 2) are two different outcomes.
A standard deck of cards will also appear often: 52 cards in four suits (hearts and diamonds are red, spades and clubs are black), 13 cards in every suit: 2 to 10, jack, queen, king, ace.
Try it yourself
Show a hint
Show the solution
Answer: 16 outcomes. "Both equal" has 4 outcomes, and "the sum is 5" has 4 outcomes.
The two events have no outcome in common: two equal numbers have an even sum.
3Events as sets
3.1Or, and, not
Events are sets, so they can be combined like sets. Three operations are enough.
| Written | Read | It happens when |
|---|---|---|
| or (the union) | at least one of the two happens (also when both do) | |
| and (the intersection) | both happen | |
| not (the complement) | does not happen |
With the events of the die, and :
Notice the word "or". In everyday language it often means "one or the other, but not both". In probability, " or " always includes the case that both happen.
3.2Mutually exclusive events
Two events that cannot happen together are called mutually exclusive (or disjoint): . "The die shows an even number" and "the die shows a 5" are mutually exclusive. An event and its complement are always mutually exclusive.
3.3Venn diagrams
A Venn diagram shows events as regions inside the sample space:
A ∪ B
A or B
A ∩ B
A and B
Ac
not A
mutually exclusive
A ∩ B is empty
A die is rolled. A is "even", B is "larger than 4". Choose an event and see its outcomes.
A ∪ B = {2, 4, 5, 6}: even or larger than 4
P(A ∪ B) = 4/6 = 2/3 ≈ 0.667
3.4Two rules that connect the operations
Two rules connect the three operations (they are called De Morgan's rules):
The first rule says: "not ( or )" is "not and not ". The second says: "not ( and )" is "not or not ". In words: "neither of them" is the opposite of "at least one of them".
Try it yourself
Show a hint
Show the solution
Answer: 2, 6, 4 and 2 outcomes.
The last set is also : the numbers larger than 4 that are odd. That is De Morgan's rule.
4What is a probability?
4.1Equally likely outcomes
When all outcomes of an experiment are equally likely (a fair coin, a fair die, a well-shuffled deck), the probability of an event is a count:
This is called the classical definition. It only works when the outcomes really are equally likely. "Tomorrow it rains or it does not" has two outcomes, but their probabilities are not each.
4.2Probability as a long-run share
What does "the probability of heads is " say about the real world? Not that 10 tosses give exactly 5 heads. It says that in a long series of tosses the share of heads comes closer and closer to :
At the beginning the share jumps wildly. After hundreds of tosses it settles near 0.5. This fact is called the law of large numbers, and it is the bridge between probability and data: a probability can be estimated by a relative frequency.
Toss a coin many times and watch the share of heads.
No tosses yet.
Every run is different at the start, and every run ends near 0.5. Up to 10,000 tosses.
If 230 of 5,000 customers cancelled their contract last year, the estimated probability that a customer cancels is . Almost every probability in data science is found in this way: from data.
4.3The three basic rules
Whatever way a probability is found, it must obey three rules:
| Rule | In symbols | In words |
|---|---|---|
| 1 | a probability is a number between 0 and 1 | |
| 2 | something always happens | |
| 3 | for mutually exclusive and | chances of events that cannot happen together add up |
means "impossible", means "certain". Everything else in this lesson follows from these three rules. A consequence that is used all the time: the probabilities of all outcomes add up to 1.
Try it yourself
Show a hint
Show the solution
Answer: , , and the estimate for the machine is .
The first two are exact: the balls are equally likely. The third is an estimate from data, and more parts would make it more reliable.
5The rules for "not" and "or"
5.1The complement rule
and are mutually exclusive, and together they are the whole sample space. So :
5.2The addition rule
For mutually exclusive events the probabilities add (rule 3). When the events can happen together, the outcomes in would be counted twice, so they are subtracted once:
Example 1. The die, with = even and = larger than 4:
The check by counting: has 4 of the 6 outcomes.
Example 2. A card is drawn. What is the probability of a king or a heart?
The king of hearts is a king and a heart. Without the subtraction it would be counted twice.
5.3"At least one" is the complement of "none"
Questions with "at least one" are almost always easier through the complement.
Example. Two dice are rolled. What is the probability of at least one six? There are outcomes without a six.
Counting directly is possible too (the row and the column of the six in the table, 11 outcomes), but with more dice only the complement stays easy.
Try it yourself
Show a hint
Show the solution
Answer: and .
The second calculation counts pairs: of the 36 pairs have no number larger than 4.
6Counting
For equally likely outcomes, probability is counting. When the sample space is large, nobody can list it, and rules for counting are needed.
6.1The multiplication principle
If a choice is made in steps, with ways for the first step and ways for the second, there are ways in all.
- 3 shirts and 4 pairs of trousers: outfits
- a PIN of 4 digits: codes
- two dice: outcomes
6.2Arranging things in a row: the factorial
In how many orders can 5 books stand on a shelf? There are 5 choices for the first place, 4 for the second, and so on:
This product is written and read "5 factorial".
| 1 | 2 | 3 | 4 | 5 | 6 | 10 | |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 6 | 24 | 120 | 720 | 3,628,800 |
Factorials grow very fast.
6.3Choosing some, in order
Eight runners are in a race. In how many ways can the gold, silver and bronze medals be given?
The product starts like a factorial and stops after 3 factors. In general, the number of ways to choose out of things when the order matters is
Such an ordered choice is called a permutation.
6.4Choosing some, without order: combinations
In how many ways can a team of 3 be chosen from 8 people? Now the order does not matter: the team Aysel, Kamran, Leyla is the same team as Leyla, Aysel, Kamran. The 336 ordered choices count every team times, once for every order of its members. So
- A K L
- A L K
- K A L
- K L A
- L A K
- L K A
6 orders, 1 team:
{A, K, L}
The number of ways to choose out of things when the order does not matter is written , read "n choose k". Many books write it as .
Such a choice is called a combination. A practical way to calculate it: factors counting down from on top, below.
The last number is the number of tickets of a "6 out of 49" lottery. Useful facts: , , and (choosing 3 people for the team is choosing 5 people who stay out).
Choose k things out of n. How many ways are there?
All 8 in a row: 8! = 40,320
3 of 8, the order matters: 8 × 7 × 6 = 336
3 of 8, the order does not matter: C(8, 3) = 336 / 3! = 336 / 6 = 56
Every choice without order was counted 6 times among the ordered ones: once for every order of its 3 members.
6.5Which rule?
| Question | Rule | Example |
|---|---|---|
| Steps with separate choices | multiply | 3 shirts, 4 trousers: |
| All things in a row | 5 books: | |
| of , the order matters | 3 medals for 8 runners: | |
| of , the order does not matter | a team of 3 from 8: |
6.6Probability with counting
Example. Two cards are drawn from a deck. What is the probability that both are aces?
Example. The probability of winning the "6 out of 49" lottery with one ticket is , about 0.00000007.
Try it yourself
Show a hint
Show the solution
Answer: (a) 5,040. (b) 21. (c) .
The result of (c) agrees with common sense: 2 of the 7 books are taken.
7Conditional probability
7.1Information changes a probability
A die is rolled, and you cannot see it. The probability of a six is . Now somebody tells you: "the number is even". The six has become more likely: only 2, 4 and 6 are still possible, and one of the three is the six.
This is a conditional probability. It is written and read "the probability of given ". The event is the information; it becomes the new, smaller sample space.
7.2The formula
Of all the cases in which happens, we take the share in which happens too. For the die: and , so
7.3A table of data
100 students took an exam. Some of them had done the homework during the term:
| Passed | Failed | Total | |
|---|---|---|---|
| Did the homework | 54 | 6 | 60 |
| Did not | 20 | 20 | 40 |
| Total | 74 | 26 | 100 |
A student is chosen at random.
For the second probability only the first row counts, for the third only the second row. With the formula: . The condition picks a row (or a column) of the table, and the probability is calculated inside it.
Choose a probability. The group we look at is outlined, the students that are counted are filled.
P(passed | homework) = 54/60 = 9/10 = 0.9
The condition "did the homework" leaves only the first row: 60 students, of whom 54 passed.
7.4The order matters
and are different questions with different answers:
The first looks at the students who did the homework, the second at the students who passed. Mixing up the two is the most common mistake of the whole subject (Part 9 returns to it).
7.5The multiplication rule
Multiply the formula by :
The probability that both happen is the probability of the first, times the probability of the second given the first.
Example. Two cards are drawn without putting the first one back. Both are aces:
After the first ace, 3 aces are left among 51 cards. The result is the same as with combinations in Part 6.
7.6Tree diagrams
A bag holds 5 red and 3 blue balls. Two balls are drawn, one after the other, without putting the first one back. A tree shows all the paths. On every branch stands a conditional probability, and the probability of a path is the product of its branches:
The four paths are mutually exclusive, and their probabilities add up to . Two rules of the tree: multiply along a path, add different paths.
Try it yourself
Show a hint
Show the solution
Answer: , and .
| New | Used | Total | |
|---|---|---|---|
| Electric | 15 | 5 | 20 |
| Petrol | 15 | 45 | 60 |
| Total | 30 | 50 | 80 |
The same 15 cars are counted in both conditional probabilities, once among the 20 electric cars and once among the 30 new cars.
8Independent events
8.1The definition
Sometimes information changes nothing. A coin is tossed and a die is rolled: knowing that the coin shows heads says nothing about the die. Two events are independent when one does not change the probability of the other:
Put this into the multiplication rule, and it becomes a simple product. and are independent exactly when
8.2Testing independence
Whether two events are independent is decided by calculating, not by feeling. A die, with = even:
and are independent, and are not. Knowing "at most 4" leaves the chance of an even number at . Knowing "larger than 3" raises it to .
8.3With and without putting back
The bag with 5 red and 3 blue balls again:
With putting back the two draws are independent. Without it the second draw depends on the first. Choosing rows of a large data set at random is practically "with putting back": taking one customer out of a million hardly changes the rest.
8.4Independent is not mutually exclusive
These two ideas are often mixed up, and they are almost opposites.
| Mutually exclusive | Independent | |
|---|---|---|
| Meaning | they cannot happen together | one tells nothing about the other |
| Used for |
If two events with positive probabilities are mutually exclusive, they are not independent: when one happens, the other is certain not to happen. That is a lot of information.
8.5Many independent events
For several independent events the probabilities multiply:
Together with the complement this gives the "at least one" pattern for repeated trials:
A server fails on a given day with the probability 0.1. With three servers that fail independently, the service is down only if all three fail: . The probability that at least one works is .
Try it yourself
Show a hint
Show the solution
Answer: (a) Yes, they are independent. (b) 0.488.
Each part alone fails rarely, but with three parts a failure somewhere is almost as likely as not.
9The law of total probability and Bayes' theorem
9.1Splitting into cases
A factory has two machines. Machine 1 makes 60 percent of the parts, and 2 percent of its parts are defective. Machine 2 makes 40 percent of the parts, and 5 percent of them are defective. What share of all parts is defective?
A part comes either from machine 1 or from machine 2. Calculate each case with the multiplication rule and add. With and for the machines and for "defective":
This is the law of total probability: if the cases are mutually exclusive and cover everything, then
In a tree it is the rule "add different paths".
9.2Turning the condition around
A defective part is found. What is the probability that it came from machine 2? We know , but the question asks for : the condition the other way round.
Machine 2 makes only 40 percent of the parts, but 62.5 percent of the defective ones.
This calculation is Bayes' theorem:
where is usually found with the law of total probability. The three parts have names that you will meet again:
| Name | In the example | Meaning |
|---|---|---|
| prior | what we believed before the information | |
| likelihood | how well the information fits this case | |
| posterior | what we believe after the information |
Bayes' theorem is the rule for learning from evidence: it updates a belief when new information arrives.
9.3A medical test
A disease affects 1 percent of the population. A test finds 95 percent of the ill people (it is positive for them), but it is also positive for 5 percent of the healthy people. A person tests positive. What is the probability that the person is ill?
Most people answer "95 percent". Count 10,000 people instead:
Only about 16 percent. The same with Bayes' theorem:
The reason is the small prior: the disease is rare, so the few ill people are outnumbered by the false alarms among the many healthy ones. Forgetting the prior is called the base rate fallacy. And notice again: and are very different numbers.
Counting people, as in the tree, is the safest way to solve such problems.
A test for 10,000 people. Change the three numbers and watch the share of ill people among the positive tests.
Percent of people who are ill
Percent of the ill with a positive test
Percent of the healthy with a positive test
Positive tests: 95 + 495 = 590
P(ill | positive) = 95 / 590 = 0.161
The rarer the disease, the smaller the answer, although the test stays the same: the base rate matters. With 0 percent false alarms every positive test is a real one.
Try it yourself
Show a hint
Show the solution
Answer: 2 percent of all orders are sent back, and .
Phone orders are only a fifth of all orders, but three fifths of the orders that come back.
10Tables of two features
10.1Joint, marginal, conditional
A spam filter was tested on 200 emails:
| Predicted spam | Predicted not spam | Total | |
|---|---|---|---|
| Really spam | 48 | 12 | 60 |
| Really not spam | 14 | 126 | 140 |
| Total | 62 | 138 | 200 |
Dividing by 200 turns the counts into probabilities. Three kinds can be read from such a table:
| Kind | Example | From the table |
|---|---|---|
| joint: both at once | ||
| marginal: one feature alone | ||
| conditional: inside a row or a column |
The marginal probabilities stand in the margins of the table, the row "Total" and the column "Total". That is where the name comes from.
10.2The confusion matrix
This table is the confusion matrix of the classifier (Topic 28), and the usual measures of a classifier are probabilities:
Precision and recall are the two directions of one condition. Precision asks: "of the emails the filter called spam, how many are spam?" Recall asks: "of the spam emails, how many did the filter catch?" It is the pair and again.
precision = 48 / 62 ≈ 0.774
recall = 48 / 60 = 0.80
Try it yourself
Show a hint
Show the solution
Answer: Accuracy 0.7, precision 0.6, recall 0.75.
| Called "cat" | Called "no cat" | Total | |
|---|---|---|---|
| Really a cat | 30 | 10 | 40 |
| Really no cat | 20 | 40 | 60 |
| Total | 50 | 50 | 100 |
The model finds three of four cats, but four of ten "cat" answers are wrong.
11Probability in machine learning
11.1A spam filter with Bayes' theorem
30 percent of all emails are spam. The word "free" appears in 60 percent of the spam emails and in 5 percent of the other emails. An email contains "free". How likely is it spam?
One word raised the probability from 0.30 to about 0.84.
11.2More words: the "naive" idea
A real email has many words. The word "meeting" appears in 10 percent of the spam emails and in 40 percent of the others. An email contains "free" and "meeting". The Naive Bayes classifier (Topic 34) makes a bold simplification: inside each class it treats the words as independent, so that their probabilities can be multiplied.
The word "meeting" pulled the probability back down. The assumption of independence is rarely exactly true (that is why the method is called naive), but the classifier works surprisingly well, and you can now calculate it by hand.
11.3More places
| Where | The probability |
|---|---|
| A classifier's output | , a conditional probability |
| The decision rule | predict "spam" when this probability is larger than a threshold, usually 0.5 |
| A language model | |
| A random train/test split | every row lands in the test set with the same probability |
| A recommendation |
Try it yourself
Show a hint
Show the solution
Answer: The products are 0.01 for spam and 0.0075 for not spam, and .
"Prize" speaks for spam, "tomorrow" a little against it. Together they raise the probability from 0.25 to about 0.57.
12A look ahead: random variables and the expected value
12.1A number that depends on chance
Often the outcome of an experiment is a number, or is turned into one: the number on a die, the number of heads in three tosses, the money won in a game. Such a quantity is called a random variable. Listing its values with their probabilities gives its distribution:
| Value | 0 | 1 | 2 |
|---|---|---|---|
| Probability |
The probabilities of a distribution add up to 1. Distributions are the subject of the next lesson.
12.2The expected value
The expected value is the average result in the long run. Multiply every value by its probability and add:
For a die:
No die ever shows 3.5. The expected value is not the value to expect in one roll; it is the average of many rolls.
Example. A game costs nothing to enter. You roll a die: with a six you win 8, otherwise you lose 2.
On average you lose about 0.33 per game. One game can be won; a thousand games will lose about 330.
The expected value is how decisions under uncertainty are compared, and the "loss" that a model minimizes during training is an expected value too.
Try it yourself
Show a hint
Show the solution
Answer: The expected win is 3, and the average result is per ticket.
The three probabilities add up to 1: . On average the buyer loses 1 per ticket, although a single ticket can win 40.
13Common mistakes
Adding probabilities of events that can happen together. is not . Subtract the king of hearts: .
Multiplying probabilities of events that are not independent. is not when the first card is not put back. Use .
Mixing up and . "95 percent of the ill people test positive" does not mean "95 percent of the positive people are ill".
Forgetting the base rate. When something is rare, most alarms are false alarms, even with a good test.
Mixing up "mutually exclusive" and "independent". Exclusive events cannot happen together. Independent events tell nothing about each other. Exclusive events are never independent (unless one of them is impossible).
Treating outcomes as equally likely when they are not. The sum of two dice has 11 possible values, but is not . The 36 pairs are equally likely, the sums are not.
Forgetting that order creates outcomes. and are two outcomes of two dice.
Believing that chance has a memory. After five heads in a row, the next toss of a fair coin is heads with the probability , as always. The coin does not "owe" a tail. (This mistake is called the gambler's fallacy.)
A probability outside 0 to 1. If a calculation gives or , something was added or subtracted wrongly.
Using permutations for a choice without order. A team or a hand of cards has no order: use .
"At least one" by adding. is not . Use .
| Mistake | What is right |
|---|---|
| always | subtract unless the events are mutually exclusive |
| always | only for independent events; otherwise |
| they are different; Bayes' theorem connects them | |
| "exclusive means independent" | exclusive events are dependent |
| : count equally likely outcomes | |
| "after five heads a tail is due" | independent tosses have no memory |
14Solved problems (from the easiest to the hardest)
Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.
Level 1: Outcomes, events and the basic rules
Problem 1: A sample space
A coin is tossed three times. Write the sample space. Find the probability of exactly two heads, and of at least one head.
Idea. List the outcomes in a fixed order so that none is lost. All of them are equally likely, so a probability is a count (Parts 2 and 4).
That is outcomes.
Why: the multiplication principle gives the size of the sample space before the list is written: 2 choices, three times. "At least one head" has seven outcomes, but its complement has only one.
Problem 2: Events as sets
A wheel has the numbers 1 to 10, all equally likely. is "a multiple of 3" and is "an odd number". Write , , and , and find their probabilities.
Idea. Write the events as sets, then count (Part 3). The addition rule is a check (Part 5).
The check: .
Why: the numbers 3 and 9 belong to both events. In the union they are written once, and in the addition rule they are subtracted once.
Problem 3: The complement
The probability that a delivery is late is 0.35. What is the probability that it is on time? And what is the probability that a die does not show a six?
Idea. An event and its complement have probabilities that add up to 1 (Part 5).
Why: "late" and "on time" are the only two possibilities and cannot happen together. Whenever a problem says "not", think of .
Problem 4: The addition rule
A card is drawn from a deck of 52. What is the probability that it is a red card or a face card (jack, queen or king)?
Idea. The two events can happen together, so the cards that are both must be subtracted once (Part 5).
Why: without the subtraction the six red face cards would be counted twice, and the result would be . Ask every time: can both events happen at once?
Problem 5: Mutually exclusive events
Two dice are rolled. What is the probability that the sum is 4 or 9?
Idea. A sum cannot be 4 and 9 at the same time: the events are mutually exclusive, and the probabilities simply add (Parts 3 and 5).
Why: the outcomes come from the table of the two dice in Part 2. Because no outcome is in both lists, nothing is subtracted.
Problem 6: Counting outcomes of two dice
Two dice are rolled. Find the probability that the sum is 8, and the probability that the sum is at most 4.
Idea. Count the pairs in the table of 36 equally likely outcomes (Parts 2 and 4).
Why: the sums are not equally likely: 8 can be made in five ways, 2 in only one. Always count the pairs, never the sums.
Problem 7: The multiplication principle
A code has 2 letters followed by 3 digits, for example KM 407. How many codes are possible, with 26 letters and repeats allowed?
Idea. One choice for every position; the numbers of choices multiply (Part 6).
Why: five positions, five independent choices. If repeats were not allowed it would be : each choice reduces the next.
Problem 8: Orders
(a) In how many orders can 6 people stand in a queue? (b) Nine runners are in a race. In how many ways can gold, silver and bronze be given?
Idea. All things in a row: a factorial. Some of them in order: a product that stops early (Part 6).
Why: in (b) the order matters: gold for Aysel and silver for Kamran is a different result from the other way round. That is why nothing is divided.
Level 2: Counting, conditions and independence
Problem 9: A team, and a place in it
A team of 4 is chosen at random from 10 people. How many teams are possible? What is the probability that one particular person, Leyla, is in the team?
Idea. A team has no order: combinations (Part 6). For the probability count the teams with Leyla.
For the teams with Leyla she is in, and 3 more people are chosen from the other 9.
Why: the answer is also what common sense says: 4 of the 10 places are in the team. A count that agrees with common sense is a good sign.
Problem 10: A hand of cards
Five cards are dealt from a deck of 52. What is the probability that all five are hearts?
Idea. A hand has no order. Count the hands of five hearts and all hands of five cards (Part 6).
Why: about 1 hand in 2,000. The same number comes from the multiplication rule: . Two methods, one result.
Problem 11: Conditional probabilities from a table
A shop looked at 200 customers. Of the 80 members of its club, 56 bought something. Of the 120 others, 36 bought something. Find , , and .
Idea. Write the table. A condition picks one row or one column (Part 7).
| Bought | Did not | Total | |
|---|---|---|---|
| Member | 56 | 24 | 80 |
| Not a member | 36 | 84 | 120 |
| Total | 92 | 108 | 200 |
Why: the second and the last question use the same number 56 on top, but different totals below: the row of the members, or the column of the buyers. The condition decides what stands below.
Problem 12: The formulas
For two events , and . Find , and . Are and independent?
Idea. The addition rule, the definition of a conditional probability, and the product test for independence (Parts 5, 7 and 8).
The product equals : the events are independent.
Why: independence shows twice here: in the product, and in . Knowing did not change the probability of .
Problem 13: Without putting back
A box holds 6 good and 4 defective bulbs. Two bulbs are taken at random without putting the first one back. Find the probability that both are good, that exactly one is defective, and that both are defective.
Idea. The multiplication rule along the paths of a tree; different paths are added (Part 7).
The check: .
Why: "exactly one defective" has two paths, and forgetting one of them is the usual mistake. The three results must add up to 1, because nothing else can happen.
Problem 14: Testing independence
Two dice are rolled. is "the first die shows 3". Is independent of = "the sum is 7"? Is it independent of = "the sum is 8"?
Idea. Compare with (Part 8).
and are independent. and are not.
Why: whatever the first die shows, exactly one value of the second die gives the sum 7. For the sum 8 this fails: after a 1 on the first die, the sum 8 is impossible. So the first die carries information about "sum is 8" but not about "sum is 7".
Problem 15: At least one
A basketball player scores a free throw with the probability 0.7, and the throws are independent. She throws three times. Find the probability that she scores all three, and that she scores at least once.
Idea. Independent events multiply. "At least one" is the complement of "none" (Parts 5 and 8).
Why: the direct way for "at least one" would need the cases one, two and three scores. The complement needs a single product.
Problem 16: In a row, or side by side
A machine has two parts. Each works with the probability 0.9, independently of the other. Find the probability that the machine works (a) if it needs both parts, and (b) if one working part is enough.
Idea. "Both" is a product. "At least one" is the complement of "both fail" (Part 8).
Why: a chain is weaker than its parts (0.81 is less than 0.9), a backup is stronger (0.99). This is why important systems have spare parts.
Problem 17: The law of total probability
A shop gets its parcels from three delivery firms: 50 percent from firm 1, 30 percent from firm 2 and 20 percent from firm 3. They are late with 2, 4 and 10 percent of their parcels. What share of all parcels is late?
Idea. Split into the three cases, multiply inside each case and add (Part 9).
Why: the result, 4.2 percent, lies between the best firm (2 percent) and the worst (10 percent). It is an average of the three rates, weighted with the shares of the firms.
Problem 18: Bayes' theorem
In the shop of Problem 17 a parcel arrives late. What is the probability that it came from firm 3?
Idea. The condition is turned around: divide the path of firm 3 by the total (Part 9).
Why: firm 3 carries only 20 percent of the parcels, but almost half of the late ones. The three numbers , , of Problem 17 give all three answers at once: , and .
Level 3: Bayes' theorem and putting ideas together
Problem 19: A medical test
A disease affects 2 percent of a population. A test is positive for 90 percent of the ill people and for 8 percent of the healthy people. A person tests positive. What is the probability that the person is ill?
Idea. Count 10,000 people, or use Bayes' theorem (Part 9).
With the formula:
Why: fewer than 1 in 5 positive results belong to an ill person, although the test is right for 90 percent of the ill. The healthy people are 49 times as many, so even their small rate of false alarms gives more positive tests.
Problem 20: A second test
The person of Problem 19 takes the same test again, and the two results are independent. The second test is positive too. What is the probability now?
Idea. Use Bayes' theorem again with two positive results. For independent tests the probabilities of the results multiply inside each case (Parts 8 and 9).
Why: the first test raised the probability from 0.02 to 0.187, the second from 0.187 to 0.721. Each piece of evidence updates the belief: the posterior of the first test is the prior of the second.
Problem 21: A small spam filter
40 percent of the emails of a mailbox are spam. The word "win" appears in 50 percent of the spam emails and in 2 percent of the others. The word "invoice" appears in 5 percent of the spam emails and in 30 percent of the others. An email contains both words. Treat the words as independent inside each class, and find the probability that the email is spam.
Idea. The Naive Bayes calculation: prior times the probabilities of the words, for each class, then the share of the spam product (Part 11).
Why: "win" speaks strongly for spam (0.50 against 0.02), "invoice" against it (0.05 against 0.30). The classifier multiplies all the evidence and compares the two products.
Problem 22: A confusion matrix
A model for a medical image was tested on 500 patients. Of the 80 ill patients it called 64 ill. Of the 420 healthy patients it called 42 ill. Find the accuracy, the precision and the recall, and the probability that a patient whom the model calls healthy is ill.
Idea. Write the table. Every measure is a share inside the right row or column (Part 10).
| Called ill | Called healthy | Total | |
|---|---|---|---|
| Really ill | 64 | 16 | 80 |
| Really healthy | 42 | 378 | 420 |
| Total | 106 | 394 | 500 |
Why: the accuracy looks good, but 4 of 10 alarms are false (precision 0.604) and 1 of 5 ill patients is missed (recall 0.80). One number never describes a classifier; the conditional probabilities do.
Problem 23: The same birthday month
Four people meet. Assume that every month is equally likely as a birthday month. What is the probability that at least two of them were born in the same month?
Idea. "At least two the same" is the complement of "all different". Count the ways with different months with the multiplication principle (Parts 5 and 6).
Why: with only four people the chance is already 43 percent, far more than most people guess. With days instead of months the same calculation gives the famous result that 23 people are enough for a chance above one half.
Problem 24: The three doors
In a game show there are three doors. Behind one is a car, behind the other two nothing. You choose door 1. The host, who knows where the car is, opens one of the other doors and shows that it is empty. He offers you to change to the remaining door. Should you change?
Idea. List the three equally likely places of the car and see what happens in each (Parts 2 and 7).
| Car behind | Host opens | You stay with door 1 | You change |
|---|---|---|---|
| door 1 | door 2 or 3 | win | lose |
| door 2 | door 3 | lose | win |
| door 3 | door 2 | lose | win |
Why: your first choice is right with the probability , and the host's action cannot change that. In the other two cases the host is forced to open the only empty door, and the remaining door has the car. Changing doubles the chance. The host's choice is information: it depends on where the car is.
Problem 25: Matching numbers
In a lottery 5 numbers are drawn from 30, and a player marks 5 numbers. How many tickets are possible? What is the probability that exactly 3 of the player's numbers are drawn?
Idea. Combinations three times: all draws, the ways to hit 3 of the 5 marked numbers, and the ways to fill the draw with 2 of the 25 other numbers (Part 6).
Why: the drawn numbers are split into two groups, and the multiplication principle joins the two counts. About 2 tickets in 100 have exactly three hits.
Problem 26: A fair game?
A game: you roll a die. With a six you win 12, with a 4 or a 5 you win 3, otherwise you lose 4. What is the expected value of one game?
Idea. Every value times its probability, then add (Part 12).
| Result of the die | Probability | Value |
|---|---|---|
| six | ||
| 4 or 5 | ||
| 1, 2 or 3 |
Why: on average the player wins 1 per game, although the player loses in half of the games. The rare large win outweighs the frequent small loss. The three probabilities add up to 1, as they must.
15Practice problems
Forty problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. A probability may be given as a fraction or as a decimal.
Level 1: Outcomes, events and the basic rules
- 1.A coin is tossed and a die is rolled. How many outcomes does the sample space have? What is the probability of heads together with an even number?
Show a hint
Multiply the numbers of outcomes. Then count the outcomes that fit.Show one possible solution
Answer: 12 outcomes. The probability is .
There are outcomes, all equally likely. Heads with an even number: , , , so .
- 2.Two coins are tossed. What is the probability of exactly one head?
Show a hint
Write the four outcomes.Show one possible solution
Answer: .
. Exactly one head: and , so .
- 3.A card is drawn from cards numbered 1 to 12. is "a multiple of 4" and is "larger than 8". Find and .
Show a hint
Write both events as sets first.Show one possible solution
Answer: and .
Check: .
- 4.A train is late with the probability 0.12. What is the probability that it is on time?
Show a hint
The complement rule.Show one possible solution
Answer: 0.88.
- 5.A die is rolled. What is the probability of a number that is at least 3?
Show a hint
Count the outcomes 3, 4, 5, 6.Show one possible solution
Answer: .
has 4 of the 6 outcomes: . With the complement: .
- 6.A card is drawn from a deck of 52. What is the probability of an ace or a king?
Show a hint
Can a card be both?Show one possible solution
Answer: .
No card is an ace and a king, so the events are mutually exclusive:
- 7.A card is drawn from a deck of 52. What is the probability of a black card or an ace?
Show a hint
Two aces are black: do not count them twice.Show one possible solution
Answer: .
- 8.Two dice are rolled. What is the probability that the sum is 5?
Show a hint
List the pairs with the sum 5.Show one possible solution
Answer: .
, , , : 4 of 36 outcomes, .
- 9.Two dice are rolled. What is the probability that both show an even number?
Show a hint
Each die has 3 even numbers.Show one possible solution
Answer: .
pairs of even numbers among 36: . The dice are independent, so also .
- 10.A menu has 3 starters, 5 main dishes and 2 desserts. How many different meals of three courses are possible?
Show a hint
The multiplication principle.Show one possible solution
Answer: 30.
- 11.A password has 4 different letters out of 26, and the order matters. How many passwords are possible?
Show a hint
Each letter that is used cannot be used again.Show one possible solution
Answer: 358,800.
- 12.In how many orders can 7 songs be played?
Show a hint
All 7 in a row: a factorial.Show one possible solution
Answer: 5,040.
- 13.Six people meet, and everybody shakes hands with everybody else once. How many handshakes are there?
Show a hint
A handshake is a choice of 2 people, without order.Show one possible solution
Answer: 15.
- 14. and are mutually exclusive with and . Find and the probability that neither happens.
Show a hint
Exclusive events add. "Neither" is the complement of "at least one".Show one possible solution
Answer: 0.8 and 0.2.
Level 2: Counting, conditions and independence
- 15.For two events , and . Find and . Are the events independent?
Show a hint
Solve the addition rule for .Show one possible solution
Answer: and . They are independent.
The product equals : independent.
- 16.A company has 150 employees. Of the 50 who work from home, 40 are satisfied with their job. Of the 100 who work in the office, 60 are satisfied. Find , and .
Show a hint
Write the table with its totals first.Show one possible solution
Answer: , and .
Satisfied in all: .
- 17.A die is rolled, and you are told that the number is odd. What is the probability that it is larger than 4?
Show a hint
The new sample space is .Show one possible solution
Answer: .
Among 1, 3, 5 only the 5 is larger than 4: . With the formula:
- 18.A bag holds 3 red and 7 green balls. Two balls are drawn without putting the first one back. Find the probability that both are green, and that they have different colours.
Show a hint
Multiply along the paths of a tree. "Different colours" has two paths.Show one possible solution
Answer: and .
Both red would be , and the three results add up to 1.
- 19.From the bag with 3 red and 7 green balls two balls are drawn, and the first one is put back before the second draw. What is the probability that both are green?
Show a hint
Now the two draws are independent.Show one possible solution
Answer: 0.49.
- 20.Two cards are drawn from a deck of 52 without putting the first one back. What is the probability that both are hearts?
Show a hint
After one heart, 12 hearts are left among 51 cards.Show one possible solution
Answer: .
- 21. and are independent with and . Find , and the probability that neither happens.
Show a hint
Independent events multiply. Then the addition rule.Show one possible solution
Answer: 0.1, 0.6 and 0.4.
The last result is also : "not " and "not " are independent too.
- 22.A fair coin is tossed 5 times. What is the probability of five heads? Of at least one tail?
Show a hint
Independent tosses multiply. The second question is the complement of the first.Show one possible solution
Answer: and .
- 23.A room has three smoke detectors. Each one notices a fire with the probability 0.8, independently of the others. What is the probability that at least one notices a fire?
Show a hint
First the probability that all three miss it.Show one possible solution
Answer: 0.992.
- 24.A committee of 3 is chosen at random from 7 people. How many committees are possible? What is the probability that two particular people, Aysel and Kamran, are both in it?
Show a hint
If both are in, only one more member is chosen from the other 5.Show one possible solution
Answer: 35 committees. The probability is .
With Aysel and Kamran in the committee there are 5 choices for the third member.
- 25.In a small game you choose 2 of the numbers 1 to 10, and then 2 numbers are drawn. What is the probability that both of your numbers are drawn?
Show a hint
Only one of all the possible pairs is yours.Show one possible solution
Answer: .
45 pairs are possible, and exactly one of them is your pair.
- 26.70 percent of the visitors of a website use a phone and 30 percent a computer. 3 percent of the phone visitors buy something, and 6 percent of the computer visitors. What share of all visitors buys something?
Show a hint
Two cases: multiply inside each case, then add.Show one possible solution
Answer: 0.039, that is 3.9 percent.
- 27.On the website of Problem 26 a visitor buys something. What is the probability that the visitor used a computer?
Show a hint
Bayes' theorem: the computer path divided by the total.Show one possible solution
Answer: .
Computers bring 30 percent of the visitors but 46 percent of the buyers.
- 28. and are mutually exclusive with and . Find . Are and independent?
Show a hint
Compare with the product .Show one possible solution
Answer: . They are not independent.
Mutually exclusive events cannot happen together, so . The product is , which is not 0. If happens, is impossible: that is strong dependence.
Level 3: Bayes' theorem and putting ideas together
- 29.A disease affects 0.5 percent of a population. A test is positive for 98 percent of the ill people and for 2 percent of the healthy people. A person tests positive. What is the probability that the person is ill?
Show a hint
Count 100,000 people.Show one possible solution
Answer: About 0.198.
Even with this very good test, four of five positive results are false alarms.
- 30.20 percent of the emails of a mailbox are spam. The word "offer" appears in 40 percent of the spam emails and in 4 percent of the others. An email contains "offer". What is the probability that it is spam?
Show a hint
Bayes' theorem with two cases: spam and not spam.Show one possible solution
Answer: .
- 31.Half of the emails are spam. Word 1 appears in 30 percent of the spam emails and in 10 percent of the others. Word 2 appears in 20 percent of the spam emails and in 40 percent of the others. An email contains both words. With the Naive Bayes assumption, what is the probability that it is spam?
Show a hint
Multiply the prior and the two word probabilities for each class.Show one possible solution
Answer: 0.6.
- 32.A model that predicts whether a loan is paid back was tested on 400 loans. Of the 50 loans that really failed, it predicted failure for 35. Of the 350 good loans, it predicted failure for 21. Find the precision and the recall for "failure", and the accuracy.
Show a hint
Write the table. Precision looks at the column of the predicted failures, recall at the row of the real failures.Show one possible solution
Answer: Precision 0.625, recall 0.7, accuracy 0.91.
The 400 loans Predicted failure Predicted good Total Really failed 35 15 50 Really good 21 329 350 Total 56 344 400 Predicted failures: . Right predictions: .
- 33.Three machines make 50, 30 and 20 percent of the parts of a factory, with 1, 2 and 4 percent of defective parts. A part is defective. What is the probability that the third machine made it?
Show a hint
First the share of all defective parts, then Bayes' theorem.Show one possible solution
Answer: .
- 34.Three cards are dealt from a deck of 52. What is the probability that all three are aces? Give the answer as a fraction, or as a decimal with at least two digits that are not zero.
Show a hint
Combinations, or three steps of the multiplication rule.Show one possible solution
Answer: .
There are hands of three aces, and hands in all:
The same with the multiplication rule: .
- 35.A box holds 5 red and 4 blue pens. Three pens are taken at random. What is the probability that exactly two of them are red?
Show a hint
Choose 2 of the 5 red pens and 1 of the 4 blue pens.Show one possible solution
Answer: .
First the ways to take 2 red pens and 1 blue pen, then all choices of 3 pens out of 9:
- 36.Three people are chosen at random. Assume that every day of the week is equally likely as the weekday of a birthday. What is the probability that at least two of them were born on the same weekday?
Show a hint
The complement: all three on different weekdays.Show one possible solution
Answer: .
- 37.A system has three parts. Each works with the probability 0.9, independently. Find the probability that the system works (a) if it needs all three parts, and (b) if one working part is enough.
Show a hint
A product for (a), the complement of "all fail" for (b).Show one possible solution
Answer: 0.729 and 0.999.
- 38.Three cards are drawn from a deck of 52 without putting them back. What is the probability that at least one of them is a heart?
Show a hint
First the probability that none of the three is a heart: 39 cards are not hearts.Show one possible solution
Answer: About 0.586.
- 39.A lottery ticket costs 2. It wins 100 with the probability 0.01 and 10 with the probability 0.05; otherwise it wins nothing. What is the expected win of a ticket, and what does the buyer gain or lose on average?
Show a hint
Every win times its probability, then add. Then subtract the price.Show one possible solution
Answer: The expected win is 1.5. On average the buyer loses 0.5 per ticket.
- 40.In a test with four possible answers for every question, a student knows the answer with the probability 0.6 and guesses otherwise. The student answered a question correctly. What is the probability that the student knew the answer?
Show a hint
Two cases: knew (then right for sure) and guessed (then right with the probability ).Show one possible solution
Answer: .
16Check yourself
Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.
0 of 12 answered.
17Checking your work with Python
This is the only code of the lesson. Python can count, calculate with exact fractions, and repeat an experiment many times.
from fractions import Fraction
from math import comb, factorial
import numpy as np
print(factorial(5), comb(8, 3), comb(49, 6))
print(Fraction(4, 52) * Fraction(3, 51))
print(round(0.0095 / (0.0095 + 0.0495), 3))
rng = np.random.default_rng(1)
first = rng.integers(1, 7, size=100_000)
second = rng.integers(1, 7, size=100_000)
print(np.mean(first + second == 7))Output
120 56 13983816 1/221 0.161 0.1645
Change it and run: change the numbers in comb and Fraction to those of a problem you solved by hand. Then put another number into default_rng and run again: the last result changes a little.
The first line gives , and the number of lottery tickets. The second is the probability of two aces as an exact fraction, and the third is the medical test of Part 9. The last lines roll two dice 100,000 times and print the share of rolls with the sum 7: 0.1645, close to the exact value . That is the law of large numbers at work. (The number 1 in default_rng(1) fixes the random numbers, so that every run gives the same result. Another number gives a slightly different share.)
| By hand | In Python |
|---|---|
math.factorial(n) | |
math.comb(n, k) | |
math.perm(n, k) | |
| an exact fraction | fractions.Fraction(4, 52) |
| repeating an experiment | np.random.default_rng() and its methods |
18Quick summary
| Idea | Definition or rule |
|---|---|
| Sample space | the set of all outcomes of an experiment |
| Event | a set of outcomes |
| , , | or ; and ; not |
| Mutually exclusive | and cannot happen together: is empty |
| Equally likely outcomes | = outcomes in divided by outcomes in |
| Relative frequency | times happened divided by the number of repetitions |
| Basic rules | , , exclusive events add |
| Complement rule | |
| Addition rule | |
| At least one | |
| Multiplication principle | ways, then ways: ways |
| Factorial | orders of things |
| Ordered choice | |
| Combination | |
| Conditional probability | |
| Multiplication rule | |
| Independent events | |
| Law of total probability | |
| Bayes' theorem | |
| Prior, likelihood, posterior | before the information; fit of the information; after the information |
| Precision and recall | and |
| Expected value | every value times its probability, added |