Sitemap
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Probability concepts explained: probability distributions (introduction part 3)

14 min readSep 10, 2018

--

In my first and second introductory posts I covered notation, fundamental laws of probability and axioms. These are the things that get mathematicians excited. However, probability theory is often useful in practice when we use probability distributions.

Probability distributions are used in many fields but rarely do we explain what they are. Often it is assumed that the reader already knows (I assume this more than I should). So I’m going to try to explain what they are in this post.

What is a probability distribution?

Recall that a random variable is a variable whose value is the outcome of a random event (see the first introductory post for a refresher if this doesn’t make any sense to you). For example, a random variable could be the outcome of the roll of a die or the flip of a coin.

A probability distribution is a list of all of the possible outcomes of a random variable along with their corresponding probability values.

To give a concrete example, here is the probability distribution of a fair 6-sided die.

Press enter or click to view image in full size
The probability distribution for a fair six-sided die

To be explicit, this is an example of a discrete univariate probability distribution with finite support. That’s a bit of a mouthful, so let’s try to break that statement down and understand it.

Discrete = This means that if I pick any two consecutive outcomes. I can’t get an outcome that’s in between. For example, if we consider 1 and 2 as outcomes of rolling a six-sided die, then I can’t have an outcome in between that (e.g. I can have an outcome of 1.5). In mathematics, we would say that the list of outcomes is countable (but let’s not go down the path of defining and understanding countable and uncountable sets. It gets weird). You can probably guess when we get to continuous probability distributions this is no longer the case.

Univariate = means that we only have one (random) variable. In this case, we only have the outcome of the die roll. In contrast, if we have more than one variable then we say that we have a multivariate distribution. In the specific case where we have 2 variables, we often say that it’s a bivariate distribution.

finite support = This means that there is a limited number of outcomes. The support is essentially the outcomes for which the probability distribution is defined. So the support in our example is. 1, 2, 3, 4, 5 and 6. And since this is not an infinite number of values, it means that the support is finite.

Introduction to functions

Why are we talking about functions?

In the above example of rolling a six-sided die, there were only six possible outcomes so we could write down the entire probability distribution in a table. In many scenarios, the number of outcomes can be much larger and hence a table would be tedious to write down. Worse still, the number of possible outcomes could be infinite, in which case, good luck writing a table for that.

To get around the problem of writing a table for every distribution, we can define a function instead. The function allows us to define a probability distribution succinctly.

So let’s first define what a function is generally and then we’ll move onto functions used for probability distributions.

What is a function?

On a very abstract level, a function is a box that takes an input and returns an output. For the vast majority of cases, the function actually has to do something with the input for the output to be useful.

Let’s define our own function. Let’s say that this function takes in a number as input, adds 2 to the input number, and returns the new number as output. Graphically our function (as a box) looks like this:

Press enter or click to view image in full size
The abstract depiction of a function as a box that takes input and returns an output. In this case, the function adds 2 to the input.

So if our input was 5, our function would add 2 to it and return the output 5+2 = 7.

Notation for functions

Now it would be tedious to draw the digram above for every function that we want to create. Instead, we use symbols/letters to represent the diagram to make it more concise. Rather than write the word “input” we use “x”, rather than write the word “function” we write “f” and rather than write the word “output” we write “f(x)”. So the above diagram can now be written as

Press enter or click to view image in full size
Our function is written with symbols instead of words to make this more concise

This is better, however, we still have the problem that we have to draw a diagram to understand what the function is doing. We’re mathematicians though, we don’t want to waste precious energy by drawing a box, so we’ve come up with a better way of writing functions that means we don’t have to draw anything. We can define our function mathematically as

Our function is now written without having to draw arrows and boxes.

This is equivalent to the diagram above because we can see explicitly the input to the function f is x, we’ve called our function f and we know that the function adds 2 to the input and returns x + 2 as the output.

It’s important to note that the choice of letter for the function and the input was arbitrary. I could say that “a” is the input and I can call the function “add_two” so my function would be

Another way of writing the same function

and this is completely equivalent to the function above.

One of the main takeaways from this is that with a function we can see how we would transform any input. With the function f(x) = x+ 2, We’d know what to do if the input was x=10 or if the input was x=10000. So we don’t need to write down a table like we did earlier in the post.

The final point I want to make here is that the functions that we’re going to use are solely going to work with numbers as both input and output. However, functions can take anything you like as an input and output anything you like (even output nothing). For example, we could write a function in a programming language that takes a string of text as input and outputs the first letter of that string. Here is an example of this function in the Python programming language

Representing functions graphically

Given that one of the main benefits of functions is to allow us to know how to transform any input, we can also use this knowledge to visualise the function explicitly. Let’s stick with our example f(x) = x+2. Graphically it looks like this:

Press enter or click to view image in full size
Graphical representation of our function f(x) = x + 2

