Asiman Data School

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. 1.Why probability is the language of uncertainty in data science and machine learning
  2. 2.Experiments, outcomes, the sample space and events
  3. 3.Events as sets: "or", "and", "not", mutually exclusive events, Venn diagrams
  4. 4.What a probability is: equally likely outcomes, relative frequency, and the three basic rules
  5. 5.The complement rule, the addition rule and the "at least one" trick
  6. 6.Counting: the multiplication principle, factorials, arrangements and combinations
  7. 7.Conditional probability, the multiplication rule and tree diagrams
  8. 8.Independent events, and why "independent" is not "mutually exclusive"
  9. 9.The law of total probability and Bayes' theorem
  10. 10.Tables of two features: joint, marginal and conditional probabilities, and the confusion matrix of a classifier
  11. 11.Probability in machine learning (a Naive Bayes spam filter), and a first look at random variables and the expected value
Notation used in this lesson
WrittenMeaning
SSthe sample space: the set of all outcomes
AA, BBevents
P(A)P(A)the probability of the event AA
A∪BA \cup BAA or BB (at least one of them happens)
A∩BA \cap BAA and BB (both happen)
AcA^cnot AA (the complement of AA)
P(A∣B)P(A \mid B)the probability of AA given that BB has happened
n!n!nn factorial: n×(n−1)×⋯×2×1n \times (n - 1) \times \dots \times 2 \times 1
C(n,k)C(n, k)the number of ways to choose kk things out of nn ("n choose k")

A probability is written as a fraction (16\tfrac{1}{6}), as a decimal (0.1670.167) 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 topics and the probability idea behind each
Later in the courseThe 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 SS 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.
Five experiments and their sample spaces
ExperimentSample spaceNumber of outcomes
Tossing a coin{H,T}\{H, T\}2
Rolling a die{1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\}6
Tossing two coins{HH,HT,TH,TT}\{HH, HT, TH, TT\}4
Rolling two diceall pairs from (1,1)(1, 1) to (6,6)(6, 6)36
Drawing a cardthe 52 cards of a deck52

Example. A die is rolled. Some events:

A="an even number"={2,4,6}B="a number larger than 4"={5,6}C="a six"={6}\begin{aligned} A &= \text{"an even number"} = \{2, 4, 6\} \\ B &= \text{"a number larger than 4"} = \{5, 6\} \\ C &= \text{"a six"} = \{6\} \end{aligned}

If the die shows 6, all three events happen. If it shows 4, only AA happens.

2.2Two special events

  • The whole sample space SS 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, (2,5)(2, 5) and (5,2)(5, 2) are different outcomes: think of a red die and a blue die. There are 6×6=366 \times 6 = 36 outcomes, and they are all equally likely. The table shows the sum of the two dice for every outcome:

The 36 equally likely outcomes of two dice, each shown with its sum. The first die is the row, the second die the column. The six outcomes with the sum 7 are filled.

The event "the sum is 7" has the six outcomes on the marked diagonal: (1,6)(1, 6), (2,5)(2, 5), (3,4)(3, 4), (4,3)(4, 3), (5,2)(5, 2), (6,1)(6, 1). 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

A wheel with the numbers 1 to 4 is turned twice. How many outcomes does the sample space have? How many of them belong to the event "both numbers are equal", and how many to the event "the sum is 5"?

Show a hint
An outcome is a pair such as (1,3)(1, 3), and (1,3)(1, 3) and (3,1)(3, 1) are different. List the pairs of each event.
Show the solution

Answer: 16 outcomes. "Both equal" has 4 outcomes, and "the sum is 5" has 4 outcomes.

sample space:4×4=16 pairsboth equal:(1,1),  (2,2),  (3,3),  (4,4)sum is 5:(1,4),  (2,3),  (3,2),  (4,1)\begin{aligned} \text{sample space:} \quad & 4 \times 4 = 16 \text{ pairs} \\ \text{both equal:} \quad & (1, 1),\; (2, 2),\; (3, 3),\; (4, 4) \\ \text{sum is 5:} \quad & (1, 4),\; (2, 3),\; (3, 2),\; (4, 1) \end{aligned}

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.

The three operations with events
WrittenReadIt happens when
A∪BA \cup BAA or BB (the union)at least one of the two happens (also when both do)
A∩BA \cap BAA and BB (the intersection)both happen
AcA^cnot AA (the complement)AA does not happen

With the events of the die, A={2,4,6}A = \{2, 4, 6\} and B={5,6}B = \{5, 6\}:

A∪B={2,4,5,6}even or larger than 4A∩B={6}even and larger than 4Ac={1,3,5}not even\begin{aligned} A \cup B &= \{2, 4, 5, 6\} && \text{even or larger than 4} \\ A \cap B &= \{6\} && \text{even and larger than 4} \\ A^c &= \{1, 3, 5\} && \text{not even} \end{aligned}

Notice the word "or". In everyday language it often means "one or the other, but not both". In probability, "AA or BB" always includes the case that both happen.

3.2Mutually exclusive events

Two events that cannot happen together are called mutually exclusive (or disjoint): A∩B={  }A \cap B = \{\;\}. "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:

SAB

A ∪ B

A or B

SAB

A ∩ B

A and B

SAB

Ac

not A

SAB

mutually exclusive

A ∩ B is empty

Events as regions of the sample space S. The filled region is the event named under each picture.

A die is rolled. A is "even", B is "larger than 4". Choose an event and see its outcomes.

SAB2, 4651, 3

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):

(A∪B)c=Ac∩Bc(A∩B)c=Ac∪Bc\begin{aligned} (A \cup B)^c &= A^c \cap B^c \\ (A \cap B)^c &= A^c \cup B^c \end{aligned}

The first rule says: "not (AA or BB)" is "not AA and not BB". The second says: "not (AA and BB)" is "not AA or not BB". In words: "neither of them" is the opposite of "at least one of them".

Try it yourself

A wheel has the numbers 1 to 8. AA is "at most 4" and BB is "an even number". How many outcomes are in A∩BA \cap B, in A∪BA \cup B, in AcA^c and in (A∪B)c(A \cup B)^c?

Show a hint
Write AA and BB as sets of numbers first. The last event contains the numbers that are in neither set.
Show the solution

Answer: 2, 6, 4 and 2 outcomes.

A={1,2,3,4},B={2,4,6,8}A∩B={2,4}A∪B={1,2,3,4,6,8}Ac={5,6,7,8}(A∪B)c={5,7}\begin{aligned} A &= \{1, 2, 3, 4\}, \quad B = \{2, 4, 6, 8\} \\ A \cap B &= \{2, 4\} \\ A \cup B &= \{1, 2, 3, 4, 6, 8\} \\ A^c &= \{5, 6, 7, 8\} \\ (A \cup B)^c &= \{5, 7\} \end{aligned}

The last set is also Ac∩BcA^c \cap B^c: 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:

P(A)=number of outcomes in Anumber of outcomes in SP(A) = \frac{\text{number of outcomes in } A}{\text{number of outcomes in } S}
die:P(even)=36=12P(larger than 4)=26=13two dice:P(sum is 7)=636=16cards:P(heart)=1352=14P(king)=452=113\begin{aligned} \text{die:} \quad & P(\text{even}) = \tfrac{3}{6} = \tfrac{1}{2} \\ & P(\text{larger than 4}) = \tfrac{2}{6} = \tfrac{1}{3} \\[4pt] \text{two dice:} \quad & P(\text{sum is 7}) = \tfrac{6}{36} = \tfrac{1}{6} \\[4pt] \text{cards:} \quad & P(\text{heart}) = \tfrac{13}{52} = \tfrac{1}{4} \\ & P(\text{king}) = \tfrac{4}{52} = \tfrac{1}{13} \end{aligned}

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 12\tfrac{1}{2} each.

4.2Probability as a long-run share

What does "the probability of heads is 12\tfrac{1}{2}" 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 12\tfrac{1}{2}:

00.511101001000number of tosses
The share of heads in 2,000 tosses of a coin. At the beginning it jumps wildly; after hundreds of tosses it settles near 0.5, the dashed line.

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.

P(A)≈number of times A happenednumber of repetitionsP(A) \approx \frac{\text{number of times } A \text{ happened}}{\text{number of repetitions}}

Toss a coin many times and watch the share of heads.

00.51110100number of tosses

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 2305000=0.046\tfrac{230}{5000} = 0.046. 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:

The three basic rules of probability
RuleIn symbolsIn words
10≤P(A)≤10 \le P(A) \le 1a probability is a number between 0 and 1
2P(S)=1P(S) = 1something always happens
3P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B) for mutually exclusive AA and BBchances of events that cannot happen together add up

P(A)=0P(A) = 0 means "impossible", P(A)=1P(A) = 1 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

A bag holds 4 red, 5 blue and 1 green ball, and one ball is taken at random. Find the probability of a blue ball, and of a ball that is not green. Then: 18 of 120 parts made by a machine were defective. Estimate the probability that a part of this machine is defective.

Show a hint
Count the balls that fit and divide by all 10 balls. For the machine use the relative frequency.
Show the solution

Answer: P(blue)=0.5P(\text{blue}) = 0.5, P(not green)=0.9P(\text{not green}) = 0.9, and the estimate for the machine is 0.150.15.

P(blue)=510=0.5P(not green)=4+510=910=0.9P(defective)≈18120=0.15\begin{aligned} P(\text{blue}) &= \tfrac{5}{10} = 0.5 \\ P(\text{not green}) &= \tfrac{4 + 5}{10} = \tfrac{9}{10} = 0.9 \\ P(\text{defective}) &\approx \tfrac{18}{120} = 0.15 \end{aligned}

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

AA and AcA^c are mutually exclusive, and together they are the whole sample space. So P(A)+P(Ac)=1P(A) + P(A^c) = 1:

P(Ac)=1−P(A)P(A^c) = 1 - P(A)
P(not a six)=1−16=56P(rain)=0.7⇒P(no rain)=0.3\begin{aligned} P(\text{not a six}) &= 1 - \tfrac{1}{6} = \tfrac{5}{6} \\ P(\text{rain}) = 0.7 \quad &\Rightarrow \quad P(\text{no rain}) = 0.3 \end{aligned}

5.2The addition rule

For mutually exclusive events the probabilities add (rule 3). When the events can happen together, the outcomes in A∩BA \cap B would be counted twice, so they are subtracted once:

