Free preview

80 lessons

Essential Probability & Statistics for ML

Free preview

Essential Probability & Statistics for ML · 80 lessons

Master the fundamentals

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Bayes' theorem

Which is larger, P(A∣B)P(A \mid B) or P(B∣A)P(B \mid A)?

Explanation

One card is drawn from a shuffled deck. Is it more likely to be red given that it is an ace, or an ace given that it is red?

Half of the aces are red, but very few red cards are aces. The diagram below finds both conditional probabilities from the same four regions.

Ω
Aces (4)
Red (26)
2
2
24
24

Step 1: The four regions

Each region of Ω is labelled with its number of cards: aces that are not red 2, red aces 2, red cards that are not aces 24, and neither 24. So there are 4 aces and 26 red cards.

Both conditional probabilities use the same overlap, the 2 red aces.

Step 2: Given an ace

We learn that the card is an ace, so every card outside the aces is ruled out, and the 4 aces become the whole. The overlap is half of it:

P(red∣ace)=22+2=24=12

Step 3: Given red

Now we learn instead that the card is red, so every card outside red is ruled out, and the 26 red cards become the whole. The same overlap is only a small part of it:

P(ace∣red)=22+24=226=113

Step 4: Same overlap, different wholes

Both conditional probabilities divide the same overlap, 2 cards, by the number of cards in the event we are given. The overlap is half of the small aces circle but only a small part of the large red circle.

So the conditional probability given the rarer event, ace, is the larger.

The same holds for any events AA and BB. The conditional probability formula gives P(A∣B)P(A \mid B) and P(B∣A)P(B \mid A) the same numerator:

P(A∣B)=P(A∩B)P(B)P(B∣A)=P(A∩B)P(A)P(A \mid B) = \dfrac{P(A \cap B)}{P(B)} \qquad P(B \mid A) = \dfrac{P(A \cap B)}{P(A)}

Dividing the same numerator by the smaller denominator gives the larger probability:

Theorem

For events AA and BB that can both happen, if P(A)<P(B)P(A) < P(B) then

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

and if P(A)=P(B)P(A) = P(B) then the two are equal.

In words, the conditional probability given the rarer event is the larger. Deciding needs only which event is rarer, not the value of P(A∩B)P(A \cap B).

Practice questions

4 questions

Events AA and BB can both happen, with P(A)=0.1P(A) = 0.1 and P(B)=0.4P(B) = 0.4.

Which is true?

Select the correct answer:

+ 3 more questions

Bayes' rule

Explanation

Often we know the probability of the evidence given a cause, such as how often a fire makes smoke, but want the probability of the cause given the evidence: how likely a fire is, given smoke. Bayes' rule turns one of these conditional probabilities into the other.

It is one of the most used results in probability, behind spam filters, medical diagnosis and models that update their probabilities as data arrives.

It needs nothing new, only the multiplication rule for probabilities and the conditional probability formula.

The overlap A∩BA \cap B can be reached from either event, so the multiplication rule writes its probability in two ways:

P(A∩B)=P(A∣B) P(B)P(A∩B)=P(B∣A) P(A)P(A \cap B) = P(A \mid B)\,P(B) \qquad P(A \cap B) = P(B \mid A)\,P(A)

Both right-hand sides equal the same probability, so they equal each other:

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

Dividing both sides by P(B)P(B) gives Bayes' rule, also called Bayes' theorem:

Bayes' rule

Theorem

For events AA and BB with P(A)>0P(A) > 0 and P(B)>0P(B) > 0,

P(A∣B)=P(B∣A) P(A)P(B)P(A \mid B) = \dfrac{P(B \mid A)\,P(A)}{P(B)}

In words, Bayes' rule finds P(A∣B)P(A \mid B) from the reverse conditional probability, P(B∣A)P(B \mid A), and the probability of each event.

Example

In a town, 0.10.1 of days are rainy, and 0.50.5 of rainy days start cloudy. Of all days, 0.60.6 do not start cloudy. Given that a day starts cloudy, find the probability that it is rainy.

Solution

We name the events for a day chosen at random:

  • RR: the day is rainy, with P(R)=0.1P(R) = 0.1.
  • CC: the day starts cloudy.

The 0.50.5 is a share of rainy days, so P(C∣R)=0.5P(C \mid R) = 0.5. We want P(R∣C)P(R \mid C), the reverse conditional probability, so we use Bayes' rule:

