An introduction to Monte Carlo simulations using Python
Learn how Monte Carlo simulations are used to predict probabilities and what they are
Monte Carlo simulations: what are they?
Due to the involvement of random variables, a Monte Carlo simulation estimates the probability that an undeterminable event may occur. In order to determine the probability, the algorithm uses repeated random sampling. Using random inputs, you simulate an event many times to get your estimate. In the example, we will see that other factors can be determined as well. A wide range of fields can benefit from Monte Carlo simulations, including economics, gambling, engineering, and energy. It’s a great thing to know, regardless of your career field.
You should start by building a basic model to understand the fundamentals of Monte Carlo simulations. In this article, we will use a dice game as the easiest and most common way to accomplish this. We will show in this example what the player’s potential earnings may look like if the house has an advantage over the player (typically a casino).
The Dice Game
Playing the diceme will involve two six-sided dice. For the player to win, both dice must have the same number. There are six possible outcomes for a six-sided die (1, 2, 3, 4, 5, and 6). There are 36 possible outcomes with two dice (1 and 1, 1 and 2, 1 and 3, etc., or 6 x 6 = 36). It is quite evident that the house has the advantage in this game (30 outcomes to the player’s 6 outcomes).
With $1,000 in their pocket, our player decides to play 1,000 rolls, betting $1 after every roll (the dice are rolled twice) and is prepared to lose it all. When a player wins, the house offers to pay four times his bet. The player’s balance increases by $4 if they win the first roll, and they end the round with $1,004. There is a possibility that they could walk away with $5,000 if they go on a 1,000-roll win-streak. They could go home empty handed if they lost every round. A good risk-reward ratio… or maybe not.
Importing Python Packages
We’ll simulate our game to see if the player made the right decision. As a first step, we import two Python packages: Pyplot from Matplotlib and random. Our results will be visualized using Pyplot and simulated using random to represent a normal six-sided die roll.
Dice Roll Function
Next, we can use a function to simulate a roll of both dice by randomizing an integer from 1 to 6. In addition to comparing the two dice, the function will also check if they are the same. Upon return, same_num will contain a Boolean value representing whether the rolls are the same or not. It will be used later to determine how to proceed with our code.
Inputs and Tracking Variables
It is essential that you know what your inputs are prior to running a Monte Carlo simulation. In our description of the game, we already defined our inputs. The player will bet $1 on each roll, and we said there would be 1,000 rolls per game. Our input variables, along with our simulation frequency, need to be defined. Our Monte Carlo simulation count can be found in the num_simulations variable. A higher number means a more accurate prediction of probability.
As the complexity of a project increases, the number of variables we can track also increases, so it’s important to determine what you want to track. Each simulation (or game) will be given a win probability (wins per game / total number of rolls) and an ending balance. Every game will update these lists, which are initialized as lists.
Setting up Figure
Putting our figure together before running the simulation is the next step. We can add lines to our figure after each game by doing this prior to the simulation. We can display the plot once we have run all of the simulations.
Monte Carlo Simulation
We have a nested while loop that runs each game (1,000 rolls) and an outer for loop that iterates through 10000 simulations. Each while loop starts with a $1,000 balance (to plot) and a count of wins and rolls.
For 1,000 rolls, we will simulate the game in a while loop. Using the Boolean variable returned by roll_dice(), we determine the outcome of the dice roll. A win is added to the win count if both dice have the same number. Our balance list is reduced if the dice are different. num_rolls is updated after each roll.
A player’s win probability can be calculated by dividing the number of wins by the total number of rolls once the number of rolls reaches 1,000. The tracking variable end_balance can also store the ending balance for a completed game. To complete the figure we defined earlier, we can plot the num_rolls and balance variables.