P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B)

Example 1. The die, with AA = even and BB = larger than 4:

P(A∪B)=36+26−16=46=23P(A \cup B) = \tfrac{3}{6} + \tfrac{2}{6} - \tfrac{1}{6} = \tfrac{4}{6} = \tfrac{2}{3}

The check by counting: A∪B={2,4,5,6}A \cup B = \{2, 4, 5, 6\} has 4 of the 6 outcomes.

Example 2. A card is drawn. What is the probability of a king or a heart?

P(king or heart)=452+1352−152=1652=413P(\text{king or heart}) = \tfrac{4}{52} + \tfrac{13}{52} - \tfrac{1}{52} = \tfrac{16}{52} = \tfrac{4}{13}

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 5×5=255 \times 5 = 25 outcomes without a six.

P(no six)=2536P(at least one six)=1−2536=1136\begin{aligned} P(\text{no six}) &= \tfrac{25}{36} \\[4pt] P(\text{at least one six}) &= 1 - \tfrac{25}{36} = \tfrac{11}{36} \end{aligned}

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

A card is drawn from cards with the numbers 1 to 20. Find the probability that the number is a multiple of 3 or a multiple of 5. Then: a die is rolled twice. Find the probability that at least one of the two numbers is larger than 4.

Show a hint
For the first question, one number is a multiple of 3 and of 5: do not count it twice. For the second, start with the probability that no number is larger than 4.
Show the solution

Answer: 920=0.45\tfrac{9}{20} = 0.45 and 59≈0.556\tfrac{5}{9} \approx 0.556.

multiples of 3:3,6,9,12,15,186 numbersmultiples of 5:5,10,15,204 numbersboth:151 number\begin{aligned} \text{multiples of 3:} \quad & 3, 6, 9, 12, 15, 18 && 6 \text{ numbers} \\ \text{multiples of 5:} \quad & 5, 10, 15, 20 && 4 \text{ numbers} \\ \text{both:} \quad & 15 && 1 \text{ number} \end{aligned}
P(multiple of 3 or of 5)=620+420−120=920P(no number larger than 4)=46×46=1636P(at least one)=1−1636=2036=59\begin{aligned} & P(\text{multiple of 3 or of 5}) \\ & \quad = \tfrac{6}{20} + \tfrac{4}{20} - \tfrac{1}{20} = \tfrac{9}{20} \\[6pt] & P(\text{no number larger than 4}) \\ & \quad = \tfrac{4}{6} \times \tfrac{4}{6} = \tfrac{16}{36} \\[4pt] & P(\text{at least one}) = 1 - \tfrac{16}{36} = \tfrac{20}{36} = \tfrac{5}{9} \end{aligned}

The second calculation counts pairs: 4×4=164 \times 4 = 16 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 mm ways for the first step and nn ways for the second, there are m×nm \times n ways in all.

  • 3 shirts and 4 pairs of trousers: 3×4=123 \times 4 = 12 outfits
  • a PIN of 4 digits: 10×10×10×10=10,00010 \times 10 \times 10 \times 10 = 10{,}000 codes
  • two dice: 6×6=366 \times 6 = 36 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:

5×4×3×2×1=1205 \times 4 \times 3 \times 2 \times 1 = 120

This product is written 5!5! and read "5 factorial".

n!=n×(n−1)×⋯×2×10!=1(by agreement)\begin{aligned} n! &= n \times (n - 1) \times \dots \times 2 \times 1 \\ 0! &= 1 \quad \text{(by agreement)} \end{aligned}
The first factorials
nn12345610
n!n!126241207203,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?

8×7×6=3368 \times 7 \times 6 = 336

The product starts like a factorial and stops after 3 factors. In general, the number of ways to choose kk out of nn things when the order matters is

n×(n−1)×…    (k factors)=n!(n−k)!n \times (n - 1) \times \dots \;\; (k \text{ factors}) = \frac{n!}{(n - k)!}

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 3!=63! = 6 times, once for every order of its members. So

3366=56 teams\frac{336}{6} = 56 \text{ teams}
  • 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}

Aysel, Kamran and Leyla can be chosen in 3! = 6 orders. For medals these are six different results; for a team they are one and the same team. That is why 336 ordered choices are only 336 / 6 = 56 teams.

The number of ways to choose kk out of nn things when the order does not matter is written C(n,k)C(n, k), read "n choose k". Many books write it as (nk)\binom{n}{k}.

C(n,k)=n!k! (n−k)!C(8,3)=8×7×63×2×1=56\begin{aligned} C(n, k) &= \frac{n!}{k!\,(n - k)!} \\[6pt] C(8, 3) &= \frac{8 \times 7 \times 6}{3 \times 2 \times 1} = 56 \end{aligned}

Such a choice is called a combination. A practical way to calculate it: kk factors counting down from nn on top, k!k! below.

C(5,2)=5×42×1=10C(10,4)=10×9×8×74×3×2×1=210C(49,6)=13,983,816\begin{aligned} C(5, 2) &= \frac{5 \times 4}{2 \times 1} = 10 \\[4pt] C(10, 4) &= \frac{10 \times 9 \times 8 \times 7}{4 \times 3 \times 2 \times 1} = 210 \\[4pt] C(49, 6) &= 13{,}983{,}816 \end{aligned}

The last number is the number of tickets of a "6 out of 49" lottery. Useful facts: C(n,0)=1C(n, 0) = 1, C(n,1)=nC(n, 1) = n, and C(n,k)=C(n,n−k)C(n, k) = C(n, n - k) (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?

Four questions and their counting rules
QuestionRuleExample
Steps with separate choicesmultiply3 shirts, 4 trousers: 3×43 \times 4
All nn things in a rown!n!5 books: 5!=1205! = 120
kk of nn, the order mattersn!(n−k)!\dfrac{n!}{(n - k)!}3 medals for 8 runners: 8×7×6=3368 \times 7 \times 6 = 336
kk of nn, the order does not matterC(n,k)C(n, k)a team of 3 from 8: 5656

6.6Probability with counting

Example. Two cards are drawn from a deck. What is the probability that both are aces?

all pairs of cards:C(52,2)=52×512=1326pairs of aces:C(4,2)=4×32=6P(two aces)=61326=1221≈0.0045\begin{aligned} \text{all pairs of cards:} \quad & C(52, 2) = \frac{52 \times 51}{2} = 1326 \\[4pt] \text{pairs of aces:} \quad & C(4, 2) = \frac{4 \times 3}{2} = 6 \\[4pt] P(\text{two aces}) &= \frac{6}{1326} = \frac{1}{221} \approx 0.0045 \end{aligned}

Example. The probability of winning the "6 out of 49" lottery with one ticket is 113,983,816\dfrac{1}{13{,}983{,}816}, about 0.00000007.

Try it yourself

(a) How many PINs of 4 digits have four different digits? (b) In how many ways can you choose 2 of 7 books to take on a trip? (c) You choose the 2 books at random. What is the probability that your favourite book is among them?

Show a hint
In (a) the order matters and a digit cannot be used twice. In (b) the order does not matter. In (c) count the pairs that contain the favourite book: its partner is one of the other 6 books.
Show the solution

Answer: (a) 5,040. (b) 21. (c) 621=27≈0.286\tfrac{6}{21} = \tfrac{2}{7} \approx 0.286.

(a)10×9×8×7=5040(b)C(7,2)=7×62=21(c)621=27≈0.286\begin{aligned} \text{(a)} \quad & 10 \times 9 \times 8 \times 7 = 5040 \\[4pt] \text{(b)} \quad & C(7, 2) = \frac{7 \times 6}{2} = 21 \\[4pt] \text{(c)} \quad & \frac{6}{21} = \frac{2}{7} \approx 0.286 \end{aligned}

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 16\tfrac{1}{6}. 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.

P(six, given that the number is even)=13P(\text{six, given that the number is even}) = \tfrac{1}{3}

This is a conditional probability. It is written P(A∣B)P(A \mid B) and read "the probability of AA given BB". The event BB is the information; it becomes the new, smaller sample space.

7.2The formula

P(A∣B)=P(A∩B)P(B)(for P(B)>0)P(A \mid B) = \frac{P(A \cap B)}{P(B)} \qquad (\text{for } P(B) > 0)

Of all the cases in which BB happens, we take the share in which AA happens too. For the die: P(six and even)=16P(\text{six and even}) = \tfrac{1}{6} and P(even)=12P(\text{even}) = \tfrac{1}{2}, so

P(six∣even)=1/61/2=13P(\text{six} \mid \text{even}) = \frac{1/6}{1/2} = \frac{1}{3}

7.3A table of data

100 students took an exam. Some of them had done the homework during the term:

100 students: homework and the result of the exam
PassedFailedTotal
Did the homework54660
Did not202040
Total7426100

A student is chosen at random.

P(passed)=74100=0.74P(passed∣homework)=5460=0.90P(passed∣no homework)=2040=0.50\begin{aligned} P(\text{passed}) &= \tfrac{74}{100} = 0.74 \\[4pt] P(\text{passed} \mid \text{homework}) &= \tfrac{54}{60} = 0.90 \\[4pt] P(\text{passed} \mid \text{no homework}) &= \tfrac{20}{40} = 0.50 \end{aligned}

For the second probability only the first row counts, for the third only the second row. With the formula: P(passed and homework)P(homework)=0.540.60=0.90\dfrac{P(\text{passed and homework})}{P(\text{homework})} = \dfrac{0.54}{0.60} = 0.90. 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

P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A) are different questions with different answers:

P(passed∣homework)=5460=0.90P(homework∣passed)=5474≈0.73\begin{aligned} P(\text{passed} \mid \text{homework}) &= \tfrac{54}{60} = 0.90 \\[4pt] P(\text{homework} \mid \text{passed}) &= \tfrac{54}{74} \approx 0.73 \end{aligned}

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 P(B)P(B):

P(A∩B)=P(B)×P(A∣B)P(A \cap B) = P(B) \times P(A \mid B)

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:

P(first is an ace)=452P(second ace∣first ace)=351P(both aces)=452×351=122652=1221\begin{aligned} & P(\text{first is an ace}) = \tfrac{4}{52} \\[4pt] & P(\text{second ace} \mid \text{first ace}) = \tfrac{3}{51} \\[4pt] & P(\text{both aces}) = \tfrac{4}{52} \times \tfrac{3}{51} = \tfrac{12}{2652} = \tfrac{1}{221} \end{aligned}

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:

