Probability of event A and event B

1. Probability of event A and event B

So far we've been using coin flips as a simple example of random phenomena. But let's step back from that to talk about what a coin flip represents: a random variable that is either "yes" or "no."

2. Event A: "Coin is heads"

For example, A could represent "a coin flip results in heads." A then either happens- it's heads- or it doesn't- it's tails: with some probability. Throughout this chapter, we'll refer to coin flips and events interchangeably - we could say "A is an event with probability 20%" or "A is a coin with probability 20% of being heads." In reality, these events could represent the probability it is snowing outside, or the probability that someone will click an advertisement on a website. And in this chapter, you'll learn some of the mathematical laws that govern these kinds of random events and let us make predictions about them.

3. Events A and B: Two Different Coins

Now let's consider if we have two events, A and B. Event A is the result of one flip, either 1 for heads or 0 for tails, each with some probability, and event B is the result of the second flip. Now suppose you want to know the probability of A and B: that is, the probability that both flips are heads?

4. Probability of A and B

Well, consider this in terms of nested branches. First, you flip coin A. There's some probability that it results in heads, and some probability it results in tails. This branches into two possibilities, A = 1 or A = 0. Second, you flip coin B. This causes both to branch again, separately. Only that one branch where both A and B resulted in 1, or heads, counts as A AND B. We represent that by multiplying those two probabilities, to represent the probability of choosing one branch after the other. The probability of A and B is the probability of A times the probability of B. Note that this is true only if events A and B are independent: that is, if the result of A doesn't affect the probability of B. That is generally true of two different coin flips, and of all the cases we'll examine in this chapter.

5. Simulating two coins

To confirm this, let's try a simulation of many coin flips. We can simulate 100,000 flips of coin A. Notice that we've set the parameters so that each draw has only one flip, and each flip has a 50% chance of being heads. We can then simulate 100,000 flips of coin B separately. Once we have all the flips, we can compare all the pairs of flips. R lets you combine them with the "AND" operator, a single ampersand. This will compare each corresponding flip in A and B, and result in TRUE if and only if both A and B are true. We thus get a sequence of TRUEs and FALSEs for each of the 100,000 pairs. Once we have these, we can take the mean to find the percentage that are TRUE, just as we did in the simulations last chapter. In this case, we see that A and B were both true in about 25% of the simulations. This is the probability that two fair coins will both result in heads. This confirms our rule: the probability both A and B are true is the probability of A times the probability of B, or point-5 times point-5 = point-25. We could use the same simulation approach if the coins weren't fair: that is, if A and B didn't each have a 50% probability. For example, we could set event A to have a 10% probability, by setting the third argument of rbinom to point-1, and set event B to have a probability of 70%, or point-7. When we again combine A and B, we see that about 7% of the pairs are both true. This once again matches what we'd expect: the probability of A and B is point-1 times point-7. You'll be able to apply and simulate this rule in the exercises, and learn more about other rules for combining probabilities throughout this chapter.

6. Let's practice!