Python Tutorial: Simulation basics

Learn the five-step framework for building simulations: define outcomes, assign probabilities, specify relationships, sample from distributions, and analyze results. A dice game example shows how to model a win condition when both dice match.
Chapters
Now let's learn about simulations. Simulation is a framework that allows us to model real world systems and processes. It's a very popular tool that has been applied in multiple domains. Simulations are typically characterized by repeated random sampling, which means that we use the power of random variables to generate multiple outcomes. Think of simulations as tossing a coin again and again to record the outcomes. Simulations typically give us an approximate solution. After recording the result of the coin tosses. You may find that after 100 tosses, you actually observe only 48 heads, as opposed to 50. This is still a good enough approximation. Finally, as we'll see later in the course, using very simple modeling techniques, we can use simulations to solve pretty complex problems. Simulations perform particularly well in some areas where traditional methods don't give us a clean solution. Simulations typically involve the following. 1. Define the set of outcomes associated with a random variable. 2. Assign a probability to each of these outcomes, the probability distribution. 3. Define the relationship between multiple random variables. These three steps essentially describe our statistical model. 4. Draw samples from the probability distributions. 5. Analyze the sample outcomes. This might seem daunting at first, but we'll work through an example in the exercises to make it more clear. In the following exercises, you will be running your first simulation, a simple dice game. The dice game involves throwing two dice and winning if they show the same number. Thus, seeing 1 and 4 is a loss, while 3 and 3 is a win. Let's see what we need for this simulation. For step one and two, we first define the outcomes of the die and assign a probability to each outcome. Since both die A and B are fair dice, we can use identical probability distributions. Also, since the probability of each outcome is the same, this is a uniform distribution. In step three, we define the relationship between each of the dice. If they show the same number, we win. Otherwise, we win, we lose. Steps 1, 2 and 3 give us the statistical model underlying the simulation. For any complex simulation, this is essentially the foundation describing the model. Finally, in step four, we generate multiple outcomes through repeated random sampling. Keep in mind that there is an additional step five where we'll need to analyze the outcomes. But for now, let's focus on the first four steps and make sure we are comfortable with them. You are now ready to run your first simulation. Let's work through it in the exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