5/8red4/7red20/563/7blue15/563/8blue5/7red15/562/7blue6/56
Two draws from a bag with 5 red and 3 blue balls, without putting the first ball back. On every branch stands a conditional probability; the probability of a path is the product of its branches.
P(both red)=58×47=2056=514P(one of each colour)=1556+1556=3056=1528P(both blue)=38×27=656=328\begin{aligned} P(\text{both red}) &= \tfrac{5}{8} \times \tfrac{4}{7} = \tfrac{20}{56} = \tfrac{5}{14} \\[4pt] P(\text{one of each colour}) &= \tfrac{15}{56} + \tfrac{15}{56} = \tfrac{30}{56} = \tfrac{15}{28} \\[4pt] P(\text{both blue}) &= \tfrac{3}{8} \times \tfrac{2}{7} = \tfrac{6}{56} = \tfrac{3}{28} \end{aligned}

The four paths are mutually exclusive, and their probabilities add up to 5656=1\tfrac{56}{56} = 1. Two rules of the tree: multiply along a path, add different paths.

Try it yourself

A car dealer has 80 cars. Of the 20 electric cars, 15 are new and 5 are used. Of the 60 petrol cars, 15 are new and 45 are used. A car is chosen at random. Find P(new)P(\text{new}), P(new∣electric)P(\text{new} \mid \text{electric}) and P(electric∣new)P(\text{electric} \mid \text{new}).

Show a hint
Write the table with its totals. A condition leaves only one row or one column.
Show the solution

Answer: P(new)=0.375P(\text{new}) = 0.375, P(new∣electric)=0.75P(\text{new} \mid \text{electric}) = 0.75 and P(electric∣new)=0.5P(\text{electric} \mid \text{new}) = 0.5.

The 80 cars
NewUsedTotal
Electric15520
Petrol154560
Total305080
P(new)=3080=0.375P(new∣electric)=1520=0.75P(electric∣new)=1530=0.5\begin{aligned} P(\text{new}) &= \tfrac{30}{80} = 0.375 \\[4pt] P(\text{new} \mid \text{electric}) &= \tfrac{15}{20} = 0.75 \\[4pt] P(\text{electric} \mid \text{new}) &= \tfrac{15}{30} = 0.5 \end{aligned}

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:

P(A∣B)=P(A)P(A \mid B) = P(A)

Put this into the multiplication rule, and it becomes a simple product. AA and BB are independent exactly when

P(A∩B)=P(A)×P(B)P(A \cap B) = P(A) \times P(B)
P(heads and a six)=12×16=112P(\text{heads and a six}) = \tfrac{1}{2} \times \tfrac{1}{6} = \tfrac{1}{12}

8.2Testing independence

Whether two events are independent is decided by calculating, not by feeling. A die, with AA = even:

B="at most 4"={1,2,3,4}P(A∩B)=P({2,4})=13P(A)×P(B)=12×23=13D="larger than 3"={4,5,6}P(A∩D)=P({4,6})=13P(A)×P(D)=12×12=14\begin{aligned} & B = \text{"at most 4"} = \{1, 2, 3, 4\} \\ & P(A \cap B) = P(\{2, 4\}) = \tfrac{1}{3} \\ & P(A) \times P(B) = \tfrac{1}{2} \times \tfrac{2}{3} = \tfrac{1}{3} \\[8pt] & D = \text{"larger than 3"} = \{4, 5, 6\} \\ & P(A \cap D) = P(\{4, 6\}) = \tfrac{1}{3} \\ & P(A) \times P(D) = \tfrac{1}{2} \times \tfrac{1}{2} = \tfrac{1}{4} \end{aligned}

AA and BB are independent, AA and DD are not. Knowing "at most 4" leaves the chance of an even number at 12\tfrac{1}{2}. Knowing "larger than 3" raises it to 23\tfrac{2}{3}.

8.3With and without putting back

The bag with 5 red and 3 blue balls again:

with putting back:P(both red)=58×58=2564≈0.391without putting back:P(both red)=58×47=2056≈0.357\begin{aligned} & \text{with putting back:} \\ & \quad P(\text{both red}) = \tfrac{5}{8} \times \tfrac{5}{8} = \tfrac{25}{64} \approx 0.391 \\[6pt] & \text{without putting back:} \\ & \quad P(\text{both red}) = \tfrac{5}{8} \times \tfrac{4}{7} = \tfrac{20}{56} \approx 0.357 \end{aligned}

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 and independent events side by side
Mutually exclusiveIndependent
Meaningthey cannot happen togetherone tells nothing about the other
P(A∩B)P(A \cap B)00P(A)×P(B)P(A) \times P(B)
Used forP(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B)P(A∩B)=P(A)×P(B)P(A \cap B) = P(A) \times P(B)

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:

P(three heads in three tosses)=12×12×12=18P(\text{three heads in three tosses}) = \tfrac{1}{2} \times \tfrac{1}{2} \times \tfrac{1}{2} = \tfrac{1}{8}

Together with the complement this gives the "at least one" pattern for repeated trials:

P(at least one six in 4 rolls)=1−(56)4=1−6251296=6711296≈0.518\begin{aligned} & P(\text{at least one six in 4 rolls}) \\ & \quad = 1 - \left(\tfrac{5}{6}\right)^4 = 1 - \tfrac{625}{1296} \\[4pt] & \quad = \tfrac{671}{1296} \approx 0.518 \end{aligned}

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: 0.1×0.1×0.1=0.0010.1 \times 0.1 \times 0.1 = 0.001. The probability that at least one works is 0.9990.999.

Try it yourself

(a) For two events P(A)=0.3P(A) = 0.3, P(B)=0.4P(B) = 0.4 and P(A∩B)=0.12P(A \cap B) = 0.12. Are they independent? (b) A machine has three parts, and each part fails with the probability 0.2, independently of the others. What is the probability that at least one part fails?

Show a hint
For (a) compare P(A∩B)P(A \cap B) with the product. For (b) first find the probability that no part fails.
Show the solution

Answer: (a) Yes, they are independent. (b) 0.488.

(a)P(A)×P(B)=0.3×0.4=0.12and P(A∩B)=0.12 too(b)P(no part fails)=0.8×0.8×0.8=0.512P(at least one fails)=1−0.512=0.488\begin{aligned} \text{(a)} \quad & P(A) \times P(B) = 0.3 \times 0.4 = 0.12 \\ & \text{and } P(A \cap B) = 0.12 \text{ too} \\[6pt] \text{(b)} \quad & P(\text{no part fails}) \\ & \quad = 0.8 \times 0.8 \times 0.8 = 0.512 \\ & P(\text{at least one fails}) \\ & \quad = 1 - 0.512 = 0.488 \end{aligned}

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 M1M_1 and M2M_2 for the machines and DD for "defective":

P(D)=P(M1)×P(D∣M1)+P(M2)×P(D∣M2)=0.60×0.02+0.40×0.05=0.012+0.020=0.032\begin{aligned} P(D) &= P(M_1) \times P(D \mid M_1) \\ &\quad + P(M_2) \times P(D \mid M_2) \\ &= 0.60 \times 0.02 + 0.40 \times 0.05 \\ &= 0.012 + 0.020 = 0.032 \end{aligned}

This is the law of total probability: if the cases B1,B2,…B_1, B_2, \dots are mutually exclusive and cover everything, then

P(A)=P(B1)×P(A∣B1)+P(B2)×P(A∣B2)+…\begin{aligned} P(A) = {} & P(B_1) \times P(A \mid B_1) \\ & + P(B_2) \times P(A \mid B_2) + \dots \end{aligned}

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 P(D∣M2)=0.05P(D \mid M_2) = 0.05, but the question asks for P(M2∣D)P(M_2 \mid D): the condition the other way round.

P(M2∣D)=P(M2 and D)P(D)=0.0200.032=0.625P(M_2 \mid D) = \frac{P(M_2 \text{ and } D)}{P(D)} = \frac{0.020}{0.032} = 0.625

Machine 2 makes only 40 percent of the parts, but 62.5 percent of the defective ones.

This calculation is Bayes' theorem:

P(B∣A)=P(B)×P(A∣B)P(A)P(B \mid A) = \frac{P(B) \times P(A \mid B)}{P(A)}

where P(A)P(A) is usually found with the law of total probability. The three parts have names that you will meet again:

The three parts of Bayes' theorem
NameIn the exampleMeaning
prior P(B)P(B)P(M2)=0.40P(M_2) = 0.40what we believed before the information
likelihood P(A∣B)P(A \mid B)P(D∣M2)=0.05P(D \mid M_2) = 0.05how well the information fits this case
posterior P(B∣A)P(B \mid A)P(M2∣D)=0.625P(M_2 \mid D) = 0.625what 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:

1%ill95%positive95 people5%negative5 people99%healthy5%positive495 people95%negative9,405 people
10,000 people and a medical test. Of the 590 people with a positive test (95 + 495) only 95 are ill.
ill:100 people,95 test positivehealthy:9,900 people,495 test positivepositive tests:95+495=590P(ill∣positive)=95590≈0.161\begin{aligned} \text{ill:} \quad & 100 \text{ people}, \quad 95 \text{ test positive} \\ \text{healthy:} \quad & 9{,}900 \text{ people}, \quad 495 \text{ test positive} \\[4pt] \text{positive tests:} \quad & 95 + 495 = 590 \\[4pt] P(\text{ill} \mid \text{positive}) &= \frac{95}{590} \approx 0.161 \end{aligned}

Only about 16 percent. The same with Bayes' theorem:

P(ill∣positive)=0.01×0.950.01×0.95+0.99×0.05=0.00950.0590≈0.161\begin{aligned} P(\text{ill} \mid \text{positive}) &= \frac{0.01 \times 0.95}{0.01 \times 0.95 + 0.99 \times 0.05} \\[4pt] &= \frac{0.0095}{0.0590} \approx 0.161 \end{aligned}

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: P(positive∣ill)=0.95P(\text{positive} \mid \text{ill}) = 0.95 and P(ill∣positive)=0.161P(\text{ill} \mid \text{positive}) = 0.161 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

1%ill95%positive95 people5%negative5 people99%healthy5%positive495 people95%negative9,405 people

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

