[Python] Probability with Rolling Dice

Edmund Lee
5 min readOct 17, 2023

--

Statistics is often seen as a complex and intimidating field, full of daunting equations and abstract concepts. But fear not, because today we are diving into the basics together. Ever played before the board game — Ludo or the Aeroplane game and wondered how likely it is to roll that perfect 12 with a pair of dice? We’re going to perform the simulation using Python and understand the concept behind it.

Photo by VD Photography on Unsplash

Probability

Before we start into rolling dice, let’s understand probability. Probability is one of the core concepts in statistics, and it’s all about making sense of uncertainty and randomness. Think about the marks of the students, the height of the students, the occurrence of car accidents in a year, the daily return of stock index, and more. All of these are related to probability and are essential in the process of data analysis.

Rolling the dice

Let’s kick things off by simulating the rolling of two dice 50 times. The following Python code shows us the sum of two dice for each outcome.

At first glance, the output seems like random numbers are being generated. However, there is a probability behind the randomness of these results.

Frequency and Relative Frequency

To better understand the results of our dice rolls, we need to organize and analyze them. Let’s count the frequency of each possible outcome over the 50 samples. Counting the frequency of each outcome allows us to observe which sums are more likely and which are less likely when rolling two dice.

We can take our analysis a further step by considering relative frequency. Relative frequency represents the proportion of each outcome relative to the total number of samples. These frequency and relative frequency plots provide us the insights into the distribution of results and help us see patterns in our data.

From the plot, it shows that the probability or the chance for obtaining number 8 is the highest out of the 50 rolls, whereas number 12 is the lowest among all.

Probability distribution

This brings us to the concept of probability distribution. A probability distribution represents the probabilities of different possible outcomes for an experiment. In simpler terms, it tells us how likely each outcome is.

In our case, the probability distribution reveals the chances of rolling each possible sum with two dice. To understand this concept better, let’s take a closer look at a theoretical distribution for rolling two dice, assuming all faces equal probabilities.

Theoretical Probability Distribution for rolling dice

First, we list down the possible outcomes of sum of two dice which is from 2 up to 12. Next, we can calculate the probabilities associated with each of these sums.. For instance, there is only one way to roll a sum of 2 (rolling a 1 on both dice), while there are six ways to roll a sum of 7 (rolling a 1 and a 6, 2 and 5, 3 and 4, and so on).

To create a probability distribution, we normalize these probabilities by dividing them by the total number of outcomes, which is 36 for two dice rolls.

As a result, we observe that the theoretical probability of rolling a 7 is the highest among all possible sums whereas rolling a 2 and a 12 have the lowest chance.

Simulating different trials

We can make the rolling dice simulation into a function

and simulate different trials with varying sample sizes: 100, 500, 1000, 2000, 3000, and 5000.

As we increase the number of trails, we’ll observe how the probability distribution evolves. Keep an eye on the plot as it reveals a fascinating pattern — a pattern related to the Central Limit Theorem concept.

Central Limit Theorem

The Central limit theorem is a statistical theory that states that the distribution of a sample variable approximates a normal distribution (i.e., a “bell curve”) as the sample size becomes larger, assuming that all samples are identical in size, and regardless of the population’s actual distribution shape. ¹

In simple, it tells us that as we collect more data, the distribution of sample means tends to follow a normal distribution. As you observe from the simulation, the patterns emerge and become more predictable as we increase our sample size (trial). When you compare the theoretical probability distribution with a large sample size, you can see that both seemingly similar in pattern.

Example: S&P500 Daily Return

We can apply the same concept to S&P500 stock index and calculate the daily return from 2010 to 2023.

By plotting the histogram and the normal distribution curve of the dataset, we can visually compare how well the observed data fits a normal distribution.

The plotted graph resonate with the statement by Fama and French

“Distributions of daily and monthly stock returns are rather symmetric about their means, but the tails are fatter (i.e. there are more outliers) than would be expected with normal distributions.” ²

In simple, although the mean of the stock return is proportionally distribution over the means, albeit there’s extreme return for both positive and negative and investor shall expect it.

Summary

We’ve explored the basic of probability distribution and Central limit theorem using the example of rolling dice. Continue to explore the different areas in statistics and the application in it. Happy learning!

[1] Ganti, A. (2023). Central Limit Theorem (CLT): Definition and Key Characteristics. Investopedia. https://www.investopedia.com/terms/c/central_limit_theorem.asp

[2]: Fama, E. F. (1965). The Behavior of Stock-Market Prices. The Journal of Business, 38(1), 34–105. http://www.jstor.org/stable/2350752

--

--