Probability Basics for Data Science
You pull a coin out of your pocket. On a single flip, what is the probability it’ll land on heads? What about tails? You’ll have a 50 percent chance of either occurrence, right?
What is the probability of getting 100 heads and 100 tails on 200 flips of a fair coin? Before you answer, let’s dive in on the basics of probability.
Probability is not a mere computation of odds on the dice or more complicated variants; it is the acceptance of the lack of certainty in our knowledge and the development of methods for dealing with our ignorance. — Nassim Taleb
The interdisciplinary practice of data science places many different concepts front and center depending on the project, career path… or day of the week. However, probability theory is one of the few that’s finds itself intertwined at the core of data science — among others.
Your journey as a data scientist will bring you up close and personal with the process of using probability theory to consider discrete and continuous events and variables. Let’s cover some basics, then move into some more common concepts you’ll encounter on a regular basis.
Basics of Probability
One of the most difficult things for people interested in data science is all the math involved. And though it is true there is a lot of math, most of the applications are more intuitive that people realize. Something like probability theory might seem daunting, or at the very least something you haven’t looked at in a long time. However, did you check the Weather app on your phone this morning to see if it was going to rain? Is there a cop posted up around the next turn trying to catch you speeding? Is that customer likely to churn, should we send her a marketing piece? These questions and countless more can all looked at through the lens of probability.
Life is full of uncertainty, you won’t know the outcome of a particular situation until it happens. But we’ve all avoided planning that picnic or hike by using probability theory.
Before we get too far down the rabbit hole, we’ll need to cover a few key vocabulary words that’ll come up again:
Probability — The measure of the likelihood of an event
Event — A collection of outcomes from an experiment
Outcome — An outcome of a random experiment is any one of the possible results of the experiment
Experiment — A situation, or experiment, for which the set of possible outcomes are known or unknown, but which particular outcome will occur cannot be said prior to performing the experiment
For example, the football game is the experiment. The outcome is Team A wins. The events would be win, lose, or draw. The probability that Team A would win based on a number of metrics was 68%.
Though it isn’t possible to know what will happen in an uncertain world, it is clearly helpful to understand the chances of what might occur. Should you bring an umbrella with you? Should you take your foot off the gas? What is the likelihood this customer is going to churn?
Probability Occurence Calculation
When you want to calculate the likelihood of a particular event, you’ll need to find a way to express the outcomes in numbers.
For a coin toss we’d say the possible outcomes are:
- 1 if tails
- 0 if heads
A slightly more complicated example would be a simple game involving two dice. If I roll the two dice and get a 7, I win, any other outcome and you win. We’ll define the sum of the two rolled die as X. The possible outcomes are:
X = 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
A few key differences about this second example are we see that sometimes things are not equally likely. You can only roll a 12 with [(6, 6)]. Whereas an 8 can be rolled with a [(2, 6), (3, 5), (4, 4)]. Rolling an 8 in this scenario is more likely than rolling a 12. Also, in this example we’re only going to get whole numbers from 1 to 6 per die. We’re not gonna get a 9.25 outcome. However, if I’m running an experiment to guess your weight it would be completely reasonable to see 133.3 pounds. Or 133.12 pounds. Or even 133.1945 pounds. Any of those weight measurements is possible, as is any infinite value between them, these variables are considered continuous variables.
Principle Counting
While your husband waits in line with the kids grabbing a funnel cake, you saunter up to a booth to play a game of chance. The guy rocking a less than stylish fanny-pack starts explaining the game. It’s basically a roulette wheel with the colors teal and orange painted evenly around the outside of the wheel.
He tells you when he drops the ball, after it bounces around, if it lands on teal you win, orange you lose. The outcome of this game is clear, he spins, drops the ball, you win or lose. And, as best as you can tell the colors are evenly painted, so you have a 50% chance either way.
However, what if the teal paint only covered 1/5th of the available area? Your chances decrease to only 20%.
Your family still isn’t back yet, so you look at the next booth over. It’s a rendition of the previously mentioned dice throwing game. You get two dice, and if you roll a 7 you win. Just like before our outcomes are defined as:
X = 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Let’s calculate the probability of each outcome.
Each die has 6 possible outcomes which translates to 36 possible outcomes in total.
Get Chase Thompson’s stories in your inbox
Join Medium for free to get updates from this writer.
The possible outcomes spelled out:
2 — [(1,1)] = 1 / 36
3 — [(1, 2), (2, 1)] = 2 / 36
4 — [(2, 2), (3, 1), (1, 3)] = 3 /36
5 — [(4, 1), (1, 4), (2, 3), (3, 2)] = 4 / 36
6 — [( 1, 5), (5, 1), (2, 4), (4, 2), (3, 3)] = 5 / 36
7 — [(1, 6), (6, 1), (3, 4), (4, 3), (2, 5), (5, 2)] = 6 / 36
8 — [(2, 6), (6, 2), (3, 5), (5, 3), (4, 4)] = 5 / 36
9 — [(4, 5), (5, 4), (3, 6), (6, 3)] = 4 / 36
10 — [(4, 6), (6, 4), (5, 5) = 3 / 36
11 — [(6, 5), (5, 6)] = 2 / 36
12 — [(6, 6)] = 1 /36
So, the chance of winning this carnival game is 6/36 or 1 in 6, while the fanny-pack clad gentleman is sitting at 30/36. His offered reward for the game isn’t sitting at 6x your money, so you decide not to play.
Another way to write this out is:
- p(Success) = 6/36
- p(Failure) = 30/36
This makes the total probability 1, with only 2 outcomes. Unlike the weight guessing game we mentioned before, the fixed, integer based result here makes it a discrete outcome.
Let’s visualize the distribution of the possible outcomes:
plt.figure(figsize=(16, 9))plt.bar(df.roll, df.probability)plt.title("Carnival Dice Game", fontsize=20)
plt.ylabel("# of chances for that roll", fontsize=14)
plt.xlabel("Sum of two dice rolls", fontsize=14)
plt.xticks([2,3,4,5,6,7,8,9,10,11,12])plt.show()
The Gambler’s Fallacy
During the next article diving into Data Science and Probability Theory we’ll explore Binomial, Uniform, Normal, Poisson, and Exponential Distributions — we will also review Central Limit Theorem and Z-Scores.
Before we head out though, let's travel back 1,400 words or so, to the question at the beginning of this article.
What is the probability of getting 100 heads and 100 tails on 200 flips of a fair coin?
This question broaches the topic of the gambler’s fallacy. This is the belief that the chances of something happening with a fixed probability become higher or lower as the process is repeated. Often times, people who commit the gambler’s fallacy believe that past events affect the probability of something happening in the future.
If we look at our 50% chance to hit heads or tails on one flip, we might suspect that that out of 200 flips, 100 heads and 100 tails wouldn’t be too far off. At the very least we’d suspect it doesn’t depart too far off from 50%, maybe 45%… at least 30%. The approximate percentage of getting that outcome is 5.63%.
Not what you expected?
We’ll review the calculation for this outcome when we review binomial distributions, along with a ton more, in the next article.
Connect with me on LinkedIn: https://www.linkedin.com/in/wchasethompson