A shop gets 80 percent of its orders through its app and 20 percent by phone. 1 percent of the app orders are sent back, and 6 percent of the phone orders. What share of all orders is sent back? An order is sent back: what is the probability that it was a phone order?

Show a hint
Two cases, app and phone: multiply inside each case and add. For the second question divide the phone part by the total.
Show the solution

Answer: 2 percent of all orders are sent back, and P(phone∣sent back)=0.6P(\text{phone} \mid \text{sent back}) = 0.6.

P(sent back)=0.80×0.01+0.20×0.06=0.008+0.012=0.020P(phone∣sent back)=0.0120.020=0.6\begin{aligned} & P(\text{sent back}) = 0.80 \times 0.01 + 0.20 \times 0.06 \\ & \quad = 0.008 + 0.012 = 0.020 \\[6pt] & P(\text{phone} \mid \text{sent back}) = \frac{0.012}{0.020} = 0.6 \end{aligned}

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:

The spam filter on 200 emails
Predicted spamPredicted not spamTotal
Really spam481260
Really not spam14126140
Total62138200

Dividing by 200 turns the counts into probabilities. Three kinds can be read from such a table:

Three kinds of probability in one table
KindExampleFrom the table
joint: both at onceP(really spam and predicted spam)P(\text{really spam and predicted spam})48200=0.24\tfrac{48}{200} = 0.24
marginal: one feature aloneP(predicted spam)P(\text{predicted spam})62200=0.31\tfrac{62}{200} = 0.31
conditional: inside a row or a columnP(really spam∣predicted spam)P(\text{really spam} \mid \text{predicted spam})4862≈0.774\tfrac{48}{62} \approx 0.774

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:

accuracy=P(the prediction is right)=48+126200=0.87precision=P(really spam∣predicted spam)=4862≈0.774recall=P(predicted spam∣really spam)=4860=0.80\begin{aligned} & \text{accuracy} = P(\text{the prediction is right}) \\ & \quad = \tfrac{48 + 126}{200} = 0.87 \\[6pt] & \text{precision} = P(\text{really spam} \mid \text{predicted spam}) \\ & \quad = \tfrac{48}{62} \approx 0.774 \\[6pt] & \text{recall} = P(\text{predicted spam} \mid \text{really spam}) \\ & \quad = \tfrac{48}{60} = 0.80 \end{aligned}

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 P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A) again.

precision = 48 / 62 ≈ 0.774

recall = 48 / 60 = 0.80

The same table read in two directions. Precision looks at a column: of the 62 emails called spam, 48 are spam. Recall looks at a row: of the 60 spam emails, 48 were caught. The counted cell is filled, the group it is compared with is outlined.

Try it yourself

A model that recognises cats was tested on 100 photos. Of the 40 photos with a cat, it said "cat" for 30. Of the 60 photos without a cat, it said "cat" for 20. Find the accuracy, the precision and the recall of the model.

Show a hint
Write the table first. Precision looks at the column of the photos called "cat", recall at the row of the real cat photos.
Show the solution

Answer: Accuracy 0.7, precision 0.6, recall 0.75.

The 100 photos
Called "cat"Called "no cat"Total
Really a cat301040
Really no cat204060
Total5050100
accuracy=30+40100=0.7precision=P(really a cat∣called "cat")=3050=0.6recall=P(called "cat"∣really a cat)=3040=0.75\begin{aligned} & \text{accuracy} = \tfrac{30 + 40}{100} = 0.7 \\[6pt] & \text{precision} = P(\text{really a cat} \mid \text{called "cat"}) \\ & \quad = \tfrac{30}{50} = 0.6 \\[6pt] & \text{recall} = P(\text{called "cat"} \mid \text{really a cat}) \\ & \quad = \tfrac{30}{40} = 0.75 \end{aligned}

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?

P(spam∣free)=0.30×0.600.30×0.60+0.70×0.05=0.180.18+0.035=0.180.215≈0.837\begin{aligned} P(\text{spam} \mid \text{free}) &= \frac{0.30 \times 0.60}{0.30 \times 0.60 + 0.70 \times 0.05} \\[4pt] &= \frac{0.18}{0.18 + 0.035} = \frac{0.18}{0.215} \approx 0.837 \end{aligned}

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.

spam:0.30×0.60×0.10=0.018not spam:0.70×0.05×0.40=0.014\begin{aligned} \text{spam:} \quad & 0.30 \times 0.60 \times 0.10 = 0.018 \\ \text{not spam:} \quad & 0.70 \times 0.05 \times 0.40 = 0.014 \end{aligned}
P(spam∣free and meeting)=0.0180.018+0.014=0.0180.032≈0.563\begin{aligned} & P(\text{spam} \mid \text{free and meeting}) \\ & \quad = \frac{0.018}{0.018 + 0.014} = \frac{0.018}{0.032} \approx 0.563 \end{aligned}

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

More places where a probability appears
WhereThe probability
A classifier's outputP(class∣features)P(\text{class} \mid \text{features}), a conditional probability
The decision rulepredict "spam" when this probability is larger than a threshold, usually 0.5
A language modelP(next word∣the words so far)P(\text{next word} \mid \text{the words so far})
A random train/test splitevery row lands in the test set with the same probability
A recommendationP(the user clicks∣the item shown)P(\text{the user clicks} \mid \text{the item shown})

Try it yourself

A quarter of the emails of a mailbox are spam. The word "prize" appears in 40 percent of the spam emails and in 5 percent of the others. The word "tomorrow" appears in 10 percent of the spam emails and in 20 percent of the others. An email contains both words. With the Naive Bayes idea, find the two products and the probability that the email is spam.

Show a hint
For each class multiply the share of the class by the probabilities of the two words. Then divide the spam product by the sum of both.
Show the solution

Answer: The products are 0.01 for spam and 0.0075 for not spam, and P(spam∣both words)=47≈0.571P(\text{spam} \mid \text{both words}) = \tfrac{4}{7} \approx 0.571.

spam:0.25×0.40×0.10=0.0100not spam:0.75×0.05×0.20=0.0075\begin{aligned} \text{spam:} \quad & 0.25 \times 0.40 \times 0.10 = 0.0100 \\ \text{not spam:} \quad & 0.75 \times 0.05 \times 0.20 = 0.0075 \end{aligned}
P(spam∣both words)=0.01000.0100+0.0075=0.01000.0175≈0.571\begin{aligned} & P(\text{spam} \mid \text{both words}) \\ & \quad = \frac{0.0100}{0.0100 + 0.0075} = \frac{0.0100}{0.0175} \approx 0.571 \end{aligned}

"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:

The number of heads in two tosses
Value012
Probability14\tfrac{1}{4}12\tfrac{1}{2}14\tfrac{1}{4}

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:

E=value1×probability1+value2×probability2+…\begin{aligned} E = {} & \text{value}_1 \times \text{probability}_1 \\ & + \text{value}_2 \times \text{probability}_2 + \dots \end{aligned}

For a die:

E=1×16+2×16+3×16+4×16+5×16+6×16=216=3.5\begin{aligned} E &= 1 \times \tfrac{1}{6} + 2 \times \tfrac{1}{6} + 3 \times \tfrac{1}{6} \\[2pt] &\quad + 4 \times \tfrac{1}{6} + 5 \times \tfrac{1}{6} + 6 \times \tfrac{1}{6} \\[2pt] &= \tfrac{21}{6} = 3.5 \end{aligned}

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.

E=8×16+(−2)×56=86−106=−26≈−0.33E = 8 \times \tfrac{1}{6} + (-2) \times \tfrac{5}{6} = \tfrac{8}{6} - \tfrac{10}{6} = -\tfrac{2}{6} \approx -0.33

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

A raffle ticket costs 4. It wins 40 with the probability 0.05 and 4 with the probability 0.25; otherwise it wins nothing. Find the expected win of a ticket, and the average result for the buyer after paying for the ticket.

Show a hint
Every win times its probability, then add. Then subtract the price of the ticket.
Show the solution

Answer: The expected win is 3, and the average result is −1-1 per ticket.

E(win)=40×0.05+4×0.25+0×0.70=2+1+0=3average result=3−4=−1\begin{aligned} & E(\text{win}) = 40 \times 0.05 + 4 \times 0.25 + 0 \times 0.70 \\ & \quad = 2 + 1 + 0 = 3 \\[4pt] & \text{average result} = 3 - 4 = -1 \end{aligned}

The three probabilities add up to 1: 0.05+0.25+0.700.05 + 0.25 + 0.70. 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. P(king or heart)P(\text{king or heart}) is not 452+1352\tfrac{4}{52} + \tfrac{13}{52}. Subtract the king of hearts: P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B).

Multiplying probabilities of events that are not independent. P(both aces)P(\text{both aces}) is not 452×452\tfrac{4}{52} \times \tfrac{4}{52} when the first card is not put back. Use P(A∩B)=P(A)×P(B∣A)P(A \cap B) = P(A) \times P(B \mid A).

Mixing up P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A). "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 P(sum is 7)P(\text{sum is 7}) is not 111\tfrac{1}{11}. The 36 pairs are equally likely, the sums are not.

Forgetting that order creates outcomes. (2,5)(2, 5) and (5,2)(5, 2) 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 12\tfrac{1}{2}, 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 1.21.2 or −0.1-0.1, something was added or subtracted wrongly.

Using permutations for a choice without order. A team or a hand of cards has no order: use C(n,k)C(n, k).

"At least one" by adding. P(at least one six in 4 rolls)P(\text{at least one six in 4 rolls}) is not 4×164 \times \tfrac{1}{6}. Use 1−P(none)1 - P(\text{none}).

Common mistakes and what is right
MistakeWhat is right
P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B) alwayssubtract P(A∩B)P(A \cap B) unless the events are mutually exclusive
P(A∩B)=P(A)×P(B)P(A \cap B) = P(A) \times P(B) alwaysonly for independent events; otherwise P(A)×P(B∣A)P(A) \times P(B \mid A)
P(A∣B)=P(B∣A)P(A \mid B) = P(B \mid A)they are different; Bayes' theorem connects them
"exclusive means independent"exclusive events are dependent
P(sum of two dice is 7)=111P(\text{sum of two dice is 7}) = \tfrac{1}{11}636=16\tfrac{6}{36} = \tfrac{1}{6}: count equally likely outcomes
"after five heads a tail is due"independent tosses have no memory
P(at least one)=n×pP(\text{at least one}) = n \times p1−P(none)1 - P(\text{none})

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).

