Probability and Bayes' Theorem
1. Probability and Bayes' Theorem
Welcome back! Let's now talk about probability!2. Probability theory
Whether we are talking about the frequentist or Bayesian interpretation, probability is always a statement about uncertainty. It is expressed as a number between 0 and 1, where a zero indicates something impossible, a 1 indicates something certain, and a probability of 0-point-5 indicates something is equally likely to happen or not happen. A probability that some event will occur is typically denoted with the capital "P" followed by the event in question in parentheses. This notation means there is a 75% chance of rain tomorrow.3. Probability rules
When making calculations involving probabilities, there are two rules. The first is the sum rule, which tells us how to calculate the probability that at least one of multiple independent events happens: the probability that either A or B occurs. The "or" operator translates to addition. To get the probability of rolling a 2 or a 4 with a die, we need to add the two respective probabilities. The other rule is the product rule, telling us how to calculate the probability of multiple independent events happening together: the probability that both A and B will occur. The "and" operator translates to multiplication. To get the probability of rolling a 2, and then a 4 in the next roll, we need to multiply the two respective probabilities.4. Conditional probability
Another important concept is the conditional probability, that is: the probability of some event occurring, given that some other event has occurred. We denote it with a vertical bar and read as the probability of A given B. Imagine a box with two orange balls and one blue ball. You draw one at random. The probability that it's orange is 2/3, and that it's blue - 1/3. These are unconditional probabilities. Now, you put the ball you've drawn away from the box and you draw another one from the remaining two. The probability that this second ball is blue given that the first one was orange is 50%, since with one orange ball gone, there is one orange and one blue left.5. Bayes' Theorem
Now we are ready to face the famous Bayes' Theorem, which is a way to calculate conditional probability when we know some other probabilities. Using the formula, we can calculate the probability of A given B, if we know the probability of B given A and the unconditional probabilities of A and B.6. Bayes' Theorem
To make it more specific, let's replace A with whether a car had an accident on a steep road, and B with whether the road was slippery. We would like to know what's the chance of an accident if it's slippery, which we will answer using data on many cars attempting to pass this road. There are two Boolean variables in the DataFrame: whether there was an accident, and whether it was slippery.7. Bayes' Theorem in practice
Let's keep the formula on the slide and do the calculations. First, the unconditional probability of an accident is the percentage of all attempts ending up with an accident, which we can calculate as the mean of the Boolean variable. In a similar fashion, we get the probability of the road being slippery. To get the probability of the road being slippery given there is an accident, we filter the DataFrame with its loc method to keep only rows indicating an accident, and then we compute the percentage of cases where the road was slippery. Finally, we put the three numbers together according to Bayes' formula to find out that 1 in 2 cars has an accident if the road is slippery. A very dangerous mountain pass!8. Let's practice!
Let's practice using Bayes' Theorem!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.