Statistics For Programmers - Conditional Probability
The conditional probability of an event is the probability of an event occuring given that another event has occured. The It is denoted as \(P(B|A)\) and is read as "the probability of B given A". That is the probability of event B occuring knowing that event A has occured.
The formula for conditional probability is
\[
P(B|A) = \frac{P(A \cap B)}{P(A)}
\]
Where (P(A \cap B)) is the probability of both events A and B occuring.
Let's break this down with an example. Given a standard 52 card deck of playing cards, we want to find the probability of drawing 2 aces in a row. We will be drawing cards without replacing them - this is an important detail.
The probability \(P(A)\) is the probability of the first event (drawing an ace).
\[
P(A) = \frac{4}{52} = \frac{1}{13}
\]
Our second event is also drawing an Ace \(P(B)\), which on its own considers the full deck of cards.
\[
P(B) = \frac{4}{52} = \frac{1}{13} = P(A)
\].
However, since we are drawing without replacement, the probability of drawing a second ace is influenced by the result of our first draw.
Assuming we hit the jackpot and draw an Ace. The probability of drawing a second Ace is the intersection of the two events. In other words, the probability of drawing an Ace and then drawing another Ace.
\[
P(A \cap B) = \frac{4}{52} \times \frac{3}{51}
\]
This is because we have 4 Aces in a deck of 52 cards. After drawing the first Ace, we have 3 Aces left in a deck of 51 cards.
Substituting these values into our formula, we get
\[
P(B|A) = \frac{\frac{4}{52} \times \frac{3}{51}}{\frac{4}{52}} = \frac{3}{51} = \frac{1}{17}
\]
Intuitively, this makes sense that the probability of drawing a second Ace is lower after drawing the first since proportionally there are fewer Aces left in the deck.
We can use the formula as a test for dependence between the two events. Two events, \(A\) and \(B\), are independent if \(P(B|A) = P(B)\). In our case, the events are dependent since the probability of drawing a second Ace is influenced by the result of the first draw. This would not be the case if we were drawing with replacement.
Implementing this in code is pretty straight forward. We start by computing the probabilities of the individual events.
const deckSize = 52;
const aces = 4;
const pA = aces / deckSize;
console.log(pFirstAce);
// 0.07692307692307693
Next, we calculate the probability of drawing a second Ace given that we have drawn the first.
const pBGivenA = (aces - 1) / (deckSize - 1);
console.log(pBGivenA);
// 0.058823529411764705