S={ HHH,  HHT,  HTH,  THH,HTT,  THT,  TTH,  TTT }\begin{aligned} S = \{\, & HHH,\; HHT,\; HTH,\; THH, \\ & HTT,\; THT,\; TTH,\; TTT \,\} \end{aligned}

That is 2×2×2=82 \times 2 \times 2 = 8 outcomes.

exactly two heads: {HHT,HTH,THH}P=38at least one head: the complement is {TTT}P=1−18=78\begin{aligned} & \text{exactly two heads: } \{HHT, HTH, THH\} \\ & \quad P = \tfrac{3}{8} \\[4pt] & \text{at least one head: the complement is } \{TTT\} \\ & \quad P = 1 - \tfrac{1}{8} = \tfrac{7}{8} \end{aligned}

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. AA is "a multiple of 3" and BB is "an odd number". Write AA, BB, A∩BA \cap B and A∪BA \cup B, and find their probabilities.

Idea. Write the events as sets, then count (Part 3). The addition rule is a check (Part 5).

A={3,6,9}P(A)=310B={1,3,5,7,9}P(B)=510A∩B={3,9}P(A∩B)=210A∪B={1,3,5,6,7,9}P(A∪B)=610\begin{aligned} A &= \{3, 6, 9\} && P(A) = \tfrac{3}{10} \\ B &= \{1, 3, 5, 7, 9\} && P(B) = \tfrac{5}{10} \\ A \cap B &= \{3, 9\} && P(A \cap B) = \tfrac{2}{10} \\ A \cup B &= \{1, 3, 5, 6, 7, 9\} && P(A \cup B) = \tfrac{6}{10} \end{aligned}

The check: 310+510−210=610\tfrac{3}{10} + \tfrac{5}{10} - \tfrac{2}{10} = \tfrac{6}{10}.

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).

P(on time)=1−0.35=0.65P(not a six)=1−16=56\begin{aligned} P(\text{on time}) &= 1 - 0.35 = 0.65 \\ P(\text{not a six}) &= 1 - \tfrac{1}{6} = \tfrac{5}{6} \end{aligned}

Why: "late" and "on time" are the only two possibilities and cannot happen together. Whenever a problem says "not", think of 1−P1 - P.

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).

red cards:26face cards:12(3 in each of the 4 suits)red face cards:6(3 hearts, 3 diamonds)\begin{aligned} \text{red cards:} \quad & 26 \\ \text{face cards:} \quad & 12 && (3 \text{ in each of the 4 suits}) \\ \text{red face cards:} \quad & 6 && (3 \text{ hearts, } 3 \text{ diamonds}) \end{aligned}
P(red or face)=2652+1252−652=3252=813≈0.615P(\text{red or face}) = \tfrac{26}{52} + \tfrac{12}{52} - \tfrac{6}{52} = \tfrac{32}{52} = \tfrac{8}{13} \approx 0.615

Why: without the subtraction the six red face cards would be counted twice, and the result would be 3852\tfrac{38}{52}. 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).

sum 4:(1,3),  (2,2),  (3,1)3 outcomessum 9:(3,6),  (4,5),  (5,4),  (6,3)4 outcomes\begin{aligned} \text{sum 4:} \quad & (1, 3),\; (2, 2),\; (3, 1) && 3 \text{ outcomes} \\ \text{sum 9:} \quad & (3, 6),\; (4, 5),\; (5, 4),\; (6, 3) && 4 \text{ outcomes} \end{aligned}
P(sum is 4 or 9)=336+436=736≈0.194P(\text{sum is 4 or 9}) = \tfrac{3}{36} + \tfrac{4}{36} = \tfrac{7}{36} \approx 0.194

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).

sum 8: (2,6),  (3,5),  (4,4),  (5,3),  (6,2)P=536sum at most 4: (1,1),  (1,2),  (2,1),(1,3),  (2,2),  (3,1)P=636=16\begin{aligned} & \text{sum 8: } (2, 6),\; (3, 5),\; (4, 4),\; (5, 3),\; (6, 2) \\ & \quad P = \tfrac{5}{36} \\[6pt] & \text{sum at most 4: } (1, 1),\; (1, 2),\; (2, 1), \\ & \quad (1, 3),\; (2, 2),\; (3, 1) \\ & \quad P = \tfrac{6}{36} = \tfrac{1}{6} \end{aligned}

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).

26×26×10×10×10=676×1000=676,00026 \times 26 \times 10 \times 10 \times 10 = 676 \times 1000 = 676{,}000

Why: five positions, five independent choices. If repeats were not allowed it would be 26×25×10×9×8=468,00026 \times 25 \times 10 \times 9 \times 8 = 468{,}000: 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).

(a)6!=6×5×4×3×2×1=720(b)9×8×7=504\begin{aligned} \text{(a)} \quad & 6! = 6 \times 5 \times 4 \times 3 \times 2 \times 1 = 720 \\ \text{(b)} \quad & 9 \times 8 \times 7 = 504 \end{aligned}

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.

all teams:C(10,4)=10×9×8×74×3×2×1=210teams with Leyla:C(9,3)=9×8×73×2×1=84P(Leyla is in the team)=84210=25\begin{aligned} & \text{all teams:} \\ & \quad C(10, 4) = \frac{10 \times 9 \times 8 \times 7}{4 \times 3 \times 2 \times 1} = 210 \\[6pt] & \text{teams with Leyla:} \\ & \quad C(9, 3) = \frac{9 \times 8 \times 7}{3 \times 2 \times 1} = 84 \\[6pt] & P(\text{Leyla is in the team}) = \frac{84}{210} = \frac{2}{5} \end{aligned}

For the teams with Leyla she is in, and 3 more people are chosen from the other 9.

Why: the answer 410\tfrac{4}{10} 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).

all hands:C(52,5)=52×51×50×49×485×4×3×2×1=2,598,960hands of hearts:C(13,5)=13×12×11×10×95×4×3×2×1=1,287P(five hearts)=1,2872,598,960≈0.000495\begin{aligned} & \text{all hands:} \\ & \quad C(52, 5) = \frac{52 \times 51 \times 50 \times 49 \times 48}{5 \times 4 \times 3 \times 2 \times 1} \\[4pt] & \quad = 2{,}598{,}960 \\[6pt] & \text{hands of hearts:} \\ & \quad C(13, 5) = \frac{13 \times 12 \times 11 \times 10 \times 9}{5 \times 4 \times 3 \times 2 \times 1} \\[4pt] & \quad = 1{,}287 \\[6pt] & P(\text{five hearts}) = \frac{1{,}287}{2{,}598{,}960} \approx 0.000495 \end{aligned}

Why: about 1 hand in 2,000. The same number comes from the multiplication rule: 1352×1251×1150×1049×948\tfrac{13}{52} \times \tfrac{12}{51} \times \tfrac{11}{50} \times \tfrac{10}{49} \times \tfrac{9}{48}. 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 P(bought)P(\text{bought}), P(bought∣member)P(\text{bought} \mid \text{member}), P(bought∣not a member)P(\text{bought} \mid \text{not a member}) and P(member∣bought)P(\text{member} \mid \text{bought}).

Idea. Write the table. A condition picks one row or one column (Part 7).

The 200 customers
BoughtDid notTotal
Member562480
Not a member3684120
Total92108200
P(bought)=92200=0.46P(bought∣member)=5680=0.70P(bought∣not a member)=36120=0.30P(member∣bought)=5692≈0.609\begin{aligned} P(\text{bought}) &= \tfrac{92}{200} = 0.46 \\[4pt] P(\text{bought} \mid \text{member}) &= \tfrac{56}{80} = 0.70 \\[4pt] P(\text{bought} \mid \text{not a member}) &= \tfrac{36}{120} = 0.30 \\[4pt] P(\text{member} \mid \text{bought}) &= \tfrac{56}{92} \approx 0.609 \end{aligned}

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 P(A)=0.5P(A) = 0.5, P(B)=0.4P(B) = 0.4 and P(A∩B)=0.2P(A \cap B) = 0.2. Find P(A∪B)P(A \cup B), P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A). Are AA and BB independent?

Idea. The addition rule, the definition of a conditional probability, and the product test for independence (Parts 5, 7 and 8).

P(A∪B)=0.5+0.4−0.2=0.7P(A∣B)=P(A∩B)P(B)=0.20.4=0.5P(B∣A)=P(A∩B)P(A)=0.20.5=0.4P(A)×P(B)=0.5×0.4=0.2=P(A∩B)\begin{aligned} P(A \cup B) &= 0.5 + 0.4 - 0.2 = 0.7 \\[4pt] P(A \mid B) &= \frac{P(A \cap B)}{P(B)} = \frac{0.2}{0.4} = 0.5 \\[4pt] P(B \mid A) &= \frac{P(A \cap B)}{P(A)} = \frac{0.2}{0.5} = 0.4 \\[4pt] P(A) \times P(B) &= 0.5 \times 0.4 = 0.2 = P(A \cap B) \end{aligned}

The product equals P(A∩B)P(A \cap B): the events are independent.

Why: independence shows twice here: in the product, and in P(A∣B)=0.5=P(A)P(A \mid B) = 0.5 = P(A). Knowing BB did not change the probability of AA.

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).

P(both good)=610×59=3090=13good, then defective:610×49=2490defective, then good:410×69=2490P(exactly one defective)=4890=815P(both defective)=410×39=1290=215\begin{aligned} P(\text{both good}) &= \tfrac{6}{10} \times \tfrac{5}{9} = \tfrac{30}{90} = \tfrac{1}{3} \\[6pt] \text{good, then defective:} \quad & \tfrac{6}{10} \times \tfrac{4}{9} = \tfrac{24}{90} \\ \text{defective, then good:} \quad & \tfrac{4}{10} \times \tfrac{6}{9} = \tfrac{24}{90} \\ P(\text{exactly one defective}) &= \tfrac{48}{90} = \tfrac{8}{15} \\[6pt] P(\text{both defective}) &= \tfrac{4}{10} \times \tfrac{3}{9} = \tfrac{12}{90} = \tfrac{2}{15} \end{aligned}