P(R∣C)=P(C∣R) P(R)P(C)P(R \mid C) = \dfrac{P(C \mid R)\,P(R)}{P(C)}

We are given the probability that a day does not start cloudy, so the complement rule gives the denominator:

P(C)=1−P(Cc)=1−0.6=0.4P(C) = 1 - P(C^c) = 1 - 0.6 = 0.4

We substitute the three values:

P(R∣C)=0.5×0.10.4=0.050.4=0.125\begin{align*} P(R \mid C) &= \dfrac{0.5 \times 0.1}{0.4} \\[6pt] &= \dfrac{0.05}{0.4} \\[6pt] &= 0.125 \end{align*}

Given that a day starts cloudy, it is rainy with probability 0.1250.125.

Practice questions

4 questions

On 0.010.01 of days there is a fire in a warehouse, and on 0.10.1 of days its smoke alarm sounds. The alarm sounds on 0.90.9 of the days with a fire.

Given that the alarm sounds on a day, what is the probability that there is a fire?

Select the correct answer:

+ 3 more questions

Prior, likelihood and posterior

Explanation

Bayes' rule describes how a probability changes when we learn something. It turns the probability of an event AA before we learn that BB happened into its probability after. Each part of the rule has a name for its role in this update.

Prior, likelihood and posterior

Definition

In Bayes' rule, with AA the event we ask about and BB the event we learn,

P(A∣B)=P(B∣A) P(A)P(B)P(A \mid B) = \dfrac{P(B \mid A)\,P(A)}{P(B)}
  • The prior is P(A)P(A), the probability of AA before we learn BB.
  • The likelihood is P(B∣A)P(B \mid A), the probability of what we learn, given AA.
  • The posterior is P(A∣B)P(A \mid B), the probability of AA after we learn BB.

The denominator P(B)P(B), the probability of what we learn, has no name here.

"Prior" and "posterior" mean before and after learning BB: the prior is where we start, and the posterior is where we end up once BB is known.

The likelihood is a probability of BB, not of AA: it says how probable what we learned is if AA holds. Bayes' rule multiplies the prior by a factor built from it:

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

The factor compares how probable BB is when AA holds with how probable BB is overall. If it is above 11, learning BB raises the probability of AA; if it is below 11, learning BB lowers it.

ML Context

In machine learning, a model makes predictions using numbers called parameters, whose values are learned from data. Write θ\theta for one choice of parameter values and DD for the observed data. Bayes' rule then reads

P(θ∣D)=P(D∣θ) P(θ)P(D)P(\theta \mid D) = \dfrac{P(D \mid \theta)\,P(\theta)}{P(D)}

Many models are fitted by maximum likelihood: among all choices of θ\theta, it picks the one with the largest likelihood P(D∣θ)P(D \mid \theta), the one that makes the observed data most probable. The prior plays no part.

Bayesian inference uses the whole rule. It starts from a prior P(θ)P(\theta), saying which values are plausible before seeing the data, and finds the posterior P(θ∣D)P(\theta \mid D): how plausible each value is afterwards, not only which one fits best.

Example

Of the items a factory makes, 0.040.04 come from a machine with a fault. Of the items from a faulty machine, 0.50.5 are defective, and of all items, 0.10.1 are defective. An item is found to be defective. For the probability that it came from a faulty machine, name the prior, likelihood and posterior.

Solution

We start with the event asked about. We learn that the item is defective and ask whether it came from a faulty machine, so:

  • AA: the item came from a faulty machine.
  • BB: the item is defective.

The fault came before the defect, but the names follow the event we ask about, not the order in which events happen, so the fault is AA.

The 0.040.04 and the 0.10.1 are shares of all items, and the 0.50.5 is a share of items from a faulty machine:

P(A)=0.04P(B)=0.1P(B∣A)=0.5P(A) = 0.04 \qquad P(B) = 0.1 \qquad P(B \mid A) = 0.5

Then we label the roles:

  • The prior is P(A)=0.04P(A) = 0.04.
  • The likelihood is P(B∣A)=0.5P(B \mid A) = 0.5.
  • The posterior is P(A∣B)P(A \mid B), the probability that a defective item came from a faulty machine.

The 0.10.1 is P(B)P(B) and has no name here.

Practice questions

4 questions

In a region, 0.010.01 of houses flood each year. Of the houses that flood, 0.70.7 stand by the river, and of all houses, 0.10.1 stand by the river.

We learn that a house stands by the river and ask how likely it is to flood. What is the likelihood?

Select the correct answer:

+ 3 more questions