We can read along the horizontal axis on the bottom as our input numbers and the corresponding numbers on the vertical axis on the left are the output values f(x) = x+2. For example, we can see that the blue line that represents the function crosses the point where the vertical (white) line at x=1 intersects the horizontal (white) line f(x) = 3. This graphically shows that f(1) = 1 + 2 = 3.

Parameters of functions

One of the most important features of functions are parameters. Parameters are the numbers that you find inside the functions that you don’t necessarily feed in as input. In our example, f(x) = x + 2, the number “2” is a parameter because we need it to define the function but we don’t include it as input to the function.

The reason that parameters are important is that they play a direct role in determining the output. For example, let’s define another function h(x) = x+3. The only difference between the function f(x) = x+2 and our new function h(x) = x+3 is the value of the parameter (we now have a “3” instead of a “2”). This difference means that the outputs we get are completely different for the same input. Let’s look at this graphically.

Press enter or click to view image in full size
The difference between our functions f(x) = x + 2 and h(x) = x + 3

Parameters are arguably the most important feature of a probability (distribution) function because they define the output of the function which tells us the likelihood of certain outcomes in a random process. It’s often parameters that we’re trying to estimate in problems that come up in data science and I’ve previously written about 2 methods that we can use to estimate them: maximum likelihood estimation and Bayesian inference.

Now we’re ready to talk about probability distributions using the language of functions.

Probability mass functions: Discrete probability distributions

When we use a probability function to describe a discrete probability distribution we call it a probability mass function (commonly abbreviated as pmf).

Remember from the first introductory post on probability concepts that the probability of a random variable, which we denote with a capital letter, X, taking on a value, denoted with a lowercase letter, x, is written as P(X=x). So if we use the dice roll as our example random variable, we can write the probability of the die landing on the number 3 as P(X=3) = 1/6.

A probability mass function, which we’ll call “f” returns the probability of an outcome. Therefore, a probability mass function is written as:

I know this is getting a little horrible and mathematical but bear with me. The equation that we see above says that the probability mass function “f” just returns the probability of the outcome x.

So let’s go back the example of the fair 6-sided die (you’re probably already sick of this example). The probability mass function, f, just returns the probability of the outcome. Therefore the probability of rolling a 3 is f(3) = 1/6. That’s it.

Since a probability mass function returns probabilities it must obey the rules of probability (the axioms) that I described in my previous post. Namely, the probability mass function outputs values between 0 and 1 inclusive and the sum of the probability mass function (pmf) over all outcomes is equal to 1. Mathematically we can write these two conditions as

So we’ve seen that we can write a discrete probability distribution as a table and as a function. We can also represent the die roll example graphically

Press enter or click to view image in full size
Graphical representation of the probability distribution for outcomes of rolling a fair six-sided die

Example discrete probability distribution: The Bernoulli distribution

Some probability distributions crop up so often that they have been extensively studied and have names. One discrete distribution that crops up a lot is called the Bernoulli distribution. It describes the probability distribution of a process that has two possible outcomes. An example of this is a coin toss where the outcome is heads or tails.

Get Jonny Brooks-Bartlett’s stories in your inbox

Join Medium for free to get updates from this writer.

The probability mass function of a Bernoulli distribution is

Here, x represents the outcome and takes the value 1 or 0. So we could say that heads = 1 and tails = 0. p is a parameter that represents the probability of the outcome being 1. So in the case of a fair coin where the probability of landed heads or tails is 0.5 we would set p = 0.5.

Often we want to be explicit about the parameters that are included in the probability mass function so we write

Notice that we use the semicolon to separate the input variables from the parameters.

Probability density functions: Continuous probability distributions

Sometimes we are concerned with the probabilities of random variables that have continuous outcomes. Examples include the height of an adult picked at random from a population or the amount of time that a taxi driver has to wait before their next job. For these examples, the random variable is better described by a continuous probability distribution.

When we use a probability function to describe a continuous probability distribution we call it a probability density function (commonly abbreviated as pdf).

Probability density functions are slightly more complicated conceptually than probability mass functions but don’t worry, we’ll get there. I think it’ll be easiest to start with an example of a continuous probability distribution and then discuss the properties from there.

Example continuous probability distribution: The Normal distribution

The normal distribution is probably the most common distribution in all of probability and statistics. One of the main reasons it crops up so much is due to the Central Limit Theorem. We’re not going to go into it in this post but here is a nice article by Carson Forter called “The Only Theorem Data Scientists Need To Know” that explains what the theorem is and how it relates to the normal distribution.

The probability density function for the normal distribution is defined as

Where the parameters (i.e. the symbols after the semicolon) represent the mean, μ, (the point where the centre of the distribution is) and the standard deviation, σ, (how spread out the distribution is) of the population.

If we set the mean to be equal to zero (μ=0) and the standard deviation equal to 1 (σ=1) then the distribution we get looks like this

Press enter or click to view image in full size
Normal distribution with mean = 0 and standard deviation equal to 1