The check: 515+815+215=1\tfrac{5}{15} + \tfrac{8}{15} + \tfrac{2}{15} = 1.

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. BB is "the first die shows 3". Is BB independent of AA = "the sum is 7"? Is it independent of CC = "the sum is 8"?

Idea. Compare P(A∩B)P(A \cap B) with P(A)×P(B)P(A) \times P(B) (Part 8).

P(B)=636=16A:P(A)=636=16,A∩B={(3,4)}P(A∩B)=136P(A)×P(B)=16×16=136C:P(C)=536,C∩B={(3,5)}P(C∩B)=136P(C)×P(B)=536×16=5216\begin{aligned} & P(B) = \tfrac{6}{36} = \tfrac{1}{6} \\[6pt] & A: \quad P(A) = \tfrac{6}{36} = \tfrac{1}{6}, \quad A \cap B = \{(3, 4)\} \\ & \quad P(A \cap B) = \tfrac{1}{36} \\ & \quad P(A) \times P(B) = \tfrac{1}{6} \times \tfrac{1}{6} = \tfrac{1}{36} \\[6pt] & C: \quad P(C) = \tfrac{5}{36}, \quad C \cap B = \{(3, 5)\} \\ & \quad P(C \cap B) = \tfrac{1}{36} \\ & \quad P(C) \times P(B) = \tfrac{5}{36} \times \tfrac{1}{6} = \tfrac{5}{216} \end{aligned}

AA and BB are independent. CC and BB 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).

P(all three)=0.7×0.7×0.7=0.343P(no score)=0.3×0.3×0.3=0.027P(at least one)=1−0.027=0.973\begin{aligned} P(\text{all three}) &= 0.7 \times 0.7 \times 0.7 = 0.343 \\[4pt] P(\text{no score}) &= 0.3 \times 0.3 \times 0.3 = 0.027 \\ P(\text{at least one}) &= 1 - 0.027 = 0.973 \end{aligned}

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).

(a) both work:0.9×0.9=0.81(b) at least one works:1−0.1×0.1=0.99\begin{aligned} \text{(a) both work:} \quad & 0.9 \times 0.9 = 0.81 \\ \text{(b) at least one works:} \quad & 1 - 0.1 \times 0.1 = 0.99 \end{aligned}

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).

P(late)=0.50×0.02+0.30×0.04+0.20×0.10=0.010+0.012+0.020=0.042\begin{aligned} P(\text{late}) &= 0.50 \times 0.02 + 0.30 \times 0.04 \\ &\quad + 0.20 \times 0.10 \\ &= 0.010 + 0.012 + 0.020 = 0.042 \end{aligned}

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).

P(firm 3∣late)=P(firm 3 and late)P(late)=0.0200.042=1021≈0.476\begin{aligned} P(\text{firm 3} \mid \text{late}) &= \frac{P(\text{firm 3 and late})}{P(\text{late})} \\[4pt] &= \frac{0.020}{0.042} = \frac{10}{21} \approx 0.476 \end{aligned}

Why: firm 3 carries only 20 percent of the parcels, but almost half of the late ones. The three numbers 0.0100.010, 0.0120.012, 0.0200.020 of Problem 17 give all three answers at once: 1042\tfrac{10}{42}, 1242\tfrac{12}{42} and 2042\tfrac{20}{42}.

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).

ill:200 people,90% positive: 180healthy:9,800 people,8% positive: 784positive tests:180+784=964P(ill∣positive)=180964≈0.187\begin{aligned} \text{ill:} \quad & 200 \text{ people}, \quad 90\% \text{ positive: } 180 \\ \text{healthy:} \quad & 9{,}800 \text{ people}, \quad 8\% \text{ positive: } 784 \\[4pt] \text{positive tests:} \quad & 180 + 784 = 964 \\[4pt] P(\text{ill} \mid \text{positive}) &= \frac{180}{964} \approx 0.187 \end{aligned}

With the formula:

0.02×0.900.02×0.90+0.98×0.08=0.0180.0964≈0.187\frac{0.02 \times 0.90}{0.02 \times 0.90 + 0.98 \times 0.08} = \frac{0.018}{0.0964} \approx 0.187

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).

ill, two positive tests:0.02×0.90×0.90=0.0162healthy, two positive tests:0.98×0.08×0.08=0.006272\begin{aligned} & \text{ill, two positive tests:} \\ & \quad 0.02 \times 0.90 \times 0.90 = 0.0162 \\[4pt] & \text{healthy, two positive tests:} \\ & \quad 0.98 \times 0.08 \times 0.08 = 0.006272 \end{aligned}
P(ill∣two positive tests)=0.01620.0162+0.006272=0.01620.022472≈0.721\begin{aligned} P(\text{ill} \mid \text{two positive tests}) &= \frac{0.0162}{0.0162 + 0.006272} \\[4pt] &= \frac{0.0162}{0.022472} \approx 0.721 \end{aligned}

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).

spam:0.40×0.50×0.05=0.0100not spam:0.60×0.02×0.30=0.0036\begin{aligned} \text{spam:} \quad & 0.40 \times 0.50 \times 0.05 = 0.0100 \\ \text{not spam:} \quad & 0.60 \times 0.02 \times 0.30 = 0.0036 \end{aligned}
P(spam∣win and invoice)=0.01000.0100+0.0036=0.01000.0136≈0.735\begin{aligned} & P(\text{spam} \mid \text{win and invoice}) \\ & \quad = \frac{0.0100}{0.0100 + 0.0036} = \frac{0.0100}{0.0136} \approx 0.735 \end{aligned}

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).

The 500 patients
Called illCalled healthyTotal
Really ill641680
Really healthy42378420
Total106394500
accuracy=64+378500=442500=0.884precision=P(really ill∣called ill)=64106≈0.604recall=P(called ill∣really ill)=6480=0.80P(really ill∣called healthy)=16394≈0.041\begin{aligned} & \text{accuracy} = \tfrac{64 + 378}{500} = \tfrac{442}{500} = 0.884 \\[6pt] & \text{precision} = P(\text{really ill} \mid \text{called ill}) \\ & \quad = \tfrac{64}{106} \approx 0.604 \\[6pt] & \text{recall} = P(\text{called ill} \mid \text{really ill}) \\ & \quad = \tfrac{64}{80} = 0.80 \\[6pt] & P(\text{really ill} \mid \text{called healthy}) = \tfrac{16}{394} \approx 0.041 \end{aligned}

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).

all possibilities:12×12×12×12=20,736all months different:12×11×10×9=11,880P(all different)=11,88020,736≈0.573P(at least two the same)=1−0.573≈0.427\begin{aligned} & \text{all possibilities:} \\ & \quad 12 \times 12 \times 12 \times 12 = 20{,}736 \\[4pt] & \text{all months different:} \\ & \quad 12 \times 11 \times 10 \times 9 = 11{,}880 \\[6pt] & P(\text{all different}) = \frac{11{,}880}{20{,}736} \approx 0.573 \\[4pt] & P(\text{at least two the same}) = 1 - 0.573 \approx 0.427 \end{aligned}

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).

The three places of the car
Car behindHost opensYou stay with door 1You change
door 1door 2 or 3winlose
door 2door 3losewin
door 3door 2losewin
P(win if you stay)=13P(win if you change)=23\begin{aligned} P(\text{win if you stay}) &= \tfrac{1}{3} \\[4pt] P(\text{win if you change}) &= \tfrac{2}{3} \end{aligned}

Why: your first choice is right with the probability 13\tfrac{1}{3}, 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).

all draws:C(30,5)=30×29×28×27×265×4×3×2×1=142,5063 of the 5 marked: C(5,3)=102 of the 25 others: C(25,2)=25×242=300exactly 3 hits: 10×300=3,000 drawsP(exactly 3 hits)=3,000142,506≈0.0211\begin{aligned} & \text{all draws:} \\ & \quad C(30, 5) = \frac{30 \times 29 \times 28 \times 27 \times 26}{5 \times 4 \times 3 \times 2 \times 1} \\[4pt] & \quad = 142{,}506 \\[6pt] & \text{3 of the 5 marked: } C(5, 3) = 10 \\[4pt] & \text{2 of the 25 others: } C(25, 2) = \frac{25 \times 24}{2} = 300 \\[6pt] & \text{exactly 3 hits: } 10 \times 300 = 3{,}000 \text{ draws} \\[6pt] & P(\text{exactly 3 hits}) = \frac{3{,}000}{142{,}506} \approx 0.0211 \end{aligned}

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).

The three results of the game
Result of the dieProbabilityValue
six16\tfrac{1}{6}1212
4 or 526\tfrac{2}{6}33
1, 2 or 336\tfrac{3}{6}−4-4
E=12×16+3×26+(−4)×36=2+1−2=1\begin{aligned} E &= 12 \times \tfrac{1}{6} + 3 \times \tfrac{2}{6} + (-4) \times \tfrac{3}{6} \\ &= 2 + 1 - 2 = 1 \end{aligned}

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. 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 14\tfrac{1}{4}.

    There are 2×6=122 \times 6 = 12 outcomes, all equally likely. Heads with an even number: (H,2)(H, 2), (H,4)(H, 4), (H,6)(H, 6), so 312=14\tfrac{3}{12} = \tfrac{1}{4}.

  2. 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: 12\tfrac{1}{2}.

    S={HH,HT,TH,TT}S = \{HH, HT, TH, TT\}. Exactly one head: HTHT and THTH, so 24=12\tfrac{2}{4} = \tfrac{1}{2}.

  3. 3.A card is drawn from cards numbered 1 to 12. AA is "a multiple of 4" and BB is "larger than 8". Find P(A∩B)P(A \cap B) and P(A∪B)P(A \cup B).

    Show a hint
    Write both events as sets first.
    Show one possible solution

    Answer: P(A∩B)=112P(A \cap B) = \tfrac{1}{12} and P(A∪B)=12P(A \cup B) = \tfrac{1}{2}.

    A={4,8,12},B={9,10,11,12}A∩B={12},P(A∩B)=112A∪B={4,8,9,10,11,12}P(A∪B)=612=12\begin{aligned} A &= \{4, 8, 12\}, \quad B = \{9, 10, 11, 12\} \\ A \cap B &= \{12\}, \quad P(A \cap B) = \tfrac{1}{12} \\ A \cup B &= \{4, 8, 9, 10, 11, 12\} \\ P(A \cup B) &= \tfrac{6}{12} = \tfrac{1}{2} \end{aligned}

    Check: 312+412−112=612\tfrac{3}{12} + \tfrac{4}{12} - \tfrac{1}{12} = \tfrac{6}{12}.

  4. 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.

    1−0.12=0.881 - 0.12 = 0.88
  5. 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: 23\tfrac{2}{3}.

    {3,4,5,6}\{3, 4, 5, 6\} has 4 of the 6 outcomes: 46=23\tfrac{4}{6} = \tfrac{2}{3}. With the complement: 1−P(1 or 2)=1−261 - P(\text{1 or 2}) = 1 - \tfrac{2}{6}.

  6. 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: 213≈0.154\tfrac{2}{13} \approx 0.154.

    No card is an ace and a king, so the events are mutually exclusive:

    452+452=852=213\tfrac{4}{52} + \tfrac{4}{52} = \tfrac{8}{52} = \tfrac{2}{13}
  7. 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: 713≈0.538\tfrac{7}{13} \approx 0.538.

    2652+452−252=2852=713\tfrac{26}{52} + \tfrac{4}{52} - \tfrac{2}{52} = \tfrac{28}{52} = \tfrac{7}{13}
  8. 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: 19≈0.111\tfrac{1}{9} \approx 0.111.

    (1,4)(1, 4), (2,3)(2, 3), (3,2)(3, 2), (4,1)(4, 1): 4 of 36 outcomes, 436=19\tfrac{4}{36} = \tfrac{1}{9}.

  9. 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: 14\tfrac{1}{4}.

    3×3=93 \times 3 = 9 pairs of even numbers among 36: 936=14\tfrac{9}{36} = \tfrac{1}{4}. The dice are independent, so also 12×12\tfrac{1}{2} \times \tfrac{1}{2}.

  10. 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.

    3×5×2=303 \times 5 \times 2 = 30
  11. 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.

    26×25×24×23=358,80026 \times 25 \times 24 \times 23 = 358{,}800
  12. 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.

    7!=7×6×5×4×3×2×1=5,0407! = 7 \times 6 \times 5 \times 4 \times 3 \times 2 \times 1 = 5{,}040
  13. 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.

    C(6,2)=6×52=15C(6, 2) = \frac{6 \times 5}{2} = 15
  14. 14.AA and BB are mutually exclusive with P(A)=0.3P(A) = 0.3 and P(B)=0.5P(B) = 0.5. Find P(A∪B)P(A \cup B) 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.

    P(A∪B)=0.3+0.5=0.8P(neither)=1−0.8=0.2\begin{aligned} P(A \cup B) &= 0.3 + 0.5 = 0.8 \\ P(\text{neither}) &= 1 - 0.8 = 0.2 \end{aligned}

Level 2: Counting, conditions and independence

  1. 15.For two events P(A)=0.6P(A) = 0.6, P(B)=0.5P(B) = 0.5 and P(A∪B)=0.8P(A \cup B) = 0.8. Find P(A∩B)P(A \cap B) and P(A∣B)P(A \mid B). Are the events independent?

    Show a hint
    Solve the addition rule for P(A∩B)P(A \cap B).
    Show one possible solution

    Answer: P(A∩B)=0.3P(A \cap B) = 0.3 and P(A∣B)=0.6P(A \mid B) = 0.6. They are independent.

    0.8=0.6+0.5−P(A∩B)P(A∩B)=0.3P(A∣B)=0.30.5=0.6=P(A)P(A)×P(B)=0.6×0.5=0.3\begin{aligned} 0.8 &= 0.6 + 0.5 - P(A \cap B) \\ P(A \cap B) &= 0.3 \\[4pt] P(A \mid B) &= \frac{0.3}{0.5} = 0.6 = P(A) \\[4pt] P(A) \times P(B) &= 0.6 \times 0.5 = 0.3 \end{aligned}

    The product equals P(A∩B)P(A \cap B): independent.

  2. 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 P(satisfied)P(\text{satisfied}), P(satisfied∣home)P(\text{satisfied} \mid \text{home}) and P(home∣satisfied)P(\text{home} \mid \text{satisfied}).

    Show a hint
    Write the table with its totals first.
    Show one possible solution

    Answer: 23≈0.667\tfrac{2}{3} \approx 0.667, 0.80.8 and 0.40.4.

    Satisfied in all: 40+60=10040 + 60 = 100.

    P(satisfied)=100150=23P(satisfied∣home)=4050=0.8P(home∣satisfied)=40100=0.4\begin{aligned} P(\text{satisfied}) &= \tfrac{100}{150} = \tfrac{2}{3} \\[4pt] P(\text{satisfied} \mid \text{home}) &= \tfrac{40}{50} = 0.8 \\[4pt] P(\text{home} \mid \text{satisfied}) &= \tfrac{40}{100} = 0.4 \end{aligned}
  3. 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 {1,3,5}\{1, 3, 5\}.
    Show one possible solution

    Answer: 13\tfrac{1}{3}.

    Among 1, 3, 5 only the 5 is larger than 4: 13\tfrac{1}{3}. With the formula:

    P(5)P(odd)=1/61/2=13\frac{P(5)}{P(\text{odd})} = \frac{1/6}{1/2} = \frac{1}{3}
  4. 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: 715≈0.467\tfrac{7}{15} \approx 0.467 and 715≈0.467\tfrac{7}{15} \approx 0.467.

    P(both green)=710×69=4290=715P(different colours)=310×79+710×39=2190+2190=4290=715\begin{aligned} P(\text{both green}) &= \tfrac{7}{10} \times \tfrac{6}{9} = \tfrac{42}{90} = \tfrac{7}{15} \\[4pt] P(\text{different colours}) &= \tfrac{3}{10} \times \tfrac{7}{9} + \tfrac{7}{10} \times \tfrac{3}{9} \\ &= \tfrac{21}{90} + \tfrac{21}{90} = \tfrac{42}{90} = \tfrac{7}{15} \end{aligned}

    Both red would be 310×29=115\tfrac{3}{10} \times \tfrac{2}{9} = \tfrac{1}{15}, and the three results add up to 1.

  5. 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.

    710×710=49100=0.49\tfrac{7}{10} \times \tfrac{7}{10} = \tfrac{49}{100} = 0.49
  6. 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: 117≈0.059\tfrac{1}{17} \approx 0.059.

    1352×1251=1562652=117≈0.059\tfrac{13}{52} \times \tfrac{12}{51} = \tfrac{156}{2652} = \tfrac{1}{17} \approx 0.059
  7. 21.AA and BB are independent with P(A)=0.2P(A) = 0.2 and P(B)=0.5P(B) = 0.5. Find P(A∩B)P(A \cap B), P(A∪B)P(A \cup B) 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.

    P(A∩B)=0.2×0.5=0.1P(A∪B)=0.2+0.5−0.1=0.6P(neither)=1−0.6=0.4\begin{aligned} P(A \cap B) &= 0.2 \times 0.5 = 0.1 \\ P(A \cup B) &= 0.2 + 0.5 - 0.1 = 0.6 \\ P(\text{neither}) &= 1 - 0.6 = 0.4 \end{aligned}

    The last result is also 0.8×0.50.8 \times 0.5: "not AA" and "not BB" are independent too.

  8. 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: 132≈0.031\tfrac{1}{32} \approx 0.031 and 3132≈0.969\tfrac{31}{32} \approx 0.969.

    P(five heads)=(12)5=132P(at least one tail)=1−132=3132\begin{aligned} P(\text{five heads}) &= \left(\tfrac{1}{2}\right)^5 = \tfrac{1}{32} \\ P(\text{at least one tail}) &= 1 - \tfrac{1}{32} = \tfrac{31}{32} \end{aligned}
  9. 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.

    P(all three miss)=0.2×0.2×0.2=0.008P(at least one notices)=1−0.008=0.992\begin{aligned} & P(\text{all three miss}) \\ & \quad = 0.2 \times 0.2 \times 0.2 = 0.008 \\[4pt] & P(\text{at least one notices}) \\ & \quad = 1 - 0.008 = 0.992 \end{aligned}
  10. 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 17≈0.143\tfrac{1}{7} \approx 0.143.

    C(7,3)=7×6×53×2×1=35P(both are in)=535=17\begin{aligned} C(7, 3) &= \frac{7 \times 6 \times 5}{3 \times 2 \times 1} = 35 \\[4pt] P(\text{both are in}) &= \frac{5}{35} = \frac{1}{7} \end{aligned}

    With Aysel and Kamran in the committee there are 5 choices for the third member.

  11. 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: 145≈0.022\tfrac{1}{45} \approx 0.022.

    C(10,2)=10×92=45C(10, 2) = \frac{10 \times 9}{2} = 45

    45 pairs are possible, and exactly one of them is your pair.

  12. 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.

    0.70×0.03+0.30×0.06=0.021+0.018=0.039\begin{aligned} 0.70 \times 0.03 + 0.30 \times 0.06 &= 0.021 + 0.018 \\ &= 0.039 \end{aligned}
  13. 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: 613≈0.462\tfrac{6}{13} \approx 0.462.

    P(computer∣buys)=0.0180.039=613≈0.462P(\text{computer} \mid \text{buys}) = \frac{0.018}{0.039} = \frac{6}{13} \approx 0.462

    Computers bring 30 percent of the visitors but 46 percent of the buyers.

  14. 28.AA and BB are mutually exclusive with P(A)=0.4P(A) = 0.4 and P(B)=0.3P(B) = 0.3. Find P(A∩B)P(A \cap B). Are AA and BB independent?

    Show a hint
    Compare P(A∩B)P(A \cap B) with the product P(A)×P(B)P(A) \times P(B).
    Show one possible solution

    Answer: P(A∩B)=0P(A \cap B) = 0. They are not independent.

    Mutually exclusive events cannot happen together, so P(A∩B)=0P(A \cap B) = 0. The product is 0.4×0.3=0.120.4 \times 0.3 = 0.12, which is not 0. If AA happens, BB is impossible: that is strong dependence.