The normal distribution is an example of a continuous univariate probability distribution with infinite support. By infinite support, I mean that we can calculate values of the probability density function for all outcomes between minus infinity and positive infinity. In mathematics, we sometimes say that it’s supported on the whole real line.

Properties of a continuous probability distribution

The first thing to notice is that the numbers on the vertical axis start at zero and go up. This is a rule that a probability density function has to obey. Any output value from a probability density function is greater than or equal to zero. In mathematical lingo we would say that the output is non-negative or write this mathematically as

However, unlike probability mass functions, the output of a probability density function is not a probability value. This is an incredibly important distinction, one of which I’ve been guilty of forgetting.

To get the probability from a probability density function we need to find the area under the curve. So from our example distribution with mean = 3 and standard deviation = 1, we can find the probability that the outcome is between 0 and 1 by finding the area shown in the image below

Press enter or click to view image in full size
The area shaded is the probability of the outcome being between 0 and 1.

Mathematically we would write this as

We can read this as “the integral of the probability density function between 0 and 1 (on the left-hand side) is equal to the probability that the outcome of the random variable is between zero and 1 (on the right-hand side)”.

Forgive me as I haven’t explicitly covered integrals and how they work (I have a brief conceptual introduction to integrals in my post on marginalisation but it doesn’t teach you how to compute them). If you don’t know about them then all you need to know for the moment is that it’s a mathematical method for finding the area under a curve, which in this case gives us the probabilities of outcomes. Perhaps I need to write a brief series covering introductory calculus.

We’ve now seen another property of probability density functions. Namely that the probability between two outcomes, let’s say ‘a’ and ‘b’, is the integral of the probability density function between those two points (this is equivalent to finding the area under the curve produced by the probability density function between the points ‘a’ and ‘b’). Mathematically this is

Remember that we still have to follow the rules of probability distributions, namely the rule that says that the sum of all possible outcomes is equal to 1. We can cover all possible values if we set our range from ‘minus infinity’ all the way to ‘positive infinity’. Therefore the following has to be true for the function to be a probability density function

This says that the area under the curve between minus infinity and positive infinity is equal to 1.

An important thing to know about continuous probability distributions (and something that may be really weird to come to terms with conceptually) is that the probability of the random variable being equal to a specific outcome is 0. For example, if we try to get the probability that the outcome is equal to the number 2 we would get

This may seem weird conceptually but if you understand calculus then it should make a little more sense. I’m not going to cover calculus in this post. Instead, what I want you to take away from this fact is that we can only talk about probabilities occurring between two values. Or we can ask about the probability of an outcome being greater than or less than a specific value. We can’t ask about the probability of an outcome being equal to a specific value.

The eagle-eyed readers may have noticed that I’ve used “less than (<)” and “greater than (>)” symbols rather than “less than or equal to (≤)” and “greater than or equal to (≥)” symbols. For continuous probability distributions, it doesn’t actually matter because they’re the same. Explicitly I mean

So the probability of the random variable taking on a value between a and b exclusive is the same as the probability of it taking on a value between a and b inclusive.

The importance of parameters

We saw that parameter values can change the output values of a function and it’s no different with probability distributions.

Press enter or click to view image in full size
Two normal distributions with different parameters give completely different probability outcomes.

In the figure above we’ve plotted the probability density functions of two normal distributions. The blue distribution has parameter values μ=0 and σ=1 whereas the red distribution has parameter values μ=2 and σ=0.5.

It’s clearer to see now why using the wrong parameter values can give results that differ wildly from what you might expect.

Summary

Wow! That was much longer than I intended. Let’s summarise the main points:

  • A probability distribution is a list of outcomes and their associated probabilities.
  • We can write small distributions with tables but it’s easier to summarise large distributions with functions.
  • A function that represents a discrete probability distribution is called a probability mass function.
  • A function that represents a continuous probability distribution is called a probability density function.
  • Functions that represent probability distributions still have to obey the rules of probability
  • The output of a probability mass function is a probability whereas the area under the curve produced by a probability density function represents a probability.
  • Parameters of a probability function play a central role in defining the probabilities of the outcomes of a random variable.

I initially wanted to cover multivariate distributions in this post but it got too long so we’ll cover it in a later post.

Now that you have the basic understanding of what a probability distribution is, check out this great article by Sean Owen which covers the common probability distributions used in data science. He does a fantastic job explaining why each distribution arises in data science, so it’s a more practical look at probability distributions. For a more extensive list of probability distributions check out this Wikipedia page (the list is quite long).

As always, thanks for reading this far. I hope this has been useful and you’ve learnt something from it. Please feel free to leave comments, suggestions and questions.

--

Jonny Brooks-Bartlett
Jonny Brooks-Bartlett

Written by Jonny Brooks-Bartlett

Data scientist at Deliveroo, public speaker, science communicator, mathematician and sports enthusiast.

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.