Level 3: Bayes' theorem and putting ideas together

  1. 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.

    ill: 500 people,  490 test positivehealthy: 99,500 people,  1,990 test positiveP(ill∣positive)=490490+1,990=4902,480≈0.198\begin{aligned} & \text{ill: } 500 \text{ people}, \; 490 \text{ test positive} \\ & \text{healthy: } 99{,}500 \text{ people}, \; 1{,}990 \text{ test positive} \\[6pt] & P(\text{ill} \mid \text{positive}) = \frac{490}{490 + 1{,}990} \\[4pt] & \quad = \frac{490}{2{,}480} \approx 0.198 \end{aligned}

    Even with this very good test, four of five positive results are false alarms.

  2. 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: 57≈0.714\tfrac{5}{7} \approx 0.714.

    spam path:0.20×0.40=0.08other path:0.80×0.04=0.032P(spam∣offer)=0.080.08+0.032=0.080.112=57\begin{aligned} \text{spam path:} \quad & 0.20 \times 0.40 = 0.08 \\ \text{other path:} \quad & 0.80 \times 0.04 = 0.032 \\[4pt] P(\text{spam} \mid \text{offer}) &= \frac{0.08}{0.08 + 0.032} = \frac{0.08}{0.112} = \frac{5}{7} \end{aligned}
  3. 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.

    spam:0.5×0.3×0.2=0.03not spam:0.5×0.1×0.4=0.02P(spam∣both words)=0.030.03+0.02=0.6\begin{aligned} \text{spam:} \quad & 0.5 \times 0.3 \times 0.2 = 0.03 \\ \text{not spam:} \quad & 0.5 \times 0.1 \times 0.4 = 0.02 \\[4pt] P(\text{spam} \mid \text{both words}) &= \frac{0.03}{0.03 + 0.02} = 0.6 \end{aligned}
  4. 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 failurePredicted goodTotal
    Really failed351550
    Really good21329350
    Total56344400

    Predicted failures: 35+21=5635 + 21 = 56. Right predictions: 35+(350−21)=35+329=36435 + (350 - 21) = 35 + 329 = 364.

    precision=3556=0.625recall=3550=0.7accuracy=35+329400=364400=0.91\begin{aligned} \text{precision} &= \tfrac{35}{56} = 0.625 \\[4pt] \text{recall} &= \tfrac{35}{50} = 0.7 \\[4pt] \text{accuracy} &= \tfrac{35 + 329}{400} = \tfrac{364}{400} = 0.91 \end{aligned}
  5. 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: 819≈0.421\tfrac{8}{19} \approx 0.421.

    P(defective)=0.50×0.01+0.30×0.02+0.20×0.04=0.005+0.006+0.008=0.019\begin{aligned} & P(\text{defective}) \\ & \quad = 0.50 \times 0.01 + 0.30 \times 0.02 + 0.20 \times 0.04 \\ & \quad = 0.005 + 0.006 + 0.008 = 0.019 \end{aligned}
    P(machine 3∣defective)=0.0080.019=819≈0.421\begin{aligned} & P(\text{machine 3} \mid \text{defective}) \\ & \quad = \frac{0.008}{0.019} = \frac{8}{19} \approx 0.421 \end{aligned}
  6. 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: 15525≈0.00018\tfrac{1}{5525} \approx 0.00018.

    There are C(4,3)C(4, 3) hands of three aces, and C(52,3)C(52, 3) hands in all:

    C(4,3)=4C(52,3)=52×51×506=22,100P(three aces)=422,100=15,525\begin{aligned} C(4, 3) &= 4 \\[4pt] C(52, 3) &= \frac{52 \times 51 \times 50}{6} = 22{,}100 \\[4pt] P(\text{three aces}) &= \frac{4}{22{,}100} = \frac{1}{5{,}525} \end{aligned}

    The same with the multiplication rule: 452×351×250\tfrac{4}{52} \times \tfrac{3}{51} \times \tfrac{2}{50}.

  7. 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: 1021≈0.476\tfrac{10}{21} \approx 0.476.

    First the ways to take 2 red pens and 1 blue pen, then all choices of 3 pens out of 9:

    C(5,2)×C(4,1)=10×4=40C(9,3)=9×8×76=84P(exactly two red)=4084=1021\begin{aligned} C(5, 2) \times C(4, 1) &= 10 \times 4 = 40 \\[4pt] C(9, 3) &= \frac{9 \times 8 \times 7}{6} = 84 \\[4pt] P(\text{exactly two red}) &= \frac{40}{84} = \frac{10}{21} \end{aligned}
  8. 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: 1949≈0.388\tfrac{19}{49} \approx 0.388.

    all possibilities: 7×7×7=343all different: 7×6×5=210P(at least two the same)=1−210343=133343=1949\begin{aligned} & \text{all possibilities: } 7 \times 7 \times 7 = 343 \\ & \text{all different: } 7 \times 6 \times 5 = 210 \\[4pt] & P(\text{at least two the same}) \\ & \quad = 1 - \tfrac{210}{343} = \tfrac{133}{343} = \tfrac{19}{49} \end{aligned}
  9. 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.

    (a)0.9×0.9×0.9=0.729(b)1−0.1×0.1×0.1=0.999\begin{aligned} \text{(a)} \quad & 0.9 \times 0.9 \times 0.9 = 0.729 \\ \text{(b)} \quad & 1 - 0.1 \times 0.1 \times 0.1 = 0.999 \end{aligned}
  10. 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.

    P(no heart)=3952×3851×3750=54,834132,600≈0.414P(at least one heart)=1−0.414≈0.586\begin{aligned} & P(\text{no heart}) = \tfrac{39}{52} \times \tfrac{38}{51} \times \tfrac{37}{50} \\[4pt] & \quad = \tfrac{54{,}834}{132{,}600} \approx 0.414 \\[4pt] & P(\text{at least one heart}) = 1 - 0.414 \approx 0.586 \end{aligned}
  11. 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.

    E(win)=100×0.01+10×0.05+0×0.94=1+0.5=1.5average result=1.5−2=−0.5\begin{aligned} & E(\text{win}) = 100 \times 0.01 + 10 \times 0.05 + 0 \times 0.94 \\ & \quad = 1 + 0.5 = 1.5 \\[4pt] & \text{average result} = 1.5 - 2 = -0.5 \end{aligned}
  12. 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 14\tfrac{1}{4}).
    Show one possible solution

    Answer: 67≈0.857\tfrac{6}{7} \approx 0.857.

    knew and right: 0.6×1=0.6guessed and right: 0.4×0.25=0.1P(knew∣right)=0.60.6+0.1=67≈0.857\begin{aligned} & \text{knew and right: } 0.6 \times 1 = 0.6 \\ & \text{guessed and right: } 0.4 \times 0.25 = 0.1 \\[4pt] & P(\text{knew} \mid \text{right}) = \frac{0.6}{0.6 + 0.1} = \frac{6}{7} \approx 0.857 \end{aligned}

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.

Question 1

1.A die is rolled. What is the probability of a number larger than 4?

Question 2

2.Two dice are rolled. How many equally likely outcomes does the sample space have?

Question 3

3.P(A)=0.35P(A) = 0.35. What is P(Ac)P(A^c)?

Question 4

4.A card is drawn from a deck of 52. What is the probability of a king or a heart?

Question 5

5.In how many ways can a team of 2 be chosen from 5 people?

Question 6

6.A die is rolled, and you are told that the number is even. What is the probability that it is a six?

Question 7

7.Two cards are drawn without putting the first one back. Which product is the probability that both are aces?

Question 8

8.What does it mean that the events AA and BB are independent?

Question 9

9.A fair coin has shown heads five times in a row. What is the probability of heads on the next toss?

Question 10

10.A test for a rare disease is positive. Besides the two rates of the test, which number do you need to find P(ill∣positive)P(\text{ill} \mid \text{positive})?

Question 11

11.What is the precision of a spam filter?

Question 12

12.A game: you win 10 with the probability 0.2, otherwise you lose 2. What is the expected value of one game?

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.

Python
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 5!5!, C(8,3)C(8, 3) 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 16≈0.1667\tfrac{1}{6} \approx 0.1667. 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 and in Python
By handIn Python
n!n!math.factorial(n)
C(n,k)C(n, k)math.comb(n, k)
n!(n−k)!\dfrac{n!}{(n - k)!}math.perm(n, k)
an exact fractionfractions.Fraction(4, 52)
repeating an experimentnp.random.default_rng() and its methods

18Quick summary

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Sample space SSthe set of all outcomes of an experiment
Eventa set of outcomes
A∪BA \cup B, A∩BA \cap B, AcA^cAA or BB; AA and BB; not AA
Mutually exclusiveAA and BB cannot happen together: A∩BA \cap B is empty
Equally likely outcomesP(A)P(A) = outcomes in AA divided by outcomes in SS
Relative frequencyP(A)≈P(A) \approx times AA happened divided by the number of repetitions
Basic rules0≤P(A)≤10 \le P(A) \le 1, P(S)=1P(S) = 1, exclusive events add
Complement ruleP(Ac)=1−P(A)P(A^c) = 1 - P(A)
Addition ruleP(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B)
At least one1−P(none)1 - P(\text{none})
Multiplication principlemm ways, then nn ways: m×nm \times n ways
Factorialn!=n×(n−1)×⋯×1n! = n \times (n - 1) \times \dots \times 1 orders of nn things
Ordered choicen!(n−k)!\dfrac{n!}{(n - k)!}
CombinationC(n,k)=n!k! (n−k)!C(n, k) = \dfrac{n!}{k!\,(n - k)!}
Conditional probabilityP(A∣B)=P(A∩B)P(B)P(A \mid B) = \dfrac{P(A \cap B)}{P(B)}
Multiplication ruleP(A∩B)=P(B)×P(A∣B)P(A \cap B) = P(B) \times P(A \mid B)
Independent eventsP(A∩B)=P(A)×P(B)P(A \cap B) = P(A) \times P(B)
Law of total probabilityP(A)=P(B1)×P(A∣B1)+P(B2)×P(A∣B2)+…P(A) = P(B_1) \times P(A \mid B_1) + P(B_2) \times P(A \mid B_2) + \dots
Bayes' theoremP(B∣A)=P(B)×P(A∣B)P(A)P(B \mid A) = \dfrac{P(B) \times P(A \mid B)}{P(A)}
Prior, likelihood, posteriorbefore the information; fit of the information; after the information
Precision and recallP(really yes∣predicted yes)P(\text{really yes} \mid \text{predicted yes}) and P(predicted yes∣really yes)P(\text{predicted yes} \mid \text{really yes})
Expected valueevery value times its probability, added